2
* ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18
* Contributor(s): Campbell Barton
20
* ***** END GPL LICENSE BLOCK *****
23
/** \file blender/modifiers/intern/MOD_meshcache_pc2.c
31
#include "BLO_sys_types.h"
32
#include "BLI_utildefines.h"
33
#include "BLI_endian_switch.h"
34
#include "BLI_fileops.h"
37
#include "MOD_meshcache_util.h" /* own include */
39
#include "DNA_modifier_types.h"
41
typedef struct PC2Head {
42
char header[12]; /* 'POINTCACHE2\0' */
43
int file_version; /* unused - should be 1 */
48
} PC2Head; /* frames, verts */
50
static bool meshcache_read_pc2_head(FILE *fp, const int verts_tot,
54
if (!fread(pc2_head, sizeof(*pc2_head), 1, fp)) {
55
*err_str = "Missing header";
59
if (strcmp(pc2_head->header, "POINTCACHE2") != 0) {
60
*err_str = "Invalid header";
65
BLI_endian_switch_int32_array(&pc2_head->huh, (sizeof(*pc2_head) - sizeof(pc2_head->header)) / sizeof(int));
68
if (pc2_head->verts_tot != verts_tot) {
69
*err_str = "Vertex count mismatch";
73
if (pc2_head->frame_tot <= 0) {
74
*err_str = "Invalid frame total";
77
/* intentionally dont seek back */
84
* Gets the index frange and factor
86
* currently same as for MDD
88
static bool meshcache_read_pc2_range(FILE *fp,
90
const float frame, const char interp,
91
int r_index_range[2], float *r_factor,
96
/* first check interpolation and get the vert locations */
98
if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
102
MOD_meshcache_calc_range(frame, interp, pc2_head.frame_tot, r_index_range, r_factor);
107
static bool meshcache_read_pc2_range_from_time(FILE *fp,
109
const float time, const float fps,
111
const char **err_str)
116
if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
120
frame = ((time / fps) - pc2_head.start) / pc2_head.sampling;
122
if (frame >= pc2_head.frame_tot) {
123
frame = (float)(pc2_head.frame_tot - 1);
125
else if (frame < 0.0f) {
133
bool MOD_meshcache_read_pc2_index(FILE *fp,
134
float (*vertexCos)[3], const int verts_tot,
135
const int index, const float factor,
136
const char **err_str)
140
if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
144
if (fseek(fp, index * pc2_head.verts_tot * sizeof(float) * 3, SEEK_CUR) != 0) {
145
*err_str = "Failed to seek frame";
149
if (factor >= 1.0f) {
150
float *vco = *vertexCos;
152
for (i = pc2_head.verts_tot; i != 0 ; i--, vco += 3) {
153
fread(vco, sizeof(float) * 3, 1, fp);
155
# ifdef __BIG_ENDIAN__
156
BLI_endian_switch_float(vco + 0);
157
BLI_endian_switch_float(vco + 1);
158
BLI_endian_switch_float(vco + 2);
159
# endif /* __BIG_ENDIAN__ */
163
const float ifactor = 1.0f - factor;
164
float *vco = *vertexCos;
166
for (i = pc2_head.verts_tot; i != 0 ; i--, vco += 3) {
168
fread(tvec, sizeof(float) * 3, 1, fp);
170
#ifdef __BIG_ENDIAN__
171
BLI_endian_switch_float(tvec + 0);
172
BLI_endian_switch_float(tvec + 1);
173
BLI_endian_switch_float(tvec + 2);
174
#endif /* __BIG_ENDIAN__ */
176
vco[0] = (vco[0] * ifactor) + (tvec[0] * factor);
177
vco[1] = (vco[1] * ifactor) + (tvec[1] * factor);
178
vco[2] = (vco[2] * ifactor) + (tvec[2] * factor);
186
bool MOD_meshcache_read_pc2_frame(FILE *fp,
187
float (*vertexCos)[3], const int verts_tot, const char interp,
189
const char **err_str)
194
if (meshcache_read_pc2_range(fp, verts_tot, frame, interp,
195
index_range, &factor, /* read into these values */
201
if (index_range[0] == index_range[1]) {
203
if ((fseek(fp, 0, SEEK_SET) == 0) &&
204
MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str))
213
/* read both and interpolate */
214
if ((fseek(fp, 0, SEEK_SET) == 0) &&
215
MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str) &&
216
(fseek(fp, 0, SEEK_SET) == 0) &&
217
MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[1], factor, err_str))
227
bool MOD_meshcache_read_pc2_times(const char *filepath,
228
float (*vertexCos)[3], const int verts_tot, const char interp,
229
const float time, const float fps, const char time_mode,
230
const char **err_str)
234
FILE *fp = BLI_fopen(filepath, "rb");
238
*err_str = errno ? strerror(errno) : "Unknown error opening file";
243
case MOD_MESHCACHE_TIME_FRAME:
248
case MOD_MESHCACHE_TIME_SECONDS:
250
/* we need to find the closest time */
251
if (meshcache_read_pc2_range_from_time(fp, verts_tot, time, fps, &frame, err_str) == false) {
258
case MOD_MESHCACHE_TIME_FACTOR:
262
if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
267
frame = CLAMPIS(time, 0.0f, 1.0f) * (float)pc2_head.frame_tot;
273
ok = MOD_meshcache_read_pc2_frame(fp, vertexCos, verts_tot, interp, frame, err_str);