590b5ea97c4053c0f4daaac607cff25a2f0159a2
[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  * Triangulate faces, also defines triangle fill.
27  */
28
29 #include "MEM_guardedalloc.h"
30 #include "DNA_listBase.h"
31
32 #include "BLI_math.h"
33 #include "BLI_smallhash.h"
34 #include "BLI_scanfill.h"
35
36 #include "bmesh.h"
37 #include "bmesh_tools.h"
38 #include "intern/bmesh_operators_private.h"
39
40
41 #define ELE_NEW         1
42 #define EDGE_MARK       4
43
44 void bmo_triangulate_exec(BMesh *bm, BMOperator *op)
45 {
46         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
47         BMOpSlot *slot_facemap_out = BMO_slot_get(op->slots_out, "face_map.out");
48
49         BM_mesh_elem_hflag_disable_all(bm, BM_FACE | BM_EDGE, BM_ELEM_TAG, false);
50         BMO_slot_buffer_hflag_enable(bm, op->slots_in, "faces", BM_FACE, BM_ELEM_TAG, false);
51
52         BM_mesh_triangulate(bm, use_beauty, true, op, slot_facemap_out);
53
54         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "edges.out", BM_EDGE, BM_ELEM_TAG);
55         BMO_slot_buffer_from_enabled_hflag(bm, op, op->slots_out, "faces.out", BM_FACE, BM_ELEM_TAG);
56 }
57
58
59 void bmo_triangle_fill_exec(BMesh *bm, BMOperator *op)
60 {
61         const bool use_beauty = BMO_slot_bool_get(op->slots_in, "use_beauty");
62         const bool use_dissolve = BMO_slot_bool_get(op->slots_in, "use_dissolve");
63         BMOIter siter;
64         BMEdge *e;
65         ScanFillContext sf_ctx;
66         /* ScanFillEdge *sf_edge; */ /* UNUSED */
67         ScanFillVert *sf_vert, *sf_vert_1, *sf_vert_2;
68         ScanFillFace *sf_tri;
69         SmallHash hash;
70         float normal[3], *normal_pt;
71
72         BLI_smallhash_init(&hash);
73
74         BMO_slot_vec_get(op->slots_in, "normal", normal);
75         
76         BLI_scanfill_begin(&sf_ctx);
77         
78         BMO_ITER (e, &siter, op->slots_in, "edges", BM_EDGE) {
79                 BMO_elem_flag_enable(bm, e, EDGE_MARK);
80                 
81                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v1)) {
82                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v1->co);
83                         sf_vert->tmp.p = e->v1;
84                         BLI_smallhash_insert(&hash, (uintptr_t)e->v1, sf_vert);
85                 }
86                 
87                 if (!BLI_smallhash_haskey(&hash, (uintptr_t)e->v2)) {
88                         sf_vert = BLI_scanfill_vert_add(&sf_ctx, e->v2->co);
89                         sf_vert->tmp.p = e->v2;
90                         BLI_smallhash_insert(&hash, (uintptr_t)e->v2, sf_vert);
91                 }
92                 
93                 sf_vert_1 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v1);
94                 sf_vert_2 = BLI_smallhash_lookup(&hash, (uintptr_t)e->v2);
95                 /* sf_edge = */ BLI_scanfill_edge_add(&sf_ctx, sf_vert_1, sf_vert_2);
96                 /* sf_edge->tmp.p = e; */ /* UNUSED */
97         }
98         
99         if (is_zero_v3(normal)) {
100                 normal_pt = NULL;
101         }
102         else {
103                 normalize_v3(normal);
104                 normal_pt = normal;
105         }
106
107         BLI_scanfill_calc_ex(&sf_ctx, BLI_SCANFILL_CALC_HOLES, normal_pt);
108         
109         for (sf_tri = sf_ctx.fillfacebase.first; sf_tri; sf_tri = sf_tri->next) {
110                 BMFace *f = BM_face_create_quad_tri(bm,
111                                                     sf_tri->v1->tmp.p, sf_tri->v2->tmp.p, sf_tri->v3->tmp.p, NULL,
112                                                     NULL, true);
113                 BMLoop *l;
114                 BMIter liter;
115                 
116                 BMO_elem_flag_enable(bm, f, ELE_NEW);
117                 BM_ITER_ELEM (l, &liter, f, BM_LOOPS_OF_FACE) {
118                         if (!BMO_elem_flag_test(bm, l->e, EDGE_MARK)) {
119                                 BMO_elem_flag_enable(bm, l->e, ELE_NEW);
120                         }
121                 }
122         }
123         
124         BLI_scanfill_end(&sf_ctx);
125         BLI_smallhash_release(&hash);
126         
127         if (use_beauty) {
128                 BMOperator bmop;
129
130                 BMO_op_initf(bm, &bmop, op->flag, "beautify_fill faces=%ff edges=%Fe", ELE_NEW, EDGE_MARK);
131                 BMO_op_exec(bm, &bmop);
132                 BMO_slot_buffer_flag_enable(bm, bmop.slots_out, "geom.out", BM_FACE | BM_EDGE, ELE_NEW);
133                 BMO_op_finish(bm, &bmop);
134         }
135         
136         BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
137
138         if (use_dissolve) {
139                 BMO_ITER (e, &siter, op->slots_out, "geom.out", BM_EDGE) {
140                         if (LIKELY(e->l)) {  /* in rare cases the edges face will have already been removed from the edge */
141                                 BMFace *f_new;
142                                 f_new = BM_faces_join_pair(bm, e->l->f,
143                                                            e->l->radial_next->f, e,
144                                                            false); /* join faces */
145                                 if (f_new) {
146                                         BMO_elem_flag_enable(bm, f_new, ELE_NEW);
147                                         BM_edge_kill(bm, e);
148                                 }
149                                 else {
150                                         BMO_error_clear(bm);
151                                 }
152                         }
153                         else {
154                                 BM_edge_kill(bm, e);
155                         }
156                 }
157
158                 BMO_slot_buffer_from_enabled_flag(bm, op, op->slots_out, "geom.out", BM_EDGE | BM_FACE, ELE_NEW);
159         }
160 }