3bf30e5fa144fa147e4ec11bd93384880054d814
[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                                                 int UNUSED(useRenderParams),
123                                                 int UNUSED(isFinalCalc))
124 {
125         BooleanModifierData *bmd = (BooleanModifierData*) md;
126         DerivedMesh *dm;
127
128         if(!bmd->object)
129                 return derivedData;
130
131         dm = bmd->object->derivedFinal;
132
133         if(dm) {
134                 DerivedMesh *result;
135
136                 /* when one of objects is empty (has got no faces) we could speed up
137                  * calculation a bit returning one of objects' derived meshes (or empty one)
138                  * Returning mesh is depended on modifiers operation (sergey) */
139                 result = get_quick_derivedMesh(derivedData, dm, bmd->operation);
140
141                 if(result == NULL) {
142
143                         DM_ensure_tessface(dm);          /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
144                         DM_ensure_tessface(derivedData); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
145
146                         // TIMEIT_START(NewBooleanDerivedMesh)
147
148                         result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
149                                         1 + bmd->operation);
150
151                         // TIMEIT_END(NewBooleanDerivedMesh)
152                 }
153
154                 /* if new mesh returned, return it; otherwise there was
155                  * an error, so delete the modifier object */
156                 if(result)
157                         return result;
158                 else
159                         modifier_setError(md, TIP_("Can't execute boolean operation."));
160         }
161         
162         return derivedData;
163 }
164 #else // WITH_MOD_BOOLEAN
165 static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
166                                                 DerivedMesh *derivedData,
167                                                 int UNUSED(useRenderParams),
168                                                 int UNUSED(isFinalCalc))
169 {
170         return derivedData;
171 }
172 #endif // WITH_MOD_BOOLEAN
173
174 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
175 {
176         CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE;
177
178         dataMask |= CD_MASK_MDEFORMVERT;
179         
180         return dataMask;
181 }
182
183
184 ModifierTypeInfo modifierType_Boolean = {
185         /* name */              "Boolean",
186         /* structName */        "BooleanModifierData",
187         /* structSize */        sizeof(BooleanModifierData),
188         /* type */              eModifierTypeType_Nonconstructive,
189         /* flags */             eModifierTypeFlag_AcceptsMesh
190                                                         | eModifierTypeFlag_UsesPointCache,
191
192         /* copyData */          copyData,
193         /* deformVerts */       NULL,
194         /* deformMatrices */    NULL,
195         /* deformVertsEM */     NULL,
196         /* deformMatricesEM */  NULL,
197         /* applyModifier */     applyModifier,
198         /* applyModifierEM */   NULL,
199         /* initData */          NULL,
200         /* requiredDataMask */  requiredDataMask,
201         /* freeData */          NULL,
202         /* isDisabled */        isDisabled,
203         /* updateDepgraph */    updateDepgraph,
204         /* dependsOnTime */     NULL,
205         /* dependsOnNormals */  NULL,
206         /* foreachObjectLink */ foreachObjectLink,
207         /* foreachIDLink */     NULL,
208         /* foreachTexLink */    NULL,
209 };