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