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])
437 v = e->is_rev ? e->e->v1 : e->e->v2;
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(r, no, dir);
454 cross_v3_v3v3(r, dir, no);
456 mul_v3_fl(r, e->offset);
459 /* Calculate coordinates of a point a distance d from v on e->e and return it in slideco */
460 static void slide_dist(EdgeHalf *e, BMVert *v, float d, float slideco[3])
464 sub_v3_v3v3(dir, v->co, BM_edge_other_vert(e->e, v)->co);
465 len = normalize_v3(dir);
467 d = len - (float)(50.0 * BEVEL_EPSILON);
468 copy_v3_v3(slideco, v->co);
469 madd_v3_v3fl(slideco, dir, -d);
472 /* Calculate the point on e where line (co_a, co_b) comes closest to and return it in projco */
473 static void project_to_edge(BMEdge *e, const float co_a[3], const float co_b[3], float projco[3])
477 if (!isect_line_line_v3(e->v1->co, e->v2->co, co_a, co_b, projco, otherco)) {
478 BLI_assert(!"project meet failure");
479 copy_v3_v3(projco, e->v1->co);
483 /* return 1 if a and b are in CCW order on the normal side of f,
484 * and -1 if they are reversed, and 0 if there is no shared face f */
485 static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
491 la = BM_face_edge_share_loop(f, a);
492 lb = BM_face_edge_share_loop(f, b);
495 return lb->next == la ? 1 : -1;
498 #ifdef USE_ALTERNATE_ADJ
500 static void vmesh_cent(VMesh *vm, float r_cent[3])
507 add_v3_v3(r_cent, v->nv.co);
508 } while ((v = v->next) != vm->boundstart);
509 mul_v3_fl(r_cent, 1.0f / (float)vm->count);
514 * This example shows a tri fan of quads,
515 * but could be an NGon fan of quads too.
517 * The whole triangle X
519 * new bevel face. / \
531 * interest -- / ---> X | \
535 * co_orig +-----------------+-----------------+
537 * For each quad, calcualte UV's based on the following:
538 * U = k / (vm->seg * 2)
539 * V = ring / (vm->seg * 2)
540 * quad = (co_orig, co_prev, co_cent, co_next)
541 * ... note that co_cent is the same for all quads in the fan.
546 static void get_point_uv(float uv[2],
547 /* all these args are int's originally
548 * but pass as floats to the function */
549 const float seg, const float ring, const float k)
551 uv[0] = (ring / seg) * 2.0f;
552 uv[1] = (k / seg) * 2.0f;
555 /* TODO: make this a lot smarter!,
556 * this is the main reason USE_ALTERNATE_ADJ isn't so good right now :S */
557 static float get_point_uv_factor(const float uv[2])
559 return sinf(1.0f - max_ff(uv[0], uv[1]) / 2.0f);
562 static void get_point_on_round_edge(const float uv[2],
566 interp_bilinear_quad_v3(quad, uv[0], uv[1], r_co);
569 #else /* USE_ALTERNATE_ADJ */
571 #ifdef OLD_ROUND_EDGE
573 * calculation of points on the round profile
574 * r - result, coordinate of point on round profile
576 * Inscribe a circle in angle va - v -vb
577 * such that it touches the arms at offset from v.
578 * Rotate the center-va segment by (i/n) of the
579 * angle va - center -vb, and put the endpoint
580 * of that segment in r.
582 static void get_point_on_round_profile(float r_co[3], float offset, int k, int count,
583 const float va[3], const float v[3], const float vb[3])
585 float vva[3], vvb[3], angle, center[3], rv[3], axis[3], co[3];
587 sub_v3_v3v3(vva, va, v);
588 sub_v3_v3v3(vvb, vb, v);
591 angle = angle_normalized_v3v3(vva, vvb);
593 add_v3_v3v3(center, vva, vvb);
594 normalize_v3(center);
595 mul_v3_fl(center, offset * (1.0f / cosf(0.5f * angle)));
596 add_v3_v3(center, v); /* coordinates of the center of the inscribed circle */
599 sub_v3_v3v3(rv, va, center); /* radius vector */
602 sub_v3_v3v3(co, v, center);
603 cross_v3_v3v3(axis, rv, co); /* calculate axis */
605 sub_v3_v3v3(vva, va, center);
606 sub_v3_v3v3(vvb, vb, center);
607 angle = angle_v3v3(vva, vvb);
609 rotate_v3_v3v3fl(co, rv, axis, angle * (float)k / (float)count);
611 add_v3_v3(co, center);
612 copy_v3_v3(r_co, co);
616 * Find the point (/n) of the way around the round profile for e,
617 * where start point is va, midarc point is vmid, and end point is vb.
618 * Return the answer in profileco.
620 * Adjust va and vb (along edge direction) so that they are perpendicular
621 * to edge at v, then use get_point_on_round_profile, then project
622 * back onto original va - vmid - vb plane.
623 * If va, vmid, and vb are all on the same plane, just interpolate between va and vb.
625 static void get_point_on_round_edge(EdgeHalf *e, int k,
626 const float va[3], const float vmid[3], const float vb[3],
629 float vva[3], vvb[3], point[3], dir[3], vaadj[3], vbadj[3], p2[3], pn[3];
632 sub_v3_v3v3(vva, va, vmid);
633 sub_v3_v3v3(vvb, vb, vmid);
635 sub_v3_v3v3(dir, e->e->v1->co, e->e->v2->co);
637 sub_v3_v3v3(dir, e->e->v2->co, e->e->v1->co);
639 if (fabsf(angle_v3v3(vva, vvb) - (float)M_PI) > 100.f *(float)BEVEL_EPSILON) {
640 copy_v3_v3(vaadj, va);
641 madd_v3_v3fl(vaadj, dir, -len_v3(vva) * cosf(angle_v3v3(vva, dir)));
642 copy_v3_v3(vbadj, vb);
643 madd_v3_v3fl(vbadj, dir, -len_v3(vvb) * cosf(angle_v3v3(vvb, dir)));
645 get_point_on_round_profile(point, e->offset, k, n, vaadj, vmid, vbadj);
647 add_v3_v3v3(p2, point, dir);
648 cross_v3_v3v3(pn, vva, vvb);
649 if (!isect_line_plane_v3(r_co, point, p2, vmid, pn, 0)) {
650 /* TODO: track down why this sometimes fails */
651 copy_v3_v3(r_co, point);
656 interp_v3_v3v3(r_co, va, vb, (float)k / (float)n);
662 * Find the point (/n) of the way around the round profile for e,
663 * where start point is va, midarc point is vmid, and end point is vb.
664 * Return the answer in profileco.
666 * Find vo, the origin of the parallelogram with other three points va, vmid, vb.
667 * Also find vd, which is in direction normal to parallelogram and 1 unit away
669 * The quarter circle in first quadrant of unit square will be mapped to the
670 * quadrant of a sheared ellipse in the parallelgram, using a matrix.
671 * The matrix mat is calculated to map:
676 * However if va -- vmid -- vb is approximately a straight line, just
677 * interpolate along the line.
679 static void get_point_on_round_edge(EdgeHalf *e, int k,
680 const float va[3], const float vmid[3], const float vb[3],
683 float vo[3], vd[3], vb_vmid[3], va_vmid[3], vddir[3], p[3], angle;
684 float m[4][4] = MAT4_UNITY;
687 sub_v3_v3v3(va_vmid, vmid, va);
688 sub_v3_v3v3(vb_vmid, vmid, vb);
689 if (fabsf(angle_v3v3(va_vmid, vb_vmid) - (float)M_PI) > 100.f *(float)BEVEL_EPSILON) {
690 sub_v3_v3v3(vo, va, vb_vmid);
691 cross_v3_v3v3(vddir, vb_vmid, va_vmid);
693 add_v3_v3v3(vd, vo, vddir);
695 /* The cols of m are: {vmid - va, vmid - vb, vmid + vd - va -vb, va + vb - vmid;
696 * blender transform matrices are stored such that m[i][*] is ith column;
697 * the last elements of each col remain as they are in unity matrix */
698 sub_v3_v3v3(&m[0][0], vmid, va);
699 sub_v3_v3v3(&m[1][0], vmid, vb);
700 add_v3_v3v3(&m[2][0], vmid, vd);
701 sub_v3_v3(&m[2][0], va);
702 sub_v3_v3(&m[2][0], vb);
703 add_v3_v3v3(&m[3][0], va, vb);
704 sub_v3_v3(&m[3][0], vmid);
706 /* Now find point k/(e->seg) along quarter circle from (0,1,0) to (1,0,0) */
707 angle = (float)M_PI * (float)k / (2.0f * (float)n); /* angle from y axis */
711 mul_v3_m4v3(r_co, m, p);
715 interp_v3_v3v3(r_co, va, vb, (float)k / (float)n);
718 #endif /* ! OLD_ROUND_EDGE */
720 #endif /* !USE_ALTERNATE_ADJ */
722 /* Make a circular list of BoundVerts for bv, each of which has the coordinates
723 * of a vertex on the the boundary of the beveled vertex bv->v.
724 * Also decide on the mesh pattern that will be used inside the boundary.
725 * Doesn't make the actual BMVerts */
726 static void build_boundary(MemArena *mem_arena, BevVert *bv)
728 EdgeHalf *efirst, *e;
735 e = efirst = next_bev(bv, NULL);
738 BLI_assert(bv->edgecount >= 2); /* since bevel edges incident to 2 faces */
740 if (bv->edgecount == 2 && bv->selcount == 1) {
741 /* special case: beveled edge meets non-beveled one at valence 2 vert */
742 no = e->fprev ? e->fprev->no : (e->fnext ? e->fnext->no : NULL);
743 offset_in_plane(e, no, TRUE, co);
744 v = add_new_bound_vert(mem_arena, vm, co);
745 v->efirst = v->elast = v->ebev = e;
747 no = e->fnext ? e->fnext->no : (e->fprev ? e->fprev->no : NULL);
748 offset_in_plane(e, no, FALSE, co);
749 v = add_new_bound_vert(mem_arena, vm, co);
750 v->efirst = v->elast = e;
752 /* make artifical extra point along unbeveled edge, and form triangle */
753 slide_dist(e->next, bv->v, e->offset, co);
754 v = add_new_bound_vert(mem_arena, vm, co);
755 v->efirst = v->elast = e->next;
756 vm->mesh_kind = M_POLY;
761 vm->boundstart = NULL;
764 /* handle only left side of beveled edge e here: next iteration should do right side */
765 if (e->prev->is_bev) {
766 BLI_assert(e->prev != e); /* see: wire edge special case */
767 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
768 v = add_new_bound_vert(mem_arena, vm, co);
770 v->elast = v->ebev = e;
775 /* e->prev is not beveled */
776 if (e->prev->prev->is_bev) {
777 BLI_assert(e->prev->prev != e); /* see: edgecount 2, selcount 1 case */
778 /* find meet point between e->prev->prev and e and attach e->prev there */
779 offset_in_two_planes(e->prev->prev, e, e->prev, bv->v,
780 e->prev->prev->fnext, e->fprev, co);
781 v = add_new_bound_vert(mem_arena, vm, co);
782 v->efirst = e->prev->prev;
783 v->elast = v->ebev = e;
786 e->prev->prev->rightv = v;
789 /* neither e->prev nor e->prev->prev are beveled: make on-edge on e->prev */
790 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
791 v = add_new_bound_vert(mem_arena, vm, co);
793 v->elast = v->ebev = e;
798 lastd = len_v3v3(bv->v->co, v->nv.co);
801 /* e is not beveled */
802 if (e->next->is_bev) {
803 /* next iteration will place e between beveled previous and next edges */
806 else if (e->prev->is_bev) {
807 /* on-edge meet between e->prev and e */
808 offset_meet(e->prev, e, bv->v, e->fprev, TRUE, co);
809 v = add_new_bound_vert(mem_arena, vm, co);
816 /* None of e->prev, e, e->next are beveled.
817 * could either leave alone or add slide points to make
818 * one polygon around bv->v. For now, we choose latter.
819 * Could slide to make an even bevel plane but for now will
820 * just use last distance a meet point moved from bv->v. */
821 slide_dist(e, bv->v, lastd, co);
822 v = add_new_bound_vert(mem_arena, vm, co);
823 v->efirst = v->elast = e;
827 } while ((e = e->next) != efirst);
829 BLI_assert(vm->count >= 2);
830 if (vm->count == 2 && bv->edgecount == 3) {
831 vm->mesh_kind = M_NONE;
833 else if (bv->selcount == 2) {
834 vm->mesh_kind = M_QUAD_STRIP;
836 else if (efirst->seg == 1 || bv->selcount == 1) {
837 if (vm->count == 3 && bv->selcount == 1) {
838 vm->mesh_kind = M_TRI_FAN;
841 vm->mesh_kind = M_POLY;
845 vm->mesh_kind = M_ADJ;
847 /* TODO: if vm->count == 4 and bv->selcount == 4, use M_CROSS pattern */
851 * Given that the boundary is built and the boundary BMVerts have been made,
852 * calculate the positions of the interior mesh points for the M_ADJ pattern,
853 * then make the BMVerts and the new faces. */
854 static void bevel_build_rings(BMesh *bm, BevVert *bv)
856 int k, ring, i, n, ns, ns2, nn;
857 VMesh *vm = bv->vmesh;
858 BoundVert *v, *vprev, *vnext;
859 NewVert *nv, *nvprev, *nvnext;
860 BMVert *bmv, *bmv1, *bmv2, *bmv3, *bmv4;
862 float co[3], coa[3], cob[3], midco[3];
864 #ifdef USE_ALTERNATE_ADJ
865 /* ordered as follows (orig, prev, center, next)*/
866 float quad_plane[4][3];
867 float quad_orig[4][3];
871 #ifdef USE_ALTERNATE_ADJ
872 /* the rest are initialized inline, this remains the same for all */
873 vmesh_cent(vm, quad_plane[2]);
874 copy_v3_v3(quad_orig[2], bv->v->co);
880 BLI_assert(n > 2 && ns > 1);
881 /* Make initial rings, going between points on neighbors.
882 * After this loop, will have coords for all (i, r, k) where
883 * BoundVert for i has a bevel, 0 <= r <= ns2, 0 <= k <= ns */
884 for (ring = 1; ring <= ns2; ring++) {
890 /* get points coords of points a and b, on outer rings
891 * of prev and next edges, k away from this edge */
896 nvprev = mesh_vert(vm, vprev->index, 0, ns - ring);
898 nvprev = mesh_vert(vm, vprev->index, 0, ns);
899 copy_v3_v3(coa, nvprev->co);
900 nv = mesh_vert(vm, i, ring, 0);
901 copy_v3_v3(nv->co, coa);
905 nvnext = mesh_vert(vm, vnext->index, 0, ring);
907 nvnext = mesh_vert(vm, vnext->index, 0, 0);
908 copy_v3_v3(cob, nvnext->co);
909 nv = mesh_vert(vm, i, ring, ns);
910 copy_v3_v3(nv->co, cob);
913 #ifdef USE_ALTERNATE_ADJ
915 copy_v3_v3(quad_plane[0], v->nv.co);
916 mid_v3_v3v3(quad_plane[1], v->nv.co, v->prev->nv.co);
918 mid_v3_v3v3(quad_plane[3], v->nv.co, v->next->nv.co);
921 copy_v3_v3(quad_orig[0], v->nv.co); /* only shared location between 2 quads */
922 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig[1]);
923 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig[3]);
925 //bl_debug_draw_quad_add(UNPACK4(quad_plane));
926 //bl_debug_draw_quad_add(UNPACK4(quad_orig));
929 #ifdef USE_ALTERNATE_ADJ
930 for (k = 1; k < ns; k++) {
936 get_point_uv(uv, v->ebev->seg, ring, k);
937 get_point_on_round_edge(uv, quad_plane, co_plane);
938 get_point_on_round_edge(uv, quad_orig, co_orig);
939 fac = get_point_uv_factor(uv);
940 interp_v3_v3v3(co, co_plane, co_orig, fac);
941 copy_v3_v3(mesh_vert(vm, i, ring, k)->co, co);
944 /* TODO: better calculation of new midarc point? */
945 project_to_edge(v->ebev->e, coa, cob, midco);
947 for (k = 1; k < ns; k++) {
948 get_point_on_round_edge(v->ebev, k, coa, midco, cob, co);
949 copy_v3_v3(mesh_vert(vm, i, ring, k)->co, co);
953 } while ((v = v->next) != vm->boundstart);
956 /* Now make sure cross points of rings share coordinates and vertices.
957 * After this loop, will have BMVerts for all (i, r, k) where
958 * i is for a BoundVert that is beveled and has either a predecessor or
959 * successor BoundVert beveled too, and
960 * for odd ns: 0 <= r <= ns2, 0 <= k <= ns
961 * for even ns: 0 <= r < ns2, 0 <= k <= ns except k=ns2 */
969 for (ring = 1; ring <= ns2; ring++) {
970 for (k = 1; k <= ns2; k++) {
971 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
972 continue; /* center line is special case: do after the rest are done */
973 nv = mesh_vert(vm, i, ring, k);
974 nvprev = mesh_vert(vm, vprev->index, k, ns - ring);
975 mid_v3_v3v3(co, nv->co, nvprev->co);
976 #ifndef USE_ALTERNATE_ADJ
977 copy_v3_v3(nv->co, co);
979 BLI_assert(nv->v == NULL && nvprev->v == NULL);
980 create_mesh_bmvert(bm, vm, i, ring, k, bv->v);
981 copy_mesh_vert(vm, vprev->index, k, ns - ring, i, ring, k);
984 if (!vprev->prev->ebev) {
985 for (ring = 1; ring <= ns2; ring++) {
986 for (k = 1; k <= ns2; k++) {
987 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
989 create_mesh_bmvert(bm, vm, vprev->index, ring, k, bv->v);
994 for (ring = 1; ring <= ns2; ring++) {
995 for (k = ns - ns2; k < ns; k++) {
996 if (ns % 2 == 0 && (k == ns2 || ring == ns2))
998 create_mesh_bmvert(bm, vm, i, ring, k, bv->v);
1004 } while ((v = v->next) != vm->boundstart);
1007 /* Do special case center lines.
1008 * This loop makes verts for (i, ns2, k) for 1 <= k <= ns-1, k!=ns2
1009 * and for (i, r, ns2) for 1 <= r <= ns2-1,
1010 * whenever i is in a sequence of at least two beveled verts */
1017 for (k = 1; k < ns2; k++) {
1018 nv = mesh_vert(vm, i, k, ns2);
1020 nvprev = mesh_vert(vm, vprev->index, ns2, ns - k);
1022 nvnext = mesh_vert(vm, vnext->index, ns2, k);
1023 if (vprev->ebev && vnext->ebev) {
1024 mid_v3_v3v3v3(co, nvprev->co, nv->co, nvnext->co);
1025 #ifndef USE_ALTERNATE_ADJ
1026 copy_v3_v3(nv->co, co);
1028 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1029 copy_mesh_vert(vm, vprev->index, ns2, ns - k, i, k, ns2);
1030 copy_mesh_vert(vm, vnext->index, ns2, k, i, k, ns2);
1033 else if (vprev->ebev) {
1034 mid_v3_v3v3(co, nvprev->co, nv->co);
1035 #ifndef USE_ALTERNATE_ADJ
1036 copy_v3_v3(nv->co, co);
1038 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1039 copy_mesh_vert(vm, vprev->index, ns2, ns - k, i, k, ns2);
1041 create_mesh_bmvert(bm, vm, i, ns2, ns - k, bv->v);
1043 else if (vnext->ebev) {
1044 mid_v3_v3v3(co, nv->co, nvnext->co);
1045 #ifndef USE_ALTERNATE_ADJ
1046 copy_v3_v3(nv->co, co);
1048 create_mesh_bmvert(bm, vm, i, k, ns2, bv->v);
1049 copy_mesh_vert(vm, vnext->index, ns2, k, i, k, ns2);
1051 create_mesh_bmvert(bm, vm, i, ns2, k, bv->v);
1055 } while ((v = v->next) != vm->boundstart);
1057 /* center point need to be average of all centers of rings */
1058 /* TODO: this is wrong if not all verts have ebev: could have
1059 * several disconnected sections of mesh. */
1066 nv = mesh_vert(vm, i, ns2, ns2);
1067 add_v3_v3(midco, nv->co);
1070 } while ((v = v->next) != vm->boundstart);
1071 mul_v3_fl(midco, 1.0f / nn);
1072 bmv = BM_vert_create(bm, midco, NULL, 0);
1077 nv = mesh_vert(vm, i, ns2, ns2);
1078 copy_v3_v3(nv->co, midco);
1081 } while ((v = v->next) != vm->boundstart);
1084 /* Make the ring quads */
1085 for (ring = 0; ring < ns2; ring++) {
1089 f = boundvert_rep_face(v);
1090 if (v->ebev && (v->prev->ebev || v->next->ebev)) {
1091 for (k = 0; k < ns2 + (ns % 2); k++) {
1092 bmv1 = mesh_vert(vm, i, ring, k)->v;
1093 bmv2 = mesh_vert(vm, i, ring, k + 1)->v;
1094 bmv3 = mesh_vert(vm, i, ring + 1, k + 1)->v;
1095 bmv4 = mesh_vert(vm, i, ring + 1, k)->v;
1096 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1097 if (bmv3 == bmv4 || bmv1 == bmv4)
1099 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f);
1102 else if (v->prev->ebev && v->prev->prev->ebev) {
1103 /* finish off a sequence of beveled edges */
1105 f = boundvert_rep_face(v->prev);
1106 for (k = ns2 + (ns % 2); k < ns; k++) {
1107 bmv1 = mesh_vert(vm, i, ring, k)->v;
1108 bmv2 = mesh_vert(vm, i, ring, k + 1)->v;
1109 bmv3 = mesh_vert(vm, i, ring + 1, k + 1)->v;
1110 bmv4 = mesh_vert(vm, i, ring + 1, k)->v;
1111 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1116 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f);
1119 } while ((v = v->next) != vm->boundstart);
1122 /* Make center ngon if odd number of segments and fully beveled */
1123 if (ns % 2 == 1 && vm->count == bv->selcount) {
1125 BLI_array_declare(vv);
1130 BLI_assert(v->ebev);
1131 BLI_array_append(vv, mesh_vert(vm, i, ns2, ns2)->v);
1132 } while ((v = v->next) != vm->boundstart);
1133 f = boundvert_rep_face(vm->boundstart);
1134 bev_create_ngon(bm, vv, BLI_array_count(vv), f);
1139 /* Make 'rest-of-vmesh' polygon if not fully beveled */
1140 if (vm->count > bv->selcount) {
1143 BLI_array_declare(vv);
1146 f = boundvert_rep_face(v);
1151 if (!v->prev->ebev) {
1152 for (k = 0; k < ns2; k++) {
1153 bmv1 = mesh_vert(vm, i, ns2, k)->v;
1155 bmv1 = mesh_vert(vm, i, 0, k)->v;
1156 if (!(j > 0 && bmv1 == vv[j - 1])) {
1157 BLI_assert(bmv1 != NULL);
1158 BLI_array_append(vv, bmv1);
1163 bmv1 = mesh_vert(vm, i, ns2, ns2)->v;
1165 bmv1 = mesh_vert(vm, i, 0, ns2)->v;
1166 if (!(j > 0 && bmv1 == vv[j - 1])) {
1167 BLI_assert(bmv1 != NULL);
1168 BLI_array_append(vv, bmv1);
1171 if (!v->next->ebev) {
1172 for (k = ns - ns2; k < ns; k++) {
1173 bmv1 = mesh_vert(vm, i, ns2, k)->v;
1175 bmv1 = mesh_vert(vm, i, 0, k)->v;
1176 if (!(j > 0 && bmv1 == vv[j - 1])) {
1177 BLI_assert(bmv1 != NULL);
1178 BLI_array_append(vv, bmv1);
1185 BLI_assert(mesh_vert(vm, i, 0, 0)->v != NULL);
1186 BLI_array_append(vv, mesh_vert(vm, i, 0, 0)->v);
1189 } while ((v = v->next) != vm->boundstart);
1190 if (vv[0] == vv[j - 1])
1192 bev_create_ngon(bm, vv, j, f);
1198 static BMFace *bevel_build_poly_ex(BMesh *bm, BevVert *bv)
1202 VMesh *vm = bv->vmesh;
1205 BLI_array_declare(vv);
1210 /* accumulate vertices for vertex ngon */
1211 BLI_array_append(vv, v->nv.v);
1213 if (v->ebev && v->ebev->seg > 1) {
1214 for (k = 1; k < v->ebev->seg; k++) {
1215 BLI_array_append(vv, mesh_vert(vm, v->index, 0, k)->v);
1219 } while ((v = v->next) != vm->boundstart);
1221 f = bev_create_ngon(bm, vv, n, boundvert_rep_face(v));
1230 static void bevel_build_poly(BMesh *bm, BevVert *bv)
1232 bevel_build_poly_ex(bm, bv);
1235 static void bevel_build_trifan(BMesh *bm, BevVert *bv)
1238 BLI_assert(next_bev(bv, NULL)->seg == 1 || bv->selcount == 1);
1240 f = bevel_build_poly_ex(bm, bv);
1243 /* we have a polygon which we know starts at the previous vertex, make it into a fan */
1244 BMLoop *l_fan = BM_FACE_FIRST_LOOP(f)->prev;
1245 BMVert *v_fan = l_fan->v;
1247 while (f->len > 3) {
1250 BLI_assert(v_fan == l_fan->v);
1251 f_new = BM_face_split(bm, f, l_fan->v, l_fan->next->next->v, &l_new, NULL, FALSE);
1253 if (f_new->len > f->len) {
1255 if (l_new->v == v_fan) { l_fan = l_new; }
1256 else if (l_new->next->v == v_fan) { l_fan = l_new->next; }
1257 else if (l_new->prev->v == v_fan) { l_fan = l_new->prev; }
1258 else { BLI_assert(0); }
1261 if (l_fan->v == v_fan) { l_fan = l_fan; }
1262 else if (l_fan->next->v == v_fan) { l_fan = l_fan->next; }
1263 else if (l_fan->prev->v == v_fan) { l_fan = l_fan->prev; }
1264 else { BLI_assert(0); }
1270 static void bevel_build_quadstrip(BMesh *bm, BevVert *bv)
1273 BLI_assert(bv->selcount == 2);
1275 f = bevel_build_poly_ex(bm, bv);
1278 /* we have a polygon which we know starts at this vertex, make it into strips */
1279 EdgeHalf *eh_a = bv->vmesh->boundstart->elast;
1280 EdgeHalf *eh_b = next_bev(bv, eh_a->next); /* since (selcount == 2) we know this is valid */
1281 BMLoop *l_a = BM_face_vert_share_loop(f, eh_a->rightv->nv.v);
1282 BMLoop *l_b = BM_face_vert_share_loop(f, eh_b->leftv->nv.v);
1283 int seg_count = bv->vmesh->seg; /* ensure we don't walk past the segments */
1286 /* step once around if we hit the same loop */
1292 BLI_assert(l_a != l_b);
1294 while (f->len > 4) {
1296 BLI_assert(l_a->f == f);
1297 BLI_assert(l_b->f == f);
1299 BM_face_split(bm, f, l_a->v, l_b->v, &l_new, NULL, FALSE);
1300 if (seg_count-- == 0) {
1304 /* turns out we don't need this,
1305 * because of how BM_face_split works we always get the loop of the next face */
1307 if (l_new->f->len < l_new->radial_next->f->len) {
1308 l_new = l_new->radial_next;
1313 /* walk around the new face to get the next verts to split */
1315 l_b = l_new->next->next;
1320 /* Given that the boundary is built, now make the actual BMVerts
1321 * for the boundary and the interior of the vertex mesh. */
1322 static void build_vmesh(MemArena *mem_arena, BMesh *bm, BevVert *bv)
1324 VMesh *vm = bv->vmesh;
1325 BoundVert *v, *weld1, *weld2;
1326 int n, ns, ns2, i, k, weld;
1327 float *va, *vb, co[3];
1329 #ifdef USE_ALTERNATE_ADJ
1330 /* ordered as follows (orig, prev, center, next)*/
1331 float quad_plane[4][3];
1332 float quad_orig_a[4][3];
1333 float quad_orig_b[4][3];
1334 const int is_odd = (vm->seg % 2);
1339 #ifdef USE_ALTERNATE_ADJ
1340 /* the rest are initialized inline, this remains the same for all */
1341 /* NOTE; in this usage we only interpolate on the 'V' so cent and next points are unused (2,3)*/
1342 vmesh_cent(vm, quad_plane[2]);
1343 copy_v3_v3(quad_orig_a[2], bv->v->co);
1344 copy_v3_v3(quad_orig_b[2], bv->v->co);
1351 vm->mesh = (NewVert *)BLI_memarena_alloc(mem_arena, n * (ns2 + 1) * (ns + 1) * sizeof(NewVert));
1353 /* special case: two beveled ends welded together */
1354 weld = (bv->selcount == 2) && (vm->count == 2);
1355 weld1 = weld2 = NULL; /* will hold two BoundVerts involved in weld */
1357 /* make (i, 0, 0) mesh verts for all i */
1361 copy_v3_v3(mesh_vert(vm, i, 0, 0)->co, v->nv.co);
1362 create_mesh_bmvert(bm, vm, i, 0, 0, bv->v);
1363 v->nv.v = mesh_vert(vm, i, 0, 0)->v;
1364 if (weld && v->ebev) {
1370 } while ((v = v->next) != vm->boundstart);
1372 /* copy other ends to (i, 0, ns) for all i, and fill in profiles for beveled edges */
1376 copy_mesh_vert(vm, i, 0, ns, v->next->index, 0, 0);
1379 #ifdef USE_ALTERNATE_ADJ
1380 copy_v3_v3(quad_plane[0], v->nv.co);
1381 mid_v3_v3v3(quad_plane[1], v->nv.co, v->prev->nv.co);
1382 /* quad[2] is set */
1383 mid_v3_v3v3(quad_plane[3], v->nv.co, v->next->nv.co);
1386 copy_v3_v3(quad_orig_a[0], v->nv.co); /* only shared location between 2 quads */
1387 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig_a[1]);
1388 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig_a[3]);
1391 copy_v3_v3(quad_orig_b[3], v->next->nv.co); /* only shared location between 2 quads */
1392 project_to_edge(v->ebev->prev->e, v->nv.co, v->prev->nv.co, quad_orig_b[1]);
1393 project_to_edge(v->ebev->e, v->nv.co, v->next->nv.co, quad_orig_b[0]);
1395 //bl_debug_draw_quad_add(UNPACK4(quad_plane));
1396 //bl_debug_draw_quad_add(UNPACK4(quad_orig_a));
1397 //bl_debug_draw_quad_add(UNPACK4(quad_orig_b));
1398 #endif /* USE_ALTERNATE_ADJ */
1400 #ifdef USE_ALTERNATE_ADJ
1401 for (k = 1; k < ns; k++) {
1408 get_point_uv(uv, v->ebev->seg, 0, k);
1409 get_point_on_round_edge(uv, quad_plane, co_plane);
1412 /* each half has different UV's */
1414 get_point_uv(uv, v->ebev->seg, 0, k);
1415 get_point_on_round_edge(uv, quad_orig_a, co_orig);
1418 get_point_uv(uv, v->ebev->seg, 0, (k - ns2) - (is_odd ? 0.5f : 0.0f));
1419 get_point_on_round_edge(uv, quad_orig_b, co_orig);
1420 uv[1] = 1.0f - uv[1]; /* so we can get the factor */
1422 fac = get_point_uv_factor(uv);
1425 interp_v3_v3v3(co, co_plane, co_orig, fac);
1426 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
1428 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
1430 #else /* USE_ALTERNATE_ADJ */
1431 va = mesh_vert(vm, i, 0, 0)->co;
1432 vb = mesh_vert(vm, i, 0, ns)->co;
1433 project_to_edge(v->ebev->e, va, vb, midco);
1434 for (k = 1; k < ns; k++) {
1435 get_point_on_round_edge(v->ebev, k, va, midco, vb, co);
1436 copy_v3_v3(mesh_vert(vm, i, 0, k)->co, co);
1438 create_mesh_bmvert(bm, vm, i, 0, k, bv->v);
1440 #endif /* !USE_ALTERNATE_ADJ */
1442 } while ((v = v->next) != vm->boundstart);
1445 vm->mesh_kind = M_NONE;
1446 for (k = 1; k < ns; k++) {
1447 va = mesh_vert(vm, weld1->index, 0, k)->co;
1448 vb = mesh_vert(vm, weld2->index, 0, ns - k)->co;
1449 mid_v3_v3v3(co, va, vb);
1450 copy_v3_v3(mesh_vert(vm, weld1->index, 0, k)->co, co);
1451 create_mesh_bmvert(bm, vm, weld1->index, 0, k, bv->v);
1453 for (k = 1; k < ns; k++)
1454 copy_mesh_vert(vm, weld2->index, 0, ns - k, weld1->index, 0, k);
1457 switch (vm->mesh_kind) {
1462 bevel_build_poly(bm, bv);
1465 bevel_build_rings(bm, bv);
1468 bevel_build_trifan(bm, bv);
1471 bevel_build_quadstrip(bm, bv);
1476 /* take care, this flag isn't cleared before use, it just so happens that its not set */
1477 #define BM_BEVEL_EDGE_TAG_ENABLE(bme) BM_elem_flag_enable( (bme)->l, BM_ELEM_TAG)
1478 #define BM_BEVEL_EDGE_TAG_DISABLE(bme) BM_elem_flag_disable( (bme)->l, BM_ELEM_TAG)
1479 #define BM_BEVEL_EDGE_TAG_TEST(bme) BM_elem_flag_test( (bme)->l, BM_ELEM_TAG)
1482 * Construction around the vertex
1484 static void bevel_vert_construct(BMesh *bm, BevelParams *bp, BMVert *v)
1488 BMEdge *bme2, *unflagged_bme;
1492 int i, found_shared_face, ccw_test_sum;
1496 /* Gather input selected edges.
1497 * Only bevel selected edges that have exactly two incident faces.
1500 BM_ITER_ELEM (bme, &iter, v, BM_EDGES_OF_VERT) {
1501 if (BM_elem_flag_test(bme, BM_ELEM_TAG)) {
1502 BLI_assert(BM_edge_is_manifold(bme));
1509 /* signal this vert isn't being beveled */
1510 BM_elem_flag_disable(v, BM_ELEM_TAG);
1514 /* avoid calling BM_vert_edge_count since we loop over edges already */
1515 // ntot = BM_vert_edge_count(v);
1516 // BLI_assert(ntot == BM_vert_edge_count(v));
1518 bv = (BevVert *)BLI_memarena_alloc(bp->mem_arena, (sizeof(BevVert)));
1520 bv->edgecount = ntot;
1521 bv->selcount = nsel;
1522 bv->edges = (EdgeHalf *)BLI_memarena_alloc(bp->mem_arena, ntot * sizeof(EdgeHalf));
1523 bv->vmesh = (VMesh *)BLI_memarena_alloc(bp->mem_arena, sizeof(VMesh));
1524 bv->vmesh->seg = bp->seg;
1525 BLI_ghash_insert(bp->vert_hash, v, bv);
1527 /* add edges to bv->edges in order that keeps adjacent edges sharing
1528 * a face, if possible */
1531 BM_BEVEL_EDGE_TAG_ENABLE(bme);
1534 for (i = 0; i < ntot; i++) {
1536 /* find an unflagged edge bme2 that shares a face f with previous bme */
1537 found_shared_face = 0;
1538 unflagged_bme = NULL;
1539 BM_ITER_ELEM (bme2, &iter, v, BM_EDGES_OF_VERT) {
1540 if (BM_BEVEL_EDGE_TAG_TEST(bme2))
1543 unflagged_bme = bme2;
1544 BM_ITER_ELEM (f, &iter2, bme2, BM_FACES_OF_EDGE) {
1545 if (BM_face_edge_share_loop(f, bme)) {
1546 found_shared_face = 1;
1550 if (found_shared_face)
1554 if (found_shared_face) {
1557 bv->edges[i - 1].fnext = f;
1560 e->e = unflagged_bme;
1564 BM_BEVEL_EDGE_TAG_ENABLE(bme);
1565 if (BM_elem_flag_test(bme, BM_ELEM_TAG)) {
1573 e->is_rev = (bme->v2 == v);
1574 e->offset = e->is_bev ? bp->offset : 0.0f;
1576 /* find wrap-around shared face */
1577 BM_ITER_ELEM (f, &iter2, bme, BM_FACES_OF_EDGE) {
1578 if (BM_face_edge_share_loop(f, bv->edges[0].e)) {
1579 if (bv->edges[0].fnext == f)
1580 continue; /* if two shared faces, want the other one now */
1581 bv->edges[ntot - 1].fnext = f;
1582 bv->edges[0].fprev = f;
1587 /* do later when we loop over edges */
1589 /* clear BEVEL_EDGE_TAG now that we are finished with it*/
1590 for (i = 0; i < ntot; i++) {
1591 BM_BEVEL_EDGE_TAG_DISABLE(bv->edges[i].e);
1595 /* if edge array doesn't go CCW around vertex from average normal side,
1596 * reverse the array, being careful to reverse face pointers too */
1599 for (i = 0; i < ntot; i++)
1600 ccw_test_sum += bev_ccw_test(bv->edges[i].e, bv->edges[(i + 1) % ntot].e,
1601 bv->edges[i].fnext);
1602 if (ccw_test_sum < 0) {
1603 for (i = 0; i <= (ntot / 2) - 1; i++) {
1604 SWAP(EdgeHalf, bv->edges[i], bv->edges[ntot - i - 1]);
1605 SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
1606 SWAP(BMFace *, bv->edges[ntot - i - 1].fprev, bv->edges[ntot - i - 1].fnext);
1608 if (ntot % 2 == 1) {
1610 SWAP(BMFace *, bv->edges[i].fprev, bv->edges[i].fnext);
1615 for (i = 0, e = bv->edges; i < ntot; i++, e++) {
1616 e->next = &bv->edges[(i + 1) % ntot];
1617 e->prev = &bv->edges[(i + ntot - 1) % ntot];
1618 BM_BEVEL_EDGE_TAG_DISABLE(e->e);
1621 build_boundary(bp->mem_arena, bv);
1622 build_vmesh(bp->mem_arena, bm, bv);
1625 /* Face f has at least one beveled vertex. Rebuild f */
1626 static int bev_rebuild_polygon(BMesh *bm, BevelParams *bp, BMFace *f)
1631 BoundVert *v, *vstart, *vend;
1632 EdgeHalf *e, *eprev;
1635 int do_rebuild = FALSE;
1638 BLI_array_staticdeclare(vv, BM_DEFAULT_NGON_STACK_SIZE);
1640 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
1641 if (BM_elem_flag_test(l->v, BM_ELEM_TAG)) {
1643 bv = find_bevvert(bp, l->v);
1644 e = find_edge_half(bv, l->e);
1645 eprev = find_edge_half(bv, lprev->e);
1646 BLI_assert(e != NULL && eprev != NULL);
1647 vstart = eprev->leftv;
1654 BLI_array_append(vv, v->nv.v);
1656 if (vm->mesh_kind == M_NONE && v->ebev && v->ebev->seg > 1 && v->ebev != e && v->ebev != eprev) {
1657 /* case of 3rd face opposite a beveled edge, with no vmesh */
1660 for (k = 1; k < e->seg; k++) {
1661 bmv = mesh_vert(vm, i, 0, k)->v;
1662 BLI_array_append(vv, bmv);
1666 BLI_array_append(vv, v->nv.v);
1672 BLI_array_append(vv, l->v);
1676 BMFace *f_new = bev_create_ngon(bm, vv, BLI_array_count(vv), f);
1678 /* don't select newly created boundary faces... */
1680 BM_elem_flag_disable(f_new, BM_ELEM_TAG);
1688 /* All polygons touching v need rebuilding because beveling v has made new vertices */
1689 static void bevel_rebuild_existing_polygons(BMesh *bm, BevelParams *bp, BMVert *v)
1691 void *faces_stack[BM_DEFAULT_ITER_STACK_SIZE];
1692 int faces_len, f_index;
1693 BMFace **faces = BM_iter_as_arrayN(bm, BM_FACES_OF_VERT, v, &faces_len,
1694 faces_stack, BM_DEFAULT_ITER_STACK_SIZE);
1696 if (LIKELY(faces != NULL)) {
1697 for (f_index = 0; f_index < faces_len; f_index++) {
1698 BMFace *f = faces[f_index];
1699 if (bev_rebuild_polygon(bm, bp, f)) {
1700 BM_face_kill(bm, f);
1704 if (faces != (BMFace **)faces_stack) {
1712 * Build the polygons along the selected Edge
1714 static void bevel_build_edge_polygons(BMesh *bm, BevelParams *bp, BMEdge *bme)
1717 BMVert *bmv1, *bmv2, *bmv3, *bmv4, *bmv1i, *bmv2i, *bmv3i, *bmv4i;
1720 BMFace *f1, *f2, *f;
1721 int k, nseg, i1, i2;
1723 if (!BM_edge_is_manifold(bme))
1726 bv1 = find_bevvert(bp, bme->v1);
1727 bv2 = find_bevvert(bp, bme->v2);
1729 BLI_assert(bv1 && bv2);
1731 e1 = find_edge_half(bv1, bme);
1732 e2 = find_edge_half(bv2, bme);
1734 BLI_assert(e1 && e2);
1743 BLI_assert(nseg > 0 && nseg == e2->seg);
1745 bmv1 = e1->leftv->nv.v;
1746 bmv4 = e1->rightv->nv.v;
1747 bmv2 = e2->rightv->nv.v;
1748 bmv3 = e2->leftv->nv.v;
1750 BLI_assert(bmv1 && bmv2 && bmv3 && bmv4);
1752 f1 = boundvert_rep_face(e1->leftv);
1753 f2 = boundvert_rep_face(e1->rightv);
1756 bev_create_quad_tri(bm, bmv1, bmv2, bmv3, bmv4, f1);
1759 i1 = e1->leftv->index;
1760 i2 = e2->leftv->index;
1765 for (k = 1; k <= nseg; k++) {
1766 bmv4i = mesh_vert(vm1, i1, 0, k)->v;
1767 bmv3i = mesh_vert(vm2, i2, 0, nseg - k)->v;
1768 f = (k <= nseg / 2 + (nseg % 2)) ? f1 : f2;
1769 bev_create_quad_tri(bm, bmv1i, bmv2i, bmv3i, bmv4i, f);
1777 * - Currently only bevels BM_ELEM_TAG'd verts and edges.
1779 * - Newly created faces are BM_ELEM_TAG'd too,
1780 * the caller needs to ensure this is cleared before calling
1781 * if its going to use this face tag.
1783 * \warning all tagged edges _must_ be manifold.
1785 void BM_mesh_bevel(BMesh *bm, const float offset, const float segments)
1790 BevelParams bp = {NULL};
1795 if (bp.offset > 0) {
1797 bp.vert_hash = BLI_ghash_ptr_new(__func__);
1798 bp.mem_arena = BLI_memarena_new((1 << 16), __func__);
1799 BLI_memarena_use_calloc(bp.mem_arena);
1801 /* The analysis of the input vertices and execution additional constructions */
1802 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1803 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1804 bevel_vert_construct(bm, &bp, v);
1808 /* Build polygons for edges */
1809 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
1810 if (BM_elem_flag_test(e, BM_ELEM_TAG)) {
1811 bevel_build_edge_polygons(bm, &bp, e);
1815 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1816 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1817 bevel_rebuild_existing_polygons(bm, &bp, v);
1821 BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
1822 if (BM_elem_flag_test(v, BM_ELEM_TAG)) {
1823 BLI_assert(find_bevvert(&bp, v) != NULL);
1824 BM_vert_kill(bm, v);
1829 BLI_ghash_free(bp.vert_hash, NULL, NULL);
1830 BLI_memarena_free(bp.mem_arena);