Blender  V2.93
MOD_meshcache_pc2.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <errno.h>
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include "BLI_utildefines.h"
26 
27 #include "BLI_fileops.h"
28 #ifdef __BIG_ENDIAN__
29 # include "BLI_endian_switch.h"
30 #endif
31 
32 #ifdef WIN32
33 # include "BLI_winstuff.h"
34 #endif
35 
36 #include "DNA_modifier_types.h"
37 
38 #include "MOD_meshcache_util.h" /* own include */
39 
40 typedef struct PC2Head {
41  char header[12]; /* 'POINTCACHE2\0' */
42  int file_version; /* unused - should be 1 */
43  int verts_tot;
44  float start;
45  float sampling;
46  int frame_tot;
47 } PC2Head; /* frames, verts */
48 
49 static bool meshcache_read_pc2_head(FILE *fp,
50  const int verts_tot,
51  PC2Head *pc2_head,
52  const char **err_str)
53 {
54  if (!fread(pc2_head, sizeof(*pc2_head), 1, fp)) {
55  *err_str = "Missing header";
56  return false;
57  }
58 
59  if (!STREQ(pc2_head->header, "POINTCACHE2")) {
60  *err_str = "Invalid header";
61  return false;
62  }
63 
64 #ifdef __BIG_ENDIAN__
66  (sizeof(*pc2_head) - sizeof(pc2_head->header)) / sizeof(int));
67 #endif
68 
69  if (pc2_head->verts_tot != verts_tot) {
70  *err_str = "Vertex count mismatch";
71  return false;
72  }
73 
74  if (pc2_head->frame_tot <= 0) {
75  *err_str = "Invalid frame total";
76  return false;
77  }
78  /* Intentionally don't seek back. */
79 
80  return true;
81 }
82 
88 static bool meshcache_read_pc2_range(FILE *fp,
89  const int verts_tot,
90  const float frame,
91  const char interp,
92  int r_index_range[2],
93  float *r_factor,
94  const char **err_str)
95 {
96  PC2Head pc2_head;
97 
98  /* first check interpolation and get the vert locations */
99 
100  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
101  return false;
102  }
103 
104  MOD_meshcache_calc_range(frame, interp, pc2_head.frame_tot, r_index_range, r_factor);
105 
106  return true;
107 }
108 
110  const int verts_tot,
111  const float time,
112  const float fps,
113  float *r_frame,
114  const char **err_str)
115 {
116  PC2Head pc2_head;
117  float frame;
118 
119  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
120  return false;
121  }
122 
123  frame = ((time / fps) - pc2_head.start) / pc2_head.sampling;
124 
125  if (frame >= pc2_head.frame_tot) {
126  frame = (float)(pc2_head.frame_tot - 1);
127  }
128  else if (frame < 0.0f) {
129  frame = 0.0f;
130  }
131 
132  *r_frame = frame;
133  return true;
134 }
135 
137  float (*vertexCos)[3],
138  const int verts_tot,
139  const int index,
140  const float factor,
141  const char **err_str)
142 {
143  PC2Head pc2_head;
144 
145  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
146  return false;
147  }
148 
149  if (BLI_fseek(fp, sizeof(float[3]) * index * pc2_head.verts_tot, SEEK_CUR) != 0) {
150  *err_str = "Failed to seek frame";
151  return false;
152  }
153 
154  size_t num_verts_read = 0;
155  errno = 0;
156  if (factor >= 1.0f) {
157  float *vco = *vertexCos;
158  uint i;
159  for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
160  num_verts_read += fread(vco, sizeof(float[3]), 1, fp);
161 
162 #ifdef __BIG_ENDIAN__
163  BLI_endian_switch_float(vco + 0);
164  BLI_endian_switch_float(vco + 1);
165  BLI_endian_switch_float(vco + 2);
166 #endif /* __BIG_ENDIAN__ */
167  }
168  }
169  else {
170  const float ifactor = 1.0f - factor;
171  float *vco = *vertexCos;
172  uint i;
173  for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
174  float tvec[3];
175  num_verts_read += fread(tvec, sizeof(float[3]), 1, fp);
176 
177 #ifdef __BIG_ENDIAN__
178  BLI_endian_switch_float(tvec + 0);
179  BLI_endian_switch_float(tvec + 1);
180  BLI_endian_switch_float(tvec + 2);
181 #endif /* __BIG_ENDIAN__ */
182 
183  vco[0] = (vco[0] * ifactor) + (tvec[0] * factor);
184  vco[1] = (vco[1] * ifactor) + (tvec[1] * factor);
185  vco[2] = (vco[2] * ifactor) + (tvec[2] * factor);
186  }
187  }
188 
189  if (num_verts_read != pc2_head.verts_tot) {
190  *err_str = errno ? strerror(errno) : "Vertex coordinate read failed";
191  return false;
192  }
193 
194  return true;
195 }
196 
198  float (*vertexCos)[3],
199  const int verts_tot,
200  const char interp,
201  const float frame,
202  const char **err_str)
203 {
204  int index_range[2];
205  float factor;
206 
208  verts_tot,
209  frame,
210  interp,
211  index_range,
212  &factor, /* read into these values */
213  err_str) == false) {
214  return false;
215  }
216 
217  if (index_range[0] == index_range[1]) {
218  /* read single */
219  if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
220  MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str)) {
221  return true;
222  }
223 
224  return false;
225  }
226 
227  /* read both and interpolate */
228  if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
229  MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str) &&
230  (BLI_fseek(fp, 0, SEEK_SET) == 0) &&
231  MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[1], factor, err_str)) {
232  return true;
233  }
234 
235  return false;
236 }
237 
238 bool MOD_meshcache_read_pc2_times(const char *filepath,
239  float (*vertexCos)[3],
240  const int verts_tot,
241  const char interp,
242  const float time,
243  const float fps,
244  const char time_mode,
245  const char **err_str)
246 {
247  float frame;
248 
249  FILE *fp = BLI_fopen(filepath, "rb");
250  bool ok;
251 
252  if (fp == NULL) {
253  *err_str = errno ? strerror(errno) : "Unknown error opening file";
254  return false;
255  }
256 
257  switch (time_mode) {
259  frame = time;
260  break;
261  }
263  /* we need to find the closest time */
264  if (meshcache_read_pc2_range_from_time(fp, verts_tot, time, fps, &frame, err_str) == false) {
265  fclose(fp);
266  return false;
267  }
268  rewind(fp);
269  break;
270  }
272  default: {
273  PC2Head pc2_head;
274  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
275  fclose(fp);
276  return false;
277  }
278 
279  frame = CLAMPIS(time, 0.0f, 1.0f) * (float)pc2_head.frame_tot;
280  rewind(fp);
281  break;
282  }
283  }
284 
285  ok = MOD_meshcache_read_pc2_frame(fp, vertexCos, verts_tot, interp, frame, err_str);
286 
287  fclose(fp);
288  return ok;
289 }
typedef float(TangentPoint)[2]
void BLI_endian_switch_int32_array(int *val, const int size) ATTR_NONNULL(1)
Definition: endian_switch.c:45
BLI_INLINE void BLI_endian_switch_float(float *val) ATTR_NONNULL(1)
File and directory operations.
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition: storage.c:186
FILE * BLI_fopen(const char *filename, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: fileops.c:1003
unsigned int uint
Definition: BLI_sys_types.h:83
#define CLAMPIS(a, b, c)
#define STREQ(a, b)
Compatibility-like things for windows.
@ MOD_MESHCACHE_TIME_FRAME
@ MOD_MESHCACHE_TIME_FACTOR
@ MOD_MESHCACHE_TIME_SECONDS
bool MOD_meshcache_read_pc2_times(const char *filepath, float(*vertexCos)[3], const int verts_tot, const char interp, const float time, const float fps, const char time_mode, const char **err_str)
static bool meshcache_read_pc2_head(FILE *fp, const int verts_tot, PC2Head *pc2_head, const char **err_str)
static bool meshcache_read_pc2_range(FILE *fp, const int verts_tot, const float frame, const char interp, int r_index_range[2], float *r_factor, const char **err_str)
static bool meshcache_read_pc2_range_from_time(FILE *fp, const int verts_tot, const float time, const float fps, float *r_frame, const char **err_str)
struct PC2Head PC2Head
bool MOD_meshcache_read_pc2_frame(FILE *fp, float(*vertexCos)[3], const int verts_tot, const char interp, const float frame, const char **err_str)
bool MOD_meshcache_read_pc2_index(FILE *fp, float(*vertexCos)[3], const int verts_tot, const int index, const float factor, const char **err_str)
void MOD_meshcache_calc_range(const float frame, const char interp, const int frame_tot, int r_index_range[2], float *r_factor)
double time
float sampling
char header[12]
ccl_device_inline float2 interp(const float2 &a, const float2 &b, float t)