8d6fafd5ab63bbf345a0b346e91af64473f664ac
[blender.git] / source / blender / modifiers / intern / MOD_surface.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_surface.c
32  *  \ingroup modifiers
33  */
34
35
36 #include "DNA_scene_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_meshdata_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_utildefines.h"
42
43
44 #include "BKE_cdderivedmesh.h"
45
46 #include "MOD_modifiertypes.h"
47 #include "MOD_util.h"
48
49 #include "MEM_guardedalloc.h"
50
51
52 static void initData(ModifierData *md) 
53 {
54         SurfaceModifierData *surmd = (SurfaceModifierData *) md;
55         
56         surmd->bvhtree = NULL;
57 }
58
59 static void freeData(ModifierData *md)
60 {
61         SurfaceModifierData *surmd = (SurfaceModifierData *) md;
62
63         if (surmd) {
64                 if (surmd->bvhtree) {
65                         free_bvhtree_from_mesh(surmd->bvhtree);
66                         MEM_freeN(surmd->bvhtree);
67                 }
68
69                 if (surmd->dm)
70                         surmd->dm->release(surmd->dm);
71
72                 if (surmd->x)
73                         MEM_freeN(surmd->x);
74                 
75                 if (surmd->v)
76                         MEM_freeN(surmd->v);
77
78                 surmd->bvhtree = NULL;
79                 surmd->dm = NULL;
80         }
81 }
82
83 static bool dependsOnTime(ModifierData *UNUSED(md))
84 {
85         return true;
86 }
87
88 static void deformVerts(ModifierData *md, struct Depsgraph *UNUSED(depsgraph),
89                         Object *ob, DerivedMesh *derivedData,
90                         float (*vertexCos)[3],
91                         int UNUSED(numVerts),
92                         ModifierApplyFlag UNUSED(flag))
93 {
94         SurfaceModifierData *surmd = (SurfaceModifierData *) md;
95         
96         if (surmd->dm)
97                 surmd->dm->release(surmd->dm);
98
99         /* if possible use/create DerivedMesh */
100         if (derivedData) surmd->dm = CDDM_copy(derivedData);
101         else surmd->dm = get_dm(ob, NULL, NULL, NULL, false, false);
102         
103         if (!ob->pd) {
104                 printf("SurfaceModifier deformVerts: Should not happen!\n");
105                 return;
106         }
107
108         if (surmd->dm) {
109                 unsigned int numverts = 0, i = 0;
110                 int init = 0;
111                 float *vec;
112                 MVert *x, *v;
113
114                 CDDM_apply_vert_coords(surmd->dm, vertexCos);
115                 CDDM_calc_normals(surmd->dm);
116                 
117                 numverts = surmd->dm->getNumVerts(surmd->dm);
118
119                 if (numverts != surmd->numverts ||
120                     surmd->x == NULL ||
121                     surmd->v == NULL ||
122                     md->scene->r.cfra != surmd->cfra + 1)
123                 {
124                         if (surmd->x) {
125                                 MEM_freeN(surmd->x);
126                                 surmd->x = NULL;
127                         }
128                         if (surmd->v) {
129                                 MEM_freeN(surmd->v);
130                                 surmd->v = NULL;
131                         }
132
133                         surmd->x = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
134                         surmd->v = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
135
136                         surmd->numverts = numverts;
137
138                         init = 1;
139                 }
140
141                 /* convert to global coordinates and calculate velocity */
142                 for (i = 0, x = surmd->x, v = surmd->v; i < numverts; i++, x++, v++) {
143                         vec = CDDM_get_vert(surmd->dm, i)->co;
144                         mul_m4_v3(ob->obmat, vec);
145
146                         if (init)
147                                 v->co[0] = v->co[1] = v->co[2] = 0.0f;
148                         else
149                                 sub_v3_v3v3(v->co, vec, x->co);
150                         
151                         copy_v3_v3(x->co, vec);
152                 }
153
154                 surmd->cfra = md->scene->r.cfra;
155
156                 if (surmd->bvhtree)
157                         free_bvhtree_from_mesh(surmd->bvhtree);
158                 else
159                         surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
160
161                 if (surmd->dm->getNumPolys(surmd->dm))
162                         bvhtree_from_mesh_looptri(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
163                 else
164                         bvhtree_from_mesh_edges(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
165         }
166 }
167
168
169 ModifierTypeInfo modifierType_Surface = {
170         /* name */              "Surface",
171         /* structName */        "SurfaceModifierData",
172         /* structSize */        sizeof(SurfaceModifierData),
173         /* type */              eModifierTypeType_OnlyDeform,
174         /* flags */             eModifierTypeFlag_AcceptsMesh |
175                                 eModifierTypeFlag_AcceptsCVs |
176                                 eModifierTypeFlag_NoUserAdd,
177
178         /* copyData */          NULL,
179
180         /* deformVerts_DM */    deformVerts,
181         /* deformMatrices_DM */ NULL,
182         /* deformVertsEM_DM */  NULL,
183         /* deformMatricesEM_DM*/NULL,
184         /* applyModifier_DM */  NULL,
185         /* applyModifierEM_DM */NULL,
186
187         /* deformVerts */       NULL,
188         /* deformMatrices */    NULL,
189         /* deformVertsEM */     NULL,
190         /* deformMatricesEM */  NULL,
191         /* applyModifier */     NULL,
192         /* applyModifierEM */   NULL,
193
194         /* initData */          initData,
195         /* requiredDataMask */  NULL,
196         /* freeData */          freeData,
197         /* isDisabled */        NULL,
198         /* updateDepsgraph */   NULL,
199         /* dependsOnTime */     dependsOnTime,
200         /* dependsOnNormals */  NULL,
201         /* foreachObjectLink */ NULL,
202         /* foreachIDLink */     NULL,
203         /* foreachTexLink */    NULL,
204 };