diff --git a/bunny.off b/bunny.off new file mode 100755 index 0000000..9d37765 --- /dev/null +++ b/bunny.off @@ -0,0 +1,1498 @@ +OFF +500 996 0 +0.179985 -0.01961091 0.2849799 +0.236332 0.01865709 0.2517147 +0.253917 -0.04694492 0.2869547 +0.1888352 -0.07435793 0.3224917 +0.1037022 -0.04792893 0.3037639 +0.116299 0.01081288 0.2791579 +0.165022 0.04720992 0.2542149 +0.210867 0.08355707 0.2167207 +0.280849 0.04960108 0.2060247 +0.302948 -0.02278793 0.2409157 +0.319245 -0.1005569 0.2683507 +0.2524562 -0.1113259 0.3141857 +0.1753381 -0.1375179 0.3578389 +0.1250392 -0.08993793 0.3506419 +0.05518919 -0.09314591 0.3582939 +0.02967018 -0.05466592 0.3043139 +0.04883897 0.008376896 0.2798819 +0.08533198 0.06648189 0.2511509 +0.135884 0.1027589 0.2155979 +0.177576 0.1274179 0.1640229 +0.249078 0.09928107 0.1592757 +0.301638 0.06632811 0.1330877 +0.333856 0.008691072 0.1800377 +0.355389 -0.06046593 0.2036167 +0.371608 -0.1297219 0.2088297 +0.34169 -0.1768739 0.2555807 +0.2894112 -0.1727169 0.3020217 +0.2263702 -0.1744989 0.3414157 +0.1401332 -0.2011279 0.3730329 +0.0964272 -0.1482679 0.3766699 +-0.01281899 -0.1095219 0.3514529 +0.02363116 -0.1638499 0.3777819 +-0.04116499 -0.06964591 0.2979159 +-0.02073699 -0.004404932 0.2734619 +0.00779599 0.05757892 0.2427429 +0.05214399 0.1046529 0.2059969 +0.101555 0.1340899 0.1584589 +0.215315 0.1257731 0.1049429 +0.144526 0.1437199 0.09848189 +0.266819 0.09713209 0.07214189 +0.343169 0.02086711 0.1104719 +0.317153 0.05236608 0.05173889 +0.37251 -0.03574592 0.1298907 +0.392424 -0.09978691 0.1416997 +0.402123 -0.1694129 0.1410887 +0.380621 -0.2049029 0.1982507 +0.305664 -0.2447179 0.2799887 +0.354818 -0.2582678 0.2355587 +0.2514072 -0.2386919 0.3237197 +0.1900972 -0.2347639 0.3578537 +0.07556617 -0.2212999 0.3820749 +0.1281852 -0.2832069 0.3703599 +-0.04205492 -0.1784549 0.3545119 +-0.07146499 -0.1306319 0.3095579 +0.004923165 -0.2367369 0.3637899 +-0.114744 -0.07534993 0.2871429 +-0.089735 -0.01587793 0.2641099 +-0.064071 0.04244089 0.2279669 +-0.02765599 0.08876592 0.1856689 +0.02952898 0.1258319 0.1430229 +0.075396 0.1398649 0.08453289 +0.189034 0.1322741 0.0421629 +0.121067 0.1369609 0.02368289 +0.233179 0.1038131 0.006642908 +0.281571 0.06530911 -0.008044094 +0.361935 -0.00788191 0.05071989 +0.328895 0.01374608 -0.0148721 +0.392588 -0.07253093 0.06614789 +0.40591 -0.1433799 0.07393789 +0.415077 -0.2377429 0.1442007 +0.418555 -0.2189309 0.0787439 +0.398354 -0.2801228 0.1885937 +0.2586812 -0.3077649 0.3058447 +0.31703 -0.3178039 0.2628837 +0.36603 -0.3317278 0.2153927 +0.1958012 -0.3020729 0.3432817 +0.0561772 -0.2858949 0.3676409 +0.1432212 -0.3571599 0.3498999 +0.07477719 -0.3485459 0.3549989 +-0.04206091 -0.2493619 0.3204039 +-0.08314291 -0.2019619 0.2890629 +-0.131107 -0.1443099 0.2871999 +0.002467155 -0.3182879 0.3396859 +-0.186047 -0.091674 0.2863829 +-0.167215 -0.026169 0.2664289 +-0.139821 0.0279749 0.2314669 +-0.113354 0.0753299 0.1874879 +-0.09759 0.1019109 0.1326969 +-0.03489301 0.1104959 0.1219849 +0.006329 0.1255629 0.06970289 +0.05095899 0.1255889 0.01178589 +0.167285 0.1111449 -0.02823809 +0.09609002 0.1047709 -0.0431281 +0.217858 0.0709821 -0.05920309 +0.270991 0.02363408 -0.06730509 +0.369694 -0.04869092 -0.00635311 +0.321974 -0.03226092 -0.0686371 +0.393993 -0.1159609 0.002415895 +0.400587 -0.1871109 0.0132249 +0.47183 -0.2438478 0.1127279 +0.466997 -0.2812818 0.1684987 +0.411089 -0.2541348 0.01689991 +0.474872 -0.2581698 0.04936689 +0.43157 -0.3410378 0.2006467 +0.2105642 -0.3673398 0.3154547 +0.269344 -0.3738818 0.2725667 +0.325503 -0.3847038 0.2228527 +0.388103 -0.3986206 0.2003667 +0.1576182 -0.4185998 0.3101039 +0.08997315 -0.4116516 0.3218649 +0.02474219 -0.3941749 0.3274569 +-0.04476491 -0.3100029 0.2881679 +-0.09151793 -0.2752559 0.2653439 +-0.1451879 -0.2130949 0.2817959 +-0.2003339 -0.160961 0.2928429 +-0.03351492 -0.3752879 0.3063839 +-0.255981 -0.108043 0.2957159 +-0.23993 -0.03633502 0.2739539 +-0.216362 0.02461392 0.2383849 +-0.190237 0.07416987 0.1990229 +-0.170241 0.1112649 0.1481649 +-0.07289401 0.1095239 0.0654439 +-0.149401 0.1246749 0.08342189 +-0.02484399 0.1075389 0.009140909 +0.02103502 0.09119892 -0.04583809 +0.144674 0.06653291 -0.08734611 +0.06731099 0.0548749 -0.09258109 +0.196738 0.01866311 -0.1050461 +0.255558 -0.03309393 -0.1098351 +0.356649 -0.09595692 -0.06109211 +0.298461 -0.08945093 -0.1118161 +0.374502 -0.1660679 -0.05309409 +0.374471 -0.2368999 -0.04312909 +0.5 -0.2961828 0.1075409 +0.478546 -0.3428948 0.1549847 +0.382694 -0.3085868 -0.02324611 +0.454088 -0.3058978 0.0006709099 +0.491848 -0.3220188 0.04785889 +0.439496 -0.3996803 0.1538407 +0.226758 -0.4343462 0.2837897 +0.277516 -0.4352694 0.2350517 +0.326421 -0.4423048 0.1820867 +0.380224 -0.4427982 0.1392027 +0.174063 -0.4746489 0.2852419 +0.1071802 -0.4729759 0.3018209 +0.04117221 -0.4634638 0.3270399 +-0.02787191 -0.4333695 0.3483039 +-0.08290792 -0.3552269 0.2574749 +-0.1501509 -0.3356619 0.2645489 +-0.1672799 -0.2752129 0.2722129 +-0.2195009 -0.227752 0.2769939 +-0.2693239 -0.178343 0.2919799 +-0.08897692 -0.4153087 0.3181099 +-0.326659 -0.129267 0.2871959 +-0.306159 -0.05988902 0.2786999 +-0.285615 0.0194459 0.2541219 +-0.243131 0.08589989 0.2433239 +-0.218442 0.1446469 0.1949929 +-0.200903 0.1672369 0.116347 +-0.107504 0.1071289 0.0109309 +-0.207537 0.1799609 0.04860198 +-0.171571 0.1281789 0.02458698 +-0.06489301 0.08206087 -0.04066709 +-0.01679608 0.04971492 -0.08826709 +0.117202 0.00604108 -0.1253151 +0.03252798 0.004088074 -0.1277751 +0.188484 -0.04577491 -0.1330661 +0.23223 -0.1000829 -0.150111 +0.333165 -0.1513859 -0.1057841 +0.274816 -0.1530359 -0.1451591 +0.329598 -0.2195489 -0.1029131 +0.339232 -0.2889699 -0.0847581 +0.480512 -0.3717228 0.0975599 +0.337058 -0.3484309 -0.0553931 +0.427707 -0.3600988 -0.0178661 +0.375502 -0.3923193 -0.02761611 +0.460842 -0.3812808 0.0323199 +0.428805 -0.4232957 0.0874649 +0.228027 -0.4770119 0.2349837 +0.263895 -0.4745517 0.1688427 +0.309251 -0.4746137 0.1165299 +0.361206 -0.4602918 0.07022589 +0.10652 -0.4657877 0.2421269 +0.165255 -0.4717708 0.2220389 +0.04634303 -0.4712858 0.2708489 +-0.0142999 -0.4805948 0.3198429 +-0.07670292 -0.4685888 0.3400469 +-0.1327289 -0.4049902 0.2730939 +-0.1954589 -0.395262 0.2648379 +-0.2104409 -0.3440039 0.2306149 +-0.2269969 -0.2871249 0.2364119 +-0.287003 -0.2458809 0.2550819 +-0.330007 -0.198113 0.2687779 +-0.1302449 -0.469164 0.2965329 +-0.3814189 -0.164343 0.2510149 +-0.3752719 -0.08126602 0.2569009 +-0.3425249 -0.01366401 0.2572389 +-0.3505329 0.01957488 0.3003609 +-0.298896 0.07118392 0.2943939 +-0.281869 0.1383459 0.2612619 +-0.264197 0.1925149 0.2193029 +-0.232991 0.2165769 0.1568219 +-0.21519 0.2443649 0.08627799 +-0.1487161 0.09510893 -0.0461871 +-0.193907 0.2382079 0.02018699 +-0.244087 0.2070919 -0.004436016 +-0.216119 0.1360449 -0.01165101 +-0.0986411 0.04463291 -0.0871141 +-0.04849109 -0.001970917 -0.1258691 +0.06654698 -0.05333391 -0.1548201 +0.13041 -0.06205493 -0.1520511 +-0.004756093 -0.05882692 -0.15391 +0.172966 -0.1117059 -0.1860071 +0.21136 -0.1775299 -0.1839371 +0.275786 -0.2173969 -0.1516191 +0.288428 -0.2839489 -0.1349471 +0.293825 -0.3551989 -0.1123461 +0.308262 -0.4037782 -0.04434711 +0.405525 -0.4254607 0.02220491 +0.338303 -0.4493631 0.003666908 +0.197075 -0.483982 0.1620377 +0.231191 -0.4790217 0.0945619 +0.287926 -0.4798302 0.0473699 +0.05633402 -0.457139 0.1956629 +0.125735 -0.464828 0.1694819 +-0.01070192 -0.4585137 0.2485259 +-0.06980991 -0.4783416 0.2783349 +-0.1885439 -0.4532003 0.2934689 +-0.2489489 -0.3944623 0.2334879 +-0.2550019 -0.4271681 0.2916549 +-0.2341749 -0.3822019 0.1727679 +-0.2443649 -0.3234409 0.1739259 +-0.29625 -0.2869259 0.2024439 +-0.3623739 -0.237694 0.2188469 +-0.1248309 -0.4836673 0.2351679 +-0.1894539 -0.484033 0.2610229 +-0.4193633 -0.189998 0.1929979 +-0.4200609 -0.124141 0.2195389 +-0.4265481 -0.06061801 0.2046649 +-0.4005863 -0.007261008 0.2451489 +-0.4188093 0.03300488 0.3104429 +-0.351163 0.06060088 0.3431179 +-0.333224 0.1216929 0.3262439 +-0.325808 0.1828669 0.2901269 +-0.303299 0.2346379 0.2541159 +-0.264334 0.2671069 0.2049689 +-0.249235 0.2845049 0.1351569 +-0.168233 0.2899339 0.04698297 +-0.238129 0.3104649 0.071686 +-0.2220701 0.08758289 -0.05776 +-0.1738011 0.04238391 -0.093548 +-0.142859 0.2710469 -0.01314509 +-0.213046 0.2818519 -0.02303302 +-0.280701 0.2600459 -0.008296013 +-0.31209 0.2063629 -0.046269 +-0.276226 0.1433529 -0.03744501 +-0.1257481 -0.01217592 -0.1222611 +-0.0764491 -0.06512693 -0.147384 +0.03283101 -0.09412092 -0.2068411 +0.103385 -0.1042849 -0.2066261 +-0.0402931 -0.1047219 -0.197669 +0.138214 -0.1711079 -0.2152921 +0.224686 -0.2542559 -0.1813321 +0.156694 -0.2375469 -0.2086291 +0.241047 -0.3266599 -0.1663571 +0.240655 -0.3905329 -0.1423291 +0.261388 -0.4215227 -0.08855709 +0.268269 -0.4557581 -0.02172011 +0.150067 -0.4839601 0.1064189 +0.223647 -0.4827105 0.0134789 +0.164918 -0.4821071 0.03826991 +-0.003992915 -0.4597246 0.1750199 +0.07355702 -0.4615077 0.1222199 +-0.0696699 -0.4616924 0.2072389 +-0.2451249 -0.4754771 0.2975109 +-0.2998139 -0.4274892 0.2452279 +-0.2847829 -0.4285284 0.1802699 +-0.2990489 -0.4708924 0.2738299 +-0.251016 -0.4187753 0.1203329 +-0.2308879 -0.3586959 0.112527 +-0.298803 -0.3197669 0.1229329 +-0.3480999 -0.28254 0.1616219 +-0.4009569 -0.240432 0.1476389 +-0.1254749 -0.484607 0.1645389 +-0.1910459 -0.4877393 0.1951979 +-0.2589029 -0.4857832 0.2359759 +-0.4439948 -0.186755 0.1184689 +-0.4556715 -0.129177 0.1622529 +-0.4625072 -0.063164 0.1421789 +-0.4363952 0.0009769797 0.1869029 +-0.4669731 0.02844489 0.2527929 +-0.4705242 0.0790059 0.3002479 +-0.4018456 0.1001819 0.3434879 +-0.383178 0.1718659 0.3311779 +-0.366909 0.2346449 0.2872919 +-0.334241 0.2794469 0.2328389 +-0.304061 0.2995559 0.1726549 +-0.319455 0.3047249 0.106281 +-0.18759 0.3545909 0.03597999 +-0.11021 0.3178839 0.01342389 +-0.2968571 0.2990619 0.037139 +-0.245044 0.3301449 0.010176 +-0.2924891 0.07200491 -0.05654502 +-0.2473861 0.02585292 -0.09655002 +-0.2016481 -0.024625 -0.123368 +-0.09316608 0.3030599 -0.04808611 +-0.1609631 0.3268619 -0.05843601 +-0.19883 0.3689719 -0.02078301 +-0.310164 0.2939909 -0.04546002 +-0.301015 0.2568409 -0.09028101 +-0.3471261 0.2346119 -0.105135 +-0.372502 0.2224739 -0.04587001 +-0.39463 0.1644869 -0.029695 +-0.343852 0.1284559 -0.04823703 +-0.1514951 -0.0788359 -0.138736 +-0.09825107 -0.1281749 -0.163047 +-0.004296094 -0.1448469 -0.239171 +0.064336 -0.1565959 -0.2357101 +-0.06481299 -0.1641969 -0.224389 +0.08000898 -0.2259219 -0.2302071 +0.173634 -0.3094519 -0.1922221 +0.105669 -0.2967899 -0.2133611 +0.183363 -0.3781419 -0.1737881 +0.206031 -0.448217 -0.1338671 +0.22045 -0.4764964 -0.0709841 +0.100156 -0.4700674 0.0582929 +0.167908 -0.478234 -0.04215109 +0.110969 -0.4614626 -0.007338107 +0.004248023 -0.4608558 0.1049639 +-0.06289592 -0.4674271 0.1355779 +0.03895301 -0.4593211 0.04469791 +-0.3129699 -0.4745335 0.2080579 +-0.2965969 -0.4734444 0.1420279 +-0.312723 -0.4364029 0.08275899 +-0.2660919 -0.3904489 0.05694598 +-0.2660699 -0.3387799 0.06280199 +-0.32421 -0.3105589 0.046655 +-0.36679 -0.286515 0.09436199 +-0.4156781 -0.236415 0.07292199 +-0.1159389 -0.4835606 0.09633389 +-0.1870209 -0.4857588 0.1243029 +-0.2556419 -0.488161 0.1697059 +-0.4485712 -0.173756 0.04389 +-0.4685848 -0.12259 0.09125799 +-0.4639895 -0.05810702 0.067296 +-0.4595275 0.0002759993 0.1189089 +-0.4837508 0.04807287 0.1746079 +-0.4920636 0.0902949 0.2315719 +-0.4736156 0.1519389 0.2647159 +-0.4420879 0.1428719 0.3213799 +-0.4296233 0.2077219 0.2896259 +-0.4065976 0.2658519 0.2375729 +-0.373128 0.2984899 0.1730549 +-0.4073446 0.2976959 0.113135 +-0.3704801 0.3001989 0.05344898 +-0.143047 0.3954659 0.005804896 +-0.08987701 0.3742969 -0.02814311 +-0.046222 0.3261989 -0.02232811 +-0.348111 0.3097569 -0.008551002 +-0.3609951 0.05203992 -0.044092 +-0.318027 0.002260983 -0.08021402 +-0.275548 -0.04511499 -0.114431 +-0.2247211 -0.096338 -0.135182 +-0.0357351 0.3356069 -0.0791401 +-0.1029781 0.3662739 -0.0905771 +-0.1466591 0.4076499 -0.05116111 +-0.3158351 0.3344689 -0.099924 +-0.3693171 0.3370819 -0.06790403 +-0.2947121 0.3006289 -0.14947 +-0.3225371 0.2533759 -0.166632 +-0.3856021 0.2799149 -0.08780202 +-0.3719121 0.2724319 -0.160715 +-0.3922411 0.2830229 -0.01815802 +-0.4215758 0.2193659 0.00553 +-0.4531314 0.1494489 0.01388699 +-0.4170975 0.09724891 -0.025565 +-0.1597621 -0.14948 -0.136129 +-0.105259 -0.1936439 -0.173679 +0.00563997 -0.2156999 -0.244189 +-0.061115 -0.2274419 -0.226035 +0.03239 -0.2864379 -0.2305751 +0.115927 -0.3688629 -0.1923281 +0.04737699 -0.3533139 -0.2088351 +0.139713 -0.4338456 -0.1560431 +0.160106 -0.4839547 -0.1153791 +0.106182 -0.4663447 -0.08018512 +0.04515302 -0.4572014 -0.03163409 +-0.05795091 -0.4639152 0.0634419 +-0.0163919 -0.459798 0.009101897 +-0.320022 -0.4837598 0.085291 +-0.2571509 -0.4906284 0.10086 +-0.3518929 -0.472076 0.04100299 +-0.3234079 -0.4254205 0.02113298 +-0.263961 -0.3908039 -0.005414009 +-0.266211 -0.3281599 -0.001855999 +-0.320059 -0.2848359 -0.02235401 +-0.373336 -0.266175 0.01685998 +-0.4139202 -0.216223 0.001865983 +-0.1435299 -0.4860647 0.03982189 +-0.2075479 -0.4870262 0.05810598 +-0.4193184 -0.159498 -0.033555 +-0.4516087 -0.112089 0.01305199 +-0.4356066 -0.04442102 -0.001740009 +-0.4555402 0.009166896 0.05026099 +-0.4928081 0.05753392 0.096037 +-0.5 0.1085459 0.1489549 +-0.4792414 0.1595529 0.1945899 +-0.4619214 0.2219999 0.2308069 +-0.4435748 0.2768529 0.1757709 +-0.4713226 0.2623349 0.10719 +-0.4316192 0.2723079 0.04861498 +-0.095777 0.4395129 -0.03423411 +-0.04169109 0.4041059 -0.0712941 +0.0007828474 0.3506379 -0.06462109 +-0.4180857 0.02544492 -0.006972998 +-0.382748 -0.021624 -0.05129802 +-0.347009 -0.07214001 -0.09139201 +-0.298979 -0.117577 -0.116635 +-0.244456 -0.163656 -0.130461 +0.02927184 0.3730029 -0.1137151 +-0.02885509 0.3872539 -0.1204451 +-0.08563808 0.4369779 -0.0930711 +-0.307807 0.3575819 -0.177862 +-0.3573081 0.3736009 -0.134681 +-0.3914911 0.3315169 -0.131383 +-0.3024721 0.3017859 -0.226027 +-0.3522051 0.2638949 -0.224819 +-0.3679771 0.3216929 -0.218945 +-0.4620644 0.2051019 0.06417698 +-0.4878461 0.1270879 0.07399598 +-0.4671831 0.07595593 0.02437198 +-0.12873 -0.2258119 -0.1203521 +-0.191685 -0.204227 -0.11698 +-0.08818901 -0.2659059 -0.170759 +-0.03349599 -0.2880169 -0.221135 +-0.01954201 -0.3529219 -0.199561 +0.07035702 -0.4219461 -0.1718911 +0.00563103 -0.4158243 -0.178231 +0.09321499 -0.4808238 -0.1400171 +0.03682798 -0.4704992 -0.0995861 +-0.028983 -0.4585803 -0.05815211 +-0.0891099 -0.4619997 -0.001954108 +-0.2830499 -0.4916359 0.02779397 +-0.3316759 -0.4802407 -0.01429701 +-0.2931759 -0.4401223 -0.03374502 +-0.224318 -0.4309384 -0.05772302 +-0.220631 -0.3611649 -0.04711401 +-0.246002 -0.2914639 -0.057208 +-0.310976 -0.2481469 -0.074761 +-0.371092 -0.219594 -0.055006 +-0.1532529 -0.4795696 -0.0331921 +-0.2152409 -0.4914252 -0.006649017 +-0.369566 -0.14793 -0.08816501 +-0.4088569 -0.09231201 -0.05250001 +-0.4823322 0.1784389 0.1228919 +-0.4794878 0.2289689 0.1647749 +-0.04753709 0.4729569 -0.0760991 +0.009697795 0.4329209 -0.1175691 +-0.315428 -0.188763 -0.108079 +-0.253604 -0.231777 -0.102649 +0.03543884 0.4147359 -0.1598601 +-0.02698809 0.4483429 -0.135988 +-0.3544581 0.4009729 -0.20476 +-0.3060771 0.3737119 -0.248645 +-0.3947421 0.3656889 -0.185025 +-0.3423121 0.2853179 -0.2824975 +-0.3036241 0.3293259 -0.298207 +-0.3572281 0.3551499 -0.2847262 +-0.3948381 0.3925729 -0.243905 +-0.111017 -0.2949359 -0.1013501 +-0.185161 -0.2670179 -0.09146202 +-0.071107 -0.3316679 -0.163097 +-0.062069 -0.4099034 -0.172077 +0.02564698 -0.4790255 -0.1539001 +-0.04021299 -0.4753222 -0.172921 +-0.03226399 -0.477395 -0.1198241 +-0.09942701 -0.4622091 -0.0755161 +-0.272719 -0.4846815 -0.046628 +-0.211665 -0.4845119 -0.07261801 +-0.18418 -0.4561203 -0.115483 +-0.160407 -0.3998286 -0.08021 +-0.170617 -0.3272589 -0.071536 +-0.160188 -0.4892699 -0.104042 +-0.0104301 0.4916359 -0.1225821 +0.02489781 0.4679059 -0.1662761 +-0.3517351 0.4263539 -0.272797 +-0.3044131 0.4048799 -0.3184325 +-0.3308401 0.3196969 -0.3362187 +-0.3042541 0.3574449 -0.3649065 +-0.3405311 0.3969219 -0.3472517 +-0.3876321 0.4233309 -0.2974645 +-0.112506 -0.3568219 -0.07333511 +-0.09360999 -0.3771009 -0.1230671 +-0.103504 -0.4711515 -0.185662 +-0.133525 -0.4227991 -0.153254 +-0.09681499 -0.4870311 -0.1367 +-0.157716 -0.4802945 -0.162682 +-0.3617861 0.4528549 -0.334462 +-0.3213021 0.4512419 -0.3671553 +-0.2986331 0.4103799 -0.382075 +3 0 2 1 +3 0 3 2 +3 0 4 3 +3 0 5 4 +3 0 6 5 +3 1 6 0 +3 1 7 6 +3 1 8 7 +3 2 9 1 +3 1 9 8 +3 2 10 9 +3 2 11 10 +3 3 11 2 +3 3 12 11 +3 3 13 12 +3 4 13 3 +3 4 14 13 +3 4 15 14 +3 5 16 4 +3 4 16 15 +3 5 17 16 +3 6 17 5 +3 6 18 17 +3 7 18 6 +3 7 19 18 +3 8 20 7 +3 7 20 19 +3 8 21 20 +3 8 22 21 +3 9 22 8 +3 9 23 22 +3 10 23 9 +3 10 24 23 +3 10 25 24 +3 10 26 25 +3 11 26 10 +3 11 27 26 +3 12 27 11 +3 12 28 27 +3 12 29 28 +3 14 29 13 +3 13 29 12 +3 15 30 14 +3 14 31 29 +3 30 31 14 +3 15 32 30 +3 15 33 32 +3 16 33 15 +3 16 34 33 +3 17 34 16 +3 17 35 34 +3 18 35 17 +3 18 36 35 +3 19 36 18 +3 20 37 19 +3 37 38 19 +3 19 38 36 +3 21 39 20 +3 20 39 37 +3 22 40 21 +3 21 41 39 +3 40 41 21 +3 22 42 40 +3 23 42 22 +3 24 43 23 +3 23 43 42 +3 24 44 43 +3 24 45 44 +3 25 45 24 +3 26 46 25 +3 46 47 25 +3 25 47 45 +3 26 48 46 +3 27 48 26 +3 27 49 48 +3 28 49 27 +3 31 50 29 +3 29 50 28 +3 50 51 28 +3 28 51 49 +3 30 52 31 +3 30 53 52 +3 32 53 30 +3 31 54 50 +3 52 54 31 +3 32 55 53 +3 32 56 55 +3 33 56 32 +3 33 57 56 +3 34 57 33 +3 34 58 57 +3 35 58 34 +3 36 59 35 +3 35 59 58 +3 38 60 36 +3 36 60 59 +3 37 61 38 +3 39 61 37 +3 38 62 60 +3 61 62 38 +3 39 63 61 +3 41 64 39 +3 39 64 63 +3 42 65 40 +3 40 65 41 +3 65 66 41 +3 41 66 64 +3 43 67 42 +3 42 67 65 +3 43 68 67 +3 44 68 43 +3 45 69 44 +3 69 70 44 +3 44 70 68 +3 45 71 69 +3 47 71 45 +3 48 72 46 +3 72 73 46 +3 46 73 47 +3 47 74 71 +3 73 74 47 +3 48 75 72 +3 51 75 49 +3 49 75 48 +3 50 76 51 +3 54 76 50 +3 51 77 75 +3 51 78 77 +3 76 78 51 +3 52 79 54 +3 52 80 79 +3 53 80 52 +3 53 81 80 +3 55 81 53 +3 54 82 76 +3 76 82 78 +3 79 82 54 +3 55 83 81 +3 55 84 83 +3 56 84 55 +3 56 85 84 +3 57 85 56 +3 57 86 85 +3 58 86 57 +3 58 87 86 +3 58 88 87 +3 59 88 58 +3 59 89 88 +3 60 89 59 +3 60 90 89 +3 62 90 60 +3 61 91 62 +3 63 91 61 +3 62 92 90 +3 91 92 62 +3 63 93 91 +3 64 93 63 +3 64 94 93 +3 66 94 64 +3 65 95 66 +3 67 95 65 +3 66 96 94 +3 95 96 66 +3 68 97 67 +3 67 97 95 +3 70 98 68 +3 68 98 97 +3 69 99 70 +3 69 100 99 +3 71 100 69 +3 70 101 98 +3 70 102 101 +3 99 102 70 +3 71 103 100 +3 74 103 71 +3 75 104 72 +3 77 104 75 +3 104 105 72 +3 72 105 73 +3 73 106 74 +3 105 106 73 +3 74 107 103 +3 106 107 74 +3 77 108 104 +3 77 109 108 +3 78 109 77 +3 78 110 109 +3 82 110 78 +3 79 111 82 +3 79 112 111 +3 80 112 79 +3 81 113 80 +3 80 113 112 +3 81 114 113 +3 83 114 81 +3 82 115 110 +3 111 115 82 +3 83 116 114 +3 83 117 116 +3 84 117 83 +3 84 118 117 +3 85 118 84 +3 85 119 118 +3 86 119 85 +3 86 120 119 +3 87 120 86 +3 89 121 88 +3 88 121 87 +3 121 122 87 +3 87 122 120 +3 89 123 121 +3 90 123 89 +3 90 124 123 +3 92 124 90 +3 91 125 92 +3 93 125 91 +3 92 126 124 +3 125 126 92 +3 93 127 125 +3 94 127 93 +3 94 128 127 +3 96 128 94 +3 97 129 95 +3 95 129 96 +3 96 130 128 +3 129 130 96 +3 97 131 129 +3 98 131 97 +3 98 132 131 +3 101 132 98 +3 99 133 102 +3 100 133 99 +3 100 134 133 +3 103 134 100 +3 101 135 132 +3 102 136 101 +3 101 136 135 +3 102 137 136 +3 133 137 102 +3 107 138 103 +3 103 138 134 +3 108 139 104 +3 104 139 105 +3 105 140 106 +3 139 140 105 +3 140 141 106 +3 106 141 107 +3 107 142 138 +3 141 142 107 +3 108 143 139 +3 108 144 143 +3 109 144 108 +3 110 145 109 +3 109 145 144 +3 115 146 110 +3 110 146 145 +3 111 147 115 +3 112 147 111 +3 112 148 147 +3 112 149 148 +3 113 149 112 +3 113 150 149 +3 114 150 113 +3 114 151 150 +3 116 151 114 +3 115 152 146 +3 147 152 115 +3 116 153 151 +3 116 154 153 +3 117 154 116 +3 117 155 154 +3 118 155 117 +3 118 156 155 +3 119 156 118 +3 119 157 156 +3 120 157 119 +3 122 158 120 +3 120 158 157 +3 121 159 122 +3 123 159 121 +3 122 160 158 +3 122 161 160 +3 159 161 122 +3 123 162 159 +3 124 162 123 +3 124 163 162 +3 126 163 124 +3 125 164 126 +3 127 164 125 +3 126 165 163 +3 164 165 126 +3 127 166 164 +3 128 166 127 +3 128 167 166 +3 130 167 128 +3 129 168 130 +3 131 168 129 +3 130 169 167 +3 168 169 130 +3 132 170 131 +3 168 170 169 +3 131 170 168 +3 135 171 132 +3 132 171 170 +3 134 172 133 +3 133 172 137 +3 138 172 134 +3 135 173 171 +3 136 174 135 +3 135 175 173 +3 174 175 135 +3 136 176 174 +3 172 176 137 +3 137 176 136 +3 138 177 172 +3 172 177 176 +3 142 177 138 +3 139 178 140 +3 143 178 139 +3 140 179 141 +3 178 179 140 +3 141 180 142 +3 179 180 141 +3 142 181 177 +3 180 181 142 +3 144 182 143 +3 182 183 143 +3 143 183 178 +3 145 184 144 +3 144 184 182 +3 145 185 184 +3 146 185 145 +3 146 186 185 +3 152 186 146 +3 148 187 147 +3 147 187 152 +3 148 188 187 +3 148 189 188 +3 149 189 148 +3 150 190 149 +3 149 190 189 +3 150 191 190 +3 151 191 150 +3 151 192 191 +3 153 192 151 +3 152 193 186 +3 187 193 152 +3 153 194 192 +3 153 195 194 +3 154 195 153 +3 154 196 195 +3 155 196 154 +3 155 197 196 +3 155 198 197 +3 156 198 155 +3 156 199 198 +3 157 199 156 +3 157 200 199 +3 157 201 200 +3 158 201 157 +3 158 202 201 +3 160 202 158 +3 159 203 161 +3 162 203 159 +3 160 204 202 +3 160 205 204 +3 160 206 205 +3 203 206 161 +3 161 206 160 +3 162 207 203 +3 163 207 162 +3 163 208 207 +3 165 208 163 +3 164 209 165 +3 164 210 209 +3 166 210 164 +3 165 211 208 +3 209 211 165 +3 166 212 210 +3 167 212 166 +3 167 213 212 +3 169 213 167 +3 169 214 213 +3 170 214 169 +3 171 215 170 +3 170 215 214 +3 171 216 215 +3 173 216 171 +3 173 217 216 +3 175 217 173 +3 174 218 175 +3 176 218 174 +3 181 218 177 +3 177 218 176 +3 218 219 175 +3 175 219 217 +3 181 219 218 +3 178 220 179 +3 183 220 178 +3 179 221 180 +3 220 221 179 +3 180 222 181 +3 181 222 219 +3 221 222 180 +3 184 223 182 +3 182 224 183 +3 223 224 182 +3 183 224 220 +3 184 225 223 +3 185 225 184 +3 185 226 225 +3 193 226 186 +3 186 226 185 +3 187 227 193 +3 188 227 187 +3 189 228 188 +3 228 229 188 +3 188 229 227 +3 189 230 228 +3 189 231 230 +3 190 231 189 +3 191 232 190 +3 190 232 231 +3 191 233 232 +3 194 233 192 +3 192 233 191 +3 193 234 226 +3 193 235 234 +3 227 235 193 +3 194 236 233 +3 194 237 236 +3 195 237 194 +3 195 238 237 +3 195 239 238 +3 197 239 196 +3 196 239 195 +3 197 240 239 +3 197 241 240 +3 198 241 197 +3 198 242 241 +3 199 242 198 +3 199 243 242 +3 200 243 199 +3 200 244 243 +3 200 245 244 +3 201 245 200 +3 201 246 245 +3 202 246 201 +3 204 247 202 +3 202 248 246 +3 247 248 202 +3 203 249 206 +3 203 250 249 +3 207 250 203 +3 204 251 247 +3 204 252 251 +3 205 252 204 +3 205 253 252 +3 205 254 253 +3 205 255 254 +3 249 255 206 +3 206 255 205 +3 207 256 250 +3 208 256 207 +3 208 257 256 +3 211 257 208 +3 209 258 211 +3 209 259 258 +3 212 259 210 +3 210 259 209 +3 211 260 257 +3 258 260 211 +3 212 261 259 +3 213 261 212 +3 215 262 214 +3 214 262 213 +3 262 263 213 +3 213 263 261 +3 215 264 262 +3 216 264 215 +3 216 265 264 +3 216 266 265 +3 217 266 216 +3 217 267 266 +3 219 267 217 +3 222 267 219 +3 220 268 221 +3 224 268 220 +3 221 269 222 +3 222 269 267 +3 221 270 269 +3 268 270 221 +3 225 271 223 +3 223 272 224 +3 271 272 223 +3 224 272 268 +3 225 273 271 +3 226 273 225 +3 234 273 226 +3 227 274 235 +3 229 274 227 +3 228 275 229 +3 228 276 275 +3 230 276 228 +3 229 277 274 +3 275 277 229 +3 230 278 276 +3 230 279 278 +3 231 279 230 +3 231 280 279 +3 232 280 231 +3 232 281 280 +3 233 281 232 +3 233 282 281 +3 236 282 233 +3 234 283 273 +3 234 284 283 +3 235 284 234 +3 235 285 284 +3 277 285 274 +3 274 285 235 +3 236 286 282 +3 236 287 286 +3 238 287 237 +3 237 287 236 +3 238 288 287 +3 238 289 288 +3 239 289 238 +3 239 290 289 +3 240 290 239 +3 240 291 290 +3 240 292 291 +3 241 292 240 +3 242 292 241 +3 242 293 292 +3 243 293 242 +3 243 294 293 +3 244 294 243 +3 244 295 294 +3 245 295 244 +3 245 296 295 +3 246 296 245 +3 246 297 296 +3 248 297 246 +3 247 298 248 +3 247 299 298 +3 251 299 247 +3 248 300 297 +3 248 301 300 +3 300 301 253 +3 298 301 248 +3 253 301 252 +3 249 302 255 +3 249 303 302 +3 250 303 249 +3 250 304 303 +3 256 304 250 +3 251 305 299 +3 251 306 305 +3 252 306 251 +3 252 307 306 +3 301 307 252 +3 298 307 301 +3 253 308 300 +3 253 309 308 +3 254 309 253 +3 254 310 309 +3 254 311 310 +3 254 312 311 +3 254 313 312 +3 255 313 254 +3 302 313 255 +3 256 314 304 +3 257 314 256 +3 257 315 314 +3 260 315 257 +3 258 316 260 +3 258 317 316 +3 261 317 259 +3 259 317 258 +3 260 318 315 +3 316 318 260 +3 261 319 317 +3 263 319 261 +3 262 320 263 +3 264 320 262 +3 320 321 263 +3 263 321 319 +3 265 322 264 +3 264 322 320 +3 265 323 322 +3 266 323 265 +3 266 324 323 +3 269 324 267 +3 267 324 266 +3 268 325 270 +3 272 325 268 +3 269 326 324 +3 270 326 269 +3 270 327 326 +3 325 327 270 +3 271 328 272 +3 271 329 328 +3 283 329 273 +3 273 329 271 +3 272 330 325 +3 325 330 327 +3 328 330 272 +3 275 331 277 +3 276 331 275 +3 277 331 285 +3 276 332 331 +3 278 332 276 +3 278 333 332 +3 278 334 333 +3 279 334 278 +3 279 335 334 +3 280 335 279 +3 280 336 335 +3 280 337 336 +3 282 337 281 +3 281 337 280 +3 286 338 282 +3 282 338 337 +3 283 339 329 +3 283 340 339 +3 284 340 283 +3 284 341 340 +3 331 341 285 +3 332 341 331 +3 285 341 284 +3 286 342 338 +3 286 343 342 +3 288 343 287 +3 287 343 286 +3 288 344 343 +3 288 345 344 +3 289 345 288 +3 289 346 345 +3 290 346 289 +3 290 347 346 +3 291 347 290 +3 291 348 347 +3 291 349 348 +3 292 349 291 +3 293 349 292 +3 293 350 349 +3 349 350 348 +3 294 350 293 +3 294 351 350 +3 295 351 294 +3 295 352 351 +3 297 352 296 +3 296 352 295 +3 297 353 352 +3 297 354 353 +3 300 354 297 +3 298 355 307 +3 299 355 298 +3 299 356 355 +3 299 357 356 +3 305 357 299 +3 308 358 300 +3 300 358 354 +3 302 359 313 +3 302 360 359 +3 303 360 302 +3 303 361 360 +3 304 361 303 +3 314 362 304 +3 304 362 361 +3 305 363 357 +3 305 364 363 +3 306 364 305 +3 306 365 364 +3 355 365 307 +3 307 365 306 +3 309 366 308 +3 366 367 308 +3 308 367 358 +3 309 368 366 +3 309 369 368 +3 310 369 309 +3 311 370 310 +3 310 371 369 +3 370 371 310 +3 311 372 370 +3 370 372 367 +3 358 372 354 +3 367 372 358 +3 311 373 372 +3 312 373 311 +3 312 374 373 +3 312 375 374 +3 359 375 313 +3 313 375 312 +3 315 376 314 +3 314 376 362 +3 315 377 376 +3 318 377 315 +3 317 378 316 +3 319 378 317 +3 316 378 318 +3 318 379 377 +3 378 379 318 +3 319 380 378 +3 321 380 319 +3 322 381 320 +3 320 381 321 +3 321 382 380 +3 381 382 321 +3 322 383 381 +3 323 383 322 +3 323 384 383 +3 324 384 323 +3 326 384 324 +3 326 385 384 +3 327 385 326 +3 327 386 385 +3 330 386 327 +3 329 387 328 +3 339 387 329 +3 328 388 330 +3 387 388 328 +3 330 388 386 +3 333 389 332 +3 389 390 332 +3 332 390 341 +3 341 390 340 +3 333 391 389 +3 333 392 391 +3 334 392 333 +3 334 393 392 +3 334 394 393 +3 335 394 334 +3 336 394 335 +3 336 395 394 +3 336 396 395 +3 338 396 337 +3 337 396 336 +3 342 397 338 +3 338 397 396 +3 339 398 387 +3 340 398 339 +3 340 399 398 +3 390 399 340 +3 342 400 397 +3 342 401 400 +3 344 401 343 +3 343 401 342 +3 344 402 401 +3 344 403 402 +3 345 403 344 +3 345 404 403 +3 346 404 345 +3 346 405 404 +3 347 405 346 +3 347 406 405 +3 348 406 347 +3 348 407 406 +3 350 407 348 +3 351 407 350 +3 353 408 352 +3 352 408 351 +3 351 408 407 +3 353 409 408 +3 354 410 353 +3 353 410 409 +3 373 410 372 +3 372 410 354 +3 355 411 365 +3 356 411 355 +3 356 412 411 +3 357 412 356 +3 363 413 357 +3 357 413 412 +3 403 414 402 +3 359 414 375 +3 359 415 414 +3 414 415 402 +3 360 415 359 +3 361 416 360 +3 360 416 415 +3 361 417 416 +3 362 417 361 +3 376 418 362 +3 362 418 417 +3 363 419 413 +3 413 419 412 +3 363 420 419 +3 364 420 363 +3 365 421 364 +3 411 421 365 +3 364 421 420 +3 368 422 366 +3 422 423 366 +3 366 423 367 +3 367 424 370 +3 423 424 367 +3 370 424 371 +3 369 425 368 +3 368 425 422 +3 369 426 425 +3 371 426 369 +3 371 427 426 +3 424 427 371 +3 373 428 410 +3 410 428 409 +3 374 428 373 +3 374 429 428 +3 405 429 404 +3 374 430 429 +3 375 430 374 +3 414 430 375 +3 403 430 414 +3 404 430 403 +3 429 430 404 +3 377 431 376 +3 431 432 376 +3 376 432 418 +3 379 433 377 +3 377 433 431 +3 380 434 378 +3 379 434 433 +3 378 434 379 +3 382 435 380 +3 380 435 434 +3 381 436 382 +3 383 436 381 +3 436 437 382 +3 382 437 435 +3 383 438 436 +3 384 438 383 +3 385 438 384 +3 386 439 385 +3 385 439 438 +3 386 440 439 +3 388 440 386 +3 388 441 440 +3 387 441 388 +3 398 441 387 +3 389 442 390 +3 390 442 399 +3 391 442 389 +3 391 443 442 +3 392 443 391 +3 392 444 443 +3 393 444 392 +3 393 445 444 +3 393 446 445 +3 394 446 393 +3 394 447 446 +3 395 447 394 +3 395 448 447 +3 395 449 448 +3 400 449 397 +3 397 449 396 +3 396 449 395 +3 398 450 441 +3 398 451 450 +3 442 451 399 +3 399 451 398 +3 400 452 449 +3 417 452 416 +3 400 453 452 +3 452 453 416 +3 415 453 402 +3 416 453 415 +3 402 453 401 +3 401 453 400 +3 406 454 405 +3 405 454 429 +3 429 454 428 +3 428 454 409 +3 406 455 454 +3 454 455 409 +3 409 455 408 +3 408 455 407 +3 407 455 406 +3 411 456 421 +3 412 456 411 +3 412 457 456 +3 419 457 412 +3 417 458 452 +3 418 458 417 +3 449 458 448 +3 452 458 449 +3 418 459 458 +3 458 459 448 +3 448 459 447 +3 432 459 418 +3 420 460 419 +3 419 460 457 +3 420 461 460 +3 421 461 420 +3 456 461 421 +3 422 462 423 +3 422 463 462 +3 425 463 422 +3 423 464 424 +3 424 464 427 +3 462 464 423 +3 426 465 425 +3 427 465 426 +3 465 466 425 +3 425 466 463 +3 427 467 465 +3 427 468 467 +3 462 468 464 +3 464 468 427 +3 433 469 431 +3 469 470 431 +3 431 470 432 +3 459 470 447 +3 432 470 459 +3 433 471 469 +3 434 471 433 +3 435 471 434 +3 437 472 435 +3 435 472 471 +3 436 473 437 +3 439 473 438 +3 438 473 436 +3 437 474 472 +3 473 474 437 +3 439 475 473 +3 473 475 474 +3 440 475 439 +3 440 476 475 +3 450 476 441 +3 441 476 440 +3 442 477 451 +3 445 477 444 +3 444 477 443 +3 443 477 442 +3 445 478 477 +3 477 478 451 +3 451 478 450 +3 445 479 478 +3 445 480 479 +3 446 480 445 +3 446 481 480 +3 469 481 470 +3 470 481 447 +3 447 481 446 +3 450 482 476 +3 478 482 450 +3 479 482 478 +3 456 483 461 +3 457 483 456 +3 457 484 483 +3 461 484 460 +3 483 484 461 +3 460 484 457 +3 462 485 468 +3 463 485 462 +3 463 486 485 +3 466 486 463 +3 465 487 466 +3 467 487 465 +3 487 488 466 +3 466 488 486 +3 467 489 487 +3 487 489 488 +3 467 490 489 +3 485 490 468 +3 468 490 467 +3 469 491 481 +3 481 491 480 +3 469 492 491 +3 472 492 471 +3 491 492 480 +3 471 492 469 +3 474 493 472 +3 493 494 472 +3 480 494 479 +3 492 494 480 +3 472 494 492 +3 474 495 493 +3 475 495 474 +3 482 495 476 +3 476 495 475 +3 479 496 482 +3 493 496 494 +3 494 496 479 +3 495 496 493 +3 482 496 495 +3 485 497 490 +3 486 497 485 +3 490 497 489 +3 486 498 497 +3 497 498 489 +3 486 499 498 +3 488 499 486 +3 489 499 488 +3 498 499 489 diff --git a/src/SimpleBVH/BVH.cpp b/src/SimpleBVH/BVH.cpp index 7fe6ea1..cb9f01f 100644 --- a/src/SimpleBVH/BVH.cpp +++ b/src/SimpleBVH/BVH.cpp @@ -4,23 +4,180 @@ #include namespace SimpleBVH { + +void point_segment_squared_distance( + const VectorMax3d& point, + const std::array& f, + VectorMax3d& closest_point, + double& dist) +{ + const double l2 = (f[0] - f[1]).squaredNorm(); + const double t = (point - f[0]).dot(f[1] - f[0]); + if (t <= 0.0 || l2 == 0.0) { + closest_point = f[0]; + } else if (t > l2) { + closest_point = f[1]; + } else { + const double lambda1 = t / l2; + const double lambda0 = 1.0 - lambda1; + closest_point = lambda0 * f[0] + lambda1 * f[1]; + } + dist = (point - closest_point).squaredNorm(); +} + +void point_triangle_squared_distance( + const VectorMax3d& x, + const std::array& f, + VectorMax3d& pt, + double& dist) +{ + const VectorMax3d& pa = f[0]; + const VectorMax3d& pb = f[1]; + const VectorMax3d& pc = f[2]; + + // source: real time collision detection + // check if x in vertex region outside pa + VectorMax3d ab = pb - pa; + VectorMax3d ac = pc - pa; + VectorMax3d ax = x - pa; + const double d1 = ab.dot(ax); + const double d2 = ac.dot(ax); + if (d1 <= 0 && d2 <= 0) { + // barycentric coordinates (1, 0, 0) + pt = pa; + dist = (x - pt).squaredNorm(); + return; + } + + // check if x in vertex region outside pb + VectorMax3d bx = x - pb; + const double d3 = ab.dot(bx); + const double d4 = ac.dot(bx); + if (d3 >= 0.0f && d4 <= d3) { + // barycentric coordinates (0, 1, 0) + pt = pb; + dist = (x - pt).squaredNorm(); + return; + } + + // check if x in vertex region outside pc + VectorMax3d cx = x - pc; + const double d5 = ab.dot(cx); + const double d6 = ac.dot(cx); + if (d6 >= 0.0f && d5 <= d6) { + // barycentric coordinates (0, 0, 1) + pt = pc; + dist = (x - pt).squaredNorm(); + return; + } + + // check if x in edge region of ab, if so return projection of x onto ab + const double vc = d1 * d4 - d3 * d2; + if (vc <= 0.0f && d1 >= 0.0f && d3 <= 0.0f) { + // barycentric coordinates (1 - v, v, 0) + const double v = d1 / (d1 - d3); + pt = pa + ab * v; + dist = (x - pt).squaredNorm(); + return; + } + + // check if x in edge region of ac, if so return projection of x onto ac + const double vb = d5 * d2 - d1 * d6; + if (vb <= 0.0f && d2 >= 0.0f && d6 <= 0.0f) { + // barycentric coordinates (1 - w, 0, w) + const double w = d2 / (d2 - d6); + pt = pa + ac * w; + dist = (x - pt).squaredNorm(); + return; + } + + // check if x in edge region of bc, if so return projection of x onto bc + const double va = d3 * d6 - d5 * d4; + if (va <= 0.0f && (d4 - d3) >= 0.0f && (d5 - d6) >= 0.0f) { + // barycentric coordinates (0, 1 - w, w) + const double w = (d4 - d3) / ((d4 - d3) + (d5 - d6)); + pt = pb + (pc - pb) * w; + dist = (x - pt).squaredNorm(); + return; + } + + // x inside face region. Compute pt through its barycentric coordinates (u, + // v, w) + const double denom = 1 / (va + vb + vc); + const double v = vb * denom; + const double w = vc * denom; + + pt = pa + ab * v + ac * w; //= u*a + v*b + w*c, u = va*denom = 1.0f - v - w + dist = (x - pt).squaredNorm(); +} + namespace { bool box_box_intersection( - const Eigen::Vector3d& min1, - const Eigen::Vector3d& max1, - const Eigen::Vector3d& min2, - const Eigen::Vector3d& max2) + const VectorMax3d& min1, + const VectorMax3d& max1, + const VectorMax3d& min2, + const VectorMax3d& max2) + { + if (min1.size() == 3) { + if (max1[0] < min2[0] || max1[1] < min2[1] || max1[2] < min2[2]) + return 0; + if (max2[0] < min1[0] || max2[1] < min1[1] || max2[2] < min1[2]) + return 0; + return 1; + } else { + if (max1[0] < min2[0] || max1[1] < min2[1]) + return 0; + if (max2[0] < min1[0] || max2[1] < min1[1]) + return 0; + return 1; + } + } + + double point_box_center_squared_distance( + const VectorMax3d& p, const std::array& B) + { + return (p - (B[0] + B[1]) / 2).squaredNorm(); + } + + double inner_point_box_squared_distance( + const VectorMax3d& p, const std::array& B) { - if (max1[0] < min2[0] || max1[1] < min2[1] || max1[2] < min2[2]) - return 0; - if (max2[0] < min1[0] || max2[1] < min1[1] || max2[2] < min1[2]) - return 0; - return 1; + assert(p.size() == B[0].size()); + + double result = std::pow(p[0] - B[0][0], 2); + result = std::min(result, std::pow(p[0] - B[1][0], 2)); + for (int c = 1; c < p.size(); ++c) { + result = std::min(result, std::pow(p[c] - B[0][c], 2)); + result = std::min(result, std::pow(p[c] - B[1][c], 2)); + } + return result; + } + + double point_box_signed_squared_distance( + const VectorMax3d& p, const std::array& B) + { + assert(p.size() == B[0].size()); + + bool inside = true; + double result = 0.0; + for (int c = 0; c < p.size(); c++) { + if (p[c] < B[0][c]) { + inside = false; + result += std::pow(p[c] - B[0][c], 2); + } else if (p[c] > B[1][c]) { + inside = false; + result += std::pow(p[c] - B[1][c], 2); + } + } + if (inside) { + result = -inner_point_box_squared_distance(p, B); + } + return result; } } // namespace void BVH::init_boxes_recursive( - const std::vector>& cornerlist, + const std::vector>& cornerlist, int node_index, int b, int e) @@ -44,7 +201,7 @@ void BVH::init_boxes_recursive( assert(childl < boxlist.size()); assert(childr < boxlist.size()); - for (int c = 0; c < 3; ++c) { + for (int c = 0; c < cornerlist[0][0].size(); ++c) { boxlist[node_index][0][c] = std::min(boxlist[childl][0][c], boxlist[childr][0][c]); boxlist[node_index][1][c] = @@ -53,8 +210,8 @@ void BVH::init_boxes_recursive( } void BVH::box_search_recursive( - const Eigen::Vector3d& bbd0, - const Eigen::Vector3d& bbd1, + const VectorMax3d& bbd0, + const VectorMax3d& bbd1, std::vector& list, int n, int b, @@ -99,25 +256,25 @@ int BVH::max_node_index(int node_index, int b, int e) return std::max(max_node_index(childl, b, m), max_node_index(childr, m, e)); } -void BVH::init(const std::vector>& cornerlist) +void BVH::init(const std::vector>& cornerlist) { n_corners = cornerlist.size(); - Eigen::MatrixXd box_centers(n_corners, 3); + Eigen::MatrixXd box_centers(n_corners, cornerlist[0][0].size()); for (int i = 0; i < n_corners; ++i) { box_centers.row(i) = (cornerlist[i][0] + cornerlist[i][1]) / 2; } - const Eigen::RowVector3d vmin = box_centers.colwise().minCoeff(); - const Eigen::RowVector3d vmax = box_centers.colwise().maxCoeff(); - const Eigen::RowVector3d center = (vmin + vmax) / 2; + const VectorMax3d vmin = box_centers.colwise().minCoeff(); + const VectorMax3d vmax = box_centers.colwise().maxCoeff(); + const VectorMax3d center = (vmin + vmax) / 2; for (int i = 0; i < n_corners; i++) { // make box centered at origin box_centers.row(i) -= center; } // after placing box at origin, vmax and vmin are symetric. - const Eigen::Vector3d scale_point = vmax - center; + const VectorMax3d scale_point = vmax - center; const double scale = scale_point.lpNorm(); // if the box is too big, resize it if (scale > 100) { @@ -135,8 +292,8 @@ void BVH::init(const std::vector>& cornerlist) for (int i = 0; i < n_corners; i++) { const Eigen::MatrixXd tmp = box_centers.row(i) * multi; - list[i].morton = - Resorting::MortonCode64(int(tmp(0)), int(tmp(1)), int(tmp(2))); + list[i].morton = Resorting::MortonCode64( + int(tmp(0)), int(tmp(1)), tmp.size() == 3 ? int(tmp(2)) : 0); list[i].order = i; } @@ -150,7 +307,7 @@ void BVH::init(const std::vector>& cornerlist) new2old[i] = list[i].order; } - std::vector> sorted_cornerlist(n_corners); + std::vector> sorted_cornerlist(n_corners); for (int i = 0; i < n_corners; i++) { sorted_cornerlist[i] = cornerlist[list[i].order]; @@ -160,6 +317,10 @@ void BVH::init(const std::vector>& cornerlist) max_node_index(1, 0, n_corners) + 1 // <-- this is because size == max_index + 1 !!! ); + for (auto& b : boxlist) { + b[0].resize(cornerlist[0][0].size()); + b[1].resize(cornerlist[0][0].size()); + } init_boxes_recursive(sorted_cornerlist, 1, 0, n_corners); } @@ -167,38 +328,182 @@ void BVH::init(const std::vector>& cornerlist) void BVH::init( const Eigen::MatrixXd& V, const Eigen::MatrixXi& F, const double tol) { - assert(F.cols() == 3); - assert(V.cols() == 3); - - std::vector> cornerlist(F.rows()); - - for (int i = 0; i < F.rows(); i++) { - const Eigen::RowVector3i face = F.row(i); - const Eigen::RowVector3d v0 = V.row(face(0)); - const Eigen::RowVector3d v1 = V.row(face(1)); - const Eigen::RowVector3d v2 = V.row(face(2)); - - Eigen::Matrix3d tmp; - tmp.row(0) = v0; - tmp.row(1) = v1; - tmp.row(2) = v2; - - const Eigen::RowVector3d min = tmp.colwise().minCoeff().array() - tol; - const Eigen::RowVector3d max = tmp.colwise().maxCoeff().array() + tol; - - cornerlist[i][0] = min.transpose(); - cornerlist[i][1] = max.transpose(); + assert(F.cols() == 3 || F.cols() == 2); + assert(V.cols() == 3 || V.cols() == 2); + + vertices = V; + faces = F; + + std::vector> cornerlist(F.rows()); + if (F.cols() == 3) { + for (int i = 0; i < F.rows(); i++) { + const Eigen::RowVector3i face = F.row(i); + const VectorMax3d v0 = V.row(face(0)); + const VectorMax3d v1 = V.row(face(1)); + const VectorMax3d v2 = V.row(face(2)); + + Eigen::MatrixXd tmp(3, v0.size()); + tmp.row(0) = v0.transpose(); + tmp.row(1) = v1.transpose(); + tmp.row(2) = v2.transpose(); + + const VectorMax3d min = tmp.colwise().minCoeff().array() - tol; + const VectorMax3d max = tmp.colwise().maxCoeff().array() + tol; + + cornerlist[i][0] = min.transpose(); + cornerlist[i][1] = max.transpose(); + } + + } else if (F.cols() == 2) { + for (int i = 0; i < F.rows(); i++) { + const Eigen::RowVector2i face = F.row(i); + const VectorMax3d v0 = V.row(face(0)); + const VectorMax3d v1 = V.row(face(1)); + + Eigen::MatrixXd tmp(2, v0.size()); + tmp.row(0) = v0.transpose(); + tmp.row(1) = v1.transpose(); + + const VectorMax3d min = tmp.colwise().minCoeff().array() - tol; + const VectorMax3d max = tmp.colwise().maxCoeff().array() + tol; + + cornerlist[i][0] = min.transpose(); + cornerlist[i][1] = max.transpose(); + } } init(cornerlist); } bool BVH::box_intersects_box( - const Eigen::Vector3d& bbd0, const Eigen::Vector3d& bbd1, int index) const + const VectorMax3d& bbd0, const VectorMax3d& bbd1, int index) const { const auto& bmin = boxlist[index][0]; const auto& bmax = boxlist[index][1]; return box_box_intersection(bbd0, bbd1, bmin, bmax); } + +void BVH::get_nearest_facet_hint( + const VectorMax3d& p, + int& nearest_f, + VectorMax3d& nearest_point, + double& sq_dist) const +{ + int b = 0; + int e = n_corners; + int n = 1; + while (e != b + 1) { + int m = b + (e - b) / 2; + int childl = 2 * n; + int childr = 2 * n + 1; + if (point_box_center_squared_distance(p, boxlist[childl]) + < point_box_center_squared_distance(p, boxlist[childr])) { + e = m; + n = childl; + } else { + b = m; + n = childr; + } + } + nearest_f = b; + + nearest_point = point_callback(new2old[nearest_f]); + sq_dist = (p - nearest_point).squaredNorm(); +} + +void BVH::nearest_facet_recursive( + const VectorMax3d& p, + int& nearest_f, + VectorMax3d& nearest_point, + double& sq_dist, + int n, + int b, + int e) const +{ + assert(e > b); + + // If node is a leaf: compute point-facet distance + // and replace current if nearer + if (b + 1 == e) { + VectorMax3d cur_nearest_point; + double cur_sq_dist; + leaf_callback(p, new2old[b], cur_nearest_point, cur_sq_dist); + if (cur_sq_dist < sq_dist) { + nearest_f = b; + nearest_point = cur_nearest_point; + sq_dist = cur_sq_dist; + } + return; + } + int m = b + (e - b) / 2; + int childl = 2 * n; + int childr = 2 * n + 1; + + assert(childl < boxlist.size()); + assert(childr < boxlist.size()); + + double dl = point_box_signed_squared_distance(p, boxlist[childl]); + double dr = point_box_signed_squared_distance(p, boxlist[childr]); + + // Traverse the "nearest" child first, so that it has more chances + // to prune the traversal of the other child. + if (dl < dr) { + if (dl < sq_dist) { + nearest_facet_recursive( + p, nearest_f, nearest_point, sq_dist, childl, b, m); + } + if (dr < sq_dist) { + nearest_facet_recursive( + p, nearest_f, nearest_point, sq_dist, childr, m, e); + } + } else { + if (dr < sq_dist) { + nearest_facet_recursive( + p, nearest_f, nearest_point, sq_dist, childr, m, e); + } + if (dl < sq_dist) { + nearest_facet_recursive( + p, nearest_f, nearest_point, sq_dist, childl, b, m); + } + } +} + +void BVH::leaf_callback( + const VectorMax3d& p, int f, VectorMax3d& np, double& sq_d) const +{ + if (leafCallback) { + leafCallback(p, f, np, sq_d); + return; + } + + if (faces.cols() == 2) { + point_segment_squared_distance( + p, { { vertices.row(faces(f, 0)), vertices.row(faces(f, 1)) } }, np, + sq_d); + return; + } else if (faces.cols() == 3) { + point_triangle_squared_distance( + p, + { { vertices.row(faces(f, 0)), vertices.row(faces(f, 1)), + vertices.row(faces(f, 2)) } }, + np, sq_d); + return; + } + + assert(false); +} +VectorMax3d BVH::point_callback(int f) const +{ + if (getPoint) { + return getPoint(f); + } + + if (faces.cols() == 2 || faces.cols() == 3) { + return vertices.row(faces(f, 0)); + } + + assert(false); + return VectorMax3d(3); +} } // namespace SimpleBVH diff --git a/src/SimpleBVH/BVH.hpp b/src/SimpleBVH/BVH.hpp index 8000a74..0001047 100644 --- a/src/SimpleBVH/BVH.hpp +++ b/src/SimpleBVH/BVH.hpp @@ -10,10 +10,25 @@ namespace SimpleBVH { using VectorMax3d = Eigen::Matrix; +using LeafCallback = + std::function; +using GetPointCallback = std::function; + +void point_segment_squared_distance( + const VectorMax3d& point, + const std::array& f, + VectorMax3d& closest_point, + double& dist); + +void point_triangle_squared_distance( + const VectorMax3d& point, + const std::array& f, + VectorMax3d& closest_point, + double& dist); class BVH { public: - void init(const std::vector>& cornerlist); + void init(const std::vector>& cornerlist); void init(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F, const double tol); @@ -25,11 +40,14 @@ class BVH { n_corners = -1; } - void intersect_3D_box( - const Eigen::Vector3d& bbd0, - const Eigen::Vector3d& bbd1, + void intersect_box( + const VectorMax3d& bbd0, + const VectorMax3d& bbd1, std::vector& list) const { + assert(bbd0.size() == 2 || bbd0.size() == 3); + assert(bbd0.size() == bbd1.size()); + std::vector tmp; assert(n_corners >= 0); box_search_recursive(bbd0, bbd1, tmp, 1, 0, n_corners); @@ -39,58 +57,85 @@ class BVH { list[i] = new2old[tmp[i]]; } - void intersect_2D_box( - const Eigen::Vector2d& bbd0, - const Eigen::Vector2d& bbd1, - std::vector& list) const + int nearest_facet( + const VectorMax3d& p, VectorMax3d& nearest_point, double& sq_dist) const { - Eigen::Vector3d bbd0_3D = Eigen::Vector3d::Zero(); - bbd0_3D.head<2>() = bbd0; - - Eigen::Vector3d bbd1_3D = Eigen::Vector3d::Zero(); - bbd1_3D.head<2>() = bbd1; - - intersect_3D_box(bbd0_3D, bbd1_3D, list); + int nearest_facet; + get_nearest_facet_hint(p, nearest_facet, nearest_point, sq_dist); + nearest_facet_recursive( + p, nearest_facet, nearest_point, sq_dist, 1, 0, n_corners); + return nearest_facet; } - void intersect_box( - const VectorMax3d& bbd0, - const VectorMax3d& bbd1, - std::vector& list) const + void nearest_facet_with_hint( + const VectorMax3d& p, + int& nearest_facet, + VectorMax3d& nearest_point, + double& sq_dist) const { - Eigen::Vector3d bbd0_3D = Eigen::Vector3d::Zero(); - bbd0_3D.head(bbd0.size()) = bbd0.head(bbd0.size()); + if (nearest_facet < 0) { + get_nearest_facet_hint(p, nearest_facet, nearest_point, sq_dist); + } + nearest_facet_recursive( + p, nearest_facet, nearest_point, sq_dist, 1, 0, n_corners); + } - Eigen::Vector3d bbd1_3D = Eigen::Vector3d::Zero(); - bbd1_3D.head(bbd1.size()) = bbd1.head(bbd1.size()); + void set_leaf_distance_callback(const LeafCallback& callback) + { + leafCallback = callback; + } - intersect_3D_box(bbd0_3D, bbd1_3D, list); + void set_get_point_callback(const GetPointCallback& callback) + { + getPoint = callback; } private: void init_boxes_recursive( - const std::vector>& cornerlist, + const std::vector>& cornerlist, int node_index, int b, int e); void box_search_recursive( - const Eigen::Vector3d& bbd0, - const Eigen::Vector3d& bbd1, + const VectorMax3d& bbd0, + const VectorMax3d& bbd1, std::vector& list, int n, int b, int e) const; + void get_nearest_facet_hint( + const VectorMax3d& p, + int& nearest_f, + VectorMax3d& nearest_point, + double& sq_dist) const; + + void nearest_facet_recursive( + const VectorMax3d& p, + int& nearest_f, + VectorMax3d& nearest_point, + double& sq_dist, + int n, + int b, + int e) const; + bool box_intersects_box( - const Eigen::Vector3d& bbd0, - const Eigen::Vector3d& bbd1, - int index) const; + const VectorMax3d& bbd0, const VectorMax3d& bbd1, int index) const; static int max_node_index(int node_index, int b, int e); - std::vector> boxlist; + void leaf_callback( + const VectorMax3d& p, int f, VectorMax3d& np, double& sq_d) const; + VectorMax3d point_callback(int f) const; + + std::vector> boxlist; std::vector new2old; - size_t n_corners = -1; + long n_corners = -1; + LeafCallback leafCallback; + GetPointCallback getPoint; + + Eigen::MatrixXd vertices; + Eigen::MatrixXi faces; }; } // namespace SimpleBVH diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a4a5ce8..9dd689d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,6 +16,7 @@ target_link_libraries(simple_bvh_tests PUBLIC Catch2::Catch2WithMain) # Extra warnings (link last for highest priority) include(simple_bvh_warnings) target_link_libraries(simple_bvh_tests PRIVATE simple_bvh::warnings) +target_compile_definitions(simple_bvh_tests PRIVATE BVH_ROOT_PATH="${CMAKE_SOURCE_DIR}") ################################################################################ # Compiler options diff --git a/tests/tests.cpp b/tests/tests.cpp index dd307f7..b396160 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,6 +1,12 @@ #include #include +#include +#include + +using namespace SimpleBVH; +using namespace Eigen; + TEST_CASE("test_tree", "[tests]") { Eigen::MatrixXd pts(4, 3); @@ -28,3 +34,125 @@ TEST_CASE("test_tree", "[tests]") CHECK(pairs.size() == tri.rows()); } + +TEST_CASE("test_distance", "[tests]") +{ + Eigen::MatrixXd pts(4, 3); + // clang-format off + pts << 0, 0, 0, + 3, 0, 0, + 0, 3, 0, + 0, 3, 3; + // clang-format on + + Eigen::MatrixXi edges(7, 2); + // clang-format off + edges << 0, 1, + 1, 2, + 2, 0, + 1, 3, + 3, 0, + 2, 3, + 1, 3; + // clang-format on + + SimpleBVH::BVH bvh; + VectorMax3d nearest_point; + double sq_dist; + bvh.init(pts, edges, 1e-10); + bvh.nearest_facet(Eigen::Vector3d(-1, -1, -1), nearest_point, sq_dist); + + CHECK(pts.row(0) == nearest_point.transpose()); +} + +TEST_CASE("test_distance_2d", "[tests]") +{ + Eigen::MatrixXd pts(4, 2); + // clang-format off + pts << 0, 0, + 3, 0, + 0, 3, + 0, 3; + // clang-format on + + Eigen::MatrixXi edges(7, 2); + // clang-format off + edges << 0, 1, + 1, 2, + 2, 0, + 1, 3, + 3, 0, + 2, 3, + 1, 3; + // clang-format on + + SimpleBVH::BVH bvh; + VectorMax3d nearest_point; + double sq_dist; + bvh.init(pts, edges, 1e-10); + bvh.nearest_facet(Eigen::Vector2d(-1, -1), nearest_point, sq_dist); + + CHECK(pts.row(0) == nearest_point.transpose()); +} + +TEST_CASE("test_distance_3d", "[tests]") +{ + MatrixXd vertices; + MatrixXi facets; + std::string root = BVH_ROOT_PATH; + + std::string mesh_filename = root + "/bunny.off"; + std::ifstream in(mesh_filename); + std::string token; + in >> token; + int nv, nf, ne; + in >> nv >> nf >> ne; + vertices.resize(nv, 3); + facets.resize(nf, 3); + for (int i = 0; i < nv; ++i) { + in >> vertices(i, 0) >> vertices(i, 1) >> vertices(i, 2); + } + for (int i = 0; i < nf; ++i) { + int s; + in >> s >> facets(i, 0) >> facets(i, 1) >> facets(i, 2); + assert(s == 3); + } + + VectorMax3d min = vertices.colwise().minCoeff(); + VectorMax3d max = vertices.colwise().maxCoeff(); + + MatrixXd pts = MatrixXd::Random(100, 3); + + for (int d = 0; d < 3; ++d) { + pts.col(d).array() += 1; + pts.col(d) *= (max(d) - min(d)) / 2; + pts.col(d).array() += min(d); + } + + // MatrixXd pts(1, 3); + // pts << 0.2, -0.2, 0.4; + + SimpleBVH::BVH bvh; + bvh.init(vertices, facets, 1e-10); + + std::ofstream out("res.obj"); + + VectorMax3d nearest_point; + double sq_dist; + for (int i = 0; i < pts.rows(); ++i) { + bvh.nearest_facet(pts.row(i), nearest_point, sq_dist); + + out << "v " << pts(i, 0) << " " << pts(i, 1) << " " << pts(i, 2) + << "\n"; + out << "v " << nearest_point(0) << " " << nearest_point(1) << " " + << nearest_point(2) << "\n"; + out << "l " << 2 * i + 1 << " " << 2 * i + 2 << "\n\n"; + } + // std::ofstream out1("res1.obj"); + // for (int i = 0; i < vertices.rows(); ++i) { + // out1 << "v " << vertices(i, 0) << " " << vertices(i, 1) << " " + // << vertices(i, 2) << "\n"; + // } + // out1 << "f " << facets(66, 0) + 1 << " " << facets(66, 1) + 1 << " " + // << facets(66, 2) + 1 << "\n"; +}