bmo_utils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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.
  19. *
  20. * ***** END GPL LICENSE BLOCK *****
  21. */
  22. /** \file blender/bmesh/operators/bmo_utils.c
  23. * \ingroup bmesh
  24. *
  25. * utility bmesh operators, e.g. transform,
  26. * translate, rotate, scale, etc.
  27. */
  28. #include "MEM_guardedalloc.h"
  29. #include "DNA_meshdata_types.h"
  30. #include "BLI_math.h"
  31. #include "BLI_alloca.h"
  32. #include "BKE_customdata.h"
  33. #include "bmesh.h"
  34. #include "intern/bmesh_operators_private.h" /* own include */
  35. #define ELE_NEW 1
  36. void bmo_create_vert_exec(BMesh *bm, BMOperator *op)
  37. {
  38. float vec[3];
  39. BMO_slot_vec_get(op->slots_in, "co", vec);
  40. BMO_vert_flag_enable(bm, BM_vert_create(bm, vec, NULL, BM_CREATE_NOP), ELE_NEW);
  41. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "vert.out", BM_VERT, ELE_NEW);
  42. }
  43. void bmo_transform_exec(BMesh *UNUSED(bm), BMOperator *op)
  44. {
  45. BMOIter iter;
  46. BMVert *v;
  47. float mat[4][4], mat_space[4][4], imat_space[4][4];
  48. BMO_slot_mat4_get(op->slots_in, "matrix", mat);
  49. BMO_slot_mat4_get(op->slots_in, "space", mat_space);
  50. if (!is_zero_m4(mat_space)) {
  51. invert_m4_m4(imat_space, mat_space);
  52. mul_m4_series(mat, imat_space, mat, mat_space);
  53. }
  54. BMO_ITER (v, &iter, op->slots_in, "verts", BM_VERT) {
  55. mul_m4_v3(mat, v->co);
  56. }
  57. }
  58. void bmo_translate_exec(BMesh *bm, BMOperator *op)
  59. {
  60. float mat[4][4], vec[3];
  61. BMO_slot_vec_get(op->slots_in, "vec", vec);
  62. unit_m4(mat);
  63. copy_v3_v3(mat[3], vec);
  64. BMO_op_callf(bm, op->flag, "transform matrix=%m4 space=%s verts=%s", mat, op, "space", op, "verts");
  65. }
  66. void bmo_scale_exec(BMesh *bm, BMOperator *op)
  67. {
  68. float mat[3][3], vec[3];
  69. BMO_slot_vec_get(op->slots_in, "vec", vec);
  70. unit_m3(mat);
  71. mat[0][0] = vec[0];
  72. mat[1][1] = vec[1];
  73. mat[2][2] = vec[2];
  74. BMO_op_callf(bm, op->flag, "transform matrix=%m3 space=%s verts=%s", mat, op, "space", op, "verts");
  75. }
  76. void bmo_rotate_exec(BMesh *bm, BMOperator *op)
  77. {
  78. float center[3];
  79. float mat[4][4];
  80. BMO_slot_vec_get(op->slots_in, "cent", center);
  81. BMO_slot_mat4_get(op->slots_in, "matrix", mat);
  82. transform_pivot_set_m4(mat, center);
  83. BMO_op_callf(bm, op->flag, "transform matrix=%m4 space=%s verts=%s", mat, op, "space", op, "verts");
  84. }
  85. void bmo_reverse_faces_exec(BMesh *bm, BMOperator *op)
  86. {
  87. const int cd_loop_mdisp_offset = CustomData_get_offset(&bm->ldata, CD_MDISPS);
  88. const bool use_loop_mdisp_flip = BMO_slot_bool_get(op->slots_in, "flip_multires");
  89. BMOIter siter;
  90. BMFace *f;
  91. BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
  92. BM_face_normal_flip_ex(bm, f, cd_loop_mdisp_offset, use_loop_mdisp_flip);
  93. }
  94. }
  95. void bmo_rotate_edges_exec(BMesh *bm, BMOperator *op)
  96. {
  97. BMOIter siter;
  98. BMEdge *e, *e2;
  99. const bool use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
  100. const bool is_single = BMO_slot_buffer_count(op->slots_in, "edges") == 1;
  101. short check_flag = is_single ?
  102. BM_EDGEROT_CHECK_EXISTS :
  103. BM_EDGEROT_CHECK_EXISTS | BM_EDGEROT_CHECK_DEGENERATE;
  104. #define EDGE_OUT 1
  105. #define FACE_TAINT 1
  106. BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
  107. /**
  108. * this ends up being called twice, could add option to not to call check in
  109. * #BM_edge_rotate to get some extra speed */
  110. if (BM_edge_rotate_check(e)) {
  111. BMFace *fa, *fb;
  112. if (BM_edge_face_pair(e, &fa, &fb)) {
  113. /* check we're untouched */
  114. if (BMO_face_flag_test(bm, fa, FACE_TAINT) == false &&
  115. BMO_face_flag_test(bm, fb, FACE_TAINT) == false)
  116. {
  117. /* don't touch again (faces will be freed so run before rotating the edge) */
  118. BMO_face_flag_enable(bm, fa, FACE_TAINT);
  119. BMO_face_flag_enable(bm, fb, FACE_TAINT);
  120. if (!(e2 = BM_edge_rotate(bm, e, use_ccw, check_flag))) {
  121. BMO_face_flag_disable(bm, fa, FACE_TAINT);
  122. BMO_face_flag_disable(bm, fb, FACE_TAINT);
  123. #if 0
  124. BMO_error_raise(bm, op, BMERR_INVALID_SELECTION, "Could not rotate edge");
  125. return;
  126. #endif
  127. continue;
  128. }
  129. BMO_edge_flag_enable(bm, e2, EDGE_OUT);
  130. }
  131. }
  132. }
  133. }
  134. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "edges.out", BM_EDGE, EDGE_OUT);
  135. #undef EDGE_OUT
  136. #undef FACE_TAINT
  137. }
  138. #define SEL_FLAG 1
  139. #define SEL_ORIG 2
  140. static void bmo_face_flag_set_flush(BMesh *bm, BMFace *f, const short oflag, const bool value)
  141. {
  142. BMLoop *l_iter;
  143. BMLoop *l_first;
  144. BMO_face_flag_set(bm, f, oflag, value);
  145. l_iter = l_first = BM_FACE_FIRST_LOOP(f);
  146. do {
  147. BMO_edge_flag_set(bm, l_iter->e, oflag, value);
  148. BMO_vert_flag_set(bm, l_iter->v, oflag, value);
  149. } while ((l_iter = l_iter->next) != l_first);
  150. }
  151. static void bmo_region_extend_expand(
  152. BMesh *bm, BMOperator *op,
  153. const bool use_faces, const bool use_faces_step)
  154. {
  155. BMOIter siter;
  156. if (!use_faces) {
  157. BMVert *v;
  158. BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
  159. bool found = false;
  160. {
  161. BMIter eiter;
  162. BMEdge *e;
  163. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  164. if (!BMO_edge_flag_test(bm, e, SEL_ORIG) &&
  165. !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
  166. {
  167. found = true;
  168. break;
  169. }
  170. }
  171. }
  172. if (found) {
  173. if (!use_faces_step) {
  174. BMIter eiter;
  175. BMEdge *e;
  176. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  177. if (!BMO_edge_flag_test(bm, e, SEL_FLAG) &&
  178. !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
  179. {
  180. BMO_edge_flag_enable(bm, e, SEL_FLAG);
  181. BMO_vert_flag_enable(bm, BM_edge_other_vert(e, v), SEL_FLAG);
  182. }
  183. }
  184. }
  185. else {
  186. BMIter fiter;
  187. BMFace *f;
  188. BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
  189. if (!BMO_face_flag_test(bm, f, SEL_FLAG) &&
  190. !BM_elem_flag_test(f, BM_ELEM_HIDDEN))
  191. {
  192. bmo_face_flag_set_flush(bm, f, SEL_FLAG, true);
  193. }
  194. }
  195. /* handle wire edges (when stepping over faces) */
  196. {
  197. BMIter eiter;
  198. BMEdge *e;
  199. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  200. if (BM_edge_is_wire(e)) {
  201. if (!BMO_edge_flag_test(bm, e, SEL_FLAG) &&
  202. !BM_elem_flag_test(e, BM_ELEM_HIDDEN))
  203. {
  204. BMO_edge_flag_enable(bm, e, SEL_FLAG);
  205. BMO_vert_flag_enable(bm, BM_edge_other_vert(e, v), SEL_FLAG);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. }
  214. else {
  215. BMFace *f;
  216. BMO_ITER (f, &siter, op->slots_in, "geom", BM_FACE) {
  217. BMIter liter;
  218. BMLoop *l;
  219. BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
  220. if (!use_faces_step) {
  221. BMIter fiter;
  222. BMFace *f_other;
  223. BM_ITER_ELEM (f_other, &fiter, l->e, BM_FACES_OF_EDGE) {
  224. if (!BMO_face_flag_test(bm, f_other, SEL_ORIG | SEL_FLAG) &&
  225. !BM_elem_flag_test(f_other, BM_ELEM_HIDDEN))
  226. {
  227. BMO_face_flag_enable(bm, f_other, SEL_FLAG);
  228. }
  229. }
  230. }
  231. else {
  232. BMIter fiter;
  233. BMFace *f_other;
  234. BM_ITER_ELEM (f_other, &fiter, l->v, BM_FACES_OF_VERT) {
  235. if (!BMO_face_flag_test(bm, f_other, SEL_ORIG | SEL_FLAG) &&
  236. !BM_elem_flag_test(f_other, BM_ELEM_HIDDEN))
  237. {
  238. BMO_face_flag_enable(bm, f_other, SEL_FLAG);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. static void bmo_region_extend_contract(
  247. BMesh *bm, BMOperator *op,
  248. const bool use_faces, const bool use_faces_step)
  249. {
  250. BMOIter siter;
  251. if (!use_faces) {
  252. BMVert *v;
  253. BMO_ITER (v, &siter, op->slots_in, "geom", BM_VERT) {
  254. bool found = false;
  255. if (!use_faces_step) {
  256. BMIter eiter;
  257. BMEdge *e;
  258. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  259. if (!BMO_edge_flag_test(bm, e, SEL_ORIG)) {
  260. found = true;
  261. break;
  262. }
  263. }
  264. }
  265. else {
  266. BMIter fiter;
  267. BMFace *f;
  268. BM_ITER_ELEM (f, &fiter, v, BM_FACES_OF_VERT) {
  269. if (!BMO_face_flag_test(bm, f, SEL_ORIG)) {
  270. found = true;
  271. break;
  272. }
  273. }
  274. /* handle wire edges (when stepping over faces) */
  275. if (!found) {
  276. BMIter eiter;
  277. BMEdge *e;
  278. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  279. if (BM_edge_is_wire(e)) {
  280. if (!BMO_edge_flag_test(bm, e, SEL_ORIG)) {
  281. found = true;
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. if (found) {
  289. BMIter eiter;
  290. BMEdge *e;
  291. BMO_vert_flag_enable(bm, v, SEL_FLAG);
  292. BM_ITER_ELEM (e, &eiter, v, BM_EDGES_OF_VERT) {
  293. BMO_edge_flag_enable(bm, e, SEL_FLAG);
  294. }
  295. }
  296. }
  297. }
  298. else {
  299. BMFace *f;
  300. BMO_ITER (f, &siter, op->slots_in, "geom", BM_FACE) {
  301. BMIter liter;
  302. BMLoop *l;
  303. BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
  304. if (!use_faces_step) {
  305. BMIter fiter;
  306. BMFace *f_other;
  307. BM_ITER_ELEM (f_other, &fiter, l->e, BM_FACES_OF_EDGE) {
  308. if (!BMO_face_flag_test(bm, f_other, SEL_ORIG)) {
  309. BMO_face_flag_enable(bm, f, SEL_FLAG);
  310. break;
  311. }
  312. }
  313. }
  314. else {
  315. BMIter fiter;
  316. BMFace *f_other;
  317. BM_ITER_ELEM (f_other, &fiter, l->v, BM_FACES_OF_VERT) {
  318. if (!BMO_face_flag_test(bm, f_other, SEL_ORIG)) {
  319. BMO_face_flag_enable(bm, f, SEL_FLAG);
  320. break;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. void bmo_region_extend_exec(BMesh *bm, BMOperator *op)
  329. {
  330. const bool use_faces = BMO_slot_bool_get(op->slots_in, "use_faces");
  331. const bool use_face_step = BMO_slot_bool_get(op->slots_in, "use_face_step");
  332. const bool constrict = BMO_slot_bool_get(op->slots_in, "use_contract");
  333. BMO_slot_buffer_flag_enable(bm, op->slots_in, "geom", BM_ALL_NOLOOP, SEL_ORIG);
  334. if (constrict) {
  335. bmo_region_extend_contract(bm, op, use_faces, use_face_step);
  336. }
  337. else {
  338. bmo_region_extend_expand(bm, op, use_faces, use_face_step);
  339. }
  340. BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_ALL_NOLOOP, SEL_FLAG);
  341. }
  342. void bmo_smooth_vert_exec(BMesh *UNUSED(bm), BMOperator *op)
  343. {
  344. BMOIter siter;
  345. BMIter iter;
  346. BMVert *v;
  347. BMEdge *e;
  348. float (*cos)[3] = MEM_mallocN(sizeof(*cos) * BMO_slot_buffer_count(op->slots_in, "verts"), __func__);
  349. float *co, *co2, clip_dist = BMO_slot_float_get(op->slots_in, "clip_dist");
  350. const float fac = BMO_slot_float_get(op->slots_in, "factor");
  351. int i, j, clipx, clipy, clipz;
  352. int xaxis, yaxis, zaxis;
  353. clipx = BMO_slot_bool_get(op->slots_in, "mirror_clip_x");
  354. clipy = BMO_slot_bool_get(op->slots_in, "mirror_clip_y");
  355. clipz = BMO_slot_bool_get(op->slots_in, "mirror_clip_z");
  356. xaxis = BMO_slot_bool_get(op->slots_in, "use_axis_x");
  357. yaxis = BMO_slot_bool_get(op->slots_in, "use_axis_y");
  358. zaxis = BMO_slot_bool_get(op->slots_in, "use_axis_z");
  359. i = 0;
  360. BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
  361. co = cos[i];
  362. zero_v3(co);
  363. j = 0;
  364. BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
  365. co2 = BM_edge_other_vert(e, v)->co;
  366. add_v3_v3v3(co, co, co2);
  367. j += 1;
  368. }
  369. if (!j) {
  370. copy_v3_v3(co, v->co);
  371. i++;
  372. continue;
  373. }
  374. mul_v3_fl(co, 1.0f / (float)j);
  375. interp_v3_v3v3(co, v->co, co, fac);
  376. if (clipx && fabsf(v->co[0]) <= clip_dist)
  377. co[0] = 0.0f;
  378. if (clipy && fabsf(v->co[1]) <= clip_dist)
  379. co[1] = 0.0f;
  380. if (clipz && fabsf(v->co[2]) <= clip_dist)
  381. co[2] = 0.0f;
  382. i++;
  383. }
  384. i = 0;
  385. BMO_ITER (v, &siter, op->slots_in, "verts", BM_VERT) {
  386. if (xaxis)
  387. v->co[0] = cos[i][0];
  388. if (yaxis)
  389. v->co[1] = cos[i][1];
  390. if (zaxis)
  391. v->co[2] = cos[i][2];
  392. i++;
  393. }
  394. MEM_freeN(cos);
  395. }
  396. /**************************************************************************** *
  397. * Cycle UVs for a face
  398. **************************************************************************** */
  399. void bmo_rotate_uvs_exec(BMesh *bm, BMOperator *op)
  400. {
  401. BMOIter fs_iter; /* selected faces iterator */
  402. BMFace *fs; /* current face */
  403. BMIter l_iter; /* iteration loop */
  404. const bool use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
  405. const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
  406. if (cd_loop_uv_offset != -1) {
  407. BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
  408. if (use_ccw == false) { /* same loops direction */
  409. BMLoop *lf; /* current face loops */
  410. MLoopUV *f_luv; /* first face loop uv */
  411. float p_uv[2]; /* previous uvs */
  412. float t_uv[2]; /* tmp uvs */
  413. int n = 0;
  414. BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
  415. /* current loop uv is the previous loop uv */
  416. MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(lf, cd_loop_uv_offset);
  417. if (n == 0) {
  418. f_luv = luv;
  419. copy_v2_v2(p_uv, luv->uv);
  420. }
  421. else {
  422. copy_v2_v2(t_uv, luv->uv);
  423. copy_v2_v2(luv->uv, p_uv);
  424. copy_v2_v2(p_uv, t_uv);
  425. }
  426. n++;
  427. }
  428. copy_v2_v2(f_luv->uv, p_uv);
  429. }
  430. else { /* counter loop direction */
  431. BMLoop *lf; /* current face loops */
  432. MLoopUV *p_luv; /* previous loop uv */
  433. MLoopUV *luv;
  434. float t_uv[2]; /* current uvs */
  435. int n = 0;
  436. BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
  437. /* previous loop uv is the current loop uv */
  438. luv = BM_ELEM_CD_GET_VOID_P(lf, cd_loop_uv_offset);
  439. if (n == 0) {
  440. p_luv = luv;
  441. copy_v2_v2(t_uv, luv->uv);
  442. }
  443. else {
  444. copy_v2_v2(p_luv->uv, luv->uv);
  445. p_luv = luv;
  446. }
  447. n++;
  448. }
  449. copy_v2_v2(luv->uv, t_uv);
  450. }
  451. }
  452. }
  453. }
  454. /**************************************************************************** *
  455. * Reverse UVs for a face
  456. **************************************************************************** */
  457. static void bm_face_reverse_uvs(BMFace *f, const int cd_loop_uv_offset)
  458. {
  459. BMIter iter;
  460. BMLoop *l;
  461. int i;
  462. float (*uvs)[2] = BLI_array_alloca(uvs, f->len);
  463. BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, i) {
  464. MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
  465. copy_v2_v2(uvs[i], luv->uv);
  466. }
  467. /* now that we have the uvs in the array, reverse! */
  468. BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, i) {
  469. /* current loop uv is the previous loop uv */
  470. MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
  471. copy_v2_v2(luv->uv, uvs[(f->len - i - 1)]);
  472. }
  473. }
  474. void bmo_reverse_uvs_exec(BMesh *bm, BMOperator *op)
  475. {
  476. BMOIter iter;
  477. BMFace *f;
  478. const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);
  479. if (cd_loop_uv_offset != -1) {
  480. BMO_ITER (f, &iter, op->slots_in, "faces", BM_FACE) {
  481. bm_face_reverse_uvs(f, cd_loop_uv_offset);
  482. }
  483. }
  484. }
  485. /**************************************************************************** *
  486. * Cycle colors for a face
  487. **************************************************************************** */
  488. void bmo_rotate_colors_exec(BMesh *bm, BMOperator *op)
  489. {
  490. BMOIter fs_iter; /* selected faces iterator */
  491. BMFace *fs; /* current face */
  492. BMIter l_iter; /* iteration loop */
  493. const bool use_ccw = BMO_slot_bool_get(op->slots_in, "use_ccw");
  494. const int cd_loop_color_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPCOL);
  495. if (cd_loop_color_offset != -1) {
  496. BMO_ITER (fs, &fs_iter, op->slots_in, "faces", BM_FACE) {
  497. if (use_ccw == false) { /* same loops direction */
  498. BMLoop *lf; /* current face loops */
  499. MLoopCol *f_lcol; /* first face loop color */
  500. MLoopCol p_col; /* previous color */
  501. MLoopCol t_col; /* tmp color */
  502. int n = 0;
  503. BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
  504. /* current loop color is the previous loop color */
  505. MLoopCol *lcol = BM_ELEM_CD_GET_VOID_P(lf, cd_loop_color_offset);
  506. if (n == 0) {
  507. f_lcol = lcol;
  508. p_col = *lcol;
  509. }
  510. else {
  511. t_col = *lcol;
  512. *lcol = p_col;
  513. p_col = t_col;
  514. }
  515. n++;
  516. }
  517. *f_lcol = p_col;
  518. }
  519. else { /* counter loop direction */
  520. BMLoop *lf; /* current face loops */
  521. MLoopCol *p_lcol; /* previous loop color */
  522. MLoopCol *lcol;
  523. MLoopCol t_col; /* current color */
  524. int n = 0;
  525. BM_ITER_ELEM (lf, &l_iter, fs, BM_LOOPS_OF_FACE) {
  526. /* previous loop color is the current loop color */
  527. lcol = BM_ELEM_CD_GET_VOID_P(lf, cd_loop_color_offset);
  528. if (n == 0) {
  529. p_lcol = lcol;
  530. t_col = *lcol;
  531. }
  532. else {
  533. *p_lcol = *lcol;
  534. p_lcol = lcol;
  535. }
  536. n++;
  537. }
  538. *lcol = t_col;
  539. }
  540. }
  541. }
  542. }
  543. /*************************************************************************** *
  544. * Reverse colors for a face
  545. *************************************************************************** */
  546. static void bm_face_reverse_colors(BMFace *f, const int cd_loop_color_offset)
  547. {
  548. BMIter iter;
  549. BMLoop *l;
  550. int i;
  551. MLoopCol *cols = BLI_array_alloca(cols, f->len);
  552. BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, i) {
  553. MLoopCol *lcol = BM_ELEM_CD_GET_VOID_P(l, cd_loop_color_offset);
  554. cols[i] = *lcol;
  555. }
  556. /* now that we have the uvs in the array, reverse! */
  557. BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, i) {
  558. /* current loop uv is the previous loop color */
  559. MLoopCol *lcol = BM_ELEM_CD_GET_VOID_P(l, cd_loop_color_offset);
  560. *lcol = cols[(f->len - i - 1)];
  561. }
  562. }
  563. void bmo_reverse_colors_exec(BMesh *bm, BMOperator *op)
  564. {
  565. BMOIter iter;
  566. BMFace *f;
  567. const int cd_loop_color_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPCOL);
  568. if (cd_loop_color_offset != -1) {
  569. BMO_ITER (f, &iter, op->slots_in, "faces", BM_FACE) {
  570. bm_face_reverse_colors(f, cd_loop_color_offset);
  571. }
  572. }
  573. }