svn merge -r37030:37035 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / modifiers / intern / MOD_boolean.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 /** \file blender/modifiers/intern/MOD_boolean.c
34  *  \ingroup modifiers
35  */
36
37 #include <stdio.h>
38
39 #include "DNA_object_types.h"
40
41 #include "BLI_utildefines.h"
42
43
44 #include "BKE_cdderivedmesh.h"
45 #include "BKE_modifier.h"
46
47 #include "depsgraph_private.h"
48
49 #include "MOD_boolean_util.h"
50 #include "MOD_util.h"
51
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->getNumFaces(derivedData) == 0 || dm->getNumFaces(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->getNumFaces(derivedData)) result = derivedData;
107                                 else result = CDDM_copy(dm, FALSE);
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 modifieier's operation (sergey) */
139                 result = get_quick_derivedMesh(derivedData, dm, bmd->operation);
140
141                 if(result == NULL) {
142                         result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
143                                         1 + bmd->operation);
144                 }
145
146                 /* if new mesh returned, return it; otherwise there was
147                 * an error, so delete the modifier object */
148                 if(result)
149                         return result;
150                 else
151                         modifier_setError(md, "Can't execute boolean operation.");
152         }
153         
154         return derivedData;
155 }
156 #else // WITH_MOD_BOOLEAN
157 static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
158                                                 DerivedMesh *derivedData,
159                                                 int UNUSED(useRenderParams),
160                                                 int UNUSED(isFinalCalc))
161 {
162         return derivedData;
163 }
164 #endif // WITH_MOD_BOOLEAN
165
166 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
167 {
168         CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE;
169
170         dataMask |= CD_MASK_MDEFORMVERT;
171         
172         return dataMask;
173 }
174
175
176 ModifierTypeInfo modifierType_Boolean = {
177         /* name */              "Boolean",
178         /* structName */        "BooleanModifierData",
179         /* structSize */        sizeof(BooleanModifierData),
180         /* type */              eModifierTypeType_Nonconstructive,
181         /* flags */             eModifierTypeFlag_AcceptsMesh
182                                                         | eModifierTypeFlag_UsesPointCache,
183
184         /* copyData */          copyData,
185         /* deformVerts */       NULL,
186         /* deformMatrices */    NULL,
187         /* deformVertsEM */     NULL,
188         /* deformMatricesEM */  NULL,
189         /* applyModifier */     applyModifier,
190         /* applyModifierEM */   NULL,
191         /* initData */          NULL,
192         /* requiredDataMask */  requiredDataMask,
193         /* freeData */          NULL,
194         /* isDisabled */        isDisabled,
195         /* updateDepgraph */    updateDepgraph,
196         /* dependsOnTime */     NULL,
197         /* dependsOnNormals */  NULL,
198         /* foreachObjectLink */ foreachObjectLink,
199         /* foreachIDLink */     NULL,
200 };