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