simplify some of the shape key code. no functional changes.
[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
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_modifier.h"
44
45 #include "depsgraph_private.h"
46
47 #include "MOD_boolean_util.h"
48 #include "MOD_util.h"
49
50 static void copyData(ModifierData *md, ModifierData *target)
51 {
52         BooleanModifierData *bmd = (BooleanModifierData*) md;
53         BooleanModifierData *tbmd = (BooleanModifierData*) target;
54
55         tbmd->object = bmd->object;
56         tbmd->operation = bmd->operation;
57 }
58
59 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
60 {
61         BooleanModifierData *bmd = (BooleanModifierData*) md;
62
63         return !bmd->object;
64 }
65
66 static void foreachObjectLink(
67                                                   ModifierData *md, Object *ob,
68            void (*walk)(void *userData, Object *ob, Object **obpoin),
69                   void *userData)
70 {
71         BooleanModifierData *bmd = (BooleanModifierData*) md;
72
73         walk(userData, ob, &bmd->object);
74 }
75
76 static void updateDepgraph(ModifierData *md, DagForest *forest,
77                                                 struct Scene *UNUSED(scene),
78                                                 Object *UNUSED(ob),
79                                                 DagNode *obNode)
80 {
81         BooleanModifierData *bmd = (BooleanModifierData*) md;
82
83         if(bmd->object) {
84                 DagNode *curNode = dag_get_node(forest, bmd->object);
85
86                 dag_add_relation(forest, curNode, obNode,
87                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Boolean Modifier");
88         }
89 }
90
91 #ifdef WITH_MOD_BOOLEAN
92 static DerivedMesh *get_quick_derivedMesh(DerivedMesh *derivedData, DerivedMesh *dm, int operation)
93 {
94         DerivedMesh *result = NULL;
95
96         if(derivedData->getNumPolys(derivedData) == 0 || dm->getNumPolys(dm) == 0) {
97                 switch(operation) {
98                         case eBooleanModifierOp_Intersect:
99                                 result = CDDM_new(0, 0, 0, 0, 0);
100                                 break;
101
102                         case eBooleanModifierOp_Union:
103                                 if(derivedData->getNumPolys(derivedData)) result = derivedData;
104                                 else result = CDDM_copy(dm);
105
106                                 break;
107
108                         case eBooleanModifierOp_Difference:
109                                 result = derivedData;
110                                 break;
111                 }
112         }
113
114         return result;
115 }
116
117 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
118                                                 DerivedMesh *derivedData,
119                                                 int UNUSED(useRenderParams),
120                                                 int UNUSED(isFinalCalc))
121 {
122         BooleanModifierData *bmd = (BooleanModifierData*) md;
123         DerivedMesh *dm;
124
125         if(!bmd->object)
126                 return derivedData;
127
128         dm = bmd->object->derivedFinal;
129
130         if(dm) {
131                 DerivedMesh *result;
132
133                 /* when one of objects is empty (has got no faces) we could speed up
134                    calculation a bit returning one of objects' derived meshes (or empty one)
135                    Returning mesh is depended on modifieier's operation (sergey) */
136                 result = get_quick_derivedMesh(derivedData, dm, bmd->operation);
137
138                 if(result == NULL) {
139
140                         DM_ensure_tessface(dm);          /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
141                         DM_ensure_tessface(derivedData); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */
142
143                         result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
144                                         1 + bmd->operation);
145                 }
146
147                 /* if new mesh returned, return it; otherwise there was
148                 * an error, so delete the modifier object */
149                 if(result)
150                         return result;
151                 else
152                         modifier_setError(md, "Can't execute boolean operation.");
153         }
154         
155         return derivedData;
156 }
157 #else // WITH_MOD_BOOLEAN
158 static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
159                                                 DerivedMesh *derivedData,
160                                                 int UNUSED(useRenderParams),
161                                                 int UNUSED(isFinalCalc))
162 {
163         return derivedData;
164 }
165 #endif // WITH_MOD_BOOLEAN
166
167 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
168 {
169         CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE;
170
171         dataMask |= CD_MASK_MDEFORMVERT;
172         
173         return dataMask;
174 }
175
176
177 ModifierTypeInfo modifierType_Boolean = {
178         /* name */              "Boolean",
179         /* structName */        "BooleanModifierData",
180         /* structSize */        sizeof(BooleanModifierData),
181         /* type */              eModifierTypeType_Nonconstructive,
182         /* flags */             eModifierTypeFlag_AcceptsMesh
183                                                         | eModifierTypeFlag_UsesPointCache,
184
185         /* copyData */          copyData,
186         /* deformVerts */       NULL,
187         /* deformMatrices */    NULL,
188         /* deformVertsEM */     NULL,
189         /* deformMatricesEM */  NULL,
190         /* applyModifier */     applyModifier,
191         /* applyModifierEM */   NULL,
192         /* initData */          NULL,
193         /* requiredDataMask */  requiredDataMask,
194         /* freeData */          NULL,
195         /* isDisabled */        isDisabled,
196         /* updateDepgraph */    updateDepgraph,
197         /* dependsOnTime */     NULL,
198         /* dependsOnNormals */  NULL,
199         /* foreachObjectLink */ foreachObjectLink,
200         /* foreachIDLink */     NULL,
201         /* foreachTexLink */    NULL,
202 };