misc warnings/fixes
[blender.git] / source / blender / modifiers / intern / MOD_build.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_utildefines.h"
36 #include "BLI_rand.h"
37 #include "BLI_ghash.h"
38
39 #include "DNA_scene_types.h"
40 #include "DNA_meshdata_types.h"
41
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_mesh.h"
44 #include "BKE_modifier.h"
45 #include "BKE_object.h"
46 #include "BKE_particle.h"
47 #include "BKE_scene.h"
48
49 #include "MOD_util.h"
50
51 static void initData(ModifierData *md)
52 {
53         BuildModifierData *bmd = (BuildModifierData*) md;
54
55         bmd->start = 1.0;
56         bmd->length = 100.0;
57 }
58
59 static void copyData(ModifierData *md, ModifierData *target)
60 {
61         BuildModifierData *bmd = (BuildModifierData*) md;
62         BuildModifierData *tbmd = (BuildModifierData*) target;
63
64         tbmd->start = bmd->start;
65         tbmd->length = bmd->length;
66         tbmd->randomize = bmd->randomize;
67         tbmd->seed = bmd->seed;
68 }
69
70 static int dependsOnTime(ModifierData *UNUSED(md))
71 {
72         return 1;
73 }
74
75 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
76                                                 DerivedMesh *derivedData,
77                                                 int UNUSED(useRenderParams),
78                                                 int UNUSED(isFinalCalc))
79 {
80         DerivedMesh *dm = derivedData;
81         DerivedMesh *result;
82         BuildModifierData *bmd = (BuildModifierData*) md;
83         int i;
84         int numFaces, numEdges;
85         int *vertMap, *edgeMap, *faceMap;
86         float frac;
87         GHashIterator *hashIter;
88         /* maps vert indices in old mesh to indices in new mesh */
89         GHash *vertHash = BLI_ghash_new(BLI_ghashutil_inthash,
90                                         BLI_ghashutil_intcmp, "build ve apply gh");
91         /* maps edge indices in new mesh to indices in old mesh */
92         GHash *edgeHash = BLI_ghash_new(BLI_ghashutil_inthash,
93                                         BLI_ghashutil_intcmp, "build ed apply gh");
94
95         const int maxVerts= dm->getNumVerts(dm);
96         const int maxEdges= dm->getNumEdges(dm);
97         const int maxFaces= dm->getNumFaces(dm);
98
99         vertMap = MEM_callocN(sizeof(*vertMap) * maxVerts, "build modifier vertMap");
100         for(i = 0; i < maxVerts; ++i) vertMap[i] = i;
101         edgeMap = MEM_callocN(sizeof(*edgeMap) * maxEdges, "build modifier edgeMap");
102         for(i = 0; i < maxEdges; ++i) edgeMap[i] = i;
103         faceMap = MEM_callocN(sizeof(*faceMap) * maxFaces, "build modifier faceMap");
104         for(i = 0; i < maxFaces; ++i) faceMap[i] = i;
105
106         if (ob) {
107                 frac = bsystem_time(md->scene, ob, md->scene->r.cfra,
108                                         bmd->start - 1.0f) / bmd->length;
109         } else {
110                 frac = BKE_curframe(md->scene) - bmd->start / bmd->length;
111         }
112         CLAMP(frac, 0.0, 1.0);
113
114         numFaces = dm->getNumFaces(dm) * frac;
115         numEdges = dm->getNumEdges(dm) * frac;
116
117         /* if there's at least one face, build based on faces */
118         if(numFaces) {
119                 if(bmd->randomize)
120                         BLI_array_randomize(faceMap, sizeof(*faceMap),
121                                                 maxFaces, bmd->seed);
122
123                 /* get the set of all vert indices that will be in the final mesh,
124                 * mapped to the new indices
125                 */
126                 for(i = 0; i < numFaces; ++i) {
127                         MFace mf;
128                         dm->getFace(dm, faceMap[i], &mf);
129
130                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v1)))
131                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v1),
132                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
133                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v2)))
134                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v2),
135                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
136                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v3)))
137                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v3),
138                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
139                         if(mf.v4 && !BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v4)))
140                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v4),
141                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
142                 }
143
144                 /* get the set of edges that will be in the new mesh (i.e. all edges
145                 * that have both verts in the new mesh)
146                 */
147                 for(i = 0; i < maxEdges; ++i) {
148                         MEdge me;
149                         dm->getEdge(dm, i, &me);
150
151                         if(BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1))
152                                                 && BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)))
153                                 BLI_ghash_insert(edgeHash,
154                                         SET_INT_IN_POINTER(BLI_ghash_size(edgeHash)), SET_INT_IN_POINTER(i));
155                 }
156         } else if(numEdges) {
157                 if(bmd->randomize)
158                         BLI_array_randomize(edgeMap, sizeof(*edgeMap),
159                                                 maxEdges, bmd->seed);
160
161                 /* get the set of all vert indices that will be in the final mesh,
162                 * mapped to the new indices
163                 */
164                 for(i = 0; i < numEdges; ++i) {
165                         MEdge me;
166                         dm->getEdge(dm, edgeMap[i], &me);
167
168                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1)))
169                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me.v1),
170                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
171                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)))
172                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me.v2),
173                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
174                 }
175
176                 /* get the set of edges that will be in the new mesh
177                 */
178                 for(i = 0; i < numEdges; ++i) {
179                         MEdge me;
180                         dm->getEdge(dm, edgeMap[i], &me);
181
182                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(BLI_ghash_size(edgeHash)),
183                                          SET_INT_IN_POINTER(edgeMap[i]));
184                 }
185         } else {
186                 int numVerts = dm->getNumVerts(dm) * frac;
187
188                 if(bmd->randomize)
189                         BLI_array_randomize(vertMap, sizeof(*vertMap),
190                                                 maxVerts, bmd->seed);
191
192                 /* get the set of all vert indices that will be in the final mesh,
193                 * mapped to the new indices
194                 */
195                 for(i = 0; i < numVerts; ++i)
196                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(vertMap[i]), SET_INT_IN_POINTER(i));
197         }
198
199         /* now we know the number of verts, edges and faces, we can create
200         * the mesh
201         */
202         result = CDDM_from_template(dm, BLI_ghash_size(vertHash),
203                                         BLI_ghash_size(edgeHash), numFaces);
204
205         /* copy the vertices across */
206         for(    hashIter = BLI_ghashIterator_new(vertHash);
207                         !BLI_ghashIterator_isDone(hashIter);
208                         BLI_ghashIterator_step(hashIter)
209         ) {
210            MVert source;
211            MVert *dest;
212            int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
213            int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
214
215            dm->getVert(dm, oldIndex, &source);
216            dest = CDDM_get_vert(result, newIndex);
217
218            DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
219            *dest = source;
220    }
221         BLI_ghashIterator_free(hashIter);
222         
223         /* copy the edges across, remapping indices */
224         for(i = 0; i < BLI_ghash_size(edgeHash); ++i) {
225                 MEdge source;
226                 MEdge *dest;
227                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i)));
228                 
229                 dm->getEdge(dm, oldIndex, &source);
230                 dest = CDDM_get_edge(result, i);
231                 
232                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
233                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
234                 
235                 DM_copy_edge_data(dm, result, oldIndex, i, 1);
236                 *dest = source;
237         }
238
239         /* copy the faces across, remapping indices */
240         for(i = 0; i < numFaces; ++i) {
241                 MFace source;
242                 MFace *dest;
243                 int orig_v4;
244                 
245                 dm->getFace(dm, faceMap[i], &source);
246                 dest = CDDM_get_face(result, i);
247                 
248                 orig_v4 = source.v4;
249                 
250                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
251                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
252                 source.v3 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v3)));
253                 if(source.v4)
254                         source.v4 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v4)));
255                 
256                 DM_copy_face_data(dm, result, faceMap[i], i, 1);
257                 *dest = source;
258                 
259                 test_index_face(dest, &result->faceData, i, (orig_v4 ? 4 : 3));
260         }
261
262         CDDM_calc_normals(result);
263         
264         BLI_ghash_free(vertHash, NULL, NULL);
265         BLI_ghash_free(edgeHash, NULL, NULL);
266         
267         MEM_freeN(vertMap);
268         MEM_freeN(edgeMap);
269         MEM_freeN(faceMap);
270         
271         return result;
272 }
273
274
275 ModifierTypeInfo modifierType_Build = {
276         /* name */              "Build",
277         /* structName */        "BuildModifierData",
278         /* structSize */        sizeof(BuildModifierData),
279         /* type */              eModifierTypeType_Nonconstructive,
280         /* flags */             eModifierTypeFlag_AcceptsMesh
281                                                         | eModifierTypeFlag_AcceptsCVs,
282         /* copyData */          copyData,
283         /* deformVerts */       NULL,
284         /* deformMatrices */    NULL,
285         /* deformVertsEM */     NULL,
286         /* deformMatricesEM */  NULL,
287         /* applyModifier */     applyModifier,
288         /* applyModifierEM */   NULL,
289         /* initData */          initData,
290         /* requiredDataMask */  NULL,
291         /* freeData */          NULL,
292         /* isDisabled */        NULL,
293         /* updateDepgraph */    NULL,
294         /* dependsOnTime */     dependsOnTime,
295         /* dependsOnNormals */  NULL,
296         /* foreachObjectLink */ NULL,
297         /* foreachIDLink */     NULL
298 };