Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MOD_meshcache_pc2.c « intern « modifiers « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 54ea27d00858f99308370339ab0701b789b3e4d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup modifiers
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "BLI_utildefines.h"

#include "BLI_fileops.h"
#ifdef __BIG_ENDIAN__
#  include "BLI_endian_switch.h"
#endif

#ifdef WIN32
#  include "BLI_winstuff.h"
#endif

#include "DNA_modifier_types.h"

#include "MOD_meshcache_util.h" /* own include */

typedef struct PC2Head {
  char header[12];  /* 'POINTCACHE2\0' */
  int file_version; /* unused - should be 1 */
  int verts_tot;
  float start;
  float sampling;
  int frame_tot;
} PC2Head; /* frames, verts */

static bool meshcache_read_pc2_head(FILE *fp,
                                    const int verts_tot,
                                    PC2Head *pc2_head,
                                    const char **err_str)
{
  if (!fread(pc2_head, sizeof(*pc2_head), 1, fp)) {
    *err_str = "Missing header";
    return false;
  }

  if (!STREQ(pc2_head->header, "POINTCACHE2")) {
    *err_str = "Invalid header";
    return false;
  }

#ifdef __BIG_ENDIAN__
  BLI_endian_switch_int32_array(&pc2_head->file_version,
                                (sizeof(*pc2_head) - sizeof(pc2_head->header)) / sizeof(int));
#endif

  if (pc2_head->verts_tot != verts_tot) {
    *err_str = "Vertex count mismatch";
    return false;
  }

  if (pc2_head->frame_tot <= 0) {
    *err_str = "Invalid frame total";
    return false;
  }
  /* Intentionally don't seek back. */

  return true;
}

/**
 * Gets the index range and factor
 *
 * currently same as for MDD
 */
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)
{
  PC2Head pc2_head;

  /* first check interpolation and get the vert locations */

  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
    return false;
  }

  MOD_meshcache_calc_range(frame, interp, pc2_head.frame_tot, r_index_range, r_factor);

  return true;
}

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)
{
  PC2Head pc2_head;
  float frame;

  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
    return false;
  }

  frame = ((time / fps) - pc2_head.start) / pc2_head.sampling;

  if (frame >= pc2_head.frame_tot) {
    frame = (float)(pc2_head.frame_tot - 1);
  }
  else if (frame < 0.0f) {
    frame = 0.0f;
  }

  *r_frame = frame;
  return true;
}

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)
{
  PC2Head pc2_head;

  if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
    return false;
  }

  if (BLI_fseek(fp, sizeof(float[3]) * index * pc2_head.verts_tot, SEEK_CUR) != 0) {
    *err_str = "Failed to seek frame";
    return false;
  }

  size_t num_verts_read = 0;
  errno = 0;
  if (factor >= 1.0f) {
    float *vco = *vertexCos;
    uint i;
    for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
      num_verts_read += fread(vco, sizeof(float[3]), 1, fp);

#ifdef __BIG_ENDIAN__
      BLI_endian_switch_float(vco + 0);
      BLI_endian_switch_float(vco + 1);
      BLI_endian_switch_float(vco + 2);
#endif /* __BIG_ENDIAN__ */
    }
  }
  else {
    const float ifactor = 1.0f - factor;
    float *vco = *vertexCos;
    uint i;
    for (i = pc2_head.verts_tot; i != 0; i--, vco += 3) {
      float tvec[3];
      num_verts_read += fread(tvec, sizeof(float[3]), 1, fp);

#ifdef __BIG_ENDIAN__
      BLI_endian_switch_float(tvec + 0);
      BLI_endian_switch_float(tvec + 1);
      BLI_endian_switch_float(tvec + 2);
#endif /* __BIG_ENDIAN__ */

      vco[0] = (vco[0] * ifactor) + (tvec[0] * factor);
      vco[1] = (vco[1] * ifactor) + (tvec[1] * factor);
      vco[2] = (vco[2] * ifactor) + (tvec[2] * factor);
    }
  }

  if (num_verts_read != pc2_head.verts_tot) {
    *err_str = errno ? strerror(errno) : "Vertex coordinate read failed";
    return false;
  }

  return true;
}

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)
{
  int index_range[2];
  float factor;

  if (meshcache_read_pc2_range(fp,
                               verts_tot,
                               frame,
                               interp,
                               index_range,
                               &factor, /* read into these values */
                               err_str) == false) {
    return false;
  }

  if (index_range[0] == index_range[1]) {
    /* read single */
    if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
        MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str)) {
      return true;
    }

    return false;
  }

  /* read both and interpolate */
  if ((BLI_fseek(fp, 0, SEEK_SET) == 0) &&
      MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[0], 1.0f, err_str) &&
      (BLI_fseek(fp, 0, SEEK_SET) == 0) &&
      MOD_meshcache_read_pc2_index(fp, vertexCos, verts_tot, index_range[1], factor, err_str)) {
    return true;
  }

  return false;
}

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)
{
  float frame;

  FILE *fp = BLI_fopen(filepath, "rb");
  bool ok;

  if (fp == NULL) {
    *err_str = errno ? strerror(errno) : "Unknown error opening file";
    return false;
  }

  switch (time_mode) {
    case MOD_MESHCACHE_TIME_FRAME: {
      frame = time;
      break;
    }
    case MOD_MESHCACHE_TIME_SECONDS: {
      /* we need to find the closest time */
      if (meshcache_read_pc2_range_from_time(fp, verts_tot, time, fps, &frame, err_str) == false) {
        fclose(fp);
        return false;
      }
      rewind(fp);
      break;
    }
    case MOD_MESHCACHE_TIME_FACTOR:
    default: {
      PC2Head pc2_head;
      if (meshcache_read_pc2_head(fp, verts_tot, &pc2_head, err_str) == false) {
        fclose(fp);
        return false;
      }

      frame = CLAMPIS(time, 0.0f, 1.0f) * (float)pc2_head.frame_tot;
      rewind(fp);
      break;
    }
  }

  ok = MOD_meshcache_read_pc2_frame(fp, vertexCos, verts_tot, interp, frame, err_str);

  fclose(fp);
  return ok;
}