Build Modifier - Add "Reversed" Option
[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_math_vector.h"
41 #include "BLI_ghash.h"
42
43 #include "DNA_scene_types.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_mesh.h"
48 #include "BKE_modifier.h"
49 #include "BKE_object.h"
50 #include "BKE_particle.h"
51 #include "BKE_scene.h"
52
53 #include "MOD_util.h"
54
55 static void initData(ModifierData *md)
56 {
57         BuildModifierData *bmd = (BuildModifierData *) md;
58
59         bmd->start = 1.0;
60         bmd->length = 100.0;
61 }
62
63 static void copyData(ModifierData *md, ModifierData *target)
64 {
65 #if 0
66         BuildModifierData *bmd = (BuildModifierData *) md;
67         BuildModifierData *tbmd = (BuildModifierData *) target;
68 #endif
69         modifier_copyData_generic(md, target);
70 }
71
72 static bool dependsOnTime(ModifierData *UNUSED(md))
73 {
74         return true;
75 }
76
77 static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
78                                   DerivedMesh *derivedData,
79                                   ModifierApplyFlag UNUSED(flag))
80 {
81         DerivedMesh *dm = derivedData;
82         DerivedMesh *result;
83         BuildModifierData *bmd = (BuildModifierData *) md;
84         int i, j, k;
85         int numFaces_dst, numEdges_dst, numLoops_dst = 0;
86         int *vertMap, *edgeMap, *faceMap;
87         float frac;
88         MPoly *mpoly_dst;
89         MLoop *ml_dst, *ml_src /*, *mloop_dst */;
90         GHashIterator *hashIter;
91         /* maps vert indices in old mesh to indices in new mesh */
92         GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
93         /* maps edge indices in new mesh to indices in old mesh */
94         GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
95         GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
96
97         const int numVert_src = dm->getNumVerts(dm);
98         const int numEdge_src = dm->getNumEdges(dm);
99         const int numPoly_src = dm->getNumPolys(dm);
100         MPoly *mpoly_src = dm->getPolyArray(dm);
101         MLoop *mloop_src = dm->getLoopArray(dm);
102         MEdge *medge_src = dm->getEdgeArray(dm);
103         MVert *mvert_src = dm->getVertArray(dm);
104
105
106         vertMap = MEM_mallocN(sizeof(*vertMap) * numVert_src, "build modifier vertMap");
107         edgeMap = MEM_mallocN(sizeof(*edgeMap) * numEdge_src, "build modifier edgeMap");
108         faceMap = MEM_mallocN(sizeof(*faceMap) * numPoly_src, "build modifier faceMap");
109
110 #pragma omp parallel sections if (numVert_src + numEdge_src + numPoly_src >= BKE_MESH_OMP_LIMIT)
111         {
112 #pragma omp section
113                 { range_vn_i(vertMap, numVert_src, 0); }
114 #pragma omp section
115                 { range_vn_i(edgeMap, numEdge_src, 0); }
116 #pragma omp section
117                 { range_vn_i(faceMap, numPoly_src, 0); }
118         }
119
120         frac = (BKE_scene_frame_get(md->scene) - bmd->start) / bmd->length;
121         CLAMP(frac, 0.0f, 1.0f);
122         
123         if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
124                 frac = 1.0 - frac;
125         }
126         
127         numFaces_dst = numPoly_src * frac;
128         numEdges_dst = numEdge_src * frac;
129
130         /* if there's at least one face, build based on faces */
131         if (numFaces_dst) {
132                 MPoly *mpoly, *mp;
133                 MLoop *ml, *mloop;
134                 MEdge *medge;
135                 
136                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
137                         BLI_array_randomize(faceMap, sizeof(*faceMap),
138                                             numPoly_src, bmd->seed);
139                 }
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 = mpoly_src;
145                 mloop = mloop_src;
146                 for (i = 0; i < numFaces_dst; 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_dst += 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 = medge_src;
163                 for (i = 0; i < numEdge_src; 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),
172                                                  SET_INT_IN_POINTER(i));
173                                 BLI_ghash_insert(edgeHash2, SET_INT_IN_POINTER(i),
174                                                  SET_INT_IN_POINTER(j));
175                         }
176                 }
177         }
178         else if (numEdges_dst) {
179                 MEdge *medge, *me;
180
181                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE)
182                         BLI_array_randomize(edgeMap, sizeof(*edgeMap),
183                                             numEdge_src, bmd->seed);
184
185                 /* get the set of all vert indices that will be in the final mesh,
186                  * mapped to the new indices
187                  */
188                 medge = medge_src;
189                 for (i = 0; i < numEdges_dst; i++) {
190                         me = medge + edgeMap[i];
191
192                         if (!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1))) {
193                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me->v1),
194                                                  SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
195                         }
196                         if (!BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2))) {
197                                 BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(me->v2), SET_INT_IN_POINTER(BLI_ghash_size(vertHash)));
198                         }
199                 }
200
201                 /* get the set of edges that will be in the new mesh */
202                 for (i = 0; i < numEdges_dst; i++) {
203                         j = BLI_ghash_size(edgeHash);
204                         
205                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j),
206                                          SET_INT_IN_POINTER(edgeMap[i]));
207                         BLI_ghash_insert(edgeHash2,  SET_INT_IN_POINTER(edgeMap[i]),
208                                          SET_INT_IN_POINTER(j));
209                 }
210         }
211         else {
212                 int numVerts = numVert_src * frac;
213
214                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
215                         BLI_array_randomize(vertMap, sizeof(*vertMap),
216                                             numVert_src, bmd->seed);
217                 }
218
219                 /* get the set of all vert indices that will be in the final mesh,
220                  * mapped to the new indices
221                  */
222                 for (i = 0; i < numVerts; i++) {
223                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(vertMap[i]), SET_INT_IN_POINTER(i));
224                 }
225         }
226
227         /* now we know the number of verts, edges and faces, we can create
228          * the mesh
229          */
230         result = CDDM_from_template(dm, BLI_ghash_size(vertHash),
231                                     BLI_ghash_size(edgeHash), 0, numLoops_dst, numFaces_dst);
232
233         /* copy the vertices across */
234         for (hashIter = BLI_ghashIterator_new(vertHash);
235              BLI_ghashIterator_done(hashIter) == false;
236              BLI_ghashIterator_step(hashIter)
237              )
238         {
239                 MVert source;
240                 MVert *dest;
241                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(hashIter));
242                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(hashIter));
243
244                 source = mvert_src[oldIndex];
245                 dest = CDDM_get_vert(result, newIndex);
246
247                 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
248                 *dest = source;
249         }
250         BLI_ghashIterator_free(hashIter);
251         
252         /* copy the edges across, remapping indices */
253         for (i = 0; i < BLI_ghash_size(edgeHash); i++) {
254                 MEdge source;
255                 MEdge *dest;
256                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash, SET_INT_IN_POINTER(i)));
257                 
258                 source = medge_src[oldIndex];
259                 dest = CDDM_get_edge(result, i);
260                 
261                 source.v1 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v1)));
262                 source.v2 = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(source.v2)));
263                 
264                 DM_copy_edge_data(dm, result, oldIndex, i, 1);
265                 *dest = source;
266         }
267
268         mpoly_dst = CDDM_get_polys(result);
269         /* mloop_dst = */ ml_dst = CDDM_get_loops(result);
270         
271         /* copy the faces across, remapping indices */
272         k = 0;
273         for (i = 0; i < numFaces_dst; i++) {
274                 MPoly *source;
275                 MPoly *dest;
276                 
277                 source = mpoly_src + faceMap[i];
278                 dest = mpoly_dst + i;
279                 DM_copy_poly_data(dm, result, faceMap[i], i, 1);
280                 
281                 *dest = *source;
282                 dest->loopstart = k;
283                 
284                 DM_copy_loop_data(dm, result, source->loopstart, dest->loopstart, dest->totloop);
285
286                 ml_src = mloop_src + source->loopstart;
287                 for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
288                         ml_dst->v = GET_INT_FROM_POINTER(BLI_ghash_lookup(vertHash, SET_INT_IN_POINTER(ml_src->v)));
289                         ml_dst->e = GET_INT_FROM_POINTER(BLI_ghash_lookup(edgeHash2, SET_INT_IN_POINTER(ml_src->e)));
290                 }
291         }
292
293         BLI_ghash_free(vertHash, NULL, NULL);
294         BLI_ghash_free(edgeHash, NULL, NULL);
295         BLI_ghash_free(edgeHash2, NULL, NULL);
296         
297         MEM_freeN(vertMap);
298         MEM_freeN(edgeMap);
299         MEM_freeN(faceMap);
300
301         if (dm->dirty & DM_DIRTY_NORMALS) {
302                 result->dirty |= DM_DIRTY_NORMALS;
303         }
304
305         return result;
306 }
307
308
309 ModifierTypeInfo modifierType_Build = {
310         /* name */              "Build",
311         /* structName */        "BuildModifierData",
312         /* structSize */        sizeof(BuildModifierData),
313         /* type */              eModifierTypeType_Nonconstructive,
314         /* flags */             eModifierTypeFlag_AcceptsMesh |
315                                 eModifierTypeFlag_AcceptsCVs,
316         /* copyData */          copyData,
317         /* deformVerts */       NULL,
318         /* deformMatrices */    NULL,
319         /* deformVertsEM */     NULL,
320         /* deformMatricesEM */  NULL,
321         /* applyModifier */     applyModifier,
322         /* applyModifierEM */   NULL,
323         /* initData */          initData,
324         /* requiredDataMask */  NULL,
325         /* freeData */          NULL,
326         /* isDisabled */        NULL,
327         /* updateDepgraph */    NULL,
328         /* dependsOnTime */     dependsOnTime,
329         /* dependsOnNormals */  NULL,
330         /* foreachObjectLink */ NULL,
331         /* foreachIDLink */     NULL,
332         /* foreachTexLink */    NULL,
333 };