code cleanup: make bmesh operator names more consistant since python has access to...
[blender.git] / source / blender / modifiers / intern / MOD_boolean.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  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_boolean.c
32  *  \ingroup modifiers
33  */
34
35 #include <stdio.h>
36
37 #include "DNA_object_types.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "BLF_translation.h"
42
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_modifier.h"
45
46 #include "depsgraph_private.h"
47
48 #include "MOD_boolean_util.h"
49 #include "MOD_util.h"
50
51 #include "PIL_time.h"
52
53 static void copyData(ModifierData *md, ModifierData *target)
54 {
55         BooleanModifierData *bmd = (BooleanModifierData *) md;
56         BooleanModifierData *tbmd = (BooleanModifierData *) target;
57
58         tbmd->object = bmd->object;
59         tbmd->operation = bmd->operation;
60 }
61
62 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
63 {
64         BooleanModifierData *bmd = (BooleanModifierData *) md;
65
66         return !bmd->object;
67 }
68
69 static void foreachObjectLink(
70         ModifierData *md, Object *ob,
71         void (*walk)(void *userData, Object *ob, Object **obpoin),
72         void *userData)
73 {
74         BooleanModifierData *bmd = (BooleanModifierData *) md;
75
76         walk(userData, ob, &bmd->object);
77 }
78
79 static void updateDepgraph(ModifierData *md, DagForest *forest,
80                            struct Scene *UNUSED(scene),
81                            Object *UNUSED(ob),
82                            DagNode *obNode)
83 {
84         BooleanModifierData *bmd = (BooleanModifierData *) md;
85
86         if (bmd->object) {
87                 DagNode *curNode = dag_get_node(forest, bmd->object);
88
89                 dag_add_relation(forest, curNode, obNode,
90                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Boolean Modifier");
91         }
92 }
93
94 #ifdef WITH_MOD_BOOLEAN
95 static DerivedMesh *get_quick_derivedMesh(DerivedMesh *derivedData, DerivedMesh *dm, int operation)
96 {
97         DerivedMesh *result = NULL;
98
99         if (derivedData->getNumPolys(derivedData) == 0 || dm->getNumPolys(dm) == 0) {
100                 switch (operation) {
101                         case eBooleanModifierOp_Intersect:
102                                 result = CDDM_new(0, 0, 0, 0, 0);
103                                 break;
104
105                         case eBooleanModifierOp_Union:
106                                 if (derivedData->getNumPolys(derivedData)) result = derivedData;
107                                 else result = CDDM_copy(dm);
108
109                                 break;
110
111                         case eBooleanModifierOp_Difference:
112                                 result = derivedData;
113                                 break;
114                 }
115         }
116
117         return result;
118 }
119
120 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
121                                   DerivedMesh *derivedData,
122                                   ModifierApplyFlag UNUSED(flag))
123 {
124         BooleanModifierData *bmd = (BooleanModifierData *) md;
125         DerivedMesh *dm;
126
127         if (!bmd->object)
128                 return derivedData;
129
130
131         /* 2.63 used this... */
132         /* dm = bmd->object->derivedFinal; */
133
134         /* but we want to make sure we can get the object
135          * in some cases the depsgraph fails us - especially for objects
136          * in other scenes when compositing */
137         if (bmd->object != ob) {
138                 dm = mesh_get_derived_final(md->scene, bmd->object, CD_MASK_MESH);
139         }
140         else {
141                 dm = NULL;
142         }
143
144         if (dm) {
145                 DerivedMesh *result;
146
147                 /* when one of objects is empty (has got no faces) we could speed up
148                  * calculation a bit returning one of objects' derived meshes (or empty one)
149                  * Returning mesh is depended on modifiers operation (sergey) */
150                 result = get_quick_derivedMesh(derivedData, dm, bmd->operation);
151
152                 if (result == NULL) {
153
154                         DM_ensure_tessface(dm);          /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
155                         DM_ensure_tessface(derivedData); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
156
157                         // TIMEIT_START(NewBooleanDerivedMesh)
158
159                         result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
160                                                        1 + bmd->operation);
161
162                         // TIMEIT_END(NewBooleanDerivedMesh)
163                 }
164
165                 /* if new mesh returned, return it; otherwise there was
166                  * an error, so delete the modifier object */
167                 if (result)
168                         return result;
169                 else
170                         modifier_setError(md, "Cannot execute boolean operation");
171         }
172         
173         return derivedData;
174 }
175 #else // WITH_MOD_BOOLEAN
176 static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
177                                   DerivedMesh *derivedData,
178                                   ModifierApplyFlag UNUSED(flag))
179 {
180         return derivedData;
181 }
182 #endif // WITH_MOD_BOOLEAN
183
184 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
185 {
186         CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE;
187
188         dataMask |= CD_MASK_MDEFORMVERT;
189         
190         return dataMask;
191 }
192
193
194 ModifierTypeInfo modifierType_Boolean = {
195         /* name */              "Boolean",
196         /* structName */        "BooleanModifierData",
197         /* structSize */        sizeof(BooleanModifierData),
198         /* type */              eModifierTypeType_Nonconstructive,
199         /* flags */             eModifierTypeFlag_AcceptsMesh |
200                                 eModifierTypeFlag_UsesPointCache,
201
202         /* copyData */          copyData,
203         /* deformVerts */       NULL,
204         /* deformMatrices */    NULL,
205         /* deformVertsEM */     NULL,
206         /* deformMatricesEM */  NULL,
207         /* applyModifier */     applyModifier,
208         /* applyModifierEM */   NULL,
209         /* initData */          NULL,
210         /* requiredDataMask */  requiredDataMask,
211         /* freeData */          NULL,
212         /* isDisabled */        isDisabled,
213         /* updateDepgraph */    updateDepgraph,
214         /* dependsOnTime */     NULL,
215         /* dependsOnNormals */  NULL,
216         /* foreachObjectLink */ foreachObjectLink,
217         /* foreachIDLink */     NULL,
218         /* foreachTexLink */    NULL,
219 };