Fix: Scrolling Preferences could cause blank UI
[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 #include "DNA_mesh_types.h"
45 #include "DNA_object_types.h"
46
47 #include "DEG_depsgraph_query.h"
48
49 #include "BKE_mesh.h"
50 #include "BKE_modifier.h"
51 #include "BKE_particle.h"
52 #include "BKE_scene.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 bool dependsOnTime(ModifierData *UNUSED(md))
63 {
64         return true;
65 }
66
67 static Mesh *applyModifier(
68         ModifierData *md, const ModifierEvalContext *ctx,
69         struct Mesh *mesh)
70 {
71         Mesh *result;
72         BuildModifierData *bmd = (BuildModifierData *) md;
73         int i, j, k;
74         int numFaces_dst, numEdges_dst, numLoops_dst = 0;
75         int *vertMap, *edgeMap, *faceMap;
76         float frac;
77         MPoly *mpoly_dst;
78         MLoop *ml_dst, *ml_src /*, *mloop_dst */;
79         GHashIterator gh_iter;
80         /* maps vert indices in old mesh to indices in new mesh */
81         GHash *vertHash = BLI_ghash_int_new("build ve apply gh");
82         /* maps edge indices in new mesh to indices in old mesh */
83         GHash *edgeHash = BLI_ghash_int_new("build ed apply gh");
84         /* maps edge indices in old mesh to indices in new mesh */
85         GHash *edgeHash2 = BLI_ghash_int_new("build ed apply gh");
86
87         const int numVert_src = mesh->totvert;
88         const int numEdge_src = mesh->totedge;
89         const int numPoly_src = mesh->totpoly;
90         MPoly *mpoly_src = mesh->mpoly;
91         MLoop *mloop_src = mesh->mloop;
92         MEdge *medge_src = mesh->medge;
93         MVert *mvert_src = mesh->mvert;
94
95         vertMap = MEM_malloc_arrayN(numVert_src, sizeof(*vertMap), "build modifier vertMap");
96         edgeMap = MEM_malloc_arrayN(numEdge_src, sizeof(*edgeMap), "build modifier edgeMap");
97         faceMap = MEM_malloc_arrayN(numPoly_src, sizeof(*faceMap), "build modifier faceMap");
98
99         range_vn_i(vertMap, numVert_src, 0);
100         range_vn_i(edgeMap, numEdge_src, 0);
101         range_vn_i(faceMap, numPoly_src, 0);
102
103         struct Scene *scene = DEG_get_input_scene(ctx->depsgraph);
104         frac = (BKE_scene_frame_get(scene) - bmd->start) / bmd->length;
105         CLAMP(frac, 0.0f, 1.0f);
106         if (bmd->flag & MOD_BUILD_FLAG_REVERSE) {
107                 frac = 1.0f - frac;
108         }
109
110         numFaces_dst = numPoly_src * frac;
111         numEdges_dst = numEdge_src * frac;
112
113         /* if there's at least one face, build based on faces */
114         if (numFaces_dst) {
115                 MPoly *mpoly, *mp;
116                 MLoop *ml, *mloop;
117                 uintptr_t hash_num, hash_num_alt;
118
119                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
120                         BLI_array_randomize(faceMap, sizeof(*faceMap),
121                                             numPoly_src, bmd->seed);
122                 }
123
124                 /* get the set of all vert indices that will be in the final mesh,
125                  * mapped to the new indices
126                  */
127                 mpoly = mpoly_src;
128                 mloop = mloop_src;
129                 hash_num = 0;
130                 for (i = 0; i < numFaces_dst; i++) {
131                         mp = mpoly + faceMap[i];
132                         ml = mloop + mp->loopstart;
133
134                         for (j = 0; j < mp->totloop; j++, ml++) {
135                                 void **val_p;
136                                 if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(ml->v), &val_p)) {
137                                         *val_p = (void *)hash_num;
138                                         hash_num++;
139                                 }
140                         }
141
142                         numLoops_dst += mp->totloop;
143                 }
144                 BLI_assert(hash_num == BLI_ghash_len(vertHash));
145
146                 /* get the set of edges that will be in the new mesh (i.e. all edges
147                  * that have both verts in the new mesh)
148                  */
149                 hash_num = 0;
150                 hash_num_alt = 0;
151                 for (i = 0; i < numEdge_src; i++, hash_num_alt++) {
152                         MEdge *me = medge_src + i;
153
154                         if (BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v1)) &&
155                             BLI_ghash_haskey(vertHash, POINTER_FROM_INT(me->v2)))
156                         {
157                                 BLI_ghash_insert(edgeHash, (void *)hash_num, (void *)hash_num_alt);
158                                 BLI_ghash_insert(edgeHash2, (void *)hash_num_alt, (void *)hash_num);
159                                 hash_num++;
160                         }
161                 }
162                 BLI_assert(hash_num == BLI_ghash_len(edgeHash));
163         }
164         else if (numEdges_dst) {
165                 MEdge *medge, *me;
166                 uintptr_t hash_num;
167
168                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE)
169                         BLI_array_randomize(edgeMap, sizeof(*edgeMap),
170                                             numEdge_src, bmd->seed);
171
172                 /* get the set of all vert indices that will be in the final mesh,
173                  * mapped to the new indices
174                  */
175                 medge = medge_src;
176                 hash_num = 0;
177                 BLI_assert(hash_num == BLI_ghash_len(vertHash));
178                 for (i = 0; i < numEdges_dst; i++) {
179                         void **val_p;
180                         me = medge + edgeMap[i];
181
182                         if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v1), &val_p)) {
183                                 *val_p = (void *)hash_num;
184                                 hash_num++;
185                         }
186                         if (!BLI_ghash_ensure_p(vertHash, POINTER_FROM_INT(me->v2), &val_p)) {
187                                 *val_p = (void *)hash_num;
188                                 hash_num++;
189                         }
190                 }
191                 BLI_assert(hash_num == BLI_ghash_len(vertHash));
192
193                 /* get the set of edges that will be in the new mesh */
194                 for (i = 0; i < numEdges_dst; i++) {
195                         j = BLI_ghash_len(edgeHash);
196
197                         BLI_ghash_insert(edgeHash, POINTER_FROM_INT(j),
198                                          POINTER_FROM_INT(edgeMap[i]));
199                         BLI_ghash_insert(edgeHash2,  POINTER_FROM_INT(edgeMap[i]),
200                                          POINTER_FROM_INT(j));
201                 }
202         }
203         else {
204                 int numVerts = numVert_src * frac;
205
206                 if (bmd->flag & MOD_BUILD_FLAG_RANDOMIZE) {
207                         BLI_array_randomize(vertMap, sizeof(*vertMap),
208                                             numVert_src, bmd->seed);
209                 }
210
211                 /* get the set of all vert indices that will be in the final mesh,
212                  * mapped to the new indices
213                  */
214                 for (i = 0; i < numVerts; i++) {
215                         BLI_ghash_insert(vertHash, POINTER_FROM_INT(vertMap[i]), POINTER_FROM_INT(i));
216                 }
217         }
218
219         /* now we know the number of verts, edges and faces, we can create the mesh. */
220         result = BKE_mesh_new_nomain_from_template(
221                 mesh, BLI_ghash_len(vertHash), BLI_ghash_len(edgeHash),
222                 0, numLoops_dst, numFaces_dst);
223
224         /* copy the vertices across */
225         GHASH_ITER (gh_iter, vertHash) {
226                 MVert source;
227                 MVert *dest;
228                 int oldIndex = POINTER_AS_INT(BLI_ghashIterator_getKey(&gh_iter));
229                 int newIndex = POINTER_AS_INT(BLI_ghashIterator_getValue(&gh_iter));
230
231                 source = mvert_src[oldIndex];
232                 dest = &result->mvert[newIndex];
233
234                 CustomData_copy_data(&mesh->vdata, &result->vdata, oldIndex, newIndex, 1);
235                 *dest = source;
236         }
237
238         /* copy the edges across, remapping indices */
239         for (i = 0; i < BLI_ghash_len(edgeHash); i++) {
240                 MEdge source;
241                 MEdge *dest;
242                 int oldIndex = POINTER_AS_INT(BLI_ghash_lookup(edgeHash, POINTER_FROM_INT(i)));
243
244                 source = medge_src[oldIndex];
245                 dest = &result->medge[i];
246
247                 source.v1 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v1)));
248                 source.v2 = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(source.v2)));
249
250                 CustomData_copy_data(&mesh->edata, &result->edata, oldIndex, i, 1);
251                 *dest = source;
252         }
253
254         mpoly_dst = result->mpoly;
255         ml_dst = result->mloop;
256
257         /* copy the faces across, remapping indices */
258         k = 0;
259         for (i = 0; i < numFaces_dst; i++) {
260                 MPoly *source;
261                 MPoly *dest;
262
263                 source = mpoly_src + faceMap[i];
264                 dest = mpoly_dst + i;
265                 CustomData_copy_data(&mesh->pdata, &result->pdata, faceMap[i], i, 1);
266
267                 *dest = *source;
268                 dest->loopstart = k;
269                 CustomData_copy_data(&mesh->ldata, &result->ldata, source->loopstart, dest->loopstart, dest->totloop);
270
271                 ml_src = mloop_src + source->loopstart;
272                 for (j = 0; j < source->totloop; j++, k++, ml_src++, ml_dst++) {
273                         ml_dst->v = POINTER_AS_INT(BLI_ghash_lookup(vertHash, POINTER_FROM_INT(ml_src->v)));
274                         ml_dst->e = POINTER_AS_INT(BLI_ghash_lookup(edgeHash2, POINTER_FROM_INT(ml_src->e)));
275                 }
276         }
277
278         BLI_ghash_free(vertHash, NULL, NULL);
279         BLI_ghash_free(edgeHash, NULL, NULL);
280         BLI_ghash_free(edgeHash2, NULL, NULL);
281
282         MEM_freeN(vertMap);
283         MEM_freeN(edgeMap);
284         MEM_freeN(faceMap);
285
286         if (mesh->runtime.cd_dirty_vert & CD_MASK_NORMAL) {
287                 result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
288         }
289
290         /* TODO(sybren): also copy flags & tags? */
291         return result;
292 }
293
294
295 ModifierTypeInfo modifierType_Build = {
296         /* name */              "Build",
297         /* structName */        "BuildModifierData",
298         /* structSize */        sizeof(BuildModifierData),
299         /* type */              eModifierTypeType_Nonconstructive,
300         /* flags */             eModifierTypeFlag_AcceptsMesh |
301                                 eModifierTypeFlag_AcceptsCVs,
302
303         /* copyData */          modifier_copyData_generic,
304
305         /* deformVerts_DM */    NULL,
306         /* deformMatrices_DM */ NULL,
307         /* deformVertsEM_DM */  NULL,
308         /* deformMatricesEM_DM*/NULL,
309         /* applyModifier_DM */  NULL,
310
311         /* deformVerts */       NULL,
312         /* deformMatrices */    NULL,
313         /* deformVertsEM */     NULL,
314         /* deformMatricesEM */  NULL,
315         /* applyModifier */     applyModifier,
316
317         /* initData */          initData,
318         /* requiredDataMask */  NULL,
319         /* freeData */          NULL,
320         /* isDisabled */        NULL,
321         /* updateDepsgraph */   NULL,
322         /* dependsOnTime */     dependsOnTime,
323         /* dependsOnNormals */  NULL,
324         /* foreachObjectLink */ NULL,
325         /* foreachIDLink */     NULL,
326         /* foreachTexLink */    NULL,
327 };