editmesh_bvh.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2010 by Blender Foundation.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): Joseph Eagar
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file blender/blenkernel/intern/editmesh_bvh.c
  28. * \ingroup bke
  29. */
  30. #include "MEM_guardedalloc.h"
  31. #include "BLI_math.h"
  32. #include "BLI_kdopbvh.h"
  33. #include "BKE_editmesh.h"
  34. #include "BKE_editmesh_bvh.h" /* own include */
  35. struct BMBVHTree {
  36. BVHTree *tree;
  37. BMLoop *(*looptris)[3];
  38. int looptris_tot;
  39. BMesh *bm;
  40. const float (*cos_cage)[3];
  41. bool cos_cage_free;
  42. int flag;
  43. };
  44. BMBVHTree *BKE_bmbvh_new_from_editmesh(
  45. BMEditMesh *em, int flag,
  46. const float (*cos_cage)[3], const bool cos_cage_free)
  47. {
  48. return BKE_bmbvh_new(em->bm, em->looptris, em->tottri, flag, cos_cage, cos_cage_free);
  49. }
  50. BMBVHTree *BKE_bmbvh_new_ex(
  51. BMesh *bm, BMLoop *(*looptris)[3], int looptris_tot, int flag,
  52. const float (*cos_cage)[3], const bool cos_cage_free,
  53. bool (*test_fn)(BMFace *, void *user_data), void *user_data)
  54. {
  55. /* could become argument */
  56. const float epsilon = FLT_EPSILON * 2.0f;
  57. BMBVHTree *bmtree = MEM_callocN(sizeof(*bmtree), "BMBVHTree");
  58. float cos[3][3];
  59. int i;
  60. int tottri;
  61. /* avoid testing every tri */
  62. BMFace *f_test, *f_test_prev;
  63. bool test_fn_ret;
  64. /* BKE_editmesh_tessface_calc() must be called already */
  65. BLI_assert(looptris_tot != 0 || bm->totface == 0);
  66. if (cos_cage) {
  67. BM_mesh_elem_index_ensure(bm, BM_VERT);
  68. }
  69. bmtree->looptris = looptris;
  70. bmtree->looptris_tot = looptris_tot;
  71. bmtree->bm = bm;
  72. bmtree->cos_cage = cos_cage;
  73. bmtree->cos_cage_free = cos_cage_free;
  74. bmtree->flag = flag;
  75. if (test_fn) {
  76. /* callback must do... */
  77. BLI_assert(!(flag & (BMBVH_RESPECT_SELECT | BMBVH_RESPECT_HIDDEN)));
  78. f_test_prev = NULL;
  79. test_fn_ret = false;
  80. tottri = 0;
  81. for (i = 0; i < looptris_tot; i++) {
  82. f_test = looptris[i][0]->f;
  83. if (f_test != f_test_prev) {
  84. test_fn_ret = test_fn(f_test, user_data);
  85. f_test_prev = f_test;
  86. }
  87. if (test_fn_ret) {
  88. tottri++;
  89. }
  90. }
  91. }
  92. else {
  93. tottri = looptris_tot;
  94. }
  95. bmtree->tree = BLI_bvhtree_new(tottri, epsilon, 8, 8);
  96. f_test_prev = NULL;
  97. test_fn_ret = false;
  98. for (i = 0; i < looptris_tot; i++) {
  99. if (test_fn) {
  100. /* note, the arrays wont align now! take care */
  101. f_test = looptris[i][0]->f;
  102. if (f_test != f_test_prev) {
  103. test_fn_ret = test_fn(f_test, user_data);
  104. f_test_prev = f_test;
  105. }
  106. if (!test_fn_ret) {
  107. continue;
  108. }
  109. }
  110. if (cos_cage) {
  111. copy_v3_v3(cos[0], cos_cage[BM_elem_index_get(looptris[i][0]->v)]);
  112. copy_v3_v3(cos[1], cos_cage[BM_elem_index_get(looptris[i][1]->v)]);
  113. copy_v3_v3(cos[2], cos_cage[BM_elem_index_get(looptris[i][2]->v)]);
  114. }
  115. else {
  116. copy_v3_v3(cos[0], looptris[i][0]->v->co);
  117. copy_v3_v3(cos[1], looptris[i][1]->v->co);
  118. copy_v3_v3(cos[2], looptris[i][2]->v->co);
  119. }
  120. BLI_bvhtree_insert(bmtree->tree, i, (float *)cos, 3);
  121. }
  122. BLI_bvhtree_balance(bmtree->tree);
  123. return bmtree;
  124. }
  125. static bool bm_face_is_select(BMFace *f, void *UNUSED(user_data))
  126. {
  127. return (BM_elem_flag_test(f, BM_ELEM_SELECT) != 0);
  128. }
  129. static bool bm_face_is_not_hidden(BMFace *f, void *UNUSED(user_data))
  130. {
  131. return (BM_elem_flag_test(f, BM_ELEM_HIDDEN) == 0);
  132. }
  133. BMBVHTree *BKE_bmbvh_new(
  134. BMesh *bm, BMLoop *(*looptris)[3], int looptris_tot, int flag,
  135. const float (*cos_cage)[3], const bool cos_cage_free)
  136. {
  137. bool (*test_fn)(BMFace *, void *user_data);
  138. if (flag & BMBVH_RESPECT_SELECT) {
  139. test_fn = bm_face_is_select;
  140. }
  141. else if (flag & BMBVH_RESPECT_HIDDEN) {
  142. test_fn = bm_face_is_not_hidden;
  143. }
  144. else {
  145. test_fn = NULL;
  146. }
  147. flag &= ~(BMBVH_RESPECT_SELECT | BMBVH_RESPECT_HIDDEN);
  148. return BKE_bmbvh_new_ex(bm, looptris, looptris_tot, flag, cos_cage, cos_cage_free, test_fn, NULL);
  149. }
  150. void BKE_bmbvh_free(BMBVHTree *bmtree)
  151. {
  152. BLI_bvhtree_free(bmtree->tree);
  153. if (bmtree->cos_cage && bmtree->cos_cage_free) {
  154. MEM_freeN((void *)bmtree->cos_cage);
  155. }
  156. MEM_freeN(bmtree);
  157. }
  158. BVHTree *BKE_bmbvh_tree_get(BMBVHTree *bmtree)
  159. {
  160. return bmtree->tree;
  161. }
  162. /* -------------------------------------------------------------------- */
  163. /* Utility BMesh cast/intersect functions */
  164. /**
  165. * Return the coords from a triangle.
  166. */
  167. static void bmbvh_tri_from_face(const float *cos[3],
  168. const BMLoop **ltri, const float (*cos_cage)[3])
  169. {
  170. if (cos_cage == NULL) {
  171. cos[0] = ltri[0]->v->co;
  172. cos[1] = ltri[1]->v->co;
  173. cos[2] = ltri[2]->v->co;
  174. }
  175. else {
  176. cos[0] = cos_cage[BM_elem_index_get(ltri[0]->v)];
  177. cos[1] = cos_cage[BM_elem_index_get(ltri[1]->v)];
  178. cos[2] = cos_cage[BM_elem_index_get(ltri[2]->v)];
  179. }
  180. }
  181. /* taken from bvhutils.c */
  182. /* -------------------------------------------------------------------- */
  183. /* BKE_bmbvh_ray_cast */
  184. struct RayCastUserData {
  185. /* from the bmtree */
  186. const BMLoop *(*looptris)[3];
  187. const float (*cos_cage)[3];
  188. /* from the hit */
  189. float uv[2];
  190. };
  191. static BMFace *bmbvh_ray_cast_handle_hit(
  192. BMBVHTree *bmtree, struct RayCastUserData *bmcb_data, const BVHTreeRayHit *hit,
  193. float *r_dist, float r_hitout[3], float r_cagehit[3])
  194. {
  195. if (r_hitout) {
  196. if (bmtree->flag & BMBVH_RETURN_ORIG) {
  197. BMLoop **ltri = bmtree->looptris[hit->index];
  198. interp_v3_v3v3v3_uv(r_hitout, ltri[0]->v->co, ltri[1]->v->co, ltri[2]->v->co, bmcb_data->uv);
  199. }
  200. else {
  201. copy_v3_v3(r_hitout, hit->co);
  202. }
  203. if (r_cagehit) {
  204. copy_v3_v3(r_cagehit, hit->co);
  205. }
  206. }
  207. if (r_dist) {
  208. *r_dist = hit->dist;
  209. }
  210. return bmtree->looptris[hit->index][0]->f;
  211. }
  212. static void bmbvh_ray_cast_cb(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
  213. {
  214. struct RayCastUserData *bmcb_data = userdata;
  215. const BMLoop **ltri = bmcb_data->looptris[index];
  216. float dist, uv[2];
  217. const float *tri_cos[3];
  218. bool isect;
  219. bmbvh_tri_from_face(tri_cos, ltri, bmcb_data->cos_cage);
  220. isect = (ray->radius > 0.0f ?
  221. isect_ray_tri_epsilon_v3(ray->origin, ray->direction,
  222. tri_cos[0], tri_cos[1], tri_cos[2], &dist, uv, ray->radius) :
  223. #ifdef USE_KDOPBVH_WATERTIGHT
  224. isect_ray_tri_watertight_v3(ray->origin, ray->isect_precalc,
  225. tri_cos[0], tri_cos[1], tri_cos[2], &dist, uv));
  226. #else
  227. isect_ray_tri_v3(ray->origin, ray->direction,
  228. tri_cos[0], tri_cos[1], tri_cos[2], &dist, uv));
  229. #endif
  230. if (isect && dist < hit->dist) {
  231. hit->dist = dist;
  232. hit->index = index;
  233. copy_v3_v3(hit->no, ltri[0]->f->no);
  234. madd_v3_v3v3fl(hit->co, ray->origin, ray->direction, dist);
  235. copy_v2_v2(bmcb_data->uv, uv);
  236. }
  237. }
  238. BMFace *BKE_bmbvh_ray_cast(BMBVHTree *bmtree, const float co[3], const float dir[3], const float radius,
  239. float *r_dist, float r_hitout[3], float r_cagehit[3])
  240. {
  241. BVHTreeRayHit hit;
  242. struct RayCastUserData bmcb_data;
  243. const float dist = r_dist ? *r_dist : FLT_MAX;
  244. if (bmtree->cos_cage) BLI_assert(!(bmtree->bm->elem_index_dirty & BM_VERT));
  245. hit.dist = dist;
  246. hit.index = -1;
  247. /* ok to leave 'uv' uninitialized */
  248. bmcb_data.looptris = (const BMLoop *(*)[3])bmtree->looptris;
  249. bmcb_data.cos_cage = (const float (*)[3])bmtree->cos_cage;
  250. BLI_bvhtree_ray_cast(bmtree->tree, co, dir, radius, &hit, bmbvh_ray_cast_cb, &bmcb_data);
  251. if (hit.index != -1 && hit.dist != dist) {
  252. return bmbvh_ray_cast_handle_hit(bmtree, &bmcb_data, &hit, r_dist, r_hitout, r_cagehit);
  253. }
  254. return NULL;
  255. }
  256. /* -------------------------------------------------------------------- */
  257. /* bmbvh_ray_cast_cb_filter */
  258. /* Same as BKE_bmbvh_ray_cast but takes a callback to filter out faces.
  259. */
  260. struct RayCastUserData_Filter {
  261. struct RayCastUserData bmcb_data;
  262. BMBVHTree_FaceFilter filter_cb;
  263. void *filter_userdata;
  264. };
  265. static void bmbvh_ray_cast_cb_filter(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *hit)
  266. {
  267. struct RayCastUserData_Filter *bmcb_data_filter = userdata;
  268. struct RayCastUserData *bmcb_data = &bmcb_data_filter->bmcb_data;
  269. const BMLoop **ltri = bmcb_data->looptris[index];
  270. if (bmcb_data_filter->filter_cb(ltri[0]->f, bmcb_data_filter->filter_userdata)) {
  271. bmbvh_ray_cast_cb(bmcb_data, index, ray, hit);
  272. }
  273. }
  274. BMFace *BKE_bmbvh_ray_cast_filter(
  275. BMBVHTree *bmtree, const float co[3], const float dir[3], const float radius,
  276. float *r_dist, float r_hitout[3], float r_cagehit[3],
  277. BMBVHTree_FaceFilter filter_cb, void *filter_userdata)
  278. {
  279. BVHTreeRayHit hit;
  280. struct RayCastUserData_Filter bmcb_data_filter;
  281. struct RayCastUserData *bmcb_data = &bmcb_data_filter.bmcb_data;
  282. const float dist = r_dist ? *r_dist : FLT_MAX;
  283. bmcb_data_filter.filter_cb = filter_cb;
  284. bmcb_data_filter.filter_userdata = filter_userdata;
  285. if (bmtree->cos_cage) BLI_assert(!(bmtree->bm->elem_index_dirty & BM_VERT));
  286. hit.dist = dist;
  287. hit.index = -1;
  288. /* ok to leave 'uv' uninitialized */
  289. bmcb_data->looptris = (const BMLoop *(*)[3])bmtree->looptris;
  290. bmcb_data->cos_cage = (const float (*)[3])bmtree->cos_cage;
  291. BLI_bvhtree_ray_cast(bmtree->tree, co, dir, radius, &hit, bmbvh_ray_cast_cb_filter, &bmcb_data_filter);
  292. if (hit.index != -1 && hit.dist != dist) {
  293. return bmbvh_ray_cast_handle_hit(bmtree, bmcb_data, &hit, r_dist, r_hitout, r_cagehit);
  294. }
  295. return NULL;
  296. }
  297. /* -------------------------------------------------------------------- */
  298. /* BKE_bmbvh_find_vert_closest */
  299. struct VertSearchUserData {
  300. /* from the bmtree */
  301. const BMLoop *(*looptris)[3];
  302. const float (*cos_cage)[3];
  303. /* from the hit */
  304. float dist_max_sq;
  305. int index_tri;
  306. };
  307. static void bmbvh_find_vert_closest_cb(void *userdata, int index, const float co[3], BVHTreeNearest *hit)
  308. {
  309. struct VertSearchUserData *bmcb_data = userdata;
  310. const BMLoop **ltri = bmcb_data->looptris[index];
  311. const float dist_max_sq = bmcb_data->dist_max_sq;
  312. int i;
  313. const float *tri_cos[3];
  314. bmbvh_tri_from_face(tri_cos, ltri, bmcb_data->cos_cage);
  315. for (i = 0; i < 3; i++) {
  316. const float dist_sq = len_squared_v3v3(co, tri_cos[i]);
  317. if (dist_sq < hit->dist_sq && dist_sq < dist_max_sq) {
  318. copy_v3_v3(hit->co, tri_cos[i]);
  319. /* XXX, normal ignores cage */
  320. copy_v3_v3(hit->no, ltri[i]->v->no);
  321. hit->dist_sq = dist_sq;
  322. hit->index = index;
  323. bmcb_data->index_tri = i;
  324. }
  325. }
  326. }
  327. BMVert *BKE_bmbvh_find_vert_closest(BMBVHTree *bmtree, const float co[3], const float dist_max)
  328. {
  329. BVHTreeNearest hit;
  330. struct VertSearchUserData bmcb_data;
  331. const float dist_max_sq = dist_max * dist_max;
  332. if (bmtree->cos_cage) BLI_assert(!(bmtree->bm->elem_index_dirty & BM_VERT));
  333. hit.dist_sq = dist_max_sq;
  334. hit.index = -1;
  335. bmcb_data.looptris = (const BMLoop *(*)[3])bmtree->looptris;
  336. bmcb_data.cos_cage = (const float (*)[3])bmtree->cos_cage;
  337. bmcb_data.dist_max_sq = dist_max_sq;
  338. BLI_bvhtree_find_nearest(bmtree->tree, co, &hit, bmbvh_find_vert_closest_cb, &bmcb_data);
  339. if (hit.index != -1) {
  340. BMLoop **ltri = bmtree->looptris[hit.index];
  341. return ltri[bmcb_data.index_tri]->v;
  342. }
  343. return NULL;
  344. }
  345. struct FaceSearchUserData {
  346. /* from the bmtree */
  347. const BMLoop *(*looptris)[3];
  348. const float (*cos_cage)[3];
  349. /* from the hit */
  350. float dist_max_sq;
  351. };
  352. static void bmbvh_find_face_closest_cb(void *userdata, int index, const float co[3], BVHTreeNearest *hit)
  353. {
  354. struct FaceSearchUserData *bmcb_data = userdata;
  355. const BMLoop **ltri = bmcb_data->looptris[index];
  356. const float dist_max_sq = bmcb_data->dist_max_sq;
  357. const float *tri_cos[3];
  358. bmbvh_tri_from_face(tri_cos, ltri, bmcb_data->cos_cage);
  359. float co_close[3];
  360. closest_on_tri_to_point_v3(co_close, co, UNPACK3(tri_cos));
  361. const float dist_sq = len_squared_v3v3(co, co_close);
  362. if (dist_sq < hit->dist_sq && dist_sq < dist_max_sq) {
  363. /* XXX, normal ignores cage */
  364. copy_v3_v3(hit->no, ltri[0]->f->no);
  365. hit->dist_sq = dist_sq;
  366. hit->index = index;
  367. }
  368. }
  369. struct BMFace *BKE_bmbvh_find_face_closest(BMBVHTree *bmtree, const float co[3], const float dist_max)
  370. {
  371. BVHTreeNearest hit;
  372. struct FaceSearchUserData bmcb_data;
  373. const float dist_max_sq = dist_max * dist_max;
  374. if (bmtree->cos_cage) BLI_assert(!(bmtree->bm->elem_index_dirty & BM_VERT));
  375. hit.dist_sq = dist_max_sq;
  376. hit.index = -1;
  377. bmcb_data.looptris = (const BMLoop *(*)[3])bmtree->looptris;
  378. bmcb_data.cos_cage = (const float (*)[3])bmtree->cos_cage;
  379. bmcb_data.dist_max_sq = dist_max_sq;
  380. BLI_bvhtree_find_nearest(bmtree->tree, co, &hit, bmbvh_find_face_closest_cb, &bmcb_data);
  381. if (hit.index != -1) {
  382. BMLoop **ltri = bmtree->looptris[hit.index];
  383. return ltri[0]->f;
  384. }
  385. return NULL;
  386. }
  387. /* -------------------------------------------------------------------- */
  388. /* BKE_bmbvh_overlap */
  389. struct BMBVHTree_OverlapData {
  390. const BMBVHTree *tree_pair[2];
  391. float epsilon;
  392. };
  393. static bool bmbvh_overlap_cb(void *userdata, int index_a, int index_b, int UNUSED(thread))
  394. {
  395. struct BMBVHTree_OverlapData *data = userdata;
  396. const BMBVHTree *bmtree_a = data->tree_pair[0];
  397. const BMBVHTree *bmtree_b = data->tree_pair[1];
  398. BMLoop **tri_a = bmtree_a->looptris[index_a];
  399. BMLoop **tri_b = bmtree_b->looptris[index_b];
  400. const float *tri_a_co[3] = {tri_a[0]->v->co, tri_a[1]->v->co, tri_a[2]->v->co};
  401. const float *tri_b_co[3] = {tri_b[0]->v->co, tri_b[1]->v->co, tri_b[2]->v->co};
  402. float ix_pair[2][3];
  403. int verts_shared = 0;
  404. if (bmtree_a->looptris == bmtree_b->looptris) {
  405. if (UNLIKELY(tri_a[0]->f == tri_b[0]->f)) {
  406. return false;
  407. }
  408. verts_shared = (
  409. ELEM(tri_a_co[0], UNPACK3(tri_b_co)) +
  410. ELEM(tri_a_co[1], UNPACK3(tri_b_co)) +
  411. ELEM(tri_a_co[2], UNPACK3(tri_b_co)));
  412. /* if 2 points are shared, bail out */
  413. if (verts_shared >= 2) {
  414. return false;
  415. }
  416. }
  417. return (isect_tri_tri_epsilon_v3(UNPACK3(tri_a_co), UNPACK3(tri_b_co), ix_pair[0], ix_pair[1], data->epsilon) &&
  418. /* if we share a vertex, check the intersection isn't a 'point' */
  419. ((verts_shared == 0) || (len_squared_v3v3(ix_pair[0], ix_pair[1]) > data->epsilon)));
  420. }
  421. /**
  422. * Overlap indices reference the looptri's
  423. */
  424. BVHTreeOverlap *BKE_bmbvh_overlap(const BMBVHTree *bmtree_a, const BMBVHTree *bmtree_b, unsigned int *r_overlap_tot)
  425. {
  426. struct BMBVHTree_OverlapData data;
  427. data.tree_pair[0] = bmtree_a;
  428. data.tree_pair[1] = bmtree_b;
  429. data.epsilon = max_ff(BLI_bvhtree_get_epsilon(bmtree_a->tree), BLI_bvhtree_get_epsilon(bmtree_b->tree));
  430. return BLI_bvhtree_overlap(bmtree_a->tree, bmtree_b->tree, r_overlap_tot, bmbvh_overlap_cb, &data);
  431. }