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