-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapmodel_off.cpp
More file actions
352 lines (289 loc) · 9.72 KB
/
capmodel_off.cpp
File metadata and controls
352 lines (289 loc) · 9.72 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
*
* Copyright (c) 2018 - 2019 Mateusz 'mteg' Golicz
*
* Distributed under Apache License version 2.0
*
*/
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "capengine.h"
#include "capdebug.h"
void capModel::trim(char *line)
{
char *eol;
if((eol = strchr(line, '\r'))) *eol = 0;
if((eol = strchr(line, '\n'))) *eol = 0;
}
int capModel::saveOff(const char *filename)
{
FILE *fh = fopen(filename, "w");
const char *hdr = "OFF";
int *newIndexes, index = 0;
unsigned int i, nnv = 0, nnt = 0;
if(!fh)
return capDebug::error("Cannot open file: %s", filename);
capDebug::report(1, "Saving model as %s...", filename);
newIndexes = (int*) malloc(sizeof(int) * this->nv);
if((this->flags & CAMF_NORMALS) && (this->flags & CAMF_COLOR))
hdr = "NCOFF";
else if(this->flags & CAMF_NORMALS)
hdr = "NOFF";
else if(this->flags & CAMF_COLOR)
hdr = "COFF";
for(i = 0; i<this->nv; i++)
if(!CA_HASFLAG(i, CAV_DELETED)) nnv++;
for(i = 0; i<this->nt; i++)
if(!(
CA_HASFLAG(this->t[i].a, CAV_DELETED) ||
CA_HASFLAG(this->t[i].b, CAV_DELETED) ||
CA_HASFLAG(this->t[i].c, CAV_DELETED))) nnt++;
fprintf(fh, "%s\n", hdr);
fprintf(fh, "%d %d 0\n", nnv, nnt);
for(i = 0; i<this->nv; i++)
{
if(CA_HASFLAG(i, CAV_DELETED)) continue;
fprintf(fh, "%.6f %.6f %.6f", this->v[i].x, this->v[i].y, this->v[i].z);
if(this->flags & CAMF_COLOR)
fprintf(fh, " %d %d %d", this->v[i].r, this->v[i].g, this->v[i].b);
if(this->flags & CAMF_NORMALS)
fprintf(fh, " %.6f %.6f %.6f", this->v[i].nx, this->v[i].ny, this->v[i].nz);
fputs("\n", fh);
newIndexes[i] = index++;
}
for(i = 0; i<this->nt; i++)
{
unsigned int a = this->t[i].a, b = this->t[i].b, c = this->t[i].c;
if(CA_HASFLAG(a, CAV_DELETED)) continue;
if(CA_HASFLAG(b, CAV_DELETED)) continue;
if(CA_HASFLAG(c, CAV_DELETED)) continue;
fprintf(fh, "3 %d %d %d\n", newIndexes[a], newIndexes[b], newIndexes[c]);
}
fclose(fh);
free(newIndexes);
capDebug::report(1, "Saved %d vertices & %d triangles...", nnv, nnt);
return 1;
}
int capModel::saveSelectionOff(const char *filename)
{
unsigned char *oldFlags;
unsigned int i;
int q;
oldFlags = (unsigned char*) malloc(this->nv);
for(i = 0; i<this->nv; i++)
{
oldFlags[i] = this->vf[i];
this->vf[i] = (unsigned char) (CA_HASFLAG(i, CAV_MARKED) ? 0 : CAV_DELETED);
}
q = saveOff(filename);
for(i = 0; i<this->nv; i++)
this->vf[i] = oldFlags[i];
free(oldFlags);
return q;
}
int capModel::loadOff(const char *filename, int append, capColor *rgba)
{
FILE *fh;
#define OFFLINE_MAX 512
char line[OFFLINE_MAX + 1];
unsigned int nf, nv, voffs = 0, foffs = 0;
int vmax = 3, coffs = 255, noffs = 255;
unsigned int i;
append = this->loadStart(filename, append);
if(!(fh = utf8open(this->fullpath, "r")))
return capDebug::error("Cannot open file: %s", this->filename);
/* Read header */
if(!fgets(line, OFFLINE_MAX, fh))
{
fclose(fh);
return capDebug::error("No header in file: %s", this->filename);
}
/* Reset normals/color flags */
this->flags &= ~(CAMF_COLOR | CAMF_NORMALS);
/* Test header */
this->trim(line);
if((!strcmp(line, "COFF")) || (!strcmp(line, "NCOFF")))
this->flags |= CAMF_COLOR;
else if((!strcmp(line, "OFF")) || (!strcmp(line, "NOFF")))
this->flags &= ~CAMF_COLOR;
else
{
fclose(fh);
return capDebug::error("Wrong (C)OFF header in file: %s", this->filename);
}
if((!strcmp(line, "NCOFF")) || (!strcmp(line, "NOFF")))
this->flags |= CAMF_NORMALS;
if(this->flags & CAMF_NORMALS)
{
capDebug::report(1, "Will read vertex normals from this file");
vmax += 3;
noffs = 3;
}
if(this->flags & CAMF_COLOR)
{
capDebug::report(1, "Will read vertex colors from this file");
vmax += 3;
coffs = 3;
noffs += 3;
}
/* Read the number of points & triangles */
if(!fgets(line, OFFLINE_MAX, fh))
{
fclose(fh);
return capDebug::error("No information on number of points/faces in file: %s", this->filename);
}
trim(line);
if(sscanf(line, "%u %u ", &nv, &nf) != 2)
{
fclose(fh);
return capDebug::error("No information on number of points/faces in file: %s", this->filename);
}
if(append)
{
voffs = this->nv;
foffs = this->nt;
free(this->ep); free(this->e);
this->e = NULL; this->ep = NULL;
}
else
{
capDebug::report(1, "Deleting old model data");
this->unload();
}
/* Save info into the model struct & allocate data */
this->reallocBuffers(nv + voffs, nf + foffs);
capDebug::report(1, "%s: Reading %d vertexes", this->filename, nv);
for(i = 0; i<nv; i++)
{
float pos[3], normal[3];
unsigned int color[3];
char *eptr, *buf;
int v;
if(!fgets(line, OFFLINE_MAX, fh))
{
fclose(fh);
this->unload();
return capDebug::error("Premature end of file '%s', while reading vertex %d of %d", this->filename, i, nv);
}
buf = line;
for(v = 0; v<vmax; v++)
{
if(v >= noffs)
normal[v - noffs] = strtof(buf, &eptr);
else if(v >= coffs)
color[v - coffs] = (unsigned int) strtoul(buf, &eptr, 0);
else
pos[v] = strtof(buf, &eptr);
if(eptr == buf)
{
fclose(fh);
this->unload();
return capDebug::error("Premature end of line in '%s', while reading vertex %d of %d", this->filename, i, nv);
}
buf = eptr;
}
this->vf[i + voffs] = 0;
this->v[i + voffs].x = pos[0]; this->v[i + voffs].y = pos[1]; this->v[i + voffs].z = pos[2];
this->v[i + voffs].isUsed = 0;
if(this->flags & CAMF_COLOR)
{
this->v[i + voffs].r = (unsigned char) color[0];
this->v[i + voffs].g = (unsigned char) color[1];
this->v[i + voffs].b = (unsigned char) color[2];
}
if(this->flags & CAMF_NORMALS)
{
this->v[i + voffs].nx = normal[0];
this->v[i + voffs].ny = normal[1];
this->v[i + voffs].nz = normal[2];
}
}
this->recolor(rgba, voffs);
capDebug::report(2, "Loading %d faces...", nf);
for(i = 0; i<nf; i++)
{
char *eptr, *buf;
int v;
unsigned int triangle[4];
if(!fgets(line, OFFLINE_MAX, fh))
{
fclose(fh);
this->unload();
return capDebug::error( "Premature end of file in '%s', while reading face %d of %d", this->filename, i, nf);
}
buf = line;
for(v = 0; v<4; v++)
{
triangle[v] = (unsigned int) strtoul(buf, &eptr, 0);
if(eptr == buf)
{
fclose(fh);
this->unload();
return capDebug::error( "Premature end of line in '%s', while reading face %d of %d", this->filename, i, nf);
}
else if((v > 0 && (triangle[v] < 0 || triangle[v] >= nv)) || (v == 0 && triangle[v] != 3))
{
fclose(fh);
this->unload();
return capDebug::error( "Premature end of line in '%s', while reading face %d of %d", this->filename, i, nf);
}
buf = eptr;
}
this->t[i + foffs].a = triangle[1] + voffs;
this->t[i + foffs].b = triangle[2] + voffs;
this->t[i + foffs].c = triangle[3] + voffs;
}
fclose(fh);
capDebug::report(1, "All data loaded.");
return loadEnd();
}
void capModel::reallocBuffers(unsigned int nnv, unsigned int nnt) {
size_t lsz;
this->nv = nnv; this->nt = nnt;
free(this->ep); free(this->e);
this->e = NULL; this->ep = NULL;
capDebug::assert(this->v = (struct capVertex*) realloc(this->v, lsz = sizeof(struct capVertex)*this->nv));
capDebug::report(1, "Allocated vertex buffer: %d MB", lsz / 1024 / 1024);
if(this->nt)
{
capDebug::assert((this->t = (struct capTriangle*) realloc(this->t, lsz = sizeof(struct capTriangle)*this->nt)));
capDebug::report(1, "Allocated triangle buffer: %d MB", lsz / 1024 / 1024);
}
else
this->t = NULL;
capDebug::assert((this->vf = (unsigned char*) realloc(this->vf, this->nv)));
}
int capModel::loadStart(const char* filename, int append)
{
if(!append) {
free(this->fullpath);
this->fullpath = strdup(filename);
this->filename = basename(this->fullpath);
}
if(append)
if(!(this->flags & CAMF_LOADED))
append = 0;
return append;
}
int capModel::loadEnd() {
if((nt == 0) && !(this->flags & CAMF_POINTS))
this->flags |= CAMF_POINTS;
if(nt && !(this->flags & (CAMF_EDGES | CAMF_FACES)))
this->flags |= CAMF_FACES;
this->flags |= CAMF_LOADED;
this->renderer->reinitModel(this);
return 1;
}
int capModel::loadFile(const char *filename, int append, capColor *rgba) {
if(strlen(filename) >= 4) {
if (!strcasecmp(filename + strlen(filename) - 4, ".ply"))
return loadPly(filename, append, rgba);
if (!strcasecmp(filename + strlen(filename) - 4, ".nvm"))
return loadNvm(filename, append, rgba);
if (!strcasecmp(filename + strlen(filename) - 3, ".3d"))
return load3d(filename, append, rgba);
}
return loadOff(filename, append, rgba);
}