Blender V5.3
mesh_normals.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <climits>
14
15#include "BLI_math_geom_c.hh"
16#include "BLI_math_vector_c.hh"
17
18#include "BLI_array_utils.hh"
19#include "BLI_bit_vector.hh"
21#include "BLI_linklist.hh"
22#include "BLI_math_base.hh"
23#include "BLI_math_vector.hh"
24#include "BLI_memarena.hh"
25#include "BLI_span.hh"
26#include "BLI_task.hh"
27#include "BLI_utildefines.hh"
28
29#include "BKE_attribute.hh"
30#include "BKE_global.hh"
31#include "BKE_mesh.hh"
32#include "BKE_mesh_mapping.hh"
33
34namespace blender {
35
36// #define DEBUG_TIME
37
38#ifdef DEBUG_TIME
39# include "BLI_timeit.hh"
40#endif
41
42/* -------------------------------------------------------------------- */
47
48namespace bke {
49
51{
52 mesh.runtime->vert_normals_true_cache.ensure(
53 [&](Vector<float3> &r_data) { r_data = vert_normals; });
54}
55
57{
58 mesh.runtime->vert_normals_true_cache.ensure(
59 [&](Vector<float3> &r_data) { r_data = std::move(vert_normals); });
60}
61
63{
64 if (auto *vector = std::get_if<Vector<float3>>(&this->data)) {
65 vector->resize(size);
66 }
67 else {
68 this->data = Vector<float3>(size);
69 }
70 return std::get<Vector<float3>>(this->data).as_mutable_span();
71}
72
74{
75 if (const auto *vector = std::get_if<Vector<float3>>(&this->data)) {
76 return vector->as_span();
77 }
78 return std::get<Span<float3>>(this->data);
79}
80
82{
83 if (data.is_span()) {
84 this->data = data.get_internal_span();
85 }
86 else {
87 data.materialize(this->ensure_vector_size(data.size()));
88 }
89}
90
92{
93 this->data = std::move(data);
94}
95
96} // namespace bke
97
99{
100 return mesh->runtime->vert_normals_cache.is_dirty();
101}
102
104{
105 return mesh->runtime->face_normals_cache.is_dirty();
106}
107
109
110namespace bke::mesh {
111
112/* -------------------------------------------------------------------- */
115
116/*
117 * COMPUTE POLY NORMAL
118 *
119 * Computes the normal of a planar
120 * face See Graphics Gems for
121 * computing newell normal.
122 */
123static float3 normal_calc_ngon(const Span<float3> vert_positions, const Span<int> face_verts)
124{
125 float3 normal(0);
126
127 /* Newell's Method */
128 const float *v_prev = vert_positions[face_verts.last()];
129 for (const int i : face_verts.index_range()) {
130 const float *v_curr = vert_positions[face_verts[i]];
131 add_newell_cross_v3_v3v3(normal, v_prev, v_curr);
132 v_prev = v_curr;
133 }
134
135 if (normalize_v3(normal) == 0.0f) [[unlikely]] {
136 /* Other axis are already set to zero. */
137 normal[2] = 1.0f;
138 }
139
140 return normal;
141}
142
143float3 face_normal_calc(const Span<float3> vert_positions, const Span<int> face_verts)
144{
145 float3 normal;
146 if (face_verts.size() == 4) {
147 normal_quad_v3(normal,
148 vert_positions[face_verts[0]],
149 vert_positions[face_verts[1]],
150 vert_positions[face_verts[2]],
151 vert_positions[face_verts[3]]);
152 }
153 else if (face_verts.size() == 3) {
154 normal = math::normal_tri(vert_positions[face_verts[0]],
155 vert_positions[face_verts[1]],
156 vert_positions[face_verts[2]]);
157 }
158 else {
159 BLI_assert(face_verts.size() > 4);
160 normal = normal_calc_ngon(vert_positions, face_verts);
161 }
162
163 if (math::is_zero(normal)) [[unlikely]] {
164 normal.z = 1.0f;
165 }
166
167 BLI_ASSERT_UNIT_V3(normal);
168 return normal;
169}
170
172
173/* -------------------------------------------------------------------- */
179
180void normals_calc_faces(const Span<float3> positions,
181 const OffsetIndices<int> faces,
182 const Span<int> corner_verts,
183 MutableSpan<float3> face_normals)
184{
186 BLI_assert(faces.size() == face_normals.size());
187 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
188 for (const int i : range) {
189 face_normals[i] = normal_calc_ngon(positions, corner_verts.slice(faces[i]));
190 }
191 });
192}
193
194void normals_calc_verts(const Span<float3> vert_positions,
195 const OffsetIndices<int> faces,
196 const Span<int> corner_verts,
197 const GroupedSpan<int> vert_to_face_map,
198 const Span<float3> face_normals,
199 MutableSpan<float3> vert_normals)
200{
202 const Span<float3> positions = vert_positions;
203 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
204 for (const int vert : range) {
205 const Span<int> vert_faces = vert_to_face_map[vert];
206 if (vert_faces.is_empty()) {
207 vert_normals[vert] = math::normalize(positions[vert]);
208 continue;
209 }
210
211 float3 vert_normal(0);
212 for (const int face : vert_faces) {
213 const int2 adjacent_verts = face_find_adjacent_verts(faces[face], corner_verts, vert);
214 const float3 dir_prev = math::normalize(positions[adjacent_verts[0]] - positions[vert]);
215 const float3 dir_next = math::normalize(positions[adjacent_verts[1]] - positions[vert]);
216 const float factor = math::safe_acos_approx(math::dot(dir_prev, dir_next));
217
218 vert_normal += face_normals[face] * factor;
219 }
220
221 vert_normals[vert] = math::normalize(vert_normal);
222 }
223 });
224}
225
227
228static void mix_normals_corner_to_vert(const Span<float3> vert_positions,
229 const OffsetIndices<int> faces,
230 const Span<int> corner_verts,
231 const GroupedSpan<int> vert_to_face_map,
232 const Span<float3> corner_normals,
233 MutableSpan<float3> vert_normals)
234{
236 const Span<float3> positions = vert_positions;
237 threading::parallel_for(positions.index_range(), 1024, [&](const IndexRange range) {
238 for (const int vert : range) {
239 const Span<int> vert_faces = vert_to_face_map[vert];
240 if (vert_faces.is_empty()) {
241 vert_normals[vert] = math::normalize(positions[vert]);
242 continue;
243 }
244
245 float3 vert_normal(0);
246 for (const int face : vert_faces) {
247 const int corner = mesh::face_find_corner_from_vert(faces[face], corner_verts, vert);
248 const int2 adjacent_verts{corner_verts[mesh::face_corner_prev(faces[face], corner)],
249 corner_verts[mesh::face_corner_next(faces[face], corner)]};
250
251 const float3 dir_prev = math::normalize(positions[adjacent_verts[0]] - positions[vert]);
252 const float3 dir_next = math::normalize(positions[adjacent_verts[1]] - positions[vert]);
253 const float factor = math::safe_acos_approx(math::dot(dir_prev, dir_next));
254
255 vert_normal += corner_normals[corner] * factor;
256 }
257
258 vert_normals[vert] = math::normalize(vert_normal);
259 }
260 });
261}
262
264 const Span<int> corner_verts,
265 const Span<float3> vert_normals,
266 MutableSpan<float3> face_normals)
267{
269 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
270 for (const int face : range) {
271 float3 sum(0);
272 for (const int vert : corner_verts.slice(faces[face])) {
273 sum += vert_normals[vert];
274 }
275 face_normals[face] = math::normalize(sum);
276 }
277 });
278}
279
281 const Span<float3> corner_normals,
282 MutableSpan<float3> face_normals)
283{
285 threading::parallel_for(faces.index_range(), 1024, [&](const IndexRange range) {
286 for (const int face : range) {
287 const Span<float3> face_corner_normals = corner_normals.slice(faces[face]);
288 const float3 sum = std::accumulate(
289 face_corner_normals.begin(), face_corner_normals.end(), float3(0));
290 face_normals[face] = math::normalize(sum);
291 }
292 });
293}
294
295} // namespace bke::mesh
296
297/* -------------------------------------------------------------------- */
300
301bke::MeshNormalDomain Mesh::normals_domain() const
302{
303 using namespace blender::bke;
304 if (this->faces_num == 0) {
306 }
307
308 const bke::AttributeAccessor attributes = this->attributes();
309 if (const std::optional<AttributeMetaData> custom = attributes.lookup_meta_data("custom_normal"))
310 {
311 switch (custom->domain) {
312 case AttrDomain::Point:
313 return MeshNormalDomain::Point;
314 case AttrDomain::Edge:
315 break;
316 case AttrDomain::Face:
317 return MeshNormalDomain::Face;
318 case AttrDomain::Corner:
319 return MeshNormalDomain::Corner;
320 default:
322 }
323 }
324
325 const VArray<bool> sharp_faces = *attributes.lookup_or_default<bool>(
326 "sharp_face", AttrDomain::Face, false);
327
328 const array_utils::BooleanMix face_mix = array_utils::booleans_mix_calc(sharp_faces);
329 if (face_mix == array_utils::BooleanMix::AllTrue) {
330 return MeshNormalDomain::Face;
331 }
332
333 const VArray<bool> sharp_edges = *attributes.lookup_or_default<bool>(
334 "sharp_edge", AttrDomain::Edge, false);
335 const array_utils::BooleanMix edge_mix = array_utils::booleans_mix_calc(sharp_edges);
336 if (edge_mix == array_utils::BooleanMix::AllTrue) {
337 return MeshNormalDomain::Face;
338 }
339
340 if (edge_mix == array_utils::BooleanMix::AllFalse &&
341 face_mix == array_utils::BooleanMix::AllFalse)
342 {
343 return MeshNormalDomain::Point;
344 }
345
346 return MeshNormalDomain::Corner;
347}
348
349Span<float3> Mesh::vert_normals() const
350{
351 using namespace blender::bke;
352 this->runtime->vert_normals_cache.ensure([&](NormalsCache &r_data) {
353 if (const GAttributeReader custom = this->attributes().lookup("custom_normal")) {
354 if (custom.varray.type().is<float3>()) {
355 if (custom.domain == AttrDomain::Point) {
356 r_data.store_varray(custom.varray.typed<float3>());
357 return;
358 }
359 if (custom.domain == AttrDomain::Face) {
360 mesh::normals_calc_verts(this->vert_positions(),
361 this->faces(),
362 this->corner_verts(),
363 this->vert_to_face_map(),
364 VArraySpan<float3>(custom.varray.typed<float3>()),
365 r_data.ensure_vector_size(this->verts_num));
366
367 return;
368 }
369 if (custom.domain == AttrDomain::Corner) {
370 mesh::mix_normals_corner_to_vert(this->vert_positions(),
371 this->faces(),
372 this->corner_verts(),
373 this->vert_to_face_map(),
374 VArraySpan<float3>(custom.varray.typed<float3>()),
375 r_data.ensure_vector_size(this->verts_num));
376 return;
377 }
378 }
379 else if (custom.varray.type().is<short2>() && custom.domain == AttrDomain::Corner) {
380 mesh::mix_normals_corner_to_vert(this->vert_positions(),
381 this->faces(),
382 this->corner_verts(),
383 this->vert_to_face_map(),
384 this->corner_normals(),
385 r_data.ensure_vector_size(this->verts_num));
386 return;
387 }
388 }
389 r_data.data = NormalsCache::UseTrueCache();
390 });
391 if (std::holds_alternative<NormalsCache::UseTrueCache>(
392 this->runtime->vert_normals_cache.data().data))
393 {
394 return this->vert_normals_true();
395 }
396
397 return this->runtime->vert_normals_cache.data().get_span();
398}
399
400Span<float3> Mesh::vert_normals_true() const
401{
402 using namespace blender::bke;
403 this->runtime->vert_normals_true_cache.ensure([&](Vector<float3> &r_data) {
404 r_data.reinitialize(this->verts_num);
405 mesh::normals_calc_verts(this->vert_positions(),
406 this->faces(),
407 this->corner_verts(),
408 this->vert_to_face_map(),
409 this->face_normals_true(),
410 r_data);
411 });
412 return this->runtime->vert_normals_true_cache.data();
413}
414
415Span<float3> Mesh::face_normals() const
416{
417 using namespace blender::bke;
418 if (this->faces_num == 0) {
419 return {};
420 }
421 this->runtime->face_normals_cache.ensure([&](NormalsCache &r_data) {
422 if (const GAttributeReader custom = this->attributes().lookup("custom_normal")) {
423 if (custom.varray.type().is<float3>()) {
424 if (custom.domain == AttrDomain::Face) {
425 r_data.store_varray(custom.varray.typed<float3>());
426 return;
427 }
428 if (custom.domain == AttrDomain::Point) {
429 mesh::mix_normals_vert_to_face(this->faces(),
430 this->corner_verts(),
431 VArraySpan<float3>(custom.varray.typed<float3>()),
432 r_data.ensure_vector_size(this->faces_num));
433 return;
434 }
435 if (custom.domain == AttrDomain::Corner) {
436 mesh::mix_normals_corner_to_face(this->faces(),
437 VArraySpan<float3>(custom.varray.typed<float3>()),
438 r_data.ensure_vector_size(this->faces_num));
439 return;
440 }
441 }
442 else if (custom.varray.type().is<short2>() && custom.domain == AttrDomain::Corner) {
443 mesh::mix_normals_corner_to_face(
444 this->faces(), this->corner_normals(), r_data.ensure_vector_size(this->faces_num));
445 return;
446 }
447 }
448 r_data.data = NormalsCache::UseTrueCache();
449 });
450 if (std::holds_alternative<NormalsCache::UseTrueCache>(
451 this->runtime->face_normals_cache.data().data))
452 {
453 return this->face_normals_true();
454 }
455 return this->runtime->face_normals_cache.data().get_span();
456}
457
458Span<float3> Mesh::face_normals_true() const
459{
460 using namespace blender::bke;
461 this->runtime->face_normals_true_cache.ensure([&](Vector<float3> &r_data) {
462 r_data.reinitialize(this->faces_num);
463 mesh::normals_calc_faces(this->vert_positions(), this->faces(), this->corner_verts(), r_data);
464 });
465 return this->runtime->face_normals_true_cache.data();
466}
467
468Span<float3> Mesh::corner_normals() const
469{
470 using namespace blender::bke;
471 if (this->faces_num == 0) {
472 return {};
473 }
474 this->runtime->corner_normals_cache.ensure([&](NormalsCache &r_data) {
475 const OffsetIndices<int> faces = this->faces();
476 switch (this->normals_domain()) {
477 case MeshNormalDomain::Point: {
478 MutableSpan<float3> data = r_data.ensure_vector_size(this->corners_num);
479 array_utils::gather(this->vert_normals(), this->corner_verts(), data);
480 break;
481 }
482 case MeshNormalDomain::Face: {
483 MutableSpan<float3> data = r_data.ensure_vector_size(this->corners_num);
484 const Span<float3> face_normals = this->face_normals();
485 array_utils::gather_to_groups(faces, faces.index_range(), face_normals, data);
486 break;
487 }
488 case MeshNormalDomain::Corner: {
489 const AttributeAccessor attributes = this->attributes();
490 const GAttributeReader custom = attributes.lookup("custom_normal");
491 if (custom && custom.varray.type().is<float3>()) {
492 if (custom.domain == bke::AttrDomain::Corner) {
493 r_data.store_varray(custom.varray.typed<float3>());
494 }
495 return;
496 }
497 MutableSpan<float3> data = r_data.ensure_vector_size(this->corners_num);
498 const VArraySpan sharp_edges = *attributes.lookup<bool>("sharp_edge", AttrDomain::Edge);
499 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
500 mesh::normals_calc_corners(this->vert_positions(),
501 this->faces(),
502 this->corner_verts(),
503 this->corner_edges(),
504 this->vert_to_face_map(),
505 this->face_normals_true(),
506 sharp_edges,
507 sharp_faces,
508 VArraySpan<short2>(custom.varray.typed<short2>()),
509 nullptr,
510 data);
511 }
512 }
513 });
514 return this->runtime->corner_normals_cache.data().get_span();
515}
516
518 const int numLoops,
519 const char data_type)
520{
521 if (!(lnors_spacearr->lspacearr && lnors_spacearr->loops_pool)) {
522 MemArena *mem;
523
524 if (!lnors_spacearr->mem) {
525 lnors_spacearr->mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
526 }
527 mem = lnors_spacearr->mem;
528 if (numLoops > 0) {
529 lnors_spacearr->lspacearr = static_cast<MLoopNorSpace **>(
530 BLI_memarena_calloc(mem, sizeof(MLoopNorSpace *) * size_t(numLoops)));
531 lnors_spacearr->loops_pool = static_cast<LinkNode *>(
532 BLI_memarena_alloc(mem, sizeof(LinkNode) * size_t(numLoops)));
533 }
534 else {
535 lnors_spacearr->lspacearr = nullptr;
536 lnors_spacearr->loops_pool = nullptr;
537 }
538
539 lnors_spacearr->spaces_num = 0;
540 }
542 lnors_spacearr->data_type = data_type;
543}
544
546 MLoopNorSpaceArray *lnors_spacearr_tls)
547{
548 *lnors_spacearr_tls = *lnors_spacearr;
549 lnors_spacearr_tls->mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
550}
551
553 MLoopNorSpaceArray *lnors_spacearr_tls)
554{
555 BLI_assert(lnors_spacearr->data_type == lnors_spacearr_tls->data_type);
556 BLI_assert(lnors_spacearr->mem != lnors_spacearr_tls->mem);
557 lnors_spacearr->spaces_num += lnors_spacearr_tls->spaces_num;
558 BLI_memarena_merge(lnors_spacearr->mem, lnors_spacearr_tls->mem);
559 BLI_memarena_free(lnors_spacearr_tls->mem);
560 lnors_spacearr_tls->mem = nullptr;
561 BKE_lnor_spacearr_clear(lnors_spacearr_tls);
562}
563
565{
566 lnors_spacearr->spaces_num = 0;
567 lnors_spacearr->lspacearr = nullptr;
568 lnors_spacearr->loops_pool = nullptr;
569 if (lnors_spacearr->mem != nullptr) {
570 BLI_memarena_clear(lnors_spacearr->mem);
571 }
572}
573
575{
576 lnors_spacearr->spaces_num = 0;
577 lnors_spacearr->lspacearr = nullptr;
578 lnors_spacearr->loops_pool = nullptr;
579 BLI_memarena_free(lnors_spacearr->mem);
580 lnors_spacearr->mem = nullptr;
581}
582
584{
585 lnors_spacearr->spaces_num++;
586 return static_cast<MLoopNorSpace *>(
587 BLI_memarena_calloc(lnors_spacearr->mem, sizeof(MLoopNorSpace)));
588}
589
590/* This threshold is a bit touchy (usual float precision issue), this value seems OK. */
591#define LNOR_SPACE_TRIGO_THRESHOLD (1.0f - 1e-4f)
592
593namespace bke::mesh {
594
596 const float3 &vec_ref,
597 const float3 &vec_other,
598 const Span<float3> edge_vectors)
599{
600 CornerNormalSpace lnor_space{};
601 const float pi2 = float(M_PI) * 2.0f;
602 const float dtp_ref = math::dot(vec_ref, lnor);
603 const float dtp_other = math::dot(vec_other, lnor);
604
605 if (std::abs(dtp_ref) >= LNOR_SPACE_TRIGO_THRESHOLD ||
606 std::abs(dtp_other) >= LNOR_SPACE_TRIGO_THRESHOLD) [[unlikely]]
607 {
608 /* If vec_ref or vec_other are too much aligned with lnor, we can't build lnor space,
609 * tag it as invalid and abort. */
610 lnor_space.ref_alpha = lnor_space.ref_beta = 0.0f;
611 return lnor_space;
612 }
613
614 lnor_space.vec_lnor = lnor;
615
616 /* Compute ref alpha, average angle of all available edge vectors to lnor. */
617 if (!edge_vectors.is_empty()) {
618 float alpha = 0.0f;
619 for (const float3 &vec : edge_vectors) {
620 alpha += math::safe_acos_approx(math::dot(vec, lnor));
621 }
622 /* This piece of code shall only be called for more than one loop. */
623 /* NOTE: In theory, this could be `count > 2`,
624 * but there is one case where we only have two edges for two loops:
625 * a smooth vertex with only two edges and two faces (our Monkey's nose has that, e.g.).
626 */
627 BLI_assert(edge_vectors.size() >= 2);
628 lnor_space.ref_alpha = alpha / float(edge_vectors.size());
629 }
630 else {
631 lnor_space.ref_alpha = (math::safe_acos_approx(math::dot(vec_ref, lnor)) +
632 math::safe_acos_approx(math::dot(vec_other, lnor))) /
633 2.0f;
634 }
635
636 /* Project vec_ref on lnor's ortho plane. */
637 lnor_space.vec_ref = math::normalize(vec_ref - lnor * dtp_ref);
638 lnor_space.vec_ortho = math::normalize(math::cross(lnor, lnor_space.vec_ref));
639
640 /* Project vec_other on lnor's ortho plane. */
641 const float3 vec_other_proj = math::normalize(vec_other - lnor * dtp_other);
642
643 /* Beta is angle between ref_vec and other_vec, around lnor. */
644 const float dtp = math::dot(lnor_space.vec_ref, vec_other_proj);
645 if (dtp < LNOR_SPACE_TRIGO_THRESHOLD) [[likely]] {
646 const float beta = math::safe_acos_approx(dtp);
647 lnor_space.ref_beta = (math::dot(lnor_space.vec_ortho, vec_other_proj) < 0.0f) ? pi2 - beta :
648 beta;
649 }
650 else {
651 lnor_space.ref_beta = pi2;
652 }
653
654 return lnor_space;
655}
656
657} // namespace bke::mesh
658
660 const float lnor[3],
661 const float vec_ref[3],
662 const float vec_other[3],
663 const Span<float3> edge_vectors)
664{
665 using namespace blender::bke::mesh;
666 const CornerNormalSpace space = corner_fan_space_define(lnor, vec_ref, vec_other, edge_vectors);
667 copy_v3_v3(lnor_space->vec_lnor, space.vec_lnor);
668 copy_v3_v3(lnor_space->vec_ref, space.vec_ref);
669 copy_v3_v3(lnor_space->vec_ortho, space.vec_ortho);
670 lnor_space->ref_alpha = space.ref_alpha;
671 lnor_space->ref_beta = space.ref_beta;
672}
673
675 MLoopNorSpace *lnor_space,
676 const int corner,
677 void *bm_loop,
678 const bool is_single)
679{
680 BLI_assert((lnors_spacearr->data_type == MLNOR_SPACEARR_LOOP_INDEX && bm_loop == nullptr) ||
681 (lnors_spacearr->data_type == MLNOR_SPACEARR_BMLOOP_PTR && bm_loop != nullptr));
682
683 lnors_spacearr->lspacearr[corner] = lnor_space;
684 if (bm_loop == nullptr) {
685 bm_loop = POINTER_FROM_INT(corner);
686 }
687 if (is_single) {
688 BLI_assert(lnor_space->loops == nullptr);
689 lnor_space->flags |= MLNOR_SPACE_IS_SINGLE;
690 lnor_space->loops = static_cast<LinkNode *>(bm_loop);
691 }
692 else {
693 BLI_assert((lnor_space->flags & MLNOR_SPACE_IS_SINGLE) == 0);
694 BLI_linklist_prepend_nlink(&lnor_space->loops, bm_loop, &lnors_spacearr->loops_pool[corner]);
695 }
696}
697
698MINLINE float unit_short_to_float(const short val)
699{
700 return float(val) / float(SHRT_MAX);
701}
702
703MINLINE short unit_float_to_short(const float val)
704{
705 /* Rounding. */
706 return short(floorf(val * float(SHRT_MAX) + 0.5f));
707}
708
709namespace bke::mesh {
710
712 const short2 clnor_data)
713{
714 /* NOP custom normal data or invalid lnor space, return. */
715 if (clnor_data[0] == 0 || lnor_space.ref_alpha == 0.0f || lnor_space.ref_beta == 0.0f) {
716 return lnor_space.vec_lnor;
717 }
718
719 float3 custom_lnor;
720
721 /* TODO: Check whether using #sincosf() gives any noticeable benefit
722 * (could not even get it working under linux though)! */
723 const float pi2 = float(M_PI * 2.0);
724 const float alphafac = unit_short_to_float(clnor_data[0]);
725 const float alpha = (alphafac > 0.0f ? lnor_space.ref_alpha : pi2 - lnor_space.ref_alpha) *
726 alphafac;
727 const float betafac = unit_short_to_float(clnor_data[1]);
728
729 mul_v3_v3fl(custom_lnor, lnor_space.vec_lnor, cosf(alpha));
730
731 if (betafac == 0.0f) {
732 madd_v3_v3fl(custom_lnor, lnor_space.vec_ref, sinf(alpha));
733 }
734 else {
735 const float sinalpha = sinf(alpha);
736 const float beta = (betafac > 0.0f ? lnor_space.ref_beta : pi2 - lnor_space.ref_beta) *
737 betafac;
738 madd_v3_v3fl(custom_lnor, lnor_space.vec_ref, sinalpha * cosf(beta));
739 madd_v3_v3fl(custom_lnor, lnor_space.vec_ortho, sinalpha * sinf(beta));
740 }
741
742 return custom_lnor;
743}
744
745} // namespace bke::mesh
746
748 const short clnor_data[2],
749 float r_custom_lnor[3])
750{
751 using namespace blender::bke::mesh;
752 CornerNormalSpace space;
753 space.vec_lnor = lnor_space->vec_lnor;
754 space.vec_ref = lnor_space->vec_ref;
755 space.vec_ortho = lnor_space->vec_ortho;
756 space.ref_alpha = lnor_space->ref_alpha;
757 space.ref_beta = lnor_space->ref_beta;
758 copy_v3_v3(r_custom_lnor, corner_space_custom_data_to_normal(space, clnor_data));
759}
760
761namespace bke::mesh {
762
764 const float3 &custom_lnor)
765{
766 /* We use zero vector as NOP custom normal (can be simpler than giving auto-computed `lnor`). */
767 if (is_zero_v3(custom_lnor) || compare_v3v3(lnor_space.vec_lnor, custom_lnor, 1e-4f)) {
768 return short2(0);
769 }
770
771 short2 clnor_data;
772
773 const float pi2 = float(M_PI * 2.0);
774 const float cos_alpha = math::dot(lnor_space.vec_lnor, custom_lnor);
775
776 const float alpha = math::safe_acos_approx(cos_alpha);
777 if (alpha > lnor_space.ref_alpha) {
778 /* Note we could stick to [0, pi] range here,
779 * but makes decoding more complex, not worth it. */
780 clnor_data[0] = unit_float_to_short(-(pi2 - alpha) / (pi2 - lnor_space.ref_alpha));
781 }
782 else {
783 clnor_data[0] = unit_float_to_short(alpha / lnor_space.ref_alpha);
784 }
785
786 /* Project custom lnor on (vec_ref, vec_ortho) plane. */
787 const float3 vec = math::normalize(lnor_space.vec_lnor * -cos_alpha + custom_lnor);
788
789 const float cos_beta = math::dot(lnor_space.vec_ref, vec);
790
791 if (cos_beta < LNOR_SPACE_TRIGO_THRESHOLD) {
792 float beta = math::safe_acos_approx(cos_beta);
793 if (math::dot(lnor_space.vec_ortho, vec) < 0.0f) {
794 beta = pi2 - beta;
795 }
796
797 if (beta > lnor_space.ref_beta) {
798 clnor_data[1] = unit_float_to_short(-(pi2 - beta) / (pi2 - lnor_space.ref_beta));
799 }
800 else {
801 clnor_data[1] = unit_float_to_short(beta / lnor_space.ref_beta);
802 }
803 }
804 else {
805 clnor_data[1] = 0;
806 }
807
808 return clnor_data;
809}
810
811} // namespace bke::mesh
812
814 const float custom_lnor[3],
815 short r_clnor_data[2])
816{
817 using namespace blender::bke::mesh;
818 CornerNormalSpace space;
819 space.vec_lnor = lnor_space->vec_lnor;
820 space.vec_ref = lnor_space->vec_ref;
821 space.vec_ortho = lnor_space->vec_ortho;
822 space.ref_alpha = lnor_space->ref_alpha;
823 space.ref_beta = lnor_space->ref_beta;
824 copy_v2_v2_short(r_clnor_data, corner_space_custom_normal_to_data(space, custom_lnor));
825}
826
827namespace bke {
828
829namespace mesh {
830
831#define INDEX_UNSET INT_MIN
832#define INDEX_INVALID -1
833/* See comment about edge_to_corners below. */
834#define IS_EDGE_SHARP(_e2l) ELEM((_e2l)[1], INDEX_UNSET, INDEX_INVALID)
835
837 const Span<int> corner_verts,
838 const Span<int> corner_edges,
839 const Span<int> corner_to_face_map,
840 const Span<float3> face_normals,
841 const Span<bool> sharp_faces,
842 const Span<bool> sharp_edges,
843 const float split_angle,
844 MutableSpan<int2> edge_to_corners,
845 MutableSpan<bool> r_sharp_edges)
846{
847 const float split_angle_cos = cosf(split_angle);
848 auto face_is_smooth = [&](const int face_i) {
849 return sharp_faces.is_empty() || !sharp_faces[face_i];
850 };
851
852 for (const int face_i : faces.index_range()) {
853 for (const int corner : faces[face_i]) {
854 const int vert = corner_verts[corner];
855 const int edge = corner_edges[corner];
856
857 int2 &e2l = edge_to_corners[edge];
858
859 /* Check whether current edge might be smooth or sharp */
860 if ((e2l[0] | e2l[1]) == 0) {
861 /* 'Empty' edge until now, set e2l[0] (and e2l[1] to INDEX_UNSET to tag it as unset). */
862 e2l[0] = corner;
863 /* We have to check this here too, else we might miss some flat faces!!! */
864 e2l[1] = face_is_smooth(face_i) ? INDEX_UNSET : INDEX_INVALID;
865 }
866 else if (e2l[1] == INDEX_UNSET) {
867 const bool is_angle_sharp = math::dot(face_normals[corner_to_face_map[e2l[0]]],
868 face_normals[face_i]) < split_angle_cos;
869
870 /* Second corner using this edge, time to test its sharpness.
871 * An edge is sharp if it is tagged as such, or its face is not smooth,
872 * or both faces have opposed (flipped) normals, i.e. both corners on the same edge share
873 * the same vertex, or angle between both its faces' normals is above split_angle value. */
874 if (!face_is_smooth(face_i) || (!sharp_edges.is_empty() && sharp_edges[edge]) ||
875 vert == corner_verts[e2l[0]] || is_angle_sharp)
876 {
877 /* NOTE: we are sure that corner != 0 here ;). */
878 e2l[1] = INDEX_INVALID;
879
880 /* We want to avoid tagging edges as sharp when it is already defined as such by
881 * other causes than angle threshold. */
882 if (is_angle_sharp) {
883 r_sharp_edges[edge] = true;
884 }
885 }
886 else {
887 e2l[1] = corner;
888 }
889 }
890 else if (!IS_EDGE_SHARP(e2l)) {
891 /* More than two corners using this edge, tag as sharp if not yet done. */
892 e2l[1] = INDEX_INVALID;
893
894 /* We want to avoid tagging edges as sharp when it is already defined as such by
895 * other causes than angle threshold. */
896 r_sharp_edges[edge] = false;
897 }
898 /* Else, edge is already 'disqualified' (i.e. sharp)! */
899 }
900 }
901}
902
904 const Span<int> corner_verts,
905 const Span<int> corner_edges,
906 const Span<float3> face_normals,
907 const Span<int> corner_to_face,
908 const Span<bool> sharp_faces,
909 const float split_angle,
910 MutableSpan<bool> sharp_edges)
911{
912 if (split_angle >= float(M_PI)) {
913 /* Nothing to do! */
914 return;
915 }
916
917 /* Mapping edge -> corners. */
918 Array<int2> edge_to_corners(sharp_edges.size(), int2(0));
919
921 corner_verts,
922 corner_edges,
923 corner_to_face,
924 face_normals,
925 sharp_faces,
926 sharp_edges,
927 split_angle,
928 edge_to_corners,
929 sharp_edges);
930}
931
942
951 const Span<int> corner_verts,
952 const Span<int> vert_faces,
953 const int vert,
954 MutableSpan<VertCornerInfo> r_corner_infos)
955{
956 for (const int i : vert_faces.index_range()) {
957 const int face = vert_faces[i];
958 r_corner_infos[i].face = face;
959 r_corner_infos[i].corner = face_find_corner_from_vert(faces[face], corner_verts, vert);
960 r_corner_infos[i].corner_prev = face_corner_prev(faces[face], r_corner_infos[i].corner);
961 r_corner_infos[i].corner_next = face_corner_next(faces[face], r_corner_infos[i].corner);
962 r_corner_infos[i].vert_prev = corner_verts[r_corner_infos[i].corner_prev];
963 r_corner_infos[i].vert_next = corner_verts[r_corner_infos[i].corner_next];
964 }
965}
966
968using EdgeUninitialized = std::monostate;
969
979
988
994struct EdgeSharp {};
995
996using VertEdgeInfo = std::variant<EdgeUninitialized, EdgeOneCorner, EdgeTwoCorners, EdgeSharp>;
997
998static void add_corner_to_edge(const Span<int> corner_edges,
999 const Span<bool> sharp_edges,
1000 const int local_corner,
1001 const int corner,
1002 const int other_corner,
1003 const bool winding_torwards_vert,
1004 VertEdgeInfo &info)
1005{
1006 if (std::holds_alternative<EdgeUninitialized>(info)) {
1007 if (!sharp_edges.is_empty()) {
1008 /* The first time we encounter the edge, we check if it is marked sharp. In that case corner
1009 * fans shouldn't propagate past it. To find the edge we need to check if the current corner
1010 * references the edge connected to `other_corner` or if `other_corner` uses the edge. */
1011 if (sharp_edges[corner_edges[winding_torwards_vert ? other_corner : corner]]) {
1012 info = EdgeSharp{};
1013 return;
1014 }
1015 }
1016 info = EdgeOneCorner{local_corner, winding_torwards_vert};
1017 }
1018 else if (const EdgeOneCorner *info_one_edge = std::get_if<EdgeOneCorner>(&info)) {
1019 /* If the edge ends up being used by faces, we still have to check if the winding direction
1020 * changes. Though it's an undesirable situation for the mesh to be in, we shouldn't propagate
1021 * smooth normals across edges facing opposite directions. Breaking the flow on these winding
1022 * direction changes also simplifies the fan traversal later on; without it the we couldn't
1023 * traverse by just continuing to use the next/previous corner. */
1024 if (info_one_edge->winding_torwards_vert == winding_torwards_vert) {
1025 info = EdgeSharp{};
1026 return;
1027 }
1028 info = EdgeTwoCorners{info_one_edge->local_corner_1, local_corner};
1029 }
1030 else {
1031 /* The edge is either already sharp, or we're trying to add a third corner. */
1032 info = EdgeSharp{};
1033 }
1034}
1035
1038 16,
1044
1051 LocalEdgeVectorSet &r_other_vert_to_edge)
1052{
1053 r_other_vert_to_edge.reserve(corner_infos.size());
1054 for (VertCornerInfo &info : corner_infos) {
1055 info.local_edge_prev = r_other_vert_to_edge.index_of_or_add(info.vert_prev);
1056 info.local_edge_next = r_other_vert_to_edge.index_of_or_add(info.vert_next);
1057 }
1058}
1059
1060static void calc_connecting_edge_info(const Span<int> corner_edges,
1061 const Span<bool> sharp_edges,
1062 const Span<bool> sharp_faces,
1063 const Span<VertCornerInfo> corner_infos,
1064 MutableSpan<VertEdgeInfo> edge_infos)
1065{
1066 for (const int local_corner : corner_infos.index_range()) {
1067 const VertCornerInfo &info = corner_infos[local_corner];
1068 if (!sharp_faces.is_empty() && sharp_faces[info.face]) {
1069 /* Sharp faces implicitly cause sharp edges. */
1070 edge_infos[info.local_edge_prev] = EdgeSharp{};
1071 edge_infos[info.local_edge_next] = EdgeSharp{};
1072 continue;
1073 }
1074 /* The "previous" edge is winding towards the vertex, the "next" edge is winding away. */
1075 add_corner_to_edge(corner_edges,
1076 sharp_edges,
1077 local_corner,
1078 info.corner,
1079 info.corner_prev,
1080 true,
1081 edge_infos[info.local_edge_prev]);
1082 add_corner_to_edge(corner_edges,
1083 sharp_edges,
1084 local_corner,
1085 info.corner,
1086 info.corner_next,
1087 false,
1088 edge_infos[info.local_edge_next]);
1089 }
1090}
1091
1098 const Span<VertEdgeInfo> edge_infos,
1099 const int start_local_corner,
1100 Vector<int, 16> &result_fan)
1101{
1102 result_fan.append(start_local_corner);
1103 {
1104 /* Travel around the vertex in a right-handed clockwise direction (based on the normal). The
1105 * corners found in this traversal are reversed so the direction matches with the next
1106 * traversal (or so that the next traversal doesn't have to be added at the beginning of the
1107 * vector). */
1108 int current = start_local_corner;
1109 int local_edge = corner_infos[current].local_edge_next;
1110 bool found_cyclic_fan = false;
1111 while (const EdgeTwoCorners *edge = std::get_if<EdgeTwoCorners>(&edge_infos[local_edge])) {
1112 current = mesh::edge_other_vert(int2(edge->local_corner_1, edge->local_corner_2), current);
1113 if (current == start_local_corner) {
1114 found_cyclic_fan = true;
1115 break;
1116 }
1117 result_fan.append(current);
1118 local_edge = corner_infos[current].local_edge_next;
1119 }
1120 /* Reverse the corners added so the final order is consistent with the next traversal. */
1121 result_fan.as_mutable_span().reverse();
1122
1123 if (found_cyclic_fan) {
1124 /* To match behavior from the previous implementation of face corner normal calculation, the
1125 * final fan is rotated so that the smallest face corner index comes first. */
1126 int *fan_first_corner = std::min_element(
1127 result_fan.begin(), result_fan.end(), [&](const int a, const int b) {
1128 return corner_infos[a].corner < corner_infos[b].corner;
1129 });
1130 std::rotate(result_fan.begin(), fan_first_corner, result_fan.end());
1131 return;
1132 }
1133 }
1134
1135 /* Travel in the other direction. */
1136 int current = start_local_corner;
1137 int local_edge = corner_infos[current].local_edge_prev;
1138 while (const EdgeTwoCorners *edge = std::get_if<EdgeTwoCorners>(&edge_infos[local_edge])) {
1139 current = current == edge->local_corner_1 ? edge->local_corner_2 : edge->local_corner_1;
1140 /* Cyclic fans have already been found, so there's no need to check for them here. */
1141 result_fan.append(current);
1142 local_edge = corner_infos[current].local_edge_prev;
1143 }
1144}
1145
1151static void calc_edge_directions(const Span<float3> vert_positions,
1152 const Span<int> local_edge_by_vert,
1153 const float3 &vert_position,
1154 MutableSpan<float3> edge_dirs)
1155{
1156 for (const int i : local_edge_by_vert.index_range()) {
1157 edge_dirs[i] = math::normalize(vert_positions[local_edge_by_vert[i]] - vert_position);
1158 }
1159}
1160
1163 const Span<float3> edge_dirs,
1164 const Span<float3> face_normals,
1165 const Span<int> local_corners_in_fan)
1166{
1167 if (local_corners_in_fan.size() == 1) {
1168 /* Logically this special case is unnecessary, but due to floating point precision it is
1169 * required for the output to be the same as previous versions of the algorithm. */
1170 return face_normals[corner_infos[local_corners_in_fan.first()].face];
1171 }
1172 float3 fan_normal(0);
1173 for (const int local_corner : local_corners_in_fan) {
1174 const VertCornerInfo &info = corner_infos[local_corner];
1175 const float3 &dir_prev = edge_dirs[info.local_edge_prev];
1176 const float3 &dir_next = edge_dirs[info.local_edge_next];
1177 const float factor = math::safe_acos_approx(math::dot(dir_prev, dir_next));
1178 fan_normal += face_normals[info.face] * factor;
1179 }
1180 return math::normalize(fan_normal);
1181}
1182
1184 /* Maybe acyclic and unordered set of adjacent corners in same smooth group around vertex. */
1187};
1188
1191 const Span<short2> custom_normals,
1192 const Span<VertCornerInfo> corner_infos,
1193 const Span<float3> edge_dirs,
1194 const Span<int> local_corners_in_fan,
1195 float3 &fan_normal,
1196 CornerNormalSpaceArray *r_fan_spaces,
1197 Vector<CornerSpaceGroup, 0> *r_local_space_groups)
1198{
1199 const int local_edge_first = corner_infos[local_corners_in_fan.first()].local_edge_next;
1200 const int local_edge_last = corner_infos[local_corners_in_fan.last()].local_edge_prev;
1201
1202 Vector<float3, 16> fan_edge_dirs;
1203 if (local_corners_in_fan.size() > 1) {
1204 fan_edge_dirs.reserve(local_corners_in_fan.size() + 1);
1205 for (const int local_corner : local_corners_in_fan) {
1206 const VertCornerInfo &info = corner_infos[local_corner];
1207 fan_edge_dirs.append_unchecked(edge_dirs[info.local_edge_next]);
1208 }
1209 if (local_edge_last != local_edge_first) {
1210 fan_edge_dirs.append_unchecked(edge_dirs[local_edge_last]);
1211 }
1212 }
1213
1215 fan_normal, edge_dirs[local_edge_first], edge_dirs[local_edge_last], fan_edge_dirs);
1216
1217 if (!custom_normals.is_empty()) {
1218 int2 average_custom_normal(0);
1219 for (const int local_corner : local_corners_in_fan) {
1220 const VertCornerInfo &info = corner_infos[local_corner];
1221 average_custom_normal += int2(custom_normals[info.corner]);
1222 }
1223 average_custom_normal /= local_corners_in_fan.size();
1224 fan_normal = corner_space_custom_data_to_normal(fan_space, short2(average_custom_normal));
1225 }
1226
1227 if (!r_fan_spaces) {
1228 return;
1229 }
1230
1231 Array<int> fan_corners(local_corners_in_fan.size());
1232 for (const int i : local_corners_in_fan.index_range()) {
1233 const VertCornerInfo &info = corner_infos[local_corners_in_fan[i]];
1234 fan_corners[i] = info.corner;
1235 }
1236 r_local_space_groups->append({std::move(fan_corners), fan_space});
1237}
1238
1239void normals_calc_corners(const Span<float3> vert_positions,
1240 const OffsetIndices<int> faces,
1241 const Span<int> corner_verts,
1242 const Span<int> corner_edges,
1243 const GroupedSpan<int> vert_to_face_map,
1244 const Span<float3> face_normals,
1245 const Span<bool> sharp_edges,
1246 const Span<bool> sharp_faces,
1247 const Span<short2> custom_normals,
1248 CornerNormalSpaceArray *r_fan_spaces,
1249 MutableSpan<float3> r_corner_normals)
1250{
1252 BLI_assert(corner_verts.size() == corner_edges.size());
1253 BLI_assert(custom_normals.is_empty() || corner_verts.size() == custom_normals.size());
1254 BLI_assert(corner_verts.size() == r_corner_normals.size());
1255 BLI_assert(corner_verts.size() == vert_to_face_map.offsets.total_size());
1256
1257 /* Mesh is not empty, but there are no faces, so no normals. */
1258 if (corner_verts.is_empty()) {
1259 return;
1260 }
1261
1263
1264 threading::parallel_for(vert_positions.index_range(), 256, [&](const IndexRange range) {
1265 Vector<VertCornerInfo, 16> corner_infos;
1266 LocalEdgeVectorSet local_edge_by_vert;
1267 Vector<VertEdgeInfo, 16> edge_infos;
1268 Vector<float3, 16> edge_dirs;
1269 Vector<bool, 16> local_corner_visited;
1270 Vector<int, 16> corners_in_fan;
1271
1272 Vector<CornerSpaceGroup, 0> *local_space_groups = r_fan_spaces ? &space_groups.local() :
1273 nullptr;
1274
1275 for (const int vert : range) {
1276 const float3 vert_position = vert_positions[vert];
1277 const Span<int> vert_faces = vert_to_face_map[vert];
1278
1279 /* Because we're iterating over vertices in order to batch work for their connected face
1280 * corners, we have to handle loose vertices and vertices not used by faces. */
1281 if (vert_faces.is_empty()) {
1282 continue;
1283 }
1284
1285 corner_infos.resize(vert_faces.size());
1286 collect_corner_info(faces, corner_verts, vert_faces, vert, corner_infos);
1287
1288 local_edge_by_vert.clear_and_keep_capacity();
1289 calc_local_edge_indices(corner_infos, local_edge_by_vert);
1290
1291 edge_infos.clear();
1292 edge_infos.resize(local_edge_by_vert.size());
1293 calc_connecting_edge_info(corner_edges, sharp_edges, sharp_faces, corner_infos, edge_infos);
1294
1295 edge_dirs.resize(edge_infos.size());
1296 calc_edge_directions(vert_positions, local_edge_by_vert, vert_position, edge_dirs);
1297
1298 /* Though we are protected from traversing to the same corner twice by the fact that 3-way
1299 * connections are marked sharp, we need to maintain the "visited" status of each corner so
1300 * we can find the next start corner for each subsequent fan traversal. Keeping track of the
1301 * number of visited corners is a quick way to avoid this book keeping for the final fan (and
1302 * there are usually just two, so that should be worth it). */
1303 int visited_count = 0;
1304 local_corner_visited.resize(vert_faces.size());
1305 local_corner_visited.fill(false);
1306
1307 int start_local_corner = 0;
1308 while (true) {
1309 corners_in_fan.clear();
1310 traverse_fan_local_corners(corner_infos, edge_infos, start_local_corner, corners_in_fan);
1311
1312 float3 fan_normal = accumulate_fan_normal(
1313 corner_infos, edge_dirs, face_normals, corners_in_fan);
1314
1315 if (!custom_normals.is_empty() || r_fan_spaces) {
1316 handle_fan_result_and_custom_normals(custom_normals,
1317 corner_infos,
1318 edge_dirs,
1319 corners_in_fan,
1320 fan_normal,
1321 r_fan_spaces,
1322 local_space_groups);
1323 }
1324
1325 for (const int local_corner : corners_in_fan) {
1326 const VertCornerInfo &info = corner_infos[local_corner];
1327 r_corner_normals[info.corner] = fan_normal;
1328 }
1329
1330 visited_count += corners_in_fan.size();
1331 if (visited_count == corner_infos.size()) {
1332 break;
1333 }
1334
1335 local_corner_visited.as_mutable_span().fill_indices(corners_in_fan.as_span(), true);
1336 BLI_assert(!local_corner_visited.as_span().take_front(start_local_corner).contains(false));
1337 BLI_assert(local_corner_visited.as_span().drop_front(start_local_corner).contains(false));
1338 /* Will start traversing the next smooth fan mixed in shared index space. */
1339 while (local_corner_visited[start_local_corner]) {
1340 start_local_corner++;
1341 }
1342 }
1343 BLI_assert(visited_count == corner_infos.size());
1344 }
1345 });
1346
1347 if (!r_fan_spaces) {
1348 return;
1349 }
1350
1351 Vector<int> space_groups_count;
1352 Vector<Vector<CornerSpaceGroup, 0>> all_space_groups;
1353 /* WARNING: can't use `auto` here, causes build failure on GCC 15.2, WITH_TBB=OFF. */
1354 for (Vector<CornerSpaceGroup, 0> &groups : space_groups) {
1355 space_groups_count.append(groups.size());
1356 all_space_groups.append(std::move(groups));
1357 }
1358 space_groups_count.append(0);
1360 space_groups_count);
1361
1362 r_fan_spaces->spaces.reinitialize(space_offsets.total_size());
1363 r_fan_spaces->corner_space_indices.reinitialize(corner_verts.size());
1364 if (r_fan_spaces->create_corners_by_space) {
1365 r_fan_spaces->corners_by_space.reinitialize(space_offsets.total_size());
1366 }
1367
1368 /* Copy the data from each local data vector to the final array. it's expected that
1369 * multi-threading has some benefit here, even though the work is largely just copying memory,
1370 * but choose a large grain size to err on the size of less parallelization. */
1371 const int64_t mean_size = std::max<int64_t>(1,
1372 space_offsets.total_size() / space_offsets.size());
1373 const int64_t grain_size = std::max<int64_t>(1, 1024 * 16 / mean_size);
1374 threading::parallel_for(all_space_groups.index_range(), grain_size, [&](const IndexRange range) {
1375 for (const int thread_i : range) {
1376 Vector<CornerSpaceGroup, 0> &local_space_groups = all_space_groups[thread_i];
1377 for (const int group_i : local_space_groups.index_range()) {
1378 const int space_index = space_offsets[thread_i][group_i];
1379 r_fan_spaces->spaces[space_index] = local_space_groups[group_i].space;
1380 r_fan_spaces->corner_space_indices.as_mutable_span().fill_indices(
1381 local_space_groups[group_i].fan_corners.as_span(), space_index);
1382 }
1383 if (!r_fan_spaces->create_corners_by_space) {
1384 continue;
1385 }
1386 for (const int group_i : local_space_groups.index_range()) {
1387 const int space_index = space_offsets[thread_i][group_i];
1388 r_fan_spaces->corners_by_space[space_index] = std::move(
1389 local_space_groups[group_i].fan_corners);
1390 }
1391 }
1392 });
1393}
1394
1395#undef INDEX_UNSET
1396#undef INDEX_INVALID
1397#undef IS_EDGE_SHARP
1398
1408
1410 const OffsetIndices<int> faces,
1411 const Span<int> corner_verts,
1412 const Span<int> corner_edges,
1413 const GroupedSpan<int> vert_to_face_map,
1414 const Span<float3> vert_normals,
1415 const Span<float3> face_normals,
1416 const Span<bool> sharp_faces,
1417 const bool use_vertices,
1418 MutableSpan<float3> r_custom_corner_normals,
1419 MutableSpan<bool> sharp_edges,
1420 MutableSpan<short2> r_clnors_data)
1421{
1422 /* We *may* make that poor #bke::mesh::normals_calc_corners() even more complex by making it
1423 * handling that feature too, would probably be more efficient in absolute. However, this
1424 * function *is not* performance-critical, since it is mostly expected to be called by IO add-ons
1425 * when importing custom normals, and modifier (and perhaps from some editing tools later?). So
1426 * better to keep some simplicity here, and just call #bke::mesh::normals_calc_corners() twice!
1427 */
1428 CornerNormalSpaceArray lnors_spacearr;
1429 lnors_spacearr.create_corners_by_space = true;
1430 BitVector<> done_corners(corner_verts.size(), false);
1431 Array<float3> corner_normals(corner_verts.size());
1432 const Array<int> corner_to_face = build_corner_to_face_map(faces);
1433
1434 /* Compute current lnor spacearr. */
1435 normals_calc_corners(positions,
1436 faces,
1437 corner_verts,
1438 corner_edges,
1439 vert_to_face_map,
1440 face_normals,
1441 sharp_edges,
1442 sharp_faces,
1443 r_clnors_data,
1444 &lnors_spacearr,
1445 corner_normals);
1446
1447 /* Set all given zero vectors to their default value. */
1448 if (use_vertices) {
1449 for (const int i : positions.index_range()) {
1450 if (is_zero_v3(r_custom_corner_normals[i])) {
1451 copy_v3_v3(r_custom_corner_normals[i], vert_normals[i]);
1452 }
1453 }
1454 }
1455 else {
1456 for (const int i : corner_verts.index_range()) {
1457 if (is_zero_v3(r_custom_corner_normals[i])) {
1458 copy_v3_v3(r_custom_corner_normals[i], corner_normals[i]);
1459 }
1460 }
1461 }
1462
1463 /* Now, check each current smooth fan (one lnor space per smooth fan!),
1464 * and if all its matching custom corner_normals are not (enough) equal, add sharp edges as
1465 * needed. This way, next time we run bke::mesh::normals_calc_corners(), we'll get lnor
1466 * spacearr/smooth fans matching given custom corner_normals. Note this code *will never* unsharp
1467 * edges! And quite obviously, when we set custom normals per vertices, running this is
1468 * absolutely useless. */
1469 if (use_vertices) {
1470 done_corners.fill(true);
1471 }
1472 else {
1473 for (const int i : corner_verts.index_range()) {
1474 if (done_corners[i]) {
1475 continue;
1476 }
1477
1478 const int space_index = lnors_spacearr.corner_space_indices[i];
1479 const Span<int> fan_corners = lnors_spacearr.corners_by_space[space_index];
1480
1481 /* Notes:
1482 * - In case of mono-corner smooth fan, we have nothing to do.
1483 * - Loops in this linklist are ordered (in reversed order compared to how they were
1484 * discovered by bke::mesh::normals_calc_corners(), but this is not a problem).
1485 * Which means if we find a mismatching clnor,
1486 * we know all remaining corners will have to be in a new, different smooth fan/lnor space.
1487 * - In smooth fan case, we compare each clnor against a ref one,
1488 * to avoid small differences adding up into a real big one in the end!
1489 */
1490 if (fan_corners.is_empty()) {
1491 done_corners[i].set();
1492 continue;
1493 }
1494
1495 int prev_corner = -1;
1496 const float *org_nor = nullptr;
1497
1498 for (int i = fan_corners.index_range().last(); i >= 0; i--) {
1499 const int corner = fan_corners[i];
1500 float *nor = r_custom_corner_normals[corner];
1501
1502 if (!org_nor) {
1503 org_nor = nor;
1504 }
1505 else if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1506 /* Current normal differs too much from org one, we have to tag the edge between
1507 * previous corner's face and current's one as sharp.
1508 * We know those two corners do not point to the same edge,
1509 * since we do not allow reversed winding in a same smooth fan. */
1510 const IndexRange face = faces[corner_to_face[corner]];
1511 const int corner_prev = face_corner_prev(face, corner);
1512 const int edge = corner_edges[corner];
1513 const int edge_prev = corner_edges[corner_prev];
1514 const int prev_edge = corner_edges[prev_corner];
1515 sharp_edges[prev_edge == edge_prev ? prev_edge : edge] = true;
1516
1517 org_nor = nor;
1518 }
1519
1520 prev_corner = corner;
1521 done_corners[corner].set();
1522 }
1523
1524 /* We also have to check between last and first corners,
1525 * otherwise we may miss some sharp edges here!
1526 * This is just a simplified version of above while loop.
1527 * See #45984. */
1528 if (fan_corners.size() > 1 && org_nor) {
1529 const int corner = fan_corners.last();
1530 float *nor = r_custom_corner_normals[corner];
1531
1532 if (dot_v3v3(org_nor, nor) < LNOR_SPACE_TRIGO_THRESHOLD) {
1533 const IndexRange face = faces[corner_to_face[corner]];
1534 const int corner_prev = face_corner_prev(face, corner);
1535 const int edge = corner_edges[corner];
1536 const int edge_prev = corner_edges[corner_prev];
1537 const int prev_edge = corner_edges[prev_corner];
1538 sharp_edges[prev_edge == edge_prev ? prev_edge : edge] = true;
1539 }
1540 }
1541 }
1542
1543 /* And now, recompute our new auto `corner_normals` and lnor spacearr! */
1544 normals_calc_corners(positions,
1545 faces,
1546 corner_verts,
1547 corner_edges,
1548 vert_to_face_map,
1549 face_normals,
1550 sharp_edges,
1551 sharp_faces,
1552 r_clnors_data,
1553 &lnors_spacearr,
1554 corner_normals);
1555 }
1556
1557 /* And we just have to convert plain object-space custom normals to our
1558 * lnor space-encoded ones. */
1559 for (const int i : corner_verts.index_range()) {
1560 if (lnors_spacearr.corner_space_indices[i] == -1) {
1561 done_corners[i].reset();
1562 if (G.debug & G_DEBUG) {
1563 printf("WARNING! Still getting invalid nullptr corner space in second for loop %d!\n", i);
1564 }
1565 continue;
1566 }
1567 if (!done_corners[i]) {
1568 continue;
1569 }
1570
1571 const int space_index = lnors_spacearr.corner_space_indices[i];
1572 const Span<int> fan_corners = lnors_spacearr.corners_by_space[space_index];
1573
1574 /* Note we accumulate and average all custom normals in current smooth fan,
1575 * to avoid getting different clnors data (tiny differences in plain custom normals can
1576 * give rather huge differences in computed 2D factors). */
1577 if (fan_corners.size() < 2) {
1578 const int nidx = use_vertices ? corner_verts[i] : i;
1579 r_clnors_data[i] = corner_space_custom_normal_to_data(lnors_spacearr.spaces[space_index],
1580 r_custom_corner_normals[nidx]);
1581 done_corners[i].reset();
1582 }
1583 else {
1584 float3 avg_nor(0.0f);
1585 for (const int corner : fan_corners) {
1586 const int nidx = use_vertices ? corner_verts[corner] : corner;
1587 avg_nor += r_custom_corner_normals[nidx];
1588 done_corners[corner].reset();
1589 }
1590
1591 mul_v3_fl(avg_nor, 1.0f / float(fan_corners.size()));
1593 lnors_spacearr.spaces[space_index], avg_nor);
1594
1595 r_clnors_data.fill_indices(fan_corners, clnor_data_tmp);
1596 }
1597 }
1598}
1599
1600void normals_corner_custom_set(const Span<float3> vert_positions,
1601 const OffsetIndices<int> faces,
1602 const Span<int> corner_verts,
1603 const Span<int> corner_edges,
1604 const GroupedSpan<int> vert_to_face_map,
1605 const Span<float3> vert_normals,
1606 const Span<float3> face_normals,
1607 const Span<bool> sharp_faces,
1608 MutableSpan<bool> sharp_edges,
1609 MutableSpan<float3> r_custom_corner_normals,
1610 MutableSpan<short2> r_clnors_data)
1611{
1612 mesh_normals_corner_custom_set(vert_positions,
1613 faces,
1614 corner_verts,
1615 corner_edges,
1616 vert_to_face_map,
1617 vert_normals,
1618 face_normals,
1619 sharp_faces,
1620 false,
1621 r_custom_corner_normals,
1622 sharp_edges,
1623 r_clnors_data);
1624}
1625
1627 const OffsetIndices<int> faces,
1628 const Span<int> corner_verts,
1629 const Span<int> corner_edges,
1630 const GroupedSpan<int> vert_to_face_map,
1631 const Span<float3> vert_normals,
1632 const Span<float3> face_normals,
1633 const Span<bool> sharp_faces,
1634 MutableSpan<bool> sharp_edges,
1635 MutableSpan<float3> r_custom_vert_normals,
1636 MutableSpan<short2> r_clnors_data)
1637{
1638 mesh_normals_corner_custom_set(vert_positions,
1639 faces,
1640 corner_verts,
1641 corner_edges,
1642 vert_to_face_map,
1643 vert_normals,
1644 face_normals,
1645 sharp_faces,
1646 true,
1647 r_custom_vert_normals,
1648 sharp_edges,
1649 r_clnors_data);
1650}
1651
1653 MutableSpan<float3> r_custom_nors,
1654 const bool use_vertices)
1655{
1656 MutableAttributeAccessor attributes = mesh.attributes_for_write();
1657 SpanAttributeWriter custom_normals = attributes.lookup_or_add_for_write_span<short2>(
1658 "custom_normal", AttrDomain::Corner);
1659 if (!custom_normals) {
1660 return;
1661 }
1662 SpanAttributeWriter<bool> sharp_edges = attributes.lookup_or_add_for_write_span<bool>(
1663 "sharp_edge", AttrDomain::Edge);
1664 const VArraySpan sharp_faces = *attributes.lookup<bool>("sharp_face", AttrDomain::Face);
1665
1666 mesh_normals_corner_custom_set(mesh.vert_positions(),
1667 mesh.faces(),
1668 mesh.corner_verts(),
1669 mesh.corner_edges(),
1670 mesh.vert_to_face_map(),
1671 mesh.vert_normals_true(),
1672 mesh.face_normals_true(),
1673 sharp_faces,
1674 use_vertices,
1675 r_custom_nors,
1676 sharp_edges.span,
1677 custom_normals.span);
1678
1679 sharp_edges.finish();
1680 custom_normals.finish();
1681}
1682
1683} // namespace mesh
1684
1686{
1687 threading::parallel_for(normals.index_range(), 4096, [&](const IndexRange range) {
1688 for (const int i : range) {
1689 normals[i] = math::normalize(normals[i]);
1690 }
1691 });
1692}
1693
1695{
1696 normalize_vecs(corner_normals);
1697 mesh::mesh_set_custom_normals(mesh, corner_normals, false);
1698}
1699
1701{
1702 mesh::mesh_set_custom_normals(mesh, corner_normals, false);
1703}
1704
1706{
1707 normalize_vecs(vert_normals);
1708 mesh::mesh_set_custom_normals(mesh, vert_normals, true);
1709}
1710
1715
1716namespace mesh {
1717
1719
1721{
1722 return meta_data == CORNER_FAN_META_DATA;
1723}
1724
1738
1743
1751
1753{
1754 if (this->result_domain) {
1755 /* Any combination of point/face domains puts the result normals on the corner domain. */
1756 if (this->result_domain != domain) {
1758 }
1759 }
1760 else {
1761 this->result_domain = domain;
1762 }
1763}
1764
1766{
1767 this->add_domain(domain);
1768 this->result_type = Output::Free;
1769}
1770
1772{
1773 const bke::AttributeAccessor attributes = mesh.attributes();
1774 const std::optional<bke::AttributeMetaData> custom_normal = attributes.lookup_meta_data(
1775 "custom_normal");
1776 if (!custom_normal) {
1777 this->add_no_custom_normals(mesh.normals_domain());
1778 return;
1779 }
1780 if (custom_normal->data_type == bke::AttrType::Float3) {
1781 if (custom_normal->domain == bke::AttrDomain::Edge) {
1782 /* Skip invalid storage on the edge domain. */
1783 this->add_no_custom_normals(mesh.normals_domain());
1784 return;
1785 }
1786 this->add_free_normals(custom_normal->domain);
1787 }
1788 else if (*custom_normal == CORNER_FAN_META_DATA) {
1789 this->add_corner_fan_normals();
1790 }
1791}
1792
1793} // namespace mesh
1794
1795} // namespace bke
1796
1797#undef LNOR_SPACE_TRIGO_THRESHOLD
1798
1800
1801} // namespace blender
#define BLI_ASSERT_UNIT_V3(v)
#define M_PI
#define MINLINE
#define BLI_MEMARENA_STD_BUFSIZE
#define PRF_scope(category)
#define BLI_assert_unreachable()
#define POINTER_FROM_INT(i)
#define BLI_NOINLINE
#define ELEM(...)
long long int int64_t
AttributeSet attributes
constexpr const T * data() const
Definition BLI_span.hh:219
bool is() const
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void fill_indices(Span< IndexT > indices, const T &value) const
Definition BLI_span.hh:532
constexpr const T & first() const
Definition BLI_span.hh:319
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t index_of_or_add(const Key &key)
void reserve(const int64_t n)
void append(const T &value)
friend class Vector
IndexRange index_range() const
MutableSpan< T > as_mutable_span()
void append_unchecked(const T &value)
void reserve(const int64_t min_capacity)
void reinitialize(const int64_t new_size)
void fill(const bool value)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
GAttributeReader lookup(const StringRef name) const
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
void store_varray(const VArray< float3 > &data)
std::variant< UseTrueCache, Vector< float3 >, Span< float3 > > data
MutableSpan< float3 > ensure_vector_size(const int size)
Span< float3 > get_span() const
void store_vector(Vector< float3 > &&data)
std::optional< bke::AttrDomain > result_domain
Definition BKE_mesh.hh:263
void add_domain(bke::AttrDomain domain)
void add_free_normals(bke::AttrDomain domain)
void add_no_custom_normals(bke::MeshNormalDomain domain)
#define SHRT_MAX
VecBase< short, 2 > short2
VecBase< float, 3 > float3
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
#define INDEX_INVALID
#define INDEX_UNSET
#define LNOR_SPACE_TRIGO_THRESHOLD
#define IS_EDGE_SHARP(_e2l)
std::monostate EdgeUninitialized
int edge_other_vert(const int2 edge, const int vert)
Definition BKE_mesh.hh:370
static CornerNormalSpace corner_fan_space_define(const float3 &lnor, const float3 &vec_ref, const float3 &vec_other, const Span< float3 > edge_vectors)
static float3 normal_calc_ngon(const Span< float3 > vert_positions, const Span< int > face_verts)
static void mesh_normals_corner_custom_set(const Span< float3 > positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > corner_edges, const GroupedSpan< int > vert_to_face_map, const Span< float3 > vert_normals, const Span< float3 > face_normals, const Span< bool > sharp_faces, const bool use_vertices, MutableSpan< float3 > r_custom_corner_normals, MutableSpan< bool > sharp_edges, MutableSpan< short2 > r_clnors_data)
static float3 corner_space_custom_data_to_normal(const CornerNormalSpace &lnor_space, const short2 clnor_data)
constexpr AttributeMetaData CORNER_FAN_META_DATA
float3 face_normal_calc(Span< float3 > vert_positions, Span< int > face_verts)
static void calc_edge_directions(const Span< float3 > vert_positions, const Span< int > local_edge_by_vert, const float3 &vert_position, MutableSpan< float3 > edge_dirs)
static void mix_normals_vert_to_face(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< float3 > vert_normals, MutableSpan< float3 > face_normals)
int face_corner_prev(const IndexRange face, const int corner)
Definition BKE_mesh.hh:306
static void mix_normals_corner_to_vert(const Span< float3 > vert_positions, const OffsetIndices< int > faces, const Span< int > corner_verts, const GroupedSpan< int > vert_to_face_map, const Span< float3 > corner_normals, MutableSpan< float3 > vert_normals)
int face_find_corner_from_vert(const IndexRange face, const Span< int > corner_verts, const int vert)
Definition BKE_mesh.hh:327
static void calc_local_edge_indices(MutableSpan< VertCornerInfo > corner_infos, LocalEdgeVectorSet &r_other_vert_to_edge)
static void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > r_custom_nors, const bool use_vertices)
static void traverse_fan_local_corners(const Span< VertCornerInfo > corner_infos, const Span< VertEdgeInfo > edge_infos, const int start_local_corner, Vector< int, 16 > &result_fan)
static float3 accumulate_fan_normal(const Span< VertCornerInfo > corner_infos, const Span< float3 > edge_dirs, const Span< float3 > face_normals, const Span< int > local_corners_in_fan)
static void calc_connecting_edge_info(const Span< int > corner_edges, const Span< bool > sharp_edges, const Span< bool > sharp_faces, const Span< VertCornerInfo > corner_infos, MutableSpan< VertEdgeInfo > edge_infos)
Array< int > build_corner_to_face_map(OffsetIndices< int > faces)
static void add_corner_to_edge(const Span< int > corner_edges, const Span< bool > sharp_edges, const int local_corner, const int corner, const int other_corner, const bool winding_torwards_vert, VertEdgeInfo &info)
static void mix_normals_corner_to_face(const OffsetIndices< int > faces, const Span< float3 > corner_normals, MutableSpan< float3 > face_normals)
bool is_corner_fan_normals(const AttributeMetaData &meta_data)
void normals_calc_corners(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, Span< bool > sharp_edges, Span< bool > sharp_faces, Span< short2 > custom_normals, CornerNormalSpaceArray *r_fan_spaces, MutableSpan< float3 > r_corner_normals)
void normals_corner_custom_set_from_verts(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > vert_normals, Span< float3 > face_normals, Span< bool > sharp_faces, MutableSpan< bool > sharp_edges, MutableSpan< float3 > r_custom_vert_normals, MutableSpan< short2 > r_clnors_data)
static bke::AttrDomain normal_domain_to_domain(bke::MeshNormalDomain domain)
VectorSet< int, 16, DefaultProbingStrategy, DefaultHash< int >, DefaultEquality< int >, SimpleVectorSetSlot< int, int >, GuardedAllocator > LocalEdgeVectorSet
static void collect_corner_info(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > vert_faces, const int vert, MutableSpan< VertCornerInfo > r_corner_infos)
short2 corner_space_custom_normal_to_data(const CornerNormalSpace &lnor_space, const float3 &custom_lnor)
void edges_sharp_from_angle_set(OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, Span< float3 > face_normals, Span< int > corner_to_face, Span< bool > sharp_faces, const float split_angle, MutableSpan< bool > sharp_edges)
int face_corner_next(const IndexRange face, const int corner)
Definition BKE_mesh.hh:315
void normals_calc_verts(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, GroupedSpan< int > vert_to_face_map, Span< float3 > face_normals, MutableSpan< float3 > vert_normals)
static void mesh_edges_sharp_tag(const OffsetIndices< int > faces, const Span< int > corner_verts, const Span< int > corner_edges, const Span< int > corner_to_face_map, const Span< float3 > face_normals, const Span< bool > sharp_faces, const Span< bool > sharp_edges, const float split_angle, MutableSpan< int2 > edge_to_corners, MutableSpan< bool > r_sharp_edges)
static BLI_NOINLINE void handle_fan_result_and_custom_normals(const Span< short2 > custom_normals, const Span< VertCornerInfo > corner_infos, const Span< float3 > edge_dirs, const Span< int > local_corners_in_fan, float3 &fan_normal, CornerNormalSpaceArray *r_fan_spaces, Vector< CornerSpaceGroup, 0 > *r_local_space_groups)
void normals_corner_custom_set(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int > corner_edges, GroupedSpan< int > vert_to_face_map, Span< float3 > vert_normals, Span< float3 > face_normals, Span< bool > sharp_faces, MutableSpan< bool > sharp_edges, MutableSpan< float3 > r_custom_corner_normals, MutableSpan< short2 > r_clnors_data)
std::variant< EdgeUninitialized, EdgeOneCorner, EdgeTwoCorners, EdgeSharp > VertEdgeInfo
void normals_calc_faces(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, MutableSpan< float3 > face_normals)
static void normalize_vecs(MutableSpan< float3 > normals)
void mesh_set_custom_normals_normalized(Mesh &mesh, MutableSpan< float3 > corner_normals)
void mesh_set_custom_normals_from_verts(Mesh &mesh, MutableSpan< float3 > vert_normals)
void mesh_vert_normals_assign(Mesh &mesh, Span< float3 > vert_normals)
void mesh_set_custom_normals_from_verts_normalized(Mesh &mesh, MutableSpan< float3 > vert_normals)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
constexpr ParallelGrainSize grain_size(int grain_size)
const MultiFunction & lookup(UString id)
VecBase< T, 3 > normal_tri(const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3)
float safe_acos_approx(float x)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
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.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BLI_memarena_merge(MemArena *ma_dst, MemArena *ma_src)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BKE_lnor_spacearr_tls_join(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
MINLINE void mul_v3_fl(float r[3], float f)
void BKE_lnor_spacearr_tls_init(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpaceArray *lnors_spacearr_tls)
Global G
Definition blender.cc:57
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
VecBase< int32_t, 2 > int2
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
float normal_quad_v3(float n[3], const float v1[3], const float v2[3], const float v3[3], const float v4[3])
Definition math_geom.cc:62
void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr)
void BLI_linklist_prepend_nlink(LinkNode **listp, void *ptr, LinkNode *nlink) ATTR_NONNULL(1
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
MINLINE void copy_v2_v2_short(short r[2], const short a[2])
void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
void BKE_lnor_space_add_loop(MLoopNorSpaceArray *lnors_spacearr, MLoopNorSpace *lnor_space, int corner, void *bm_loop, bool is_single)
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
MINLINE float normalize_v3(float n[3])
void BKE_lnor_spacearr_free(MLoopNorSpaceArray *lnors_spacearr)
@ MLNOR_SPACE_IS_SINGLE
Definition BKE_mesh.h:288
@ MLNOR_SPACEARR_LOOP_INDEX
Definition BKE_mesh.h:309
@ 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
VecBase< int16_t, 2 > short2
PythonProbingStrategy<> DefaultProbingStrategy
MINLINE void add_newell_cross_v3_v3v3(float n[3], const float v_prev[3], const float v_curr[3])
void BKE_lnor_space_custom_data_to_normal(const MLoopNorSpace *lnor_space, const short clnor_data[2], float r_custom_lnor[3])
bool BKE_mesh_vert_normals_are_dirty(const Mesh *mesh)
VecBase< float, 3 > float3
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void * BLI_memarena_calloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float unit_short_to_float(const short val)
bool BKE_mesh_face_normals_are_dirty(const Mesh *mesh)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
MLoopNorSpace * BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr)
MINLINE short unit_float_to_short(const float val)
#define floorf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct LinkNode * loops_pool
Definition BKE_mesh.h:298
struct MemArena * mem
Definition BKE_mesh.h:303
MLoopNorSpace ** lspacearr
Definition BKE_mesh.h:296
struct LinkNode * loops
Definition BKE_mesh.h:281
bke::MeshRuntime * runtime
Vector< Array< int > > corners_by_space
Definition BKE_mesh.hh:177
Vector< CornerNormalSpace > spaces
Definition BKE_mesh.hh:165