Blender  V2.93
bmesh_mesh.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_listBase.h"
26 #include "DNA_scene_types.h"
27 
28 #include "BLI_bitmap.h"
29 #include "BLI_linklist_stack.h"
30 #include "BLI_listbase.h"
31 #include "BLI_math.h"
32 #include "BLI_stack.h"
33 #include "BLI_task.h"
34 #include "BLI_utildefines.h"
35 
36 #include "BKE_editmesh.h"
37 #include "BKE_global.h"
38 #include "BKE_mesh.h"
39 #include "BKE_multires.h"
40 
41 #include "atomic_ops.h"
42 
43 #include "intern/bmesh_private.h"
44 
45 /* used as an extern, defined in bmesh.h */
46 const BMAllocTemplate bm_mesh_allocsize_default = {512, 1024, 2048, 512};
47 const BMAllocTemplate bm_mesh_chunksize_default = {512, 1024, 2048, 512};
48 
49 static void bm_mempool_init_ex(const BMAllocTemplate *allocsize,
50  const bool use_toolflags,
51  BLI_mempool **r_vpool,
52  BLI_mempool **r_epool,
53  BLI_mempool **r_lpool,
54  BLI_mempool **r_fpool)
55 {
56  size_t vert_size, edge_size, loop_size, face_size;
57 
58  if (use_toolflags == true) {
59  vert_size = sizeof(BMVert_OFlag);
60  edge_size = sizeof(BMEdge_OFlag);
61  loop_size = sizeof(BMLoop);
62  face_size = sizeof(BMFace_OFlag);
63  }
64  else {
65  vert_size = sizeof(BMVert);
66  edge_size = sizeof(BMEdge);
67  loop_size = sizeof(BMLoop);
68  face_size = sizeof(BMFace);
69  }
70 
71  if (r_vpool) {
72  *r_vpool = BLI_mempool_create(
74  }
75  if (r_epool) {
76  *r_epool = BLI_mempool_create(
78  }
79  if (r_lpool) {
80  *r_lpool = BLI_mempool_create(
82  }
83  if (r_fpool) {
84  *r_fpool = BLI_mempool_create(
86  }
87 }
88 
89 static void bm_mempool_init(BMesh *bm, const BMAllocTemplate *allocsize, const bool use_toolflags)
90 {
91  bm_mempool_init_ex(allocsize, use_toolflags, &bm->vpool, &bm->epool, &bm->lpool, &bm->fpool);
92 
93 #ifdef USE_BMESH_HOLES
94  bm->looplistpool = BLI_mempool_create(sizeof(BMLoopList), 512, 512, BLI_MEMPOOL_NOP);
95 #endif
96 }
97 
99 {
101 
103  return;
104  }
105 
109 
110  BMIter iter;
111  BMVert_OFlag *v_olfag;
112  BLI_mempool *toolflagpool = bm->vtoolflagpool;
113  BM_ITER_MESH (v_olfag, &iter, bm, BM_VERTS_OF_MESH) {
114  v_olfag->oflags = BLI_mempool_calloc(toolflagpool);
115  }
116 
117  BMEdge_OFlag *e_olfag;
118  toolflagpool = bm->etoolflagpool;
119  BM_ITER_MESH (e_olfag, &iter, bm, BM_EDGES_OF_MESH) {
120  e_olfag->oflags = BLI_mempool_calloc(toolflagpool);
121  }
122 
123  BMFace_OFlag *f_olfag;
124  toolflagpool = bm->ftoolflagpool;
125  BM_ITER_MESH (f_olfag, &iter, bm, BM_FACES_OF_MESH) {
126  f_olfag->oflags = BLI_mempool_calloc(toolflagpool);
127  }
128 
129  bm->totflags = 1;
130 }
131 
133 {
134  if (bm->vtoolflagpool) {
136  bm->vtoolflagpool = NULL;
137  }
138  if (bm->etoolflagpool) {
140  bm->etoolflagpool = NULL;
141  }
142  if (bm->ftoolflagpool) {
144  bm->ftoolflagpool = NULL;
145  }
146 }
147 
158 {
159  /* allocate the structure */
160  BMesh *bm = MEM_callocN(sizeof(BMesh), __func__);
161 
162  /* allocate the memory pools for the mesh elements */
163  bm_mempool_init(bm, allocsize, params->use_toolflags);
164 
165  /* allocate one flag pool that we don't get rid of. */
166  bm->use_toolflags = params->use_toolflags;
167  bm->toolflag_index = 0;
168  bm->totflags = 0;
169 
174 
175  return bm;
176 }
177 
186 {
187  BMVert *v;
188  BMEdge *e;
189  BMLoop *l;
190  BMFace *f;
191 
192  BMIter iter;
193  BMIter itersub;
194 
195  const bool is_ldata_free = CustomData_bmesh_has_free(&bm->ldata);
196  const bool is_pdata_free = CustomData_bmesh_has_free(&bm->pdata);
197 
198  /* Check if we have to call free, if not we can avoid a lot of looping */
200  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
202  }
203  }
205  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
207  }
208  }
209 
210  if (is_ldata_free || is_pdata_free) {
211  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
212  if (is_pdata_free) {
214  }
215  if (is_ldata_free) {
216  BM_ITER_ELEM (l, &itersub, f, BM_LOOPS_OF_FACE) {
218  }
219  }
220  }
221  }
222 
223  /* Free custom data pools, This should probably go in CustomData_free? */
224  if (bm->vdata.totlayer) {
226  }
227  if (bm->edata.totlayer) {
229  }
230  if (bm->ldata.totlayer) {
232  }
233  if (bm->pdata.totlayer) {
235  }
236 
237  /* free custom data */
238  CustomData_free(&bm->vdata, 0);
239  CustomData_free(&bm->edata, 0);
240  CustomData_free(&bm->ldata, 0);
241  CustomData_free(&bm->pdata, 0);
242 
243  /* destroy element pools */
248 
249  if (bm->vtable) {
250  MEM_freeN(bm->vtable);
251  }
252  if (bm->etable) {
253  MEM_freeN(bm->etable);
254  }
255  if (bm->ftable) {
256  MEM_freeN(bm->ftable);
257  }
258 
259  /* destroy flag pool */
261 
262 #ifdef USE_BMESH_HOLES
263  BLI_mempool_destroy(bm->looplistpool);
264 #endif
265 
267 
268  if (bm->lnor_spacearr) {
271  }
272 
274 }
275 
282 {
283  const bool use_toolflags = bm->use_toolflags;
284 
285  /* free old mesh */
287  memset(bm, 0, sizeof(BMesh));
288 
289  /* allocate the memory pools for the mesh elements */
290  bm_mempool_init(bm, &bm_mesh_allocsize_default, use_toolflags);
291 
292  bm->use_toolflags = use_toolflags;
293  bm->toolflag_index = 0;
294  bm->totflags = 0;
295 
300 }
301 
308 {
310 
311  if (bm->py_handle) {
312  /* keep this out of 'BM_mesh_data_free' because we want python
313  * to be able to clear the mesh and maintain access. */
315  bm->py_handle = NULL;
316  }
317 
318  MEM_freeN(bm);
319 }
320 
325 /* We use that existing internal API flag,
326  * assuming no other tool using it would run concurrently to clnors editing. */
327 #define BM_LNORSPACE_UPDATE _FLAG_MF
328 
329 typedef struct BMEdgesCalcVectorsData {
330  /* Read-only data. */
331  const float (*vcos)[3];
332 
333  /* Read-write data, but no need to protect it, no concurrency to fear here. */
334  float (*edgevec)[3];
336 
337 static void mesh_edges_calc_vectors_cb(void *userdata, MempoolIterData *mp_e)
338 {
339  BMEdgesCalcVectorsData *data = userdata;
340  BMEdge *e = (BMEdge *)mp_e;
341 
342  if (e->l) {
343  const float *v1_co = data->vcos ? data->vcos[BM_elem_index_get(e->v1)] : e->v1->co;
344  const float *v2_co = data->vcos ? data->vcos[BM_elem_index_get(e->v2)] : e->v2->co;
345  sub_v3_v3v3(data->edgevec[BM_elem_index_get(e)], v2_co, v1_co);
346  normalize_v3(data->edgevec[BM_elem_index_get(e)]);
347  }
348  else {
349  /* the edge vector will not be needed when the edge has no radial */
350  }
351 }
352 
353 static void bm_mesh_edges_calc_vectors(BMesh *bm, float (*edgevec)[3], const float (*vcos)[3])
354 {
355  BM_mesh_elem_index_ensure(bm, BM_EDGE | (vcos ? BM_VERT : 0));
356 
358  .vcos = vcos,
359  .edgevec = edgevec,
360  };
361 
362  BM_iter_parallel(
364 }
365 
366 typedef struct BMVertsCalcNormalsData {
367  /* Read-only data. */
368  const float (*fnos)[3];
369  const float (*edgevec)[3];
370  const float (*vcos)[3];
371 
372  /* Read-write data, protected by an atomic-based fake spin-lock like system. */
373  float (*vnos)[3];
375 
376 static void mesh_verts_calc_normals_accum_cb(void *userdata, MempoolIterData *mp_f)
377 {
378 #define FLT_EQ_NONAN(_fa, _fb) (*((const uint32_t *)&_fa) == *((const uint32_t *)&_fb))
379 
380  BMVertsCalcNormalsData *data = userdata;
381  BMFace *f = (BMFace *)mp_f;
382 
383  const float *f_no = data->fnos ? data->fnos[BM_elem_index_get(f)] : f->no;
384 
385  BMLoop *l_first, *l_iter;
386  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
387  do {
388  const float *e1diff, *e2diff;
389  float dotprod;
390  float fac;
391 
392  /* calculate the dot product of the two edges that
393  * meet at the loop's vertex */
394  e1diff = data->edgevec[BM_elem_index_get(l_iter->prev->e)];
395  e2diff = data->edgevec[BM_elem_index_get(l_iter->e)];
396  dotprod = dot_v3v3(e1diff, e2diff);
397 
398  /* edge vectors are calculated from e->v1 to e->v2, so
399  * adjust the dot product if one but not both loops
400  * actually runs from from e->v2 to e->v1 */
401  if ((l_iter->prev->e->v1 == l_iter->prev->v) ^ (l_iter->e->v1 == l_iter->v)) {
402  dotprod = -dotprod;
403  }
404 
405  fac = saacos(-dotprod);
406 
407  if (fac != fac) { /* NAN detection. */
408  /* Degenerated case, nothing to do here, just ignore that vertex. */
409  continue;
410  }
411 
412  /* accumulate weighted face normal into the vertex's normal */
413  float *v_no = data->vnos ? data->vnos[BM_elem_index_get(l_iter->v)] : l_iter->v->no;
414 
415  /* This block is a lockless threadsafe madd_v3_v3fl.
416  * It uses the first float of the vector as a sort of cheap spin-lock,
417  * assuming FLT_MAX is a safe 'illegal' value that cannot be set here otherwise.
418  * It also assumes that collisions between threads are highly unlikely,
419  * else performances would be quite bad here. */
420  float virtual_lock = v_no[0];
421  while (true) {
422  /* This loops until following conditions are met:
423  * - v_no[0] has same value as virtual_lock (i.e. it did not change since last try).
424  * - v_no[0] was not FLT_MAX, i.e. it was not locked by another thread.
425  */
426  const float vl = atomic_cas_float(&v_no[0], virtual_lock, FLT_MAX);
427  if (FLT_EQ_NONAN(vl, virtual_lock) && vl != FLT_MAX) {
428  break;
429  }
430  virtual_lock = vl;
431  }
432  BLI_assert(v_no[0] == FLT_MAX);
433  /* Now we own that normal value, and can change it.
434  * But first scalar of the vector must not be changed yet, it's our lock! */
435  virtual_lock += f_no[0] * fac;
436  v_no[1] += f_no[1] * fac;
437  v_no[2] += f_no[2] * fac;
438  /* Second atomic operation to 'release'
439  * our lock on that vector and set its first scalar value. */
440  /* Note that we do not need to loop here, since we 'locked' v_no[0],
441  * nobody should have changed it in the mean time. */
442  virtual_lock = atomic_cas_float(&v_no[0], FLT_MAX, virtual_lock);
443  BLI_assert(virtual_lock == FLT_MAX);
444 
445  } while ((l_iter = l_iter->next) != l_first);
446 
447 #undef FLT_EQ_NONAN
448 }
449 
450 static void mesh_verts_calc_normals_normalize_cb(void *userdata, MempoolIterData *mp_v)
451 {
452  BMVertsCalcNormalsData *data = userdata;
453  BMVert *v = (BMVert *)mp_v;
454 
455  float *v_no = data->vnos ? data->vnos[BM_elem_index_get(v)] : v->no;
456  if (UNLIKELY(normalize_v3(v_no) == 0.0f)) {
457  const float *v_co = data->vcos ? data->vcos[BM_elem_index_get(v)] : v->co;
458  normalize_v3_v3(v_no, v_co);
459  }
460 }
461 
463  const float (*edgevec)[3],
464  const float (*fnos)[3],
465  const float (*vcos)[3],
466  float (*vnos)[3])
467 {
468  BM_mesh_elem_index_ensure(bm, (BM_EDGE | BM_FACE) | ((vnos || vcos) ? BM_VERT : 0));
469 
471  .fnos = fnos,
472  .edgevec = edgevec,
473  .vcos = vcos,
474  .vnos = vnos,
475  };
476 
477  BM_iter_parallel(
479 
480  /* normalize the accumulated vertex normals */
481  BM_iter_parallel(bm,
484  &data,
485  bm->totvert >= BM_OMP_LIMIT);
486 }
487 
488 static void mesh_faces_calc_normals_cb(void *UNUSED(userdata), MempoolIterData *mp_f)
489 {
490  BMFace *f = (BMFace *)mp_f;
491 
493 }
494 
501 {
502  float(*edgevec)[3] = MEM_mallocN(sizeof(*edgevec) * bm->totedge, __func__);
503 
504  /* Parallel mempool iteration does not allow generating indices inline anymore... */
506 
507  /* calculate all face normals */
508  BM_iter_parallel(
510 
511  /* Zero out vertex normals */
512  BMIter viter;
513  BMVert *v;
514  int i;
515 
516  BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, i) {
517  BM_elem_index_set(v, i); /* set_inline */
518  zero_v3(v->no);
519  }
521 
522  /* Compute normalized direction vectors for each edge.
523  * Directions will be used for calculating the weights of the face normals on the vertex normals.
524  */
526 
527  /* Add weighted face normals to vertices, and normalize vert normals. */
528  bm_mesh_verts_calc_normals(bm, (const float(*)[3])edgevec, NULL, NULL, NULL);
529  MEM_freeN(edgevec);
530 }
531 
539  const float (*fnos)[3],
540  const float (*vcos)[3],
541  float (*vnos)[3])
542 {
543  float(*edgevec)[3] = MEM_mallocN(sizeof(*edgevec) * bm->totedge, __func__);
544 
545  /* Compute normalized direction vectors for each edge.
546  * Directions will be used for calculating the weights of the face normals on the vertex normals.
547  */
548  bm_mesh_edges_calc_vectors(bm, edgevec, vcos);
549 
550  /* Add weighted face normals to vertices, and normalize vert normals. */
551  bm_mesh_verts_calc_normals(bm, (const float(*)[3])edgevec, fnos, vcos, vnos);
552  MEM_freeN(edgevec);
553 }
554 
559  const float (*vnos)[3],
560  const float (*fnos)[3],
561  float (*r_lnos)[3],
562  const float split_angle,
563  const bool do_sharp_edges_tag)
564 {
565  BMIter eiter;
566  BMEdge *e;
567  int i;
568 
569  const bool check_angle = (split_angle < (float)M_PI);
570  const float split_angle_cos = check_angle ? cosf(split_angle) : -1.0f;
571 
572  {
573  char htype = BM_VERT | BM_LOOP;
574  if (fnos) {
575  htype |= BM_FACE;
576  }
578  }
579 
580  /* This first loop checks which edges are actually smooth,
581  * and pre-populate lnos with vnos (as if they were all smooth). */
582  BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, i) {
583  BMLoop *l_a, *l_b;
584 
585  BM_elem_index_set(e, i); /* set_inline */
586  BM_elem_flag_disable(e, BM_ELEM_TAG); /* Clear tag (means edge is sharp). */
587 
588  /* An edge with only two loops, might be smooth... */
589  if (BM_edge_loop_pair(e, &l_a, &l_b)) {
590  bool is_angle_smooth = true;
591  if (check_angle) {
592  const float *no_a = fnos ? fnos[BM_elem_index_get(l_a->f)] : l_a->f->no;
593  const float *no_b = fnos ? fnos[BM_elem_index_get(l_b->f)] : l_b->f->no;
594  is_angle_smooth = (dot_v3v3(no_a, no_b) >= split_angle_cos);
595  }
596 
597  /* We only tag edges that are *really* smooth:
598  * If the angle between both its polys' normals is below split_angle value,
599  * and it is tagged as such,
600  * and both its faces are smooth,
601  * and both its faces have compatible (non-flipped) normals,
602  * i.e. both loops on the same edge do not share the same vertex.
603  */
605  BM_elem_flag_test(l_b->f, BM_ELEM_SMOOTH) && l_a->v != l_b->v) {
606  if (is_angle_smooth) {
607  const float *no;
609 
610  /* linked vertices might be fully smooth, copy their normals to loop ones. */
611  if (r_lnos) {
612  no = vnos ? vnos[BM_elem_index_get(l_a->v)] : l_a->v->no;
613  copy_v3_v3(r_lnos[BM_elem_index_get(l_a)], no);
614  no = vnos ? vnos[BM_elem_index_get(l_b->v)] : l_b->v->no;
615  copy_v3_v3(r_lnos[BM_elem_index_get(l_b)], no);
616  }
617  }
618  else if (do_sharp_edges_tag) {
619  /* Note that we do not care about the other sharp-edge cases
620  * (sharp poly, non-manifold edge, etc.),
621  * only tag edge as sharp when it is due to angle threshold. */
623  }
624  }
625  }
626  }
627 
629 }
630 
637 {
638  BMLoop *lfan_pivot_next = l_curr;
639  BMEdge *e_next = l_curr->e;
640 
641  BLI_assert(!BM_elem_flag_test(lfan_pivot_next, BM_ELEM_TAG));
642  BM_elem_flag_enable(lfan_pivot_next, BM_ELEM_TAG);
643 
644  while (true) {
645  /* Much simpler than in sibling code with basic Mesh data! */
646  lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot_next, &e_next);
647 
648  if (!lfan_pivot_next || !BM_elem_flag_test(e_next, BM_ELEM_TAG)) {
649  /* Sharp loop/edge, so not a cyclic smooth fan... */
650  return false;
651  }
652  /* Smooth loop/edge... */
653  if (BM_elem_flag_test(lfan_pivot_next, BM_ELEM_TAG)) {
654  if (lfan_pivot_next == l_curr) {
655  /* We walked around a whole cyclic smooth fan
656  * without finding any already-processed loop,
657  * means we can use initial l_curr/l_prev edge as start for this smooth fan. */
658  return true;
659  }
660  /* ... already checked in some previous looping, we can abort. */
661  return false;
662  }
663  /* ... we can skip it in future, and keep checking the smooth fan. */
664  BM_elem_flag_enable(lfan_pivot_next, BM_ELEM_TAG);
665  }
666 }
667 
677  const float (*vcos)[3],
678  const float (*fnos)[3],
679  float (*r_lnos)[3],
680  MLoopNorSpaceArray *r_lnors_spacearr,
681  const short (*clnors_data)[2],
682  const int cd_loop_clnors_offset,
683  const bool do_rebuild)
684 {
685  BMIter fiter;
686  BMFace *f_curr;
687  const bool has_clnors = clnors_data || (cd_loop_clnors_offset != -1);
688 
689  MLoopNorSpaceArray _lnors_spacearr = {NULL};
690 
691  /* Temp normal stack. */
692  BLI_SMALLSTACK_DECLARE(normal, float *);
693  /* Temp clnors stack. */
694  BLI_SMALLSTACK_DECLARE(clnors, short *);
695  /* Temp edge vectors stack, only used when computing lnor spacearr. */
696  BLI_Stack *edge_vectors = NULL;
697 
698  {
699  char htype = 0;
700  if (vcos) {
701  htype |= BM_VERT;
702  }
703  /* Face/Loop indices are set inline below. */
705  }
706 
707  if (!r_lnors_spacearr && has_clnors) {
708  /* We need to compute lnor spacearr if some custom lnor data are given to us! */
709  r_lnors_spacearr = &_lnors_spacearr;
710  }
711  if (r_lnors_spacearr) {
713  edge_vectors = BLI_stack_new(sizeof(float[3]), __func__);
714  }
715 
716  /* Clear all loops' tags (means none are to be skipped for now). */
717  int index_face, index_loop = 0;
718  BM_ITER_MESH_INDEX (f_curr, &fiter, bm, BM_FACES_OF_MESH, index_face) {
719  BMLoop *l_curr, *l_first;
720 
721  BM_elem_index_set(f_curr, index_face); /* set_inline */
722 
723  l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
724  do {
725  BM_elem_index_set(l_curr, index_loop++); /* set_inline */
727  } while ((l_curr = l_curr->next) != l_first);
728  }
730 
731  /* We now know edges that can be smoothed (they are tagged),
732  * and edges that will be hard (they aren't).
733  * Now, time to generate the normals.
734  */
735  BM_ITER_MESH (f_curr, &fiter, bm, BM_FACES_OF_MESH) {
736  BMLoop *l_curr, *l_first;
737 
738  l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
739  do {
740  if (do_rebuild && !BM_ELEM_API_FLAG_TEST(l_curr, BM_LNORSPACE_UPDATE) &&
742  continue;
743  }
744  /* A smooth edge, we have to check for cyclic smooth fan case.
745  * If we find a new, never-processed cyclic smooth fan, we can do it now using that loop/edge
746  * as 'entry point', otherwise we can skip it. */
747 
748  /* Note: In theory, we could make bm_mesh_loop_check_cyclic_smooth_fan() store
749  * mlfan_pivot's in a stack, to avoid having to fan again around
750  * the vert during actual computation of clnor & clnorspace. However, this would complicate
751  * the code, add more memory usage, and
752  * BM_vert_step_fan_loop() is quite cheap in term of CPU cycles,
753  * so really think it's not worth it. */
754  if (BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
756  }
757  else if (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
758  !BM_elem_flag_test(l_curr->prev->e, BM_ELEM_TAG)) {
759  /* Simple case (both edges around that vertex are sharp in related polygon),
760  * this vertex just takes its poly normal.
761  */
762  const int l_curr_index = BM_elem_index_get(l_curr);
763  const float *no = fnos ? fnos[BM_elem_index_get(f_curr)] : f_curr->no;
764  copy_v3_v3(r_lnos[l_curr_index], no);
765 
766  /* If needed, generate this (simple!) lnor space. */
767  if (r_lnors_spacearr) {
768  float vec_curr[3], vec_prev[3];
769  MLoopNorSpace *lnor_space = BKE_lnor_space_create(r_lnors_spacearr);
770 
771  {
772  const BMVert *v_pivot = l_curr->v;
773  const float *co_pivot = vcos ? vcos[BM_elem_index_get(v_pivot)] : v_pivot->co;
774  const BMVert *v_1 = BM_edge_other_vert(l_curr->e, v_pivot);
775  const float *co_1 = vcos ? vcos[BM_elem_index_get(v_1)] : v_1->co;
776  const BMVert *v_2 = BM_edge_other_vert(l_curr->prev->e, v_pivot);
777  const float *co_2 = vcos ? vcos[BM_elem_index_get(v_2)] : v_2->co;
778 
779  sub_v3_v3v3(vec_curr, co_1, co_pivot);
780  normalize_v3(vec_curr);
781  sub_v3_v3v3(vec_prev, co_2, co_pivot);
782  normalize_v3(vec_prev);
783  }
784 
785  BKE_lnor_space_define(lnor_space, r_lnos[l_curr_index], vec_curr, vec_prev, NULL);
786  /* We know there is only one loop in this space,
787  * no need to create a linklist in this case... */
788  BKE_lnor_space_add_loop(r_lnors_spacearr, lnor_space, l_curr_index, l_curr, true);
789 
790  if (has_clnors) {
791  const short(*clnor)[2] = clnors_data ? &clnors_data[l_curr_index] :
792  (const void *)BM_ELEM_CD_GET_VOID_P(
793  l_curr, cd_loop_clnors_offset);
794  BKE_lnor_space_custom_data_to_normal(lnor_space, *clnor, r_lnos[l_curr_index]);
795  }
796  }
797  }
798  /* We *do not need* to check/tag loops as already computed!
799  * Due to the fact a loop only links to one of its two edges,
800  * a same fan *will never be walked more than once!*
801  * Since we consider edges having neighbor faces with inverted (flipped) normals as sharp,
802  * we are sure that no fan will be skipped, even only considering the case
803  * (sharp curr_edge, smooth prev_edge), and not the alternative
804  * (smooth curr_edge, sharp prev_edge).
805  * All this due/thanks to link between normals and loop ordering.
806  */
807  else {
808  /* We have to fan around current vertex, until we find the other non-smooth edge,
809  * and accumulate face normals into the vertex!
810  * Note in case this vertex has only one sharp edge,
811  * this is a waste because the normal is the same as the vertex normal,
812  * but I do not see any easy way to detect that (would need to count number of sharp edges
813  * per vertex, I doubt the additional memory usage would be worth it, especially as it
814  * should not be a common case in real-life meshes anyway).
815  */
816  BMVert *v_pivot = l_curr->v;
817  BMEdge *e_next;
818  const BMEdge *e_org = l_curr->e;
819  BMLoop *lfan_pivot, *lfan_pivot_next;
820  int lfan_pivot_index;
821  float lnor[3] = {0.0f, 0.0f, 0.0f};
822  float vec_curr[3], vec_next[3], vec_org[3];
823 
824  /* We validate clnors data on the fly - cheapest way to do! */
825  int clnors_avg[2] = {0, 0};
826  const short(*clnor_ref)[2] = NULL;
827  int clnors_nbr = 0;
828  bool clnors_invalid = false;
829 
830  const float *co_pivot = vcos ? vcos[BM_elem_index_get(v_pivot)] : v_pivot->co;
831 
832  MLoopNorSpace *lnor_space = r_lnors_spacearr ? BKE_lnor_space_create(r_lnors_spacearr) :
833  NULL;
834 
835  BLI_assert((edge_vectors == NULL) || BLI_stack_is_empty(edge_vectors));
836 
837  lfan_pivot = l_curr;
838  lfan_pivot_index = BM_elem_index_get(lfan_pivot);
839  e_next = lfan_pivot->e; /* Current edge here, actually! */
840 
841  /* Only need to compute previous edge's vector once,
842  * then we can just reuse old current one! */
843  {
844  const BMVert *v_2 = BM_edge_other_vert(e_next, v_pivot);
845  const float *co_2 = vcos ? vcos[BM_elem_index_get(v_2)] : v_2->co;
846 
847  sub_v3_v3v3(vec_org, co_2, co_pivot);
848  normalize_v3(vec_org);
849  copy_v3_v3(vec_curr, vec_org);
850 
851  if (r_lnors_spacearr) {
852  BLI_stack_push(edge_vectors, vec_org);
853  }
854  }
855 
856  while (true) {
857  /* Much simpler than in sibling code with basic Mesh data! */
858  lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
859  if (lfan_pivot_next) {
860  BLI_assert(lfan_pivot_next->v == v_pivot);
861  }
862  else {
863  /* next edge is non-manifold, we have to find it ourselves! */
864  e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
865  }
866 
867  /* Compute edge vector.
868  * NOTE: We could pre-compute those into an array, in the first iteration,
869  * instead of computing them twice (or more) here.
870  * However, time gained is not worth memory and time lost,
871  * given the fact that this code should not be called that much in real-life meshes.
872  */
873  {
874  const BMVert *v_2 = BM_edge_other_vert(e_next, v_pivot);
875  const float *co_2 = vcos ? vcos[BM_elem_index_get(v_2)] : v_2->co;
876 
877  sub_v3_v3v3(vec_next, co_2, co_pivot);
878  normalize_v3(vec_next);
879  }
880 
881  {
882  /* Code similar to accumulate_vertex_normals_poly_v3. */
883  /* Calculate angle between the two poly edges incident on this vertex. */
884  const BMFace *f = lfan_pivot->f;
885  const float fac = saacos(dot_v3v3(vec_next, vec_curr));
886  const float *no = fnos ? fnos[BM_elem_index_get(f)] : f->no;
887  /* Accumulate */
888  madd_v3_v3fl(lnor, no, fac);
889 
890  if (has_clnors) {
891  /* Accumulate all clnors, if they are not all equal we have to fix that! */
892  const short(*clnor)[2] = clnors_data ? &clnors_data[lfan_pivot_index] :
893  (const void *)BM_ELEM_CD_GET_VOID_P(
894  lfan_pivot, cd_loop_clnors_offset);
895  if (clnors_nbr) {
896  clnors_invalid |= ((*clnor_ref)[0] != (*clnor)[0] ||
897  (*clnor_ref)[1] != (*clnor)[1]);
898  }
899  else {
900  clnor_ref = clnor;
901  }
902  clnors_avg[0] += (*clnor)[0];
903  clnors_avg[1] += (*clnor)[1];
904  clnors_nbr++;
905  /* We store here a pointer to all custom lnors processed. */
906  BLI_SMALLSTACK_PUSH(clnors, (short *)*clnor);
907  }
908  }
909 
910  /* We store here a pointer to all loop-normals processed. */
911  BLI_SMALLSTACK_PUSH(normal, (float *)r_lnos[lfan_pivot_index]);
912 
913  if (r_lnors_spacearr) {
914  /* Assign current lnor space to current 'vertex' loop. */
916  r_lnors_spacearr, lnor_space, lfan_pivot_index, lfan_pivot, false);
917  if (e_next != e_org) {
918  /* We store here all edges-normalized vectors processed. */
919  BLI_stack_push(edge_vectors, vec_next);
920  }
921  }
922 
923  if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
924  /* Next edge is sharp, we have finished with this fan of faces around this vert! */
925  break;
926  }
927 
928  /* Copy next edge vector to current one. */
929  copy_v3_v3(vec_curr, vec_next);
930  /* Next pivot loop to current one. */
931  lfan_pivot = lfan_pivot_next;
932  lfan_pivot_index = BM_elem_index_get(lfan_pivot);
933  }
934 
935  {
936  float lnor_len = normalize_v3(lnor);
937 
938  /* If we are generating lnor spacearr, we can now define the one for this fan. */
939  if (r_lnors_spacearr) {
940  if (UNLIKELY(lnor_len == 0.0f)) {
941  /* Use vertex normal as fallback! */
942  copy_v3_v3(lnor, r_lnos[lfan_pivot_index]);
943  lnor_len = 1.0f;
944  }
945 
946  BKE_lnor_space_define(lnor_space, lnor, vec_org, vec_next, edge_vectors);
947 
948  if (has_clnors) {
949  if (clnors_invalid) {
950  short *clnor;
951 
952  clnors_avg[0] /= clnors_nbr;
953  clnors_avg[1] /= clnors_nbr;
954  /* Fix/update all clnors of this fan with computed average value. */
955 
956  /* Prints continuously when merge custom normals, so commenting. */
957  /* printf("Invalid clnors in this fan!\n"); */
958 
959  while ((clnor = BLI_SMALLSTACK_POP(clnors))) {
960  // print_v2("org clnor", clnor);
961  clnor[0] = (short)clnors_avg[0];
962  clnor[1] = (short)clnors_avg[1];
963  }
964  // print_v2("new clnors", clnors_avg);
965  }
966  else {
967  /* We still have to consume the stack! */
968  while (BLI_SMALLSTACK_POP(clnors)) {
969  /* pass */
970  }
971  }
972  BKE_lnor_space_custom_data_to_normal(lnor_space, *clnor_ref, lnor);
973  }
974  }
975 
976  /* In case we get a zero normal here, just use vertex normal already set! */
977  if (LIKELY(lnor_len != 0.0f)) {
978  /* Copy back the final computed normal into all related loop-normals. */
979  float *nor;
980 
981  while ((nor = BLI_SMALLSTACK_POP(normal))) {
982  copy_v3_v3(nor, lnor);
983  }
984  }
985  else {
986  /* We still have to consume the stack! */
987  while (BLI_SMALLSTACK_POP(normal)) {
988  /* pass */
989  }
990  }
991  }
992 
993  /* Tag related vertex as sharp, to avoid fanning around it again
994  * (in case it was a smooth one). */
995  if (r_lnors_spacearr) {
997  }
998  }
999  } while ((l_curr = l_curr->next) != l_first);
1000  }
1001 
1002  if (r_lnors_spacearr) {
1003  BLI_stack_free(edge_vectors);
1004  if (r_lnors_spacearr == &_lnors_spacearr) {
1005  BKE_lnor_spacearr_free(r_lnors_spacearr);
1006  }
1007  }
1008 }
1009 
1010 /* This threshold is a bit touchy (usual float precision issue), this value seems OK. */
1011 #define LNOR_SPACE_TRIGO_THRESHOLD (1.0f - 1e-4f)
1012 
1018  MLoopNorSpaceArray *lnors_spacearr,
1019  const float (*new_lnors)[3])
1020 {
1021  BLI_bitmap *done_loops = BLI_BITMAP_NEW((size_t)bm->totloop, __func__);
1022  bool changed = false;
1023 
1024  BLI_assert(lnors_spacearr->data_type == MLNOR_SPACEARR_BMLOOP_PTR);
1025 
1026  for (int i = 0; i < bm->totloop; i++) {
1027  if (!lnors_spacearr->lspacearr[i]) {
1028  /* This should not happen in theory, but in some rare case (probably ugly geometry)
1029  * we can get some NULL loopspacearr at this point. :/
1030  * Maybe we should set those loops' edges as sharp?
1031  */
1032  BLI_BITMAP_ENABLE(done_loops, i);
1033  if (G.debug & G_DEBUG) {
1034  printf("WARNING! Getting invalid NULL loop space for loop %d!\n", i);
1035  }
1036  continue;
1037  }
1038 
1039  if (!BLI_BITMAP_TEST(done_loops, i)) {
1040  /* Notes:
1041  * * In case of mono-loop smooth fan, we have nothing to do.
1042  * * Loops in this linklist are ordered (in reversed order compared to how they were
1043  * discovered by BKE_mesh_normals_loop_split(), but this is not a problem).
1044  * Which means if we find a mismatching clnor,
1045  * we know all remaining loops will have to be in a new, different smooth fan/lnor space.
1046  * * In smooth fan case, we compare each clnor against a ref one,
1047  * to avoid small differences adding up into a real big one in the end!
1048  */
1049  if (lnors_spacearr->lspacearr[i]->flags & MLNOR_SPACE_IS_SINGLE) {
1050  BLI_BITMAP_ENABLE(done_loops, i);
1051  continue;
1052  }
1053 
1054  LinkNode *loops = lnors_spacearr->lspacearr[i]->loops;
1055  BMLoop *prev_ml = NULL;
1056  const float *org_nor = NULL;
1057 
1058  while (loops) {
1059  BMLoop *ml = loops->link;
1060  const int lidx = BM_elem_index_get(ml);
1061  const float *nor = new_lnors[lidx];
1062 
1063  if (!org_nor) {
1064  org_nor = nor;
1065  }
1066  else if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1067  /* Current normal differs too much from org one, we have to tag the edge between
1068  * previous loop's face and current's one as sharp.
1069  * We know those two loops do not point to the same edge,
1070  * since we do not allow reversed winding in a same smooth fan.
1071  */
1072  BMEdge *e = (prev_ml->e == ml->prev->e) ? prev_ml->e : ml->e;
1073 
1075  changed = true;
1076 
1077  org_nor = nor;
1078  }
1079 
1080  prev_ml = ml;
1081  loops = loops->next;
1082  BLI_BITMAP_ENABLE(done_loops, lidx);
1083  }
1084 
1085  /* We also have to check between last and first loops,
1086  * otherwise we may miss some sharp edges here!
1087  * This is just a simplified version of above while loop.
1088  * See T45984. */
1089  loops = lnors_spacearr->lspacearr[i]->loops;
1090  if (loops && org_nor) {
1091  BMLoop *ml = loops->link;
1092  const int lidx = BM_elem_index_get(ml);
1093  const float *nor = new_lnors[lidx];
1094 
1095  if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1096  BMEdge *e = (prev_ml->e == ml->prev->e) ? prev_ml->e : ml->e;
1097 
1099  changed = true;
1100  }
1101  }
1102  }
1103  }
1104 
1105  MEM_freeN(done_loops);
1106  return changed;
1107 }
1108 
1114  MLoopNorSpaceArray *lnors_spacearr,
1115  short (*r_clnors_data)[2],
1116  const int cd_loop_clnors_offset,
1117  const float (*new_lnors)[3])
1118 {
1119  BLI_bitmap *done_loops = BLI_BITMAP_NEW((size_t)bm->totloop, __func__);
1120 
1121  BLI_SMALLSTACK_DECLARE(clnors_data, short *);
1122 
1123  BLI_assert(lnors_spacearr->data_type == MLNOR_SPACEARR_BMLOOP_PTR);
1124 
1125  for (int i = 0; i < bm->totloop; i++) {
1126  if (!lnors_spacearr->lspacearr[i]) {
1127  BLI_BITMAP_ENABLE(done_loops, i);
1128  if (G.debug & G_DEBUG) {
1129  printf("WARNING! Still getting invalid NULL loop space in second loop for loop %d!\n", i);
1130  }
1131  continue;
1132  }
1133 
1134  if (!BLI_BITMAP_TEST(done_loops, i)) {
1135  /* Note we accumulate and average all custom normals in current smooth fan,
1136  * to avoid getting different clnors data (tiny differences in plain custom normals can
1137  * give rather huge differences in computed 2D factors).
1138  */
1139  LinkNode *loops = lnors_spacearr->lspacearr[i]->loops;
1140 
1141  if (lnors_spacearr->lspacearr[i]->flags & MLNOR_SPACE_IS_SINGLE) {
1142  BMLoop *ml = (BMLoop *)loops;
1143  const int lidx = BM_elem_index_get(ml);
1144 
1145  BLI_assert(lidx == i);
1146 
1147  const float *nor = new_lnors[lidx];
1148  short *clnor = r_clnors_data ? &r_clnors_data[lidx] :
1149  BM_ELEM_CD_GET_VOID_P(ml, cd_loop_clnors_offset);
1150 
1151  BKE_lnor_space_custom_normal_to_data(lnors_spacearr->lspacearr[i], nor, clnor);
1152  BLI_BITMAP_ENABLE(done_loops, i);
1153  }
1154  else {
1155  int nbr_nors = 0;
1156  float avg_nor[3];
1157  short clnor_data_tmp[2], *clnor_data;
1158 
1159  zero_v3(avg_nor);
1160 
1161  while (loops) {
1162  BMLoop *ml = loops->link;
1163  const int lidx = BM_elem_index_get(ml);
1164  const float *nor = new_lnors[lidx];
1165  short *clnor = r_clnors_data ? &r_clnors_data[lidx] :
1166  BM_ELEM_CD_GET_VOID_P(ml, cd_loop_clnors_offset);
1167 
1168  nbr_nors++;
1169  add_v3_v3(avg_nor, nor);
1170  BLI_SMALLSTACK_PUSH(clnors_data, clnor);
1171 
1172  loops = loops->next;
1173  BLI_BITMAP_ENABLE(done_loops, lidx);
1174  }
1175 
1176  mul_v3_fl(avg_nor, 1.0f / (float)nbr_nors);
1178  lnors_spacearr->lspacearr[i], avg_nor, clnor_data_tmp);
1179 
1180  while ((clnor_data = BLI_SMALLSTACK_POP(clnors_data))) {
1181  clnor_data[0] = clnor_data_tmp[0];
1182  clnor_data[1] = clnor_data_tmp[1];
1183  }
1184  }
1185  }
1186  }
1187 
1188  MEM_freeN(done_loops);
1189 }
1190 
1199  const float (*vcos)[3],
1200  const float (*vnos)[3],
1201  const float (*fnos)[3],
1202  MLoopNorSpaceArray *r_lnors_spacearr,
1203  short (*r_clnors_data)[2],
1204  const int cd_loop_clnors_offset,
1205  float (*new_lnors)[3],
1206  const int cd_new_lnors_offset,
1207  bool do_split_fans)
1208 {
1209  BMFace *f;
1210  BMLoop *l;
1211  BMIter liter, fiter;
1212  float(*cur_lnors)[3] = MEM_mallocN(sizeof(*cur_lnors) * bm->totloop, __func__);
1213 
1214  BKE_lnor_spacearr_clear(r_lnors_spacearr);
1215 
1216  /* Tag smooth edges and set lnos from vnos when they might be completely smooth...
1217  * When using custom loop normals, disable the angle feature! */
1218  bm_mesh_edges_sharp_tag(bm, vnos, fnos, cur_lnors, (float)M_PI, false);
1219 
1220  /* Finish computing lnos by accumulating face normals
1221  * in each fan of faces defined by sharp edges. */
1223  bm, vcos, fnos, cur_lnors, r_lnors_spacearr, r_clnors_data, cd_loop_clnors_offset, false);
1224 
1225  /* Extract new normals from the data layer if necessary. */
1226  float(*custom_lnors)[3] = new_lnors;
1227 
1228  if (new_lnors == NULL) {
1229  custom_lnors = MEM_mallocN(sizeof(*new_lnors) * bm->totloop, __func__);
1230 
1231  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1232  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1233  const float *normal = BM_ELEM_CD_GET_VOID_P(l, cd_new_lnors_offset);
1234  copy_v3_v3(custom_lnors[BM_elem_index_get(l)], normal);
1235  }
1236  }
1237  }
1238 
1239  /* Validate the new normals. */
1240  for (int i = 0; i < bm->totloop; i++) {
1241  if (is_zero_v3(custom_lnors[i])) {
1242  copy_v3_v3(custom_lnors[i], cur_lnors[i]);
1243  }
1244  else {
1245  normalize_v3(custom_lnors[i]);
1246  }
1247  }
1248 
1249  /* Now, check each current smooth fan (one lnor space per smooth fan!),
1250  * and if all its matching custom lnors are not equal, add sharp edges as needed. */
1251  if (do_split_fans && bm_mesh_loops_split_lnor_fans(bm, r_lnors_spacearr, custom_lnors)) {
1252  /* If any sharp edges were added, run bm_mesh_loops_calc_normals() again to get lnor
1253  * spacearr/smooth fans matching the given custom lnors. */
1254  BKE_lnor_spacearr_clear(r_lnors_spacearr);
1255 
1257  bm, vcos, fnos, cur_lnors, r_lnors_spacearr, r_clnors_data, cd_loop_clnors_offset, false);
1258  }
1259 
1260  /* And we just have to convert plain object-space custom normals to our
1261  * lnor space-encoded ones. */
1263  bm, r_lnors_spacearr, r_clnors_data, cd_loop_clnors_offset, custom_lnors);
1264 
1265  MEM_freeN(cur_lnors);
1266 
1267  if (custom_lnors != new_lnors) {
1268  MEM_freeN(custom_lnors);
1269  }
1270 }
1271 
1273  const float (*vnos)[3],
1274  const float (*fnos)[3],
1275  float (*r_lnos)[3])
1276 {
1277  BMIter fiter;
1278  BMFace *f_curr;
1279 
1280  {
1281  char htype = BM_LOOP;
1282  if (vnos) {
1283  htype |= BM_VERT;
1284  }
1285  if (fnos) {
1286  htype |= BM_FACE;
1287  }
1288  BM_mesh_elem_index_ensure(bm, htype);
1289  }
1290 
1291  BM_ITER_MESH (f_curr, &fiter, bm, BM_FACES_OF_MESH) {
1292  BMLoop *l_curr, *l_first;
1293  const bool is_face_flat = !BM_elem_flag_test(f_curr, BM_ELEM_SMOOTH);
1294 
1295  l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
1296  do {
1297  const float *no = is_face_flat ? (fnos ? fnos[BM_elem_index_get(f_curr)] : f_curr->no) :
1298  (vnos ? vnos[BM_elem_index_get(l_curr->v)] : l_curr->v->no);
1299  copy_v3_v3(r_lnos[BM_elem_index_get(l_curr)], no);
1300 
1301  } while ((l_curr = l_curr->next) != l_first);
1302  }
1303 }
1304 
1305 #if 0 /* Unused currently */
1312 void BM_mesh_loop_normals_update(BMesh *bm,
1313  const bool use_split_normals,
1314  const float split_angle,
1315  float (*r_lnos)[3],
1316  MLoopNorSpaceArray *r_lnors_spacearr,
1317  const short (*clnors_data)[2],
1318  const int cd_loop_clnors_offset)
1319 {
1320  const bool has_clnors = clnors_data || (cd_loop_clnors_offset != -1);
1321 
1322  if (use_split_normals) {
1323  /* Tag smooth edges and set lnos from vnos when they might be completely smooth...
1324  * When using custom loop normals, disable the angle feature! */
1325  bm_mesh_edges_sharp_tag(bm, NULL, NULL, has_clnors ? (float)M_PI : split_angle, r_lnos);
1326 
1327  /* Finish computing lnos by accumulating face normals
1328  * in each fan of faces defined by sharp edges. */
1330  bm, NULL, NULL, r_lnos, r_lnors_spacearr, clnors_data, cd_loop_clnors_offset);
1331  }
1332  else {
1333  BLI_assert(!r_lnors_spacearr);
1335  }
1336 }
1337 #endif
1338 
1347  const float (*vcos)[3],
1348  const float (*vnos)[3],
1349  const float (*fnos)[3],
1350  const bool use_split_normals,
1351  const float split_angle,
1352  float (*r_lnos)[3],
1353  MLoopNorSpaceArray *r_lnors_spacearr,
1354  short (*clnors_data)[2],
1355  const int cd_loop_clnors_offset,
1356  const bool do_rebuild)
1357 {
1358  const bool has_clnors = clnors_data || (cd_loop_clnors_offset != -1);
1359 
1360  if (use_split_normals) {
1361  /* Tag smooth edges and set lnos from vnos when they might be completely smooth...
1362  * When using custom loop normals, disable the angle feature! */
1363  bm_mesh_edges_sharp_tag(bm, vnos, fnos, r_lnos, has_clnors ? (float)M_PI : split_angle, false);
1364 
1365  /* Finish computing lnos by accumulating face normals
1366  * in each fan of faces defined by sharp edges. */
1368  bm, vcos, fnos, r_lnos, r_lnors_spacearr, clnors_data, cd_loop_clnors_offset, do_rebuild);
1369  }
1370  else {
1371  BLI_assert(!r_lnors_spacearr);
1372  bm_mesh_loops_calc_normals_no_autosmooth(bm, vnos, fnos, r_lnos);
1373  }
1374 }
1375 
1382 void BM_edges_sharp_from_angle_set(BMesh *bm, const float split_angle)
1383 {
1384  if (split_angle >= (float)M_PI) {
1385  /* Nothing to do! */
1386  return;
1387  }
1388 
1389  bm_mesh_edges_sharp_tag(bm, NULL, NULL, NULL, split_angle, true);
1390 }
1391 
1392 void BM_lnorspacearr_store(BMesh *bm, float (*r_lnors)[3])
1393 {
1395 
1398  }
1399 
1400  int cd_loop_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1401 
1403  NULL,
1404  NULL,
1405  NULL,
1406  true,
1407  M_PI,
1408  r_lnors,
1409  bm->lnor_spacearr,
1410  NULL,
1411  cd_loop_clnors_offset,
1412  false);
1414 }
1415 
1416 #define CLEAR_SPACEARRAY_THRESHOLD(x) ((x) / 2)
1417 
1418 void BM_lnorspace_invalidate(BMesh *bm, const bool do_invalidate_all)
1419 {
1421  return;
1422  }
1423  if (do_invalidate_all || bm->totvertsel > CLEAR_SPACEARRAY_THRESHOLD(bm->totvert)) {
1425  return;
1426  }
1427  if (bm->lnor_spacearr == NULL) {
1429  return;
1430  }
1431 
1432  BMVert *v;
1433  BMLoop *l;
1434  BMIter viter, liter;
1435  /* Note: we could use temp tag of BMItem for that,
1436  * but probably better not use it in such a low-level func?
1437  * --mont29 */
1438  BLI_bitmap *done_verts = BLI_BITMAP_NEW(bm->totvert, __func__);
1439 
1441 
1442  /* When we affect a given vertex, we may affect following smooth fans:
1443  * - all smooth fans of said vertex;
1444  * - all smooth fans of all immediate loop-neighbors vertices;
1445  * This can be simplified as 'all loops of selected vertices and their immediate neighbors'
1446  * need to be tagged for update.
1447  */
1448  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1450  BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1452 
1453  /* Note that we only handle unselected neighbor vertices here, main loop will take care of
1454  * selected ones. */
1455  if ((!BM_elem_flag_test(l->prev->v, BM_ELEM_SELECT)) &&
1456  !BLI_BITMAP_TEST(done_verts, BM_elem_index_get(l->prev->v))) {
1457 
1458  BMLoop *l_prev;
1459  BMIter liter_prev;
1460  BM_ITER_ELEM (l_prev, &liter_prev, l->prev->v, BM_LOOPS_OF_VERT) {
1462  }
1463  BLI_BITMAP_ENABLE(done_verts, BM_elem_index_get(l_prev->v));
1464  }
1465 
1466  if ((!BM_elem_flag_test(l->next->v, BM_ELEM_SELECT)) &&
1467  !BLI_BITMAP_TEST(done_verts, BM_elem_index_get(l->next->v))) {
1468 
1469  BMLoop *l_next;
1470  BMIter liter_next;
1471  BM_ITER_ELEM (l_next, &liter_next, l->next->v, BM_LOOPS_OF_VERT) {
1473  }
1474  BLI_BITMAP_ENABLE(done_verts, BM_elem_index_get(l_next->v));
1475  }
1476  }
1477 
1478  BLI_BITMAP_ENABLE(done_verts, BM_elem_index_get(v));
1479  }
1480  }
1481 
1482  MEM_freeN(done_verts);
1484 }
1485 
1486 void BM_lnorspace_rebuild(BMesh *bm, bool preserve_clnor)
1487 {
1489 
1491  return;
1492  }
1493  BMFace *f;
1494  BMLoop *l;
1495  BMIter fiter, liter;
1496 
1497  float(*r_lnors)[3] = MEM_callocN(sizeof(*r_lnors) * bm->totloop, __func__);
1498  float(*oldnors)[3] = preserve_clnor ? MEM_mallocN(sizeof(*oldnors) * bm->totloop, __func__) :
1499  NULL;
1500 
1501  int cd_loop_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1502 
1504 
1505  if (preserve_clnor) {
1507 
1508  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1509  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1512  short(*clnor)[2] = BM_ELEM_CD_GET_VOID_P(l, cd_loop_clnors_offset);
1513  int l_index = BM_elem_index_get(l);
1514 
1516  bm->lnor_spacearr->lspacearr[l_index], *clnor, oldnors[l_index]);
1517  }
1518  }
1519  }
1520  }
1521 
1524  }
1526  NULL,
1527  NULL,
1528  NULL,
1529  true,
1530  M_PI,
1531  r_lnors,
1532  bm->lnor_spacearr,
1533  NULL,
1534  cd_loop_clnors_offset,
1535  true);
1536  MEM_freeN(r_lnors);
1537 
1538  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1539  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1542  if (preserve_clnor) {
1543  short(*clnor)[2] = BM_ELEM_CD_GET_VOID_P(l, cd_loop_clnors_offset);
1544  int l_index = BM_elem_index_get(l);
1546  bm->lnor_spacearr->lspacearr[l_index], oldnors[l_index], *clnor);
1547  }
1549  }
1550  }
1551  }
1552 
1553  MEM_SAFE_FREE(oldnors);
1555 
1556 #ifndef NDEBUG
1558 #endif
1559 }
1560 
1566 {
1567  if (bm->lnor_spacearr == NULL) {
1568  bm->lnor_spacearr = MEM_callocN(sizeof(*bm->lnor_spacearr), __func__);
1569  }
1570  if (bm->lnor_spacearr->lspacearr == NULL) {
1571  float(*lnors)[3] = MEM_callocN(sizeof(*lnors) * bm->totloop, __func__);
1572 
1573  BM_lnorspacearr_store(bm, lnors);
1574 
1575  MEM_freeN(lnors);
1576  }
1578  BM_lnorspace_rebuild(bm, false);
1579  }
1580 }
1581 
1582 void BM_normals_loops_edges_tag(BMesh *bm, const bool do_edges)
1583 {
1584  BMFace *f;
1585  BMEdge *e;
1586  BMIter fiter, eiter;
1587  BMLoop *l_curr, *l_first;
1588 
1589  if (do_edges) {
1590  int index_edge;
1591  BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, index_edge) {
1592  BMLoop *l_a, *l_b;
1593 
1594  BM_elem_index_set(e, index_edge); /* set_inline */
1596  if (BM_edge_loop_pair(e, &l_a, &l_b)) {
1597  if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) && l_a->v != l_b->v) {
1599  }
1600  }
1601  }
1603  }
1604 
1605  int index_face, index_loop = 0;
1606  BM_ITER_MESH_INDEX (f, &fiter, bm, BM_FACES_OF_MESH, index_face) {
1607  BM_elem_index_set(f, index_face); /* set_inline */
1608  l_curr = l_first = BM_FACE_FIRST_LOOP(f);
1609  do {
1610  BM_elem_index_set(l_curr, index_loop++); /* set_inline */
1612  } while ((l_curr = l_curr->next) != l_first);
1613  }
1615 }
1616 
1622 #ifndef NDEBUG
1624 {
1626  bool clear = true;
1627 
1628  MLoopNorSpaceArray *temp = MEM_callocN(sizeof(*temp), __func__);
1629  temp->lspacearr = NULL;
1630 
1632 
1633  int cd_loop_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1634  float(*lnors)[3] = MEM_callocN(sizeof(*lnors) * bm->totloop, __func__);
1636  bm, NULL, NULL, NULL, true, M_PI, lnors, temp, NULL, cd_loop_clnors_offset, true);
1637 
1638  for (int i = 0; i < bm->totloop; i++) {
1639  int j = 0;
1640  j += compare_ff(
1641  temp->lspacearr[i]->ref_alpha, bm->lnor_spacearr->lspacearr[i]->ref_alpha, 1e-4f);
1642  j += compare_ff(
1643  temp->lspacearr[i]->ref_beta, bm->lnor_spacearr->lspacearr[i]->ref_beta, 1e-4f);
1644  j += compare_v3v3(
1645  temp->lspacearr[i]->vec_lnor, bm->lnor_spacearr->lspacearr[i]->vec_lnor, 1e-4f);
1646  j += compare_v3v3(
1647  temp->lspacearr[i]->vec_ortho, bm->lnor_spacearr->lspacearr[i]->vec_ortho, 1e-4f);
1648  j += compare_v3v3(
1649  temp->lspacearr[i]->vec_ref, bm->lnor_spacearr->lspacearr[i]->vec_ref, 1e-4f);
1650 
1651  if (j != 5) {
1652  clear = false;
1653  break;
1654  }
1655  }
1656  BKE_lnor_spacearr_free(temp);
1657  MEM_freeN(temp);
1658  MEM_freeN(lnors);
1659  BLI_assert(clear);
1660 
1662 }
1663 #endif
1664 
1666  BLI_bitmap *loops,
1667  MLoopNorSpaceArray *lnor_spacearr,
1668  int *totloopsel,
1669  const bool do_all_loops_of_vert)
1670 {
1671  if (l != NULL) {
1672  const int l_idx = BM_elem_index_get(l);
1673 
1674  if (!BLI_BITMAP_TEST(loops, l_idx)) {
1675  /* If vert and face selected share a loop, mark it for editing. */
1676  BLI_BITMAP_ENABLE(loops, l_idx);
1677  (*totloopsel)++;
1678 
1679  if (do_all_loops_of_vert) {
1680  /* If required, also mark all loops shared by that vertex.
1681  * This is needed when loop spaces may change
1682  * (i.e. when some faces or edges might change of smooth/sharp status). */
1683  BMIter liter;
1684  BMLoop *lfan;
1685  BM_ITER_ELEM (lfan, &liter, l->v, BM_LOOPS_OF_VERT) {
1686  const int lfan_idx = BM_elem_index_get(lfan);
1687  if (!BLI_BITMAP_TEST(loops, lfan_idx)) {
1688  BLI_BITMAP_ENABLE(loops, lfan_idx);
1689  (*totloopsel)++;
1690  }
1691  }
1692  }
1693  else {
1694  /* Mark all loops in same loop normal space (aka smooth fan). */
1695  if ((lnor_spacearr->lspacearr[l_idx]->flags & MLNOR_SPACE_IS_SINGLE) == 0) {
1696  for (LinkNode *node = lnor_spacearr->lspacearr[l_idx]->loops; node; node = node->next) {
1697  const int lfan_idx = BM_elem_index_get((BMLoop *)node->link);
1698  if (!BLI_BITMAP_TEST(loops, lfan_idx)) {
1699  BLI_BITMAP_ENABLE(loops, lfan_idx);
1700  (*totloopsel)++;
1701  }
1702  }
1703  }
1704  }
1705  }
1706  }
1707 }
1708 
1709 /* Mark the individual clnors to be edited, if multiple selection methods are used. */
1710 static int bm_loop_normal_mark_indiv(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
1711 {
1712  BMEditSelection *ese, *ese_prev;
1713  int totloopsel = 0;
1714 
1715  const bool sel_verts = (bm->selectmode & SCE_SELECT_VERTEX) != 0;
1716  const bool sel_edges = (bm->selectmode & SCE_SELECT_EDGE) != 0;
1717  const bool sel_faces = (bm->selectmode & SCE_SELECT_FACE) != 0;
1718  const bool use_sel_face_history = sel_faces && (sel_edges || sel_verts);
1719 
1721 
1724 
1725  if (use_sel_face_history) {
1726  /* Using face history allows to select a single loop from a single face...
1727  * Note that this is On² piece of code,
1728  * but it is not designed to be used with huge selection sets,
1729  * rather with only a few items selected at most.*/
1730  /* Goes from last selected to the first selected element. */
1731  for (ese = bm->selected.last; ese; ese = ese->prev) {
1732  if (ese->htype == BM_FACE) {
1733  /* If current face is selected,
1734  * then any verts to be edited must have been selected before it. */
1735  for (ese_prev = ese->prev; ese_prev; ese_prev = ese_prev->prev) {
1736  if (ese_prev->htype == BM_VERT) {
1738  BM_face_vert_share_loop((BMFace *)ese->ele, (BMVert *)ese_prev->ele),
1739  loops,
1740  bm->lnor_spacearr,
1741  &totloopsel,
1742  do_all_loops_of_vert);
1743  }
1744  else if (ese_prev->htype == BM_EDGE) {
1745  BMEdge *e = (BMEdge *)ese_prev->ele;
1747  loops,
1748  bm->lnor_spacearr,
1749  &totloopsel,
1750  do_all_loops_of_vert);
1751 
1753  loops,
1754  bm->lnor_spacearr,
1755  &totloopsel,
1756  do_all_loops_of_vert);
1757  }
1758  }
1759  }
1760  }
1761  }
1762  else {
1763  if (sel_faces) {
1764  /* Only select all loops of selected faces. */
1765  BMLoop *l;
1766  BMFace *f;
1767  BMIter liter, fiter;
1768  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1770  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1772  l, loops, bm->lnor_spacearr, &totloopsel, do_all_loops_of_vert);
1773  }
1774  }
1775  }
1776  }
1777  if (sel_edges) {
1778  /* Only select all loops of selected edges. */
1779  BMLoop *l;
1780  BMEdge *e;
1781  BMIter liter, eiter;
1782  BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
1784  BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
1786  l, loops, bm->lnor_spacearr, &totloopsel, do_all_loops_of_vert);
1787  /* Loops actually 'have' two edges, or said otherwise, a selected edge actually selects
1788  * *two* loops in each of its faces. We have to find the other one too. */
1789  if (BM_vert_in_edge(e, l->next->v)) {
1791  l->next, loops, bm->lnor_spacearr, &totloopsel, do_all_loops_of_vert);
1792  }
1793  else {
1796  l->prev, loops, bm->lnor_spacearr, &totloopsel, do_all_loops_of_vert);
1797  }
1798  }
1799  }
1800  }
1801  }
1802  if (sel_verts) {
1803  /* Select all loops of selected verts. */
1804  BMLoop *l;
1805  BMVert *v;
1806  BMIter liter, viter;
1807  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1809  BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1811  l, loops, bm->lnor_spacearr, &totloopsel, do_all_loops_of_vert);
1812  }
1813  }
1814  }
1815  }
1816  }
1817 
1818  return totloopsel;
1819 }
1820 
1822  BMesh *bm, BMLoopNorEditData *lnor_ed, BMVert *v, BMLoop *l, const int offset)
1823 {
1826 
1827  const int l_index = BM_elem_index_get(l);
1828  short *clnors_data = BM_ELEM_CD_GET_VOID_P(l, offset);
1829 
1830  lnor_ed->loop_index = l_index;
1831  lnor_ed->loop = l;
1832 
1833  float custom_normal[3];
1835  bm->lnor_spacearr->lspacearr[l_index], clnors_data, custom_normal);
1836 
1837  lnor_ed->clnors_data = clnors_data;
1838  copy_v3_v3(lnor_ed->nloc, custom_normal);
1839  copy_v3_v3(lnor_ed->niloc, custom_normal);
1840 
1841  lnor_ed->loc = v->co;
1842 }
1843 
1845  const bool do_all_loops_of_vert)
1846 {
1847  BMLoop *l;
1848  BMVert *v;
1849  BMIter liter, viter;
1850 
1851  int totloopsel = 0;
1852 
1853  BLI_assert(bm->spacearr_dirty == 0);
1854 
1855  BMLoopNorEditDataArray *lnors_ed_arr = MEM_callocN(sizeof(*lnors_ed_arr), __func__);
1856  lnors_ed_arr->lidx_to_lnor_editdata = MEM_callocN(
1857  sizeof(*lnors_ed_arr->lidx_to_lnor_editdata) * bm->totloop, __func__);
1858 
1861  }
1862  const int cd_custom_normal_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1863 
1865 
1866  BLI_bitmap *loops = BLI_BITMAP_NEW(bm->totloop, __func__);
1867 
1868  /* This function define loop normals to edit, based on selection modes and history. */
1869  totloopsel = bm_loop_normal_mark_indiv(bm, loops, do_all_loops_of_vert);
1870 
1871  if (totloopsel) {
1872  BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata = MEM_mallocN(
1873  sizeof(*lnor_ed) * totloopsel, __func__);
1874 
1875  BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1876  BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1877  if (BLI_BITMAP_TEST(loops, BM_elem_index_get(l))) {
1878  loop_normal_editdata_init(bm, lnor_ed, v, l, cd_custom_normal_offset);
1879  lnors_ed_arr->lidx_to_lnor_editdata[BM_elem_index_get(l)] = lnor_ed;
1880  lnor_ed++;
1881  }
1882  }
1883  }
1884  lnors_ed_arr->totloop = totloopsel;
1885  }
1886 
1887  MEM_freeN(loops);
1888  lnors_ed_arr->cd_custom_normal_offset = cd_custom_normal_offset;
1889  return lnors_ed_arr;
1890 }
1891 
1893 {
1894  MEM_SAFE_FREE(lnors_ed_arr->lnor_editdata);
1895  MEM_SAFE_FREE(lnors_ed_arr->lidx_to_lnor_editdata);
1896  MEM_freeN(lnors_ed_arr);
1897 }
1898 
1904 {
1905  BMFace *f;
1906  BMLoop *l;
1907  BMIter liter, fiter;
1908 
1910  return false;
1911  }
1912 
1915 
1916  /* Create a loop normal layer. */
1919 
1921  }
1922 
1923  const int cd_custom_normal_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1924  const int cd_normal_offset = CustomData_get_offset(&bm->ldata, CD_NORMAL);
1925 
1926  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1927  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1928  const int l_index = BM_elem_index_get(l);
1929  const short *clnors_data = BM_ELEM_CD_GET_VOID_P(l, cd_custom_normal_offset);
1930  float *normal = BM_ELEM_CD_GET_VOID_P(l, cd_normal_offset);
1931 
1933  bm->lnor_spacearr->lspacearr[l_index], clnors_data, normal);
1934  }
1935  }
1936 
1937  return true;
1938 }
1939 
1941 {
1944  return;
1945  }
1946 
1947  const int cd_custom_normal_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
1948  const int cd_normal_offset = CustomData_get_offset(&bm->ldata, CD_NORMAL);
1949 
1950  if (bm->lnor_spacearr == NULL) {
1951  bm->lnor_spacearr = MEM_callocN(sizeof(*bm->lnor_spacearr), __func__);
1952  }
1953 
1955  NULL,
1956  NULL,
1957  NULL,
1958  bm->lnor_spacearr,
1959  NULL,
1960  cd_custom_normal_offset,
1961  NULL,
1962  cd_normal_offset,
1963  add_sharp_edges);
1964 
1966 }
1967 
1976 {
1977  /* Most operators seem to be using BMO_OPTYPE_FLAG_UNTAN_MULTIRES to change the MDisps to
1978  * absolute space during mesh edits. With this enabled, changes to the topology
1979  * (loop cuts, edge subdivides, etc) are not reflected in the higher levels of
1980  * the mesh at all, which doesn't seem right. Turning off completely for now,
1981  * until this is shown to be better for certain types of mesh edits. */
1982 #ifdef BMOP_UNTAN_MULTIRES_ENABLED
1983  /* switch multires data out of tangent space */
1984  if ((type_flag & BMO_OPTYPE_FLAG_UNTAN_MULTIRES) &&
1986  bmesh_mdisps_space_set(bm, MULTIRES_SPACE_TANGENT, MULTIRES_SPACE_ABSOLUTE);
1987 
1988  /* ensure correct normals, if possible */
1989  bmesh_rationalize_normals(bm, 0);
1991  }
1992 #endif
1993 }
1994 
1999 {
2000  ListBase select_history;
2001 
2002  /* BMO_OPTYPE_FLAG_UNTAN_MULTIRES disabled for now, see comment above in bmesh_edit_begin. */
2003 #ifdef BMOP_UNTAN_MULTIRES_ENABLED
2004  /* switch multires data into tangent space */
2006  /* set normals to their previous winding */
2007  bmesh_rationalize_normals(bm, 1);
2008  bmesh_mdisps_space_set(bm, MULTIRES_SPACE_ABSOLUTE, MULTIRES_SPACE_TANGENT);
2009  }
2010  else if (flag & BMO_OP_FLAG_RATIONALIZE_NORMALS) {
2011  bmesh_rationalize_normals(bm, 1);
2012  }
2013 #endif
2014 
2015  /* compute normals, clear temp flags and flush selections */
2016  if (type_flag & BMO_OPTYPE_FLAG_NORMALS_CALC) {
2019  }
2020 
2021  if ((type_flag & BMO_OPTYPE_FLAG_SELECT_VALIDATE) == 0) {
2022  select_history = bm->selected;
2024  }
2025 
2026  if (type_flag & BMO_OPTYPE_FLAG_SELECT_FLUSH) {
2028  }
2029 
2030  if ((type_flag & BMO_OPTYPE_FLAG_SELECT_VALIDATE) == 0) {
2031  bm->selected = select_history;
2032  }
2033  if (type_flag & BMO_OPTYPE_FLAG_INVALIDATE_CLNOR_ALL) {
2035  }
2036 }
2037 
2038 void BM_mesh_elem_index_ensure_ex(BMesh *bm, const char htype, int elem_offset[4])
2039 {
2040 
2041 #ifdef DEBUG
2042  BM_ELEM_INDEX_VALIDATE(bm, "Should Never Fail!", __func__);
2043 #endif
2044 
2045  if (elem_offset == NULL) {
2046  /* Simple case. */
2047  const char htype_needed = bm->elem_index_dirty & htype;
2048  if (htype_needed == 0) {
2049  goto finally;
2050  }
2051  }
2052 
2053  if (htype & BM_VERT) {
2054  if ((bm->elem_index_dirty & BM_VERT) || (elem_offset && elem_offset[0])) {
2055  BMIter iter;
2056  BMElem *ele;
2057 
2058  int index = elem_offset ? elem_offset[0] : 0;
2059  BM_ITER_MESH (ele, &iter, bm, BM_VERTS_OF_MESH) {
2060  BM_elem_index_set(ele, index++); /* set_ok */
2061  }
2062  BLI_assert(elem_offset || index == bm->totvert);
2063  }
2064  else {
2065  // printf("%s: skipping vert index calc!\n", __func__);
2066  }
2067  }
2068 
2069  if (htype & BM_EDGE) {
2070  if ((bm->elem_index_dirty & BM_EDGE) || (elem_offset && elem_offset[1])) {
2071  BMIter iter;
2072  BMElem *ele;
2073 
2074  int index = elem_offset ? elem_offset[1] : 0;
2075  BM_ITER_MESH (ele, &iter, bm, BM_EDGES_OF_MESH) {
2076  BM_elem_index_set(ele, index++); /* set_ok */
2077  }
2078  BLI_assert(elem_offset || index == bm->totedge);
2079  }
2080  else {
2081  // printf("%s: skipping edge index calc!\n", __func__);
2082  }
2083  }
2084 
2085  if (htype & (BM_FACE | BM_LOOP)) {
2086  if ((bm->elem_index_dirty & (BM_FACE | BM_LOOP)) ||
2087  (elem_offset && (elem_offset[2] || elem_offset[3]))) {
2088  BMIter iter;
2089  BMElem *ele;
2090 
2091  const bool update_face = (htype & BM_FACE) && (bm->elem_index_dirty & BM_FACE);
2092  const bool update_loop = (htype & BM_LOOP) && (bm->elem_index_dirty & BM_LOOP);
2093 
2094  int index_loop = elem_offset ? elem_offset[2] : 0;
2095  int index = elem_offset ? elem_offset[3] : 0;
2096 
2097  BM_ITER_MESH (ele, &iter, bm, BM_FACES_OF_MESH) {
2098  if (update_face) {
2099  BM_elem_index_set(ele, index++); /* set_ok */
2100  }
2101 
2102  if (update_loop) {
2103  BMLoop *l_iter, *l_first;
2104 
2105  l_iter = l_first = BM_FACE_FIRST_LOOP((BMFace *)ele);
2106  do {
2107  BM_elem_index_set(l_iter, index_loop++); /* set_ok */
2108  } while ((l_iter = l_iter->next) != l_first);
2109  }
2110  }
2111 
2112  BLI_assert(elem_offset || !update_face || index == bm->totface);
2113  if (update_loop) {
2114  BLI_assert(elem_offset || !update_loop || index_loop == bm->totloop);
2115  }
2116  }
2117  else {
2118  // printf("%s: skipping face/loop index calc!\n", __func__);
2119  }
2120  }
2121 
2122 finally:
2123  bm->elem_index_dirty &= ~htype;
2124  if (elem_offset) {
2125  if (htype & BM_VERT) {
2126  elem_offset[0] += bm->totvert;
2127  if (elem_offset[0] != bm->totvert) {
2129  }
2130  }
2131  if (htype & BM_EDGE) {
2132  elem_offset[1] += bm->totedge;
2133  if (elem_offset[1] != bm->totedge) {
2135  }
2136  }
2137  if (htype & BM_LOOP) {
2138  elem_offset[2] += bm->totloop;
2139  if (elem_offset[2] != bm->totloop) {
2141  }
2142  }
2143  if (htype & BM_FACE) {
2144  elem_offset[3] += bm->totface;
2145  if (elem_offset[3] != bm->totface) {
2147  }
2148  }
2149  }
2150 }
2151 
2152 void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
2153 {
2155 }
2156 
2170  BMesh *bm, const char *location, const char *func, const char *msg_a, const char *msg_b)
2171 {
2172  const char iter_types[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
2173 
2174  const char flag_types[3] = {BM_VERT, BM_EDGE, BM_FACE};
2175  const char *type_names[3] = {"vert", "edge", "face"};
2176 
2177  BMIter iter;
2178  BMElem *ele;
2179  int i;
2180  bool is_any_error = 0;
2181 
2182  for (i = 0; i < 3; i++) {
2183  const bool is_dirty = (flag_types[i] & bm->elem_index_dirty) != 0;
2184  int index = 0;
2185  bool is_error = false;
2186  int err_val = 0;
2187  int err_idx = 0;
2188 
2189  BM_ITER_MESH (ele, &iter, bm, iter_types[i]) {
2190  if (!is_dirty) {
2191  if (BM_elem_index_get(ele) != index) {
2192  err_val = BM_elem_index_get(ele);
2193  err_idx = index;
2194  is_error = true;
2195  break;
2196  }
2197  }
2198  index++;
2199  }
2200 
2201  if ((is_error == true) && (is_dirty == false)) {
2202  is_any_error = true;
2203  fprintf(stderr,
2204  "Invalid Index: at %s, %s, %s[%d] invalid index %d, '%s', '%s'\n",
2205  location,
2206  func,
2207  type_names[i],
2208  err_idx,
2209  err_val,
2210  msg_a,
2211  msg_b);
2212  }
2213  else if ((is_error == false) && (is_dirty == true)) {
2214 
2215 #if 0 /* mostly annoying */
2216 
2217  /* dirty may have been incorrectly set */
2218  fprintf(stderr,
2219  "Invalid Dirty: at %s, %s (%s), dirty flag was set but all index values are "
2220  "correct, '%s', '%s'\n",
2221  location,
2222  func,
2223  type_names[i],
2224  msg_a,
2225  msg_b);
2226 #endif
2227  }
2228  }
2229 
2230 #if 0 /* mostly annoying, even in debug mode */
2231 # ifdef DEBUG
2232  if (is_any_error == 0) {
2233  fprintf(stderr, "Valid Index Success: at %s, %s, '%s', '%s'\n", location, func, msg_a, msg_b);
2234  }
2235 # endif
2236 #endif
2237  (void)is_any_error; /* shut up the compiler */
2238 }
2239 
2240 /* debug check only - no need to optimize */
2241 #ifndef NDEBUG
2243 {
2244  BMIter iter;
2245  BMElem *ele;
2246  int i;
2247 
2248  if (bm->vtable && ((bm->elem_table_dirty & BM_VERT) == 0)) {
2249  BM_ITER_MESH_INDEX (ele, &iter, bm, BM_VERTS_OF_MESH, i) {
2250  if (ele != (BMElem *)bm->vtable[i]) {
2251  return false;
2252  }
2253  }
2254  }
2255 
2256  if (bm->etable && ((bm->elem_table_dirty & BM_EDGE) == 0)) {
2257  BM_ITER_MESH_INDEX (ele, &iter, bm, BM_EDGES_OF_MESH, i) {
2258  if (ele != (BMElem *)bm->etable[i]) {
2259  return false;
2260  }
2261  }
2262  }
2263 
2264  if (bm->ftable && ((bm->elem_table_dirty & BM_FACE) == 0)) {
2265  BM_ITER_MESH_INDEX (ele, &iter, bm, BM_FACES_OF_MESH, i) {
2266  if (ele != (BMElem *)bm->ftable[i]) {
2267  return false;
2268  }
2269  }
2270  }
2271 
2272  return true;
2273 }
2274 #endif
2275 
2276 void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
2277 {
2278  /* assume if the array is non-null then its valid and no need to recalc */
2279  const char htype_needed =
2280  (((bm->vtable && ((bm->elem_table_dirty & BM_VERT) == 0)) ? 0 : BM_VERT) |
2281  ((bm->etable && ((bm->elem_table_dirty & BM_EDGE) == 0)) ? 0 : BM_EDGE) |
2282  ((bm->ftable && ((bm->elem_table_dirty & BM_FACE) == 0)) ? 0 : BM_FACE)) &
2283  htype;
2284 
2285  BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
2286 
2287  /* in debug mode double check we didn't need to recalculate */
2289 
2290  if (htype_needed == 0) {
2291  goto finally;
2292  }
2293 
2294  if (htype_needed & BM_VERT) {
2295  if (bm->vtable && bm->totvert <= bm->vtable_tot && bm->totvert * 2 >= bm->vtable_tot) {
2296  /* pass (re-use the array) */
2297  }
2298  else {
2299  if (bm->vtable) {
2300  MEM_freeN(bm->vtable);
2301  }
2302  bm->vtable = MEM_mallocN(sizeof(void **) * bm->totvert, "bm->vtable");
2303  bm->vtable_tot = bm->totvert;
2304  }
2305  }
2306  if (htype_needed & BM_EDGE) {
2307  if (bm->etable && bm->totedge <= bm->etable_tot && bm->totedge * 2 >= bm->etable_tot) {
2308  /* pass (re-use the array) */
2309  }
2310  else {
2311  if (bm->etable) {
2312  MEM_freeN(bm->etable);
2313  }
2314  bm->etable = MEM_mallocN(sizeof(void **) * bm->totedge, "bm->etable");
2315  bm->etable_tot = bm->totedge;
2316  }
2317  }
2318  if (htype_needed & BM_FACE) {
2319  if (bm->ftable && bm->totface <= bm->ftable_tot && bm->totface * 2 >= bm->ftable_tot) {
2320  /* pass (re-use the array) */
2321  }
2322  else {
2323  if (bm->ftable) {
2324  MEM_freeN(bm->ftable);
2325  }
2326  bm->ftable = MEM_mallocN(sizeof(void **) * bm->totface, "bm->ftable");
2327  bm->ftable_tot = bm->totface;
2328  }
2329  }
2330 
2331  if (htype_needed & BM_VERT) {
2333  }
2334 
2335  if (htype_needed & BM_EDGE) {
2337  }
2338 
2339  if (htype_needed & BM_FACE) {
2341  }
2342 
2343 finally:
2344  /* Only clear dirty flags when all the pointers and data are actually valid.
2345  * This prevents possible threading issues when dirty flag check failed but
2346  * data wasn't ready still.
2347  */
2348  bm->elem_table_dirty &= ~htype_needed;
2349 }
2350 
2351 /* use BM_mesh_elem_table_ensure where possible to avoid full rebuild */
2352 void BM_mesh_elem_table_init(BMesh *bm, const char htype)
2353 {
2354  BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
2355 
2356  /* force recalc */
2358  BM_mesh_elem_table_ensure(bm, htype);
2359 }
2360 
2361 void BM_mesh_elem_table_free(BMesh *bm, const char htype)
2362 {
2363  if (htype & BM_VERT) {
2365  }
2366 
2367  if (htype & BM_EDGE) {
2369  }
2370 
2371  if (htype & BM_FACE) {
2373  }
2374 }
2375 
2377 {
2378  return BLI_mempool_findelem(bm->vpool, index);
2379 }
2380 
2382 {
2383  return BLI_mempool_findelem(bm->epool, index);
2384 }
2385 
2387 {
2388  return BLI_mempool_findelem(bm->fpool, index);
2389 }
2390 
2392 {
2393  BMIter iter;
2394  BMFace *f;
2395  int i = index;
2396  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
2397  if (i < f->len) {
2398  BMLoop *l_first, *l_iter;
2399  l_iter = l_first = BM_FACE_FIRST_LOOP(f);
2400  do {
2401  if (i == 0) {
2402  return l_iter;
2403  }
2404  i -= 1;
2405  } while ((l_iter = l_iter->next) != l_first);
2406  }
2407  i -= f->len;
2408  }
2409  return NULL;
2410 }
2411 
2418 {
2419  if ((bm->elem_table_dirty & BM_VERT) == 0) {
2420  return (index < bm->totvert) ? bm->vtable[index] : NULL;
2421  }
2422  return BM_vert_at_index_find(bm, index);
2423 }
2424 
2426 {
2427  if ((bm->elem_table_dirty & BM_EDGE) == 0) {
2428  return (index < bm->totedge) ? bm->etable[index] : NULL;
2429  }
2430  return BM_edge_at_index_find(bm, index);
2431 }
2432 
2434 {
2435  if ((bm->elem_table_dirty & BM_FACE) == 0) {
2436  return (index < bm->totface) ? bm->ftable[index] : NULL;
2437  }
2438  return BM_face_at_index_find(bm, index);
2439 }
2440 
2444 int BM_mesh_elem_count(BMesh *bm, const char htype)
2445 {
2446  BLI_assert((htype & ~BM_ALL_NOLOOP) == 0);
2447 
2448  switch (htype) {
2449  case BM_VERT:
2450  return bm->totvert;
2451  case BM_EDGE:
2452  return bm->totedge;
2453  case BM_FACE:
2454  return bm->totface;
2455  default: {
2456  BLI_assert(0);
2457  return 0;
2458  }
2459  }
2460 }
2461 
2476 void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const uint *face_idx)
2477 {
2478  /* Mapping old to new pointers. */
2479  GHash *vptr_map = NULL, *eptr_map = NULL, *fptr_map = NULL;
2480  BMIter iter, iterl;
2481  BMVert *ve;
2482  BMEdge *ed;
2483  BMFace *fa;
2484  BMLoop *lo;
2485 
2486  if (!(vert_idx || edge_idx || face_idx)) {
2487  return;
2488  }
2489 
2491  bm, (vert_idx ? BM_VERT : 0) | (edge_idx ? BM_EDGE : 0) | (face_idx ? BM_FACE : 0));
2492 
2493  /* Remap Verts */
2494  if (vert_idx) {
2495  BMVert **verts_pool, *verts_copy, **vep;
2496  int i, totvert = bm->totvert;
2497  const uint *new_idx;
2498  /* Special case: Python uses custom - data layers to hold PyObject references.
2499  * These have to be kept in - place, else the PyObject's we point to, wont point back to us. */
2500  const int cd_vert_pyptr = CustomData_get_offset(&bm->vdata, CD_BM_ELEM_PYPTR);
2501 
2502  /* Init the old-to-new vert pointers mapping */
2503  vptr_map = BLI_ghash_ptr_new_ex("BM_mesh_remap vert pointers mapping", bm->totvert);
2504 
2505  /* Make a copy of all vertices. */
2506  verts_pool = bm->vtable;
2507  verts_copy = MEM_mallocN(sizeof(BMVert) * totvert, "BM_mesh_remap verts copy");
2508  void **pyptrs = (cd_vert_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totvert, __func__) : NULL;
2509  for (i = totvert, ve = verts_copy + totvert - 1, vep = verts_pool + totvert - 1; i--;
2510  ve--, vep--) {
2511  *ve = **vep;
2512  /* printf("*vep: %p, verts_pool[%d]: %p\n", *vep, i, verts_pool[i]);*/
2513  if (cd_vert_pyptr != -1) {
2514  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)ve), cd_vert_pyptr);
2515  pyptrs[i] = *pyptr;
2516  }
2517  }
2518 
2519  /* Copy back verts to their new place, and update old2new pointers mapping. */
2520  new_idx = vert_idx + totvert - 1;
2521  ve = verts_copy + totvert - 1;
2522  vep = verts_pool + totvert - 1; /* old, org pointer */
2523  for (i = totvert; i--; new_idx--, ve--, vep--) {
2524  BMVert *new_vep = verts_pool[*new_idx];
2525  *new_vep = *ve;
2526 #if 0
2527  printf(
2528  "mapping vert from %d to %d (%p/%p to %p)\n", i, *new_idx, *vep, verts_pool[i], new_vep);
2529 #endif
2530  BLI_ghash_insert(vptr_map, *vep, new_vep);
2531  if (cd_vert_pyptr != -1) {
2532  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_vep), cd_vert_pyptr);
2533  *pyptr = pyptrs[*new_idx];
2534  }
2535  }
2538 
2539  MEM_freeN(verts_copy);
2540  if (pyptrs) {
2541  MEM_freeN(pyptrs);
2542  }
2543  }
2544 
2545  /* Remap Edges */
2546  if (edge_idx) {
2547  BMEdge **edges_pool, *edges_copy, **edp;
2548  int i, totedge = bm->totedge;
2549  const uint *new_idx;
2550  /* Special case: Python uses custom - data layers to hold PyObject references.
2551  * These have to be kept in - place, else the PyObject's we point to, wont point back to us. */
2552  const int cd_edge_pyptr = CustomData_get_offset(&bm->edata, CD_BM_ELEM_PYPTR);
2553 
2554  /* Init the old-to-new vert pointers mapping */
2555  eptr_map = BLI_ghash_ptr_new_ex("BM_mesh_remap edge pointers mapping", bm->totedge);
2556 
2557  /* Make a copy of all vertices. */
2558  edges_pool = bm->etable;
2559  edges_copy = MEM_mallocN(sizeof(BMEdge) * totedge, "BM_mesh_remap edges copy");
2560  void **pyptrs = (cd_edge_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totedge, __func__) : NULL;
2561  for (i = totedge, ed = edges_copy + totedge - 1, edp = edges_pool + totedge - 1; i--;
2562  ed--, edp--) {
2563  *ed = **edp;
2564  if (cd_edge_pyptr != -1) {
2565  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)ed), cd_edge_pyptr);
2566  pyptrs[i] = *pyptr;
2567  }
2568  }
2569 
2570  /* Copy back verts to their new place, and update old2new pointers mapping. */
2571  new_idx = edge_idx + totedge - 1;
2572  ed = edges_copy + totedge - 1;
2573  edp = edges_pool + totedge - 1; /* old, org pointer */
2574  for (i = totedge; i--; new_idx--, ed--, edp--) {
2575  BMEdge *new_edp = edges_pool[*new_idx];
2576  *new_edp = *ed;
2577  BLI_ghash_insert(eptr_map, *edp, new_edp);
2578 #if 0
2579  printf(
2580  "mapping edge from %d to %d (%p/%p to %p)\n", i, *new_idx, *edp, edges_pool[i], new_edp);
2581 #endif
2582  if (cd_edge_pyptr != -1) {
2583  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_edp), cd_edge_pyptr);
2584  *pyptr = pyptrs[*new_idx];
2585  }
2586  }
2589 
2590  MEM_freeN(edges_copy);
2591  if (pyptrs) {
2592  MEM_freeN(pyptrs);
2593  }
2594  }
2595 
2596  /* Remap Faces */
2597  if (face_idx) {
2598  BMFace **faces_pool, *faces_copy, **fap;
2599  int i, totface = bm->totface;
2600  const uint *new_idx;
2601  /* Special case: Python uses custom - data layers to hold PyObject references.
2602  * These have to be kept in - place, else the PyObject's we point to, wont point back to us. */
2603  const int cd_poly_pyptr = CustomData_get_offset(&bm->pdata, CD_BM_ELEM_PYPTR);
2604 
2605  /* Init the old-to-new vert pointers mapping */
2606  fptr_map = BLI_ghash_ptr_new_ex("BM_mesh_remap face pointers mapping", bm->totface);
2607 
2608  /* Make a copy of all vertices. */
2609  faces_pool = bm->ftable;
2610  faces_copy = MEM_mallocN(sizeof(BMFace) * totface, "BM_mesh_remap faces copy");
2611  void **pyptrs = (cd_poly_pyptr != -1) ? MEM_mallocN(sizeof(void *) * totface, __func__) : NULL;
2612  for (i = totface, fa = faces_copy + totface - 1, fap = faces_pool + totface - 1; i--;
2613  fa--, fap--) {
2614  *fa = **fap;
2615  if (cd_poly_pyptr != -1) {
2616  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)fa), cd_poly_pyptr);
2617  pyptrs[i] = *pyptr;
2618  }
2619  }
2620 
2621  /* Copy back verts to their new place, and update old2new pointers mapping. */
2622  new_idx = face_idx + totface - 1;
2623  fa = faces_copy + totface - 1;
2624  fap = faces_pool + totface - 1; /* old, org pointer */
2625  for (i = totface; i--; new_idx--, fa--, fap--) {
2626  BMFace *new_fap = faces_pool[*new_idx];
2627  *new_fap = *fa;
2628  BLI_ghash_insert(fptr_map, *fap, new_fap);
2629  if (cd_poly_pyptr != -1) {
2630  void **pyptr = BM_ELEM_CD_GET_VOID_P(((BMElem *)new_fap), cd_poly_pyptr);
2631  *pyptr = pyptrs[*new_idx];
2632  }
2633  }
2634 
2637 
2638  MEM_freeN(faces_copy);
2639  if (pyptrs) {
2640  MEM_freeN(pyptrs);
2641  }
2642  }
2643 
2644  /* And now, fix all vertices/edges/faces/loops pointers! */
2645  /* Verts' pointers, only edge pointers... */
2646  if (eptr_map) {
2647  BM_ITER_MESH (ve, &iter, bm, BM_VERTS_OF_MESH) {
2648  /* printf("Vert e: %p -> %p\n", ve->e, BLI_ghash_lookup(eptr_map, ve->e));*/
2649  if (ve->e) {
2650  ve->e = BLI_ghash_lookup(eptr_map, ve->e);
2651  BLI_assert(ve->e);
2652  }
2653  }
2654  }
2655 
2656  /* Edges' pointers, only vert pointers (as we don't mess with loops!),
2657  * and - ack! - edge pointers,
2658  * as we have to handle disklinks... */
2659  if (vptr_map || eptr_map) {
2660  BM_ITER_MESH (ed, &iter, bm, BM_EDGES_OF_MESH) {
2661  if (vptr_map) {
2662  /* printf("Edge v1: %p -> %p\n", ed->v1, BLI_ghash_lookup(vptr_map, ed->v1));*/
2663  /* printf("Edge v2: %p -> %p\n", ed->v2, BLI_ghash_lookup(vptr_map, ed->v2));*/
2664  ed->v1 = BLI_ghash_lookup(vptr_map, ed->v1);
2665  ed->v2 = BLI_ghash_lookup(vptr_map, ed->v2);
2666  BLI_assert(ed->v1);
2667  BLI_assert(ed->v2);
2668  }
2669  if (eptr_map) {
2670  /* printf("Edge v1_disk_link prev: %p -> %p\n", ed->v1_disk_link.prev,*/
2671  /* BLI_ghash_lookup(eptr_map, ed->v1_disk_link.prev));*/
2672  /* printf("Edge v1_disk_link next: %p -> %p\n", ed->v1_disk_link.next,*/
2673  /* BLI_ghash_lookup(eptr_map, ed->v1_disk_link.next));*/
2674  /* printf("Edge v2_disk_link prev: %p -> %p\n", ed->v2_disk_link.prev,*/
2675  /* BLI_ghash_lookup(eptr_map, ed->v2_disk_link.prev));*/
2676  /* printf("Edge v2_disk_link next: %p -> %p\n", ed->v2_disk_link.next,*/
2677  /* BLI_ghash_lookup(eptr_map, ed->v2_disk_link.next));*/
2678  ed->v1_disk_link.prev = BLI_ghash_lookup(eptr_map, ed->v1_disk_link.prev);
2679  ed->v1_disk_link.next = BLI_ghash_lookup(eptr_map, ed->v1_disk_link.next);
2680  ed->v2_disk_link.prev = BLI_ghash_lookup(eptr_map, ed->v2_disk_link.prev);
2681  ed->v2_disk_link.next = BLI_ghash_lookup(eptr_map, ed->v2_disk_link.next);
2686  }
2687  }
2688  }
2689 
2690  /* Faces' pointers (loops, in fact), always needed... */
2691  BM_ITER_MESH (fa, &iter, bm, BM_FACES_OF_MESH) {
2692  BM_ITER_ELEM (lo, &iterl, fa, BM_LOOPS_OF_FACE) {
2693  if (vptr_map) {
2694  /* printf("Loop v: %p -> %p\n", lo->v, BLI_ghash_lookup(vptr_map, lo->v));*/
2695  lo->v = BLI_ghash_lookup(vptr_map, lo->v);
2696  BLI_assert(lo->v);
2697  }
2698  if (eptr_map) {
2699  /* printf("Loop e: %p -> %p\n", lo->e, BLI_ghash_lookup(eptr_map, lo->e));*/
2700  lo->e = BLI_ghash_lookup(eptr_map, lo->e);
2701  BLI_assert(lo->e);
2702  }
2703  if (fptr_map) {
2704  /* printf("Loop f: %p -> %p\n", lo->f, BLI_ghash_lookup(fptr_map, lo->f));*/
2705  lo->f = BLI_ghash_lookup(fptr_map, lo->f);
2706  BLI_assert(lo->f);
2707  }
2708  }
2709  }
2710 
2711  /* Selection history */
2712  {
2713  BMEditSelection *ese;
2714  for (ese = bm->selected.first; ese; ese = ese->next) {
2715  switch (ese->htype) {
2716  case BM_VERT:
2717  if (vptr_map) {
2718  ese->ele = BLI_ghash_lookup(vptr_map, ese->ele);
2719  BLI_assert(ese->ele);
2720  }
2721  break;
2722  case BM_EDGE:
2723  if (eptr_map) {
2724  ese->ele = BLI_ghash_lookup(eptr_map, ese->ele);
2725  BLI_assert(ese->ele);
2726  }
2727  break;
2728  case BM_FACE:
2729  if (fptr_map) {
2730  ese->ele = BLI_ghash_lookup(fptr_map, ese->ele);
2731  BLI_assert(ese->ele);
2732  }
2733  break;
2734  }
2735  }
2736  }
2737 
2738  if (fptr_map) {
2739  if (bm->act_face) {
2740  bm->act_face = BLI_ghash_lookup(fptr_map, bm->act_face);
2742  }
2743  }
2744 
2745  if (vptr_map) {
2746  BLI_ghash_free(vptr_map, NULL, NULL);
2747  }
2748  if (eptr_map) {
2749  BLI_ghash_free(eptr_map, NULL, NULL);
2750  }
2751  if (fptr_map) {
2752  BLI_ghash_free(fptr_map, NULL, NULL);
2753  }
2754 }
2755 
2763  const struct BMeshCreateParams *params,
2764  BLI_mempool *vpool_dst,
2765  BLI_mempool *epool_dst,
2766  BLI_mempool *lpool_dst,
2767  BLI_mempool *fpool_dst)
2768 {
2769  const char remap = (vpool_dst ? BM_VERT : 0) | (epool_dst ? BM_EDGE : 0) |
2770  (lpool_dst ? BM_LOOP : 0) | (fpool_dst ? BM_FACE : 0);
2771 
2772  BMVert **vtable_dst = (remap & BM_VERT) ? MEM_mallocN(bm->totvert * sizeof(BMVert *), __func__) :
2773  NULL;
2774  BMEdge **etable_dst = (remap & BM_EDGE) ? MEM_mallocN(bm->totedge * sizeof(BMEdge *), __func__) :
2775  NULL;
2776  BMLoop **ltable_dst = (remap & BM_LOOP) ? MEM_mallocN(bm->totloop * sizeof(BMLoop *), __func__) :
2777  NULL;
2778  BMFace **ftable_dst = (remap & BM_FACE) ? MEM_mallocN(bm->totface * sizeof(BMFace *), __func__) :
2779  NULL;
2780 
2781  const bool use_toolflags = params->use_toolflags;
2782 
2783  if (remap & BM_VERT) {
2784  BMIter iter;
2785  int index;
2786  BMVert *v_src;
2787  BM_ITER_MESH_INDEX (v_src, &iter, bm, BM_VERTS_OF_MESH, index) {
2788  BMVert *v_dst = BLI_mempool_alloc(vpool_dst);
2789  memcpy(v_dst, v_src, sizeof(BMVert));
2790  if (use_toolflags) {
2791  ((BMVert_OFlag *)v_dst)->oflags = bm->vtoolflagpool ?
2793  NULL;
2794  }
2795 
2796  vtable_dst[index] = v_dst;
2797  BM_elem_index_set(v_src, index); /* set_ok */
2798  }
2799  }
2800 
2801  if (remap & BM_EDGE) {
2802  BMIter iter;
2803  int index;
2804  BMEdge *e_src;
2805  BM_ITER_MESH_INDEX (e_src, &iter, bm, BM_EDGES_OF_MESH, index) {
2806  BMEdge *e_dst = BLI_mempool_alloc(epool_dst);
2807  memcpy(e_dst, e_src, sizeof(BMEdge));
2808  if (use_toolflags) {
2809  ((BMEdge_OFlag *)e_dst)->oflags = bm->etoolflagpool ?
2811  NULL;
2812  }
2813 
2814  etable_dst[index] = e_dst;
2815  BM_elem_index_set(e_src, index); /* set_ok */
2816  }
2817  }
2818 
2819  if (remap & (BM_LOOP | BM_FACE)) {
2820  BMIter iter;
2821  int index, index_loop = 0;
2822  BMFace *f_src;
2823  BM_ITER_MESH_INDEX (f_src, &iter, bm, BM_FACES_OF_MESH, index) {
2824 
2825  if (remap & BM_FACE) {
2826  BMFace *f_dst = BLI_mempool_alloc(fpool_dst);
2827  memcpy(f_dst, f_src, sizeof(BMFace));
2828  if (use_toolflags) {
2829  ((BMFace_OFlag *)f_dst)->oflags = bm->ftoolflagpool ?
2831  NULL;
2832  }
2833 
2834  ftable_dst[index] = f_dst;
2835  BM_elem_index_set(f_src, index); /* set_ok */
2836  }
2837 
2838  /* handle loops */
2839  if (remap & BM_LOOP) {
2840  BMLoop *l_iter_src, *l_first_src;
2841  l_iter_src = l_first_src = BM_FACE_FIRST_LOOP((BMFace *)f_src);
2842  do {
2843  BMLoop *l_dst = BLI_mempool_alloc(lpool_dst);
2844  memcpy(l_dst, l_iter_src, sizeof(BMLoop));
2845  ltable_dst[index_loop] = l_dst;
2846  BM_elem_index_set(l_iter_src, index_loop++); /* set_ok */
2847  } while ((l_iter_src = l_iter_src->next) != l_first_src);
2848  }
2849  }
2850  }
2851 
2852 #define MAP_VERT(ele) vtable_dst[BM_elem_index_get(ele)]
2853 #define MAP_EDGE(ele) etable_dst[BM_elem_index_get(ele)]
2854 #define MAP_LOOP(ele) ltable_dst[BM_elem_index_get(ele)]
2855 #define MAP_FACE(ele) ftable_dst[BM_elem_index_get(ele)]
2856 
2857 #define REMAP_VERT(ele) \
2858  { \
2859  if (remap & BM_VERT) { \
2860  ele = MAP_VERT(ele); \
2861  } \
2862  } \
2863  ((void)0)
2864 #define REMAP_EDGE(ele) \
2865  { \
2866  if (remap & BM_EDGE) { \
2867  ele = MAP_EDGE(ele); \
2868  } \
2869  } \
2870  ((void)0)
2871 #define REMAP_LOOP(ele) \
2872  { \
2873  if (remap & BM_LOOP) { \
2874  ele = MAP_LOOP(ele); \
2875  } \
2876  } \
2877  ((void)0)
2878 #define REMAP_FACE(ele) \
2879  { \
2880  if (remap & BM_FACE) { \
2881  ele = MAP_FACE(ele); \
2882  } \
2883  } \
2884  ((void)0)
2885 
2886  /* verts */
2887  {
2888  for (int i = 0; i < bm->totvert; i++) {
2889  BMVert *v = vtable_dst[i];
2890  if (v->e) {
2891  REMAP_EDGE(v->e);
2892  }
2893  }
2894  }
2895 
2896  /* edges */
2897  {
2898  for (int i = 0; i < bm->totedge; i++) {
2899  BMEdge *e = etable_dst[i];
2900  REMAP_VERT(e->v1);
2901  REMAP_VERT(e->v2);
2902  REMAP_EDGE(e->v1_disk_link.next);
2903  REMAP_EDGE(e->v1_disk_link.prev);
2904  REMAP_EDGE(e->v2_disk_link.next);
2905  REMAP_EDGE(e->v2_disk_link.prev);
2906  if (e->l) {
2907  REMAP_LOOP(e->l);
2908  }
2909  }
2910  }
2911 
2912  /* faces */
2913  {
2914  for (int i = 0; i < bm->totface; i++) {
2915  BMFace *f = ftable_dst[i];
2916  REMAP_LOOP(f->l_first);
2917 
2918  {
2919  BMLoop *l_iter, *l_first;
2920  l_iter = l_first = BM_FACE_FIRST_LOOP((BMFace *)f);
2921  do {
2922  REMAP_VERT(l_iter->v);
2923  REMAP_EDGE(l_iter->e);
2924  REMAP_FACE(l_iter->f);
2925 
2926  REMAP_LOOP(l_iter->radial_next);
2927  REMAP_LOOP(l_iter->radial_prev);
2928  REMAP_LOOP(l_iter->next);
2929  REMAP_LOOP(l_iter->prev);
2930  } while ((l_iter = l_iter->next) != l_first);
2931  }
2932  }
2933  }
2934 
2936  switch (ese->htype) {
2937  case BM_VERT:
2938  if (remap & BM_VERT) {
2939  ese->ele = (BMElem *)MAP_VERT(ese->ele);
2940  }
2941  break;
2942  case BM_EDGE:
2943  if (remap & BM_EDGE) {
2944  ese->ele = (BMElem *)MAP_EDGE(ese->ele);
2945  }
2946  break;
2947  case BM_FACE:
2948  if (remap & BM_FACE) {
2949  ese->ele = (BMElem *)MAP_FACE(ese->ele);
2950  }
2951  break;
2952  }
2953  }
2954 
2955  if (bm->act_face) {
2957  }
2958 
2959 #undef MAP_VERT
2960 #undef MAP_EDGE
2961 #undef MAP_LOOP
2962 #undef MAP_EDGE
2963 
2964 #undef REMAP_VERT
2965 #undef REMAP_EDGE
2966 #undef REMAP_LOOP
2967 #undef REMAP_EDGE
2968 
2969  /* Cleanup, re-use local tables if the current mesh had tables allocated.
2970  * could use irrespective but it may use more memory than the caller wants
2971  * (and not be needed). */
2972  if (remap & BM_VERT) {
2973  if (bm->vtable) {
2974  SWAP(BMVert **, vtable_dst, bm->vtable);
2975  bm->vtable_tot = bm->totvert;
2977  }
2978  MEM_freeN(vtable_dst);
2980  bm->vpool = vpool_dst;
2981  }
2982 
2983  if (remap & BM_EDGE) {
2984  if (bm->etable) {
2985  SWAP(BMEdge **, etable_dst, bm->etable);
2986  bm->etable_tot = bm->totedge;
2988  }
2989  MEM_freeN(etable_dst);
2991  bm->epool = epool_dst;
2992  }
2993 
2994  if (remap & BM_LOOP) {
2995  /* no loop table */
2996  MEM_freeN(ltable_dst);
2998  bm->lpool = lpool_dst;
2999  }
3000 
3001  if (remap & BM_FACE) {
3002  if (bm->ftable) {
3003  SWAP(BMFace **, ftable_dst, bm->ftable);
3004  bm->ftable_tot = bm->totface;
3006  }
3007  MEM_freeN(ftable_dst);
3009  bm->fpool = fpool_dst;
3010  }
3011 }
3012 
3016 void BM_mesh_toolflags_set(BMesh *bm, bool use_toolflags)
3017 {
3018  if (bm->use_toolflags == use_toolflags) {
3019  return;
3020  }
3021 
3022  const BMAllocTemplate allocsize = BMALLOC_TEMPLATE_FROM_BM(bm);
3023 
3024  BLI_mempool *vpool_dst = NULL;
3025  BLI_mempool *epool_dst = NULL;
3026  BLI_mempool *fpool_dst = NULL;
3027 
3028  bm_mempool_init_ex(&allocsize, use_toolflags, &vpool_dst, &epool_dst, NULL, &fpool_dst);
3029 
3030  if (use_toolflags == false) {
3034 
3035  bm->vtoolflagpool = NULL;
3036  bm->etoolflagpool = NULL;
3037  bm->ftoolflagpool = NULL;
3038  }
3039 
3041  &((struct BMeshCreateParams){
3042  .use_toolflags = use_toolflags,
3043  }),
3044  vpool_dst,
3045  epool_dst,
3046  NULL,
3047  fpool_dst);
3048 
3049  bm->use_toolflags = use_toolflags;
3050 }
3051 
3052 /* -------------------------------------------------------------------- */
3056 void BM_mesh_vert_coords_get(BMesh *bm, float (*vert_coords)[3])
3057 {
3058  BMIter iter;
3059  BMVert *v;
3060  int i;
3061  BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
3062  copy_v3_v3(vert_coords[i], v->co);
3063  }
3064 }
3065 
3066 float (*BM_mesh_vert_coords_alloc(BMesh *bm, int *r_vert_len))[3]
3067 {
3068  float(*vert_coords)[3] = MEM_mallocN(bm->totvert * sizeof(*vert_coords), __func__);
3069  BM_mesh_vert_coords_get(bm, vert_coords);
3070  *r_vert_len = bm->totvert;
3071  return vert_coords;
3072 }
3073 
3074 void BM_mesh_vert_coords_apply(BMesh *bm, const float (*vert_coords)[3])
3075 {
3076  BMIter iter;
3077  BMVert *v;
3078  int i;
3079  BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
3080  copy_v3_v3(v->co, vert_coords[i]);
3081  }
3082 }
3083 
3085  const float (*vert_coords)[3],
3086  const float mat[4][4])
3087 {
3088  BMIter iter;
3089  BMVert *v;
3090  int i;
3091  BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
3092  mul_v3_m4v3(v->co, mat, vert_coords[i]);
3093  }
3094 }
3095 
typedef float(TangentPoint)[2]
void CustomData_free(struct CustomData *data, int totelem)
Definition: customdata.c:2239
bool CustomData_has_layer(const struct CustomData *data, int type)
void CustomData_set_layer_flag(struct CustomData *data, int type, int flag)
Definition: customdata.c:2475
bool CustomData_bmesh_has_free(const struct CustomData *data)
Definition: customdata.c:3856
int CustomData_get_offset(const struct CustomData *data, int type)
void CustomData_bmesh_free_block(struct CustomData *data, void **block)
Definition: customdata.c:3606
void CustomData_reset(struct CustomData *data)
Definition: customdata.c:2233
@ G_DEBUG
Definition: BKE_global.h:133
MLoopNorSpace * BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr)
@ MLNOR_SPACE_IS_SINGLE
Definition: BKE_mesh.h:365
@ MLNOR_SPACEARR_BMLOOP_PTR
Definition: BKE_mesh.h:384
void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3], float vec_ref[3], float vec_other[3], struct BLI_Stack *edge_vectors)
void BKE_lnor_spacearr_init(MLoopNorSpaceArray *lnors_spacearr, const int numLoops, const char data_type)
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr)
void BKE_lnor_spacearr_free(MLoopNorSpaceArray *lnors_spacearr)
void BKE_lnor_space_custom_data_to_normal(MLoopNorSpace *lnor_space, const short clnor_data[2], float r_custom_lnor[3])
void BKE_lnor_space_add_loop(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpace *lnor_space, const int ml_index, void *bm_loop, const bool is_single)
void BKE_lnor_space_custom_normal_to_data(MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition: BLI_bitmap.h:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:78
#define BLI_BITMAP_NEW(_tot, _alloc_string)
Definition: BLI_bitmap.h:50
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:32
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
GHash * BLI_ghash_ptr_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
BLI_INLINE void BLI_listbase_clear(struct ListBase *lb)
Definition: BLI_listbase.h:128
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
MINLINE float saacos(float fac)
MINLINE int compare_ff(float a, float b, const float max_diff)
#define M_PI
Definition: BLI_math_base.h:38
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE bool compare_v3v3(const float a[3], const float b[3], const float limit) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void * BLI_mempool_calloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: BLI_mempool.c:362
@ BLI_MEMPOOL_ALLOW_ITER
Definition: BLI_mempool.h:85
@ BLI_MEMPOOL_NOP
Definition: BLI_mempool.h:77
BLI_mempool * BLI_mempool_create(unsigned int esize, unsigned int totelem, unsigned int pchunk, unsigned int flag) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition: BLI_mempool.c:268
void * BLI_mempool_alloc(BLI_mempool *pool) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: BLI_mempool.c:334
void BLI_mempool_destroy(BLI_mempool *pool) ATTR_NONNULL(1)
Definition: BLI_mempool.c:757
void * BLI_mempool_findelem(BLI_mempool *pool, unsigned int index) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: BLI_mempool.c:459
void BLI_stack_push(BLI_Stack *stack, const void *src) ATTR_NONNULL()
Definition: stack.c:163
bool BLI_stack_is_empty(const BLI_Stack *stack) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: stack.c:310
void BLI_stack_free(BLI_Stack *stack) ATTR_NONNULL()
Definition: stack.c:114
#define BLI_stack_new(esize, descr)
unsigned int uint
Definition: BLI_sys_types.h:83
struct MempoolIterData MempoolIterData
Definition: BLI_task.h:223
#define SWAP(type, a, b)
#define UNUSED(x)
#define UNLIKELY(x)
#define LIKELY(x)
@ CD_CUSTOMLOOPNORMAL
@ CD_BM_ELEM_PYPTR
@ CD_FLAG_TEMPORARY
These structs are the foundation for all linked lists in the library system.
#define SCE_SELECT_FACE
#define SCE_SELECT_VERTEX
#define SCE_SELECT_EDGE
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE float atomic_cas_float(float *v, float old, float _new)
struct BMFace BMFace
@ BM_SPACEARR_DIRTY_ALL
Definition: bmesh_class.h:416
@ BM_SPACEARR_DIRTY
Definition: bmesh_class.h:415
struct BMFace_OFlag BMFace_OFlag
#define BM_ALL_NOLOOP
Definition: bmesh_class.h:411
struct BMEdge BMEdge
struct BMVert_OFlag BMVert_OFlag
@ BM_LOOP
Definition: bmesh_class.h:385
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_VERT
Definition: bmesh_class.h:383
@ BM_EDGE
Definition: bmesh_class.h:384
#define BM_OMP_LIMIT
Definition: bmesh_class.h:587
struct BMLoop BMLoop
@ BM_ELEM_SELECT
Definition: bmesh_class.h:471
@ BM_ELEM_SMOOTH
Definition: bmesh_class.h:477
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:553
struct BMEdge_OFlag BMEdge_OFlag
struct BMVert BMVert
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
Definition: bmesh_class.h:530
void BMO_error_clear(BMesh *bm)
#define BM_ELEM_INDEX_VALIDATE(_bm, _msg_a, _msg_b)
Definition: bmesh_error.h:51
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:124
#define BM_elem_flag_disable(ele, hflag)
Definition: bmesh_inline.h:29
#define BM_elem_index_set(ele, index)
Definition: bmesh_inline.h:125
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:26
#define BM_elem_flag_enable(ele, hflag)
Definition: bmesh_inline.h:28
void BM_data_layer_add(BMesh *bm, CustomData *data, int type)
Definition: bmesh_interp.c:894
int BM_iter_as_array(BMesh *bm, const char itype, void *data, void **array, const int len)
Iterator as Array.
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_EDGES_OF_MESH
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_VERT
@ BM_LOOPS_OF_EDGE
@ BM_LOOPS_OF_FACE
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_mesh_select_mode_flush(BMesh *bm)
void BM_mesh_elem_toolflags_clear(BMesh *bm)
Definition: bmesh_mesh.c:132
void BM_mesh_vert_coords_apply_with_mat4(BMesh *bm, const float(*vert_coords)[3], const float mat[4][4])
Definition: bmesh_mesh.c:3084
static void bm_mesh_loops_assign_normal_data(BMesh *bm, MLoopNorSpaceArray *lnors_spacearr, short(*r_clnors_data)[2], const int cd_loop_clnors_offset, const float(*new_lnors)[3])
Definition: bmesh_mesh.c:1113
const BMAllocTemplate bm_mesh_allocsize_default
Definition: bmesh_mesh.c:46
static void mesh_verts_calc_normals_normalize_cb(void *userdata, MempoolIterData *mp_v)
Definition: bmesh_mesh.c:450
void BM_lnorspacearr_store(BMesh *bm, float(*r_lnors)[3])
Definition: bmesh_mesh.c:1392
BMFace * BM_face_at_index_find(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2386
void BM_mesh_toolflags_set(BMesh *bm, bool use_toolflags)
Definition: bmesh_mesh.c:3016
void BM_mesh_data_free(BMesh *bm)
BMesh Free Mesh Data.
Definition: bmesh_mesh.c:185
struct BMVertsCalcNormalsData BMVertsCalcNormalsData
static void bm_mempool_init_ex(const BMAllocTemplate *allocsize, const bool use_toolflags, BLI_mempool **r_vpool, BLI_mempool **r_epool, BLI_mempool **r_lpool, BLI_mempool **r_fpool)
Definition: bmesh_mesh.c:49
void BM_mesh_remap(BMesh *bm, const uint *vert_idx, const uint *edge_idx, const uint *face_idx)
Definition: bmesh_mesh.c:2476
#define CLEAR_SPACEARRAY_THRESHOLD(x)
Definition: bmesh_mesh.c:1416
void BM_mesh_vert_coords_apply(BMesh *bm, const float(*vert_coords)[3])
Definition: bmesh_mesh.c:3074
#define REMAP_VERT(ele)
void BM_mesh_clear(BMesh *bm)
BMesh Clear Mesh.
Definition: bmesh_mesh.c:281
void BM_lnorspace_update(BMesh *bm)
Definition: bmesh_mesh.c:1565
static void bm_mesh_verts_calc_normals(BMesh *bm, const float(*edgevec)[3], const float(*fnos)[3], const float(*vcos)[3], float(*vnos)[3])
Definition: bmesh_mesh.c:462
#define MAP_FACE(ele)
#define BM_LNORSPACE_UPDATE
Definition: bmesh_mesh.c:327
const BMAllocTemplate bm_mesh_chunksize_default
Definition: bmesh_mesh.c:47
static void bm_mesh_edges_sharp_tag(BMesh *bm, const float(*vnos)[3], const float(*fnos)[3], float(*r_lnos)[3], const float split_angle, const bool do_sharp_edges_tag)
Definition: bmesh_mesh.c:558
void BM_lnorspace_invalidate(BMesh *bm, const bool do_invalidate_all)
Definition: bmesh_mesh.c:1418
#define REMAP_LOOP(ele)
void BM_normals_loops_edges_tag(BMesh *bm, const bool do_edges)
Definition: bmesh_mesh.c:1582
BMEdge * BM_edge_at_index_find_or_table(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2425
static void bm_mempool_init(BMesh *bm, const BMAllocTemplate *allocsize, const bool use_toolflags)
Definition: bmesh_mesh.c:89
#define FLT_EQ_NONAN(_fa, _fb)
void BM_mesh_elem_toolflags_ensure(BMesh *bm)
Definition: bmesh_mesh.c:98
void BM_lnorspace_rebuild(BMesh *bm, bool preserve_clnor)
Definition: bmesh_mesh.c:1486
bool BM_custom_loop_normals_to_vector_layer(BMesh *bm)
Definition: bmesh_mesh.c:1903
static bool bm_mesh_loops_split_lnor_fans(BMesh *bm, MLoopNorSpaceArray *lnors_spacearr, const float(*new_lnors)[3])
Definition: bmesh_mesh.c:1017
BMEdge * BM_edge_at_index_find(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2381
struct BMEdgesCalcVectorsData BMEdgesCalcVectorsData
static void bm_mesh_loops_custom_normals_set(BMesh *bm, const float(*vcos)[3], const float(*vnos)[3], const float(*fnos)[3], MLoopNorSpaceArray *r_lnors_spacearr, short(*r_clnors_data)[2], const int cd_loop_clnors_offset, float(*new_lnors)[3], const int cd_new_lnors_offset, bool do_split_fans)
Definition: bmesh_mesh.c:1198
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Definition: bmesh_mesh.c:307
void BM_mesh_rebuild(BMesh *bm, const struct BMeshCreateParams *params, BLI_mempool *vpool_dst, BLI_mempool *epool_dst, BLI_mempool *lpool_dst, BLI_mempool *fpool_dst)
Definition: bmesh_mesh.c:2762
BMFace * BM_face_at_index_find_or_table(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2433
#define REMAP_EDGE(ele)
BMVert * BM_vert_at_index_find_or_table(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2417
static void mesh_faces_calc_normals_cb(void *UNUSED(userdata), MempoolIterData *mp_f)
Definition: bmesh_mesh.c:488
BMesh * BM_mesh_create(const BMAllocTemplate *allocsize, const struct BMeshCreateParams *params)
BMesh Make Mesh.
Definition: bmesh_mesh.c:157
void bmesh_edit_begin(BMesh *UNUSED(bm), BMOpTypeFlag UNUSED(type_flag))
BMesh Begin Edit.
Definition: bmesh_mesh.c:1975
void BM_loops_calc_normal_vcos(BMesh *bm, const float(*vcos)[3], const float(*vnos)[3], const float(*fnos)[3], const bool use_split_normals, const float split_angle, float(*r_lnos)[3], MLoopNorSpaceArray *r_lnors_spacearr, short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild)
BMesh Compute Loop Normals from/to external data.
Definition: bmesh_mesh.c:1346
static void bm_loop_normal_mark_indiv_do_loop(BMLoop *l, BLI_bitmap *loops, MLoopNorSpaceArray *lnor_spacearr, int *totloopsel, const bool do_all_loops_of_vert)
Definition: bmesh_mesh.c:1665
void BM_mesh_normals_update(BMesh *bm)
BMesh Compute Normals.
Definition: bmesh_mesh.c:500
void BM_mesh_elem_table_free(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2361
#define LNOR_SPACE_TRIGO_THRESHOLD
Definition: bmesh_mesh.c:1011
BMVert * BM_vert_at_index_find(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2376
void BM_lnorspace_err(BMesh *bm)
Definition: bmesh_mesh.c:1623
#define MAP_EDGE(ele)
BMLoopNorEditDataArray * BM_loop_normal_editdata_array_init(BMesh *bm, const bool do_all_loops_of_vert)
Definition: bmesh_mesh.c:1844
static void loop_normal_editdata_init(BMesh *bm, BMLoopNorEditData *lnor_ed, BMVert *v, BMLoop *l, const int offset)
Definition: bmesh_mesh.c:1821
int BM_mesh_elem_count(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2444
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2276
void BM_mesh_elem_table_init(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2352
void BM_loop_normal_editdata_array_free(BMLoopNorEditDataArray *lnors_ed_arr)
Definition: bmesh_mesh.c:1892
float(* BM_mesh_vert_coords_alloc(BMesh *bm, int *r_vert_len))[3]
Definition: bmesh_mesh.c:3066
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2152
static void mesh_verts_calc_normals_accum_cb(void *userdata, MempoolIterData *mp_f)
Definition: bmesh_mesh.c:376
static int bm_loop_normal_mark_indiv(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
Definition: bmesh_mesh.c:1710
void BM_mesh_elem_index_validate(BMesh *bm, const char *location, const char *func, const char *msg_a, const char *msg_b)
Definition: bmesh_mesh.c:2169
static void bm_mesh_edges_calc_vectors(BMesh *bm, float(*edgevec)[3], const float(*vcos)[3])
Definition: bmesh_mesh.c:353
BMLoop * BM_loop_at_index_find(BMesh *bm, const int index)
Definition: bmesh_mesh.c:2391
#define MAP_VERT(ele)
void bmesh_edit_end(BMesh *bm, BMOpTypeFlag type_flag)
BMesh End Edit.
Definition: bmesh_mesh.c:1998
bool BM_loop_check_cyclic_smooth_fan(BMLoop *l_curr)
Definition: bmesh_mesh.c:636
bool BM_mesh_elem_table_check(BMesh *bm)
Definition: bmesh_mesh.c:2242
#define REMAP_FACE(ele)
void BM_mesh_vert_coords_get(BMesh *bm, float(*vert_coords)[3])
Definition: bmesh_mesh.c:3056
void BM_verts_calc_normal_vcos(BMesh *bm, const float(*fnos)[3], const float(*vcos)[3], float(*vnos)[3])
BMesh Compute Normals from/to external data.
Definition: bmesh_mesh.c:538
void BM_edges_sharp_from_angle_set(BMesh *bm, const float split_angle)
Definition: bmesh_mesh.c:1382
static void bm_mesh_loops_calc_normals(BMesh *bm, const float(*vcos)[3], const float(*fnos)[3], float(*r_lnos)[3], MLoopNorSpaceArray *r_lnors_spacearr, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild)
Definition: bmesh_mesh.c:676
void BM_custom_loop_normals_from_vector_layer(BMesh *bm, bool add_sharp_edges)
Definition: bmesh_mesh.c:1940
static void bm_mesh_loops_calc_normals_no_autosmooth(BMesh *bm, const float(*vnos)[3], const float(*fnos)[3], float(*r_lnos)[3])
Definition: bmesh_mesh.c:1272
void BM_mesh_elem_index_ensure_ex(BMesh *bm, const char htype, int elem_offset[4])
Definition: bmesh_mesh.c:2038
static void mesh_edges_calc_vectors_cb(void *userdata, MempoolIterData *mp_e)
Definition: bmesh_mesh.c:337
#define BMALLOC_TEMPLATE_FROM_BM(bm)
Definition: bmesh_mesh.h:146
BMOpTypeFlag
@ BMO_OPTYPE_FLAG_INVALIDATE_CLNOR_ALL
@ BMO_OPTYPE_FLAG_SELECT_VALIDATE
@ BMO_OPTYPE_FLAG_UNTAN_MULTIRES
@ BMO_OPTYPE_FLAG_NORMALS_CALC
@ BMO_OPTYPE_FLAG_SELECT_FLUSH
void BM_face_normal_update(BMFace *f)
#define BM_ELEM_API_FLAG_DISABLE(element, f)
Definition: bmesh_private.h:76
#define BM_ELEM_API_FLAG_TEST(element, f)
Definition: bmesh_private.h:81
#define BM_ELEM_API_FLAG_ENABLE(element, f)
Definition: bmesh_private.h:71
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
Definition: bmesh_query.c:753
BMLoop * BM_vert_step_fan_loop(BMLoop *l, BMEdge **e_step)
Definition: bmesh_query.c:637
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
Definition: bmesh_query.c:1403
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
ATTR_WARN_UNUSED_RESULT const BMVert * v
void bpy_bm_generic_invalidate(struct BPy_BMGeneric *UNUSED(self))
Definition: customdata.c:224
OperationNode * node
static int elem_offset(const SDNA *sdna, const char *type, const char *name, const SDNA_Struct *old)
Definition: dna_genfile.c:988
uint nor
IconTextureDrawCall normal
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
#define cosf(x)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static void clear(Message *msg)
Definition: msgfmt.c:294
struct BMFlagLayer * oflags
Definition: bmesh_class.h:153
BMVert * v1
Definition: bmesh_class.h:134
BMDiskLink v2_disk_link
Definition: bmesh_class.h:148
BMDiskLink v1_disk_link
Definition: bmesh_class.h:148
BMVert * v2
Definition: bmesh_class.h:134
const float(* vcos)[3]
Definition: bmesh_mesh.c:331
struct BMEditSelection * next
Definition: bmesh_marking.h:24
struct BMEditSelection * prev
Definition: bmesh_marking.h:24
struct BMFlagLayer * oflags
Definition: bmesh_class.h:287
int len
Definition: bmesh_class.h:279
BMHeader head
Definition: bmesh_class.h:267
float no[3]
Definition: bmesh_class.h:280
BMLoop * l_first
Definition: bmesh_class.h:273
void * data
Definition: bmesh_class.h:63
BMLoopNorEditData ** lidx_to_lnor_editdata
Definition: bmesh_class.h:404
BMLoopNorEditData * lnor_editdata
Definition: bmesh_class.h:399
BMHeader head
Definition: bmesh_class.h:157
struct BMVert * v
Definition: bmesh_class.h:165
struct BMEdge * e
Definition: bmesh_class.h:176
struct BMLoop * radial_prev
Definition: bmesh_class.h:216
struct BMLoop * radial_next
Definition: bmesh_class.h:216
struct BMLoop * prev
Definition: bmesh_class.h:245
struct BMFace * f
Definition: bmesh_class.h:183
struct BMLoop * next
Definition: bmesh_class.h:245
struct BMFlagLayer * oflags
Definition: bmesh_class.h:114
float co[3]
Definition: bmesh_class.h:99
struct BMEdge * e
Definition: bmesh_class.h:109
float no[3]
Definition: bmesh_class.h:100
BMHeader head
Definition: bmesh_class.h:97
const float(* vcos)[3]
Definition: bmesh_mesh.c:370
const float(* fnos)[3]
Definition: bmesh_mesh.c:368
const float(* edgevec)[3]
Definition: bmesh_mesh.c:369
int totvert
Definition: bmesh_class.h:297
BMEdge ** etable
Definition: bmesh_class.h:322
struct BLI_mempool * epool
Definition: bmesh_class.h:314
int totflags
Definition: bmesh_class.h:355
struct MLoopNorSpaceArray * lnor_spacearr
Definition: bmesh_class.h:343
char elem_index_dirty
Definition: bmesh_class.h:305
CustomData vdata
Definition: bmesh_class.h:337
int totedge
Definition: bmesh_class.h:297
char elem_table_dirty
Definition: bmesh_class.h:311
struct BLI_mempool * vtoolflagpool
Definition: bmesh_class.h:331
ListBase selected
Definition: bmesh_class.h:356
CustomData edata
Definition: bmesh_class.h:337
uint use_toolflags
Definition: bmesh_class.h:333
int totvertsel
Definition: bmesh_class.h:298
int totloop
Definition: bmesh_class.h:297
int ftable_tot
Definition: bmesh_class.h:328
void * py_handle
Definition: bmesh_class.h:378
struct BLI_mempool * etoolflagpool
Definition: bmesh_class.h:331
BMFace * act_face
Definition: bmesh_class.h:366
short selectmode
Definition: bmesh_class.h:350
BMVert ** vtable
Definition: bmesh_class.h:321
struct BLI_mempool * ftoolflagpool
Definition: bmesh_class.h:331
char spacearr_dirty
Definition: bmesh_class.h:344
CustomData pdata
Definition: bmesh_class.h:337
CustomData ldata
Definition: bmesh_class.h:337
int vtable_tot
Definition: bmesh_class.h:326
BMFace ** ftable
Definition: bmesh_class.h:323
int totface
Definition: bmesh_class.h:297
int toolflag_index
Definition: bmesh_class.h:335
int etable_tot
Definition: bmesh_class.h:327
struct BLI_mempool * fpool
Definition: bmesh_class.h:314
struct BLI_mempool * vpool
Definition: bmesh_class.h:314
struct BLI_mempool * lpool
Definition: bmesh_class.h:314
struct BLI_mempool * pool
void * link
Definition: BLI_linklist.h:40
struct LinkNode * next
Definition: BLI_linklist.h:39
void * last
Definition: DNA_listBase.h:47
void * first
Definition: DNA_listBase.h:47
MLoopNorSpace ** lspacearr
Definition: BKE_mesh.h:372
float ref_alpha
Definition: BKE_mesh.h:348
float vec_ortho[3]
Definition: BKE_mesh.h:346
float ref_beta
Definition: BKE_mesh.h:350
float vec_ref[3]
Definition: BKE_mesh.h:344
float vec_lnor[3]
Definition: BKE_mesh.h:342
struct LinkNode * loops
Definition: BKE_mesh.h:355
#define G(x, y, z)
uint len