code cleanup: make bmesh operator names more consistant since python has access to...
[blender.git] / source / blender / modifiers / intern / MOD_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): Antony Riakiotakis
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  *
22  */
23
24 /** \file blender/modifiers/intern/MOD_triangulate.c
25  *  \ingroup modifiers
26  */
27
28 #include "DNA_object_types.h"
29
30 #include "BLI_utildefines.h"
31
32 #include "BKE_cdderivedmesh.h"
33 #include "BKE_modifier.h"
34 #include "BKE_tessmesh.h"
35
36 /* triangulation modifier, directly calls the bmesh operator */
37
38 static DerivedMesh *triangulate_dm(DerivedMesh *dm, const int flag)
39 {
40         DerivedMesh *result;
41         BMesh *bm;
42
43         bm = DM_to_bmesh(dm);
44
45         BM_mesh_elem_toolflags_ensure(bm);
46         BMO_push(bm, NULL);
47
48         BMO_op_callf(bm, BMO_FLAG_DEFAULTS,
49                      "triangulate faces=%af use_beauty=%b",
50                      (flag & MOD_TRIANGULATE_BEAUTY));
51         BMO_pop(bm);
52
53         result = CDDM_from_bmesh(bm, FALSE);
54         BM_mesh_free(bm);
55
56         CDDM_calc_edges(result);
57
58         CDDM_calc_normals(result);
59
60         return result;
61 }
62
63
64 static void initData(ModifierData *md)
65 {
66         TriangulateModifierData *tmd = (TriangulateModifierData *)md;
67
68         /* Enable in editmode by default */
69         md->mode |= eModifierMode_Editmode;
70         tmd->flag = MOD_TRIANGULATE_BEAUTY;
71 }
72
73
74 static void copyData(ModifierData *md, ModifierData *target)
75 {
76         TriangulateModifierData *smd = (TriangulateModifierData *) md;
77         TriangulateModifierData *tsmd = (TriangulateModifierData *) target;
78
79         *tsmd = *smd;
80 }
81
82 static DerivedMesh *applyModifierEM(ModifierData *md,
83                                     Object *UNUSED(ob),
84                                     struct BMEditMesh *UNUSED(em),
85                                     DerivedMesh *dm)
86 {
87         TriangulateModifierData *tmd = (TriangulateModifierData *)md;
88         DerivedMesh *result;
89         if (!(result = triangulate_dm(dm, tmd->flag))) {
90                 return dm;
91         }
92
93         return result;
94 }
95
96 static DerivedMesh *applyModifier(ModifierData *md,
97                                   Object *UNUSED(ob),
98                                   DerivedMesh *dm,
99                                   ModifierApplyFlag UNUSED(flag))
100 {
101         TriangulateModifierData *tmd = (TriangulateModifierData *)md;
102         DerivedMesh *result;
103         if (!(result = triangulate_dm(dm, tmd->flag))) {
104                 return dm;
105         }
106
107         return result;
108 }
109
110 ModifierTypeInfo modifierType_Triangulate = {
111         /* name */              "Triangulate",
112         /* structName */        "TriangulateModifierData",
113         /* structSize */        sizeof(TriangulateModifierData),
114         /* type */              eModifierTypeType_Constructive,
115         /* flags */             eModifierTypeFlag_AcceptsMesh |
116                                 eModifierTypeFlag_SupportsEditmode |
117                                 eModifierTypeFlag_SupportsMapping |
118                                 eModifierTypeFlag_EnableInEditmode |
119                                 eModifierTypeFlag_AcceptsCVs,
120
121         /* copyData */          copyData,
122         /* deformVerts */       NULL,
123         /* deformMatrices */    NULL,
124         /* deformVertsEM */     NULL,
125         /* deformMatricesEM */  NULL,
126         /* applyModifier */     applyModifier,
127         /* applyModifierEM */   applyModifierEM,
128         /* initData */          initData,
129         /* requiredDataMask */  NULL, //requiredDataMask,
130         /* freeData */          NULL,
131         /* isDisabled */        NULL,
132         /* updateDepgraph */    NULL,
133         /* dependsOnTime */     NULL,
134         /* dependsOnNormals */  NULL,
135         /* foreachObjectLink */ NULL,
136         /* foreachIDLink */     NULL,
137 };