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