62da8b8ffeaacc75941d86ec1ccd34275b91e980
[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_meshdata_types.h"
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_modifier.h"
47 #include "BKE_particle.h"
48 #include "BKE_scene.h"
49
50
51
52 #ifdef _OPENMP
53 #  include "BKE_mesh.h"  /* BKE_MESH_OMP_LIMIT */
54 #endif
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 #if 0
67         BuildModifierData *bmd = (BuildModifierData *) md;
68         BuildModifierData *tbmd = (BuildModifierData *) target;
69 #endif
70         modifier_copyData_generic(md, target);
71 }
72
73 static bool dependsOnTime(ModifierData *UNUSED(md))
74 {
75         return true;
76 }
77
78 static DerivedMesh *applyModifier(ModifierData *md, Object *UNUSED(ob),
79                                   DerivedMesh *derivedData,
80                                   ModifierApplyFlag UNUSED(flag))
81 {
82         DerivedMesh *dm = derivedData;
83         DerivedMesh *result;
84         BuildModifierData *bmd = (BuildModifierData *) md;
85         int i, j, k;
86         int numFaces_dst, numEdges_dst, numLoops_dst = 0;
87         int *vertMap, *edgeMap, *faceMap;
88         float frac;
89         MPoly *mpoly_dst;
90         MLoop *ml_dst, *ml_src /*, *mloop_dst */;
91         GHashIterator gh_iter;
92         /* maps vert indices in old mesh to indices in new mesh */
93         GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
94         /* maps edge indices in new mesh to indices in old mesh */
95         GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
96         GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
97
98         const int numVert_src = dm->getNumVerts(dm);
99         const int numEdge_src = dm->getNumEdges(dm);
100         const int numPoly_src = dm->getNumPolys(dm);
101         MPoly *mpoly_src = dm->getPolyArray(dm);
102         MLoop *mloop_src = dm->getLoopArray(dm);
103         MEdge *medge_src = dm->getEdgeArray(dm);
104         MVert *mvert_src = dm->getVertArray(dm);
105
106
107         vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
108         edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
109         faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
110
111         range_vn_i(vertMap, numVert_src, 0);
112         range_vn_i(edgeMap, numEdge_src, 0);
113         range_vn_i(faceMap, numPoly_src, 0);
114
115         frac = (BKE_scene_frame_get(md->scene) - bmd->start) / bmd->length;
116         CLAMP(frac, 0.0f, 1.0f);
117         
118         if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
119                 frac = 1.0f - frac;
120         }
121         
122         numFaces_dst = numPoly_src * frac;
123         numEdges_dst = numEdge_src * frac;
124
125         /* if there's at least one face, build based on faces */
126         if (numFaces_dst) {
127                 MPoly *mpoly, *mp;
128                 MLoop *ml, *mloop;
129                 MEdge *medge;
130                 uintptr_t hash_num, hash_num_alt;
131                 
132                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
133                         BLI_array_randomize(faceMap, sizeof(*faceMap),
134                                             numPoly_src, bmd->seed);
135                 }
136
137                 /* get the set of all vert indices that will be in the final mesh,
138                  * mapped to the new indices
139                  */
140                 mpoly = mpoly_src;
141                 mloop = mloop_src;
142                 hash_num = 0;
143                 for (i = 0; i < numFaces_dst; i++) {
144                         mp = mpoly + faceMap[i];
145                         ml = mloop + mp->loopstart;
146
147                         for (j = 0; j < mp->totloop; j++, ml++) {
148                                 void **val_p;
149                                 if (!BLI_ghash_ensure_p(vertHash, SET_INT_IN_POINTER(ml->v), &val_p)) {
150                                         *val_p = (void *)hash_num;
151                                         hash_num++;
152                                 }
153                         }
154
155                         numLoops_dst += mp->totloop;
156                 }
157                 BLI_assert(hash_num == BLI_ghash_size(vertHash));
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                 hash_num = 0;
164                 hash_num_alt = 0;
165                 for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
166                         MEdge *me = medge + i;
167
168                         if (BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v1)) &&
169                             BLI_ghash_haskey(vertHash, SET_INT_IN_POINTER(me->v2)))
170                         {
171                                 BLI_ghash_insert(edgeHash, (void *)hash_num, (void *)hash_num_alt);
172                                 BLI_ghash_insert(edgeHash2, (void *)hash_num_alt, (void *)hash_num);
173                                 hash_num++;
174                         }
175                 }
176         }
177         else if (numEdges_dst) {
178                 MEdge *medge, *me;
179                 uintptr_t hash_num;
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                 hash_num = 0;
190                 BLI_assert(hash_num == BLI_ghash_size(vertHash));
191                 for (i = 0; i < numEdges_dst; i++) {
192                         void **val_p;
193                         me = medge + edgeMap[i];
194
195                         if (!BLI_ghash_ensure_p(vertHash, SET_INT_IN_POINTER(me->v1), &val_p)) {
196                                 *val_p = (void *)hash_num;
197                                 hash_num++;
198                         }
199                         if (!BLI_ghash_ensure_p(vertHash, SET_INT_IN_POINTER(me->v2), &val_p)) {
200                                 *val_p = (void *)hash_num;
201                                 hash_num++;
202                         }
203                 }
204                 BLI_assert(hash_num == BLI_ghash_size(vertHash));
205
206                 /* get the set of edges that will be in the new mesh */
207                 for (i = 0; i < numEdges_dst; i++) {
208                         j = BLI_ghash_size(edgeHash);
209                         
210                         BLI_ghash_insert(edgeHash, SET_INT_IN_POINTER(j),
211                                          SET_INT_IN_POINTER(edgeMap[i]));
212                         BLI_ghash_insert(edgeHash2,  SET_INT_IN_POINTER(edgeMap[i]),
213                                          SET_INT_IN_POINTER(j));
214                 }
215         }
216         else {
217                 int numVerts = numVert_src * frac;
218
219                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
220                         BLI_array_randomize(vertMap, sizeof(*vertMap),
221                                             numVert_src, bmd->seed);
222                 }
223
224                 /* get the set of all vert indices that will be in the final mesh,
225                  * mapped to the new indices
226                  */
227                 for (i = 0; i < numVerts; i++) {
228                         BLI_ghash_insert(vertHash, SET_INT_IN_POINTER(vertMap[i]), SET_INT_IN_POINTER(i));
229                 }
230         }
231
232         /* now we know the number of verts, edges and faces, we can create
233          * the mesh
234          */
235         result = CDDM_from_template(dm, BLI_ghash_size(vertHash),
236                                     BLI_ghash_size(edgeHash), 0, numLoops_dst, numFaces_dst);
237
238         /* copy the vertices across */
239         GHASH_ITER (gh_iter, vertHash) {
240                 MVert source;
241                 MVert *dest;
242                 int oldIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getKey(&gh_iter));
243                 int newIndex = GET_INT_FROM_POINTER(BLI_ghashIterator_getValue(&gh_iter));
244
245                 source = mvert_src[oldIndex];
246                 dest = CDDM_get_vert(result, newIndex);
247
248                 DM_copy_vert_data(dm, result, oldIndex, newIndex, 1);
249                 *dest = source;
250         }
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         /* updateDepsgraph */   NULL,
329         /* dependsOnTime */     dependsOnTime,
330         /* dependsOnNormals */  NULL,
331         /* foreachObjectLink */ NULL,
332         /* foreachIDLink */     NULL,
333         /* foreachTexLink */    NULL,
334 };