Blender V2.61 - r43446

MOD_shrinkwrap.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software  Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Daniel Dunbar
00022  *                 Ton Roosendaal,
00023  *                 Ben Batt,
00024  *                 Brecht Van Lommel,
00025  *                 Campbell Barton
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  *
00029  */
00030 
00036 #include <string.h>
00037 
00038 #include "DNA_object_types.h"
00039 
00040 #include "BLI_string.h"
00041 #include "BLI_utildefines.h"
00042 
00043 #include "BKE_cdderivedmesh.h"
00044 #include "BKE_modifier.h"
00045 #include "BKE_shrinkwrap.h"
00046 
00047 #include "depsgraph_private.h"
00048 
00049 #include "MOD_util.h"
00050 
00051 
00052 static void initData(ModifierData *md)
00053 {
00054     ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
00055     smd->shrinkType = MOD_SHRINKWRAP_NEAREST_SURFACE;
00056     smd->shrinkOpts = MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR;
00057     smd->keepDist   = 0.0f;
00058 
00059     smd->target     = NULL;
00060     smd->auxTarget  = NULL;
00061 }
00062 
00063 static void copyData(ModifierData *md, ModifierData *target)
00064 {
00065     ShrinkwrapModifierData *smd  = (ShrinkwrapModifierData*)md;
00066     ShrinkwrapModifierData *tsmd = (ShrinkwrapModifierData*)target;
00067 
00068     tsmd->target    = smd->target;
00069     tsmd->auxTarget = smd->auxTarget;
00070 
00071     BLI_strncpy(tsmd->vgroup_name, smd->vgroup_name, sizeof(tsmd->vgroup_name));
00072 
00073     tsmd->keepDist  = smd->keepDist;
00074     tsmd->shrinkType= smd->shrinkType;
00075     tsmd->shrinkOpts= smd->shrinkOpts;
00076     tsmd->projAxis = smd->projAxis;
00077     tsmd->subsurfLevels = smd->subsurfLevels;
00078 }
00079 
00080 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
00081 {
00082     ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
00083     CustomDataMask dataMask = 0;
00084 
00085     /* ask for vertexgroups if we need them */
00086     if(smd->vgroup_name[0])
00087         dataMask |= CD_MASK_MDEFORMVERT;
00088 
00089     if(smd->shrinkType == MOD_SHRINKWRAP_PROJECT
00090     && smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL)
00091         dataMask |= CD_MASK_MVERT;
00092         
00093     return dataMask;
00094 }
00095 
00096 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
00097 {
00098     ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
00099     return !smd->target;
00100 }
00101 
00102 
00103 static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
00104 {
00105     ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
00106 
00107     walk(userData, ob, &smd->target);
00108     walk(userData, ob, &smd->auxTarget);
00109 }
00110 
00111 static void deformVerts(ModifierData *md, Object *ob,
00112                         DerivedMesh *derivedData,
00113                         float (*vertexCos)[3],
00114                         int numVerts,
00115                         int UNUSED(useRenderParams),
00116                         int UNUSED(isFinalCalc))
00117 {
00118     DerivedMesh *dm = derivedData;
00119     CustomDataMask dataMask = requiredDataMask(ob, md);
00120 
00121     /* ensure we get a CDDM with applied vertex coords */
00122     if(dataMask)
00123         dm= get_cddm(ob, NULL, dm, vertexCos);
00124 
00125     shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts);
00126 
00127     if(dm != derivedData)
00128         dm->release(dm);
00129 }
00130 
00131 static void deformVertsEM(ModifierData *md, Object *ob, struct EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
00132 {
00133     DerivedMesh *dm = derivedData;
00134     CustomDataMask dataMask = requiredDataMask(ob, md);
00135 
00136     /* ensure we get a CDDM with applied vertex coords */
00137     if(dataMask)
00138         dm= get_cddm(ob, editData, dm, vertexCos);
00139 
00140     shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts);
00141 
00142     if(dm != derivedData)
00143         dm->release(dm);
00144 }
00145 
00146 static void updateDepgraph(ModifierData *md, DagForest *forest,
00147                         struct Scene *UNUSED(scene),
00148                         Object *UNUSED(ob),
00149                         DagNode *obNode)
00150 {
00151     ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
00152 
00153     if (smd->target)
00154         dag_add_relation(forest, dag_get_node(forest, smd->target),   obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier");
00155 
00156     if (smd->auxTarget)
00157         dag_add_relation(forest, dag_get_node(forest, smd->auxTarget), obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier");
00158 }
00159 
00160 
00161 ModifierTypeInfo modifierType_Shrinkwrap = {
00162     /* name */              "Shrinkwrap",
00163     /* structName */        "ShrinkwrapModifierData",
00164     /* structSize */        sizeof(ShrinkwrapModifierData),
00165     /* type */              eModifierTypeType_OnlyDeform,
00166     /* flags */             eModifierTypeFlag_AcceptsMesh
00167                             | eModifierTypeFlag_AcceptsCVs
00168                             | eModifierTypeFlag_SupportsEditmode
00169                             | eModifierTypeFlag_EnableInEditmode,
00170 
00171     /* copyData */          copyData,
00172     /* deformVerts */       deformVerts,
00173     /* deformMatrices */    NULL,
00174     /* deformVertsEM */     deformVertsEM,
00175     /* deformMatricesEM */  NULL,
00176     /* applyModifier */     NULL,
00177     /* applyModifierEM */   NULL,
00178     /* initData */          initData,
00179     /* requiredDataMask */  requiredDataMask,
00180     /* freeData */          NULL,
00181     /* isDisabled */        isDisabled,
00182     /* updateDepgraph */    updateDepgraph,
00183     /* dependsOnTime */     NULL,
00184     /* dependsOnNormals */  NULL,
00185     /* foreachObjectLink */ foreachObjectLink,
00186     /* foreachIDLink */     NULL,
00187     /* foreachTexLink */    NULL,
00188 };