Blender  V2.93
bmesh_bevel.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_curveprofile_types.h"
26 #include "DNA_meshdata_types.h"
27 #include "DNA_modifier_types.h"
28 #include "DNA_scene_types.h"
29 
30 #include "BLI_alloca.h"
31 #include "BLI_array.h"
32 #include "BLI_math.h"
33 #include "BLI_memarena.h"
34 #include "BLI_utildefines.h"
35 
36 #include "BKE_curveprofile.h"
37 #include "BKE_customdata.h"
38 #include "BKE_deform.h"
39 #include "BKE_mesh.h"
40 
41 #include "eigen_capi.h"
42 
43 #include "bmesh.h"
44 #include "bmesh_bevel.h" /* own include */
45 
46 #include "./intern/bmesh_private.h"
47 
48 // #define BEVEL_DEBUG_TIME
49 #ifdef BEVEL_DEBUG_TIME
50 # include "PIL_time.h"
51 #endif
52 
53 #define BEVEL_EPSILON_D 1e-6
54 #define BEVEL_EPSILON 1e-6f
55 #define BEVEL_EPSILON_SQ 1e-12f
56 #define BEVEL_EPSILON_BIG 1e-4f
57 #define BEVEL_EPSILON_BIG_SQ 1e-8f
58 #define BEVEL_EPSILON_ANG DEG2RADF(2.0f)
59 #define BEVEL_SMALL_ANG DEG2RADF(10.0f)
61 #define BEVEL_SMALL_ANG_DOT (1.0f - cosf(BEVEL_SMALL_ANG))
63 #define BEVEL_EPSILON_ANG_DOT (1.0f - cosf(BEVEL_EPSILON_ANG))
64 #define BEVEL_MAX_ADJUST_PCT 10.0f
65 #define BEVEL_MAX_AUTO_ADJUST_PCT 300.0f
66 #define BEVEL_MATCH_SPEC_WEIGHT 0.2
67 
68 //#define DEBUG_CUSTOM_PROFILE_CUTOFF
69 /* Happens far too often, uncomment for development. */
70 // #define BEVEL_ASSERT_PROJECT
71 
72 /* for testing */
73 // #pragma GCC diagnostic error "-Wpadded"
74 
75 /* Constructed vertex, sometimes later instantiated as BMVert. */
76 typedef struct NewVert {
78  float co[3];
79  char _pad[4];
81 
82 struct BoundVert;
83 
84 /* Data for one end of an edge involved in a bevel. */
85 typedef struct EdgeHalf {
87  struct EdgeHalf *next, *prev;
95  struct BoundVert *leftv;
97  struct BoundVert *rightv;
101  int seg;
103  float offset_l;
105  float offset_r;
111  bool is_bev;
113  bool is_rev;
115  bool is_seam;
118  char _pad[4];
120 
135 typedef struct Profile {
137  float super_r;
139  float height;
141  float start[3];
143  float middle[3];
145  float end[3];
147  float plane_no[3];
149  float plane_co[3];
151  float proj_dir[3];
153  float *prof_co;
155  float *prof_co_2;
159 #define PRO_SQUARE_R 1e4f
160 #define PRO_CIRCLE_R 2.0f
161 #define PRO_LINE_R 1.0f
162 #define PRO_SQUARE_IN_R 0.0f
163 
169 typedef struct ProfileSpacing {
171  double *xvals;
173  double *yvals;
175  double *xvals_2;
177  double *yvals_2;
179  int seg_2;
181  float fullness;
183 
197 typedef struct MathLayerInfo {
203 
208 typedef struct BoundVert {
210  struct BoundVert *next, *prev;
220  int index;
222  float sinratio;
228  bool any_seam;
230  bool visited;
237  char _pad[3];
239  int seam_len;
243 
245 typedef struct VMesh {
251  int count;
253  int seg;
255  enum {
256  M_NONE, /* No polygon mesh needed. */
257  M_POLY, /* A simple polygon. */
258  M_ADJ, /* "Adjacent edges" mesh pattern. */
259  M_TRI_FAN, /* A simple polygon - fan filled. */
260  M_CUTOFF, /* A triangulated face at the end of each profile. */
262 
263  int _pad;
265 
266 /* Data for a vertex involved in a bevel. */
267 typedef struct BevVert {
273  int selcount;
277  float offset;
279  bool any_seam;
281  bool visited;
283  char _pad[6];
290 
291 /* Face classification. Note: depends on F_RECON > F_EDGE > F_VERT .*/
292 typedef enum {
303 } FKind;
304 
306 typedef enum AngleKind {
314 
316 typedef struct BevelParams {
332  float offset;
340  int seg;
342  float profile;
344  float pro_super_r;
354  bool mark_seam;
359  char _pad[1];
363  const struct MDeformVert *dvert;
367  int mat_nr;
377  float spread;
379  float smoothresh;
381 
382 // #pragma GCC diagnostic ignored "-Wpadded"
383 
384 /* Only for debugging, this file shouldn't be in blender repo. */
385 // #include "bevdebug.c"
386 
387 /* Use the unused _BM_ELEM_TAG_ALT flag to flag the 'long' loops (parallel to beveled edge)
388  * of edge-polygons. */
389 #define BM_ELEM_LONG_TAG (1 << 6)
390 
391 /* These flag values will get set on geom we want to return in 'out' slots for edges and verts. */
392 #define EDGE_OUT 4
393 #define VERT_OUT 8
394 
395 /* If we're called from the modifier, tool flags aren't available,
396  * but don't need output geometry. */
397 static void flag_out_edge(BMesh *bm, BMEdge *bme)
398 {
399  if (bm->use_toolflags) {
401  }
402 }
403 
404 static void flag_out_vert(BMesh *bm, BMVert *bmv)
405 {
406  if (bm->use_toolflags) {
408  }
409 }
410 
412 {
413  if (bm->use_toolflags) {
415  }
416 }
417 
418 static void record_face_kind(BevelParams *bp, BMFace *f, FKind fkind)
419 {
420  if (bp->face_hash) {
422  }
423 }
424 
426 {
427  void *val = BLI_ghash_lookup(bp->face_hash, f);
428  return val ? (FKind)POINTER_AS_INT(val) : F_ORIG;
429 }
430 
431 /* Are d1 and d2 parallel or nearly so? */
432 static bool nearly_parallel(const float d1[3], const float d2[3])
433 {
434  float ang = angle_v3v3(d1, d2);
435 
436  return (fabsf(ang) < BEVEL_EPSILON_ANG) || (fabsf(ang - (float)M_PI) < BEVEL_EPSILON_ANG);
437 }
438 
442 static bool nearly_parallel_normalized(const float d1[3], const float d2[3])
443 {
444  BLI_ASSERT_UNIT_V3(d1);
445  BLI_ASSERT_UNIT_V3(d2);
446 
447  const float direction_dot = dot_v3v3(d1, d2);
448  return compare_ff(fabsf(direction_dot), 1.0f, BEVEL_EPSILON_ANG_DOT);
449 }
450 
451 /* Make a new BoundVert of the given kind, inserting it at the end of the circular linked
452  * list with entry point bv->boundstart, and return it. */
453 static BoundVert *add_new_bound_vert(MemArena *mem_arena, VMesh *vm, const float co[3])
454 {
456 
457  copy_v3_v3(ans->nv.co, co);
458  if (!vm->boundstart) {
459  ans->index = 0;
460  vm->boundstart = ans;
461  ans->next = ans->prev = ans;
462  }
463  else {
464  BoundVert *tail = vm->boundstart->prev;
465  ans->index = tail->index + 1;
466  ans->prev = tail;
467  ans->next = vm->boundstart;
468  tail->next = ans;
469  vm->boundstart->prev = ans;
470  }
471  ans->profile.super_r = PRO_LINE_R;
472  ans->adjchain = NULL;
473  ans->sinratio = 1.0f;
474  ans->visited = false;
475  ans->any_seam = false;
476  ans->is_arc_start = false;
477  ans->is_patch_start = false;
478  ans->is_profile_start = false;
479  vm->count++;
480  return ans;
481 }
482 
483 BLI_INLINE void adjust_bound_vert(BoundVert *bv, const float co[3])
484 {
485  copy_v3_v3(bv->nv.co, co);
486 }
487 
488 /* Mesh verts are indexed (i, j, k) where
489  * i = boundvert index (0 <= i < nv)
490  * j = ring index (0 <= j <= ns2)
491  * k = segment index (0 <= k <= ns)
492  * Not all of these are used, and some will share BMVerts. */
493 static NewVert *mesh_vert(VMesh *vm, int i, int j, int k)
494 {
495  int nj = (vm->seg / 2) + 1;
496  int nk = vm->seg + 1;
497 
498  return &vm->mesh[i * nk * nj + j * nk + k];
499 }
500 
501 static void create_mesh_bmvert(BMesh *bm, VMesh *vm, int i, int j, int k, BMVert *eg)
502 {
503  NewVert *nv = mesh_vert(vm, i, j, k);
504  nv->v = BM_vert_create(bm, nv->co, eg, BM_CREATE_NOP);
506  flag_out_vert(bm, nv->v);
507 }
508 
509 static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto, int ifrom, int jfrom, int kfrom)
510 {
511  NewVert *nvto = mesh_vert(vm, ito, jto, kto);
512  NewVert *nvfrom = mesh_vert(vm, ifrom, jfrom, kfrom);
513  nvto->v = nvfrom->v;
514  copy_v3_v3(nvto->co, nvfrom->co);
515 }
516 
517 /* Find the EdgeHalf in bv's array that has edge bme. */
519 {
520  for (int i = 0; i < bv->edgecount; i++) {
521  if (bv->edges[i].e == bme) {
522  return &bv->edges[i];
523  }
524  }
525  return NULL;
526 }
527 
528 /* Find the BevVert corresponding to BMVert bmv. */
530 {
531  return BLI_ghash_lookup(bp->vert_hash, bmv);
532 }
533 
540 {
541  BevVert *bvo = find_bevvert(bp, e->is_rev ? e->e->v1 : e->e->v2);
542  if (bvo) {
543  if (r_bvother) {
544  *r_bvother = bvo;
545  }
546  EdgeHalf *eother = find_edge_half(bvo, e->e);
547  BLI_assert(eother != NULL);
548  return eother;
549  }
550  if (r_bvother) {
551  *r_bvother = NULL;
552  }
553  return NULL;
554 }
555 
556 /* Return the next EdgeHalf after from_e that is beveled.
557  * If from_e is NULL, find the first beveled edge. */
558 static EdgeHalf *next_bev(BevVert *bv, EdgeHalf *from_e)
559 {
560  if (from_e == NULL) {
561  from_e = &bv->edges[bv->edgecount - 1];
562  }
563  EdgeHalf *e = from_e;
564  do {
565  if (e->is_bev) {
566  return e;
567  }
568  } while ((e = e->next) != from_e);
569  return NULL;
570 }
571 
572 /* Return the count of edges between e1 and e2 when going around bv CCW. */
574 {
575  int cnt = 0;
576  EdgeHalf *e = e1;
577 
578  do {
579  if (e == e2) {
580  break;
581  }
582  e = e->next;
583  cnt++;
584  } while (e != e1);
585  return cnt;
586 }
587 
588 /* Assume bme1 and bme2 both share some vert. Do they share a face?
589  * If they share a face then there is some loop around bme1 that is in a face
590  * where the next or previous edge in the face must be bme2. */
591 static bool edges_face_connected_at_vert(BMEdge *bme1, BMEdge *bme2)
592 {
593  BMIter iter;
594  BMLoop *l;
595  BM_ITER_ELEM (l, &iter, bme1, BM_LOOPS_OF_EDGE) {
596  if (l->prev->e == bme2 || l->next->e == bme2) {
597  return true;
598  }
599  }
600  return false;
601 }
602 
611 {
612  BMFace *frep;
613 
614  BMFace *frep2 = NULL;
615  if (v->ebev) {
616  frep = v->ebev->fprev;
617  if (v->efirst->fprev != frep) {
618  frep2 = v->efirst->fprev;
619  }
620  }
621  else if (v->efirst) {
622  frep = v->efirst->fprev;
623  if (frep) {
624  if (v->elast->fnext != frep) {
625  frep2 = v->elast->fnext;
626  }
627  else if (v->efirst->fnext != frep) {
628  frep2 = v->efirst->fnext;
629  }
630  else if (v->elast->fprev != frep) {
631  frep2 = v->efirst->fprev;
632  }
633  }
634  else if (v->efirst->fnext) {
635  frep = v->efirst->fnext;
636  if (v->elast->fnext != frep) {
637  frep2 = v->elast->fnext;
638  }
639  }
640  else if (v->elast->fprev) {
641  frep = v->elast->fprev;
642  }
643  }
644  else if (v->prev->elast) {
645  frep = v->prev->elast->fnext;
646  if (v->next->efirst) {
647  if (frep) {
648  frep2 = v->next->efirst->fprev;
649  }
650  else {
651  frep = v->next->efirst->fprev;
652  }
653  }
654  }
655  else {
656  frep = NULL;
657  }
658  if (r_fother) {
659  *r_fother = frep2;
660  }
661  return frep;
662 }
663 
675  BMVert **vert_arr,
676  const int totv,
677  BMFace **face_arr,
678  BMFace *facerep,
679  BMEdge **edge_arr,
680  int mat_nr,
681  bool do_interp)
682 {
683  BMFace *f = BM_face_create_verts(bm, vert_arr, totv, facerep, BM_CREATE_NOP, true);
684 
685  if ((facerep || (face_arr && face_arr[0])) && f) {
686  BM_elem_attrs_copy(bm, bm, facerep ? facerep : face_arr[0], f);
687  if (do_interp) {
688  int i = 0;
689  BMIter iter;
690  BMLoop *l;
691  BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
692  BMFace *interp_f;
693  if (face_arr) {
694  /* Assume loops of created face are in same order as verts. */
695  BLI_assert(l->v == vert_arr[i]);
696  interp_f = face_arr[i];
697  }
698  else {
699  interp_f = facerep;
700  }
701  if (interp_f) {
702  BMEdge *bme = NULL;
703  if (edge_arr) {
704  bme = edge_arr[i];
705  }
706  float save_co[3];
707  if (bme) {
708  copy_v3_v3(save_co, l->v->co);
709  closest_to_line_segment_v3(l->v->co, save_co, bme->v1->co, bme->v2->co);
710  }
711  BM_loop_interp_from_face(bm, l, interp_f, true, true);
712  if (bme) {
713  copy_v3_v3(l->v->co, save_co);
714  }
715  }
716  i++;
717  }
718  }
719  }
720 
721  /* Not essential for bevels own internal logic,
722  * this is done so the operator can select newly created geometry. */
723  if (f) {
725  BMIter iter;
726  BMEdge *bme;
727  BM_ITER_ELEM (bme, &iter, f, BM_EDGES_OF_FACE) {
728  flag_out_edge(bm, bme);
729  }
730  }
731 
732  if (mat_nr >= 0) {
733  f->mat_nr = (short)mat_nr;
734  }
735  return f;
736 }
737 
739  BMVert *v1,
740  BMVert *v2,
741  BMVert *v3,
742  BMVert *v4,
743  BMFace *f1,
744  BMFace *f2,
745  BMFace *f3,
746  BMFace *f4,
747  int mat_nr)
748 {
749  BMVert *varr[4] = {v1, v2, v3, v4};
750  BMFace *farr[4] = {f1, f2, f3, f4};
751  return bev_create_ngon(bm, varr, 4, farr, f1, NULL, mat_nr, true);
752 }
753 
755  BMVert *v1,
756  BMVert *v2,
757  BMVert *v3,
758  BMVert *v4,
759  BMFace *f1,
760  BMFace *f2,
761  BMFace *f3,
762  BMFace *f4,
763  BMEdge *e1,
764  BMEdge *e2,
765  BMEdge *e3,
766  BMEdge *e4,
767  BMFace *frep,
768  int mat_nr)
769 {
770  BMVert *varr[4] = {v1, v2, v3, v4};
771  BMFace *farr[4] = {f1, f2, f3, f4};
772  BMEdge *earr[4] = {e1, e2, e3, e4};
773  return bev_create_ngon(bm, varr, 4, farr, frep, earr, mat_nr, true);
774 }
775 
776 /* Is Loop layer layer_index contiguous across shared vertex of l1 and l2? */
777 static bool contig_ldata_across_loops(BMesh *bm, BMLoop *l1, BMLoop *l2, int layer_index)
778 {
779  const int offset = bm->ldata.layers[layer_index].offset;
780  const int type = bm->ldata.layers[layer_index].type;
781 
782  return CustomData_data_equals(
783  type, (char *)l1->head.data + offset, (char *)l2->head.data + offset);
784 }
785 
786 /* Are all loop layers with have math (e.g., UVs)
787  * contiguous from face f1 to face f2 across edge e?
788  */
790 {
791  if (bm->ldata.totlayer == 0) {
792  return true;
793  }
794 
795  BMLoop *lef1, *lef2;
796  if (!BM_edge_loop_pair(e, &lef1, &lef2)) {
797  return false;
798  }
799  /* If faces are oriented consistently around e,
800  * should now have lef1 and lef2 being f1 and f2 in either order.
801  */
802  if (lef1->f == f2) {
803  SWAP(BMLoop *, lef1, lef2);
804  }
805  if (lef1->f != f1 || lef2->f != f2) {
806  return false;
807  }
808  BMVert *v1 = lef1->v;
809  BMVert *v2 = lef2->v;
810  if (v1 == v2) {
811  return false;
812  }
813  BLI_assert((v1 == e->v1 && v2 == e->v2) || (v1 == e->v2 && v2 == e->v1));
815  BMLoop *lv1f1 = lef1;
816  BMLoop *lv2f1 = lef1->next;
817  BMLoop *lv1f2 = lef2->next;
818  BMLoop *lv2f2 = lef2;
819  BLI_assert(lv1f1->v == v1 && lv1f1->f == f1 && lv2f1->v == v2 && lv2f1->f == f1 &&
820  lv1f2->v == v1 && lv1f2->f == f2 && lv2f2->v == v2 && lv2f2->f == f2);
821  for (int i = 0; i < bm->ldata.totlayer; i++) {
822  if (CustomData_layer_has_math(&bm->ldata, i)) {
823  if (!contig_ldata_across_loops(bm, lv1f1, lv1f2, i) ||
824  !contig_ldata_across_loops(bm, lv2f1, lv2f2, i)) {
825  return false;
826  }
827  }
828  }
829  return true;
830 }
831 
832 /*
833  * Set up the fields of bp->math_layer_info.
834  * We always set has_math_layers to the correct value.
835  * Only if there are UV layers and the number of segments is odd,
836  * we need to calculate connected face components in UV space.
837  */
839 {
840  bp->math_layer_info.has_math_layers = false;
842  for (int i = 0; i < bm->ldata.totlayer; i++) {
844  bp->math_layer_info.has_math_layers = true;
845  break;
846  }
847  }
848  if (!bp->math_layer_info.has_math_layers || (bp->seg % 2) == 0) {
849  return;
850  }
851 
854  int totface = bm->totface;
855  int *face_component = BLI_memarena_alloc(bp->mem_arena, sizeof(int) * totface);
856  bp->math_layer_info.face_component = face_component;
857 
858  /* Use an array as a stack. Stack size can't exceed total faces if keep track of what is in
859  * stack. */
860  BMFace **stack = MEM_malloc_arrayN(totface, sizeof(BMFace *), __func__);
861  bool *in_stack = MEM_malloc_arrayN(totface, sizeof(bool), __func__);
862 
863  /* Set all component ids by DFS from faces with unassigned components. */
864  for (int f = 0; f < totface; f++) {
865  face_component[f] = -1;
866  in_stack[f] = false;
867  }
868  int current_component = -1;
869  for (int f = 0; f < totface; f++) {
870  if (face_component[f] == -1 && !in_stack[f]) {
871  int stack_top = 0;
872  current_component++;
873  BLI_assert(stack_top < totface);
874  stack[stack_top] = BM_face_at_index(bm, f);
875  in_stack[f] = true;
876  while (stack_top >= 0) {
877  BMFace *bmf = stack[stack_top];
878  stack_top--;
879  int bmf_index = BM_elem_index_get(bmf);
880  in_stack[bmf_index] = false;
881  if (face_component[bmf_index] != -1) {
882  continue;
883  }
884  face_component[bmf_index] = current_component;
885  /* Neighbors are faces that share an edge with bmf and
886  * are where contig_ldata_across_edge(...) is true for the
887  * shared edge and two faces.
888  */
889  BMIter eiter;
890  BMEdge *bme;
891  BM_ITER_ELEM (bme, &eiter, bmf, BM_EDGES_OF_FACE) {
892  BMIter fiter;
893  BMFace *bmf_other;
894  BM_ITER_ELEM (bmf_other, &fiter, bme, BM_FACES_OF_EDGE) {
895  if (bmf_other != bmf) {
896  int bmf_other_index = BM_elem_index_get(bmf_other);
897  if (face_component[bmf_other_index] != -1 || in_stack[bmf_other_index]) {
898  continue;
899  }
900  if (contig_ldata_across_edge(bm, bme, bmf, bmf_other)) {
901  stack_top++;
902  BLI_assert(stack_top < totface);
903  stack[stack_top] = bmf_other;
904  in_stack[bmf_other_index] = true;
905  }
906  }
907  }
908  }
909  }
910  }
911  }
912  MEM_freeN(stack);
913  MEM_freeN(in_stack);
914 }
915 
928 static BMFace *choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
929 {
930 #define VEC_VALUE_LEN 6
931  float(*value_vecs)[VEC_VALUE_LEN] = NULL;
932  int num_viable = 0;
933 
934  value_vecs = BLI_array_alloca(value_vecs, nfaces);
935  bool *still_viable = BLI_array_alloca(still_viable, nfaces);
936  for (int f = 0; f < nfaces; f++) {
937  BMFace *bmf = face[f];
938  if (bmf == NULL) {
939  still_viable[f] = false;
940  continue;
941  }
942  still_viable[f] = true;
943  num_viable++;
944  int bmf_index = BM_elem_index_get(bmf);
945  int value_index = 0;
946  /* First tie-breaker: lower math-layer connected component id. */
947  value_vecs[f][value_index++] = bp->math_layer_info.face_component ?
948  (float)bp->math_layer_info.face_component[bmf_index] :
949  0.0f;
950  /* Next tie-breaker: selected face beats unselected one. */
951  value_vecs[f][value_index++] = BM_elem_flag_test(bmf, BM_ELEM_SELECT) ? 0.0f : 1.0f;
952  /* Next tie-breaker: lower material index. */
953  value_vecs[f][value_index++] = bmf->mat_nr >= 0 ? (float)bmf->mat_nr : 0.0f;
954  /* Next three tie-breakers: z, x, y components of face center. */
955  float cent[3];
956  BM_face_calc_center_bounds(bmf, cent);
957  value_vecs[f][value_index++] = cent[2];
958  value_vecs[f][value_index++] = cent[0];
959  value_vecs[f][value_index++] = cent[1];
960  BLI_assert(value_index == VEC_VALUE_LEN);
961  }
962 
963  /* Look for a face that has a unique minimum value for in a value_index,
964  * trying each value_index in turn until find a unique minimum.
965  */
966  int best_f = -1;
967  for (int value_index = 0; num_viable > 1 && value_index < VEC_VALUE_LEN; value_index++) {
968  for (int f = 0; f < nfaces; f++) {
969  if (!still_viable[f] || f == best_f) {
970  continue;
971  }
972  if (best_f == -1) {
973  best_f = f;
974  continue;
975  }
976  if (value_vecs[f][value_index] < value_vecs[best_f][value_index]) {
977  best_f = f;
978  /* Previous f's are now not viable any more. */
979  for (int i = f - 1; i >= 0; i--) {
980  if (still_viable[i]) {
981  still_viable[i] = false;
982  num_viable--;
983  }
984  }
985  }
986  else if (value_vecs[f][value_index] > value_vecs[best_f][value_index]) {
987  still_viable[f] = false;
988  num_viable--;
989  }
990  }
991  }
992  if (best_f == -1) {
993  best_f = 0;
994  }
995  return face[best_f];
996 #undef VEC_VALUE_LEN
997 }
998 
999 /* Merge (using average) all the UV values for loops of v's faces.
1000  * Caller should ensure that no seams are violated by doing this. */
1001 static void bev_merge_uvs(BMesh *bm, BMVert *v)
1002 {
1003  int num_of_uv_layers = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
1004 
1005  for (int i = 0; i < num_of_uv_layers; i++) {
1006  int cd_loop_uv_offset = CustomData_get_n_offset(&bm->ldata, CD_MLOOPUV, i);
1007 
1008  if (cd_loop_uv_offset == -1) {
1009  return;
1010  }
1011 
1012  int n = 0;
1013  float uv[2] = {0.0f, 0.0f};
1014  BMIter iter;
1015  BMLoop *l;
1016  BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1017  MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
1018  add_v2_v2(uv, luv->uv);
1019  n++;
1020  }
1021  if (n > 1) {
1022  mul_v2_fl(uv, 1.0f / (float)n);
1023  BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1024  MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
1025  copy_v2_v2(luv->uv, uv);
1026  }
1027  }
1028  }
1029 }
1030 
1031 /* Merge (using average) the UV values for two specific loops of v: those for faces containing v,
1032  * and part of faces that share edge bme. */
1033 static void bev_merge_edge_uvs(BMesh *bm, BMEdge *bme, BMVert *v)
1034 {
1035  int num_of_uv_layers = CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV);
1036 
1037  BMLoop *l1 = NULL;
1038  BMLoop *l2 = NULL;
1039  BMIter iter;
1040  BMLoop *l;
1041  BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1042  if (l->e == bme) {
1043  l1 = l;
1044  }
1045  else if (l->prev->e == bme) {
1046  l2 = l;
1047  }
1048  }
1049  if (l1 == NULL || l2 == NULL) {
1050  return;
1051  }
1052 
1053  for (int i = 0; i < num_of_uv_layers; i++) {
1054  int cd_loop_uv_offset = CustomData_get_n_offset(&bm->ldata, CD_MLOOPUV, i);
1055 
1056  if (cd_loop_uv_offset == -1) {
1057  return;
1058  }
1059 
1060  float uv[2] = {0.0f, 0.0f};
1061  MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l1, cd_loop_uv_offset);
1062  add_v2_v2(uv, luv->uv);
1063  luv = BM_ELEM_CD_GET_VOID_P(l2, cd_loop_uv_offset);
1064  add_v2_v2(uv, luv->uv);
1065  mul_v2_fl(uv, 0.5f);
1066  luv = BM_ELEM_CD_GET_VOID_P(l1, cd_loop_uv_offset);
1067  copy_v2_v2(luv->uv, uv);
1068  luv = BM_ELEM_CD_GET_VOID_P(l2, cd_loop_uv_offset);
1069  copy_v2_v2(luv->uv, uv);
1070  }
1071 }
1072 
1073 /* Calculate coordinates of a point a distance d from v on e->e and return it in slideco. */
1074 static void slide_dist(EdgeHalf *e, BMVert *v, float d, float r_slideco[3])
1075 {
1076  float dir[3];
1077  sub_v3_v3v3(dir, v->co, BM_edge_other_vert(e->e, v)->co);
1078  float len = normalize_v3(dir);
1079 
1080  if (d > len) {
1081  d = len - (float)(50.0 * BEVEL_EPSILON_D);
1082  }
1083  copy_v3_v3(r_slideco, v->co);
1084  madd_v3_v3fl(r_slideco, dir, -d);
1085 }
1086 
1087 /* Is co not on the edge e? If not, return the closer end of e in ret_closer_v. */
1088 static bool is_outside_edge(EdgeHalf *e, const float co[3], BMVert **ret_closer_v)
1089 {
1090  float h[3], u[3];
1091  float *l1 = e->e->v1->co;
1092 
1093  sub_v3_v3v3(u, e->e->v2->co, l1);
1094  sub_v3_v3v3(h, co, l1);
1095  float lenu = normalize_v3(u);
1096  float lambda = dot_v3v3(u, h);
1097  if (lambda <= -BEVEL_EPSILON_BIG * lenu) {
1098  *ret_closer_v = e->e->v1;
1099  return true;
1100  }
1101  if (lambda >= (1.0f + BEVEL_EPSILON_BIG) * lenu) {
1102  *ret_closer_v = e->e->v2;
1103  return true;
1104  }
1105  return false;
1106 }
1107 
1108 /* Return whether the angle is less than, equal to, or larger than 180 degrees. */
1110 {
1111  BMVert *v1 = BM_edge_other_vert(e1->e, v);
1112  BMVert *v2 = BM_edge_other_vert(e2->e, v);
1113  float dir1[3], dir2[3];
1114  sub_v3_v3v3(dir1, v->co, v1->co);
1115  sub_v3_v3v3(dir2, v->co, v2->co);
1116  normalize_v3(dir1);
1117  normalize_v3(dir2);
1118 
1119  /* First check for in-line edges using a simpler test. */
1120  if (nearly_parallel_normalized(dir1, dir2)) {
1121  return ANGLE_STRAIGHT;
1122  }
1123 
1124  /* Angles are in [0,pi]. Need to compare cross product with normal to see if they are reflex. */
1125  float cross[3];
1126  cross_v3_v3v3(cross, dir1, dir2);
1128  float *no;
1129  if (e1->fnext) {
1130  no = e1->fnext->no;
1131  }
1132  else if (e2->fprev) {
1133  no = e2->fprev->no;
1134  }
1135  else {
1136  no = v->no;
1137  }
1138 
1139  if (dot_v3v3(cross, no) < 0.0f) {
1140  return ANGLE_LARGER;
1141  }
1142  return ANGLE_SMALLER;
1143 }
1144 
1145 /* co should be approximately on the plane between e1 and e2, which share common vert v and common
1146  * face f (which cannot be NULL). Is it between those edges, sweeping CCW? */
1148  const float co[3], BMVert *v, BMFace *f, EdgeHalf *e1, EdgeHalf *e2)
1149 {
1150  float dir1[3], dir2[3], dirco[3], no[3];
1151 
1152  BMVert *v1 = BM_edge_other_vert(e1->e, v);
1153  BMVert *v2 = BM_edge_other_vert(e2->e, v);
1154  sub_v3_v3v3(dir1, v->co, v1->co);
1155  sub_v3_v3v3(dir2, v->co, v2->co);
1156  sub_v3_v3v3(dirco, v->co, co);
1157  normalize_v3(dir1);
1158  normalize_v3(dir2);
1159  normalize_v3(dirco);
1160  float ang11 = angle_normalized_v3v3(dir1, dir2);
1161  float ang1co = angle_normalized_v3v3(dir1, dirco);
1162  /* Angles are in [0,pi]. Need to compare cross product with normal to see if they are reflex. */
1163  cross_v3_v3v3(no, dir1, dir2);
1164  if (dot_v3v3(no, f->no) < 0.0f) {
1165  ang11 = (float)(M_PI * 2.0) - ang11;
1166  }
1167  cross_v3_v3v3(no, dir1, dirco);
1168  if (dot_v3v3(no, f->no) < 0.0f) {
1169  ang1co = (float)(M_PI * 2.0) - ang1co;
1170  }
1171  return (ang11 - ang1co > -BEVEL_EPSILON_ANG);
1172 }
1173 
1174 /* Is the angle swept from e1 to e2, CCW when viewed from the normal side of f,
1175  * not a reflex angle or a straight angle? Assume e1 and e2 share a vert. */
1176 static bool edge_edge_angle_less_than_180(const BMEdge *e1, const BMEdge *e2, const BMFace *f)
1177 {
1178  float dir1[3], dir2[3], cross[3];
1179  BLI_assert(f != NULL);
1180  BMVert *v, *v1, *v2;
1181  if (e1->v1 == e2->v1) {
1182  v = e1->v1;
1183  v1 = e1->v2;
1184  v2 = e2->v2;
1185  }
1186  else if (e1->v1 == e2->v2) {
1187  v = e1->v1;
1188  v1 = e1->v2;
1189  v2 = e2->v1;
1190  }
1191  else if (e1->v2 == e2->v1) {
1192  v = e1->v2;
1193  v1 = e1->v1;
1194  v2 = e2->v2;
1195  }
1196  else if (e1->v2 == e2->v2) {
1197  v = e1->v2;
1198  v1 = e1->v1;
1199  v2 = e2->v1;
1200  }
1201  else {
1202  BLI_assert(false);
1203  return false;
1204  }
1205  sub_v3_v3v3(dir1, v1->co, v->co);
1206  sub_v3_v3v3(dir2, v2->co, v->co);
1207  cross_v3_v3v3(cross, dir1, dir2);
1208  return dot_v3v3(cross, f->no) > 0.0f;
1209 }
1210 
1211 /* When the offset_type is BEVEL_AMT_PERCENT or BEVEL_AMT_ABSOLUTE, fill in the coordinates
1212  * of the lines whose intersection defines the boundary point between e1 and e2 with common
1213  * vert v, as defined in the parameters of offset_meet.
1214  */
1216  EdgeHalf *e1,
1217  EdgeHalf *e2,
1218  BMVert *v,
1219  float r_l1a[3],
1220  float r_l1b[3],
1221  float r_l2a[3],
1222  float r_l2b[3])
1223 {
1224  /* Get points the specified distance along each leg.
1225  * Note: not all BevVerts and EdgeHalfs have been made yet, so we have
1226  * to find required edges by moving around faces and use fake EdgeHalfs for
1227  * some of the edges. If there aren't faces to move around, we have to give up.
1228  * The legs we need are:
1229  * e0 : the next edge around e1->fnext (==f1) after e1.
1230  * e3 : the prev edge around e2->fprev (==f2) before e2.
1231  * e4 : the previous edge around f1 before e1 (may be e2).
1232  * e5 : the next edge around f2 after e2 (may be e1).
1233  */
1234  BMVert *v1, *v2;
1235  EdgeHalf e0, e3, e4, e5;
1236  BMFace *f1, *f2;
1237  float d0, d3, d4, d5;
1238  float e1_wt, e2_wt;
1239  v1 = BM_edge_other_vert(e1->e, v);
1240  v2 = BM_edge_other_vert(e2->e, v);
1241  f1 = e1->fnext;
1242  f2 = e2->fprev;
1243  bool no_offsets = f1 == NULL || f2 == NULL;
1244  if (!no_offsets) {
1246  e0.e = l->e;
1247  l = BM_face_vert_share_loop(f2, v2);
1248  e3.e = l->prev->e;
1249  l = BM_face_vert_share_loop(f1, v);
1250  e4.e = l->prev->e;
1251  l = BM_face_vert_share_loop(f2, v);
1252  e5.e = l->e;
1253  /* All the legs must be visible from their opposite legs. */
1254  no_offsets = !edge_edge_angle_less_than_180(e0.e, e1->e, f1) ||
1255  !edge_edge_angle_less_than_180(e1->e, e4.e, f1) ||
1256  !edge_edge_angle_less_than_180(e2->e, e3.e, f2) ||
1257  !edge_edge_angle_less_than_180(e5.e, e2->e, f1);
1258  if (!no_offsets) {
1259  if (bp->offset_type == BEVEL_AMT_ABSOLUTE) {
1260  d0 = d3 = d4 = d5 = bp->offset;
1261  }
1262  else {
1263  d0 = bp->offset * BM_edge_calc_length(e0.e) / 100.0f;
1264  d3 = bp->offset * BM_edge_calc_length(e3.e) / 100.0f;
1265  d4 = bp->offset * BM_edge_calc_length(e4.e) / 100.0f;
1266  d5 = bp->offset * BM_edge_calc_length(e5.e) / 100.0f;
1267  }
1268  if (bp->use_weights) {
1269  CustomData *cd = &bp->bm->edata;
1270  e1_wt = BM_elem_float_data_get(cd, e1->e, CD_BWEIGHT);
1271  e2_wt = BM_elem_float_data_get(cd, e2->e, CD_BWEIGHT);
1272  }
1273  else {
1274  e1_wt = 1.0f;
1275  e2_wt = 1.0f;
1276  }
1277  slide_dist(&e4, v, d4 * e1_wt, r_l1a);
1278  slide_dist(&e0, v1, d0 * e1_wt, r_l1b);
1279  slide_dist(&e5, v, d5 * e2_wt, r_l2a);
1280  slide_dist(&e3, v2, d3 * e2_wt, r_l2b);
1281  }
1282  }
1283  if (no_offsets) {
1284  copy_v3_v3(r_l1a, v->co);
1285  copy_v3_v3(r_l1b, v1->co);
1286  copy_v3_v3(r_l2a, v->co);
1287  copy_v3_v3(r_l2b, v2->co);
1288  }
1289 }
1290 
1309 static void offset_meet(BevelParams *bp,
1310  EdgeHalf *e1,
1311  EdgeHalf *e2,
1312  BMVert *v,
1313  BMFace *f,
1314  bool edges_between,
1315  float meetco[3],
1316  const EdgeHalf *e_in_plane)
1317 {
1318  /* Get direction vectors for two offset lines. */
1319  float dir1[3], dir2[3];
1320  sub_v3_v3v3(dir1, v->co, BM_edge_other_vert(e1->e, v)->co);
1321  sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
1322 
1323  float dir1n[3], dir2p[3];
1324  if (edges_between) {
1325  EdgeHalf *e1next = e1->next;
1326  EdgeHalf *e2prev = e2->prev;
1327  sub_v3_v3v3(dir1n, BM_edge_other_vert(e1next->e, v)->co, v->co);
1328  sub_v3_v3v3(dir2p, v->co, BM_edge_other_vert(e2prev->e, v)->co);
1329  }
1330  else {
1331  /* Shut up 'maybe unused' warnings. */
1332  zero_v3(dir1n);
1333  zero_v3(dir2p);
1334  }
1335 
1336  float ang = angle_v3v3(dir1, dir2);
1337  float norm_perp1[3];
1338  if (ang < BEVEL_EPSILON_ANG) {
1339  /* Special case: e1 and e2 are parallel; put offset point perp to both, from v.
1340  * need to find a suitable plane.
1341  * This code used to just use offset and dir1, but that makes for visible errors
1342  * on a circle with > 200 sides, which trips this "nearly perp" code (see T61214).
1343  * so use the average of the two, and the offset formula for angle bisector.
1344  * If offsets are different, we're out of luck:
1345  * Use the max of the two (so get consistent looking results if the same situation
1346  * arises elsewhere in the object but with opposite roles for e1 and e2. */
1347  float norm_v[3];
1348  if (f) {
1349  copy_v3_v3(norm_v, f->no);
1350  }
1351  else {
1352  /* Get average of face norms of faces between e and e2. */
1353  int fcount = 0;
1354  zero_v3(norm_v);
1355  for (EdgeHalf *eloop = e1; eloop != e2; eloop = eloop->next) {
1356  if (eloop->fnext != NULL) {
1357  add_v3_v3(norm_v, eloop->fnext->no);
1358  fcount++;
1359  }
1360  }
1361  if (fcount == 0) {
1362  copy_v3_v3(norm_v, v->no);
1363  }
1364  else {
1365  mul_v3_fl(norm_v, 1.0f / fcount);
1366  }
1367  }
1368  add_v3_v3(dir1, dir2);
1369  cross_v3_v3v3(norm_perp1, dir1, norm_v);
1370  normalize_v3(norm_perp1);
1371  float off1a[3];
1372  copy_v3_v3(off1a, v->co);
1373  float d = max_ff(e1->offset_r, e2->offset_l);
1374  d = d / cosf(ang / 2.0f);
1375  madd_v3_v3fl(off1a, norm_perp1, d);
1376  copy_v3_v3(meetco, off1a);
1377  }
1378  else if (fabsf(ang - (float)M_PI) < BEVEL_EPSILON_ANG) {
1379  /* Special case: e1 and e2 are antiparallel, so bevel is into a zero-area face.
1380  * Just make the offset point on the common line, at offset distance from v. */
1381  float d = max_ff(e1->offset_r, e2->offset_l);
1382  slide_dist(e2, v, d, meetco);
1383  }
1384  else {
1385  /* Get normal to plane where meet point should be, using cross product instead of f->no
1386  * in case f is non-planar.
1387  * Except: sometimes locally there can be a small angle between dir1 and dir2 that leads
1388  * to a normal that is actually almost perpendicular to the face normal;
1389  * in this case it looks wrong to use the local (cross-product) normal, so use the face normal
1390  * if the angle between dir1 and dir2 is smallish.
1391  * If e1-v-e2 is a reflex angle (viewed from vertex normal side), need to flip.
1392  * Use f->no to figure out which side to look at angle from, as even if f is non-planar,
1393  * will be more accurate than vertex normal. */
1394  float norm_v1[3], norm_v2[3];
1395  if (f && ang < BEVEL_SMALL_ANG) {
1396  copy_v3_v3(norm_v1, f->no);
1397  copy_v3_v3(norm_v2, f->no);
1398  }
1399  else if (!edges_between) {
1400  cross_v3_v3v3(norm_v1, dir2, dir1);
1401  normalize_v3(norm_v1);
1402  if (dot_v3v3(norm_v1, f ? f->no : v->no) < 0.0f) {
1403  negate_v3(norm_v1);
1404  }
1405  copy_v3_v3(norm_v2, norm_v1);
1406  }
1407  else {
1408  /* Separate faces; get face norms at corners for each separately. */
1409  cross_v3_v3v3(norm_v1, dir1n, dir1);
1410  normalize_v3(norm_v1);
1411  f = e1->fnext;
1412  if (dot_v3v3(norm_v1, f ? f->no : v->no) < 0.0f) {
1413  negate_v3(norm_v1);
1414  }
1415  cross_v3_v3v3(norm_v2, dir2, dir2p);
1416  normalize_v3(norm_v2);
1417  f = e2->fprev;
1418  if (dot_v3v3(norm_v2, f ? f->no : v->no) < 0.0f) {
1419  negate_v3(norm_v2);
1420  }
1421  }
1422 
1423  /* Get vectors perp to each edge, perp to norm_v, and pointing into face. */
1424  float norm_perp2[3];
1425  cross_v3_v3v3(norm_perp1, dir1, norm_v1);
1426  cross_v3_v3v3(norm_perp2, dir2, norm_v2);
1427  normalize_v3(norm_perp1);
1428  normalize_v3(norm_perp2);
1429 
1430  float off1a[3], off1b[3], off2a[3], off2b[3];
1432  offset_meet_lines_percent_or_absolute(bp, e1, e2, v, off1a, off1b, off2a, off2b);
1433  }
1434  else {
1435  /* Get points that are offset distances from each line, then another point on each line. */
1436  copy_v3_v3(off1a, v->co);
1437  madd_v3_v3fl(off1a, norm_perp1, e1->offset_r);
1438  add_v3_v3v3(off1b, off1a, dir1);
1439  copy_v3_v3(off2a, v->co);
1440  madd_v3_v3fl(off2a, norm_perp2, e2->offset_l);
1441  add_v3_v3v3(off2b, off2a, dir2);
1442  }
1443 
1444  /* Intersect the offset lines. */
1445  float isect2[3];
1446  int isect_kind = isect_line_line_v3(off1a, off1b, off2a, off2b, meetco, isect2);
1447  if (isect_kind == 0) {
1448  /* Lines are collinear: we already tested for this, but this used a different epsilon. */
1449  copy_v3_v3(meetco, off1a); /* Just to do something. */
1450  }
1451  else {
1452  /* The lines intersect, but is it at a reasonable place?
1453  * One problem to check: if one of the offsets is 0, then we don't want an intersection
1454  * that is outside that edge itself. This can happen if angle between them is > 180 degrees,
1455  * or if the offset amount is > the edge length. */
1456  BMVert *closer_v;
1457  if (e1->offset_r == 0.0f && is_outside_edge(e1, meetco, &closer_v)) {
1458  copy_v3_v3(meetco, closer_v->co);
1459  }
1460  if (e2->offset_l == 0.0f && is_outside_edge(e2, meetco, &closer_v)) {
1461  copy_v3_v3(meetco, closer_v->co);
1462  }
1463  if (edges_between && e1->offset_r > 0.0f && e2->offset_l > 0.0f) {
1464  /* Try to drop meetco to a face between e1 and e2. */
1465  if (isect_kind == 2) {
1466  /* Lines didn't meet in 3d: get average of meetco and isect2. */
1467  mid_v3_v3v3(meetco, meetco, isect2);
1468  }
1469  for (EdgeHalf *e = e1; e != e2; e = e->next) {
1470  BMFace *fnext = e->fnext;
1471  if (!fnext) {
1472  continue;
1473  }
1474  float plane[4];
1475  plane_from_point_normal_v3(plane, v->co, fnext->no);
1476  float dropco[3];
1477  closest_to_plane_normalized_v3(dropco, plane, meetco);
1478  /* Don't drop to the faces next to the in plane edge. */
1479  if (e_in_plane) {
1480  ang = angle_v3v3(fnext->no, e_in_plane->fnext->no);
1481  if ((fabsf(ang) < BEVEL_SMALL_ANG) || (fabsf(ang - (float)M_PI) < BEVEL_SMALL_ANG)) {
1482  continue;
1483  }
1484  }
1485  if (point_between_edges(dropco, v, fnext, e, e->next)) {
1486  copy_v3_v3(meetco, dropco);
1487  break;
1488  }
1489  }
1490  }
1491  }
1492  }
1493 }
1494 
1495 /* Chosen so 1/sin(BEVEL_GOOD_ANGLE) is about 4, giving that expansion factor to bevel width. */
1496 #define BEVEL_GOOD_ANGLE 0.25f
1497 
1506 static bool offset_meet_edge(
1507  EdgeHalf *e1, EdgeHalf *e2, BMVert *v, float meetco[3], float *r_angle)
1508 {
1509  float dir1[3], dir2[3];
1510  sub_v3_v3v3(dir1, BM_edge_other_vert(e1->e, v)->co, v->co);
1511  sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
1512  normalize_v3(dir1);
1513  normalize_v3(dir2);
1514 
1515  /* Find angle from dir1 to dir2 as viewed from vertex normal side. */
1516  float ang = angle_normalized_v3v3(dir1, dir2);
1517  if (fabsf(ang) < BEVEL_GOOD_ANGLE) {
1518  if (r_angle) {
1519  *r_angle = 0.0f;
1520  }
1521  return false;
1522  }
1523  float fno[3];
1524  cross_v3_v3v3(fno, dir1, dir2);
1525  if (dot_v3v3(fno, v->no) < 0.0f) {
1526  ang = 2.0f * (float)M_PI - ang; /* Angle is reflex. */
1527  if (r_angle) {
1528  *r_angle = ang;
1529  }
1530  return false;
1531  }
1532  if (r_angle) {
1533  *r_angle = ang;
1534  }
1535 
1536  if (fabsf(ang - (float)M_PI) < BEVEL_GOOD_ANGLE) {
1537  return false;
1538  }
1539 
1540  float sinang = sinf(ang);
1541 
1542  copy_v3_v3(meetco, v->co);
1543  if (e1->offset_r == 0.0f) {
1544  madd_v3_v3fl(meetco, dir1, e2->offset_l / sinang);
1545  }
1546  else {
1547  madd_v3_v3fl(meetco, dir2, e1->offset_r / sinang);
1548  }
1549  return true;
1550 }
1551 
1557 {
1558  float ang;
1559  float meet[3];
1560 
1561  return offset_meet_edge(e1, emid, v, meet, &ang) && offset_meet_edge(emid, e2, v, meet, &ang);
1562 }
1563 
1573  EdgeHalf *e1,
1574  EdgeHalf *e2,
1575  EdgeHalf *emid,
1576  BMVert *v,
1577  float meetco[3],
1578  float *r_sinratio)
1579 {
1580  bool retval = false;
1581 
1582  BLI_assert(e1->is_bev && e2->is_bev && !emid->is_bev);
1583 
1584  float ang1, ang2;
1585  float meet1[3], meet2[3];
1586  bool ok1 = offset_meet_edge(e1, emid, v, meet1, &ang1);
1587  bool ok2 = offset_meet_edge(emid, e2, v, meet2, &ang2);
1589  BMVert *v2 = BM_edge_other_vert(emid->e, v);
1590  if (bp->offset_type == BEVEL_AMT_PERCENT) {
1591  float wt = 1.0;
1592  if (bp->use_weights) {
1593  CustomData *cd = &bp->bm->edata;
1594  wt = 0.5f * (BM_elem_float_data_get(cd, e1->e, CD_BWEIGHT) +
1595  BM_elem_float_data_get(cd, e2->e, CD_BWEIGHT));
1596  }
1597  interp_v3_v3v3(meetco, v->co, v2->co, wt * bp->offset / 100.0f);
1598  }
1599  else {
1600  float dir[3];
1601  sub_v3_v3v3(dir, v2->co, v->co);
1602  normalize_v3(dir);
1603  madd_v3_v3v3fl(meetco, v->co, dir, bp->offset);
1604  }
1605  if (r_sinratio) {
1606  *r_sinratio = (ang1 == 0.0f) ? 1.0f : sinf(ang2) / sinf(ang1);
1607  }
1608  return true;
1609  }
1610  if (ok1 && ok2) {
1611  mid_v3_v3v3(meetco, meet1, meet2);
1612  if (r_sinratio) {
1613  /* ang1 should not be 0, but be paranoid. */
1614  *r_sinratio = (ang1 == 0.0f) ? 1.0f : sinf(ang2) / sinf(ang1);
1615  }
1616  retval = true;
1617  }
1618  else if (ok1 && !ok2) {
1619  copy_v3_v3(meetco, meet1);
1620  }
1621  else if (!ok1 && ok2) {
1622  copy_v3_v3(meetco, meet2);
1623  }
1624  else {
1625  /* Neither offset line met emid.
1626  * This should only happen if all three lines are on top of each other. */
1627  slide_dist(emid, v, e1->offset_r, meetco);
1628  }
1629 
1630  return retval;
1631 }
1632 
1633 /* Offset by e->offset in plane with normal plane_no, on left if left==true, else on right.
1634  * If plane_no is NULL, choose an arbitrary plane different from eh's direction. */
1635 static void offset_in_plane(EdgeHalf *e, const float plane_no[3], bool left, float r_co[3])
1636 {
1637  BMVert *v = e->is_rev ? e->e->v2 : e->e->v1;
1638 
1639  float dir[3], no[3];
1640  sub_v3_v3v3(dir, BM_edge_other_vert(e->e, v)->co, v->co);
1641  normalize_v3(dir);
1642  if (plane_no) {
1643  copy_v3_v3(no, plane_no);
1644  }
1645  else {
1646  zero_v3(no);
1647  if (fabsf(dir[0]) < fabsf(dir[1])) {
1648  no[0] = 1.0f;
1649  }
1650  else {
1651  no[1] = 1.0f;
1652  }
1653  }
1654 
1655  float fdir[3];
1656  if (left) {
1657  cross_v3_v3v3(fdir, dir, no);
1658  }
1659  else {
1660  cross_v3_v3v3(fdir, no, dir);
1661  }
1662  normalize_v3(fdir);
1663  copy_v3_v3(r_co, v->co);
1664  madd_v3_v3fl(r_co, fdir, left ? e->offset_l : e->offset_r);
1665 }
1666 
1667 /* Calculate the point on e where line (co_a, co_b) comes closest to and return it in projco. */
1668 static void project_to_edge(const BMEdge *e,
1669  const float co_a[3],
1670  const float co_b[3],
1671  float projco[3])
1672 {
1673  float otherco[3];
1674  if (!isect_line_line_v3(e->v1->co, e->v2->co, co_a, co_b, projco, otherco)) {
1675 #ifdef BEVEL_ASSERT_PROJECT
1676  BLI_assert(!"project meet failure");
1677 #endif
1678  copy_v3_v3(projco, e->v1->co);
1679  }
1680 }
1681 
1682 /* If there is a bndv->ebev edge, find the mid control point if necessary.
1683  * It is the closest point on the beveled edge to the line segment between bndv and bndv->next. */
1684 static void set_profile_params(BevelParams *bp, BevVert *bv, BoundVert *bndv)
1685 {
1686  bool do_linear_interp = true;
1687  EdgeHalf *e = bndv->ebev;
1688  Profile *pro = &bndv->profile;
1689 
1690  float start[3], end[3];
1691  copy_v3_v3(start, bndv->nv.co);
1692  copy_v3_v3(end, bndv->next->nv.co);
1693  if (e) {
1694  do_linear_interp = false;
1695  pro->super_r = bp->pro_super_r;
1696  /* Projection direction is direction of the edge. */
1697  sub_v3_v3v3(pro->proj_dir, e->e->v1->co, e->e->v2->co);
1698  if (e->is_rev) {
1699  negate_v3(pro->proj_dir);
1700  }
1701  normalize_v3(pro->proj_dir);
1702  project_to_edge(e->e, start, end, pro->middle);
1703  copy_v3_v3(pro->start, start);
1704  copy_v3_v3(pro->end, end);
1705  /* Default plane to project onto is the one with triangle start - middle - end in it. */
1706  float d1[3], d2[3];
1707  sub_v3_v3v3(d1, pro->middle, start);
1708  sub_v3_v3v3(d2, pro->middle, end);
1709  normalize_v3(d1);
1710  normalize_v3(d2);
1711  cross_v3_v3v3(pro->plane_no, d1, d2);
1712  normalize_v3(pro->plane_no);
1713  if (nearly_parallel(d1, d2)) {
1714  /* Start - middle - end are collinear.
1715  * It should be the case that beveled edge is coplanar with two boundary verts.
1716  * We want to move the profile to that common plane, if possible.
1717  * That makes the multi-segment bevels curve nicely in that plane, as users expect.
1718  * The new middle should be either v (when neighbor edges are unbeveled)
1719  * or the intersection of the offset lines (if they are).
1720  * If the profile is going to lead into unbeveled edges on each side
1721  * (that is, both BoundVerts are "on-edge" points on non-beveled edges). */
1722  copy_v3_v3(pro->middle, bv->v->co);
1723  if (e->prev->is_bev && e->next->is_bev && bv->selcount >= 3) {
1724  /* Want mid at the meet point of next and prev offset edges. */
1725  float d3[3], d4[3], co4[3], meetco[3], isect2[3];
1726  int isect_kind;
1727 
1728  sub_v3_v3v3(d3, e->prev->e->v1->co, e->prev->e->v2->co);
1729  sub_v3_v3v3(d4, e->next->e->v1->co, e->next->e->v2->co);
1730  normalize_v3(d3);
1731  normalize_v3(d4);
1732  if (nearly_parallel(d3, d4)) {
1733  /* Offset lines are collinear - want linear interpolation. */
1734  mid_v3_v3v3(pro->middle, start, end);
1735  do_linear_interp = true;
1736  }
1737  else {
1738  float co3[3];
1739  add_v3_v3v3(co3, start, d3);
1740  add_v3_v3v3(co4, end, d4);
1741  isect_kind = isect_line_line_v3(start, co3, end, co4, meetco, isect2);
1742  if (isect_kind != 0) {
1743  copy_v3_v3(pro->middle, meetco);
1744  }
1745  else {
1746  /* Offset lines don't intersect - want linear interpolation. */
1747  mid_v3_v3v3(pro->middle, start, end);
1748  do_linear_interp = true;
1749  }
1750  }
1751  }
1752  copy_v3_v3(pro->end, end);
1753  sub_v3_v3v3(d1, pro->middle, start);
1754  normalize_v3(d1);
1755  sub_v3_v3v3(d2, pro->middle, end);
1756  normalize_v3(d2);
1757  cross_v3_v3v3(pro->plane_no, d1, d2);
1758  normalize_v3(pro->plane_no);
1759  if (nearly_parallel(d1, d2)) {
1760  /* Whole profile is collinear with edge: just interpolate. */
1761  do_linear_interp = true;
1762  }
1763  else {
1764  copy_v3_v3(pro->plane_co, bv->v->co);
1765  copy_v3_v3(pro->proj_dir, pro->plane_no);
1766  }
1767  }
1768  copy_v3_v3(pro->plane_co, start);
1769  }
1770  else if (bndv->is_arc_start) {
1771  /* Assume pro->middle was already set. */
1772  copy_v3_v3(pro->start, start);
1773  copy_v3_v3(pro->end, end);
1774  pro->super_r = PRO_CIRCLE_R;
1775  zero_v3(pro->plane_co);
1776  zero_v3(pro->plane_no);
1777  zero_v3(pro->proj_dir);
1778  do_linear_interp = false;
1779  }
1780  else if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
1781  copy_v3_v3(pro->start, start);
1782  copy_v3_v3(pro->middle, bv->v->co);
1783  copy_v3_v3(pro->end, end);
1784  pro->super_r = bp->pro_super_r;
1785  zero_v3(pro->plane_co);
1786  zero_v3(pro->plane_no);
1787  zero_v3(pro->proj_dir);
1788  do_linear_interp = false;
1789  }
1790 
1791  if (do_linear_interp) {
1792  pro->super_r = PRO_LINE_R;
1793  copy_v3_v3(pro->start, start);
1794  copy_v3_v3(pro->end, end);
1795  mid_v3_v3v3(pro->middle, start, end);
1796  /* Won't use projection for this line profile. */
1797  zero_v3(pro->plane_co);
1798  zero_v3(pro->plane_no);
1799  zero_v3(pro->proj_dir);
1800  }
1801 }
1802 
1810 static void move_profile_plane(BoundVert *bndv, BMVert *bmvert)
1811 {
1812  Profile *pro = &bndv->profile;
1813 
1814  /* Only do this if projecting, and start, end, and proj_dir are not coplanar. */
1815  if (is_zero_v3(pro->proj_dir)) {
1816  return;
1817  }
1818 
1819  float d1[3], d2[3];
1820  sub_v3_v3v3(d1, bmvert->co, pro->start);
1821  normalize_v3(d1);
1822  sub_v3_v3v3(d2, bmvert->co, pro->end);
1823  normalize_v3(d2);
1824  float no[3], no2[3], no3[3];
1825  cross_v3_v3v3(no, d1, d2);
1826  cross_v3_v3v3(no2, d1, pro->proj_dir);
1827  cross_v3_v3v3(no3, d2, pro->proj_dir);
1828 
1831  float dot2 = dot_v3v3(no, no2);
1832  float dot3 = dot_v3v3(no, no3);
1833  if (fabsf(dot2) < (1 - BEVEL_EPSILON_BIG) && fabsf(dot3) < (1 - BEVEL_EPSILON_BIG)) {
1834  copy_v3_v3(bndv->profile.plane_no, no);
1835  }
1836  }
1837 
1838  /* We've changed the parameters from their defaults, so don't recalculate them later. */
1839  pro->special_params = true;
1840 }
1841 
1849 static void move_weld_profile_planes(BevVert *bv, BoundVert *bndv1, BoundVert *bndv2)
1850 {
1851  /* Only do this if projecting, and d1, d2, and proj_dir are not coplanar. */
1852  if (is_zero_v3(bndv1->profile.proj_dir) || is_zero_v3(bndv2->profile.proj_dir)) {
1853  return;
1854  }
1855  float d1[3], d2[3], no[3];
1856  sub_v3_v3v3(d1, bv->v->co, bndv1->nv.co);
1857  sub_v3_v3v3(d2, bv->v->co, bndv2->nv.co);
1858  cross_v3_v3v3(no, d1, d2);
1859  float l1 = normalize_v3(no);
1860 
1861  /* "no" is new normal projection plane, but don't move if it is coplanar with both of the
1862  * projection dirs. */
1863  float no2[3], no3[3];
1864  cross_v3_v3v3(no2, d1, bndv1->profile.proj_dir);
1865  float l2 = normalize_v3(no2);
1866  cross_v3_v3v3(no3, d2, bndv2->profile.proj_dir);
1867  float l3 = normalize_v3(no3);
1868  if (l1 > BEVEL_EPSILON && (l2 > BEVEL_EPSILON || l3 > BEVEL_EPSILON)) {
1869  float dot1 = fabsf(dot_v3v3(no, no2));
1870  float dot2 = fabsf(dot_v3v3(no, no3));
1871  if (fabsf(dot1 - 1.0f) > BEVEL_EPSILON) {
1872  copy_v3_v3(bndv1->profile.plane_no, no);
1873  }
1874  if (fabsf(dot2 - 1.0f) > BEVEL_EPSILON) {
1875  copy_v3_v3(bndv2->profile.plane_no, no);
1876  }
1877  }
1878 
1879  /* We've changed the parameters from their defaults, so don't recalculate them later. */
1880  bndv1->profile.special_params = true;
1881  bndv2->profile.special_params = true;
1882 }
1883 
1884 /* Return 1 if a and b are in CCW order on the normal side of f,
1885  * and -1 if they are reversed, and 0 if there is no shared face f. */
1886 static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
1887 {
1888  if (!f) {
1889  return 0;
1890  }
1891  BMLoop *la = BM_face_edge_share_loop(f, a);
1892  BMLoop *lb = BM_face_edge_share_loop(f, b);
1893  if (!la || !lb) {
1894  return 0;
1895  }
1896  return lb->next == la ? 1 : -1;
1897 }
1898 
1920 static bool make_unit_square_map(const float va[3],
1921  const float vmid[3],
1922  const float vb[3],
1923  float r_mat[4][4])
1924 {
1925  float vb_vmid[3], va_vmid[3];
1926  sub_v3_v3v3(va_vmid, vmid, va);
1927  sub_v3_v3v3(vb_vmid, vmid, vb);
1928 
1929  if (is_zero_v3(va_vmid) || is_zero_v3(vb_vmid)) {
1930  return false;
1931  }
1932 
1933  if (fabsf(angle_v3v3(va_vmid, vb_vmid) - (float)M_PI) <= BEVEL_EPSILON_ANG) {
1934  return false;
1935  }
1936 
1937  float vo[3], vd[3], vddir[3];
1938  sub_v3_v3v3(vo, va, vb_vmid);
1939  cross_v3_v3v3(vddir, vb_vmid, va_vmid);
1940  normalize_v3(vddir);
1941  add_v3_v3v3(vd, vo, vddir);
1942 
1943  /* The cols of m are: {vmid - va, vmid - vb, vmid + vd - va -vb, va + vb - vmid;
1944  * Blender transform matrices are stored such that m[i][*] is ith column;
1945  * the last elements of each col remain as they are in unity matrix. */
1946  sub_v3_v3v3(&r_mat[0][0], vmid, va);
1947  r_mat[0][3] = 0.0f;
1948  sub_v3_v3v3(&r_mat[1][0], vmid, vb);
1949  r_mat[1][3] = 0.0f;
1950  add_v3_v3v3(&r_mat[2][0], vmid, vd);
1951  sub_v3_v3(&r_mat[2][0], va);
1952  sub_v3_v3(&r_mat[2][0], vb);
1953  r_mat[2][3] = 0.0f;
1954  add_v3_v3v3(&r_mat[3][0], va, vb);
1955  sub_v3_v3(&r_mat[3][0], vmid);
1956  r_mat[3][3] = 1.0f;
1957 
1958  return true;
1959 }
1960 
1978  const float va[3], const float vb[3], const float vc[3], const float vd[3], float r_mat[4][4])
1979 {
1980  copy_v3_v3(r_mat[0], va);
1981  sub_v3_v3(r_mat[0], vb);
1982  sub_v3_v3(r_mat[0], vc);
1983  add_v3_v3(r_mat[0], vd);
1984  mul_v3_fl(r_mat[0], 0.5f);
1985  r_mat[0][3] = 0.0f;
1986  copy_v3_v3(r_mat[1], vb);
1987  sub_v3_v3(r_mat[1], va);
1988  sub_v3_v3(r_mat[1], vc);
1989  add_v3_v3(r_mat[1], vd);
1990  mul_v3_fl(r_mat[1], 0.5f);
1991  r_mat[1][3] = 0.0f;
1992  copy_v3_v3(r_mat[2], vc);
1993  sub_v3_v3(r_mat[2], va);
1994  sub_v3_v3(r_mat[2], vb);
1995  add_v3_v3(r_mat[2], vd);
1996  mul_v3_fl(r_mat[2], 0.5f);
1997  r_mat[2][3] = 0.0f;
1998  copy_v3_v3(r_mat[3], va);
1999  add_v3_v3(r_mat[3], vb);
2000  add_v3_v3(r_mat[3], vc);
2001  sub_v3_v3(r_mat[3], vd);
2002  mul_v3_fl(r_mat[3], 0.5f);
2003  r_mat[3][3] = 1.0f;
2004 }
2005 
2012 static double superellipse_co(double x, float r, bool rbig)
2013 {
2014  BLI_assert(r > 0.0f);
2015 
2016  /* If r<1, mirror the superellipse function by (y=x)-line to get a numerically stable range
2017  * Possible because of symmetry, later mirror back. */
2018  if (rbig) {
2019  return pow((1.0 - pow(x, r)), (1.0 / r));
2020  }
2021  return 1.0 - pow((1.0 - pow(1.0 - x, r)), (1.0 / r));
2022 }
2023 
2033 static void get_profile_point(BevelParams *bp, const Profile *pro, int i, int nseg, float r_co[3])
2034 {
2035  if (bp->seg == 1) {
2036  if (i == 0) {
2037  copy_v3_v3(r_co, pro->start);
2038  }
2039  else {
2040  copy_v3_v3(r_co, pro->end);
2041  }
2042  }
2043 
2044  else {
2045  if (nseg == bp->seg) {
2046  BLI_assert(pro->prof_co != NULL);
2047  copy_v3_v3(r_co, pro->prof_co + 3 * i);
2048  }
2049  else {
2050  BLI_assert(is_power_of_2_i(nseg) && nseg <= bp->pro_spacing.seg_2);
2051  /* Find spacing between subsamples in prof_co_2. */
2052  int subsample_spacing = bp->pro_spacing.seg_2 / nseg;
2053  copy_v3_v3(r_co, pro->prof_co_2 + 3 * i * subsample_spacing);
2054  }
2055  }
2056 }
2057 
2062 static void calculate_profile_segments(const Profile *profile,
2063  const float map[4][4],
2064  const bool use_map,
2065  const bool reversed,
2066  const int ns,
2067  const double *xvals,
2068  const double *yvals,
2069  float *r_prof_co)
2070 {
2071  /* Iterate over the vertices along the boundary arc. */
2072  for (int k = 0; k <= ns; k++) {
2073  float co[3];
2074  if (k == 0) {
2075  copy_v3_v3(co, profile->start);
2076  }
2077  else if (k == ns) {
2078  copy_v3_v3(co, profile->end);
2079  }
2080  else {
2081  if (use_map) {
2082  const float p[3] = {
2083  reversed ? (float)yvals[ns - k] : (float)xvals[k],
2084  reversed ? (float)xvals[ns - k] : (float)yvals[k],
2085  0.0f,
2086  };
2087  /* Do the 2D->3D transformation of the profile coordinates. */
2088  mul_v3_m4v3(co, map, p);
2089  }
2090  else {
2091  interp_v3_v3v3(co, profile->start, profile->end, (float)k / (float)ns);
2092  }
2093  }
2094  /* Finish the 2D->3D transformation by projecting onto the final profile plane. */
2095  float *prof_co_k = r_prof_co + 3 * k;
2096  if (!is_zero_v3(profile->proj_dir)) {
2097  float co2[3];
2098  add_v3_v3v3(co2, co, profile->proj_dir);
2099  /* pro->plane_co and pro->plane_no are filled in #set_profile_params. */
2100  if (!isect_line_plane_v3(prof_co_k, co, co2, profile->plane_co, profile->plane_no)) {
2101  /* Shouldn't happen. */
2102  copy_v3_v3(prof_co_k, co);
2103  }
2104  }
2105  else {
2106  copy_v3_v3(prof_co_k, co);
2107  }
2108  }
2109 }
2110 
2119 static void calculate_profile(BevelParams *bp, BoundVert *bndv, bool reversed, bool miter)
2120 {
2121  Profile *pro = &bndv->profile;
2122  ProfileSpacing *pro_spacing = (miter) ? &bp->pro_spacing_miter : &bp->pro_spacing;
2123 
2124  if (bp->seg == 1) {
2125  return;
2126  }
2127 
2128  bool need_2 = bp->seg != bp->pro_spacing.seg_2;
2129  if (pro->prof_co == NULL) {
2130  pro->prof_co = (float *)BLI_memarena_alloc(bp->mem_arena, sizeof(float[3]) * (bp->seg + 1));
2131  if (need_2) {
2132  pro->prof_co_2 = (float *)BLI_memarena_alloc(bp->mem_arena,
2133  sizeof(float[3]) * (bp->pro_spacing.seg_2 + 1));
2134  }
2135  else {
2136  pro->prof_co_2 = pro->prof_co;
2137  }
2138  }
2139 
2140  bool use_map;
2141  float map[4][4];
2143  use_map = false;
2144  }
2145  else {
2146  use_map = make_unit_square_map(pro->start, pro->middle, pro->end, map);
2147  }
2148 
2149  if (bp->vmesh_method == BEVEL_VMESH_CUTOFF && use_map) {
2150  /* Calculate the "height" of the profile by putting the (0,0) and (1,1) corners of the
2151  * un-transformed profile through the 2D->3D map and calculating the distance between them. */
2152  float bottom_corner[3] = {0.0f, 0.0f, 0.0f};
2153  mul_v3_m4v3(bottom_corner, map, bottom_corner);
2154  float top_corner[3] = {1.0f, 1.0f, 0.0f};
2155  mul_v3_m4v3(top_corner, map, top_corner);
2156 
2157  pro->height = len_v3v3(bottom_corner, top_corner);
2158  }
2159 
2160  /* Calculate the 3D locations for the profile points */
2162  pro, map, use_map, reversed, bp->seg, pro_spacing->xvals, pro_spacing->yvals, pro->prof_co);
2163  /* Also calculate for the is the seg_2 case if it's needed .*/
2164  if (need_2) {
2166  map,
2167  use_map,
2168  reversed,
2169  bp->pro_spacing.seg_2,
2170  pro_spacing->xvals_2,
2171  pro_spacing->yvals_2,
2172  pro->prof_co_2);
2173  }
2174 }
2175 
2182 static void snap_to_superellipsoid(float co[3], const float super_r, bool midline)
2183 {
2184  float r = super_r;
2185  if (r == PRO_CIRCLE_R) {
2186  normalize_v3(co);
2187  return;
2188  }
2189 
2190  float a = max_ff(0.0f, co[0]);
2191  float b = max_ff(0.0f, co[1]);
2192  float c = max_ff(0.0f, co[2]);
2193  float x = a;
2194  float y = b;
2195  float z = c;
2197  /* Will only be called for 2d profile. */
2199  z = 0.0f;
2200  x = min_ff(1.0f, x);
2201  y = min_ff(1.0f, y);
2202  if (r == PRO_SQUARE_R) {
2203  /* Snap to closer of x==1 and y==1 lines, or maybe both. */
2204  float dx = 1.0f - x;
2205  float dy = 1.0f - y;
2206  if (dx < dy) {
2207  x = 1.0f;
2208  y = midline ? 1.0f : y;
2209  }
2210  else {
2211  y = 1.0f;
2212  x = midline ? 1.0f : x;
2213  }
2214  }
2215  else {
2216  /* Snap to closer of x==0 and y==0 lines, or maybe both. */
2217  if (x < y) {
2218  x = 0.0f;
2219  y = midline ? 0.0f : y;
2220  }
2221  else {
2222  y = 0.0f;
2223  x = midline ? 0.0f : x;
2224  }
2225  }
2226  }
2227  else {
2228  float rinv = 1.0f / r;
2229  if (a == 0.0f) {
2230  if (b == 0.0f) {
2231  x = 0.0f;
2232  y = 0.0f;
2233  z = powf(c, rinv);
2234  }
2235  else {
2236  x = 0.0f;
2237  y = powf(1.0f / (1.0f + powf(c / b, r)), rinv);
2238  z = c * y / b;
2239  }
2240  }
2241  else {
2242  x = powf(1.0f / (1.0f + powf(b / a, r) + powf(c / a, r)), rinv);
2243  y = b * x / a;
2244  z = c * x / a;
2245  }
2246  }
2247  co[0] = x;
2248  co[1] = y;
2249  co[2] = z;
2250 }
2251 
2252 #define BEV_EXTEND_EDGE_DATA_CHECK(eh, flag) (BM_elem_flag_test(eh->e, flag))
2253 
2254 static void check_edge_data_seam_sharp_edges(BevVert *bv, int flag, bool neg)
2255 {
2256  EdgeHalf *e = &bv->edges[0], *efirst = &bv->edges[0];
2257 
2258  /* First first edge with seam or sharp edge data. */
2259  while ((!neg && !BEV_EXTEND_EDGE_DATA_CHECK(e, flag)) ||
2260  (neg && BEV_EXTEND_EDGE_DATA_CHECK(e, flag))) {
2261  e = e->next;
2262  if (e == efirst) {
2263  break;
2264  }
2265  }
2266 
2267  /* If no such edge found, return. */
2268  if ((!neg && !BEV_EXTEND_EDGE_DATA_CHECK(e, flag)) ||
2269  (neg && BEV_EXTEND_EDGE_DATA_CHECK(e, flag))) {
2270  return;
2271  }
2272 
2273  /* Set efirst to this first encountered edge. */
2274  efirst = e;
2275 
2276  do {
2277  int flag_count = 0;
2278  EdgeHalf *ne = e->next;
2279 
2280  while (((!neg && !BEV_EXTEND_EDGE_DATA_CHECK(ne, flag)) ||
2281  (neg && BEV_EXTEND_EDGE_DATA_CHECK(ne, flag))) &&
2282  ne != efirst) {
2283  if (ne->is_bev) {
2284  flag_count++;
2285  }
2286  ne = ne->next;
2287  }
2288  if (ne == e || (ne == efirst && ((!neg && !BEV_EXTEND_EDGE_DATA_CHECK(efirst, flag)) ||
2289  (neg && BEV_EXTEND_EDGE_DATA_CHECK(efirst, flag))))) {
2290  break;
2291  }
2292  /* Set seam_len / sharp_len of starting edge. */
2293  if (flag == BM_ELEM_SEAM) {
2294  e->rightv->seam_len = flag_count;
2295  }
2296  else if (flag == BM_ELEM_SMOOTH) {
2297  e->rightv->sharp_len = flag_count;
2298  }
2299  e = ne;
2300  } while (e != efirst);
2301 }
2302 
2304 {
2305  VMesh *vm = bv->vmesh;
2306 
2307  if (vm->mesh_kind == M_TRI_FAN) {
2308  return;
2309  }
2310 
2311  BoundVert *bcur = bv->vmesh->boundstart, *start = bcur;
2312 
2313  do {
2314  /* If current boundvert has a seam length > 0 then it has a seam running along its edges. */
2315  if (bcur->seam_len) {
2316  if (!bv->vmesh->boundstart->seam_len && start == bv->vmesh->boundstart) {
2317  start = bcur; /* Set start to first boundvert with seam_len > 0. */
2318  }
2319 
2320  /* Now for all the mesh_verts starting at current index and ending at idxlen
2321  * we go through outermost ring and through all its segments and add seams
2322  * for those edges. */
2323  int idxlen = bcur->index + bcur->seam_len;
2324  for (int i = bcur->index; i < idxlen; i++) {
2325  BMVert *v1 = mesh_vert(vm, i % vm->count, 0, 0)->v, *v2;
2326  BMEdge *e;
2327  for (int k = 1; k < vm->seg; k++) {
2328  v2 = mesh_vert(vm, i % vm->count, 0, k)->v;
2329 
2330  /* Here v1 & v2 are current and next BMverts,
2331  * we find common edge and set its edge data. */
2332  e = v1->e;
2333  while (e->v1 != v2 && e->v2 != v2) {
2334  if (e->v1 == v1) {
2335  e = e->v1_disk_link.next;
2336  }
2337  else {
2338  e = e->v2_disk_link.next;
2339  }
2340  }
2342  v1 = v2;
2343  }
2344  BMVert *v3 = mesh_vert(vm, (i + 1) % vm->count, 0, 0)->v;
2345  e = v1->e; /* Do same as above for first and last vert. */
2346  while (e->v1 != v3 && e->v2 != v3) {
2347  if (e->v1 == v1) {
2348  e = e->v1_disk_link.next;
2349  }
2350  else {
2351  e = e->v2_disk_link.next;
2352  }
2353  }
2355  bcur = bcur->next;
2356  }
2357  }
2358  else {
2359  bcur = bcur->next;
2360  }
2361  } while (bcur != start);
2362 
2363  bcur = bv->vmesh->boundstart;
2364  start = bcur;
2365  do {
2366  if (bcur->sharp_len) {
2367  if (!bv->vmesh->boundstart->sharp_len && start == bv->vmesh->boundstart) {
2368  start = bcur;
2369  }
2370 
2371  int idxlen = bcur->index + bcur->sharp_len;
2372  for (int i = bcur->index; i < idxlen; i++) {
2373  BMVert *v1 = mesh_vert(vm, i % vm->count, 0, 0)->v, *v2;
2374  BMEdge *e;
2375  for (int k = 1; k < vm->seg; k++) {
2376  v2 = mesh_vert(vm, i % vm->count, 0, k)->v;
2377 
2378  e = v1->e;
2379  while (e->v1 != v2 && e->v2 != v2) {
2380  if (e->v1 == v1) {
2381  e = e->v1_disk_link.next;
2382  }
2383  else {
2384  e = e->v2_disk_link.next;
2385  }
2386  }
2388  v1 = v2;
2389  }
2390  BMVert *v3 = mesh_vert(vm, (i + 1) % vm->count, 0, 0)->v;
2391  e = v1->e;
2392  while (e->v1 != v3 && e->v2 != v3) {
2393  if (e->v1 == v1) {
2394  e = e->v1_disk_link.next;
2395  }
2396  else {
2397  e = e->v2_disk_link.next;
2398  }
2399  }
2401  bcur = bcur->next;
2402  }
2403  }
2404  else {
2405  bcur = bcur->next;
2406  }
2407  } while (bcur != start);
2408 }
2409 
2410 /* Mark edges as sharp if they are between a smooth reconstructed face and a new face. */
2412 {
2413  BMIter fiter;
2414  BMFace *f;
2415  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2416  if (!BM_elem_flag_test(f, BM_ELEM_SMOOTH)) {
2417  continue;
2418  }
2419  if (get_face_kind(bp, f) != F_RECON) {
2420  continue;
2421  }
2422  BMIter liter;
2423  BMLoop *l;
2424  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2425  /* Cases we care about will have exactly one adjacent face. */
2426  BMLoop *lother = l->radial_next;
2427  BMFace *fother = lother->f;
2428  if (lother != l && fother) {
2429  FKind fkind = get_face_kind(bp, lother->f);
2430  if (ELEM(fkind, F_EDGE, F_VERT)) {
2432  }
2433  }
2434  }
2435  }
2436 }
2437 
2447 {
2448  if (bp->offset == 0.0 || !bp->harden_normals) {
2449  return;
2450  }
2451 
2452  /* Recalculate all face and vertex normals. Side effect: ensures vertex, edge, face indices. */
2453  /* I suspect this is not necessary. TODO: test that guess. */
2455 
2456  int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
2457 
2458  /* If there is not already a custom split normal layer then making one (with BM_lnorspace_update)
2459  * will not respect the autosmooth angle between smooth faces. To get that to happen, we have
2460  * to mark the sharpen the edges that are only sharp because of the angle test -- otherwise would
2461  * be smooth. */
2462  if (cd_clnors_offset == -1) {
2465  }
2466 
2467  /* Ensure that bm->lnor_spacearr has properly stored loop normals.
2468  * Side effect: ensures loop indices. */
2470 
2471  if (cd_clnors_offset == -1) {
2472  cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL);
2473  }
2474 
2475  BMIter fiter;
2476  BMFace *f;
2477  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2478  FKind fkind = get_face_kind(bp, f);
2479  if (ELEM(fkind, F_ORIG, F_RECON)) {
2480  continue;
2481  }
2482  BMIter liter;
2483  BMLoop *l;
2484  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
2485  BMEdge *estep = l->prev->e; /* Causes CW walk around l->v fan. */
2486  BMLoop *lprev = BM_vert_step_fan_loop(l, &estep);
2487  estep = l->e; /* Causes CCW walk around l->v fan. */
2488  BMLoop *lnext = BM_vert_step_fan_loop(l, &estep);
2489  FKind fprevkind = lprev ? get_face_kind(bp, lprev->f) : F_NONE;
2490  FKind fnextkind = lnext ? get_face_kind(bp, lnext->f) : F_NONE;
2491 
2492  float norm[3];
2493  float *pnorm = NULL;
2494  if (fkind == F_EDGE) {
2495  if (fprevkind == F_EDGE && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
2496  add_v3_v3v3(norm, f->no, lprev->f->no);
2497  pnorm = norm;
2498  }
2499  else if (fnextkind == F_EDGE && BM_elem_flag_test(lnext, BM_ELEM_LONG_TAG)) {
2500  add_v3_v3v3(norm, f->no, lnext->f->no);
2501  pnorm = norm;
2502  }
2503  else if (fprevkind == F_RECON && BM_elem_flag_test(l, BM_ELEM_LONG_TAG)) {
2504  pnorm = lprev->f->no;
2505  }
2506  else if (fnextkind == F_RECON && BM_elem_flag_test(l->prev, BM_ELEM_LONG_TAG)) {
2507  pnorm = lnext->f->no;
2508  }
2509  else {
2510  /* printf("unexpected harden case (edge)\n"); */
2511  }
2512  }
2513  else if (fkind == F_VERT) {
2514  if (fprevkind == F_VERT && fnextkind == F_VERT) {
2515  pnorm = l->v->no;
2516  }
2517  else if (fprevkind == F_RECON) {
2518  pnorm = lprev->f->no;
2519  }
2520  else if (fnextkind == F_RECON) {
2521  pnorm = lnext->f->no;
2522  }
2523  else {
2524  BMLoop *lprevprev, *lnextnext;
2525  if (lprev) {
2526  estep = lprev->prev->e;
2527  lprevprev = BM_vert_step_fan_loop(lprev, &estep);
2528  }
2529  else {
2530  lprevprev = NULL;
2531  }
2532  if (lnext) {
2533  estep = lnext->e;
2534  lnextnext = BM_vert_step_fan_loop(lnext, &estep);
2535  }
2536  else {
2537  lnextnext = NULL;
2538  }
2539  FKind fprevprevkind = lprevprev ? get_face_kind(bp, lprevprev->f) : F_NONE;
2540  FKind fnextnextkind = lnextnext ? get_face_kind(bp, lnextnext->f) : F_NONE;
2541  if (fprevkind == F_EDGE && fprevprevkind == F_RECON) {
2542  pnorm = lprevprev->f->no;
2543  }
2544  else if (fprevkind == F_EDGE && fnextkind == F_VERT && fprevprevkind == F_EDGE) {
2545  add_v3_v3v3(norm, lprev->f->no, lprevprev->f->no);
2546  pnorm = norm;
2547  }
2548  else if (fnextkind == F_EDGE && fprevkind == F_VERT && fnextnextkind == F_EDGE) {
2549  add_v3_v3v3(norm, lnext->f->no, lnextnext->f->no);
2550  pnorm = norm;
2551  }
2552  else {
2553  /* printf("unexpected harden case (vert)\n"); */
2554  }
2555  }
2556  }
2557  if (pnorm) {
2558  if (pnorm == norm) {
2559  normalize_v3(norm);
2560  }
2561  int l_index = BM_elem_index_get(l);
2562  short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset);
2564  }
2565  }
2566  }
2567 }
2568 
2570 {
2571  const int mode = bp->face_strength_mode;
2572  const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID;
2573  int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, wn_layer_id);
2574 
2575  if (cd_prop_int_idx == -1) {
2576  BM_data_layer_add_named(bm, &bm->pdata, CD_PROP_INT32, wn_layer_id);
2577  cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT32, wn_layer_id);
2578  }
2579  cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT32);
2580  const int cd_prop_int_offset = CustomData_get_n_offset(
2581  &bm->pdata, CD_PROP_INT32, cd_prop_int_idx);
2582 
2583  BMIter fiter;
2584  BMFace *f;
2585  BM_ITER_MESH (f, &fiter, bm, BM_FACES_OF_MESH) {
2586  FKind fkind = get_face_kind(bp, f);
2587  bool do_set_strength = true;
2588  int strength;
2589  switch (fkind) {
2590  case F_VERT:
2591  strength = FACE_STRENGTH_WEAK;
2592  do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
2593  break;
2594  case F_EDGE:
2595  strength = FACE_STRENGTH_MEDIUM;
2596  do_set_strength = (mode >= BEVEL_FACE_STRENGTH_NEW);
2597  break;
2598  case F_RECON:
2599  strength = FACE_STRENGTH_STRONG;
2600  do_set_strength = (mode >= BEVEL_FACE_STRENGTH_AFFECTED);
2601  break;
2602  case F_ORIG:
2603  strength = FACE_STRENGTH_STRONG;
2604  do_set_strength = (mode == BEVEL_FACE_STRENGTH_ALL);
2605  break;
2606  default:
2607  do_set_strength = false;
2608  }
2609  if (do_set_strength) {
2610  int *strength_ptr = BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset);
2611  *strength_ptr = strength;
2612  }
2613  }
2614 }
2615 
2616 /* Set the any_seam property for a BevVert and all its BoundVerts. */
2617 static void set_bound_vert_seams(BevVert *bv, bool mark_seam, bool mark_sharp)
2618 {
2619  bv->any_seam = false;
2620  BoundVert *v = bv->vmesh->boundstart;
2621  do {
2622  v->any_seam = false;
2623  for (EdgeHalf *e = v->efirst; e; e = e->next) {
2624  v->any_seam |= e->is_seam;
2625  if (e == v->elast) {
2626  break;
2627  }
2628  }
2629  bv->any_seam |= v->any_seam;
2630  } while ((v = v->next) != bv->vmesh->boundstart);
2631 
2632  if (mark_seam) {
2634  }
2635  if (mark_sharp) {
2637  }
2638 }
2639 
2641 {
2642  if (!bv->any_seam) {
2643  return 0;
2644  }
2645 
2646  int ans = 0;
2647  for (int i = 0; i < bv->edgecount; i++) {
2648  if (bv->edges[i].is_seam) {
2649  ans++;
2650  }
2651  }
2652  return ans;
2653 }
2654 
2655 /* Is e between two faces with a 180 degree angle between their normals? */
2656 static bool eh_on_plane(EdgeHalf *e)
2657 {
2658  if (e->fprev && e->fnext) {
2659  float dot = dot_v3v3(e->fprev->no, e->fnext->no);
2660  if (fabsf(dot + 1.0f) <= BEVEL_EPSILON_BIG || fabsf(dot - 1.0f) <= BEVEL_EPSILON_BIG) {
2661  return true;
2662  }
2663  }
2664  return false;
2665 }
2666 
2674 {
2675  BoundVert *bndv = vm->boundstart;
2676  do {
2677  /* In special cases the params will have already been set. */
2678  if (!bndv->profile.special_params) {
2679  set_profile_params(bp, bv, bndv);
2680  }
2681  bool miter_profile = false;
2682  bool reverse_profile = false;
2683  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
2684  /* Use the miter profile spacing struct if the default is filled with the custom profile. */
2685  miter_profile = (bndv->is_arc_start || bndv->is_patch_start);
2686  /* Don't bother reversing the profile if it's a miter profile */
2687  reverse_profile = !bndv->is_profile_start && !miter_profile;
2688  }
2689  calculate_profile(bp, bndv, reverse_profile, miter_profile);
2690  } while ((bndv = bndv->next) != vm->boundstart);
2691 }
2692 
2693 /* Implements build_boundary for the vertex-only case. */
2694 static void build_boundary_vertex_only(BevelParams *bp, BevVert *bv, bool construct)
2695 {
2696  VMesh *vm = bv->vmesh;
2697 
2699 
2700  EdgeHalf *efirst = &bv->edges[0];
2701  EdgeHalf *e = efirst;
2702  do {
2703  float co[3];
2704  slide_dist(e, bv->v, e->offset_l, co);
2705  if (construct) {
2706  BoundVert *v = add_new_bound_vert(bp->mem_arena, vm, co);
2707  v->efirst = v->elast = e;
2708  e->leftv = e->rightv = v;
2709  }
2710  else {
2711  adjust_bound_vert(e->leftv, co);
2712  }
2713  } while ((e = e->next) != efirst);
2714 
2715  if (construct) {
2717  if (vm->count == 2) {
2718  vm->mesh_kind = M_NONE;
2719  }
2720  else if (bp->seg == 1) {
2721  vm->mesh_kind = M_POLY;
2722  }
2723  else {
2724  vm->mesh_kind = M_ADJ;
2725  }
2726  }
2727 }
2728 
2735  BevVert *bv,
2736  EdgeHalf *efirst,
2737  const bool construct)
2738 {
2739  MemArena *mem_arena = bp->mem_arena;
2740  VMesh *vm = bv->vmesh;
2741 
2742  EdgeHalf *e = efirst;
2743  float co[3];
2744  if (bv->edgecount == 2) {
2745  /* Only 2 edges in, so terminate the edge with an artificial vertex on the unbeveled edge.
2746  * If the offset type is BEVEL_AMT_PERCENT or BEVEL_AMT_ABSOLUTE, what to do is a bit
2747  * undefined (there aren't two "legs"), so just let the code do what it does. */
2748  const float *no = e->fprev ? e->fprev->no : (e->fnext ? e->fnext->no : NULL);
2749  offset_in_plane(e, no, true, co);
2750  if (construct) {
2751  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2752  bndv->efirst = bndv->elast = bndv->ebev = e;
2753  e->leftv = bndv;
2754  }
2755  else {
2756  adjust_bound_vert(e->leftv, co);
2757  }
2758  no = e->fnext ? e->fnext->no : (e->fprev ? e->fprev->no : NULL);
2759  offset_in_plane(e, no, false, co);
2760  if (construct) {
2761  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2762  bndv->efirst = bndv->elast = e;
2763  e->rightv = bndv;
2764  }
2765  else {
2766  adjust_bound_vert(e->rightv, co);
2767  }
2768  /* Make artificial extra point along unbeveled edge, and form triangle. */
2769  slide_dist(e->next, bv->v, e->offset_l, co);
2770  if (construct) {
2771  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2772  bndv->efirst = bndv->elast = e->next;
2773  e->next->leftv = e->next->rightv = bndv;
2775  }
2776  else {
2777  adjust_bound_vert(e->next->leftv, co);
2778  }
2779  }
2780  else {
2781  /* More than 2 edges in. Put on-edge verts on all the other edges and join with the beveled
2782  * edge to make a poly or adj mesh, because e->prev has offset 0, offset_meet will put co on
2783  * that edge. */
2784  /* TODO: should do something else if angle between e and e->prev > 180 */
2785  bool leg_slide = bp->offset_type == BEVEL_AMT_PERCENT || bp->offset_type == BEVEL_AMT_ABSOLUTE;
2786  if (leg_slide) {
2787  slide_dist(e->prev, bv->v, e->offset_l, co);
2788  }
2789  else {
2790  offset_meet(bp, e->prev, e, bv->v, e->fprev, false, co, NULL);
2791  }
2792  if (construct) {
2793  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2794  bndv->efirst = e->prev;
2795  bndv->elast = bndv->ebev = e;
2796  e->leftv = bndv;
2797  e->prev->leftv = e->prev->rightv = bndv;
2798  }
2799  else {
2800  adjust_bound_vert(e->leftv, co);
2801  }
2802  e = e->next;
2803  if (leg_slide) {
2804  slide_dist(e, bv->v, e->prev->offset_r, co);
2805  }
2806  else {
2807  offset_meet(bp, e->prev, e, bv->v, e->fprev, false, co, NULL);
2808  }
2809  if (construct) {
2810  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2811  bndv->efirst = e->prev;
2812  bndv->elast = e;
2813  e->leftv = e->rightv = bndv;
2814  e->prev->rightv = bndv;
2815  }
2816  else {
2817  adjust_bound_vert(e->leftv, co);
2818  }
2819  /* For the edges not adjacent to the beveled edge, slide the bevel amount along. */
2820  float d = efirst->offset_l_spec;
2821  if (bp->profile_type == BEVEL_PROFILE_CUSTOM || bp->profile < 0.25f) {
2822  d *= sqrtf(2.0f); /* Need to go further along the edge to make room for full profile area. */
2823  }
2824  for (e = e->next; e->next != efirst; e = e->next) {
2825  slide_dist(e, bv->v, d, co);
2826  if (construct) {
2827  BoundVert *bndv = add_new_bound_vert(mem_arena, vm, co);
2828  bndv->efirst = bndv->elast = e;
2829  e->leftv = e->rightv = bndv;
2830  }
2831  else {
2832  adjust_bound_vert(e->leftv, co);
2833  }
2834  }
2835  }
2836 
2837  if (bv->edgecount >= 3) {
2838  /* Special case: snap profile to plane of adjacent two edges. */
2839  BoundVert *bndv = vm->boundstart;
2840  BLI_assert(bndv->ebev != NULL);
2841  set_profile_params(bp, bv, bndv);
2842  move_profile_plane(bndv, bv->v);
2843  }
2844 
2845  if (construct) {
2847 
2848  if (vm->count == 2 && bv->edgecount == 3) {
2849  vm->mesh_kind = M_NONE;
2850  }
2851  else if (vm->count == 3) {
2852  bool use_tri_fan = true;
2853  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
2854  /* Prevent overhanging edges: use M_POLY if the extra point is planar with the profile. */
2855  BoundVert *bndv = efirst->leftv;
2856  float profile_plane[4];
2857  plane_from_point_normal_v3(profile_plane, bndv->profile.plane_co, bndv->profile.plane_no);
2858  bndv = efirst->rightv->next; /* The added boundvert placed along the non-adjacent edge. */
2859  if (dist_squared_to_plane_v3(bndv->nv.co, profile_plane) < BEVEL_EPSILON_BIG) {
2860  use_tri_fan = false;
2861  }
2862  }
2863  vm->mesh_kind = (use_tri_fan) ? M_TRI_FAN : M_POLY;
2864  }
2865  else {
2866  vm->mesh_kind = M_POLY;
2867  }
2868  }
2869 }
2870 
2871 /* Helper for build_boundary to handle special miters. */
2872 static void adjust_miter_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
2873 {
2874  int miter_outer = bp->miter_outer;
2875 
2876  BoundVert *v1 = emiter->rightv;
2877  BoundVert *v2, *v3;
2878  if (miter_outer == BEVEL_MITER_PATCH) {
2879  v2 = v1->next;
2880  v3 = v2->next;
2881  }
2882  else {
2883  BLI_assert(miter_outer == BEVEL_MITER_ARC);
2884  v2 = NULL;
2885  v3 = v1->next;
2886  }
2887  BoundVert *v1prev = v1->prev;
2888  BoundVert *v3next = v3->next;
2889  float co2[3];
2890  copy_v3_v3(co2, v1->nv.co);
2891  if (v1->is_arc_start) {
2892  copy_v3_v3(v1->profile.middle, co2);
2893  }
2894 
2895  /* co1 is intersection of line through co2 in dir of emiter->e
2896  * and plane with normal the dir of emiter->e and through v1prev. */
2897  float co1[3], edge_dir[3], line_p[3];
2898  BMVert *vother = BM_edge_other_vert(emiter->e, bv->v);
2899  sub_v3_v3v3(edge_dir, bv->v->co, vother->co);
2900  normalize_v3(edge_dir);
2901  float d = bp->offset / (bp->seg / 2.0f); /* A fallback amount to move. */
2902  madd_v3_v3v3fl(line_p, co2, edge_dir, d);
2903  if (!isect_line_plane_v3(co1, co2, line_p, v1prev->nv.co, edge_dir)) {
2904  copy_v3_v3(co1, line_p);
2905  }
2906  adjust_bound_vert(v1, co1);
2907 
2908  /* co3 is similar, but plane is through v3next and line is other side of miter edge. */
2909  float co3[3];
2910  EdgeHalf *emiter_other = v3->elast;
2911  vother = BM_edge_other_vert(emiter_other->e, bv->v);
2912  sub_v3_v3v3(edge_dir, bv->v->co, vother->co);
2913  normalize_v3(edge_dir);
2914  madd_v3_v3v3fl(line_p, co2, edge_dir, d);
2915  if (!isect_line_plane_v3(co3, co2, line_p, v3next->nv.co, edge_dir)) {
2916  copy_v3_v3(co1, line_p);
2917  }
2918  adjust_bound_vert(v3, co3);
2919 }
2920 
2922 {
2923  BoundVert *vstart = bv->vmesh->boundstart;
2924  BoundVert *v = vstart;
2925  do {
2926  if (v->is_arc_start) {
2927  BoundVert *v3 = v->next;
2928  EdgeHalf *e = v->efirst;
2929  if (e != emiter) {
2930  float edge_dir[3], co[3];
2931  copy_v3_v3(co, v->nv.co);
2932  BMVert *vother = BM_edge_other_vert(e->e, bv->v);
2933  sub_v3_v3v3(edge_dir, vother->co, bv->v->co);
2934  normalize_v3(edge_dir);
2935  madd_v3_v3v3fl(v->nv.co, co, edge_dir, bp->spread);
2936  e = v3->elast;
2937  vother = BM_edge_other_vert(e->e, bv->v);
2938  sub_v3_v3v3(edge_dir, vother->co, bv->v->co);
2939  normalize_v3(edge_dir);
2940  madd_v3_v3v3fl(v3->nv.co, co, edge_dir, bp->spread);
2941  }
2942  v = v3->next;
2943  }
2944  else {
2945  v = v->next;
2946  }
2947  } while (v != vstart);
2948 }
2949 
2964 static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
2965 {
2966  MemArena *mem_arena = bp->mem_arena;
2967 
2968  /* Current bevel does nothing if only one edge into a vertex. */
2969  if (bv->edgecount <= 1) {
2970  return;
2971  }
2972 
2973  if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
2974  build_boundary_vertex_only(bp, bv, construct);
2975  return;
2976  }
2977 
2978  VMesh *vm = bv->vmesh;
2979 
2980  /* Find a beveled edge to be efirst. */
2981  EdgeHalf *efirst = next_bev(bv, NULL);
2982  BLI_assert(efirst->is_bev);
2983 
2984  if (bv->selcount == 1) {
2985  /* Special case: only one beveled edge in. */
2986  build_boundary_terminal_edge(bp, bv, efirst, construct);
2987  return;
2988  }
2989 
2990  /* Special miters outside only for 3 or more beveled edges. */
2991  int miter_outer = (bv->selcount >= 3) ? bp->miter_outer : BEVEL_MITER_SHARP;
2992  int miter_inner = bp->miter_inner;
2993 
2994  /* Keep track of the first beveled edge of an outside miter (there can be at most 1 per bv). */
2995  EdgeHalf *emiter = NULL;
2996 
2997  /* There is more than one beveled edge.
2998  * We make BoundVerts to connect the sides of the beveled edges.
2999  * Non-beveled edges in between will just join to the appropriate juncture point. */
3000  EdgeHalf *e = efirst;
3001  do {
3002  BLI_assert(e->is_bev);
3003  EdgeHalf *eon = NULL;
3004  /* Make the BoundVert for the right side of e; the other side will be made when the beveled
3005  * edge to the left of e is handled.
3006  * Analyze edges until next beveled edge: They are either "in plane" (preceding and subsequent
3007  * faces are coplanar) or not. The "non-in-plane" edges affect the silhouette and we prefer to
3008  * slide along one of those if possible. */
3009  int in_plane = 0; /* Counts of in-plane / not-in-plane. */
3010  int not_in_plane = 0;
3011  EdgeHalf *enip = NULL; /* Representatives of each type. */
3012  EdgeHalf *eip = NULL;
3013  EdgeHalf *e2;
3014  for (e2 = e->next; !e2->is_bev; e2 = e2->next) {
3015  if (eh_on_plane(e2)) {
3016  in_plane++;
3017  eip = e2;
3018  }
3019  else {
3020  not_in_plane++;
3021  enip = e2;
3022  }
3023  }
3024 
3025  float r, co[3];
3026  if (in_plane == 0 && not_in_plane == 0) {
3027  offset_meet(bp, e, e2, bv->v, e->fnext, false, co, NULL);
3028  }
3029  else if (not_in_plane > 0) {
3030  if (bp->loop_slide && not_in_plane == 1 && good_offset_on_edge_between(e, e2, enip, bv->v)) {
3031  if (offset_on_edge_between(bp, e, e2, enip, bv->v, co, &r)) {
3032  eon = enip;
3033  }
3034  }
3035  else {
3036  offset_meet(bp, e, e2, bv->v, NULL, true, co, eip);
3037  }
3038  }
3039  else {
3040  /* n_in_plane > 0 and n_not_in_plane == 0. */
3041  if (bp->loop_slide && in_plane == 1 && good_offset_on_edge_between(e, e2, eip, bv->v)) {
3042  if (offset_on_edge_between(bp, e, e2, eip, bv->v, co, &r)) {
3043  eon = eip;
3044  }
3045  }
3046  else {
3047  offset_meet(bp, e, e2, bv->v, e->fnext, true, co, eip);
3048  }
3049  }
3050 
3051  if (construct) {
3053  v->efirst = e;
3054  v->elast = e2;
3055  v->ebev = e2;
3056  v->eon = eon;
3057  if (eon) {
3058  v->sinratio = r;
3059  }
3060  e->rightv = v;
3061  e2->leftv = v;
3062  for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3063  e3->leftv = e3->rightv = v;
3064  }
3065  AngleKind ang_kind = edges_angle_kind(e, e2, bv->v);
3066 
3067  /* Are we doing special mitering?
3068  * There can only be one outer reflex angle, so only one outer miter,
3069  * and emiter will be set to the first edge of such an edge.
3070  * A miter kind of BEVEL_MITER_SHARP means no special miter */
3071  if ((miter_outer != BEVEL_MITER_SHARP && !emiter && ang_kind == ANGLE_LARGER) ||
3072  (miter_inner != BEVEL_MITER_SHARP && ang_kind == ANGLE_SMALLER)) {
3073  if (ang_kind == ANGLE_LARGER) {
3074  emiter = e;
3075  }
3076  /* Make one or two more boundverts; for now all will have same co. */
3077  BoundVert *v1 = v;
3078  v1->ebev = NULL;
3079  BoundVert *v2;
3080  if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3081  v2 = add_new_bound_vert(mem_arena, vm, co);
3082  }
3083  else {
3084  v2 = NULL;
3085  }
3086  BoundVert *v3 = add_new_bound_vert(mem_arena, vm, co);
3087  v3->ebev = e2;
3088  v3->efirst = e2;
3089  v3->elast = e2;
3090  v3->eon = NULL;
3091  e2->leftv = v3;
3092  if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3093  v1->is_patch_start = true;
3094  v2->eon = v1->eon;
3095  v2->sinratio = v1->sinratio;
3096  v2->ebev = NULL;
3097  v1->eon = NULL;
3098  v1->sinratio = 1.0f;
3099  v1->elast = e;
3100  if (e->next == e2) {
3101  v2->efirst = NULL;
3102  v2->elast = NULL;
3103  }
3104  else {
3105  v2->efirst = e->next;
3106  for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3107  e3->leftv = e3->rightv = v2;
3108  v2->elast = e3;
3109  }
3110  }
3111  }
3112  else {
3113  v1->is_arc_start = true;
3114  copy_v3_v3(v1->profile.middle, co);
3115  if (e->next == e2) {
3116  v1->elast = v1->efirst;
3117  }
3118  else {
3119  int between = in_plane + not_in_plane;
3120  int bet2 = between / 2;
3121  bool betodd = (between % 2) == 1;
3122  int i = 0;
3123  /* Put first half of in-between edges at index 0, second half at index bp->seg.
3124  * If between is odd, put middle one at mid-index. */
3125  for (EdgeHalf *e3 = e->next; e3 != e2; e3 = e3->next) {
3126  v1->elast = e3;
3127  if (i < bet2) {
3128  e3->profile_index = 0;
3129  }
3130  else if (betodd && i == bet2) {
3131  e3->profile_index = bp->seg / 2;
3132  }
3133  else {
3134  e3->profile_index = bp->seg;
3135  }
3136  i++;
3137  }
3138  }
3139  }
3140  }
3141  }
3142  else { /* construct == false. */
3143  AngleKind ang_kind = edges_angle_kind(e, e2, bv->v);
3144  if ((miter_outer != BEVEL_MITER_SHARP && !emiter && ang_kind == ANGLE_LARGER) ||
3145  (miter_inner != BEVEL_MITER_SHARP && ang_kind == ANGLE_SMALLER)) {
3146  if (ang_kind == ANGLE_LARGER) {
3147  emiter = e;
3148  }
3149  BoundVert *v1 = e->rightv;
3150  BoundVert *v2;
3151  BoundVert *v3;
3152  if (ang_kind == ANGLE_LARGER && miter_outer == BEVEL_MITER_PATCH) {
3153  v2 = v1->next;
3154  v3 = v2->next;
3155  }
3156  else {
3157  v2 = NULL;
3158  v3 = v1->next;
3159  }
3160  adjust_bound_vert(v1, co);
3161  if (v2) {
3162  adjust_bound_vert(v2, co);
3163  }
3164  adjust_bound_vert(v3, co);
3165  }
3166  else {
3167  adjust_bound_vert(e->rightv, co);
3168  }
3169  }
3170  e = e2;
3171  } while (e != efirst);
3172 
3173  if (miter_inner != BEVEL_MITER_SHARP) {
3174  adjust_miter_inner_coords(bp, bv, emiter);
3175  }
3176  if (emiter) {
3177  adjust_miter_coords(bp, bv, emiter);
3178  }
3179 
3180  if (construct) {
3181  set_bound_vert_seams(bv, bp->mark_seam, bp->mark_sharp);
3182 
3183  if (vm->count == 2) {
3184  vm->mesh_kind = M_NONE;
3185  }
3186  else if (efirst->seg == 1) {
3187  vm->mesh_kind = M_POLY;
3188  }
3189  else {
3190  switch (bp->vmesh_method) {
3191  case BEVEL_VMESH_ADJ:
3192  vm->mesh_kind = M_ADJ;
3193  break;
3194  case BEVEL_VMESH_CUTOFF:
3195  vm->mesh_kind = M_CUTOFF;
3196  break;
3197  }
3198  }
3199  }
3200 }
3201 
3202 #ifdef DEBUG_ADJUST
3203 static void print_adjust_stats(BoundVert *vstart)
3204 {
3205  printf("\nSolution analysis\n");
3206  double even_residual2 = 0.0;
3207  double spec_residual2 = 0.0;
3208  double max_even_r = 0.0;
3209  double max_even_r_pct = 0.0;
3210  double max_spec_r = 0.0;
3211  double max_spec_r_pct = 0.0;
3212  printf("width matching\n");
3213  BoundVert *v = vstart;
3214  do {
3215  if (v->adjchain != NULL) {
3216  EdgeHalf *eright = v->efirst;
3217  EdgeHalf *eleft = v->adjchain->elast;
3218  double delta = fabs(eright->offset_r - eleft->offset_l);
3219  double delta_pct = 100.0 * delta / eright->offset_r_spec;
3220  printf("e%d r(%f) vs l(%f): abs(delta)=%f, delta_pct=%f\n",
3221  BM_elem_index_get(eright->e),
3222  eright->offset_r,
3223  eleft->offset_l,
3224  delta,
3225  delta_pct);
3226  even_residual2 += delta * delta;
3227  if (delta > max_even_r) {
3228  max_even_r = delta;
3229  }
3230  if (delta_pct > max_even_r_pct) {
3231  max_even_r_pct = delta_pct;
3232  }
3233  }
3234  v = v->adjchain;
3235  } while (v && v != vstart);
3236 
3237  printf("spec matching\n");
3238  v = vstart;
3239  do {
3240  if (v->adjchain != NULL) {
3241  EdgeHalf *eright = v->efirst;
3242  EdgeHalf *eleft = v->adjchain->elast;
3243  double delta = eright->offset_r - eright->offset_r_spec;
3244  double delta_pct = 100.0 * delta / eright->offset_r_spec;
3245  printf("e%d r(%f) vs r spec(%f): delta=%f, delta_pct=%f\n",
3246  BM_elem_index_get(eright->e),
3247  eright->offset_r,
3248  eright->offset_r_spec,
3249  delta,
3250  delta_pct);
3251  spec_residual2 += delta * delta;
3252  delta = fabs(delta);
3253  delta_pct = fabs(delta_pct);
3254  if (delta > max_spec_r) {
3255  max_spec_r = delta;
3256  }
3257  if (delta_pct > max_spec_r_pct) {
3258  max_spec_r_pct = delta_pct;
3259  }
3260 
3261  delta = eleft->offset_l - eleft->offset_l_spec;
3262  delta_pct = 100.0 * delta / eright->offset_l_spec;
3263  printf("e%d l(%f) vs l spec(%f): delta=%f, delta_pct=%f\n",
3264  BM_elem_index_get(eright->e),
3265  eleft->offset_l,
3266  eleft->offset_l_spec,
3267  delta,
3268  delta_pct);
3269  spec_residual2 += delta * delta;
3270  delta = fabs(delta);
3271  delta_pct = fabs(delta_pct);
3272  if (delta > max_spec_r) {
3273  max_spec_r = delta;
3274  }
3275  if (delta_pct > max_spec_r_pct) {
3276  max_spec_r_pct = delta_pct;
3277  }
3278  }
3279  v = v->adjchain;
3280  } while (v && v != vstart);
3281 
3282  printf("Analysis Result:\n");
3283  printf("even residual2 = %f, spec residual2 = %f\n", even_residual2, spec_residual2);
3284  printf("max even delta = %f, max as percent of spec = %f\n", max_even_r, max_even_r_pct);
3285  printf("max spec delta = %f, max as percent of spec = %f\n", max_spec_r, max_spec_r_pct);
3286 }
3287 #endif
3288 
3289 #ifdef FAST_ADJUST_CODE
3290 /* This code uses a direct solution to the adjustment problem for chains and certain cycles.
3291  * It is a two-step approach: first solve for the exact solution of the 'match widths' constraints
3292  * using the one degree of freedom that allows for expressing all other widths in terms of that.
3293  * And then minimize the spec-matching constraints using the derivative of the least squares
3294  * residual in terms of that one degree of freedom.
3295  * Unfortunately, the results are in some cases worse than the general least squares solution
3296  * for the combined (with weights) problem, so this code is not used.
3297  * But keep it here for a while in case performance issues demand that it be used sometimes. */
3298 static bool adjust_the_cycle_or_chain_fast(BoundVert *vstart, int np, bool iscycle)
3299 {
3300  float *g = MEM_mallocN(np * sizeof(float), "beveladjust");
3301  float *g_prod = MEM_mallocN(np * sizeof(float), "beveladjust");
3302 
3303  BoundVert *v = vstart;
3304  float spec_sum = 0.0f;
3305  int i = 0;
3306  do {
3307  g[i] = v->sinratio;
3308  if (iscycle || v->adjchain != NULL) {
3309  spec_sum += v->efirst->offset_r;
3310  }
3311  else {
3312  spec_sum += v->elast->offset_l;
3313  }
3314  i++;
3315  v = v->adjchain;
3316  } while (v && v != vstart);
3317 
3318  float gprod = 1.00f;
3319  float gprod_sum = 1.0f;
3320  for (i = np - 1; i > 0; i--) {
3321  gprod *= g[i];
3322  g_prod[i] = gprod;
3323  gprod_sum += gprod;
3324  }
3325  g_prod[0] = 1.0f;
3326  if (iscycle) {
3327  gprod *= g[0];
3328  if (fabs(gprod - 1.0f) > BEVEL_EPSILON) {
3329  /* Fast cycle calc only works if total product is 1. */
3330  MEM_freeN(g);
3331  MEM_freeN(g_prod);
3332  return false;
3333  }
3334  }
3335  if (gprod_sum == 0.0f) {
3336  MEM_freeN(g);
3337  MEM_freeN(g_prod);
3338  return false;
3339  }
3340  float p = spec_sum / gprod_sum;
3341 
3342  /* Apply the new offsets. */
3343  v = vstart;
3344  i = 0;
3345  do {
3346  if (iscycle || v->adjchain != NULL) {
3347  EdgeHalf *eright = v->efirst;
3348  EdgeHalf *eleft = v->elast;
3349  eright->offset_r = g_prod[(i + 1) % np] * p;
3350  if (iscycle || v != vstart) {
3351  eleft->offset_l = v->sinratio * eright->offset_r;
3352  }
3353  }
3354  else {
3355  /* Not a cycle, and last of chain. */
3356  EdgeHalf *eleft = v->elast;
3357  eleft->offset_l = p;
3358  }
3359  i++;
3360  v = v->adjchain;
3361  } while (v && v != vstart);
3362 
3363  MEM_freeN(g);
3364  MEM_freeN(g_prod);
3365  return true;
3366 }
3367 #endif
3368 
3382  EdgeHalf *start_edge,
3383  bool toward_bv,
3384  BevVert **r_bv)
3385 {
3386  /* Case 1: The next EdgeHalf is the other side of the BMEdge.
3387  * It's part of the same BMEdge, so we know the other EdgeHalf is also beveled. */
3388  if (!toward_bv) {
3389  return find_other_end_edge_half(bp, start_edge, r_bv);
3390  }
3391 
3392  /* Case 2: The next EdgeHalf is across a BevVert from the current EdgeHalf. */
3393  /* Skip all the logic if there's only one beveled edge at the vertex, we're at an end. */
3394  if ((*r_bv)->selcount == 1) {
3395  return NULL; /* No other edges to go to. */
3396  }
3397 
3398  /* The case with only one other edge connected to the vertex is special too. */
3399  if ((*r_bv)->selcount == 2) {
3400  /* Just find the next beveled edge, that's the only other option. */
3401  EdgeHalf *new_edge = start_edge;
3402  do {
3403  new_edge = new_edge->next;
3404  } while (!new_edge->is_bev);
3405 
3406  return new_edge;
3407  }
3408 
3409  /* Find the direction vector of the current edge (pointing INTO the BevVert).
3410  * v1 and v2 don't necessarily have an order, so we need to check which is closer to bv. */
3411  float dir_start_edge[3];
3412  if (start_edge->e->v1 == (*r_bv)->v) {
3413  sub_v3_v3v3(dir_start_edge, start_edge->e->v1->co, start_edge->e->v2->co);
3414  }
3415  else {
3416  sub_v3_v3v3(dir_start_edge, start_edge->e->v2->co, start_edge->e->v1->co);
3417  }
3418  normalize_v3(dir_start_edge);
3419 
3420  /* Find the beveled edge coming out of the BevVert that's most parallel to the current edge. */
3421  EdgeHalf *new_edge = start_edge->next;
3422  float second_best_dot = 0.0f, best_dot = 0.0f;
3423  EdgeHalf *next_edge = NULL;
3424  while (new_edge != start_edge) {
3425  if (!new_edge->is_bev) {
3426  new_edge = new_edge->next;
3427  continue;
3428  }
3429  /* Find direction vector of the possible next edge (pointing OUT of the BevVert). */
3430  float dir_new_edge[3];
3431  if (new_edge->e->v2 == (*r_bv)->v) {
3432  sub_v3_v3v3(dir_new_edge, new_edge->e->v1->co, new_edge->e->v2->co);
3433  }
3434  else {
3435  sub_v3_v3v3(dir_new_edge, new_edge->e->v2->co, new_edge->e->v1->co);
3436  }
3437  normalize_v3(dir_new_edge);
3438 
3439  /* Use this edge if it is the most parallel to the original so far. */
3440  float new_dot = dot_v3v3(dir_new_edge, dir_start_edge);
3441  if (new_dot > best_dot) {
3442  second_best_dot = best_dot; /* For remembering if the choice was too close. */
3443  best_dot = new_dot;
3444  next_edge = new_edge;
3445  }
3446  else if (new_dot > second_best_dot) {
3447  second_best_dot = new_dot;
3448  }
3449 
3450  new_edge = new_edge->next;
3451  }
3452 
3453  /* Only return a new Edge if one was found and if the choice of next edge was not too close. */
3454  if ((next_edge != NULL) && compare_ff(best_dot, second_best_dot, BEVEL_SMALL_ANG_DOT)) {
3455  return NULL;
3456  }
3457  return next_edge;
3458 }
3459 
3467 {
3468  BevVert *start_bv = find_bevvert(bp, bme->v1);
3469  EdgeHalf *start_edgehalf = find_edge_half(start_bv, bme);
3470  if (!start_edgehalf->is_bev || start_edgehalf->visited_rpo) {
3471  return;
3472  }
3473 
3474  /* Pick a BoundVert on one side of the profile to use for the starting side. Use the one highest
3475  * on the Z axis because even any rule is better than an arbitrary decision. */
3476  bool right_highest = start_edgehalf->leftv->nv.co[2] < start_edgehalf->rightv->nv.co[2];
3477  start_edgehalf->leftv->is_profile_start = right_highest;
3478  start_edgehalf->visited_rpo = true;
3479 
3480  /* First loop starts in the away from BevVert direction and the second starts toward it. */
3481  for (int i = 0; i < 2; i++) {
3482  EdgeHalf *edgehalf = start_edgehalf;
3483  BevVert *bv = start_bv;
3484  bool toward_bv = (i == 0);
3485  edgehalf = next_edgehalf_bev(bp, edgehalf, toward_bv, &bv);
3486 
3487  /* Keep traveling until there is no unvisited beveled edgehalf to visit next. */
3488  while (edgehalf && !edgehalf->visited_rpo) {
3489  /* Mark the correct BoundVert as the start of the newly visited profile.
3490  * The direction relative to the BevVert switches every step, so also switch
3491  * the orientation every step. */
3492  if (i == 0) {
3493  edgehalf->leftv->is_profile_start = toward_bv ^ right_highest;
3494  }
3495  else {
3496  /* The opposite side as the first direction because we're moving the other way. */
3497  edgehalf->leftv->is_profile_start = (!toward_bv) ^ right_highest;
3498  }
3499 
3500  /* The next jump will in the opposite direction relative to the BevVert. */
3501  toward_bv = !toward_bv;
3502 
3503  edgehalf->visited_rpo = true;
3504  edgehalf = next_edgehalf_bev(bp, edgehalf, toward_bv, &bv);
3505  }
3506  }
3507 }
3508 
3517 static void adjust_the_cycle_or_chain(BoundVert *vstart, bool iscycle)
3518 {
3519  int np = 0;
3520 #ifdef DEBUG_ADJUST
3521  printf("\nadjust the %s (with eigen)\n", iscycle ? "cycle" : "chain");
3522 #endif
3523  BoundVert *v = vstart;
3524  do {
3525 #ifdef DEBUG_ADJUST
3526  eleft = v->elast;
3527  eright = v->efirst;
3528  printf(" (left=e%d, right=e%d)", BM_elem_index_get(eleft->e), BM_elem_index_get(eright->e));
3529 #endif
3530  np++;
3531  v = v->adjchain;
3532  } while (v && v != vstart);
3533 #ifdef DEBUG_ADJUST
3534  printf(" -> %d parms\n", np);
3535 #endif
3536 
3537 #ifdef FAST_ADJUST_CODE
3538  if (adjust_the_cycle_or_chain_fast(vstart, np, iscycle)) {
3539  return;
3540  }
3541 #endif
3542 
3543  int nrows = iscycle ? 3 * np : 3 * np - 3;
3544 
3545  LinearSolver *solver = EIG_linear_least_squares_solver_new(nrows, np, 1);
3546 
3547  v = vstart;
3548  int i = 0;
3549  /* Sqrt of factor to weight down importance of spec match. */
3550  double weight = BEVEL_MATCH_SPEC_WEIGHT;
3551  EdgeHalf *eleft, *eright, *enextleft;
3552  do {
3553  /* Except at end of chain, v's indep variable is offset_r of v->efirst. */
3554  if (iscycle || i < np - 1) {
3555  eright = v->efirst;
3556  eleft = v->elast;
3557  enextleft = v->adjchain->elast;
3558 #ifdef DEBUG_ADJUST
3559  printf("p%d: e%d->offset_r = %f\n", i, BM_elem_index_get(eright->e), eright->offset_r);
3560  if (iscycle || v != vstart) {
3561  printf(" dependent: e%d->offset_l = %f * p%d\n",
3562  BM_elem_index_get(eleft->e),
3563  v->sinratio,
3564  i);
3565  }
3566 #endif
3567 
3568  /* Residue i: width difference between eright and eleft of next. */
3569  EIG_linear_solver_matrix_add(solver, i, i, 1.0);
3570  EIG_linear_solver_right_hand_side_add(solver, 0, i, 0.0);
3571  if (iscycle) {
3572  EIG_linear_solver_matrix_add(solver, i > 0 ? i - 1 : np - 1, i, -v->sinratio);
3573  }
3574  else {
3575  if (i > 0) {
3576  EIG_linear_solver_matrix_add(solver, i - 1, i, -v->sinratio);
3577  }
3578  }
3579 
3580  /* Residue np + 2*i (if cycle) else np - 1 + 2*i:
3581  * right offset for parm i matches its spec; weighted. */
3582  int row = iscycle ? np + 2 * i : np - 1 + 2 * i;
3583  EIG_linear_solver_matrix_add(solver, row, i, weight);
3584  EIG_linear_solver_right_hand_side_add(solver, 0, row, weight * eright->offset_r);
3585 #ifdef DEBUG_ADJUST
3586  printf("b[%d]=%f * %f, for e%d->offset_r\n",
3587  row,
3588  weight,
3589  eright->offset_r,
3590  BM_elem_index_get(eright->e));
3591 #endif
3592 
3593  /* Residue np + 2*i + 1 (if cycle) else np - 1 + 2*i + 1:
3594  * left offset for parm i matches its spec; weighted. */
3595  row = row + 1;
3597  solver, row, (i == np - 1) ? 0 : i + 1, weight * v->adjchain->sinratio);
3598  EIG_linear_solver_right_hand_side_add(solver, 0, row, weight * enextleft->offset_l);
3599 #ifdef DEBUG_ADJUST
3600  printf("b[%d]=%f * %f, for e%d->offset_l\n",
3601  row,
3602  weight,
3603  enextleft->offset_l,
3604  BM_elem_index_get(enextleft->e));
3605 #endif
3606  }
3607  else {
3608  /* Not a cycle, and last of chain. */
3609  eleft = v->elast;
3610 #ifdef DEBUG_ADJUST
3611  printf("p%d: e%d->offset_l = %f\n", i, BM_elem_index_get(eleft->e), eleft->offset_l);
3612 #endif
3613  /* Second part of residue i for last i. */
3614  EIG_linear_solver_matrix_add(solver, i - 1, i, -1.0);
3615  }
3616  i++;
3617  v = v->adjchain;
3618  } while (v && v != vstart);
3619  EIG_linear_solver_solve(solver);
3620 #ifdef DEBUG_ADJUST
3621  /* Note: this print only works after solve, but by that time b has been cleared. */
3623  printf("\nSolution:\n");
3624  for (i = 0; i < np; i++) {
3625  printf("p%d = %f\n", i, EIG_linear_solver_variable_get(solver, 0, i));
3626  }
3627 #endif
3628 
3629  /* Use the solution to set new widths. */
3630  v = vstart;
3631  i = 0;
3632  do {
3633  double val = EIG_linear_solver_variable_get(solver, 0, i);
3634  if (iscycle || i < np - 1) {
3635  eright = v->efirst;
3636  eleft = v->elast;
3637  eright->offset_r = (float)val;
3638 #ifdef DEBUG_ADJUST
3639  printf("e%d->offset_r = %f\n", BM_elem_index_get(eright->e), eright->offset_r);
3640 #endif
3641  if (iscycle || v != vstart) {
3642  eleft->offset_l = (float)(v->sinratio * val);
3643 #ifdef DEBUG_ADJUST
3644  printf("e%d->offset_l = %f\n", BM_elem_index_get(eleft->e), eleft->offset_l);
3645 #endif
3646  }
3647  }
3648  else {
3649  /* Not a cycle, and last of chain. */
3650  eleft = v->elast;
3651  eleft->offset_l = (float)val;
3652 #ifdef DEBUG_ADJUST
3653  printf("e%d->offset_l = %f\n", BM_elem_index_get(eleft->e), eleft->offset_l);
3654 #endif
3655  }
3656  i++;
3657  v = v->adjchain;
3658  } while (v && v != vstart);
3659 
3660 #ifdef DEBUG_ADJUST
3661  print_adjust_stats(vstart);
3663 #endif
3664 
3665  EIG_linear_solver_delete(solver);
3666 }
3667 
3680 {
3681  /* Find and process chains and cycles of unvisited BoundVerts that have eon set. */
3682  /* Note: for repeatability, iterate over all verts of mesh rather than over ghash'ed BMVerts. */
3683  BMIter iter;
3684  BMVert *bmv;
3685  BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
3686  if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
3687  continue;
3688  }
3689  BevVert *bv = find_bevvert(bp, bmv);
3690  BevVert *bvcur = bv;
3691  if (!bv) {
3692  continue;
3693  }
3694  BoundVert *vanchor = bv->vmesh->boundstart;
3695  do {
3696  if (vanchor->visited || !vanchor->eon) {
3697  continue;
3698  }
3699 
3700  /* Find one of (1) a cycle that starts and ends at v
3701  * where each v has v->eon set and had not been visited before;
3702  * or (2) a chain of v's where the start and end of the chain do not have
3703  * v->eon set but all else do.
3704  * It is OK for the first and last elements to
3705  * have been visited before, but not any of the inner ones.
3706  * We chain the v's together through v->adjchain, and are following
3707  * them in left->right direction, meaning that the left side of one edge
3708  * pairs with the right side of the next edge in the cycle or chain. */
3709 
3710  /* First follow paired edges in left->right direction. */
3711  BoundVert *v, *vchainstart, *vchainend;
3712  v = vchainstart = vchainend = vanchor;
3713 
3714  bool iscycle = false;
3715  int chainlen = 1;
3716  while (v->eon && !v->visited && !iscycle) {
3717  v->visited = true;
3718  if (!v->efirst) {
3719  break;
3720  }
3721  EdgeHalf *enext = find_other_end_edge_half(bp, v->efirst, &bvcur);
3722  if (!enext) {
3723  break;
3724  }
3725  BLI_assert(enext != NULL);
3726  BoundVert *vnext = enext->leftv;
3727  v->adjchain = vnext;
3728  vchainend = vnext;
3729  chainlen++;
3730  if (vnext->visited) {
3731  if (vnext != vchainstart) {
3732  break;
3733  }
3734  adjust_the_cycle_or_chain(vchainstart, true);
3735  iscycle = true;
3736  }
3737  v = vnext;
3738  }
3739  if (!iscycle) {
3740  /* right->left direction, changing vchainstart at each step. */
3741  v->adjchain = NULL;
3742  v = vchainstart;
3743  bvcur = bv;
3744  do {
3745  v->visited = true;
3746  if (!v->elast) {
3747  break;
3748  }
3749  EdgeHalf *enext = find_other_end_edge_half(bp, v->elast, &bvcur);
3750  if (!enext) {
3751  break;
3752  }
3753  BoundVert *vnext = enext->rightv;
3754  vnext->adjchain = v;
3755  chainlen++;
3756  vchainstart = vnext;
3757  v = vnext;
3758  } while (!v->visited && v->eon);
3759  if (chainlen >= 3 && !vchainstart->eon && !vchainend->eon) {
3760  adjust_the_cycle_or_chain(vchainstart, false);
3761  }
3762  }
3763  } while ((vanchor = vanchor->next) != bv->vmesh->boundstart);
3764  }
3765 
3766  /* Rebuild boundaries with new width specs. */
3767  BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
3768  if (BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
3769  BevVert *bv = find_bevvert(bp, bmv);
3770  if (bv) {
3771  build_boundary(bp, bv, false);
3772  }
3773  }
3774  }
3775 }
3776 
3787 {
3788  VMesh *vm = bv->vmesh;
3789  if (vm->count < 3 || vm->count > 4 || bv->selcount < 3 || bv->selcount > 4) {
3790  return NULL;
3791  }
3792 
3793  /* Find v1, v2, v3 all with beveled edges, where v1 and v3 have collinear edges. */
3794  EdgeHalf *epipe = NULL;
3795  BoundVert *v1 = vm->boundstart;
3796  float dir1[3], dir3[3];
3797  do {
3798  BoundVert *v2 = v1->next;
3799  BoundVert *v3 = v2->next;
3800  if (v1->ebev && v2->ebev && v3->ebev) {
3801  sub_v3_v3v3(dir1, bv->v->co, BM_edge_other_vert(v1->ebev->e, bv->v)->co);
3802  sub_v3_v3v3(dir3, BM_edge_other_vert(v3->ebev->e, bv->v)->co, bv->v->co);
3803  normalize_v3(dir1);
3804  normalize_v3(dir3);
3805  if (angle_normalized_v3v3(dir1, dir3) < BEVEL_EPSILON_ANG) {
3806  epipe = v1->ebev;
3807  break;
3808  }
3809  }
3810  } while ((v1 = v1->next) != vm->boundstart);
3811 
3812  if (!epipe) {
3813  return NULL;
3814  }
3815 
3816  /* Check face planes: all should have normals perpendicular to epipe. */
3817  for (EdgeHalf *e = &bv->edges[0]; e != &bv->edges[bv->edgecount]; e++) {
3818  if (e->fnext) {
3819  if (fabsf(dot_v3v3(dir1, e->fnext->no)) > BEVEL_EPSILON_BIG) {
3820  return NULL;
3821  }
3822  }
3823  }
3824  return v1;
3825 }
3826 
3828 {
3829  VMesh *vm = (VMesh *)BLI_memarena_alloc(mem_arena, sizeof(VMesh));
3830  vm->count = count;
3831  vm->seg = seg;
3832  vm->boundstart = bounds;
3834  sizeof(NewVert) * count * (1 + seg / 2) * (1 + seg));
3835  vm->mesh_kind = M_ADJ;
3836  return vm;
3837 }
3838 
3849 static NewVert *mesh_vert_canon(VMesh *vm, int i, int j, int k)
3850 {
3851  int n = vm->count;
3852  int ns = vm->seg;
3853  int ns2 = ns / 2;
3854  int odd = ns % 2;
3855  BLI_assert(0 <= i && i <= n && 0 <= j && j <= ns && 0 <= k && k <= ns);
3856 
3857  if (!odd && j == ns2 && k == ns2) {
3858  return mesh_vert(vm, 0, j, k);
3859  }
3860  if (j <= ns2 - 1 + odd && k <= ns2) {
3861  return mesh_vert(vm, i, j, k);
3862  }
3863  if (k <= ns2) {
3864  return mesh_vert(vm, (i + n - 1) % n, k, ns - j);
3865  }
3866  return mesh_vert(vm, (i + 1) % n, ns - k, j);
3867 }
3868 
3869 static bool is_canon(VMesh *vm, int i, int j, int k)
3870 {
3871  int ns2 = vm->seg / 2;
3872  if (vm->seg % 2 == 1) { /* Odd. */
3873  return (j <= ns2 && k <= ns2);
3874  }
3875  /* Even. */
3876  return ((j < ns2 && k <= ns2) || (j == ns2 && k == ns2 && i == 0));
3877 }
3878 
3879 /* Copy the vertex data to all of vm verts from canonical ones. */
3881 {
3882  int n = vm->count;
3883  int ns = vm->seg;
3884  int ns2 = ns / 2;
3885  for (int i = 0; i < n; i++) {
3886  for (int j = 0; j <= ns2; j++) {
3887  for (int k = 0; k <= ns; k++) {
3888  if (is_canon(vm, i, j, k)) {
3889  continue;
3890  }
3891  NewVert *v1 = mesh_vert(vm, i, j, k);
3892  NewVert *v0 = mesh_vert_canon(vm, i, j, k);
3893  copy_v3_v3(v1->co, v0->co);
3894  v1->v = v0->v;
3895  }
3896  }
3897  }
3898 }
3899 
3900 /* Calculate and return in r_cent the centroid of the center poly. */
3901 static void vmesh_center(VMesh *vm, float r_cent[3])
3902 {
3903  int n = vm->count;
3904  int ns2 = vm->seg / 2;
3905  if (vm->seg % 2) {
3906  zero_v3(r_cent);
3907  for (int i = 0; i < n; i++) {
3908  add_v3_v3(r_cent, mesh_vert(vm, i, ns2, ns2)->co);
3909  }
3910  mul_v3_fl(r_cent, 1.0f / (float)n);
3911  }
3912  else {
3913  copy_v3_v3(r_cent, mesh_vert(vm, 0, ns2, ns2)->co);
3914  }
3915 }
3916 
3917 static void avg4(
3918  float co[3], const NewVert *v0, const NewVert *v1, const NewVert *v2, const NewVert *v3)
3919 {
3920  add_v3_v3v3(co, v0->co, v1->co);
3921  add_v3_v3(co, v2->co);
3922  add_v3_v3(co, v3->co);
3923  mul_v3_fl(co, 0.25f);
3924 }
3925 
3926 /* Gamma needed for smooth Catmull-Clark, Sabin modification. */
3927 static float sabin_gamma(int n)
3928 {
3929  /* pPrecalculated for common cases of n. */
3930  if (n < 3) {
3931  return 0.0f;
3932  }
3933  if (n == 3) {
3934  return 0.065247584f;
3935  }
3936  if (n == 4) {
3937  return 0.25f;
3938  }
3939  if (n == 5) {
3940  return 0.401983447f;
3941  }
3942  if (n == 6) {
3943  return 0.523423277f;
3944  }
3945  double k = cos(M_PI / (double)n);
3946  /* Need x, real root of x^3 + (4k^2 - 3)x - 2k = 0.
3947  * Answer calculated via Wolfram Alpha. */
3948  double k2 = k * k;
3949  double k4 = k2 * k2;
3950  double k6 = k4 * k2;
3951  double y = pow(M_SQRT3 * sqrt(64.0 * k6 - 144.0 * k4 + 135.0 * k2 - 27.0) + 9.0 * k, 1.0 / 3.0);
3952  double x = 0.480749856769136 * y - (0.231120424783545 * (12.0 * k2 - 9.0)) / y;
3953  return (k * x + 2.0 * k2 - 1.0) / (x * x * (k * x + 1.0));
3954 }
3955 
3956 /* Fill frac with fractions of the way along ring 0 for vertex i, for use with interp_range
3957  * function. */
3958 static void fill_vmesh_fracs(VMesh *vm, float *frac, int i)
3959 {
3960  float total = 0.0f;
3961 
3962  int ns = vm->seg;
3963  frac[0] = 0.0f;
3964  for (int k = 0; k < ns; k++) {
3965  total += len_v3v3(mesh_vert(vm, i, 0, k)->co, mesh_vert(vm, i, 0, k + 1)->co);
3966  frac[k + 1] = total;
3967  }
3968  if (total > 0.0f) {
3969  for (int k = 1; k <= ns; k++) {
3970  frac[k] /= total;
3971  }
3972  }
3973  else {
3974  frac[ns] = 1.0f;
3975  }
3976 }
3977 
3978 /* Like fill_vmesh_fracs but want fractions for profile points of bndv, with ns segments. */
3979 static void fill_profile_fracs(BevelParams *bp, BoundVert *bndv, float *frac, int ns)
3980 {
3981  float co[3], nextco[3];
3982  float total = 0.0f;
3983 
3984  frac[0] = 0.0f;
3985  copy_v3_v3(co, bndv->nv.co);
3986  for (int k = 0; k < ns; k++) {
3987  get_profile_point(bp, &bndv->profile, k + 1, ns, nextco);
3988  total += len_v3v3(co, nextco);
3989  frac[k + 1] = total;
3990  copy_v3_v3(co, nextco);
3991  }
3992  if (total > 0.0f) {
3993  for (int k = 1; k <= ns; k++) {
3994  frac[k] /= total;
3995  }
3996  }
3997  else {
3998  frac[ns] = 1.0f;
3999  }
4000 }
4001 
4002 /* Return i such that frac[i] <= f <= frac[i + 1], where frac[n] == 1.0
4003  * and put fraction of rest of way between frac[i] and frac[i + 1] into r_rest. */
4004 static int interp_range(const float *frac, int n, const float f, float *r_rest)
4005 {
4006  /* Could binary search in frac, but expect n to be reasonably small. */
4007  for (int i = 0; i < n; i++) {
4008  if (f <= frac[i + 1]) {
4009  float rest = f - frac[i];
4010  if (rest == 0) {
4011  *r_rest = 0.0f;
4012  }
4013  else {
4014  *r_rest = rest / (frac[i + 1] - frac[i]);
4015  }
4016  if (i == n - 1 && *r_rest == 1.0f) {
4017  i = n;
4018  *r_rest = 0.0f;
4019  }
4020  return i;
4021  }
4022  }
4023  *r_rest = 0.0f;
4024  return n;
4025 }
4026 
4027 /* Interpolate given vmesh to make one with target nseg border vertices on the profiles.
4028  * TODO(Hans): This puts the center mesh vert at a slightly off location sometimes, which seems to
4029  * be associated with the rest of that ring being shifted or connected slightly incorrectly to its
4030  * neighbors. */
4031 static VMesh *interp_vmesh(BevelParams *bp, VMesh *vm_in, int nseg)
4032 {
4033  int n_bndv = vm_in->count;
4034  int ns_in = vm_in->seg;
4035  int nseg2 = nseg / 2;
4036  int odd = nseg % 2;
4037  VMesh *vm_out = new_adj_vmesh(bp->mem_arena, n_bndv, nseg, vm_in->boundstart);
4038 
4039  float *prev_frac = BLI_array_alloca(prev_frac, (ns_in + 1));
4040  float *frac = BLI_array_alloca(frac, (ns_in + 1));
4041  float *new_frac = BLI_array_alloca(new_frac, (nseg + 1));
4042  float *prev_new_frac = BLI_array_alloca(prev_new_frac, (nseg + 1));
4043 
4044  fill_vmesh_fracs(vm_in, prev_frac, n_bndv - 1);
4045  BoundVert *bndv = vm_in->boundstart;
4046  fill_profile_fracs(bp, bndv->prev, prev_new_frac, nseg);
4047  for (int i = 0; i < n_bndv; i++) {
4048  fill_vmesh_fracs(vm_in, frac, i);
4049  fill_profile_fracs(bp, bndv, new_frac, nseg);
4050  for (int j = 0; j <= nseg2 - 1 + odd; j++) {
4051  for (int k = 0; k <= nseg2; k++) {
4052  /* Finding the locations where "fraction" fits into previous and current "frac". */
4053  float fraction = new_frac[k];
4054  float restk;
4055  float restkprev;
4056  int k_in = interp_range(frac, ns_in, fraction, &restk);
4057  fraction = prev_new_frac[nseg - j];
4058  int k_in_prev = interp_range(prev_frac, ns_in, fraction, &restkprev);
4059  int j_in = ns_in - k_in_prev;
4060  float restj = -restkprev;
4061  if (restj > -BEVEL_EPSILON) {
4062  restj = 0.0f;
4063  }
4064  else {
4065  j_in = j_in - 1;
4066  restj = 1.0f + restj;
4067  }
4068  /* Use bilinear interpolation within the source quad; could be smarter here. */
4069  float co[3];
4070  if (restj < BEVEL_EPSILON && restk < BEVEL_EPSILON) {
4071  copy_v3_v3(co, mesh_vert_canon(vm_in, i, j_in, k_in)->co);
4072  }
4073  else {
4074  int j0inc = (restj < BEVEL_EPSILON || j_in == ns_in) ? 0 : 1;
4075  int k0inc = (restk < BEVEL_EPSILON || k_in == ns_in) ? 0 : 1;
4076  float quad[4][3];
4077  copy_v3_v3(quad[0], mesh_vert_canon(vm_in, i, j_in, k_in)->co);
4078  copy_v3_v3(quad[1], mesh_vert_canon(vm_in, i, j_in, k_in + k0inc)->co);
4079  copy_v3_v3(quad[2], mesh_vert_canon(vm_in, i, j_in + j0inc, k_in + k0inc)->co);
4080  copy_v3_v3(quad[3], mesh_vert_canon(vm_in, i, j_in + j0inc, k_in)->co);
4081  interp_bilinear_quad_v3(quad, restk, restj, co);
4082  }
4083  copy_v3_v3(mesh_vert(vm_out, i, j, k)->co, co);
4084  }
4085  }
4086  bndv = bndv->next;
4087  memcpy(prev_frac, frac, sizeof(float) * (ns_in + 1));
4088  memcpy(prev_new_frac, new_frac, sizeof(float) * (nseg + 1));
4089  }
4090  if (!odd) {
4091  float center[3];
4092  vmesh_center(vm_in, center);
4093  copy_v3_v3(mesh_vert(vm_out, 0, nseg2, nseg2)->co, center);
4094  }
4095  vmesh_copy_equiv_verts(vm_out);
4096  return vm_out;
4097 }
4098 
4099 /* Do one step of cubic subdivision (Catmull-Clark), with special rules at boundaries.
4100  * For now, this is written assuming vm0->nseg is even and > 0.
4101  * We are allowed to modify vm_in, as it will not be used after this call.
4102  * See Levin 1999 paper: "Filling an N-sided hole using combined subdivision schemes". */
4103 static VMesh *cubic_subdiv(BevelParams *bp, VMesh *vm_in)
4104 {
4105  float co[3];
4106 
4107  int n_boundary = vm_in->count;
4108  int ns_in = vm_in->seg;
4109  int ns_in2 = ns_in / 2;
4110  BLI_assert(ns_in % 2 == 0);
4111  int ns_out = 2 * ns_in;
4112  VMesh *vm_out = new_adj_vmesh(bp->mem_arena, n_boundary, ns_out, vm_in->boundstart);
4113 
4114  /* First we adjust the boundary vertices of the input mesh, storing in output mesh. */
4115  for (int i = 0; i < n_boundary; i++) {
4116  copy_v3_v3(mesh_vert(vm_out, i, 0, 0)->co, mesh_vert(vm_in, i, 0, 0)->co);
4117  for (int k = 1; k < ns_in; k++) {
4118  copy_v3_v3(co, mesh_vert(vm_in, i, 0, k)->co);
4119 
4120  /* Smooth boundary rule. Custom profiles shouldn't be smoothed. */
4121  if (bp->profile_type != BEVEL_PROFILE_CUSTOM) {
4122  float co1[3], co2[3], acc[3];
4123  copy_v3_v3(co1, mesh_vert(vm_in, i, 0, k - 1)->co);
4124  copy_v3_v3(co2, mesh_vert(vm_in, i, 0, k + 1)->co);
4125 
4126  add_v3_v3v3(acc, co1, co2);
4127  madd_v3_v3fl(acc, co, -2.0f);
4128  madd_v3_v3fl(co, acc, -1.0f / 6.0f);
4129  }
4130 
4131  copy_v3_v3(mesh_vert_canon(vm_out, i, 0, 2 * k)->co, co);
4132  }
4133  }
4134  /* Now adjust odd boundary vertices in output mesh, based on even ones. */
4135  BoundVert *bndv = vm_out->boundstart;
4136  for (int i = 0; i < n_boundary; i++) {
4137  for (int k = 1; k < ns_out; k += 2) {
4138  get_profile_point(bp, &bndv->profile, k, ns_out, co);
4139 
4140  /* Smooth if using a non-custom profile. */
4141  if (bp->profile_type != BEVEL_PROFILE_CUSTOM) {
4142  float co1[3], co2[3], acc[3];
4143  copy_v3_v3(co1, mesh_vert_canon(vm_out, i, 0, k - 1)->co);
4144  copy_v3_v3(co2, mesh_vert_canon(vm_out, i, 0, k + 1)->co);
4145 
4146  add_v3_v3v3(acc, co1, co2);
4147  madd_v3_v3fl(acc, co, -2.0f);
4148  madd_v3_v3fl(co, acc, -1.0f / 6.0f);
4149  }
4150 
4151  copy_v3_v3(mesh_vert_canon(vm_out, i, 0, k)->co, co);
4152  }
4153  bndv = bndv->next;
4154  }
4155  vmesh_copy_equiv_verts(vm_out);
4156 
4157  /* Copy adjusted verts back into vm_in. */
4158  for (int i = 0; i < n_boundary; i++) {
4159  for (int k = 0; k < ns_in; k++) {
4160  copy_v3_v3(mesh_vert(vm_in, i, 0, k)->co, mesh_vert(vm_out, i, 0, 2 * k)->co);
4161  }
4162  }
4163 
4164  vmesh_copy_equiv_verts(vm_in);
4165 
4166  /* Now we do the internal vertices, using standard Catmull-Clark
4167  * and assuming all boundary vertices have valence 4. */
4168 
4169  /* The new face vertices. */
4170  for (int i = 0; i < n_boundary; i++) {
4171  for (int j = 0; j < ns_in2; j++) {
4172  for (int k = 0; k < ns_in2; k++) {
4173  /* Face up and right from (j, k). */
4174  avg4(co,
4175  mesh_vert(vm_in, i, j, k),
4176  mesh_vert(vm_in, i, j, k + 1),
4177  mesh_vert(vm_in, i, j + 1, k),
4178  mesh_vert(vm_in, i, j + 1, k + 1));
4179  copy_v3_v3(mesh_vert(vm_out, i, 2 * j + 1, 2 * k + 1)->co, co);
4180  }
4181  }
4182  }
4183 
4184  /* The new vertical edge vertices. */
4185  for (int i = 0; i < n_boundary; i++) {
4186  for (int j = 0; j < ns_in2; j++) {
4187  for (int k = 1; k <= ns_in2; k++) {
4188  /* Vertical edge between (j, k) and (j+1, k). */
4189  avg4(co,
4190  mesh_vert(vm_in, i, j, k),
4191  mesh_vert(vm_in, i, j + 1, k),
4192  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k - 1),
4193  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4194  copy_v3_v3(mesh_vert(vm_out, i, 2 * j + 1, 2 * k)->co, co);
4195  }
4196  }
4197  }
4198 
4199  /* The new horizontal edge vertices. */
4200  for (int i = 0; i < n_boundary; i++) {
4201  for (int j = 1; j < ns_in2; j++) {
4202  for (int k = 0; k < ns_in2; k++) {
4203  /* Horizontal edge between (j, k) and (j, k+1). */
4204  avg4(co,
4205  mesh_vert(vm_in, i, j, k),
4206  mesh_vert(vm_in, i, j, k + 1),
4207  mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k + 1),
4208  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4209  copy_v3_v3(mesh_vert(vm_out, i, 2 * j, 2 * k + 1)->co, co);
4210  }
4211  }
4212  }
4213 
4214  /* The new vertices, not on border. */
4215  float gamma = 0.25f;
4216  float beta = -gamma;
4217  for (int i = 0; i < n_boundary; i++) {
4218  for (int j = 1; j < ns_in2; j++) {
4219  for (int k = 1; k <= ns_in2; k++) {
4220  float co1[3], co2[3];
4221  /* co1 = centroid of adjacent new edge verts. */
4222  avg4(co1,
4223  mesh_vert_canon(vm_out, i, 2 * j, 2 * k - 1),
4224  mesh_vert_canon(vm_out, i, 2 * j, 2 * k + 1),
4225  mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k),
4226  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k));
4227  /* co2 = centroid of adjacent new face verts. */
4228  avg4(co2,
4229  mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k - 1),
4230  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k - 1),
4231  mesh_vert_canon(vm_out, i, 2 * j - 1, 2 * k + 1),
4232  mesh_vert_canon(vm_out, i, 2 * j + 1, 2 * k + 1));
4233  /* Combine with original vert with alpha, beta, gamma factors. */
4234  copy_v3_v3(co, co1); /* Alpha = 1.0. */
4235  madd_v3_v3fl(co, co2, beta);
4236  madd_v3_v3fl(co, mesh_vert(vm_in, i, j, k)->co, gamma);
4237  copy_v3_v3(mesh_vert(vm_out, i, 2 * j, 2 * k)->co, co);
4238  }
4239  }
4240  }
4241 
4242  vmesh_copy_equiv_verts(vm_out);
4243 
4244  /* The center vertex is special. */
4245  gamma = sabin_gamma(n_boundary);
4246  beta = -gamma;
4247  /* Accumulate edge verts in co1, face verts in co2. */
4248  float co1[3], co2[3];
4249  zero_v3(co1);
4250  zero_v3(co2);
4251  for (int i = 0; i < n_boundary; i++) {
4252  add_v3_v3(co1, mesh_vert(vm_out, i, ns_in, ns_in - 1)->co);
4253  add_v3_v3(co2, mesh_vert(vm_out, i, ns_in - 1, ns_in - 1)->co);
4254  add_v3_v3(co2, mesh_vert(vm_out, i, ns_in - 1, ns_in + 1)->co);
4255  }
4256  copy_v3_v3(co, co1);
4257  mul_v3_fl(co, 1.0f / (float)n_boundary);
4258  madd_v3_v3fl(co, co2, beta / (2.0f * (float)n_boundary));
4259  madd_v3_v3fl(co, mesh_vert(vm_in, 0, ns_in2, ns_in2)->co, gamma);
4260  for (int i = 0; i < n_boundary; i++) {
4261  copy_v3_v3(mesh_vert(vm_out, i, ns_in, ns_in)->co, co);
4262  }
4263 
4264  /* Final step: Copy the profile vertices to the VMesh's boundary. */
4265  bndv = vm_out->boundstart;
4266  for (int i = 0; i < n_boundary; i++) {
4267  int inext = (i + 1) % n_boundary;
4268  for (int k = 0; k <= ns_out; k++) {
4269  get_profile_point(bp, &bndv->profile, k, ns_out, co);
4270  copy_v3_v3(mesh_vert(vm_out, i, 0, k)->co, co);
4271  if (k >= ns_in && k < ns_out) {
4272  copy_v3_v3(mesh_vert(vm_out, inext, ns_out - k, 0)->co, co);
4273  }
4274  }
4275  bndv = bndv->next;
4276  }
4277 
4278  return vm_out;
4279 }
4280 
4281 /* Special case for cube corner, when r is PRO_SQUARE_R, meaning straight sides. */
4283 {
4284  int ns2 = nseg / 2;
4285  VMesh *vm = new_adj_vmesh(mem_arena, 3, nseg, NULL);
4286  vm->count = 0; /* Reset, so the following loop will end up with correct count. */
4287  for (int i = 0; i < 3; i++) {
4288  float co[3] = {0.0f, 0.0f, 0.0f};
4289  co[i] = 1.0f;
4290  add_new_bound_vert(mem_arena, vm, co);
4291  }
4292  for (int i = 0; i < 3; i++) {
4293  for (int j = 0; j <= ns2; j++) {
4294  for (int k = 0; k <= ns2; k++) {
4295  if (!is_canon(vm, i, j, k)) {
4296  continue;
4297  }
4298  float co[3];
4299  co[i] = 1.0f;
4300  co[(i + 1) % 3] = (float)k * 2.0f / (float)nseg;
4301  co[(i + 2) % 3] = (float)j * 2.0f / (float)nseg;
4302  copy_v3_v3(mesh_vert(vm, i, j, k)->co, co);
4303  }
4304  }
4305  }
4307  return vm;
4308 }
4309 
4317 {
4318  int ns2 = nseg / 2;
4319  int odd = nseg % 2;
4320  VMesh *vm = new_adj_vmesh(mem_arena, 3, nseg, NULL);
4321  vm->count = 0; /* Reset, so following loop will end up with correct count. */
4322  for (int i = 0; i < 3; i++) {
4323  float co[3] = {0.0f, 0.0f, 0.0f};
4324  co[i] = 1.0f;
4325  add_new_bound_vert(mem_arena, vm, co);
4326  }
4327 
4328  float b;
4329  if (odd) {
4330  b = 2.0f / (2.0f * (float)ns2 + (float)M_SQRT2);
4331  }
4332  else {
4333  b = 2.0f / (float)nseg;
4334  }
4335  for (int i = 0; i < 3; i++) {
4336  for (int k = 0; k <= ns2; k++) {
4337  float co[3];
4338  co[i] = 1.0f - (float)k * b;
4339  co[(i + 1) % 3] = 0.0f;
4340  co[(i + 2) % 3] = 0.0f;
4341  copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
4342  co[(i + 1) % 3] = 1.0f - (float)k * b;
4343  co[(i + 2) % 3] = 0.0f;
4344  co[i] = 0.0f;
4345  copy_v3_v3(mesh_vert(vm, i, 0, nseg - k)->co, co);
4346  }
4347  }
4348  return vm;
4349 }
4350 
4358 {
4359  MemArena *mem_arena = bp->mem_arena;
4360  int nseg = bp->seg;
4361  float r = bp->pro_super_r;
4362 
4363  if (bp->profile_type != BEVEL_PROFILE_CUSTOM) {
4364  if (r == PRO_SQUARE_R) {
4365  return make_cube_corner_square(mem_arena, nseg);
4366  }
4367  if (r == PRO_SQUARE_IN_R) {
4368  return make_cube_corner_square_in(mem_arena, nseg);
4369  }
4370  }
4371 
4372  /* Initial mesh has 3 sides and 2 segments on each side. */
4373  VMesh *vm0 = new_adj_vmesh(mem_arena, 3, 2, NULL);
4374  vm0->count = 0; /* Reset, so the following loop will end up with correct count. */
4375  for (int i = 0; i < 3; i++) {
4376  float co[3] = {0.0f, 0.0f, 0.0f};
4377  co[i] = 1.0f;
4378  add_new_bound_vert(mem_arena, vm0, co);
4379  }
4380  BoundVert *bndv = vm0->boundstart;
4381  for (int i = 0; i < 3; i++) {
4382  float coc[3];
4383  /* Get point, 1/2 of the way around profile, on arc between this and next. */
4384  coc[i] = 1.0f;
4385  coc[(i + 1) % 3] = 1.0f;
4386  coc[(i + 2) % 3] = 0.0f;
4387  bndv->profile.super_r = r;
4388  copy_v3_v3(bndv->profile.start, bndv->nv.co);
4389  copy_v3_v3(bndv->profile.end, bndv->next->nv.co);
4390  copy_v3_v3(bndv->profile.middle, coc);
4391  copy_v3_v3(mesh_vert(vm0, i, 0, 0)->co, bndv->profile.start);
4392  copy_v3_v3(bndv->profile.plane_co, bndv->profile.start);
4393  cross_v3_v3v3(bndv->profile.plane_no, bndv->profile.start, bndv->profile.end);
4394  copy_v3_v3(bndv->profile.proj_dir, bndv->profile.plane_no);
4395  /* Calculate profiles again because we started over with new boundverts. */
4396  calculate_profile(bp, bndv, false, false); /* No custom profiles in this case. */
4397 
4398  /* Just building the boundaries here, so sample the profile halfway through. */
4399  get_profile_point(bp, &bndv->profile, 1, 2, mesh_vert(vm0, i, 0, 1)->co);
4400 
4401  bndv = bndv->next;
4402  }
4403  /* Center vertex. */
4404  float co[3];
4405  copy_v3_fl(co, (float)M_SQRT1_3);
4406 
4407  if (nseg > 2) {
4408  if (r > 1.5f) {
4409  mul_v3_fl(co, 1.4f);
4410  }
4411  else if (r < 0.75f) {
4412  mul_v3_fl(co, 0.6f);
4413  }
4414  }
4415  copy_v3_v3(mesh_vert(vm0, 0, 1, 1)->co, co);
4416 
4418 
4419  VMesh *vm1 = vm0;
4420  while (vm1->seg < nseg) {
4421  vm1 = cubic_subdiv(bp, vm1);
4422  }
4423  if (vm1->seg != nseg) {
4424  vm1 = interp_vmesh(bp, vm1, nseg);
4425  }
4426 
4427  /* Now snap each vertex to the superellipsoid. */
4428  int ns2 = nseg / 2;
4429  for (int i = 0; i < 3; i++) {
4430  for (int j = 0; j <= ns2; j++) {
4431  for (int k = 0; k <= nseg; k++) {
4432  snap_to_superellipsoid(mesh_vert(vm1, i, j, k)->co, r, false);
4433  }
4434  }
4435  }
4436 
4437  return vm1;
4438 }
4439 
4440 /* Is this a good candidate for using tri_corner_adj_vmesh? */
4442 {
4443  int in_plane_e = 0;
4444 
4445  /* The superellipse snapping of this case isn't helpful with custom profiles enabled. */
4447  return -1;
4448  }
4449  if (bv->vmesh->count != 3) {
4450  return 0;
4451  }
4452 
4453  /* Only use the tri-corner special case if the offset is the same for every edge. */
4454  float offset = bv->edges[0].offset_l;
4455 
4456  float totang = 0.0f;
4457  for (int i = 0; i < bv->edgecount; i++) {
4458  EdgeHalf *e = &bv->edges[i];
4459  float ang = BM_edge_calc_face_angle_signed_ex(e->e, 0.0f);
4460  float absang = fabsf(ang);
4461  if (absang <= M_PI_4) {
4462  in_plane_e++;
4463  }
4464  else if (absang >= 3.0f * (float)M_PI_4) {
4465  return -1;
4466  }
4467 
4468  if (e->is_bev && !compare_ff(e->offset_l, offset, BEVEL_EPSILON)) {
4469  return -1;
4470  }
4471 
4472  totang += ang;
4473  }
4474  if (in_plane_e != bv->edgecount - 3) {
4475  return -1;
4476  }
4477  float angdiff = fabsf(fabsf(totang) - 3.0f * (float)M_PI_2);
4478  if ((bp->pro_super_r == PRO_SQUARE_R && angdiff > (float)M_PI / 16.0f) ||
4479  (angdiff > (float)M_PI_4)) {
4480  return -1;
4481  }
4482  if (bv->edgecount != 3 || bv->selcount != 3) {
4483  return 0;
4484  }
4485  return 1;
4486 }
4487 
4489 {
4490  BoundVert *bndv = bv->vmesh->boundstart;
4491 
4492  float co0[3], co1[3], co2[3];
4493  copy_v3_v3(co0, bndv->nv.co);
4494  bndv = bndv->next;
4495  copy_v3_v3(co1, bndv->nv.co);
4496  bndv = bndv->next;
4497  copy_v3_v3(co2, bndv->nv.co);
4498 
4499  float mat[4][4];
4500  make_unit_cube_map(co0, co1, co2, bv->v->co, mat);
4501  int ns = bp->seg;
4502  int ns2 = ns / 2;
4504  for (int i = 0; i < 3; i++) {
4505  for (int j = 0; j <= ns2; j++) {
4506  for (int k = 0; k <= ns; k++) {
4507  float v[4];
4508  copy_v3_v3(v, mesh_vert(vm, i, j, k)->co);
4509  v[3] = 1.0f;
4510  mul_m4_v4(mat, v);
4511  copy_v3_v3(mesh_vert(vm, i, j, k)->co, v);
4512  }
4513  }
4514  }
4515 
4516  return vm;
4517 }
4518 
4519 /* Makes the mesh that replaces the original vertex, bounded by the profiles on the sides. */
4521 {
4522  MemArena *mem_arena = bp->mem_arena;
4523 
4524  int n_bndv = bv->vmesh->count;
4525 
4526  /* Same bevel as that of 3 edges of vert in a cube. */
4527  if (n_bndv == 3 && tri_corner_test(bp, bv) != -1 && bp->pro_super_r != PRO_SQUARE_IN_R) {
4528  return tri_corner_adj_vmesh(bp, bv);
4529  }
4530 
4531  /* First construct an initial control mesh, with nseg == 2. */
4532  int nseg = bv->vmesh->seg;
4533  VMesh *vm0 = new_adj_vmesh(mem_arena, n_bndv, 2, bv->vmesh->boundstart);
4534 
4535  /* Find the center of the boundverts that make up the vmesh. */
4536  BoundVert *bndv = vm0->boundstart;
4537  float boundverts_center[3] = {0.0f, 0.0f, 0.0f};
4538  for (int i = 0; i < n_bndv; i++) {
4539  /* Boundaries just divide input polygon edges into 2 even segments. */
4540  copy_v3_v3(mesh_vert(vm0, i, 0, 0)->co, bndv->nv.co);
4541  get_profile_point(bp, &bndv->profile, 1, 2, mesh_vert(vm0, i, 0, 1)->co);
4542  add_v3_v3(boundverts_center, bndv->nv.co);
4543  bndv = bndv->next;
4544  }
4545  mul_v3_fl(boundverts_center, 1.0f / (float)n_bndv);
4546 
4547  /* To place the center vertex:
4548  * 'negative_fullest' is the reflection of the original vertex across the boundverts' center.
4549  * 'fullness' is the fraction of the way from the boundvert's centroid to the original vertex
4550  * (if positive) or to negative_fullest (if negative). */
4551  float original_vertex[3], negative_fullest[3];
4552  copy_v3_v3(original_vertex, bv->v->co);
4553  sub_v3_v3v3(negative_fullest, boundverts_center, original_vertex);
4554  add_v3_v3(negative_fullest, boundverts_center);
4555 
4556  /* Find the vertex mesh's start center with the profile's fullness. */
4557  float fullness = bp->pro_spacing.fullness;
4558  float center_direction[3];
4559  sub_v3_v3v3(center_direction, original_vertex, boundverts_center);
4560  if (len_squared_v3(center_direction) > BEVEL_EPSILON_SQ) {
4561  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
4562  fullness *= 2.0f;
4563  madd_v3_v3v3fl(mesh_vert(vm0, 0, 1, 1)->co, negative_fullest, center_direction, fullness);
4564  }
4565  else {
4566  madd_v3_v3v3fl(mesh_vert(vm0, 0, 1, 1)->co, boundverts_center, center_direction, fullness);
4567  }
4568  }
4569  else {
4570  copy_v3_v3(mesh_vert(vm0, 0, 1, 1)->co, boundverts_center);
4571  }
4573 
4574  /* Do the subdivision process to go from the two segment start mesh to the final vertex mesh. */
4575  VMesh *vm1 = vm0;
4576  do {
4577  vm1 = cubic_subdiv(bp, vm1);
4578  } while (vm1->seg < nseg);
4579  if (vm1->seg != nseg) {
4580  vm1 = interp_vmesh(bp, vm1, nseg);
4581  }
4582  return vm1;
4583 }
4584 
4591 static void snap_to_pipe_profile(BoundVert *vpipe, bool midline, float co[3])
4592 {
4593  Profile *pro = &vpipe->profile;
4594  EdgeHalf *e = vpipe->ebev;
4595 
4596  if (compare_v3v3(pro->start, pro->end, BEVEL_EPSILON_D)) {
4597  copy_v3_v3(co, pro->start);
4598  return;
4599  }
4600 
4601  /* Get a plane with the normal pointing along the beveled edge. */
4602  float edir[3], plane[4];
4603  sub_v3_v3v3(edir, e->e->v1->co, e->e->v2->co);
4604  plane_from_point_normal_v3(plane, co, edir);
4605 
4606  float start_plane[3], end_plane[3], middle_plane[3];
4607  closest_to_plane_v3(start_plane, plane, pro->start);
4608  closest_to_plane_v3(end_plane, plane, pro->end);
4609  closest_to_plane_v3(middle_plane, plane, pro->middle);
4610 
4611  float m[4][4], minv[4][4];
4612  if (make_unit_square_map(start_plane, middle_plane, end_plane, m) && invert_m4_m4(minv, m)) {
4613  /* Transform co and project it onto superellipse. */
4614  float p[3];
4615  mul_v3_m4v3(p, minv, co);
4616  snap_to_superellipsoid(p, pro->super_r, midline);
4617 
4618  float snap[3];
4619  mul_v3_m4v3(snap, m, p);
4620  copy_v3_v3(co, snap);
4621  }
4622  else {
4623  /* Planar case: just snap to line start_plane--end_plane. */
4624  float p[3];
4625  closest_to_line_segment_v3(p, co, start_plane, end_plane);
4626  copy_v3_v3(co, p);
4627  }
4628 }
4629 
4636 {
4637  /* Some unnecessary overhead running this subdivision with custom profile snapping later on. */
4638  VMesh *vm = adj_vmesh(bp, bv);
4639 
4640  /* Now snap all interior coordinates to be on the epipe profile. */
4641  int n_bndv = bv->vmesh->count;
4642  int ns = bv->vmesh->seg;
4643  int half_ns = ns / 2;
4644  int ipipe1 = vpipe->index;
4645  int ipipe2 = vpipe->next->next->index;
4646 
4647  for (int i = 0; i < n_bndv; i++) {
4648  for (int j = 1; j <= half_ns; j++) {
4649  for (int k = 0; k <= half_ns; k++) {
4650  if (!is_canon(vm, i, j, k)) {
4651  continue;
4652  }
4653  /* With a custom profile just copy the shape of the profile at each ring. */
4654  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
4655  /* Find both profile vertices that correspond to this point. */
4656  float *profile_point_pipe1, *profile_point_pipe2, f;
4657  if (ELEM(i, ipipe1, ipipe2)) {
4658  if (n_bndv == 3 && i == ipipe1) {
4659  /* This part of the vmesh is the triangular corner between the two pipe profiles. */
4660  int ring = max_ii(j, k);
4661  profile_point_pipe2 = mesh_vert(vm, i, 0, ring)->co;
4662  profile_point_pipe1 = mesh_vert(vm, i, ring, 0)->co;
4663  /* End profile index increases with k on one side and j on the other. */
4664  f = ((k < j) ? min_ff(j, k) : ((2.0f * ring) - j)) / (2.0f * ring);
4665  }
4666  else {
4667  /* This is part of either pipe profile boundvert area in the 4-way intersection. */
4668  profile_point_pipe1 = mesh_vert(vm, i, 0, k)->co;
4669  profile_point_pipe2 = mesh_vert(vm, (i == ipipe1) ? ipipe2 : ipipe1, 0, ns - k)->co;
4670  f = (float)j / (float)ns; /* The ring index brings us closer to the other side. */
4671  }
4672  }
4673  else {
4674  /* The profile vertices are on both ends of each of the side profile's rings. */
4675  profile_point_pipe1 = mesh_vert(vm, i, j, 0)->co;
4676  profile_point_pipe2 = mesh_vert(vm, i, j, ns)->co;
4677  f = (float)k / (float)ns; /* Ring runs along the pipe, so segment is used here. */
4678  }
4679 
4680  /* Place the vertex by interpolating between the two profile points using the factor. */
4681  interp_v3_v3v3(mesh_vert(vm, i, j, k)->co, profile_point_pipe1, profile_point_pipe2, f);
4682  }
4683  else {
4684  /* A tricky case is for the 'square' profiles and an even nseg: we want certain
4685  * vertices to snap to the midline on the pipe, not just to one plane or the other. */
4686  bool even = (ns % 2) == 0;
4687  bool midline = even && k == half_ns &&
4688  ((i == 0 && j == half_ns) || (i == ipipe1 || i == ipipe2));
4689  snap_to_pipe_profile(vpipe, midline, mesh_vert(vm, i, j, k)->co);
4690  }
4691  }
4692  }
4693  }
4694  return vm;
4695 }
4696 
4697 static void get_incident_edges(BMFace *f, BMVert *v, BMEdge **r_e1, BMEdge **r_e2)
4698 {
4699  *r_e1 = NULL;
4700  *r_e2 = NULL;
4701  if (!f) {
4702  return;
4703  }
4704 
4705  BMIter iter;
4706  BMEdge *e;
4707  BM_ITER_ELEM (e, &iter, f, BM_EDGES_OF_FACE) {
4708  if (e->v1 == v || e->v2 == v) {
4709  if (*r_e1 == NULL) {
4710  *r_e1 = e;
4711  }
4712  else if (*r_e2 == NULL) {
4713  *r_e2 = e;
4714  }
4715  }
4716  }
4717 }
4718 
4719 static BMEdge *find_closer_edge(float *co, BMEdge *e1, BMEdge *e2)
4720 {
4721  BLI_assert(e1 != NULL && e2 != NULL);
4722  float dsq1 = dist_squared_to_line_segment_v3(co, e1->v1->co, e1->v2->co);
4723  float dsq2 = dist_squared_to_line_segment_v3(co, e2->v1->co, e2->v2->co);
4724  if (dsq1 < dsq2) {
4725  return e1;
4726  }
4727  return e2;
4728 }
4729 
4730 /* Snap co to the closest edge of face f. Return the edge in *r_snap_e,
4731  * the coordinates of snap point in r_ snap_co,
4732  * and the distance squared to the snap point as function return */
4733 static float snap_face_dist_squared(float *co, BMFace *f, BMEdge **r_snap_e, float *r_snap_co)
4734 {
4735  BMEdge *beste = NULL;
4736  float beste_d2 = 1e20f;
4737  BMIter iter;
4738  BMEdge *e;
4739  BM_ITER_ELEM (e, &iter, f, BM_EDGES_OF_FACE) {
4740  float closest[3];
4741  closest_to_line_segment_v3(closest, co, e->v1->co, e->v2->co);
4742  float d2 = len_squared_v3v3(closest, co);
4743  if (d2 < beste_d2) {
4744  beste_d2 = d2;
4745  beste = e;
4746  copy_v3_v3(r_snap_co, closest);
4747  }
4748  }
4749  *r_snap_e = beste;
4750  return beste_d2;
4751 }
4752 
4753 /* What would be the area of the polygon around bv if interpolated in face frep?
4754  */
4755 static float interp_poly_area(BevVert *bv, BMFace *frep)
4756 {
4757  VMesh *vm = bv->vmesh;
4758 
4759  BLI_assert(vm != NULL);
4760  float(*uv_co)[3] = BLI_array_alloca(uv_co, vm->count);
4761  BoundVert *v = vm->boundstart;
4762  int n = 0;
4763  do {
4764  BLI_assert(n < vm->count);
4765  BMEdge *snape;
4766  snap_face_dist_squared(v->nv.v->co, frep, &snape, uv_co[n]);
4767  n++;
4768  } while ((v = v->next) != vm->boundstart);
4769  float area = fabsf(area_poly_v3(uv_co, n));
4770  return area;
4771 }
4772 
4780 static bool is_bad_uv_poly(BevVert *bv, BMFace *frep)
4781 {
4782  float area = interp_poly_area(bv, frep);
4783  return area < BEVEL_EPSILON_BIG;
4784 }
4785 
4801 {
4802  int fcount = 0;
4803  BMFace *any_bmf = NULL;
4804  bool consider_all_faces = bv->selcount == 1;
4805  /* Make an array that can hold maximum possible number of choices. */
4806  BMFace **fchoices = BLI_array_alloca(fchoices, bv->edgecount);
4807  for (int i = 0; i < bv->edgecount; i++) {
4808  if (!bv->edges[i].is_bev && !consider_all_faces) {
4809  continue;
4810  }
4811  BMFace *bmf1 = bv->edges[i].fprev;
4812  BMFace *bmf2 = bv->edges[i].fnext;
4813  BMFace *ftwo[2] = {bmf1, bmf2};
4814  BMFace *bmf = choose_rep_face(bp, ftwo, 2);
4815  if (bmf != NULL) {
4816  if (any_bmf == NULL) {
4817  any_bmf = bmf;
4818  }
4819  bool already_there = false;
4820  for (int j = fcount - 1; j >= 0; j--) {
4821  if (fchoices[j] == bmf) {
4822  already_there = true;
4823  break;
4824  }
4825  }
4826  if (!already_there) {
4827  if (bp->math_layer_info.has_math_layers) {
4828  if (is_bad_uv_poly(bv, bmf)) {
4829  continue;
4830  }
4831  }
4832  fchoices[fcount++] = bmf;
4833  }
4834  }
4835  }
4836  if (fcount == 0) {
4837  return any_bmf;
4838  }
4839  return choose_rep_face(bp, fchoices, fcount);
4840 }
4841 
4842 static void build_center_ngon(BevelParams *bp, BMesh *bm, BevVert *bv, int mat_nr)
4843 {
4844  VMesh *vm = bv->vmesh;
4845  BMVert **vv = NULL;
4846  BMFace **vf = NULL;
4847  BMEdge **ve = NULL;
4851 
4852  int ns2 = vm->seg / 2;
4853  BMFace *frep;
4854  BMEdge *frep_e1, *frep_e2;
4855  if (bv->any_seam) {
4856  frep = frep_for_center_poly(bp, bv);
4857  get_incident_edges(frep, bv->v, &frep_e1, &frep_e2);
4858  }
4859  else {
4860  frep = NULL;
4861  frep_e1 = frep_e2 = NULL;
4862  }
4863  BoundVert *v = vm->boundstart;
4864  do {
4865  int i = v->index;
4866  BLI_array_append(vv, mesh_vert(vm, i, ns2, ns2)->v);
4867  if (frep) {
4868  BLI_array_append(vf, frep);
4869  BMEdge *frep_e = find_closer_edge(mesh_vert(vm, i, ns2, ns2)->v->co, frep_e1, frep_e2);
4870  BLI_array_append(ve, v == vm->boundstart ? NULL : frep_e);
4871  }
4872  else {
4874  BLI_array_append(ve, NULL);
4875  }
4876  } while ((v = v->next) != vm->boundstart);
4877  BMFace *f = bev_create_ngon(bm, vv, BLI_array_len(vv), vf, frep, ve, mat_nr, true);
4878  record_face_kind(bp, f, F_VERT);
4879 
4880  BLI_array_free(vv);
4881  BLI_array_free(vf);
4882  BLI_array_free(ve);
4883 }
4884 
4892 {
4893  VMesh *vm = bv->vmesh;
4894  int n = vm->count;
4895  int ns = vm->seg;
4896  int ns2 = ns / 2;
4897  int odd = ns % 2;
4898 
4899  for (int i = 0; i < n; i++) {
4900  for (int k = 1; k < ns; k++) {
4901  copy_v3_v3(mesh_vert(vm, i, 0, k)->co, mesh_vert(vm1, i, 0, k)->co);
4902  if (i > 0 && k <= ns2) {
4903  mesh_vert(vm, i, 0, k)->v = mesh_vert(vm, i - 1, 0, ns - k)->v;
4904  }
4905  else if (i == n - 1 && k > ns2) {
4906  mesh_vert(vm, i, 0, k)->v = mesh_vert(vm, 0, 0, ns - k)->v;
4907  }
4908  else {
4909  create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
4910  }
4911  }
4912  }
4913  if (odd) {
4914  for (int i = 0; i < n; i++) {
4915  mesh_vert(vm, i, ns2, ns2)->v = mesh_vert(vm, i, 0, ns2)->v;
4916  }
4917  build_center_ngon(bp, bm, bv, bp->mat_nr);
4918  }
4919 }
4920 
4924 static void closer_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float v[3])
4925 {
4926  if (len_squared_v3v3(a, v) <= len_squared_v3v3(b, v)) {
4927  copy_v3_v3(r, a);
4928  }
4929  else {
4930  copy_v3_v3(r, b);
4931  }
4932 }
4933 
4947 {
4948  int n_bndv = bv->vmesh->count;
4949  int ns = bv->vmesh->seg;
4950  int ns2 = ns / 2;
4951  int odd = ns % 2;
4952  float ns2inv = 1.0f / (float)ns2;
4953  VMesh *vm = new_adj_vmesh(bp->mem_arena, n_bndv, ns, bv->vmesh->boundstart);
4954  int clstride = 3 * (ns2 + 1);
4955  float *centerline = MEM_mallocN(sizeof(float) * clstride * n_bndv, "bevel");
4956  bool *cset = MEM_callocN(sizeof(bool) * n_bndv, "bevel");
4957 
4958  /* Find on_edge, place on bndv[i]'s elast where offset line would meet,
4959  * taking min-distance-to bv->v with position where next sector's offset line would meet. */
4960  BoundVert *bndv = vm->boundstart;
4961  for (int i = 0; i < n_bndv; i++) {
4962  float bndco[3];
4963  copy_v3_v3(bndco, bndv->nv.co);
4964  EdgeHalf *e1 = bndv->efirst;
4965  EdgeHalf *e2 = bndv->elast;
4966  AngleKind ang_kind = ANGLE_STRAIGHT;
4967  if (e1 && e2) {
4968  ang_kind = edges_angle_kind(e1, e2, bv->v);
4969  }
4970  if (bndv->is_patch_start) {
4971  mid_v3_v3v3(centerline + clstride * i, bndv->nv.co, bndv->next->nv.co);
4972  cset[i] = true;
4973  bndv = bndv->next;
4974  i++;
4975  mid_v3_v3v3(centerline + clstride * i, bndv->nv.co, bndv->next->nv.co);
4976  cset[i] = true;
4977  bndv = bndv->next;
4978  i++;
4979  /* Leave cset[i] where it was - probably false, unless i == n - 1. */
4980  }
4981  else if (bndv->is_arc_start) {
4982  e1 = bndv->efirst;
4983  e2 = bndv->next->efirst;
4984  copy_v3_v3(centerline + clstride * i, bndv->profile.middle);
4985  bndv = bndv->next;
4986  cset[i] = true;
4987  i++;
4988  /* Leave cset[i] where it was - probably false, unless i == n - 1. */
4989  }
4990  else if (ang_kind == ANGLE_SMALLER) {
4991  float dir1[3], dir2[3], co1[3], co2[3];
4992  sub_v3_v3v3(dir1, e1->e->v1->co, e1->e->v2->co);
4993  sub_v3_v3v3(dir2, e2->e->v1->co, e2->e->v2->co);
4994  add_v3_v3v3(co1, bndco, dir1);
4995  add_v3_v3v3(co2, bndco, dir2);
4996  /* Intersect e1 with line through bndv parallel to e2 to get v1co. */
4997  float meet1[3], meet2[3];
4998  int ikind = isect_line_line_v3(e1->e->v1->co, e1->e->v2->co, bndco, co2, meet1, meet2);
4999  float v1co[3];
5000  bool v1set;
5001  if (ikind == 0) {
5002  v1set = false;
5003  }
5004  else {
5005  /* If the lines are skew (ikind == 2), want meet1 which is on e1. */
5006  copy_v3_v3(v1co, meet1);
5007  v1set = true;
5008  }
5009  /* Intersect e2 with line through bndv parallel to e1 to get v2co. */
5010  ikind = isect_line_line_v3(e2->e->v1->co, e2->e->v2->co, bndco, co1, meet1, meet2);
5011  float v2co[3];
5012  bool v2set;
5013  if (ikind == 0) {
5014  v2set = false;
5015  }
5016  else {
5017  v2set = true;
5018  copy_v3_v3(v2co, meet1);
5019  }
5020 
5021  /* We want on_edge[i] to be min dist to bv->v of v2co and the v1co of next iteration. */
5022  float *on_edge_cur = centerline + clstride * i;
5023  int iprev = (i == 0) ? n_bndv - 1 : i - 1;
5024  float *on_edge_prev = centerline + clstride * iprev;
5025  if (v2set) {
5026  if (cset[i]) {
5027  closer_v3_v3v3v3(on_edge_cur, on_edge_cur, v2co, bv->v->co);
5028  }
5029  else {
5030  copy_v3_v3(on_edge_cur, v2co);
5031  cset[i] = true;
5032  }
5033  }
5034  if (v1set) {
5035  if (cset[iprev]) {
5036  closer_v3_v3v3v3(on_edge_prev, on_edge_prev, v1co, bv->v->co);
5037  }
5038  else {
5039  copy_v3_v3(on_edge_prev, v1co);
5040  cset[iprev] = true;
5041  }
5042  }
5043  }
5044  bndv = bndv->next;
5045  }
5046  /* Maybe not everything was set by the previous loop. */
5047  bndv = vm->boundstart;
5048  for (int i = 0; i < n_bndv; i++) {
5049  if (!cset[i]) {
5050  float *on_edge_cur = centerline + clstride * i;
5051  EdgeHalf *e1 = bndv->next->efirst;
5052  float co1[3], co2[3];
5053  copy_v3_v3(co1, bndv->nv.co);
5054  copy_v3_v3(co2, bndv->next->nv.co);
5055  if (e1) {
5056  if (bndv->prev->is_arc_start && bndv->next->is_arc_start) {
5057  float meet1[3], meet2[3];
5058  int ikind = isect_line_line_v3(e1->e->v1->co, e1->e->v2->co, co1, co2, meet1, meet2);
5059  if (ikind != 0) {
5060  copy_v3_v3(on_edge_cur, meet1);
5061  cset[i] = true;
5062  }
5063  }
5064  else {
5065  if (bndv->prev->is_arc_start) {
5066  closest_to_line_segment_v3(on_edge_cur, co1, e1->e->v1->co, e1->e->v2->co);
5067  }
5068  else {
5069  closest_to_line_segment_v3(on_edge_cur, co2, e1->e->v1->co, e1->e->v2->co);
5070  }
5071  cset[i] = true;
5072  }
5073  }
5074  if (!cset[i]) {
5075  mid_v3_v3v3(on_edge_cur, co1, co2);
5076  cset[i] = true;
5077  }
5078  }
5079  bndv = bndv->next;
5080  }
5081 
5082  /* Fill in rest of center-lines by interpolation. */
5083  float co1[3], co2[3];
5084  copy_v3_v3(co2, bv->v->co);
5085  bndv = vm->boundstart;
5086  for (int i = 0; i < n_bndv; i++) {
5087  if (odd) {
5088  float ang = 0.5f * angle_v3v3v3(bndv->nv.co, co1, bndv->next->nv.co);
5089  float finalfrac;
5090  if (ang > BEVEL_SMALL_ANG) {
5091  /* finalfrac is the length along arms of isosceles triangle with top angle 2*ang
5092  * such that the base of the triangle is 1.
5093  * This is used in interpolation along center-line in odd case.
5094  * To avoid too big a drop from bv, cap finalfrac a 0.8 arbitrarily */
5095  finalfrac = 0.5f / sinf(ang);
5096  if (finalfrac > 0.8f) {
5097  finalfrac = 0.8f;
5098  }
5099  }
5100  else {
5101  finalfrac = 0.8f;
5102  }
5103  ns2inv = 1.0f / (ns2 + finalfrac);
5104  }
5105 
5106  float *p = centerline + clstride * i;
5107  copy_v3_v3(co1, p);
5108  p += 3;
5109  for (int j = 1; j <= ns2; j++) {
5110  interp_v3_v3v3(p, co1, co2, j * ns2inv);
5111  p += 3;
5112  }
5113  bndv = bndv->next;
5114  }
5115 
5116  /* Coords of edges and mid or near-mid line. */
5117  bndv = vm->boundstart;
5118  for (int i = 0; i < n_bndv; i++) {
5119  copy_v3_v3(co1, bndv->nv.co);
5120  copy_v3_v3(co2, centerline + clstride * (i == 0 ? n_bndv - 1 : i - 1));
5121  for (int j = 0; j < ns2 + odd; j++) {
5122  interp_v3_v3v3(mesh_vert(vm, i, j, 0)->co, co1, co2, j * ns2inv);
5123  }
5124  copy_v3_v3(co2, centerline + clstride * i);
5125  for (int k = 1; k <= ns2; k++) {
5126  interp_v3_v3v3(mesh_vert(vm, i, 0, k)->co, co1, co2, k * ns2inv);
5127  }
5128  bndv = bndv->next;
5129  }
5130  if (!odd) {
5131  copy_v3_v3(mesh_vert(vm, 0, ns2, ns2)->co, bv->v->co);
5132  }
5134 
5135  /* Fill in interior points by interpolation from edges to center-lines. */
5136  bndv = vm->boundstart;
5137  for (int i = 0; i < n_bndv; i++) {
5138  int im1 = (i == 0) ? n_bndv - 1 : i - 1;
5139  for (int j = 1; j < ns2 + odd; j++) {
5140  for (int k = 1; k <= ns2; k++) {
5141  float meet1[3], meet2[3];
5142  int ikind = isect_line_line_v3(mesh_vert(vm, i, 0, k)->co,
5143  centerline + clstride * im1 + 3 * k,
5144  mesh_vert(vm, i, j, 0)->co,
5145  centerline + clstride * i + 3 * j,
5146  meet1,
5147  meet2);
5148  if (ikind == 0) {
5149  /* How can this happen? fall back on interpolation in one direction if it does. */
5150  interp_v3_v3v3(mesh_vert(vm, i, j, k)->co,
5151  mesh_vert(vm, i, 0, k)->co,
5152  centerline + clstride * im1 + 3 * k,
5153  j * ns2inv);
5154  }
5155  else if (ikind == 1) {
5156  copy_v3_v3(mesh_vert(vm, i, j, k)->co, meet1);
5157  }
5158  else {
5159  mid_v3_v3v3(mesh_vert(vm, i, j, k)->co, meet1, meet2);
5160  }
5161  }
5162  }
5163  bndv = bndv->next;
5164  }
5165 
5167 
5168  MEM_freeN(centerline);
5169  MEM_freeN(cset);
5170  return vm;
5171 }
5172 
5178 static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv, BoundVert *vpipe)
5179 {
5180  int mat_nr = bp->mat_nr;
5181 
5182  int n_bndv = bv->vmesh->count;
5183  int ns = bv->vmesh->seg;
5184  int ns2 = ns / 2;
5185  int odd = ns % 2;
5186  BLI_assert(n_bndv >= 3 && ns > 1);
5187 
5188  VMesh *vm1;
5189  if (bp->pro_super_r == PRO_SQUARE_R && bv->selcount >= 3 && !odd &&
5191  vm1 = square_out_adj_vmesh(bp, bv);
5192  }
5193  else if (vpipe) {
5194  vm1 = pipe_adj_vmesh(bp, bv, vpipe);
5195  }
5196  else if (tri_corner_test(bp, bv) == 1) {
5197  vm1 = tri_corner_adj_vmesh(bp, bv);
5198  /* The PRO_SQUARE_IN_R profile has boundary edges that merge
5199  * and no internal ring polys except possibly center ngon. */
5201  build_square_in_vmesh(bp, bm, bv, vm1);
5202  return;
5203  }
5204  }
5205  else {
5206  vm1 = adj_vmesh(bp, bv);
5207  }
5208 
5209  /* Copy final vmesh into bv->vmesh, make BMVerts and BMFaces. */
5210  VMesh *vm = bv->vmesh;
5211  for (int i = 0; i < n_bndv; i++) {
5212  for (int j = 0; j <= ns2; j++) {
5213  for (int k = 0; k <= ns; k++) {
5214  if (j == 0 && (k == 0 || k == ns)) {
5215  continue; /* Boundary corners already made. */
5216  }
5217  if (!is_canon(vm, i, j, k)) {
5218  continue;
5219  }
5220  copy_v3_v3(mesh_vert(vm, i, j, k)->co, mesh_vert(vm1, i, j, k)->co);
5221  create_mesh_bmvert(bm, vm, i, j, k, bv->v);
5222  }
5223  }
5224  }
5226  /* Make the polygons. */
5227  BoundVert *bndv = vm->boundstart;
5228  do {
5229  int i = bndv->index;
5230  BMFace *f = boundvert_rep_face(bndv, NULL);
5231  BMFace *f2 = boundvert_rep_face(bndv->next, NULL);
5232  BMFace *fchoices[2] = {f, f2};
5233  BMFace *fc = odd ? choose_rep_face(bp, fchoices, 2) : NULL;
5234 
5235  EdgeHalf *e = (bp->affect_type == BEVEL_AFFECT_VERTICES) ? bndv->efirst : bndv->ebev;
5236  BMEdge *bme = e ? e->e : NULL;
5237  /* For odd ns, make polys with lower left corner at (i,j,k) for
5238  * j in [0, ns2-1], k in [0, ns2]. And then the center ngon.
5239  * For even ns,
5240  * j in [0, ns2-1], k in [0, ns2-1].
5241  *
5242  * Recall: j is ring index, k is segment index.
5243  */
5244  for (int j = 0; j < ns2; j++) {
5245  for (int k = 0; k < ns2 + odd; k++) {
5246  BMVert *bmv1 = mesh_vert(vm, i, j, k)->v;
5247  BMVert *bmv2 = mesh_vert(vm, i, j, k + 1)->v;
5248  BMVert *bmv3 = mesh_vert(vm, i, j + 1, k + 1)->v;
5249  BMVert *bmv4 = mesh_vert(vm, i, j + 1, k)->v;
5250  BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
5251  BMFace *r_f;
5252  if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
5253  if (j < k) {
5254  if (k == ns2 && j == ns2 - 1) {
5255  r_f = bev_create_quad_ex(bm,
5256  bmv1,
5257  bmv2,
5258  bmv3,
5259  bmv4,
5260  f2,
5261  f2,
5262  f2,
5263  f2,
5264  NULL,
5265  NULL,
5266  bndv->next->efirst->e,
5267  bme,
5268  f2,
5269  mat_nr);
5270  }
5271  else {
5272  r_f = bev_create_quad(bm, bmv1, bmv2, bmv3, bmv4, f2, f2, f2, f2, mat_nr);
5273  }
5274  }
5275  else if (j > k) {
5276  r_f = bev_create_quad(bm, bmv1, bmv2, bmv3, bmv4, f2, f2, f2, f2, mat_nr);
5277  }
5278  else { /* j == k */
5279  /* Only one edge attached to v, since vertex only. */
5280  if (e->is_seam) {
5281  r_f = bev_create_quad_ex(
5282  bm, bmv1, bmv2, bmv3, bmv4, f2, f2, f2, f2, bme, NULL, bme, NULL, f2, mat_nr);
5283  }
5284  else {
5285  r_f = bev_create_quad_ex(
5286  bm, bmv1, bmv2, bmv3, bmv4, f2, f2, f2, f, bme, NULL, bme, NULL, f2, mat_nr);
5287  }
5288  }
5289  }
5290  else { /* Edge bevel. */
5291  if (odd) {
5292  if (k == ns2) {
5293  if (e && e->is_seam) {
5294  r_f = bev_create_quad_ex(
5295  bm, bmv1, bmv2, bmv3, bmv4, fc, fc, fc, fc, NULL, bme, bme, NULL, fc, mat_nr);
5296  }
5297  else {
5298  r_f = bev_create_quad_ex(
5299  bm, bmv1, bmv2, bmv3, bmv4, f, f2, f2, f, NULL, bme, bme, NULL, fc, mat_nr);
5300  }
5301  }
5302  else {
5303  r_f = bev_create_quad(bm, bmv1, bmv2, bmv3, bmv4, f, f, f, f, mat_nr);
5304  }
5305  }
5306  else {
5307  BMEdge *bme1 = k == ns2 - 1 ? bme : NULL;
5308  BMEdge *bme3 = NULL;
5309  if (j == ns2 - 1 && bndv->prev->ebev) {
5310  bme3 = bndv->prev->ebev->e;
5311  }
5312  BMEdge *bme2 = bme1 != NULL ? bme1 : bme3;
5313  r_f = bev_create_quad_ex(
5314  bm, bmv1, bmv2, bmv3, bmv4, f, f, f, f, NULL, bme1, bme2, bme3, f, mat_nr);
5315  }
5316  }
5317  record_face_kind(bp, r_f, F_VERT);
5318  }
5319  }
5320  } while ((bndv = bndv->next) != vm->boundstart);
5321 
5322  /* Fix UVs along center lines if even number of segments. */
5323  if (!odd) {
5324  bndv = vm->boundstart;
5325  do {
5326  int i = bndv->index;
5327  if (!bndv->any_seam) {
5328  for (int ring = 1; ring < ns2; ring++) {
5329  BMVert *v_uv = mesh_vert(vm, i, ring, ns2)->v;
5330  if (v_uv) {
5331  bev_merge_uvs(bm, v_uv);
5332  }
5333  }
5334  }
5335  } while ((bndv = bndv->next) != vm->boundstart);
5336  BMVert *bmv = mesh_vert(vm, 0, ns2, ns2)->v;
5337  if (bp->affect_type == BEVEL_AFFECT_VERTICES || count_bound_vert_seams(bv) <= 1) {
5338  bev_merge_uvs(bm, bmv);
5339  }
5340  }
5341 
5342  /* Center ngon. */
5343  if (odd) {
5344  build_center_ngon(bp, bm, bv, mat_nr);
5345  }
5346 }
5347 
5360 {
5361 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5362  printf("BEVEL BUILD CUTOFF\n");
5363 # define F3(v) (v)[0], (v)[1], (v)[2]
5364 #endif
5365  int n_bndv = bv->vmesh->count;
5366 
5367  /* Find the locations for the corner vertices at the bottom of the cutoff faces. */
5368  BoundVert *bndv = bv->vmesh->boundstart;
5369  do {
5370  int i = bndv->index;
5371 
5372  /* Find the "down" direction for this side of the cutoff face. */
5373  /* Find the direction along the intersection of the two adjacent profile normals. */
5374  float down_direction[3];
5375  cross_v3_v3v3(down_direction, bndv->profile.plane_no, bndv->prev->profile.plane_no);
5376  if (dot_v3v3(down_direction, bv->v->no) > 0.0f) {
5377  negate_v3(down_direction);
5378  }
5379 
5380  /* Move down from the boundvert by average profile height from the two adjacent profiles. */
5381  float length = (bndv->profile.height / sqrtf(2.0f) +
5382  bndv->prev->profile.height / sqrtf(2.0f)) /
5383  2;
5384  float new_vert[3];
5385  madd_v3_v3v3fl(new_vert, bndv->nv.co, down_direction, length);
5386 
5387  /* Use this location for this profile's first corner vert and the last profile's second. */
5388  copy_v3_v3(mesh_vert(bv->vmesh, i, 1, 0)->co, new_vert);
5389  copy_v3_v3(mesh_vert(bv->vmesh, bndv->prev->index, 1, 1)->co, new_vert);
5390 
5391  } while ((bndv = bndv->next) != bv->vmesh->boundstart);
5392 
5393 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5394  printf("Corner vertices:\n");
5395  for (int j = 0; j < n_bndv; j++) {
5396  printf(" (%.3f, %.3f, %.3f)\n", F3(mesh_vert(bv->vmesh, j, 1, 0)->co));
5397  }
5398 #endif
5399 
5400  /* Disable the center face if the corner vertices share the same location. */
5401  bool build_center_face = true;
5402  if (n_bndv == 3) { /* Vertices only collapse with a 3-way VMesh. */
5403  build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 0, 1, 0)->co,
5404  mesh_vert(bv->vmesh, 1, 1, 0)->co) > BEVEL_EPSILON;
5405  build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 0, 1, 0)->co,
5406  mesh_vert(bv->vmesh, 2, 1, 0)->co) > BEVEL_EPSILON;
5407  build_center_face &= len_squared_v3v3(mesh_vert(bv->vmesh, 1, 1, 0)->co,
5408  mesh_vert(bv->vmesh, 2, 1, 0)->co) > BEVEL_EPSILON;
5409  }
5410 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5411  printf("build_center_face: %d\n", build_center_face);
5412 #endif
5413 
5414  /* Create the corner vertex BMVerts. */
5415  if (build_center_face) {
5416  do {
5417  int i = bndv->index;
5418  create_mesh_bmvert(bm, bv->vmesh, i, 1, 0, bv->v);
5419  /* The second corner vertex for the previous profile shares this BMVert. */
5420  mesh_vert(bv->vmesh, bndv->prev->index, 1, 1)->v = mesh_vert(bv->vmesh, i, 1, 0)->v;
5421 
5422  } while ((bndv = bndv->next) != bv->vmesh->boundstart);
5423  }
5424  else {
5425  /* Use the same BMVert for all of the corner vertices. */
5426  create_mesh_bmvert(bm, bv->vmesh, 0, 1, 0, bv->v);
5427  for (int i = 1; i < n_bndv; i++) {
5428  mesh_vert(bv->vmesh, i, 1, 0)->v = mesh_vert(bv->vmesh, 0, 1, 0)->v;
5429  }
5430  }
5431 
5432  /* Build the profile cutoff faces. */
5433  /* Extra one or two for corner vertices and one for last point along profile, or the size of the
5434  * center face array if it's bigger. */
5435 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5436  printf("Building profile cutoff faces.\n");
5437 #endif
5438  BMVert **face_bmverts = BLI_memarena_alloc(
5439  bp->mem_arena, sizeof(BMVert *) * max_ii(bp->seg + 2 + build_center_face, n_bndv));
5440  bndv = bv->vmesh->boundstart;
5441  do {
5442  int i = bndv->index;
5443  BMEdge **bmedges = NULL;
5444  BMFace **bmfaces = NULL;
5447 
5448  /* Add the first corner vertex under this boundvert. */
5449  face_bmverts[0] = mesh_vert(bv->vmesh, i, 1, 0)->v;
5450 
5451 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5452  printf("Profile Number %d:\n", i);
5453  if (bndv->is_patch_start || bndv->is_arc_start) {
5454  printf(" Miter profile\n");
5455  }
5456  printf(" Corner 1: (%0.3f, %0.3f, %0.3f)\n", F3(mesh_vert(bv->vmesh, i, 1, 0)->co));
5457 #endif
5458 
5459  /* Add profile point vertices to the face, including the last one. */
5460  for (int k = 0; k < bp->seg + 1; k++) {
5461  face_bmverts[k + 1] = mesh_vert(bv->vmesh, i, 0, k)->v; /* Leave room for first vert. */
5462 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5463  printf(" Profile %d: (%0.3f, %0.3f, %0.3f)\n", k, F3(mesh_vert(bv->vmesh, i, 0, k)->co));
5464 #endif
5465  }
5466 
5467  /* Add the second corner vert to complete the bottom of the face. */
5468  if (build_center_face) {
5469  face_bmverts[bp->seg + 2] = mesh_vert(bv->vmesh, i, 1, 1)->v;
5470 #ifdef DEBUG_CUSTOM_PROFILE_CUTOFF
5471  printf(" Corner 2: (%0.3f, %0.3f, %0.3f)\n", F3(mesh_vert(bv->vmesh, i, 1, 1)->co));
5472 #endif
5473  }
5474 
5475  /* Create the profile cutoff face for this boundvert. */
5476  /* repface = boundvert_rep_face(bndv, NULL); */
5478  face_bmverts,
5479  bp->seg + 2 + build_center_face,
5480  bmfaces,
5481  NULL,
5482  bmedges,
5483  bp->mat_nr,
5484  true);
5485 
5486  BLI_array_free(bmedges);
5487  BLI_array_free(bmfaces);
5488  } while ((bndv = bndv->next) != bv->vmesh->boundstart);
5489 
5490  /* Create the bottom face if it should be built, reusing previous face_bmverts allocation. */
5491  if (build_center_face) {
5492  BMEdge **bmedges = NULL;
5493  BMFace **bmfaces = NULL;
5496 
5497  /* Add all of the corner vertices to this face. */
5498  for (int i = 0; i < n_bndv; i++) {
5499  /* Add verts from each cutoff face. */
5500  face_bmverts[i] = mesh_vert(bv->vmesh, i, 1, 0)->v;
5501  }
5502  /* BLI_array_append(bmfaces, repface); */
5503  bev_create_ngon(bm, face_bmverts, n_bndv, bmfaces, NULL, bmedges, bp->mat_nr, true);
5504 
5505  BLI_array_free(bmedges);
5506  BLI_array_free(bmfaces);
5507  }
5508 }
5509 
5511 {
5512  VMesh *vm = bv->vmesh;
5513  BMVert **bmverts = NULL;
5514  BMEdge **bmedges = NULL;
5515  BMFace **bmfaces = NULL;
5519 
5520  BMFace *repface;
5521  BMEdge *repface_e1, *repface_e2;
5522  if (bv->any_seam) {
5523  repface = frep_for_center_poly(bp, bv);
5524  get_incident_edges(repface, bv->v, &repface_e1, &repface_e2);
5525  }
5526  else {
5527  repface = NULL;
5528  repface_e1 = repface_e2 = NULL;
5529  }
5530  BoundVert *bndv = vm->boundstart;
5531  int n = 0;
5532  do {
5533  /* Accumulate vertices for vertex ngon. */
5534  /* Also accumulate faces in which uv interpolation is to happen for each. */
5535  BLI_array_append(bmverts, bndv->nv.v);
5536  if (repface) {
5537  BLI_array_append(bmfaces, repface);
5538  BMEdge *frep_e = find_closer_edge(bndv->nv.v->co, repface_e1, repface_e2);
5539  BLI_array_append(bmedges, n > 0 ? frep_e : NULL);
5540  }
5541  else {
5542  BLI_array_append(bmfaces, boundvert_rep_face(bndv, NULL));
5543  BLI_array_append(bmedges, NULL);
5544  }
5545  n++;
5546  if (bndv->ebev && bndv->ebev->seg > 1) {
5547  for (int k = 1; k < bndv->ebev->seg; k++) {
5548  BLI_array_append(bmverts, mesh_vert(vm, bndv->index, 0, k)->v);
5549  if (repface) {
5550  BLI_array_append(bmfaces, repface);
5551  BMEdge *frep_e = find_closer_edge(
5552  mesh_vert(vm, bndv->index, 0, k)->v->co, repface_e1, repface_e2);
5553  BLI_array_append(bmedges, k < bndv->ebev->seg / 2 ? NULL : frep_e);
5554  }
5555  else {
5556  BLI_array_append(bmfaces, boundvert_rep_face(bndv, NULL));
5557  BLI_array_append(bmedges, NULL);
5558  }
5559  n++;
5560  }
5561  }
5562  } while ((bndv = bndv->next) != vm->boundstart);
5563 
5564  BMFace *f;
5565  if (n > 2) {
5566  f = bev_create_ngon(bm, bmverts, n, bmfaces, repface, bmedges, bp->mat_nr, true);
5567  record_face_kind(bp, f, F_VERT);
5568  }
5569  else {
5570  f = NULL;
5571  }
5572  BLI_array_free(bmverts);
5573  BLI_array_free(bmedges);
5574  BLI_array_free(bmfaces);
5575  return f;
5576 }
5577 
5579 {
5580  BLI_assert(next_bev(bv, NULL)->seg == 1 || bv->selcount == 1);
5581 
5582  BMFace *f = bevel_build_poly(bp, bm, bv);
5583 
5584  if (f == NULL) {
5585  return;
5586  }
5587 
5588  /* We have a polygon which we know starts at the previous vertex, make it into a fan. */
5589  BMLoop *l_fan = BM_FACE_FIRST_LOOP(f)->prev;
5590  BMVert *v_fan = l_fan->v;
5591 
5592  while (f->len > 3) {
5593  BMLoop *l_new;
5594  BMFace *f_new;
5595  BLI_assert(v_fan == l_fan->v);
5596  f_new = BM_face_split(bm, f, l_fan, l_fan->next->next, &l_new, NULL, false);
5597  flag_out_edge(bm, l_new->e);
5598 
5599  if (f_new->len > f->len) {
5600  f = f_new;
5601  if (l_new->v == v_fan) {
5602  l_fan = l_new;
5603  }
5604  else if (l_new->next->v == v_fan) {
5605  l_fan = l_new->next;
5606  }
5607  else if (l_new->prev->v == v_fan) {
5608  l_fan = l_new->prev;
5609  }
5610  else {
5611  BLI_assert(0);
5612  }
5613  }
5614  else {
5615  if (l_fan->v == v_fan) { /* l_fan = l_fan. */
5616  }
5617  else if (l_fan->next->v == v_fan) {
5618  l_fan = l_fan->next;
5619  }
5620  else if (l_fan->prev->v == v_fan) {
5621  l_fan = l_fan->prev;
5622  }
5623  else {
5624  BLI_assert(0);
5625  }
5626  }
5627  record_face_kind(bp, f_new, F_VERT);
5628  }
5629 }
5630 
5631 /* Special case: vertex bevel with only two boundary verts.
5632  * Want to make a curved edge if seg > 0.
5633  * If there are no faces in the original mesh at the original vertex,
5634  * there will be no rebuilt face to make the edge between the boundary verts,
5635  * we have to make it here. */
5637 {
5638  VMesh *vm = bv->vmesh;
5639 
5640  BLI_assert(vm->count == 2 && bp->affect_type == BEVEL_AFFECT_VERTICES);
5641 
5642  BMVert *v1 = mesh_vert(vm, 0, 0, 0)->v;
5643  BMVert *v2 = mesh_vert(vm, 1, 0, 0)->v;
5644 
5645  int ns = vm->seg;
5646  if (ns > 1) {
5647  /* Set up profile parameters. */
5648  BoundVert *bndv = vm->boundstart;
5649  Profile *pro = &bndv->profile;
5650  pro->super_r = bp->pro_super_r;
5651  copy_v3_v3(pro->start, v1->co);
5652  copy_v3_v3(pro->end, v2->co);
5653  copy_v3_v3(pro->middle, bv->v->co);
5654  /* Don't use projection. */
5655  zero_v3(pro->plane_co);
5656  zero_v3(pro->plane_no);
5657  zero_v3(pro->proj_dir);
5658 
5659  for (int k = 1; k < ns; k++) {
5660  float co[3];
5661  get_profile_point(bp, pro, k, ns, co);
5662  copy_v3_v3(mesh_vert(vm, 0, 0, k)->co, co);
5663  create_mesh_bmvert(bm, vm, 0, 0, k, bv->v);
5664  }
5665  copy_v3_v3(mesh_vert(vm, 0, 0, ns)->co, v2->co);
5666  for (int k = 1; k < ns; k++) {
5667  copy_mesh_vert(vm, 1, 0, ns - k, 0, 0, k);
5668  }
5669  }
5670 
5671  if (BM_vert_face_check(bv->v) == false) {
5672  BMEdge *e_eg = bv->edges[0].e;
5673  BLI_assert(e_eg != NULL);
5674  for (int k = 0; k < ns; k++) {
5675  v1 = mesh_vert(vm, 0, 0, k)->v;
5676  v2 = mesh_vert(vm, 0, 0, k + 1)->v;
5677  BLI_assert(v1 != NULL && v2 != NULL);
5678  BMEdge *bme = BM_edge_create(bm, v1, v2, e_eg, BM_CREATE_NO_DOUBLE);
5679  if (bme) {
5680  flag_out_edge(bm, bme);
5681  }
5682  }
5683  }
5684 }
5685 
5686 /* Given that the boundary is built, now make the actual BMVerts
5687  * for the boundary and the interior of the vertex mesh. */
5688 static void build_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv)
5689 {
5690  VMesh *vm = bv->vmesh;
5691  float co[3];
5692 
5693  int n = vm->count;
5694  int ns = vm->seg;
5695  int ns2 = ns / 2;
5696 
5698  sizeof(NewVert) * n * (ns2 + 1) * (ns + 1));
5699 
5700  /* Special case: just two beveled edges welded together. */
5701  const bool weld = (bv->selcount == 2) && (vm->count == 2);
5702  BoundVert *weld1 = NULL; /* Will hold two BoundVerts involved in weld. */
5703  BoundVert *weld2 = NULL;
5704 
5705  /* Make (i, 0, 0) mesh verts for all i boundverts. */
5706  BoundVert *bndv = vm->boundstart;
5707  do {
5708  int i = bndv->index;
5709  copy_v3_v3(mesh_vert(vm, i, 0, 0)->co, bndv->nv.co); /* Mesh NewVert to boundary NewVert. */
5710  create_mesh_bmvert(bm, vm, i, 0, 0, bv->v); /* Create BMVert for that NewVert. */
5711  bndv->nv.v = mesh_vert(vm, i, 0, 0)->v; /* Use the BMVert for the BoundVert's NewVert. */
5712 
5713  /* Find boundverts and move profile planes if this is a weld case. */
5714  if (weld && bndv->ebev) {
5715  if (!weld1) {
5716  weld1 = bndv;
5717  }
5718  else { /* Get the last of the two BoundVerts. */
5719  weld2 = bndv;
5720  set_profile_params(bp, bv, weld1);
5721  set_profile_params(bp, bv, weld2);
5722  move_weld_profile_planes(bv, weld1, weld2);
5723  }
5724  }
5725  } while ((bndv = bndv->next) != vm->boundstart);
5726 
5727  /* It's simpler to calculate all profiles only once at a single moment, so keep just a single
5728  * profile calculation here, the last point before actual mesh verts are created. */
5729  calculate_vm_profiles(bp, bv, vm);
5730 
5731  /* Create new vertices and place them based on the profiles. */
5732  /* Copy other ends to (i, 0, ns) for all i, and fill in profiles for edges. */
5733  bndv = vm->boundstart;
5734  do {
5735  int i = bndv->index;
5736  /* bndv's last vert along the boundary arc is the first of the next BoundVert's arc. */
5737  copy_mesh_vert(vm, i, 0, ns, bndv->next->index, 0, 0);
5738 
5739  if (vm->mesh_kind != M_ADJ) {
5740  for (int k = 1; k < ns; k++) {
5741  if (bndv->ebev) {
5742  get_profile_point(bp, &bndv->profile, k, ns, co);
5743  copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
5744  if (!weld) {
5745  /* This is done later with (possibly) better positions for the weld case. */
5746  create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
5747  }
5748  }
5749  else if (n == 2 && !bndv->ebev) {
5750  /* case of one edge beveled and this is the v without ebev */
5751  /* want to copy the verts from other v, in reverse order */
5752  copy_mesh_vert(bv->vmesh, i, 0, k, 1 - i, 0, ns - k);
5753  }
5754  }
5755  }
5756  } while ((bndv = bndv->next) != vm->boundstart);
5757 
5758  /* Build the profile for the weld case (just a connection between the two boundverts). */
5759  if (weld) {
5760  bv->vmesh->mesh_kind = M_NONE;
5761  for (int k = 1; k < ns; k++) {
5762  float *v_weld1 = mesh_vert(bv->vmesh, weld1->index, 0, k)->co;
5763  float *v_weld2 = mesh_vert(bv->vmesh, weld2->index, 0, ns - k)->co;
5764  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
5765  /* Don't bother with special case profile check from below. */
5766  mid_v3_v3v3(co, v_weld1, v_weld2);
5767  }
5768  else {
5769  /* Use the point from the other profile if one is in a special case. */
5770  if (weld1->profile.super_r == PRO_LINE_R && weld2->profile.super_r != PRO_LINE_R) {
5771  copy_v3_v3(co, v_weld2);
5772  }
5773  else if (weld2->profile.super_r == PRO_LINE_R && weld1->profile.super_r != PRO_LINE_R) {
5774  copy_v3_v3(co, v_weld1);
5775  }
5776  else {
5777  /* In case the profiles aren't snapped to the same plane, use their midpoint. */
5778  mid_v3_v3v3(co, v_weld1, v_weld2);
5779  }
5780  }
5781  copy_v3_v3(mesh_vert(bv->vmesh, weld1->index, 0, k)->co, co);
5782  create_mesh_bmvert(bm, bv->vmesh, weld1->index, 0, k, bv->v);
5783  }
5784  for (int k = 1; k < ns; k++) {
5785  copy_mesh_vert(bv->vmesh, weld2->index, 0, ns - k, weld1->index, 0, k);
5786  }
5787  }
5788 
5789  /* Make sure the pipe case ADJ mesh is used for both the "Grid Fill" (ADJ) and cutoff options. */
5790  BoundVert *vpipe = NULL;
5791  if ((vm->count == 3 || vm->count == 4) && bp->seg > 1) {
5792  /* Result is passed to bevel_build_rings to avoid overhead. */
5793  vpipe = pipe_test(bv);
5794  if (vpipe) {
5795  vm->mesh_kind = M_ADJ;
5796  }
5797  }
5798 
5799  switch (vm->mesh_kind) {
5800  case M_NONE:
5801  if (n == 2 && bp->affect_type == BEVEL_AFFECT_VERTICES) {
5802  bevel_vert_two_edges(bp, bm, bv);
5803  }
5804  break;
5805  case M_POLY:
5806  bevel_build_poly(bp, bm, bv);
5807  break;
5808  case M_ADJ:
5809  bevel_build_rings(bp, bm, bv, vpipe);
5810  break;
5811  case M_TRI_FAN:
5812  bevel_build_trifan(bp, bm, bv);
5813  break;
5814  case M_CUTOFF:
5815  bevel_build_cutoff(bp, bm, bv);
5816  }
5817 }
5818 
5819 /* Return the angle between the two faces adjacent to e.
5820  * If there are not two, return 0. */
5822 {
5823  if (e->fprev && e->fnext) {
5824  /* Angle between faces is supplement of angle between face normals. */
5825  return (float)M_PI - angle_normalized_v3v3(e->fprev->no, e->fnext->no);
5826  }
5827  return 0.0f;
5828 }
5829 
5830 /* Take care, this flag isn't cleared before use, it just so happens that its not set. */
5831 #define BM_BEVEL_EDGE_TAG_ENABLE(bme) BM_ELEM_API_FLAG_ENABLE((bme), _FLAG_OVERLAP)
5832 #define BM_BEVEL_EDGE_TAG_DISABLE(bme) BM_ELEM_API_FLAG_DISABLE((bme), _FLAG_OVERLAP)
5833 #define BM_BEVEL_EDGE_TAG_TEST(bme) BM_ELEM_API_FLAG_TEST((bme), _FLAG_OVERLAP)
5834 
5844 static int bevel_edge_order_extend(BMesh *bm, BevVert *bv, int i)
5845 {
5846  BMEdge **sucs = NULL;
5847  BMEdge **save_path = NULL;
5848  BLI_array_staticdeclare(sucs, 4); /* Likely very few faces attached to same edge. */
5850 
5851  /* Fill sucs with all unmarked edges of bmesh. */
5852  BMEdge *bme = bv->edges[i].e;
5853  BMIter iter;
5854  BMLoop *l;
5855  BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
5856  BMEdge *bme2 = (l->v == bv->v) ? l->prev->e : l->next->e;
5857  if (!BM_BEVEL_EDGE_TAG_TEST(bme2)) {
5858  BLI_array_append(sucs, bme2);
5859  }
5860  }
5861  int nsucs = BLI_array_len(sucs);
5862 
5863  int bestj = i;
5864  int j = i;
5865  for (int sucindex = 0; sucindex < nsucs; sucindex++) {
5866  BMEdge *nextbme = sucs[sucindex];
5867  BLI_assert(nextbme != NULL);
5869  BLI_assert(j + 1 < bv->edgecount);
5870  bv->edges[j + 1].e = nextbme;
5871  BM_BEVEL_EDGE_TAG_ENABLE(nextbme);
5872  int tryj = bevel_edge_order_extend(bm, bv, j + 1);
5873  if (tryj > bestj ||
5874  (tryj == bestj && edges_face_connected_at_vert(bv->edges[tryj].e, bv->edges[0].e))) {
5875  bestj = tryj;
5876  BLI_array_clear(save_path);
5877  for (int k = j + 1; k <= bestj; k++) {
5878  BLI_array_append(save_path, bv->edges[k].e);
5879  }
5880  }
5881  /* Now reset to path only-going-to-j state. */
5882  for (int k = j + 1; k <= tryj; k++) {
5884  bv->edges[k].e = NULL;
5885  }
5886  }
5887  /* At this point we should be back at invariant on entrance: path up to j. */
5888  if (bestj > j) {
5889  /* Save_path should have from j + 1 to bestj inclusive.
5890  * Edges to add to edges[] before returning. */
5891  for (int k = j + 1; k <= bestj; k++) {
5892  BLI_assert(save_path[k - (j + 1)] != NULL);
5893  bv->edges[k].e = save_path[k - (j + 1)];
5895  }
5896  }
5897  BLI_array_free(sucs);
5898  BLI_array_free(save_path);
5899  return bestj;
5900 }
5901 
5902 /* See if we have usual case for bevel edge order:
5903  * there is an ordering such that all the faces are between
5904  * successive edges and form a manifold "cap" at bv.
5905  * If this is the case, set bv->edges to such an order
5906  * and return true; else return unmark any partial path and return false.
5907  * Assume the first edge is already in bv->edges[0].e and it is tagged. */
5908 #ifdef FASTER_FASTORDER
5909 /* The alternative older code is O(n^2) where n = # of edges incident to bv->v.
5910  * This implementation is O(n * m) where m = average number of faces attached to an edge incident
5911  * to bv->v, which is almost certainly a small constant except in very strange cases.
5912  * But this code produces different choices of ordering than the legacy system,
5913  * leading to differences in vertex orders etc. in user models,
5914  * so for now will continue to use the legacy code. */
5915 static bool fast_bevel_edge_order(BevVert *bv)
5916 {
5917  for (int j = 1; j < bv->edgecount; j++) {
5918  BMEdge *bme = bv->edges[j - 1].e;
5919  BMEdge *bmenext = NULL;
5920  int nsucs = 0;
5921  BMIter iter;
5922  BMLoop *l;
5923  BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
5924  BMEdge *bme2 = (l->v == bv->v) ? l->prev->e : l->next->e;
5925  if (!BM_BEVEL_EDGE_TAG_TEST(bme2)) {
5926  nsucs++;
5927  if (bmenext == NULL) {
5928  bmenext = bme2;
5929  }
5930  }
5931  }
5932  if (nsucs == 0 || (nsucs == 2 && j != 1) || nsucs > 2 ||
5933  (j == bv->edgecount - 1 && !edges_face_connected_at_vert(bmenext, bv->edges[0].e))) {
5934  for (int k = 1; k < j; k++) {
5936  bv->edges[k].e = NULL;
5937  }
5938  return false;
5939  }
5940  bv->edges[j].e = bmenext;
5941  BM_BEVEL_EDGE_TAG_ENABLE(bmenext);
5942  }
5943  return true;
5944 }
5945 #else
5947 {
5948  int ntot = bv->edgecount;
5949 
5950  /* Add edges to bv->edges in order that keeps adjacent edges sharing
5951  * a unique face, if possible. */
5952  EdgeHalf *e = &bv->edges[0];
5953  BMEdge *bme = e->e;
5954  if (!bme->l) {
5955  return false;
5956  }
5957 
5958  for (int i = 1; i < ntot; i++) {
5959  /* Find an unflagged edge bme2 that shares a face f with previous bme. */
5960  int num_shared_face = 0;
5961  BMEdge *first_suc = NULL; /* Keep track of first successor to match legacy behavior. */
5962  BMIter iter;
5963  BMEdge *bme2;
5964  BM_ITER_ELEM (bme2, &iter, bv->v, BM_EDGES_OF_VERT) {
5965  if (BM_BEVEL_EDGE_TAG_TEST(bme2)) {
5966  continue;
5967  }
5968 
5969  BMIter iter2;
5970  BMFace *f;
5971  BM_ITER_ELEM (f, &iter2, bme2, BM_FACES_OF_EDGE) {
5972  if (BM_face_edge_share_loop(f, bme)) {
5973  num_shared_face++;
5974  if (first_suc == NULL) {
5975  first_suc = bme2;
5976  }
5977  }
5978  }
5979  if (num_shared_face >= 3) {
5980  break;
5981  }
5982  }
5983  if (num_shared_face == 1 || (i == 1 && num_shared_face == 2)) {
5984  e = &bv->edges[i];
5985  e->e = bme = first_suc;
5987  }
5988  else {
5989  for (int k = 1; k < i; k++) {
5991  bv->edges[k].e = NULL;
5992  }
5993  return false;
5994  }
5995  }
5996  return true;
5997 }
5998 #endif
5999 
6000 /* Fill in bv->edges with a good ordering of non-wire edges around bv->v.
6001  * Use only edges where BM_BEVEL_EDGE_TAG is disabled so far (if edge beveling, others are wire).
6002  * first_bme is a good edge to start with. */
6003 static void find_bevel_edge_order(BMesh *bm, BevVert *bv, BMEdge *first_bme)
6004 {
6005  int ntot = bv->edgecount;
6006  for (int i = 0;;) {
6007  BLI_assert(first_bme != NULL);
6008  bv->edges[i].e = first_bme;
6009  BM_BEVEL_EDGE_TAG_ENABLE(first_bme);
6010  if (i == 0 && fast_bevel_edge_order(bv)) {
6011  break;
6012  }
6013  i = bevel_edge_order_extend(bm, bv, i);
6014  i++;
6015  if (i >= bv->edgecount) {
6016  break;
6017  }
6018  /* Not done yet: find a new first_bme. */
6019  first_bme = NULL;
6020  BMIter iter;
6021  BMEdge *bme;
6022  BM_ITER_ELEM (bme, &iter, bv->v, BM_EDGES_OF_VERT) {
6023  if (BM_BEVEL_EDGE_TAG_TEST(bme)) {
6024  continue;
6025  }
6026  if (!first_bme) {
6027  first_bme = bme;
6028  }
6029  if (BM_edge_face_count(bme) == 1) {
6030  first_bme = bme;
6031  break;
6032  }
6033  }
6034  }
6035  /* Now fill in the faces. */
6036  for (int i = 0; i < ntot; i++) {
6037  EdgeHalf *e = &bv->edges[i];
6038  EdgeHalf *e2 = (i == bv->edgecount - 1) ? &bv->edges[0] : &bv->edges[i + 1];
6039  BMEdge *bme = e->e;
6040  BMEdge *bme2 = e2->e;
6041  BLI_assert(bme != NULL);
6042  if (e->fnext != NULL || e2->fprev != NULL) {
6043  continue;
6044  }
6045  /* Which faces have successive loops that are for bme and bme2?
6046  * There could be more than one. E.g., in manifold ntot==2 case.
6047  * Prefer one that has loop in same direction as e. */
6048  BMFace *bestf = NULL;
6049  BMIter iter;
6050  BMLoop *l;
6051  BM_ITER_ELEM (l, &iter, bme, BM_LOOPS_OF_EDGE) {
6052  BMFace *f = l->f;
6053  if ((l->prev->e == bme2 || l->next->e == bme2)) {
6054  if (!bestf || l->v == bv->v) {
6055  bestf = f;
6056  }
6057  }
6058  if (bestf) {
6059  e->fnext = e2->fprev = bestf;
6060  }
6061  }
6062  }
6063 }
6064 
6065 /* Construction around the vertex. */
6067 {
6068  /* Gather input selected edges.
6069  * Only bevel selected edges that have exactly two incident faces.
6070  * Want edges to be ordered so that they share faces.
6071  * There may be one or more chains of shared faces broken by
6072  * gaps where there are no faces.
6073  * Want to ignore wire edges completely for edge beveling.
6074  * TODO: make following work when more than one gap. */
6075 
6076  int nsel = 0;
6077  int tot_edges = 0;
6078  int tot_wire = 0;
6079  BMEdge *first_bme = NULL;
6080  BMIter iter;
6081  BMEdge *bme;
6082  BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
6083  int face_count = BM_edge_face_count(bme);
6086  BLI_assert(face_count == 2);
6087  nsel++;
6088  if (!first_bme) {
6089  first_bme = bme;
6090  }
6091  }
6092  if (face_count == 1) {
6093  /* Good to start face chain from this edge. */
6094  first_bme = bme;
6095  }
6096  if (face_count > 0 || bp->affect_type == BEVEL_AFFECT_VERTICES) {
6097  tot_edges++;
6098  }
6099  if (BM_edge_is_wire(bme)) {
6100  tot_wire++;
6101  /* If edge beveling, exclude wire edges from edges array.
6102  * Mark this edge as "chosen" so loop below won't choose it. */
6103  if (bp->affect_type != BEVEL_AFFECT_VERTICES) {
6105  }
6106  }
6107  }
6108  if (!first_bme) {
6109  first_bme = v->e;
6110  }
6111 
6112  if ((nsel == 0 && bp->affect_type != BEVEL_AFFECT_VERTICES) ||
6113  (tot_edges < 2 && bp->affect_type == BEVEL_AFFECT_VERTICES)) {
6114  /* Signal this vert isn't being beveled. */
6116  return NULL;
6117  }
6118 
6119  BevVert *bv = (BevVert *)BLI_memarena_alloc(bp->mem_arena, sizeof(BevVert));
6120  bv->v = v;
6121  bv->edgecount = tot_edges;
6122  bv->selcount = nsel;
6123  bv->wirecount = tot_wire;
6124  bv->offset = bp->offset;
6125  bv->edges = (EdgeHalf *)BLI_memarena_alloc(bp->mem_arena, sizeof(EdgeHalf) * tot_edges);
6126  if (tot_wire) {
6127  bv->wire_edges = (BMEdge **)BLI_memarena_alloc(bp->mem_arena, sizeof(BMEdge *) * tot_wire);
6128  }
6129  else {
6130  bv->wire_edges = NULL;
6131  }
6132  bv->vmesh = (VMesh *)BLI_memarena_alloc(bp->mem_arena, sizeof(VMesh));
6133  bv->vmesh->seg = bp->seg;
6134 
6135  BLI_ghash_insert(bp->vert_hash, v, bv);
6136 
6137  find_bevel_edge_order(bm, bv, first_bme);
6138 
6139  /* Fill in other attributes of EdgeHalfs. */
6140  for (int i = 0; i < tot_edges; i++) {
6141  EdgeHalf *e = &bv->edges[i];
6142  bme = e->e;
6144  e->is_bev = true;
6145  e->seg = bp->seg;
6146  }
6147  else {
6148  e->is_bev = false;
6149  e->seg = 0;
6150  }
6151  e->is_rev = (bme->v2 == v);
6152  e->leftv = e->rightv = NULL;
6153  e->profile_index = 0;
6154  }
6155 
6156  /* Now done with tag flag. */
6157  BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
6159  }
6160 
6161  /* If edge array doesn't go CCW around vertex from average normal side,
6162  * reverse the array, being careful to reverse face pointers too. */
6163  if (tot_edges > 1) {
6164  int ccw_test_sum = 0;
6165  for (int i = 0; i < tot_edges; i++) {
6166  ccw_test_sum += bev_ccw_test(
6167  bv->edges[i].e, bv->edges[(i + 1) % tot_edges].e, bv->edges[i].fnext);
6168  }
6169  if (ccw_test_sum < 0) {
6170  for (int i = 0; i <= (tot_edges / 2) - 1; i++) {
6171  SWAP(EdgeHalf, bv->edges[i], bv->edges[tot_edges - i - 1]);
6172  SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
6173  SWAP(BMFace *, bv->edges[tot_edges - i - 1].fprev, bv->edges[tot_edges - i - 1].fnext);
6174  }
6175  if (tot_edges % 2 == 1) {
6176  int i = tot_edges / 2;
6177  SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
6178  }
6179  }
6180  }
6181 
6182  float weight;
6183  float vert_axis[3] = {0, 0, 0};
6184  if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
6185  /* Modify the offset by the vertex group or bevel weight if they are specified. */
6186  if (bp->dvert != NULL && bp->vertex_group != -1) {
6188  bv->offset *= weight;
6189  }
6190  else if (bp->use_weights) {
6191  weight = BM_elem_float_data_get(&bm->vdata, v, CD_BWEIGHT);
6192  bv->offset *= weight;
6193  }
6194  /* Find center axis. Note: Don't use vert normal, can give unwanted results. */
6196  float edge_dir[3];
6197  EdgeHalf *e = bv->edges;
6198  for (int i = 0; i < tot_edges; i++, e++) {
6199  BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
6200  sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
6201  normalize_v3(edge_dir);
6202  add_v3_v3v3(vert_axis, vert_axis, edge_dir);
6203  }
6204  }
6205  }
6206 
6207  /* Set offsets for each beveled edge. */
6208  EdgeHalf *e = bv->edges;
6209  for (int i = 0; i < tot_edges; i++, e++) {
6210  e->next = &bv->edges[(i + 1) % tot_edges];
6211  e->prev = &bv->edges[(i + tot_edges - 1) % tot_edges];
6212 
6213  if (e->is_bev) {
6214  /* Convert distance as specified by user into offsets along
6215  * faces on the left side and right sides of this edgehalf.
6216  * Except for percent method, offset will be same on each side. */
6217 
6218  switch (bp->offset_type) {
6219  case BEVEL_AMT_OFFSET: {
6220  e->offset_l_spec = bp->offset;
6221  break;
6222  }
6223  case BEVEL_AMT_WIDTH: {
6224  float z = fabsf(2.0f * sinf(edge_face_angle(e) / 2.0f));
6225  if (z < BEVEL_EPSILON) {
6226  e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
6227  }
6228  else {
6229  e->offset_l_spec = bp->offset / z;
6230  }
6231  break;
6232  }
6233  case BEVEL_AMT_DEPTH: {
6234  float z = fabsf(cosf(edge_face_angle(e) / 2.0f));
6235  if (z < BEVEL_EPSILON) {
6236  e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
6237  }
6238  else {
6239  e->offset_l_spec = bp->offset / z;
6240  }
6241  break;
6242  }
6243  case BEVEL_AMT_PERCENT: {
6244  /* Offset needs to meet adjacent edges at percentage of their lengths.
6245  * Since the width isn't constant, we don't store a width at all, but
6246  * rather the distance along the adjacent edge that we need to go
6247  * at this end of the edge.
6248  */
6249 
6250  e->offset_l_spec = BM_edge_calc_length(e->prev->e) * bp->offset / 100.0f;
6251  e->offset_r_spec = BM_edge_calc_length(e->next->e) * bp->offset / 100.0f;
6252 
6253  break;
6254  }
6255  case BEVEL_AMT_ABSOLUTE: {
6256  /* Like Percent, but the amount gives the absolute distance along adjacent edges. */
6257  e->offset_l_spec = bp->offset;
6258  e->offset_r_spec = bp->offset;
6259  break;
6260  }
6261  default: {
6262  BLI_assert(!"bad bevel offset kind");
6263  e->offset_l_spec = bp->offset;
6264  break;
6265  }
6266  }
6268  e->offset_r_spec = e->offset_l_spec;
6269  }
6270  if (bp->use_weights) {
6271  weight = BM_elem_float_data_get(&bm->edata, e->e, CD_BWEIGHT);
6272  e->offset_l_spec *= weight;
6273  e->offset_r_spec *= weight;
6274  }
6275  }
6276  else if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
6277  /* Weight has already been applied to bv->offset, if present.
6278  * Transfer to e->offset_[lr]_spec according to offset_type. */
6279  float edge_dir[3];
6280  switch (bp->offset_type) {
6281  case BEVEL_AMT_OFFSET: {
6282  e->offset_l_spec = bv->offset;
6283  break;
6284  }
6285  case BEVEL_AMT_WIDTH: {
6286  BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
6287  sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
6288  float z = fabsf(2.0f * sinf(angle_v3v3(vert_axis, edge_dir)));
6289  if (z < BEVEL_EPSILON) {
6290  e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
6291  }
6292  else {
6293  e->offset_l_spec = bp->offset / z;
6294  }
6295  break;
6296  }
6297  case BEVEL_AMT_DEPTH: {
6298  BMVert *v2 = BM_edge_other_vert(e->e, bv->v);
6299  sub_v3_v3v3(edge_dir, bv->v->co, v2->co);
6300  float z = fabsf(cosf(angle_v3v3(vert_axis, edge_dir)));
6301  if (z < BEVEL_EPSILON) {
6302  e->offset_l_spec = 0.01f * bp->offset; /* Undefined behavior, so tiny bevel. */
6303  }
6304  else {
6305  e->offset_l_spec = bp->offset / z;
6306  }
6307  break;
6308  }
6309  case BEVEL_AMT_PERCENT: {
6310  e->offset_l_spec = BM_edge_calc_length(e->e) * bv->offset / 100.0f;
6311  break;
6312  }
6313  case BEVEL_AMT_ABSOLUTE: {
6314  e->offset_l_spec = bv->offset;
6315  break;
6316  }
6317  }
6318  e->offset_r_spec = e->offset_l_spec;
6319  }
6320  else {
6321  e->offset_l_spec = e->offset_r_spec = 0.0f;
6322  }
6323  e->offset_l = e->offset_l_spec;
6324  e->offset_r = e->offset_r_spec;
6325 
6326  if (e->fprev && e->fnext) {
6327  e->is_seam = !contig_ldata_across_edge(bm, e->e, e->fprev, e->fnext);
6328  }
6329  else {
6330  e->is_seam = true;
6331  }
6332  }
6333 
6334  /* Collect wire edges if we found any earlier. */
6335  if (tot_wire != 0) {
6336  int i = 0;
6337  BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
6338  if (BM_edge_is_wire(bme)) {
6339  BLI_assert(i < bv->wirecount);
6340  bv->wire_edges[i++] = bme;
6341  }
6342  }
6343  BLI_assert(i == bv->wirecount);
6344  }
6345 
6346  return bv;
6347 }
6348 
6349 /* Face f has at least one beveled vertex. Rebuild f. */
6351 {
6352  bool do_rebuild = false;
6353  BMVert **vv = NULL;
6354  BMVert **vv_fix = NULL;
6355  BMEdge **ee = NULL;
6359 
6360  BMIter liter;
6361  BMLoop *l;
6362  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
6363  if (BM_elem_flag_test(l->v, BM_ELEM_TAG)) {
6364  BMLoop *lprev = l->prev;
6365  BevVert *bv = find_bevvert(bp, l->v);
6366  VMesh *vm = bv->vmesh;
6367  EdgeHalf *e = find_edge_half(bv, l->e);
6368  BLI_assert(e != NULL);
6369  BMEdge *bme = e->e;
6370  EdgeHalf *eprev = find_edge_half(bv, lprev->e);
6371  BLI_assert(eprev != NULL);
6372 
6373  /* Which direction around our vertex do we travel to match orientation of f? */
6374  bool go_ccw;
6375  if (e->prev == eprev) {
6376  if (eprev->prev == e) {
6377  /* Valence 2 vertex: use f is one of e->fnext or e->fprev to break tie. */
6378  go_ccw = (e->fnext != f);
6379  }
6380  else {
6381  go_ccw = true; /* Going CCW around bv to trace this corner. */
6382  }
6383  }
6384  else if (eprev->prev == e) {
6385  go_ccw = false; /* Going cw around bv to trace this corner. */
6386  }
6387  else {
6388  /* Edges in face are non-contiguous in our ordering around bv.
6389  * Which way should we go when going from eprev to e? */
6390  if (count_ccw_edges_between(eprev, e) < count_ccw_edges_between(e, eprev)) {
6391  /* Go counter-clockwise from eprev to e. */
6392  go_ccw = true;
6393  }
6394  else {
6395  /* Go clockwise from eprev to e. */
6396  go_ccw = false;
6397  }
6398  }
6399  bool on_profile_start = false;
6400  BoundVert *vstart;
6401  BoundVert *vend;
6402  if (go_ccw) {
6403  vstart = eprev->rightv;
6404  vend = e->leftv;
6405  if (e->profile_index > 0) {
6406  vstart = vstart->prev;
6407  on_profile_start = true;
6408  }
6409  }
6410  else {
6411  vstart = eprev->leftv;
6412  vend = e->rightv;
6413  if (eprev->profile_index > 0) {
6414  vstart = vstart->next;
6415  on_profile_start = true;
6416  }
6417  }
6418  BLI_assert(vstart != NULL && vend != NULL);
6419  BoundVert *v = vstart;
6420  if (!on_profile_start) {
6421  BLI_array_append(vv, v->nv.v);
6422  BLI_array_append(ee, bme);
6423  }
6424  while (v != vend) {
6425  /* Check for special case: multi-segment 3rd face opposite a beveled edge with no vmesh. */
6426  bool corner3special = (vm->mesh_kind == M_NONE && v->ebev != e && v->ebev != eprev);
6427  if (go_ccw) {
6428  int i = v->index;
6429  int kstart, kend;
6430  if (on_profile_start) {
6431  kstart = e->profile_index;
6432  on_profile_start = false;
6433  }
6434  else {
6435  kstart = 1;
6436  }
6437  if (eprev->rightv == v && eprev->profile_index > 0) {
6438  kend = eprev->profile_index;
6439  }
6440  else {
6441  kend = vm->seg;
6442  }
6443  for (int k = kstart; k <= kend; k++) {
6444  BMVert *bmv = mesh_vert(vm, i, 0, k)->v;
6445  if (bmv) {
6446  BLI_array_append(vv, bmv);
6447  BLI_array_append(ee, bme); /* TODO: Maybe better edge here. */
6448  if (corner3special && v->ebev && !bv->any_seam && k != vm->seg) {
6449  BLI_array_append(vv_fix, bmv);
6450  }
6451  }
6452  }
6453  v = v->next;
6454  }
6455  else {
6456  /* Going cw. */
6457  int i = v->prev->index;
6458  int kstart, kend;
6459  if (on_profile_start) {
6460  kstart = eprev->profile_index;
6461  on_profile_start = false;
6462  }
6463  else {
6464  kstart = vm->seg - 1;
6465  }
6466  if (e->rightv == v->prev && e->profile_index > 0) {
6467  kend = e->profile_index;
6468  }
6469  else {
6470  kend = 0;
6471  }
6472  for (int k = kstart; k >= kend; k--) {
6473  BMVert *bmv = mesh_vert(vm, i, 0, k)->v;
6474  if (bmv) {
6475  BLI_array_append(vv, bmv);
6476  BLI_array_append(ee, bme);
6477  if (corner3special && v->ebev && !bv->any_seam && k != 0) {
6478  BLI_array_append(vv_fix, bmv);
6479  }
6480  }
6481  }
6482  v = v->prev;
6483  }
6484  }
6485  do_rebuild = true;
6486  }
6487  else {
6488  BLI_array_append(vv, l->v);
6489  BLI_array_append(ee, l->e);
6490  }
6491  }
6492  if (do_rebuild) {
6493  int n = BLI_array_len(vv);
6494  BMFace *f_new = bev_create_ngon(bm, vv, n, NULL, f, NULL, -1, true);
6495 
6496  for (int k = 0; k < BLI_array_len(vv_fix); k++) {
6497  bev_merge_uvs(bm, vv_fix[k]);
6498  }
6499 
6500  /* Copy attributes from old edges. */
6501  BLI_assert(n == BLI_array_len(ee));
6502  BMEdge *bme_prev = ee[n - 1];
6503  for (int k = 0; k < n; k++) {
6504  BMEdge *bme_new = BM_edge_exists(vv[k], vv[(k + 1) % n]);
6505  BLI_assert(ee[k] && bme_new);
6506  if (ee[k] != bme_new) {
6507  BM_elem_attrs_copy(bm, bm, ee[k], bme_new);
6508  /* Want to undo seam and smooth for corner segments
6509  * if those attrs aren't contiguous around face. */
6510  if (k < n - 1 && ee[k] == ee[k + 1]) {
6511  if (BM_elem_flag_test(ee[k], BM_ELEM_SEAM) &&
6512  !BM_elem_flag_test(bme_prev, BM_ELEM_SEAM)) {
6514  }
6515  /* Actually want "sharp" to be contiguous, so reverse the test. */
6516  if (!BM_elem_flag_test(ee[k], BM_ELEM_SMOOTH) &&
6517  BM_elem_flag_test(bme_prev, BM_ELEM_SMOOTH)) {
6519  }
6520  }
6521  else {
6522  bme_prev = ee[k];
6523  }
6524  }
6525  }
6526 
6527  /* Don't select newly or return created boundary faces. */
6528  if (f_new) {
6529  record_face_kind(bp, f_new, F_RECON);
6531  /* Also don't want new edges that aren't part of a new bevel face. */
6532  BMIter eiter;
6533  BMEdge *bme;
6534  BM_ITER_ELEM (bme, &eiter, f_new, BM_EDGES_OF_FACE) {
6535  bool keep = false;
6536  BMIter fiter;
6537  BMFace *f_other;
6538  BM_ITER_ELEM (f_other, &fiter, bme, BM_FACES_OF_EDGE) {
6539  if (BM_elem_flag_test(f_other, BM_ELEM_TAG)) {
6540  keep = true;
6541  break;
6542  }
6543  }
6544  if (!keep) {
6545  disable_flag_out_edge(bm, bme);
6546  }
6547  }
6548  }
6549  }
6550 
6551  BLI_array_free(vv);
6552  BLI_array_free(vv_fix);
6553  BLI_array_free(ee);
6554  return do_rebuild;
6555 }
6556 
6557 /* All polygons touching v need rebuilding because beveling v has made new vertices. */
6559 {
6560  void *faces_stack[BM_DEFAULT_ITER_STACK_SIZE];
6561  int faces_len, f_index;
6563  bm, BM_FACES_OF_VERT, v, &faces_len, faces_stack, BM_DEFAULT_ITER_STACK_SIZE);
6564 
6565  if (LIKELY(faces != NULL)) {
6566  for (f_index = 0; f_index < faces_len; f_index++) {
6567  BMFace *f = faces[f_index];
6568  if (bev_rebuild_polygon(bm, bp, f)) {
6569  BM_face_kill(bm, f);
6570  }
6571  }
6572 
6573  if (faces != (BMFace **)faces_stack) {
6574  MEM_freeN(faces);
6575  }
6576  }
6577 }
6578 
6579 /* If there were any wire edges, they need to be reattached somewhere. */
6581 {
6582  BevVert *bv = find_bevvert(bp, v);
6583  if (!bv || bv->wirecount == 0 || !bv->vmesh) {
6584  return;
6585  }
6586 
6587  for (int i = 0; i < bv->wirecount; i++) {
6588  BMEdge *e = bv->wire_edges[i];
6589  /* Look for the new vertex closest to the other end of e. */
6590  BMVert *vclosest = NULL;
6591  float dclosest = FLT_MAX;
6592  BMVert *votherclosest = NULL;
6593  BMVert *vother = BM_edge_other_vert(e, v);
6594  BevVert *bvother = NULL;
6595  if (BM_elem_flag_test(vother, BM_ELEM_TAG)) {
6596  bvother = find_bevvert(bp, vother);
6597  if (!bvother || !bvother->vmesh) {
6598  return; /* Shouldn't happen. */
6599  }
6600  }
6601  BoundVert *bndv = bv->vmesh->boundstart;
6602  do {
6603  if (bvother) {
6604  BoundVert *bndvother = bvother->vmesh->boundstart;
6605  do {
6606  float d = len_squared_v3v3(bndvother->nv.co, bndv->nv.co);
6607  if (d < dclosest) {
6608  vclosest = bndv->nv.v;
6609  votherclosest = bndvother->nv.v;
6610  dclosest = d;
6611  }
6612  } while ((bndvother = bndvother->next) != bvother->vmesh->boundstart);
6613  }
6614  else {
6615  float d = len_squared_v3v3(vother->co, bndv->nv.co);
6616  if (d < dclosest) {
6617  vclosest = bndv->nv.v;
6618  votherclosest = vother;
6619  dclosest = d;
6620  }
6621  }
6622  } while ((bndv = bndv->next) != bv->vmesh->boundstart);
6623  if (vclosest) {
6624  BM_edge_create(bm, vclosest, votherclosest, e, BM_CREATE_NO_DOUBLE);
6625  }
6626  }
6627 }
6628 
6630 {
6631  VMesh *vm = bv->vmesh;
6632 
6633  int nseg = e->seg;
6634  int i = e->leftv->index;
6635  for (int k = 1; k < nseg; k++) {
6636  bev_merge_uvs(bm, mesh_vert(vm, i, 0, k)->v);
6637  }
6638 }
6639 
6640 /*
6641  * Is this BevVert the special case of a weld (no vmesh) where there are
6642  * four edges total, two are beveled, and the other two are on opposite sides?
6643  */
6645 {
6646  return (bv->edgecount == 4 && bv->selcount == 2 &&
6647  ((bv->edges[0].is_bev && bv->edges[2].is_bev) ||
6648  (bv->edges[1].is_bev && bv->edges[3].is_bev)));
6649 }
6650 
6670 static void weld_cross_attrs_copy(BMesh *bm, BevVert *bv, VMesh *vm, int vmindex, EdgeHalf *e)
6671 {
6672  BMEdge *bme_prev = NULL;
6673  BMEdge *bme_next = NULL;
6674  for (int i = 0; i < 4; i++) {
6675  if (&bv->edges[i] == e) {
6676  bme_prev = bv->edges[(i + 3) % 4].e;
6677  bme_next = bv->edges[(i + 1) % 4].e;
6678  break;
6679  }
6680  }
6681  BLI_assert(bme_prev && bme_next);
6682 
6683  /* Want seams and sharp edges to cross only if that way on both sides. */
6684  bool disable_seam = BM_elem_flag_test(bme_prev, BM_ELEM_SEAM) !=
6685  BM_elem_flag_test(bme_next, BM_ELEM_SEAM);
6686  bool enable_smooth = BM_elem_flag_test(bme_prev, BM_ELEM_SMOOTH) !=
6687  BM_elem_flag_test(bme_next, BM_ELEM_SMOOTH);
6688 
6689  int nseg = e->seg;
6690  for (int i = 0; i < nseg; i++) {
6691  BMEdge *bme = BM_edge_exists(mesh_vert(vm, vmindex, 0, i)->v,
6692  mesh_vert(vm, vmindex, 0, i + 1)->v);
6693  BLI_assert(bme);
6694  BM_elem_attrs_copy(bm, bm, bme_prev, bme);
6695  if (disable_seam) {
6697  }
6698  if (enable_smooth) {
6700  }
6701  }
6702 }
6703 
6708 {
6709  int mat_nr = bp->mat_nr;
6710 
6711  if (!BM_edge_is_manifold(bme)) {
6712  return;
6713  }
6714 
6715  BevVert *bv1 = find_bevvert(bp, bme->v1);
6716  BevVert *bv2 = find_bevvert(bp, bme->v2);
6717 
6718  BLI_assert(bv1 && bv2);
6719 
6720  EdgeHalf *e1 = find_edge_half(bv1, bme);
6721  EdgeHalf *e2 = find_edge_half(bv2, bme);
6722 
6723  BLI_assert(e1 && e2);
6724 
6725  /*
6726  * bme->v1
6727  * / | \
6728  * v1--|--v4
6729  * | | |
6730  * | | |
6731  * v2--|--v3
6732  * \ | /
6733  * bme->v2
6734  */
6735  int nseg = e1->seg;
6736  BLI_assert(nseg > 0 && nseg == e2->seg);
6737 
6738  BMVert *bmv1 = e1->leftv->nv.v;
6739  BMVert *bmv4 = e1->rightv->nv.v;
6740  BMVert *bmv2 = e2->rightv->nv.v;
6741  BMVert *bmv3 = e2->leftv->nv.v;
6742 
6743  BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
6744 
6745  BMFace *f1 = e1->fprev;
6746  BMFace *f2 = e1->fnext;
6747  BMFace *faces[4] = {f1, f1, f2, f2};
6748 
6749  int i1 = e1->leftv->index;
6750  int i2 = e2->leftv->index;
6751  VMesh *vm1 = bv1->vmesh;
6752  VMesh *vm2 = bv2->vmesh;
6753 
6754  BMVert *verts[4];
6755  verts[0] = bmv1;
6756  verts[1] = bmv2;
6757  int odd = nseg % 2;
6758  int mid = nseg / 2;
6759  BMEdge *center_bme = NULL;
6760  for (int k = 1; k <= nseg; k++) {
6761  verts[3] = mesh_vert(vm1, i1, 0, k)->v;
6762  verts[2] = mesh_vert(vm2, i2, 0, nseg - k)->v;
6763  BMFace *r_f;
6764  if (odd && k == mid + 1) {
6765  BMFace *fchoices[2] = {f1, f2};
6766  BMFace *f_choice = choose_rep_face(bp, fchoices, 2);
6767  if (e1->is_seam) {
6768  /* Straddles a seam: choose to interpolate in f_choice and snap the loops whose verts
6769  * are in the non-chosen face to bme for interpolation purposes.
6770  */
6771  BMEdge *edges[4];
6772  if (f_choice == f1) {
6773  edges[0] = edges[1] = NULL;
6774  edges[2] = edges[3] = bme;
6775  }
6776  else {
6777  edges[0] = edges[1] = bme;
6778  edges[2] = edges[3] = NULL;
6779  }
6780  r_f = bev_create_ngon(bm, verts, 4, NULL, f_choice, edges, mat_nr, true);
6781  }
6782  else {
6783  /* Straddles but not a seam: interpolate left half in f1, right half in f2. */
6784  r_f = bev_create_ngon(bm, verts, 4, faces, f_choice, NULL, mat_nr, true);
6785  }
6786  }
6787  else if (!odd && k == mid) {
6788  /* Left poly that touches an even center line on right. */
6789  BMEdge *edges[4] = {NULL, NULL, bme, bme};
6790  r_f = bev_create_ngon(bm, verts, 4, NULL, f1, edges, mat_nr, true);
6791  center_bme = BM_edge_exists(verts[2], verts[3]);
6792  BLI_assert(center_bme != NULL);
6793  }
6794  else if (!odd && k == mid + 1) {
6795  /* Right poly that touches an even center line on left. */
6796  BMEdge *edges[4] = {bme, bme, NULL, NULL};
6797  r_f = bev_create_ngon(bm, verts, 4, NULL, f2, edges, mat_nr, true);
6798  }
6799  else {
6800  /* Doesn't cross or touch the center line, so interpolate in appropriate f1 or f2. */
6801  BMFace *f = (k <= mid) ? f1 : f2;
6802  r_f = bev_create_ngon(bm, verts, 4, NULL, f, NULL, mat_nr, true);
6803  }
6804  record_face_kind(bp, r_f, F_EDGE);
6805  /* Tag the long edges: those out of verts[0] and verts[2]. */
6806  BMIter iter;
6807  BMLoop *l;
6808  BM_ITER_ELEM (l, &iter, r_f, BM_LOOPS_OF_FACE) {
6809  if (ELEM(l->v, verts[0], verts[2])) {
6811  }
6812  }
6813  verts[0] = verts[3];
6814  verts[1] = verts[2];
6815  }
6816  if (!odd) {
6817  if (!e1->is_seam) {
6818  bev_merge_edge_uvs(bm, center_bme, mesh_vert(vm1, i1, 0, mid)->v);
6819  }
6820  if (!e2->is_seam) {
6821  bev_merge_edge_uvs(bm, center_bme, mesh_vert(vm2, i2, 0, mid)->v);
6822  }
6823  }
6824 
6825  /* Fix UVs along end edge joints. A nop unless other side built already. */
6826  /* TODO: If some seam, may want to do selective merge. */
6827  if (!bv1->any_seam && bv1->vmesh->mesh_kind == M_NONE) {
6828  bev_merge_end_uvs(bm, bv1, e1);
6829  }
6830  if (!bv2->any_seam && bv2->vmesh->mesh_kind == M_NONE) {
6831  bev_merge_end_uvs(bm, bv2, e2);
6832  }
6833 
6834  /* Copy edge data to first and last edge. */
6835  BMEdge *bme1 = BM_edge_exists(bmv1, bmv2);
6836  BMEdge *bme2 = BM_edge_exists(bmv3, bmv4);
6837  BLI_assert(bme1 && bme2);
6838  BM_elem_attrs_copy(bm, bm, bme, bme1);
6839  BM_elem_attrs_copy(bm, bm, bme, bme2);
6840 
6841  /* If either end is a "weld cross", want continuity of edge attributes across end edge(s). */
6842  if (bevvert_is_weld_cross(bv1)) {
6843  weld_cross_attrs_copy(bm, bv1, vm1, i1, e1);
6844  }
6845  if (bevvert_is_weld_cross(bv2)) {
6846  weld_cross_attrs_copy(bm, bv2, vm2, i2, e2);
6847  }
6848 }
6849 
6850 /* Find xnew > x0 so that distance((x0,y0), (xnew, ynew)) = dtarget.
6851  * False position Illinois method used because the function is somewhat linear
6852  * -> linear interpolation converges fast.
6853  * Assumes that the gradient is always between 1 and -1 for x in [x0, x0+dtarget]. */
6854 static double find_superellipse_chord_endpoint(double x0, double dtarget, float r, bool rbig)
6855 {
6856  double y0 = superellipse_co(x0, r, rbig);
6857  const double tol = 1e-13; /* accumulates for many segments so use low value. */
6858  const int maxiter = 10;
6859 
6860  /* For gradient between -1 and 1, xnew can only be in [x0 + sqrt(2)/2*dtarget, x0 + dtarget]. */
6861  double xmin = x0 + M_SQRT2 / 2.0 * dtarget;
6862  if (xmin > 1.0) {
6863  xmin = 1.0;
6864  }
6865  double xmax = x0 + dtarget;
6866  if (xmax > 1.0) {
6867  xmax = 1.0;
6868  }
6869  double ymin = superellipse_co(xmin, r, rbig);
6870  double ymax = superellipse_co(xmax, r, rbig);
6871 
6872  /* Note: using distance**2 (no sqrt needed) does not converge that well. */
6873  double dmaxerr = sqrt(pow((xmax - x0), 2) + pow((ymax - y0), 2)) - dtarget;
6874  double dminerr = sqrt(pow((xmin - x0), 2) + pow((ymin - y0), 2)) - dtarget;
6875 
6876  double xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
6877  bool lastupdated_upper = true;
6878 
6879  for (int iter = 0; iter < maxiter; iter++) {
6880  double ynew = superellipse_co(xnew, r, rbig);
6881  double dnewerr = sqrt(pow((xnew - x0), 2) + pow((ynew - y0), 2)) - dtarget;
6882  if (fabs(dnewerr) < tol) {
6883  break;
6884  }
6885  if (dnewerr < 0) {
6886  xmin = xnew;
6887  ymin = ynew;
6888  dminerr = dnewerr;
6889  if (!lastupdated_upper) {
6890  xnew = (dmaxerr / 2 * xmin - dminerr * xmax) / (dmaxerr / 2 - dminerr);
6891  }
6892  else {
6893  xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
6894  }
6895  lastupdated_upper = false;
6896  }
6897  else {
6898  xmax = xnew;
6899  ymax = ynew;
6900  dmaxerr = dnewerr;
6901  if (lastupdated_upper) {
6902  xnew = (dmaxerr * xmin - dminerr / 2 * xmax) / (dmaxerr - dminerr / 2);
6903  }
6904  else {
6905  xnew = xmax - dmaxerr * (xmax - xmin) / (dmaxerr - dminerr);
6906  }
6907  lastupdated_upper = true;
6908  }
6909  }
6910  return xnew;
6911 }
6912 
6922 static void find_even_superellipse_chords_general(int seg, float r, double *xvals, double *yvals)
6923 {
6924  const int smoothitermax = 10;
6925  const double error_tol = 1e-7;
6926  int imax = (seg + 1) / 2 - 1; /* Ceiling division - 1. */
6927 
6928  bool seg_odd = seg % 2;
6929 
6930  bool rbig;
6931  double mx;
6932  if (r > 1.0f) {
6933  rbig = true;
6934  mx = pow(0.5, 1.0 / r);
6935  }
6936  else {
6937  rbig = false;
6938  mx = 1 - pow(0.5, 1.0 / r);
6939  }
6940 
6941  /* Initial positions, linear spacing along x axis. */
6942  for (int i = 0; i <= imax; i++) {
6943  xvals[i] = i * mx / seg * 2;
6944  yvals[i] = superellipse_co(xvals[i], r, rbig);
6945  }
6946  yvals[0] = 1;
6947 
6948  /* Smooth distance loop. */
6949  for (int iter = 0; iter < smoothitermax; iter++) {
6950  double sum = 0.0;
6951  double dmin = 2.0;
6952  double dmax = 0.0;
6953  /* Update distances between neighbor points. Store the highest and
6954  * lowest to see if the maximum error to average distance (which isn't
6955  * known yet) is below required precision. */
6956  for (int i = 0; i < imax; i++) {
6957  double d = sqrt(pow((xvals[i + 1] - xvals[i]), 2) + pow((yvals[i + 1] - yvals[i]), 2));
6958  sum += d;
6959  if (d > dmax) {
6960  dmax = d;
6961  }
6962  if (d < dmin) {
6963  dmin = d;
6964  }
6965  }
6966  /* For last distance, weight with 1/2 if seg_odd. */
6967  double davg;
6968  if (seg_odd) {
6969  sum += M_SQRT2 / 2 * (yvals[imax] - xvals[imax]);
6970  davg = sum / (imax + 0.5);
6971  }
6972  else {
6973  sum += sqrt(pow((xvals[imax] - mx), 2) + pow((yvals[imax] - mx), 2));
6974  davg = sum / (imax + 1.0);
6975  }
6976  /* Max error in tolerance? -> Quit. */
6977  bool precision_reached = true;
6978  if (dmax - davg > error_tol) {
6979  precision_reached = false;
6980  }
6981  if (dmin - davg < error_tol) {
6982  precision_reached = false;
6983  }
6984  if (precision_reached) {
6985  break;
6986  }
6987 
6988  /* Update new coordinates. */
6989  for (int i = 1; i <= imax; i++) {
6990  xvals[i] = find_superellipse_chord_endpoint(xvals[i - 1], davg, r, rbig);
6991  yvals[i] = superellipse_co(xvals[i], r, rbig);
6992  }
6993  }
6994 
6995  /* Fill remaining. */
6996  if (!seg_odd) {
6997  xvals[imax + 1] = mx;
6998  yvals[imax + 1] = mx;
6999  }
7000  for (int i = imax + 1; i <= seg; i++) {
7001  yvals[i] = xvals[seg - i];
7002  xvals[i] = yvals[seg - i];
7003  }
7004 
7005  if (!rbig) {
7006  for (int i = 0; i <= seg; i++) {
7007  double temp = xvals[i];
7008  xvals[i] = 1.0 - yvals[i];
7009  yvals[i] = 1.0 - temp;
7010  }
7011  }
7012 }
7013 
7022 static void find_even_superellipse_chords(int n, float r, double *xvals, double *yvals)
7023 {
7024  bool seg_odd = n % 2;
7025  int n2 = n / 2;
7026 
7027  /* Special cases. */
7028  if (r == PRO_LINE_R) {
7029  /* Linear spacing. */
7030  for (int i = 0; i <= n; i++) {
7031  xvals[i] = (double)i / n;
7032  yvals[i] = 1.0 - (double)i / n;
7033  }
7034  return;
7035  }
7036  if (r == PRO_CIRCLE_R) {
7037  double temp = (M_PI / 2) / n;
7038  /* Angle spacing. */
7039  for (int i = 0; i <= n; i++) {
7040  xvals[i] = sin(i * temp);
7041  yvals[i] = cos(i * temp);
7042  }
7043  return;
7044  }
7045  if (r == PRO_SQUARE_IN_R) {
7046  /* n is even, distribute first and second half linear. */
7047  if (!seg_odd) {
7048  for (int i = 0; i <= n2; i++) {
7049  xvals[i] = 0.0;
7050  yvals[i] = 1.0 - (double)i / n2;
7051  xvals[n - i] = yvals[i];
7052  yvals[n - i] = xvals[i];
7053  }
7054  }
7055  /* n is odd, so get one corner-cut chord. */
7056  else {
7057  double temp = 1.0 / (n2 + M_SQRT2 / 2.0);
7058  for (int i = 0; i <= n2; i++) {
7059  xvals[i] = 0.0;
7060  yvals[i] = 1.0 - (double)i * temp;
7061  xvals[n - i] = yvals[i];
7062  yvals[n - i] = xvals[i];
7063  }
7064  }
7065  return;
7066  }
7067  if (r == PRO_SQUARE_R) {
7068  /* n is even, distribute first and second half linear. */
7069  if (!seg_odd) {
7070  for (int i = 0; i <= n2; i++) {
7071  xvals[i] = (double)i / n2;
7072  yvals[i] = 1.0;
7073  xvals[n - i] = yvals[i];
7074  yvals[n - i] = xvals[i];
7075  }
7076  }
7077  /* n is odd, so get one corner-cut chord. */
7078  else {
7079  double temp = 1.0 / (n2 + M_SQRT2 / 2);
7080  for (int i = 0; i <= n2; i++) {
7081  xvals[i] = (double)i * temp;
7082  yvals[i] = 1.0;
7083  xvals[n - i] = yvals[i];
7084  yvals[n - i] = xvals[i];
7085  }
7086  }
7087  return;
7088  }
7089  /* For general case use the more expensive search algorithm. */
7090  find_even_superellipse_chords_general(n, r, xvals, yvals);
7091 }
7092 
7099 {
7100  int nseg = bp->seg;
7101 
7102  /* Precalculated fullness for circle profile radius and more common low seg values. */
7103 #define CIRCLE_FULLNESS_SEGS 11
7104  static const float circle_fullness[CIRCLE_FULLNESS_SEGS] = {
7105  0.0f, /* nsegs == 1 */
7106  0.559f, /* 2 */
7107  0.642f, /* 3 */
7108  0.551f, /* 4 */
7109  0.646f, /* 5 */
7110  0.624f, /* 6 */
7111  0.646f, /* 7 */
7112  0.619f, /* 8 */
7113  0.647f, /* 9 */
7114  0.639f, /* 10 */
7115  0.647f, /* 11 */
7116  };
7117 
7118  float fullness;
7119  if (bp->profile_type == BEVEL_PROFILE_CUSTOM) {
7120  /* Set fullness to the average "height" of the profile's sampled points. */
7121  fullness = 0.0f;
7122  for (int i = 0; i < nseg; i++) { /* Don't use the end points. */
7123  fullness += (float)(bp->pro_spacing.xvals[i] + bp->pro_spacing.yvals[i]) / (2.0f * nseg);
7124  }
7125  }
7126  else {
7127  /* An offline optimization process found fullness that led to closest fit to sphere as
7128  * a function of r and ns (for case of cube corner). */
7129  if (bp->pro_super_r == PRO_LINE_R) {
7130  fullness = 0.0f;
7131  }
7132  else if (bp->pro_super_r == PRO_CIRCLE_R && nseg > 0 && nseg <= CIRCLE_FULLNESS_SEGS) {
7133  fullness = circle_fullness[nseg - 1];
7134  }
7135  else {
7136  /* Linear regression fit found best linear function, separately for even/odd segs. */
7137  if (nseg % 2 == 0) {
7138  fullness = 2.4506f * bp->profile - 0.00000300f * nseg - 0.6266f;
7139  }
7140  else {
7141  fullness = 2.3635f * bp->profile + 0.000152f * nseg - 0.6060f;
7142  }
7143  }
7144  }
7145  return fullness;
7146 }
7147 
7159 static void set_profile_spacing(BevelParams *bp, ProfileSpacing *pro_spacing, bool custom)
7160 {
7161  int seg = bp->seg;
7162 
7163  if (seg <= 1) {
7164  /* Only 1 segment, we don't need any profile information. */
7165  pro_spacing->xvals = NULL;
7166  pro_spacing->yvals = NULL;
7167  pro_spacing->xvals_2 = NULL;
7168  pro_spacing->yvals_2 = NULL;
7169  pro_spacing->seg_2 = 0;
7170  return;
7171  }
7172 
7173  int seg_2 = max_ii(power_of_2_max_i(bp->seg), 4);
7174 
7175  /* Sample the seg_2 segments used during vertex mesh subdivision. */
7176  bp->pro_spacing.seg_2 = seg_2;
7177  if (seg_2 == seg) {
7178  pro_spacing->xvals_2 = pro_spacing->xvals;
7179  pro_spacing->yvals_2 = pro_spacing->yvals;
7180  }
7181  else {
7182  pro_spacing->xvals_2 = (double *)BLI_memarena_alloc(bp->mem_arena,
7183  sizeof(double) * (seg_2 + 1));
7184  pro_spacing->yvals_2 = (double *)BLI_memarena_alloc(bp->mem_arena,
7185  sizeof(double) * (seg_2 + 1));
7186  if (custom) {
7187  /* Make sure the curve profile widget's sample table is full of the seg_2 samples. */
7188  BKE_curveprofile_init((CurveProfile *)bp->custom_profile, (short)seg_2);
7189 
7190  /* Copy segment locations into the profile spacing struct. */
7191  for (int i = 0; i < seg_2 + 1; i++) {
7192  pro_spacing->xvals_2[i] = (double)bp->custom_profile->segments[i].y;
7193  pro_spacing->yvals_2[i] = (double)bp->custom_profile->segments[i].x;
7194  }
7195  }
7196  else {
7198  seg_2, bp->pro_super_r, pro_spacing->xvals_2, pro_spacing->yvals_2);
7199  }
7200  }
7201 
7202  /* Sample the input number of segments. */
7203  pro_spacing->xvals = (double *)BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg + 1));
7204  pro_spacing->yvals = (double *)BLI_memarena_alloc(bp->mem_arena, sizeof(double) * (seg + 1));
7205  if (custom) {
7206  /* Make sure the curve profile's sample table is full. */
7207  if (bp->custom_profile->segments_len != seg || !bp->custom_profile->segments) {
7209  }
7210 
7211  /* Copy segment locations into the profile spacing struct. */
7212  for (int i = 0; i < seg + 1; i++) {
7213  pro_spacing->xvals[i] = (double)bp->custom_profile->segments[i].y;
7214  pro_spacing->yvals[i] = (double)bp->custom_profile->segments[i].x;
7215  }
7216  }
7217  else {
7218  find_even_superellipse_chords(seg, bp->pro_super_r, pro_spacing->xvals, pro_spacing->yvals);
7219  }
7220 }
7221 
7246 {
7247  float no_collide_offset = bp->offset + 1e6;
7248  float limit = no_collide_offset;
7249  if (bp->offset == 0.0f) {
7250  return no_collide_offset;
7251  }
7252  float kb = eb->offset_l_spec;
7253  EdgeHalf *ea = eb->next; /* Note: this is in direction b --> a. */
7254  float ka = ea->offset_r_spec;
7255  BMVert *vb, *vc;
7256  if (eb->is_rev) {
7257  vc = eb->e->v1;
7258  vb = eb->e->v2;
7259  }
7260  else {
7261  vb = eb->e->v1;
7262  vc = eb->e->v2;
7263  }
7264  BMVert *va = ea->is_rev ? ea->e->v1 : ea->e->v2;
7265  BevVert *bvc = NULL;
7266  EdgeHalf *ebother = find_other_end_edge_half(bp, eb, &bvc);
7267  EdgeHalf *ec;
7268  BMVert *vd;
7269  float kc;
7271  if (ea->is_bev && ebother != NULL && ebother->prev->is_bev) {
7272  if (bp->offset_type == BEVEL_AMT_PERCENT) {
7273  return 50.0f;
7274  }
7275  /* This is only right sometimes. The exact answer is very hard to calculate. */
7276  float blen = BM_edge_calc_length(eb->e);
7277  return bp->offset > blen / 2.0f ? blen / 2.0f : blen;
7278  }
7279  return no_collide_offset;
7280  }
7281  if (ebother != NULL) {
7282  ec = ebother->prev; /* Note: this is in direction c --> d. */
7283  vc = bvc->v;
7284  kc = ec->offset_l_spec;
7285  vd = ec->is_rev ? ec->e->v1 : ec->e->v2;
7286  }
7287  else {
7288  /* No bevvert for w, so C can't be beveled. */
7289  kc = 0.0f;
7290  ec = NULL;
7291  /* Find an edge from c that has same face. */
7292  if (eb->fnext == NULL) {
7293  return no_collide_offset;
7294  }
7295  BMLoop *lb = BM_face_edge_share_loop(eb->fnext, eb->e);
7296  if (!lb) {
7297  return no_collide_offset;
7298  }
7299  if (lb->next->v == vc) {
7300  vd = lb->next->next->v;
7301  }
7302  else if (lb->v == vc) {
7303  vd = lb->prev->v;
7304  }
7305  else {
7306  return no_collide_offset;
7307  }
7308  }
7309  if (ea->e == eb->e || (ec && ec->e == eb->e)) {
7310  return no_collide_offset;
7311  }
7312  ka = ka / bp->offset;
7313  kb = kb / bp->offset;
7314  kc = kc / bp->offset;
7315  float th1 = angle_v3v3v3(va->co, vb->co, vc->co);
7316  float th2 = angle_v3v3v3(vb->co, vc->co, vd->co);
7317 
7318  /* First calculate offset at which edge B collapses, which happens
7319  * when advancing clones of A, B, C all meet at a point.
7320  * This only happens if at least two of those three edges have non-zero k's. */
7321  float sin1 = sinf(th1);
7322  float sin2 = sinf(th2);
7323  if ((ka > 0.0f) + (kb > 0.0f) + (kc > 0.0f) >= 2) {
7324  float tan1 = tanf(th1);
7325  float tan2 = tanf(th2);
7326  float g = tan1 * tan2;
7327  float h = sin1 * sin2;
7328  float den = g * (ka * sin2 + kc * sin1) + kb * h * (tan1 + tan2);
7329  if (den != 0.0f) {
7330  float t = BM_edge_calc_length(eb->e);
7331  t *= g * h / den;
7332  if (t >= 0.0f) {
7333  limit = t;
7334  }
7335  }
7336  }
7337 
7338  /* Now check edge slide cases. */
7339  if (kb > 0.0f && ka == 0.0f /*&& bvb->selcount == 1 && bvb->edgecount > 2 */) {
7340  float t = BM_edge_calc_length(ea->e);
7341  t *= sin1 / kb;
7342  if (t >= 0.0f && t < limit) {
7343  limit = t;
7344  }
7345  }
7346  if (kb > 0.0f && kc == 0.0f /* && bvc && ec && bvc->selcount == 1 && bvc->edgecount > 2 */) {
7347  float t = BM_edge_calc_length(ec->e);
7348  t *= sin2 / kb;
7349  if (t >= 0.0f && t < limit) {
7350  limit = t;
7351  }
7352  }
7353  return limit;
7354 }
7355 
7362 {
7363  float no_collide_offset = bp->offset + 1e6;
7364  float limit = no_collide_offset;
7365  if (bp->offset == 0.0f) {
7366  return no_collide_offset;
7367  }
7368  float ka = ea->offset_l_spec / bp->offset;
7369  EdgeHalf *eb = find_other_end_edge_half(bp, ea, NULL);
7370  float kb = eb ? eb->offset_l_spec / bp->offset : 0.0f;
7371  float kab = ka + kb;
7372  float la = BM_edge_calc_length(ea->e);
7373  if (kab <= 0.0f) {
7374  return no_collide_offset;
7375  }
7376  limit = la / kab;
7377  return limit;
7378 }
7379 
7386 {
7387  float limited_offset = bp->offset;
7388  BMIter iter;
7389  BMVert *bmv;
7390  BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
7391  if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
7392  continue;
7393  }
7394  BevVert *bv = find_bevvert(bp, bmv);
7395  if (!bv) {
7396  continue;
7397  }
7398  for (int i = 0; i < bv->edgecount; i++) {
7399  EdgeHalf *eh = &bv->edges[i];
7400  if (bp->affect_type == BEVEL_AFFECT_VERTICES) {
7401  float collision_offset = vertex_collide_offset(bp, eh);
7402  if (collision_offset < limited_offset) {
7403  limited_offset = collision_offset;
7404  }
7405  }
7406  else {
7407  float collision_offset = geometry_collide_offset(bp, eh);
7408  if (collision_offset < limited_offset) {
7409  limited_offset = collision_offset;
7410  }
7411  }
7412  }
7413  }
7414 
7415  if (limited_offset < bp->offset) {
7416  /* All current offset specs have some number times bp->offset,
7417  * so we can just multiply them all by the reduction factor
7418  * of the offset to have the effect of recalculating the specs
7419  * with the new limited_offset.
7420  */
7421  float offset_factor = limited_offset / bp->offset;
7422  BM_ITER_MESH (bmv, &iter, bm, BM_VERTS_OF_MESH) {
7423  if (!BM_elem_flag_test(bmv, BM_ELEM_TAG)) {
7424  continue;
7425  }
7426  BevVert *bv = find_bevvert(bp, bmv);
7427  if (!bv) {
7428  continue;
7429  }
7430  for (int i = 0; i < bv->edgecount; i++) {
7431  EdgeHalf *eh = &bv->edges[i];
7432  eh->offset_l_spec *= offset_factor;
7433  eh->offset_r_spec *= offset_factor;
7434  eh->offset_l *= offset_factor;
7435  eh->offset_r *= offset_factor;
7436  }
7437  }
7438  bp->offset = limited_offset;
7439  }
7440 }
7441 
7455  const float offset,
7456  const int offset_type,
7457  const int profile_type,
7458  const int segments,
7459  const float profile,
7460  const bool affect_type,
7461  const bool use_weights,
7462  const bool limit_offset,
7463  const struct MDeformVert *dvert,
7464  const int vertex_group,
7465  const int mat,
7466  const bool loop_slide,
7467  const bool mark_seam,
7468  const bool mark_sharp,
7469  const bool harden_normals,
7470  const int face_strength_mode,
7471  const int miter_outer,
7472  const int miter_inner,
7473  const float spread,
7474  const float smoothresh,
7475  const struct CurveProfile *custom_profile,
7476  const int vmesh_method)
7477 {
7478  BMIter iter, liter;
7479  BMVert *v, *v_next;
7480  BMEdge *e;
7481  BMFace *f;
7482  BMLoop *l;
7483  BevVert *bv;
7484  BevelParams bp = {
7485  .bm = bm,
7486  .offset = offset,
7487  .offset_type = offset_type,
7488  .seg = max_ii(segments, 1),
7489  .profile = profile,
7490  .pro_super_r = -logf(2.0) / logf(sqrtf(profile)), /* Convert to superellipse exponent. */
7491  .affect_type = affect_type,
7492  .use_weights = use_weights,
7493  .loop_slide = loop_slide,
7494  .limit_offset = limit_offset,
7495  .offset_adjust = (bp.affect_type != BEVEL_AFFECT_VERTICES) &&
7496  !ELEM(offset_type, BEVEL_AMT_PERCENT, BEVEL_AMT_ABSOLUTE),
7497  .dvert = dvert,
7498  .vertex_group = vertex_group,
7499  .mat_nr = mat,
7500  .mark_seam = mark_seam,
7501  .mark_sharp = mark_sharp,
7502  .harden_normals = harden_normals,
7503  .face_strength_mode = face_strength_mode,
7504  .miter_outer = miter_outer,
7505  .miter_inner = miter_inner,
7506  .spread = spread,
7507  .smoothresh = smoothresh,
7508  .face_hash = NULL,
7509  .profile_type = profile_type,
7510  .custom_profile = custom_profile,
7511  .vmesh_method = vmesh_method,
7512  };
7513 
7514  if (bp.offset <= 0) {
7515  return;
7516  }
7517 
7518 #ifdef BEVEL_DEBUG_TIME
7519  double start_time = PIL_check_seconds_timer();
7520 #endif
7521 
7522  /* Disable the miters with the cutoff vertex mesh method, the combination isn't useful anyway. */
7523  if (bp.vmesh_method == BEVEL_VMESH_CUTOFF) {
7526  }
7527 
7528  if (profile >= 0.950f) { /* r ~ 692, so PRO_SQUARE_R is 1e4 */
7530  }
7531  else if (fabsf(bp.pro_super_r - PRO_CIRCLE_R) < 1e-4) {
7533  }
7534  else if (fabsf(bp.pro_super_r - PRO_LINE_R) < 1e-4) {
7535  bp.pro_super_r = PRO_LINE_R;
7536  }
7537  else if (bp.pro_super_r < 1e-4) {
7539  }
7540 
7541  /* Primary alloc. */
7542  bp.vert_hash = BLI_ghash_ptr_new(__func__);
7543  bp.mem_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 16), __func__);
7545 
7546  /* Get the 2D profile point locations from either the superellipse or the custom profile. */
7548 
7549  /* Get the 'fullness' of the profile for the ADJ vertex mesh method. */
7550  if (bp.seg > 1) {
7552  }
7553 
7554  /* Get separate non-custom profile samples for the miter profiles if they are needed */
7555  if (bp.profile_type == BEVEL_PROFILE_CUSTOM &&
7557  set_profile_spacing(&bp, &bp.pro_spacing_miter, false);
7558  }
7559 
7560  bp.face_hash = BLI_ghash_ptr_new(__func__);
7562 
7563  math_layer_info_init(&bp, bm);
7564 
7565  /* Analyze input vertices, sorting edges and assigning initial new vertex positions. */
7566  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7568  bv = bevel_vert_construct(bm, &bp, v);
7569  if (!limit_offset && bv) {
7570  build_boundary(&bp, bv, true);
7571  }
7572  }
7573  }
7574 
7575  /* Perhaps clamp offset to avoid geometry collisions. */
7576  if (limit_offset) {
7577  bevel_limit_offset(&bp, bm);
7578 
7579  /* Assign initial new vertex positions. */
7580  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7582  bv = find_bevvert(&bp, v);
7583  if (bv) {
7584  build_boundary(&bp, bv, true);
7585  }
7586  }
7587  }
7588  }
7589 
7590  /* Perhaps do a pass to try to even out widths. */
7591  if (bp.offset_adjust) {
7592  adjust_offsets(&bp, bm);
7593  }
7594 
7595  /* Maintain consistent orientations for the asymmetrical custom profiles. */
7596  if (bp.profile_type == BEVEL_PROFILE_CUSTOM) {
7597  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
7600  }
7601  }
7602  }
7603 
7604  /* Build the meshes around vertices, now that positions are final. */
7605  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7607  bv = find_bevvert(&bp, v);
7608  if (bv) {
7609  build_vmesh(&bp, bm, bv);
7610  }
7611  }
7612  }
7613 
7614  /* Build polygons for edges. */
7615  if (bp.affect_type != BEVEL_AFFECT_VERTICES) {
7616  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
7619  }
7620  }
7621  }
7622 
7623  /* Extend edge data like sharp edges and precompute normals for harden. */
7624  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7626  bv = find_bevvert(&bp, v);
7627  if (bv) {
7629  }
7630  }
7631  }
7632 
7633  /* Rebuild face polygons around affected vertices. */
7634  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7637  bevel_reattach_wires(bm, &bp, v);
7638  }
7639  }
7640 
7641  BM_ITER_MESH_MUTABLE (v, v_next, &iter, bm, BM_VERTS_OF_MESH) {
7643  BLI_assert(find_bevvert(&bp, v) != NULL);
7644  BM_vert_kill(bm, v);
7645  }
7646  }
7647 
7648  if (bp.harden_normals) {
7649  bevel_harden_normals(&bp, bm);
7650  }
7653  }
7654 
7655  /* When called from operator (as opposed to modifier), bm->use_toolflags
7656  * will be set, and we need to transfer the oflags to BM_ELEM_TAGs. */
7657  if (bm->use_toolflags) {
7658  BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
7659  if (BMO_vert_flag_test(bm, v, VERT_OUT)) {
7661  }
7662  }
7663  BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
7664  if (BMO_edge_flag_test(bm, e, EDGE_OUT)) {
7666  }
7667  }
7668  }
7669 
7670  /* Clear the BM_ELEM_LONG_TAG tags, which were only set on some edges in F_EDGE faces. */
7671  BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
7672  if (get_face_kind(&bp, f) != F_EDGE) {
7673  continue;
7674  }
7675  BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
7677  }
7678  }
7679 
7680  /* Primary free. */
7684 
7685 #ifdef BEVEL_DEBUG_TIME
7686  double end_time = PIL_check_seconds_timer();
7687  printf("BMESH BEVEL TIME = %.3f\n", end_time - start_time);
7688 #endif
7689 }
typedef float(TangentPoint)[2]
void BKE_curveprofile_init(struct CurveProfile *profile, short segments_len)
Definition: curveprofile.c:966
CustomData interface, see also DNA_customdata_types.h.
int CustomData_number_of_layers(const struct CustomData *data, int type)
bool CustomData_has_layer(const struct CustomData *data, int type)
int CustomData_get_named_layer_index(const struct CustomData *data, int type, const char *name)
bool CustomData_data_equals(int type, const void *data1, const void *data2)
Definition: customdata.c:3929
int CustomData_get_layer_index(const struct CustomData *data, int type)
int CustomData_get_n_offset(const struct CustomData *data, int type, int n)
bool CustomData_layer_has_math(const struct CustomData *data, int layer_n)
Definition: customdata.c:3820
int CustomData_get_offset(const struct CustomData *data, int type)
support for deformation groups and hooks.
float BKE_defvert_find_weight(const struct MDeformVert *dvert, const int defgroup)
Definition: deform.c:632
void BKE_lnor_space_custom_normal_to_data(MLoopNorSpace *lnor_space, const float custom_lnor[3], short r_clnor_data[2])
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
A (mainly) macro array library.
#define BLI_array_append(arr, item)
Definition: BLI_array.h:104
#define BLI_array_staticdeclare(arr, maxstatic)
Definition: BLI_array.h:69
#define BLI_array_len(arr)
Definition: BLI_array.h:74
#define BLI_array_clear(arr)
Definition: BLI_array.h:130
#define BLI_array_free(arr)
Definition: BLI_array.h:116
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
sqrt(x)+1/max(0
@ GHASH_FLAG_ALLOW_DUPES
Definition: BLI_ghash.h:67
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
GHash * BLI_ghash_ptr_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void BLI_ghash_flag_set(GHash *gh, unsigned int flag)
Definition: BLI_ghash.c:1023
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
#define M_SQRT3
Definition: BLI_math_base.h:53
MINLINE float max_ff(float a, float b)
MINLINE int power_of_2_max_i(int n)
MINLINE int compare_ff(float a, float b, const float max_diff)
#define M_SQRT2
Definition: BLI_math_base.h:47
MINLINE float min_ff(float a, float b)
#define M_SQRT1_3
Definition: BLI_math_base.h:56
MINLINE int max_ii(int a, int b)
#define M_PI_2
Definition: BLI_math_base.h:41
MINLINE int is_power_of_2_i(int n)
#define BLI_ASSERT_UNIT_V3(v)
#define M_PI
Definition: BLI_math_base.h:38
#define M_PI_4
Definition: BLI_math_base.h:44
void plane_from_point_normal_v3(float r_plane[4], const float plane_co[3], const float plane_no[3])
Definition: math_geom.c:243
float dist_squared_to_line_segment_v3(const float p[3], const float l1[3], const float l2[3])
Definition: math_geom.c:493
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])
Definition: math_geom.c:3103
float dist_squared_to_plane_v3(const float p[3], const float plane[4])
Definition: math_geom.c:440
float area_poly_v3(const float verts[][3], unsigned int nr)
Definition: math_geom.c:149
void closest_to_plane_normalized_v3(float r_close[3], const float plane[4], const float pt[3])
Definition: math_geom.c:412
void closest_to_plane_v3(float r_close[3], const float plane[4], const float pt[3])
Definition: math_geom.c:405
void closest_to_line_segment_v3(float r_close[3], const float p[3], const float l1[3], const float l2[3])
Definition: math_geom.c:375
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
Definition: math_geom.c:2191
void interp_bilinear_quad_v3(float data[4][3], float u, float v, float res[3])
Definition: math_geom.c:4766
void mul_m4_v4(const float M[4][4], float r[4])
Definition: math_matrix.c:866
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], const float t)
Definition: math_vector.c:49
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:443
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE bool compare_v3v3(const float a[3], const float b[3], const float limit) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v2_fl(float r[2], float f)
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
void mid_v3_v3v3(float r[3], const float a[3], const float b[3])
Definition: math_vector.c:270
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
float angle_v3v3v3(const float a[3], const float b[3], const float c[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:417
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
Definition: math_vector.c:505
MINLINE void add_v3_v3(float r[3], const float a[3])
void BLI_memarena_free(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:109
void BLI_memarena_use_calloc(struct MemArena *ma) ATTR_NONNULL(1)
Definition: BLI_memarena.c:91
void * BLI_memarena_alloc(struct MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
Definition: BLI_memarena.c:131
struct MemArena * BLI_memarena_new(const size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2) ATTR_MALLOC
Definition: BLI_memarena.c:79
#define SWAP(type, a, b)
#define POINTER_FROM_INT(i)
#define UNUSED_VARS_NDEBUG(...)
#define POINTER_AS_INT(i)
#define ELEM(...)
#define LIKELY(x)
typedef double(DMatrix)[4][4]
@ CD_CUSTOMLOOPNORMAL
@ CD_PROP_INT32
@ CD_BWEIGHT
@ CD_MLOOPUV
#define MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID
NSNotificationCenter * center
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble GLdouble r _GL_VOID_RET _GL_VOID GLfloat GLfloat r _GL_VOID_RET _GL_VOID GLint GLint r _GL_VOID_RET _GL_VOID GLshort GLshort r _GL_VOID_RET _GL_VOID GLdouble GLdouble r
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint i1
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
#define MEM_SIZE_OPTIMAL(size)
Platform independent time functions.
static void set_profile_params(BevelParams *bp, BevVert *bv, BoundVert *bndv)
Definition: bmesh_bevel.c:1684
static float find_profile_fullness(BevelParams *bp)
Definition: bmesh_bevel.c:7098
struct Profile Profile
static double find_superellipse_chord_endpoint(double x0, double dtarget, float r, bool rbig)
Definition: bmesh_bevel.c:6854
static void vmesh_center(VMesh *vm, float r_cent[3])
Definition: bmesh_bevel.c:3901
static bool make_unit_square_map(const float va[3], const float vmid[3], const float vb[3], float r_mat[4][4])
Definition: bmesh_bevel.c:1920
#define BM_BEVEL_EDGE_TAG_DISABLE(bme)
Definition: bmesh_bevel.c:5832
struct BevelParams BevelParams
static void regularize_profile_orientation(BevelParams *bp, BMEdge *bme)
Definition: bmesh_bevel.c:3466
static BMFace * boundvert_rep_face(BoundVert *v, BMFace **r_fother)
Definition: bmesh_bevel.c:610
static VMesh * pipe_adj_vmesh(BevelParams *bp, BevVert *bv, BoundVert *vpipe)
Definition: bmesh_bevel.c:4635
static void get_incident_edges(BMFace *f, BMVert *v, BMEdge **r_e1, BMEdge **r_e2)
Definition: bmesh_bevel.c:4697
static BMFace * choose_rep_face(BevelParams *bp, BMFace **face, int nfaces)
Definition: bmesh_bevel.c:928
AngleKind
Definition: bmesh_bevel.c:306
@ ANGLE_STRAIGHT
Definition: bmesh_bevel.c:310
@ ANGLE_SMALLER
Definition: bmesh_bevel.c:308
@ ANGLE_LARGER
Definition: bmesh_bevel.c:312
static int count_ccw_edges_between(EdgeHalf *e1, EdgeHalf *e2)
Definition: bmesh_bevel.c:573
static void move_profile_plane(BoundVert *bndv, BMVert *bmvert)
Definition: bmesh_bevel.c:1810
static bool fast_bevel_edge_order(BevVert *bv)
Definition: bmesh_bevel.c:5946
static void bevel_build_rings(BevelParams *bp, BMesh *bm, BevVert *bv, BoundVert *vpipe)
Definition: bmesh_bevel.c:5178
#define BEVEL_EPSILON_ANG
Definition: bmesh_bevel.c:58
static void build_boundary_terminal_edge(BevelParams *bp, BevVert *bv, EdgeHalf *efirst, const bool construct)
Definition: bmesh_bevel.c:2734
static void get_profile_point(BevelParams *bp, const Profile *pro, int i, int nseg, float r_co[3])
Definition: bmesh_bevel.c:2033
static BMEdge * find_closer_edge(float *co, BMEdge *e1, BMEdge *e2)
Definition: bmesh_bevel.c:4719
#define BEVEL_SMALL_ANG_DOT
Definition: bmesh_bevel.c:61
static FKind get_face_kind(BevelParams *bp, BMFace *f)
Definition: bmesh_bevel.c:425
static void disable_flag_out_edge(BMesh *bm, BMEdge *bme)
Definition: bmesh_bevel.c:411
static bool point_between_edges(const float co[3], BMVert *v, BMFace *f, EdgeHalf *e1, EdgeHalf *e2)
Definition: bmesh_bevel.c:1147
static BMFace * bev_create_ngon(BMesh *bm, BMVert **vert_arr, const int totv, BMFace **face_arr, BMFace *facerep, BMEdge **edge_arr, int mat_nr, bool do_interp)
Definition: bmesh_bevel.c:674
static NewVert * mesh_vert(VMesh *vm, int i, int j, int k)
Definition: bmesh_bevel.c:493
#define VERT_OUT
Definition: bmesh_bevel.c:393
BLI_INLINE void adjust_bound_vert(BoundVert *bv, const float co[3])
Definition: bmesh_bevel.c:483
static void adjust_miter_inner_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
Definition: bmesh_bevel.c:2921
static BevVert * find_bevvert(BevelParams *bp, BMVert *bmv)
Definition: bmesh_bevel.c:529
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])
Definition: bmesh_bevel.c:1215
#define BM_BEVEL_EDGE_TAG_TEST(bme)
Definition: bmesh_bevel.c:5833
static float sabin_gamma(int n)
Definition: bmesh_bevel.c:3927
static void weld_cross_attrs_copy(BMesh *bm, BevVert *bv, VMesh *vm, int vmindex, EdgeHalf *e)
Definition: bmesh_bevel.c:6670
static void bevel_set_weighted_normal_face_strength(BMesh *bm, BevelParams *bp)
Definition: bmesh_bevel.c:2569
static void flag_out_vert(BMesh *bm, BMVert *bmv)
Definition: bmesh_bevel.c:404
static void flag_out_edge(BMesh *bm, BMEdge *bme)
Definition: bmesh_bevel.c:397
#define BEVEL_EPSILON_ANG_DOT
Definition: bmesh_bevel.c:63
static void bevel_rebuild_existing_polygons(BMesh *bm, BevelParams *bp, BMVert *v)
Definition: bmesh_bevel.c:6558
static void record_face_kind(BevelParams *bp, BMFace *f, FKind fkind)
Definition: bmesh_bevel.c:418
#define BEVEL_SMALL_ANG
Definition: bmesh_bevel.c:59
struct NewVert NewVert
#define BEVEL_GOOD_ANGLE
Definition: bmesh_bevel.c:1496
static int tri_corner_test(BevelParams *bp, BevVert *bv)
Definition: bmesh_bevel.c:4441
#define VEC_VALUE_LEN
static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
Definition: bmesh_bevel.c:6707
static bool bev_rebuild_polygon(BMesh *bm, BevelParams *bp, BMFace *f)
Definition: bmesh_bevel.c:6350
struct EdgeHalf EdgeHalf
static void build_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv)
Definition: bmesh_bevel.c:5688
static BMFace * frep_for_center_poly(BevelParams *bp, BevVert *bv)
Definition: bmesh_bevel.c:4800
static bool good_offset_on_edge_between(EdgeHalf *e1, EdgeHalf *e2, EdgeHalf *emid, BMVert *v)
Definition: bmesh_bevel.c:1556
static void bev_merge_edge_uvs(BMesh *bm, BMEdge *bme, BMVert *v)
Definition: bmesh_bevel.c:1033
static bool bevvert_is_weld_cross(BevVert *bv)
Definition: bmesh_bevel.c:6644
static void adjust_the_cycle_or_chain(BoundVert *vstart, bool iscycle)
Definition: bmesh_bevel.c:3517
static void bevel_limit_offset(BevelParams *bp, BMesh *bm)
Definition: bmesh_bevel.c:7385
struct MathLayerInfo MathLayerInfo
static EdgeHalf * next_bev(BevVert *bv, EdgeHalf *from_e)
Definition: bmesh_bevel.c:558
#define BEV_EXTEND_EDGE_DATA_CHECK(eh, flag)
Definition: bmesh_bevel.c:2252
static void calculate_vm_profiles(BevelParams *bp, BevVert *bv, VMesh *vm)
Definition: bmesh_bevel.c:2673
static AngleKind edges_angle_kind(EdgeHalf *e1, EdgeHalf *e2, BMVert *v)
Definition: bmesh_bevel.c:1109
static void build_boundary_vertex_only(BevelParams *bp, BevVert *bv, bool construct)
Definition: bmesh_bevel.c:2694
static void offset_in_plane(EdgeHalf *e, const float plane_no[3], bool left, float r_co[3])
Definition: bmesh_bevel.c:1635
#define EDGE_OUT
Definition: bmesh_bevel.c:392
FKind
Definition: bmesh_bevel.c:292
@ F_RECON
Definition: bmesh_bevel.c:302
@ F_VERT
Definition: bmesh_bevel.c:298
@ F_ORIG
Definition: bmesh_bevel.c:296
@ F_EDGE
Definition: bmesh_bevel.c:300
@ F_NONE
Definition: bmesh_bevel.c:294
static void bevel_vert_two_edges(BevelParams *bp, BMesh *bm, BevVert *bv)
Definition: bmesh_bevel.c:5636
#define BEVEL_EPSILON_SQ
Definition: bmesh_bevel.c:55
static void closer_v3_v3v3v3(float r[3], const float a[3], const float b[3], const float v[3])
Definition: bmesh_bevel.c:4924
static BoundVert * add_new_bound_vert(MemArena *mem_arena, VMesh *vm, const float co[3])
Definition: bmesh_bevel.c:453
#define BM_BEVEL_EDGE_TAG_ENABLE(bme)
Definition: bmesh_bevel.c:5831
static VMesh * new_adj_vmesh(MemArena *mem_arena, int count, int seg, BoundVert *bounds)
Definition: bmesh_bevel.c:3827
static void set_bound_vert_seams(BevVert *bv, bool mark_seam, bool mark_sharp)
Definition: bmesh_bevel.c:2617
static void move_weld_profile_planes(BevVert *bv, BoundVert *bndv1, BoundVert *bndv2)
Definition: bmesh_bevel.c:1849
static void bev_merge_end_uvs(BMesh *bm, BevVert *bv, EdgeHalf *e)
Definition: bmesh_bevel.c:6629
static bool edge_edge_angle_less_than_180(const BMEdge *e1, const BMEdge *e2, const BMFace *f)
Definition: bmesh_bevel.c:1176
static void create_mesh_bmvert(BMesh *bm, VMesh *vm, int i, int j, int k, BMVert *eg)
Definition: bmesh_bevel.c:501
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)
Definition: bmesh_bevel.c:2062
static void bevel_build_trifan(BevelParams *bp, BMesh *bm, BevVert *bv)
Definition: bmesh_bevel.c:5578
static EdgeHalf * next_edgehalf_bev(BevelParams *bp, EdgeHalf *start_edge, bool toward_bv, BevVert **r_bv)
Definition: bmesh_bevel.c:3381
static void vmesh_copy_equiv_verts(VMesh *vm)
Definition: bmesh_bevel.c:3880
static int count_bound_vert_seams(BevVert *bv)
Definition: bmesh_bevel.c:2640
static bool offset_meet_edge(EdgeHalf *e1, EdgeHalf *e2, BMVert *v, float meetco[3], float *r_angle)
Definition: bmesh_bevel.c:1506
static EdgeHalf * find_edge_half(BevVert *bv, BMEdge *bme)
Definition: bmesh_bevel.c:518
static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
Definition: bmesh_bevel.c:2964
static VMesh * make_cube_corner_square_in(MemArena *mem_arena, int nseg)
Definition: bmesh_bevel.c:4316
static BMFace * bev_create_quad_ex(BMesh *bm, BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4, BMFace *f1, BMFace *f2, BMFace *f3, BMFace *f4, BMEdge *e1, BMEdge *e2, BMEdge *e3, BMEdge *e4, BMFace *frep, int mat_nr)
Definition: bmesh_bevel.c:754
static void build_center_ngon(BevelParams *bp, BMesh *bm, BevVert *bv, int mat_nr)
Definition: bmesh_bevel.c:4842
#define BEVEL_EPSILON
Definition: bmesh_bevel.c:54
static NewVert * mesh_vert_canon(VMesh *vm, int i, int j, int k)
Definition: bmesh_bevel.c:3849
static bool edges_face_connected_at_vert(BMEdge *bme1, BMEdge *bme2)
Definition: bmesh_bevel.c:591
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])
Definition: bmesh_bevel.c:1977
#define BEVEL_EPSILON_D
Definition: bmesh_bevel.c:53
static void adjust_offsets(BevelParams *bp, BMesh *bm)
Definition: bmesh_bevel.c:3679
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)
Definition: bmesh_bevel.c:1309
static bool offset_on_edge_between(BevelParams *bp, EdgeHalf *e1, EdgeHalf *e2, EdgeHalf *emid, BMVert *v, float meetco[3], float *r_sinratio)
Definition: bmesh_bevel.c:1572
static VMesh * cubic_subdiv(BevelParams *bp, VMesh *vm_in)
Definition: bmesh_bevel.c:4103
static void bevel_edges_sharp_boundary(BMesh *bm, BevelParams *bp)
Definition: bmesh_bevel.c:2411
struct ProfileSpacing ProfileSpacing
static void fill_profile_fracs(BevelParams *bp, BoundVert *bndv, float *frac, int ns)
Definition: bmesh_bevel.c:3979
static bool nearly_parallel(const float d1[3], const float d2[3])
Definition: bmesh_bevel.c:432
static void build_square_in_vmesh(BevelParams *bp, BMesh *bm, BevVert *bv, VMesh *vm1)
Definition: bmesh_bevel.c:4891
static bool is_canon(VMesh *vm, int i, int j, int k)
Definition: bmesh_bevel.c:3869
static bool is_outside_edge(EdgeHalf *e, const float co[3], BMVert **ret_closer_v)
Definition: bmesh_bevel.c:1088
static void find_even_superellipse_chords(int n, float r, double *xvals, double *yvals)
Definition: bmesh_bevel.c:7022
static float interp_poly_area(BevVert *bv, BMFace *frep)
Definition: bmesh_bevel.c:4755
static BevVert * bevel_vert_construct(BMesh *bm, BevelParams *bp, BMVert *v)
Definition: bmesh_bevel.c:6066
static int interp_range(const float *frac, int n, const float f, float *r_rest)
Definition: bmesh_bevel.c:4004
static float geometry_collide_offset(BevelParams *bp, EdgeHalf *eb)
Definition: bmesh_bevel.c:7245
static void set_profile_spacing(BevelParams *bp, ProfileSpacing *pro_spacing, bool custom)
Definition: bmesh_bevel.c:7159
#define PRO_CIRCLE_R
Definition: bmesh_bevel.c:160
static void slide_dist(EdgeHalf *e, BMVert *v, float d, float r_slideco[3])
Definition: bmesh_bevel.c:1074
static void fill_vmesh_fracs(VMesh *vm, float *frac, int i)
Definition: bmesh_bevel.c:3958
static BMFace * bev_create_quad(BMesh *bm, BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4, BMFace *f1, BMFace *f2, BMFace *f3, BMFace *f4, int mat_nr)
Definition: bmesh_bevel.c:738
static void bev_merge_uvs(BMesh *bm, BMVert *v)
Definition: bmesh_bevel.c:1001
#define PRO_SQUARE_IN_R
Definition: bmesh_bevel.c:162
static void avg4(float co[3], const NewVert *v0, const NewVert *v1, const NewVert *v2, const NewVert *v3)
Definition: bmesh_bevel.c:3917
static void check_edge_data_seam_sharp_edges(BevVert *bv, int flag, bool neg)
Definition: bmesh_bevel.c:2254
#define BEVEL_EPSILON_BIG
Definition: bmesh_bevel.c:56
#define PRO_SQUARE_R
Definition: bmesh_bevel.c:159
static void math_layer_info_init(BevelParams *bp, BMesh *bm)
Definition: bmesh_bevel.c:838
#define PRO_LINE_R
Definition: bmesh_bevel.c:161
static VMesh * make_cube_corner_square(MemArena *mem_arena, int nseg)
Definition: bmesh_bevel.c:4282
static VMesh * make_cube_corner_adj_vmesh(BevelParams *bp)
Definition: bmesh_bevel.c:4357
static void bevel_harden_normals(BevelParams *bp, BMesh *bm)
Harden normals for bevel.
Definition: bmesh_bevel.c:2446
static void bevel_build_cutoff(BevelParams *bp, BMesh *bm, BevVert *bv)
Definition: bmesh_bevel.c:5359
static void snap_to_pipe_profile(BoundVert *vpipe, bool midline, float co[3])
Definition: bmesh_bevel.c:4591
static void find_bevel_edge_order(BMesh *bm, BevVert *bv, BMEdge *first_bme)
Definition: bmesh_bevel.c:6003
static float vertex_collide_offset(BevelParams *bp, EdgeHalf *ea)
Definition: bmesh_bevel.c:7361
static EdgeHalf * find_other_end_edge_half(BevelParams *bp, EdgeHalf *e, BevVert **r_bvother)
Definition: bmesh_bevel.c:539
static void calculate_profile(BevelParams *bp, BoundVert *bndv, bool reversed, bool miter)
Definition: bmesh_bevel.c:2119
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 struct 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 float smoothresh, const struct CurveProfile *custom_profile, const int vmesh_method)
Definition: bmesh_bevel.c:7454
#define BM_ELEM_LONG_TAG
Definition: bmesh_bevel.c:389
static int bevel_edge_order_extend(BMesh *bm, BevVert *bv, int i)
Definition: bmesh_bevel.c:5844
static float snap_face_dist_squared(float *co, BMFace *f, BMEdge **r_snap_e, float *r_snap_co)
Definition: bmesh_bevel.c:4733
#define BEVEL_MATCH_SPEC_WEIGHT
Definition: bmesh_bevel.c:66
static float edge_face_angle(EdgeHalf *e)
Definition: bmesh_bevel.c:5821
static BoundVert * pipe_test(BevVert *bv)
Definition: bmesh_bevel.c:3786
static void adjust_miter_coords(BevelParams *bp, BevVert *bv, EdgeHalf *emiter)
Definition: bmesh_bevel.c:2872
static bool contig_ldata_across_edge(BMesh *bm, BMEdge *e, BMFace *f1, BMFace *f2)
Definition: bmesh_bevel.c:789
static VMesh * tri_corner_adj_vmesh(BevelParams *bp, BevVert *bv)
Definition: bmesh_bevel.c:4488
static void snap_to_superellipsoid(float co[3], const float super_r, bool midline)
Definition: bmesh_bevel.c:2182
static void bevel_reattach_wires(BMesh *bm, BevelParams *bp, BMVert *v)
Definition: bmesh_bevel.c:6580
static void find_even_superellipse_chords_general(int seg, float r, double *xvals, double *yvals)
Definition: bmesh_bevel.c:6922
static bool is_bad_uv_poly(BevVert *bv, BMFace *frep)
Definition: bmesh_bevel.c:4780
static BMFace * bevel_build_poly(BevelParams *bp, BMesh *bm, BevVert *bv)
Definition: bmesh_bevel.c:5510
static bool contig_ldata_across_loops(BMesh *bm, BMLoop *l1, BMLoop *l2, int layer_index)
Definition: bmesh_bevel.c:777
static bool nearly_parallel_normalized(const float d1[3], const float d2[3])
Definition: bmesh_bevel.c:442
static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto, int ifrom, int jfrom, int kfrom)
Definition: bmesh_bevel.c:509
static double superellipse_co(double x, float r, bool rbig)
Definition: bmesh_bevel.c:2012
static void bevel_extend_edge_data(BevVert *bv)
Definition: bmesh_bevel.c:2303
struct BevVert BevVert
struct BoundVert BoundVert
static VMesh * square_out_adj_vmesh(BevelParams *bp, BevVert *bv)
Definition: bmesh_bevel.c:4946
static bool eh_on_plane(EdgeHalf *e)
Definition: bmesh_bevel.c:2656
static VMesh * adj_vmesh(BevelParams *bp, BevVert *bv)
Definition: bmesh_bevel.c:4520
#define CIRCLE_FULLNESS_SEGS
struct VMesh VMesh
static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
Definition: bmesh_bevel.c:1886
static void project_to_edge(const BMEdge *e, const float co_a[3], const float co_b[3], float projco[3])
Definition: bmesh_bevel.c:1668
static VMesh * interp_vmesh(BevelParams *bp, VMesh *vm_in, int nseg)
Definition: bmesh_bevel.c:4031
#define BM_DEFAULT_NGON_STACK_SIZE
Definition: bmesh_class.h:571
@ BM_FACE
Definition: bmesh_class.h:386
@ BM_ELEM_SEAM
Definition: bmesh_class.h:473
@ BM_ELEM_SELECT
Definition: bmesh_class.h:471
@ BM_ELEM_SMOOTH
Definition: bmesh_class.h:477
@ BM_ELEM_TAG
Definition: bmesh_class.h:484
#define BM_FACE_FIRST_LOOP(p)
Definition: bmesh_class.h:553
#define BM_DEFAULT_ITER_STACK_SIZE
Definition: bmesh_class.h:576
#define BM_ELEM_CD_GET_VOID_P(ele, offset)
Definition: bmesh_class.h:530
void BM_elem_attrs_copy(BMesh *bm_src, BMesh *bm_dst, const void *ele_src, void *ele_dst)
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.
Definition: bmesh_core.c:58
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)
Definition: bmesh_core.c:500
void BM_vert_kill(BMesh *bm, BMVert *v)
Definition: bmesh_core.c:1002
void BM_face_kill(BMesh *bm, BMFace *f)
Definition: bmesh_core.c:881
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.
Definition: bmesh_core.c:147
@ BM_CREATE_NOP
Definition: bmesh_core.h:27
@ BM_CREATE_NO_DOUBLE
Definition: bmesh_core.h:29
#define BM_elem_index_get(ele)
Definition: bmesh_inline.h:124
#define BM_elem_flag_disable(ele, hflag)
Definition: bmesh_inline.h:29
#define BM_elem_flag_set(ele, hflag, val)
Definition: bmesh_inline.h:30
#define BM_elem_flag_test(ele, hflag)
Definition: bmesh_inline.h:26
#define BM_elem_flag_enable(ele, hflag)
Definition: bmesh_inline.h:28
float BM_elem_float_data_get(CustomData *cd, void *element, int type)
void BM_data_layer_add_named(BMesh *bm, CustomData *data, int type, const char *name)
Definition: bmesh_interp.c:912
void BM_loop_interp_from_face(BMesh *bm, BMLoop *l_dst, const BMFace *f_src, const bool do_vertex, const bool do_multires)
Definition: bmesh_interp.c:737
void * BM_iter_as_arrayN(BMesh *bm, const char itype, void *data, int *r_len, void **stack_array, int stack_array_size)
Iterator as Array.
#define BM_ITER_ELEM(ele, iter, data, itype)
#define BM_ITER_MESH(ele, iter, bm, itype)
@ BM_FACES_OF_EDGE
@ BM_FACES_OF_VERT
@ BM_EDGES_OF_MESH
@ BM_VERTS_OF_MESH
@ BM_FACES_OF_MESH
@ BM_LOOPS_OF_VERT
@ BM_LOOPS_OF_EDGE
@ BM_EDGES_OF_VERT
@ BM_EDGES_OF_FACE
@ BM_LOOPS_OF_FACE
#define BM_ITER_MESH_MUTABLE(ele, ele_next, iter, bm, itype)
ATTR_WARN_UNUSED_RESULT BMesh * bm
void BM_lnorspace_update(BMesh *bm)
Definition: bmesh_mesh.c:1565
void BM_mesh_normals_update(BMesh *bm)
BMesh Compute Normals.
Definition: bmesh_mesh.c:500
void BM_mesh_elem_table_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2276
void BM_mesh_elem_index_ensure(BMesh *bm, const char htype)
Definition: bmesh_mesh.c:2152
void BM_edges_sharp_from_angle_set(BMesh *bm, const float split_angle)
Definition: bmesh_mesh.c:1382
BLI_INLINE BMFace * BM_face_at_index(BMesh *bm, const int index)
Definition: bmesh_mesh.h:110
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.
Definition: bmesh_mods.c:252
#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)
@ BEVEL_VMESH_ADJ
@ BEVEL_VMESH_CUTOFF
@ BEVEL_AMT_WIDTH
@ BEVEL_AMT_ABSOLUTE
@ BEVEL_AMT_PERCENT
@ BEVEL_AMT_OFFSET
@ BEVEL_AMT_DEPTH
@ BEVEL_MITER_PATCH
@ BEVEL_MITER_SHARP
@ BEVEL_MITER_ARC
@ BEVEL_PROFILE_SUPERELLIPSE
@ BEVEL_PROFILE_CUSTOM
@ FACE_STRENGTH_STRONG
@ FACE_STRENGTH_WEAK
@ FACE_STRENGTH_MEDIUM
@ BEVEL_FACE_STRENGTH_NONE
@ BEVEL_FACE_STRENGTH_AFFECTED
@ BEVEL_FACE_STRENGTH_NEW
@ BEVEL_FACE_STRENGTH_ALL
@ BEVEL_AFFECT_VERTICES
void BM_face_calc_center_bounds(const BMFace *f, float r_cent[3])
BMEdge * BM_edge_exists(BMVert *v_a, BMVert *v_b)
Definition: bmesh_query.c:1995
bool BM_edge_loop_pair(BMEdge *e, BMLoop **r_la, BMLoop **r_lb)
Definition: bmesh_query.c:753
BMLoop * BM_face_edge_share_loop(BMFace *f, BMEdge *e)
Return the Loop Shared by Face and Edge.
Definition: bmesh_query.c:1426
int BM_edge_face_count(const BMEdge *e)
Definition: bmesh_query.c:847
float BM_edge_calc_length(const BMEdge *e)
Definition: bmesh_query.c:713
BMLoop * BM_vert_step_fan_loop(BMLoop *l, BMEdge **e_step)
Definition: bmesh_query.c:637
bool BM_vert_face_check(const BMVert *v)
Definition: bmesh_query.c:901
float BM_edge_calc_face_angle_signed_ex(const BMEdge *e, const float fallback)
BMESH EDGE/FACE ANGLE.
Definition: bmesh_query.c:1775
BMLoop * BM_face_vert_share_loop(BMFace *f, BMVert *v)
Return the Loop Shared by Face and Vertex.
Definition: bmesh_query.c:1403
BLI_INLINE bool BM_edge_is_manifold(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE BMVert * BM_edge_other_vert(BMEdge *e, const BMVert *v) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
BLI_INLINE bool BM_edge_is_wire(const BMEdge *e) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMLoop * l
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ATTR_WARN_UNUSED_RESULT const BMVert * v
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition: btDbvt.cpp:299
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
static T sum(const btAlignedObjectArray< T > &items)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition: btVector3.h:263
bool closest(btVector3 &v)
static float verts[][3]
GPUBatch * quad
int count
#define logf(x)
#define sinf(x)
#define cosf(x)
#define tanf(x)
#define powf(x, y)
#define fabsf(x)
#define sqrtf(x)
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_rhs)
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)
static MemArena * mem_arena
Definition: makesdna.c:155
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static int left
static char faces[256]
ccl_device_inline float frac(float x, int *ix)
static unsigned c
Definition: RandGen.cpp:97
static unsigned a[3]
Definition: RandGen.cpp:92
INLINE Rall1d< T, V, S > cos(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:319
INLINE Rall1d< T, V, S > pow(const Rall1d< T, V, S > &arg, double m)
Definition: rall1d.h:359
INLINE Rall1d< T, V, S > sin(const Rall1d< T, V, S > &arg)
Definition: rall1d.h:311
static void area(int d1, int d2, int e1, int e2, float weights[2])
vector snap(vector a, vector b)
Definition: node_math.h:72
BMVert * v1
Definition: bmesh_class.h:134
BMVert * v2
Definition: bmesh_class.h:134
struct BMLoop * l
Definition: bmesh_class.h:140
short mat_nr
Definition: bmesh_class.h:281
int len
Definition: bmesh_class.h:279
float no[3]
Definition: bmesh_class.h:280
void * data
Definition: bmesh_class.h:63
BMHeader head
Definition: bmesh_class.h:157
struct BMVert * v
Definition: bmesh_class.h:165
struct BMEdge * e
Definition: bmesh_class.h:176
struct BMLoop * radial_next
Definition: bmesh_class.h:216
struct BMLoop * prev
Definition: bmesh_class.h:245
struct BMFace * f
Definition: bmesh_class.h:183
struct BMLoop * next
Definition: bmesh_class.h:245
float co[3]
Definition: bmesh_class.h:99
struct BMEdge * e
Definition: bmesh_class.h:109
float no[3]
Definition: bmesh_class.h:100
struct MLoopNorSpaceArray * lnor_spacearr
Definition: bmesh_class.h:343
CustomData vdata
Definition: bmesh_class.h:337
CustomData edata
Definition: bmesh_class.h:337
uint use_toolflags
Definition: bmesh_class.h:333
CustomData pdata
Definition: bmesh_class.h:337
CustomData ldata
Definition: bmesh_class.h:337
int totface
Definition: bmesh_class.h:297
BMVert * v
Definition: bmesh_bevel.c:269
bool any_seam
Definition: bmesh_bevel.c:279
char _pad[6]
Definition: bmesh_bevel.c:283
float offset
Definition: bmesh_bevel.c:277
int edgecount
Definition: bmesh_bevel.c:271
bool visited
Definition: bmesh_bevel.c:281
int selcount
Definition: bmesh_bevel.c:273
int wirecount
Definition: bmesh_bevel.c:275
VMesh * vmesh
Definition: bmesh_bevel.c:288
BMEdge ** wire_edges
Definition: bmesh_bevel.c:286
EdgeHalf * edges
Definition: bmesh_bevel.c:284
MathLayerInfo math_layer_info
Definition: bmesh_bevel.c:328
const struct CurveProfile * custom_profile
Definition: bmesh_bevel.c:361
bool mark_seam
Definition: bmesh_bevel.c:354
const struct MDeformVert * dvert
Definition: bmesh_bevel.c:363
ProfileSpacing pro_spacing
Definition: bmesh_bevel.c:324
char _pad[1]
Definition: bmesh_bevel.c:359
GHash * vert_hash
Definition: bmesh_bevel.c:318
float pro_super_r
Definition: bmesh_bevel.c:344
bool loop_slide
Definition: bmesh_bevel.c:348
ProfileSpacing pro_spacing_miter
Definition: bmesh_bevel.c:326
bool harden_normals
Definition: bmesh_bevel.c:358
float offset
Definition: bmesh_bevel.c:332
bool use_weights
Definition: bmesh_bevel.c:346
int vmesh_method
Definition: bmesh_bevel.c:375
float spread
Definition: bmesh_bevel.c:377
GHash * face_hash
Definition: bmesh_bevel.c:320
int profile_type
Definition: bmesh_bevel.c:336
BMesh * bm
Definition: bmesh_bevel.c:330
bool limit_offset
Definition: bmesh_bevel.c:350
MemArena * mem_arena
Definition: bmesh_bevel.c:322
float smoothresh
Definition: bmesh_bevel.c:379
float profile
Definition: bmesh_bevel.c:342
int vertex_group
Definition: bmesh_bevel.c:365
int face_strength_mode
Definition: bmesh_bevel.c:369
bool mark_sharp
Definition: bmesh_bevel.c:356
bool offset_adjust
Definition: bmesh_bevel.c:352
struct BoundVert * next
Definition: bmesh_bevel.c:210
Profile profile
Definition: bmesh_bevel.c:226
bool visited
Definition: bmesh_bevel.c:230
NewVert nv
Definition: bmesh_bevel.c:211
EdgeHalf * eon
Definition: bmesh_bevel.c:216
EdgeHalf * elast
Definition: bmesh_bevel.c:214
struct BoundVert * prev
Definition: bmesh_bevel.c:210
bool any_seam
Definition: bmesh_bevel.c:228
struct BoundVert * adjchain
Definition: bmesh_bevel.c:224
bool is_patch_start
Definition: bmesh_bevel.c:234
bool is_profile_start
Definition: bmesh_bevel.c:236
EdgeHalf * efirst
Definition: bmesh_bevel.c:213
int sharp_len
Definition: bmesh_bevel.c:241
EdgeHalf * ebev
Definition: bmesh_bevel.c:218
float sinratio
Definition: bmesh_bevel.c:222
bool is_arc_start
Definition: bmesh_bevel.c:232
char _pad[3]
Definition: bmesh_bevel.c:237
int seam_len
Definition: bmesh_bevel.c:239
CurveProfilePoint * segments
CustomDataLayer * layers
float offset_r
Definition: bmesh_bevel.c:105
BMFace * fprev
Definition: bmesh_bevel.c:91
char _pad[4]
Definition: bmesh_bevel.c:118
struct EdgeHalf * next
Definition: bmesh_bevel.c:87
float offset_r_spec
Definition: bmesh_bevel.c:109
BMEdge * e
Definition: bmesh_bevel.c:89
struct BoundVert * leftv
Definition: bmesh_bevel.c:95
bool is_seam
Definition: bmesh_bevel.c:115
BMFace * fnext
Definition: bmesh_bevel.c:93
struct BoundVert * rightv
Definition: bmesh_bevel.c:97
bool is_bev
Definition: bmesh_bevel.c:111
bool is_rev
Definition: bmesh_bevel.c:113
int profile_index
Definition: bmesh_bevel.c:99
float offset_l_spec
Definition: bmesh_bevel.c:107
bool visited_rpo
Definition: bmesh_bevel.c:117
float offset_l
Definition: bmesh_bevel.c:103
struct EdgeHalf * prev
Definition: bmesh_bevel.c:87
MLoopNorSpace ** lspacearr
Definition: BKE_mesh.h:372
bool has_math_layers
Definition: bmesh_bevel.c:201
int * face_component
Definition: bmesh_bevel.c:199
char _pad[4]
Definition: bmesh_bevel.c:79
BMVert * v
Definition: bmesh_bevel.c:77
float co[3]
Definition: bmesh_bevel.c:78
double * yvals_2
Definition: bmesh_bevel.c:177
double * xvals_2
Definition: bmesh_bevel.c:175
double * yvals
Definition: bmesh_bevel.c:173
double * xvals
Definition: bmesh_bevel.c:171
bool special_params
Definition: bmesh_bevel.c:157
float end[3]
Definition: bmesh_bevel.c:145
float * prof_co
Definition: bmesh_bevel.c:153
float plane_co[3]
Definition: bmesh_bevel.c:149
float proj_dir[3]
Definition: bmesh_bevel.c:151
float * prof_co_2
Definition: bmesh_bevel.c:155
float start[3]
Definition: bmesh_bevel.c:141
float plane_no[3]
Definition: bmesh_bevel.c:147
float super_r
Definition: bmesh_bevel.c:137
float height
Definition: bmesh_bevel.c:139
float middle[3]
Definition: bmesh_bevel.c:143
int seg
Definition: bmesh_bevel.c:253
enum VMesh::@162 mesh_kind
int _pad
Definition: bmesh_bevel.c:263
BoundVert * boundstart
Definition: bmesh_bevel.c:249
@ M_TRI_FAN
Definition: bmesh_bevel.c:259
@ M_CUTOFF
Definition: bmesh_bevel.c:260
@ M_POLY
Definition: bmesh_bevel.c:257
@ M_NONE
Definition: bmesh_bevel.c:256
int count
Definition: bmesh_bevel.c:251
NewVert * mesh
Definition: bmesh_bevel.c:247
double PIL_check_seconds_timer(void)
Definition: time.c:80
__forceinline avxf cross(const avxf &a, const avxf &b)
Definition: util_avxf.h:119
ccl_device_inline float beta(float x, float y)
Definition: util_math.h:666
ccl_device_inline float2 fabs(const float2 &a)
#define dot3(a, b)
uint len