bmo_similar.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. * Contributor(s): Joseph Eagar, Campbell Barton
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_similar.c
  23. * \ingroup bmesh
  24. *
  25. * bmesh operators to select based on
  26. * comparisons with the existing selection.
  27. */
  28. #include "MEM_guardedalloc.h"
  29. #include "DNA_object_types.h"
  30. #include "DNA_meshdata_types.h"
  31. #include "BLI_math.h"
  32. #include "BKE_customdata.h"
  33. #include "BKE_deform.h"
  34. #include "bmesh.h"
  35. #include "intern/bmesh_operators_private.h" /* own include */
  36. /* in fact these could all be the same */
  37. /*
  38. * extra face data (computed data)
  39. */
  40. typedef struct SimSel_FaceExt {
  41. BMFace *f; /* the face */
  42. float c[3]; /* center */
  43. union {
  44. float area; /* area */
  45. float perim; /* perimeter */
  46. float d; /* 4th component of plane (the first three being the normal) */
  47. struct Image *t; /* image pointer */
  48. };
  49. } SimSel_FaceExt;
  50. static int bm_sel_similar_cmp_fl(const float delta, const float thresh, const int compare)
  51. {
  52. switch (compare) {
  53. case SIM_CMP_EQ:
  54. return (fabsf(delta) <= thresh);
  55. case SIM_CMP_GT:
  56. return ((delta + thresh) >= 0.0f);
  57. case SIM_CMP_LT:
  58. return ((delta - thresh) <= 0.0f);
  59. default:
  60. BLI_assert(0);
  61. return 0;
  62. }
  63. }
  64. static int bm_sel_similar_cmp_i(const int delta, const int compare)
  65. {
  66. switch (compare) {
  67. case SIM_CMP_EQ:
  68. return (delta == 0);
  69. case SIM_CMP_GT:
  70. return (delta > 0);
  71. case SIM_CMP_LT:
  72. return (delta < 0);
  73. default:
  74. BLI_assert(0);
  75. return 0;
  76. }
  77. }
  78. /*
  79. * Select similar faces, the choices are in the enum in source/blender/bmesh/bmesh_operators.h
  80. * We select either similar faces based on material, image, area, perimeter, normal, or the coplanar faces
  81. */
  82. void bmo_similar_faces_exec(BMesh *bm, BMOperator *op)
  83. {
  84. #define FACE_MARK 1
  85. BMIter fm_iter;
  86. BMFace *fs, *fm;
  87. BMOIter fs_iter;
  88. int num_sels = 0, num_total = 0, i = 0, idx = 0;
  89. float angle = 0.0f;
  90. SimSel_FaceExt *f_ext = NULL;
  91. int *indices = NULL;
  92. const int type = BMO_slot_int_get(op->slots_in, "type");
  93. const float thresh = BMO_slot_float_get(op->slots_in, "thresh");
  94. const float thresh_radians = thresh * (float)M_PI;
  95. const int compare = BMO_slot_int_get(op->slots_in, "compare");
  96. /* initial_elem - other_elem */
  97. float delta_fl;
  98. int delta_i;
  99. num_total = BM_mesh_elem_count(bm, BM_FACE);
  100. /*
  101. * The first thing to do is to iterate through all the selected items and mark them since
  102. * they will be in the selection anyway.
  103. * This will increase performance, (especially when the number of originally selected faces is high)
  104. * so the overall complexity will be less than $O(mn)$ where is the total number of selected faces,
  105. * and n is the total number of faces
  106. */
  107. BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
  108. if (!BMO_face_flag_test(bm, fs, FACE_MARK)) { /* is this really needed ? */
  109. BMO_face_flag_enable(bm, fs, FACE_MARK);
  110. num_sels++;
  111. }
  112. }
  113. /* allocate memory for the selected faces indices and for all temporary faces */
  114. indices = (int *)MEM_callocN(sizeof(int) * num_sels, "face indices util.c");
  115. f_ext = (SimSel_FaceExt *)MEM_callocN(sizeof(SimSel_FaceExt) * num_total, "f_ext util.c");
  116. /* loop through all the faces and fill the faces/indices structure */
  117. BM_ITER_MESH (fm, &fm_iter, bm, BM_FACES_OF_MESH) {
  118. f_ext[i].f = fm;
  119. if (BMO_face_flag_test(bm, fm, FACE_MARK)) {
  120. indices[idx] = i;
  121. idx++;
  122. }
  123. i++;
  124. }
  125. /*
  126. * Save us some computation burden: In case of perimeter/area/coplanar selection we compute
  127. * only once.
  128. */
  129. if (type == SIMFACE_PERIMETER || type == SIMFACE_AREA || type == SIMFACE_COPLANAR || type == SIMFACE_IMAGE) {
  130. for (i = 0; i < num_total; i++) {
  131. switch (type) {
  132. case SIMFACE_PERIMETER:
  133. /* set the perimeter */
  134. f_ext[i].perim = BM_face_calc_perimeter(f_ext[i].f);
  135. break;
  136. case SIMFACE_COPLANAR:
  137. /* compute the center of the polygon */
  138. BM_face_calc_center_mean(f_ext[i].f, f_ext[i].c);
  139. /* compute the plane distance */
  140. f_ext[i].d = dot_v3v3(f_ext[i].f->no, f_ext[i].c);
  141. break;
  142. case SIMFACE_AREA:
  143. f_ext[i].area = BM_face_calc_area(f_ext[i].f);
  144. break;
  145. case SIMFACE_IMAGE:
  146. f_ext[i].t = NULL;
  147. if (CustomData_has_layer(&(bm->pdata), CD_MTEXPOLY)) {
  148. MTexPoly *mtpoly = CustomData_bmesh_get(&bm->pdata, f_ext[i].f->head.data, CD_MTEXPOLY);
  149. f_ext[i].t = mtpoly->tpage;
  150. }
  151. break;
  152. }
  153. }
  154. }
  155. /* now select the rest (if any) */
  156. for (i = 0; i < num_total; i++) {
  157. fm = f_ext[i].f;
  158. if (!BMO_face_flag_test(bm, fm, FACE_MARK) && !BM_elem_flag_test(fm, BM_ELEM_HIDDEN)) {
  159. bool cont = true;
  160. for (idx = 0; idx < num_sels && cont == true; idx++) {
  161. fs = f_ext[indices[idx]].f;
  162. switch (type) {
  163. case SIMFACE_MATERIAL:
  164. if (fm->mat_nr == fs->mat_nr) {
  165. BMO_face_flag_enable(bm, fm, FACE_MARK);
  166. cont = false;
  167. }
  168. break;
  169. case SIMFACE_IMAGE:
  170. if (f_ext[i].t == f_ext[indices[idx]].t) {
  171. BMO_face_flag_enable(bm, fm, FACE_MARK);
  172. cont = false;
  173. }
  174. break;
  175. case SIMFACE_NORMAL:
  176. angle = angle_normalized_v3v3(fs->no, fm->no); /* if the angle between the normals -> 0 */
  177. if (angle <= thresh_radians) {
  178. BMO_face_flag_enable(bm, fm, FACE_MARK);
  179. cont = false;
  180. }
  181. break;
  182. case SIMFACE_COPLANAR:
  183. {
  184. float sign = 1.0f;
  185. angle = angle_normalized_v3v3(fs->no, fm->no); /* angle -> 0 */
  186. /* allow for normal pointing in either direction (just check the plane) */
  187. if (angle > (float)M_PI * 0.5f) {
  188. angle = (float)M_PI - angle;
  189. sign = -1.0f;
  190. }
  191. if (angle <= thresh_radians) { /* and dot product difference -> 0 */
  192. delta_fl = f_ext[i].d - (f_ext[indices[idx]].d * sign);
  193. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  194. BMO_face_flag_enable(bm, fm, FACE_MARK);
  195. cont = false;
  196. }
  197. }
  198. break;
  199. }
  200. case SIMFACE_AREA:
  201. delta_fl = f_ext[i].area - f_ext[indices[idx]].area;
  202. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  203. BMO_face_flag_enable(bm, fm, FACE_MARK);
  204. cont = false;
  205. }
  206. break;
  207. case SIMFACE_SIDES:
  208. delta_i = fm->len - fs->len;
  209. if (bm_sel_similar_cmp_i(delta_i, compare)) {
  210. BMO_face_flag_enable(bm, fm, FACE_MARK);
  211. cont = false;
  212. }
  213. break;
  214. case SIMFACE_PERIMETER:
  215. delta_fl = f_ext[i].perim - f_ext[indices[idx]].perim;
  216. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  217. BMO_face_flag_enable(bm, fm, FACE_MARK);
  218. cont = false;
  219. }
  220. break;
  221. case SIMFACE_SMOOTH:
  222. if (BM_elem_flag_test(fm, BM_ELEM_SMOOTH) == BM_elem_flag_test(fs, BM_ELEM_SMOOTH)) {
  223. BMO_face_flag_enable(bm, fm, FACE_MARK);
  224. cont = false;
  225. }
  226. break;
  227. #ifdef WITH_FREESTYLE
  228. case SIMFACE_FREESTYLE:
  229. if (CustomData_has_layer(&bm->pdata, CD_FREESTYLE_FACE)) {
  230. FreestyleEdge *ffa1, *ffa2;
  231. ffa1 = CustomData_bmesh_get(&bm->pdata, fs->head.data, CD_FREESTYLE_FACE);
  232. ffa2 = CustomData_bmesh_get(&bm->pdata, fm->head.data, CD_FREESTYLE_FACE);
  233. if (ffa1 && ffa2 && (ffa1->flag & FREESTYLE_FACE_MARK) == (ffa2->flag & FREESTYLE_FACE_MARK)) {
  234. BMO_face_flag_enable(bm, fm, FACE_MARK);
  235. cont = false;
  236. }
  237. }
  238. break;
  239. #endif
  240. default:
  241. BLI_assert(0);
  242. break;
  243. }
  244. }
  245. }
  246. }
  247. MEM_freeN(f_ext);
  248. MEM_freeN(indices);
  249. /* transfer all marked faces to the output slot */
  250. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "faces.out", BM_FACE, FACE_MARK);
  251. #undef FACE_MARK
  252. }
  253. /**************************************************************************** *
  254. * Similar Edges
  255. **************************************************************************** */
  256. /*
  257. * extra edge information
  258. */
  259. typedef struct SimSel_EdgeExt {
  260. BMEdge *e;
  261. union {
  262. float dir[3];
  263. float angle; /* angle between the face */
  264. };
  265. union {
  266. float length; /* edge length */
  267. int faces; /* faces count */
  268. };
  269. } SimSel_EdgeExt;
  270. /*
  271. * select similar edges: the choices are in the enum in source/blender/bmesh/bmesh_operators.h
  272. * choices are length, direction, face, ...
  273. */
  274. void bmo_similar_edges_exec(BMesh *bm, BMOperator *op)
  275. {
  276. #define EDGE_MARK 1
  277. BMOIter es_iter; /* selected edges iterator */
  278. BMIter e_iter; /* mesh edges iterator */
  279. BMEdge *es; /* selected edge */
  280. BMEdge *e; /* mesh edge */
  281. int idx = 0, i = 0 /* , f = 0 */;
  282. int *indices = NULL;
  283. SimSel_EdgeExt *e_ext = NULL;
  284. // float *angles = NULL;
  285. float angle;
  286. int num_sels = 0, num_total = 0;
  287. const int type = BMO_slot_int_get(op->slots_in, "type");
  288. const float thresh = BMO_slot_float_get(op->slots_in, "thresh");
  289. const int compare = BMO_slot_int_get(op->slots_in, "compare");
  290. /* initial_elem - other_elem */
  291. float delta_fl;
  292. int delta_i;
  293. /* sanity checks that the data we need is available */
  294. switch (type) {
  295. case SIMEDGE_CREASE:
  296. if (!CustomData_has_layer(&bm->edata, CD_CREASE)) {
  297. return;
  298. }
  299. break;
  300. case SIMEDGE_BEVEL:
  301. if (!CustomData_has_layer(&bm->edata, CD_BWEIGHT)) {
  302. return;
  303. }
  304. break;
  305. }
  306. num_total = BM_mesh_elem_count(bm, BM_EDGE);
  307. /* iterate through all selected edges and mark them */
  308. BMO_ITER (es, &es_iter, op->slots_in, "edges", BM_EDGE) {
  309. BMO_edge_flag_enable(bm, es, EDGE_MARK);
  310. num_sels++;
  311. }
  312. /* allocate memory for the selected edges indices and for all temporary edges */
  313. indices = (int *)MEM_callocN(sizeof(int) * num_sels, __func__);
  314. e_ext = (SimSel_EdgeExt *)MEM_callocN(sizeof(SimSel_EdgeExt) * num_total, __func__);
  315. /* loop through all the edges and fill the edges/indices structure */
  316. BM_ITER_MESH (e, &e_iter, bm, BM_EDGES_OF_MESH) {
  317. e_ext[i].e = e;
  318. if (BMO_edge_flag_test(bm, e, EDGE_MARK)) {
  319. indices[idx] = i;
  320. idx++;
  321. }
  322. i++;
  323. }
  324. /* save us some computation time by doing heavy computation once */
  325. if (type == SIMEDGE_LENGTH || type == SIMEDGE_FACE || type == SIMEDGE_DIR || type == SIMEDGE_FACE_ANGLE) {
  326. for (i = 0; i < num_total; i++) {
  327. switch (type) {
  328. case SIMEDGE_LENGTH: /* compute the length of the edge */
  329. e_ext[i].length = len_v3v3(e_ext[i].e->v1->co, e_ext[i].e->v2->co);
  330. break;
  331. case SIMEDGE_DIR: /* compute the direction */
  332. sub_v3_v3v3(e_ext[i].dir, e_ext[i].e->v1->co, e_ext[i].e->v2->co);
  333. normalize_v3(e_ext[i].dir);
  334. break;
  335. case SIMEDGE_FACE: /* count the faces around the edge */
  336. e_ext[i].faces = BM_edge_face_count(e_ext[i].e);
  337. break;
  338. case SIMEDGE_FACE_ANGLE:
  339. e_ext[i].faces = BM_edge_face_count(e_ext[i].e);
  340. if (e_ext[i].faces == 2)
  341. e_ext[i].angle = BM_edge_calc_face_angle(e_ext[i].e);
  342. break;
  343. }
  344. }
  345. }
  346. /* select the edges if any */
  347. for (i = 0; i < num_total; i++) {
  348. e = e_ext[i].e;
  349. if (!BMO_edge_flag_test(bm, e, EDGE_MARK) &&
  350. !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
  351. {
  352. bool cont = true;
  353. for (idx = 0; idx < num_sels && cont == true; idx++) {
  354. es = e_ext[indices[idx]].e;
  355. switch (type) {
  356. case SIMEDGE_LENGTH:
  357. delta_fl = e_ext[i].length - e_ext[indices[idx]].length;
  358. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  359. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  360. cont = false;
  361. }
  362. break;
  363. case SIMEDGE_DIR:
  364. /* compute the angle between the two edges */
  365. angle = angle_normalized_v3v3(e_ext[i].dir, e_ext[indices[idx]].dir);
  366. if (angle > (float)M_PI_2) /* use the smallest angle between the edges */
  367. angle = fabsf(angle - (float)M_PI);
  368. if (angle / (float)M_PI_2 <= thresh) {
  369. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  370. cont = false;
  371. }
  372. break;
  373. case SIMEDGE_FACE:
  374. delta_i = e_ext[i].faces - e_ext[indices[idx]].faces;
  375. if (bm_sel_similar_cmp_i(delta_i, compare)) {
  376. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  377. cont = false;
  378. }
  379. break;
  380. case SIMEDGE_FACE_ANGLE:
  381. if (e_ext[i].faces == 2) {
  382. if (e_ext[indices[idx]].faces == 2) {
  383. if (fabsf(e_ext[i].angle - e_ext[indices[idx]].angle) <= thresh) {
  384. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  385. cont = false;
  386. }
  387. }
  388. }
  389. else {
  390. cont = false;
  391. }
  392. break;
  393. case SIMEDGE_CREASE:
  394. {
  395. const float *c1, *c2;
  396. c1 = CustomData_bmesh_get(&bm->edata, e->head.data, CD_CREASE);
  397. c2 = CustomData_bmesh_get(&bm->edata, es->head.data, CD_CREASE);
  398. delta_fl = *c1 - *c2;
  399. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  400. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  401. cont = false;
  402. }
  403. }
  404. break;
  405. case SIMEDGE_BEVEL:
  406. {
  407. const float *c1, *c2;
  408. c1 = CustomData_bmesh_get(&bm->edata, e->head.data, CD_BWEIGHT);
  409. c2 = CustomData_bmesh_get(&bm->edata, es->head.data, CD_BWEIGHT);
  410. delta_fl = *c1 - *c2;
  411. if (bm_sel_similar_cmp_fl(delta_fl, thresh, compare)) {
  412. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  413. cont = false;
  414. }
  415. }
  416. break;
  417. case SIMEDGE_SEAM:
  418. if (BM_elem_flag_test(e, BM_ELEM_SEAM) == BM_elem_flag_test(es, BM_ELEM_SEAM)) {
  419. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  420. cont = false;
  421. }
  422. break;
  423. case SIMEDGE_SHARP:
  424. if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) == BM_elem_flag_test(es, BM_ELEM_SMOOTH)) {
  425. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  426. cont = false;
  427. }
  428. break;
  429. #ifdef WITH_FREESTYLE
  430. case SIMEDGE_FREESTYLE:
  431. if (CustomData_has_layer(&bm->edata, CD_FREESTYLE_EDGE)) {
  432. FreestyleEdge *fed1, *fed2;
  433. fed1 = CustomData_bmesh_get(&bm->edata, e->head.data, CD_FREESTYLE_EDGE);
  434. fed2 = CustomData_bmesh_get(&bm->edata, es->head.data, CD_FREESTYLE_EDGE);
  435. if (fed1 && fed2 && (fed1->flag & FREESTYLE_EDGE_MARK) == (fed2->flag & FREESTYLE_EDGE_MARK)) {
  436. BMO_edge_flag_enable(bm, e, EDGE_MARK);
  437. cont = false;
  438. }
  439. }
  440. break;
  441. #endif
  442. default:
  443. BLI_assert(0);
  444. break;
  445. }
  446. }
  447. }
  448. }
  449. MEM_freeN(e_ext);
  450. MEM_freeN(indices);
  451. /* transfer all marked edges to the output slot */
  452. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_MARK);
  453. #undef EDGE_MARK
  454. }
  455. /**************************************************************************** *
  456. * Similar Vertices
  457. **************************************************************************** */
  458. typedef struct SimSel_VertExt {
  459. BMVert *v;
  460. union {
  461. int num_faces; /* adjacent faces */
  462. int num_edges; /* adjacent edges */
  463. MDeformVert *dvert; /* deform vertex */
  464. };
  465. } SimSel_VertExt;
  466. /*
  467. * select similar vertices: the choices are in the enum in source/blender/bmesh/bmesh_operators.h
  468. * choices are normal, face, vertex group...
  469. */
  470. void bmo_similar_verts_exec(BMesh *bm, BMOperator *op)
  471. {
  472. #define VERT_MARK 1
  473. const int cd_dvert_offset = CustomData_get_offset(&bm->vdata, CD_MDEFORMVERT);
  474. BMOIter vs_iter; /* selected verts iterator */
  475. BMIter v_iter; /* mesh verts iterator */
  476. BMVert *vs; /* selected vertex */
  477. BMVert *v; /* mesh vertex */
  478. SimSel_VertExt *v_ext = NULL;
  479. int *indices = NULL;
  480. int num_total = 0, num_sels = 0, i = 0, idx = 0;
  481. const int type = BMO_slot_int_get(op->slots_in, "type");
  482. const float thresh = BMO_slot_float_get(op->slots_in, "thresh");
  483. const float thresh_radians = thresh * (float)M_PI;
  484. const int compare = BMO_slot_int_get(op->slots_in, "compare");
  485. /* initial_elem - other_elem */
  486. // float delta_fl;
  487. int delta_i;
  488. num_total = BM_mesh_elem_count(bm, BM_VERT);
  489. /* iterate through all selected edges and mark them */
  490. BMO_ITER (vs, &vs_iter, op->slots_in, "verts", BM_VERT) {
  491. BMO_vert_flag_enable(bm, vs, VERT_MARK);
  492. num_sels++;
  493. }
  494. /* allocate memory for the selected vertices indices and for all temporary vertices */
  495. indices = (int *)MEM_mallocN(sizeof(int) * num_sels, "vertex indices");
  496. v_ext = (SimSel_VertExt *)MEM_mallocN(sizeof(SimSel_VertExt) * num_total, "vertex extra");
  497. /* loop through all the vertices and fill the vertices/indices structure */
  498. BM_ITER_MESH (v, &v_iter, bm, BM_VERTS_OF_MESH) {
  499. v_ext[i].v = v;
  500. if (BMO_vert_flag_test(bm, v, VERT_MARK)) {
  501. indices[idx] = i;
  502. idx++;
  503. }
  504. switch (type) {
  505. case SIMVERT_FACE:
  506. /* calling BM_vert_face_count every time is time consumming, so call it only once per vertex */
  507. v_ext[i].num_faces = BM_vert_face_count(v);
  508. break;
  509. case SIMVERT_VGROUP:
  510. v_ext[i].dvert = (cd_dvert_offset != -1) ? BM_ELEM_CD_GET_VOID_P(v_ext[i].v, cd_dvert_offset) : NULL;
  511. break;
  512. case SIMVERT_EDGE:
  513. v_ext[i].num_edges = BM_vert_edge_count(v);
  514. break;
  515. }
  516. i++;
  517. }
  518. /* select the vertices if any */
  519. for (i = 0; i < num_total; i++) {
  520. v = v_ext[i].v;
  521. if (!BMO_vert_flag_test(bm, v, VERT_MARK) &&
  522. !BM_elem_flag_test(v, BM_ELEM_HIDDEN))
  523. {
  524. bool cont = true;
  525. for (idx = 0; idx < num_sels && cont == true; idx++) {
  526. vs = v_ext[indices[idx]].v;
  527. switch (type) {
  528. case SIMVERT_NORMAL:
  529. /* compare the angle between the normals */
  530. if (angle_normalized_v3v3(v->no, vs->no) <= thresh_radians) {
  531. BMO_vert_flag_enable(bm, v, VERT_MARK);
  532. cont = false;
  533. }
  534. break;
  535. case SIMVERT_FACE:
  536. /* number of adjacent faces */
  537. delta_i = v_ext[i].num_faces - v_ext[indices[idx]].num_faces;
  538. if (bm_sel_similar_cmp_i(delta_i, compare)) {
  539. BMO_vert_flag_enable(bm, v, VERT_MARK);
  540. cont = false;
  541. }
  542. break;
  543. case SIMVERT_VGROUP:
  544. if (v_ext[i].dvert != NULL && v_ext[indices[idx]].dvert != NULL) {
  545. if (defvert_find_shared(v_ext[i].dvert, v_ext[indices[idx]].dvert) != -1) {
  546. BMO_vert_flag_enable(bm, v, VERT_MARK);
  547. cont = false;
  548. }
  549. }
  550. break;
  551. case SIMVERT_EDGE:
  552. /* number of adjacent edges */
  553. delta_i = v_ext[i].num_edges - v_ext[indices[idx]].num_edges;
  554. if (bm_sel_similar_cmp_i(delta_i, compare)) {
  555. BMO_vert_flag_enable(bm, v, VERT_MARK);
  556. cont = false;
  557. }
  558. break;
  559. default:
  560. BLI_assert(0);
  561. break;
  562. }
  563. }
  564. }
  565. }
  566. MEM_freeN(indices);
  567. MEM_freeN(v_ext);
  568. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "verts.out", BM_VERT, VERT_MARK);
  569. #undef VERT_MARK
  570. }