Blender V5.3
bmesh_mesh_normals.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include "MEM_guardedalloc.h"
14
15#include "DNA_scene_types.h"
16
17#include "BLI_array.hh"
18#include "BLI_bitmap.hh"
19#include "BLI_linklist_stack.hh"
20#include "BLI_listbase.hh"
21#include "BLI_math_base.hh"
22#include "BLI_math_vector_c.hh"
23#include "BLI_task.hh"
24#include "BLI_task_c.hh"
25#include "BLI_utildefines.hh"
26#include "BLI_vector.hh"
27
28#include "BKE_customdata.hh"
29#include "BKE_editmesh.hh"
30#include "BKE_global.hh"
31#include "BKE_mesh.hh"
32
35
36namespace blender {
37
38/* Smooth angle to use when tagging edges is disabled entirely. */
39#define EDGE_TAG_FROM_SPLIT_ANGLE_BYPASS -FLT_MAX
40
42 BMEdge *e,
43 const float split_angle_cos);
44static void bm_edge_tag_from_smooth(Span<float3> fnos, BMEdge *e, const float split_angle_cos);
45
46/* -------------------------------------------------------------------- */
49
53
54/* We use that existing internal API flag,
55 * assuming no other tool using it would run concurrently to clnors editing. */
56#define BM_LNORSPACE_UPDATE _FLAG_MF
57
59 /* Read-only data. */
62
63 /* Write data. */
65};
66
68 const float e1diff[3],
69 const float e2diff[3],
70 const float f_no[3],
71 float v_no[3])
72{
73 /* Calculate the dot product of the two edges that meet at the loop's vertex. */
74 /* Edge vectors are calculated from `e->v1` to `e->v2`, so adjust the dot product if one but not
75 * both loops actually runs from `e->v2` to `e->v1`. */
76 float dotprod = dot_v3v3(e1diff, e2diff);
77 if ((l_iter->prev->e->v1 == l_iter->prev->v) ^ (l_iter->e->v1 == l_iter->v)) {
78 dotprod = -dotprod;
79 }
80 const float fac = math::safe_acos_approx(-dotprod);
81 /* Shouldn't happen as normalizing edge-vectors cause degenerate values to be zeroed out. */
82 BLI_assert(!isnan(fac));
83 madd_v3_v3fl(v_no, f_no, fac);
84}
85
87{
88 /* NOTE(@ideasman42): Regarding redundant unit-length edge-vector calculation:
89 *
90 * This functions calculates unit-length edge-vector for every loop edge
91 * in practice this means 2x `sqrt` calls per face-corner connected to each vertex.
92 *
93 * Previously (2.9x and older), the edge vectors were calculated and stored for reuse.
94 * However the overhead of did not perform well (~16% slower - single & multi-threaded)
95 * when compared with calculating the values as they are needed.
96 *
97 * For simple grid topologies this function calculates the edge-vectors 4x times.
98 * There is some room for improved performance by storing the edge-vectors for reuse locally
99 * in this function, reducing the number of redundant `sqrtf` in half (2x instead of 4x).
100 * so face loops that share an edge would not calculate it multiple times.
101 * From my tests the performance improvements are so small they're difficult to measure,
102 * the time saved removing `sqrtf` calls is lost on storing and looking up the information,
103 * even in the case of small inline lookup tables.
104 *
105 * Further, local data structures would need to support cases where
106 * stack memory isn't sufficient - adding additional complexity for corner-cases
107 * (a vertex that has thousands of connected edges for example).
108 * Unless there are important use-cases that benefit from edge-vector caching,
109 * keep this simple and calculate ~4x as many edge-vectors.
110 *
111 * In conclusion, the cost of caching & looking up edge-vectors both globally or per-vertex
112 * doesn't save enough time to make it worthwhile.
113 */
114
115 float *v_no = v->no;
116 zero_v3(v_no);
117
118 BMEdge *e_first = v->e;
119 if (e_first != nullptr) {
120 float e1diff[3], e2diff[3];
121 BMEdge *e_iter = e_first;
122 do {
123 BMLoop *l_first = e_iter->l;
124 if (l_first != nullptr) {
125 sub_v3_v3v3(e2diff, e_iter->v1->co, e_iter->v2->co);
126 normalize_v3(e2diff);
127
128 BMLoop *l_iter = l_first;
129 do {
130 if (l_iter->v == v) {
131 BMEdge *e_prev = l_iter->prev->e;
132 sub_v3_v3v3(e1diff, e_prev->v1->co, e_prev->v2->co);
133 normalize_v3(e1diff);
134
135 bm_vert_calc_normals_accum_loop(l_iter, e1diff, e2diff, l_iter->f->no, v_no);
136 }
137 } while ((l_iter = l_iter->radial_next) != l_first);
138 }
139 } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != e_first);
140
141 if (normalize_v3(v_no) != 0.0f) [[likely]] {
142 return;
143 }
144 }
145 /* Fallback normal. */
146 normalize_v3_v3(v_no, v->co);
147}
148
149static void bm_vert_calc_normals_cb(void * /*userdata*/,
150 MempoolIterData *mp_v,
151 const TaskParallelTLS *__restrict /*tls*/)
152{
153 BMVert *v = reinterpret_cast<BMVert *>(mp_v);
155}
156
158{
159 /* See #bm_vert_calc_normals_impl note on performance. */
160 float *v_no = data->vnos[BM_elem_index_get(v)];
161 zero_v3(v_no);
162
163 /* Loop over edges. */
164 BMEdge *e_first = v->e;
165 if (e_first != nullptr) {
166 float e1diff[3], e2diff[3];
167 BMEdge *e_iter = e_first;
168 do {
169 BMLoop *l_first = e_iter->l;
170 if (l_first != nullptr) {
171 sub_v3_v3v3(e2diff,
172 data->vcos[BM_elem_index_get(e_iter->v1)],
173 data->vcos[BM_elem_index_get(e_iter->v2)]);
174 normalize_v3(e2diff);
175
176 BMLoop *l_iter = l_first;
177 do {
178 if (l_iter->v == v) {
179 BMEdge *e_prev = l_iter->prev->e;
180 sub_v3_v3v3(e1diff,
181 data->vcos[BM_elem_index_get(e_prev->v1)],
182 data->vcos[BM_elem_index_get(e_prev->v2)]);
183 normalize_v3(e1diff);
184
186 l_iter, e1diff, e2diff, data->fnos[BM_elem_index_get(l_iter->f)], v_no);
187 }
188 } while ((l_iter = l_iter->radial_next) != l_first);
189 }
190 } while ((e_iter = BM_DISK_EDGE_NEXT(e_iter, v)) != e_first);
191
192 if (normalize_v3(v_no) != 0.0f) [[likely]] {
193 return;
194 }
195 }
196 /* Fallback normal. */
198}
199
200static void bm_vert_calc_normals_with_coords_cb(void *userdata,
201 MempoolIterData *mp_v,
202 const TaskParallelTLS *__restrict /*tls*/)
203{
205 userdata);
206 BMVert *v = reinterpret_cast<BMVert *>(mp_v);
208}
209
211 const Span<float3> fnos,
212 const Span<float3> vcos,
214{
215 BM_mesh_elem_index_ensure(bm, BM_FACE | ((!vnos.is_empty() || !vcos.is_empty()) ? BM_VERT : 0));
216
217 TaskParallelSettings settings;
220
221 if (vcos.is_empty()) {
223 }
224 else {
225 BLI_assert(!fnos.is_empty() || !vnos.is_empty());
227 data.fnos = fnos;
228 data.vcos = vcos;
229 data.vnos = vnos;
231 }
232}
233
234static void bm_face_calc_normals_cb(void * /*userdata*/,
235 MempoolIterData *mp_f,
236 const TaskParallelTLS *__restrict /*tls*/)
237{
238 BMFace *f = reinterpret_cast<BMFace *>(mp_f);
239
240 BM_face_calc_normal(f, f->no);
241}
242
244{
245 if (params->face_normals) {
246 /* Calculate all face normals. */
247 TaskParallelSettings settings;
250
252 }
253
254 /* Add weighted face normals to vertices, and normalize vert normals. */
255 bm_mesh_verts_calc_normals(bm, {}, {}, {});
256}
257
259{
261 params.face_normals = true;
263}
264
266
267/* -------------------------------------------------------------------- */
270
272 const BMPartialUpdate *bmpinfo,
273 const BMeshNormalsUpdate_Params *params)
274{
275 BLI_assert(bmpinfo->params.do_normals);
276 /* While harmless, exit early if there is nothing to do. */
277 if (bmpinfo->verts.is_empty() && bmpinfo->faces.is_empty()) [[unlikely]] {
278 return;
279 }
280
281 if (params->face_normals) {
282 threading::parallel_for(bmpinfo->faces.index_range(), 1024, [&](const IndexRange range) {
283 for (const int i : range) {
284 BMFace *f = bmpinfo->faces[i];
285 BM_face_calc_normal(f, f->no);
286 }
287 });
288 }
289
290 threading::parallel_for(bmpinfo->verts.index_range(), 1024, [&](const IndexRange range) {
291 for (const int i : range) {
292 BMVert *v = bmpinfo->verts[i];
293 bm_vert_calc_normals_impl(v);
294 }
295 });
296}
297
299{
301 params.face_normals = true;
303}
304
306
307/* -------------------------------------------------------------------- */
310
312 const Span<float3> fnos,
313 const Span<float3> vcos,
315{
316 /* Add weighted face normals to vertices, and normalize vert normals. */
317 bm_mesh_verts_calc_normals(bm, fnos, vcos, vnos);
318}
319
321
322/* -------------------------------------------------------------------- */
325
326void BM_normals_loops_edges_tag(BMesh *bm, const bool do_edges)
327{
328 BMFace *f;
329 BMEdge *e;
330 BMIter fiter, eiter;
331 BMLoop *l_curr, *l_first;
332
333 if (do_edges) {
334 int index_edge;
335 BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, index_edge) {
336 BMLoop *l_a, *l_b;
337
338 BM_elem_index_set(e, index_edge); /* set_inline */
340 if (BM_edge_loop_pair(e, &l_a, &l_b)) {
341 if (BM_elem_flag_test(e, BM_ELEM_SMOOTH) && l_a->v != l_b->v) {
343 }
344 }
345 }
347 }
348
349 int index_face, index_loop = 0;
350 BM_ITER_MESH_INDEX (f, &fiter, bm, BM_FACES_OF_MESH, index_face) {
351 BM_elem_index_set(f, index_face); /* set_inline */
352 l_curr = l_first = BM_FACE_FIRST_LOOP(f);
353 do {
354 BM_elem_index_set(l_curr, index_loop++); /* set_inline */
356 } while ((l_curr = l_curr->next) != l_first);
357 }
359}
360
365 const Span<float3> fnos,
366 float split_angle_cos,
367 const bool do_sharp_edges_tag)
368{
369 BMIter eiter;
370 BMEdge *e;
371 int i;
372
373 if (!fnos.is_empty()) {
375 }
376
377 if (do_sharp_edges_tag) {
378 BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, i) {
379 BM_elem_index_set(e, i); /* set_inline */
380 if (e->l != nullptr) {
381 bm_edge_tag_from_smooth_and_set_sharp(fnos, e, split_angle_cos);
382 }
383 }
384 }
385 else {
386 BM_ITER_MESH_INDEX (e, &eiter, bm, BM_EDGES_OF_MESH, i) {
387 BM_elem_index_set(e, i); /* set_inline */
388 if (e->l != nullptr) {
389 bm_edge_tag_from_smooth(fnos, e, split_angle_cos);
390 }
391 }
392 }
393
395}
396
397void BM_edges_sharp_from_angle_set(BMesh *bm, const float split_angle)
398{
399 if (split_angle >= float(M_PI)) {
400 /* Nothing to do! */
401 return;
402 }
403
404 bm_mesh_edges_sharp_tag(bm, {}, cosf(split_angle), true);
405}
406
408
409/* -------------------------------------------------------------------- */
412
414{
415 BMLoop *lfan_pivot_next = l_curr;
416 BMEdge *e_next = l_curr->e;
417
418 BLI_assert(!BM_elem_flag_test(lfan_pivot_next, BM_ELEM_TAG));
419 BM_elem_flag_enable(lfan_pivot_next, BM_ELEM_TAG);
420
421 while (true) {
422 /* Much simpler than in sibling code with basic Mesh data! */
423 lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot_next, &e_next);
424
425 if (!lfan_pivot_next || !BM_elem_flag_test(e_next, BM_ELEM_TAG)) {
426 /* Sharp loop/edge, so not a cyclic smooth fan... */
427 return false;
428 }
429 /* Smooth loop/edge... */
430 if (BM_elem_flag_test(lfan_pivot_next, BM_ELEM_TAG)) {
431 if (lfan_pivot_next == l_curr) {
432 /* We walked around a whole cyclic smooth fan
433 * without finding any already-processed loop,
434 * means we can use initial l_curr/l_prev edge as start for this smooth fan. */
435 return true;
436 }
437 /* ... already checked in some previous looping, we can abort. */
438 return false;
439 }
440 /* ... we can skip it in future, and keep checking the smooth fan. */
441 BM_elem_flag_enable(lfan_pivot_next, BM_ELEM_TAG);
442 }
443}
444
458 const Span<float3> vcos,
459 const Span<float3> fnos,
460 const short (*clnors_data)[2],
461 const int cd_loop_clnors_offset,
462 const bool has_clnors,
463 /* Cache. */
464 Vector<float3, 16> *edge_vectors,
465 /* Iterate. */
466 BMLoop *l_curr,
467 /* Result. */
468 MutableSpan<float3> r_lnos,
469 MLoopNorSpaceArray *r_lnors_spacearr)
470{
472 BLI_assert(fnos.is_empty() || ((bm->elem_index_dirty & BM_FACE) == 0));
473 BLI_assert(vcos.is_empty() || ((bm->elem_index_dirty & BM_VERT) == 0));
475
476 int handled = 0;
477
478 /* Temp normal stack. */
479 BLI_SMALLSTACK_DECLARE(normal, float *);
480 /* Temp clnors stack. */
481 BLI_SMALLSTACK_DECLARE(clnors, short *);
482 /* Temp edge vectors stack, only used when computing lnor spacearr. */
483
484 /* A smooth edge, we have to check for cyclic smooth fan case.
485 * If we find a new, never-processed cyclic smooth fan, we can do it now using that loop/edge
486 * as 'entry point', otherwise we can skip it. */
487
488 /* NOTE: In theory, we could make bm_mesh_loop_check_cyclic_smooth_fan() store
489 * mlfan_pivot's in a stack, to avoid having to fan again around
490 * the vert during actual computation of clnor & clnorspace. However, this would complicate
491 * the code, add more memory usage, and
492 * BM_vert_step_fan_loop() is quite cheap in term of CPU cycles,
493 * so really think it's not worth it. */
494 if (BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
496 {
497 }
498 else if (!BM_elem_flag_test(l_curr->e, BM_ELEM_TAG) &&
500 {
501 /* Simple case (both edges around that vertex are sharp in related face),
502 * this vertex just takes its face normal.
503 */
504 const int l_curr_index = BM_elem_index_get(l_curr);
505 const float3 &no = !fnos.is_empty() ? fnos[BM_elem_index_get(l_curr->f)] :
506 float3(l_curr->f->no);
507 copy_v3_v3(r_lnos[l_curr_index], no);
508
509 /* If needed, generate this (simple!) lnor space. */
510 if (r_lnors_spacearr) {
511 float vec_curr[3], vec_prev[3];
512 MLoopNorSpace *lnor_space = BKE_lnor_space_create(r_lnors_spacearr);
513
514 {
515 const BMVert *v_pivot = l_curr->v;
516 const float3 &co_pivot = !vcos.is_empty() ? vcos[BM_elem_index_get(v_pivot)] :
517 float3(v_pivot->co);
518 const BMVert *v_1 = l_curr->next->v;
519 const float3 co_1 = !vcos.is_empty() ? vcos[BM_elem_index_get(v_1)] : float3(v_1->co);
520 const BMVert *v_2 = l_curr->prev->v;
521 const float3 co_2 = !vcos.is_empty() ? vcos[BM_elem_index_get(v_2)] : float3(v_2->co);
522
523 BLI_assert(v_1 == BM_edge_other_vert(l_curr->e, v_pivot));
524 BLI_assert(v_2 == BM_edge_other_vert(l_curr->prev->e, v_pivot));
525
526 sub_v3_v3v3(vec_curr, co_1, co_pivot);
527 normalize_v3(vec_curr);
528 sub_v3_v3v3(vec_prev, co_2, co_pivot);
529 normalize_v3(vec_prev);
530 }
531
532 BKE_lnor_space_define(lnor_space, r_lnos[l_curr_index], vec_curr, vec_prev, {});
533 /* We know there is only one loop in this space,
534 * no need to create a linklist in this case... */
535 BKE_lnor_space_add_loop(r_lnors_spacearr, lnor_space, l_curr_index, l_curr, true);
536
537 if (has_clnors) {
538 const short (*clnor)[2] = clnors_data ?
539 &clnors_data[l_curr_index] :
540 static_cast<const short (*)[2]>(
541 BM_ELEM_CD_GET_VOID_P(l_curr, cd_loop_clnors_offset));
542 BKE_lnor_space_custom_data_to_normal(lnor_space, *clnor, r_lnos[l_curr_index]);
543 }
544 }
545 handled = 1;
546 }
547 /* We *do not need* to check/tag loops as already computed!
548 * Due to the fact a loop only links to one of its two edges,
549 * a same fan *will never be walked more than once!*
550 * Since we consider edges having neighbor faces with inverted (flipped) normals as sharp,
551 * we are sure that no fan will be skipped, even only considering the case
552 * (sharp curr_edge, smooth prev_edge), and not the alternative
553 * (smooth curr_edge, sharp prev_edge).
554 * All this due/thanks to link between normals and loop ordering.
555 */
556 else {
557 /* We have to fan around current vertex, until we find the other non-smooth edge,
558 * and accumulate face normals into the vertex!
559 * Note in case this vertex has only one sharp edge,
560 * this is a waste because the normal is the same as the vertex normal,
561 * but I do not see any easy way to detect that (would need to count number of sharp edges
562 * per vertex, I doubt the additional memory usage would be worth it, especially as it
563 * should not be a common case in real-life meshes anyway).
564 */
565 BMVert *v_pivot = l_curr->v;
566 BMEdge *e_next;
567 const BMEdge *e_org = l_curr->e;
568 BMLoop *lfan_pivot, *lfan_pivot_next;
569 int lfan_pivot_index;
570 float lnor[3] = {0.0f, 0.0f, 0.0f};
571 float vec_curr[3], vec_next[3], vec_org[3];
572
573 /* We validate clnors data on the fly - cheapest way to do! */
574 int clnors_avg[2] = {0, 0};
575 const short (*clnor_ref)[2] = nullptr;
576 int clnors_count = 0;
577 bool clnors_invalid = false;
578
579 const float3 &co_pivot = !vcos.is_empty() ? vcos[BM_elem_index_get(v_pivot)] :
580 float3(v_pivot->co);
581
582 MLoopNorSpace *lnor_space = r_lnors_spacearr ? BKE_lnor_space_create(r_lnors_spacearr) :
583 nullptr;
584
585 BLI_assert((edge_vectors == nullptr) || edge_vectors->is_empty());
586
587 lfan_pivot = l_curr;
588 lfan_pivot_index = BM_elem_index_get(lfan_pivot);
589 e_next = lfan_pivot->e; /* Current edge here, actually! */
590
591 /* Only need to compute previous edge's vector once,
592 * then we can just reuse old current one! */
593 {
594 const BMVert *v_2 = lfan_pivot->next->v;
595 const float3 co_2 = !vcos.is_empty() ? vcos[BM_elem_index_get(v_2)] : float3(v_2->co);
596
597 BLI_assert(v_2 == BM_edge_other_vert(e_next, v_pivot));
598
599 sub_v3_v3v3(vec_org, co_2, co_pivot);
600 normalize_v3(vec_org);
601 copy_v3_v3(vec_curr, vec_org);
602
603 if (r_lnors_spacearr) {
604 edge_vectors->append(vec_org);
605 }
606 }
607
608 while (true) {
609 lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next);
610 if (lfan_pivot_next) {
611 BLI_assert(lfan_pivot_next->v == v_pivot);
612 }
613 else {
614 /* next edge is non-manifold, we have to find it ourselves! */
615 e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e;
616 }
617
618 /* Compute edge vector.
619 * NOTE: We could pre-compute those into an array, in the first iteration,
620 * instead of computing them twice (or more) here.
621 * However, time gained is not worth memory and time lost,
622 * given the fact that this code should not be called that much in real-life meshes.
623 */
624 {
625 const BMVert *v_2 = BM_edge_other_vert(e_next, v_pivot);
626 const float3 co_2 = !vcos.is_empty() ? vcos[BM_elem_index_get(v_2)] : float3(v_2->co);
627
628 sub_v3_v3v3(vec_next, co_2, co_pivot);
629 normalize_v3(vec_next);
630 }
631
632 {
633 /* Code similar to accumulate_vertex_normals_poly_v3. */
634 /* Calculate angle between the two face edges incident on this vertex. */
635 const BMFace *f = lfan_pivot->f;
636 const float fac = math::safe_acos_approx(dot_v3v3(vec_next, vec_curr));
637 const float3 &no = !fnos.is_empty() ? fnos[BM_elem_index_get(f)] : float3(f->no);
638 /* Accumulate */
639 madd_v3_v3fl(lnor, no, fac);
640
641 if (has_clnors) {
642 /* Accumulate all clnors, if they are not all equal we have to fix that! */
643 const short (*clnor)[2] = clnors_data ?
644 &clnors_data[lfan_pivot_index] :
645 static_cast<const short (*)[2]>(BM_ELEM_CD_GET_VOID_P(
646 lfan_pivot, cd_loop_clnors_offset));
647 if (clnors_count) {
648 clnors_invalid |= ((*clnor_ref)[0] != (*clnor)[0] || (*clnor_ref)[1] != (*clnor)[1]);
649 }
650 else {
651 clnor_ref = clnor;
652 }
653 clnors_avg[0] += (*clnor)[0];
654 clnors_avg[1] += (*clnor)[1];
655 clnors_count++;
656 /* We store here a pointer to all custom lnors processed. */
657 BLI_SMALLSTACK_PUSH(clnors, (short *)*clnor);
658 }
659 }
660
661 /* We store here a pointer to all loop-normals processed. */
662 BLI_SMALLSTACK_PUSH(normal, (float *)r_lnos[lfan_pivot_index]);
663
664 if (r_lnors_spacearr) {
665 /* Assign current lnor space to current 'vertex' loop. */
666 BKE_lnor_space_add_loop(r_lnors_spacearr, lnor_space, lfan_pivot_index, lfan_pivot, false);
667 if (e_next != e_org) {
668 /* We store here all edges-normalized vectors processed. */
669 edge_vectors->append(vec_next);
670 }
671 }
672
673 handled += 1;
674
675 if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) {
676 /* Next edge is sharp, we have finished with this fan of faces around this vert! */
677 break;
678 }
679
680 /* Copy next edge vector to current one. */
681 copy_v3_v3(vec_curr, vec_next);
682 /* Next pivot loop to current one. */
683 lfan_pivot = lfan_pivot_next;
684 lfan_pivot_index = BM_elem_index_get(lfan_pivot);
685 }
686
687 {
688 float lnor_len = normalize_v3(lnor);
689
690 /* If we are generating lnor spacearr, we can now define the one for this fan. */
691 if (r_lnors_spacearr) {
692 if (lnor_len == 0.0f) [[unlikely]] {
693 /* Use vertex normal as fallback! */
694 copy_v3_v3(lnor, r_lnos[lfan_pivot_index]);
695 lnor_len = 1.0f;
696 }
697
698 BKE_lnor_space_define(lnor_space, lnor, vec_org, vec_next, *edge_vectors);
699 edge_vectors->clear();
700
701 if (has_clnors) {
702 if (clnors_invalid) {
703 short *clnor;
704
705 clnors_avg[0] /= clnors_count;
706 clnors_avg[1] /= clnors_count;
707 /* Fix/update all clnors of this fan with computed average value. */
708
709 /* Prints continuously when merge custom normals, so commenting. */
710 // printf("Invalid clnors in this fan!\n");
711
712 while ((clnor = static_cast<short *>(BLI_SMALLSTACK_POP(clnors)))) {
713 // print_v2("org clnor", clnor);
714 clnor[0] = short(clnors_avg[0]);
715 clnor[1] = short(clnors_avg[1]);
716 }
717 // print_v2("new clnors", clnors_avg);
718 }
719 else {
720 /* We still have to consume the stack! */
721 while (BLI_SMALLSTACK_POP(clnors)) {
722 /* pass */
723 }
724 }
725 BKE_lnor_space_custom_data_to_normal(lnor_space, *clnor_ref, lnor);
726 }
727 }
728
729 /* In case we get a zero normal here, just use vertex normal already set! */
730 if (lnor_len != 0.0f) [[likely]] {
731 /* Copy back the final computed normal into all related loop-normals. */
732 float *nor;
733
734 while ((nor = static_cast<float *>(BLI_SMALLSTACK_POP(normal)))) {
735 copy_v3_v3(nor, lnor);
736 }
737 }
738 else {
739 /* We still have to consume the stack! */
740 while (BLI_SMALLSTACK_POP(normal)) {
741 /* pass */
742 }
743 }
744 }
745
746 /* Tag related vertex as sharp, to avoid fanning around it again
747 * (in case it was a smooth one). */
748 if (r_lnors_spacearr) {
750 }
751 }
752 return handled;
753}
754
755static int bm_loop_index_cmp(const void *a, const void *b)
756{
759 return -1;
760 }
761 return 1;
762}
763
773 const BMLoop *l_a,
774 const BMLoop *l_b)
775{
776 BLI_assert(l_a->radial_next == l_b);
777 return (
778 /* The face is manifold. */
779 (l_b->radial_next == l_a) &&
780 /* Faces have winding that faces the same way. */
781 (l_a->v != l_b->v) &&
782 /* The edge is smooth. */
784 /* Both faces are smooth. */
786}
787
789 BMEdge *e,
790 const float split_angle_cos)
791{
792 BLI_assert(e->l != nullptr);
793 BMLoop *l_a = e->l, *l_b = l_a->radial_next;
794 bool is_smooth = false;
796 if (split_angle_cos != -1.0f) {
797 const float dot = fnos.is_empty() ? dot_v3v3(l_a->f->no, l_b->f->no) :
798 dot_v3v3(fnos[BM_elem_index_get(l_a->f)],
799 fnos[BM_elem_index_get(l_b->f)]);
800 if (dot >= split_angle_cos) {
801 is_smooth = true;
802 }
803 }
804 else {
805 is_smooth = true;
806 }
807 }
808
809 /* Perform `BM_elem_flag_set(e, BM_ELEM_TAG, is_smooth)`
810 * NOTE: This will be set by multiple threads however it will be set to the same value. */
811
812 /* No need for atomics here as this is a single byte. */
813 char *hflag_p = &e->head.hflag;
814 if (is_smooth) {
815 *hflag_p = *hflag_p | BM_ELEM_TAG;
816 }
817 else {
818 *hflag_p = *hflag_p & ~BM_ELEM_TAG;
819 }
820}
821
830 BMEdge *e,
831 const float split_angle_cos)
832{
833 BLI_assert(e->l != nullptr);
834 BMLoop *l_a = e->l, *l_b = l_a->radial_next;
835 bool is_smooth = false;
837 if (split_angle_cos != -1.0f) {
838 const float dot = fnos.is_empty() ? dot_v3v3(l_a->f->no, l_b->f->no) :
839 dot_v3v3(fnos[BM_elem_index_get(l_a->f)],
840 fnos[BM_elem_index_get(l_b->f)]);
841 if (dot >= split_angle_cos) {
842 is_smooth = true;
843 }
844 else {
845 /* Note that we do not care about the other sharp-edge cases
846 * (sharp face, non-manifold edge, etc.),
847 * only tag edge as sharp when it is due to angle threshold. */
849 }
850 }
851 else {
852 is_smooth = true;
853 }
854 }
855
856 BM_elem_flag_set(e, BM_ELEM_TAG, is_smooth);
857}
858
865 const Span<float3> vcos,
866 const Span<float3> fnos,
867 MutableSpan<float3> r_lnos,
868 const short (*clnors_data)[2],
869 const int cd_loop_clnors_offset,
870 const bool do_rebuild,
871 const float split_angle_cos,
872 /* TLS */
873 MLoopNorSpaceArray *r_lnors_spacearr,
874 Vector<float3, 16> *edge_vectors,
875 /* Iterate over. */
876 BMVert *v)
877{
878 /* Respecting face order is necessary so the initial starting loop is consistent
879 * with looping over loops of all faces.
880 *
881 * Logically we could sort the loops by their index & loop over them
882 * however it's faster to use the lowest index of an un-ordered list
883 * since it's common that smooth vertices only ever need to pick one loop
884 * which then handles all the others.
885 *
886 * Sorting is only performed when multiple fans are found. */
887 const bool has_clnors = true;
888 LinkNode *loops_of_vert = nullptr;
889 int loops_of_vert_count = 0;
890 /* When false the caller must have already tagged the edges. */
891 const bool do_edge_tag = (split_angle_cos != EDGE_TAG_FROM_SPLIT_ANGLE_BYPASS);
892
893 /* The loop with the lowest index. */
894 {
895 LinkNode *link_best;
896 uint index_best = UINT_MAX;
897 BMEdge *e_curr_iter = v->e;
898 do { /* Edges of vertex. */
899 BMLoop *l_curr = e_curr_iter->l;
900 if (l_curr == nullptr) {
901 continue;
902 }
903
904 if (do_edge_tag) {
905 bm_edge_tag_from_smooth(fnos, e_curr_iter, split_angle_cos);
906 }
907
908 do { /* Radial loops. */
909 if (l_curr->v != v) {
910 continue;
911 }
912 if (do_rebuild && !BM_ELEM_API_FLAG_TEST(l_curr, BM_LNORSPACE_UPDATE) &&
914 {
915 continue;
916 }
918 BLI_linklist_prepend_alloca(&loops_of_vert, l_curr);
919 loops_of_vert_count += 1;
920
921 const uint index_test = uint(BM_elem_index_get(l_curr));
922 if (index_best > index_test) {
923 index_best = index_test;
924 link_best = loops_of_vert;
925 }
926 } while ((l_curr = l_curr->radial_next) != e_curr_iter->l);
927 } while ((e_curr_iter = BM_DISK_EDGE_NEXT(e_curr_iter, v)) != v->e);
928
929 if (loops_of_vert == nullptr) [[unlikely]] {
930 return;
931 }
932
933 /* Immediately pop the best element.
934 * The order doesn't matter, so swap the links as it's simpler than tracking
935 * reference to `link_best`. */
936 if (link_best != loops_of_vert) {
937 std::swap(link_best->link, loops_of_vert->link);
938 }
939 }
940
941 bool loops_of_vert_is_sorted = false;
942
943 /* Keep track of the number of loops that have been assigned. */
944 int loops_of_vert_handled = 0;
945
946 while (loops_of_vert != nullptr) {
947 BMLoop *l_best = static_cast<BMLoop *>(loops_of_vert->link);
948 loops_of_vert = loops_of_vert->next;
949
950 BLI_assert(l_best->v == v);
951 loops_of_vert_handled += bm_mesh_loops_calc_normals_for_loop(bm,
952 vcos,
953 fnos,
954 clnors_data,
955 cd_loop_clnors_offset,
956 has_clnors,
957 edge_vectors,
958 l_best,
959 r_lnos,
960 r_lnors_spacearr);
961
962 /* Check if an early exit is possible without an exhaustive inspection of every loop
963 * where 1 loop's fan extends out to all remaining loops.
964 * This is a common case for smooth vertices. */
965 BLI_assert(loops_of_vert_handled <= loops_of_vert_count);
966 if (loops_of_vert_handled == loops_of_vert_count) {
967 break;
968 }
969
970 /* Note on sorting, in some cases it will be faster to scan for the lowest index each time.
971 * However in the worst case this is `O(N^2)`, so use a single sort call instead. */
972 if (!loops_of_vert_is_sorted) {
973 if (loops_of_vert && loops_of_vert->next) {
974 loops_of_vert = BLI_linklist_sort(loops_of_vert, bm_loop_index_cmp);
975 loops_of_vert_is_sorted = true;
976 }
977 }
978 }
979}
980
986 BMesh *bm,
987 const Span<float3> vcos,
988 const Span<float3> fnos,
989 MutableSpan<float3> r_lnos,
990 const bool do_rebuild,
991 const float split_angle_cos,
992 /* TLS */
993 MLoopNorSpaceArray *r_lnors_spacearr,
994 Vector<float3, 16> *edge_vectors,
995 /* Iterate over. */
996 BMVert *v)
997{
998 const bool has_clnors = false;
999 const short (*clnors_data)[2] = nullptr;
1000 /* When false the caller must have already tagged the edges. */
1001 const bool do_edge_tag = (split_angle_cos != EDGE_TAG_FROM_SPLIT_ANGLE_BYPASS);
1002 const int cd_loop_clnors_offset = -1;
1003
1004 BMEdge *e_curr_iter;
1005
1006 /* Unfortunately a loop is needed just to clear loop-tags. */
1007 e_curr_iter = v->e;
1008 do { /* Edges of vertex. */
1009 BMLoop *l_curr = e_curr_iter->l;
1010 if (l_curr == nullptr) {
1011 continue;
1012 }
1013
1014 if (do_edge_tag) {
1015 bm_edge_tag_from_smooth(fnos, e_curr_iter, split_angle_cos);
1016 }
1017
1018 do { /* Radial loops. */
1019 if (l_curr->v != v) {
1020 continue;
1021 }
1023 } while ((l_curr = l_curr->radial_next) != e_curr_iter->l);
1024 } while ((e_curr_iter = BM_DISK_EDGE_NEXT(e_curr_iter, v)) != v->e);
1025
1026 e_curr_iter = v->e;
1027 do { /* Edges of vertex. */
1028 BMLoop *l_curr = e_curr_iter->l;
1029 if (l_curr == nullptr) {
1030 continue;
1031 }
1032 do { /* Radial loops. */
1033 if (l_curr->v != v) {
1034 continue;
1035 }
1036 if (do_rebuild && !BM_ELEM_API_FLAG_TEST(l_curr, BM_LNORSPACE_UPDATE) &&
1038 {
1039 continue;
1040 }
1042 vcos,
1043 fnos,
1044 clnors_data,
1045 cd_loop_clnors_offset,
1046 has_clnors,
1047 edge_vectors,
1048 l_curr,
1049 r_lnos,
1050 r_lnors_spacearr);
1051 } while ((l_curr = l_curr->radial_next) != e_curr_iter->l);
1052 } while ((e_curr_iter = BM_DISK_EDGE_NEXT(e_curr_iter, v)) != v->e);
1053}
1054
1064 const Span<float3> vcos,
1065 const Span<float3> fnos,
1066 MutableSpan<float3> r_lnos,
1067 MLoopNorSpaceArray *r_lnors_spacearr,
1068 const short (*clnors_data)[2],
1069 const int cd_loop_clnors_offset,
1070 const bool do_rebuild,
1071 const float split_angle_cos)
1072{
1073 BMIter fiter;
1074 BMFace *f_curr;
1075 const bool has_clnors = clnors_data || (cd_loop_clnors_offset != -1);
1076 /* When false the caller must have already tagged the edges. */
1077 const bool do_edge_tag = (split_angle_cos != EDGE_TAG_FROM_SPLIT_ANGLE_BYPASS);
1078
1079 MLoopNorSpaceArray _lnors_spacearr = {nullptr};
1080
1081 std::unique_ptr<Vector<float3, 16>> edge_vectors = nullptr;
1082
1083 {
1084 char htype = 0;
1085 if (!vcos.is_empty()) {
1086 htype |= BM_VERT;
1087 }
1088 /* Face/Loop indices are set inline below. */
1090 }
1091
1092 if (!r_lnors_spacearr && has_clnors) {
1093 /* We need to compute lnor spacearr if some custom lnor data are given to us! */
1094 r_lnors_spacearr = &_lnors_spacearr;
1095 }
1096 if (r_lnors_spacearr) {
1098 edge_vectors = std::make_unique<Vector<float3, 16>>();
1099 }
1100
1101 /* Clear all loops' tags (means none are to be skipped for now). */
1102 int index_face, index_loop = 0;
1103 BM_ITER_MESH_INDEX (f_curr, &fiter, bm, BM_FACES_OF_MESH, index_face) {
1104 BMLoop *l_curr, *l_first;
1105
1106 BM_elem_index_set(f_curr, index_face); /* set_inline */
1107
1108 l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
1109 do {
1110 BM_elem_index_set(l_curr, index_loop++); /* set_inline */
1112 } while ((l_curr = l_curr->next) != l_first);
1113 }
1115
1116 /* Always tag edges based on winding & sharp edge flag
1117 * (even when the auto-smooth angle doesn't need to be calculated). */
1118 if (do_edge_tag) {
1119 bm_mesh_edges_sharp_tag(bm, fnos, has_clnors ? -1.0f : split_angle_cos, false);
1120 }
1121
1122 /* We now know edges that can be smoothed (they are tagged),
1123 * and edges that will be hard (they aren't).
1124 * Now, time to generate the normals.
1125 */
1126 BM_ITER_MESH (f_curr, &fiter, bm, BM_FACES_OF_MESH) {
1127 BMLoop *l_curr, *l_first;
1128
1129 l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
1130 do {
1131 if (do_rebuild && !BM_ELEM_API_FLAG_TEST(l_curr, BM_LNORSPACE_UPDATE) &&
1133 {
1134 continue;
1135 }
1137 vcos,
1138 fnos,
1139 clnors_data,
1140 cd_loop_clnors_offset,
1141 has_clnors,
1142 edge_vectors.get(),
1143 l_curr,
1144 r_lnos,
1145 r_lnors_spacearr);
1146 } while ((l_curr = l_curr->next) != l_first);
1147 }
1148
1149 if (r_lnors_spacearr) {
1150 if (r_lnors_spacearr == &_lnors_spacearr) {
1151 BKE_lnor_spacearr_free(r_lnors_spacearr);
1152 }
1153 }
1154}
1155
1170
1178
1179static void bm_mesh_loops_calc_normals_for_vert_init_fn(const void *__restrict userdata,
1180 void *__restrict chunk)
1181{
1182 const auto *data = static_cast<const BMLoopsCalcNormalsWithCoordsData *>(userdata);
1183 auto *tls_data = static_cast<BMLoopsCalcNormalsWithCoords_TLS *>(chunk);
1184 if (data->r_lnors_spacearr) {
1185 tls_data->edge_vectors = MEM_new<Vector<float3, 16>>(__func__);
1186 BKE_lnor_spacearr_tls_init(data->r_lnors_spacearr, &tls_data->lnors_spacearr_buf);
1187 tls_data->lnors_spacearr = &tls_data->lnors_spacearr_buf;
1188 }
1189 else {
1190 tls_data->lnors_spacearr = nullptr;
1191 }
1192}
1193
1194static void bm_mesh_loops_calc_normals_for_vert_reduce_fn(const void *__restrict userdata,
1195 void *__restrict /*chunk_join*/,
1196 void *__restrict chunk)
1197{
1198 const auto *data = static_cast<const BMLoopsCalcNormalsWithCoordsData *>(userdata);
1199 auto *tls_data = static_cast<BMLoopsCalcNormalsWithCoords_TLS *>(chunk);
1200
1201 if (data->r_lnors_spacearr) {
1202 BKE_lnor_spacearr_tls_join(data->r_lnors_spacearr, tls_data->lnors_spacearr);
1203 }
1204}
1205
1206static void bm_mesh_loops_calc_normals_for_vert_free_fn(const void *__restrict userdata,
1207 void *__restrict chunk)
1208{
1209 const auto *data = static_cast<const BMLoopsCalcNormalsWithCoordsData *>(userdata);
1210 auto *tls_data = static_cast<BMLoopsCalcNormalsWithCoords_TLS *>(chunk);
1211
1212 if (data->r_lnors_spacearr) {
1213 MEM_delete(tls_data->edge_vectors);
1214 }
1215}
1216
1218 void *userdata, MempoolIterData *mp_v, const TaskParallelTLS *__restrict tls)
1219{
1220 BMVert *v = reinterpret_cast<BMVert *>(mp_v);
1221 if (v->e == nullptr) {
1222 return;
1223 }
1224 auto *data = static_cast<BMLoopsCalcNormalsWithCoordsData *>(userdata);
1225 auto *tls_data = static_cast<BMLoopsCalcNormalsWithCoords_TLS *>(tls->userdata_chunk);
1227 data->vcos,
1228 data->fnos,
1229 data->r_lnos,
1230
1231 data->clnors_data,
1232 data->cd_loop_clnors_offset,
1233 data->do_rebuild,
1234 data->split_angle_cos,
1235 /* Thread local. */
1236 tls_data->lnors_spacearr,
1237 tls_data->edge_vectors,
1238 /* Iterate over. */
1239 v);
1240}
1241
1243 void *userdata, MempoolIterData *mp_v, const TaskParallelTLS *__restrict tls)
1244{
1245 BMVert *v = reinterpret_cast<BMVert *>(mp_v);
1246 if (v->e == nullptr) {
1247 return;
1248 }
1249 auto *data = static_cast<BMLoopsCalcNormalsWithCoordsData *>(userdata);
1250 auto *tls_data = static_cast<BMLoopsCalcNormalsWithCoords_TLS *>(tls->userdata_chunk);
1252 data->vcos,
1253 data->fnos,
1254 data->r_lnos,
1255
1256 data->do_rebuild,
1257 data->split_angle_cos,
1258 /* Thread local. */
1259 tls_data->lnors_spacearr,
1260 tls_data->edge_vectors,
1261 /* Iterate over. */
1262 v);
1263}
1264
1266 const Span<float3> vcos,
1267 const Span<float3> fnos,
1268 MutableSpan<float3> r_lnos,
1269 MLoopNorSpaceArray *r_lnors_spacearr,
1270 const short (*clnors_data)[2],
1271 const int cd_loop_clnors_offset,
1272 const bool do_rebuild,
1273 const float split_angle_cos)
1274{
1275 const bool has_clnors = clnors_data || (cd_loop_clnors_offset != -1);
1276 MLoopNorSpaceArray _lnors_spacearr = {nullptr};
1277
1278 {
1279 char htype = BM_LOOP;
1280 if (!vcos.is_empty()) {
1281 htype |= BM_VERT;
1282 }
1283 if (!fnos.is_empty()) {
1284 htype |= BM_FACE;
1285 }
1286 /* Face/Loop indices are set inline below. */
1288 }
1289
1290 if (!r_lnors_spacearr && has_clnors) {
1291 /* We need to compute lnor spacearr if some custom lnor data are given to us! */
1292 r_lnors_spacearr = &_lnors_spacearr;
1293 }
1294 if (r_lnors_spacearr) {
1296 }
1297
1298 /* We now know edges that can be smoothed (they are tagged),
1299 * and edges that will be hard (they aren't).
1300 * Now, time to generate the normals.
1301 */
1302
1303 TaskParallelSettings settings;
1305
1306 BMLoopsCalcNormalsWithCoords_TLS tls = {nullptr};
1307
1308 settings.userdata_chunk = &tls;
1309 settings.userdata_chunk_size = sizeof(tls);
1310
1314
1316 data.bm = bm;
1317 data.vcos = vcos;
1318 data.fnos = fnos;
1319 data.r_lnos = r_lnos;
1320 data.r_lnors_spacearr = r_lnors_spacearr;
1321 data.clnors_data = clnors_data;
1322 data.cd_loop_clnors_offset = cd_loop_clnors_offset;
1323 data.do_rebuild = do_rebuild;
1324 data.split_angle_cos = split_angle_cos;
1325
1330 &data,
1331 &settings);
1332
1333 if (r_lnors_spacearr) {
1334 if (r_lnors_spacearr == &_lnors_spacearr) {
1335 BKE_lnor_spacearr_free(r_lnors_spacearr);
1336 }
1337 }
1338}
1339
1341 const Span<float3> vcos,
1342 const Span<float3> fnos,
1343 MutableSpan<float3> r_lnos,
1344 MLoopNorSpaceArray *r_lnors_spacearr,
1345 const short (*clnors_data)[2],
1346 const int cd_loop_clnors_offset,
1347 const bool do_rebuild,
1348 const float split_angle_cos)
1349{
1350 if (bm->totloop < BM_THREAD_LIMIT) {
1352 vcos,
1353 fnos,
1354 r_lnos,
1355 r_lnors_spacearr,
1356 clnors_data,
1357 cd_loop_clnors_offset,
1358 do_rebuild,
1359 split_angle_cos);
1360 }
1361 else {
1363 vcos,
1364 fnos,
1365 r_lnos,
1366 r_lnors_spacearr,
1367 clnors_data,
1368 cd_loop_clnors_offset,
1369 do_rebuild,
1370 split_angle_cos);
1371 }
1372}
1373
1374/* This threshold is a bit touchy (usual float precision issue), this value seems OK. */
1375#define LNOR_SPACE_TRIGO_THRESHOLD (1.0f - 1e-4f)
1376
1382 MLoopNorSpaceArray *lnors_spacearr,
1383 const float (*new_lnors)[3])
1384{
1385 BLI_bitmap *done_loops = BLI_BITMAP_NEW(size_t(bm->totloop), __func__);
1386 bool changed = false;
1387
1389
1390 for (int i = 0; i < bm->totloop; i++) {
1391 if (!lnors_spacearr->lspacearr[i]) {
1392 /* This should not happen in theory, but in some rare case (probably ugly geometry)
1393 * we can get some nullptr loopspacearr at this point. :/
1394 * Maybe we should set those loops' edges as sharp?
1395 */
1396 BLI_BITMAP_ENABLE(done_loops, i);
1397 if (G.debug & G_DEBUG) {
1398 printf("WARNING! Getting invalid nullptr loop space for loop %d!\n", i);
1399 }
1400 continue;
1401 }
1402
1403 if (!BLI_BITMAP_TEST(done_loops, i)) {
1404 /* Notes:
1405 * * In case of mono-loop smooth fan, we have nothing to do.
1406 * * Loops in this linklist are ordered (in reversed order compared to how they were
1407 * discovered by bke::mesh::normals_calc_corners(), but this is not a problem).
1408 * Which means if we find a mismatching clnor,
1409 * we know all remaining loops will have to be in a new, different smooth fan/lnor space.
1410 * * In smooth fan case, we compare each clnor against a ref one,
1411 * to avoid small differences adding up into a real big one in the end!
1412 */
1413 if (lnors_spacearr->lspacearr[i]->flags & MLNOR_SPACE_IS_SINGLE) {
1414 BLI_BITMAP_ENABLE(done_loops, i);
1415 continue;
1416 }
1417
1418 LinkNode *loops = lnors_spacearr->lspacearr[i]->loops;
1419 BMLoop *prev_ml = nullptr;
1420 const float *org_nor = nullptr;
1421
1422 while (loops) {
1423 BMLoop *ml = static_cast<BMLoop *>(loops->link);
1424 const int lidx = BM_elem_index_get(ml);
1425 const float *nor = new_lnors[lidx];
1426
1427 if (!org_nor) {
1428 org_nor = nor;
1429 }
1430 else if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1431 /* Current normal differs too much from org one, we have to tag the edge between
1432 * previous loop's face and current's one as sharp.
1433 * We know those two loops do not point to the same edge,
1434 * since we do not allow reversed winding in a same smooth fan.
1435 */
1436 BMEdge *e = (prev_ml->e == ml->prev->e) ? prev_ml->e : ml->e;
1437
1439 changed = true;
1440
1441 org_nor = nor;
1442 }
1443
1444 prev_ml = ml;
1445 loops = loops->next;
1446 BLI_BITMAP_ENABLE(done_loops, lidx);
1447 }
1448
1449 /* We also have to check between last and first loops,
1450 * otherwise we may miss some sharp edges here!
1451 * This is just a simplified version of above while loop.
1452 * See #45984. */
1453 loops = lnors_spacearr->lspacearr[i]->loops;
1454 if (loops && org_nor) {
1455 BMLoop *ml = static_cast<BMLoop *>(loops->link);
1456 const int lidx = BM_elem_index_get(ml);
1457 const float *nor = new_lnors[lidx];
1458
1459 if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1460 BMEdge *e = (prev_ml->e == ml->prev->e) ? prev_ml->e : ml->e;
1461
1463 changed = true;
1464 }
1465 }
1466 }
1467 }
1468
1469 MEM_delete(done_loops);
1470 return changed;
1471}
1472
1478 MLoopNorSpaceArray *lnors_spacearr,
1479 short (*r_clnors_data)[2],
1480 const int cd_loop_clnors_offset,
1481 const float (*new_lnors)[3])
1482{
1483 BLI_bitmap *done_loops = BLI_BITMAP_NEW(size_t(bm->totloop), __func__);
1484
1485 BLI_SMALLSTACK_DECLARE(clnors_data, short *);
1486
1488
1489 for (int i = 0; i < bm->totloop; i++) {
1490 if (!lnors_spacearr->lspacearr[i]) {
1491 BLI_BITMAP_ENABLE(done_loops, i);
1492 if (G.debug & G_DEBUG) {
1493 printf("WARNING! Still getting invalid nullptr loop space in second loop for loop %d!\n",
1494 i);
1495 }
1496 continue;
1497 }
1498
1499 if (!BLI_BITMAP_TEST(done_loops, i)) {
1500 /* Note we accumulate and average all custom normals in current smooth fan,
1501 * to avoid getting different clnors data (tiny differences in plain custom normals can
1502 * give rather huge differences in computed 2D factors).
1503 */
1504 LinkNode *loops = lnors_spacearr->lspacearr[i]->loops;
1505
1506 if (lnors_spacearr->lspacearr[i]->flags & MLNOR_SPACE_IS_SINGLE) {
1507 BMLoop *ml = reinterpret_cast<BMLoop *>(loops);
1508 const int lidx = BM_elem_index_get(ml);
1509
1510 BLI_assert(lidx == i);
1511
1512 const float *nor = new_lnors[lidx];
1513 short *clnor = static_cast<short *>(r_clnors_data ?
1514 &r_clnors_data[lidx] :
1515 BM_ELEM_CD_GET_VOID_P(ml, cd_loop_clnors_offset));
1516
1517 BKE_lnor_space_custom_normal_to_data(lnors_spacearr->lspacearr[i], nor, clnor);
1518 BLI_BITMAP_ENABLE(done_loops, i);
1519 }
1520 else {
1521 int avg_nor_count = 0;
1522 float avg_nor[3];
1523 short clnor_data_tmp[2], *clnor_data;
1524
1525 zero_v3(avg_nor);
1526
1527 while (loops) {
1528 BMLoop *ml = static_cast<BMLoop *>(loops->link);
1529 const int lidx = BM_elem_index_get(ml);
1530 const float *nor = new_lnors[lidx];
1531 short *clnor = static_cast<short *>(
1532 r_clnors_data ? &r_clnors_data[lidx] :
1533 BM_ELEM_CD_GET_VOID_P(ml, cd_loop_clnors_offset));
1534
1535 avg_nor_count++;
1536 add_v3_v3(avg_nor, nor);
1537 BLI_SMALLSTACK_PUSH(clnors_data, clnor);
1538
1539 loops = loops->next;
1540 BLI_BITMAP_ENABLE(done_loops, lidx);
1541 }
1542
1543 mul_v3_fl(avg_nor, 1.0f / float(avg_nor_count));
1545 lnors_spacearr->lspacearr[i], avg_nor, clnor_data_tmp);
1546
1547 while ((clnor_data = static_cast<short *>(BLI_SMALLSTACK_POP(clnors_data)))) {
1548 clnor_data[0] = clnor_data_tmp[0];
1549 clnor_data[1] = clnor_data_tmp[1];
1550 }
1551 }
1552 }
1553 }
1554
1555 MEM_delete(done_loops);
1556}
1557
1566 const Span<float3> vcos,
1567 const Span<float3> fnos,
1568 MLoopNorSpaceArray *r_lnors_spacearr,
1569 short (*r_clnors_data)[2],
1570 const int cd_loop_clnors_offset,
1571 float (*new_lnors)[3],
1572 const int cd_new_lnors_offset,
1573 bool do_split_fans)
1574{
1575 BMFace *f;
1576 BMLoop *l;
1577 BMIter liter, fiter;
1578 Array<float3> cur_lnors(bm->totloop);
1579
1580 BKE_lnor_spacearr_clear(r_lnors_spacearr);
1581
1582 /* Tag smooth edges and set lnos from vnos when they might be completely smooth...
1583 * When using custom loop normals, disable the angle feature! */
1584 bm_mesh_edges_sharp_tag(bm, fnos, -1.0f, false);
1585
1586 /* Finish computing lnos by accumulating face normals
1587 * in each fan of faces defined by sharp edges. */
1589 vcos,
1590 fnos,
1591 cur_lnors,
1592 r_lnors_spacearr,
1593 r_clnors_data,
1594 cd_loop_clnors_offset,
1595 false,
1597
1598 /* Extract new normals from the data layer if necessary. */
1599 float (*custom_lnors)[3] = new_lnors;
1600
1601 if (new_lnors == nullptr) {
1602 custom_lnors = MEM_new_array_uninitialized<float[3]>(bm->totloop, __func__);
1603
1604 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1605 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1606 const float *normal = static_cast<float *>(BM_ELEM_CD_GET_VOID_P(l, cd_new_lnors_offset));
1607 copy_v3_v3(custom_lnors[BM_elem_index_get(l)], normal);
1608 }
1609 }
1610 }
1611
1612 /* Validate the new normals. */
1613 for (int i = 0; i < bm->totloop; i++) {
1614 if (is_zero_v3(custom_lnors[i])) {
1615 copy_v3_v3(custom_lnors[i], cur_lnors[i]);
1616 }
1617 else {
1618 normalize_v3(custom_lnors[i]);
1619 }
1620 }
1621
1622 /* Now, check each current smooth fan (one lnor space per smooth fan!),
1623 * and if all its matching custom lnors are not equal, add sharp edges as needed. */
1624 if (do_split_fans && bm_mesh_loops_split_lnor_fans(bm, r_lnors_spacearr, custom_lnors)) {
1625 /* If any sharp edges were added, run bm_mesh_loops_calc_normals() again to get lnor
1626 * spacearr/smooth fans matching the given custom lnors. */
1627 BKE_lnor_spacearr_clear(r_lnors_spacearr);
1628
1630 vcos,
1631 fnos,
1632 cur_lnors,
1633 r_lnors_spacearr,
1634 r_clnors_data,
1635 cd_loop_clnors_offset,
1636 false,
1638 }
1639
1640 /* And we just have to convert plain object-space custom normals to our
1641 * lnor space-encoded ones. */
1643 bm, r_lnors_spacearr, r_clnors_data, cd_loop_clnors_offset, custom_lnors);
1644
1645 if (custom_lnors != new_lnors) {
1646 MEM_delete(custom_lnors);
1647 }
1648}
1649
1651 const Span<float3> vnos,
1652 const Span<float3> fnos,
1653 MutableSpan<float3> r_lnos)
1654{
1655 BMIter fiter;
1656 BMFace *f_curr;
1657
1658 {
1659 char htype = BM_LOOP;
1660 if (!vnos.is_empty()) {
1661 htype |= BM_VERT;
1662 }
1663 if (!fnos.is_empty()) {
1664 htype |= BM_FACE;
1665 }
1667 }
1668
1669 BM_ITER_MESH (f_curr, &fiter, bm, BM_FACES_OF_MESH) {
1670 BMLoop *l_curr, *l_first;
1671 const bool is_face_flat = !BM_elem_flag_test(f_curr, BM_ELEM_SMOOTH);
1672
1673 l_curr = l_first = BM_FACE_FIRST_LOOP(f_curr);
1674 do {
1675 const float3 &no = is_face_flat ? (!fnos.is_empty() ? fnos[BM_elem_index_get(f_curr)] :
1676 float3(f_curr->no)) :
1677 (!vnos.is_empty() ? vnos[BM_elem_index_get(l_curr->v)] :
1678 float3(l_curr->v->no));
1679 copy_v3_v3(r_lnos[BM_elem_index_get(l_curr)], no);
1680
1681 } while ((l_curr = l_curr->next) != l_first);
1682 }
1683}
1684
1686 const Span<float3> vcos,
1687 const Span<float3> vnos,
1688 const Span<float3> fnos,
1689 const bool use_split_normals,
1690 MutableSpan<float3> r_lnos,
1691 MLoopNorSpaceArray *r_lnors_spacearr,
1692 short (*clnors_data)[2],
1693 const int cd_loop_clnors_offset,
1694 const bool do_rebuild)
1695{
1696
1697 if (use_split_normals) {
1699 vcos,
1700 fnos,
1701 r_lnos,
1702 r_lnors_spacearr,
1703 clnors_data,
1704 cd_loop_clnors_offset,
1705 do_rebuild,
1706 -1.0f);
1707 }
1708 else {
1709 BLI_assert(!r_lnors_spacearr);
1710 bm_mesh_loops_calc_normals_no_autosmooth(bm, vnos, fnos, r_lnos);
1711 }
1712}
1713
1715
1716/* -------------------------------------------------------------------- */
1719
1721{
1722 BLI_assert(bm->lnor_spacearr != nullptr);
1723
1725
1726 int cd_loop_clnors_offset = CustomData_get_offset_named(
1727 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
1728
1730 bm, {}, {}, {}, true, r_lnors, bm->lnor_spacearr, nullptr, cd_loop_clnors_offset, false);
1732}
1733
1734#define CLEAR_SPACEARRAY_THRESHOLD(x) ((x) / 2)
1735
1736void BM_lnorspace_invalidate(BMesh *bm, const bool do_invalidate_all)
1737{
1739 return;
1740 }
1741 if (do_invalidate_all || bm->totvertsel > CLEAR_SPACEARRAY_THRESHOLD(bm->totvert)) {
1743 return;
1744 }
1745 if (bm->lnor_spacearr == nullptr) {
1747 return;
1748 }
1749
1750 BMVert *v;
1751 BMLoop *l;
1752 BMIter viter, liter;
1753 /* NOTE: we could use temp tag of BMItem for that,
1754 * but probably better not use it in such a low-level func?
1755 * --mont29 */
1756 BLI_bitmap *done_verts = BLI_BITMAP_NEW(bm->totvert, __func__);
1757
1759
1760 /* When we affect a given vertex, we may affect following smooth fans:
1761 * - all smooth fans of said vertex;
1762 * - all smooth fans of all immediate loop-neighbors vertices;
1763 * This can be simplified as 'all loops of selected vertices and their immediate neighbors'
1764 * need to be tagged for update.
1765 */
1766 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
1768 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
1770
1771 /* Note that we only handle unselected neighbor vertices here, main loop will take care of
1772 * selected ones. */
1774 !BLI_BITMAP_TEST(done_verts, BM_elem_index_get(l->prev->v)))
1775 {
1776
1777 BMLoop *l_prev;
1778 BMIter liter_prev;
1779 BM_ITER_ELEM (l_prev, &liter_prev, l->prev->v, BM_LOOPS_OF_VERT) {
1781 }
1782 BLI_BITMAP_ENABLE(done_verts, BM_elem_index_get(l_prev->v));
1783 }
1784
1786 !BLI_BITMAP_TEST(done_verts, BM_elem_index_get(l->next->v)))
1787 {
1788
1789 BMLoop *l_next;
1790 BMIter liter_next;
1791 BM_ITER_ELEM (l_next, &liter_next, l->next->v, BM_LOOPS_OF_VERT) {
1793 }
1794 BLI_BITMAP_ENABLE(done_verts, BM_elem_index_get(l_next->v));
1795 }
1796 }
1797
1799 }
1800 }
1801
1802 MEM_delete(done_verts);
1804}
1805
1806void BM_lnorspace_rebuild(BMesh *bm, bool preserve_clnor)
1807{
1808 BLI_assert(bm->lnor_spacearr != nullptr);
1809
1811 return;
1812 }
1813 BMFace *f;
1814 BMLoop *l;
1815 BMIter fiter, liter;
1816
1817 Array<float3> r_lnors(bm->totloop, float3(0));
1818 Array<float3> oldnors(preserve_clnor ? bm->totloop : 0, float3(0));
1819
1820 int cd_loop_clnors_offset = CustomData_get_offset_named(
1821 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
1822
1824
1825 if (preserve_clnor) {
1826 BLI_assert(bm->lnor_spacearr->lspacearr != nullptr);
1827
1828 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1829 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1832 {
1833 short (*clnor)[2] = static_cast<short (*)[2]>(
1834 BM_ELEM_CD_GET_VOID_P(l, cd_loop_clnors_offset));
1835 int l_index = BM_elem_index_get(l);
1836
1838 bm->lnor_spacearr->lspacearr[l_index], *clnor, oldnors[l_index]);
1839 }
1840 }
1841 }
1842 }
1843
1846 }
1848 bm, {}, {}, {}, true, r_lnors, bm->lnor_spacearr, nullptr, cd_loop_clnors_offset, true);
1849
1850 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1851 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1854 {
1855 if (preserve_clnor) {
1856 short (*clnor)[2] = static_cast<short (*)[2]>(
1857 BM_ELEM_CD_GET_VOID_P(l, cd_loop_clnors_offset));
1858 int l_index = BM_elem_index_get(l);
1860 bm->lnor_spacearr->lspacearr[l_index], oldnors[l_index], *clnor);
1861 }
1863 }
1864 }
1865 }
1866
1868
1869#ifndef NDEBUG
1871#endif
1872}
1873
1880{
1881 /* Zero values tell the normals calculation code to use the automatic normals (rather than any
1882 * custom normal vector). */
1883 Array<float3> lnors(bm->totloop, float3(0));
1884 const int vert_free_offset = CustomData_get_offset_named(
1885 &bm->vdata, CD_PROP_FLOAT3, "custom_normal");
1886 const int edge_free_offset = CustomData_get_offset_named(
1887 &bm->edata, CD_PROP_FLOAT3, "custom_normal");
1888 const int face_free_offset = CustomData_get_offset_named(
1889 &bm->pdata, CD_PROP_FLOAT3, "custom_normal");
1890 const int loop_free_offset = CustomData_get_offset_named(
1891 &bm->ldata, CD_PROP_FLOAT3, "custom_normal");
1892 if (vert_free_offset != -1) {
1893 int loop_index = 0;
1894 BMFace *f;
1895 BMLoop *l;
1896 BMIter fiter, liter;
1897 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1898 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1899 lnors[loop_index++] = float3(BM_ELEM_CD_GET_FLOAT_P(l->v, vert_free_offset));
1900 }
1901 }
1902 BM_data_layer_free_named(bm, &bm->vdata, "custom_normal");
1903 }
1904 else if (edge_free_offset != -1) {
1905 BM_data_layer_free_named(bm, &bm->edata, "custom_normal");
1906 }
1907 else if (face_free_offset != -1) {
1908 int loop_index = 0;
1909 BMFace *f;
1910 BMLoop *l;
1911 BMIter fiter, liter;
1912 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1913 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1914 lnors[loop_index++] = float3(BM_ELEM_CD_GET_FLOAT_P(f, face_free_offset));
1915 }
1916 }
1917 BM_data_layer_free_named(bm, &bm->pdata, "custom_normal");
1918 }
1919 else if (loop_free_offset != -1) {
1920 int loop_index = 0;
1921 BMFace *f;
1922 BMLoop *l;
1923 BMIter fiter, liter;
1924 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
1925 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1926 lnors[loop_index++] = float3(BM_ELEM_CD_GET_FLOAT_P(l, loop_free_offset));
1927 }
1928 }
1929 BM_data_layer_free_named(bm, &bm->ldata, "custom_normal");
1930 }
1931 BM_lnorspacearr_store(bm, lnors);
1932}
1933
1935{
1936 if (bm->lnor_spacearr == nullptr) {
1938 }
1939 if (bm->lnor_spacearr->lspacearr == nullptr) {
1941 }
1943 BM_lnorspace_rebuild(bm, false);
1944 }
1945}
1946
1948
1949/* -------------------------------------------------------------------- */
1954
1960#ifndef NDEBUG
1962{
1964 bool clear = true;
1965
1967 temp->lspacearr = nullptr;
1968
1970
1971 int cd_loop_clnors_offset = CustomData_get_offset_named(
1972 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
1973 Array<float3> lnors(bm->totloop, float3(0));
1975 bm, {}, {}, {}, true, lnors, temp, nullptr, cd_loop_clnors_offset, true);
1976
1977 for (int i = 0; i < bm->totloop; i++) {
1978 /* Degenerate faces can produce non-finite normals which can't be usefully compared.
1979 * Skip them as the comparison is likely to fail and assert, see #160015. */
1980 if (!is_finite_v3(temp->lspacearr[i]->vec_lnor)) {
1981 continue;
1982 }
1983
1984 int j = 0;
1985 j += compare_ff(
1986 temp->lspacearr[i]->ref_alpha, bm->lnor_spacearr->lspacearr[i]->ref_alpha, 1e-4f);
1987 j += compare_ff(
1988 temp->lspacearr[i]->ref_beta, bm->lnor_spacearr->lspacearr[i]->ref_beta, 1e-4f);
1989 j += compare_v3v3(
1990 temp->lspacearr[i]->vec_lnor, bm->lnor_spacearr->lspacearr[i]->vec_lnor, 1e-4f);
1991 j += compare_v3v3(
1992 temp->lspacearr[i]->vec_ortho, bm->lnor_spacearr->lspacearr[i]->vec_ortho, 1e-4f);
1993 j += compare_v3v3(
1994 temp->lspacearr[i]->vec_ref, bm->lnor_spacearr->lspacearr[i]->vec_ref, 1e-4f);
1995
1996 if (j != 5) {
1997 clear = false;
1998 break;
1999 }
2000 }
2002 MEM_delete(temp);
2003 BLI_assert(clear);
2004
2006}
2007#endif
2008
2010 BLI_bitmap *loops,
2011 MLoopNorSpaceArray *lnor_spacearr,
2012 int *totloopsel,
2013 const bool do_all_loops_of_vert)
2014{
2015 if (l != nullptr) {
2016 const int l_idx = BM_elem_index_get(l);
2017
2018 if (!BLI_BITMAP_TEST(loops, l_idx)) {
2019 /* If vert and face selected share a loop, mark it for editing. */
2020 BLI_BITMAP_ENABLE(loops, l_idx);
2021 (*totloopsel)++;
2022
2023 if (do_all_loops_of_vert) {
2024 /* If required, also mark all loops shared by that vertex.
2025 * This is needed when loop spaces may change
2026 * (i.e. when some faces or edges might change of smooth/sharp status). */
2027 BMIter liter;
2028 BMLoop *lfan;
2029 BM_ITER_ELEM (lfan, &liter, l->v, BM_LOOPS_OF_VERT) {
2030 const int lfan_idx = BM_elem_index_get(lfan);
2031 if (!BLI_BITMAP_TEST(loops, lfan_idx)) {
2032 BLI_BITMAP_ENABLE(loops, lfan_idx);
2033 (*totloopsel)++;
2034 }
2035 }
2036 }
2037 else {
2038 /* Mark all loops in same loop normal space (aka smooth fan). */
2039 if ((lnor_spacearr->lspacearr[l_idx]->flags & MLNOR_SPACE_IS_SINGLE) == 0) {
2040 for (LinkNode *node = lnor_spacearr->lspacearr[l_idx]->loops; node; node = node->next) {
2041 const int lfan_idx = BM_elem_index_get((BMLoop *)node->link);
2042 if (!BLI_BITMAP_TEST(loops, lfan_idx)) {
2043 BLI_BITMAP_ENABLE(loops, lfan_idx);
2044 (*totloopsel)++;
2045 }
2046 }
2047 }
2048 }
2049 }
2050 }
2051}
2052
2054 BLI_bitmap *loops,
2055 const bool do_all_loops_of_vert,
2056 int *totloopsel_p)
2057{
2058 /* Select all loops of selected verts. */
2059 BMLoop *l;
2060 BMVert *v;
2061 BMIter liter, viter;
2062 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
2064 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
2066 l, loops, bm->lnor_spacearr, totloopsel_p, do_all_loops_of_vert);
2067 }
2068 }
2069 }
2070}
2071
2073 BLI_bitmap *loops,
2074 const bool do_all_loops_of_vert,
2075 int *totloopsel_p)
2076{
2077 /* Only select all loops of selected edges. */
2078 BMLoop *l;
2079 BMEdge *e;
2080 BMIter liter, eiter;
2081 BM_ITER_MESH (e, &eiter, bm, BM_EDGES_OF_MESH) {
2083 BM_ITER_ELEM (l, &liter, e, BM_LOOPS_OF_EDGE) {
2085 l, loops, bm->lnor_spacearr, totloopsel_p, do_all_loops_of_vert);
2086 /* Loops actually 'have' two edges, or said otherwise, a selected edge actually selects
2087 * *two* loops in each of its faces. We have to find the other one too. */
2088 if (BM_vert_in_edge(e, l->next->v)) {
2090 l->next, loops, bm->lnor_spacearr, totloopsel_p, do_all_loops_of_vert);
2091 }
2092 else {
2095 l->prev, loops, bm->lnor_spacearr, totloopsel_p, do_all_loops_of_vert);
2096 }
2097 }
2098 }
2099 }
2100}
2101
2103 BLI_bitmap *loops,
2104 const bool do_all_loops_of_vert,
2105 int *totloopsel_p)
2106{
2107 /* Only select all loops of selected faces. */
2108 BMLoop *l;
2109 BMFace *f;
2110 BMIter liter, fiter;
2111 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2113 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2115 l, loops, bm->lnor_spacearr, totloopsel_p, do_all_loops_of_vert);
2116 }
2117 }
2118 }
2119}
2120
2121static int bm_loop_normal_mark_verts(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
2122{
2124 BLI_assert(bm->lnor_spacearr != nullptr);
2126 int totloopsel = 0;
2127 bm_loop_normal_mark_verts_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2128 return totloopsel;
2129}
2130
2131static int bm_loop_normal_mark_edges(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
2132{
2134 BLI_assert(bm->lnor_spacearr != nullptr);
2136 int totloopsel = 0;
2137 bm_loop_normal_mark_edges_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2138 return totloopsel;
2139}
2140
2141static int bm_loop_normal_mark_faces(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
2142{
2144 BLI_assert(bm->lnor_spacearr != nullptr);
2146 int totloopsel = 0;
2147 bm_loop_normal_mark_faces_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2148 return totloopsel;
2149}
2150
2151/* Mark the individual clnors to be edited, if multiple selection methods are used. */
2152static int bm_loop_normal_mark_indiv(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
2153{
2154 int totloopsel = 0;
2155
2156 const bool sel_verts = (bm->selectmode & SCE_SELECT_VERTEX) != 0;
2157 const bool sel_edges = (bm->selectmode & SCE_SELECT_EDGE) != 0;
2158 const bool sel_faces = (bm->selectmode & SCE_SELECT_FACE) != 0;
2159 const bool use_sel_face_history = sel_faces && (sel_edges || sel_verts);
2160
2162
2163 BLI_assert(bm->lnor_spacearr != nullptr);
2165
2166 if (use_sel_face_history) {
2167 /* Using face history allows to select a single loop from a single face...
2168 * Note that this is O(n^2) piece of code,
2169 * but it is not designed to be used with huge selection sets,
2170 * rather with only a few items selected at most. */
2171 /* Goes from last selected to the first selected element. */
2172 for (BMEditSelection &ese : bm->selected.items_reversed()) {
2173 if (ese.htype == BM_FACE) {
2174 /* If current face is selected,
2175 * then any verts to be edited must have been selected before it. */
2176 for (BMEditSelection *ese_prev = ese.prev; ese_prev; ese_prev = ese_prev->prev) {
2177 if (ese_prev->htype == BM_VERT) {
2179 BM_face_vert_share_loop(reinterpret_cast<BMFace *>(ese.ele),
2180 reinterpret_cast<BMVert *>(ese_prev->ele)),
2181 loops,
2183 &totloopsel,
2184 do_all_loops_of_vert);
2185 }
2186 else if (ese_prev->htype == BM_EDGE) {
2187 BMEdge *e = reinterpret_cast<BMEdge *>(ese_prev->ele);
2189 BM_face_vert_share_loop(reinterpret_cast<BMFace *>(ese.ele), e->v1),
2190 loops,
2192 &totloopsel,
2193 do_all_loops_of_vert);
2194
2196 BM_face_vert_share_loop(reinterpret_cast<BMFace *>(ese.ele), e->v2),
2197 loops,
2199 &totloopsel,
2200 do_all_loops_of_vert);
2201 }
2202 }
2203 }
2204 }
2205 }
2206
2207 /* If the selection history could not be used, fall back to regular selection. */
2208 if (totloopsel == 0) {
2209 if (sel_faces) {
2210 bm_loop_normal_mark_faces_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2211 }
2212 if (sel_edges) {
2213 bm_loop_normal_mark_edges_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2214 }
2215 if (sel_verts) {
2216 bm_loop_normal_mark_verts_impl(bm, loops, do_all_loops_of_vert, &totloopsel);
2217 }
2218 }
2219
2220 return totloopsel;
2221}
2222
2224 BMesh *bm, BMLoopNorEditData *lnor_ed, BMVert *v, BMLoop *l, const int offset)
2225{
2226 BLI_assert(bm->lnor_spacearr != nullptr);
2227 BLI_assert(bm->lnor_spacearr->lspacearr != nullptr);
2228
2229 const int l_index = BM_elem_index_get(l);
2230 short *clnors_data = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, offset));
2231
2232 lnor_ed->loop_index = l_index;
2233 lnor_ed->loop = l;
2234
2235 float custom_normal[3];
2237 bm->lnor_spacearr->lspacearr[l_index], clnors_data, custom_normal);
2238
2239 lnor_ed->clnors_data = clnors_data;
2240 copy_v3_v3(lnor_ed->nloc, custom_normal);
2241 copy_v3_v3(lnor_ed->niloc, custom_normal);
2242
2243 lnor_ed->loc = v->co;
2244}
2245
2247 BMesh *bm, const bool do_all_loops_of_vert, const char htype_override)
2248{
2249 BMLoop *l;
2250 BMVert *v;
2251 BMIter liter, viter;
2252
2253 int totloopsel = 0;
2254
2256
2259 __func__);
2260
2262 const int cd_custom_normal_offset = CustomData_get_offset_named(
2263 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
2264
2266
2267 BLI_bitmap *loops = BLI_BITMAP_NEW(bm->totloop, __func__);
2268
2269 /* This function define loop normals to edit, based on selection modes and history. */
2270 if (htype_override != 0) {
2271 BLI_assert(ELEM(htype_override, BM_VERT, BM_EDGE, BM_FACE));
2272 switch (htype_override) {
2273 case BM_VERT: {
2274 totloopsel = bm_loop_normal_mark_verts(bm, loops, do_all_loops_of_vert);
2275 break;
2276 }
2277 case BM_EDGE: {
2278 totloopsel = bm_loop_normal_mark_edges(bm, loops, do_all_loops_of_vert);
2279 break;
2280 }
2281 case BM_FACE: {
2282 totloopsel = bm_loop_normal_mark_faces(bm, loops, do_all_loops_of_vert);
2283 break;
2284 }
2285 }
2286 }
2287 else {
2288 totloopsel = bm_loop_normal_mark_indiv(bm, loops, do_all_loops_of_vert);
2289 }
2290
2291 if (totloopsel) {
2292 BMLoopNorEditData *lnor_ed = lnors_ed_arr->lnor_editdata =
2294
2295 BM_ITER_MESH (v, &viter, bm, BM_VERTS_OF_MESH) {
2296 BM_ITER_ELEM (l, &liter, v, BM_LOOPS_OF_VERT) {
2297 if (BLI_BITMAP_TEST(loops, BM_elem_index_get(l))) {
2298 loop_normal_editdata_init(bm, lnor_ed, v, l, cd_custom_normal_offset);
2299 lnors_ed_arr->lidx_to_lnor_editdata[BM_elem_index_get(l)] = lnor_ed;
2300 lnor_ed++;
2301 }
2302 }
2303 }
2304 lnors_ed_arr->totloop = totloopsel;
2305 }
2306
2307 MEM_delete(loops);
2308 lnors_ed_arr->cd_custom_normal_offset = cd_custom_normal_offset;
2309 return lnors_ed_arr;
2310}
2311
2313 const bool do_all_loops_of_vert)
2314{
2315 return BM_loop_normal_editdata_array_init_with_htype(bm, do_all_loops_of_vert, 0);
2316}
2317
2319{
2320 MEM_SAFE_DELETE(lnors_ed_arr->lnor_editdata);
2321 MEM_SAFE_DELETE(lnors_ed_arr->lidx_to_lnor_editdata);
2322 MEM_delete(lnors_ed_arr);
2323}
2324
2326
2327/* -------------------------------------------------------------------- */
2330
2332{
2333 BMFace *f;
2334 BMLoop *l;
2335 BMIter liter, fiter;
2336
2337 if (!CustomData_has_layer_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal")) {
2338 return false;
2339 }
2340
2342
2343 /* Create a loop normal layer. */
2346
2348 }
2349
2350 const int cd_custom_normal_offset = CustomData_get_offset_named(
2351 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
2352 const int cd_normal_offset = CustomData_get_offset(&bm->ldata, CD_NORMAL);
2353
2354 int l_index = 0;
2355 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2356 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2357 const short *clnors_data = static_cast<const short *>(
2358 BM_ELEM_CD_GET_VOID_P(l, cd_custom_normal_offset));
2359 float *normal = static_cast<float *>(BM_ELEM_CD_GET_VOID_P(l, cd_normal_offset));
2360
2362 bm->lnor_spacearr->lspacearr[l_index], clnors_data, normal);
2363 l_index += 1;
2364 }
2365 }
2366
2367 return true;
2368}
2369
2371{
2372 const int cd_custom_normal_offset = CustomData_get_offset_named(
2373 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
2374 if (cd_custom_normal_offset == -1) {
2375 return;
2376 }
2377 const int cd_normal_offset = CustomData_get_offset(&bm->ldata, CD_NORMAL);
2378 if (cd_normal_offset == -1) {
2379 return;
2380 }
2381
2382 if (bm->lnor_spacearr == nullptr) {
2384 }
2385
2387 {},
2388 {},
2390 nullptr,
2391 cd_custom_normal_offset,
2392 nullptr,
2393 cd_normal_offset,
2394 add_sharp_edges);
2395
2397}
2398
2400
2401} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition BLI_bitmap.hh:39
#define BLI_BITMAP_TEST(_bitmap, _index)
Definition BLI_bitmap.hh:63
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition BLI_bitmap.hh:80
#define M_PI
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define ELEM(...)
#define BM_DISK_EDGE_NEXT(e, v)
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_THREAD_LIMIT
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#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)
#define CLEAR_SPACEARRAY_THRESHOLD(x)
#define BM_LNORSPACE_UPDATE
#define EDGE_TAG_FROM_SPLIT_ANGLE_BYPASS
#define BM_ELEM_API_FLAG_DISABLE(element, f)
#define BM_ELEM_API_FLAG_TEST(element, f)
#define BM_ELEM_API_FLAG_ENABLE(element, f)
constexpr bool is_empty() const
Definition BLI_span.hh:515
constexpr bool is_empty() const
Definition BLI_span.hh:264
void append(const T &value)
bool is_empty() const
#define isnan
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define UINT_MAX
Definition hash_md5.cc:46
#define LNOR_SPACE_TRIGO_THRESHOLD
float safe_acos_approx(float x)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
static void bm_vert_calc_normals_impl(BMVert *v)
void BM_lnorspace_update(BMesh *bm)
bool BM_data_layer_free_named(BMesh *bm, CustomData *data, StringRef name)
int CustomData_get_offset(const CustomData *data, eCustomDataType type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void bm_mesh_loops_calc_normals_for_vert_free_fn(const void *__restrict userdata, void *__restrict chunk)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
void BM_data_layer_add(BMesh *bm, CustomData *data, int type)
MINLINE bool is_finite_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
@ BM_SPACEARR_DIRTY_ALL
@ BM_SPACEARR_DIRTY
LinkNode * BLI_linklist_sort(LinkNode *list, int(*cmp)(const void *, const void *)) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
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])
static int bm_loop_normal_mark_faces(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
static void bm_mesh_loops_calc_normals__single_threaded(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos, MLoopNorSpaceArray *r_lnors_spacearr, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild, const float split_angle_cos)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void bm_mesh_loops_calc_normals_for_vert_with_clnors(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild, const float split_angle_cos, MLoopNorSpaceArray *r_lnors_spacearr, Vector< float3, 16 > *edge_vectors, BMVert *v)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static void bm_lnorspace_ensure_from_free_normals(BMesh *bm)
static void bm_mesh_loops_calc_normals_for_vert_without_clnors_fn(void *userdata, MempoolIterData *mp_v, const TaskParallelTLS *__restrict tls)
void BKE_lnor_spacearr_tls_join(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
static int bm_loop_index_cmp(const void *a, const void *b)
static void bm_edge_tag_from_smooth(Span< float3 > fnos, BMEdge *e, const float split_angle_cos)
BMLoop * BM_vert_step_fan_loop(BMLoop *l, BMEdge **e_step)
void BM_custom_loop_normals_from_vector_layer(BMesh *bm, bool add_sharp_edges)
static void bm_mesh_loops_calc_normals__multi_threaded(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos, MLoopNorSpaceArray *r_lnors_spacearr, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild, const float split_angle_cos)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void BM_loops_calc_normal_vcos(BMesh *bm, const Span< float3 > vcos, const Span< float3 > vnos, const Span< float3 > fnos, const bool use_split_normals, MutableSpan< float3 > r_lnos, 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.
static int bm_mesh_loops_calc_normals_for_loop(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool has_clnors, Vector< float3, 16 > *edge_vectors, BMLoop *l_curr, MutableSpan< float3 > r_lnos, MLoopNorSpaceArray *r_lnors_spacearr)
void BM_edges_sharp_from_angle_set(BMesh *bm, const float split_angle)
void BM_lnorspacearr_store(BMesh *bm, MutableSpan< float3 > r_lnors)
void BM_mesh_normals_update_ex(BMesh *bm, const BMeshNormalsUpdate_Params *params)
BMesh Compute Normals.
BLI_INLINE bool BM_vert_in_edge(const BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void mul_v3_fl(float r[3], float f)
void BKE_lnor_spacearr_tls_init(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
BLI_INLINE void BLI_parallel_mempool_settings_defaults(TaskParallelSettings *settings)
void BM_lnorspace_err(BMesh *bm)
BLI_INLINE bool bm_edge_is_smooth_no_angle_test(const BMEdge *e, const BMLoop *l_a, const BMLoop *l_b)
Global G
Definition blender.cc:57
bool BM_loop_check_cyclic_smooth_fan(BMLoop *l_curr)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
static void bm_mesh_loops_custom_normals_set(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, 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)
static void bm_mesh_loops_calc_normals_for_vert_with_clnors_fn(void *userdata, MempoolIterData *mp_v, const TaskParallelTLS *__restrict tls)
static int bm_loop_normal_mark_edges(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_lnor_space_define(MLoopNorSpace *lnor_space, const float lnor[3], const float vec_ref[3], const float vec_other[3], Span< float3 > edge_vectors)
void BKE_lnor_spacearr_init(MLoopNorSpaceArray *lnors_spacearr, int numLoops, char data_type)
BMesh const char void * data
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)
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr)
void CustomData_set_layer_flag(CustomData *data, eCustomDataType type, eCustomDataLayer_Flag flag)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
void BM_loop_normal_editdata_array_free(BMLoopNorEditDataArray *lnors_ed_arr)
void BM_normals_loops_edges_tag(BMesh *bm, const bool do_edges)
static void bm_loop_normal_mark_edges_impl(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert, int *totloopsel_p)
BLI_INLINE void bm_vert_calc_normals_accum_loop(const BMLoop *l_iter, const float e1diff[3], const float e2diff[3], const float f_no[3], float v_no[3])
void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
static void bm_mesh_loops_calc_normals_no_autosmooth(BMesh *bm, const Span< float3 > vnos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos)
static void bm_mesh_edges_sharp_tag(BMesh *bm, const Span< float3 > fnos, float split_angle_cos, const bool do_sharp_edges_tag)
static void bm_mesh_loops_calc_normals(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos, MLoopNorSpaceArray *r_lnors_spacearr, const short(*clnors_data)[2], const int cd_loop_clnors_offset, const bool do_rebuild, const float split_angle_cos)
void BKE_lnor_space_add_loop(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpace *lnor_space, int corner, void *bm_loop, bool is_single)
MINLINE void zero_v3(float r[3])
static void bm_mesh_loops_calc_normals_for_vert_init_fn(const void *__restrict userdata, void *__restrict chunk)
static void bm_face_calc_normals_cb(void *, MempoolIterData *mp_f, const TaskParallelTLS *__restrict)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BKE_lnor_spacearr_free(MLoopNorSpaceArray *lnors_spacearr)
@ MLNOR_SPACE_IS_SINGLE
Definition BKE_mesh.h:288
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
static int bm_loop_normal_mark_verts(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void bm_vert_calc_normals_with_coords(BMVert *v, BMVertsCalcNormalsWithCoordsData *data)
static void bm_mesh_loops_calc_normals_for_vert_without_clnors(BMesh *bm, const Span< float3 > vcos, const Span< float3 > fnos, MutableSpan< float3 > r_lnos, const bool do_rebuild, const float split_angle_cos, MLoopNorSpaceArray *r_lnors_spacearr, Vector< float3, 16 > *edge_vectors, BMVert *v)
@ MLNOR_SPACEARR_BMLOOP_PTR
Definition BKE_mesh.h:310
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
void BM_data_layer_ensure_named(BMesh *bm, CustomData *data, int type, const StringRef name)
static void bm_vert_calc_normals_with_coords_cb(void *userdata, MempoolIterData *mp_v, const TaskParallelTLS *__restrict)
unsigned int BLI_bitmap
Definition BLI_bitmap.hh:15
static void bm_mesh_verts_calc_normals(BMesh *bm, const Span< float3 > fnos, const Span< float3 > vcos, MutableSpan< float3 > vnos)
static int bm_loop_normal_mark_indiv(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert)
float BM_face_calc_normal(const BMFace *f, float r_no[3])
BMESH UPDATE FACE NORMAL.
BMLoopNorEditDataArray * BM_loop_normal_editdata_array_init(BMesh *bm, const bool do_all_loops_of_vert)
static void bm_loop_normal_mark_verts_impl(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert, int *totloopsel_p)
BMLoopNorEditDataArray * BM_loop_normal_editdata_array_init_with_htype(BMesh *bm, const bool do_all_loops_of_vert, const char htype_override)
void BKE_lnor_space_custom_data_to_normal(const MLoopNorSpace *lnor_space, const short clnor_data[2], float r_custom_lnor[3])
static void bm_mesh_loops_calc_normals_for_vert_reduce_fn(const void *__restrict userdata, void *__restrict, void *__restrict chunk)
static void bm_loop_normal_mark_faces_impl(BMesh *bm, BLI_bitmap *loops, const bool do_all_loops_of_vert, int *totloopsel_p)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
static bool bm_mesh_loops_split_lnor_fans(BMesh *bm, MLoopNorSpaceArray *lnors_spacearr, const float(*new_lnors)[3])
VecBase< float, 3 > float3
void BM_mesh_normals_update_with_partial(BMesh *bm, const BMPartialUpdate *bmpinfo)
bool CustomData_has_layer_named(const CustomData *data, eCustomDataType type, StringRef name)
void BM_lnorspace_rebuild(BMesh *bm, bool preserve_clnor)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bool BM_custom_loop_normals_to_vector_layer(BMesh *bm)
static void bm_edge_tag_from_smooth_and_set_sharp(Span< float3 > fnos, BMEdge *e, const float split_angle_cos)
static void loop_normal_editdata_init(BMesh *bm, BMLoopNorEditData *lnor_ed, BMVert *v, BMLoop *l, const int offset)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void BM_mesh_normals_update_with_partial_ex(BMesh *, const BMPartialUpdate *bmpinfo, const BMeshNormalsUpdate_Params *params)
static void bm_vert_calc_normals_cb(void *, MempoolIterData *mp_v, const TaskParallelTLS *__restrict)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
MINLINE int compare_ff(float a, float b, float max_diff)
void BM_iter_parallel(BMesh *bm, const char itype, TaskParallelMempoolFunc func, void *userdata, const TaskParallelSettings *settings)
Parallel (threaded) iterator, only available for most basic iteration-types (verts/edges/faces of mes...
void BM_mesh_normals_update(BMesh *bm)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void BM_lnorspace_invalidate(BMesh *bm, const bool do_invalidate_all)
MLoopNorSpace * BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr)
void BM_verts_calc_normal_vcos(BMesh *bm, const Span< float3 > fnos, const Span< float3 > vcos, MutableSpan< float3 > vnos)
BMesh Compute Normals from/to external data.
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * l
struct BMEditSelection * prev
BMLoopNorEditData * lnor_editdata
BMLoopNorEditData ** lidx_to_lnor_editdata
struct BMLoop * next
struct BMLoop * prev
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
struct BMEdge * e
CustomData pdata
ListBaseT< BMEditSelection > selected
CustomData vdata
struct MLoopNorSpaceArray * lnor_spacearr
CustomData edata
CustomData ldata
struct LinkNode * next
MLoopNorSpace ** lspacearr
Definition BKE_mesh.h:296
struct LinkNode * loops
Definition BKE_mesh.h:281
TaskParallelReduceFunc func_reduce
TaskParallelInitFunc func_init
TaskParallelFreeFunc func_free