Blender V5.0
bmo_join_triangles.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
13
14#include <algorithm>
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_heap.h"
19#include "BLI_math_base.h"
20#include "BLI_math_geom.h"
21#include "BLI_math_rotation.h"
22#include "BLI_math_vector.h"
23
24#include "BKE_customdata.hh"
25
26#include "bmesh.hh"
27
28#include "intern/bmesh_operators_private.hh" /* own include */
29
33#define ASSERT_VALID_ERROR_METRIC(val) BLI_assert(isfinite(val) && (val) >= 0 && (val) <= 2 * M_PI)
34
35#if 0
78# define USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
79#endif
80
81#define FACE_OUT (1 << 0)
82#define FACE_INPUT (1 << 2)
83
92constexpr float maximum_improvement = 0.99f;
93
94/* -------------------------------------------------------------------- */
98
102
108
111
114
117
118#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
120 BMesh *debug_bm;
121
126 int debug_merge_limit;
127
129 int debug_merge_count;
130
138 int debug_neighbor;
139
144 int debug_neighbor_global_count;
145
151 bool debug_this_step;
152#endif
153};
154
156/* -------------------------------------------------------------------- */
157
171static float quad_calc_error(const float v1[3],
172 const float v2[3],
173 const float v3[3],
174 const float v4[3])
175{
176 float error = 0.0f;
177
178 /* Normal difference: a perfectly flat planar face adds a difference of zero. */
179 {
180 float n1[3], n2[3];
181 float angle_a, angle_b;
182 float diff;
183
184 normal_tri_v3(n1, v1, v2, v3);
185 normal_tri_v3(n2, v1, v3, v4);
186 angle_a = compare_v3v3(n1, n2, FLT_EPSILON) ? 0.0f : angle_normalized_v3v3(n1, n2);
187
188 normal_tri_v3(n1, v2, v3, v4);
189 normal_tri_v3(n2, v4, v1, v2);
190 angle_b = compare_v3v3(n1, n2, FLT_EPSILON) ? 0.0f : angle_normalized_v3v3(n1, n2);
191
192 diff = (angle_a + angle_b) / float(M_PI * 2);
193
195
196 error += diff;
197 }
198
199 /* Co-linearity: a face with four right angle corners adds a difference of zero. */
200 {
201 float edge_vecs[4][3];
202 float diff;
203
204 sub_v3_v3v3(edge_vecs[0], v1, v2);
205 sub_v3_v3v3(edge_vecs[1], v2, v3);
206 sub_v3_v3v3(edge_vecs[2], v3, v4);
207 sub_v3_v3v3(edge_vecs[3], v4, v1);
208
209 normalize_v3(edge_vecs[0]);
210 normalize_v3(edge_vecs[1]);
211 normalize_v3(edge_vecs[2]);
212 normalize_v3(edge_vecs[3]);
213
214 /* A completely skinny face is 'pi' after halving. */
215 diff = (fabsf(angle_normalized_v3v3(edge_vecs[0], edge_vecs[1]) - float(M_PI_2)) +
216 fabsf(angle_normalized_v3v3(edge_vecs[1], edge_vecs[2]) - float(M_PI_2)) +
217 fabsf(angle_normalized_v3v3(edge_vecs[2], edge_vecs[3]) - float(M_PI_2)) +
218 fabsf(angle_normalized_v3v3(edge_vecs[3], edge_vecs[0]) - float(M_PI_2))) /
219 float(M_PI * 2);
220
222
223 error += diff;
224 }
225
226 /* Concavity: a face with no concavity adds an error of 0. */
227 {
228 float area_min, area_max, area_a, area_b;
229 float diff;
230
231 area_a = area_tri_v3(v1, v2, v3) + area_tri_v3(v1, v3, v4);
232 area_b = area_tri_v3(v2, v3, v4) + area_tri_v3(v4, v1, v2);
233
234 area_min = min_ff(area_a, area_b);
235 area_max = max_ff(area_a, area_b);
236
237 /* Note use of ternary operator to guard against divide by zero. */
238 diff = area_max ? (1.0f - (area_min / area_max)) : 1.0f;
239
241
242 error += diff;
243 }
244
246
247 return error;
248}
249
256static void bm_edge_to_quad_verts(const BMEdge *e, const BMVert *r_v_quad[4])
257{
259 BLI_assert(e->l->f->len == 3 && e->l->radial_next->f->len == 3);
260 r_v_quad[0] = e->l->v;
261 r_v_quad[1] = e->l->radial_next->prev->v;
262 r_v_quad[2] = e->l->next->v;
263 r_v_quad[3] = e->l->prev->v;
264}
265
266/* -------------------------------------------------------------------- */
269
271
272namespace {
273
274struct DelimitData_CD {
275 int cd_type;
276 int cd_size;
277 int cd_offset;
278 int cd_offset_end;
279};
280
281struct DelimitData {
282 uint do_seam : 1;
283 uint do_sharp : 1;
284 uint do_mat : 1;
285 uint do_angle_face : 1;
286 uint do_angle_shape : 1;
287
288 float angle_face;
289 float angle_face__cos;
290
291 float angle_shape;
292
293 DelimitData_CD cdata[4];
294 int cdata_len;
295};
296
297} // namespace
298
300static bool bm_edge_is_contiguous_loop_cd_all(const BMEdge *e, const DelimitData_CD *delimit_data)
301{
302 int cd_offset;
303 for (cd_offset = delimit_data->cd_offset; cd_offset < delimit_data->cd_offset_end;
304 cd_offset += delimit_data->cd_size)
305 {
306 if (BM_edge_is_contiguous_loop_cd(e, delimit_data->cd_type, cd_offset) == false) {
307 return false;
308 }
309 }
310
311 return true;
312}
313
316 eCustomDataType type,
317 DelimitData_CD *r_delim_cd)
318{
319 const int layer_len = CustomData_number_of_layers(ldata, type);
320 r_delim_cd->cd_type = type;
321 r_delim_cd->cd_size = CustomData_sizeof(eCustomDataType(r_delim_cd->cd_type));
322 r_delim_cd->cd_offset = CustomData_get_n_offset(ldata, type, 0);
323 r_delim_cd->cd_offset_end = r_delim_cd->cd_offset + (r_delim_cd->cd_size * layer_len);
324 return (r_delim_cd->cd_offset != -1);
325}
326
334{
335 DelimitData delimit_data = {0};
336 delimit_data.do_seam = BMO_slot_bool_get(op->slots_in, "cmp_seam");
337 delimit_data.do_sharp = BMO_slot_bool_get(op->slots_in, "cmp_sharp");
338 delimit_data.do_mat = BMO_slot_bool_get(op->slots_in, "cmp_materials");
339
340 /* Determine if angle face processing occurs and its parameters. */
341 float angle_face = BMO_slot_float_get(op->slots_in, "angle_face_threshold");
342 if (angle_face < DEG2RADF(180.0f)) {
343 delimit_data.angle_face = angle_face;
344 delimit_data.angle_face__cos = cosf(angle_face);
345 delimit_data.do_angle_face = true;
346 }
347 else {
348 delimit_data.do_angle_face = false;
349 }
350
351 /* Determine if angle shape processing occurs and its parameters. */
352 float angle_shape = BMO_slot_float_get(op->slots_in, "angle_shape_threshold");
353 if (angle_shape < DEG2RADF(180.0f)) {
354 delimit_data.angle_shape = angle_shape;
355 delimit_data.do_angle_shape = true;
356 }
357 else {
358 delimit_data.do_angle_shape = false;
359 }
360
361 if (BMO_slot_bool_get(op->slots_in, "cmp_uvs") &&
363 &bm->ldata, CD_PROP_FLOAT2, &delimit_data.cdata[delimit_data.cdata_len]))
364 {
365 delimit_data.cdata_len += 1;
366 }
367
368 delimit_data.cdata[delimit_data.cdata_len].cd_offset = -1;
369 if (BMO_slot_bool_get(op->slots_in, "cmp_vcols") &&
371 &bm->ldata, CD_PROP_BYTE_COLOR, &delimit_data.cdata[delimit_data.cdata_len]))
372 {
373 delimit_data.cdata_len += 1;
374 }
375 return delimit_data;
376}
377
385static bool bm_edge_is_delimit(const BMEdge *e, const DelimitData *delimit_data)
386{
387 BMFace *f_a = e->l->f, *f_b = e->l->radial_next->f;
388#if 0
389 const bool is_contig = BM_edge_is_contiguous(e);
390 float angle;
391#endif
392
393 if (delimit_data->do_seam && BM_elem_flag_test(e, BM_ELEM_SEAM)) {
394 return true;
395 }
396
397 if (delimit_data->do_sharp && (BM_elem_flag_test(e, BM_ELEM_SMOOTH) == 0)) {
398 return true;
399 }
400
401 if (delimit_data->do_mat && (f_a->mat_nr != f_b->mat_nr)) {
402 return true;
403 }
404
405 if (delimit_data->do_angle_face) {
406 if (dot_v3v3(f_a->no, f_b->no) < delimit_data->angle_face__cos) {
407 return true;
408 }
409 }
410
411 if (delimit_data->do_angle_shape) {
412 const BMVert *verts[4];
414
415 /* if we're checking the shape at all, a flipped face is out of the question */
416 if (is_quad_flip_v3(verts[0]->co, verts[1]->co, verts[2]->co, verts[3]->co)) {
417 return true;
418 }
419
420 float edge_vecs[4][3];
421
422 sub_v3_v3v3(edge_vecs[0], verts[0]->co, verts[1]->co);
423 sub_v3_v3v3(edge_vecs[1], verts[1]->co, verts[2]->co);
424 sub_v3_v3v3(edge_vecs[2], verts[2]->co, verts[3]->co);
425 sub_v3_v3v3(edge_vecs[3], verts[3]->co, verts[0]->co);
426
427 normalize_v3(edge_vecs[0]);
428 normalize_v3(edge_vecs[1]);
429 normalize_v3(edge_vecs[2]);
430 normalize_v3(edge_vecs[3]);
431
432 if ((fabsf(angle_normalized_v3v3(edge_vecs[0], edge_vecs[1]) - float(M_PI_2)) >
433 delimit_data->angle_shape) ||
434 (fabsf(angle_normalized_v3v3(edge_vecs[1], edge_vecs[2]) - float(M_PI_2)) >
435 delimit_data->angle_shape) ||
436 (fabsf(angle_normalized_v3v3(edge_vecs[2], edge_vecs[3]) - float(M_PI_2)) >
437 delimit_data->angle_shape) ||
438 (fabsf(angle_normalized_v3v3(edge_vecs[3], edge_vecs[0]) - float(M_PI_2)) >
439 delimit_data->angle_shape))
440 {
441 return true;
442 }
443 }
444
445 if (delimit_data->cdata_len) {
446 int i;
447 for (i = 0; i < delimit_data->cdata_len; i++) {
448 if (!bm_edge_is_contiguous_loop_cd_all(e, &delimit_data->cdata[i])) {
449 return true;
450 }
451 }
452 }
453
454 return false;
455}
456
458
463
475
490{
491 BLI_assert(neighbor_info.items_num < ARRAY_SIZE(neighbor_info.items));
492
493 /* Don't add null pointers. Another safeguard which cannot happen. */
494 BLI_assert(e != nullptr);
495
496 /* Don't add duplicates. */
497 for (uint index = 0; index < neighbor_info.items_num; index++) {
498 if (neighbor_info.items[neighbor_info.items_num].e == e) {
499 return;
500 }
501 }
502
503 /* Add the edge and increase the count by 1. */
504 JoinEdgesNeighborItem *item = &neighbor_info.items[neighbor_info.items_num++];
505 item->e = e;
506 item->l = l;
507}
508
517static void add_neighbors(JoinEdgesNeighborInfo &neighbor_info, BMLoop *l_in_quad)
518{
519 /* If the edge is not manifold, there is no neighboring face to process. */
520 if (!BM_edge_is_manifold(l_in_quad->e)) {
521 /* No new edges added. */
522 return;
523 }
524
525 BMLoop *l_in_neighbor = l_in_quad->radial_next;
526
527 /* If the neighboring face is not a triangle, don't process it. */
528 if (l_in_neighbor->f->len != 3) {
529 /* No new edges added. */
530 return;
531 }
532
533#ifndef NDEBUG
534 const int items_num_prev = neighbor_info.items_num;
535#endif
536
537 /* Get the other two loops of the neighboring triangle. */
538 BMLoop *l_other_arr[2] = {l_in_neighbor->prev, l_in_neighbor->next};
539 for (int i = 0; i < ARRAY_SIZE(l_other_arr); i++) {
540 BMLoop *l_other = l_other_arr[i];
541
542 /* If `l_other` is manifold, and the adjacent face is also a triangle,
543 * mark it for potential improvement. */
544 if (BM_edge_is_manifold(l_other->e) && l_other->radial_next->f->len == 3) {
545 add_without_duplicates(neighbor_info, l_other->e, l_in_neighbor);
546 }
547 }
548
549 /* Added either 0, 1, or 2 edges. */
550#ifndef NDEBUG
551 BLI_assert(neighbor_info.items_num - items_num_prev < 3);
552#endif
553}
554
565static void rotate_to_plane(const JoinEdgesState &s,
566 const BMVert *quad_verts[4],
567 const BMLoop *l_shared,
568 const float plane_normal[3],
569 float r_quad_coordinates[4][3])
570{
571#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
572 if (s.debug_this_step) {
573 printf("angle");
574 }
575#else
576 UNUSED_VARS(s);
577#endif
578
579 float rotation_axis[3];
580 sub_v3_v3v3(rotation_axis, l_shared->v->co, l_shared->next->v->co);
581 normalize_v3(rotation_axis);
582
583 float quad_normal[3] = {0};
585 quad_normal, quad_verts[0]->co, quad_verts[1]->co, quad_verts[2]->co, quad_verts[3]->co);
586
587 float angle = angle_signed_on_axis_v3v3_v3(plane_normal, quad_normal, rotation_axis);
588
589#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
590 if (s.debug_this_step) {
591 printf(" %f, ", RAD2DEGF(angle));
592 }
593#endif
594
595 for (int i = 0; i < 4; i++) {
596 if (ELEM(quad_verts[i], l_shared->v, l_shared->next->v)) {
597 /* Two coordinates of the quad match the vector that defines the axis of rotation, so they
598 * don't change. */
599 copy_v3_v3(r_quad_coordinates[i], quad_verts[i]->co);
600 }
601 else {
602 /* The other two coordinates get rotated around the axis, and so they change. */
603 float local_coordinate[3];
604 sub_v3_v3v3(local_coordinate, quad_verts[i]->co, l_shared->v->co);
605 rotate_normalized_v3_v3v3fl(r_quad_coordinates[i], local_coordinate, rotation_axis, angle);
606 add_v3_v3(r_quad_coordinates[i], l_shared->v->co);
607 }
608 }
609}
610
642static float compute_alignment(const JoinEdgesState &s,
643 const float quad_a_vecs[4][3],
644 const BMVert *quad_b_verts[4],
645 const BMLoop *l_shared,
646 const float plane_normal[3])
647{
648 /* Many meshes have lots of curvature or sharp edges. Pairs of quads shouldn't be penalized
649 * *worse* because they represent a curved surface or define an edge. So we rotate quad_b around
650 * its common edge with quad_a until both are, as much as possible, in the same plane.
651 * This ensures the best possible chance to align. */
652 float quad_b_coordinates[4][3];
653 rotate_to_plane(s, quad_b_verts, l_shared, plane_normal, quad_b_coordinates);
654
655#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
656 if (s.debug_this_step) {
657 /* For visualization purposes *only*, rotate the face being considered.
658 * The const_cast here is purposeful. We want to specify `const BMVert` deliberately
659 * to show that we're not *supposed* to be moving verts around.
660 * But only for debug visualization, we do. This alters the mesh to visualize the
661 * effect of rotating the face into the plane for alignment testing. */
662 copy_v3_v3(const_cast<BMVert *>(quad_b_verts[0])->co, quad_b_coordinates[0]);
663 copy_v3_v3(const_cast<BMVert *>(quad_b_verts[1])->co, quad_b_coordinates[1]);
664 copy_v3_v3(const_cast<BMVert *>(quad_b_verts[2])->co, quad_b_coordinates[2]);
665 copy_v3_v3(const_cast<BMVert *>(quad_b_verts[3])->co, quad_b_coordinates[3]);
666 }
667#endif
668
669 /* compute the four unit vectors of the quad b edges. */
670 float quad_b_vecs[4][3];
671 sub_v3_v3v3(quad_b_vecs[0], quad_b_coordinates[0], quad_b_coordinates[1]);
672 sub_v3_v3v3(quad_b_vecs[1], quad_b_coordinates[1], quad_b_coordinates[2]);
673 sub_v3_v3v3(quad_b_vecs[2], quad_b_coordinates[2], quad_b_coordinates[3]);
674 sub_v3_v3v3(quad_b_vecs[3], quad_b_coordinates[3], quad_b_coordinates[0]);
675 normalize_v3(quad_b_vecs[0]);
676 normalize_v3(quad_b_vecs[1]);
677 normalize_v3(quad_b_vecs[2]);
678 normalize_v3(quad_b_vecs[3]);
679
680 /* Given that we're not certain of how the first loop of the quad and the first loop
681 * of the proposed merge quad relate to each other, there are four possible combinations
682 * to check, to test that the neighbor face and the merged face have good alignment.
683 *
684 * In theory, a very nuanced analysis involving l_shared, loop pointers, vertex pointers,
685 * etc, would allow determining which sets of vectors are the right matches sets to compare.
686 *
687 * Do not meddle in the affairs of algorithms, for they are subtle and quick to anger.
688 *
689 * Instead, this code does the math twice, then it just flips each component by 180 degrees to
690 * pick up the other two cases. Four extra angle tests aren't that much worse than optimal.
691 * Brute forcing the math and ending up with clear and understandable code is better. */
692
693 float error[4] = {0.0f};
694 for (int i = 0; i < ARRAY_SIZE(error); i++) {
695 const float angle_a = fabsf(angle_normalized_v3v3(quad_a_vecs[i], quad_b_vecs[i]));
696 const float angle_b = fabsf(angle_normalized_v3v3(quad_a_vecs[i], quad_b_vecs[(i + 1) % 4]));
697
698 /* Compute the case if the quads are aligned. */
699 error[0] += angle_a;
700 /* Compute the case if the quads are 90 degrees rotated. */
701 error[1] += angle_b;
702
703 /* Compute the case if the quads are 180 degrees rotated.
704 * This is `error[0]` except each error component is individually rotated 180 degrees. */
705 error[2] += M_PI - angle_a;
706
707 /* Compute the case if the quads are 270 degrees rotated.
708 * This is `error[1]` except each error component is individually rotated 180 degrees. */
709 error[3] += M_PI - angle_b;
710 }
711
712 /* Pick the best option and average the four components. */
713 const float best_error = std::min({error[0], error[1], error[2], error[3]}) / 4.0f;
714
715 ASSERT_VALID_ERROR_METRIC(best_error);
716
717 /* Based on the best error, we scale how aligned we are to the range 0...1
718 * `M_PI / 4` is used here because the worst case is a quad with all four edges
719 * at 45 degree angles. */
720 float alignment = 1.0f - (best_error / (M_PI / 4.0f));
721
722 /* if alignment is *truly* awful, then do nothing. Don't make a join worse. */
723 alignment = std::max(alignment, 0.0f);
724
725 ASSERT_VALID_ERROR_METRIC(alignment);
726
727 return alignment;
728}
729
760 BMEdge *e_merge,
761 BMLoop *l_shared,
762 float neighbor_quad_vecs[4][3],
763 const float neighbor_quad_error,
764 const float neighbor_quad_normal[3])
765{
766 ASSERT_VALID_ERROR_METRIC(neighbor_quad_error);
767
768 /* If the edge wasn't found, (delimit, non-manifold, etc) then return.
769 * Nothing to do here. */
770 BLI_assert(BM_elem_index_get(e_merge) >= 0);
771 HeapNode *node = s.edge_queue_nodes[BM_elem_index_get(e_merge)];
772 if (node == nullptr) {
773 return;
774 }
775
776 float join_error_curr = BLI_heap_node_value(node);
777
778 ASSERT_VALID_ERROR_METRIC(join_error_curr);
779
780 /* Never make a join *worse* because of topology around it.
781 * Because we are sorted during the join phase of the algorithm, this should *only* happen when
782 * processing any pre-existing quads in the input mesh during setup. They might have high error.
783 * If they do, ignore them. */
784 if (neighbor_quad_error > join_error_curr) {
785
786#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
787 /* This should only happen during setup.
788 * Indicates an error, if it happens once we've started merging. */
789 BLI_assert(s.debug_merge_count == 0);
790#endif
791
792 return;
793 }
794
795#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
796 if (s.debug_this_step) {
797 printf("Edge improved from ");
798 }
799#endif
800
801 /* Get the four corners of the quad that would result if we merged. */
802 const BMVert *quad_verts_merge[4];
803 bm_edge_to_quad_verts(e_merge, quad_verts_merge);
804
805 /* Now compute the alignment.
806 * Regular grids of rectangles or trapezoids have high alignment
807 * Mismatched combinations of rectangles diamonds and trapezoids have low alignment. */
808 const float alignment = compute_alignment(
809 s, neighbor_quad_vecs, quad_verts_merge, l_shared, neighbor_quad_normal);
810
811 /* Compute how much the neighbor is better than the candidate.
812 * Since the neighbor quad error is smaller, Improvement is always represented as negative. */
813 const float improvement = (neighbor_quad_error - join_error_curr);
814
815 ASSERT_VALID_ERROR_METRIC(-improvement);
816
817 /* Compute the scale factor for how much of that possible improvement we should apply to this
818 * edge. This combines... topology_influence, which is an operator setting... and alignment,
819 * which is computed. Faces which are diagonal have an alignment of 0% - perfect rectangular
820 * grids have an alignment of 100% Neither topology_influence nor alignment can be negative;
821 * therefore the multiplier *never* makes error worse. once combined, 0 means no improvement, 1
822 * means improve all the way to exactly match the quality of the contributing neighbor.
823 * topology_influece is allowed to exceed 1.0, which lets it cancel out some of the alignment
824 * penalty. */
825 float multiplier = s.topo_influnce * alignment;
826
827 /* However, the combined multiplier shouldn't ever be allowed to exceed 1.0 because permitting
828 * that would cause exponential growth when alignment is very good, and when that happens, the
829 * algorithm becomes crazy.
830 *
831 * Further, if we allow a multiplier of exactly 1.0, then all eight edges around the neighbor
832 * quad would end up with a quality that is *exactly* equal to the neighbor - and each other;
833 * losing valuable information about their relative sorting.
834 * In order to preserve that, the multiplier is capped at 99%.
835 * The last 1% that is left uncorrected is enough to preserve relative ordering.
836 *
837 * This especially helps in quads that touch 3-poles and 5-poles. Since those quads naturally
838 * have diamond shapes, their initial error values tend to be higher and they sort to the end of
839 * the priority queue. Limiting improvement at 99% ensures those quads tend to retain their bad
840 * sort, meaning they end up surrounded by quads that define a good grid,
841 * then they merge last, which tends to produce better results. */
842 multiplier = std::min(multiplier, maximum_improvement);
843
844 ASSERT_VALID_ERROR_METRIC(multiplier);
845
846 /* improvement is always represented as a negative number (that will reduce error)
847 * Based on that convention, `+` is correct here. */
848 float join_error_next = join_error_curr + (improvement * multiplier);
849
850 ASSERT_VALID_ERROR_METRIC(join_error_next);
851
852#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
853 if (s.debug_this_step) {
854 printf("%f to %f with alignment of %f.\n", join_error_curr, join_error_next, alignment);
855 BMO_face_flag_enable(s.debug_bm, e_merge->l->f, FACE_OUT);
856 BMO_face_flag_enable(s.debug_bm, e_merge->l->radial_next->f, FACE_OUT);
857 }
858#endif
859
860 /* Now, update the node value in the heap, which may cause the node
861 * to be moved toward the head of the priority queue. */
862 BLI_heap_node_value_update(s.edge_queue, node, join_error_next);
863}
864
872static void reprioritize_face_neighbors(JoinEdgesState &s, BMFace *f, float f_error)
873{
874 BLI_assert(f->len == 4);
875
876 /* Identify any mergeable edges of any neighbor triangles that face us.
877 * - Some of our four edges... might not be manifold.
878 * - Some of our neighbor faces... might not be triangles.
879 * - Some of our neighbor triangles... might have other non-manifold (unmergeable) edges.
880 * - Some of our neighbor triangles' manifold edges... might have non-triangle neighbors.
881 * Therefore, there can be have up to eight mergeable edges, although there are often fewer. */
882 JoinEdgesNeighborInfo neighbor_info = {};
883
884 /* Get the four loops around the face. */
885 BMLoop *l_quad[4];
887
888 /* Add the mergeable neighbors for each of those loops. */
889 for (int i = 0; i < ARRAY_SIZE(l_quad); i++) {
890 add_neighbors(neighbor_info, l_quad[i]);
891 }
892
893 /* Return if there is nothing to do. */
894 if (neighbor_info.items_num == 0) {
895 return;
896 }
897
898 /* Compute the four unit vectors around this quad. */
899 float quad_vecs[4][3];
900 for (int i_next = 0, i = ARRAY_SIZE(l_quad) - 1; i_next < ARRAY_SIZE(l_quad); i = i_next++) {
901 sub_v3_v3v3(quad_vecs[i], l_quad[i]->v->co, l_quad[i_next]->v->co);
902 normalize_v3(quad_vecs[i]);
903 }
904
905 /* Re-prioritize each neighbor. */
906 for (int i = 0; i < neighbor_info.items_num; i++) {
907#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
908 s.debug_this_step = (s.debug_merge_limit > 0 && s.debug_merge_count == s.debug_merge_limit &&
909 i + 1 == s.debug_neighbor) ||
910 (++s.debug_neighbor_global_count == s.debug_neighbor &&
911 s.debug_merge_limit == 0);
912#endif
913 const JoinEdgesNeighborItem *item = &neighbor_info.items[i];
914 reprioritize_join(s, item->e, item->l, quad_vecs, f_error, f->no);
915 }
916}
917
925 BMEdge *e
926#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
927 ,
929#endif
930)
931{
932 /* Non-manifold edges can't be merged. */
934
935 /* Identify the loops on either side of the edge which may be joined. */
936 BMLoop *l_a = e->l;
937 BMLoop *l_b = e->l->radial_next;
938
939 /* If previous face merges have created quads, which now make this edge unmergeable,
940 * then skip it and move on. This happens frequently and that's ok.
941 * It's much easier and more efficient to just skip these edges when we encounter them,
942 * than it is to try to search the heap for them and remove them preemptively. */
943 if ((l_a->f->len != 3) || (l_b->f->len != 3)) {
944 return nullptr;
945 }
946
947#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
948 /* Stop doing merges in the middle of processing if we reached a user limit.
949 * This is allowed so a developer can check steps in the process of the algorithm. */
950 if (++s.debug_merge_count > s.debug_merge_limit && s.debug_merge_limit != -1) {
951 return nullptr;
952 }
953#endif
954
955 BMFace *f_double;
956
957 /* Join the edge and identify the face. */
958 BMFace *f = BM_faces_join_pair(bm, l_a, l_b, true, &f_double);
959 /* See #BM_faces_join note on callers asserting when `r_double` is non-null. */
960 BLI_assert_msg(f_double == nullptr,
961 "Doubled face detected at " AT ". Resulting mesh may be corrupt.");
962
963 return f;
964}
965
968{
969 BMIter iter;
970 BMOIter siter;
971 BMFace *f;
972 BMEdge *e;
973
974 DelimitData delimit_data = bm_edge_delmimit_data_from_op(bm, op);
975
976 /* Initial setup of state. */
977 JoinEdgesState s = {nullptr};
978 s.topo_influnce = BMO_slot_float_get(op->slots_in, "topology_influence");
979 s.use_topo_influence = (s.topo_influnce != 0.0f);
981 s.select_tris_only = BMO_slot_bool_get(op->slots_in, "deselect_joined");
982 if (s.use_topo_influence) {
983 s.edge_queue_nodes = MEM_malloc_arrayN<HeapNode *>(bm->totedge, __func__);
984 }
985
986#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
987 s.debug_bm = bm;
988 s.debug_merge_limit = BMO_slot_int_get(op->slots_in, "merge_limit") - 1;
989 s.debug_neighbor = BMO_slot_int_get(op->slots_in, "neighbor_debug");
990 s.debug_merge_count = 0;
991 s.debug_neighbor_global_count = 0;
992#endif
993
994 /* Go through every face in the input slot. Mark triangles for processing. */
995 BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
996 if (f->len == 3) {
998
999 /* And setup the initial selection. */
1000 if (s.select_tris_only) {
1002 }
1003 }
1004 }
1005
1006 /* Go through every edge in the mesh, mark edges that can be merged. */
1007 int i = 0;
1009 BM_elem_index_set(e, i); /* set_inline */
1010
1011 /* If the edge is manifold, has a tagged input triangle on both sides,
1012 * and is *not* delimited, then it's a candidate to merge. */
1013 BMFace *f_a, *f_b;
1014 if (BM_edge_face_pair(e, &f_a, &f_b) && BMO_face_flag_test(bm, f_a, FACE_INPUT) &&
1015 BMO_face_flag_test(bm, f_b, FACE_INPUT) && !bm_edge_is_delimit(e, &delimit_data))
1016 {
1017 /* Compute the error that would result from a merge. */
1018 const BMVert *e_verts[4];
1019 bm_edge_to_quad_verts(e, e_verts);
1020 const float merge_error = quad_calc_error(
1021 e_verts[0]->co, e_verts[1]->co, e_verts[2]->co, e_verts[3]->co);
1022
1023 /* Record the candidate merge in both the heap, and the heap index. */
1024 HeapNode *node = BLI_heap_insert(s.edge_queue, merge_error, e);
1025 if (s.use_topo_influence) {
1026 s.edge_queue_nodes[i] = node;
1027 }
1028 }
1029 else {
1030 if (s.use_topo_influence) {
1031 s.edge_queue_nodes[i] = nullptr;
1032 }
1033 }
1034 }
1035
1036 /* Go through all the faces of the input slot, this time to find quads.
1037 * Improve the candidates around any preexisting quads in the mesh.
1038 *
1039 * NOTE: This unfortunately misses any quads which are not selected, but
1040 * which neighbor the selection. The only alternate would be to iterate the
1041 * whole mesh, which might be expensive for very large meshes with small selections. */
1042 if (s.use_topo_influence && (BLI_heap_is_empty(s.edge_queue) == false)) {
1043 BMO_ITER (f, &siter, op->slots_in, "faces", BM_FACE) {
1044 if (f->len == 4) {
1045 BMVert *f_verts[4];
1046 BM_face_as_array_vert_quad(f, f_verts);
1047
1048 /* Flat quads with right angle corners and no concavity have lower error. */
1049 float f_error = quad_calc_error(
1050 f_verts[0]->co, f_verts[1]->co, f_verts[2]->co, f_verts[3]->co);
1051
1052 /* Apply the compensated error.
1053 * Since we're early in the process we over-prioritize any already existing quads to
1054 * allow them to have an especially strong influence on the resulting mesh.
1055 * At a topology influence of 200%, they're considered to be *almost perfect* quads
1056 * regardless of their actual error. Either way, the multiplier is never completely
1057 * allowed to reach zero. Instead, 1% of the original error is preserved...
1058 * which is enough to maintain the relative priority sorting between existing quads. */
1059 f_error *= (2.0f - (s.topo_influnce * maximum_improvement));
1060
1061 reprioritize_face_neighbors(s, f, f_error);
1062 }
1063 }
1064 }
1065
1066 /* Process all possible merges. */
1067 while (!BLI_heap_is_empty(s.edge_queue)) {
1068
1069 /* Get the best merge from the priority queue.
1070 * Remove it from the priority queue. */
1071 const float f_error = BLI_heap_top_value(s.edge_queue);
1072 BMEdge *e = reinterpret_cast<BMEdge *>(BLI_heap_pop_min(s.edge_queue));
1073
1074 /* Attempt the merge. */
1076 e
1077#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
1078 ,
1079 s
1080#endif
1081 );
1082
1083#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
1084 /* If stopping partway through, clear the selection entirely, and instead
1085 * highlight the faces being considered in the step the user is checking. */
1086 if (s.debug_merge_limit != -1 && s.debug_merge_count == s.debug_merge_limit) {
1087 BMEdge *f;
1088 BMIter iter;
1089 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
1091 }
1092 }
1093#endif
1094
1095 if (f_new) {
1096
1097 /* Tag the face so the selection can be extended to include the new face. */
1098 if (s.select_tris_only == false) {
1100 }
1101
1102 /* Improve the neighbors on success. */
1103 if (s.use_topo_influence) {
1104 reprioritize_face_neighbors(s, f_new, f_error);
1105 }
1106 }
1107
1108#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
1109 /* If we're supposed to stop partway though, do that.
1110 * This allows a developer to inspect the mesh at intermediate stages of processing. */
1111 if (s.debug_merge_limit != -1 && s.debug_merge_count >= s.debug_merge_limit) {
1112 break;
1113 }
1114#endif
1115 }
1116
1117#ifdef USE_JOIN_TRIANGLE_INTERACTIVE_TESTING
1118 /* Expect a full processing to have occurred, *only* if we didn't stop partway through. */
1119 if (!(s.debug_merge_limit != -1 && s.debug_merge_count >= s.debug_merge_limit))
1120#endif
1121 {
1122 /* Expect a full processing to have occurred. */
1124 }
1125
1126 /* Clean up. */
1127 BLI_heap_free(s.edge_queue, nullptr);
1128 if (s.use_topo_influence) {
1130 }
1131
1132 /* Return the selection results. */
1134}
CustomData interface, see also DNA_customdata_types.h.
int CustomData_sizeof(eCustomDataType type)
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
A min-heap / priority queue ADT.
void BLI_heap_free(Heap *heap, HeapFreeFP ptrfreefp) ATTR_NONNULL(1)
Definition BLI_heap.cc:191
void void float BLI_heap_node_value(const HeapNode *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.cc:347
void void bool BLI_heap_is_empty(const Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.cc:269
float BLI_heap_top_value(const Heap *heap) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition BLI_heap.cc:284
void BLI_heap_node_value_update(Heap *heap, HeapNode *node, float value) ATTR_NONNULL(1
void * BLI_heap_pop_min(Heap *heap) ATTR_NONNULL(1)
Definition BLI_heap.cc:291
HeapNode * BLI_heap_insert(Heap *heap, float value, void *ptr) ATTR_NONNULL(1)
Definition BLI_heap.cc:234
Heap * BLI_heap_new(void) ATTR_WARN_UNUSED_RESULT
Definition BLI_heap.cc:186
MINLINE float max_ff(float a, float b)
MINLINE float min_ff(float a, float b)
#define DEG2RADF(_deg)
#define M_PI_2
#define RAD2DEGF(_rad)
#define M_PI
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:58
int is_quad_flip_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
float area_tri_v3(const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:100
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:41
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void rotate_normalized_v3_v3v3fl(float out[3], const float p[3], const float axis[3], float angle)
float angle_signed_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float normalize_v3(float n[3])
unsigned int uint
#define ARRAY_SIZE(arr)
#define UNUSED_VARS(...)
#define ELEM(...)
#define AT
@ CD_PROP_BYTE_COLOR
@ CD_PROP_FLOAT2
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:117
Read Guarded memory(de)allocation.
@ BM_ELEM_SEAM
@ BM_ELEM_SMOOTH
#define BM_elem_index_get(ele)
#define BM_elem_index_set(ele, index)
#define BM_elem_flag_test(ele, hflag)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_INDEX(ele, iter, bm, itype, indexvar)
@ BM_EDGES_OF_MESH
@ BM_FACES_OF_MESH
BMesh * bm
BMFace * BM_faces_join_pair(BMesh *bm, BMLoop *l_a, BMLoop *l_b, const bool do_del, BMFace **r_double)
Faces Join Pair.
#define BM_FACE
float BMO_slot_float_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BMO_slot_buffer_from_enabled_flag(BMesh *bm, BMOperator *op, BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name, char htype, short oflag)
#define BMO_face_flag_enable(bm, e, oflag)
#define BMO_ITER(ele, iter, slot_args, slot_name, restrict_flag)
int BMO_slot_int_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
#define BMO_face_flag_disable(bm, e, oflag)
#define BMO_face_flag_test(bm, e, oflag)
bool BMO_slot_bool_get(BMOpSlot slot_args[BMO_OP_MAX_SLOTS], const char *slot_name)
void BM_face_as_array_vert_quad(BMFace *f, BMVert *r_verts[4])
void BM_face_as_array_loop_quad(BMFace *f, BMLoop *r_loops[4])
bool BM_edge_is_contiguous_loop_cd(const BMEdge *e, const int cd_loop_type, const int cd_loop_offset)
bool BM_edge_face_pair(BMEdge *e, BMFace **r_fa, BMFace **r_fb)
BLI_INLINE bool BM_edge_is_contiguous(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMLoop * l_b
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define FACE_OUT
Definition bmo_bridge.cc:28
static bool bm_edge_is_delimit(const BMEdge *e, const DelimitData *delimit_data)
static void add_neighbors(JoinEdgesNeighborInfo &neighbor_info, BMLoop *l_in_quad)
#define ASSERT_VALID_ERROR_METRIC(val)
static DelimitData bm_edge_delmimit_data_from_op(BMesh *bm, BMOperator *op)
static void reprioritize_join(JoinEdgesState &s, BMEdge *e_merge, BMLoop *l_shared, float neighbor_quad_vecs[4][3], const float neighbor_quad_error, const float neighbor_quad_normal[3])
void bmo_join_triangles_exec(BMesh *bm, BMOperator *op)
static void add_without_duplicates(JoinEdgesNeighborInfo &neighbor_info, BMEdge *e, BMLoop *l)
static void bm_edge_to_quad_verts(const BMEdge *e, const BMVert *r_v_quad[4])
static bool bm_edge_is_contiguous_loop_cd_all(const BMEdge *e, const DelimitData_CD *delimit_data)
static void reprioritize_face_neighbors(JoinEdgesState &s, BMFace *f, float f_error)
static BMFace * bm_faces_join_pair_by_edge(BMesh *bm, BMEdge *e)
#define FACE_INPUT
static float compute_alignment(const JoinEdgesState &s, const float quad_a_vecs[4][3], const BMVert *quad_b_verts[4], const BMLoop *l_shared, const float plane_normal[3])
static float quad_calc_error(const float v1[3], const float v2[3], const float v3[3], const float v4[3])
static void rotate_to_plane(const JoinEdgesState &s, const BMVert *quad_verts[4], const BMLoop *l_shared, const float plane_normal[3], float r_quad_coordinates[4][3])
static bool bm_edge_delimit_cdata(CustomData *ldata, eCustomDataType type, DelimitData_CD *r_delim_cd)
constexpr float maximum_improvement
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
static float verts[][3]
#define printf(...)
void * MEM_malloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:133
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
static void error(const char *str)
#define fabsf
#define cosf
struct BMLoop * l
short mat_nr
float no[3]
struct BMVert * v
struct BMEdge * e
struct BMLoop * radial_next
struct BMLoop * prev
struct BMFace * f
struct BMLoop * next
struct BMOpSlot slots_out[BMO_OP_MAX_SLOTS]
struct BMOpSlot slots_in[BMO_OP_MAX_SLOTS]
float co[3]
JoinEdgesNeighborItem items[8]
HeapNode ** edge_queue_nodes
i
Definition text_draw.cc:230