Merge branch 'master' into blender2.8
[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_SAFE_FREE(surmd->bvhtree);
67                 }
68
69                 if (surmd->dm) {
70                         surmd->dm->release(surmd->dm);
71                         surmd->dm = NULL;
72                 }
73
74                 MEM_SAFE_FREE(surmd->x);
75                 
76                 MEM_SAFE_FREE(surmd->v);
77         }
78 }
79
80 static bool dependsOnTime(ModifierData *UNUSED(md))
81 {
82         return true;
83 }
84
85 static void deformVerts(
86         ModifierData *md, const ModifierEvalContext *ctx,
87         DerivedMesh *derivedData,
88         float (*vertexCos)[3],
89         int UNUSED(numVerts))
90 {
91         SurfaceModifierData *surmd = (SurfaceModifierData *) md;
92         
93         if (surmd->dm)
94                 surmd->dm->release(surmd->dm);
95
96         /* if possible use/create DerivedMesh */
97         if (derivedData) surmd->dm = CDDM_copy(derivedData);
98         else surmd->dm = get_dm(ctx->object, NULL, NULL, NULL, false, false);
99         
100         if (!ctx->object->pd) {
101                 printf("SurfaceModifier deformVerts: Should not happen!\n");
102                 return;
103         }
104
105         if (surmd->dm) {
106                 unsigned int numverts = 0, i = 0;
107                 int init = 0;
108                 float *vec;
109                 MVert *x, *v;
110
111                 CDDM_apply_vert_coords(surmd->dm, vertexCos);
112                 CDDM_calc_normals(surmd->dm);
113                 
114                 numverts = surmd->dm->getNumVerts(surmd->dm);
115
116                 if (numverts != surmd->numverts ||
117                     surmd->x == NULL ||
118                     surmd->v == NULL ||
119                     md->scene->r.cfra != surmd->cfra + 1)
120                 {
121                         if (surmd->x) {
122                                 MEM_freeN(surmd->x);
123                                 surmd->x = NULL;
124                         }
125                         if (surmd->v) {
126                                 MEM_freeN(surmd->v);
127                                 surmd->v = NULL;
128                         }
129
130                         surmd->x = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
131                         surmd->v = MEM_calloc_arrayN(numverts, sizeof(MVert), "MVert");
132
133                         surmd->numverts = numverts;
134
135                         init = 1;
136                 }
137
138                 /* convert to global coordinates and calculate velocity */
139                 for (i = 0, x = surmd->x, v = surmd->v; i < numverts; i++, x++, v++) {
140                         vec = CDDM_get_vert(surmd->dm, i)->co;
141                         mul_m4_v3(ctx->object->obmat, vec);
142
143                         if (init)
144                                 v->co[0] = v->co[1] = v->co[2] = 0.0f;
145                         else
146                                 sub_v3_v3v3(v->co, vec, x->co);
147                         
148                         copy_v3_v3(x->co, vec);
149                 }
150
151                 surmd->cfra = md->scene->r.cfra;
152
153                 if (surmd->bvhtree)
154                         free_bvhtree_from_mesh(surmd->bvhtree);
155                 else
156                         surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
157
158                 if (surmd->dm->getNumPolys(surmd->dm))
159                         bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_LOOPTRI, 2);
160                 else
161                         bvhtree_from_mesh_get(surmd->bvhtree, surmd->dm, BVHTREE_FROM_EDGES, 2);
162         }
163 }
164
165
166 ModifierTypeInfo modifierType_Surface = {
167         /* name */              "Surface",
168         /* structName */        "SurfaceModifierData",
169         /* structSize */        sizeof(SurfaceModifierData),
170         /* type */              eModifierTypeType_OnlyDeform,
171         /* flags */             eModifierTypeFlag_AcceptsMesh |
172                                 eModifierTypeFlag_AcceptsCVs |
173                                 eModifierTypeFlag_NoUserAdd,
174
175         /* copyData */          NULL,
176
177         /* deformVerts_DM */    deformVerts,
178         /* deformMatrices_DM */ NULL,
179         /* deformVertsEM_DM */  NULL,
180         /* deformMatricesEM_DM*/NULL,
181         /* applyModifier_DM */  NULL,
182         /* applyModifierEM_DM */NULL,
183
184         /* deformVerts */       NULL,
185         /* deformMatrices */    NULL,
186         /* deformVertsEM */     NULL,
187         /* deformMatricesEM */  NULL,
188         /* applyModifier */     NULL,
189         /* applyModifierEM */   NULL,
190
191         /* initData */          initData,
192         /* requiredDataMask */  NULL,
193         /* freeData */          freeData,
194         /* isDisabled */        NULL,
195         /* updateDepsgraph */   NULL,
196         /* dependsOnTime */     dependsOnTime,
197         /* dependsOnNormals */  NULL,
198         /* foreachObjectLink */ NULL,
199         /* foreachIDLink */     NULL,
200         /* foreachTexLink */    NULL,
201 };