style cleanup, brackets in else/if, some indentation.
[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 int dependsOnTime(ModifierData *UNUSED(md))
84 {
85         return 1;
86 }
87
88 static void deformVerts(ModifierData *md, Object *ob,
89                                                 DerivedMesh *derivedData,
90                                                 float (*vertexCos)[3],
91                                                 int UNUSED(numVerts),
92                                                 int UNUSED(useRenderParams),
93                                                 int UNUSED(isFinalCalc))
94 {
95         SurfaceModifierData *surmd = (SurfaceModifierData*) md;
96         
97         if(surmd->dm)
98                 surmd->dm->release(surmd->dm);
99
100         /* if possible use/create DerivedMesh */
101         if(derivedData) surmd->dm = CDDM_copy(derivedData);
102         else surmd->dm = get_dm(ob, NULL, NULL, NULL, 0);
103         
104         if (!ob->pd) {
105                 printf("SurfaceModifier deformVerts: Should not happen!\n");
106                 return;
107         }
108
109         if(surmd->dm) {
110                 unsigned int numverts = 0, i = 0;
111                 int init = 0;
112                 float *vec;
113                 MVert *x, *v;
114
115                 CDDM_apply_vert_coords(surmd->dm, vertexCos);
116                 CDDM_calc_normals(surmd->dm);
117                 
118                 numverts = surmd->dm->getNumVerts ( surmd->dm );
119
120                 if(numverts != surmd->numverts || surmd->x == NULL || surmd->v == NULL || md->scene->r.cfra != surmd->cfra+1) {
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_callocN(numverts * sizeof(MVert), "MVert");
131                         surmd->v = MEM_callocN(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(ob->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->getNumTessFaces(surmd->dm))
159                         bvhtree_from_mesh_faces(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
160                 else
161                         bvhtree_from_mesh_edges(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
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_NoUserAdd,
173
174         /* copyData */          NULL,
175         /* deformVerts */       deformVerts,
176         /* deformMatrices */    NULL,
177         /* deformVertsEM */     NULL,
178         /* deformMatricesEM */  NULL,
179         /* applyModifier */     NULL,
180         /* applyModifierEM */   NULL,
181         /* initData */          initData,
182         /* requiredDataMask */  NULL,
183         /* freeData */          freeData,
184         /* isDisabled */        NULL,
185         /* updateDepgraph */    NULL,
186         /* dependsOnTime */     dependsOnTime,
187         /* dependsOnNormals */  NULL,
188         /* foreachObjectLink */ NULL,
189         /* foreachIDLink */     NULL,
190         /* foreachTexLink */    NULL,
191 };