a8bf53a136f35e23fee9dbb34f0a35cc8ef7f7ec
[blender.git] / source / blender / bmesh / operators / bmo_triangulate.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * Contributor(s): Joseph Eagar.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/bmesh/operators/bmo_triangulate.c
24  *  \ingroup bmesh
25  */
26
27 #include "MEM_guardedalloc.h"
28 #include "DNA_listBase.h"
29
30 #include "BLI_math.h"
31 #include "BLI_smallhash.h"
32 #include "BLI_scanfill.h"
33
34 #include "bmesh.h"
35 #include "intern/bmesh_private.h"
36
37
38 #define ELE_NEW         1
39 #define EDGE_MARK       4
40
41 void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
42 {
43         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
44         BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
45
46         BM_mesh_elem_hflag_disable_all(bm, BM_FACE | BM_EDGE, BM_ELEM_TAG, false);
47         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
48
49         BM_mesh_triangulate(bm, use_beauty, true, op, slot_facemap_out);
50
51         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "edges.out", BM_EDGE, BM_ELEM_TAG);
52         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
53 }
54
55
56 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
57 {
58         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
59         BMOIter siter;
60         BMEdge *e;
61         ScanFillContext sf_ctx;
62         /* ScanFillEdge *sf_edge; */ /* UNUSED */
63         ScanFillVert *sf_vert, *sf_vert_1, *sf_vert_2;
64         ScanFillFace *sf_tri;
65         SmallHash hash;
66
67         BLI_smallhash_init(&hash);
68         
69         BLI_scanfill_begin(&sf_ctx);
70         
71         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
72                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
73                 
74                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
75                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v1->co);
76                         sf_vert->tmp.p = e->v1;
77                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, sf_vert);
78                 }
79                 
80                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
81                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v2->co);
82                         sf_vert->tmp.p = e->v2;
83                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, sf_vert);
84                 }
85                 
86                 sf_vert_1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
87                 sf_vert_2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
88                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, sf_vert_1, sf_vert_2);
89                 /* sf_edge->tmp.p = e; */ /* UNUSED */
90         }
91         
92         BLI_scanfill_calc(&sf_ctx, BLI_SCANFILL_CALC_HOLES);
93         
94         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
95                 BMFace *f = BM_face_create_quad_tri(bm,
96                                                     sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
97                                                     NULL, true);
98                 BMLoop *l;
99                 BMIter liter;
100                 
101                 BMO_elem_flag_enable(bm, f, ELE_NEW);
102                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
103                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
104                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
105                         }
106                 }
107         }
108         
109         BLI_scanfill_end(&sf_ctx);
110         BLI_smallhash_release(&hash);
111         
112         if (use_beauty) {
113                 BMOperator bmop;
114
115                 BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
116                 BMO_op_exec(bm, &bmop);
117                 BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
118                 BMO_op_finish(bm, &bmop);
119         }
120         
121         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
122 }