mesh-cache deform modifier,
[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
32 #include "BLI_utildefines.h"
33 #include "BLI_string.h"
34 #include "BLI_path_util.h"
35 #include "BLI_math.h"
36
37
38 #include "BKE_DerivedMesh.h"
39 #include "BKE_scene.h"
40 #include "BKE_global.h"
41 #include "BKE_main.h"
42
43 #include "MOD_meshcache_util.h"  /* utility functions */
44
45 #include "MOD_modifiertypes.h"
46
47 #include "MOD_util.h"
48
49 /* -------------------------------------------------------------------- */
50 /* Utility function shared by formats */
51 void MOD_meshcache_calc_range(const float frame, const char interp,
52                               const int frame_tot,
53                               int r_index_range[2], float *r_factor)
54 {
55         if (interp == MOD_MESHCACHE_INTERP_NONE) {
56                 r_index_range[0] = r_index_range[1] = max_ii(0, min_ii(frame_tot - 1, (int)(floorf(frame) + 0.5f)));
57                 *r_factor = 1.0f; /* dummy */
58         }
59         else {
60                 const float tframe = floorf(frame);
61                 const float range  = frame - tframe;
62                 r_index_range[0] = (int)tframe;
63                 if (range <= FRAME_SNAP_EPS) {
64                         /* we're close enough not to need blending */
65                         r_index_range[1] = r_index_range[0];
66                         *r_factor = 1.0f; /* dummy */
67                 }
68                 else {
69                         /* blend between 2 frames */
70                         r_index_range[1] = r_index_range[0] + 1;
71                         *r_factor = range;
72                 }
73
74                 /* clamp */
75                 if ((r_index_range[0] >= frame_tot) ||
76                     (r_index_range[1] >= frame_tot))
77                 {
78                         r_index_range[0] = r_index_range[1] = frame_tot - 1;
79                         *r_factor = 1.0f; /* dummy */
80                 }
81                 else if ((r_index_range[0] < 0) ||
82                          (r_index_range[1] < 0))
83                 {
84                         r_index_range[0] = r_index_range[1] = 0;
85                         *r_factor = 1.0f; /* dummy */
86                 }
87         }
88 }
89
90
91 /* -------------------------------------------------------------------- */
92
93 static void initData(ModifierData *md)
94 {
95         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
96
97         mcmd->flag = 0;
98         mcmd->type = MOD_MESHCACHE_TYPE_MDD;
99         mcmd->interp = MOD_MESHCACHE_INTERP_LINEAR;
100         mcmd->frame_scale = 1.0f;
101
102         /* (Y, Z). Blender default */
103         mcmd->forward_axis = 1;
104         mcmd->up_axis      = 2;
105 }
106
107 static void copyData(ModifierData *md, ModifierData *target)
108 {
109         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
110         MeshCacheModifierData *tmcmd = (MeshCacheModifierData *)target;
111
112         tmcmd->flag = mcmd->flag;
113         tmcmd->type = mcmd->type;
114
115         tmcmd->time_mode = mcmd->time_mode;
116         tmcmd->play_mode = mcmd->play_mode;
117
118         tmcmd->forward_axis = mcmd->forward_axis;
119         tmcmd->up_axis      = mcmd->up_axis;
120         tmcmd->flip_axis    = mcmd->flip_axis;
121
122         tmcmd->interp = mcmd->interp;
123
124         tmcmd->frame_start = mcmd->frame_start;
125         tmcmd->frame_scale = mcmd->frame_scale;
126
127         tmcmd->eval_frame  = mcmd->eval_frame;
128         tmcmd->eval_time   = mcmd->eval_time;
129         tmcmd->eval_factor = mcmd->eval_factor;
130
131         BLI_strncpy(tmcmd->filepath, mcmd->filepath, sizeof(tmcmd->filepath));
132 }
133
134 static int dependsOnTime(ModifierData *md)
135 {
136         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
137         return (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA);
138 }
139
140 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
141 {
142         MeshCacheModifierData *mcmd = (MeshCacheModifierData *) md;
143
144         /* leave it up to the modifier to check the file is valid on calculation */
145         return (mcmd->filepath[0] == '\0');
146 }
147
148
149 static void meshcache_do(
150         MeshCacheModifierData *mcmd, Object *ob, DerivedMesh *UNUSED(dm),
151         float (*vertexCos)[3], int numVerts)
152 {
153         Scene *scene = mcmd->modifier.scene;
154         const float fps = FPS;
155
156         char filepath[FILE_MAX];
157         const char *err_str = NULL;
158         bool ok;
159
160         float time;
161
162
163         /* -------------------------------------------------------------------- */
164         /* Interpret Time (the reading functions also do some of this ) */
165         if (mcmd->play_mode == MOD_MESHCACHE_PLAY_CFEA) {
166                 const float cfra = BKE_scene_frame_get(scene);
167
168                 switch (mcmd->time_mode) {
169                         case MOD_MESHCACHE_TIME_FRAME:
170                         {
171                                 time = cfra;
172                                 break;
173                         }
174                         case MOD_MESHCACHE_TIME_SECONDS:
175                         {
176                                 time = cfra / fps;
177                                 break;
178                         }
179                         case MOD_MESHCACHE_TIME_FACTOR:
180                         default:
181                         {
182                                 time = cfra / fps;
183                                 break;
184                         }
185                 }
186
187                 /* apply offset and scale */
188                 time = (mcmd->frame_scale * time) - mcmd->frame_start;
189         }
190         else {  /*  if (mcmd->play_mode == MOD_MESHCACHE_PLAY_EVAL) { */
191                 switch (mcmd->time_mode) {
192                         case MOD_MESHCACHE_TIME_FRAME:
193                         {
194                                 time = mcmd->eval_frame;
195                                 break;
196                         }
197                         case MOD_MESHCACHE_TIME_SECONDS:
198                         {
199                                 time = mcmd->eval_time;
200                                 break;
201                         }
202                         case MOD_MESHCACHE_TIME_FACTOR:
203                         default:
204                         {
205                                 time = mcmd->eval_factor;
206                                 break;
207                         }
208                 }
209         }
210
211
212         /* -------------------------------------------------------------------- */
213         /* Read the File (or error out when the file is bad) */
214
215         /* would be nice if we could avoid doing this _every_ frame */
216         BLI_strncpy(filepath, mcmd->filepath, sizeof(filepath));
217         BLI_path_abs(filepath, ID_BLEND_PATH(G.main, (ID *)ob));
218
219         switch (mcmd->type) {
220                 case MOD_MESHCACHE_TYPE_MDD:
221                         ok = MOD_meshcache_read_mdd_times(filepath, vertexCos, numVerts,
222                                                           mcmd->interp, time, fps, mcmd->time_mode, &err_str);
223                         break;
224                 case MOD_MESHCACHE_TYPE_PC2:
225                         ok = MOD_meshcache_read_pc2_times(filepath, vertexCos, numVerts,
226                                                           mcmd->interp, time, fps, mcmd->time_mode, &err_str);
227                         break;
228                 default:
229                         ok = false;
230                         break;
231         }
232
233
234         /* -------------------------------------------------------------------- */
235         /* Apply the transformation matrix (if needed) */
236         if (UNLIKELY(err_str)) {
237                 modifier_setError(&mcmd->modifier, err_str);
238         }
239         else if (ok) {
240                 bool use_matrix = false;
241                 float mat[3][3];
242                 unit_m3(mat);
243
244                 if (mat3_from_axis_conversion(mcmd->forward_axis, mcmd->up_axis, 1, 2, mat)) {
245                         use_matrix = true;
246                 }
247
248                 if (mcmd->flip_axis) {
249                         float tmat[3][3];
250                         unit_m3(tmat);
251                         if (mcmd->flip_axis & (1 << 0)) tmat[0][0] = -1.0f;
252                         if (mcmd->flip_axis & (1 << 1)) tmat[1][1] = -1.0f;
253                         if (mcmd->flip_axis & (1 << 2)) tmat[2][2] = -1.0f;
254                         mul_m3_m3m3(mat, tmat, mat);
255
256                         use_matrix = true;
257                 }
258
259                 if (use_matrix) {
260                         int i;
261                         for (i = 0; i < numVerts; i++) {
262                                 mul_m3_v3(mat, vertexCos[i]);
263                         }
264                 }
265         }
266 }
267
268 static void deformVerts(ModifierData *md, Object *ob,
269                         DerivedMesh *derivedData,
270                         float (*vertexCos)[3],
271                         int numVerts,
272                         ModifierApplyFlag UNUSED(flag))
273 {
274         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
275
276         meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts);
277 }
278
279 static void deformVertsEM(
280         ModifierData *md, Object *ob, struct BMEditMesh *UNUSED(editData),
281         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
282 {
283         MeshCacheModifierData *mcmd = (MeshCacheModifierData *)md;
284
285         meshcache_do(mcmd, ob, derivedData, vertexCos, numVerts);
286 }
287
288
289 ModifierTypeInfo modifierType_MeshCache = {
290         /* name */              "Mesh Cache",
291         /* structName */        "MeshCacheModifierData",
292         /* structSize */        sizeof(MeshCacheModifierData),
293         /* type */              eModifierTypeType_OnlyDeform,
294         /* flags */             eModifierTypeFlag_AcceptsCVs |
295                                 eModifierTypeFlag_SupportsEditmode,
296
297         /* copyData */          copyData,
298         /* deformVerts */       deformVerts,
299         /* deformMatrices */    NULL,
300         /* deformVertsEM */     deformVertsEM,
301         /* deformMatricesEM */  NULL,
302         /* applyModifier */     NULL,
303         /* applyModifierEM */   NULL,
304         /* initData */          initData,
305         /* requiredDataMask */  NULL,
306         /* freeData */          NULL,
307         /* isDisabled */        isDisabled,
308         /* updateDepgraph */    NULL,
309         /* dependsOnTime */     dependsOnTime,
310         /* dependsOnNormals */  NULL,
311         /* foreachObjectLink */ NULL,
312         /* foreachIDLink */     NULL,
313         /* foreachTexLink */    NULL,
314 };