svn merge ^/trunk/blender -r41100:41150
[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 /** \file blender/modifiers/intern/MOD_build.c
34  *  \ingroup modifiers
35  */
36
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_utildefines.h"
41 #include "BLI_rand.h"
42 #include "BLI_ghash.h"
43
44 #include "DNA_scene_types.h"
45 #include "DNA_meshdata_types.h"
46
47 #include "BKE_cdderivedmesh.h"
48 #include "BKE_mesh.h"
49 #include "BKE_modifier.h"
50 #include "BKE_object.h"
51 #include "BKE_particle.h"
52 #include "BKE_scene.h"
53
54 #include "MOD_util.h"
55
56 static void initData(ModifierData *md)
57 {
58         BuildModifierData *bmd = (BuildModifierData*) md;
59
60         bmd->start = 1.0;
61         bmd->length = 100.0;
62 }
63
64 static void copyData(ModifierData *md, ModifierData *target)
65 {
66         BuildModifierData *bmd = (BuildModifierData*) md;
67         BuildModifierData *tbmd = (BuildModifierData*) target;
68
69         tbmd->start = bmd->start;
70         tbmd->length = bmd->length;
71         tbmd->randomize = bmd->randomize;
72         tbmd->seed = bmd->seed;
73 }
74
75 static int dependsOnTime(ModifierData *UNUSED(md))
76 {
77         return 1;
78 }
79
80 static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
81                                                 DerivedMesh *derivedData,
82                                                 int UNUSED(useRenderParams),
83                                                 int UNUSED(isFinalCalc))
84 {
85         DerivedMesh *dm = derivedData;
86         DerivedMesh *result;
87         BuildModifierData *bmd = (BuildModifierData*) md;
88         int i, j, k;
89         int numFaces, numEdges, numLoops = 0;
90         int *vertMap, *edgeMap, *faceMap;
91         float frac;
92         MPoly *mpolys, *mpolyd;
93         MLoop *mld, *mloops, *mls, *mloopd;
94         GHashIterator *hashIter;
95         /* maps vert indices in old mesh to indices in new mesh */
96         GHash *vertHash = BLI_ghash_new(BLI_ghashutil_inthash,
97                                         BLI_ghashutil_intcmp, "build ve apply gh");
98         /* maps edge indices in new mesh to indices in old mesh */
99         GHash *edgeHash = BLI_ghash_new(BLI_ghashutil_inthash,
100                                         BLI_ghashutil_intcmp, "build ed apply gh");
101         GHash *edgeHash2 = BLI_ghash_new(BLI_ghashutil_inthash,
102                                         BLI_ghashutil_intcmp, "build ed apply gh");
103
104         const int maxVerts= dm->getNumVerts(dm);
105         const int maxEdges= dm->getNumEdges(dm);
106         const int maxFaces= dm->getNumFaces(dm);
107         
108         if (!CDDM_Check(dm)) {
109                 result = CDDM_copy(dm, 0);
110                 dm = result;
111         }
112         
113         vertMap = MEM_callocN(sizeof(*vertMap) * maxVerts, "build modifier vertMap");
114         for(i = 0; i < maxVerts; ++i) vertMap[i] = i;
115         edgeMap = MEM_callocN(sizeof(*edgeMap) * maxEdges, "build modifier edgeMap");
116         for(i = 0; i < maxEdges; ++i) edgeMap[i] = i;
117         faceMap = MEM_callocN(sizeof(*faceMap) * maxFaces, "build modifier faceMap");
118         for(i = 0; i < maxFaces; ++i) faceMap[i] = i;
119
120         if (ob) {
121                 frac = bsystem_time(md->scene, ob, md->scene->r.cfra,
122                                         bmd->start - 1.0f) / bmd->length;
123         } else {
124                 frac = BKE_curframe(md->scene) - bmd->start / bmd->length;
125         }
126         CLAMP(frac, 0.0f, 1.0f);
127
128         numFaces = dm->getNumFaces(dm) * frac;
129         numEdges = dm->getNumEdges(dm) * frac;
130
131         /* if there's at least one face, build based on faces */
132         if(numFaces) {
133                 MPoly *mpoly, *mp;
134                 MLoop *ml, *mloop;
135                 MEdge *medge;
136                 
137                 if(bmd->randomize)
138                         BLI_array_randomize(faceMap, sizeof(*faceMap),
139                                                 maxFaces, bmd->seed);
140
141                 /* get the set of all vert indices that will be in the final mesh,
142                 * mapped to the new indices
143                 */
144                 mpoly = CDDM_get_polys(dm);
145                 mloop = CDDM_get_loops(dm);
146                 for(i = 0; i < numFaces; ++i) {
147                         mp = mpoly + faceMap[i];
148                         ml = mloop + mp->loopstart;
149
150                         for (j=0; j<mp->totloop; j++, ml++) {
151                                 if (!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(ml->v)))
152                                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(ml->v), 
153                                                 SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
154                         }
155                         
156                         numLoops += mp->totloop;
157                 }
158
159                 /* get the set of edges that will be in the new mesh (i.e. all edges
160                 * that have both verts in the new mesh)
161                 */
162                 medge = CDDM_get_edges(dm);
163                 for(i = 0; i < maxEdges; ++i) {
164                         MEdge *me = medge + i;
165
166                         if(BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1))
167                                                 && BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2)))
168                         {
169                                 j = BLI_ghash_size(edgeHash);
170                                 
171                                 BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j), SET_INT_IN_POINTER(i));
172                                 BLI_ghash_insert(edgeHash2, SET_INT_IN_POINTER(i), SET_INT_IN_POINTER(j));
173                         }
174                 }
175         } else if(numEdges) {
176                 MEdge *medge, *me;
177
178                 if(bmd->randomize)
179                         BLI_array_randomize(edgeMap, sizeof(*edgeMap),
180                                                 maxEdges, bmd->seed);
181
182                 /* get the set of all vert indices that will be in the final mesh,
183                 * mapped to the new indices
184                 */
185                 medge = CDDM_get_edges(dm);
186                 for(i = 0; i < numEdges; ++i) {
187                         me = medge + edgeMap[i];
188
189                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1)))
190                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me->v1),
191                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
192                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2)))
193                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me->v2),
194                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
195                 }
196
197                 /* get the set of edges that will be in the new mesh
198                 */
199                 for(i = 0; i < numEdges; ++i) {
200                         j = BLI_ghash_size(edgeHash);
201                         
202                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j),
203                                          SET_INT_IN_POINTER(edgeMap[i]));
204                         BLI_ghash_insert(edgeHash2,  SET_INT_IN_POINTER(edgeMap[i]),
205                                                          SET_INT_IN_POINTER(j));
206                 }
207         } else {
208                 int numVerts = dm->getNumVerts(dm) * frac;
209
210                 if(bmd->randomize)
211                         BLI_array_randomize(vertMap, sizeof(*vertMap),
212                                                 maxVerts, bmd->seed);
213
214                 /* get the set of all vert indices that will be in the final mesh,
215                 * mapped to the new indices
216                 */
217                 for(i = 0; i < numVerts; ++i)
218                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(vertMap[i]), SET_INT_IN_POINTER(i));
219         }
220
221         /* now we know the number of verts, edges and faces, we can create
222         * the mesh
223         */
224         result = CDDM_from_template(dm, BLI_ghash_size(vertHash),
225                                         BLI_ghash_size(edgeHash), 0, numLoops, numFaces);
226
227         /* copy the vertices across */
228         for(    hashIter = BLI_ghashIterator_new(vertHash);
229                         !BLI_ghashIterator_isDone(hashIter);
230                         BLI_ghashIterator_step(hashIter)
231         ) {
232                 MVert source;
233                 MVert *dest;
234                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
235                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
236
237                 dm->getVert(dm, oldIndex, &source);
238                 dest = CDDM_get_vert(result, newIndex);
239
240                 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
241                 *dest = source;
242         }
243         BLI_ghashIterator_free(hashIter);
244         
245         /* copy the edges across, remapping indices */
246         for(i = 0; i < BLI_ghash_size(edgeHash); ++i) {
247                 MEdge source;
248                 MEdge *dest;
249                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i)));
250                 
251                 dm->getEdge(dm, oldIndex, &source);
252                 dest = CDDM_get_edge(result, i);
253                 
254                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
255                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
256                 
257                 DM_copy_edge_data(dm, result, oldIndex, i, 1);
258                 *dest = source;
259         }
260
261         mpolys = CDDM_get_polys(dm);
262         mpolyd = CDDM_get_polys(result);
263         mloops = CDDM_get_loops(dm);
264         mloopd = mld = CDDM_get_loops(result);
265         
266         /* copy the faces across, remapping indices */
267         k = 0;
268         for(i = 0; i < numFaces; ++i) {
269                 MPoly *source;
270                 MPoly *dest;
271                 
272                 source = mpolys + faceMap[i];
273                 dest = mpolyd + i;
274                 DM_copy_face_data(dm, result, faceMap[i], i, 1);
275                 
276                 *dest = *source;
277                 dest->loopstart = k;
278                 
279                 DM_copy_loop_data(dm, result, source->loopstart, dest->loopstart, dest->totloop);
280
281                 mls = mloops + source->loopstart;
282                 for (j=0; j<source->totloop; j++, k++, mls++, mld++) {
283                         mld->v = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(mls->v)));
284                         mld->e = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash2, SET_INT_IN_POINTER(mls->e)));
285                 }
286         }
287
288         CDDM_calc_normals(result);
289         
290         BLI_ghash_free(vertHash, NULL, NULL);
291         BLI_ghash_free(edgeHash, NULL, NULL);
292         BLI_ghash_free(edgeHash2, NULL, NULL);
293         
294         MEM_freeN(vertMap);
295         MEM_freeN(edgeMap);
296         MEM_freeN(faceMap);
297         
298         if (dm != derivedData) {
299                 dm->needsFree = 1;
300                 dm->release(dm);
301         }
302         
303         return result;
304 }
305
306
307 ModifierTypeInfo modifierType_Build = {
308         /* name */              "Build",
309         /* structName */        "BuildModifierData",
310         /* structSize */        sizeof(BuildModifierData),
311         /* type */              eModifierTypeType_Nonconstructive,
312         /* flags */             eModifierTypeFlag_AcceptsMesh
313                                                         | eModifierTypeFlag_AcceptsCVs,
314         /* copyData */          copyData,
315         /* deformVerts */       NULL,
316         /* deformMatrices */    NULL,
317         /* deformVertsEM */     NULL,
318         /* deformMatricesEM */  NULL,
319         /* applyModifier */     applyModifier,
320         /* applyModifierEM */   NULL,
321         /* initData */          initData,
322         /* requiredDataMask */  NULL,
323         /* freeData */          NULL,
324         /* isDisabled */        NULL,
325         /* updateDepgraph */    NULL,
326         /* dependsOnTime */     dependsOnTime,
327         /* dependsOnNormals */  NULL,
328         /* foreachObjectLink */ NULL,
329         /* foreachIDLink */     NULL,
330         /* foreachTexLink */    NULL,
331 };