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