Blender V4.3
MOD_meshcache_pc2.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <algorithm>
10#include <cerrno>
11#include <cstdio>
12#include <cstring>
13
14#include "BLI_utildefines.h"
15
16#include "BLI_fileops.h"
17#ifdef __BIG_ENDIAN__
18# include "BLI_endian_switch.h"
19#endif
20
21#ifdef WIN32
22# include "BLI_winstuff.h"
23#endif
24
25#include "BLT_translation.hh"
26
27#include "DNA_modifier_types.h"
28
29#include "MOD_meshcache_util.hh" /* own include */
30
31struct PC2Head {
32 char header[12]; /* 'POINTCACHE2\0' */
33 int file_version; /* unused - should be 1 */
35 float start;
36 float sampling;
38}; /* frames, verts */
39
40static bool meshcache_read_pc2_head(FILE *fp,
41 const int verts_tot,
42 PC2Head *pc2_head,
43 const char **r_err_str)
44{
45 if (!fread(pc2_head, sizeof(*pc2_head), 1, fp)) {
46 *r_err_str = RPT_("Missing header");
47 return false;
48 }
49
50 if (!STREQ(pc2_head->header, "POINTCACHE2")) {
51 *r_err_str = RPT_("Invalid header");
52 return false;
53 }
54
55#ifdef __BIG_ENDIAN__
57 (sizeof(*pc2_head) - sizeof(pc2_head->header)) / sizeof(int));
58#endif
59
60 if (pc2_head->verts_tot != verts_tot) {
61 *r_err_str = RPT_("Vertex count mismatch");
62 return false;
63 }
64
65 if (pc2_head->frame_tot <= 0) {
66 *r_err_str = RPT_("Invalid frame total");
67 return false;
68 }
69 /* Intentionally don't seek back. */
70
71 return true;
72}
73
79static bool meshcache_read_pc2_range(FILE *fp,
80 const int verts_tot,
81 const float frame,
82 const char interp,
83 int r_index_range[2],
84 float *r_factor,
85 const char **r_err_str)
86{
87 PC2Head pc2_head;
88
89 /* first check interpolation and get the vert locations */
90
91 if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, r_err_str) == false) {
92 return false;
93 }
94
95 MOD_meshcache_calc_range(frame, interp, pc2_head.frame_tot, r_index_range, r_factor);
96
97 return true;
98}
99
101 const int verts_tot,
102 const float time,
103 const float fps,
104 float *r_frame,
105 const char **r_err_str)
106{
107 PC2Head pc2_head;
108 float frame;
109
110 if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, r_err_str) == false) {
111 return false;
112 }
113
114 frame = ((time / fps) - pc2_head.start) / pc2_head.sampling;
115
116 if (frame >= pc2_head.frame_tot) {
117 frame = float(pc2_head.frame_tot - 1);
118 }
119 else if (frame < 0.0f) {
120 frame = 0.0f;
121 }
122
123 *r_frame = frame;
124 return true;
125}
126
128 float (*vertexCos)[3],
129 const int verts_tot,
130 const int index,
131 const float factor,
132 const char **r_err_str)
133{
134 PC2Head pc2_head;
135
136 if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, r_err_str) == false) {
137 return false;
138 }
139
140 if (BLI_fseek(fp, sizeof(float[3]) * index * pc2_head.verts_tot, SEEK_CUR) != 0) {
141 *r_err_str = RPT_("Failed to seek frame");
142 return false;
143 }
144
145 size_t verts_read_num = 0;
146 errno = 0;
147 if (factor >= 1.0f) {
148 float *vco = *vertexCos;
149 uint i;
150 for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
151 verts_read_num += fread(vco, sizeof(float[3]), 1, fp);
152
153#ifdef __BIG_ENDIAN__
157#endif /* __BIG_ENDIAN__ */
158 }
159 }
160 else {
161 const float ifactor = 1.0f - factor;
162 float *vco = *vertexCos;
163 uint i;
164 for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
165 float tvec[3];
166 verts_read_num += fread(tvec, sizeof(float[3]), 1, fp);
167
168#ifdef __BIG_ENDIAN__
169 BLI_endian_switch_float(tvec + 0);
170 BLI_endian_switch_float(tvec + 1);
171 BLI_endian_switch_float(tvec + 2);
172#endif /* __BIG_ENDIAN__ */
173
174 vco[0] = (vco[0] * ifactor) + (tvec[0] * factor);
175 vco[1] = (vco[1] * ifactor) + (tvec[1] * factor);
176 vco[2] = (vco[2] * ifactor) + (tvec[2] * factor);
177 }
178 }
179
180 if (verts_read_num != pc2_head.verts_tot) {
181 *r_err_str = errno ? strerror(errno) : RPT_("Vertex coordinate read failed");
182 return false;
183 }
184
185 return true;
186}
187
189 float (*vertexCos)[3],
190 const int verts_tot,
191 const char interp,
192 const float frame,
193 const char **r_err_str)
194{
195 int index_range[2];
196 float factor;
197
199 verts_tot,
200 frame,
201 interp,
202 index_range,
203 &factor, /* read into these values */
204 r_err_str) == false)
205 {
206 return false;
207 }
208
209 if (index_range[0] == index_range[1]) {
210 /* read single */
211 if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
212 MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, r_err_str))
213 {
214 return true;
215 }
216
217 return false;
218 }
219
220 /* read both and interpolate */
221 if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
222 MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, r_err_str) &&
223 (BLI_fseek(fp, 0, SEEK_SET) == 0) &&
224 MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[1], factor, r_err_str))
225 {
226 return true;
227 }
228
229 return false;
230}
231
232bool MOD_meshcache_read_pc2_times(const char *filepath,
233 float (*vertexCos)[3],
234 const int verts_tot,
235 const char interp,
236 const float time,
237 const float fps,
238 const char time_mode,
239 const char **r_err_str)
240{
241 float frame;
242
243 FILE *fp = BLI_fopen(filepath, "rb");
244 bool ok;
245
246 if (fp == nullptr) {
247 *r_err_str = errno ? strerror(errno) : RPT_("Unknown error opening file");
248 return false;
249 }
250
251 switch (time_mode) {
253 frame = time;
254 break;
255 }
257 /* we need to find the closest time */
258 if (meshcache_read_pc2_range_from_time(fp, verts_tot, time, fps, &frame, r_err_str) == false)
259 {
260 fclose(fp);
261 return false;
262 }
263 rewind(fp);
264 break;
265 }
267 default: {
268 PC2Head pc2_head;
269 if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, r_err_str) == false) {
270 fclose(fp);
271 return false;
272 }
273
274 frame = std::clamp(time, 0.0f, 1.0f) * float(pc2_head.frame_tot);
275 rewind(fp);
276 break;
277 }
278 }
279
280 ok = MOD_meshcache_read_pc2_frame(fp, vertexCos, verts_tot, interp, frame, r_err_str);
281
282 fclose(fp);
283 return ok;
284}
void BLI_endian_switch_int32_array(int *val, int size) ATTR_NONNULL(1)
BLI_INLINE void BLI_endian_switch_float(float *val) ATTR_NONNULL(1)
File and directory operations.
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:188
unsigned int uint
#define STREQ(a, b)
Compatibility-like things for windows.
#define RPT_(msgid)
@ MOD_MESHCACHE_TIME_FRAME
@ MOD_MESHCACHE_TIME_FACTOR
@ MOD_MESHCACHE_TIME_SECONDS
static bool meshcache_read_pc2_head(FILE *fp, const int verts_tot, PC2Head *pc2_head, const char **r_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 **r_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 **r_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 **r_err_str)
bool MOD_meshcache_read_pc2_frame(FILE *fp, float(*vertexCos)[3], const int verts_tot, const char interp, const float frame, const char **r_err_str)
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 **r_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
draw_view in_light_buf[] float
ccl_device_inline float2 interp(const float2 a, const float2 b, float t)
char header[12]