6c047a81fa5dd54ba1bbd2ec529a1788c4d468f9
[blender.git] / source / blender / modifiers / intern / MOD_shrinkwrap.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_shrinkwrap.c
32  *  \ingroup modifiers
33  */
34
35
36 #include <string.h>
37
38 #include "BLI_string.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_modifier.h"
43 #include "BKE_shrinkwrap.h"
44
45 #include "DNA_object_types.h"
46
47 #include "depsgraph_private.h"
48
49 #include "MOD_util.h"
50
51
52 static void initData(ModifierData *md)
53 {
54         ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
55         smd->shrinkType = MOD_SHRINKWRAP_NEAREST_SURFACE;
56         smd->shrinkOpts = MOD_SHRINKWRAP_PROJECT_ALLOW_POS_DIR;
57         smd->keepDist   = 0.0f;
58
59         smd->target             = NULL;
60         smd->auxTarget  = NULL;
61 }
62
63 static void copyData(ModifierData *md, ModifierData *target)
64 {
65         ShrinkwrapModifierData *smd  = (ShrinkwrapModifierData*)md;
66         ShrinkwrapModifierData *tsmd = (ShrinkwrapModifierData*)target;
67
68         tsmd->target    = smd->target;
69         tsmd->auxTarget = smd->auxTarget;
70
71         BLI_strncpy(tsmd->vgroup_name, smd->vgroup_name, sizeof(tsmd->vgroup_name));
72
73         tsmd->keepDist  = smd->keepDist;
74         tsmd->shrinkType= smd->shrinkType;
75         tsmd->shrinkOpts= smd->shrinkOpts;
76         tsmd->projAxis = smd->projAxis;
77         tsmd->subsurfLevels = smd->subsurfLevels;
78 }
79
80 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
81 {
82         ShrinkwrapModifierData *smd = (ShrinkwrapModifierData *)md;
83         CustomDataMask dataMask = 0;
84
85         /* ask for vertexgroups if we need them */
86         if(smd->vgroup_name[0])
87                 dataMask |= CD_MASK_MDEFORMVERT;
88
89         if(smd->shrinkType == MOD_SHRINKWRAP_PROJECT
90         && smd->projAxis == MOD_SHRINKWRAP_PROJECT_OVER_NORMAL)
91                 dataMask |= CD_MASK_MVERT;
92                 
93         return dataMask;
94 }
95
96 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
97 {
98         ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
99         return !smd->target;
100 }
101
102
103 static void foreachObjectLink(ModifierData *md, Object *ob, ObjectWalkFunc walk, void *userData)
104 {
105         ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
106
107         walk(userData, ob, &smd->target);
108         walk(userData, ob, &smd->auxTarget);
109 }
110
111 static void deformVerts(ModifierData *md, Object *ob,
112                                                 DerivedMesh *derivedData,
113                                                 float (*vertexCos)[3],
114                                                 int numVerts,
115                                                 int UNUSED(useRenderParams),
116                                                 int UNUSED(isFinalCalc))
117 {
118         DerivedMesh *dm = derivedData;
119         CustomDataMask dataMask = requiredDataMask(ob, md);
120
121         /* ensure we get a CDDM with applied vertex coords */
122         if(dataMask)
123                 dm= get_cddm(ob, NULL, dm, vertexCos);
124
125         shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts);
126
127         if(dm != derivedData)
128                 dm->release(dm);
129 }
130
131 static void deformVertsEM(ModifierData *md, Object *ob, struct EditMesh *editData, DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
132 {
133         DerivedMesh *dm = derivedData;
134         CustomDataMask dataMask = requiredDataMask(ob, md);
135
136         /* ensure we get a CDDM with applied vertex coords */
137         if(dataMask)
138                 dm= get_cddm(ob, editData, dm, vertexCos);
139
140         shrinkwrapModifier_deform((ShrinkwrapModifierData*)md, ob, dm, vertexCos, numVerts);
141
142         if(dm != derivedData)
143                 dm->release(dm);
144 }
145
146 static void updateDepgraph(ModifierData *md, DagForest *forest,
147                                                 struct Scene *UNUSED(scene),
148                                                 Object *UNUSED(ob),
149                                                 DagNode *obNode)
150 {
151         ShrinkwrapModifierData *smd = (ShrinkwrapModifierData*) md;
152
153         if (smd->target)
154                 dag_add_relation(forest, dag_get_node(forest, smd->target),   obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier");
155
156         if (smd->auxTarget)
157                 dag_add_relation(forest, dag_get_node(forest, smd->auxTarget), obNode, DAG_RL_OB_DATA | DAG_RL_DATA_DATA, "Shrinkwrap Modifier");
158 }
159
160
161 ModifierTypeInfo modifierType_Shrinkwrap = {
162         /* name */              "Shrinkwrap",
163         /* structName */        "ShrinkwrapModifierData",
164         /* structSize */        sizeof(ShrinkwrapModifierData),
165         /* type */              eModifierTypeType_OnlyDeform,
166         /* flags */             eModifierTypeFlag_AcceptsMesh
167                                                         | eModifierTypeFlag_AcceptsCVs
168                                                         | eModifierTypeFlag_SupportsEditmode
169                                                         | eModifierTypeFlag_EnableInEditmode,
170
171         /* copyData */          copyData,
172         /* deformVerts */       deformVerts,
173         /* deformMatrices */    NULL,
174         /* deformVertsEM */     deformVertsEM,
175         /* deformMatricesEM */  NULL,
176         /* applyModifier */     NULL,
177         /* applyModifierEM */   NULL,
178         /* initData */          initData,
179         /* requiredDataMask */  requiredDataMask,
180         /* freeData */          NULL,
181         /* isDisabled */        isDisabled,
182         /* updateDepgraph */    updateDepgraph,
183         /* dependsOnTime */     NULL,
184         /* dependsOnNormals */  NULL,
185         /* foreachObjectLink */ foreachObjectLink,
186         /* foreachIDLink */     NULL,
187         /* foreachTexLink */    NULL,
188 };