~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/modifiers/intern/MOD_meshcache_pc2.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

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