5 * Functions for changing the topology of a mesh.
9 * ***** BEGIN GPL LICENSE BLOCK *****
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * The Original Code is Copyright (C) 2004 Blender Foundation.
26 * All rights reserved.
28 * The Original Code is: all of this file.
30 * Contributor(s): Geoffrey Bantle and Levi Schooley.
32 * ***** END GPL LICENSE BLOCK *****
35 /** \file blender/blenkernel/intern/BME_tools.c
42 #include "MEM_guardedalloc.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_object_types.h"
48 #include "BLI_utildefines.h"
50 #include "BKE_bmesh.h"
52 /*split this all into a seperate bevel.c file in src*/
54 /* ------- Bevel code starts here -------- */
56 BME_TransData_Head *BME_init_transdata(int bufsize) {
57 BME_TransData_Head *td;
59 td = MEM_callocN(sizeof(BME_TransData_Head), "BMesh transdata header");
60 td->gh = BLI_ghash_new(BLI_ghashutil_ptrhash,BLI_ghashutil_ptrcmp, "BME_init_transdata gh");
61 td->ma = BLI_memarena_new(bufsize, "BME_TransData arena");
62 BLI_memarena_use_calloc(td->ma);
67 void BME_free_transdata(BME_TransData_Head *td) {
68 BLI_ghash_free(td->gh,NULL,NULL);
69 BLI_memarena_free(td->ma);
73 BME_TransData *BME_assign_transdata(BME_TransData_Head *td, BME_Mesh *bm, BME_Vert *v,
74 float *co, float *org, float *vec, float *loc,
75 float factor, float weight, float maxfactor, float *max) {
79 if (v == NULL) return NULL;
81 if ((vtd = BLI_ghash_lookup(td->gh, v)) == NULL && bm != NULL) {
82 vtd = BLI_memarena_alloc(td->ma, sizeof(*vtd));
83 BLI_ghash_insert(td->gh, v, vtd);
90 if (co != NULL) VECCOPY(vtd->co,co);
91 if (org == NULL && is_new) { VECCOPY(vtd->org,v->co); } /* default */
92 else if (org != NULL) VECCOPY(vtd->org,org);
94 VECCOPY(vtd->vec,vec);
95 normalize_v3(vtd->vec);
100 vtd->weight = weight;
101 vtd->maxfactor = maxfactor;
107 BME_TransData *BME_get_transdata(BME_TransData_Head *td, BME_Vert *v) {
109 vtd = BLI_ghash_lookup(td->gh, v);
113 /* a hack (?) to use the transdata memarena to allocate floats for use with the max limits */
114 float *BME_new_transdata_float(BME_TransData_Head *td) {
115 return BLI_memarena_alloc(td->ma, sizeof(float));
118 static int BME_is_nonmanifold_vert(BME_Mesh *UNUSED(bm), BME_Vert *v) {
121 int len, count, flag;
128 /* count edges while looking for non-manifold edges */
130 for (len=0,e=v->e; e != oe || (e == oe && len == 0); len++,e=BME_disk_nextedge(e,v)) {
136 if (BME_cycle_length(&(e->l->radial)) > 2) {
137 /* edge shared by more than two faces */
148 if (l->v == v) l = l->prev;
151 count++; /* count the edges */
153 if (flag && l->radial_next == l) {
154 /* we've hit the edge of an open mesh, reset once */
161 else if (l->radial_next == l) {
171 /* vert shared by multiple regions */
178 /* a wrapper for BME_JFKE that [for now just] checks to
179 * make sure loop directions are compatible */
180 static BME_Poly *BME_JFKE_safe(BME_Mesh *bm, BME_Poly *f1, BME_Poly *f2, BME_Edge *e) {
184 l2 = l1->radial_next;
185 if (l1->v == l2->v) {
186 BME_loop_reverse(bm, f2);
189 return BME_JFKE(bm, f1, f2, e);
192 /* a wrapper for BME_SFME that transfers element flags */
193 static BME_Poly *BME_split_face(BME_Mesh *bm, BME_Poly *f, BME_Vert *v1, BME_Vert *v2, BME_Loop **nl, BME_Edge *example) {
195 nf = BME_SFME(bm,f,v1,v2,nl);
197 /* if the edge was selected, select this face, too */
198 if (example && (example->flag & SELECT)) f->flag |= ME_FACE_SEL;
200 nf->mat_nr = f->mat_nr;
202 (*nl)->e->flag = example->flag;
203 (*nl)->e->h = example->h;
204 (*nl)->e->crease = example->crease;
205 (*nl)->e->bweight = example->bweight;
213 static void BME_data_interp_from_verts(BME_Mesh *bm, BME_Vert *v1, BME_Vert *v2, BME_Vert *v, float fac)
217 if (v1->data && v2->data) {
222 CustomData_bmesh_interp(&bm->vdata, src, w, NULL, 2, v->data);
228 static void BME_data_facevert_edgesplit(BME_Mesh *bm, BME_Vert *v1, BME_Vert *UNUSED(v2), BME_Vert *v, BME_Edge *e1, float fac){
231 BME_Loop *l=NULL, *v1loop = NULL, *vloop = NULL, *v2loop = NULL;
241 vloop = v1loop->next;
242 v2loop = vloop->next;
250 src[0] = v1loop->data;
251 src[1] = v2loop->data;
253 CustomData_bmesh_interp(&bm->ldata, src,w, NULL, 2, vloop->data);
259 /* a wrapper for BME_SEMV that transfers element flags */ /*add custom data interpolation in here!*/
260 static BME_Vert *BME_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Edge *e, BME_Edge **ne, float percent) {
264 v2 = BME_edge_getothervert(e,v);
265 nv = BME_SEMV(bm,v,e,ne);
266 if (nv == NULL) return NULL;
267 VECSUB(nv->co,v2->co,v->co);
268 len = len_v3(nv->co);
269 VECADDFAC(nv->co,v->co,nv->co,len*percent);
271 nv->bweight = v->bweight;
273 (*ne)->flag = e->flag;
275 (*ne)->crease = e->crease;
276 (*ne)->bweight = e->bweight;
279 BME_data_facevert_edgesplit(bm,v2, v, nv, e, 0.75);
283 static void BME_collapse_vert(BME_Mesh *bm, BME_Edge *ke, BME_Vert *kv, float fac){
286 BME_Loop *l=NULL, *kvloop=NULL, *tvloop=NULL;
287 BME_Vert *tv = BME_edge_getothervert(ke,kv);
295 if(l->v == tv && l->next->v == kv){
299 src[0] = kvloop->data;
300 src[1] = tvloop->data;
301 CustomData_bmesh_interp(&bm->ldata, src,w, NULL, 2, kvloop->data);
311 static int BME_bevel_is_split_vert(BME_Loop *l) {
312 /* look for verts that have already been added to the edge when
313 * beveling other polys; this can be determined by testing the
314 * vert and the edges around it for originality
316 if ((l->v->tflag1 & BME_BEVEL_ORIG)==0
317 && (l->e->tflag1 & BME_BEVEL_ORIG)
318 && (l->prev->e->tflag1 & BME_BEVEL_ORIG))
325 /* get a vector, vec, that points from v1->co to wherever makes sense to
326 * the bevel operation as a whole based on the relationship between v1 and v2
327 * (won't necessarily be a vec from v1->co to v2->co, though it probably will be);
328 * the return value is -1 for failure, 0 if we used vert co's, and 1 if we used transform origins */
329 static int BME_bevel_get_vec(float *vec, BME_Vert *v1, BME_Vert *v2, BME_TransData_Head *td) {
330 BME_TransData *vtd1, *vtd2;
332 vtd1 = BME_get_transdata(td,v1);
333 vtd2 = BME_get_transdata(td,v2);
334 if (!vtd1 || !vtd2) {
335 //printf("BME_bevel_get_vec() got called without proper BME_TransData\n");
339 /* compare the transform origins to see if we can use the vert co's;
340 * if they belong to different origins, then we will use the origins to determine
342 if (compare_v3v3(vtd1->org,vtd2->org,0.000001f)) {
343 VECSUB(vec,v2->co,v1->co);
344 if (len_v3(vec) < 0.000001f) {
350 VECSUB(vec,vtd2->org,vtd1->org);
351 if (len_v3(vec) < 0.000001f) {
358 /* "Projects" a vector perpendicular to vec2 against vec1, such that
359 * the projected vec1 + vec2 has a min distance of 1 from the "edge" defined by vec2.
360 * note: the direction, is_forward, is used in conjunction with up_vec to determine
361 * whether this is a convex or concave corner. If it is a concave corner, it will
362 * be projected "backwards." If vec1 is before vec2, is_forward should be 0 (we are projecting backwards).
363 * vec1 is the vector to project onto (expected to be normalized)
364 * vec2 is the direction of projection (pointing away from vec1)
365 * up_vec is used for orientation (expected to be normalized)
366 * returns the length of the projected vector that lies along vec1 */
367 static float BME_bevel_project_vec(float *vec1, float *vec2, float *up_vec, int is_forward, BME_TransData_Head *UNUSED(td)) {
368 float factor, vec3[3], tmp[3],c1,c2;
370 cross_v3_v3v3(tmp,vec1,vec2);
372 factor = dot_v3v3(up_vec,tmp);
373 if ((factor > 0 && is_forward) || (factor < 0 && !is_forward)) {
374 cross_v3_v3v3(vec3,vec2,tmp); /* hmm, maybe up_vec should be used instead of tmp */
377 cross_v3_v3v3(vec3,tmp,vec2); /* hmm, maybe up_vec should be used instead of tmp */
380 c1 = dot_v3v3(vec3,vec1);
381 c2 = dot_v3v3(vec1,vec1);
382 if (fabs(c1) < 0.000001f || fabs(c2) < 0.000001f) {
392 /* BME_bevel_split_edge() is the main math work-house; its responsibilities are:
393 * using the vert and the loop passed, get or make the split vert, set its coordinates
394 * and transform properties, and set the max limits.
395 * Finally, return the split vert. */
396 static BME_Vert *BME_bevel_split_edge(BME_Mesh *bm, BME_Vert *v, BME_Vert *v1, BME_Loop *l, float *up_vec, float value, BME_TransData_Head *td) {
397 BME_TransData *vtd, *vtd1, *vtd2;
398 BME_Vert *sv, *v2, *v3, *ov;
400 BME_Edge *ne, *e1, *e2;
401 float maxfactor, scale, len, dis, vec1[3], vec2[3], t_up_vec[3];
402 int is_edge, forward, is_split_vert;
405 /* what you call operator overloading in C :)
406 * I wanted to use the same function for both wire edges and poly loops
407 * so... here we walk around edges to find the needed verts */
411 //printf("We can't split a loose vert's edge!\n");
414 e1 = v->e; /* we just use the first two edges */
415 e2 = BME_disk_nextedge(v->e, v);
417 //printf("You need at least two edges to use BME_bevel_split_edge()\n");
420 v2 = BME_edge_getothervert(e1, v);
421 v3 = BME_edge_getothervert(e2, v);
422 if (v1 != v2 && v1 != v3) {
423 //printf("Error: more than 2 edges in v's disk cycle, or v1 does not share an edge with v\n");
432 ov = BME_edge_getothervert(e1,v);
433 sv = BME_split_edge(bm,v,e1,&ne,0);
434 //BME_data_interp_from_verts(bm, v, ov, sv, 0.25); /*this is technically wrong...*/
435 //BME_data_interp_from_faceverts(bm, v, ov, sv, 0.25);
436 //BME_data_interp_from_faceverts(bm, ov, v, sv, 0.25);
437 BME_assign_transdata(td, bm, sv, sv->co, sv->co, NULL, sv->co, 0, -1, -1, NULL); /* quick default */
438 sv->tflag1 |= BME_BEVEL_BEVEL;
439 ne->tflag1 = BME_BEVEL_ORIG; /* mark edge as original, even though it isn't */
440 BME_bevel_get_vec(vec1,v1,v,td);
441 BME_bevel_get_vec(vec2,v2,v,td);
442 cross_v3_v3v3(t_up_vec,vec1,vec2);
443 normalize_v3(t_up_vec);
447 /* establish loop direction */
455 else if (l->next->v == v) {
460 v2 = l->next->next->v;
463 //printf("ERROR: BME_bevel_split_edge() - v must be adjacent to l\n");
467 if (BME_bevel_is_split_vert(lv1)) {
470 if (forward) v1 = l->next->next->v;
471 else v1 = l->prev->v;
475 ov = BME_edge_getothervert(l->e,v);
476 sv = BME_split_edge(bm,v,l->e,&ne,0);
477 //BME_data_interp_from_verts(bm, v, ov, sv, 0.25); /*this is technically wrong...*/
478 //BME_data_interp_from_faceverts(bm, v, ov, sv, 0.25);
479 //BME_data_interp_from_faceverts(bm, ov, v, sv, 0.25);
480 BME_assign_transdata(td, bm, sv, sv->co, sv->co, NULL, sv->co, 0, -1, -1, NULL); /* quick default */
481 sv->tflag1 |= BME_BEVEL_BEVEL;
482 ne->tflag1 = BME_BEVEL_ORIG; /* mark edge as original, even though it isn't */
485 if (BME_bevel_is_split_vert(lv2)) {
486 if (forward) v2 = lv2->prev->v;
487 else v2 = lv2->next->v;
491 is_edge = BME_bevel_get_vec(vec1,v,v1,td); /* get the vector we will be projecting onto */
492 BME_bevel_get_vec(vec2,v,v2,td); /* get the vector we will be projecting parallel to */
496 vtd = BME_get_transdata(td, sv);
497 vtd1 = BME_get_transdata(td, v);
498 vtd2 = BME_get_transdata(td,v1);
500 if (vtd1->loc == NULL) {
501 /* this is a vert with data only for calculating initial weights */
502 if (vtd1->weight < 0) {
505 scale = vtd1->weight/vtd1->factor;
507 vtd1->max = BME_new_transdata_float(td);
512 scale = vtd1->weight;
514 vtd->max = vtd1->max;
516 if (is_edge && vtd1->loc != NULL) {
517 maxfactor = vtd1->maxfactor;
520 maxfactor = scale*BME_bevel_project_vec(vec1,vec2,up_vec,forward,td);
521 if (vtd->maxfactor > 0 && vtd->maxfactor < maxfactor) {
522 maxfactor = vtd->maxfactor;
526 dis = (v1->tflag1 & BME_BEVEL_ORIG)? len/3 : len/2;
527 if (is_edge || dis > maxfactor*value) {
528 dis = maxfactor*value;
530 VECADDFAC(sv->co,v->co,vec1,dis);
531 VECSUB(vec1,sv->co,vtd1->org);
534 BME_assign_transdata(td, bm, sv, vtd1->org, vtd1->org, vec1, sv->co, dis, scale, maxfactor, vtd->max);
539 static float BME_bevel_set_max(BME_Vert *v1, BME_Vert *v2, float value, BME_TransData_Head *td) {
540 BME_TransData *vtd1, *vtd2;
541 float max, fac1, fac2, vec1[3], vec2[3], vec3[3];
543 BME_bevel_get_vec(vec1,v1,v2,td);
544 vtd1 = BME_get_transdata(td,v1);
545 vtd2 = BME_get_transdata(td,v2);
547 if (vtd1->loc == NULL) {
551 VECCOPY(vec2,vtd1->vec);
552 mul_v3_fl(vec2,vtd1->factor);
553 if (dot_v3v3(vec1, vec1)) {
554 project_v3_v3v3(vec2,vec2,vec1);
555 fac1 = len_v3(vec2)/value;
562 if (vtd2->loc == NULL) {
566 VECCOPY(vec3,vtd2->vec);
567 mul_v3_fl(vec3,vtd2->factor);
568 if (dot_v3v3(vec1, vec1)) {
569 project_v3_v3v3(vec2,vec3,vec1);
570 fac2 = len_v3(vec2)/value;
578 max = len_v3(vec1)/(fac1 + fac2);
579 if (vtd1->max && (*vtd1->max < 0 || max < *vtd1->max)) {
582 if (vtd2->max && (*vtd2->max < 0 || max < *vtd2->max)) {
593 static BME_Vert *BME_bevel_wire(BME_Mesh *bm, BME_Vert *v, float value, int res, int UNUSED(options), BME_TransData_Head *td) {
594 BME_Vert *ov1, *ov2, *v1, *v2;
596 ov1 = BME_edge_getothervert(v->e, v);
597 ov2 = BME_edge_getothervert(BME_disk_nextedge(v->e, v), v);
599 /* split the edges */
600 v1 = BME_bevel_split_edge(bm,v,ov1,NULL,NULL,value,td);
601 v1->tflag1 |= BME_BEVEL_NONMAN;
602 v2 = BME_bevel_split_edge(bm,v,ov2,NULL,NULL,value,td);
603 v2->tflag1 |= BME_BEVEL_NONMAN;
606 BME_bevel_set_max(v1,ov1,value,td);
607 BME_bevel_set_max(v2,ov2,value,td);
610 /* remove the original vert */
618 static BME_Loop *BME_bevel_edge(BME_Mesh *bm, BME_Loop *l, float value, int UNUSED(options), float *up_vec, BME_TransData_Head *td) {
619 BME_Vert *v1, *v2, *kv;
620 BME_Loop *kl=NULL, *nl;
627 if ((l->e->tflag1 & BME_BEVEL_BEVEL) == 0
628 && ((l->v->tflag1 & BME_BEVEL_BEVEL) || (l->next->v->tflag1 & BME_BEVEL_BEVEL)))
633 /* checks and operations for prev edge */
634 /* first, check to see if this edge was inset previously */
635 if ((l->prev->e->tflag1 & BME_BEVEL_ORIG) == 0
636 && (l->v->tflag1 & BME_BEVEL_NONMAN) == 0) {
637 kl = l->prev->radial_next;
638 if (kl->v == l->v) kl = kl->prev;
645 /* get/make the first vert to be used in SFME */
646 if (l->v->tflag1 & BME_BEVEL_NONMAN){
649 else { /* we'll need to split the previous edge */
650 v1 = BME_bevel_split_edge(bm,l->v,NULL,l->prev,up_vec,value,td);
652 /* if we need to clean up geometry... */
656 BME_split_face(bm,kl->f,kl->prev->v,kl->next->v,&nl,kl->prev->e);
657 BME_JFKE(bm,((BME_Loop*)kl->prev->radial_next)->f,kl->f,kl->prev->e);
658 BME_collapse_vert(bm, kl->e, kv, 1.0);
659 //BME_JEKV(bm,kl->e,kv);
663 BME_split_face(bm,kl->f,kl->next->next->v,kl->v,&nl,kl->next->e);
664 BME_JFKE(bm,((BME_Loop*)kl->next->radial_next)->f,kl->f,kl->next->e);
665 BME_collapse_vert(bm, kl->e, kv, 1.0);
666 //BME_JEKV(bm,kl->e,kv);
671 /* checks and operations for the next edge */
672 /* first, check to see if this edge was inset previously */
673 if ((l->next->e->tflag1 & BME_BEVEL_ORIG) == 0
674 && (l->next->v->tflag1 & BME_BEVEL_NONMAN) == 0) {
675 kl = l->next->radial_next;
676 if (kl->v == l->next->v) kl = kl->prev;
683 /* get/make the second vert to be used in SFME */
684 if (l->next->v->tflag1 & BME_BEVEL_NONMAN) {
687 else { /* we'll need to split the next edge */
688 v2 = BME_bevel_split_edge(bm,l->next->v,NULL,l->next,up_vec,value,td);
690 /* if we need to clean up geometry... */
693 BME_split_face(bm,kl->f,kl->prev->v,kl->next->v,&nl,kl->prev->e);
694 BME_JFKE(bm,((BME_Loop*)kl->prev->radial_next)->f,kl->f,kl->prev->e);
695 BME_collapse_vert(bm, kl->e, kv, 1.0);
696 //BME_JEKV(bm,kl->e,kv);
699 BME_split_face(bm,kl->f,kl->next->next->v,kl->v,&nl,kl->next->e);
700 BME_JFKE(bm,((BME_Loop*)kl->next->radial_next)->f,kl->f,kl->next->e);
701 BME_collapse_vert(bm, kl->e, kv, 1.0);
702 //BME_JEKV(bm,kl->e,kv);
706 if ((v1->tflag1 & BME_BEVEL_NONMAN)==0 || (v2->tflag1 & BME_BEVEL_NONMAN)==0) {
707 BME_split_face(bm,f,v2,v1,&l,e);
708 l->e->tflag1 = BME_BEVEL_BEVEL;
713 //printf("Whoops! You got something out of order in BME_bevel_edge()!\n");
719 static BME_Loop *BME_bevel_vert(BME_Mesh *bm, BME_Loop *l, float value, int UNUSED(options), float *up_vec, BME_TransData_Head *td) {
723 /* get/make the first vert to be used in SFME */
724 /* may need to split the previous edge */
725 v1 = BME_bevel_split_edge(bm,l->v,NULL,l->prev,up_vec,value,td);
727 /* get/make the second vert to be used in SFME */
728 /* may need to split this edge (so move l) */
730 v2 = BME_bevel_split_edge(bm,l->next->v,NULL,l->next,up_vec,value,td);
733 /* "cut off" this corner */
734 f = BME_split_face(bm,l->f,v2,v1,NULL,l->e);
742 * Polygon inset tool:
744 * Insets a polygon/face based on the tflag1's of its vertices
745 * and edges. Used by the bevel tool only, for now.
746 * The parameter "value" is the distance to inset (should be negative).
747 * The parameter "options" is not currently used.
750 * A BME_Poly pointer to the resulting inner face.
752 static BME_Poly *BME_bevel_poly(BME_Mesh *bm, BME_Poly *f, float value, int options, BME_TransData_Head *td) {
754 BME_TransData *vtd1, *vtd2;
755 float up_vec[3], vec1[3], vec2[3], vec3[3], fac1, fac2, max = -1;
761 /* find a good normal for this face (there's better ways, I'm sure) */
764 for (i=0,ol=f->loopbase,l=ol->next; l->next!=ol; l=l->next) {
765 BME_bevel_get_vec(vec1,l->next->v,ol->v,td);
766 BME_bevel_get_vec(vec2,l->v,ol->v,td);
767 cross_v3_v3v3(vec3,vec2,vec1);
768 VECADD(up_vec,up_vec,vec3);
771 mul_v3_fl(up_vec,1.0f/i);
772 normalize_v3(up_vec);
774 for (i=0,len=f->len; i<len; i++,l=l->next) {
775 if ((l->e->tflag1 & BME_BEVEL_BEVEL) && (l->e->tflag1 & BME_BEVEL_ORIG)) {
777 l = BME_bevel_edge(bm, l, value, options, up_vec, td);
779 else if ((l->v->tflag1 & BME_BEVEL_BEVEL) && (l->v->tflag1 & BME_BEVEL_ORIG) && (l->prev->e->tflag1 & BME_BEVEL_BEVEL) == 0) {
781 l = BME_bevel_vert(bm, l, value, options, up_vec, td);
786 if (value > 0.5 && max > 0) {
788 for (i=0,len=f->len; i<len; i++,l=l->next) {
789 if ((l->e->tflag1 & BME_BEVEL_BEVEL) || (l->e->tflag1 & BME_BEVEL_ORIG)) {
790 BME_bevel_get_vec(vec1,l->v,l->next->v,td);
791 vtd1 = BME_get_transdata(td,l->v);
792 vtd2 = BME_get_transdata(td,l->next->v);
793 if (vtd1->loc == NULL) {
797 VECCOPY(vec2,vtd1->vec);
798 mul_v3_fl(vec2,vtd1->factor);
799 if (dot_v3v3(vec1, vec1)) {
800 project_v3_v3v3(vec2,vec2,vec1);
801 fac1 = len_v3(vec2)/value;
807 if (vtd2->loc == NULL) {
811 VECCOPY(vec3,vtd2->vec);
812 mul_v3_fl(vec3,vtd2->factor);
813 if (dot_v3v3(vec1, vec1)) {
814 project_v3_v3v3(vec2,vec3,vec1);
815 fac2 = len_v3(vec2)/value;
822 max = len_v3(vec1)/(fac1 + fac2);
823 if (vtd1->max && (*vtd1->max < 0 || max < *vtd1->max)) {
826 if (vtd2->max && (*vtd2->max < 0 || max < *vtd2->max)) {
837 static void BME_bevel_add_vweight(BME_TransData_Head *td, BME_Mesh *bm, BME_Vert *v, float weight, float factor, int options) {
840 if (v->tflag1 & BME_BEVEL_NONMAN) return;
841 v->tflag1 |= BME_BEVEL_BEVEL;
842 if ( (vtd = BME_get_transdata(td, v)) ) {
843 if (options & BME_BEVEL_EMIN) {
845 if (vtd->weight < 0 || weight < vtd->weight) {
846 vtd->weight = weight;
849 else if (options & BME_BEVEL_EMAX) {
851 if (weight > vtd->weight) {
852 vtd->weight = weight;
855 else if (vtd->weight < 0) {
856 vtd->factor = factor;
857 vtd->weight = weight;
860 vtd->factor += factor; /* increment number of edges with weights (will be averaged) */
861 vtd->weight += weight; /* accumulate all the weights */
865 /* we'll use vtd->loc == NULL to mark that this vert is not moving */
866 vtd = BME_assign_transdata(td, bm, v, v->co, NULL, NULL, NULL, factor, weight, -1, NULL);
870 static float BME_bevel_get_angle(BME_Mesh *UNUSED(bm), BME_Edge *e, BME_Vert *v) {
873 float vec1[3], vec2[3], vec3[3], vec4[3];
876 l2 = e->l->radial_next;
882 v1 = l1->next->next->v;
885 VECSUB(vec1,v1->co,v->co);
886 VECSUB(vec2,v2->co,v->co);
887 cross_v3_v3v3(vec3,vec1,vec2);
895 v1 = l1->next->next->v;
898 VECSUB(vec1,v1->co,v->co);
899 VECSUB(vec2,v2->co,v->co);
900 cross_v3_v3v3(vec4,vec2,vec1);
905 return dot_v3v3(vec3,vec4);
907 static int BME_face_sharededges(BME_Poly *f1, BME_Poly *f2){
913 if(BME_radial_find_face(l->e,f2)) count++;
915 }while(l != f1->lbase);
920 * BME_bevel_initialize
922 * Prepare the mesh for beveling:
924 * Sets the tflag1's of the mesh elements based on the options passed.
927 * A BME_Mesh pointer to the BMesh passed as a parameter.
929 static BME_Mesh *BME_bevel_initialize(BME_Mesh *bm, int options, int UNUSED(defgrp_index), float angle, BME_TransData_Head *td) {
937 float weight, threshold;
940 for (v=bm->verts.first; v; v=v->next) {
943 v->tflag1 = BME_BEVEL_ORIG;
944 /* originally coded, a vertex gets tagged with BME_BEVEL_BEVEL in this pass if
945 * the vert is manifold (or is shared by only two edges - wire bevel)
946 * BME_BEVEL_SELECT is passed and the vert has v->flag&SELECT or
947 * BME_BEVEL_VWEIGHT is passed, and the vert has a defgrp and weight
948 * BME_BEVEL_ANGLE is not passed
949 * BME_BEVEL_EWEIGHT is not passed
951 /* originally coded, a vertex gets tagged with BME_BEVEL_NONMAN in this pass if
952 * the vert is loose, shared by multiple regions, or is shared by wire edges
953 * note: verts belonging to edges of open meshes are not tagged with BME_BEVEL_NONMAN
955 /* originally coded, a vertex gets a transform weight set in this pass if
956 * BME_BEVEL_VWEIGHT is passed, and the vert has a defgrp and weight
959 /* get disk cycle length */
964 len = BME_cycle_length(BME_disk_getpointer(v->e,v));
965 /* we'll assign a default transform data to every vert (except the loose ones) */
966 vtd = BME_assign_transdata(td, bm, v, v->co, v->co, NULL, NULL, 0, -1, -1, NULL);
969 /* check for non-manifold vert */
970 if (BME_is_nonmanifold_vert(bm,v)) {
971 v->tflag1 |= BME_BEVEL_NONMAN;
974 /* BME_BEVEL_BEVEL tests */
975 if ((v->tflag1 & BME_BEVEL_NONMAN) == 0 || len == 2) { /* either manifold vert, or wire vert */
976 if (((options & BME_BEVEL_SELECT) && (v->flag & SELECT))
977 || ((options & BME_BEVEL_WEIGHT) && (options & BME_BEVEL_VERT)) /* use weights for verts */
978 || ((options & BME_BEVEL_ANGLE) == 0
979 && (options & BME_BEVEL_SELECT) == 0
980 && (options & BME_BEVEL_WEIGHT) == 0))
982 if (options & BME_BEVEL_WEIGHT) {
983 /* do vert weight stuff */
984 //~ dvert = CustomData_em_get(&bm->vdata,v->data,CD_MDEFORMVERT);
985 //~ if (!dvert) continue;
986 //~ for (i = 0; i < dvert->totweight; ++i) {
987 //~ if(dvert->dw[i].def_nr == defgrp_index) {
988 //~ dw = &dvert->dw[i];
992 //~ if (!dw || dw->weight == 0.0) continue;
993 if (v->bweight == 0.0) continue;
994 vtd = BME_assign_transdata(td, bm, v, v->co, v->co, NULL, NULL, 1.0, v->bweight, -1, NULL);
995 v->tflag1 |= BME_BEVEL_BEVEL;
998 vtd = BME_assign_transdata(td, bm, v, v->co, v->co, NULL, NULL, 1.0, 1.0, -1, NULL);
999 v->tflag1 |= BME_BEVEL_BEVEL;
1006 threshold = (float)cos((angle + 0.001) * M_PI / 180.0);
1007 for (e=bm->edges.first; e; e=e->next) {
1008 e->tflag1 = BME_BEVEL_ORIG;
1010 /* originally coded, an edge gets tagged with BME_BEVEL_BEVEL in this pass if
1011 * BME_BEVEL_VERT is not set
1012 * the edge is manifold (shared by exactly two faces)
1013 * BME_BEVEL_SELECT is passed and the edge has e->flag&SELECT or
1014 * BME_BEVEL_EWEIGHT is passed, and the edge has the crease set or
1015 * BME_BEVEL_ANGLE is passed, and the edge is sharp enough
1016 * BME_BEVEL_VWEIGHT is passed, and both verts are set for bevel
1018 /* originally coded, a vertex gets tagged with BME_BEVEL_BEVEL in this pass if
1019 * the vert belongs to the edge
1020 * the vert is not tagged with BME_BEVEL_NONMAN
1021 * the edge is eligible for bevel (even if BME_BEVEL_VERT is set, or the edge is shared by less than 2 faces)
1023 /* originally coded, a vertex gets a transform weight set in this pass if
1024 * the vert belongs to the edge
1025 * the edge has a weight
1027 /* note: edge weights are cumulative at the verts,
1028 * i.e. the vert's weight is the average of the weights of its weighted edges
1033 e->v1->tflag1 |= BME_BEVEL_NONMAN;
1034 e->v2->tflag1 |= BME_BEVEL_NONMAN;
1037 len = BME_cycle_length(&(e->l->radial));
1041 /* non-manifold edge of the worst kind */
1045 if ((options & BME_BEVEL_SELECT) && (e->flag & SELECT)) {
1047 /* stupid editmode doesn't always flush selections, or something */
1048 e->v1->flag |= SELECT;
1049 e->v2->flag |= SELECT;
1051 else if ((options & BME_BEVEL_WEIGHT) && (options & BME_BEVEL_VERT) == 0) {
1052 weight = e->bweight;
1054 else if (options & BME_BEVEL_ANGLE) {
1055 if ((e->v1->tflag1 & BME_BEVEL_NONMAN) == 0 && BME_bevel_get_angle(bm,e,e->v1) < threshold) {
1056 e->tflag1 |= BME_BEVEL_BEVEL;
1057 e->v1->tflag1 |= BME_BEVEL_BEVEL;
1058 BME_bevel_add_vweight(td, bm, e->v1, 1.0, 1.0, options);
1061 BME_bevel_add_vweight(td, bm, e->v1, 0.0, 1.0, options);
1063 if ((e->v2->tflag1 & BME_BEVEL_NONMAN) == 0 && BME_bevel_get_angle(bm,e,e->v2) < threshold) {
1064 e->tflag1 |= BME_BEVEL_BEVEL;
1065 e->v2->tflag1 |= BME_BEVEL_BEVEL;
1066 BME_bevel_add_vweight(td, bm, e->v2, 1.0, 1.0, options);
1069 BME_bevel_add_vweight(td, bm, e->v2, 0.0, 1.0, options);
1072 //~ else if ((options & BME_BEVEL_VWEIGHT) && (options & BME_BEVEL_VERT) == 0) {
1073 //~ if ((e->v1->tflag1 & BME_BEVEL_BEVEL) && (e->v2->tflag1 & BME_BEVEL_BEVEL)) {
1074 //~ e->tflag1 |= BME_BEVEL_BEVEL;
1077 else if ((options & BME_BEVEL_SELECT) == 0
1078 && (options & BME_BEVEL_VERT) == 0)
1084 e->tflag1 |= BME_BEVEL_BEVEL;
1085 BME_bevel_add_vweight(td, bm, e->v1, weight, 1.0, options);
1086 BME_bevel_add_vweight(td, bm, e->v2, weight, 1.0, options);
1089 if (len != 2 || options & BME_BEVEL_VERT) {
1090 e->tflag1 &= ~BME_BEVEL_BEVEL;
1095 for (f=bm->polys.first; f; f=f->next) f->tflag1 = BME_BEVEL_ORIG;
1097 /*clean up edges with 2 faces that share more than one edge*/
1098 for (e=bm->edges.first; e; e=e->next){
1099 if(e->tflag1 & BME_BEVEL_BEVEL){
1101 count = BME_face_sharededges(e->l->f, ((BME_Loop*)e->l->radial_next)->f);
1103 e->tflag1 &= ~BME_BEVEL_BEVEL;
1111 /* tags all elements as originals */
1112 static BME_Mesh *BME_bevel_reinitialize(BME_Mesh *bm) {
1117 for (v = bm->verts.first; v; v=v->next) {
1118 v->tflag1 |= BME_BEVEL_ORIG;
1121 for (e=bm->edges.first; e; e=e->next) {
1122 e->tflag1 |= BME_BEVEL_ORIG;
1125 for (f=bm->polys.first; f; f=f->next) {
1126 f->tflag1 |= BME_BEVEL_ORIG;
1135 * Mesh beveling tool:
1137 * Bevels an entire mesh. It currently uses the tflag1's of
1138 * its vertices and edges to track topological changes.
1139 * The parameter "value" is the distance to inset (should be negative).
1140 * The parameter "options" is not currently used.
1143 * A BME_Mesh pointer to the BMesh passed as a parameter.
1146 static void bmesh_dissolve_disk(BME_Mesh *bm, BME_Vert *v){
1155 e = v->e; /*loop the edge looking for a edge to dissolve*/
1158 len = BME_cycle_length(&(e->l->radial));
1160 f = BME_JFKE_safe(bm,e->l->f, ((BME_Loop*)(e->l->radial_next))->f, e);
1166 e = BME_disk_nextedge(e,v);
1169 BME_collapse_vert(bm, v->e, v, 1.0);
1170 //BME_JEKV(bm,v->e,v);
1173 static BME_Mesh *BME_bevel_mesh(BME_Mesh *bm, float value, int res, int options, int UNUSED(defgrp_index), BME_TransData_Head *td) {
1178 unsigned int i, len;
1180 for (f=bm->polys.first; f; f=f->next) {
1181 if(f->tflag1 & BME_BEVEL_ORIG) {
1182 BME_bevel_poly(bm,f,value,options,td);
1186 /* here we will loop through all the verts to clean up the left over geometry */
1187 /* crazy idea. when res == 0, don't remove the original geometry */
1188 for (v = bm->verts.first; v; /* we may kill v, so increment in-loop */) {
1190 if ((v->tflag1 & BME_BEVEL_NONMAN) && (v->tflag1 & BME_BEVEL_BEVEL) && (v->tflag1 & BME_BEVEL_ORIG)) {
1191 v = BME_bevel_wire(bm, v, value, res, options, td);
1193 else if (res && ((v->tflag1 & BME_BEVEL_BEVEL) && (v->tflag1 & BME_BEVEL_ORIG))) {
1195 /* first, make sure we're not sitting on an edge to be removed */
1197 e = BME_disk_nextedge(oe,v);
1198 while ((e->tflag1 & BME_BEVEL_BEVEL) && (e->tflag1 & BME_BEVEL_ORIG)) {
1199 e = BME_disk_nextedge(e,v);
1201 //printf("Something's wrong! We can't remove every edge here!\n");
1205 /* look for original edges, and remove them */
1207 while ( (e = BME_disk_next_edgeflag(oe, v, 0, BME_BEVEL_ORIG | BME_BEVEL_BEVEL)) ) {
1209 /* join the faces (we'll split them later) */
1210 f = BME_JFKE_safe(bm,e->l->f,((BME_Loop*)e->l->radial_next)->f,e);
1212 //printf("Non-manifold geometry not getting tagged right?\n");
1216 /*need to do double check *before* you bevel to make sure that manifold edges are for two faces that share only *one* edge to make sure it doesnt hang here!*/
1219 /* all original edges marked to be beveled have been removed;
1220 * now we need to link up the edges for this "corner" */
1221 len = BME_cycle_length(BME_disk_getpointer(v->e, v));
1222 for (i=0,e=v->e; i < len; i++,e=BME_disk_nextedge(e,v)) {
1224 l2 = l->radial_next;
1225 if (l->v != v) l = l->next;
1226 if (l2->v != v) l2 = l2->next;
1227 /* look for faces that have had the original edges removed via JFKE */
1228 if (l->f->len > 3) {
1229 BME_split_face(bm,l->f,l->next->v,l->prev->v,&l,l->e); /* clip this corner off */
1231 l->e->tflag1 |= BME_BEVEL_BEVEL;
1234 if (l2->f->len > 3) {
1235 BME_split_face(bm,l2->f,l2->next->v,l2->prev->v,&l,l2->e); /* clip this corner off */
1237 l->e->tflag1 |= BME_BEVEL_BEVEL;
1241 bmesh_dissolve_disk(bm, v);
1249 static BME_Mesh *BME_tesselate(BME_Mesh *bm) {
1250 BME_Loop *l, *nextloop;
1253 for (f=bm->polys.first; f; f=f->next) {
1255 while (l->f->len > 4) {
1256 nextloop = l->next->next->next;
1258 BME_split_face(bm,l->f,l->v,nextloop->v,NULL,l->e);
1266 /*Main bevel function:
1267 Should be only one exported
1271 /* options that can be passed:
1272 * BME_BEVEL_VWEIGHT <---- v, Look at vertex weights; use defgrp_index if option is present
1273 * BME_BEVEL_SELECT <---- v,e, check selection for verts and edges
1274 * BME_BEVEL_ANGLE <---- v,e, don't bevel-tag verts - tag verts per edge
1275 * BME_BEVEL_VERT <---- e, don't tag edges
1276 * BME_BEVEL_EWEIGHT <---- e, use crease flag for now
1277 * BME_BEVEL_PERCENT <---- Will need to think about this one; will probably need to incorporate into actual bevel routine
1278 * BME_BEVEL_RADIUS <---- Will need to think about this one; will probably need to incorporate into actual bevel routine
1279 * All weights/limits are stored per-vertex
1281 BME_Mesh *BME_bevel(BME_Mesh *bm, float value, int res, int options, int defgrp_index, float angle, BME_TransData_Head **rtd) {
1283 BME_TransData_Head *td;
1288 td = BME_init_transdata(BLI_MEMARENA_STD_BUFSIZE);
1290 BME_bevel_initialize(bm, options, defgrp_index, angle, td);
1292 /* recursion math courtesy of Martin Poirier (theeth) */
1293 for (i=0; i<res-1; i++) {
1294 if (i==0) fac += 1.0f/3.0f; else fac += 1.0f/(3 * i * 2.0f);
1297 /* crazy idea. if res == 0, don't remove original geometry */
1298 for (i=0; i<res || (res==0 && i==0); i++) {
1299 if (i != 0) BME_bevel_reinitialize(bm);
1300 BME_model_begin(bm);
1301 BME_bevel_mesh(bm,d,res,options,defgrp_index,td);
1303 if (i==0) d /= 3; else d /= 2;
1313 /* transform pass */
1314 for (v = bm->verts.first; v; v=v->next) {
1315 if ( (vtd = BME_get_transdata(td, v)) ) {
1316 if (vtd->max && (*vtd->max > 0 && value > *vtd->max)) {
1322 VECADDFAC(v->co,vtd->org,vtd->vec,vtd->factor*d);
1327 BME_free_transdata(td);