2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/bmesh/tools/bmesh_bevel.c
31 #include "MEM_guardedalloc.h"
33 #include "BLI_array.h"
35 #include "BLI_memarena.h"
37 #include "BKE_customdata.h"
43 /* experemental - Campbell */
44 // #define USE_ALTERNATE_ADJ
46 #define BEVEL_EPSILON 1e-6
49 // #pragma GCC diagnostic error "-Wpadded"
51 /* Constructed vertex, sometimes later instantiated as BMVert */
52 typedef struct NewVert {
60 /* Data for one end of an edge involved in a bevel */
61 typedef struct EdgeHalf {
62 struct EdgeHalf *next, *prev; /* in CCW order */
63 BMEdge *e; /* original mesh edge */
64 BMFace *fprev; /* face between this edge and previous, if any */
65 BMFace *fnext; /* face between this edge and next, if any */
66 struct BoundVert *leftv; /* left boundary vert (looking along edge to end) */
67 struct BoundVert *rightv; /* right boundary vert, if beveled */
68 short is_bev; /* is this edge beveled? */
69 short is_rev; /* is e->v2 the vertex at this end? */
70 int seg; /* how many segments for the bevel */
71 float offset; /* offset for this edge */
75 /* An element in a cyclic boundary of a Vertex Mesh (VMesh) */
76 typedef struct BoundVert {
77 struct BoundVert *next, *prev; /* in CCW order */
79 EdgeHalf *efirst; /* first of edges attached here: in CCW order */
81 EdgeHalf *ebev; /* beveled edge whose left side is attached here, if any */
82 int index; /* used for vmesh indexing */
86 /* Mesh structure replacing a vertex */
87 typedef struct VMesh {
88 NewVert *mesh; /* allocated array - size and structure depends on kind */
89 BoundVert *boundstart; /* start of boundary double-linked list */
90 int count; /* number of vertices in the boundary */
91 int seg; /* common # of segments for segmented edges */
93 M_NONE, /* no polygon mesh needed */
94 M_POLY, /* a simple polygon */
95 M_ADJ, /* "adjacent edges" mesh pattern */
96 // M_CROSS, /* "cross edges" mesh pattern */
97 M_TRI_FAN, /* a simple polygon - fan filled */
98 M_QUAD_STRIP, /* a simple polygon - cut into paralelle strips */
103 /* Data for a vertex involved in a bevel */
104 typedef struct BevVert {
105 BMVert *v; /* original mesh vertex */
106 int edgecount; /* total number of edges around the vertex */
107 int selcount; /* number of selected edges around the vertex */
108 EdgeHalf *edges; /* array of size edgecount; CCW order from vertex normal side */
109 VMesh *vmesh; /* mesh structure for replacing vertex */
112 /* Bevel parameters and state */
113 typedef struct BevelParams {
114 /* hash of BevVert for each vertex involved in bevel
115 * GHash: (key=(BMVert *), value=(BevVert *)) */
117 MemArena *mem_arena; /* use for all allocs while bevel runs, if we need to free we can switch to mempool */
119 float offset; /* blender units to offset each side of a beveled edge */
120 int seg; /* number of segments in beveled edge profile */
123 // #pragma GCC diagnostic ignored "-Wpadded"
125 //#include "bevdebug.c"
127 /* Make a new BoundVert of the given kind, insert it at the end of the circular linked
128 * list with entry point bv->boundstart, and return it. */
129 static BoundVert *add_new_bound_vert(MemArena *mem_arena, VMesh *vm, const float co[3])
131 BoundVert *ans = (BoundVert *)BLI_memarena_alloc(mem_arena, sizeof(BoundVert));
133 copy_v3_v3(ans->nv.co, co);
134 if (!vm->boundstart) {
136 vm->boundstart = ans;
137 ans->next = ans->prev = ans;
140 BoundVert *tail = vm->boundstart->prev;
141 ans->index = tail->index + 1;
143 ans->next = vm->boundstart;
145 vm->boundstart->prev = ans;
151 /* Mesh verts are indexed (i, j, k) where
152 * i = boundvert index (0 <= i < nv)
153 * j = ring index (0 <= j <= ns2)
154 * k = segment index (0 <= k <= ns)
155 * Not all of these are used, and some will share BMVerts */
156 static NewVert *mesh_vert(VMesh *vm, int i, int j, int k)
158 int nj = (vm->seg / 2) + 1;
159 int nk = vm->seg + 1;
161 return &vm->mesh[i * nk * nj + j * nk + k];
164 static void create_mesh_bmvert(BMesh *bm, VMesh *vm, int i, int j, int k, BMVert *eg)
166 NewVert *nv = mesh_vert(vm, i, j, k);
167 nv->v = BM_vert_create(bm, nv->co, eg, 0);
170 static void copy_mesh_vert(VMesh *vm, int ito, int jto, int kto,
171 int ifrom, int jfrom, int kfrom)
173 NewVert *nvto, *nvfrom;
175 nvto = mesh_vert(vm, ito, jto, kto);
176 nvfrom = mesh_vert(vm, ifrom, jfrom, kfrom);
178 copy_v3_v3(nvto->co, nvfrom->co);
181 /* find the EdgeHalf in bv's array that has edge bme */
182 static EdgeHalf *find_edge_half(BevVert *bv, BMEdge *bme)
186 for (i = 0; i < bv->edgecount; i++) {
187 if (bv->edges[i].e == bme)
188 return &bv->edges[i];
193 /* Return the next EdgeHalf after from_e that is beveled.
194 * If from_e is NULL, find the first beveled edge. */
195 static EdgeHalf *next_bev(BevVert *bv, EdgeHalf *from_e)
200 from_e = &bv->edges[bv->edgecount - 1];
206 } while ((e = e->next) != from_e);
210 /* find the BevVert corresponding to BMVert bmv */
211 static BevVert *find_bevvert(BevelParams *bp, BMVert *bmv)
213 return BLI_ghash_lookup(bp->vert_hash, bmv);
216 /* Return a good respresentative face (for materials, etc.) for faces
217 * created around/near BoundVert v */
218 static BMFace *boundvert_rep_face(BoundVert *v)
221 BMFace *firstf = NULL;
226 BLI_assert(v->efirst != NULL && v->elast != NULL);
229 BM_ITER_ELEM (f1, &iter1, e1, BM_FACES_OF_EDGE) {
232 BM_ITER_ELEM (f2, &iter2, e2, BM_FACES_OF_EDGE) {
246 * Make ngon from verts alone.
247 * Make sure to properly copy face attributes and do custom data interpolation from
248 * example face, facerep.
250 * \note ALL face creation goes through this function, this is important to keep!
252 static BMFace *bev_create_ngon(BMesh *bm, BMVert **vert_arr, const int totv, BMFace *facerep)
259 f = BM_face_create_quad_tri_v(bm, vert_arr, 3, facerep, FALSE);
261 else if (totv == 4) {
262 f = BM_face_create_quad_tri_v(bm, vert_arr, 4, facerep, FALSE);
267 BLI_array_fixedstack_declare(ee, BM_DEFAULT_NGON_STACK_SIZE, totv, __func__);
269 for (i = 0; i < totv; i++) {
270 ee[i] = BM_edge_create(bm, vert_arr[i], vert_arr[(i + 1) % totv], NULL, BM_CREATE_NO_DOUBLE);
272 f = BM_face_create_ngon(bm, vert_arr[0], vert_arr[1], ee, totv, 0);
273 BLI_array_fixedstack_free(ee);
276 int has_mdisps = CustomData_has_layer(&bm->ldata, CD_MDISPS);
277 BM_elem_attrs_copy(bm, bm, facerep, f);
278 BM_ITER_ELEM (l, &iter, f, BM_LOOPS_OF_FACE) {
279 BM_loop_interp_from_face(bm, l, facerep, TRUE, TRUE);
281 BM_loop_interp_multires(bm, l, facerep);
285 /* not essential for bevels own internal logic,
286 * this is done so the operator can select newly created faces */
288 BM_elem_flag_enable(f, BM_ELEM_TAG);
294 static BMFace *bev_create_quad_tri(BMesh *bm, BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4,
297 BMVert *varr[4] = {v1, v2, v3, v4};
298 return bev_create_ngon(bm, varr, v4 ? 4 : 3, facerep);
302 * Calculate the meeting point between the offset edges for e1 and e2, putting answer in meetco.
303 * e1 and e2 share vertex v and face f (may be NULL) and viewed from the normal side of
304 * the bevel vertex, e1 precedes e2 in CCW order.
305 * If on_right is true, offset edge is on right of both edges, where e1 enters v and
306 * e2 leave it. If on_right is false, then the offset edge is on the left.
307 * When offsets are equal, the new point is on the edge bisector, with length offset/sin(angle/2),
308 * but if the offsets are not equal (allowing for this, as bevel modifier has edge weights that may
309 * lead to different offsets) then meeting point can be found be intersecting offset lines.
311 static void offset_meet(EdgeHalf *e1, EdgeHalf *e2, BMVert *v, BMFace *f,
312 int on_right, float meetco[3])
314 float dir1[3], dir2[3], norm_v[3], norm_perp1[3], norm_perp2[3],
315 off1a[3], off1b[3], off2a[3], off2b[3], isect2[3];
317 /* get direction vectors for two offset lines */
318 sub_v3_v3v3(dir1, v->co, BM_edge_other_vert(e1->e, v)->co);
319 sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
321 if (angle_v3v3(dir1, dir2) < 100.0f * (float)BEVEL_EPSILON) {
322 /* special case: e1 and e2 are parallel; put offset point perp to both, from v.
323 * need to find a suitable plane.
324 * if offsets are different, we're out of luck: just use e1->offset */
326 copy_v3_v3(norm_v, f->no);
328 copy_v3_v3(norm_v, v->no);
329 cross_v3_v3v3(norm_perp1, dir1, norm_v);
330 normalize_v3(norm_perp1);
331 copy_v3_v3(off1a, v->co);
332 madd_v3_v3fl(off1a, norm_perp1, e1->offset);
333 copy_v3_v3(meetco, off1a);
336 /* get normal to plane where meet point should be */
337 cross_v3_v3v3(norm_v, dir2, dir1);
338 normalize_v3(norm_v);
342 /* get vectors perp to each edge, perp to norm_v, and pointing into face */
344 copy_v3_v3(norm_v, f->no);
346 cross_v3_v3v3(norm_perp1, dir1, norm_v);
347 cross_v3_v3v3(norm_perp2, dir2, norm_v);
348 normalize_v3(norm_perp1);
349 normalize_v3(norm_perp2);
351 /* get points that are offset distances from each line, then another point on each line */
352 copy_v3_v3(off1a, v->co);
353 madd_v3_v3fl(off1a, norm_perp1, e1->offset);
354 add_v3_v3v3(off1b, off1a, dir1);
355 copy_v3_v3(off2a, v->co);
356 madd_v3_v3fl(off2a, norm_perp2, e2->offset);
357 add_v3_v3v3(off2b, off2a, dir2);
359 /* intersect the lines; by construction they should be on the same plane and not parallel */
360 if (!isect_line_line_v3(off1a, off1b, off2a, off2b, meetco, isect2)) {
361 BLI_assert(!"offset_meet failure");
362 copy_v3_v3(meetco, off1a); /* just to do something */
367 /* Like offset_meet, but here f1 and f2 must not be NULL and give the
368 * planes in which to run the offset lines.
369 * They may not meet exactly: the offsets for the edges may be different
370 * or both the planes and the lines may be angled so that they can't meet.
371 * In that case, pick a close point on emid, which should be the dividing
372 * edge between the two planes.
373 * TODO: should have a global 'offset consistency' prepass to adjust offset
374 * widths so that all edges have the same offset at both ends. */
375 static void offset_in_two_planes(EdgeHalf *e1, EdgeHalf *e2, EdgeHalf *emid,
376 BMVert *v, BMFace *f1, BMFace *f2, float meetco[3])
378 float dir1[3], dir2[3], dirmid[3], norm_perp1[3], norm_perp2[3],
379 off1a[3], off1b[3], off2a[3], off2b[3], isect2[3], co[3],
383 BLI_assert(f1 != NULL && f2 != NULL);
385 /* get direction vectors for two offset lines */
386 sub_v3_v3v3(dir1, v->co, BM_edge_other_vert(e1->e, v)->co);
387 sub_v3_v3v3(dir2, BM_edge_other_vert(e2->e, v)->co, v->co);
388 sub_v3_v3v3(dirmid, BM_edge_other_vert(emid->e, v)->co, v->co);
390 /* get directions into offset planes */
391 /* calculate face normals at corner in case faces are nonplanar */
392 cross_v3_v3v3(f1no, dirmid, dir1);
393 cross_v3_v3v3(f2no, dirmid, dir2);
394 cross_v3_v3v3(norm_perp1, dir1, f1no);
395 normalize_v3(norm_perp1);
396 cross_v3_v3v3(norm_perp2, dir2, f2no);
397 normalize_v3(norm_perp2);
399 /* get points that are offset distances from each line, then another point on each line */
400 copy_v3_v3(off1a, v->co);
401 madd_v3_v3fl(off1a, norm_perp1, e1->offset);
402 sub_v3_v3v3(off1b, off1a, dir1);
403 copy_v3_v3(off2a, v->co);
404 madd_v3_v3fl(off2a, norm_perp2, e2->offset);
405 add_v3_v3v3(off2b, off2a, dir2);
407 if (angle_v3v3(dir1, dir2) < 100.0f * (float)BEVEL_EPSILON) {
408 /* lines are parallel; off1a is a good meet point */
409 copy_v3_v3(meetco, off1a);
412 iret = isect_line_line_v3(off1a, off1b, off2a, off2b, meetco, isect2);
414 /* lines colinear: another test says they are parallel. so shouldn't happen */
415 copy_v3_v3(meetco, off1a);
417 else if (iret == 2) {
418 /* lines are not coplanar; meetco and isect2 are nearest to first and second lines */
419 if (len_v3v3(meetco, isect2) > 100.0f * (float)BEVEL_EPSILON) {
420 /* offset lines don't meet: project average onto emid; this is not ideal (see TODO above) */
421 mid_v3_v3v3(co, meetco, isect2);
422 closest_to_line_v3(meetco, co, v->co, BM_edge_other_vert(emid->e, v)->co);
425 /* else iret == 1 and the lines are coplanar so meetco has the intersection */
429 /* Offset by e->offset in plane with normal plane_no, on left if left==TRUE,
430 * else on right. If no is NULL, choose an arbitrary plane different
431 * from eh's direction. */
432 static void offset_in_plane(EdgeHalf *e, const float plane_no[3], int left, float r[3])
434 float dir[3], no[3], fdir[3];
437 v = e->is_rev ? e->e->v2 : e->e->v1;
439 sub_v3_v3v3(dir, BM_edge_other_vert(e->e, v)->co, v->co);
442 copy_v3_v3(no, plane_no);
446 if (fabs(dir[0]) < fabs(dir[1]))
452 cross_v3_v3v3(fdir, dir, no);
454 cross_v3_v3v3(fdir, no, dir);
456 copy_v3_v3(r, v->co);
457 madd_v3_v3fl(r, fdir, e->offset);
460 /* Calculate coordinates of a point a distance d from v on e->e and return it in slideco */
461 static void slide_dist(EdgeHalf *e, BMVert *v, float d, float slideco[3])
465 sub_v3_v3v3(dir, v->co, BM_edge_other_vert(e->e, v)->co);
466 len = normalize_v3(dir);
468 d = len - (float)(50.0 * BEVEL_EPSILON);
469 copy_v3_v3(slideco, v->co);
470 madd_v3_v3fl(slideco, dir, -d);
473 /* Calculate the point on e where line (co_a, co_b) comes closest to and return it in projco */
474 static void project_to_edge(BMEdge *e, const float co_a[3], const float co_b[3], float projco[3])
478 if (!isect_line_line_v3(e->v1->co, e->v2->co, co_a, co_b, projco, otherco)) {
479 BLI_assert(!"project meet failure");
480 copy_v3_v3(projco, e->v1->co);
484 /* return 1 if a and b are in CCW order on the normal side of f,
485 * and -1 if they are reversed, and 0 if there is no shared face f */
486 static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
492 la = BM_face_edge_share_loop(f, a);
493 lb = BM_face_edge_share_loop(f, b);
496 return lb->next == la ? 1 : -1;
499 #ifdef USE_ALTERNATE_ADJ
501 static void vmesh_cent(VMesh *vm, float r_cent[3])
508 add_v3_v3(r_cent, v->nv.co);
509 } while ((v = v->next) != vm->boundstart);
510 mul_v3_fl(r_cent, 1.0f / (float)vm->count);
515 * This example shows a tri fan of quads,
516 * but could be an NGon fan of quads too.
518 * The whole triangle X
520 * new bevel face. / \
532 * interest -- / ---> X | \
536 * co_orig +-----------------+-----------------+
538 * For each quad, calcualte UV's based on the following:
539 * U = k / (vm->seg * 2)
540 * V = ring / (vm->seg * 2)
541 * quad = (co_orig, co_prev, co_cent, co_next)
542 * ... note that co_cent is the same for all quads in the fan.
547 static void get_point_uv(float uv[2],
548 /* all these args are int's originally
549 * but pass as floats to the function */
550 const float seg, const float ring, const float k)
552 uv[0] = (ring / seg) * 2.0f;
553 uv[1] = (k / seg) * 2.0f;
556 /* TODO: make this a lot smarter!,
557 * this is the main reason USE_ALTERNATE_ADJ isn't so good right now :S */
558 static float get_point_uv_factor(const float uv[2])
560 return sinf(1.0f - max_ff(uv[0], uv[1]) / 2.0f);
563 static void get_point_on_round_edge(const float uv[2],
567 interp_bilinear_quad_v3(quad, uv[0], uv[1], r_co);
570 #else /* USE_ALTERNATE_ADJ */
572 #ifdef OLD_ROUND_EDGE
574 * calculation of points on the round profile
575 * r - result, coordinate of point on round profile
577 * Inscribe a circle in angle va - v -vb
578 * such that it touches the arms at offset from v.
579 * Rotate the center-va segment by (i/n) of the
580 * angle va - center -vb, and put the endpoint
581 * of that segment in r.
583 static void get_point_on_round_profile(float r_co[3], float offset, int k, int count,
584 const float va[3], const float v[3], const float vb[3])
586 float vva[3], vvb[3], angle, center[3], rv[3], axis[3], co[3];
588 sub_v3_v3v3(vva, va, v);
589 sub_v3_v3v3(vvb, vb, v);
592 angle = angle_normalized_v3v3(vva, vvb);
594 add_v3_v3v3(center, vva, vvb);
595 normalize_v3(center);
596 mul_v3_fl(center, offset * (1.0f / cosf(0.5f * angle)));
597 add_v3_v3(center, v); /* coordinates of the center of the inscribed circle */
600 sub_v3_v3v3(rv, va, center); /* radius vector */
603 sub_v3_v3v3(co, v, center);
604 cross_v3_v3v3(axis, rv, co); /* calculate axis */
606 sub_v3_v3v3(vva, va, center);
607 sub_v3_v3v3(vvb, vb, center);
608 angle = angle_v3v3(vva, vvb);
610 rotate_v3_v3v3fl(co, rv, axis, angle * (float)k / (float)count);
612 add_v3_v3(co, center);
613 copy_v3_v3(r_co, co);
617 * Find the point (/n) of the way around the round profile for e,
618 * where start point is va, midarc point is vmid, and end point is vb.
619 * Return the answer in profileco.
621 * Adjust va and vb (along edge direction) so that they are perpendicular
622 * to edge at v, then use get_point_on_round_profile, then project
623 * back onto original va - vmid - vb plane.
624 * If va, vmid, and vb are all on the same plane, just interpolate between va and vb.
626 static void get_point_on_round_edge(EdgeHalf *e, int k,
627 const float va[3], const float vmid[3], const float vb[3],
630 float vva[3], vvb[3], point[3], dir[3], vaadj[3], vbadj[3], p2[3], pn[3];
633 sub_v3_v3v3(vva, va, vmid);
634 sub_v3_v3v3(vvb, vb, vmid);
636 sub_v3_v3v3(dir, e->e->v1->co, e->e->v2->co);
638 sub_v3_v3v3(dir, e->e->v2->co, e->e->v1->co);
640 if (fabsf(angle_v3v3(vva, vvb) - (float)M_PI) > 100.f *(float)BEVEL_EPSILON) {
641 copy_v3_v3(vaadj, va);
642 madd_v3_v3fl(vaadj, dir, -len_v3(vva) * cosf(angle_v3v3(vva, dir)));
643 copy_v3_v3(vbadj, vb);
644 madd_v3_v3fl(vbadj, dir, -len_v3(vvb) * cosf(angle_v3v3(vvb, dir)));
646 get_point_on_round_profile(point, e->offset, k, n, vaadj, vmid, vbadj);
648 add_v3_v3v3(p2, point, dir);
649 cross_v3_v3v3(pn, vva, vvb);
650 if (!isect_line_plane_v3(r_co, point, p2, vmid, pn, 0)) {
651 /* TODO: track down why this sometimes fails */
652 copy_v3_v3(r_co, point);
657 interp_v3_v3v3(r_co, va, vb, (float)k / (float)n);
663 * Find the point (/n) of the way around the round profile for e,
664 * where start point is va, midarc point is vmid, and end point is vb.
665 * Return the answer in profileco.
667 * Find vo, the origin of the parallelogram with other three points va, vmid, vb.
668 * Also find vd, which is in direction normal to parallelogram and 1 unit away
670 * The quarter circle in first quadrant of unit square will be mapped to the
671 * quadrant of a sheared ellipse in the parallelgram, using a matrix.
672 * The matrix mat is calculated to map:
677 * However if va -- vmid -- vb is approximately a straight line, just
678 * interpolate along the line.
680 static void get_point_on_round_edge(EdgeHalf *e, int k,
681 const float va[3], const float vmid[3], const float vb[3],
684 float vo[3], vd[3], vb_vmid[3], va_vmid[3], vddir[3], p[3], angle;
685 float m[4][4] = MAT4_UNITY;
688 sub_v3_v3v3(va_vmid, vmid, va);
689 sub_v3_v3v3(vb_vmid, vmid, vb);
690 if (fabsf(angle_v3v3(va_vmid, vb_vmid) - (float)M_PI) > 100.f *(float)BEVEL_EPSILON) {
691 sub_v3_v3v3(vo, va, vb_vmid);
692 cross_v3_v3v3(vddir, vb_vmid, va_vmid);
694 add_v3_v3v3(vd, vo, vddir);
696 /* The cols of m are: {vmid - va, vmid - vb, vmid + vd - va -vb, va + vb - vmid;
697 * blender transform matrices are stored such that m[i][*] is ith column;
698 * the last elements of each col remain as they are in unity matrix */
699 sub_v3_v3v3(&m[0][0], vmid, va);
700 sub_v3_v3v3(&m[1][0], vmid, vb);
701 add_v3_v3v3(&m[2][0], vmid, vd);
702 sub_v3_v3(&m[2][0], va);
703 sub_v3_v3(&m[2][0], vb);
704 add_v3_v3v3(&m[3][0], va, vb);
705 sub_v3_v3(&m[3][0], vmid);
707 /* Now find point k/(e->seg) along quarter circle from (0,1,0) to (1,0,0) */
708 angle = (float)M_PI * (float)k / (2.0f * (float)n); /* angle from y axis */
712 mul_v3_m4v3(r_co, m, p);
716 interp_v3_v3v3(r_co, va, vb, (float)k / (float)n);
719 #endif /* ! OLD_ROUND_EDGE */
721 #endif /* !USE_ALTERNATE_ADJ */
723 /* Make a circular list of BoundVerts for bv, each of which has the coordinates
724 * of a vertex on the the boundary of the beveled vertex bv->v.
725 * Also decide on the mesh pattern that will be used inside the boundary.
726 * Doesn't make the actual BMVerts */
727 static void build_boundary(MemArena *mem_arena, BevVert *bv)
729 EdgeHalf *efirst, *e;
736 e = efirst = next_bev(bv, NULL);
739 BLI_assert(bv->edgecount >= 2); /* since bevel edges incident to 2 faces */
741 if (bv->edgecount == 2 && bv->selcount == 1) {
742 /* special case: beveled edge meets non-beveled one at valence 2 vert */
743 no = e->fprev ? e->fprev->no : (e->fnext ? e->fnext->no : NULL);
744 offset_in_plane(e, no, TRUE, co);
745 v = add_new_bound_vert(mem_arena, vm, co);
746 v->efirst = v->elast = v->ebev = e;
748 no = e->fnext ? e->fnext->no : (e->fprev ? e->fprev->no : NULL);
749 offset_in_plane(e, no, FALSE, co);
750 v = add_new_bound_vert(mem_arena, vm, co);
751 v->efirst = v->elast = e;
753 /* make artifical extra point along unbeveled edge, and form triangle */
754 slide_dist(e->next, bv->v, e->offset, co);
755 v = add_new_bound_vert(mem_arena, vm, co);
756 v->efirst = v->elast = e->next;
757 e->next->leftv = e->next->rightv = v;
758 vm->mesh_kind = M_POLY;
763 vm->boundstart = NULL;
766 /* handle only left side of beveled edge e here: next iteration should do right side */
767 if (e->prev->is_bev) {
768 BLI_assert(e->prev != e); /* see: wire edge special case */
769 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
770 v = add_new_bound_vert(mem_arena, vm, co);
772 v->elast = v->ebev = e;
777 /* e->prev is not beveled */
778 if (e->prev->prev->is_bev) {
779 BLI_assert(e->prev->prev != e); /* see: edgecount 2, selcount 1 case */
780 /* find meet point between e->prev->prev and e and attach e->prev there */
781 offset_in_two_planes(e->prev->prev, e, e->prev, bv->v,
782 e->prev->prev->fnext, e->fprev, co);
783 v = add_new_bound_vert(mem_arena, vm, co);
784 v->efirst = e->prev->prev;
785 v->elast = v->ebev = e;
788 e->prev->prev->rightv = v;
791 /* neither e->prev nor e->prev->prev are beveled: make on-edge on e->prev */
792 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
793 v = add_new_bound_vert(mem_arena, vm, co);
795 v->elast = v->ebev = e;
800 lastd = len_v3v3(bv->v->co, v->nv.co);
803 /* e is not beveled */
804 if (e->next->is_bev) {
805 /* next iteration will place e between beveled previous and next edges */
808 else if (e->prev->is_bev) {
809 /* on-edge meet between e->prev and e */
810 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
811 v = add_new_bound_vert(mem_arena, vm, co);
818 /* None of e->prev, e, e->next are beveled.
819 * could either leave alone or add slide points to make
820 * one polygon around bv->v. For now, we choose latter.
821 * Could slide to make an even bevel plane but for now will
822 * just use last distance a meet point moved from bv->v. */
823 slide_dist(e, bv->v, lastd, co);
824 v = add_new_bound_vert(mem_arena, vm, co);
825 v->efirst = v->elast = e;
829 } while ((e = e->next) != efirst);
831 BLI_assert(vm->count >= 2);
832 if (vm->count == 2 && bv->edgecount == 3) {
833 vm->mesh_kind = M_NONE;
835 else if (bv->selcount == 2) {
836 vm->mesh_kind = M_QUAD_STRIP;
838 else if (efirst->seg == 1 || bv->selcount == 1) {
839 if (vm->count == 3 && bv->selcount == 1) {
840 vm->mesh_kind = M_TRI_FAN;
843 vm->mesh_kind = M_POLY;
847 vm->mesh_kind = M_ADJ;
852 * Given that the boundary is built and the boundary BMVerts have been made,
853 * calculate the positions of the interior mesh points for the M_ADJ pattern,
854 * then make the BMVerts and the new faces. */
855 static void bevel_build_rings(BMesh *bm, BevVert *bv)
857 int k, ring, i, n, ns, ns2, nn;
858 VMesh *vm = bv->vmesh;
859 BoundVert *v, *vprev, *vnext;
860 NewVert *nv, *nvprev, *nvnext;
861 BMVert *bmv, *bmv1, *bmv2, *bmv3, *bmv4;
863 float co[3], coa[3], cob[3], midco[3];
865 #ifdef USE_ALTERNATE_ADJ
866 /* ordered as follows (orig, prev, center, next)*/
867 float quad_plane[4][3];
868 float quad_orig[4][3];
872 #ifdef USE_ALTERNATE_ADJ
873 /* the rest are initialized inline, this remains the same for all */
874 vmesh_cent(vm, quad_plane[2]);
875 copy_v3_v3(quad_orig[2], bv->v->co);
881 BLI_assert(n > 2 && ns > 1);
882 /* Make initial rings, going between points on neighbors.
883 * After this loop, will have coords for all (i, r, k) where
884 * BoundVert for i has a bevel, 0 <= r <= ns2, 0 <= k <= ns */
885 for (ring = 1; ring <= ns2; ring++) {
891 /* get points coords of points a and b, on outer rings
892 * of prev and next edges, k away from this edge */
897 nvprev = mesh_vert(vm, vprev->index, 0, ns - ring);
899 nvprev = mesh_vert(vm, vprev->index, 0, ns);
900 copy_v3_v3(coa, nvprev->co);
901 nv = mesh_vert(vm, i, ring, 0);
902 copy_v3_v3(nv->co, coa);
906 nvnext = mesh_vert(vm, vnext->index, 0, ring);
908 nvnext = mesh_vert(vm, vnext->index, 0, 0);
909 copy_v3_v3(cob, nvnext->co);
910 nv = mesh_vert(vm, i, ring, ns);
911 copy_v3_v3(nv->co, cob);
914 #ifdef USE_ALTERNATE_ADJ
916 copy_v3_v3(quad_plane[0], v->nv.co);
917 mid_v3_v3v3(quad_plane[1], v->nv.co, v->prev->nv.co);
919 mid_v3_v3v3(quad_plane[3], v->nv.co, v->next->nv.co);
922 copy_v3_v3(quad_orig[0], v->nv.co); /* only shared location between 2 quads */
923 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig[1]);
924 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig[3]);
926 //bl_debug_draw_quad_add(UNPACK4(quad_plane));
927 //bl_debug_draw_quad_add(UNPACK4(quad_orig));
930 #ifdef USE_ALTERNATE_ADJ
931 for (k = 1; k < ns; k++) {
937 get_point_uv(uv, v->ebev->seg, ring, k);
938 get_point_on_round_edge(uv, quad_plane, co_plane);
939 get_point_on_round_edge(uv, quad_orig, co_orig);
940 fac = get_point_uv_factor(uv);
941 interp_v3_v3v3(co, co_plane, co_orig, fac);
942 copy_v3_v3(mesh_vert(vm, i, ring, k)->co, co);
945 /* TODO: better calculation of new midarc point? */
946 project_to_edge(v->ebev->e, coa, cob, midco);
948 for (k = 1; k < ns; k++) {
949 get_point_on_round_edge(v->ebev, k, coa, midco, cob, co);
950 copy_v3_v3(mesh_vert(vm, i, ring, k)->co, co);
954 } while ((v = v->next) != vm->boundstart);
957 /* Now make sure cross points of rings share coordinates and vertices.
958 * After this loop, will have BMVerts for all (i, r, k) where
959 * i is for a BoundVert that is beveled and has either a predecessor or
960 * successor BoundVert beveled too, and
961 * for odd ns: 0 <= r <= ns2, 0 <= k <= ns
962 * for even ns: 0 <= r < ns2, 0 <= k <= ns except k=ns2 */
970 for (ring = 1; ring <= ns2; ring++) {
971 for (k = 1; k <= ns2; k++) {
972 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
973 continue; /* center line is special case: do after the rest are done */
974 nv = mesh_vert(vm, i, ring, k);
975 nvprev = mesh_vert(vm, vprev->index, k, ns - ring);
976 mid_v3_v3v3(co, nv->co, nvprev->co);
977 #ifndef USE_ALTERNATE_ADJ
978 copy_v3_v3(nv->co, co);
980 BLI_assert(nv->v == NULL && nvprev->v == NULL);
981 create_mesh_bmvert(bm, vm, i, ring, k, bv->v);
982 copy_mesh_vert(vm, vprev->index, k, ns - ring, i, ring, k);
985 if (!vprev->prev->ebev) {
986 for (ring = 1; ring <= ns2; ring++) {
987 for (k = 1; k <= ns2; k++) {
988 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
990 create_mesh_bmvert(bm, vm, vprev->index, ring, k, bv->v);
995 for (ring = 1; ring <= ns2; ring++) {
996 for (k = ns - ns2; k < ns; k++) {
997 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
999 create_mesh_bmvert(bm, vm, i, ring, k, bv->v);
1005 } while ((v = v->next) != vm->boundstart);
1008 /* Do special case center lines.
1009 * This loop makes verts for (i, ns2, k) for 1 <= k <= ns-1, k!=ns2
1010 * and for (i, r, ns2) for 1 <= r <= ns2-1,
1011 * whenever i is in a sequence of at least two beveled verts */
1018 for (k = 1; k < ns2; k++) {
1019 nv = mesh_vert(vm, i, k, ns2);
1021 nvprev = mesh_vert(vm, vprev->index, ns2, ns - k);
1023 nvnext = mesh_vert(vm, vnext->index, ns2, k);
1024 if (vprev->ebev && vnext->ebev) {
1025 mid_v3_v3v3v3(co, nvprev->co, nv->co, nvnext->co);
1026 #ifndef USE_ALTERNATE_ADJ
1027 copy_v3_v3(nv->co, co);
1029 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1030 copy_mesh_vert(vm, vprev->index, ns2, ns - k, i, k, ns2);
1031 copy_mesh_vert(vm, vnext->index, ns2, k, i, k, ns2);
1034 else if (vprev->ebev) {
1035 mid_v3_v3v3(co, nvprev->co, nv->co);
1036 #ifndef USE_ALTERNATE_ADJ
1037 copy_v3_v3(nv->co, co);
1039 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1040 copy_mesh_vert(vm, vprev->index, ns2, ns - k, i, k, ns2);
1042 create_mesh_bmvert(bm, vm, i, ns2, ns - k, bv->v);
1044 else if (vnext->ebev) {
1045 mid_v3_v3v3(co, nv->co, nvnext->co);
1046 #ifndef USE_ALTERNATE_ADJ
1047 copy_v3_v3(nv->co, co);
1049 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1050 copy_mesh_vert(vm, vnext->index, ns2, k, i, k, ns2);
1052 create_mesh_bmvert(bm, vm, i, ns2, k, bv->v);
1056 } while ((v = v->next) != vm->boundstart);
1058 /* center point need to be average of all centers of rings */
1059 /* TODO: this is wrong if not all verts have ebev: could have
1060 * several disconnected sections of mesh. */
1067 nv = mesh_vert(vm, i, ns2, ns2);
1068 add_v3_v3(midco, nv->co);
1071 } while ((v = v->next) != vm->boundstart);
1072 mul_v3_fl(midco, 1.0f / nn);
1073 bmv = BM_vert_create(bm, midco, NULL, 0);
1078 nv = mesh_vert(vm, i, ns2, ns2);
1079 copy_v3_v3(nv->co, midco);
1082 } while ((v = v->next) != vm->boundstart);
1085 /* Make the ring quads */
1086 for (ring = 0; ring < ns2; ring++) {
1090 f = boundvert_rep_face(v);
1091 if (v->ebev && (v->prev->ebev || v->next->ebev)) {
1092 for (k = 0; k < ns2 + (ns % 2); k++) {
1093 bmv1 = mesh_vert(vm, i, ring, k)->v;
1094 bmv2 = mesh_vert(vm, i, ring, k + 1)->v;
1095 bmv3 = mesh_vert(vm, i, ring + 1, k + 1)->v;
1096 bmv4 = mesh_vert(vm, i, ring + 1, k)->v;
1097 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1098 if (bmv3 == bmv4 || bmv1 == bmv4)
1100 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f);
1103 else if (v->prev->ebev && v->prev->prev->ebev) {
1104 /* finish off a sequence of beveled edges */
1106 f = boundvert_rep_face(v->prev);
1107 for (k = ns2 + (ns % 2); k < ns; k++) {
1108 bmv1 = mesh_vert(vm, i, ring, k)->v;
1109 bmv2 = mesh_vert(vm, i, ring, k + 1)->v;
1110 bmv3 = mesh_vert(vm, i, ring + 1, k + 1)->v;
1111 bmv4 = mesh_vert(vm, i, ring + 1, k)->v;
1112 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1117 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f);
1120 } while ((v = v->next) != vm->boundstart);
1123 /* Make center ngon if odd number of segments and fully beveled */
1124 if (ns % 2 == 1 && vm->count == bv->selcount) {
1126 BLI_array_declare(vv);
1131 BLI_assert(v->ebev);
1132 BLI_array_append(vv, mesh_vert(vm, i, ns2, ns2)->v);
1133 } while ((v = v->next) != vm->boundstart);
1134 f = boundvert_rep_face(vm->boundstart);
1135 bev_create_ngon(bm, vv, BLI_array_count(vv), f);
1140 /* Make 'rest-of-vmesh' polygon if not fully beveled */
1141 if (vm->count > bv->selcount) {
1144 BLI_array_declare(vv);
1147 f = boundvert_rep_face(v);
1152 if (!v->prev->ebev) {
1153 for (k = 0; k < ns2; k++) {
1154 bmv1 = mesh_vert(vm, i, ns2, k)->v;
1156 bmv1 = mesh_vert(vm, i, 0, k)->v;
1157 if (!(j > 0 && bmv1 == vv[j - 1])) {
1158 BLI_assert(bmv1 != NULL);
1159 BLI_array_append(vv, bmv1);
1164 bmv1 = mesh_vert(vm, i, ns2, ns2)->v;
1166 bmv1 = mesh_vert(vm, i, 0, ns2)->v;
1167 if (!(j > 0 && bmv1 == vv[j - 1])) {
1168 BLI_assert(bmv1 != NULL);
1169 BLI_array_append(vv, bmv1);
1172 if (!v->next->ebev) {
1173 for (k = ns - ns2; k < ns; k++) {
1174 bmv1 = mesh_vert(vm, i, ns2, k)->v;
1176 bmv1 = mesh_vert(vm, i, 0, k)->v;
1177 if (!(j > 0 && bmv1 == vv[j - 1])) {
1178 BLI_assert(bmv1 != NULL);
1179 BLI_array_append(vv, bmv1);
1186 BLI_assert(mesh_vert(vm, i, 0, 0)->v != NULL);
1187 BLI_array_append(vv, mesh_vert(vm, i, 0, 0)->v);
1190 } while ((v = v->next) != vm->boundstart);
1191 if (vv[0] == vv[j - 1])
1193 bev_create_ngon(bm, vv, j, f);
1199 static BMFace *bevel_build_poly_ex(BMesh *bm, BevVert *bv)
1203 VMesh *vm = bv->vmesh;
1206 BLI_array_declare(vv);
1211 /* accumulate vertices for vertex ngon */
1212 BLI_array_append(vv, v->nv.v);
1214 if (v->ebev && v->ebev->seg > 1) {
1215 for (k = 1; k < v->ebev->seg; k++) {
1216 BLI_array_append(vv, mesh_vert(vm, v->index, 0, k)->v);
1220 } while ((v = v->next) != vm->boundstart);
1222 f = bev_create_ngon(bm, vv, n, boundvert_rep_face(v));
1231 static void bevel_build_poly(BMesh *bm, BevVert *bv)
1233 bevel_build_poly_ex(bm, bv);
1236 static void bevel_build_trifan(BMesh *bm, BevVert *bv)
1239 BLI_assert(next_bev(bv, NULL)->seg == 1 || bv->selcount == 1);
1241 f = bevel_build_poly_ex(bm, bv);
1244 /* we have a polygon which we know starts at the previous vertex, make it into a fan */
1245 BMLoop *l_fan = BM_FACE_FIRST_LOOP(f)->prev;
1246 BMVert *v_fan = l_fan->v;
1248 while (f->len > 3) {
1251 BLI_assert(v_fan == l_fan->v);
1252 f_new = BM_face_split(bm, f, l_fan->v, l_fan->next->next->v, &l_new, NULL, FALSE);
1254 if (f_new->len > f->len) {
1256 if (l_new->v == v_fan) { l_fan = l_new; }
1257 else if (l_new->next->v == v_fan) { l_fan = l_new->next; }
1258 else if (l_new->prev->v == v_fan) { l_fan = l_new->prev; }
1259 else { BLI_assert(0); }
1262 if (l_fan->v == v_fan) { l_fan = l_fan; }
1263 else if (l_fan->next->v == v_fan) { l_fan = l_fan->next; }
1264 else if (l_fan->prev->v == v_fan) { l_fan = l_fan->prev; }
1265 else { BLI_assert(0); }
1271 static void bevel_build_quadstrip(BMesh *bm, BevVert *bv)
1274 BLI_assert(bv->selcount == 2);
1276 f = bevel_build_poly_ex(bm, bv);
1279 /* we have a polygon which we know starts at this vertex, make it into strips */
1280 EdgeHalf *eh_a = bv->vmesh->boundstart->elast;
1281 EdgeHalf *eh_b = next_bev(bv, eh_a->next); /* since (selcount == 2) we know this is valid */
1282 BMLoop *l_a = BM_face_vert_share_loop(f, eh_a->rightv->nv.v);
1283 BMLoop *l_b = BM_face_vert_share_loop(f, eh_b->leftv->nv.v);
1284 int seg_count = bv->vmesh->seg; /* ensure we don't walk past the segments */
1287 /* step once around if we hit the same loop */
1293 BLI_assert(l_a != l_b);
1295 while (f->len > 4) {
1297 BLI_assert(l_a->f == f);
1298 BLI_assert(l_b->f == f);
1300 BM_face_split(bm, f, l_a->v, l_b->v, &l_new, NULL, FALSE);
1301 if (seg_count-- == 0) {
1305 /* turns out we don't need this,
1306 * because of how BM_face_split works we always get the loop of the next face */
1308 if (l_new->f->len < l_new->radial_next->f->len) {
1309 l_new = l_new->radial_next;
1314 /* walk around the new face to get the next verts to split */
1316 l_b = l_new->next->next;
1321 /* Given that the boundary is built, now make the actual BMVerts
1322 * for the boundary and the interior of the vertex mesh. */
1323 static void build_vmesh(MemArena *mem_arena, BMesh *bm, BevVert *bv)
1325 VMesh *vm = bv->vmesh;
1326 BoundVert *v, *weld1, *weld2;
1327 int n, ns, ns2, i, k, weld;
1328 float *va, *vb, co[3];
1330 #ifdef USE_ALTERNATE_ADJ
1331 /* ordered as follows (orig, prev, center, next)*/
1332 float quad_plane[4][3];
1333 float quad_orig_a[4][3];
1334 float quad_orig_b[4][3];
1335 const int is_odd = (vm->seg % 2);
1340 #ifdef USE_ALTERNATE_ADJ
1341 /* the rest are initialized inline, this remains the same for all */
1342 /* NOTE; in this usage we only interpolate on the 'V' so cent and next points are unused (2,3)*/
1343 vmesh_cent(vm, quad_plane[2]);
1344 copy_v3_v3(quad_orig_a[2], bv->v->co);
1345 copy_v3_v3(quad_orig_b[2], bv->v->co);
1352 vm->mesh = (NewVert *)BLI_memarena_alloc(mem_arena, n * (ns2 + 1) * (ns + 1) * sizeof(NewVert));
1354 /* special case: two beveled ends welded together */
1355 weld = (bv->selcount == 2) && (vm->count == 2);
1356 weld1 = weld2 = NULL; /* will hold two BoundVerts involved in weld */
1358 /* make (i, 0, 0) mesh verts for all i */
1362 copy_v3_v3(mesh_vert(vm, i, 0, 0)->co, v->nv.co);
1363 create_mesh_bmvert(bm, vm, i, 0, 0, bv->v);
1364 v->nv.v = mesh_vert(vm, i, 0, 0)->v;
1365 if (weld && v->ebev) {
1371 } while ((v = v->next) != vm->boundstart);
1373 /* copy other ends to (i, 0, ns) for all i, and fill in profiles for beveled edges */
1377 copy_mesh_vert(vm, i, 0, ns, v->next->index, 0, 0);
1380 #ifdef USE_ALTERNATE_ADJ
1381 copy_v3_v3(quad_plane[0], v->nv.co);
1382 mid_v3_v3v3(quad_plane[1], v->nv.co, v->prev->nv.co);
1383 /* quad[2] is set */
1384 mid_v3_v3v3(quad_plane[3], v->nv.co, v->next->nv.co);
1387 copy_v3_v3(quad_orig_a[0], v->nv.co); /* only shared location between 2 quads */
1388 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig_a[1]);
1389 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig_a[3]);
1392 copy_v3_v3(quad_orig_b[3], v->next->nv.co); /* only shared location between 2 quads */
1393 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig_b[1]);
1394 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig_b[0]);
1396 //bl_debug_draw_quad_add(UNPACK4(quad_plane));
1397 //bl_debug_draw_quad_add(UNPACK4(quad_orig_a));
1398 //bl_debug_draw_quad_add(UNPACK4(quad_orig_b));
1399 #endif /* USE_ALTERNATE_ADJ */
1401 #ifdef USE_ALTERNATE_ADJ
1402 for (k = 1; k < ns; k++) {
1409 get_point_uv(uv, v->ebev->seg, 0, k);
1410 get_point_on_round_edge(uv, quad_plane, co_plane);
1413 /* each half has different UV's */
1415 get_point_uv(uv, v->ebev->seg, 0, k);
1416 get_point_on_round_edge(uv, quad_orig_a, co_orig);
1419 get_point_uv(uv, v->ebev->seg, 0, (k - ns2) - (is_odd ? 0.5f : 0.0f));
1420 get_point_on_round_edge(uv, quad_orig_b, co_orig);
1421 uv[1] = 1.0f - uv[1]; /* so we can get the factor */
1423 fac = get_point_uv_factor(uv);
1426 interp_v3_v3v3(co, co_plane, co_orig, fac);
1427 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
1429 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
1431 #else /* USE_ALTERNATE_ADJ */
1432 va = mesh_vert(vm, i, 0, 0)->co;
1433 vb = mesh_vert(vm, i, 0, ns)->co;
1434 project_to_edge(v->ebev->e, va, vb, midco);
1435 for (k = 1; k < ns; k++) {
1436 get_point_on_round_edge(v->ebev, k, va, midco, vb, co);
1437 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
1439 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
1441 #endif /* !USE_ALTERNATE_ADJ */
1443 } while ((v = v->next) != vm->boundstart);
1446 vm->mesh_kind = M_NONE;
1447 for (k = 1; k < ns; k++) {
1448 va = mesh_vert(vm, weld1->index, 0, k)->co;
1449 vb = mesh_vert(vm, weld2->index, 0, ns - k)->co;
1450 mid_v3_v3v3(co, va, vb);
1451 copy_v3_v3(mesh_vert(vm, weld1->index, 0, k)->co, co);
1452 create_mesh_bmvert(bm, vm, weld1->index, 0, k, bv->v);
1454 for (k = 1; k < ns; k++)
1455 copy_mesh_vert(vm, weld2->index, 0, ns - k, weld1->index, 0, k);
1458 switch (vm->mesh_kind) {
1463 bevel_build_poly(bm, bv);
1466 bevel_build_rings(bm, bv);
1469 bevel_build_trifan(bm, bv);
1472 bevel_build_quadstrip(bm, bv);
1477 /* take care, this flag isn't cleared before use, it just so happens that its not set */
1478 #define BM_BEVEL_EDGE_TAG_ENABLE(bme) BM_elem_flag_enable( (bme)->l, BM_ELEM_TAG)
1479 #define BM_BEVEL_EDGE_TAG_DISABLE(bme) BM_elem_flag_disable( (bme)->l, BM_ELEM_TAG)
1480 #define BM_BEVEL_EDGE_TAG_TEST(bme) BM_elem_flag_test( (bme)->l, BM_ELEM_TAG)
1483 * Construction around the vertex
1485 static void bevel_vert_construct(BMesh *bm, BevelParams *bp, BMVert *v)
1489 BMEdge *bme2, *unflagged_bme;
1493 int i, found_shared_face, ccw_test_sum;
1497 /* Gather input selected edges.
1498 * Only bevel selected edges that have exactly two incident faces.
1501 BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
1502 if (BM_elem_flag_test(bme, BM_ELEM_TAG)) {
1503 BLI_assert(BM_edge_is_manifold(bme));
1510 /* signal this vert isn't being beveled */
1511 BM_elem_flag_disable(v, BM_ELEM_TAG);
1515 /* avoid calling BM_vert_edge_count since we loop over edges already */
1516 // ntot = BM_vert_edge_count(v);
1517 // BLI_assert(ntot == BM_vert_edge_count(v));
1519 bv = (BevVert *)BLI_memarena_alloc(bp->mem_arena, (sizeof(BevVert)));
1521 bv->edgecount = ntot;
1522 bv->selcount = nsel;
1523 bv->edges = (EdgeHalf *)BLI_memarena_alloc(bp->mem_arena, ntot * sizeof(EdgeHalf));
1524 bv->vmesh = (VMesh *)BLI_memarena_alloc(bp->mem_arena, sizeof(VMesh));
1525 bv->vmesh->seg = bp->seg;
1526 BLI_ghash_insert(bp->vert_hash, v, bv);
1528 /* add edges to bv->edges in order that keeps adjacent edges sharing
1529 * a face, if possible */
1532 BM_BEVEL_EDGE_TAG_ENABLE(bme);
1535 for (i = 0; i < ntot; i++) {
1537 /* find an unflagged edge bme2 that shares a face f with previous bme */
1538 found_shared_face = 0;
1539 unflagged_bme = NULL;
1540 BM_ITER_ELEM (bme2, &iter, v, BM_EDGES_OF_VERT) {
1541 if (BM_BEVEL_EDGE_TAG_TEST(bme2))
1544 unflagged_bme = bme2;
1545 BM_ITER_ELEM (f, &iter2, bme2, BM_FACES_OF_EDGE) {
1546 if (BM_face_edge_share_loop(f, bme)) {
1547 found_shared_face = 1;
1551 if (found_shared_face)
1555 if (found_shared_face) {
1558 bv->edges[i - 1].fnext = f;
1561 e->e = unflagged_bme;
1565 BM_BEVEL_EDGE_TAG_ENABLE(bme);
1566 if (BM_elem_flag_test(bme, BM_ELEM_TAG)) {
1574 e->is_rev = (bme->v2 == v);
1575 e->offset = e->is_bev ? bp->offset : 0.0f;
1577 /* find wrap-around shared face */
1578 BM_ITER_ELEM (f, &iter2, bme, BM_FACES_OF_EDGE) {
1579 if (BM_face_edge_share_loop(f, bv->edges[0].e)) {
1580 if (bv->edges[0].fnext == f)
1581 continue; /* if two shared faces, want the other one now */
1582 bv->edges[ntot - 1].fnext = f;
1583 bv->edges[0].fprev = f;
1588 /* do later when we loop over edges */
1590 /* clear BEVEL_EDGE_TAG now that we are finished with it*/
1591 for (i = 0; i < ntot; i++) {
1592 BM_BEVEL_EDGE_TAG_DISABLE(bv->edges[i].e);
1596 /* if edge array doesn't go CCW around vertex from average normal side,
1597 * reverse the array, being careful to reverse face pointers too */
1600 for (i = 0; i < ntot; i++)
1601 ccw_test_sum += bev_ccw_test(bv->edges[i].e, bv->edges[(i + 1) % ntot].e,
1602 bv->edges[i].fnext);
1603 if (ccw_test_sum < 0) {
1604 for (i = 0; i <= (ntot / 2) - 1; i++) {
1605 SWAP(EdgeHalf, bv->edges[i], bv->edges[ntot - i - 1]);
1606 SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
1607 SWAP(BMFace *, bv->edges[ntot - i - 1].fprev, bv->edges[ntot - i - 1].fnext);
1609 if (ntot % 2 == 1) {
1611 SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
1616 for (i = 0, e = bv->edges; i < ntot; i++, e++) {
1617 e->next = &bv->edges[(i + 1) % ntot];
1618 e->prev = &bv->edges[(i + ntot - 1) % ntot];
1619 BM_BEVEL_EDGE_TAG_DISABLE(e->e);
1622 build_boundary(bp->mem_arena, bv);
1623 build_vmesh(bp->mem_arena, bm, bv);
1626 /* Face f has at least one beveled vertex. Rebuild f */
1627 static int bev_rebuild_polygon(BMesh *bm, BevelParams *bp, BMFace *f)
1632 BoundVert *v, *vstart, *vend;
1633 EdgeHalf *e, *eprev;
1636 int do_rebuild = FALSE;
1639 BLI_array_staticdeclare(vv, BM_DEFAULT_NGON_STACK_SIZE);
1641 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1642 if (BM_elem_flag_test(l->v, BM_ELEM_TAG)) {
1644 bv = find_bevvert(bp, l->v);
1645 e = find_edge_half(bv, l->e);
1646 eprev = find_edge_half(bv, lprev->e);
1647 BLI_assert(e != NULL && eprev != NULL);
1648 vstart = eprev->leftv;
1655 BLI_array_append(vv, v->nv.v);
1657 if (vm->mesh_kind == M_NONE && v->ebev && v->ebev->seg > 1 && v->ebev != e && v->ebev != eprev) {
1658 /* case of 3rd face opposite a beveled edge, with no vmesh */
1661 for (k = 1; k < e->seg; k++) {
1662 bmv = mesh_vert(vm, i, 0, k)->v;
1663 BLI_array_append(vv, bmv);
1667 BLI_array_append(vv, v->nv.v);
1673 BLI_array_append(vv, l->v);
1677 BMFace *f_new = bev_create_ngon(bm, vv, BLI_array_count(vv), f);
1679 /* don't select newly created boundary faces... */
1681 BM_elem_flag_disable(f_new, BM_ELEM_TAG);
1689 /* All polygons touching v need rebuilding because beveling v has made new vertices */
1690 static void bevel_rebuild_existing_polygons(BMesh *bm, BevelParams *bp, BMVert *v)
1692 void *faces_stack[BM_DEFAULT_ITER_STACK_SIZE];
1693 int faces_len, f_index;
1694 BMFace **faces = BM_iter_as_arrayN(bm, BM_FACES_OF_VERT, v, &faces_len,
1695 faces_stack, BM_DEFAULT_ITER_STACK_SIZE);
1697 if (LIKELY(faces != NULL)) {
1698 for (f_index = 0; f_index < faces_len; f_index++) {
1699 BMFace *f = faces[f_index];
1700 if (bev_rebuild_polygon(bm, bp, f)) {
1701 BM_face_kill(bm, f);
1705 if (faces != (BMFace **)faces_stack) {
1713 * Build the polygons along the selected Edge
1715 static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
1718 BMVert *bmv1, *bmv2, *bmv3, *bmv4, *bmv1i, *bmv2i, *bmv3i, *bmv4i;
1721 BMFace *f1, *f2, *f;
1722 int k, nseg, i1, i2;
1724 if (!BM_edge_is_manifold(bme))
1727 bv1 = find_bevvert(bp, bme->v1);
1728 bv2 = find_bevvert(bp, bme->v2);
1730 BLI_assert(bv1 && bv2);
1732 e1 = find_edge_half(bv1, bme);
1733 e2 = find_edge_half(bv2, bme);
1735 BLI_assert(e1 && e2);
1744 BLI_assert(nseg > 0 && nseg == e2->seg);
1746 bmv1 = e1->leftv->nv.v;
1747 bmv4 = e1->rightv->nv.v;
1748 bmv2 = e2->rightv->nv.v;
1749 bmv3 = e2->leftv->nv.v;
1751 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1753 f1 = boundvert_rep_face(e1->leftv);
1754 f2 = boundvert_rep_face(e1->rightv);
1757 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f1);
1760 i1 = e1->leftv->index;
1761 i2 = e2->leftv->index;
1766 for (k = 1; k <= nseg; k++) {
1767 bmv4i = mesh_vert(vm1, i1, 0, k)->v;
1768 bmv3i = mesh_vert(vm2, i2, 0, nseg - k)->v;
1769 f = (k <= nseg / 2 + (nseg % 2)) ? f1 : f2;
1770 bev_create_quad_tri(bm, bmv1i, bmv2i, bmv3i, bmv4i, f);
1778 * - Currently only bevels BM_ELEM_TAG'd verts and edges.
1780 * - Newly created faces are BM_ELEM_TAG'd too,
1781 * the caller needs to ensure this is cleared before calling
1782 * if its going to use this face tag.
1784 * \warning all tagged edges _must_ be manifold.
1786 void BM_mesh_bevel(BMesh *bm, const float offset, const float segments)
1791 BevelParams bp = {NULL};
1796 if (bp.offset > 0) {
1798 bp.vert_hash = BLI_ghash_ptr_new(__func__);
1799 bp.mem_arena = BLI_memarena_new((1 << 16), __func__);
1800 BLI_memarena_use_calloc(bp.mem_arena);
1802 /* The analysis of the input vertices and execution additional constructions */
1803 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1804 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1805 bevel_vert_construct(bm, &bp, v);
1809 /* Build polygons for edges */
1810 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
1811 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
1812 bevel_build_edge_polygons(bm, &bp, e);
1816 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1817 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1818 bevel_rebuild_existing_polygons(bm, &bp, v);
1822 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1823 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1824 BLI_assert(find_bevvert(&bp, v) != NULL);
1825 BM_vert_kill(bm, v);
1830 BLI_ghash_free(bp.vert_hash, NULL, NULL);
1831 BLI_memarena_free(bp.mem_arena);