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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2007 Blender Foundation.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): Geoffrey Bantle.
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/bmesh/intern/bmesh_construct.c
32 * BM construction functions.
35 #include "MEM_guardedalloc.h"
37 #include "BKE_customdata.h"
38 #include "BKE_utildefines.h"
40 #include "BLI_array.h"
41 #include "BLI_utildefines.h"
43 #include "DNA_meshdata_types.h"
44 #include "DNA_mesh_types.h"
47 #include "BLI_utildefines.h"
50 #include "bmesh_private.h"
59 static void bm_copy_loop_attributes(BMesh *source_mesh, BMesh *target_mesh,
60 const BMLoop *source_loop, BMLoop *target_loop);
66 * This file contains functions for making and destroying
67 * individual elements like verts, edges and faces.
74 * Creates a new vertex and returns a pointer
75 * to it. If a pointer to an example vertex is
76 * passed in, it's custom data and properties
77 * will be copied to the new vertex.
81 BMVert *BM_Make_Vert(BMesh *bm, float co[3], BMVert *example)
86 CustomData_bmesh_copy_data(&bm->vdata, &bm->vdata, example->head.data, &v->head.data);
93 * Creates a new edge betweeen two vertices and returns a
94 * pointer to it. If 'nodouble' equals 1, then a check is
95 * is done to make sure that an edge between those two vertices
96 * does not already exist. If it does, that edge is returned instead
97 * of creating a new one.
99 * If a new edge is created, and a pointer to an example edge is
100 * provided, it's custom data and properties will be copied to the
105 BMEdge *BM_Make_Edge(BMesh *bm, BMVert *v1, BMVert *v2, BMEdge *example, int nodouble)
109 if (nodouble) /* test if edge already exists. */
110 e = BM_Edge_Exist(v1, v2);
113 e = bmesh_me(bm, v1, v2);
116 CustomData_bmesh_copy_data(&bm->edata, &bm->edata, example->head.data, &e->head.data);
125 * BMESH MAKE QUADTRIANGLE
127 * Creates a new quad or triangle from
128 * a list of 3 or 4 vertices. If nodouble
129 * equals 1, then a check is done to see
130 * if a face with these vertices already
131 * exists and returns it instead. If a pointer
132 * to an example face is provided, it's custom
133 * data and properties will be copied to the new
136 * Note that the winding of the face is determined
137 * by the order of the vertices in the vertex array
140 BMFace *BM_Make_Face_QuadTri(BMesh *bm,
141 BMVert *v1, BMVert *v2, BMVert *v3, BMVert *v4,
142 const BMFace *example, const int nodouble)
144 BMVert *vtar[4] = {v1, v2, v3, v4};
145 return BM_Make_Face_QuadTri_v(bm, vtar, v4 ? 4 : 3, example, nodouble);
148 /* remove the edge array bits from this. Its not really needed? */
149 BMFace *BM_Make_Face_QuadTri_v(BMesh *bm, BMVert **verts, int len, const BMFace *example, const int nodouble)
151 BMEdge *edar[4] = {NULL};
155 edar[0] = BM_Edge_Exist(verts[0], verts[1]);
156 edar[1] = BM_Edge_Exist(verts[1], verts[2]);
158 edar[2] = BM_Edge_Exist(verts[2], verts[3]);
159 edar[3] = BM_Edge_Exist(verts[3], verts[0]);
162 edar[2] = BM_Edge_Exist(verts[2], verts[0]);
166 /* check if face exists or overlaps */
168 overlap = BM_Exist_Face_Overlaps(bm, verts, len, &f);
171 overlap = BM_Exist_Face_Overlaps(bm, verts, len, &f);
176 if ((!f) && (!overlap)) {
177 if (!edar[0]) edar[0] = BM_Make_Edge(bm, verts[0], verts[1], NULL, 0);
178 if (!edar[1]) edar[1] = BM_Make_Edge(bm, verts[1], verts[2], NULL, 0);
180 if (!edar[2]) edar[2] = BM_Make_Edge(bm, verts[2], verts[3], NULL, 0);
181 if (!edar[3]) edar[3] = BM_Make_Edge(bm, verts[3], verts[0], NULL, 0);
184 if (!edar[2]) edar[2] = BM_Make_Edge(bm, verts[2], verts[0], NULL, 0);
187 f = BM_Make_Face(bm, verts, edar, len, 0);
190 BM_Copy_Attributes(bm, bm, example, f);
198 /* copies face data from shared adjacent faces */
199 void BM_Face_CopyShared(BMesh *bm, BMFace *f)
206 l = BMIter_New(&iter, bm, BM_LOOPS_OF_FACE, f);
207 for ( ; l; l = BMIter_Step(&iter)) {
212 bm_copy_loop_attributes(bm, bm, l2, l);
216 bm_copy_loop_attributes(bm, bm, l2, l);
225 * Attempts to make a new Ngon from a list of edges.
226 * If nodouble equals one, a check for overlaps or existing
228 * The edges are not required to be ordered, simply to to form
229 * a single closed loop as a whole
231 * Note that while this function will work fine when the edges
232 * are already sorted, if the edges are always going to be sorted,
233 * BM_Make_Face should be considered over this function as it
234 * avoids some unnecessary work.
236 BMFace *BM_Make_Ngon(BMesh *bm, BMVert *v1, BMVert *v2, BMEdge **edges, int len, int nodouble)
238 BMEdge **edges2 = NULL;
239 BLI_array_staticdeclare(edges2, BM_NGON_STACK_SIZE);
240 BMVert **verts = NULL, *v;
241 BLI_array_staticdeclare(verts, BM_NGON_STACK_SIZE);
245 int i, /* j, */ v1found, reverse;
247 /* this code is hideous, yeek. I'll have to think about ways of
248 * cleaning it up. basically, it now combines the old BM_Make_Ngon
249 * _and_ the old bmesh_mf functions, so its kindof smashed together
252 if (!len || !v1 || !v2 || !edges || !bm)
255 /* put edges in correct order */
256 for (i = 0; i < len; i++) {
257 bmesh_api_setflag(edges[i], _FLAG_MF);
264 /* Swapping here improves performance and consistency of face
265 * structure in the special case that the edges are already in
266 * the correct order and winding */
267 SWAP(BMVert *, ev1, ev2);
270 BLI_array_append(verts, ev1);
276 BLI_array_append(verts, v);
277 BLI_array_append(edges2, e);
280 e2 = bmesh_disk_nextedge(e2, v);
281 if (e2 != e && bmesh_api_getflag(e2, _FLAG_MF)) {
282 v = BM_OtherEdgeVert(e2, v);
288 goto err; /* the edges do not form a closed loop */
291 } while (e != edges[0]);
293 if (BLI_array_count(edges2) != len) {
294 goto err; /* we didn't use all edges in forming the boundary loop */
297 /* ok, edges are in correct order, now ensure they are going
298 * in the correct direction */
299 v1found = reverse = 0;
300 for (i = 0; i < len; i++) {
301 if (BM_Vert_In_Edge(edges2[i], v1)) {
302 /* see if v1 and v2 are in the same edge */
303 if (BM_Vert_In_Edge(edges2[i], v2)) {
304 /* if v1 is shared by the *next* edge, then the winding
306 if (BM_Vert_In_Edge(edges2[(i + 1) % len], v1)) {
315 if (!v1found && BM_Vert_In_Edge(edges2[i], v2)) {
322 for (i = 0; i < len / 2; i++) {
324 verts[i] = verts[len - i - 1];
325 verts[len - i - 1] = v;
329 for (i = 0; i < len; i++) {
330 edges2[i] = BM_Edge_Exist(verts[i], verts[(i + 1) % len]);
336 f = BM_Make_Face(bm, verts, edges2, len, nodouble);
339 for (i = 0; i < len; i++) {
340 bmesh_api_clearflag(edges2[i], _FLAG_MF);
343 BLI_array_free(verts);
344 BLI_array_free(edges2);
349 for (i = 0; i < len; i++) {
350 bmesh_api_clearflag(edges[i], _FLAG_MF);
353 BLI_array_free(verts);
354 BLI_array_free(edges2);
360 /* bmesh_make_face_from_face(BMesh *bm, BMFace *source, BMFace *target) */
366 * Called by operators to remove elements that they have marked for
371 void BM_remove_tagged_faces(BMesh *bm, const int oflag)
376 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
377 if (BMO_TestFlag(bm, f, oflag)) {
383 void BM_remove_tagged_edges(BMesh *bm, const int oflag)
388 BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
389 if (BMO_TestFlag(bm, e, oflag)) {
395 void BM_remove_tagged_verts(BMesh *bm, const int oflag)
400 BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
401 if (BMO_TestFlag(bm, v, oflag)) {
407 static void bm_copy_vert_attributes(BMesh *source_mesh, BMesh *target_mesh,
408 const BMVert *source_vertex, BMVert *target_vertex)
410 if ((source_mesh == target_mesh) && (source_vertex == target_vertex)) {
413 copy_v3_v3(target_vertex->no, source_vertex->no);
414 CustomData_bmesh_free_block(&target_mesh->vdata, &target_vertex->head.data);
415 CustomData_bmesh_copy_data(&source_mesh->vdata, &target_mesh->vdata,
416 source_vertex->head.data, &target_vertex->head.data);
419 static void bm_copy_edge_attributes(BMesh *source_mesh, BMesh *target_mesh,
420 const BMEdge *source_edge, BMEdge *target_edge)
422 if ((source_mesh == target_mesh) && (source_edge == target_edge)) {
425 CustomData_bmesh_free_block(&target_mesh->edata, &target_edge->head.data);
426 CustomData_bmesh_copy_data(&source_mesh->edata, &target_mesh->edata,
427 source_edge->head.data, &target_edge->head.data);
430 static void bm_copy_loop_attributes(BMesh *source_mesh, BMesh *target_mesh,
431 const BMLoop *source_loop, BMLoop *target_loop)
433 if ((source_mesh == target_mesh) && (source_loop == target_loop)) {
436 CustomData_bmesh_free_block(&target_mesh->ldata, &target_loop->head.data);
437 CustomData_bmesh_copy_data(&source_mesh->ldata, &target_mesh->ldata,
438 source_loop->head.data, &target_loop->head.data);
441 static void bm_copy_face_attributes(BMesh *source_mesh, BMesh *target_mesh,
442 const BMFace *source_face, BMFace *target_face)
444 if ((source_mesh == target_mesh) && (source_face == target_face)) {
447 copy_v3_v3(target_face->no, source_face->no);
448 CustomData_bmesh_free_block(&target_mesh->pdata, &target_face->head.data);
449 CustomData_bmesh_copy_data(&source_mesh->pdata, &target_mesh->pdata,
450 source_face->head.data, &target_face->head.data);
451 target_face->mat_nr = source_face->mat_nr;
454 /* BMESH_TODO: Special handling for hide flags? */
456 void BM_Copy_Attributes(BMesh *source_mesh, BMesh *target_mesh, const void *source, void *target)
458 const BMHeader *sheader = source;
459 BMHeader *theader = target;
461 if (sheader->htype != theader->htype)
464 /* First we copy select */
465 if (BM_Selected(source_mesh, source)) BM_Select(target_mesh, target, TRUE);
467 /* Now we copy flags */
468 theader->hflag = sheader->hflag;
470 /* Copy specific attributes */
471 if (theader->htype == BM_VERT)
472 bm_copy_vert_attributes(source_mesh, target_mesh, (const BMVert *)source, (BMVert *)target);
473 else if (theader->htype == BM_EDGE)
474 bm_copy_edge_attributes(source_mesh, target_mesh, (const BMEdge *)source, (BMEdge *)target);
475 else if (theader->htype == BM_LOOP)
476 bm_copy_loop_attributes(source_mesh, target_mesh, (const BMLoop *)source, (BMLoop *)target);
477 else if (theader->htype == BM_FACE)
478 bm_copy_face_attributes(source_mesh, target_mesh, (const BMFace *)source, (BMFace *)target);
481 BMesh *BM_Copy_Mesh(BMesh *bmold)
484 BMVert *v, *v2, **vtable = NULL;
485 BMEdge *e, *e2, **edges = NULL, **etable = NULL;
486 BLI_array_declare(edges);
487 BMLoop *l, /* *l2, */ **loops = NULL;
488 BLI_array_declare(loops);
489 BMFace *f, *f2, **ftable = NULL;
490 BMEditSelection *ese;
492 int allocsize[4] = {512, 512, 2048, 512};
495 /* allocate a bmesh */
496 bm = BM_Make_Mesh(bmold->ob, allocsize);
498 CustomData_copy(&bmold->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
499 CustomData_copy(&bmold->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
500 CustomData_copy(&bmold->ldata, &bm->ldata, CD_MASK_BMESH, CD_CALLOC, 0);
501 CustomData_copy(&bmold->pdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
503 CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
504 CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
505 CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
506 CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
508 vtable = MEM_mallocN(sizeof(BMVert *) * bmold->totvert, "BM_Copy_Mesh vtable");
509 etable = MEM_mallocN(sizeof(BMEdge *) * bmold->totedge, "BM_Copy_Mesh etable");
510 ftable = MEM_mallocN(sizeof(BMFace *) * bmold->totface, "BM_Copy_Mesh ftable");
512 v = BMIter_New(&iter, bmold, BM_VERTS_OF_MESH, NULL);
513 for (i = 0; v; v = BMIter_Step(&iter), i++) {
514 v2 = BM_Make_Vert(bm, v->co, NULL); /* copy between meshes so cant use 'example' argument */
515 BM_Copy_Attributes(bmold, bm, v, v2);
517 BM_SetIndex(v, i); /* set_inline */
518 BM_SetIndex(v2, i); /* set_inline */
520 bmold->elem_index_dirty &= ~BM_VERT;
521 bm->elem_index_dirty &= ~BM_VERT;
524 BLI_assert(i == bmold->totvert);
526 e = BMIter_New(&iter, bmold, BM_EDGES_OF_MESH, NULL);
527 for (i = 0; e; e = BMIter_Step(&iter), i++) {
528 e2 = BM_Make_Edge(bm, vtable[BM_GetIndex(e->v1)],
529 vtable[BM_GetIndex(e->v2)], e, 0);
531 BM_Copy_Attributes(bmold, bm, e, e2);
533 BM_SetIndex(e, i); /* set_inline */
534 BM_SetIndex(e2, i); /* set_inline */
536 bmold->elem_index_dirty &= ~BM_EDGE;
537 bm->elem_index_dirty &= ~BM_EDGE;
540 BLI_assert(i == bmold->totedge);
542 f = BMIter_New(&iter, bmold, BM_FACES_OF_MESH, NULL);
543 for (i = 0; f; f = BMIter_Step(&iter), i++) {
544 BM_SetIndex(f, i); /* set_inline */
546 BLI_array_empty(loops);
547 BLI_array_empty(edges);
548 BLI_array_growitems(loops, f->len);
549 BLI_array_growitems(edges, f->len);
551 l = BMIter_New(&liter, bmold, BM_LOOPS_OF_FACE, f);
552 for (j = 0; j < f->len; j++, l = BMIter_Step(&liter)) {
554 edges[j] = etable[BM_GetIndex(l->e)];
557 v = vtable[BM_GetIndex(loops[0]->v)];
558 v2 = vtable[BM_GetIndex(loops[1]->v)];
560 if (!bmesh_verts_in_edge(v, v2, edges[0])) {
561 v = vtable[BM_GetIndex(loops[BLI_array_count(loops) - 1]->v)];
562 v2 = vtable[BM_GetIndex(loops[0]->v)];
565 f2 = BM_Make_Ngon(bm, v, v2, edges, f->len, 0);
568 /* use totface incase adding some faces fails */
569 BM_SetIndex(f2, (bm->totface - 1)); /* set_inline */
573 BM_Copy_Attributes(bmold, bm, f, f2);
574 copy_v3_v3(f2->no, f->no);
576 l = BMIter_New(&liter, bm, BM_LOOPS_OF_FACE, f2);
577 for (j = 0; j < f->len; j++, l = BMIter_Step(&liter)) {
578 BM_Copy_Attributes(bmold, bm, loops[j], l);
581 if (f == bmold->act_face) bm->act_face = f2;
583 bmold->elem_index_dirty &= ~BM_FACE;
584 bm->elem_index_dirty &= ~BM_FACE;
587 BLI_assert(i == bmold->totface);
589 /* copy over edit selection history */
590 for (ese = bmold->selected.first; ese; ese = ese->next) {
593 if (ese->htype == BM_VERT)
594 ele = vtable[BM_GetIndex(ese->data)];
595 else if (ese->htype == BM_EDGE)
596 ele = etable[BM_GetIndex(ese->data)];
597 else if (ese->htype == BM_FACE) {
598 ele = ftable[BM_GetIndex(ese->data)];
605 BM_store_selection(bm, ele);
612 BLI_array_free(loops);
613 BLI_array_free(edges);
619 char BM_Vert_Flag_From_MEFlag(const char meflag)
621 return ( ((meflag & SELECT) ? BM_SELECT : 0) |
622 ((meflag & ME_HIDE) ? BM_HIDDEN : 0)
625 char BM_Edge_Flag_From_MEFlag(const short meflag)
627 return ( ((meflag & SELECT) ? BM_SELECT : 0) |
628 ((meflag & ME_SEAM) ? BM_SEAM : 0) |
629 ((meflag & ME_SHARP) ? BM_SHARP : 0) |
630 ((meflag & ME_HIDE) ? BM_HIDDEN : 0)
633 char BM_Face_Flag_From_MEFlag(const char meflag)
635 return ( ((meflag & ME_FACE_SEL) ? BM_SELECT : 0) |
636 ((meflag & ME_SMOOTH) ? BM_SMOOTH : 0) |
637 ((meflag & ME_HIDE) ? BM_HIDDEN : 0)
642 char BM_Vert_Flag_To_MEFlag(BMVert *eve)
644 const char hflag = eve->head.hflag;
646 return ( ((hflag & BM_SELECT) ? SELECT : 0) |
647 ((hflag & BM_HIDDEN) ? ME_HIDE : 0)
650 short BM_Edge_Flag_To_MEFlag(BMEdge *eed)
652 const char hflag = eed->head.hflag;
654 return ( ((hflag & BM_SELECT) ? SELECT : 0) |
655 ((hflag & BM_SEAM) ? ME_SEAM : 0) |
656 ((hflag & BM_SHARP) ? ME_SHARP : 0) |
657 ((hflag & BM_HIDDEN) ? ME_HIDE : 0) |
658 ((BM_Wire_Edge(NULL, eed)) ? ME_LOOSEEDGE : 0) | /* not typical */
659 (ME_EDGEDRAW | ME_EDGERENDER)
662 char BM_Face_Flag_To_MEFlag(BMFace *efa)
664 const char hflag = efa->head.hflag;
666 return ( ((hflag & BM_SELECT) ? ME_FACE_SEL : 0) |
667 ((hflag & BM_SMOOTH) ? ME_SMOOTH : 0) |
668 ((hflag & BM_HIDDEN) ? ME_HIDE : 0)
672 /* unused, type spesific functions below */
677 Returns the flags stored in element,
678 which much be either a BMVert, BMEdge,
679 or BMFace, converted to mesh flags.
681 short BMFlags_To_MEFlags(void *element)
683 const char src_htype = ((BMHeader *)element)->htype;
685 if (src_htype == BM_FACE) {
686 return BM_Face_Flag_To_MEFlag(element);
688 else if (src_htype == BM_EDGE) {
689 return BM_Edge_Flag_To_MEFlag(element);
691 else if (src_htype == BM_VERT) {
692 return BM_Vert_Flag_To_MEFlag(element);
702 Returns the flags stored in element,
703 which much be either a MVert, MEdge,
704 or MPoly, converted to mesh flags.
705 type must be either BM_VERT, BM_EDGE,
708 char MEFlags_To_BMFlags(const short hflag, const char htype)
710 if (htype == BM_FACE) {
711 return BM_Face_Flag_From_MEFlag(hflag);
713 else if (htype == BM_EDGE) {
714 return BM_Edge_Flag_From_MEFlag(hflag);
716 else if (htype == BM_VERT) {
717 return BM_Vert_Flag_From_MEFlag(hflag);