Blender V5.3
MOD_meshcache_mdd.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
8
9#include <algorithm>
10#include <cerrno>
11#include <cstdio>
12#include <cstring>
13
14#include "BLI_endian_switch.hh"
15#include "BLI_fileops.hh"
16#include "BLI_math_base_c.hh"
17#ifdef WIN32
18# include "BLI_winstuff.hh"
19#endif
20
21#include "BLT_translation.hh"
22
23#include "DNA_modifier_types.h"
24
25#include "MOD_meshcache_util.hh" /* own include */
26
27namespace blender {
28
29struct MDDHead {
32}; /* frames, verts */
33
34static bool meshcache_read_mdd_head(FILE *fp,
35 const int verts_tot,
36 MDDHead *mdd_head,
37 const char **r_err_str)
38{
39 if (!fread(mdd_head, sizeof(*mdd_head), 1, fp)) {
40 *r_err_str = RPT_("Missing header");
41 return false;
42 }
43
44 /* NOTE: this is endianness-sensitive. */
45 /* MDD is big-endian, its values need to be switched on little-endian systems. */
46 BLI_endian_switch_int32_array(reinterpret_cast<int *>(mdd_head), 2);
47
48 if (mdd_head->verts_tot != verts_tot) {
49 *r_err_str = RPT_("Vertex count mismatch");
50 return false;
51 }
52
53 if (mdd_head->frame_tot <= 0) {
54 *r_err_str = RPT_("Invalid frame total");
55 return false;
56 }
57 /* Intentionally don't seek back. */
58
59 return true;
60}
61
65static bool meshcache_read_mdd_range(FILE *fp,
66 const int verts_tot,
67 const float frame,
68 const char interp,
69 int r_index_range[2],
70 float *r_factor,
71 const char **r_err_str)
72{
73 MDDHead mdd_head;
74
75 /* first check interpolation and get the vert locations */
76
77 if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, r_err_str) == false) {
78 return false;
79 }
80
81 MOD_meshcache_calc_range(frame, interp, mdd_head.frame_tot, r_index_range, r_factor);
82
83 return true;
84}
85
87 const int verts_tot,
88 const float time,
89 const float /*fps*/,
90 float *r_frame,
91 const char **r_err_str)
92{
93 MDDHead mdd_head;
94 int i;
95 float f_time, f_time_prev = FLT_MAX;
96 float frame;
97
98 if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, r_err_str) == false) {
99 return false;
100 }
101
102 size_t frames_num_read = 0;
103 size_t frames_num_expect = mdd_head.frame_tot;
104 errno = 0;
105 for (i = 0; i < mdd_head.frame_tot; i++) {
106 frames_num_read += fread(&f_time, sizeof(float), 1, fp);
107 /* NOTE: this is endianness-sensitive. */
108 /* MDD is big-endian, its values need to be switched on little-endian systems. */
110
111 if (f_time >= time) {
112 frames_num_expect = i + 1;
113 break;
114 }
115 f_time_prev = f_time;
116 }
117
118 if (frames_num_read != frames_num_expect) {
119 *r_err_str = errno ? strerror(errno) : RPT_("Timestamp read failed");
120 return false;
121 }
122
123 if (f_time_prev == FLT_MAX) [[unlikely]] {
124 frame = 0.0f;
125 }
126 else {
127 const float range = f_time - f_time_prev;
128
129 if (range <= FRAME_SNAP_EPS) {
130 frame = float(i);
131 }
132 else {
133 frame = float(i - 1) + ((time - f_time_prev) / range);
134 }
135 }
136
137 *r_frame = frame;
138 return true;
139}
140
142 float (*vertexCos)[3],
143 const int verts_tot,
144 const int index,
145 const float factor,
146 const char **r_err_str)
147{
148 MDDHead mdd_head;
149
150 if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, r_err_str) == false) {
151 return false;
152 }
153
154 if (BLI_fseek(fp, mdd_head.frame_tot * sizeof(int), SEEK_CUR) != 0) {
155 *r_err_str = RPT_("Header seek failed");
156 return false;
157 }
158
159 if (BLI_fseek(fp, sizeof(float[3]) * index * mdd_head.verts_tot, SEEK_CUR) != 0) {
160 *r_err_str = RPT_("Failed to seek frame");
161 return false;
162 }
163
164 size_t verts_read_num = 0;
165 errno = 0;
166 if (factor >= 1.0f) {
167#if 1
168 float *vco = *vertexCos;
169 uint i;
170 for (i = mdd_head.verts_tot; i != 0; i--, vco += 3) {
171 verts_read_num += fread(vco, sizeof(float[3]), 1, fp);
172
173 /* NOTE: this is endianness-sensitive. */
174 /* MDD is big-endian, its values need to be switched on little-endian systems. */
178 }
179#else
180 /* no blending */
181 if (!fread(vertexCos, sizeof(float[3]), mdd_head.verts_tot, f)) {
182 *r_err_str = errno ? strerror(errno) : RPT_("Failed to read frame");
183 return false;
184 }
185 /* NOTE: this is endianness-sensitive. */
186 /* MDD is big-endian, its values need to be switched on little-endian systems. */
187 BLI_endian_switch_float_array(vertexCos[0], mdd_head.verts_tot * 3);
188#endif
189 }
190 else {
191 const float ifactor = 1.0f - factor;
192 float *vco = *vertexCos;
193 uint i;
194 for (i = mdd_head.verts_tot; i != 0; i--, vco += 3) {
195 float tvec[3];
196 verts_read_num += fread(tvec, sizeof(float[3]), 1, fp);
197
198 /* NOTE: this is endianness-sensitive. */
199 /* MDD is big-endian, its values need to be switched on little-endian systems. */
200 BLI_endian_switch_float(tvec + 0);
201 BLI_endian_switch_float(tvec + 1);
202 BLI_endian_switch_float(tvec + 2);
203
204 vco[0] = (vco[0] * ifactor) + (tvec[0] * factor);
205 vco[1] = (vco[1] * ifactor) + (tvec[1] * factor);
206 vco[2] = (vco[2] * ifactor) + (tvec[2] * factor);
207 }
208 }
209
210 if (verts_read_num != mdd_head.verts_tot) {
211 *r_err_str = errno ? strerror(errno) : RPT_("Vertex coordinate read failed");
212 return false;
213 }
214
215 return true;
216}
217
219 float (*vertexCos)[3],
220 const int verts_tot,
221 const char interp,
222 const float frame,
223 const char **r_err_str)
224{
225 int index_range[2];
226 float factor;
227
229 verts_tot,
230 frame,
231 interp,
232 index_range,
233 &factor, /* read into these values */
234 r_err_str) == false)
235 {
236 return false;
237 }
238
239 if (index_range[0] == index_range[1]) {
240 /* read single */
241 if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
242 MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, r_err_str))
243 {
244 return true;
245 }
246
247 return false;
248 }
249
250 /* read both and interpolate */
251 if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
252 MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, r_err_str) &&
253 (BLI_fseek(fp, 0, SEEK_SET) == 0) &&
254 MOD_meshcache_read_mdd_index(fp, vertexCos, verts_tot, index_range[1], factor, r_err_str))
255 {
256 return true;
257 }
258
259 return false;
260}
261
262bool MOD_meshcache_read_mdd_times(const char *filepath,
263 float (*vertexCos)[3],
264 const int verts_tot,
265 const char interp,
266 const float time,
267 const float fps,
268 const char time_mode,
269 const char **r_err_str)
270{
271 float frame;
272
273 FILE *fp = BLI_fopen(filepath, "rb");
274 bool ok;
275
276 if (fp == nullptr) {
277 *r_err_str = errno ? strerror(errno) : RPT_("Unknown error opening file");
278 return false;
279 }
280
281 switch (time_mode) {
283 frame = time;
284 break;
285 }
287 /* we need to find the closest time */
288 if (meshcache_read_mdd_range_from_time(fp, verts_tot, time, fps, &frame, r_err_str) == false)
289 {
290 fclose(fp);
291 return false;
292 }
293 rewind(fp);
294 break;
295 }
297 default: {
298 MDDHead mdd_head;
299 if (meshcache_read_mdd_head(fp, verts_tot, &mdd_head, r_err_str) == false) {
300 fclose(fp);
301 return false;
302 }
303
304 frame = std::clamp(time, 0.0f, 1.0f) * float(mdd_head.frame_tot);
305 rewind(fp);
306 break;
307 }
308 }
309
310 ok = MOD_meshcache_read_mdd_frame(fp, vertexCos, verts_tot, interp, frame, r_err_str);
311
312 fclose(fp);
313 return ok;
314}
315
316} // namespace blender
File and directory operations.
Compatibility-like things for windows.
#define RPT_(msgid)
#define FRAME_SNAP_EPS
#define FLT_MAX
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
BMesh Iterators.
unsigned int uint
Definition BKE_text.h:55
BLI_INLINE void BLI_endian_switch_float(float *val) ATTR_NONNULL(1)
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
static bool meshcache_read_mdd_range_from_time(FILE *fp, const int verts_tot, const float time, const float, float *r_frame, const char **r_err_str)
static bool meshcache_read_mdd_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)
int BLI_fseek(FILE *stream, int64_t offset, int whence)
Definition storage.cc:201
void MOD_meshcache_calc_range(const float frame, const char interp, const int frame_tot, int r_index_range[2], float *r_factor)
bool MOD_meshcache_read_mdd_index(FILE *fp, float(*vertexCos)[3], const int verts_tot, const int index, const float factor, const char **r_err_str)
bool MOD_meshcache_read_mdd_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 BLI_endian_switch_int32_array(int *val, int size) ATTR_NONNULL(1)
static bool meshcache_read_mdd_head(FILE *fp, const int verts_tot, MDDHead *mdd_head, const char **r_err_str)
bool MOD_meshcache_read_mdd_frame(FILE *fp, float(*vertexCos)[3], const int verts_tot, const char interp, const float frame, const char **r_err_str)
void BLI_endian_switch_float_array(float *val, int size) ATTR_NONNULL(1)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float