Blender V5.3
bmesh_bevel.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
10
11#include <algorithm>
12
13#include "MEM_guardedalloc.h"
14
16#include "DNA_meshdata_types.h"
17#include "DNA_modifier_types.h"
18
19#include "BLI_alloca.hh"
20#include "BLI_map.hh"
21#include "BLI_math_base_c.hh"
22#include "BLI_math_base_safe.hh"
23#include "BLI_math_geom_c.hh"
24#include "BLI_math_matrix_c.hh"
26#include "BLI_math_vector_c.hh"
27#include "BLI_memarena.hh"
28#include "BLI_set.hh"
29#include "BLI_utildefines.hh"
30#include "BLI_vector.hh"
31
32#include "BKE_curveprofile.h"
33#include "BKE_customdata.hh"
34#include "BKE_deform.hh"
35#include "BKE_mesh.hh"
36#include "BKE_mesh_mapping.hh"
37
38#include "eigen_capi.h"
39
40#include "bmesh.hh"
41#include "bmesh_bevel.hh" /* own include */
42
44
45namespace blender {
46
47// #define BEVEL_DEBUG_TIME
48#ifdef BEVEL_DEBUG_TIME
49# include "BLI_time.hh"
50#endif
51
52#define BEVEL_EPSILON_D 1e-6
53#define BEVEL_EPSILON 1e-6f
54#define BEVEL_EPSILON_SQ 1e-12f
55#define BEVEL_EPSILON_BIG 1e-4f
56#define BEVEL_EPSILON_BIG_SQ 1e-8f
57#define BEVEL_EPSILON_ANG DEG2RADF(2.0f)
58#define BEVEL_SMALL_ANG DEG2RADF(10.0f)
60#define BEVEL_SMALL_ANG_DOT (1.0f - cosf(BEVEL_SMALL_ANG))
62#define BEVEL_EPSILON_ANG_DOT (1.0f - cosf(BEVEL_EPSILON_ANG))
63#define BEVEL_MAX_ADJUST_PCT 10.0f
64#define BEVEL_MAX_AUTO_ADJUST_PCT 300.0f
65#define BEVEL_MATCH_SPEC_WEIGHT 0.2
66
67// #define DEBUG_CUSTOM_PROFILE_CUTOFF
68/* Happens far too often, uncomment for development. */
69// #define BEVEL_ASSERT_PROJECT
70
71/* for testing */
72// #pragma GCC diagnostic error "-Wpadded"
73
74/* Constructed vertex, sometimes later instantiated as BMVert. */
75struct NewVert {
77 float co[3];
78 char _pad[4];
79};
80
81struct BoundVert;
82
83/* Data for one end of an edge involved in a bevel. */
119
134struct Profile {
136 float super_r;
138 float height;
140 float start[3];
142 float middle[3];
144 float end[3];
146 float plane_no[3];
148 float plane_co[3];
150 float proj_dir[3];
152 float *prof_co;
154 float *prof_co_2;
157};
158#define PRO_SQUARE_R 1e4f
159#define PRO_CIRCLE_R 2.0f
160#define PRO_LINE_R 1.0f
161#define PRO_SQUARE_IN_R 0.0f
162
170 double *xvals;
172 double *yvals;
174 double *xvals_2;
176 double *yvals_2;
178 int seg_2;
180 float fullness;
181};
182
202
215
255
257 M_NONE, /* No polygon mesh needed. */
258 M_POLY, /* A simple polygon. */
259 M_ADJ, /* "Adjacent edges" mesh pattern. */
260 M_TRI_FAN, /* A simple polygon - fan filled. */
261 M_CUTOFF, /* A triangulated face at the end of each profile. */
262};
263
279
280/* Data for a vertex involved in a bevel. */
304
321
331
334
337
426
427// #pragma GCC diagnostic ignored "-Wpadded"
428
429/* -------------------------------------------------------------------- */
432
433// #define BEVEL_DEBUG
434#ifdef BEVEL_DEBUG
435
436namespace debug {
437
438/* Ensures vert, edge, and face indices are up to date so that #vi, #ei, and #fi
439 * return readable sequential values. Call this once before any dump function. */
440[[maybe_unused]] static void ensure_indices(BMesh *bm)
441{
443}
444
445/* Prints a float3 array as "(x,y,z)" with no trailing newline. */
446[[maybe_unused]] static void print_float3(const float v[3])
447{
448 fmt::print("({},{},{})", v[0], v[1], v[2]);
449}
450
451/* Returns the index of a #BMVert, or -1 if null. */
452[[maybe_unused]] static int vi(const BMVert *v)
453{
454 return v ? BM_elem_index_get(v) : -1;
455}
456
457/* Returns the index of a #BMEdge, or -1 if null. */
458[[maybe_unused]] static int ei(const BMEdge *e)
459{
460 return e ? BM_elem_index_get(e) : -1;
461}
462
463/* Returns the index of a #BMFace, or -1 if null. */
464[[maybe_unused]] static int fi(const BMFace *f)
465{
466 return f ? BM_elem_index_get(f) : -1;
467}
468
469/* Returns a human-readable name for a #MeshKind value. */
470[[maybe_unused]] static const char *mesh_kind_name(MeshKind kind)
471{
472 switch (kind) {
473 case M_NONE:
474 return "NONE";
475 case M_POLY:
476 return "POLY";
477 case M_ADJ:
478 return "ADJ";
479 case M_TRI_FAN:
480 return "TRI_FAN";
481 case M_CUTOFF:
482 return "CUTOFF";
483 default:
484 return "?";
485 }
486}
487
488/* Prints a single #Profile's key parameters. */
489[[maybe_unused]] static void dump_profile(const Profile &prof)
490{
491 fmt::print(" Profile: super_r={} height={} special_params={}\n",
492 prof.super_r,
493 prof.height,
494 prof.special_params);
495 fmt::print(" start=");
496 print_float3(prof.start);
497 fmt::print(" middle=");
498 print_float3(prof.middle);
499 fmt::print(" end=");
500 print_float3(prof.end);
501 fmt::println("");
502 fmt::print(" plane_no=");
503 print_float3(prof.plane_no);
504 fmt::print(" plane_co=");
505 print_float3(prof.plane_co);
506 fmt::print(" proj_dir=");
507 print_float3(prof.proj_dir);
508 fmt::println("");
509}
510
511/* Prints a single #EdgeHalf's fields.
512 * BMesh elements are identified by their index (retrieved with #BM_elem_index_get). */
513[[maybe_unused]] static void dump_edge_half(const EdgeHalf &eh, const int index)
514{
515 fmt::println(
516 " EdgeHalf[{}]: e={} fprev={} fnext={}", index, ei(eh.e), fi(eh.fprev), fi(eh.fnext));
517 fmt::println(" offset_l={} offset_r={} offset_l_spec={} offset_r_spec={}",
518 eh.offset_l,
519 eh.offset_r,
520 eh.offset_l_spec,
521 eh.offset_r_spec);
522 fmt::println(" is_bev={} is_rev={} is_seam={} visited_rpo={}",
523 eh.is_bev,
524 eh.is_rev,
525 eh.is_seam,
526 eh.visited_rpo);
527 fmt::println(" leftv={} rightv={}",
528 eh.leftv ? eh.leftv->index : -1,
529 eh.rightv ? eh.rightv->index : -1);
530}
531
532/* Prints a single #BoundVert's fields.
533 * BMesh elements are identified by their index (retrieved with #BM_elem_index_get). */
534[[maybe_unused]] static void dump_bound_vert(const BoundVert &bndv)
535{
536 fmt::print(" BoundVert[{}]: co=", bndv.index);
537 print_float3(bndv.nv.co);
538 fmt::println("");
539 fmt::println(" efirst={} elast={} eon={} ebev={}",
540 bndv.efirst ? ei(bndv.efirst->e) : -1,
541 bndv.elast ? ei(bndv.elast->e) : -1,
542 bndv.eon ? ei(bndv.eon->e) : -1,
543 bndv.ebev ? ei(bndv.ebev->e) : -1);
544 fmt::println(
545 " sinratio={} any_seam={} visited={}", bndv.sinratio, bndv.any_seam, bndv.visited);
546 fmt::println(" is_arc_start={} is_patch_start={} is_profile_start={}",
547 bndv.is_arc_start,
548 bndv.is_patch_start,
549 bndv.is_profile_start);
550 fmt::println(" seam_len={} sharp_len={}", bndv.seam_len, bndv.sharp_len);
551 dump_profile(bndv.profile);
552}
553
554/* Prints a #VMesh and all its #BoundVert chain. */
555[[maybe_unused]] static void dump_vmesh(const VMesh &vm)
556{
557 fmt::println(
558 " VMesh: count={} seg={} mesh_kind={}", vm.count, vm.seg, mesh_kind_name(vm.mesh_kind));
559 if (vm.boundstart == nullptr) {
560 fmt::println(" (no boundverts)");
561 return;
562 }
563 /* Walk the circular linked list of BoundVerts. */
564 const BoundVert *bndv = vm.boundstart;
565 do {
566 dump_bound_vert(*bndv);
567 bndv = bndv->next;
568 } while (bndv != vm.boundstart);
569
570 /* Print the NewVert grid if it has been allocated. */
571 if (vm.mesh != nullptr) {
572 const int n = vm.count;
573 const int ns = vm.seg;
574 const int ns2 = ns / 2;
575 /* Non-const pointer needed by mesh_vert (accessor is not const-qualified). */
576 VMesh *vmp = const_cast<VMesh *>(&vm);
577 fmt::println(" NewVerts (i, j, k) for 0<=i<{} 0<=j<={} 0<=k<{}:", n, ns2, ns);
578 for (int i = 0; i < n; i++) {
579 for (int j = 0; j <= ns2; j++) {
580 fmt::print(" ({},{}): ", i, j);
581 for (int k = 0; k < ns; k++) {
582 const int nj = ns / 2 + 1;
583 const int nk = ns + 1;
584 const NewVert *nv = &vmp->mesh[i * nj * nk + j * nk + k];
585 fmt::print("({},({:.3f},{:.3f},{:.3f})) ",
586 nv->v ? BM_elem_index_get(nv->v) : -1,
587 nv->co[0],
588 nv->co[1],
589 nv->co[2]);
590 }
591 fmt::println("");
592 }
593 }
594 }
595}
596
597/* Dumps a full #BevVert, including its #EdgeHalf array, wire edges, and #VMesh.
598 * BMesh elements are identified by their index (retrieved with #BM_elem_index_get). */
599[[maybe_unused]] static void dump_bev_vert(const BevVert &bv)
600{
601 fmt::println("BevVert: v={} edgecount={} selcount={} wirecount={}",
602 vi(bv.v),
603 bv.edgecount,
604 bv.selcount,
605 bv.wirecount);
606 fmt::println(" offset={} any_seam={} visited={}", bv.offset, bv.any_seam, bv.visited);
607
608 /* Print the EdgeHalf array. */
609 fmt::println(" edges ({}):", bv.edgecount);
610 for (int i = 0; i < bv.edgecount; i++) {
611 dump_edge_half(bv.edges[i], i);
612 }
613
614 /* Print wire edges. */
615 if (bv.wirecount > 0) {
616 fmt::print(" wire_edges:");
617 for (int i = 0; i < bv.wirecount; i++) {
618 if (i % 10 == 0) {
619 fmt::print("\n[{}] ", i);
620 }
621 fmt::print("{} ", ei(bv.wire_edges[i]));
622 }
623 fmt::println("");
624 }
625
626 /* Print the VMesh if present. */
627 if (bv.vmesh) {
628 dump_vmesh(*bv.vmesh);
629 }
630 else {
631 fmt::println(" (no vmesh)");
632 }
633}
634
646[[maybe_unused]] static void dump_edge_polygons(const BevVert &bv)
647{
648 const VMesh *vm = bv.vmesh;
649 if (!vm) {
650 return;
651 }
652 const int ns = vm->seg;
653 VMesh *vmp = const_cast<VMesh *>(vm);
654 /* Non-const pointer needed by mesh_vert. */
655
656 fmt::println(" Edge polygons for bv={}:", vi(bv.v));
657
658 for (int eidx = 0; eidx < bv.edgecount; eidx++) {
659 const EdgeHalf &eh = bv.edges[eidx];
660 if (!eh.is_bev || !eh.leftv) {
661 continue;
662 }
663 const int i = eh.leftv->index;
664 fmt::println(" EdgeHalf[{}] e={} i={}:", eidx, ei(eh.e), i);
665
666 for (int k = 0; k < ns; k++) {
667 /* The two consecutive boundary ring verts on this endpoint. */
668 const int nj = ns / 2 + 1;
669 const int nk = ns + 1;
670 const NewVert *nv_a = &vmp->mesh[i * nj * nk + 0 * nk + k];
671 const NewVert *nv_b = &vmp->mesh[i * nj * nk + 0 * nk + (k + 1)];
672 BMVert *va = nv_a->v;
673 BMVert *vb = nv_b->v;
674 if (!va || !vb) {
675 continue;
676 }
677 /* Find the BMEdge connecting va and vb. */
678 BMEdge *bme = BM_edge_exists(va, vb);
679 if (!bme) {
680 fmt::println(" k={} va={} vb={}: no edge found", k, vi(va), vi(vb));
681 continue;
682 }
683 /* Walk the radial faces of this edge to find the F_EDGE face
684 * (the one that is not an original input face, i.e., not fprev/fnext). */
685 BMFace *f_edge = nullptr;
686 BMLoop *l = bme->l;
687 if (l) {
688 BMLoop *l_start = l;
689 do {
690 /* Pick the face that shares neither fprev nor fnext with eh. */
691 BMFace *f = l->f;
692 if (f != eh.fprev && f != eh.fnext) {
693 f_edge = f;
694 break;
695 }
696 l = l->radial_next;
697 } while (l != l_start);
698 }
699 if (!f_edge) {
700 /* If not found by exclusion, just pick any radial face. */
701 f_edge = bme->l ? bme->l->f : nullptr;
702 }
703 if (!f_edge) {
704 fmt::println(" k={} va={} vb={}: no face found", k, vi(va), vi(vb));
705 continue;
706 }
707 /* Dump the face corners. */
708 fmt::print(" k={} face={} corners:", k, fi(f_edge));
709 BMIter liter;
710 BMLoop *l_dump;
711 BM_ITER_ELEM (l_dump, &liter, f_edge, BM_LOOPS_OF_FACE) {
712 fmt::print(" [v={} e={}]", vi(l_dump->v), ei(l_dump->e));
713 }
714 fmt::println("");
715 }
716 }
717}
718
723[[maybe_unused]] static void dump_rebuilt_face(BMFace *f)
724{
725 if (!f) {
726 fmt::println(" dump_rebuilt_face: face is null");
727 return;
728 }
729 fmt::print(" Rebuilt face={} ({} verts):", fi(f), f->len);
730 BMIter liter;
731 BMLoop *l;
732 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
733 fmt::print(" [v={} e={} co=({:.3f},{:.3f},{:.3f})]",
734 vi(l->v),
735 ei(l->e),
736 l->v->co[0],
737 l->v->co[1],
738 l->v->co[2]);
739 }
740 fmt::println("");
741}
742
743} /* namespace debug */
744
745#endif /* BEVEL_DEBUG */
746
748
749/* Use the unused _BM_ELEM_TAG_ALT flag to flag the 'long' loops (parallel to beveled edge)
750 * of edge-polygons. */
751#define BM_ELEM_LONG_TAG (1 << 6)
752
753/* These flag values will get set on geom we want to return in 'out' slots for edges and verts. */
754#define EDGE_OUT 4
755#define VERT_OUT 8
756
757/* If we're called from the modifier, tool flags aren't available,
758 * but don't need output geometry. */
759static void flag_out_edge(BMesh *bm, BMEdge *bme)
760{
761 if (bm->use_toolflags) {
763 }
764}
765
766static void flag_out_vert(BMesh *bm, BMVert *bmv)
767{
768 if (bm->use_toolflags) {
770 }
771}
772
774{
775 if (bm->use_toolflags) {
777 }
778}
779
780static void record_face_kind(BevelParams *bp, BMFace *f, FKind fkind)
781{
782 if (bp->face_hash) {
783 bp->face_hash->add(f, fkind);
784 }
785}
786
788{
789 return bp->face_hash->lookup_default(f, F_ORIG);
790}
791
792/* Are d1 and d2 parallel or nearly so? */
793static bool nearly_parallel(const float d1[3], const float d2[3])
794{
795 float ang = angle_v3v3(d1, d2);
796
797 return (fabsf(ang) < BEVEL_EPSILON_ANG) || (fabsf(ang - float(M_PI)) < BEVEL_EPSILON_ANG);
798}
799
803static bool nearly_parallel_normalized(const float d1[3], const float d2[3])
804{
807
808 const float direction_dot = dot_v3v3(d1, d2);
809 return compare_ff(fabsf(direction_dot), 1.0f, BEVEL_EPSILON_ANG_DOT);
810}
811
812/* Make a new BoundVert of the given kind, inserting it at the end of the circular linked
813 * list with entry point bv->boundstart, and return it. */
814static BoundVert *add_new_bound_vert(MemArena *mem_arena, VMesh *vm, const float co[3])
815{
816 BoundVert *ans = static_cast<BoundVert *>(BLI_memarena_alloc(mem_arena, sizeof(BoundVert)));
817
818 copy_v3_v3(ans->nv.co, co);
819 if (!vm->boundstart) {
820 ans->index = 0;
821 vm->boundstart = ans;
822 ans->next = ans->prev = ans;
823 }
824 else {
825 BoundVert *tail = vm->boundstart->prev;
826 ans->index = tail->index + 1;
827 ans->prev = tail;
828 ans->next = vm->boundstart;
829 tail->next = ans;
830 vm->boundstart->prev = ans;
831 }
833 ans->adjchain = nullptr;
834 ans->sinratio = 1.0f;
835 ans->visited = false;
836 ans->any_seam = false;
837 ans->is_arc_start = false;
838 ans->is_patch_start = false;
839 ans->is_profile_start = false;
840 vm->count++;
841 return ans;
842}
843
844BLI_INLINE void adjust_bound_vert(BoundVert *bv, const float co[3])
845{
846 copy_v3_v3(bv->nv.co, co);
847}
848
849/* Mesh verts are indexed (i, j, k) where
850 * i = boundvert index (0 <= i < nv)
851 * j = ring index (0 <= j <= ns2)
852 * k = segment index (0 <= k <= ns)
853 * Not all of these are used, and some will share BMVerts. */
854static NewVert *mesh_vert(VMesh *vm, int i, int j, int k)
855{
856 int nj = (vm->seg / 2) + 1;
857 int nk = vm->seg + 1;
858
859 return &vm->mesh[i * nk * nj + j * nk + k];
860}
861
862static void create_mesh_bmvert(BMesh *bm, VMesh *vm, int i, int j, int k, BMVert *eg)
863{
864 NewVert *nv = mesh_vert(vm, i, j, k);
865 nv->v = BM_vert_create(bm, nv->co, eg, BM_CREATE_NOP);
867 flag_out_vert(bm, nv->v);
868}
869
870static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto, int ifrom, int jfrom, int kfrom)
871{
872 NewVert *nvto = mesh_vert(vm, ito, jto, kto);
873 NewVert *nvfrom = mesh_vert(vm, ifrom, jfrom, kfrom);
874 nvto->v = nvfrom->v;
875 copy_v3_v3(nvto->co, nvfrom->co);
876}
877
878/* Find the EdgeHalf in bv's array that has edge bme. */
880{
881 for (int i = 0; i < bv->edgecount; i++) {
882 if (bv->edges[i].e == bme) {
883 return &bv->edges[i];
884 }
885 }
886 return nullptr;
887}
888
889/* Find the BevVert corresponding to BMVert bmv. */
891{
892 return bp->vert_hash.lookup_default(bmv, nullptr);
893}
894
895/* Find the `UVFace` corresponding to `bmf` face. */
897{
898 return bp->uv_face_hash.lookup_default(bmf, nullptr);
899}
900
907{
908 BevVert *bvo = find_bevvert(bp, e->is_rev ? e->e->v1 : e->e->v2);
909 if (bvo) {
910 if (r_bvother) {
911 *r_bvother = bvo;
912 }
913 EdgeHalf *eother = find_edge_half(bvo, e->e);
914 BLI_assert(eother != nullptr);
915 return eother;
916 }
917 if (r_bvother) {
918 *r_bvother = nullptr;
919 }
920 return nullptr;
921}
922
923/* Return the next EdgeHalf after from_e that is beveled.
924 * If from_e is nullptr, find the first beveled edge. */
925static EdgeHalf *next_bev(BevVert *bv, EdgeHalf *from_e)
926{
927 if (from_e == nullptr) {
928 from_e = &bv->edges[bv->edgecount - 1];
929 }
930 EdgeHalf *e = from_e;
931 do {
932 if (e->is_bev) {
933 return e;
934 }
935 } while ((e = e->next) != from_e);
936 return nullptr;
937}
938
939/* Return the count of edges between e1 and e2 when going around bv CCW. */
941{
942 int count = 0;
943 EdgeHalf *e = e1;
944
945 do {
946 if (e == e2) {
947 break;
948 }
949 e = e->next;
950 count++;
951 } while (e != e1);
952 return count;
953}
954
955/* Assume bme1 and bme2 both share some vert. Do they share a face?
956 * If they share a face then there is some loop around bme1 that is in a face
957 * where the next or previous edge in the face must be bme2. */
959{
960 BMIter iter;
961 BMLoop *l;
962 BM_ITER_ELEM (l, &iter, bme1, BM_LOOPS_OF_EDGE) {
963 if (l->prev->e == bme2 || l->next->e == bme2) {
964 return true;
965 }
966 }
967 return false;
968}
969
974static UVFace *register_uv_face(BevelParams *bp, BMFace *fnew, BMFace *frep, BMFace **frep_arr)
975{
976 if (!fnew) {
977 return nullptr;
978 }
979
980 UVFace *uv_face = static_cast<UVFace *>(BLI_memarena_alloc(bp->mem_arena, sizeof(UVFace)));
981 uv_face->f = fnew;
982 uv_face->attached_frep = nullptr;
983 if (frep_arr && frep_arr[0]) {
984 /* Choosing first face from `frep_arr` is an arbitrary choice but for our algorithm it doesn't
985 * matter. Usually the difference in `frep` and `frep_arr` is that the latter is used when
986 * loops' custom data for a new bevel face is interpolated between multiple original mesh faces
987 * on top of which this new bevel face is being constructed; in such case original faces
988 * _should_ be already connected in UV space (i.e. no seam) and we handle such scenarios when
989 * setting proper value for `is_orig_uv_verts_connected` variable. */
990 uv_face->attached_frep = frep_arr[0];
991 }
992 else if (frep) {
993 uv_face->attached_frep = frep;
994 }
995
996 bp->uv_face_hash.add(fnew, uv_face);
997 return uv_face;
998}
999
1006 UVFace *uv_face,
1007 BMVert *bv,
1008 Map<BMVert *, BMVert *> *nv_bv_map)
1009{
1010 if (!uv_face || !uv_face->attached_frep) {
1011 return;
1012 }
1013
1014 for (UVVertMap &uv_vert_map : bp->uv_vert_maps) {
1015 BMIter iter;
1016 BMLoop *l;
1017 BM_ITER_ELEM (l, &iter, uv_face->f, BM_LOOPS_OF_FACE) {
1018 Vector<UVVertBucket> *uv_vert_buckets = uv_vert_map.lookup_ptr(l->v);
1019 if (!uv_vert_buckets) {
1020 /* New vertex (and a corresponding loop) needs to be registered. No need for further UV
1021 * connectivity search, we just create a new bucket and move on. */
1022 uv_vert_map.add(l->v, Vector<UVVertBucket>{{l}});
1023 continue;
1024 }
1025
1026 /* `orig_v` should always point to a vertex which takes part in a bevel operation and comes
1027 * from the original mesh. This vertex is equivalent to what is stored in the `BevVert::v`
1028 * field. */
1029 BMVert *orig_v = nv_bv_map ? nv_bv_map->lookup(l->v) : bv;
1030 BMLoop *orig_l = BM_face_vert_share_loop(uv_face->attached_frep, orig_v);
1031 BLI_assert(orig_l != nullptr);
1032
1033 bool is_bucket_found = false;
1034 BMIter iter2;
1035 BMLoop *l2;
1036 BM_ITER_ELEM (l2, &iter2, l->v, BM_LOOPS_OF_VERT) {
1037 if (l == l2) {
1038 continue;
1039 }
1040
1041 UVFace *uv_face2 = find_uv_face(bp, l2->f);
1042 if (!uv_face2 || !uv_face2->attached_frep) {
1043 continue;
1044 }
1045
1046 BMLoop *orig_l2 = BM_face_vert_share_loop(uv_face2->attached_frep, orig_v);
1047 BLI_assert(orig_l2 != nullptr);
1048
1049 bool is_orig_uv_verts_connected = false;
1050 Vector<UVVertBucket> &orig_uv_vert_buckets = uv_vert_map.lookup(orig_v);
1051 for (UVVertBucket &orig_uv_vert_bucket : orig_uv_vert_buckets) {
1052 if (orig_uv_vert_bucket.contains(orig_l) && orig_uv_vert_bucket.contains(orig_l2)) {
1053 is_orig_uv_verts_connected = true;
1054 break;
1055 }
1056 }
1057
1058 /* Add loop `l` to the existing bucket containing neighboring loop `l2` if either of those
1059 * conditions are met:
1060 * 1. Neighboring faces (represented by `UVFace` objects) have the same representative face
1061 * attached to them.
1062 * 2. If representative faces attached to faces containing both loops (`l` and `l2`) are
1063 * different but otherwise connected in UV space (`orig_l` and `orig_l2` are
1064 * overlapping) for original input mesh. */
1065 if (uv_face->attached_frep == uv_face2->attached_frep || is_orig_uv_verts_connected) {
1066 for (UVVertBucket &uv_vert_bucket : *uv_vert_buckets) {
1067 if (uv_vert_bucket.contains(l2)) {
1068 uv_vert_bucket.add(l);
1069 is_bucket_found = true;
1070 break;
1071 }
1072 }
1073 }
1074 if (is_bucket_found) {
1075 break;
1076 }
1077 }
1078 if (!is_bucket_found) {
1079 uv_vert_buckets->append(UVVertBucket{l});
1080 }
1081 }
1082 }
1083}
1084
1094{
1095 int num_uv_layers = CustomData_number_of_layers(&bm->ldata, CD_PROP_FLOAT2);
1096 BLI_assert(bp->uv_vert_maps.size() == num_uv_layers);
1097
1098 for (int i = 0; i < num_uv_layers; ++i) {
1099 int uv_data_offset = CustomData_get_n_offset(&bm->ldata, CD_PROP_FLOAT2, i);
1100 Vector<UVVertBucket> uv_vert_buckets;
1101 BMIter iter;
1102 BMLoop *l;
1103 BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1104 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, uv_data_offset);
1105 bool is_overlap_found = false;
1106 for (UVVertBucket &uv_vert_bucket : uv_vert_buckets) {
1107 for (BMLoop *l2 : uv_vert_bucket) {
1108 float *luv2 = BM_ELEM_CD_GET_FLOAT_P(l2, uv_data_offset);
1109 if (compare_v2v2(luv, luv2, STD_UV_CONNECT_LIMIT)) {
1110 uv_vert_bucket.add(l);
1111 is_overlap_found = true;
1112 break;
1113 }
1114 }
1115 if (is_overlap_found) {
1116 break;
1117 }
1118 }
1119 if (!is_overlap_found) {
1120 uv_vert_buckets.append(UVVertBucket{l});
1121 }
1122 }
1123
1124 /* For now `determine_uv_vert_connectivity` is expected to be run at the beginning of the bevel
1125 * operation, determining connectivity for each vertex `v` (from the original mesh). We expect
1126 * `bp->uv_vert_maps[i]` to not contain vertex `v` at this point in time. */
1127 BLI_assert(bp->uv_vert_maps[i].contains(v) == false);
1128 bp->uv_vert_maps[i].add_new(v, uv_vert_buckets);
1129 }
1130}
1131
1138{
1139 int num_uv_layers = CustomData_number_of_layers(&bm->ldata, CD_PROP_FLOAT2);
1140 BLI_assert(bp->uv_vert_maps.size() == num_uv_layers);
1141
1142 for (int i = 0; i < num_uv_layers; ++i) {
1143 int uv_data_offset = CustomData_get_n_offset(&bm->ldata, CD_PROP_FLOAT2, i);
1144 for (Vector<UVVertBucket> &uv_vert_buckets : bp->uv_vert_maps[i].values()) {
1145 for (UVVertBucket &uv_vert_bucket : uv_vert_buckets) {
1146 int num_uv_verts = uv_vert_bucket.size();
1147 if (num_uv_verts <= 1) {
1148 continue;
1149 }
1150 float uv[2] = {0.0f, 0.0f};
1151 for (BMLoop *l : uv_vert_bucket) {
1152 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, uv_data_offset);
1153 add_v2_v2(uv, luv);
1154 }
1155 mul_v2_fl(uv, 1.0f / float(num_uv_verts));
1156 for (BMLoop *l : uv_vert_bucket) {
1157 float *luv = BM_ELEM_CD_GET_FLOAT_P(l, uv_data_offset);
1158 copy_v2_v2(luv, uv);
1159 }
1160 }
1161 }
1162 }
1163}
1164
1173{
1174 BMFace *frep;
1175
1176 BMFace *frep2 = nullptr;
1177 if (v->ebev) {
1178 frep = v->ebev->fprev;
1179 if (v->efirst->fprev != frep) {
1180 frep2 = v->efirst->fprev;
1181 }
1182 }
1183 else if (v->efirst) {
1184 frep = v->efirst->fprev;
1185 if (frep) {
1186 if (v->elast->fnext != frep) {
1187 frep2 = v->elast->fnext;
1188 }
1189 else if (v->efirst->fnext != frep) {
1190 frep2 = v->efirst->fnext;
1191 }
1192 else if (v->elast->fprev != frep) {
1193 frep2 = v->efirst->fprev;
1194 }
1195 }
1196 else if (v->efirst->fnext) {
1197 frep = v->efirst->fnext;
1198 if (v->elast->fnext != frep) {
1199 frep2 = v->elast->fnext;
1200 }
1201 }
1202 else if (v->elast->fprev) {
1203 frep = v->elast->fprev;
1204 }
1205 }
1206 else if (v->prev->elast) {
1207 frep = v->prev->elast->fnext;
1208 if (v->next->efirst) {
1209 if (frep) {
1210 frep2 = v->next->efirst->fprev;
1211 }
1212 else {
1213 frep = v->next->efirst->fprev;
1214 }
1215 }
1216 }
1217 else {
1218 frep = nullptr;
1219 }
1220 if (r_fother) {
1221 *r_fother = frep2;
1222 }
1223 return frep;
1224}
1225
1237 BMesh *bm,
1238 BMVert **vert_arr,
1239 const int totv,
1240 BMFace **face_arr,
1241 BMFace *facerep,
1242 BMEdge **snap_edge_arr,
1243 BMVert *bv,
1244 Map<BMVert *, BMVert *> *nv_bv_map,
1245 int mat_nr,
1246 bool do_interp)
1247{
1248 BMFace *f = BM_face_create_verts(bm, vert_arr, totv, facerep, BM_CREATE_NOP, true);
1249 if (!f) {
1250 return nullptr;
1251 }
1252
1253 if (facerep || (face_arr && face_arr[0])) {
1254 BM_elem_attrs_copy(bm, facerep ? facerep : face_arr[0], f);
1255 if (do_interp) {
1256 int i = 0;
1257 BMIter iter;
1258 BMLoop *l;
1259 BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
1260 BMFace *interp_f;
1261 if (face_arr) {
1262 /* Assume loops of created face are in same order as verts. */
1263 BLI_assert(l->v == vert_arr[i]);
1264 interp_f = face_arr[i];
1265 }
1266 else {
1267 interp_f = facerep;
1268 }
1269 if (interp_f) {
1270 BMEdge *bme = nullptr;
1271 if (snap_edge_arr) {
1272 bme = snap_edge_arr[i];
1273 }
1274 float save_co[3];
1275 if (bme) {
1276 copy_v3_v3(save_co, l->v->co);
1277 closest_to_line_segment_v3(l->v->co, save_co, bme->v1->co, bme->v2->co);
1278 }
1279 BM_loop_interp_from_face(bm, l, interp_f, true, true);
1280 if (bme) {
1281 copy_v3_v3(l->v->co, save_co);
1282 }
1283 }
1284 i++;
1285 }
1286 }
1287 }
1288
1290 BMIter iter;
1291 BMEdge *bme;
1292 BM_ITER_ELEM (bme, &iter, f, BM_EDGES_OF_FACE) {
1293 /* Not essential for bevels own internal logic, this is done so the operator can select newly
1294 * created geometry. */
1295 flag_out_edge(bm, bme);
1296 }
1297
1298 if (mat_nr >= 0) {
1299 f->mat_nr = short(mat_nr);
1300 }
1301
1302 UVFace *uv_face = register_uv_face(bp, f, facerep, face_arr);
1303 update_uv_vert_map(bp, uv_face, bv, nv_bv_map);
1304
1305 return f;
1306}
1307
1308/* Is Loop layer layer_index contiguous across shared vertex of l1 and l2? */
1309static bool contig_ldata_across_loops(BMesh *bm, BMLoop *l1, BMLoop *l2, int layer_index)
1310{
1311 const int offset = bm->ldata.layers[layer_index].offset;
1312 const int type = bm->ldata.layers[layer_index].type;
1313
1315 static_cast<char *>(l1->head.data) + offset,
1316 static_cast<char *>(l2->head.data) + offset);
1317}
1318
1319/* Are all loop layers with have math (e.g., UVs)
1320 * contiguous from face f1 to face f2 across edge e?
1321 */
1323{
1324 if (bm->ldata.totlayer == 0) {
1325 return true;
1326 }
1327
1328 BMLoop *lef1, *lef2;
1329 if (!BM_edge_loop_pair(e, &lef1, &lef2)) {
1330 return false;
1331 }
1332 /* If faces are oriented consistently around e,
1333 * should now have lef1 and lef2 being f1 and f2 in either order.
1334 */
1335 if (lef1->f == f2) {
1336 std::swap(lef1, lef2);
1337 }
1338 if (lef1->f != f1 || lef2->f != f2) {
1339 return false;
1340 }
1341 BMVert *v1 = lef1->v;
1342 BMVert *v2 = lef2->v;
1343 if (v1 == v2) {
1344 return false;
1345 }
1346 BLI_assert((v1 == e->v1 && v2 == e->v2) || (v1 == e->v2 && v2 == e->v1));
1348 BMLoop *lv1f1 = lef1;
1349 BMLoop *lv2f1 = lef1->next;
1350 BMLoop *lv1f2 = lef2->next;
1351 BMLoop *lv2f2 = lef2;
1352 BLI_assert(lv1f1->v == v1 && lv1f1->f == f1 && lv2f1->v == v2 && lv2f1->f == f1 &&
1353 lv1f2->v == v1 && lv1f2->f == f2 && lv2f2->v == v2 && lv2f2->f == f2);
1354 for (int i = 0; i < bm->ldata.totlayer; i++) {
1356 if (!contig_ldata_across_loops(bm, lv1f1, lv1f2, i) ||
1357 !contig_ldata_across_loops(bm, lv2f1, lv2f2, i))
1358 {
1359 return false;
1360 }
1361 }
1362 }
1363 return true;
1364}
1365
1366/* Are all loop layers that have math (e.g., UVs)
1367 * contiguous for all faces around vertex \a v?
1368 */
1370{
1371 if (bm->ldata.totlayer == 0) {
1372 return true;
1373 }
1374
1375 BMIter iter;
1376 BMLoop *l_first = nullptr;
1377 BMLoop *l;
1378
1379 BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1380 if (l_first == nullptr) {
1381 l_first = l;
1382 continue;
1383 }
1384 /* Check all math layers (UVs, etc). */
1385 for (int i = 0; i < bm->ldata.totlayer; i++) {
1387 if (!contig_ldata_across_loops(bm, l_first, l, i)) {
1388 return false;
1389 }
1390 }
1391 }
1392 }
1393
1394 return true;
1395}
1396
1401static void swap_face_components(int *face_component, int totface, int c1, int c2)
1402{
1403 if (c1 == c2) {
1404 return; /* Nothing to do. */
1405 }
1406 for (int f = 0; f < totface; f++) {
1407 if (face_component[f] == c1) {
1408 face_component[f] = c2;
1409 }
1410 else if (face_component[f] == c2) {
1411 face_component[f] = c1;
1412 }
1413 }
1414}
1415
1420{
1421 int num_uv_layers = CustomData_number_of_layers(&bm->ldata, CD_PROP_FLOAT2);
1422 bp->uv_vert_maps.clear();
1423 bp->uv_vert_maps.resize(num_uv_layers);
1424}
1425
1430{
1431 for (UVVertMap &uv_vert_map : bp->uv_vert_maps) {
1432 uv_vert_map.pop_try(v);
1433 }
1434}
1435
1436/*
1437 * Set up the fields of bp->math_layer_info.
1438 * We always set has_math_layers to the correct value.
1439 * Only if there are UV layers and the number of segments is odd,
1440 * we need to calculate connected face components in UV space.
1441 */
1443{
1444 int f;
1445 bp->math_layer_info.has_math_layers = false;
1446 bp->math_layer_info.face_component = nullptr;
1447 for (int i = 0; i < bm->ldata.totlayer; i++) {
1450 break;
1451 }
1452 }
1453 if (!bp->math_layer_info.has_math_layers || (bp->seg % 2) == 0) {
1454 return;
1455 }
1456
1459 int totface = bm->totface;
1460 int *face_component = static_cast<int *>(
1461 BLI_memarena_alloc(bp->mem_arena, sizeof(int) * totface));
1462 bp->math_layer_info.face_component = face_component;
1463
1464 /* Use an array as a stack. Stack size can't exceed total faces if keep track of what is in
1465 * stack. */
1466 BMFace **stack = MEM_new_array_uninitialized<BMFace *>(totface, __func__);
1467 bool *in_stack = MEM_new_array_uninitialized<bool>(totface, __func__);
1468
1469 /* Set all component ids by DFS from faces with unassigned components. */
1470 for (f = 0; f < totface; f++) {
1471 face_component[f] = -1;
1472 in_stack[f] = false;
1473 }
1474 int current_component = -1;
1475 for (f = 0; f < totface; f++) {
1476 if (face_component[f] == -1 && !in_stack[f]) {
1477 int stack_top = 0;
1478 current_component++;
1479 BLI_assert(stack_top < totface);
1480 stack[stack_top] = BM_face_at_index(bm, f);
1481 in_stack[f] = true;
1482 while (stack_top >= 0) {
1483 BMFace *bmf = stack[stack_top];
1484 stack_top--;
1485 int bmf_index = BM_elem_index_get(bmf);
1486 in_stack[bmf_index] = false;
1487 if (face_component[bmf_index] != -1) {
1488 continue;
1489 }
1490 face_component[bmf_index] = current_component;
1491 /* Neighbors are faces that share an edge with bmf and
1492 * are where contig_ldata_across_edge(...) is true for the
1493 * shared edge and two faces.
1494 */
1495 BMIter eiter;
1496 BMEdge *bme;
1497 BM_ITER_ELEM (bme, &eiter, bmf, BM_EDGES_OF_FACE) {
1498 BMIter fiter;
1499 BMFace *bmf_other;
1500 BM_ITER_ELEM (bmf_other, &fiter, bme, BM_FACES_OF_EDGE) {
1501 if (bmf_other != bmf) {
1502 int bmf_other_index = BM_elem_index_get(bmf_other);
1503 if (face_component[bmf_other_index] != -1 || in_stack[bmf_other_index]) {
1504 continue;
1505 }
1506 if (contig_ldata_across_edge(bm, bme, bmf, bmf_other)) {
1507 stack_top++;
1508 BLI_assert(stack_top < totface);
1509 stack[stack_top] = bmf_other;
1510 in_stack[bmf_other_index] = true;
1511 }
1512 }
1513 }
1514 }
1515 }
1516 }
1517 }
1518 MEM_delete(stack);
1519 MEM_delete(in_stack);
1520 /* We can usually get more pleasing result if components 0 and 1
1521 * are the topmost and bottom-most (in z-coordinate) components,
1522 * so adjust component indices to make that so. */
1523 if (current_component <= 0) {
1524 return; /* Only one component, so no need to do this. */
1525 }
1526 BMFace *top_face = nullptr;
1527 float top_face_z = -1e30f;
1528 int top_face_component = -1;
1529 BMFace *bot_face = nullptr;
1530 float bot_face_z = 1e30f;
1531 int bot_face_component = -1;
1532 for (f = 0; f < totface; f++) {
1533 float cent[3];
1534 BMFace *bmf = BM_face_at_index(bm, f);
1535 BM_face_calc_center_bounds(bmf, cent);
1536 float fz = cent[2];
1537 if (fz > top_face_z) {
1538 top_face_z = fz;
1539 top_face = bmf;
1540 top_face_component = face_component[f];
1541 }
1542 if (fz < bot_face_z) {
1543 bot_face_z = fz;
1544 bot_face = bmf;
1545 bot_face_component = face_component[f];
1546 }
1547 }
1548 BLI_assert(top_face != nullptr && bot_face != nullptr);
1549 UNUSED_VARS_NDEBUG(top_face, bot_face);
1550 swap_face_components(face_component, totface, face_component[0], top_face_component);
1551 if (bot_face_component != top_face_component) {
1552 if (bot_face_component == 0) {
1553 /* It was swapped with old top_face_component. */
1554 bot_face_component = top_face_component;
1555 }
1556 swap_face_components(face_component, totface, face_component[1], bot_face_component);
1557 }
1558}
1559
1572static BMFace *choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
1573{
1574#define VEC_VALUE_LEN 6
1575 float (*value_vecs)[VEC_VALUE_LEN] = nullptr;
1576 int num_viable = 0;
1577
1578 value_vecs = BLI_array_alloca(value_vecs, nfaces);
1579 bool *still_viable = BLI_array_alloca(still_viable, nfaces);
1580 for (int f = 0; f < nfaces; f++) {
1581 BMFace *bmf = face[f];
1582 if (bmf == nullptr) {
1583 still_viable[f] = false;
1584 continue;
1585 }
1586 still_viable[f] = true;
1587 num_viable++;
1588 int bmf_index = BM_elem_index_get(bmf);
1589 int value_index = 0;
1590 /* First tie-breaker: lower math-layer connected component id. */
1591 value_vecs[f][value_index++] = bp->math_layer_info.face_component ?
1592 float(bp->math_layer_info.face_component[bmf_index]) :
1593 0.0f;
1594 /* Next tie-breaker: selected face beats unselected one. */
1595 value_vecs[f][value_index++] = BM_elem_flag_test(bmf, BM_ELEM_SELECT) ? 0.0f : 1.0f;
1596 /* Next tie-breaker: lower material index. */
1597 value_vecs[f][value_index++] = bmf->mat_nr >= 0 ? float(bmf->mat_nr) : 0.0f;
1598 /* Next three tie-breakers: z, x, y components of face center. */
1599 float cent[3];
1600 BM_face_calc_center_bounds(bmf, cent);
1601 value_vecs[f][value_index++] = cent[2];
1602 value_vecs[f][value_index++] = cent[0];
1603 value_vecs[f][value_index++] = cent[1];
1604 BLI_assert(value_index == VEC_VALUE_LEN);
1605 }
1606
1607 /* Look for a face that has a unique minimum value for in a value_index,
1608 * trying each value_index in turn until find a unique minimum.
1609 */
1610 int best_f = -1;
1611 for (int value_index = 0; num_viable > 1 && value_index < VEC_VALUE_LEN; value_index++) {
1612 for (int f = 0; f < nfaces; f++) {
1613 if (!still_viable[f] || f == best_f) {
1614 continue;
1615 }
1616 if (best_f == -1) {
1617 best_f = f;
1618 continue;
1619 }
1620 if (value_vecs[f][value_index] < value_vecs[best_f][value_index]) {
1621 best_f = f;
1622 /* Previous f's are now not viable any more. */
1623 for (int i = f - 1; i >= 0; i--) {
1624 if (still_viable[i]) {
1625 still_viable[i] = false;
1626 num_viable--;
1627 }
1628 }
1629 }
1630 else if (value_vecs[f][value_index] > value_vecs[best_f][value_index]) {
1631 still_viable[f] = false;
1632 num_viable--;
1633 }
1634 }
1635 }
1636 if (best_f == -1) {
1637 best_f = 0;
1638 }
1639 return face[best_f];
1640#undef VEC_VALUE_LEN
1641}
1642
1643/* Calculate coordinates of a point a distance d from v on e->e and return it in slideco. */
1644static void slide_dist(EdgeHalf *e, BMVert *v, float d, float r_slideco[3])
1645{
1646 float dir[3];
1647 sub_v3_v3v3(dir, v->co, BM_edge_other_vert(e->e, v)->co);
1648 float len = normalize_v3(dir);
1649
1650 if (d > len) {
1651 d = len - float(50.0 * BEVEL_EPSILON_D);
1652 }
1653 copy_v3_v3(r_slideco, v->co);
1654 madd_v3_v3fl(r_slideco, dir, -d);
1655}
1656
1657/* Is co not on the edge e? If not, return the closer end of e in ret_closer_v. */
1658static bool is_outside_edge(EdgeHalf *e, const float co[3], BMVert **ret_closer_v)
1659{
1660 float h[3], u[3];
1661 float *l1 = e->e->v1->co;
1662
1663 sub_v3_v3v3(u, e->e->v2->co, l1);
1664 sub_v3_v3v3(h, co, l1);
1665 float lenu = normalize_v3(u);
1666 float lambda = dot_v3v3(u, h);
1667 if (lambda <= -BEVEL_EPSILON_BIG * lenu) {
1668 *ret_closer_v = e->e->v1;
1669 return true;
1670 }
1671 if (lambda >= (1.0f + BEVEL_EPSILON_BIG) * lenu) {
1672 *ret_closer_v = e->e->v2;
1673 return true;
1674 }
1675 return false;
1676}
1677
1678/* Return whether the angle is less than, equal to, or larger than 180 degrees. */
1680{
1681 BMVert *v1 = BM_edge_other_vert(e1->e, v);
1682 BMVert *v2 = BM_edge_other_vert(e2->e, v);
1683 float dir1[3], dir2[3];
1684 sub_v3_v3v3(dir1, v->co, v1->co);
1685 sub_v3_v3v3(dir2, v->co, v2->co);
1686 normalize_v3(dir1);
1687 normalize_v3(dir2);
1688
1689 /* First check for in-line edges using a simpler test. */
1690 if (nearly_parallel_normalized(dir1, dir2)) {
1691 return ANGLE_STRAIGHT;
1692 }
1693
1694 /* Angles are in [0,pi]. Need to compare cross product with normal to see if they are reflex. */
1695 float cross[3];
1696 cross_v3_v3v3(cross, dir1, dir2);
1698 float *no;
1699 if (e1->fnext) {
1700 no = e1->fnext->no;
1701 }
1702 else if (e2->fprev) {
1703 no = e2->fprev->no;
1704 }
1705 else {
1706 no = v->no;
1707 }
1708
1709 if (dot_v3v3(cross, no) < 0.0f) {
1710 return ANGLE_LARGER;
1711 }
1712 return ANGLE_SMALLER;
1713}
1714
1715/* co should be approximately on the plane between e1 and e2, which share common vert v and common
1716 * face f (which cannot be nullptr). Is it between those edges, sweeping CCW? */
1718 const float co[3], BMVert *v, BMFace *f, EdgeHalf *e1, EdgeHalf *e2)
1719{
1720 float dir1[3], dir2[3], dirco[3], no[3];
1721
1722 BMVert *v1 = BM_edge_other_vert(e1->e, v);
1723 BMVert *v2 = BM_edge_other_vert(e2->e, v);
1724 sub_v3_v3v3(dir1, v->co, v1->co);
1725 sub_v3_v3v3(dir2, v->co, v2->co);
1726 sub_v3_v3v3(dirco, v->co, co);
1727 normalize_v3(dir1);
1728 normalize_v3(dir2);
1729 normalize_v3(dirco);
1730 float ang11 = angle_normalized_v3v3(dir1, dir2);
1731 float ang1co = angle_normalized_v3v3(dir1, dirco);
1732 /* Angles are in [0,pi]. Need to compare cross product with normal to see if they are reflex. */
1733 cross_v3_v3v3(no, dir1, dir2);
1734 if (dot_v3v3(no, f->no) < 0.0f) {
1735 ang11 = float(M_PI * 2.0) - ang11;
1736 }
1737 cross_v3_v3v3(no, dir1, dirco);
1738 if (dot_v3v3(no, f->no) < 0.0f) {
1739 ang1co = float(M_PI * 2.0) - ang1co;
1740 }
1741 return (ang11 - ang1co > -BEVEL_EPSILON_ANG);
1742}
1743
1744/* Is the angle swept from e1 to e2, CCW when viewed from the normal side of f,
1745 * not a reflex angle or a straight angle? Assume e1 and e2 share a vert. */
1746static bool edge_edge_angle_less_than_180(const BMEdge *e1, const BMEdge *e2, const BMFace *f)
1747{
1748 float dir1[3], dir2[3], cross[3];
1749 BLI_assert(f != nullptr);
1750 BMVert *v, *v1, *v2;
1751 if (e1->v1 == e2->v1) {
1752 v = e1->v1;
1753 v1 = e1->v2;
1754 v2 = e2->v2;
1755 }
1756 else if (e1->v1 == e2->v2) {
1757 v = e1->v1;
1758 v1 = e1->v2;
1759 v2 = e2->v1;
1760 }
1761 else if (e1->v2 == e2->v1) {
1762 v = e1->v2;
1763 v1 = e1->v1;
1764 v2 = e2->v2;
1765 }
1766 else if (e1->v2 == e2->v2) {
1767 v = e1->v2;
1768 v1 = e1->v1;
1769 v2 = e2->v1;
1770 }
1771 else {
1772 BLI_assert(false);
1773 return false;
1774 }
1775 sub_v3_v3v3(dir1, v1->co, v->co);
1776 sub_v3_v3v3(dir2, v2->co, v->co);
1777 cross_v3_v3v3(cross, dir1, dir2);
1778 return dot_v3v3(cross, f->no) > 0.0f;
1779}
1780
1781/* When the offset_type is BEVEL_AMT_PERCENT or BEVEL_AMT_ABSOLUTE, fill in the coordinates
1782 * of the lines whose intersection defines the boundary point between e1 and e2 with common
1783 * vert v, as defined in the parameters of offset_meet.
1784 */
1786 EdgeHalf *e1,
1787 EdgeHalf *e2,
1788 BMVert *v,
1789 float r_l1a[3],
1790 float r_l1b[3],
1791 float r_l2a[3],
1792 float r_l2b[3])
1793{
1794 /* Get points the specified distance along each leg.
1795 * NOTE: not all BevVerts and EdgeHalfs have been made yet, so we have
1796 * to find required edges by moving around faces and use fake EdgeHalfs for
1797 * some of the edges. If there aren't faces to move around, we have to give up.
1798 * The legs we need are:
1799 * e0 : the next edge around e1->fnext (==f1) after e1.
1800 * e3 : the prev edge around e2->fprev (==f2) before e2.
1801 * e4 : the previous edge around f1 before e1 (may be e2).
1802 * e5 : the next edge around f2 after e2 (may be e1).
1803 */
1804 BMVert *v1, *v2;
1805 EdgeHalf e0, e3, e4, e5;
1806 BMFace *f1, *f2;
1807 float d0, d3, d4, d5;
1808 float e1_wt, e2_wt;
1809 v1 = BM_edge_other_vert(e1->e, v);
1810 v2 = BM_edge_other_vert(e2->e, v);
1811 f1 = e1->fnext;
1812 f2 = e2->fprev;
1813 bool no_offsets = f1 == nullptr || f2 == nullptr;
1814 if (!no_offsets) {
1815 BMLoop *l = BM_face_vert_share_loop(f1, v1);
1816 e0.e = l->e;
1818 e3.e = l->prev->e;
1820 e4.e = l->prev->e;
1822 e5.e = l->e;
1823 /* All the legs must be visible from their opposite legs. */
1824 no_offsets = !edge_edge_angle_less_than_180(e0.e, e1->e, f1) ||
1825 !edge_edge_angle_less_than_180(e1->e, e4.e, f1) ||
1826 !edge_edge_angle_less_than_180(e2->e, e3.e, f2) ||
1827 !edge_edge_angle_less_than_180(e5.e, e2->e, f1);
1828 if (!no_offsets) {
1829 if (bp->offset_type == BEVEL_AMT_ABSOLUTE) {
1830 d0 = d3 = d4 = d5 = bp->offset;
1831 }
1832 else {
1833 d0 = bp->offset * BM_edge_calc_length(e0.e) / 100.0f;
1834 d3 = bp->offset * BM_edge_calc_length(e3.e) / 100.0f;
1835 d4 = bp->offset * BM_edge_calc_length(e4.e) / 100.0f;
1836 d5 = bp->offset * BM_edge_calc_length(e5.e) / 100.0f;
1837 }
1838 if (bp->use_weights) {
1839 e1_wt = bp->bweight_offset_edge == -1 ?
1840 0.0f :
1842 e2_wt = bp->bweight_offset_edge == -1 ?
1843 0.0f :
1845 }
1846 else {
1847 e1_wt = 1.0f;
1848 e2_wt = 1.0f;
1849 }
1850 slide_dist(&e4, v, d4 * e1_wt, r_l1a);
1851 slide_dist(&e0, v1, d0 * e1_wt, r_l1b);
1852 slide_dist(&e5, v, d5 * e2_wt, r_l2a);
1853 slide_dist(&e3, v2, d3 * e2_wt, r_l2b);
1854 }
1855 }
1856 if (no_offsets) {
1857 copy_v3_v3(r_l1a, v->co);
1858 copy_v3_v3(r_l1b, v1->co);
1859 copy_v3_v3(r_l2a, v->co);
1860 copy_v3_v3(r_l2b, v2->co);
1861 }
1862}
1863
1882static void offset_meet(BevelParams *bp,
1883 EdgeHalf *e1,
1884 EdgeHalf *e2,
1885 BMVert *v,
1886 BMFace *f,
1887 bool edges_between,
1888 float meetco[3],
1889 const EdgeHalf *e_in_plane)
1890{
1891 /* Get direction vectors for two offset lines. */
1892 float dir1[3], dir2[3];
1893 sub_v3_v3v3(dir1, v->co, BM_edge_other_vert(e1->e, v)->co);
1894 sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
1895
1896 float dir1n[3], dir2p[3];
1897 if (edges_between) {
1898 EdgeHalf *e1next = e1->next;
1899 EdgeHalf *e2prev = e2->prev;
1900 sub_v3_v3v3(dir1n, BM_edge_other_vert(e1next->e, v)->co, v->co);
1901 sub_v3_v3v3(dir2p, v->co, BM_edge_other_vert(e2prev->e, v)->co);
1902 }
1903 else {
1904 /* Shut up 'maybe unused' warnings. */
1905 zero_v3(dir1n);
1906 zero_v3(dir2p);
1907 }
1908
1909 float ang = angle_v3v3(dir1, dir2);
1910 float norm_perp1[3];
1911 if (ang < BEVEL_EPSILON_ANG) {
1912 /* Special case: e1 and e2 are parallel; put offset point perp to both, from v.
1913 * need to find a suitable plane.
1914 * This code used to just use offset and dir1, but that makes for visible errors
1915 * on a circle with > 200 sides, which trips this "nearly perp" code (see #61214).
1916 * so use the average of the two, and the offset formula for angle bisector.
1917 * If offsets are different, we're out of luck:
1918 * Use the max of the two (so get consistent looking results if the same situation
1919 * arises elsewhere in the object but with opposite roles for e1 and e2). */
1920 float norm_v[3];
1921 if (f) {
1922 copy_v3_v3(norm_v, f->no);
1923 }
1924 else {
1925 /* Get average of face norms of faces between e and e2. */
1926 int fcount = 0;
1927 zero_v3(norm_v);
1928 for (EdgeHalf *eloop = e1; eloop != e2; eloop = eloop->next) {
1929 if (eloop->fnext != nullptr) {
1930 add_v3_v3(norm_v, eloop->fnext->no);
1931 fcount++;
1932 }
1933 }
1934 if (fcount == 0) {
1935 copy_v3_v3(norm_v, v->no);
1936 }
1937 else {
1938 mul_v3_fl(norm_v, 1.0f / fcount);
1939 }
1940 }
1941 add_v3_v3(dir1, dir2);
1942 cross_v3_v3v3(norm_perp1, dir1, norm_v);
1943 normalize_v3(norm_perp1);
1944 float off1a[3];
1945 copy_v3_v3(off1a, v->co);
1946 float d = max_ff(e1->offset_r, e2->offset_l);
1947 d = d / cosf(ang / 2.0f);
1948 madd_v3_v3fl(off1a, norm_perp1, d);
1949 copy_v3_v3(meetco, off1a);
1950 }
1951 else if (fabsf(ang - float(M_PI)) < BEVEL_EPSILON_ANG) {
1952 /* Special case: e1 and e2 are anti-parallel, so bevel is into a zero-area face.
1953 * Just make the offset point on the common line, at offset distance from v. */
1954 float d = max_ff(e1->offset_r, e2->offset_l);
1955 slide_dist(e2, v, d, meetco);
1956 }
1957 else {
1958 /* Get normal to plane where meet point should be, using cross product instead of f->no
1959 * in case f is non-planar.
1960 * Except: sometimes locally there can be a small angle between dir1 and dir2 that leads
1961 * to a normal that is actually almost perpendicular to the face normal;
1962 * in this case it looks wrong to use the local (cross-product) normal, so use the face normal
1963 * if the angle between dir1 and dir2 is smallish.
1964 * If e1-v-e2 is a reflex angle (viewed from vertex normal side), need to flip.
1965 * Use f->no to figure out which side to look at angle from, as even if f is non-planar,
1966 * will be more accurate than vertex normal. */
1967 float norm_v1[3], norm_v2[3];
1968 if (f && ang < BEVEL_SMALL_ANG) {
1969 copy_v3_v3(norm_v1, f->no);
1970 copy_v3_v3(norm_v2, f->no);
1971 }
1972 else if (!edges_between) {
1973 cross_v3_v3v3(norm_v1, dir2, dir1);
1974 normalize_v3(norm_v1);
1975 if (dot_v3v3(norm_v1, f ? f->no : v->no) < 0.0f) {
1976 negate_v3(norm_v1);
1977 }
1978 copy_v3_v3(norm_v2, norm_v1);
1979 }
1980 else {
1981 /* Separate faces; get face norms at corners for each separately. */
1982 cross_v3_v3v3(norm_v1, dir1n, dir1);
1983 normalize_v3(norm_v1);
1984 f = e1->fnext;
1985 if (dot_v3v3(norm_v1, f ? f->no : v->no) < 0.0f) {
1986 negate_v3(norm_v1);
1987 }
1988 cross_v3_v3v3(norm_v2, dir2, dir2p);
1989 normalize_v3(norm_v2);
1990 f = e2->fprev;
1991 if (dot_v3v3(norm_v2, f ? f->no : v->no) < 0.0f) {
1992 negate_v3(norm_v2);
1993 }
1994 }
1995
1996 /* Get vectors perp to each edge, perp to norm_v, and pointing into face. */
1997 float norm_perp2[3];
1998 cross_v3_v3v3(norm_perp1, dir1, norm_v1);
1999 cross_v3_v3v3(norm_perp2, dir2, norm_v2);
2000 normalize_v3(norm_perp1);
2001 normalize_v3(norm_perp2);
2002
2003 float off1a[3], off1b[3], off2a[3], off2b[3];
2005 offset_meet_lines_percent_or_absolute(bp, e1, e2, v, off1a, off1b, off2a, off2b);
2006 }
2007 else {
2008 /* Get points that are offset distances from each line, then another point on each line. */
2009 copy_v3_v3(off1a, v->co);
2010 madd_v3_v3fl(off1a, norm_perp1, e1->offset_r);
2011 add_v3_v3v3(off1b, off1a, dir1);
2012 copy_v3_v3(off2a, v->co);
2013 madd_v3_v3fl(off2a, norm_perp2, e2->offset_l);
2014 add_v3_v3v3(off2b, off2a, dir2);
2015 }
2016
2017 /* Intersect the offset lines. */
2018 float isect2[3];
2019 int isect_kind = isect_line_line_v3(off1a, off1b, off2a, off2b, meetco, isect2);
2020 if (isect_kind == 0) {
2021 /* Lines are collinear: we already tested for this, but this used a different epsilon. */
2022 copy_v3_v3(meetco, off1a); /* Just to do something. */
2023 }
2024 else {
2025 /* The lines intersect, but is it at a reasonable place?
2026 * One problem to check: if one of the offsets is 0, then we don't want an intersection
2027 * that is outside that edge itself. This can happen if angle between them is > 180 degrees,
2028 * or if the offset amount is > the edge length. */
2029 BMVert *closer_v;
2030 if (e1->offset_r == 0.0f && is_outside_edge(e1, meetco, &closer_v)) {
2031 copy_v3_v3(meetco, closer_v->co);
2032 }
2033 if (e2->offset_l == 0.0f && is_outside_edge(e2, meetco, &closer_v)) {
2034 copy_v3_v3(meetco, closer_v->co);
2035 }
2036 if (edges_between && e1->offset_r > 0.0f && e2->offset_l > 0.0f) {
2037 /* Try to drop meetco to a face between e1 and e2. */
2038 if (isect_kind == 2) {
2039 /* Lines didn't meet in 3d: get average of meetco and isect2. */
2040 mid_v3_v3v3(meetco, meetco, isect2);
2041 }
2042 for (EdgeHalf *e = e1; e != e2; e = e->next) {
2043 BMFace *fnext = e->fnext;
2044 if (!fnext) {
2045 continue;
2046 }
2047 float plane[4];
2048 plane_from_point_normal_v3(plane, v->co, fnext->no);
2049 float dropco[3];
2050 closest_to_plane_normalized_v3(dropco, plane, meetco);
2051 /* Don't drop to the faces next to the in plane edge. */
2052 if (e_in_plane) {
2053 ang = angle_v3v3(fnext->no, e_in_plane->fnext->no);
2054 if ((fabsf(ang) < BEVEL_SMALL_ANG) || (fabsf(ang - float(M_PI)) < BEVEL_SMALL_ANG)) {
2055 continue;
2056 }
2057 }
2058 if (point_between_edges(dropco, v, fnext, e, e->next)) {
2059 copy_v3_v3(meetco, dropco);
2060 break;
2061 }
2062 }
2063 }
2064 }
2065 }
2066}
2067
2068/* This was changed from 0.25f to fix bug #86768.
2069 * Original bug #44961 remains fixed with this value.
2070 * Update: changed again from 0.0001f to fix bug #95335.
2071 * Original two bugs remained fixed.
2072 */
2073#define BEVEL_GOOD_ANGLE 0.1f
2074
2084 EdgeHalf *e1, EdgeHalf *e2, BMVert *v, float meetco[3], float *r_angle)
2085{
2086 float dir1[3], dir2[3];
2087 sub_v3_v3v3(dir1, BM_edge_other_vert(e1->e, v)->co, v->co);
2088 sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
2089 normalize_v3(dir1);
2090 normalize_v3(dir2);
2091
2092 /* Find angle from dir1 to dir2 as viewed from vertex normal side. */
2093 float ang = angle_normalized_v3v3(dir1, dir2);
2094 if (fabsf(ang) < BEVEL_GOOD_ANGLE) {
2095 if (r_angle) {
2096 *r_angle = 0.0f;
2097 }
2098 return false;
2099 }
2100 float fno[3];
2101 cross_v3_v3v3(fno, dir1, dir2);
2102 if (dot_v3v3(fno, v->no) < 0.0f) {
2103 ang = 2.0f * float(M_PI) - ang; /* Angle is reflex. */
2104 if (r_angle) {
2105 *r_angle = ang;
2106 }
2107 return false;
2108 }
2109 if (r_angle) {
2110 *r_angle = ang;
2111 }
2112
2113 if (fabsf(ang - float(M_PI)) < BEVEL_GOOD_ANGLE) {
2114 return false;
2115 }
2116
2117 float sinang = sinf(ang);
2118
2119 copy_v3_v3(meetco, v->co);
2120 if (e1->offset_r == 0.0f) {
2121 madd_v3_v3fl(meetco, dir1, e2->offset_l / sinang);
2122 }
2123 else {
2124 madd_v3_v3fl(meetco, dir2, e1->offset_r / sinang);
2125 }
2126 return true;
2127}
2128
2134{
2135 float ang;
2136 float meet[3];
2137
2138 return offset_meet_edge(e1, emid, v, meet, &ang) && offset_meet_edge(emid, e2, v, meet, &ang);
2139}
2140
2150 EdgeHalf *e1,
2151 EdgeHalf *e2,
2152 EdgeHalf *emid,
2153 BMVert *v,
2154 float meetco[3],
2155 float *r_sinratio)
2156{
2157 bool retval = false;
2158
2159 BLI_assert(e1->is_bev && e2->is_bev && !emid->is_bev);
2160
2161 float ang1, ang2;
2162 float meet1[3], meet2[3];
2163 bool ok1 = offset_meet_edge(e1, emid, v, meet1, &ang1);
2164 bool ok2 = offset_meet_edge(emid, e2, v, meet2, &ang2);
2166 BMVert *v2 = BM_edge_other_vert(emid->e, v);
2167 if (bp->offset_type == BEVEL_AMT_PERCENT) {
2168 float wt = 1.0;
2169 if (bp->use_weights) {
2170 wt = bp->bweight_offset_edge == -1 ?
2171 0.0f :
2172 0.5f * (BM_ELEM_CD_GET_FLOAT(e1->e, bp->bweight_offset_edge) +
2174 }
2175 interp_v3_v3v3(meetco, v->co, v2->co, wt * bp->offset / 100.0f);
2176 }
2177 else {
2178 float dir[3];
2179 sub_v3_v3v3(dir, v2->co, v->co);
2180 normalize_v3(dir);
2181 madd_v3_v3v3fl(meetco, v->co, dir, bp->offset);
2182 }
2183 if (r_sinratio) {
2184 *r_sinratio = (ang1 == 0.0f) ? 1.0f : sinf(ang2) / sinf(ang1);
2185 }
2186 return true;
2187 }
2188 if (ok1 && ok2) {
2189 mid_v3_v3v3(meetco, meet1, meet2);
2190 if (r_sinratio) {
2191 /* ang1 should not be 0, but be paranoid. */
2192 *r_sinratio = (ang1 == 0.0f) ? 1.0f : sinf(ang2) / sinf(ang1);
2193 }
2194 retval = true;
2195 }
2196 else if (ok1 && !ok2) {
2197 copy_v3_v3(meetco, meet1);
2198 }
2199 else if (!ok1 && ok2) {
2200 copy_v3_v3(meetco, meet2);
2201 }
2202 else {
2203 /* Neither offset line met emid.
2204 * This should only happen if all three lines are on top of each other. */
2205 slide_dist(emid, v, e1->offset_r, meetco);
2206 }
2207
2208 return retval;
2209}
2210
2211/* Offset by e->offset in plane with normal plane_no, on left if left==true, else on right.
2212 * If plane_no is nullptr, choose an arbitrary plane different from eh's direction. */
2213static void offset_in_plane(EdgeHalf *e, const float plane_no[3], bool left, float r_co[3])
2214{
2215 BMVert *v = e->is_rev ? e->e->v2 : e->e->v1;
2216
2217 float dir[3], no[3];
2218 sub_v3_v3v3(dir, BM_edge_other_vert(e->e, v)->co, v->co);
2219 normalize_v3(dir);
2220 if (plane_no) {
2221 copy_v3_v3(no, plane_no);
2222 }
2223 else {
2224 zero_v3(no);
2225 if (fabsf(dir[0]) < fabsf(dir[1])) {
2226 no[0] = 1.0f;
2227 }
2228 else {
2229 no[1] = 1.0f;
2230 }
2231 }
2232
2233 float fdir[3];
2234 if (left) {
2235 cross_v3_v3v3(fdir, dir, no);
2236 }
2237 else {
2238 cross_v3_v3v3(fdir, no, dir);
2239 }
2240 normalize_v3(fdir);
2241 copy_v3_v3(r_co, v->co);
2242 madd_v3_v3fl(r_co, fdir, left ? e->offset_l : e->offset_r);
2243}
2244
2245/* Calculate the point on e where line (co_a, co_b) comes closest to and return it in projco. */
2246static void project_to_edge(const BMEdge *e,
2247 const float co_a[3],
2248 const float co_b[3],
2249 float projco[3])
2250{
2251 float otherco[3];
2252 if (!isect_line_line_v3(e->v1->co, e->v2->co, co_a, co_b, projco, otherco)) {
2253#ifdef BEVEL_ASSERT_PROJECT
2254 BLI_assert_msg(0, "project meet failure");
2255#endif
2256 copy_v3_v3(projco, e->v1->co);
2257 }
2258}
2259
2260/* If there is a bndv->ebev edge, find the mid control point if necessary.
2261 * It is the closest point on the beveled edge to the line segment between bndv and bndv->next. */
2263{
2264 bool do_linear_interp = true;
2265 EdgeHalf *e = bndv->ebev;
2266 Profile *pro = &bndv->profile;
2267
2268 float start[3], end[3];
2269 copy_v3_v3(start, bndv->nv.co);
2270 copy_v3_v3(end, bndv->next->nv.co);
2271 if (e) {
2272 do_linear_interp = false;
2273 pro->super_r = bp->pro_super_r;
2274 /* Projection direction is direction of the edge. */
2275 sub_v3_v3v3(pro->proj_dir, e->e->v1->co, e->e->v2->co);
2276 if (e->is_rev) {
2277 negate_v3(pro->proj_dir);
2278 }
2279 normalize_v3(pro->proj_dir);
2280 project_to_edge(e->e, start, end, pro->middle);
2281 copy_v3_v3(pro->start, start);
2282 copy_v3_v3(pro->end, end);
2283 /* Default plane to project onto is the one with triangle start - middle - end in it. */
2284 float d1[3], d2[3];
2285 sub_v3_v3v3(d1, pro->middle, start);
2286 sub_v3_v3v3(d2, pro->middle, end);
2287 normalize_v3(d1);
2288 normalize_v3(d2);
2289 cross_v3_v3v3(pro->plane_no, d1, d2);
2290 normalize_v3(pro->plane_no);
2291 if (nearly_parallel(d1, d2)) {
2292 /* Start - middle - end are collinear.
2293 * It should be the case that beveled edge is coplanar with two boundary verts.
2294 * We want to move the profile to that common plane, if possible.
2295 * That makes the multi-segment bevels curve nicely in that plane, as users expect.
2296 * The new middle should be either v (when neighbor edges are unbeveled)
2297 * or the intersection of the offset lines (if they are).
2298 * If the profile is going to lead into unbeveled edges on each side
2299 * (that is, both BoundVerts are "on-edge" points on non-beveled edges). */
2300 copy_v3_v3(pro->middle, bv->v->co);
2301 if (e->prev->is_bev && e->next->is_bev && bv->selcount >= 3) {
2302 /* Want mid at the meet point of next and prev offset edges. */
2303 float d3[3], d4[3], co4[3], meetco[3], isect2[3];
2304 int isect_kind;
2305
2306 sub_v3_v3v3(d3, e->prev->e->v1->co, e->prev->e->v2->co);
2307 sub_v3_v3v3(d4, e->next->e->v1->co, e->next->e->v2->co);
2308 normalize_v3(d3);
2309 normalize_v3(d4);
2310 if (nearly_parallel(d3, d4)) {
2311 /* Offset lines are collinear - want linear interpolation. */
2312 mid_v3_v3v3(pro->middle, start, end);
2313 do_linear_interp = true;
2314 }
2315 else {
2316 float co3[3];
2317 add_v3_v3v3(co3, start, d3);
2318 add_v3_v3v3(co4, end, d4);
2319 isect_kind = isect_line_line_v3(start, co3, end, co4, meetco, isect2);
2320 if (isect_kind != 0) {
2321 copy_v3_v3(pro->middle, meetco);
2322 }
2323 else {
2324 /* Offset lines don't intersect - want linear interpolation. */
2325 mid_v3_v3v3(pro->middle, start, end);
2326 do_linear_interp = true;
2327 }
2328 }
2329 }
2330 copy_v3_v3(pro->end, end);
2331 sub_v3_v3v3(d1, pro->middle, start);
2332 normalize_v3(d1);
2333 sub_v3_v3v3(d2, pro->middle, end);
2334 normalize_v3(d2);
2335 cross_v3_v3v3(pro->plane_no, d1, d2);
2336 normalize_v3(pro->plane_no);
2337 if (nearly_parallel(d1, d2)) {
2338 /* Whole profile is collinear with edge: just interpolate. */
2339 do_linear_interp = true;
2340 }
2341 else {
2342 copy_v3_v3(pro->plane_co, bv->v->co);
2343 copy_v3_v3(pro->proj_dir, pro->plane_no);
2344 }
2345 }
2346 copy_v3_v3(pro->plane_co, start);
2347 }
2348 else if (bndv->is_arc_start) {
2349 /* Assume pro->middle was already set. */
2350 copy_v3_v3(pro->start, start);
2351 copy_v3_v3(pro->end, end);
2352 pro->super_r = PRO_CIRCLE_R;
2353 zero_v3(pro->plane_co);
2354 zero_v3(pro->plane_no);
2355 zero_v3(pro->proj_dir);
2356 do_linear_interp = false;
2357 }
2358 else if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
2359 copy_v3_v3(pro->start, start);
2360 copy_v3_v3(pro->middle, bv->v->co);
2361 copy_v3_v3(pro->end, end);
2362 pro->super_r = bp->pro_super_r;
2363 zero_v3(pro->plane_co);
2364 zero_v3(pro->plane_no);
2365 zero_v3(pro->proj_dir);
2366 do_linear_interp = false;
2367 }
2368
2369 if (do_linear_interp) {
2370 pro->super_r = PRO_LINE_R;
2371 copy_v3_v3(pro->start, start);
2372 copy_v3_v3(pro->end, end);
2373 mid_v3_v3v3(pro->middle, start, end);
2374 /* Won't use projection for this line profile. */
2375 zero_v3(pro->plane_co);
2376 zero_v3(pro->plane_no);
2377 zero_v3(pro->proj_dir);
2378 }
2379}
2380
2388static void move_profile_plane(BoundVert *bndv, BMVert *bmvert)
2389{
2390 Profile *pro = &bndv->profile;
2391
2392 /* Only do this if projecting, and start, end, and proj_dir are not coplanar. */
2393 if (is_zero_v3(pro->proj_dir)) {
2394 return;
2395 }
2396
2397 float d1[3], d2[3];
2398 sub_v3_v3v3(d1, bmvert->co, pro->start);
2399 normalize_v3(d1);
2400 sub_v3_v3v3(d2, bmvert->co, pro->end);
2401 normalize_v3(d2);
2402 float no[3], no2[3], no3[3];
2403 cross_v3_v3v3(no, d1, d2);
2404 cross_v3_v3v3(no2, d1, pro->proj_dir);
2405 cross_v3_v3v3(no3, d2, pro->proj_dir);
2406
2409 {
2410 float dot2 = dot_v3v3(no, no2);
2411 float dot3 = dot_v3v3(no, no3);
2412 if (fabsf(dot2) < (1 - BEVEL_EPSILON_BIG) && fabsf(dot3) < (1 - BEVEL_EPSILON_BIG)) {
2413 copy_v3_v3(bndv->profile.plane_no, no);
2414 }
2415 }
2416
2417 /* We've changed the parameters from their defaults, so don't recalculate them later. */
2418 pro->special_params = true;
2419}
2420
2428static void move_weld_profile_planes(BevVert *bv, BoundVert *bndv1, BoundVert *bndv2)
2429{
2430 /* Only do this if projecting, and d1, d2, and proj_dir are not coplanar. */
2431 if (is_zero_v3(bndv1->profile.proj_dir) || is_zero_v3(bndv2->profile.proj_dir)) {
2432 return;
2433 }
2434 float d1[3], d2[3], no[3];
2435 sub_v3_v3v3(d1, bv->v->co, bndv1->nv.co);
2436 sub_v3_v3v3(d2, bv->v->co, bndv2->nv.co);
2437 cross_v3_v3v3(no, d1, d2);
2438 float l1 = normalize_v3(no);
2439
2440 /* "no" is new normal projection plane, but don't move if it is coplanar with both of the
2441 * projection directions. */
2442 float no2[3], no3[3];
2443 cross_v3_v3v3(no2, d1, bndv1->profile.proj_dir);
2444 float l2 = normalize_v3(no2);
2445 cross_v3_v3v3(no3, d2, bndv2->profile.proj_dir);
2446 float l3 = normalize_v3(no3);
2447 if (l1 != 0.0f && (l2 != 0.0f || l3 != 0.0f)) {
2448 float dot1 = fabsf(dot_v3v3(no, no2));
2449 float dot2 = fabsf(dot_v3v3(no, no3));
2450 if (fabsf(dot1 - 1.0f) > BEVEL_EPSILON) {
2451 copy_v3_v3(bndv1->profile.plane_no, no);
2452 }
2453 if (fabsf(dot2 - 1.0f) > BEVEL_EPSILON) {
2454 copy_v3_v3(bndv2->profile.plane_no, no);
2455 }
2456 }
2457
2458 /* We've changed the parameters from their defaults, so don't recalculate them later. */
2459 bndv1->profile.special_params = true;
2460 bndv2->profile.special_params = true;
2461}
2462
2463/* Return 1 if a and b are in CCW order on the normal side of f,
2464 * and -1 if they are reversed, and 0 if there is no shared face f. */
2465static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
2466{
2467 if (!f) {
2468 return 0;
2469 }
2470 BMLoop *la = BM_face_edge_share_loop(f, a);
2472 if (!la || !lb) {
2473 return 0;
2474 }
2475 return lb->next == la ? 1 : -1;
2476}
2477
2499static bool make_unit_square_map(const float va[3],
2500 const float vmid[3],
2501 const float vb[3],
2502 float r_mat[4][4])
2503{
2504 float vb_vmid[3], va_vmid[3];
2505 sub_v3_v3v3(va_vmid, vmid, va);
2506 sub_v3_v3v3(vb_vmid, vmid, vb);
2507
2508 if (is_zero_v3(va_vmid) || is_zero_v3(vb_vmid)) {
2509 return false;
2510 }
2511
2512 if (fabsf(angle_v3v3(va_vmid, vb_vmid) - float(M_PI)) <= BEVEL_EPSILON_ANG) {
2513 return false;
2514 }
2515
2516 float vo[3], vd[3], vddir[3];
2517 sub_v3_v3v3(vo, va, vb_vmid);
2518 cross_v3_v3v3(vddir, vb_vmid, va_vmid);
2519 normalize_v3(vddir);
2520 add_v3_v3v3(vd, vo, vddir);
2521
2522 /* The cols of m are: `vmid - va, vmid - vb, vmid + vd - va -vb, va + vb - vmid`;
2523 * Blender transform matrices are stored such that `m[i][*]` is `i-th` column;
2524 * the last elements of each col remain as they are in unity matrix. */
2525 sub_v3_v3v3(&r_mat[0][0], vmid, va);
2526 r_mat[0][3] = 0.0f;
2527 sub_v3_v3v3(&r_mat[1][0], vmid, vb);
2528 r_mat[1][3] = 0.0f;
2529 add_v3_v3v3(&r_mat[2][0], vmid, vd);
2530 sub_v3_v3(&r_mat[2][0], va);
2531 sub_v3_v3(&r_mat[2][0], vb);
2532 r_mat[2][3] = 0.0f;
2533 add_v3_v3v3(&r_mat[3][0], va, vb);
2534 sub_v3_v3(&r_mat[3][0], vmid);
2535 r_mat[3][3] = 1.0f;
2536
2537 return true;
2538}
2539
2557 const float va[3], const float vb[3], const float vc[3], const float vd[3], float r_mat[4][4])
2558{
2559 copy_v3_v3(r_mat[0], va);
2560 sub_v3_v3(r_mat[0], vb);
2561 sub_v3_v3(r_mat[0], vc);
2562 add_v3_v3(r_mat[0], vd);
2563 mul_v3_fl(r_mat[0], 0.5f);
2564 r_mat[0][3] = 0.0f;
2565 copy_v3_v3(r_mat[1], vb);
2566 sub_v3_v3(r_mat[1], va);
2567 sub_v3_v3(r_mat[1], vc);
2568 add_v3_v3(r_mat[1], vd);
2569 mul_v3_fl(r_mat[1], 0.5f);
2570 r_mat[1][3] = 0.0f;
2571 copy_v3_v3(r_mat[2], vc);
2572 sub_v3_v3(r_mat[2], va);
2573 sub_v3_v3(r_mat[2], vb);
2574 add_v3_v3(r_mat[2], vd);
2575 mul_v3_fl(r_mat[2], 0.5f);
2576 r_mat[2][3] = 0.0f;
2577 copy_v3_v3(r_mat[3], va);
2578 add_v3_v3(r_mat[3], vb);
2579 add_v3_v3(r_mat[3], vc);
2580 sub_v3_v3(r_mat[3], vd);
2581 mul_v3_fl(r_mat[3], 0.5f);
2582 r_mat[3][3] = 1.0f;
2583}
2584
2591static double superellipse_co(double x, float r, bool rbig)
2592{
2593 BLI_assert(r > 0.0f);
2594
2595 /* If r<1, mirror the superellipse function by (y=x)-line to get a numerically stable range
2596 * Possible because of symmetry, later mirror back. */
2597 if (rbig) {
2598 return pow((1.0 - pow(x, r)), (1.0 / r));
2599 }
2600 return 1.0 - pow((1.0 - pow(1.0 - x, r)), (1.0 / r));
2601}
2602
2612static void get_profile_point(BevelParams *bp, const Profile *pro, int i, int nseg, float r_co[3])
2613{
2614 if (bp->seg == 1) {
2615 if (i == 0) {
2616 copy_v3_v3(r_co, pro->start);
2617 }
2618 else {
2619 copy_v3_v3(r_co, pro->end);
2620 }
2621 }
2622
2623 else {
2624 if (nseg == bp->seg) {
2625 BLI_assert(pro->prof_co != nullptr);
2626 copy_v3_v3(r_co, pro->prof_co + 3 * i);
2627 }
2628 else {
2629 BLI_assert(is_power_of_2_i(nseg) && nseg <= bp->pro_spacing.seg_2);
2630 /* Find spacing between sub-samples in `prof_co_2`. */
2631 int subsample_spacing = bp->pro_spacing.seg_2 / nseg;
2632 copy_v3_v3(r_co, pro->prof_co_2 + 3 * i * subsample_spacing);
2633 }
2634 }
2635}
2636
2641static void calculate_profile_segments(const Profile *profile,
2642 const float map[4][4],
2643 const bool use_map,
2644 const bool reversed,
2645 const int ns,
2646 const double *xvals,
2647 const double *yvals,
2648 float *r_prof_co)
2649{
2650 /* Iterate over the vertices along the boundary arc. */
2651 for (int k = 0; k <= ns; k++) {
2652 float co[3];
2653 if (k == 0) {
2654 copy_v3_v3(co, profile->start);
2655 }
2656 else if (k == ns) {
2657 copy_v3_v3(co, profile->end);
2658 }
2659 else {
2660 if (use_map) {
2661 const float p[3] = {
2662 reversed ? float(yvals[ns - k]) : float(xvals[k]),
2663 reversed ? float(xvals[ns - k]) : float(yvals[k]),
2664 0.0f,
2665 };
2666 /* Do the 2D->3D transformation of the profile coordinates. */
2667 mul_v3_m4v3(co, map, p);
2668 }
2669 else {
2670 interp_v3_v3v3(co, profile->start, profile->end, float(k) / float(ns));
2671 }
2672 }
2673 /* Finish the 2D->3D transformation by projecting onto the final profile plane. */
2674 float *prof_co_k = r_prof_co + 3 * k;
2675 if (!is_zero_v3(profile->proj_dir)) {
2676 float co2[3];
2677 add_v3_v3v3(co2, co, profile->proj_dir);
2678 /* pro->plane_co and pro->plane_no are filled in #set_profile_params. */
2679 if (!isect_line_plane_v3(prof_co_k, co, co2, profile->plane_co, profile->plane_no)) {
2680 /* Shouldn't happen. */
2681 copy_v3_v3(prof_co_k, co);
2682 }
2683 }
2684 else {
2685 copy_v3_v3(prof_co_k, co);
2686 }
2687 }
2688}
2689
2698static void calculate_profile(BevelParams *bp, BoundVert *bndv, bool reversed, bool miter)
2699{
2700 Profile *pro = &bndv->profile;
2701 ProfileSpacing *pro_spacing = (miter) ? &bp->pro_spacing_miter : &bp->pro_spacing;
2702
2703 if (bp->seg == 1) {
2704 return;
2705 }
2706
2707 bool need_2 = bp->seg != bp->pro_spacing.seg_2;
2708 if (pro->prof_co == nullptr) {
2709 pro->prof_co = static_cast<float *>(
2710 BLI_memarena_alloc(bp->mem_arena, sizeof(float[3]) * (bp->seg + 1)));
2711 if (need_2) {
2712 pro->prof_co_2 = static_cast<float *>(
2713 BLI_memarena_alloc(bp->mem_arena, sizeof(float[3]) * (bp->pro_spacing.seg_2 + 1)));
2714 }
2715 else {
2716 pro->prof_co_2 = pro->prof_co;
2717 }
2718 }
2719
2720 bool use_map;
2721 float map[4][4];
2723 use_map = false;
2724 }
2725 else {
2726 use_map = make_unit_square_map(pro->start, pro->middle, pro->end, map);
2727 }
2728
2729 if (bp->vmesh_method == BEVEL_VMESH_CUTOFF && use_map) {
2730 /* Calculate the "height" of the profile by putting the (0,0) and (1,1) corners of the
2731 * un-transformed profile through the 2D->3D map and calculating the distance between them. */
2732 float bottom_corner[3] = {0.0f, 0.0f, 0.0f};
2733 mul_v3_m4v3(bottom_corner, map, bottom_corner);
2734 float top_corner[3] = {1.0f, 1.0f, 0.0f};
2735 mul_v3_m4v3(top_corner, map, top_corner);
2736
2737 pro->height = len_v3v3(bottom_corner, top_corner);
2738 }
2739
2740 /* Calculate the 3D locations for the profile points */
2742 pro, map, use_map, reversed, bp->seg, pro_spacing->xvals, pro_spacing->yvals, pro->prof_co);
2743 /* Also calculate for the seg_2 case if it's needed. */
2744 if (need_2) {
2746 map,
2747 use_map,
2748 reversed,
2749 bp->pro_spacing.seg_2,
2750 pro_spacing->xvals_2,
2751 pro_spacing->yvals_2,
2752 pro->prof_co_2);
2753 }
2754}
2755
2762static void snap_to_superellipsoid(float co[3], const float super_r, bool midline)
2763{
2764 float r = super_r;
2765 if (r == PRO_CIRCLE_R) {
2766 normalize_v3(co);
2767 return;
2768 }
2769
2770 float a = max_ff(0.0f, co[0]);
2771 float b = max_ff(0.0f, co[1]);
2772 float c = max_ff(0.0f, co[2]);
2773 float x = a;
2774 float y = b;
2775 float z = c;
2777 /* Will only be called for 2d profile. */
2779 z = 0.0f;
2780 x = min_ff(1.0f, x);
2781 y = min_ff(1.0f, y);
2782 if (r == PRO_SQUARE_R) {
2783 /* Snap to closer of x==1 and y==1 lines, or maybe both. */
2784 float dx = 1.0f - x;
2785 float dy = 1.0f - y;
2786 if (dx < dy) {
2787 x = 1.0f;
2788 y = midline ? 1.0f : y;
2789 }
2790 else {
2791 y = 1.0f;
2792 x = midline ? 1.0f : x;
2793 }
2794 }
2795 else {
2796 /* Snap to closer of x==0 and y==0 lines, or maybe both. */
2797 if (x < y) {
2798 x = 0.0f;
2799 y = midline ? 0.0f : y;
2800 }
2801 else {
2802 y = 0.0f;
2803 x = midline ? 0.0f : x;
2804 }
2805 }
2806 }
2807 else {
2808 float rinv = 1.0f / r;
2809 if (a == 0.0f) {
2810 if (b == 0.0f) {
2811 x = 0.0f;
2812 y = 0.0f;
2813 z = powf(c, rinv);
2814 }
2815 else {
2816 x = 0.0f;
2817 y = powf(1.0f / (1.0f + powf(c / b, r)), rinv);
2818 z = c * y / b;
2819 }
2820 }
2821 else {
2822 x = powf(1.0f / (1.0f + powf(b / a, r) + powf(c / a, r)), rinv);
2823 y = b * x / a;
2824 z = c * x / a;
2825 }
2826 }
2827 co[0] = x;
2828 co[1] = y;
2829 co[2] = z;
2830}
2831
2832#define BEV_EXTEND_EDGE_DATA_CHECK(eh, flag) BM_elem_flag_test(eh->e, flag)
2833
2834/* If a beveled edge has a seam (flag == BM_ELEM_SEAM) or a sharp
2835 * (flag == BM_ELEM_SMOOTH and the test is for the negation of that flag),
2836 * then we may need to correct for discontinuities in those edge flags after
2837 * beveling. The code will automatically make the outer edges of a multi-segment
2838 * beveled edge have the same flags. So beveled edges next to each other will not
2839 * lead to discontinuities. But if there are beveled edges that do NOT have a seam
2840 * (or sharp), then we need to mark all the edge segments of such beveled edges
2841 * with seam (or sharp) until we hit the next beveled edge that has such a mark.
2842 * This routine sets, for each rightv of a beveled edge that has seam (or sharp),
2843 * how many edges follow without the corresponding property. The count is put in
2844 * the seam_len field for seams and the sharp_len field for sharps.
2845 *
2846 * TODO: This approach doesn't work for terminal edges or miters.
2847 */
2848#define HASNOT_SEAMSHARP(eh, flag) \
2849 ((flag == BM_ELEM_SEAM && !BM_elem_flag_test(eh->e, BM_ELEM_SEAM)) || \
2850 (flag == BM_ELEM_SMOOTH && BM_elem_flag_test(eh->e, BM_ELEM_SMOOTH)))
2852{
2853 EdgeHalf *e = &bv->edges[0], *efirst = &bv->edges[0];
2854
2855 /* Get to first edge with seam or sharp edge data. */
2856 while (HASNOT_SEAMSHARP(e, flag)) {
2857 e = e->next;
2858 if (e == efirst) {
2859 break;
2860 }
2861 }
2862
2863 /* If no such edge found, return. */
2864 if (HASNOT_SEAMSHARP(e, flag)) {
2865 return;
2866 }
2867
2868 /* Set efirst to this first encountered edge. */
2869 efirst = e;
2870
2871 do {
2872 int flag_count = 0;
2873 EdgeHalf *ne = e->next;
2874
2875 while (HASNOT_SEAMSHARP(ne, flag) && ne != efirst) {
2876 if (ne->is_bev) {
2877 flag_count++;
2878 }
2879 ne = ne->next;
2880 }
2881 if (ne == e || (ne == efirst && HASNOT_SEAMSHARP(efirst, flag))) {
2882 break;
2883 }
2884 /* Set seam_len / sharp_len of starting edge. */
2885 if (flag == BM_ELEM_SEAM) {
2886 e->rightv->seam_len = flag_count;
2887 }
2888 else if (flag == BM_ELEM_SMOOTH) {
2889 e->rightv->sharp_len = flag_count;
2890 }
2891 e = ne;
2892 } while (e != efirst);
2893}
2894
2895/* Extend the marking of edges as seam (if flag == BM_ELEM_SEAM) or sharp
2896 * (if flag == BM_ELEM_SMOOTH) around the appropriate edges added as part
2897 * of doing a bevel at vert bv. */
2898
2899static void bevel_extend_edge_data_ex(BevVert *bv, int flag)
2900{
2902 VMesh *vm = bv->vmesh;
2903
2904 BoundVert *bcur = bv->vmesh->boundstart, *start = bcur;
2905
2906 do {
2907 /* If current boundvert has a seam/sharp length > 0 then we need to extend here. */
2908 int extend_len = flag == BM_ELEM_SEAM ? bcur->seam_len : bcur->sharp_len;
2909 if (extend_len) {
2910 if (!bv->vmesh->boundstart->seam_len && start == bv->vmesh->boundstart) {
2911 start = bcur; /* Set start to first boundvert with seam_len > 0. */
2912 }
2913
2914 /* Now for all the mesh_verts starting at current index and ending at `idx_end`
2915 * we go through outermost ring and through all its segments and add seams
2916 * for those edges. */
2917 int idx_end = bcur->index + extend_len;
2918 for (int i = bcur->index; i < idx_end; i++) {
2919 BMVert *v1 = mesh_vert(vm, i % vm->count, 0, 0)->v, *v2;
2920 BMEdge *e;
2921 for (int k = 1; k < vm->seg; k++) {
2922 v2 = mesh_vert(vm, i % vm->count, 0, k)->v;
2923
2924 /* Here v1 & v2 are current and next BMverts,
2925 * we find common edge and set its edge data. */
2926 e = v1->e;
2927 while (e->v1 != v2 && e->v2 != v2) {
2928 e = BM_DISK_EDGE_NEXT(e, v1);
2929 }
2930 if (flag == BM_ELEM_SEAM) {
2932 }
2933 else {
2935 }
2936 v1 = v2;
2937 }
2938 BMVert *v3 = mesh_vert(vm, (i + 1) % vm->count, 0, 0)->v;
2939 e = v1->e; /* Do same as above for first and last vert. */
2940 while (e->v1 != v3 && e->v2 != v3) {
2941 e = BM_DISK_EDGE_NEXT(e, v1);
2942 }
2943 if (flag == BM_ELEM_SEAM) {
2945 }
2946 else {
2948 }
2949 bcur = bcur->next;
2950 }
2951 }
2952 else {
2953 bcur = bcur->next;
2954 }
2955 } while (bcur != start);
2956}
2957
2959{
2960 VMesh *vm = bv->vmesh;
2961
2962 if (vm->mesh_kind == M_TRI_FAN || bv->selcount < 2) {
2963 return;
2964 }
2965
2968}
2969
2970/* Mark edges as sharp if they are between a smooth reconstructed face and a new face. */
2972{
2973 BMIter fiter;
2974 BMFace *f;
2975 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2977 continue;
2978 }
2979 if (get_face_kind(bp, f) != F_RECON) {
2980 continue;
2981 }
2982 BMIter liter;
2983 BMLoop *l;
2984 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2985 /* Cases we care about will have exactly one adjacent face. */
2986 BMLoop *lother = l->radial_next;
2987 BMFace *fother = lother->f;
2988 if (lother != l && fother) {
2989 FKind fkind = get_face_kind(bp, lother->f);
2990 if (ELEM(fkind, F_EDGE, F_VERT)) {
2992 }
2993 }
2994 }
2995 }
2996}
2997
3007{
3008 if (bp->offset == 0.0 || !bp->harden_normals) {
3009 return;
3010 }
3011
3012 /* Recalculate all face and vertex normals. Side effect: ensures vertex, edge, face indices. */
3013 /* I suspect this is not necessary. TODO: test that guess. */
3015
3016 int cd_clnors_offset = CustomData_get_offset_named(
3017 &bm->ldata, CD_PROP_INT16_2D, "custom_normal");
3018
3019 /* If there is not already a custom normal layer then making one
3020 * (with #BM_lnorspace_update) will not respect the auto-smooth angle between smooth faces.
3021 * To get that to happen, we have to mark the sharpen the edges that are only sharp because
3022 * of the angle test -- otherwise would be smooth. */
3023 if (cd_clnors_offset == -1) {
3025 }
3026
3027 /* Ensure that `bm->lnor_spacearr` has properly stored loop normals.
3028 * Side effect: ensures loop indices. */
3030
3031 if (cd_clnors_offset == -1) {
3032 cd_clnors_offset = CustomData_get_offset_named(&bm->ldata, CD_PROP_INT16_2D, "custom_normal");
3033 }
3034
3035 /* If the custom normals attribute still hasn't been added with the correct type, at least don't
3036 * crash. */
3037 if (cd_clnors_offset == -1) {
3038 return;
3039 }
3040
3041 BMIter fiter;
3042 BMFace *f;
3043 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
3044 FKind fkind = get_face_kind(bp, f);
3045 if (ELEM(fkind, F_ORIG, F_RECON)) {
3046 continue;
3047 }
3048 BMIter liter;
3049 BMLoop *l;
3050 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
3051 BMEdge *estep = l->prev->e; /* Causes CW walk around l->v fan. */
3052 BMLoop *lprev = BM_vert_step_fan_loop(l, &estep);
3053 estep = l->e; /* Causes CCW walk around l->v fan. */
3054 BMLoop *lnext = BM_vert_step_fan_loop(l, &estep);
3055 FKind fprevkind = lprev ? get_face_kind(bp, lprev->f) : F_NONE;
3056 FKind fnextkind = lnext ? get_face_kind(bp, lnext->f) : F_NONE;
3057
3058 float norm[3];
3059 float *pnorm = nullptr;
3060 if (fkind == F_EDGE) {
3061 if (fprevkind == F_EDGE && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
3062 add_v3_v3v3(norm, f->no, lprev->f->no);
3063 pnorm = norm;
3064 }
3065 else if (fnextkind == F_EDGE && BM_elem_flag_test(lnext, BM_ELEM_LONG_TAG)) {
3066 add_v3_v3v3(norm, f->no, lnext->f->no);
3067 pnorm = norm;
3068 }
3069 else if (fprevkind == F_RECON && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
3070 pnorm = lprev->f->no;
3071 }
3072 else if (fnextkind == F_RECON && BM_elem_flag_test(l->prev, BM_ELEM_LONG_TAG)) {
3073 pnorm = lnext->f->no;
3074 }
3075 else {
3076 // printf("unexpected harden case (edge)\n");
3077 }
3078 }
3079 else if (fkind == F_VERT) {
3080 if (fprevkind == F_VERT && fnextkind == F_VERT) {
3081 pnorm = l->v->no;
3082 }
3083 else if (fprevkind == F_RECON) {
3084 pnorm = lprev->f->no;
3085 }
3086 else if (fnextkind == F_RECON) {
3087 pnorm = lnext->f->no;
3088 }
3089 else {
3090 BMLoop *lprevprev, *lnextnext;
3091 if (lprev) {
3092 estep = lprev->prev->e;
3093 lprevprev = BM_vert_step_fan_loop(lprev, &estep);
3094 }
3095 else {
3096 lprevprev = nullptr;
3097 }
3098 if (lnext) {
3099 estep = lnext->e;
3100 lnextnext = BM_vert_step_fan_loop(lnext, &estep);
3101 }
3102 else {
3103 lnextnext = nullptr;
3104 }
3105 FKind fprevprevkind = lprevprev ? get_face_kind(bp, lprevprev->f) : F_NONE;
3106 FKind fnextnextkind = lnextnext ? get_face_kind(bp, lnextnext->f) : F_NONE;
3107 if (fprevkind == F_EDGE && fprevprevkind == F_RECON) {
3108 pnorm = lprevprev->f->no;
3109 }
3110 else if (fprevkind == F_EDGE && fnextkind == F_VERT && fprevprevkind == F_EDGE) {
3111 add_v3_v3v3(norm, lprev->f->no, lprevprev->f->no);
3112 pnorm = norm;
3113 }
3114 else if (fnextkind == F_EDGE && fprevkind == F_VERT && fnextnextkind == F_EDGE) {
3115 add_v3_v3v3(norm, lnext->f->no, lnextnext->f->no);
3116 pnorm = norm;
3117 }
3118 else {
3119 // printf("unexpected harden case (vert)\n");
3120 }
3121 }
3122 }
3123 if (pnorm) {
3124 if (pnorm == norm) {
3126 }
3127 int l_index = BM_elem_index_get(l);
3128 short *clnors = static_cast<short *>(BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset));
3130 }
3131 }
3132 }
3133}
3134
3136{
3137 const int mode = bp->face_strength_mode;
3138 const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
3139 int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, wn_layer_id);
3140
3141 if (cd_prop_int_idx == -1) {
3143 cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, wn_layer_id);
3144 }
3145 cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT32);
3146 const int cd_prop_int_offset = CustomData_get_n_offset(
3147 &bm->pdata, CD_PROP_INT32, cd_prop_int_idx);
3148
3149 BMIter fiter;
3150 BMFace *f;
3151 BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
3152 FKind fkind = get_face_kind(bp, f);
3153 bool do_set_strength = true;
3154 int strength;
3155 switch (fkind) {
3156 case F_VERT:
3157 strength = FACE_STRENGTH_WEAK;
3158 do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
3159 break;
3160 case F_EDGE:
3161 strength = FACE_STRENGTH_MEDIUM;
3162 do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
3163 break;
3164 case F_RECON:
3165 strength = FACE_STRENGTH_STRONG;
3166 do_set_strength = (mode >= BEVEL_FACE_STRENGTH_AFFECTED);
3167 break;
3168 case F_ORIG:
3169 strength = FACE_STRENGTH_STRONG;
3170 do_set_strength = (mode == BEVEL_FACE_STRENGTH_ALL);
3171 break;
3172 default:
3173 do_set_strength = false;
3174 }
3175 if (do_set_strength) {
3176 int *strength_ptr = static_cast<int *>(BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset));
3177 *strength_ptr = strength;
3178 }
3179 }
3180}
3181
3182/* Set the any_seam property for a BevVert and all its BoundVerts. */
3183static void set_bound_vert_seams(BevVert *bv, bool mark_seam, bool mark_sharp)
3184{
3185 bv->any_seam = false;
3186 BoundVert *v = bv->vmesh->boundstart;
3187 do {
3188 v->any_seam = false;
3189 for (EdgeHalf *e = v->efirst; e; e = e->next) {
3190 v->any_seam |= e->is_seam;
3191 if (e == v->elast) {
3192 break;
3193 }
3194 }
3195 bv->any_seam |= v->any_seam;
3196 } while ((v = v->next) != bv->vmesh->boundstart);
3197
3198 if (mark_seam) {
3200 }
3201 if (mark_sharp) {
3203 }
3204}
3205
3206/* Is e between two faces with a 180 degree angle between their normals? */
3208{
3209 if (e->fprev && e->fnext) {
3210 float dot = dot_v3v3(e->fprev->no, e->fnext->no);
3211 if (fabsf(dot + 1.0f) <= BEVEL_EPSILON_BIG || fabsf(dot - 1.0f) <= BEVEL_EPSILON_BIG) {
3212 return true;
3213 }
3214 }
3215 return false;
3216}
3217
3225{
3226 BoundVert *bndv = vm->boundstart;
3227 do {
3228 /* In special cases the params will have already been set. */
3229 if (!bndv->profile.special_params) {
3230 set_profile_params(bp, bv, bndv);
3231 }
3232 bool miter_profile = false;
3233 bool reverse_profile = false;
3235 /* Use the miter profile spacing struct if the default is filled with the custom profile. */
3236 miter_profile = (bndv->is_arc_start || bndv->is_patch_start);
3237 /* Don't bother reversing the profile if it's a miter profile */
3238 reverse_profile = !bndv->is_profile_start && !miter_profile;
3239 }
3240 calculate_profile(bp, bndv, reverse_profile, miter_profile);
3241 } while ((bndv = bndv->next) != vm->boundstart);
3242}
3243
3244/* Implements build_boundary for the vertex-only case. */
3245static void build_boundary_vertex_only(BevelParams *bp, BevVert *bv, bool construct)
3246{
3247 VMesh *vm = bv->vmesh;
3248
3250
3251 EdgeHalf *efirst = &bv->edges[0];
3252 EdgeHalf *e = efirst;
3253 do {
3254 float co[3];
3255 slide_dist(e, bv->v, e->offset_l, co);
3256 if (construct) {
3257 BoundVert *v = add_new_bound_vert(bp->mem_arena, vm, co);
3258 v->efirst = v->elast = e;
3259 e->leftv = e->rightv = v;
3260 }
3261 else {
3262 adjust_bound_vert(e->leftv, co);
3263 }
3264 } while ((e = e->next) != efirst);
3265
3266 if (construct) {
3268 /* Also check for seams at the vertex itself. */
3269 if (bp->affect_vertices_odd) {
3270 if (!bv->any_seam && !contig_ldata_around_vert(bp->bm, bv->v)) {
3271 bv->any_seam = true;
3272 }
3273 }
3274 if (vm->count == 2) {
3275 vm->mesh_kind = M_NONE;
3276 }
3277 else if (bp->seg == 1) {
3278 vm->mesh_kind = M_POLY;
3279 }
3280 else {
3281 vm->mesh_kind = M_ADJ;
3282 }
3283 }
3284}
3285
3292 BevVert *bv,
3293 EdgeHalf *efirst,
3294 const bool construct)
3295{
3296 MemArena *mem_arena = bp->mem_arena;
3297 VMesh *vm = bv->vmesh;
3298
3299 EdgeHalf *e = efirst;
3300 float co[3];
3301 if (bv->edgecount == 2) {
3302 /* Only 2 edges in, so terminate the edge with an artificial vertex on the unbeveled edge.
3303 * If the offset type is BEVEL_AMT_PERCENT or BEVEL_AMT_ABSOLUTE, what to do is a bit
3304 * undefined (there aren't two "legs"), so just let the code do what it does. */
3305 const float *no = e->fprev ? e->fprev->no : (e->fnext ? e->fnext->no : nullptr);
3306 offset_in_plane(e, no, true, co);
3307 if (construct) {
3308 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3309 bndv->efirst = bndv->elast = bndv->ebev = e;
3310 e->leftv = bndv;
3311 }
3312 else {
3313 adjust_bound_vert(e->leftv, co);
3314 }
3315 no = e->fnext ? e->fnext->no : (e->fprev ? e->fprev->no : nullptr);
3316 offset_in_plane(e, no, false, co);
3317 if (construct) {
3318 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3319 bndv->efirst = bndv->elast = e;
3320 e->rightv = bndv;
3321 }
3322 else {
3323 adjust_bound_vert(e->rightv, co);
3324 }
3325 /* Make artificial extra point along unbeveled edge, and form triangle. */
3326 slide_dist(e->next, bv->v, e->offset_l, co);
3327 if (construct) {
3328 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3329 bndv->efirst = bndv->elast = e->next;
3330 e->next->leftv = e->next->rightv = bndv;
3332 }
3333 else {
3334 adjust_bound_vert(e->next->leftv, co);
3335 }
3336 }
3337 else {
3338 /* More than 2 edges in. Put on-edge verts on all the other edges and join with the beveled
3339 * edge to make a poly or adj mesh, because e->prev has offset 0, offset_meet will put co on
3340 * that edge. */
3341 /* TODO: should do something else if angle between e and e->prev > 180 */
3342 bool leg_slide = bp->offset_type == BEVEL_AMT_PERCENT || bp->offset_type == BEVEL_AMT_ABSOLUTE;
3343 if (leg_slide) {
3344 slide_dist(e->prev, bv->v, e->offset_l, co);
3345 }
3346 else {
3347 offset_meet(bp, e->prev, e, bv->v, e->fprev, false, co, nullptr);
3348 }
3349 if (construct) {
3350 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3351 bndv->efirst = e->prev;
3352 bndv->elast = bndv->ebev = e;
3353 e->leftv = bndv;
3354 e->prev->leftv = e->prev->rightv = bndv;
3355 }
3356 else {
3357 adjust_bound_vert(e->leftv, co);
3358 }
3359 e = e->next;
3360 if (leg_slide) {
3361 slide_dist(e, bv->v, e->prev->offset_r, co);
3362 }
3363 else {
3364 offset_meet(bp, e->prev, e, bv->v, e->fprev, false, co, nullptr);
3365 }
3366 if (construct) {
3367 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3368 bndv->efirst = e->prev;
3369 bndv->elast = e;
3370 e->leftv = e->rightv = bndv;
3371 e->prev->rightv = bndv;
3372 }
3373 else {
3374 adjust_bound_vert(e->leftv, co);
3375 }
3376 /* For the edges not adjacent to the beveled edge, slide the bevel amount along. */
3377 float d = efirst->offset_l_spec;
3378 if (bp->profile_type == BEVEL_PROFILE_CUSTOM || bp->profile < 0.25f) {
3379 d *= sqrtf(2.0f); /* Need to go further along the edge to make room for full profile area. */
3380 }
3381 for (e = e->next; e->next != efirst; e = e->next) {
3382 slide_dist(e, bv->v, d, co);
3383 if (construct) {
3384 BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
3385 bndv->efirst = bndv->elast = e;
3386 e->leftv = e->rightv = bndv;
3387 }
3388 else {
3389 adjust_bound_vert(e->leftv, co);
3390 }
3391 }
3392 }
3393
3394 if (bv->edgecount >= 3) {
3395 /* Special case: snap profile to plane of adjacent two edges. */
3396 BoundVert *bndv = vm->boundstart;
3397 BLI_assert(bndv->ebev != nullptr);
3398 set_profile_params(bp, bv, bndv);
3399 move_profile_plane(bndv, bv->v);
3400 }
3401
3402 if (construct) {
3404
3405 if (vm->count == 2 && bv->edgecount == 3) {
3406 vm->mesh_kind = M_NONE;
3407 }
3408 else if (vm->count == 3) {
3409 bool use_tri_fan = true;
3411 /* Prevent overhanging edges: use M_POLY if the extra point is planar with the profile. */
3412 BoundVert *bndv = efirst->leftv;
3413 float profile_plane[4];
3414 plane_from_point_normal_v3(profile_plane, bndv->profile.plane_co, bndv->profile.plane_no);
3415 bndv = efirst->rightv->next; /* The added boundvert placed along the non-adjacent edge. */
3416 if (dist_squared_to_plane_v3(bndv->nv.co, profile_plane) < BEVEL_EPSILON_BIG) {
3417 use_tri_fan = false;
3418 }
3419 }
3420 vm->mesh_kind = (use_tri_fan) ? M_TRI_FAN : M_POLY;
3421 }
3422 else {
3423 vm->mesh_kind = M_POLY;
3424 }
3425 }
3426}
3427
3428/* Helper for build_boundary to handle special miters. */
3429static void adjust_miter_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
3430{
3431 int miter_outer = bp->miter_outer;
3432
3433 BoundVert *v1 = emiter->rightv;
3434 BoundVert *v2, *v3;
3435 if (miter_outer == BEVEL_MITER_PATCH) {
3436 v2 = v1->next;
3437 v3 = v2->next;
3438 }
3439 else {
3440 BLI_assert(miter_outer == BEVEL_MITER_ARC);
3441 v2 = nullptr;
3442 v3 = v1->next;
3443 }
3444 BoundVert *v1prev = v1->prev;
3445 BoundVert *v3next = v3->next;
3446 float co2[3];
3447 copy_v3_v3(co2, v1->nv.co);
3448 if (v1->is_arc_start) {
3449 copy_v3_v3(v1->profile.middle, co2);
3450 }
3451
3452 /* co1 is intersection of line through co2 in dir of emiter->e
3453 * and plane with normal the dir of emiter->e and through v1prev. */
3454 float co1[3], edge_dir[3], line_p[3];
3455 BMVert *vother = BM_edge_other_vert(emiter->e, bv->v);
3456 sub_v3_v3v3(edge_dir, bv->v->co, vother->co);
3457 normalize_v3(edge_dir);
3458 float d = bp->offset / (bp->seg / 2.0f); /* A fallback amount to move. */
3459 madd_v3_v3v3fl(line_p, co2, edge_dir, d);
3460 if (!isect_line_plane_v3(co1, co2, line_p, v1prev->nv.co, edge_dir)) {
3461 copy_v3_v3(co1, line_p);
3462 }
3463 adjust_bound_vert(v1, co1);
3464
3465 /* co3 is similar, but plane is through v3next and line is other side of miter edge. */
3466 float co3[3];
3467 EdgeHalf *emiter_other = v3->elast;
3468 vother = BM_edge_other_vert(emiter_other->e, bv->v);
3469 sub_v3_v3v3(edge_dir, bv->v->co, vother->co);
3470 normalize_v3(edge_dir);
3471 madd_v3_v3v3fl(line_p, co2, edge_dir, d);
3472 if (!isect_line_plane_v3(co3, co2, line_p, v3next->nv.co, edge_dir)) {
3473 copy_v3_v3(co1, line_p);
3474 }
3475 adjust_bound_vert(v3, co3);
3476}
3477
3479{
3480 BoundVert *vstart = bv->vmesh->boundstart;
3481 BoundVert *v = vstart;
3482 do {
3483 if (v->is_arc_start) {
3484 BoundVert *v3 = v->next;
3485 EdgeHalf *e = v->efirst;
3486 if (e != emiter) {
3487 float edge_dir[3], co[3];
3488 copy_v3_v3(co, v->nv.co);
3489 BMVert *vother = BM_edge_other_vert(e->e, bv->v);
3490 sub_v3_v3v3(edge_dir, vother->co, bv->v->co);
3491 normalize_v3(edge_dir);
3492 madd_v3_v3v3fl(v->nv.co, co, edge_dir, bp->spread);
3493 e = v3->elast;
3494 vother = BM_edge_other_vert(e->e, bv->v);
3495 sub_v3_v3v3(edge_dir, vother->co, bv->v->co);
3496 normalize_v3(edge_dir);
3497 madd_v3_v3v3fl(v3->nv.co, co, edge_dir, bp->spread);
3498 }
3499 v = v3->next;
3500 }
3501 else {
3502 v = v->next;
3503 }
3504 } while (v != vstart);
3505}
3506
3521static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
3522{
3523 MemArena *mem_arena = bp->mem_arena;
3524
3525 /* Current bevel does nothing if only one edge into a vertex. */
3526 if (bv->edgecount <= 1) {
3527 return;
3528 }
3529
3531 build_boundary_vertex_only(bp, bv, construct);
3532 return;
3533 }
3534
3535 VMesh *vm = bv->vmesh;
3536
3537 /* Find a beveled edge to be efirst. */
3538 EdgeHalf *efirst = next_bev(bv, nullptr);
3539 BLI_assert(efirst->is_bev);
3540
3541 if (bv->selcount == 1) {
3542 /* Special case: only one beveled edge in. */
3543 build_boundary_terminal_edge(bp, bv, efirst, construct);
3544 return;
3545 }
3546
3547 /* Special miters outside only for 3 or more beveled edges. */
3548 int miter_outer = (bv->selcount >= 3) ? bp->miter_outer : BEVEL_MITER_SHARP;
3549 int miter_inner = bp->miter_inner;
3550
3551 /* Keep track of the first beveled edge of an outside miter (there can be at most 1 per bv). */
3552 EdgeHalf *emiter = nullptr;
3553
3554 /* There is more than one beveled edge.
3555 * We make BoundVerts to connect the sides of the beveled edges.
3556 * Non-beveled edges in between will just join to the appropriate juncture point. */
3557 EdgeHalf *e = efirst;
3558 do {
3559 BLI_assert(e->is_bev);
3560 EdgeHalf *eon = nullptr;
3561 /* Make the BoundVert for the right side of e; the other side will be made when the beveled
3562 * edge to the left of e is handled.
3563 * Analyze edges until next beveled edge: They are either "in plane" (preceding and subsequent
3564 * faces are coplanar) or not. The "non-in-plane" edges affect the silhouette and we prefer to
3565 * slide along one of those if possible. */
3566 int in_plane = 0; /* Counts of in-plane / not-in-plane. */
3567 int not_in_plane = 0;
3568 EdgeHalf *enip = nullptr; /* Representatives of each type. */
3569 EdgeHalf *eip = nullptr;
3570 EdgeHalf *e2;
3571 for (e2 = e->next; !e2->is_bev; e2 = e2->next) {
3572 if (eh_on_plane(e2)) {
3573 in_plane++;
3574 eip = e2;
3575 }
3576 else {
3577 not_in_plane++;
3578 enip = e2;
3579 }
3580 }
3581
3582 float r, co[3];
3583 if (in_plane == 0 && not_in_plane == 0) {
3584 offset_meet(bp, e, e2, bv->v, e->fnext, false, co, nullptr);
3585 }
3586 else if (not_in_plane > 0) {
3587 if (bp->loop_slide && not_in_plane == 1 && good_offset_on_edge_between(e, e2, enip, bv->v)) {
3588 if (offset_on_edge_between(bp, e, e2, enip, bv->v, co, &r)) {
3589 eon = enip;
3590 }
3591 }
3592 else {
3593 offset_meet(bp, e, e2, bv->v, nullptr, true, co, eip);
3594 }
3595 }
3596 else {
3597 /* n_in_plane > 0 and n_not_in_plane == 0. */
3598 if (bp->loop_slide && in_plane == 1 && good_offset_on_edge_between(e, e2, eip, bv->v)) {
3599 if (offset_on_edge_between(bp, e, e2, eip, bv->v, co, &r)) {
3600 eon = eip;
3601 }
3602 }
3603 else {
3604 /* Since all edges between e and e2 are in the same plane, it is OK
3605 * to treat this like the case where there are no edges between. */
3606 offset_meet(bp, e, e2, bv->v, e->fnext, false, co, nullptr);
3607 }
3608 }
3609
3610 if (construct) {
3611 BoundVert *v = add_new_bound_vert(mem_arena, vm, co);
3612 v->efirst = e;
3613 v->elast = e2;
3614 v->ebev = e2;
3615 v->eon = eon;
3616 if (eon) {
3617 v->sinratio = r;
3618 }
3619 e->rightv = v;
3620 e2->leftv = v;
3621 for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3622 e3->leftv = e3->rightv = v;
3623 }
3624 AngleKind ang_kind = edges_angle_kind(e, e2, bv->v);
3625
3626 /* Are we doing special mitering?
3627 * There can only be one outer reflex angle, so only one outer miter,
3628 * and emiter will be set to the first edge of such an edge.
3629 * A miter kind of BEVEL_MITER_SHARP means no special miter */
3630 if ((miter_outer != BEVEL_MITER_SHARP && !emiter && ang_kind == ANGLE_LARGER) ||
3631 (miter_inner != BEVEL_MITER_SHARP && ang_kind == ANGLE_SMALLER))
3632 {
3633 if (ang_kind == ANGLE_LARGER) {
3634 emiter = e;
3635 }
3636 /* Make one or two more boundverts; for now all will have same co. */
3637 BoundVert *v1 = v;
3638 v1->ebev = nullptr;
3639 BoundVert *v2;
3640 if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3641 v2 = add_new_bound_vert(mem_arena, vm, co);
3642 }
3643 else {
3644 v2 = nullptr;
3645 }
3646 BoundVert *v3 = add_new_bound_vert(mem_arena, vm, co);
3647 v3->ebev = e2;
3648 v3->efirst = e2;
3649 v3->elast = e2;
3650 v3->eon = nullptr;
3651 e2->leftv = v3;
3652 if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3653 v1->is_patch_start = true;
3654 v2->eon = v1->eon;
3655 v2->sinratio = v1->sinratio;
3656 v2->ebev = nullptr;
3657 v1->eon = nullptr;
3658 v1->sinratio = 1.0f;
3659 v1->elast = e;
3660 if (e->next == e2) {
3661 v2->efirst = nullptr;
3662 v2->elast = nullptr;
3663 }
3664 else {
3665 v2->efirst = e->next;
3666 for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3667 e3->leftv = e3->rightv = v2;
3668 v2->elast = e3;
3669 }
3670 }
3671 }
3672 else {
3673 v1->is_arc_start = true;
3674 copy_v3_v3(v1->profile.middle, co);
3675 if (e->next == e2) {
3676 v1->elast = v1->efirst;
3677 }
3678 else {
3679 int between = in_plane + not_in_plane;
3680 int bet2 = between / 2;
3681 bool betodd = (between % 2) == 1;
3682 int i = 0;
3683 /* Put first half of in-between edges at index 0, second half at index bp->seg.
3684 * If between is odd, put middle one at mid-index. */
3685 for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3686 v1->elast = e3;
3687 if (i < bet2) {
3688 e3->profile_index = 0;
3689 }
3690 else if (betodd && i == bet2) {
3691 e3->profile_index = bp->seg / 2;
3692 }
3693 else {
3694 e3->profile_index = bp->seg;
3695 }
3696 i++;
3697 }
3698 }
3699 }
3700 }
3701 }
3702 else { /* construct == false. */
3703 AngleKind ang_kind = edges_angle_kind(e, e2, bv->v);
3704 if ((miter_outer != BEVEL_MITER_SHARP && !emiter && ang_kind == ANGLE_LARGER) ||
3705 (miter_inner != BEVEL_MITER_SHARP && ang_kind == ANGLE_SMALLER))
3706 {
3707 if (ang_kind == ANGLE_LARGER) {
3708 emiter = e;
3709 }
3710 BoundVert *v1 = e->rightv;
3711 BoundVert *v2;
3712 BoundVert *v3;
3713 if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3714 v2 = v1->next;
3715 v3 = v2->next;
3716 }
3717 else {
3718 v2 = nullptr;
3719 v3 = v1->next;
3720 }
3721 adjust_bound_vert(v1, co);
3722 if (v2) {
3723 adjust_bound_vert(v2, co);
3724 }
3725 adjust_bound_vert(v3, co);
3726 }
3727 else {
3728 adjust_bound_vert(e->rightv, co);
3729 }
3730 }
3731 e = e2;
3732 } while (e != efirst);
3733
3734 if (miter_inner != BEVEL_MITER_SHARP) {
3735 adjust_miter_inner_coords(bp, bv, emiter);
3736 }
3737 if (emiter) {
3738 adjust_miter_coords(bp, bv, emiter);
3739 }
3740
3741 if (construct) {
3743
3744 if (vm->count == 2) {
3745 vm->mesh_kind = M_NONE;
3746 }
3747 else if (efirst->seg == 1) {
3748 vm->mesh_kind = M_POLY;
3749 }
3750 else {
3751 switch (bp->vmesh_method) {
3752 case BEVEL_VMESH_ADJ:
3753 vm->mesh_kind = M_ADJ;
3754 break;
3755 case BEVEL_VMESH_CUTOFF:
3756 vm->mesh_kind = M_CUTOFF;
3757 break;
3758 }
3759 }
3760 }
3761}
3762
3763#ifdef DEBUG_ADJUST
3764static void print_adjust_stats(BoundVert *vstart)
3765{
3766 printf("\nSolution analysis\n");
3767 double even_residual2 = 0.0;
3768 double spec_residual2 = 0.0;
3769 double max_even_r = 0.0;
3770 double max_even_r_pct = 0.0;
3771 double max_spec_r = 0.0;
3772 double max_spec_r_pct = 0.0;
3773 printf("width matching\n");
3774 BoundVert *v = vstart;
3775 do {
3776 if (v->adjchain != nullptr) {
3777 EdgeHalf *eright = v->efirst;
3778 EdgeHalf *eleft = v->adjchain->elast;
3779 double delta = fabs(eright->offset_r - eleft->offset_l);
3780 double delta_pct = 100.0 * delta / eright->offset_r_spec;
3781 printf("e%d r(%f) vs l(%f): abs(delta)=%f, delta_pct=%f\n",
3782 BM_elem_index_get(eright->e),
3783 eright->offset_r,
3784 eleft->offset_l,
3785 delta,
3786 delta_pct);
3787 even_residual2 += delta * delta;
3788 if (delta > max_even_r) {
3789 max_even_r = delta;
3790 }
3791 if (delta_pct > max_even_r_pct) {
3792 max_even_r_pct = delta_pct;
3793 }
3794 }
3795 v = v->adjchain;
3796 } while (v && v != vstart);
3797
3798 printf("spec matching\n");
3799 v = vstart;
3800 do {
3801 if (v->adjchain != nullptr) {
3802 EdgeHalf *eright = v->efirst;
3803 EdgeHalf *eleft = v->adjchain->elast;
3804 double delta = eright->offset_r - eright->offset_r_spec;
3805 double delta_pct = 100.0 * delta / eright->offset_r_spec;
3806 printf("e%d r(%f) vs r spec(%f): delta=%f, delta_pct=%f\n",
3807 BM_elem_index_get(eright->e),
3808 eright->offset_r,
3809 eright->offset_r_spec,
3810 delta,
3811 delta_pct);
3812 spec_residual2 += delta * delta;
3813 delta = fabs(delta);
3814 delta_pct = fabs(delta_pct);
3815 if (delta > max_spec_r) {
3816 max_spec_r = delta;
3817 }
3818 if (delta_pct > max_spec_r_pct) {
3819 max_spec_r_pct = delta_pct;
3820 }
3821
3822 delta = eleft->offset_l - eleft->offset_l_spec;
3823 delta_pct = 100.0 * delta / eright->offset_l_spec;
3824 printf("e%d l(%f) vs l spec(%f): delta=%f, delta_pct=%f\n",
3825 BM_elem_index_get(eright->e),
3826 eleft->offset_l,
3827 eleft->offset_l_spec,
3828 delta,
3829 delta_pct);
3830 spec_residual2 += delta * delta;
3831 delta = fabs(delta);
3832 delta_pct = fabs(delta_pct);
3833 if (delta > max_spec_r) {
3834 max_spec_r = delta;
3835 }
3836 if (delta_pct > max_spec_r_pct) {
3837 max_spec_r_pct = delta_pct;
3838 }
3839 }
3840 v = v->adjchain;
3841 } while (v && v != vstart);
3842
3843 printf("Analysis Result:\n");
3844 printf("even residual2 = %f, spec residual2 = %f\n", even_residual2, spec_residual2);
3845 printf("max even delta = %f, max as percent of spec = %f\n", max_even_r, max_even_r_pct);
3846 printf("max spec delta = %f, max as percent of spec = %f\n", max_spec_r, max_spec_r_pct);
3847}
3848#endif
3849
3850#ifdef FAST_ADJUST_CODE
3851/* This code uses a direct solution to the adjustment problem for chains and certain cycles.
3852 * It is a two-step approach: first solve for the exact solution of the 'match widths' constraints
3853 * using the one degree of freedom that allows for expressing all other widths in terms of that.
3854 * And then minimize the spec-matching constraints using the derivative of the least squares
3855 * residual in terms of that one degree of freedom.
3856 * Unfortunately, the results are in some cases worse than the general least squares solution
3857 * for the combined (with weights) problem, so this code is not used.
3858 * But keep it here for a while in case performance issues demand that it be used sometimes. */
3859static bool adjust_the_cycle_or_chain_fast(BoundVert *vstart, int np, bool iscycle)
3860{
3861 float *g = MEM_new_array_uninitialized<float>(np, "beveladjust");
3862 float *g_prod = MEM_new_array_uninitialized<float>(np, "beveladjust");
3863
3864 BoundVert *v = vstart;
3865 float spec_sum = 0.0f;
3866 int i = 0;
3867 do {
3868 g[i] = v->sinratio;
3869 if (iscycle || v->adjchain != nullptr) {
3870 spec_sum += v->efirst->offset_r;
3871 }
3872 else {
3873 spec_sum += v->elast->offset_l;
3874 }
3875 i++;
3876 v = v->adjchain;
3877 } while (v && v != vstart);
3878
3879 float gprod = 1.00f;
3880 float gprod_sum = 1.0f;
3881 for (i = np - 1; i > 0; i--) {
3882 gprod *= g[i];
3883 g_prod[i] = gprod;
3884 gprod_sum += gprod;
3885 }
3886 g_prod[0] = 1.0f;
3887 if (iscycle) {
3888 gprod *= g[0];
3889 if (fabs(gprod - 1.0f) > BEVEL_EPSILON) {
3890 /* Fast cycle calc only works if total product is 1. */
3891 MEM_delete(g);
3892 MEM_delete(g_prod);
3893 return false;
3894 }
3895 }
3896 if (gprod_sum == 0.0f) {
3897 MEM_delete(g);
3898 MEM_delete(g_prod);
3899 return false;
3900 }
3901 float p = spec_sum / gprod_sum;
3902
3903 /* Apply the new offsets. */
3904 v = vstart;
3905 i = 0;
3906 do {
3907 if (iscycle || v->adjchain != nullptr) {
3908 EdgeHalf *eright = v->efirst;
3909 EdgeHalf *eleft = v->elast;
3910 eright->offset_r = g_prod[(i + 1) % np] * p;
3911 if (iscycle || v != vstart) {
3912 eleft->offset_l = v->sinratio * eright->offset_r;
3913 }
3914 }
3915 else {
3916 /* Not a cycle, and last of chain. */
3917 EdgeHalf *eleft = v->elast;
3918 eleft->offset_l = p;
3919 }
3920 i++;
3921 v = v->adjchain;
3922 } while (v && v != vstart);
3923
3924 MEM_delete(g);
3925 MEM_delete(g_prod);
3926 return true;
3927}
3928#endif
3929
3943 EdgeHalf *start_edge,
3944 bool toward_bv,
3945 BevVert **r_bv)
3946{
3947 /* Case 1: The next EdgeHalf is the other side of the BMEdge.
3948 * It's part of the same BMEdge, so we know the other EdgeHalf is also beveled. */
3949 if (!toward_bv) {
3950 return find_other_end_edge_half(bp, start_edge, r_bv);
3951 }
3952
3953 /* Case 2: The next EdgeHalf is across a BevVert from the current EdgeHalf. */
3954 /* Skip all the logic if there's only one beveled edge at the vertex, we're at an end. */
3955 if ((*r_bv)->selcount == 1) {
3956 return nullptr; /* No other edges to go to. */
3957 }
3958
3959 /* The case with only one other edge connected to the vertex is special too. */
3960 if ((*r_bv)->selcount == 2) {
3961 /* Just find the next beveled edge, that's the only other option. */
3962 EdgeHalf *new_edge = start_edge;
3963 do {
3964 new_edge = new_edge->next;
3965 } while (!new_edge->is_bev);
3966
3967 return new_edge;
3968 }
3969
3970 /* Find the direction vector of the current edge (pointing INTO the BevVert).
3971 * v1 and v2 don't necessarily have an order, so we need to check which is closer to bv. */
3972 float dir_start_edge[3];
3973 if (start_edge->e->v1 == (*r_bv)->v) {
3974 sub_v3_v3v3(dir_start_edge, start_edge->e->v1->co, start_edge->e->v2->co);
3975 }
3976 else {
3977 sub_v3_v3v3(dir_start_edge, start_edge->e->v2->co, start_edge->e->v1->co);
3978 }
3979 normalize_v3(dir_start_edge);
3980
3981 /* Find the beveled edge coming out of the BevVert that's most parallel to the current edge. */
3982 EdgeHalf *new_edge = start_edge->next;
3983 float second_best_dot = 0.0f, best_dot = 0.0f;
3984 EdgeHalf *next_edge = nullptr;
3985 while (new_edge != start_edge) {
3986 if (!new_edge->is_bev) {
3987 new_edge = new_edge->next;
3988 continue;
3989 }
3990 /* Find direction vector of the possible next edge (pointing OUT of the BevVert). */
3991 float dir_new_edge[3];
3992 if (new_edge->e->v2 == (*r_bv)->v) {
3993 sub_v3_v3v3(dir_new_edge, new_edge->e->v1->co, new_edge->e->v2->co);
3994 }
3995 else {
3996 sub_v3_v3v3(dir_new_edge, new_edge->e->v2->co, new_edge->e->v1->co);
3997 }
3998 normalize_v3(dir_new_edge);
3999
4000 /* Use this edge if it is the most parallel to the original so far. */
4001 float new_dot = dot_v3v3(dir_new_edge, dir_start_edge);
4002 if (new_dot > best_dot) {
4003 second_best_dot = best_dot; /* For remembering if the choice was too close. */
4004 best_dot = new_dot;
4005 next_edge = new_edge;
4006 }
4007 else if (new_dot > second_best_dot) {
4008 second_best_dot = new_dot;
4009 }
4010
4011 new_edge = new_edge->next;
4012 }
4013
4014 /* Only return a new Edge if one was found and if the choice of next edge was not too close. */
4015 if ((next_edge != nullptr) && compare_ff(best_dot, second_best_dot, BEVEL_SMALL_ANG_DOT)) {
4016 return nullptr;
4017 }
4018 return next_edge;
4019}
4020
4028{
4029 BevVert *start_bv = find_bevvert(bp, bme->v1);
4030 EdgeHalf *start_edgehalf = find_edge_half(start_bv, bme);
4031 if (!start_edgehalf->is_bev || start_edgehalf->visited_rpo) {
4032 return;
4033 }
4034
4035 /* Pick a BoundVert on one side of the profile to use for the starting side. Use the one highest
4036 * on the Z axis because even any rule is better than an arbitrary decision. */
4037 bool right_highest = start_edgehalf->leftv->nv.co[2] < start_edgehalf->rightv->nv.co[2];
4038 start_edgehalf->leftv->is_profile_start = right_highest;
4039 start_edgehalf->visited_rpo = true;
4040
4041 /* First loop starts in the away from BevVert direction and the second starts toward it. */
4042 for (int i = 0; i < 2; i++) {
4043 EdgeHalf *edgehalf = start_edgehalf;
4044 BevVert *bv = start_bv;
4045 bool toward_bv = (i == 0);
4046 edgehalf = next_edgehalf_bev(bp, edgehalf, toward_bv, &bv);
4047
4048 /* Keep traveling until there is no unvisited beveled edgehalf to visit next. */
4049 while (edgehalf && !edgehalf->visited_rpo) {
4050 /* Mark the correct BoundVert as the start of the newly visited profile.
4051 * The direction relative to the BevVert switches every step, so also switch
4052 * the orientation every step. */
4053 if (i == 0) {
4054 edgehalf->leftv->is_profile_start = toward_bv ^ right_highest;
4055 }
4056 else {
4057 /* The opposite side as the first direction because we're moving the other way. */
4058 edgehalf->leftv->is_profile_start = (!toward_bv) ^ right_highest;
4059 }
4060
4061 /* The next jump will in the opposite direction relative to the BevVert. */
4062 toward_bv = !toward_bv;
4063
4064 edgehalf->visited_rpo = true;
4065 edgehalf = next_edgehalf_bev(bp, edgehalf, toward_bv, &bv);
4066 }
4067 }
4068}
4069
4078static void adjust_the_cycle_or_chain(BoundVert *vstart, bool iscycle)
4079{
4080 int np = 0;
4081#ifdef DEBUG_ADJUST
4082 printf("\nadjust the %s (with eigen)\n", iscycle ? "cycle" : "chain");
4083#endif
4084 BoundVert *v = vstart;
4085 do {
4086#ifdef DEBUG_ADJUST
4087 eleft = v->elast;
4088 eright = v->efirst;
4089 printf(" (left=e%d, right=e%d)", BM_elem_index_get(eleft->e), BM_elem_index_get(eright->e));
4090#endif
4091 np++;
4092 v = v->adjchain;
4093 } while (v && v != vstart);
4094#ifdef DEBUG_ADJUST
4095 printf(" -> %d parms\n", np);
4096#endif
4097
4098#ifdef FAST_ADJUST_CODE
4099 if (adjust_the_cycle_or_chain_fast(vstart, np, iscycle)) {
4100 return;
4101 }
4102#endif
4103
4104 int nrows = iscycle ? 3 * np : 3 * np - 3;
4105
4106 LinearSolver *solver = EIG_linear_least_squares_solver_new(nrows, np, 1);
4107
4108 v = vstart;
4109 int i = 0;
4110 /* Square root of factor to weight down importance of spec match. */
4111 double weight = BEVEL_MATCH_SPEC_WEIGHT;
4112 EdgeHalf *eleft, *eright, *enextleft;
4113 do {
4114 /* Except at end of chain, v's indep variable is offset_r of `v->efirst`. */
4115 if (iscycle || i < np - 1) {
4116 eright = v->efirst;
4117 eleft = v->elast;
4118 enextleft = v->adjchain->elast;
4119#ifdef DEBUG_ADJUST
4120 printf("p%d: e%d->offset_r = %f\n", i, BM_elem_index_get(eright->e), eright->offset_r);
4121 if (iscycle || v != vstart) {
4122 printf(" dependent: e%d->offset_l = %f * p%d\n",
4123 BM_elem_index_get(eleft->e),
4124 v->sinratio,
4125 i);
4126 }
4127#endif
4128
4129 /* Residue i: width difference between eright and eleft of next. */
4130 EIG_linear_solver_matrix_add(solver, i, i, 1.0);
4131 EIG_linear_solver_right_hand_side_add(solver, 0, i, 0.0);
4132 if (iscycle) {
4133 EIG_linear_solver_matrix_add(solver, i > 0 ? i - 1 : np - 1, i, -v->sinratio);
4134 }
4135 else {
4136 if (i > 0) {
4137 EIG_linear_solver_matrix_add(solver, i - 1, i, -v->sinratio);
4138 }
4139 }
4140
4141 /* Residue np + 2*i (if cycle) else np - 1 + 2*i:
4142 * right offset for parameter i matches its spec; weighted. */
4143 int row = iscycle ? np + 2 * i : np - 1 + 2 * i;
4144 EIG_linear_solver_matrix_add(solver, row, i, weight);
4145 EIG_linear_solver_right_hand_side_add(solver, 0, row, weight * eright->offset_r);
4146#ifdef DEBUG_ADJUST
4147 printf("b[%d]=%f * %f, for e%d->offset_r\n",
4148 row,
4149 weight,
4150 eright->offset_r,
4151 BM_elem_index_get(eright->e));
4152#endif
4153
4154 /* Residue np + 2*i + 1 (if cycle) else np - 1 + 2*i + 1:
4155 * left offset for parameter i matches its spec; weighted. */
4156 row = row + 1;
4158 solver, row, (i == np - 1) ? 0 : i + 1, weight * v->adjchain->sinratio);
4159 EIG_linear_solver_right_hand_side_add(solver, 0, row, weight * enextleft->offset_l);
4160#ifdef DEBUG_ADJUST
4161 printf("b[%d]=%f * %f, for e%d->offset_l\n",
4162 row,
4163 weight,
4164 enextleft->offset_l,
4165 BM_elem_index_get(enextleft->e));
4166#endif
4167 }
4168 else {
4169 /* Not a cycle, and last of chain. */
4170 eleft = v->elast;
4171#ifdef DEBUG_ADJUST
4172 printf("p%d: e%d->offset_l = %f\n", i, BM_elem_index_get(eleft->e), eleft->offset_l);
4173#endif
4174 /* Second part of residue i for last i. */
4175 EIG_linear_solver_matrix_add(solver, i - 1, i, -1.0);
4176 }
4177 i++;
4178 v = v->adjchain;
4179 } while (v && v != vstart);
4181#ifdef DEBUG_ADJUST
4182 /* NOTE: this print only works after solve, but by that time b has been cleared. */
4184 printf("\nSolution:\n");
4185 for (i = 0; i < np; i++) {
4186 printf("p%d = %f\n", i, EIG_linear_solver_variable_get(solver, 0, i));
4187 }
4188#endif
4189
4190 /* Use the solution to set new widths. */
4191 v = vstart;
4192 i = 0;
4193 do {
4194 double val = EIG_linear_solver_variable_get(solver, 0, i);
4195 if (iscycle || i < np - 1) {
4196 eright = v->efirst;
4197 eleft = v->elast;
4198 eright->offset_r = float(val);
4199#ifdef DEBUG_ADJUST
4200 printf("e%d->offset_r = %f\n", BM_elem_index_get(eright->e), eright->offset_r);
4201#endif
4202 if (iscycle || v != vstart) {
4203 eleft->offset_l = float(v->sinratio * val);
4204#ifdef DEBUG_ADJUST
4205 printf("e%d->offset_l = %f\n", BM_elem_index_get(eleft->e), eleft->offset_l);
4206#endif
4207 }
4208 }
4209 else {
4210 /* Not a cycle, and last of chain. */
4211 eleft = v->elast;
4212 eleft->offset_l = float(val);
4213#ifdef DEBUG_ADJUST
4214 printf("e%d->offset_l = %f\n", BM_elem_index_get(eleft->e), eleft->offset_l);
4215#endif
4216 }
4217 i++;
4218 v = v->adjchain;
4219 } while (v && v != vstart);
4220
4221#ifdef DEBUG_ADJUST
4222 print_adjust_stats(vstart);
4224#endif
4225
4227}
4228
4241{
4242 /* Find and process chains and cycles of unvisited BoundVerts that have eon set. */
4243 /* NOTE: for repeatability, iterate over all verts of mesh rather than over ghash'ed BMVerts. */
4244 BMIter iter;
4245 BMVert *bmv;
4246 BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
4247 if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
4248 continue;
4249 }
4250 BevVert *bv = find_bevvert(bp, bmv);
4251 BevVert *bvcur = bv;
4252 if (!bv) {
4253 continue;
4254 }
4255 BoundVert *vanchor = bv->vmesh->boundstart;
4256 do {
4257 if (vanchor->visited || !vanchor->eon) {
4258 continue;
4259 }
4260
4261 /* Find one of (1) a cycle that starts and ends at v
4262 * where each v has v->eon set and had not been visited before;
4263 * or (2) a chain of v's where the start and end of the chain do not have
4264 * v->eon set but all else do.
4265 * It is OK for the first and last elements to
4266 * have been visited before, but not any of the inner ones.
4267 * We chain the v's together through v->adjchain, and are following
4268 * them in left->right direction, meaning that the left side of one edge
4269 * pairs with the right side of the next edge in the cycle or chain. */
4270
4271 /* First follow paired edges in left->right direction. */
4272 BoundVert *v, *vchainstart, *vchainend;
4273 v = vchainstart = vchainend = vanchor;
4274
4275 bool iscycle = false;
4276 int chainlen = 1;
4277 while (v->eon && !v->visited && !iscycle) {
4278 v->visited = true;
4279 if (!v->efirst) {
4280 break;
4281 }
4282 EdgeHalf *enext = find_other_end_edge_half(bp, v->efirst, &bvcur);
4283 if (!enext) {
4284 break;
4285 }
4286 BLI_assert(enext != nullptr);
4287 BoundVert *vnext = enext->leftv;
4288 v->adjchain = vnext;
4289 vchainend = vnext;
4290 chainlen++;
4291 if (vnext->visited) {
4292 if (vnext != vchainstart) {
4293 break;
4294 }
4295 adjust_the_cycle_or_chain(vchainstart, true);
4296 iscycle = true;
4297 }
4298 v = vnext;
4299 }
4300 if (!iscycle) {
4301 /* right->left direction, changing vchainstart at each step. */
4302 v->adjchain = nullptr;
4303 v = vchainstart;
4304 bvcur = bv;
4305 do {
4306 v->visited = true;
4307 if (!v->elast) {
4308 break;
4309 }
4310 EdgeHalf *enext = find_other_end_edge_half(bp, v->elast, &bvcur);
4311 if (!enext) {
4312 break;
4313 }
4314 BoundVert *vnext = enext->rightv;
4315 vnext->adjchain = v;
4316 chainlen++;
4317 vchainstart = vnext;
4318 v = vnext;
4319 } while (!v->visited && v->eon);
4320 if (chainlen >= 3 && !vchainstart->eon && !vchainend->eon) {
4321 adjust_the_cycle_or_chain(vchainstart, false);
4322 }
4323 }
4324 } while ((vanchor = vanchor->next) != bv->vmesh->boundstart);
4325 }
4326
4327 /* Rebuild boundaries with new width specs. */
4328 BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
4329 if (BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
4330 BevVert *bv = find_bevvert(bp, bmv);
4331 if (bv) {
4332 build_boundary(bp, bv, false);
4333 }
4334 }
4335 }
4336}
4337
4348{
4349 VMesh *vm = bv->vmesh;
4350 if (vm->count < 3 || vm->count > 4 || bv->selcount < 3 || bv->selcount > 4) {
4351 return nullptr;
4352 }
4353
4354 /* Find v1, v2, v3 all with beveled edges, where v1 and v3 have collinear edges. */
4355 EdgeHalf *epipe = nullptr;
4356 BoundVert *v1 = vm->boundstart;
4357 float dir1[3], dir3[3];
4358 do {
4359 BoundVert *v2 = v1->next;
4360 BoundVert *v3 = v2->next;
4361 if (v1->ebev && v2->ebev && v3->ebev) {
4362 sub_v3_v3v3(dir1, bv->v->co, BM_edge_other_vert(v1->ebev->e, bv->v)->co);
4363 sub_v3_v3v3(dir3, BM_edge_other_vert(v3->ebev->e, bv->v)->co, bv->v->co);
4364 normalize_v3(dir1);
4365 normalize_v3(dir3);
4366 if (angle_normalized_v3v3(dir1, dir3) < BEVEL_EPSILON_ANG) {
4367 epipe = v1->ebev;
4368 break;
4369 }
4370 }
4371 } while ((v1 = v1->next) != vm->boundstart);
4372
4373 if (!epipe) {
4374 return nullptr;
4375 }
4376
4377 /* Check face planes: all should have normals perpendicular to epipe. */
4378 for (EdgeHalf *e = &bv->edges[0]; e != &bv->edges[bv->edgecount]; e++) {
4379 if (e->fnext) {
4380 if (fabsf(dot_v3v3(dir1, e->fnext->no)) > BEVEL_EPSILON_BIG) {
4381 return nullptr;
4382 }
4383 }
4384 }
4385 return v1;
4386}
4387
4388static VMesh *new_adj_vmesh(MemArena *mem_arena, int count, int seg, BoundVert *bounds)
4389{
4390 VMesh *vm = static_cast<VMesh *>(BLI_memarena_alloc(mem_arena, sizeof(VMesh)));
4391 vm->count = count;
4392 vm->seg = seg;
4393 vm->boundstart = bounds;
4394 vm->mesh = static_cast<NewVert *>(
4395 BLI_memarena_alloc(mem_arena, sizeof(NewVert) * count * (1 + seg / 2) * (1 + seg)));
4396 vm->mesh_kind = M_ADJ;
4397 return vm;
4398}
4399
4410static NewVert *mesh_vert_canon(VMesh *vm, int i, int j, int k)
4411{
4412 int n = vm->count;
4413 int ns = vm->seg;
4414 int ns2 = ns / 2;
4415 int odd = ns % 2;
4416 BLI_assert(0 <= i && i <= n && 0 <= j && j <= ns && 0 <= k && k <= ns);
4417
4418 if (!odd && j == ns2 && k == ns2) {
4419 return mesh_vert(vm, 0, j, k);
4420 }
4421 if (j <= ns2 - 1 + odd && k <= ns2) {
4422 return mesh_vert(vm, i, j, k);
4423 }
4424 if (k <= ns2) {
4425 return mesh_vert(vm, (i + n - 1) % n, k, ns - j);
4426 }
4427 return mesh_vert(vm, (i + 1) % n, ns - k, j);
4428}
4429
4430static bool is_canon(VMesh *vm, int i, int j, int k)
4431{
4432 int ns2 = vm->seg / 2;
4433 if (vm->seg % 2 == 1) { /* Odd. */
4434 return (j <= ns2 && k <= ns2);
4435 }
4436 /* Even. */
4437 return ((j < ns2 && k <= ns2) || (j == ns2 && k == ns2 && i == 0));
4438}
4439
4440/* Copy the vertex data to all of vm verts from canonical ones. */
4442{
4443 int n = vm->count;
4444 int ns = vm->seg;
4445 int ns2 = ns / 2;
4446 for (int i = 0; i < n; i++) {
4447 for (int j = 0; j <= ns2; j++) {
4448 for (int k = 0; k <= ns; k++) {
4449 if (is_canon(vm, i, j, k)) {
4450 continue;
4451 }
4452 NewVert *v1 = mesh_vert(vm, i, j, k);
4453 NewVert *v0 = mesh_vert_canon(vm, i, j, k);
4454 copy_v3_v3(v1->co, v0->co);
4455 v1->v = v0->v;
4456 }
4457 }
4458 }
4459}
4460
4461/* Calculate and return in r_cent the centroid of the center poly. */
4462static void vmesh_center(VMesh *vm, float r_cent[3])
4463{
4464 int n = vm->count;
4465 int ns2 = vm->seg / 2;
4466 if (vm->seg % 2) {
4467 zero_v3(r_cent);
4468 for (int i = 0; i < n; i++) {
4469 add_v3_v3(r_cent, mesh_vert(vm, i, ns2, ns2)->co);
4470 }
4471 mul_v3_fl(r_cent, 1.0f / float(n));
4472 }
4473 else {
4474 copy_v3_v3(r_cent, mesh_vert(vm, 0, ns2, ns2)->co);
4475 }
4476}
4477
4478static void avg4(
4479 float co[3], const NewVert *v0, const NewVert *v1, const NewVert *v2, const NewVert *v3)
4480{
4481 add_v3_v3v3(co, v0->co, v1->co);
4482 add_v3_v3(co, v2->co);
4483 add_v3_v3(co, v3->co);
4484 mul_v3_fl(co, 0.25f);
4485}
4486
4487/* Gamma needed for smooth Catmull-Clark, Sabin modification. */
4488static float sabin_gamma(int n)
4489{
4490 /* pPrecalculated for common cases of n. */
4491 if (n < 3) {
4492 return 0.0f;
4493 }
4494 if (n == 3) {
4495 return 0.065247584f;
4496 }
4497 if (n == 4) {
4498 return 0.25f;
4499 }
4500 if (n == 5) {
4501 return 0.401983447f;
4502 }
4503 if (n == 6) {
4504 return 0.523423277f;
4505 }
4506 double k = cos(M_PI / double(n));
4507 /* Need x, real root of x^3 + (4k^2 - 3)x - 2k = 0.
4508 * Answer calculated via Wolfram Alpha. */
4509 double k2 = k * k;
4510 double k4 = k2 * k2;
4511 double k6 = k4 * k2;
4512 double y = pow(M_SQRT3 * sqrt(64.0 * k6 - 144.0 * k4 + 135.0 * k2 - 27.0) + 9.0 * k, 1.0 / 3.0);
4513 double x = 0.480749856769136 * y - (0.231120424783545 * (12.0 * k2 - 9.0)) / y;
4514 return (k * x + 2.0 * k2 - 1.0) / (x * x * (k * x + 1.0));
4515}
4516
4517/* Fill frac with fractions of the way along ring 0 for vertex i, for use with interp_range
4518 * function. */
4519static void fill_vmesh_fracs(VMesh *vm, float *frac, int i)
4520{
4521 float total = 0.0f;
4522
4523 int ns = vm->seg;
4524 frac[0] = 0.0f;
4525 for (int k = 0; k < ns; k++) {
4526 total += len_v3v3(mesh_vert(vm, i, 0, k)->co, mesh_vert(vm, i, 0, k + 1)->co);
4527 frac[k + 1] = total;
4528 }
4529 if (total > 0.0f) {
4530 for (int k = 1; k <= ns; k++) {
4531 frac[k] /= total;
4532 }
4533 }
4534 else {
4535 frac[ns] = 1.0f;
4536 }
4537}
4538
4539/* Like fill_vmesh_fracs but want fractions for profile points of bndv, with ns segments. */
4540static void fill_profile_fracs(BevelParams *bp, BoundVert *bndv, float *frac, int ns)
4541{
4542 float co[3], nextco[3];
4543 float total = 0.0f;
4544
4545 frac[0] = 0.0f;
4546 copy_v3_v3(co, bndv->nv.co);
4547 for (int k = 0; k < ns; k++) {
4548 get_profile_point(bp, &bndv->profile, k + 1, ns, nextco);
4549 total += len_v3v3(co, nextco);
4550 frac[k + 1] = total;
4551 copy_v3_v3(co, nextco);
4552 }
4553 if (total > 0.0f) {
4554 for (int k = 1; k <= ns; k++) {
4555 frac[k] /= total;
4556 }
4557 }
4558 else {
4559 frac[ns] = 1.0f;
4560 }
4561}
4562
4563/* Return i such that frac[i] <= f <= frac[i + 1], where frac[n] == 1.0
4564 * and put fraction of rest of way between frac[i] and frac[i + 1] into r_rest. */
4565static int interp_range(const float *frac, int n, const float f, float *r_rest)
4566{
4567 /* Could binary search in frac, but expect n to be reasonably small. */
4568 for (int i = 0; i < n; i++) {
4569 if (f <= frac[i + 1]) {
4570 float rest = f - frac[i];
4571 if (rest == 0) {
4572 *r_rest = 0.0f;
4573 }
4574 else {
4575 *r_rest = rest / (frac[i + 1] - frac[i]);
4576 }
4577 if (i == n - 1 && *r_rest == 1.0f) {
4578 i = n;
4579 *r_rest = 0.0f;
4580 }
4581 return i;
4582 }
4583 }
4584 *r_rest = 0.0f;
4585 return n;
4586}
4587
4588/* Interpolate given vmesh to make one with target nseg border vertices on the profiles.
4589 * TODO(Hans): This puts the center mesh vert at a slightly off location sometimes, which seems to
4590 * be associated with the rest of that ring being shifted or connected slightly incorrectly to its
4591 * neighbors. */
4592static VMesh *interp_vmesh(BevelParams *bp, VMesh *vm_in, int nseg)
4593{
4594 int n_bndv = vm_in->count;
4595 int ns_in = vm_in->seg;
4596 int nseg2 = nseg / 2;
4597 int odd = nseg % 2;
4598 VMesh *vm_out = new_adj_vmesh(bp->mem_arena, n_bndv, nseg, vm_in->boundstart);
4599
4600 float *prev_frac = BLI_array_alloca(prev_frac, (ns_in + 1));
4601 float *frac = BLI_array_alloca(frac, (ns_in + 1));
4602 float *new_frac = BLI_array_alloca(new_frac, (nseg + 1));
4603 float *prev_new_frac = BLI_array_alloca(prev_new_frac, (nseg + 1));
4604
4605 fill_vmesh_fracs(vm_in, prev_frac, n_bndv - 1);
4606 BoundVert *bndv = vm_in->boundstart;
4607 fill_profile_fracs(bp, bndv->prev, prev_new_frac, nseg);
4608 for (int i = 0; i < n_bndv; i++) {
4609 fill_vmesh_fracs(vm_in, frac, i);
4610 fill_profile_fracs(bp, bndv, new_frac, nseg);
4611 for (int j = 0; j <= nseg2 - 1 + odd; j++) {
4612 for (int k = 0; k <= nseg2; k++) {
4613 /* Finding the locations where "fraction" fits into previous and current "frac". */
4614 float fraction = new_frac[k];
4615 float restk;
4616 float restkprev;
4617 int k_in = interp_range(frac, ns_in, fraction, &restk);
4618 fraction = prev_new_frac[nseg - j];
4619 int k_in_prev = interp_range(prev_frac, ns_in, fraction, &restkprev);
4620 int j_in = ns_in - k_in_prev;
4621 float restj = -restkprev;
4622 if (restj > -BEVEL_EPSILON) {
4623 restj = 0.0f;
4624 }
4625 else {
4626 j_in = j_in - 1;
4627 restj = 1.0f + restj;
4628 }
4629 /* Use bilinear interpolation within the source quad; could be smarter here. */
4630 float co[3];
4631 if (restj < BEVEL_EPSILON && restk < BEVEL_EPSILON) {
4632 copy_v3_v3(co, mesh_vert_canon(vm_in, i, j_in, k_in)->co);
4633 }
4634 else {
4635 int j0inc = (restj < BEVEL_EPSILON || j_in == ns_in) ? 0 : 1;
4636 int k0inc = (restk < BEVEL_EPSILON || k_in == ns_in) ? 0 : 1;
4637 float quad[4][3];
4638 copy_v3_v3(quad[0], mesh_vert_canon(vm_in, i, j_in, k_in)->co);
4639 copy_v3_v3(quad[1], mesh_vert_canon(vm_in, i, j_in, k_in + k0inc)->co);
4640 copy_v3_v3(quad[2], mesh_vert_canon(vm_in, i, j_in + j0inc, k_in + k0inc)->co);
4641 copy_v3_v3(quad[3], mesh_vert_canon(vm_in, i, j_in + j0inc, k_in)->co);
4642 interp_bilinear_quad_v3(quad, restk, restj, co);
4643 }
4644 copy_v3_v3(mesh_vert(vm_out, i, j, k)->co, co);
4645 }
4646 }
4647 bndv = bndv->next;
4648 memcpy(prev_frac, frac, sizeof(float) * (ns_in + 1));
4649 memcpy(prev_new_frac, new_frac, sizeof(float) * (nseg + 1));
4650 }
4651 if (!odd) {
4652 float center[3];
4653 vmesh_center(vm_in, center);
4654 copy_v3_v3(mesh_vert(vm_out, 0, nseg2, nseg2)->co, center);
4655 }
4656 vmesh_copy_equiv_verts(vm_out);
4657 return vm_out;
4658}
4659
4660/* Do one step of cubic subdivision (Catmull-Clark), with special rules at boundaries.
4661 * For now, this is written assuming vm0->nseg is even and > 0.
4662 * We are allowed to modify vm_in, as it will not be used after this call.
4663 * See Levin 1999 paper: "Filling an N-sided hole using combined subdivision schemes". */
4665{
4666 float co[3];
4667
4668 int n_boundary = vm_in->count;
4669 int ns_in = vm_in->seg;
4670 int ns_in2 = ns_in / 2;
4671 BLI_assert(ns_in % 2 == 0);
4672 int ns_out = 2 * ns_in;
4673 VMesh *vm_out = new_adj_vmesh(bp->mem_arena, n_boundary, ns_out, vm_in->boundstart);
4674
4675 /* First we adjust the boundary vertices of the input mesh, storing in output mesh. */
4676 for (int i = 0; i < n_boundary; i++) {
4677 copy_v3_v3(mesh_vert(vm_out, i, 0, 0)->co, mesh_vert(vm_in, i, 0, 0)->co);
4678 for (int k = 1; k < ns_in; k++) {
4679 copy_v3_v3(co, mesh_vert(vm_in, i, 0, k)->co);
4680
4681 /* Smooth boundary rule. Custom profiles shouldn't be smoothed. */
4683 float co1[3], co2[3], acc[3];
4684 copy_v3_v3(co1, mesh_vert(vm_in, i, 0, k - 1)->co);
4685 copy_v3_v3(co2, mesh_vert(vm_in, i, 0, k + 1)->co);
4686
4687 add_v3_v3v3(acc, co1, co2);
4688 madd_v3_v3fl(acc, co, -2.0f);
4689 madd_v3_v3fl(co, acc, -1.0f / 6.0f);
4690 }
4691
4692 copy_v3_v3(mesh_vert_canon(vm_out, i, 0, 2 * k)->co, co);
4693 }
4694 }
4695 /* Now adjust odd boundary vertices in output mesh, based on even ones. */
4696 BoundVert *bndv = vm_out->boundstart;
4697 for (int i = 0; i < n_boundary; i++) {
4698 for (int k = 1; k < ns_out; k += 2) {
4699 get_profile_point(bp, &bndv->profile, k, ns_out, co);
4700
4701 /* Smooth if using a non-custom profile. */
4703 float co1[3], co2[3], acc[3];
4704 copy_v3_v3(co1, mesh_vert_canon(vm_out, i, 0, k - 1)->co);
4705 copy_v3_v3(co2, mesh_vert_canon(vm_out, i, 0, k + 1)->co);
4706
4707 add_v3_v3v3(acc, co1, co2);
4708 madd_v3_v3fl(acc, co, -2.0f);
4709 madd_v3_v3fl(co, acc, -1.0f / 6.0f);
4710 }
4711
4712 copy_v3_v3(mesh_vert_canon(vm_out, i, 0, k)->co, co);
4713 }
4714 bndv = bndv->next;
4715 }
4716 vmesh_copy_equiv_verts(vm_out);
4717
4718 /* Copy adjusted verts back into vm_in. */
4719 for (int i = 0; i < n_boundary; i++) {
4720 for (int k = 0; k < ns_in; k++) {
4721 copy_v3_v3(mesh_vert(vm_in, i, 0, k)->co, mesh_vert(vm_out, i, 0, 2 * k)->co);
4722 }
4723 }
4724
4726
4727 /* Now we do the internal vertices, using standard Catmull-Clark
4728 * and assuming all boundary vertices have valence 4. */
4729
4730 /* The new face vertices. */
4731 for (int i = 0; i < n_boundary; i++) {
4732 for (int j = 0; j < ns_in2; j++) {
4733 for (int k = 0; k < ns_in2; k++) {
4734 /* Face up and right from (j, k). */
4735 avg4(co,
4736 mesh_vert(vm_in, i, j, k),
4737 mesh_vert(vm_in, i, j, k + 1),
4738 mesh_vert(vm_in, i, j + 1, k),
4739 mesh_vert(vm_in, i, j + 1, k + 1));
4740 copy_v3_v3(mesh_vert(vm_out, i, 2 * j + 1, 2 * k + 1)->co, co);
4741 }
4742 }
4743 }
4744
4745 /* The new vertical edge vertices. */
4746 for (int i = 0; i < n_boundary; i++) {
4747 for (int j = 0; j < ns_in2; j++) {
4748 for (int k = 1; k <= ns_in2; k++) {
4749 /* Vertical edge between (j, k) and (j+1, k). */
4750 avg4(co,
4751 mesh_vert(vm_in, i, j, k),
4752 mesh_vert(vm_in, i, j + 1, k),
4753 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k - 1),
4754 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4755 copy_v3_v3(mesh_vert(vm_out, i, 2 * j + 1, 2 * k)->co, co);
4756 }
4757 }
4758 }
4759
4760 /* The new horizontal edge vertices. */
4761 for (int i = 0; i < n_boundary; i++) {
4762 for (int j = 1; j < ns_in2; j++) {
4763 for (int k = 0; k < ns_in2; k++) {
4764 /* Horizontal edge between (j, k) and (j, k+1). */
4765 avg4(co,
4766 mesh_vert(vm_in, i, j, k),
4767 mesh_vert(vm_in, i, j, k + 1),
4768 mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k + 1),
4769 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4770 copy_v3_v3(mesh_vert(vm_out, i, 2 * j, 2 * k + 1)->co, co);
4771 }
4772 }
4773 }
4774
4775 /* The new vertices, not on border. */
4776 float gamma = 0.25f;
4777 float beta = -gamma;
4778 for (int i = 0; i < n_boundary; i++) {
4779 for (int j = 1; j < ns_in2; j++) {
4780 for (int k = 1; k <= ns_in2; k++) {
4781 float co1[3], co2[3];
4782 /* co1 = centroid of adjacent new edge verts. */
4783 avg4(co1,
4784 mesh_vert_canon(vm_out, i, 2 * j, 2 * k - 1),
4785 mesh_vert_canon(vm_out, i, 2 * j, 2 * k + 1),
4786 mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k),
4787 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k));
4788 /* co2 = centroid of adjacent new face verts. */
4789 avg4(co2,
4790 mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k - 1),
4791 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k - 1),
4792 mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k + 1),
4793 mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4794 /* Combine with original vert with alpha, beta, gamma factors. */
4795 copy_v3_v3(co, co1); /* Alpha = 1.0. */
4796 madd_v3_v3fl(co, co2, beta);
4797 madd_v3_v3fl(co, mesh_vert(vm_in, i, j, k)->co, gamma);
4798 copy_v3_v3(mesh_vert(vm_out, i, 2 * j, 2 * k)->co, co);
4799 }
4800 }
4801 }
4802
4803 vmesh_copy_equiv_verts(vm_out);
4804
4805 /* The center vertex is special. */
4806 gamma = sabin_gamma(n_boundary);
4807 beta = -gamma;
4808 /* Accumulate edge verts in co1, face verts in co2. */
4809 float co1[3], co2[3];
4810 zero_v3(co1);
4811 zero_v3(co2);
4812 for (int i = 0; i < n_boundary; i++) {
4813 add_v3_v3(co1, mesh_vert(vm_out, i, ns_in, ns_in - 1)->co);
4814 add_v3_v3(co2, mesh_vert(vm_out, i, ns_in - 1, ns_in - 1)->co);
4815 add_v3_v3(co2, mesh_vert(vm_out, i, ns_in - 1, ns_in + 1)->co);
4816 }
4817 copy_v3_v3(co, co1);
4818 mul_v3_fl(co, 1.0f / float(n_boundary));
4819 madd_v3_v3fl(co, co2, beta / (2.0f * float(n_boundary)));
4820 madd_v3_v3fl(co, mesh_vert(vm_in, 0, ns_in2, ns_in2)->co, gamma);
4821 for (int i = 0; i < n_boundary; i++) {
4822 copy_v3_v3(mesh_vert(vm_out, i, ns_in, ns_in)->co, co);
4823 }
4824
4825 /* Final step: Copy the profile vertices to the VMesh's boundary. */
4826 bndv = vm_out->boundstart;
4827 for (int i = 0; i < n_boundary; i++) {
4828 int inext = (i + 1) % n_boundary;
4829 for (int k = 0; k <= ns_out; k++) {
4830 get_profile_point(bp, &bndv->profile, k, ns_out, co);
4831 copy_v3_v3(mesh_vert(vm_out, i, 0, k)->co, co);
4832 if (k >= ns_in && k < ns_out) {
4833 copy_v3_v3(mesh_vert(vm_out, inext, ns_out - k, 0)->co, co);
4834 }
4835 }
4836 bndv = bndv->next;
4837 }
4838
4839 return vm_out;
4840}
4841
4842/* Special case for cube corner, when r is PRO_SQUARE_R, meaning straight sides. */
4843static VMesh *make_cube_corner_square(MemArena *mem_arena, int nseg)
4844{
4845 int ns2 = nseg / 2;
4846 VMesh *vm = new_adj_vmesh(mem_arena, 3, nseg, nullptr);
4847 vm->count = 0; /* Reset, so the following loop will end up with correct count. */
4848 for (int i = 0; i < 3; i++) {
4849 float co[3] = {0.0f, 0.0f, 0.0f};
4850 co[i] = 1.0f;
4851 add_new_bound_vert(mem_arena, vm, co);
4852 }
4853 for (int i = 0; i < 3; i++) {
4854 for (int j = 0; j <= ns2; j++) {
4855 for (int k = 0; k <= ns2; k++) {
4856 if (!is_canon(vm, i, j, k)) {
4857 continue;
4858 }
4859 float co[3];
4860 co[i] = 1.0f;
4861 co[(i + 1) % 3] = float(k) * 2.0f / float(nseg);
4862 co[(i + 2) % 3] = float(j) * 2.0f / float(nseg);
4863 copy_v3_v3(mesh_vert(vm, i, j, k)->co, co);
4864 }
4865 }
4866 }
4868 return vm;
4869}
4870
4877static VMesh *make_cube_corner_square_in(MemArena *mem_arena, int nseg)
4878{
4879 int ns2 = nseg / 2;
4880 int odd = nseg % 2;
4881 VMesh *vm = new_adj_vmesh(mem_arena, 3, nseg, nullptr);
4882 vm->count = 0; /* Reset, so following loop will end up with correct count. */
4883 for (int i = 0; i < 3; i++) {
4884 float co[3] = {0.0f, 0.0f, 0.0f};
4885 co[i] = 1.0f;
4886 add_new_bound_vert(mem_arena, vm, co);
4887 }
4888
4889 float b;
4890 if (odd) {
4891 b = 2.0f / (2.0f * float(ns2) + float(M_SQRT2));
4892 }
4893 else {
4894 b = 2.0f / float(nseg);
4895 }
4896 for (int i = 0; i < 3; i++) {
4897 for (int k = 0; k <= ns2; k++) {
4898 float co[3];
4899 co[i] = 1.0f - float(k) * b;
4900 co[(i + 1) % 3] = 0.0f;
4901 co[(i + 2) % 3] = 0.0f;
4902 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
4903 co[(i + 1) % 3] = 1.0f - float(k) * b;
4904 co[(i + 2) % 3] = 0.0f;
4905 co[i] = 0.0f;
4906 copy_v3_v3(mesh_vert(vm, i, 0, nseg - k)->co, co);
4907 }
4908 }
4909 return vm;
4910}
4911
4919{
4920 MemArena *mem_arena = bp->mem_arena;
4921 int nseg = bp->seg;
4922 float r = bp->pro_super_r;
4923
4925 if (r == PRO_SQUARE_R) {
4926 return make_cube_corner_square(mem_arena, nseg);
4927 }
4928 if (r == PRO_SQUARE_IN_R) {
4929 return make_cube_corner_square_in(mem_arena, nseg);
4930 }
4931 }
4932
4933 /* Initial mesh has 3 sides and 2 segments on each side. */
4934 VMesh *vm0 = new_adj_vmesh(mem_arena, 3, 2, nullptr);
4935 vm0->count = 0; /* Reset, so the following loop will end up with correct count. */
4936 for (int i = 0; i < 3; i++) {
4937 float co[3] = {0.0f, 0.0f, 0.0f};
4938 co[i] = 1.0f;
4939 add_new_bound_vert(mem_arena, vm0, co);
4940 }
4941 BoundVert *bndv = vm0->boundstart;
4942 for (int i = 0; i < 3; i++) {
4943 float coc[3];
4944 /* Get point, 1/2 of the way around profile, on arc between this and next. */
4945 coc[i] = 1.0f;
4946 coc[(i + 1) % 3] = 1.0f;
4947 coc[(i + 2) % 3] = 0.0f;
4948 bndv->profile.super_r = r;
4949 copy_v3_v3(bndv->profile.start, bndv->nv.co);
4950 copy_v3_v3(bndv->profile.end, bndv->next->nv.co);
4951 copy_v3_v3(bndv->profile.middle, coc);
4952 copy_v3_v3(mesh_vert(vm0, i, 0, 0)->co, bndv->profile.start);
4953 copy_v3_v3(bndv->profile.plane_co, bndv->profile.start);
4954 cross_v3_v3v3(bndv->profile.plane_no, bndv->profile.start, bndv->profile.end);
4956 /* Calculate profiles again because we started over with new boundverts. */
4957 calculate_profile(bp, bndv, false, false); /* No custom profiles in this case. */
4958
4959 /* Just building the boundaries here, so sample the profile halfway through. */
4960 get_profile_point(bp, &bndv->profile, 1, 2, mesh_vert(vm0, i, 0, 1)->co);
4961
4962 bndv = bndv->next;
4963 }
4964 /* Center vertex. */
4965 float co[3];
4966 copy_v3_fl(co, float(M_SQRT1_3));
4967
4968 if (nseg > 2) {
4969 if (r > 1.5f) {
4970 mul_v3_fl(co, 1.4f);
4971 }
4972 else if (r < 0.75f) {
4973 mul_v3_fl(co, 0.6f);
4974 }
4975 }
4976 copy_v3_v3(mesh_vert(vm0, 0, 1, 1)->co, co);
4977
4979
4980 VMesh *vm1 = vm0;
4981 while (vm1->seg < nseg) {
4982 vm1 = cubic_subdiv(bp, vm1);
4983 }
4984 if (vm1->seg != nseg) {
4985 vm1 = interp_vmesh(bp, vm1, nseg);
4986 }
4987
4988 /* Now snap each vertex to the superellipsoid. */
4989 int ns2 = nseg / 2;
4990 for (int i = 0; i < 3; i++) {
4991 for (int j = 0; j <= ns2; j++) {
4992 for (int k = 0; k <= nseg; k++) {
4993 snap_to_superellipsoid(mesh_vert(vm1, i, j, k)->co, r, false);
4994 }
4995 }
4996 }
4997
4998 return vm1;
4999}
5000
5001/* Is this a good candidate for using tri_corner_adj_vmesh? */
5003{
5004 int in_plane_e = 0;
5005
5006 /* The superellipse snapping of this case isn't helpful with custom profiles enabled. */
5008 return -1;
5009 }
5010 if (bv->vmesh->count != 3) {
5011 return 0;
5012 }
5013
5014 /* Only use the tri-corner special case if the offset is the same for every edge. */
5015 float offset = bv->edges[0].offset_l;
5016
5017 float totang = 0.0f;
5018 for (int i = 0; i < bv->edgecount; i++) {
5019 EdgeHalf *e = &bv->edges[i];
5020 float ang = BM_edge_calc_face_angle_signed_ex(e->e, 0.0f);
5021 float absang = fabsf(ang);
5022 if (absang <= M_PI_4) {
5023 in_plane_e++;
5024 }
5025 else if (absang >= 3.0f * float(M_PI_4)) {
5026 return -1;
5027 }
5028
5029 if (e->is_bev && !compare_ff(e->offset_l, offset, BEVEL_EPSILON)) {
5030 return -1;
5031 }
5032
5033 totang += ang;
5034 }
5035 if (in_plane_e != bv->edgecount - 3) {
5036 return -1;
5037 }
5038 float angdiff = fabsf(fabsf(totang) - 3.0f * float(M_PI_2));
5039 if ((bp->pro_super_r == PRO_SQUARE_R && angdiff > float(M_PI) / 16.0f) ||
5040 (angdiff > float(M_PI_4)))
5041 {
5042 return -1;
5043 }
5044 if (bv->edgecount != 3 || bv->selcount != 3) {
5045 return 0;
5046 }
5047 return 1;
5048}
5049
5051{
5052 BoundVert *bndv = bv->vmesh->boundstart;
5053
5054 float co0[3], co1[3], co2[3];
5055 copy_v3_v3(co0, bndv->nv.co);
5056 bndv = bndv->next;
5057 copy_v3_v3(co1, bndv->nv.co);
5058 bndv = bndv->next;
5059 copy_v3_v3(co2, bndv->nv.co);
5060
5061 float mat[4][4];
5062 make_unit_cube_map(co0, co1, co2, bv->v->co, mat);
5063 int ns = bp->seg;
5064 int ns2 = ns / 2;
5066 for (int i = 0; i < 3; i++) {
5067 for (int j = 0; j <= ns2; j++) {
5068 for (int k = 0; k <= ns; k++) {
5069 float v[4];
5070 copy_v3_v3(v, mesh_vert(vm, i, j, k)->co);
5071 v[3] = 1.0f;
5072 mul_m4_v4(mat, v);
5073 copy_v3_v3(mesh_vert(vm, i, j, k)->co, v);
5074 }
5075 }
5076 }
5077
5078 return vm;
5079}
5080
5081/* Makes the mesh that replaces the original vertex, bounded by the profiles on the sides. */
5083{
5084 MemArena *mem_arena = bp->mem_arena;
5085
5086 int n_bndv = bv->vmesh->count;
5087
5088 /* Same bevel as that of 3 edges of vert in a cube. */
5089 if (n_bndv == 3 && tri_corner_test(bp, bv) != -1 && bp->pro_super_r != PRO_SQUARE_IN_R) {
5090 return tri_corner_adj_vmesh(bp, bv);
5091 }
5092
5093 /* First construct an initial control mesh, with nseg == 2. */
5094 int nseg = bv->vmesh->seg;
5095 VMesh *vm0 = new_adj_vmesh(mem_arena, n_bndv, 2, bv->vmesh->boundstart);
5096
5097 /* Find the center of the boundverts that make up the vmesh. */
5098 BoundVert *bndv = vm0->boundstart;
5099 float boundverts_center[3] = {0.0f, 0.0f, 0.0f};
5100 for (int i = 0; i < n_bndv; i++) {
5101 /* Boundaries just divide input polygon edges into 2 even segments. */
5102 copy_v3_v3(mesh_vert(vm0, i, 0, 0)->co, bndv->nv.co);
5103 get_profile_point(bp, &bndv->profile, 1, 2, mesh_vert(vm0, i, 0, 1)->co);
5104 add_v3_v3(boundverts_center, bndv->nv.co);
5105 bndv = bndv->next;
5106 }
5107 mul_v3_fl(boundverts_center, 1.0f / float(n_bndv));
5108
5109 /* To place the center vertex:
5110 * 'negative_fullest' is the reflection of the original vertex across the boundverts' center.
5111 * 'fullness' is the fraction of the way from the boundvert's centroid to the original vertex
5112 * (if positive) or to negative_fullest (if negative). */
5113 float original_vertex[3], negative_fullest[3];
5114 copy_v3_v3(original_vertex, bv->v->co);
5115 sub_v3_v3v3(negative_fullest, boundverts_center, original_vertex);
5116 add_v3_v3(negative_fullest, boundverts_center);
5117
5118 /* Find the vertex mesh's start center with the profile's fullness. */
5119 float fullness = bp->pro_spacing.fullness;
5120 float center_direction[3];
5121 sub_v3_v3v3(center_direction, original_vertex, boundverts_center);
5122 if (len_squared_v3(center_direction) > BEVEL_EPSILON_SQ) {
5124 fullness *= 2.0f;
5125 madd_v3_v3v3fl(mesh_vert(vm0, 0, 1, 1)->co, negative_fullest, center_direction, fullness);
5126 }
5127 else {
5128 madd_v3_v3v3fl(mesh_vert(vm0, 0, 1, 1)->co, boundverts_center, center_direction, fullness);
5129 }
5130 }
5131 else {
5132 copy_v3_v3(mesh_vert(vm0, 0, 1, 1)->co, boundverts_center);
5133 }
5135
5136 /* Do the subdivision process to go from the two segment start mesh to the final vertex mesh. */
5137 VMesh *vm1 = vm0;
5138 do {
5139 vm1 = cubic_subdiv(bp, vm1);
5140 } while (vm1->seg < nseg);
5141 if (vm1->seg != nseg) {
5142 vm1 = interp_vmesh(bp, vm1, nseg);
5143 }
5144 return vm1;
5145}
5146
5153static void snap_to_pipe_profile(BoundVert *vpipe, bool midline, float co[3])
5154{
5155 Profile *pro = &vpipe->profile;
5156 EdgeHalf *e = vpipe->ebev;
5157
5158 if (compare_v3v3(pro->start, pro->end, BEVEL_EPSILON_D)) {
5159 copy_v3_v3(co, pro->start);
5160 return;
5161 }
5162
5163 /* Get a plane with the normal pointing along the beveled edge. */
5164 float edir[3], plane[4];
5165 sub_v3_v3v3(edir, e->e->v1->co, e->e->v2->co);
5166 plane_from_point_normal_v3(plane, co, edir);
5167
5168 float start_plane[3], end_plane[3], middle_plane[3];
5169 closest_to_plane_v3(start_plane, plane, pro->start);
5170 closest_to_plane_v3(end_plane, plane, pro->end);
5171 closest_to_plane_v3(middle_plane, plane, pro->middle);
5172
5173 float m[4][4], minv[4][4];
5174 if (make_unit_square_map(start_plane, middle_plane, end_plane, m) && invert_m4_m4(minv, m)) {
5175 /* Transform co and project it onto superellipse. */
5176 float p[3];
5177 mul_v3_m4v3(p, minv, co);
5178 snap_to_superellipsoid(p, pro->super_r, midline);
5179
5180 float snap[3];
5181 mul_v3_m4v3(snap, m, p);
5182 copy_v3_v3(co, snap);
5183 }
5184 else {
5185 /* Planar case: just snap to line start_plane--end_plane. */
5186 float p[3];
5187 closest_to_line_segment_v3(p, co, start_plane, end_plane);
5188 copy_v3_v3(co, p);
5189 }
5190}
5191
5198{
5199 /* Some unnecessary overhead running this subdivision with custom profile snapping later on. */
5200 VMesh *vm = adj_vmesh(bp, bv);
5201
5202 /* Now snap all interior coordinates to be on the epipe profile. */
5203 int n_bndv = bv->vmesh->count;
5204 int ns = bv->vmesh->seg;
5205 int half_ns = ns / 2;
5206 int ipipe1 = vpipe->index;
5207 int ipipe2 = vpipe->next->next->index;
5208
5209 for (int i = 0; i < n_bndv; i++) {
5210 for (int j = 1; j <= half_ns; j++) {
5211 for (int k = 0; k <= half_ns; k++) {
5212 if (!is_canon(vm, i, j, k)) {
5213 continue;
5214 }
5215 /* With a custom profile just copy the shape of the profile at each ring. */
5217 /* Find both profile vertices that correspond to this point. */
5218 float *profile_point_pipe1, *profile_point_pipe2, f;
5219 if (ELEM(i, ipipe1, ipipe2)) {
5220 if (n_bndv == 3 && i == ipipe1) {
5221 /* This part of the vmesh is the triangular corner between the two pipe profiles. */
5222 int ring = max_ii(j, k);
5223 profile_point_pipe2 = mesh_vert(vm, i, 0, ring)->co;
5224 profile_point_pipe1 = mesh_vert(vm, i, ring, 0)->co;
5225 /* End profile index increases with k on one side and j on the other. */
5226 f = ((k < j) ? min_ff(j, k) : ((2.0f * ring) - j)) / (2.0f * ring);
5227 }
5228 else {
5229 /* This is part of either pipe profile boundvert area in the 4-way intersection. */
5230 profile_point_pipe1 = mesh_vert(vm, i, 0, k)->co;
5231 profile_point_pipe2 = mesh_vert(vm, (i == ipipe1) ? ipipe2 : ipipe1, 0, ns - k)->co;
5232 f = float(j) / float(ns); /* The ring index brings us closer to the other side. */
5233 }
5234 }
5235 else {
5236 /* The profile vertices are on both ends of each of the side profile's rings. */
5237 profile_point_pipe1 = mesh_vert(vm, i, j, 0)->co;
5238 profile_point_pipe2 = mesh_vert(vm, i, j, ns)->co;
5239 f = float(k) / float(ns); /* Ring runs along the pipe, so segment is used here. */
5240 }
5241
5242 /* Place the vertex by interpolating between the two profile points using the factor. */
5243 interp_v3_v3v3(mesh_vert(vm, i, j, k)->co, profile_point_pipe1, profile_point_pipe2, f);
5244 }
5245 else {
5246 /* A tricky case is for the 'square' profiles and an even nseg: we want certain
5247 * vertices to snap to the midline on the pipe, not just to one plane or the other. */
5248 bool even = (ns % 2) == 0;
5249 bool midline = even && k == half_ns &&
5250 ((i == 0 && j == half_ns) || ELEM(i, ipipe1, ipipe2));
5251 snap_to_pipe_profile(vpipe, midline, mesh_vert(vm, i, j, k)->co);
5252 }
5253 }
5254 }
5255 }
5256 return vm;
5257}
5258
5259static void get_incident_edges(BMFace *f, BMVert *v, BMEdge **r_e1, BMEdge **r_e2)
5260{
5261 *r_e1 = nullptr;
5262 *r_e2 = nullptr;
5263 if (!f) {
5264 return;
5265 }
5266
5267 BMIter iter;
5268 BMEdge *e;
5269 BM_ITER_ELEM (e, &iter, f, BM_EDGES_OF_FACE) {
5270 if (e->v1 == v || e->v2 == v) {
5271 if (*r_e1 == nullptr) {
5272 *r_e1 = e;
5273 }
5274 else if (*r_e2 == nullptr) {
5275 *r_e2 = e;
5276 }
5277 }
5278 }
5279}
5280
5281static BMEdge *find_closer_edge(float *co, BMEdge *e1, BMEdge *e2)
5282{
5283 BLI_assert(e1 != nullptr && e2 != nullptr);
5284 float dsq1 = dist_squared_to_line_segment_v3(co, e1->v1->co, e1->v2->co);
5285 float dsq2 = dist_squared_to_line_segment_v3(co, e2->v1->co, e2->v2->co);
5286 if (dsq1 < dsq2) {
5287 return e1;
5288 }
5289 return e2;
5290}
5291
5301 const BMFace *f,
5302 BoundVert *(r_internal[3]))
5303{
5304 if (f == nullptr) {
5305 return 0;
5306 }
5307 int n_internal = 0;
5308 VMesh *vm = bv->vmesh;
5309 BLI_assert(vm != nullptr);
5310 BoundVert *v = vm->boundstart;
5311 do {
5312 /* Possible speedup: do the matrix projection done by the following
5313 * once, outside the loop, or even better, cache it if ever done
5314 * in the course of Bevel. */
5315 if (BM_face_point_inside_test(f, v->nv.co)) {
5316 r_internal[n_internal++] = v;
5317 if (n_internal == 3) {
5318 break;
5319 }
5320 }
5321 } while ((v = v->next) != vm->boundstart);
5322 for (int i = n_internal; i < 3; i++) {
5323 r_internal[i] = nullptr;
5324 }
5325 return n_internal;
5326}
5327
5337{
5338 BMEdge *e1, *e2;
5339 VMesh *vm = bv->vmesh;
5340 float (*proj_co)[2] = BLI_array_alloca(proj_co, vm->count);
5341 float axis_mat[3][3];
5342 axis_dominant_v3_to_m3(axis_mat, f->no);
5343 get_incident_edges(f, bv->v, &e1, &e2);
5344 BLI_assert(e1 != nullptr && e2 != nullptr);
5345 BLI_assert(vm != nullptr);
5346 BoundVert *v = vm->boundstart;
5347 int i = 0;
5348 BoundVert *unsnapped[3];
5349 find_face_internal_boundverts(bv, f, unsnapped);
5350 do {
5351 float *co = v->nv.v->co;
5352 if (ELEM(v, unsnapped[0], unsnapped[1], unsnapped[2])) {
5353 mul_v2_m3v3(proj_co[i], axis_mat, co);
5354 }
5355 else {
5356 float snap1[3], snap2[3];
5357 closest_to_line_segment_v3(snap1, co, e1->v1->co, e1->v2->co);
5358 closest_to_line_segment_v3(snap2, co, e2->v1->co, e2->v2->co);
5359 float d1_sq = len_squared_v3v3(snap1, co);
5360 float d2_sq = len_squared_v3v3(snap2, co);
5361 if (d1_sq <= d2_sq) {
5362 mul_v2_m3v3(proj_co[i], axis_mat, snap1);
5363 }
5364 else {
5365 mul_v2_m3v3(proj_co[i], axis_mat, snap2);
5366 }
5367 }
5368 ++i;
5369 } while ((v = v->next) != vm->boundstart);
5370 float area = area_poly_v2(proj_co, vm->count);
5371 return area;
5372}
5373
5381static bool is_bad_uv_poly(BevVert *bv, BMFace *frep)
5382{
5383 BLI_assert(bv->vmesh != nullptr);
5384 float area = projected_boundary_area(bv, frep);
5385 return area < BEVEL_EPSILON_BIG;
5386}
5387
5406{
5407 int fcount = 0;
5408 BMFace *any_bmf = nullptr;
5409 bool consider_all_faces = bv->selcount == 1 || bp->affect_vertices_odd;
5410 /* Make an array that can hold maximum possible number of choices. */
5411 BMFace **fchoices = BLI_array_alloca(fchoices, bv->edgecount);
5412 /* For each choice, need to remember the unsnapped BoundVerts. */
5413
5414 for (int i = 0; i < bv->edgecount; i++) {
5415 if (!bv->edges[i].is_bev && !consider_all_faces) {
5416 continue;
5417 }
5418 BMFace *bmf1 = bv->edges[i].fprev;
5419 BMFace *bmf2 = bv->edges[i].fnext;
5420 BMFace *ftwo[2] = {bmf1, bmf2};
5421 BMFace *bmf = choose_rep_face(bp, ftwo, 2);
5422 if (bmf != nullptr) {
5423 if (any_bmf == nullptr) {
5424 any_bmf = bmf;
5425 }
5426 bool already_there = false;
5427 for (int j = fcount - 1; j >= 0; j--) {
5428 if (fchoices[j] == bmf) {
5429 already_there = true;
5430 break;
5431 }
5432 }
5433 if (!already_there) {
5435 if (is_bad_uv_poly(bv, bmf)) {
5436 continue;
5437 }
5438 }
5439 fchoices[fcount++] = bmf;
5440 }
5441 }
5442 }
5443 if (fcount == 0) {
5444 return any_bmf;
5445 }
5446 return choose_rep_face(bp, fchoices, fcount);
5447}
5448
5449static void build_center_ngon(BevelParams *bp, BMesh *bm, BevVert *bv, int mat_nr)
5450{
5451 VMesh *vm = bv->vmesh;
5455
5456 int ns2 = vm->seg / 2;
5457 BMFace *frep;
5458 BMEdge *frep_e1, *frep_e2;
5459 BoundVert *frep_unsnapped[3];
5460 if (bv->any_seam) {
5461 frep = frep_for_center_poly(bp, bv);
5462 get_incident_edges(frep, bv->v, &frep_e1, &frep_e2);
5463 find_face_internal_boundverts(bv, frep, frep_unsnapped);
5464 }
5465 else {
5466 frep = nullptr;
5467 frep_e1 = frep_e2 = nullptr;
5468 }
5469 BoundVert *v = vm->boundstart;
5470 do {
5471 int i = v->index;
5472 vv.append(mesh_vert(vm, i, ns2, ns2)->v);
5473 if (frep) {
5474 vf.append(frep);
5475 if (ELEM(v, frep_unsnapped[0], frep_unsnapped[1], frep_unsnapped[2])) {
5476 ve.append(nullptr);
5477 }
5478 else {
5479 BMEdge *frep_e = find_closer_edge(mesh_vert(vm, i, ns2, ns2)->v->co, frep_e1, frep_e2);
5480 ve.append(frep_e);
5481 }
5482 }
5483 else {
5484 vf.append(boundvert_rep_face(v, nullptr));
5485 ve.append(nullptr);
5486 }
5487 } while ((v = v->next) != vm->boundstart);
5489 bp, bm, vv.data(), vv.size(), vf.data(), frep, ve.data(), bv->v, nullptr, mat_nr, true);
5490 record_face_kind(bp, f, F_VERT);
5491}
5492
5500{
5501 VMesh *vm = bv->vmesh;
5502 int n = vm->count;
5503 int ns = vm->seg;
5504 int ns2 = ns / 2;
5505 int odd = ns % 2;
5506
5507 for (int i = 0; i < n; i++) {
5508 for (int k = 1; k < ns; k++) {
5509 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, mesh_vert(vm1, i, 0, k)->co);
5510 if (i > 0 && k <= ns2) {
5511 mesh_vert(vm, i, 0, k)->v = mesh_vert(vm, i - 1, 0, ns - k)->v;
5512 }
5513 else if (i == n - 1 && k > ns2) {
5514 mesh_vert(vm, i, 0, k)->v = mesh_vert(vm, 0, 0, ns - k)->v;
5515 }
5516 else {
5517 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
5518 }
5519 }
5520 }
5521 if (odd) {
5522 for (int i = 0; i < n; i++) {
5523 mesh_vert(vm, i, ns2, ns2)->v = mesh_vert(vm, i, 0, ns2)->v;
5524 }
5525 build_center_ngon(bp, bm, bv, bp->mat_nr);
5526 }
5527}
5528
5532static void closer_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float v[3])
5533{
5534 if (len_squared_v3v3(a, v) <= len_squared_v3v3(b, v)) {
5535 copy_v3_v3(r, a);
5536 }
5537 else {
5538 copy_v3_v3(r, b);
5539 }
5540}
5541
5555{
5556 int n_bndv = bv->vmesh->count;
5557 int ns = bv->vmesh->seg;
5558 int ns2 = ns / 2;
5559 int odd = ns % 2;
5560 float ns2inv = 1.0f / float(ns2);
5561 VMesh *vm = new_adj_vmesh(bp->mem_arena, n_bndv, ns, bv->vmesh->boundstart);
5562 int clstride = 3 * (ns2 + 1);
5563 float *centerline = MEM_new_array_uninitialized<float>(clstride * n_bndv, "bevel");
5564 bool *cset = MEM_new_array_zeroed<bool>(n_bndv, "bevel");
5565
5566 /* Find on_edge, place on bndv[i]'s elast where offset line would meet,
5567 * taking min-distance-to bv->v with position where next sector's offset line would meet. */
5568 BoundVert *bndv = vm->boundstart;
5569 for (int i = 0; i < n_bndv; i++) {
5570 float bndco[3];
5571 copy_v3_v3(bndco, bndv->nv.co);
5572 EdgeHalf *e1 = bndv->efirst;
5573 EdgeHalf *e2 = bndv->elast;
5574 AngleKind ang_kind = ANGLE_STRAIGHT;
5575 if (e1 && e2) {
5576 ang_kind = edges_angle_kind(e1, e2, bv->v);
5577 }
5578 if (bndv->is_patch_start) {
5579 mid_v3_v3v3(centerline + clstride * i, bndv->nv.co, bndv->next->nv.co);
5580 cset[i] = true;
5581 bndv = bndv->next;
5582 i++;
5583 mid_v3_v3v3(centerline + clstride * i, bndv->nv.co, bndv->next->nv.co);
5584 cset[i] = true;
5585 bndv = bndv->next;
5586 i++;
5587 /* Leave cset[i] where it was - probably false, unless i == n - 1. */
5588 }
5589 else if (bndv->is_arc_start) {
5590 e1 = bndv->efirst;
5591 e2 = bndv->next->efirst;
5592 copy_v3_v3(centerline + clstride * i, bndv->profile.middle);
5593 bndv = bndv->next;
5594 cset[i] = true;
5595 i++;
5596 /* Leave cset[i] where it was - probably false, unless i == n - 1. */
5597 }
5598 else if (ang_kind == ANGLE_SMALLER) {
5599 float dir1[3], dir2[3], co1[3], co2[3];
5600 sub_v3_v3v3(dir1, e1->e->v1->co, e1->e->v2->co);
5601 sub_v3_v3v3(dir2, e2->e->v1->co, e2->e->v2->co);
5602 add_v3_v3v3(co1, bndco, dir1);
5603 add_v3_v3v3(co2, bndco, dir2);
5604 /* Intersect e1 with line through bndv parallel to e2 to get v1co. */
5605 float meet1[3], meet2[3];
5606 int ikind = isect_line_line_v3(e1->e->v1->co, e1->e->v2->co, bndco, co2, meet1, meet2);
5607 float v1co[3];
5608 bool v1set;
5609 if (ikind == 0) {
5610 v1set = false;
5611 }
5612 else {
5613 /* If the lines are skew (ikind == 2), want meet1 which is on e1. */
5614 copy_v3_v3(v1co, meet1);
5615 v1set = true;
5616 }
5617 /* Intersect e2 with line through bndv parallel to e1 to get v2co. */
5618 ikind = isect_line_line_v3(e2->e->v1->co, e2->e->v2->co, bndco, co1, meet1, meet2);
5619 float v2co[3];
5620 bool v2set;
5621 if (ikind == 0) {
5622 v2set = false;
5623 }
5624 else {
5625 v2set = true;
5626 copy_v3_v3(v2co, meet1);
5627 }
5628
5629 /* We want on_edge[i] to be min dist to bv->v of v2co and the v1co of next iteration. */
5630 float *on_edge_cur = centerline + clstride * i;
5631 int iprev = (i == 0) ? n_bndv - 1 : i - 1;
5632 float *on_edge_prev = centerline + clstride * iprev;
5633 if (v2set) {
5634 if (cset[i]) {
5635 closer_v3_v3v3v3(on_edge_cur, on_edge_cur, v2co, bv->v->co);
5636 }
5637 else {
5638 copy_v3_v3(on_edge_cur, v2co);
5639 cset[i] = true;
5640 }
5641 }
5642 if (v1set) {
5643 if (cset[iprev]) {
5644 closer_v3_v3v3v3(on_edge_prev, on_edge_prev, v1co, bv->v->co);
5645 }
5646 else {
5647 copy_v3_v3(on_edge_prev, v1co);
5648 cset[iprev] = true;
5649 }
5650 }
5651 }
5652 bndv = bndv->next;
5653 }
5654 /* Maybe not everything was set by the previous loop. */
5655 bndv = vm->boundstart;
5656 for (int i = 0; i < n_bndv; i++) {
5657 if (!cset[i]) {
5658 float *on_edge_cur = centerline + clstride * i;
5659 EdgeHalf *e1 = bndv->next->efirst;
5660 float co1[3], co2[3];
5661 copy_v3_v3(co1, bndv->nv.co);
5662 copy_v3_v3(co2, bndv->next->nv.co);
5663 if (e1) {
5664 if (bndv->prev->is_arc_start && bndv->next->is_arc_start) {
5665 float meet1[3], meet2[3];
5666 int ikind = isect_line_line_v3(e1->e->v1->co, e1->e->v2->co, co1, co2, meet1, meet2);
5667 if (ikind != 0) {
5668 copy_v3_v3(on_edge_cur, meet1);
5669 cset[i] = true;
5670 }
5671 }
5672 else {
5673 if (bndv->prev->is_arc_start) {
5674 closest_to_line_segment_v3(on_edge_cur, co1, e1->e->v1->co, e1->e->v2->co);
5675 }
5676 else {
5677 closest_to_line_segment_v3(on_edge_cur, co2, e1->e->v1->co, e1->e->v2->co);
5678 }
5679 cset[i] = true;
5680 }
5681 }
5682 if (!cset[i]) {
5683 mid_v3_v3v3(on_edge_cur, co1, co2);
5684 cset[i] = true;
5685 }
5686 }
5687 bndv = bndv->next;
5688 }
5689
5690 /* Fill in rest of center-lines by interpolation. */
5691 float co1[3], co2[3];
5692 copy_v3_v3(co2, bv->v->co);
5693 bndv = vm->boundstart;
5694 for (int i = 0; i < n_bndv; i++) {
5695 if (odd) {
5696 float ang = 0.5f * angle_v3v3v3(bndv->nv.co, co1, bndv->next->nv.co);
5697 float finalfrac;
5698 if (ang > BEVEL_SMALL_ANG) {
5699 /* finalfrac is the length along arms of isosceles triangle with top angle 2*ang
5700 * such that the base of the triangle is 1.
5701 * This is used in interpolation along center-line in odd case.
5702 * To avoid too big a drop from bv, cap finalfrac a 0.8 arbitrarily */
5703 finalfrac = 0.5f / sinf(ang);
5704 finalfrac = std::min(finalfrac, 0.8f);
5705 }
5706 else {
5707 finalfrac = 0.8f;
5708 }
5709 ns2inv = 1.0f / (ns2 + finalfrac);
5710 }
5711
5712 float *p = centerline + clstride * i;
5713 copy_v3_v3(co1, p);
5714 p += 3;
5715 for (int j = 1; j <= ns2; j++) {
5716 interp_v3_v3v3(p, co1, co2, j * ns2inv);
5717 p += 3;
5718 }
5719 bndv = bndv->next;
5720 }
5721
5722 /* Coords of edges and mid or near-mid line. */
5723 bndv = vm->boundstart;
5724 for (int i = 0; i < n_bndv; i++) {
5725 copy_v3_v3(co1, bndv->nv.co);
5726 copy_v3_v3(co2, centerline + clstride * (i == 0 ? n_bndv - 1 : i - 1));
5727 for (int j = 0; j < ns2 + odd; j++) {
5728 interp_v3_v3v3(mesh_vert(vm, i, j, 0)->co, co1, co2, j * ns2inv);
5729 }
5730 copy_v3_v3(co2, centerline + clstride * i);
5731 for (int k = 1; k <= ns2; k++) {
5732 interp_v3_v3v3(mesh_vert(vm, i, 0, k)->co, co1, co2, k * ns2inv);
5733 }
5734 bndv = bndv->next;
5735 }
5736 if (!odd) {
5737 copy_v3_v3(mesh_vert(vm, 0, ns2, ns2)->co, bv->v->co);
5738 }
5740
5741 /* Fill in interior points by interpolation from edges to center-lines. */
5742 bndv = vm->boundstart;
5743 for (int i = 0; i < n_bndv; i++) {
5744 int im1 = (i == 0) ? n_bndv - 1 : i - 1;
5745 for (int j = 1; j < ns2 + odd; j++) {
5746 for (int k = 1; k <= ns2; k++) {
5747 float meet1[3], meet2[3];
5748 int ikind = isect_line_line_v3(mesh_vert(vm, i, 0, k)->co,
5749 centerline + clstride * im1 + 3 * k,
5750 mesh_vert(vm, i, j, 0)->co,
5751 centerline + clstride * i + 3 * j,
5752 meet1,
5753 meet2);
5754 if (ikind == 0) {
5755 /* How can this happen? fall back on interpolation in one direction if it does. */
5756 interp_v3_v3v3(mesh_vert(vm, i, j, k)->co,
5757 mesh_vert(vm, i, 0, k)->co,
5758 centerline + clstride * im1 + 3 * k,
5759 j * ns2inv);
5760 }
5761 else if (ikind == 1) {
5762 copy_v3_v3(mesh_vert(vm, i, j, k)->co, meet1);
5763 }
5764 else {
5765 mid_v3_v3v3(mesh_vert(vm, i, j, k)->co, meet1, meet2);
5766 }
5767 }
5768 }
5769 bndv = bndv->next;
5770 }
5771
5773
5774 MEM_delete(centerline);
5775 MEM_delete(cset);
5776 return vm;
5777}
5778
5780 int n_bndv,
5781 BMEdge *eprev,
5782 BMEdge *enext,
5783 BMFace **bndv_rep_faces,
5784 BMFace *center_frep,
5785 const bool *frep_beats_next)
5786{
5787 int previ = (i + n_bndv - 1) % n_bndv;
5788 int nexti = (i + 1) % n_bndv;
5789
5790 if (frep_beats_next[previ] && bndv_rep_faces[previ] == center_frep) {
5791 return eprev;
5792 }
5793 if (!frep_beats_next[i] && bndv_rep_faces[nexti] == center_frep) {
5794 return enext;
5795 }
5796 /* If n_bndv > 3 then we won't snap in the boundvert regions
5797 * that are not directly adjacent to the center-winning boundvert.
5798 * This is probably wrong, maybe getting UV positions outside the
5799 * original area, but the alternative may be even worse. */
5800 return nullptr;
5801}
5802
5825 int j,
5826 int k,
5827 int ns,
5828 int ns2,
5829 int n_bndv,
5830 BMEdge *eprev,
5831 BMEdge *enext,
5832 BMEdge *enextnext,
5833 BMFace **bndv_rep_faces,
5834 BMFace *center_frep,
5835 const bool *frep_beats_next,
5836 BMEdge *r_snap_edges[4])
5837{
5838 BLI_assert(0 <= i && i < n_bndv && 0 <= j && j < ns2 && 0 <= k && k <= ns2);
5839 for (int corner = 0; corner < 4; corner++) {
5840 r_snap_edges[corner] = nullptr;
5841 if (ns % 2 == 0) {
5842 continue;
5843 }
5844 int previ = (i + n_bndv - 1) % n_bndv;
5845 /* Make jj and kk be the j and k indices for this corner. */
5846 int jj = corner < 2 ? j : j + 1;
5847 int kk = ELEM(corner, 0, 3) ? k : k + 1;
5848 if (jj < ns2 && kk < ns2) {
5849 ; /* No snap. */
5850 }
5851 else if (jj < ns2 && kk == ns2) {
5852 /* On the left side of the center strip quads, but not on center poly. */
5853 if (!frep_beats_next[i]) {
5854 r_snap_edges[corner] = enext;
5855 }
5856 }
5857 else if (jj < ns2 && kk == ns2 + 1) {
5858 /* On the right side of the center strip quads, but not on center poly. */
5859 if (frep_beats_next[i]) {
5860 r_snap_edges[corner] = enext;
5861 }
5862 }
5863 else if (jj == ns2 && kk < ns2) {
5864 /* On the top of the top strip quads, but not on center poly. */
5865 if (frep_beats_next[previ]) {
5866 r_snap_edges[corner] = eprev;
5867 }
5868 }
5869 else if (jj == ns2 && kk == ns2) {
5870 /* Center poly vert for boundvert i. */
5871 r_snap_edges[corner] = snap_edge_for_center_vmesh_vert(
5872 i, n_bndv, eprev, enext, bndv_rep_faces, center_frep, frep_beats_next);
5873 }
5874 else if (jj == ns2 && kk == ns2 + 1) {
5875 /* Center poly vert for boundvert i+1. */
5876 int nexti = (i + 1) % n_bndv;
5877 r_snap_edges[corner] = snap_edge_for_center_vmesh_vert(
5878 nexti, n_bndv, enext, enextnext, bndv_rep_faces, center_frep, frep_beats_next);
5879 }
5880 }
5881}
5882
5889{
5890 int mat_nr = bp->mat_nr;
5891
5892 int n_bndv = bv->vmesh->count;
5893 int ns = bv->vmesh->seg;
5894 int ns2 = ns / 2;
5895 int odd = ns % 2;
5896 BLI_assert(n_bndv >= 3 && ns > 1);
5897
5898 VMesh *vm1;
5899 if (bp->pro_super_r == PRO_SQUARE_R && bv->selcount >= 3 && !odd &&
5901 {
5902 vm1 = square_out_adj_vmesh(bp, bv);
5903 }
5904 else if (vpipe) {
5905 vm1 = pipe_adj_vmesh(bp, bv, vpipe);
5906 }
5907 else if (tri_corner_test(bp, bv) == 1) {
5908 vm1 = tri_corner_adj_vmesh(bp, bv);
5909 /* The PRO_SQUARE_IN_R profile has boundary edges that merge
5910 * and no internal ring polys except possibly center ngon. */
5912 build_square_in_vmesh(bp, bm, bv, vm1);
5913 return;
5914 }
5915 }
5916 else {
5917 vm1 = adj_vmesh(bp, bv);
5918 }
5919
5920 /* Copy final vmesh into bv->vmesh, make BMVerts and BMFaces. */
5921 VMesh *vm = bv->vmesh;
5922 for (int i = 0; i < n_bndv; i++) {
5923 for (int j = 0; j <= ns2; j++) {
5924 for (int k = 0; k <= ns; k++) {
5925 if (j == 0 && ELEM(k, 0, ns)) {
5926 continue; /* Boundary corners already made. */
5927 }
5928 if (!is_canon(vm, i, j, k)) {
5929 continue;
5930 }
5931 copy_v3_v3(mesh_vert(vm, i, j, k)->co, mesh_vert(vm1, i, j, k)->co);
5932 create_mesh_bmvert(bm, vm, i, j, k, bv->v);
5933 }
5934 }
5935 }
5937
5938 /* Find and store the interpolation face for each BoundVert. */
5939 BMFace **bndv_rep_faces = BLI_array_alloca(bndv_rep_faces, n_bndv);
5940 BoundVert *bndv = vm->boundstart;
5941 do {
5942 int i = bndv->index;
5943 bndv_rep_faces[i] = boundvert_rep_face(bndv, nullptr);
5944 } while ((bndv = bndv->next) != vm->boundstart);
5945
5946 /* If odd number of segments, need data to break interpolation ties. */
5947 BMVert **center_verts = nullptr;
5948 BMEdge **center_edge_snaps = nullptr;
5949 BMFace **center_face_interps = nullptr;
5950 bool *frep_beats_next = nullptr;
5951 BMFace *center_frep = nullptr;
5952 if (odd && bp->affect_type == BEVEL_AFFECT_EDGES) {
5953 center_verts = BLI_array_alloca(center_verts, n_bndv);
5954 center_edge_snaps = BLI_array_alloca(center_edge_snaps, n_bndv);
5955 center_face_interps = BLI_array_alloca(center_face_interps, n_bndv);
5956 frep_beats_next = BLI_array_alloca(frep_beats_next, n_bndv);
5957 center_frep = frep_for_center_poly(bp, bv);
5958 for (int i = 0; i < n_bndv; i++) {
5959 center_edge_snaps[i] = nullptr;
5960 /* frep_beats_next[i] == true if frep for i is chosen over that for i + 1. */
5961 int inext = (i + 1) % n_bndv;
5962 BMFace *fchoices[2] = {bndv_rep_faces[i], bndv_rep_faces[inext]};
5963 BMFace *fwinner = choose_rep_face(bp, fchoices, 2);
5964 frep_beats_next[i] = fwinner == bndv_rep_faces[i];
5965 }
5966 }
5967 /* Make the polygons. */
5968 bndv = vm->boundstart;
5969 do {
5970 int i = bndv->index;
5971 int inext = bndv->next->index;
5972 BMFace *f = bndv_rep_faces[i];
5973 BMFace *f2 = bndv_rep_faces[inext];
5974 BMFace *fc = nullptr;
5975 if (odd && bp->affect_type == BEVEL_AFFECT_EDGES) {
5976 fc = frep_beats_next[i] ? f : f2;
5977 }
5978
5979 EdgeHalf *e, *eprev, *enext;
5981 e = bndv->efirst;
5982 eprev = bndv->prev->efirst;
5983 enext = bndv->next->efirst;
5984 }
5985 else {
5986 e = bndv->ebev;
5987 eprev = bndv->prev->ebev;
5988 enext = bndv->next->ebev;
5989 }
5990 BMEdge *bme = e ? e->e : nullptr;
5991 BMEdge *bmeprev = eprev ? eprev->e : nullptr;
5992 BMEdge *bmenext = enext ? enext->e : nullptr;
5993 /* For odd ns, make polys with lower left corner at (i,j,k) for
5994 * j in [0, ns2-1], k in [0, ns2]. And then the center ngon.
5995 * For even ns,
5996 * j in [0, ns2-1], k in [0, ns2-1].
5997 *
5998 * Recall: j is ring index, k is segment index.
5999 */
6000 for (int j = 0; j < ns2; j++) {
6001 for (int k = 0; k < ns2 + odd; k++) {
6002 /* We will create a quad with these four corners. */
6003 BMVert *bmv1 = mesh_vert(vm, i, j, k)->v;
6004 BMVert *bmv2 = mesh_vert(vm, i, j, k + 1)->v;
6005 BMVert *bmv3 = mesh_vert(vm, i, j + 1, k + 1)->v;
6006 BMVert *bmv4 = mesh_vert(vm, i, j + 1, k)->v;
6007 BMVert *bmvs[4] = {bmv1, bmv2, bmv3, bmv4};
6008 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
6009 /* For each created quad, the UVs etc. will be interpolated
6010 * in potentially a different face for each corner and may need
6011 * to snap to a particular edge before interpolating.
6012 * The fr and se arrays will be filled with the interpolation faces
6013 * and snapping edges for the for corners in the order given
6014 * in the bmvs array.
6015 */
6016 BMFace *fr[4];
6017 BMEdge *se[4] = {nullptr, nullptr, nullptr, nullptr};
6019 fr[0] = fr[1] = fr[2] = fr[3] = f2;
6020 if (j < k) {
6021 if (k == ns2 && j == ns2 - 1) {
6022 se[2] = bndv->next->efirst->e;
6023 se[3] = bme;
6024 }
6025 }
6026 else if (j == k) {
6027 /* Only one edge attached to v, since vertex only. */
6028 se[0] = se[2] = bme;
6029 if (!e->is_seam) {
6030 fr[3] = f;
6031 }
6032 }
6033 }
6034 else { /* Edge bevel. */
6035 fr[0] = fr[1] = fr[2] = fr[3] = f;
6036 if (odd) {
6037 BMEdge *b1 = (eprev && eprev->is_seam) ? bmeprev : nullptr;
6038 BMEdge *b2 = (e && e->is_seam) ? bme : nullptr;
6039 BMEdge *b3 = (enext && enext->is_seam) ? bmenext : nullptr;
6041 j,
6042 k,
6043 ns,
6044 ns2,
6045 n_bndv,
6046 b1,
6047 b2,
6048 b3,
6049 bndv_rep_faces,
6050 center_frep,
6051 frep_beats_next,
6052 se);
6053 if (k == ns2) {
6054 if (!e || e->is_seam) {
6055 fr[0] = fr[1] = fr[2] = fr[3] = fc;
6056 }
6057 else {
6058 fr[0] = fr[3] = f;
6059 fr[1] = fr[2] = f2;
6060 }
6061 if (j == ns2 - 1) {
6062 /* Use the 4th vertex of these faces as the ones used for the center polygon. */
6063 center_verts[i] = bmvs[3];
6064 center_edge_snaps[i] = se[3];
6065 center_face_interps[i] = bv->any_seam ? center_frep : f;
6066 }
6067 }
6068 }
6069 else { /* Edge bevel, Even number of segments. */
6070 if (k == ns2 - 1) {
6071 se[1] = bme;
6072 }
6073 if (j == ns2 - 1 && bndv->prev->ebev) {
6074 se[3] = bmeprev;
6075 }
6076 se[2] = se[1] != nullptr ? se[1] : se[3];
6077 }
6078 }
6079 BMFace *r_f = bev_create_ngon(
6080 bp, bm, bmvs, 4, fr, nullptr, se, bv->v, nullptr, mat_nr, true);
6081 record_face_kind(bp, r_f, F_VERT);
6082 }
6083 }
6084 } while ((bndv = bndv->next) != vm->boundstart);
6085
6086 /* Center ngon. */
6087 if (odd) {
6088 if (bp->affect_type == BEVEL_AFFECT_EDGES) {
6089 BMFace *frep = nullptr;
6090 if (bv->any_seam) {
6091 frep = frep_for_center_poly(bp, bv);
6092 }
6093 BMFace *cen_f = bev_create_ngon(bp,
6094 bm,
6095 center_verts,
6096 n_bndv,
6097 center_face_interps,
6098 frep,
6099 center_edge_snaps,
6100 bv->v,
6101 nullptr,
6102 mat_nr,
6103 true);
6104 record_face_kind(bp, cen_f, F_VERT);
6105 }
6106 else {
6107 build_center_ngon(bp, bm, bv, mat_nr);
6108 }
6109 }
6110}
6111
6124{
6125#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6126 printf("BEVEL BUILD CUTOFF\n");
6127# define F3(v) (v)[0], (v)[1], (v)[2]
6128#endif
6129 int n_bndv = bv->vmesh->count;
6130
6131 /* Find the locations for the corner vertices at the bottom of the cutoff faces. */
6132 BoundVert *bndv = bv->vmesh->boundstart;
6133 do {
6134 int i = bndv->index;
6135
6136 /* Find the "down" direction for this side of the cutoff face. */
6137 /* Find the direction along the intersection of the two adjacent profile normals. */
6138 float down_direction[3];
6139 cross_v3_v3v3(down_direction, bndv->profile.plane_no, bndv->prev->profile.plane_no);
6140 if (dot_v3v3(down_direction, bv->v->no) > 0.0f) {
6141 negate_v3(down_direction);
6142 }
6143
6144 /* Move down from the boundvert by average profile height from the two adjacent profiles. */
6145 float length = (bndv->profile.height / sqrtf(2.0f) +
6146 bndv->prev->profile.height / sqrtf(2.0f)) /
6147 2;
6148 float new_vert[3];
6149 madd_v3_v3v3fl(new_vert, bndv->nv.co, down_direction, length);
6150
6151 /* Use this location for this profile's first corner vert and the last profile's second. */
6152 copy_v3_v3(mesh_vert(bv->vmesh, i, 1, 0)->co, new_vert);
6153 copy_v3_v3(mesh_vert(bv->vmesh, bndv->prev->index, 1, 1)->co, new_vert);
6154
6155 } while ((bndv = bndv->next) != bv->vmesh->boundstart);
6156
6157#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6158 printf("Corner vertices:\n");
6159 for (int j = 0; j < n_bndv; j++) {
6160 printf(" (%.3f, %.3f, %.3f)\n", F3(mesh_vert(bv->vmesh, j, 1, 0)->co));
6161 }
6162#endif
6163
6164 /* Disable the center face if the corner vertices share the same location. */
6165 bool build_center_face = true;
6166 if (n_bndv == 3) { /* Vertices only collapse with a 3-way VMesh. */
6167 build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 0, 1, 0)->co,
6168 mesh_vert(bv->vmesh, 1, 1, 0)->co) > BEVEL_EPSILON;
6169 build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 0, 1, 0)->co,
6170 mesh_vert(bv->vmesh, 2, 1, 0)->co) > BEVEL_EPSILON;
6171 build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 1, 1, 0)->co,
6172 mesh_vert(bv->vmesh, 2, 1, 0)->co) > BEVEL_EPSILON;
6173 }
6174#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6175 printf("build_center_face: %d\n", build_center_face);
6176#endif
6177
6178 /* Create the corner vertex BMVerts. */
6179 if (build_center_face) {
6180 do {
6181 int i = bndv->index;
6182 create_mesh_bmvert(bm, bv->vmesh, i, 1, 0, bv->v);
6183 /* The second corner vertex for the previous profile shares this BMVert. */
6184 mesh_vert(bv->vmesh, bndv->prev->index, 1, 1)->v = mesh_vert(bv->vmesh, i, 1, 0)->v;
6185
6186 } while ((bndv = bndv->next) != bv->vmesh->boundstart);
6187 }
6188 else {
6189 /* Use the same BMVert for all of the corner vertices. */
6190 create_mesh_bmvert(bm, bv->vmesh, 0, 1, 0, bv->v);
6191 for (int i = 1; i < n_bndv; i++) {
6192 mesh_vert(bv->vmesh, i, 1, 0)->v = mesh_vert(bv->vmesh, 0, 1, 0)->v;
6193 }
6194 }
6195
6196/* Build the profile cutoff faces. */
6197/* Extra one or two for corner vertices and one for last point along profile, or the size of the
6198 * center face array if it's bigger. */
6199#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6200 printf("Building profile cutoff faces.\n");
6201#endif
6202 BMVert **face_bmverts = static_cast<BMVert **>(BLI_memarena_alloc(
6203 bp->mem_arena, sizeof(BMVert *) * max_ii(bp->seg + 2 + build_center_face, n_bndv)));
6204 bndv = bv->vmesh->boundstart;
6205 do {
6206 int i = bndv->index;
6207
6208 /* Add the first corner vertex under this boundvert. */
6209 face_bmverts[0] = mesh_vert(bv->vmesh, i, 1, 0)->v;
6210
6211#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6212 printf("Profile Number %d:\n", i);
6213 if (bndv->is_patch_start || bndv->is_arc_start) {
6214 printf(" Miter profile\n");
6215 }
6216 printf(" Corner 1: (%0.3f, %0.3f, %0.3f)\n", F3(mesh_vert(bv->vmesh, i, 1, 0)->co));
6217#endif
6218
6219 /* Add profile point vertices to the face, including the last one. */
6220 for (int k = 0; k < bp->seg + 1; k++) {
6221 face_bmverts[k + 1] = mesh_vert(bv->vmesh, i, 0, k)->v; /* Leave room for first vert. */
6222#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6223 printf(" Profile %d: (%0.3f, %0.3f, %0.3f)\n", k, F3(mesh_vert(bv->vmesh, i, 0, k)->co));
6224#endif
6225 }
6226
6227 /* Add the second corner vert to complete the bottom of the face. */
6228 if (build_center_face) {
6229 face_bmverts[bp->seg + 2] = mesh_vert(bv->vmesh, i, 1, 1)->v;
6230#ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
6231 printf(" Corner 2: (%0.3f, %0.3f, %0.3f)\n", F3(mesh_vert(bv->vmesh, i, 1, 1)->co));
6232#endif
6233 }
6234
6235 /* Create the profile cutoff face for this boundvert. */
6236 // repface = boundvert_rep_face(bndv, nullptr);
6237 bev_create_ngon(bp,
6238 bm,
6239 face_bmverts,
6240 bp->seg + 2 + build_center_face,
6241 nullptr,
6242 nullptr,
6243 nullptr,
6244 bv->v,
6245 nullptr,
6246 bp->mat_nr,
6247 true);
6248 } while ((bndv = bndv->next) != bv->vmesh->boundstart);
6249
6250 /* Create the bottom face if it should be built, reusing previous face_bmverts allocation. */
6251 if (build_center_face) {
6252 /* Add all of the corner vertices to this face. */
6253 for (int i = 0; i < n_bndv; i++) {
6254 /* Add verts from each cutoff face. */
6255 face_bmverts[i] = mesh_vert(bv->vmesh, i, 1, 0)->v;
6256 }
6257
6259 bp, bm, face_bmverts, n_bndv, nullptr, nullptr, nullptr, bv->v, nullptr, bp->mat_nr, true);
6260 }
6261}
6262
6264{
6265 VMesh *vm = bv->vmesh;
6269
6270 BMFace *repface;
6271 BMEdge *repface_e1, *repface_e2;
6272 BoundVert *unsnapped[3];
6273 if (bv->any_seam) {
6274 repface = frep_for_center_poly(bp, bv);
6275 get_incident_edges(repface, bv->v, &repface_e1, &repface_e2);
6276 find_face_internal_boundverts(bv, repface, unsnapped);
6277 }
6278 else {
6279 repface = nullptr;
6280 repface_e1 = repface_e2 = nullptr;
6281 }
6282 BoundVert *bndv = vm->boundstart;
6283 int n = 0;
6284 do {
6285 /* Accumulate vertices for vertex ngon. */
6286 /* Also accumulate faces in which uv interpolation is to happen for each. */
6287 bmverts.append(bndv->nv.v);
6288 if (repface) {
6289 bmfaces.append(repface);
6290 if (ELEM(bndv, unsnapped[0], unsnapped[1], unsnapped[2])) {
6291 bmedges.append(nullptr);
6292 }
6293 else {
6294 BMEdge *frep_e = find_closer_edge(bndv->nv.v->co, repface_e1, repface_e2);
6295 bmedges.append(frep_e);
6296 }
6297 }
6298 else {
6299 bmfaces.append(boundvert_rep_face(bndv, nullptr));
6300 bmedges.append(nullptr);
6301 }
6302 n++;
6303 if (bndv->ebev && bndv->ebev->seg > 1) {
6304 for (int k = 1; k < bndv->ebev->seg; k++) {
6305 bmverts.append(mesh_vert(vm, bndv->index, 0, k)->v);
6306 if (repface) {
6307 bmfaces.append(repface);
6308 BMEdge *frep_e = find_closer_edge(
6309 mesh_vert(vm, bndv->index, 0, k)->v->co, repface_e1, repface_e2);
6310 bmedges.append(k < bndv->ebev->seg / 2 ? nullptr : frep_e);
6311 }
6312 else {
6313 bmfaces.append(boundvert_rep_face(bndv, nullptr));
6314 bmedges.append(nullptr);
6315 }
6316 n++;
6317 }
6318 }
6319 } while ((bndv = bndv->next) != vm->boundstart);
6320
6321 BMFace *f;
6322 if (n > 2) {
6323 f = bev_create_ngon(bp,
6324 bm,
6325 bmverts.data(),
6326 n,
6327 bmfaces.data(),
6328 repface,
6329 bmedges.data(),
6330 bv->v,
6331 nullptr,
6332 bp->mat_nr,
6333 true);
6334 record_face_kind(bp, f, F_VERT);
6335 }
6336 else {
6337 f = nullptr;
6338 }
6339 return f;
6340}
6341
6343{
6344 BLI_assert(next_bev(bv, nullptr)->seg == 1 || bv->selcount == 1);
6345
6346 BMFace *f = bevel_build_poly(bp, bm, bv);
6347
6348 if (f == nullptr) {
6349 return;
6350 }
6351
6352 /* We have a polygon which we know starts at the previous vertex, make it into a fan. */
6353 BMLoop *l_fan = BM_FACE_FIRST_LOOP(f)->prev;
6354 BMVert *v_fan = l_fan->v;
6355
6356 while (f->len > 3) {
6357 BMLoop *l_new;
6358 BMFace *f_new;
6359 BLI_assert(v_fan == l_fan->v);
6360 f_new = BM_face_split(bm, f, l_fan, l_fan->next->next, &l_new, nullptr, false);
6361 flag_out_edge(bm, l_new->e);
6362
6363 if (f_new->len > f->len) {
6364 f = f_new;
6365 if (l_new->v == v_fan) {
6366 l_fan = l_new;
6367 }
6368 else if (l_new->next->v == v_fan) {
6369 l_fan = l_new->next;
6370 }
6371 else if (l_new->prev->v == v_fan) {
6372 l_fan = l_new->prev;
6373 }
6374 else {
6375 BLI_assert(0);
6376 }
6377 }
6378 else {
6379 if (l_fan->v == v_fan) { /* l_fan = l_fan. */
6380 }
6381 else if (l_fan->next->v == v_fan) {
6382 l_fan = l_fan->next;
6383 }
6384 else if (l_fan->prev->v == v_fan) {
6385 l_fan = l_fan->prev;
6386 }
6387 else {
6388 BLI_assert(0);
6389 }
6390 }
6391 record_face_kind(bp, f_new, F_VERT);
6392 }
6393}
6394
6395/* Special case: vertex bevel with only two boundary verts.
6396 * Want to make a curved edge if seg > 0.
6397 * If there are no faces in the original mesh at the original vertex,
6398 * there will be no rebuilt face to make the edge between the boundary verts,
6399 * we have to make it here. */
6401{
6402 VMesh *vm = bv->vmesh;
6403
6405
6406 BMVert *v1 = mesh_vert(vm, 0, 0, 0)->v;
6407 BMVert *v2 = mesh_vert(vm, 1, 0, 0)->v;
6408
6409 int ns = vm->seg;
6410 if (ns > 1) {
6411 /* Set up profile parameters. */
6412 BoundVert *bndv = vm->boundstart;
6413 Profile *pro = &bndv->profile;
6414 pro->super_r = bp->pro_super_r;
6415 copy_v3_v3(pro->start, v1->co);
6416 copy_v3_v3(pro->end, v2->co);
6417 copy_v3_v3(pro->middle, bv->v->co);
6418 /* Don't use projection. */
6419 zero_v3(pro->plane_co);
6420 zero_v3(pro->plane_no);
6421 zero_v3(pro->proj_dir);
6422
6423 for (int k = 1; k < ns; k++) {
6424 float co[3];
6425 get_profile_point(bp, pro, k, ns, co);
6426 copy_v3_v3(mesh_vert(vm, 0, 0, k)->co, co);
6427 create_mesh_bmvert(bm, vm, 0, 0, k, bv->v);
6428 }
6429 copy_v3_v3(mesh_vert(vm, 0, 0, ns)->co, v2->co);
6430 for (int k = 1; k < ns; k++) {
6431 copy_mesh_vert(vm, 1, 0, ns - k, 0, 0, k);
6432 }
6433 }
6434
6435 if (BM_vert_face_check(bv->v) == false) {
6436 BMEdge *e_eg = bv->edges[0].e;
6437 BLI_assert(e_eg != nullptr);
6438 for (int k = 0; k < ns; k++) {
6439 v1 = mesh_vert(vm, 0, 0, k)->v;
6440 v2 = mesh_vert(vm, 0, 0, k + 1)->v;
6441 BLI_assert(v1 != nullptr && v2 != nullptr);
6442 BMEdge *bme = BM_edge_create(bm, v1, v2, e_eg, BM_CREATE_NO_DOUBLE);
6443 if (bme) {
6444 flag_out_edge(bm, bme);
6445 }
6446 }
6447 }
6448}
6449
6450/* Given that the boundary is built, now make the actual BMVerts
6451 * for the boundary and the interior of the vertex mesh. */
6452static void build_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv)
6453{
6454 VMesh *vm = bv->vmesh;
6455 float co[3];
6456
6457 int n = vm->count;
6458 int ns = vm->seg;
6459 int ns2 = ns / 2;
6460
6461 vm->mesh = static_cast<NewVert *>(
6462 BLI_memarena_alloc(bp->mem_arena, sizeof(NewVert) * n * (ns2 + 1) * (ns + 1)));
6463
6464 /* Special case: just two beveled edges welded together. */
6465 const bool weld = (bv->selcount == 2) && (vm->count == 2);
6466 BoundVert *weld1 = nullptr; /* Will hold two BoundVerts involved in weld. */
6467 BoundVert *weld2 = nullptr;
6468
6469 /* Make (i, 0, 0) mesh verts for all i boundverts. */
6470 BoundVert *bndv = vm->boundstart;
6471 do {
6472 int i = bndv->index;
6473 copy_v3_v3(mesh_vert(vm, i, 0, 0)->co, bndv->nv.co); /* Mesh NewVert to boundary NewVert. */
6474 create_mesh_bmvert(bm, vm, i, 0, 0, bv->v); /* Create BMVert for that NewVert. */
6475 bndv->nv.v = mesh_vert(vm, i, 0, 0)->v; /* Use the BMVert for the BoundVert's NewVert. */
6476
6477 /* Find boundverts and move profile planes if this is a weld case. */
6478 if (weld && bndv->ebev) {
6479 if (!weld1) {
6480 weld1 = bndv;
6481 }
6482 else { /* Get the last of the two BoundVerts. */
6483 weld2 = bndv;
6484 set_profile_params(bp, bv, weld1);
6485 set_profile_params(bp, bv, weld2);
6486 move_weld_profile_planes(bv, weld1, weld2);
6487 }
6488 }
6489 } while ((bndv = bndv->next) != vm->boundstart);
6490
6491 /* It's simpler to calculate all profiles only once at a single moment, so keep just a single
6492 * profile calculation here, the last point before actual mesh verts are created. */
6493 calculate_vm_profiles(bp, bv, vm);
6494
6495 /* Create new vertices and place them based on the profiles. */
6496 /* Copy other ends to (i, 0, ns) for all i, and fill in profiles for edges. */
6497 bndv = vm->boundstart;
6498 do {
6499 int i = bndv->index;
6500 /* bndv's last vert along the boundary arc is the first of the next BoundVert's arc. */
6501 copy_mesh_vert(vm, i, 0, ns, bndv->next->index, 0, 0);
6502
6503 if (vm->mesh_kind != M_ADJ) {
6504 for (int k = 1; k < ns; k++) {
6505 if (bndv->ebev) {
6506 get_profile_point(bp, &bndv->profile, k, ns, co);
6507 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
6508 if (!weld) {
6509 /* This is done later with (possibly) better positions for the weld case. */
6510 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
6511 }
6512 }
6513 else if (n == 2 && !bndv->ebev) {
6514 /* case of one edge beveled and this is the v without ebev */
6515 /* want to copy the verts from other v, in reverse order */
6516 copy_mesh_vert(bv->vmesh, i, 0, k, 1 - i, 0, ns - k);
6517 }
6518 }
6519 }
6520 } while ((bndv = bndv->next) != vm->boundstart);
6521
6522 /* Build the profile for the weld case (just a connection between the two boundverts). */
6523 if (weld) {
6524 bv->vmesh->mesh_kind = M_NONE;
6525 for (int k = 1; k < ns; k++) {
6526 float *v_weld1 = mesh_vert(bv->vmesh, weld1->index, 0, k)->co;
6527 float *v_weld2 = mesh_vert(bv->vmesh, weld2->index, 0, ns - k)->co;
6529 /* Don't bother with special case profile check from below. */
6530 mid_v3_v3v3(co, v_weld1, v_weld2);
6531 }
6532 else {
6533 /* Use the point from the other profile if one is in a special case. */
6534 if (weld1->profile.super_r == PRO_LINE_R && weld2->profile.super_r != PRO_LINE_R) {
6535 copy_v3_v3(co, v_weld2);
6536 }
6537 else if (weld2->profile.super_r == PRO_LINE_R && weld1->profile.super_r != PRO_LINE_R) {
6538 copy_v3_v3(co, v_weld1);
6539 }
6540 else {
6541 /* In case the profiles aren't snapped to the same plane, use their midpoint. */
6542 mid_v3_v3v3(co, v_weld1, v_weld2);
6543 }
6544 }
6545 copy_v3_v3(mesh_vert(bv->vmesh, weld1->index, 0, k)->co, co);
6546 create_mesh_bmvert(bm, bv->vmesh, weld1->index, 0, k, bv->v);
6547 }
6548 for (int k = 1; k < ns; k++) {
6549 copy_mesh_vert(bv->vmesh, weld2->index, 0, ns - k, weld1->index, 0, k);
6550 }
6551 }
6552
6553 /* Make sure the pipe case ADJ mesh is used for both the "Grid Fill" (ADJ) and cutoff options. */
6554 BoundVert *vpipe = nullptr;
6555 if (ELEM(vm->count, 3, 4) && bp->seg > 1) {
6556 /* Result is passed to bevel_build_rings to avoid overhead. */
6557 vpipe = pipe_test(bv);
6558 if (vpipe) {
6559 vm->mesh_kind = M_ADJ;
6560 }
6561 }
6562
6563 switch (vm->mesh_kind) {
6564 case M_NONE:
6565 if (n == 2 && bp->affect_type == BEVEL_AFFECT_VERTICES) {
6566 bevel_vert_two_edges(bp, bm, bv);
6567 }
6568 break;
6569 case M_POLY:
6570 bevel_build_poly(bp, bm, bv);
6571 break;
6572 case M_ADJ:
6573 bevel_build_rings(bp, bm, bv, vpipe);
6574 break;
6575 case M_TRI_FAN:
6576 bevel_build_trifan(bp, bm, bv);
6577 break;
6578 case M_CUTOFF:
6579 bevel_build_cutoff(bp, bm, bv);
6580 }
6581}
6582
6583/* Return the angle between the two faces adjacent to e.
6584 * If there are not two, return 0. */
6586{
6587 if (e->fprev && e->fnext) {
6588 /* Angle between faces is supplement of angle between face normals. */
6589 return float(M_PI) - angle_normalized_v3v3(e->fprev->no, e->fnext->no);
6590 }
6591 return 0.0f;
6592}
6593
6594/* Take care, this flag isn't cleared before use, it just so happens that its not set. */
6595#define BM_BEVEL_EDGE_TAG_ENABLE(bme) BM_ELEM_API_FLAG_ENABLE((bme), _FLAG_OVERLAP)
6596#define BM_BEVEL_EDGE_TAG_DISABLE(bme) BM_ELEM_API_FLAG_DISABLE((bme), _FLAG_OVERLAP)
6597#define BM_BEVEL_EDGE_TAG_TEST(bme) BM_ELEM_API_FLAG_TEST((bme), _FLAG_OVERLAP)
6598
6608static int bevel_edge_order_extend(BMesh *bm, BevVert *bv, int i)
6609{
6610 Vector<BMEdge *, 4> sucs; /* Likely very few faces attached to same edge. */
6612
6613 /* Fill sucs with all unmarked edges of bmesh. */
6614 BMEdge *bme = bv->edges[i].e;
6615 BMIter iter;
6616 BMLoop *l;
6617 BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
6618 BMEdge *bme2 = (l->v == bv->v) ? l->prev->e : l->next->e;
6619 if (!BM_BEVEL_EDGE_TAG_TEST(bme2)) {
6620 sucs.append(bme2);
6621 }
6622 }
6623 const int64_t nsucs = sucs.size();
6624
6625 int bestj = i;
6626 int j = i;
6627 for (int sucindex = 0; sucindex < nsucs; sucindex++) {
6628 BMEdge *nextbme = sucs[sucindex];
6629 BLI_assert(nextbme != nullptr);
6631 BLI_assert(j + 1 < bv->edgecount);
6632 bv->edges[j + 1].e = nextbme;
6633 BM_BEVEL_EDGE_TAG_ENABLE(nextbme);
6634 int tryj = bevel_edge_order_extend(bm, bv, j + 1);
6635 if (tryj > bestj ||
6636 (tryj == bestj && edges_face_connected_at_vert(bv->edges[tryj].e, bv->edges[0].e)))
6637 {
6638 bestj = tryj;
6639 save_path.clear();
6640 for (int k = j + 1; k <= bestj; k++) {
6641 save_path.append(bv->edges[k].e);
6642 }
6643 }
6644 /* Now reset to path only-going-to-j state. */
6645 for (int k = j + 1; k <= tryj; k++) {
6647 bv->edges[k].e = nullptr;
6648 }
6649 }
6650 /* At this point we should be back at invariant on entrance: path up to j. */
6651 if (bestj > j) {
6652 /* Save_path should have from j + 1 to bestj inclusive.
6653 * Edges to add to edges[] before returning. */
6654 for (int k = j + 1; k <= bestj; k++) {
6655 BLI_assert(save_path[k - (j + 1)] != nullptr);
6656 bv->edges[k].e = save_path[k - (j + 1)];
6658 }
6659 }
6660 return bestj;
6661}
6662
6663/* See if we have usual case for bevel edge order:
6664 * there is an ordering such that all the faces are between
6665 * successive edges and form a manifold "cap" at bv.
6666 * If this is the case, set bv->edges to such an order
6667 * and return true; else return unmark any partial path and return false.
6668 * Assume the first edge is already in bv->edges[0].e and it is tagged. */
6669#ifdef FASTER_FASTORDER
6670/* The alternative older code is O(n^2) where n = # of edges incident to bv->v.
6671 * This implementation is O(n * m) where m = average number of faces attached to an edge incident
6672 * to bv->v, which is almost certainly a small constant except in very strange cases.
6673 * But this code produces different choices of ordering than the legacy system,
6674 * leading to differences in vertex orders etc. in user models,
6675 * so for now will continue to use the legacy code. */
6676static bool fast_bevel_edge_order(BevVert *bv)
6677{
6678 for (int j = 1; j < bv->edgecount; j++) {
6679 BMEdge *bme = bv->edges[j - 1].e;
6680 BMEdge *bmenext = nullptr;
6681 int nsucs = 0;
6682 BMIter iter;
6683 BMLoop *l;
6684 BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
6685 BMEdge *bme2 = (l->v == bv->v) ? l->prev->e : l->next->e;
6686 if (!BM_BEVEL_EDGE_TAG_TEST(bme2)) {
6687 nsucs++;
6688 if (bmenext == nullptr) {
6689 bmenext = bme2;
6690 }
6691 }
6692 }
6693 if (nsucs == 0 || (nsucs == 2 && j != 1) || nsucs > 2 ||
6694 (j == bv->edgecount - 1 && !edges_face_connected_at_vert(bmenext, bv->edges[0].e)))
6695 {
6696 for (int k = 1; k < j; k++) {
6697 BM_BEVEL_EDGE_TAG_DISABLE(bv->edges[k].e);
6698 bv->edges[k].e = nullptr;
6699 }
6700 return false;
6701 }
6702 bv->edges[j].e = bmenext;
6703 BM_BEVEL_EDGE_TAG_ENABLE(bmenext);
6704 }
6705 return true;
6706}
6707#else
6709{
6710 int ntot = bv->edgecount;
6711
6712 /* Add edges to bv->edges in order that keeps adjacent edges sharing
6713 * a unique face, if possible. */
6714 EdgeHalf *e = &bv->edges[0];
6715 BMEdge *bme = e->e;
6716 if (!bme->l) {
6717 return false;
6718 }
6719
6720 for (int i = 1; i < ntot; i++) {
6721 /* Find an unflagged edge bme2 that shares a face f with previous bme. */
6722 int num_shared_face = 0;
6723 BMEdge *first_suc = nullptr; /* Keep track of first successor to match legacy behavior. */
6724 BMIter iter;
6725 BMEdge *bme2;
6726 BM_ITER_ELEM (bme2, &iter, bv->v, BM_EDGES_OF_VERT) {
6727 if (BM_BEVEL_EDGE_TAG_TEST(bme2)) {
6728 continue;
6729 }
6730
6731 BMIter iter2;
6732 BMFace *f;
6733 BM_ITER_ELEM (f, &iter2, bme2, BM_FACES_OF_EDGE) {
6734 if (BM_face_edge_share_loop(f, bme)) {
6735 num_shared_face++;
6736 if (first_suc == nullptr) {
6737 first_suc = bme2;
6738 }
6739 }
6740 }
6741 if (num_shared_face >= 3) {
6742 break;
6743 }
6744 }
6745 if (num_shared_face == 1 || (i == 1 && num_shared_face == 2)) {
6746 e = &bv->edges[i];
6747 e->e = bme = first_suc;
6749 }
6750 else {
6751 for (int k = 1; k < i; k++) {
6753 bv->edges[k].e = nullptr;
6754 }
6755 return false;
6756 }
6757 }
6758 return true;
6759}
6760#endif
6761
6762/* Fill in bv->edges with a good ordering of non-wire edges around bv->v.
6763 * Use only edges where BM_BEVEL_EDGE_TAG is disabled so far (if edge beveling, others are wire).
6764 * first_bme is a good edge to start with. */
6765static void find_bevel_edge_order(BMesh *bm, BevVert *bv, BMEdge *first_bme)
6766{
6767 int ntot = bv->edgecount;
6768 for (int i = 0;;) {
6769 BLI_assert(first_bme != nullptr);
6770 bv->edges[i].e = first_bme;
6771 BM_BEVEL_EDGE_TAG_ENABLE(first_bme);
6772 if (i == 0 && fast_bevel_edge_order(bv)) {
6773 break;
6774 }
6775 i = bevel_edge_order_extend(bm, bv, i);
6776 i++;
6777 if (i >= bv->edgecount) {
6778 break;
6779 }
6780 /* Not done yet: find a new first_bme. */
6781 first_bme = nullptr;
6782 BMIter iter;
6783 BMEdge *bme;
6784 BM_ITER_ELEM (bme, &iter, bv->v, BM_EDGES_OF_VERT) {
6785 if (BM_BEVEL_EDGE_TAG_TEST(bme)) {
6786 continue;
6787 }
6788 if (!first_bme) {
6789 first_bme = bme;
6790 }
6791 if (BM_edge_face_count(bme) == 1) {
6792 first_bme = bme;
6793 break;
6794 }
6795 }
6796 }
6797 /* Now fill in the faces. */
6798 for (int i = 0; i < ntot; i++) {
6799 EdgeHalf *e = &bv->edges[i];
6800 EdgeHalf *e2 = (i == bv->edgecount - 1) ? &bv->edges[0] : &bv->edges[i + 1];
6801 BMEdge *bme = e->e;
6802 BMEdge *bme2 = e2->e;
6803 BLI_assert(bme != nullptr);
6804 if (e->fnext != nullptr || e2->fprev != nullptr) {
6805 continue;
6806 }
6807 /* Which faces have successive loops that are for bme and bme2?
6808 * There could be more than one. E.g., in manifold ntot==2 case.
6809 * Prefer one that has loop in same direction as e. */
6810 BMFace *bestf = nullptr;
6811 BMIter iter;
6812 BMLoop *l;
6813 BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
6814 BMFace *f = l->f;
6815 if (l->prev->e == bme2 || l->next->e == bme2) {
6816 if (!bestf || l->v == bv->v) {
6817 bestf = f;
6818 }
6819 }
6820 if (bestf) {
6821 e->fnext = e2->fprev = bestf;
6822 }
6823 }
6824 }
6825}
6826
6827/* Construction around the vertex. */
6829{
6830 /* Gather input selected edges.
6831 * Only bevel selected edges that have exactly two incident faces.
6832 * Want edges to be ordered so that they share faces.
6833 * There may be one or more chains of shared faces broken by
6834 * gaps where there are no faces.
6835 * Want to ignore wire edges completely for edge beveling.
6836 * TODO: make following work when more than one gap. */
6837
6838 int nsel = 0;
6839 int tot_edges = 0;
6840 int tot_wire = 0;
6841 BMEdge *first_bme = nullptr;
6842 BMIter iter;
6843 BMEdge *bme;
6844 BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
6845 int face_count = BM_edge_face_count(bme);
6848 BLI_assert(face_count == 2);
6849 nsel++;
6850 if (!first_bme) {
6851 first_bme = bme;
6852 }
6853 }
6854 if (face_count == 1) {
6855 /* Good to start face chain from this edge. */
6856 first_bme = bme;
6857 }
6858 if (face_count > 0 || bp->affect_type == BEVEL_AFFECT_VERTICES) {
6859 tot_edges++;
6860 }
6861 if (BM_edge_is_wire(bme)) {
6862 tot_wire++;
6863 /* If edge beveling, exclude wire edges from edges array.
6864 * Mark this edge as "chosen" so loop below won't choose it. */
6867 }
6868 }
6869 }
6870 if (!first_bme) {
6871 first_bme = v->e;
6872 }
6873
6874 if ((nsel == 0 && bp->affect_type != BEVEL_AFFECT_VERTICES) ||
6875 (tot_edges < 2 && bp->affect_type == BEVEL_AFFECT_VERTICES))
6876 {
6877 /* Signal this vert isn't being beveled. */
6879 return nullptr;
6880 }
6881
6882 BevVert *bv = static_cast<BevVert *>(BLI_memarena_alloc(bp->mem_arena, sizeof(BevVert)));
6883 bv->v = v;
6884 bv->edgecount = tot_edges;
6885 bv->selcount = nsel;
6886 bv->wirecount = tot_wire;
6887 bv->offset = bp->offset;
6888 bv->edges = static_cast<EdgeHalf *>(
6889 BLI_memarena_alloc(bp->mem_arena, sizeof(EdgeHalf) * tot_edges));
6890 if (tot_wire) {
6891 bv->wire_edges = static_cast<BMEdge **>(
6892 BLI_memarena_alloc(bp->mem_arena, sizeof(BMEdge *) * tot_wire));
6893 }
6894 else {
6895 bv->wire_edges = nullptr;
6896 }
6897 bv->vmesh = static_cast<VMesh *>(BLI_memarena_alloc(bp->mem_arena, sizeof(VMesh)));
6898 bv->vmesh->seg = bp->seg;
6899
6900 bp->vert_hash.add(v, bv);
6901
6902 find_bevel_edge_order(bm, bv, first_bme);
6903
6904 /* Fill in other attributes of EdgeHalfs. */
6905 for (int i = 0; i < tot_edges; i++) {
6906 EdgeHalf *e = &bv->edges[i];
6907 bme = e->e;
6909 e->is_bev = true;
6910 e->seg = bp->seg;
6911 }
6912 else {
6913 e->is_bev = false;
6914 e->seg = 0;
6915 }
6916 e->is_rev = (bme->v2 == v);
6917 e->leftv = e->rightv = nullptr;
6918 e->profile_index = 0;
6919 }
6920
6921 /* Now done with tag flag. */
6922 BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
6924 }
6925
6926 /* If edge array doesn't go CCW around vertex from average normal side,
6927 * reverse the array, being careful to reverse face pointers too. */
6928 if (tot_edges > 1) {
6929 int ccw_test_sum = 0;
6930 for (int i = 0; i < tot_edges; i++) {
6931 ccw_test_sum += bev_ccw_test(
6932 bv->edges[i].e, bv->edges[(i + 1) % tot_edges].e, bv->edges[i].fnext);
6933 }
6934 if (ccw_test_sum < 0) {
6935 for (int i = 0; i <= (tot_edges / 2) - 1; i++) {
6936 std::swap(bv->edges[i], bv->edges[tot_edges - i - 1]);
6937 std::swap(bv->edges[i].fprev, bv->edges[i].fnext);
6938 std::swap(bv->edges[tot_edges - i - 1].fprev, bv->edges[tot_edges - i - 1].fnext);
6939 }
6940 if (tot_edges % 2 == 1) {
6941 int i = tot_edges / 2;
6942 std::swap(bv->edges[i].fprev, bv->edges[i].fnext);
6943 }
6944 }
6945 }
6946
6947 float weight;
6948 float vert_axis[3] = {0, 0, 0};
6950 /* Modify the offset by the vertex group or bevel weight if they are specified. */
6951 if (bp->dvert != nullptr && bp->vertex_group != -1) {
6953 bv->offset *= weight;
6954 }
6955 else if (bp->use_weights) {
6956 weight = bp->bweight_offset_vert == -1 ? 0.0f :
6958 bv->offset *= weight;
6959 }
6960 /* Find center axis. NOTE: Don't use vert normal, can give unwanted results. */
6962 float edge_dir[3];
6963 EdgeHalf *e = bv->edges;
6964 for (int i = 0; i < tot_edges; i++, e++) {
6965 BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
6966 sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
6967 normalize_v3(edge_dir);
6968 add_v3_v3v3(vert_axis, vert_axis, edge_dir);
6969 }
6970 }
6971 }
6972
6973 /* Set offsets for each beveled edge. */
6974 EdgeHalf *e = bv->edges;
6975 for (int i = 0; i < tot_edges; i++, e++) {
6976 e->next = &bv->edges[(i + 1) % tot_edges];
6977 e->prev = &bv->edges[(i + tot_edges - 1) % tot_edges];
6978
6979 if (e->is_bev) {
6980 /* Convert distance as specified by user into offsets along
6981 * faces on the left side and right sides of this edgehalf.
6982 * Except for percent method, offset will be same on each side. */
6983
6984 switch (bp->offset_type) {
6985 case BEVEL_AMT_OFFSET: {
6986 e->offset_l_spec = bp->offset;
6987 break;
6988 }
6989 case BEVEL_AMT_WIDTH: {
6990 float z = fabsf(2.0f * sinf(edge_face_angle(e) / 2.0f));
6991 if (z < BEVEL_EPSILON) {
6992 e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
6993 }
6994 else {
6995 e->offset_l_spec = bp->offset / z;
6996 }
6997 break;
6998 }
6999 case BEVEL_AMT_DEPTH: {
7000 float z = fabsf(cosf(edge_face_angle(e) / 2.0f));
7001 if (z < BEVEL_EPSILON) {
7002 e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
7003 }
7004 else {
7005 e->offset_l_spec = bp->offset / z;
7006 }
7007 break;
7008 }
7009 case BEVEL_AMT_PERCENT: {
7010 /* Offset needs to meet adjacent edges at percentage of their lengths.
7011 * Since the width isn't constant, we don't store a width at all, but
7012 * rather the distance along the adjacent edge that we need to go
7013 * at this end of the edge.
7014 */
7015
7016 e->offset_l_spec = BM_edge_calc_length(e->prev->e) * bp->offset / 100.0f;
7017 e->offset_r_spec = BM_edge_calc_length(e->next->e) * bp->offset / 100.0f;
7018
7019 break;
7020 }
7021 case BEVEL_AMT_ABSOLUTE: {
7022 /* Like Percent, but the amount gives the absolute distance along adjacent edges. */
7023 e->offset_l_spec = bp->offset;
7024 e->offset_r_spec = bp->offset;
7025 break;
7026 }
7027 default: {
7028 BLI_assert_msg(0, "bad bevel offset kind");
7029 e->offset_l_spec = bp->offset;
7030 break;
7031 }
7032 }
7034 e->offset_r_spec = e->offset_l_spec;
7035 }
7036 if (bp->use_weights) {
7037 weight = bp->bweight_offset_edge == -1 ?
7038 0.0f :
7040 e->offset_l_spec *= weight;
7041 e->offset_r_spec *= weight;
7042 }
7043 }
7044 else if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
7045 /* Weight has already been applied to bv->offset, if present.
7046 * Transfer to e->offset_[lr]_spec according to offset_type. */
7047 float edge_dir[3];
7048 switch (bp->offset_type) {
7049 case BEVEL_AMT_OFFSET: {
7050 e->offset_l_spec = bv->offset;
7051 break;
7052 }
7053 case BEVEL_AMT_WIDTH: {
7054 BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
7055 sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
7056 float z = fabsf(2.0f * sinf(angle_v3v3(vert_axis, edge_dir)));
7057 if (z < BEVEL_EPSILON) {
7058 e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
7059 }
7060 else {
7061 e->offset_l_spec = bp->offset / z;
7062 }
7063 break;
7064 }
7065 case BEVEL_AMT_DEPTH: {
7066 BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
7067 sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
7068 float z = fabsf(cosf(angle_v3v3(vert_axis, edge_dir)));
7069 if (z < BEVEL_EPSILON) {
7070 e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
7071 }
7072 else {
7073 e->offset_l_spec = bp->offset / z;
7074 }
7075 break;
7076 }
7077 case BEVEL_AMT_PERCENT: {
7078 e->offset_l_spec = BM_edge_calc_length(e->e) * bv->offset / 100.0f;
7079 break;
7080 }
7081 case BEVEL_AMT_ABSOLUTE: {
7082 e->offset_l_spec = bv->offset;
7083 break;
7084 }
7085 }
7086 e->offset_r_spec = e->offset_l_spec;
7087 }
7088 else {
7089 e->offset_l_spec = e->offset_r_spec = 0.0f;
7090 }
7091 e->offset_l = e->offset_l_spec;
7092 e->offset_r = e->offset_r_spec;
7093
7094 if (e->fprev && e->fnext) {
7095 e->is_seam = !contig_ldata_across_edge(bm, e->e, e->fprev, e->fnext);
7096 }
7097 else {
7098 e->is_seam = true;
7099 }
7100 }
7101
7102 /* Collect wire edges if we found any earlier. */
7103 if (tot_wire != 0) {
7104 int i = 0;
7105 BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
7106 if (BM_edge_is_wire(bme)) {
7107 BLI_assert(i < bv->wirecount);
7108 bv->wire_edges[i++] = bme;
7109 }
7110 }
7111 BLI_assert(i == bv->wirecount);
7112 }
7113
7114 return bv;
7115}
7116
7117/* Face f has at least one beveled vertex. Rebuild f. */
7119 BevelParams *bp,
7120 BMFace *f,
7121 BMFace **r_new_face = nullptr)
7122{
7123 bool do_rebuild = false;
7126 Map<BMVert *, BMVert *> nv_bv_map; /* New vertex to the (original) bevel vertex mapping. */
7127
7128 BMIter liter;
7129 BMLoop *l;
7130 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
7132 BMLoop *lprev = l->prev;
7133 BevVert *bv = find_bevvert(bp, l->v);
7134 VMesh *vm = bv->vmesh;
7135 EdgeHalf *e = find_edge_half(bv, l->e);
7136 BLI_assert(e != nullptr);
7137 BMEdge *bme = e->e;
7138 EdgeHalf *eprev = find_edge_half(bv, lprev->e);
7139 BLI_assert(eprev != nullptr);
7140
7141 /* Which direction around our vertex do we travel to match orientation of f? */
7142 bool go_ccw;
7143 if (e->prev == eprev) {
7144 if (eprev->prev == e) {
7145 /* Valence 2 vertex: use f is one of e->fnext or e->fprev to break tie. */
7146 go_ccw = (e->fnext != f);
7147 }
7148 else {
7149 go_ccw = true; /* Going CCW around bv to trace this corner. */
7150 }
7151 }
7152 else if (eprev->prev == e) {
7153 go_ccw = false; /* Going cw around bv to trace this corner. */
7154 }
7155 else {
7156 /* Edges in face are non-contiguous in our ordering around bv.
7157 * Which way should we go when going from eprev to e? */
7158 if (count_ccw_edges_between(eprev, e) < count_ccw_edges_between(e, eprev)) {
7159 /* Go counter-clockwise from eprev to e. */
7160 go_ccw = true;
7161 }
7162 else {
7163 /* Go clockwise from eprev to e. */
7164 go_ccw = false;
7165 }
7166 }
7167 bool on_profile_start = false;
7168 BoundVert *vstart;
7169 BoundVert *vend;
7170 if (go_ccw) {
7171 vstart = eprev->rightv;
7172 vend = e->leftv;
7173 if (e->profile_index > 0) {
7174 vstart = vstart->prev;
7175 on_profile_start = true;
7176 }
7177 }
7178 else {
7179 vstart = eprev->leftv;
7180 vend = e->rightv;
7181 if (eprev->profile_index > 0) {
7182 vstart = vstart->next;
7183 on_profile_start = true;
7184 }
7185 }
7186 BLI_assert(vstart != nullptr && vend != nullptr);
7187 BoundVert *v = vstart;
7188 if (!on_profile_start) {
7189 vv.append(v->nv.v);
7190 ee.append(bme);
7191 nv_bv_map.add(v->nv.v, l->v);
7192 }
7193 while (v != vend) {
7194 if (go_ccw) {
7195 int i = v->index;
7196 int kstart, kend;
7197 if (on_profile_start) {
7198 kstart = e->profile_index;
7199 on_profile_start = false;
7200 }
7201 else {
7202 kstart = 1;
7203 }
7204 if (eprev->rightv == v && eprev->profile_index > 0) {
7205 kend = eprev->profile_index;
7206 }
7207 else {
7208 kend = vm->seg;
7209 }
7210 for (int k = kstart; k <= kend; k++) {
7211 BMVert *bmv = mesh_vert(vm, i, 0, k)->v;
7212 if (bmv) {
7213 vv.append(bmv);
7214 ee.append(bme); /* TODO: Maybe better edge here. */
7215 nv_bv_map.add(bmv, l->v);
7216 }
7217 }
7218 v = v->next;
7219 }
7220 else {
7221 /* Going cw. */
7222 int i = v->prev->index;
7223 int kstart, kend;
7224 if (on_profile_start) {
7225 kstart = eprev->profile_index;
7226 on_profile_start = false;
7227 }
7228 else {
7229 kstart = vm->seg - 1;
7230 }
7231 if (e->rightv == v->prev && e->profile_index > 0) {
7232 kend = e->profile_index;
7233 }
7234 else {
7235 kend = 0;
7236 }
7237 for (int k = kstart; k >= kend; k--) {
7238 BMVert *bmv = mesh_vert(vm, i, 0, k)->v;
7239 if (bmv) {
7240 vv.append(bmv);
7241 ee.append(bme);
7242 nv_bv_map.add(bmv, l->v);
7243 }
7244 }
7245 v = v->prev;
7246 }
7247 }
7248 do_rebuild = true;
7249 }
7250 else {
7251 vv.append(l->v);
7252 ee.append(l->e);
7253 nv_bv_map.add(l->v, l->v); // We keep the old vertex, i.e. mapping to itself.
7254 }
7255 }
7256 if (do_rebuild) {
7257 const int64_t n = vv.size();
7258 BMFace *f_new = bev_create_ngon(
7259 bp, bm, vv.data(), n, nullptr, f, nullptr, nullptr, &nv_bv_map, -1, true);
7260
7261 /* Copy attributes from old edges. */
7262 BLI_assert(n == ee.size());
7263 BMEdge *bme_prev = ee[n - 1];
7264 for (int k = 0; k < n; k++) {
7265 BMEdge *bme_new = BM_edge_exists(vv[k], vv[(k + 1) % n]);
7266 BLI_assert(ee[k] && bme_new);
7267 if (ee[k] != bme_new) {
7268 BM_elem_attrs_copy(bm, ee[k], bme_new);
7269 /* Want to undo seam and smooth for corner segments
7270 * if those attrs aren't contiguous around face. */
7271 if (k < n - 1 && ee[k] == ee[k + 1]) {
7273 {
7275 }
7276 /* Actually want "sharp" to be contiguous, so reverse the test. */
7277 if (!BM_elem_flag_test(ee[k], BM_ELEM_SMOOTH) &&
7279 {
7281 }
7282 }
7283 else {
7284 bme_prev = ee[k];
7285 }
7286 }
7287 }
7288
7289 /* Don't select newly or return created boundary faces. */
7290 if (f_new) {
7291 if (r_new_face) {
7292 *r_new_face = f_new;
7293 }
7294 record_face_kind(bp, f_new, F_RECON);
7296 /* Also don't want new edges that aren't part of a new bevel face. */
7297 BMIter eiter;
7298 BMEdge *bme;
7299 BM_ITER_ELEM (bme, &eiter, f_new, BM_EDGES_OF_FACE) {
7300 bool keep = false;
7301 BMIter fiter;
7302 BMFace *f_other;
7303 BM_ITER_ELEM (f_other, &fiter, bme, BM_FACES_OF_EDGE) {
7304 if (BM_elem_flag_test(f_other, BM_ELEM_TAG)) {
7305 keep = true;
7306 break;
7307 }
7308 }
7309 if (!keep) {
7311 }
7312 }
7313 }
7314 }
7315
7316 return do_rebuild;
7317}
7318
7319/* All polygons touching `v` need rebuilding because beveling `v` has made new vertices. */
7321 BevelParams *bp,
7322 BMVert *v,
7323 Set<BMFace *> &rebuilt_orig_faces,
7324 BMFace **r_rebuilt_face_0 = nullptr)
7325{
7326 BMIter iter;
7327 BMFace *f;
7328 BM_ITER_ELEM (f, &iter, v, BM_FACES_OF_VERT) {
7329 /* Deletion of original mesh faces that are being rebuild is deferred thus we have to perform
7330 * a check against `rebuilt_orig_faces` container - previous calls to
7331 * `bevel_rebuild_existing_polygons` could have already rebuilt faces touching vertex `v`. */
7332 if (!rebuilt_orig_faces.contains(f)) {
7333 BMFace *new_face = nullptr;
7334 if (bev_rebuild_polygon(bm, bp, f, &new_face)) {
7335 rebuilt_orig_faces.add(f);
7336 if (r_rebuilt_face_0 && BM_elem_index_get(f) == 0) {
7337 *r_rebuilt_face_0 = new_face;
7338 }
7339 }
7340 }
7341 }
7342}
7343
7344/* If there were any wire edges, they need to be reattached somewhere. */
7346{
7347 BevVert *bv = find_bevvert(bp, v);
7348 if (!bv || bv->wirecount == 0 || !bv->vmesh) {
7349 return;
7350 }
7351
7352 for (int i = 0; i < bv->wirecount; i++) {
7353 BMEdge *e = bv->wire_edges[i];
7354 /* Look for the new vertex closest to the other end of e. */
7355 BMVert *vclosest = nullptr;
7356 float dclosest = FLT_MAX;
7357 BMVert *votherclosest = nullptr;
7358 BMVert *vother = BM_edge_other_vert(e, v);
7359 BevVert *bvother = nullptr;
7360 if (BM_elem_flag_test(vother, BM_ELEM_TAG)) {
7361 bvother = find_bevvert(bp, vother);
7362 if (!bvother || !bvother->vmesh) {
7363 return; /* Shouldn't happen. */
7364 }
7365 }
7366 BoundVert *bndv = bv->vmesh->boundstart;
7367 do {
7368 if (bvother) {
7369 BoundVert *bndvother = bvother->vmesh->boundstart;
7370 do {
7371 float d = len_squared_v3v3(bndvother->nv.co, bndv->nv.co);
7372 if (d < dclosest) {
7373 vclosest = bndv->nv.v;
7374 votherclosest = bndvother->nv.v;
7375 dclosest = d;
7376 }
7377 } while ((bndvother = bndvother->next) != bvother->vmesh->boundstart);
7378 }
7379 else {
7380 float d = len_squared_v3v3(vother->co, bndv->nv.co);
7381 if (d < dclosest) {
7382 vclosest = bndv->nv.v;
7383 votherclosest = vother;
7384 dclosest = d;
7385 }
7386 }
7387 } while ((bndv = bndv->next) != bv->vmesh->boundstart);
7388 if (vclosest) {
7389 BM_edge_create(bm, vclosest, votherclosest, e, BM_CREATE_NO_DOUBLE);
7390 }
7391 }
7392}
7393
7394/*
7395 * Is this BevVert the special case of a weld (no vmesh) where there are
7396 * four edges total, two are beveled, and the other two are on opposite sides?
7397 */
7399{
7400 return (bv->edgecount == 4 && bv->selcount == 2 &&
7401 ((bv->edges[0].is_bev && bv->edges[2].is_bev) ||
7402 (bv->edges[1].is_bev && bv->edges[3].is_bev)));
7403}
7404
7424static void weld_cross_attrs_copy(BMesh *bm, BevVert *bv, VMesh *vm, int vmindex, EdgeHalf *e)
7425{
7426 BMEdge *bme_prev = nullptr;
7427 BMEdge *bme_next = nullptr;
7428 for (int i = 0; i < 4; i++) {
7429 if (&bv->edges[i] == e) {
7430 bme_prev = bv->edges[(i + 3) % 4].e;
7431 bme_next = bv->edges[(i + 1) % 4].e;
7432 break;
7433 }
7434 }
7435 BLI_assert(bme_prev && bme_next);
7436
7437 /* Want seams and sharp edges to cross only if that way on both sides. */
7438 bool disable_seam = BM_elem_flag_test(bme_prev, BM_ELEM_SEAM) !=
7440 bool enable_smooth = BM_elem_flag_test(bme_prev, BM_ELEM_SMOOTH) !=
7442
7443 int nseg = e->seg;
7444 for (int i = 0; i < nseg; i++) {
7445 BMEdge *bme = BM_edge_exists(mesh_vert(vm, vmindex, 0, i)->v,
7446 mesh_vert(vm, vmindex, 0, i + 1)->v);
7447 BLI_assert(bme);
7448 BM_elem_attrs_copy(bm, bme_prev, bme);
7449 if (disable_seam) {
7451 }
7452 if (enable_smooth) {
7454 }
7455 }
7456}
7457
7462{
7463 int mat_nr = bp->mat_nr;
7464
7465 if (!BM_edge_is_manifold(bme)) {
7466 return;
7467 }
7468
7469 BevVert *bv1 = find_bevvert(bp, bme->v1);
7470 BevVert *bv2 = find_bevvert(bp, bme->v2);
7471
7472 BLI_assert(bv1 && bv2);
7473
7474 EdgeHalf *e1 = find_edge_half(bv1, bme);
7475 EdgeHalf *e2 = find_edge_half(bv2, bme);
7476
7477 BLI_assert(e1 && e2);
7478
7479 /*
7480 * bme->v1
7481 * / | \
7482 * v1--|--v4
7483 * | | |
7484 * | | |
7485 * v2--|--v3
7486 * \ | /
7487 * bme->v2
7488 */
7489 int nseg = e1->seg;
7490 BLI_assert(nseg > 0 && nseg == e2->seg);
7491
7492 BMVert *bmv1 = e1->leftv->nv.v;
7493 BMVert *bmv4 = e1->rightv->nv.v;
7494 BMVert *bmv2 = e2->rightv->nv.v;
7495 BMVert *bmv3 = e2->leftv->nv.v;
7496
7497 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
7498
7499 BMFace *f1 = e1->fprev;
7500 BMFace *f2 = e1->fnext;
7501 BMFace *faces[4] = {f1, f1, f2, f2};
7502
7503 int i1 = e1->leftv->index;
7504 int i2 = e2->leftv->index;
7505 VMesh *vm1 = bv1->vmesh;
7506 VMesh *vm2 = bv2->vmesh;
7507
7508 BMVert *verts[4];
7509 verts[0] = bmv1;
7510 verts[1] = bmv2;
7511
7512 Map<BMVert *, BMVert *> nv_bv_map; /* New vertex to the (original) bevel vertex mapping. */
7513 nv_bv_map.add(verts[0], bv1->v);
7514 nv_bv_map.add(verts[1], bv2->v);
7515
7516 int odd = nseg % 2;
7517 int mid = nseg / 2;
7518 BMFace *fchoices[2] = {f1, f2};
7519 BMFace *f_choice = nullptr;
7520 int center_adj_k = -1;
7521 if (odd && e1->is_seam) {
7522 f_choice = choose_rep_face(bp, fchoices, 2);
7523 if (nseg > 1) {
7524 center_adj_k = f_choice == f1 ? mid + 2 : mid;
7525 }
7526 }
7527 for (int k = 1; k <= nseg; k++) {
7528 verts[3] = mesh_vert(vm1, i1, 0, k)->v;
7529 verts[2] = mesh_vert(vm2, i2, 0, nseg - k)->v;
7530 nv_bv_map.add(verts[3], bv1->v);
7531 nv_bv_map.add(verts[2], bv2->v);
7532 BMFace *r_f;
7533 if (odd && k == mid + 1) {
7534 if (e1->is_seam) {
7535 /* Straddles a seam: choose to interpolate in f_choice and snap the loops whose verts
7536 * are in the non-chosen face to bme for interpolation purposes.
7537 */
7538 BMEdge *edges[4];
7539 if (f_choice == f1) {
7540 edges[0] = edges[1] = nullptr;
7541 edges[2] = edges[3] = bme;
7542 }
7543 else {
7544 edges[0] = edges[1] = bme;
7545 edges[2] = edges[3] = nullptr;
7546 }
7547 r_f = bev_create_ngon(
7548 bp, bm, verts, 4, nullptr, f_choice, edges, nullptr, &nv_bv_map, mat_nr, true);
7549 }
7550 else {
7551 /* Straddles but not a seam: interpolate left half in f1, right half in f2. */
7552 r_f = bev_create_ngon(
7553 bp, bm, verts, 4, faces, f_choice, nullptr, nullptr, &nv_bv_map, mat_nr, true);
7554 }
7555 }
7556 else if (odd && k == center_adj_k && e1->is_seam) {
7557 /* The strip adjacent to the center one, in another UV island.
7558 * Snap the edge near the seam to bme to match what happens in
7559 * the bevel rings.
7560 */
7561 BMEdge *edges[4];
7562 BMFace *f_interp;
7563 if (k == mid) {
7564 edges[0] = edges[1] = nullptr;
7565 edges[2] = edges[3] = bme;
7566 f_interp = f1;
7567 }
7568 else {
7569 edges[0] = edges[1] = bme;
7570 edges[2] = edges[3] = nullptr;
7571 f_interp = f2;
7572 }
7573 r_f = bev_create_ngon(
7574 bp, bm, verts, 4, nullptr, f_interp, edges, nullptr, &nv_bv_map, mat_nr, true);
7575 }
7576 else if (!odd && k == mid) {
7577 /* Left poly that touches an even center line on right. */
7578 BMEdge *edges[4] = {nullptr, nullptr, bme, bme};
7579 r_f = bev_create_ngon(
7580 bp, bm, verts, 4, nullptr, f1, edges, nullptr, &nv_bv_map, mat_nr, true);
7581 }
7582 else if (!odd && k == mid + 1) {
7583 /* Right poly that touches an even center line on left. */
7584 BMEdge *edges[4] = {bme, bme, nullptr, nullptr};
7585 r_f = bev_create_ngon(
7586 bp, bm, verts, 4, nullptr, f2, edges, nullptr, &nv_bv_map, mat_nr, true);
7587 }
7588 else {
7589 /* Doesn't cross or touch the center line, so interpolate in appropriate f1 or f2. */
7590 BMFace *f = (k <= mid) ? f1 : f2;
7591 r_f = bev_create_ngon(
7592 bp, bm, verts, 4, nullptr, f, nullptr, nullptr, &nv_bv_map, mat_nr, true);
7593 }
7594 record_face_kind(bp, r_f, F_EDGE);
7595 /* Tag the long edges: those out of verts[0] and verts[2]. */
7596 BMIter iter;
7597 BMLoop *l;
7598 BM_ITER_ELEM (l, &iter, r_f, BM_LOOPS_OF_FACE) {
7599 if (ELEM(l->v, verts[0], verts[2])) {
7601 }
7602 }
7603 verts[0] = verts[3];
7604 verts[1] = verts[2];
7605 }
7606
7607 /* Copy edge data to first and last edge. */
7608 BMEdge *bme1 = BM_edge_exists(bmv1, bmv2);
7609 BMEdge *bme2 = BM_edge_exists(bmv3, bmv4);
7610 BLI_assert(bme1 && bme2);
7611 BM_elem_attrs_copy(bm, bme, bme1);
7612 BM_elem_attrs_copy(bm, bme, bme2);
7613
7614 /* If either end is a "weld cross", want continuity of edge attributes across end edge(s). */
7615 if (bevvert_is_weld_cross(bv1)) {
7616 weld_cross_attrs_copy(bm, bv1, vm1, i1, e1);
7617 }
7618 if (bevvert_is_weld_cross(bv2)) {
7619 weld_cross_attrs_copy(bm, bv2, vm2, i2, e2);
7620 }
7621}
7622
7623/* Find xnew > x0 so that distance((x0,y0), (xnew, ynew)) = dtarget.
7624 * False position Illinois method used because the function is somewhat linear
7625 * -> linear interpolation converges fast.
7626 * Assumes that the gradient is always between 1 and -1 for x in [x0, x0+dtarget]. */
7627static double find_superellipse_chord_endpoint(double x0, double dtarget, float r, bool rbig)
7628{
7629 double y0 = superellipse_co(x0, r, rbig);
7630 const double tol = 1e-13; /* accumulates for many segments so use low value. */
7631 const int maxiter = 10;
7632
7633 /* For gradient between -1 and 1, xnew can only be in [x0 + sqrt(2)/2*dtarget, x0 + dtarget]. */
7634 double xmin = x0 + M_SQRT2 / 2.0 * dtarget;
7635 xmin = std::min(xmin, 1.0);
7636 double xmax = x0 + dtarget;
7637 xmax = std::min(xmax, 1.0);
7638 double ymin = superellipse_co(xmin, r, rbig);
7639 double ymax = superellipse_co(xmax, r, rbig);
7640
7641 /* NOTE: using distance**2 (no sqrt needed) does not converge that well. */
7642 double dmaxerr = sqrt(pow((xmax - x0), 2) + pow((ymax - y0), 2)) - dtarget;
7643 double dminerr = sqrt(pow((xmin - x0), 2) + pow((ymin - y0), 2)) - dtarget;
7644
7645 double xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
7646 bool lastupdated_upper = true;
7647
7648 for (int iter = 0; iter < maxiter; iter++) {
7649 double ynew = superellipse_co(xnew, r, rbig);
7650 double dnewerr = sqrt(pow((xnew - x0), 2) + pow((ynew - y0), 2)) - dtarget;
7651 if (fabs(dnewerr) < tol) {
7652 break;
7653 }
7654 if (dnewerr < 0) {
7655 xmin = xnew;
7656 ymin = ynew;
7657 dminerr = dnewerr;
7658 if (!lastupdated_upper) {
7659 xnew = (dmaxerr / 2 * xmin - dminerr * xmax) / (dmaxerr / 2 - dminerr);
7660 }
7661 else {
7662 xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
7663 }
7664 lastupdated_upper = false;
7665 }
7666 else {
7667 xmax = xnew;
7668 ymax = ynew;
7669 dmaxerr = dnewerr;
7670 if (lastupdated_upper) {
7671 xnew = (dmaxerr * xmin - dminerr / 2 * xmax) / (dmaxerr - dminerr / 2);
7672 }
7673 else {
7674 xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
7675 }
7676 lastupdated_upper = true;
7677 }
7678 }
7679 return xnew;
7680}
7681
7691static void find_even_superellipse_chords_general(int seg, float r, double *xvals, double *yvals)
7692{
7693 const int smoothitermax = 10;
7694 const double error_tol = 1e-7;
7695 int imax = (seg + 1) / 2 - 1; /* Ceiling division - 1. */
7696
7697 bool seg_odd = seg % 2;
7698
7699 bool rbig;
7700 double mx;
7701 if (r > 1.0f) {
7702 rbig = true;
7703 mx = pow(0.5, 1.0 / r);
7704 }
7705 else {
7706 rbig = false;
7707 mx = 1 - pow(0.5, 1.0 / r);
7708 }
7709
7710 /* Initial positions, linear spacing along x axis. */
7711 for (int i = 0; i <= imax; i++) {
7712 xvals[i] = i * mx / seg * 2;
7713 yvals[i] = superellipse_co(xvals[i], r, rbig);
7714 }
7715 yvals[0] = 1;
7716
7717 /* Smooth distance loop. */
7718 for (int iter = 0; iter < smoothitermax; iter++) {
7719 double sum = 0.0;
7720 double dmin = 2.0;
7721 double dmax = 0.0;
7722 /* Update distances between neighbor points. Store the highest and
7723 * lowest to see if the maximum error to average distance (which isn't
7724 * known yet) is below required precision. */
7725 for (int i = 0; i < imax; i++) {
7726 double d = sqrt(pow((xvals[i + 1] - xvals[i]), 2) + pow((yvals[i + 1] - yvals[i]), 2));
7727 sum += d;
7728 dmax = std::max(d, dmax);
7729 dmin = std::min(d, dmin);
7730 }
7731 /* For last distance, weight with 1/2 if seg_odd. */
7732 double davg;
7733 if (seg_odd) {
7734 sum += M_SQRT2 / 2 * (yvals[imax] - xvals[imax]);
7735 davg = sum / (imax + 0.5);
7736 }
7737 else {
7738 sum += sqrt(pow((xvals[imax] - mx), 2) + pow((yvals[imax] - mx), 2));
7739 davg = sum / (imax + 1.0);
7740 }
7741 /* Max error in tolerance? -> Quit. */
7742 bool precision_reached = true;
7743 if (dmax - davg > error_tol) {
7744 precision_reached = false;
7745 }
7746 if (dmin - davg < error_tol) {
7747 precision_reached = false;
7748 }
7749 if (precision_reached) {
7750 break;
7751 }
7752
7753 /* Update new coordinates. */
7754 for (int i = 1; i <= imax; i++) {
7755 xvals[i] = find_superellipse_chord_endpoint(xvals[i - 1], davg, r, rbig);
7756 yvals[i] = superellipse_co(xvals[i], r, rbig);
7757 }
7758 }
7759
7760 /* Fill remaining. */
7761 if (!seg_odd) {
7762 xvals[imax + 1] = mx;
7763 yvals[imax + 1] = mx;
7764 }
7765 for (int i = imax + 1; i <= seg; i++) {
7766 yvals[i] = xvals[seg - i];
7767 xvals[i] = yvals[seg - i];
7768 }
7769
7770 if (!rbig) {
7771 for (int i = 0; i <= seg; i++) {
7772 double temp = xvals[i];
7773 xvals[i] = 1.0 - yvals[i];
7774 yvals[i] = 1.0 - temp;
7775 }
7776 }
7777}
7778
7787static void find_even_superellipse_chords(int n, float r, double *xvals, double *yvals)
7788{
7789 bool seg_odd = n % 2;
7790 int n2 = n / 2;
7791
7792 /* Special cases. */
7793 if (r == PRO_LINE_R) {
7794 /* Linear spacing. */
7795 for (int i = 0; i <= n; i++) {
7796 xvals[i] = double(i) / n;
7797 yvals[i] = 1.0 - double(i) / n;
7798 }
7799 return;
7800 }
7801 if (r == PRO_CIRCLE_R) {
7802 double temp = M_PI_2 / n;
7803 /* Angle spacing. */
7804 for (int i = 0; i <= n; i++) {
7805 xvals[i] = sin(i * temp);
7806 yvals[i] = cos(i * temp);
7807 }
7808 return;
7809 }
7810 if (r == PRO_SQUARE_IN_R) {
7811 /* n is even, distribute first and second half linear. */
7812 if (!seg_odd) {
7813 for (int i = 0; i <= n2; i++) {
7814 xvals[i] = 0.0;
7815 yvals[i] = 1.0 - double(i) / n2;
7816 xvals[n - i] = yvals[i];
7817 yvals[n - i] = xvals[i];
7818 }
7819 }
7820 /* n is odd, so get one corner-cut chord. */
7821 else {
7822 double temp = 1.0 / (n2 + M_SQRT2 / 2.0);
7823 for (int i = 0; i <= n2; i++) {
7824 xvals[i] = 0.0;
7825 yvals[i] = 1.0 - double(i) * temp;
7826 xvals[n - i] = yvals[i];
7827 yvals[n - i] = xvals[i];
7828 }
7829 }
7830 return;
7831 }
7832 if (r == PRO_SQUARE_R) {
7833 /* n is even, distribute first and second half linear. */
7834 if (!seg_odd) {
7835 for (int i = 0; i <= n2; i++) {
7836 xvals[i] = double(i) / n2;
7837 yvals[i] = 1.0;
7838 xvals[n - i] = yvals[i];
7839 yvals[n - i] = xvals[i];
7840 }
7841 }
7842 /* n is odd, so get one corner-cut chord. */
7843 else {
7844 double temp = 1.0 / (n2 + M_SQRT2 / 2);
7845 for (int i = 0; i <= n2; i++) {
7846 xvals[i] = double(i) * temp;
7847 yvals[i] = 1.0;
7848 xvals[n - i] = yvals[i];
7849 yvals[n - i] = xvals[i];
7850 }
7851 }
7852 return;
7853 }
7854 /* For general case use the more expensive search algorithm. */
7855 find_even_superellipse_chords_general(n, r, xvals, yvals);
7856}
7857
7864{
7865 int nseg = bp->seg;
7866
7867/* Precalculated fullness for circle profile radius and more common low seg values. */
7868#define CIRCLE_FULLNESS_SEGS 11
7869 static const float circle_fullness[CIRCLE_FULLNESS_SEGS] = {
7870 0.0f, /* nsegs == 1 */
7871 0.559f, /* 2 */
7872 0.642f, /* 3 */
7873 0.551f, /* 4 */
7874 0.646f, /* 5 */
7875 0.624f, /* 6 */
7876 0.646f, /* 7 */
7877 0.619f, /* 8 */
7878 0.647f, /* 9 */
7879 0.639f, /* 10 */
7880 0.647f, /* 11 */
7881 };
7882
7883 float fullness;
7885 /* Set fullness to the average "height" of the profile's sampled points. */
7886 fullness = 0.0f;
7887 for (int i = 0; i < nseg; i++) { /* Don't use the end points. */
7888 fullness += float(bp->pro_spacing.xvals[i] + bp->pro_spacing.yvals[i]) / (2.0f * nseg);
7889 }
7890 }
7891 else {
7892 /* An offline optimization process found fullness that led to closest fit to sphere as
7893 * a function of r and ns (for case of cube corner). */
7894 if (bp->pro_super_r == PRO_LINE_R) {
7895 fullness = 0.0f;
7896 }
7897 else if (bp->pro_super_r == PRO_CIRCLE_R && nseg > 0 && nseg <= CIRCLE_FULLNESS_SEGS) {
7898 fullness = circle_fullness[nseg - 1];
7899 }
7900 else {
7901 /* Linear regression fit found best linear function, separately for even/odd segs. */
7902 if (nseg % 2 == 0) {
7903 fullness = 2.4506f * bp->profile - 0.00000300f * nseg - 0.6266f;
7904 }
7905 else {
7906 fullness = 2.3635f * bp->profile + 0.000152f * nseg - 0.6060f;
7907 }
7908 }
7909 }
7910 return fullness;
7911}
7912
7924static void set_profile_spacing(BevelParams *bp, ProfileSpacing *pro_spacing, bool custom)
7925{
7926 int seg = bp->seg;
7927
7928 if (seg <= 1) {
7929 /* Only 1 segment, we don't need any profile information. */
7930 pro_spacing->xvals = nullptr;
7931 pro_spacing->yvals = nullptr;
7932 pro_spacing->xvals_2 = nullptr;
7933 pro_spacing->yvals_2 = nullptr;
7934 pro_spacing->seg_2 = 0;
7935 return;
7936 }
7937
7938 int seg_2 = max_ii(power_of_2_max_i(bp->seg), 4);
7939
7940 /* Sample the seg_2 segments used during vertex mesh subdivision. */
7941 bp->pro_spacing.seg_2 = seg_2;
7942 if (seg_2 == seg) {
7943 pro_spacing->xvals_2 = pro_spacing->xvals;
7944 pro_spacing->yvals_2 = pro_spacing->yvals;
7945 }
7946 else {
7947 pro_spacing->xvals_2 = static_cast<double *>(
7948 BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg_2 + 1)));
7949 pro_spacing->yvals_2 = static_cast<double *>(
7950 BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg_2 + 1)));
7951 if (custom) {
7952 /* Make sure the curve profile widget's sample table is full of the seg_2 samples. */
7953 BKE_curveprofile_init(const_cast<CurveProfile *>(bp->custom_profile), short(seg_2));
7954
7955 /* Copy segment locations into the profile spacing struct. */
7956 for (int i = 0; i < seg_2 + 1; i++) {
7957 pro_spacing->xvals_2[i] = double(bp->custom_profile->segments[i].y);
7958 pro_spacing->yvals_2[i] = double(bp->custom_profile->segments[i].x);
7959 }
7960 }
7961 else {
7963 seg_2, bp->pro_super_r, pro_spacing->xvals_2, pro_spacing->yvals_2);
7964 }
7965 }
7966
7967 /* Sample the input number of segments. */
7968 pro_spacing->xvals = static_cast<double *>(
7969 BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg + 1)));
7970 pro_spacing->yvals = static_cast<double *>(
7971 BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg + 1)));
7972 if (custom) {
7973 /* Make sure the curve profile's sample table is full. */
7974 if (bp->custom_profile->segments_len != seg || !bp->custom_profile->segments) {
7975 BKE_curveprofile_init(const_cast<CurveProfile *>(bp->custom_profile), short(seg));
7976 }
7977
7978 /* Copy segment locations into the profile spacing struct. */
7979 for (int i = 0; i < seg + 1; i++) {
7980 pro_spacing->xvals[i] = double(bp->custom_profile->segments[i].y);
7981 pro_spacing->yvals[i] = double(bp->custom_profile->segments[i].x);
7982 }
7983 }
7984 else {
7985 find_even_superellipse_chords(seg, bp->pro_super_r, pro_spacing->xvals, pro_spacing->yvals);
7986 }
7987}
7988
8013{
8014 float no_collide_offset = bp->offset + 1e6;
8015 float limit = no_collide_offset;
8016 if (bp->offset == 0.0f) {
8017 return no_collide_offset;
8018 }
8019 float kb = eb->offset_l_spec;
8020 EdgeHalf *ea = eb->next; /* NOTE: this is in direction b --> a. */
8021 float ka = ea->offset_r_spec;
8022 BMVert *vb, *vc;
8023 if (eb->is_rev) {
8024 vc = eb->e->v1;
8025 vb = eb->e->v2;
8026 }
8027 else {
8028 vb = eb->e->v1;
8029 vc = eb->e->v2;
8030 }
8031 BMVert *va = ea->is_rev ? ea->e->v1 : ea->e->v2;
8032 BevVert *bvc = nullptr;
8033 EdgeHalf *ebother = find_other_end_edge_half(bp, eb, &bvc);
8034 EdgeHalf *ec;
8035 BMVert *vd;
8036 float kc;
8038 if (ea->is_bev && ebother != nullptr && ebother->prev->is_bev) {
8039 if (bp->offset_type == BEVEL_AMT_PERCENT) {
8040 return 50.0f;
8041 }
8042 /* This is only right sometimes. The exact answer is very hard to calculate. */
8043 float blen = BM_edge_calc_length(eb->e);
8044 return bp->offset > blen / 2.0f ? blen / 2.0f : blen;
8045 }
8046 return no_collide_offset;
8047 }
8048 if (ebother != nullptr) {
8049 ec = ebother->prev; /* NOTE: this is in direction c --> d. */
8050 vc = bvc->v;
8051 kc = ec->offset_l_spec;
8052 vd = ec->is_rev ? ec->e->v1 : ec->e->v2;
8053 }
8054 else {
8055 /* No bevvert for w, so C can't be beveled. */
8056 kc = 0.0f;
8057 ec = nullptr;
8058 /* Find an edge from c that has same face. */
8059 if (eb->fnext == nullptr) {
8060 return no_collide_offset;
8061 }
8062 BMLoop *lb = BM_face_edge_share_loop(eb->fnext, eb->e);
8063 if (!lb) {
8064 return no_collide_offset;
8065 }
8066 if (lb->next->v == vc) {
8067 vd = lb->next->next->v;
8068 }
8069 else if (lb->v == vc) {
8070 vd = lb->prev->v;
8071 }
8072 else {
8073 return no_collide_offset;
8074 }
8075 }
8076 if (ea->e == eb->e || (ec && ec->e == eb->e)) {
8077 return no_collide_offset;
8078 }
8079 float th1 = angle_v3v3v3(va->co, vb->co, vc->co);
8080 float th2 = angle_v3v3v3(vb->co, vc->co, vd->co);
8081
8082 /* First calculate offset at which edge B collapses, which happens
8083 * when advancing clones of A, B, C all meet at a point. */
8084 float sin1 = sinf(th1);
8085 float sin2 = sinf(th2);
8086 float cos1 = cosf(th1);
8087 float cos2 = cosf(th2);
8088 /* The side offsets, overlap at the two corners, to create two corner vectors.
8089 * The intersection of these two corner vectors is the collapse point.
8090 * The length of edge B divided by the projection of these vectors onto edge B
8091 * is the number of 'offsets' that can be accommodated. */
8092 float offsets_projected_on_B = safe_divide(ka + cos1 * kb, sin1) +
8093 safe_divide(kc + cos2 * kb, sin2);
8094 if (offsets_projected_on_B > BEVEL_EPSILON) {
8095 offsets_projected_on_B = bp->offset * (len_v3v3(vb->co, vc->co) / offsets_projected_on_B);
8096 if (offsets_projected_on_B > BEVEL_EPSILON) {
8097 limit = offsets_projected_on_B;
8098 }
8099 }
8100
8101 /* Now check edge slide cases.
8102 * where side edges are in line with edge B and are not beveled, we should continue
8103 * iterating until we find a return edge (not in line with B) to provide a minimum offset
8104 * to the far side of the N-gon. This is not perfect, but is simpler and will catch many
8105 * more overlap issues. */
8106 if (kb > FLT_EPSILON && (ka == 0.0f || kc == 0.0f)) {
8107 // use bevel weight offsets and not the full offset where weights are used
8108 kb = bp->offset / kb;
8109
8110 if (ka == 0.0f) {
8111 BMLoop *la = BM_face_edge_share_loop(eb->fnext, ea->e);
8112 if (la) {
8113 float A_side_slide = 0.0f;
8114 float exterior_angle = 0.0f;
8115 bool first = true;
8116
8117 while (exterior_angle < 0.0001f) {
8118 if (first) {
8119 exterior_angle = float(M_PI) - th1;
8120 first = false;
8121 }
8122 else {
8123 la = la->prev;
8124 exterior_angle += float(M_PI) -
8125 angle_v3v3v3(la->v->co, la->next->v->co, la->next->next->v->co);
8126 }
8127 A_side_slide += BM_edge_calc_length(la->e) * sinf(exterior_angle);
8128 }
8129 limit = std::min(A_side_slide * kb, limit);
8130 }
8131 }
8132
8133 if (kc == 0.0f) {
8134 BMLoop *lc = BM_face_edge_share_loop(eb->fnext, eb->e);
8135 if (lc) {
8136 lc = lc->next;
8137 float C_side_slide = 0.0f;
8138 float exterior_angle = 0.0f;
8139 bool first = true;
8140 while (exterior_angle < 0.0001f) {
8141 if (first) {
8142 exterior_angle = float(M_PI) - th2;
8143 first = false;
8144 }
8145 else {
8146 lc = lc->next;
8147 exterior_angle += float(M_PI) -
8148 angle_v3v3v3(lc->prev->v->co, lc->v->co, lc->next->v->co);
8149 }
8150 C_side_slide += BM_edge_calc_length(lc->e) * sinf(exterior_angle);
8151 }
8152 limit = std::min(C_side_slide * kb, limit);
8153 }
8154 }
8155 }
8156 return limit;
8157}
8158
8165{
8166 float no_collide_offset = bp->offset + 1e6;
8167 if (bp->offset == 0.0f) {
8168 return no_collide_offset;
8169 }
8170 float ka = ea->offset_l_spec / bp->offset;
8171 EdgeHalf *eb = find_other_end_edge_half(bp, ea, nullptr);
8172 float kb = eb ? eb->offset_l_spec / bp->offset : 0.0f;
8173 float kab = ka + kb;
8174 float la = BM_edge_calc_length(ea->e);
8175 if (kab <= 0.0f) {
8176 return no_collide_offset;
8177 }
8178 return la / kab;
8179}
8180
8187{
8188 float limited_offset = bp->offset;
8189 BMIter iter;
8190 BMVert *bmv;
8191 BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
8192 if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
8193 continue;
8194 }
8195 BevVert *bv = find_bevvert(bp, bmv);
8196 if (!bv) {
8197 continue;
8198 }
8199 for (int i = 0; i < bv->edgecount; i++) {
8200 EdgeHalf *eh = &bv->edges[i];
8202 float collision_offset = vertex_collide_offset(bp, eh);
8203 limited_offset = std::min(collision_offset, limited_offset);
8204 }
8205 else {
8206 float collision_offset = geometry_collide_offset(bp, eh);
8207 limited_offset = std::min(collision_offset, limited_offset);
8208 }
8209 }
8210 }
8211
8212 if (limited_offset < bp->offset) {
8213 /* All current offset specs have some number times bp->offset,
8214 * so we can just multiply them all by the reduction factor
8215 * of the offset to have the effect of recalculating the specs
8216 * with the new limited_offset.
8217 */
8218 float offset_factor = limited_offset / bp->offset;
8219 BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
8220 if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
8221 continue;
8222 }
8223 BevVert *bv = find_bevvert(bp, bmv);
8224 if (!bv) {
8225 continue;
8226 }
8227 for (int i = 0; i < bv->edgecount; i++) {
8228 EdgeHalf *eh = &bv->edges[i];
8229 eh->offset_l_spec *= offset_factor;
8230 eh->offset_r_spec *= offset_factor;
8231 eh->offset_l *= offset_factor;
8232 eh->offset_r *= offset_factor;
8233 }
8234 }
8235 bp->offset = limited_offset;
8236 }
8237}
8238
8240 const float offset,
8241 const int offset_type,
8242 const int profile_type,
8243 const int segments,
8244 const float profile,
8245 const bool affect_type,
8246 const bool use_weights,
8247 const bool limit_offset,
8248 const MDeformVert *dvert,
8249 const int vertex_group,
8250 const int mat,
8251 const bool loop_slide,
8252 const bool mark_seam,
8253 const bool mark_sharp,
8254 const bool harden_normals,
8255 const int face_strength_mode,
8256 const int miter_outer,
8257 const int miter_inner,
8258 const float spread,
8259 const CurveProfile *custom_profile,
8260 const int vmesh_method,
8261 const int bweight_offset_vert,
8262 const int bweight_offset_edge)
8263{
8264 BMIter iter, liter;
8265 BMVert *v, *v_next;
8266 BMEdge *e;
8267 BMFace *f;
8268 BMLoop *l;
8269 BevVert *bv;
8270 BevelParams bp{};
8271 bp.bm = bm;
8272 bp.offset = offset;
8273 bp.offset_type = offset_type;
8274 bp.seg = max_ii(segments, 1);
8275 bp.profile = profile;
8276 bp.pro_super_r = -logf(2.0) / logf(sqrtf(profile)); /* Convert to superellipse exponent. */
8277 bp.affect_type = affect_type;
8278 bp.affect_vertices_odd = (affect_type == BEVEL_AFFECT_VERTICES) && (bp.seg % 2 == 1);
8279 bp.use_weights = use_weights;
8280 bp.bweight_offset_vert = bweight_offset_vert;
8281 bp.bweight_offset_edge = bweight_offset_edge;
8282 bp.loop_slide = loop_slide;
8283 bp.limit_offset = limit_offset;
8286 bp.dvert = dvert;
8287 bp.vertex_group = vertex_group;
8288 bp.mat_nr = mat;
8289 bp.mark_seam = mark_seam;
8290 bp.mark_sharp = mark_sharp;
8291 bp.harden_normals = harden_normals;
8292 bp.face_strength_mode = face_strength_mode;
8293 bp.miter_outer = miter_outer;
8294 bp.miter_inner = miter_inner;
8295 bp.spread = spread;
8296 bp.profile_type = profile_type;
8297 bp.custom_profile = custom_profile;
8298 bp.vmesh_method = vmesh_method;
8299
8300 if (bp.offset <= 0) {
8301 return;
8302 }
8303
8304#ifdef BEVEL_DEBUG_TIME
8305 double start_time = BLI_time_now_seconds();
8306#endif
8307
8308 /* Disable the miters with the cutoff vertex mesh method, the combination isn't useful anyway. */
8309 if (bp.vmesh_method == BEVEL_VMESH_CUTOFF) {
8312 }
8313
8314 if (profile >= 0.950f) { /* r ~ 692, so PRO_SQUARE_R is 1e4 */
8316 }
8317 else if (fabsf(bp.pro_super_r - PRO_CIRCLE_R) < 1e-4) {
8319 }
8320 else if (fabsf(bp.pro_super_r - PRO_LINE_R) < 1e-4) {
8322 }
8323 else if (bp.pro_super_r < 1e-4) {
8325 }
8326
8327 /* Primary alloc. */
8328 bp.mem_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), __func__);
8330
8331 /* Get the 2D profile point locations from either the superellipse or the custom profile. */
8333
8334 /* Get the 'fullness' of the profile for the ADJ vertex mesh method. */
8335 if (bp.seg > 1) {
8337 }
8338
8339 /* Get separate non-custom profile samples for the miter profiles if they are needed */
8342 {
8343 set_profile_spacing(&bp, &bp.pro_spacing_miter, false);
8344 }
8345
8346 bp.face_hash.emplace();
8347
8349 uv_vert_map_init(&bp, bm);
8350
8351 /* Analyze input vertices, sorting edges and assigning initial new vertex positions. */
8352 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8354 bv = bevel_vert_construct(bm, &bp, v);
8355 if (!limit_offset && bv) {
8356 build_boundary(&bp, bv, true);
8358 }
8359 }
8360 }
8361
8362 /* Perhaps clamp offset to avoid geometry collisions. */
8363 if (limit_offset) {
8364 bevel_limit_offset(&bp, bm);
8365
8366 /* Assign initial new vertex positions. */
8367 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8369 bv = find_bevvert(&bp, v);
8370 if (bv) {
8371 build_boundary(&bp, bv, true);
8373 }
8374 }
8375 }
8376 }
8377
8378 /* Perhaps do a pass to try to even out widths. */
8379 if (bp.offset_adjust) {
8380 adjust_offsets(&bp, bm);
8381 }
8382
8383 /* Maintain consistent orientations for the asymmetrical custom profiles. */
8385 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
8388 }
8389 }
8390 }
8391
8392 /* Build the meshes around vertices, now that positions are final. */
8393 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8395 bv = find_bevvert(&bp, v);
8396 if (bv) {
8397 build_vmesh(&bp, bm, bv);
8398 }
8399 }
8400 }
8401
8402 /* Build polygons for edges. */
8404 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
8407 }
8408 }
8409 }
8410
8411 /* Extend edge data like sharp edges and precompute normals for harden. */
8412 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8414 bv = find_bevvert(&bp, v);
8415 if (bv) {
8417 }
8418 }
8419 }
8420
8421 /* Rebuild face polygons around affected vertices. */
8422 Set<BMFace *> rebuilt_orig_faces;
8423 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8425 bevel_rebuild_existing_polygons(bm, &bp, v, rebuilt_orig_faces);
8426 bevel_reattach_wires(bm, &bp, v);
8427 }
8428 }
8429
8430 for (BMFace *f : rebuilt_orig_faces) {
8431 BM_face_kill(bm, f);
8432 }
8433
8434 BM_ITER_MESH_MUTABLE (v, v_next, &iter, bm, BM_VERTS_OF_MESH) {
8436 BLI_assert(find_bevvert(&bp, v) != nullptr);
8437 uv_vert_map_pop(&bp, v);
8438 BM_vert_kill(bm, v);
8439 }
8440 }
8441
8442 bevel_merge_uvs(&bp, bm);
8443
8444 if (bp.harden_normals) {
8446 }
8449 }
8450
8451 /* When called from operator (as opposed to modifier), bm->use_toolflags
8452 * will be set, and we need to transfer the oflags to BM_ELEM_TAGs. */
8453 if (bm->use_toolflags) {
8454 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
8457 }
8458 }
8459 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
8462 }
8463 }
8464 }
8465
8466 /* Clear the BM_ELEM_LONG_TAG tags, which were only set on some edges in F_EDGE faces. */
8467 BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
8468 if (get_face_kind(&bp, f) != F_EDGE) {
8469 continue;
8470 }
8471 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
8473 }
8474 }
8475
8476 /* Primary free. */
8478
8479#ifdef BEVEL_DEBUG_TIME
8480 double end_time = BLI_time_now_seconds();
8481 printf("BMESH BEVEL TIME = %.3f\n", end_time - start_time);
8482#endif
8483}
8484
8485} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
support for deformation groups and hooks.
#define STD_UV_CONNECT_LIMIT
#define BLI_array_alloca(arr, realsize)
Definition BLI_alloca.hh:18
#define BLI_ASSERT_UNIT_V3(v)
#define M_SQRT3
#define M_SQRT2
#define M_SQRT1_3
#define M_PI_2
#define M_PI
#define M_PI_4
Platform independent time functions.
#define MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID
Read Blender memory allocation and freeing.
#define UNUSED_VARS_NDEBUG(...)
#define BLI_INLINE
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define BM_BEVEL_EDGE_TAG_DISABLE(bme)
#define BEVEL_EPSILON_ANG
#define BEVEL_SMALL_ANG_DOT
#define BM_BEVEL_EDGE_TAG_TEST(bme)
#define BEVEL_EPSILON_ANG_DOT
#define BEVEL_SMALL_ANG
#define BEVEL_GOOD_ANGLE
#define VEC_VALUE_LEN
#define BEVEL_EPSILON_SQ
#define BM_BEVEL_EDGE_TAG_ENABLE(bme)
#define HASNOT_SEAMSHARP(eh, flag)
#define BEVEL_EPSILON
#define BEVEL_EPSILON_D
#define PRO_CIRCLE_R
#define PRO_SQUARE_IN_R
#define BEVEL_EPSILON_BIG
#define PRO_SQUARE_R
#define PRO_LINE_R
#define BM_ELEM_LONG_TAG
#define BEVEL_MATCH_SPEC_WEIGHT
#define CIRCLE_FULLNESS_SEGS
#define BM_ELEM_CD_GET_FLOAT(ele, offset)
#define BM_DISK_EDGE_NEXT(e, v)
#define BM_FACE_FIRST_LOOP(p)
#define BM_ELEM_CD_GET_FLOAT_P(ele, offset)
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
#define BM_elem_index_get(ele)
#define BM_elem_flag_disable(ele, hflag)
#define BM_elem_flag_set(ele, hflag, val)
#define BM_elem_flag_test(ele, hflag)
#define BM_elem_flag_enable(ele, hflag)
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
#define BM_ITER_MESH_MUTABLE(ele, ele_next, iter, bm, itype)
#define BMO_edge_flag_test(bm, e, oflag)
#define BMO_edge_flag_enable(bm, e, oflag)
#define BMO_vert_flag_enable(bm, e, oflag)
#define BMO_vert_flag_test(bm, e, oflag)
#define BMO_edge_flag_disable(bm, e, oflag)
#define MEM_SIZE_OPTIMAL(size)
#define EDGE_OUT
Definition bmo_bridge.cc:29
#define VERT_OUT
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
SIMD_FORCE_INLINE btVector3 dot3(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2) const
Definition btVector3.h:720
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
std::optional< Value > pop_try(const Key &key)
Definition BLI_map.hh:419
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:295
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
int64_t size() const
void append(const T &value)
#define printf
CCL_NAMESPACE_BEGIN ccl_device_inline float frac(const float x, ccl_private int *ix)
#define logf(x)
#define powf(x, y)
#define FLT_MAX
#define FLT_EPSILON
#define sin
#define pow
#define cos
float dot(VecOp< float, D >, VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float length(VecOp< float, D >) RET
void EIG_linear_solver_print_matrix(LinearSolver *solver)
void EIG_linear_solver_right_hand_side_add(LinearSolver *solver, int rhs, int index, double value)
LinearSolver * EIG_linear_least_squares_solver_new(int num_rows, int num_columns, int num_right_hand_sides)
void EIG_linear_solver_delete(LinearSolver *solver)
double EIG_linear_solver_variable_get(LinearSolver *solver, int rhs, int index)
void EIG_linear_solver_matrix_add(LinearSolver *solver, int row, int col, double value)
bool EIG_linear_solver_solve(LinearSolver *solver)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str)
Definition mallocn.cc:129
ccl_device_inline float beta(const float x, const float y)
Definition math_base.h:708
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
BMesh Iterators.
MINLINE float min_ff(float a, float b)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
void BM_lnorspace_update(BMesh *bm)
static bool is_canon(VMesh *vm, int i, int j, int k)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:451
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
BMFace * BM_face_create_verts(BMesh *bm, BMVert **vert_arr, const int len, const BMFace *f_example, const eBMCreateFlag create_flag, const bool create_edges)
static void create_mesh_bmvert(BMesh *bm, VMesh *vm, int i, int j, int k, BMVert *eg)
void BKE_curveprofile_init(struct CurveProfile *profile, short segments_len)
static void set_bound_vert_seams(BevVert *bv, bool mark_seam, bool mark_sharp)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
static void build_boundary_terminal_edge(BevelParams *bp, BevVert *bv, EdgeHalf *efirst, const bool construct)
int BM_edge_face_count(const BMEdge *e)
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
static void move_weld_profile_planes(BevVert *bv, BoundVert *bndv1, BoundVert *bndv2)
__int64 int64_t
Definition BKE_text.h:90
@ BEVEL_FACE_STRENGTH_ALL
@ BEVEL_FACE_STRENGTH_NEW
@ BEVEL_FACE_STRENGTH_NONE
@ BEVEL_FACE_STRENGTH_AFFECTED
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static BMFace * frep_for_center_poly(BevelParams *bp, BevVert *bv)
static void uv_vert_map_pop(BevelParams *bp, BMVert *v)
void BLI_memarena_use_calloc(MemArena *ma) ATTR_NONNULL(1)
int CustomData_number_of_layers(const CustomData *data, eCustomDataType type)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
static void update_uv_vert_map(BevelParams *bp, UVFace *uv_face, BMVert *bv, Map< BMVert *, BMVert * > *nv_bv_map)
Set< BMLoop * > UVVertBucket
static float vertex_collide_offset(BevelParams *bp, EdgeHalf *ea)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void fill_profile_fracs(BevelParams *bp, BoundVert *bndv, float *frac, int ns)
static EdgeHalf * next_edgehalf_bev(BevelParams *bp, EdgeHalf *start_edge, bool toward_bv, BevVert **r_bv)
static NewVert * mesh_vert_canon(VMesh *vm, int i, int j, int k)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
static BMFace * choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
static void bevel_edges_sharp_boundary(BMesh *bm, BevelParams *bp)
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
void BM_vert_kill(BMesh *bm, BMVert *v)
static void bevel_merge_uvs(BevelParams *bp, BMesh *bm)
float BM_edge_calc_face_angle_signed_ex(const BMEdge *e, const float fallback)
BMESH EDGE/FACE ANGLE.
static bool bev_rebuild_polygon(BMesh *bm, BevelParams *bp, BMFace *f, BMFace **r_new_face=nullptr)
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
MINLINE void mul_v2_fl(float r[2], float f)
static void swap_face_components(int *face_component, int totface, int c1, int c2)
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
static BoundVert * add_new_bound_vert(MemArena *mem_arena, VMesh *vm, const float co[3])
static void offset_in_plane(EdgeHalf *e, const float plane_no[3], bool left, float r_co[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
static BMFace * boundvert_rep_face(BoundVert *v, BMFace **r_fother)
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
static void build_square_in_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv, VMesh *vm1)
static bool good_offset_on_edge_between(EdgeHalf *e1, EdgeHalf *e2, EdgeHalf *emid, BMVert *v)
int CustomData_get_n_offset(const CustomData *data, eCustomDataType type, int n)
static EdgeHalf * next_bev(BevVert *bv, EdgeHalf *from_e)
static AngleKind edges_angle_kind(EdgeHalf *e1, EdgeHalf *e2, BMVert *v)
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mul_v2_m3v3(float r[2], const float M[3][3], const float a[3])
static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto, int ifrom, int jfrom, int kfrom)
BMLoop * BM_vert_step_fan_loop(BMLoop *l, BMEdge **e_step)
MINLINE int power_of_2_max_i(int n)
static BevVert * find_bevvert(BevelParams *bp, BMVert *bmv)
BMLoop * BM_face_edge_share_loop(BMFace *f, BMEdge *e)
Return the Loop Shared by Face and Edge.
bool CustomData_data_equals(eCustomDataType type, const void *data1, const void *data2)
static int interp_range(const float *frac, int n, const float f, float *r_rest)
float closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:403
static void bevel_build_trifan(BevelParams *bp, BMesh *bm, BevVert *bv)
static bool nearly_parallel(const float d1[3], const float d2[3])
static bool bevvert_is_weld_cross(BevVert *bv)
static void adjust_miter_inner_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
MINLINE int max_ii(int a, int b)
static void project_to_edge(const BMEdge *e, const float co_a[3], const float co_b[3], float projco[3])
int CustomData_get_layer_index(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const BMVert * v
BMVert * BM_vert_create(BMesh *bm, const float co[3], const BMVert *v_example, const eBMCreateFlag create_flag)
Main function for creating a new vertex.
static float geometry_collide_offset(BevelParams *bp, EdgeHalf *eb)
static bool eh_on_plane(EdgeHalf *e)
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static void math_layer_info_init(BevelParams *bp, BMesh *bm)
static int bevel_edge_order_extend(BMesh *bm, BevVert *bv, int i)
static void bevel_rebuild_existing_polygons(BMesh *bm, BevelParams *bp, BMVert *v, Set< BMFace * > &rebuilt_orig_faces, BMFace **r_rebuilt_face_0=nullptr)
BLI_INLINE void adjust_bound_vert(BoundVert *bv, const float co[3])
static void calculate_profile(BevelParams *bp, BoundVert *bndv, bool reversed, bool miter)
static BoundVert * pipe_test(BevVert *bv)
static void get_profile_point(BevelParams *bp, const Profile *pro, int i, int nseg, float r_co[3])
float BM_edge_calc_length(const BMEdge *e)
static void vmesh_copy_equiv_verts(VMesh *vm)
void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
Definition math_geom.cc:458
static void snap_to_pipe_profile(BoundVert *vpipe, bool midline, float co[3])
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition math_geom.cc:225
static void build_boundary_vertex_only(BevelParams *bp, BevVert *bv, bool construct)
static void record_face_kind(BevelParams *bp, BMFace *f, FKind fkind)
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MINLINE void mul_v3_fl(float r[3], float f)
static void set_profile_params(BevelParams *bp, BevVert *bv, BoundVert *bndv)
void BM_face_calc_center_bounds(const BMFace *f, float r_cent[3])
static void bevel_limit_offset(BevelParams *bp, BMesh *bm)
bool BM_face_point_inside_test(const BMFace *f, const float co[3])
static void calculate_vm_profiles(BevelParams *bp, BevVert *bv, VMesh *vm)
MINLINE void sub_v3_v3(float r[3], const float a[3])
static void get_incident_edges(BMFace *f, BMVert *v, BMEdge **r_e1, BMEdge **r_e2)
static void fill_vmesh_fracs(VMesh *vm, float *frac, int i)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
float BKE_defvert_find_weight(const MDeformVert *dvert, int defgroup)
Definition deform.cc:779
float area_poly_v2(const float verts[][2], unsigned int nr)
Definition math_geom.cc:186
static void adjust_the_cycle_or_chain(BoundVert *vstart, bool iscycle)
static void move_profile_plane(BoundVert *bndv, BMVert *bmvert)
static void make_unit_cube_map(const float va[3], const float vb[3], const float vc[3], const float vd[3], float r_mat[4][4])
static bool is_bad_uv_poly(BevVert *bv, BMFace *frep)
@ BM_CREATE_NO_DOUBLE
Definition bmesh_core.hh:36
@ BM_CREATE_NOP
Definition bmesh_core.hh:34
MINLINE void add_v2_v2(float r[2], const float a[2])
static BMEdge * find_closer_edge(float *co, BMEdge *e1, BMEdge *e2)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static VMesh * make_cube_corner_square_in(MemArena *mem_arena, int nseg)
static bool edges_face_connected_at_vert(BMEdge *bme1, BMEdge *bme2)
static BMFace * bevel_build_poly(BevelParams *bp, BMesh *bm, BevVert *bv)
static void build_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv)
static VMesh * interp_vmesh(BevelParams *bp, VMesh *vm_in, int nseg)
static float find_profile_fullness(BevelParams *bp)
static BevVert * bevel_vert_construct(BMesh *bm, BevelParams *bp, BMVert *v)
static UVFace * find_uv_face(BevelParams *bp, BMFace *bmf)
static bool contig_ldata_around_vert(BMesh *bm, BMVert *v)
static VMesh * pipe_adj_vmesh(BevelParams *bp, BevVert *bv, BoundVert *vpipe)
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
static void bevel_reattach_wires(BMesh *bm, BevelParams *bp, BMVert *v)
static double superellipse_co(double x, float r, bool rbig)
static void flag_out_edge(BMesh *bm, BMEdge *bme)
sqrt(x)+1/max(0
static bool fast_bevel_edge_order(BevVert *bv)
MINLINE void negate_v3(float r[3])
static void flag_out_vert(BMesh *bm, BMVert *bmv)
static void adjust_offsets(BevelParams *bp, BMesh *bm)
static bool offset_on_edge_between(BevelParams *bp, EdgeHalf *e1, EdgeHalf *e2, EdgeHalf *emid, BMVert *v, float meetco[3], float *r_sinratio)
static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
static void find_even_superellipse_chords_general(int seg, float r, double *xvals, double *yvals)
static float sabin_gamma(int n)
static int find_face_internal_boundverts(const BevVert *bv, const BMFace *f, BoundVert *(r_internal[3]))
static void calculate_profile_segments(const Profile *profile, const float map[4][4], const bool use_map, const bool reversed, const int ns, const double *xvals, const double *yvals, float *r_prof_co)
static void bevel_harden_normals(BevelParams *bp, BMesh *bm)
Harden normals for bevel.
static float projected_boundary_area(BevVert *bv, BMFace *f)
void BKE_lnor_space_custom_normal_to_data(const MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition math_geom.cc:535
static void slide_dist(EdgeHalf *e, BMVert *v, float d, float r_slideco[3])
static NewVert * mesh_vert(VMesh *vm, int i, int j, int k)
MINLINE void zero_v3(float r[3])
static bool is_outside_edge(EdgeHalf *e, const float co[3], BMVert **ret_closer_v)
void mul_m4_v4(const float mat[4][4], float r[4])
static UVFace * register_uv_face(BevelParams *bp, BMFace *fnew, BMFace *frep, BMFace **frep_arr)
static void snap_to_superellipsoid(float co[3], const float super_r, bool midline)
bool isect_line_plane_v3(float r_isect_co[3], const float l1[3], const float l2[3], const float plane_co[3], const float plane_no[3]) ATTR_WARN_UNUSED_RESULT
void BM_mesh_bevel(BMesh *bm, const float offset, const int offset_type, const int profile_type, const int segments, const float profile, const bool affect_type, const bool use_weights, const bool limit_offset, const MDeformVert *dvert, const int vertex_group, const int mat, const bool loop_slide, const bool mark_seam, const bool mark_sharp, const bool harden_normals, const int face_strength_mode, const int miter_outer, const int miter_inner, const float spread, const CurveProfile *custom_profile, const int vmesh_method, const int bweight_offset_vert, const int bweight_offset_edge)
static int tri_corner_test(BevelParams *bp, BevVert *bv)
static bool contig_ldata_across_edge(BMesh *bm, BMEdge *e, BMFace *f1, BMFace *f2)
int CustomData_get_named_layer_index(const CustomData *data, eCustomDataType type, StringRef name)
static void find_even_superellipse_chords(int n, float r, double *xvals, double *yvals)
static void avg4(float co[3], const NewVert *v0, const NewVert *v1, const NewVert *v2, const NewVert *v3)
static void snap_edges_for_vmesh_vert(int i, int j, int k, int ns, int ns2, int n_bndv, BMEdge *eprev, BMEdge *enext, BMEdge *enextnext, BMFace **bndv_rep_faces, BMFace *center_frep, const bool *frep_beats_next, BMEdge *r_snap_edges[4])
static void disable_flag_out_edge(BMesh *bm, BMEdge *bme)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
static bool make_unit_square_map(const float va[3], const float vmid[3], const float vb[3], float r_mat[4][4])
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
static void bevel_extend_edge_data_ex(BevVert *bv, int flag)
static void uv_vert_map_init(BevelParams *bp, BMesh *bm)
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
static VMesh * make_cube_corner_square(MemArena *mem_arena, int nseg)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static float edge_face_angle(EdgeHalf *e)
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
static void bevel_build_cutoff(BevelParams *bp, BMesh *bm, BevVert *bv)
static bool point_between_edges(const float co[3], BMVert *v, BMFace *f, EdgeHalf *e1, EdgeHalf *e2)
MINLINE bool compare_v3v3(const float v1[3], const float v2[3], float limit) ATTR_WARN_UNUSED_RESULT
static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
float dist_squared_to_plane_v3(const float p[3], const float plane[4])
Definition math_geom.cc:486
Map< BMVert *, Vector< UVVertBucket > > UVVertMap
static BMFace * bev_create_ngon(BevelParams *bp, BMesh *bm, BMVert **vert_arr, const int totv, BMFace **face_arr, BMFace *facerep, BMEdge **snap_edge_arr, BMVert *bv, Map< BMVert *, BMVert * > *nv_bv_map, int mat_nr, bool do_interp)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
MINLINE void copy_v3_fl(float r[3], float f)
void BM_face_kill(BMesh *bm, BMFace *f)
void interp_bilinear_quad_v3(float data[4][3], float u, float v, float res[3])
static EdgeHalf * find_edge_half(BevVert *bv, BMEdge *bme)
MINLINE float max_ff(float a, float b)
BMFace * BM_face_split(BMesh *bm, BMFace *f, BMLoop *l_a, BMLoop *l_b, BMLoop **r_l, BMEdge *example, const bool no_double)
Face Split.
static bool contig_ldata_across_loops(BMesh *bm, BMLoop *l1, BMLoop *l2, int layer_index)
static void closer_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float v[3])
static bool nearly_parallel_normalized(const float d1[3], const float d2[3])
static void weld_cross_attrs_copy(BMesh *bm, BevVert *bv, VMesh *vm, int vmindex, EdgeHalf *e)
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
static BMEdge * snap_edge_for_center_vmesh_vert(int i, int n_bndv, BMEdge *eprev, BMEdge *enext, BMFace **bndv_rep_faces, BMFace *center_frep, const bool *frep_beats_next)
static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv, BoundVert *vpipe)
static void offset_meet(BevelParams *bp, EdgeHalf *e1, EdgeHalf *e2, BMVert *v, BMFace *f, bool edges_between, float meetco[3], const EdgeHalf *e_in_plane)
static VMesh * cubic_subdiv(BevelParams *bp, VMesh *vm_in)
static double find_superellipse_chord_endpoint(double x0, double dtarget, float r, bool rbig)
@ BEVEL_PROFILE_SUPERELLIPSE
double BLI_time_now_seconds()
Definition time.cc:30
static VMesh * tri_corner_adj_vmesh(BevelParams *bp, BevVert *bv)
static void regularize_profile_orientation(BevelParams *bp, BMEdge *bme)
static int count_ccw_edges_between(EdgeHalf *e1, EdgeHalf *e2)
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const StringRef name)
static void build_center_ngon(BevelParams *bp, BMesh *bm, BevVert *bv, int mat_nr)
static EdgeHalf * find_other_end_edge_half(BevelParams *bp, EdgeHalf *e, BevVert **r_bvother)
static VMesh * square_out_adj_vmesh(BevelParams *bp, BevVert *bv)
static void bevel_extend_edge_data(BevVert *bv)
void BM_elem_attrs_copy(BMesh *bm, const BMCustomDataCopyMap &map, const BMVert *src, BMVert *dst)
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
MINLINE int is_power_of_2_i(int n)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void find_bevel_edge_order(BMesh *bm, BevVert *bv, BMEdge *first_bme)
static void check_edge_data_seam_sharp_edges(BevVert *bv, int flag)
static VMesh * make_cube_corner_adj_vmesh(BevelParams *bp)
static void vmesh_center(VMesh *vm, float r_cent[3])
static VMesh * adj_vmesh(BevelParams *bp, BevVert *bv)
static bool offset_meet_edge(EdgeHalf *e1, EdgeHalf *e2, BMVert *v, float meetco[3], float *r_angle)
MINLINE float safe_divide(float a, float b)
static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
BMEdge * BM_edge_create(BMesh *bm, BMVert *v1, BMVert *v2, const BMEdge *e_example, const eBMCreateFlag create_flag)
Main function for creating a new edge.
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
bool CustomData_layer_has_math(const CustomData *data, int layer_n)
static bool edge_edge_angle_less_than_180(const BMEdge *e1, const BMEdge *e2, const BMFace *f)
static void determine_uv_vert_connectivity(BevelParams *bp, BMesh *bm, BMVert *v)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
MINLINE int compare_ff(float a, float b, float max_diff)
bool BM_vert_face_check(const BMVert *v)
void BM_mesh_normals_update(BMesh *bm)
static VMesh * new_adj_vmesh(MemArena *mem_arena, int count, int seg, BoundVert *bounds)
void BM_loop_interp_from_face(BMesh *bm, BMLoop *l_dst, const BMFace *f_src, const bool do_vertex, const bool do_multires)
void axis_dominant_v3_to_m3(float r_mat[3][3], const float normal[3])
Normal to x,y matrix.
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
int isect_line_line_v3(const float v1[3], const float v2[3], const float v3[3], const float v4[3], float r_i1[3], float r_i2[3])
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static FKind get_face_kind(BevelParams *bp, BMFace *f)
static void offset_meet_lines_percent_or_absolute(BevelParams *bp, EdgeHalf *e1, EdgeHalf *e2, BMVert *v, float r_l1a[3], float r_l1b[3], float r_l2a[3], float r_l2b[3])
static void bevel_vert_two_edges(BevelParams *bp, BMesh *bm, BevVert *bv)
static void adjust_miter_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
MINLINE bool compare_v2v2(const float v1[2], const float v2[2], float limit) ATTR_WARN_UNUSED_RESULT
static void bevel_set_weighted_normal_face_strength(BMesh *bm, BevelParams *bp)
static void set_profile_spacing(BevelParams *bp, ProfileSpacing *pro_spacing, bool custom)
vector snap(vector a, vector b)
Definition node_math.h:147
#define fabsf
#define sqrtf
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * l
struct BMLoop * next
struct BMLoop * prev
struct BMEdge * e
struct BMVert * v
struct BMLoop * radial_next
struct BMFace * f
struct BMEdge * e
CustomData pdata
struct MLoopNorSpaceArray * lnor_spacearr
CustomData ldata
BMEdge ** wire_edges
Vector< UVVertMap > uv_vert_maps
const MDeformVert * dvert
ProfileSpacing pro_spacing
Map< BMVert *, BevVert * > vert_hash
const CurveProfile * custom_profile
Map< BMFace *, UVFace * > uv_face_hash
MathLayerInfo math_layer_info
std::optional< Map< BMFace *, FKind > > face_hash
ProfileSpacing pro_spacing_miter
BoundVert * adjchain
CurveProfilePoint * segments
BoundVert * leftv
BoundVert * rightv
MLoopNorSpace ** lspacearr
Definition BKE_mesh.h:296
BMFace * attached_frep
BoundVert * boundstart
MeshKind mesh_kind
ccl_device_inline void print_float3(const ccl_private char *label, const float3 a)