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