| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2011-10-23 17:52:20 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software  Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2005 by the Blender Foundation. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Daniel Dunbar | 
					
						
							|  |  |  |  *                 Ton Roosendaal, | 
					
						
							|  |  |  |  *                 Ben Batt, | 
					
						
							|  |  |  |  *                 Brecht Van Lommel, | 
					
						
							|  |  |  |  *                 Campbell Barton | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-25 13:57:17 +00:00
										 |  |  | /** \file blender/modifiers/intern/MOD_boolean.c
 | 
					
						
							|  |  |  |  *  \ingroup modifiers | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2011-02-25 13:57:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-04 04:01:27 +00:00
										 |  |  | #include "DNA_object_types.h"
 | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 18:36:47 +00:00
										 |  |  | #include "BLI_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-07 19:18:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | #include "BKE_cdderivedmesh.h"
 | 
					
						
							|  |  |  | #include "BKE_modifier.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "depsgraph_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MOD_boolean_util.h"
 | 
					
						
							| 
									
										
										
										
											2011-02-13 14:16:36 +00:00
										 |  |  | #include "MOD_util.h"
 | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-30 09:19:38 +00:00
										 |  |  | #include "PIL_time.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | static void copyData(ModifierData *md, ModifierData *target) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BooleanModifierData *bmd = (BooleanModifierData*) md; | 
					
						
							|  |  |  | 	BooleanModifierData *tbmd = (BooleanModifierData*) target; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tbmd->object = bmd->object; | 
					
						
							|  |  |  | 	tbmd->operation = bmd->operation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 06:29:17 +00:00
										 |  |  | static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BooleanModifierData *bmd = (BooleanModifierData*) md; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return !bmd->object; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void foreachObjectLink( | 
					
						
							|  |  |  | 						  ModifierData *md, Object *ob, | 
					
						
							|  |  |  | 	   void (*walk)(void *userData, Object *ob, Object **obpoin), | 
					
						
							|  |  |  | 		  void *userData) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BooleanModifierData *bmd = (BooleanModifierData*) md; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	walk(userData, ob, &bmd->object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 06:29:17 +00:00
										 |  |  | static void updateDepgraph(ModifierData *md, DagForest *forest, | 
					
						
							|  |  |  | 						struct Scene *UNUSED(scene), | 
					
						
							|  |  |  | 						Object *UNUSED(ob), | 
					
						
							|  |  |  | 						DagNode *obNode) | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BooleanModifierData *bmd = (BooleanModifierData*) md; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(bmd->object) { | 
					
						
							|  |  |  | 		DagNode *curNode = dag_get_node(forest, bmd->object); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dag_add_relation(forest, curNode, obNode, | 
					
						
							| 
									
										
										
										
											2011-11-11 13:09:14 +00:00
										 |  |  | 		                 DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Boolean Modifier"); | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-23 15:40:13 +00:00
										 |  |  | #ifdef WITH_MOD_BOOLEAN
 | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | static DerivedMesh *get_quick_derivedMesh(DerivedMesh *derivedData, DerivedMesh *dm, int operation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	DerivedMesh *result = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-29 13:01:51 +00:00
										 |  |  | 	if(derivedData->getNumPolys(derivedData) == 0 || dm->getNumPolys(dm) == 0) { | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 		switch(operation) { | 
					
						
							|  |  |  | 			case eBooleanModifierOp_Intersect: | 
					
						
							| 
									
										
										
										
											2011-05-31 06:39:57 +00:00
										 |  |  | 				result = CDDM_new(0, 0, 0, 0, 0); | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			case eBooleanModifierOp_Union: | 
					
						
							| 
									
										
										
										
											2011-11-29 13:01:51 +00:00
										 |  |  | 				if(derivedData->getNumPolys(derivedData)) result = derivedData; | 
					
						
							| 
									
										
										
										
											2012-01-29 21:59:47 +00:00
										 |  |  | 				else result = CDDM_copy(dm); | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			case eBooleanModifierOp_Difference: | 
					
						
							|  |  |  | 				result = derivedData; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 06:29:17 +00:00
										 |  |  | static DerivedMesh *applyModifier(ModifierData *md, Object *ob, | 
					
						
							|  |  |  | 						DerivedMesh *derivedData, | 
					
						
							|  |  |  | 						int UNUSED(useRenderParams), | 
					
						
							|  |  |  | 						int UNUSED(isFinalCalc)) | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	BooleanModifierData *bmd = (BooleanModifierData*) md; | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 	DerivedMesh *dm; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(!bmd->object) | 
					
						
							|  |  |  | 		return derivedData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dm = bmd->object->derivedFinal; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(dm) { | 
					
						
							|  |  |  | 		DerivedMesh *result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* when one of objects is empty (has got no faces) we could speed up
 | 
					
						
							|  |  |  | 		   calculation a bit returning one of objects' derived meshes (or empty one) | 
					
						
							|  |  |  | 		   Returning mesh is depended on modifieier's operation (sergey) */ | 
					
						
							|  |  |  | 		result = get_quick_derivedMesh(derivedData, dm, bmd->operation); | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 		if(result == NULL) { | 
					
						
							| 
									
										
										
										
											2012-01-05 12:40:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			DM_ensure_tessface(dm);          /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */ | 
					
						
							|  |  |  | 			DM_ensure_tessface(derivedData); /* BMESH - UNTIL MODIFIER IS UPDATED FOR MPoly */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-30 09:19:38 +00:00
										 |  |  | 			// TIMEIT_START(NewBooleanDerivedMesh)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 			result = NewBooleanDerivedMesh(dm, bmd->object, derivedData, ob, | 
					
						
							|  |  |  | 					1 + bmd->operation); | 
					
						
							| 
									
										
										
										
											2012-01-30 09:19:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// TIMEIT_END(NewBooleanDerivedMesh)
 | 
					
						
							| 
									
										
										
										
											2011-05-29 15:53:38 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* if new mesh returned, return it; otherwise there was
 | 
					
						
							|  |  |  | 		* an error, so delete the modifier object */ | 
					
						
							|  |  |  | 		if(result) | 
					
						
							|  |  |  | 			return result; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			modifier_setError(md, "Can't execute boolean operation."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return derivedData; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-10-23 15:40:13 +00:00
										 |  |  | #else // WITH_MOD_BOOLEAN
 | 
					
						
							|  |  |  | static DerivedMesh *applyModifier(ModifierData *UNUSED(md), Object *UNUSED(ob), | 
					
						
							|  |  |  | 						DerivedMesh *derivedData, | 
					
						
							|  |  |  | 						int UNUSED(useRenderParams), | 
					
						
							|  |  |  | 						int UNUSED(isFinalCalc)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return derivedData; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif // WITH_MOD_BOOLEAN
 | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 06:29:17 +00:00
										 |  |  | static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *UNUSED(md)) | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-10-21 01:55:39 +00:00
										 |  |  | 	CustomDataMask dataMask = CD_MASK_MTFACE | CD_MASK_MEDGE; | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-21 01:55:39 +00:00
										 |  |  | 	dataMask |= CD_MASK_MDEFORMVERT; | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	return dataMask; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ModifierTypeInfo modifierType_Boolean = { | 
					
						
							|  |  |  | 	/* name */              "Boolean", | 
					
						
							|  |  |  | 	/* structName */        "BooleanModifierData", | 
					
						
							|  |  |  | 	/* structSize */        sizeof(BooleanModifierData), | 
					
						
							|  |  |  | 	/* type */              eModifierTypeType_Nonconstructive, | 
					
						
							|  |  |  | 	/* flags */             eModifierTypeFlag_AcceptsMesh | 
					
						
							|  |  |  | 							| eModifierTypeFlag_UsesPointCache, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* copyData */          copyData, | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | 	/* deformVerts */       NULL, | 
					
						
							|  |  |  | 	/* deformMatrices */    NULL, | 
					
						
							|  |  |  | 	/* deformVertsEM */     NULL, | 
					
						
							|  |  |  | 	/* deformMatricesEM */  NULL, | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	/* applyModifier */     applyModifier, | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | 	/* applyModifierEM */   NULL, | 
					
						
							|  |  |  | 	/* initData */          NULL, | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	/* requiredDataMask */  requiredDataMask, | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | 	/* freeData */          NULL, | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	/* isDisabled */        isDisabled, | 
					
						
							|  |  |  | 	/* updateDepgraph */    updateDepgraph, | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | 	/* dependsOnTime */     NULL, | 
					
						
							|  |  |  | 	/* dependsOnNormals */	NULL, | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | 	/* foreachObjectLink */ foreachObjectLink, | 
					
						
							| 
									
										
										
										
											2011-03-05 10:29:10 +00:00
										 |  |  | 	/* foreachIDLink */     NULL, | 
					
						
							| 
									
										
										
										
											2011-08-12 18:11:22 +00:00
										 |  |  | 	/* foreachTexLink */    NULL, | 
					
						
							| 
									
										
										
										
											2010-04-11 22:12:30 +00:00
										 |  |  | }; |