Removed old "bsystem_time()" function, which by now is just a
[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 *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;
87         int numFaces, numEdges;
88         int *vertMap, *edgeMap, *faceMap;
89         float frac;
90         GHashIterator *hashIter;
91         /* maps vert indices in old mesh to indices in new mesh */
92         GHash *vertHash = BLI_ghash_new(BLI_ghashutil_inthash,
93                                         BLI_ghashutil_intcmp, "build ve apply gh");
94         /* maps edge indices in new mesh to indices in old mesh */
95         GHash *edgeHash = BLI_ghash_new(BLI_ghashutil_inthash,
96                                         BLI_ghashutil_intcmp, "build ed apply gh");
97
98         const int maxVerts= dm->getNumVerts(dm);
99         const int maxEdges= dm->getNumEdges(dm);
100         const int maxFaces= dm->getNumFaces(dm);
101
102         vertMap = MEM_callocN(sizeof(*vertMap) * maxVerts, "build modifier vertMap");
103         for(i = 0; i < maxVerts; ++i) vertMap[i] = i;
104         edgeMap = MEM_callocN(sizeof(*edgeMap) * maxEdges, "build modifier edgeMap");
105         for(i = 0; i < maxEdges; ++i) edgeMap[i] = i;
106         faceMap = MEM_callocN(sizeof(*faceMap) * maxFaces, "build modifier faceMap");
107         for(i = 0; i < maxFaces; ++i) faceMap[i] = i;
108
109         frac = BKE_curframe(md->scene) - bmd->start / bmd->length;
110         CLAMP(frac, 0.0f, 1.0f);
111
112         numFaces = dm->getNumFaces(dm) * frac;
113         numEdges = dm->getNumEdges(dm) * frac;
114
115         /* if there's at least one face, build based on faces */
116         if(numFaces) {
117                 if(bmd->randomize)
118                         BLI_array_randomize(faceMap, sizeof(*faceMap),
119                                                 maxFaces, bmd->seed);
120
121                 /* get the set of all vert indices that will be in the final mesh,
122                 * mapped to the new indices
123                 */
124                 for(i = 0; i < numFaces; ++i) {
125                         MFace mf;
126                         dm->getFace(dm, faceMap[i], &mf);
127
128                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v1)))
129                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v1),
130                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
131                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v2)))
132                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v2),
133                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
134                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v3)))
135                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v3),
136                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
137                         if(mf.v4 && !BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(mf.v4)))
138                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(mf.v4),
139                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
140                 }
141
142                 /* get the set of edges that will be in the new mesh (i.e. all edges
143                 * that have both verts in the new mesh)
144                 */
145                 for(i = 0; i < maxEdges; ++i) {
146                         MEdge me;
147                         dm->getEdge(dm, i, &me);
148
149                         if(BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1))
150                                                 && BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)))
151                                 BLI_ghash_insert(edgeHash,
152                                         SET_INT_IN_POINTER(BLI_ghash_size(edgeHash)), SET_INT_IN_POINTER(i));
153                 }
154         } else if(numEdges) {
155                 if(bmd->randomize)
156                         BLI_array_randomize(edgeMap, sizeof(*edgeMap),
157                                                 maxEdges, bmd->seed);
158
159                 /* get the set of all vert indices that will be in the final mesh,
160                 * mapped to the new indices
161                 */
162                 for(i = 0; i < numEdges; ++i) {
163                         MEdge me;
164                         dm->getEdge(dm, edgeMap[i], &me);
165
166                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v1)))
167                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me.v1),
168                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
169                         if(!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me.v2)))
170                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me.v2),
171                                         SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
172                 }
173
174                 /* get the set of edges that will be in the new mesh
175                 */
176                 for(i = 0; i < numEdges; ++i) {
177                         MEdge me;
178                         dm->getEdge(dm, edgeMap[i], &me);
179
180                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(BLI_ghash_size(edgeHash)),
181                                          SET_INT_IN_POINTER(edgeMap[i]));
182                 }
183         } else {
184                 int numVerts = dm->getNumVerts(dm) * frac;
185
186                 if(bmd->randomize)
187                         BLI_array_randomize(vertMap, sizeof(*vertMap),
188                                                 maxVerts, bmd->seed);
189
190                 /* get the set of all vert indices that will be in the final mesh,
191                 * mapped to the new indices
192                 */
193                 for(i = 0; i < numVerts; ++i)
194                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(vertMap[i]), SET_INT_IN_POINTER(i));
195         }
196
197         /* now we know the number of verts, edges and faces, we can create
198         * the mesh
199         */
200         result = CDDM_from_template(dm, BLI_ghash_size(vertHash),
201                                         BLI_ghash_size(edgeHash), numFaces);
202
203         /* copy the vertices across */
204         for(    hashIter = BLI_ghashIterator_new(vertHash);
205                         !BLI_ghashIterator_isDone(hashIter);
206                         BLI_ghashIterator_step(hashIter)
207         ) {
208                 MVert source;
209                 MVert *dest;
210                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
211                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
212
213                 dm->getVert(dm, oldIndex, &source);
214                 dest = CDDM_get_vert(result, newIndex);
215
216                 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
217                 *dest = source;
218         }
219         BLI_ghashIterator_free(hashIter);
220         
221         /* copy the edges across, remapping indices */
222         for(i = 0; i < BLI_ghash_size(edgeHash); ++i) {
223                 MEdge source;
224                 MEdge *dest;
225                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i)));
226                 
227                 dm->getEdge(dm, oldIndex, &source);
228                 dest = CDDM_get_edge(result, i);
229                 
230                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
231                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
232                 
233                 DM_copy_edge_data(dm, result, oldIndex, i, 1);
234                 *dest = source;
235         }
236
237         /* copy the faces across, remapping indices */
238         for(i = 0; i < numFaces; ++i) {
239                 MFace source;
240                 MFace *dest;
241                 int orig_v4;
242                 
243                 dm->getFace(dm, faceMap[i], &source);
244                 dest = CDDM_get_face(result, i);
245                 
246                 orig_v4 = source.v4;
247                 
248                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
249                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
250                 source.v3 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v3)));
251                 if(source.v4)
252                         source.v4 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v4)));
253                 
254                 DM_copy_face_data(dm, result, faceMap[i], i, 1);
255                 *dest = source;
256                 
257                 test_index_face(dest, &result->faceData, i, (orig_v4 ? 4 : 3));
258         }
259
260         CDDM_calc_normals(result);
261         
262         BLI_ghash_free(vertHash, NULL, NULL);
263         BLI_ghash_free(edgeHash, NULL, NULL);
264         
265         MEM_freeN(vertMap);
266         MEM_freeN(edgeMap);
267         MEM_freeN(faceMap);
268         
269         return result;
270 }
271
272
273 ModifierTypeInfo modifierType_Build = {
274         /* name */              "Build",
275         /* structName */        "BuildModifierData",
276         /* structSize */        sizeof(BuildModifierData),
277         /* type */              eModifierTypeType_Nonconstructive,
278         /* flags */             eModifierTypeFlag_AcceptsMesh
279                                                         | eModifierTypeFlag_AcceptsCVs,
280         /* copyData */          copyData,
281         /* deformVerts */       NULL,
282         /* deformMatrices */    NULL,
283         /* deformVertsEM */     NULL,
284         /* deformMatricesEM */  NULL,
285         /* applyModifier */     applyModifier,
286         /* applyModifierEM */   NULL,
287         /* initData */          initData,
288         /* requiredDataMask */  NULL,
289         /* freeData */          NULL,
290         /* isDisabled */        NULL,
291         /* updateDepgraph */    NULL,
292         /* dependsOnTime */     dependsOnTime,
293         /* dependsOnNormals */  NULL,
294         /* foreachObjectLink */ NULL,
295         /* foreachIDLink */     NULL,
296         /* foreachTexLink */    NULL,
297 };