Fix #30028: VSE Paste Strip Crash
[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 #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->getNumFaces(derivedData) == 0 || dm->getNumFaces(dm) == 0) {
99                 switch(operation) {
100                         case eBooleanModifierOp_Intersect:
101                                 result = CDDM_new(0, 0, 0);
102                                 break;
103
104                         case eBooleanModifierOp_Union:
105                                 if(derivedData->getNumFaces(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                         // TIMEIT_START(NewBooleanDerivedMesh)
142
143                         result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob,
144                                         1 + bmd->operation);
145
146                         // TIMEIT_END(NewBooleanDerivedMesh)
147                 }
148
149                 /* if new mesh returned, return it; otherwise there was
150                 * an error, so delete the modifier object */
151                 if(result)
152                         return result;
153                 else
154                         modifier_setError(md, "Can't execute boolean operation.");
155         }
156         
157         return derivedData;
158 }
159 #else // WITH_MOD_BOOLEAN
160 static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob),
161                                                 DerivedMesh *derivedData,
162                                                 int UNUSED(useRenderParams),
163                                                 int UNUSED(isFinalCalc))
164 {
165         return derivedData;
166 }
167 #endif // WITH_MOD_BOOLEAN
168
169 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md))
170 {
171         CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE;
172
173         dataMask |= CD_MASK_MDEFORMVERT;
174         
175         return dataMask;
176 }
177
178
179 ModifierTypeInfo modifierType_Boolean = {
180         /* name */              "Boolean",
181         /* structName */        "BooleanModifierData",
182         /* structSize */        sizeof(BooleanModifierData),
183         /* type */              eModifierTypeType_Nonconstructive,
184         /* flags */             eModifierTypeFlag_AcceptsMesh
185                                                         | eModifierTypeFlag_UsesPointCache,
186
187         /* copyData */          copyData,
188         /* deformVerts */       NULL,
189         /* deformMatrices */    NULL,
190         /* deformVertsEM */     NULL,
191         /* deformMatricesEM */  NULL,
192         /* applyModifier */     applyModifier,
193         /* applyModifierEM */   NULL,
194         /* initData */          NULL,
195         /* requiredDataMask */  requiredDataMask,
196         /* freeData */          NULL,
197         /* isDisabled */        isDisabled,
198         /* updateDepgraph */    updateDepgraph,
199         /* dependsOnTime */     NULL,
200         /* dependsOnNormals */  NULL,
201         /* foreachObjectLink */ foreachObjectLink,
202         /* foreachIDLink */     NULL,
203         /* foreachTexLink */    NULL,
204 };