add 'deform - integrate' option to mesh-cache,
[blender.git] / source / blender / modifiers / intern / MOD_meshcache.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  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/modifiers/intern/MOD_meshcache.c
24  *  \ingroup modifiers
25  */
26
27 #include <stdio.h>
28
29 #include "DNA_scene_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_mesh_types.h"
32 #include "DNA_meshdata_types.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_string.h"
36 #include "BLI_path_util.h"
37 #include "BLI_math.h"
38
39 #include "BKE_DerivedMesh.h"
40 #include "BKE_scene.h"
41 #include "BKE_global.h"
42 #include "BKE_mesh.h"
43 #include "BKE_main.h"
44
45 #include "MEM_guardedalloc.h"
46
47 #include "MOD_meshcache_util.h"  /* utility functions */
48
49 #include "MOD_modifiertypes.h"
50
51 #include "MOD_util.h"
52
53 static void initData(ModifierData *md)
54 {
55         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
56
57         mcmd->flag = 0;
58         mcmd->type = MOD_MESHCACHE_TYPE_MDD;
59         mcmd->interp = MOD_MESHCACHE_INTERP_LINEAR;
60         mcmd->frame_scale = 1.0f;
61
62         mcmd->factor = 1.0f;
63
64         /* (Y, Z). Blender default */
65         mcmd->forward_axis = 1;
66         mcmd->up_axis      = 2;
67 }
68
69 static void copyData(ModifierData *md, ModifierData *target)
70 {
71         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
72         MeshCacheModifierData *tmcmd = (MeshCacheModifierData *)target;
73
74         tmcmd->flag = mcmd->flag;
75         tmcmd->type = mcmd->type;
76
77         tmcmd->time_mode = mcmd->time_mode;
78         tmcmd->play_mode = mcmd->play_mode;
79
80         tmcmd->forward_axis = mcmd->forward_axis;
81         tmcmd->up_axis      = mcmd->up_axis;
82         tmcmd->flip_axis    = mcmd->flip_axis;
83
84         tmcmd->interp = mcmd->interp;
85
86         tmcmd->frame_start = mcmd->frame_start;
87         tmcmd->frame_scale = mcmd->frame_scale;
88
89         tmcmd->factor = mcmd->factor;
90         tmcmd->deform_mode = mcmd->deform_mode;
91
92         tmcmd->eval_frame  = mcmd->eval_frame;
93         tmcmd->eval_time   = mcmd->eval_time;
94         tmcmd->eval_factor = mcmd->eval_factor;
95
96         BLI_strncpy(tmcmd->filepath, mcmd->filepath, sizeof(tmcmd->filepath));
97 }
98
99 static int dependsOnTime(ModifierData *md)
100 {
101         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
102         return (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA);
103 }
104
105 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
106 {
107         MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
108
109         /* leave it up to the modifier to check the file is valid on calculation */
110         return (mcmd->factor <= 0.0f) || (mcmd->filepath[0] == '\0');
111 }
112
113
114 static void meshcache_do(
115         MeshCacheModifierData *mcmd, Object *ob, DerivedMesh *UNUSED(dm),
116         float (*vertexCos_Real)[3], int numVerts)
117 {
118         const bool use_factor = mcmd->factor < 1.0f;
119         float (*vertexCos_Store)[3] = (use_factor || (mcmd->deform_mode == MOD_MESHCACHE_DEFORM_INTEGRATE)) ?
120                                       MEM_mallocN(sizeof(*vertexCos_Store) * numVerts, __func__) : NULL;
121         float (*vertexCos)[3] = vertexCos_Store ? vertexCos_Store : vertexCos_Real;
122
123         Scene *scene = mcmd->modifier.scene;
124         const float fps = FPS;
125
126         char filepath[FILE_MAX];
127         const char *err_str = NULL;
128         bool ok;
129
130         float time;
131
132
133         /* -------------------------------------------------------------------- */
134         /* Interpret Time (the reading functions also do some of this ) */
135         if (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA) {
136                 const float cfra = BKE_scene_frame_get(scene);
137
138                 switch (mcmd->time_mode) {
139                         case MOD_MESHCACHE_TIME_FRAME:
140                         {
141                                 time = cfra;
142                                 break;
143                         }
144                         case MOD_MESHCACHE_TIME_SECONDS:
145                         {
146                                 time = cfra / fps;
147                                 break;
148                         }
149                         case MOD_MESHCACHE_TIME_FACTOR:
150                         default:
151                         {
152                                 time = cfra / fps;
153                                 break;
154                         }
155                 }
156
157                 /* apply offset and scale */
158                 time = (mcmd->frame_scale * time) - mcmd->frame_start;
159         }
160         else {  /*  if (mcmd->play_mode == MOD_MESHCACHE_PLAY_EVAL) { */
161                 switch (mcmd->time_mode) {
162                         case MOD_MESHCACHE_TIME_FRAME:
163                         {
164                                 time = mcmd->eval_frame;
165                                 break;
166                         }
167                         case MOD_MESHCACHE_TIME_SECONDS:
168                         {
169                                 time = mcmd->eval_time;
170                                 break;
171                         }
172                         case MOD_MESHCACHE_TIME_FACTOR:
173                         default:
174                         {
175                                 time = mcmd->eval_factor;
176                                 break;
177                         }
178                 }
179         }
180
181
182         /* -------------------------------------------------------------------- */
183         /* Read the File (or error out when the file is bad) */
184
185         /* would be nice if we could avoid doing this _every_ frame */
186         BLI_strncpy(filepath, mcmd->filepath, sizeof(filepath));
187         BLI_path_abs(filepath, ID_BLEND_PATH(G.main, (ID *)ob));
188
189         switch (mcmd->type) {
190                 case MOD_MESHCACHE_TYPE_MDD:
191                         ok = MOD_meshcache_read_mdd_times(filepath, vertexCos, numVerts,
192                                                           mcmd->interp, time, fps, mcmd->time_mode, &err_str);
193                         break;
194                 case MOD_MESHCACHE_TYPE_PC2:
195                         ok = MOD_meshcache_read_pc2_times(filepath, vertexCos, numVerts,
196                                                           mcmd->interp, time, fps, mcmd->time_mode, &err_str);
197                         break;
198                 default:
199                         ok = false;
200                         break;
201         }
202
203
204         /* -------------------------------------------------------------------- */
205         /* Apply the transformation matrix (if needed) */
206         if (UNLIKELY(err_str)) {
207                 modifier_setError(&mcmd->modifier, err_str);
208         }
209         else if (ok) {
210                 bool use_matrix = false;
211                 float mat[3][3];
212                 unit_m3(mat);
213
214                 if (mat3_from_axis_conversion(mcmd->forward_axis, mcmd->up_axis, 1, 2, mat)) {
215                         use_matrix = true;
216                 }
217
218                 if (mcmd->flip_axis) {
219                         float tmat[3][3];
220                         unit_m3(tmat);
221                         if (mcmd->flip_axis & (1 << 0)) tmat[0][0] = -1.0f;
222                         if (mcmd->flip_axis & (1 << 1)) tmat[1][1] = -1.0f;
223                         if (mcmd->flip_axis & (1 << 2)) tmat[2][2] = -1.0f;
224                         mul_m3_m3m3(mat, tmat, mat);
225
226                         use_matrix = true;
227                 }
228
229                 if (use_matrix) {
230                         int i;
231                         for (i = 0; i < numVerts; i++) {
232                                 mul_m3_v3(mat, vertexCos[i]);
233                         }
234                 }
235         }
236
237         /* tricky shape key integration (slow!) */
238         if (mcmd->deform_mode == MOD_MESHCACHE_DEFORM_INTEGRATE) {
239                 /* we could support any object type */
240                 if (ob->type != OB_MESH) {
241                         modifier_setError(&mcmd->modifier, "'Integrate' only valid for Mesh objects");
242                 }
243                 else {
244                         Mesh *me = ob->data;
245                         if (me->totvert != numVerts) {
246                                 modifier_setError(&mcmd->modifier, "'Integrate' original mesh vertex mismatch");
247                         }
248                         else {
249                                 if (me->totpoly == 0) {
250                                         modifier_setError(&mcmd->modifier, "'Integrate' requires faces");
251                                 }
252                                 else {
253                                         /* the moons align! */
254                                         int i;
255
256                                         float (*vertexCos_Source)[3] = MEM_mallocN(sizeof(*vertexCos_Source) * numVerts, __func__);
257                                         float (*vertexCos_New)[3]    = MEM_mallocN(sizeof(*vertexCos_New) * numVerts, __func__);
258                                         MVert *mv = me->mvert;
259
260                                         for (i = 0; i < numVerts; i++, mv++) {
261                                                 copy_v3_v3(vertexCos_Source[i], mv->co);
262                                         }
263
264                                         BKE_mesh_calc_relative_deform(
265                                                 me->mpoly, me->totpoly,
266                                                 me->mloop, me->totvert,
267
268                                                 (const float (*)[3])vertexCos_Source,   /* from the original Mesh*/
269                                                 (const float (*)[3])vertexCos_Real,     /* the input we've been given (shape keys!) */
270
271                                                 (const float (*)[3])vertexCos,          /* the result of this modifier */
272                                                 vertexCos_New       /* the result of this function */
273                                                 );
274
275                                         /* write the corrected locations back into the result */
276                                         memcpy(use_factor ? vertexCos : vertexCos_Real, vertexCos_New, sizeof(*vertexCos) * numVerts);
277
278                                         MEM_freeN(vertexCos_Source);
279                                         MEM_freeN(vertexCos_New);
280                                 }
281                         }
282                 }
283         }
284
285         if (vertexCos_Store) {
286
287                 if (ok && use_factor) {
288                         interp_vn_vn(*vertexCos_Real, *vertexCos_Store, mcmd->factor, numVerts * 3);
289                 }
290
291                 MEM_freeN(vertexCos_Store);
292         }
293 }
294
295 static void deformVerts(ModifierData *md, Object *ob,
296                         DerivedMesh *derivedData,
297                         float (*vertexCos)[3],
298                         int numVerts,
299                         ModifierApplyFlag UNUSED(flag))
300 {
301         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
302
303         meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts);
304 }
305
306 static void deformVertsEM(
307         ModifierData *md, Object *ob, struct BMEditMesh *UNUSED(editData),
308         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
309 {
310         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
311
312         meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts);
313 }
314
315
316 ModifierTypeInfo modifierType_MeshCache = {
317         /* name */              "Mesh Cache",
318         /* structName */        "MeshCacheModifierData",
319         /* structSize */        sizeof(MeshCacheModifierData),
320         /* type */              eModifierTypeType_OnlyDeform,
321         /* flags */             eModifierTypeFlag_AcceptsCVs |
322                                 eModifierTypeFlag_SupportsEditmode,
323
324         /* copyData */          copyData,
325         /* deformVerts */       deformVerts,
326         /* deformMatrices */    NULL,
327         /* deformVertsEM */     deformVertsEM,
328         /* deformMatricesEM */  NULL,
329         /* applyModifier */     NULL,
330         /* applyModifierEM */   NULL,
331         /* initData */          initData,
332         /* requiredDataMask */  NULL,
333         /* freeData */          NULL,
334         /* isDisabled */        isDisabled,
335         /* updateDepgraph */    NULL,
336         /* dependsOnTime */     dependsOnTime,
337         /* dependsOnNormals */  NULL,
338         /* foreachObjectLink */ NULL,
339         /* foreachIDLink */     NULL,
340         /* foreachTexLink */    NULL,
341 };