-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcapmodel_ply.cpp
135 lines (117 loc) · 4.89 KB
/
capmodel_ply.cpp
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
/*
*
* Copyright (c) 2019 Mateusz 'mteg' Golicz
*
* Distributed under Apache License version 2.0
*
*/
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stdint.h>
#include "ply_io.h"
#include "capengine.h"
#include "capdebug.h"
int capModel::loadPly(const char *filename, int append, capColor *rgba) {
FILE *fh;
unsigned int voffs = 0, foffs = 0;
append = this->loadStart(filename, append);
struct tempFace {
unsigned int num;
unsigned int *verts;
};
PlyProperty vert_props[] = { /* list of property information for a vertex */
{(char*) "x", Float32, Float32, (int) offsetof(capVertex,x), 0, 0, 0, 0},
{(char*) "y", Float32, Float32, (int) offsetof(capVertex,y), 0, 0, 0, 0},
{(char*) "z", Float32, Float32, (int) offsetof(capVertex,z), 0, 0, 0, 0},
{(char*) "red", Uint8, Uint8, (int) offsetof(capVertex,r), 0, 0, 0, 0},
{(char*) "green", Uint8, Uint8, (int) offsetof(capVertex,g), 0, 0, 0, 0},
{(char*) "blue", Uint8, Uint8, (int) offsetof(capVertex,b), 0, 0, 0, 0},
{(char*) "nx", Float32, Float32, (int) offsetof(capVertex,nx), 0, 0, 0, 0},
{(char*) "ny", Float32, Float32, (int) offsetof(capVertex,ny), 0, 0, 0, 0},
{(char*) "nz", Float32, Float32, (int) offsetof(capVertex,nz), 0, 0, 0, 0},
};
PlyProperty face_props[] = { /* list of property information for a face */
{(char*) "vertex_indices", Int32, Int32, (int) offsetof(tempFace,verts), 1, Uint32, Uint32, (int) offsetof(tempFace,num)},
};
PlyFile *ply;
if(!(fh = utf8open(this->fullpath, "rb")))
return capDebug::error("Cannot open file: %s", this->filename);
if (!(ply = read_ply(fh))) {
return capDebug::error("Unable to read PLY file: %s", this->filename);
}
if(append)
{
voffs = this->nv;
foffs = this->nt;
}
else
{
capDebug::report(1, "Deleting old model data");
this->unload();
}
this->flags &= ~(CAMF_COLOR | CAMF_NORMALS);
for(int e = 0; e<ply->num_elem_types; e++)
{
int ecount;
char *ename = setup_element_read_ply(ply, e, &ecount);
if(equal_strings((char*) "vertex", ename)) {
this->reallocBuffers(ecount + voffs, this->nt);
setup_property_ply(ply, &vert_props[0]);
setup_property_ply(ply, &vert_props[1]);
setup_property_ply(ply, &vert_props[2]);
for (int j = 0; j < ply->elems[e]->nprops; j++) {
PlyProperty *prop;
prop = ply->elems[e]->props[j];
if (equal_strings((char*) "r", prop->name) || equal_strings((char*) "red", prop->name)) {
setup_property_ply(ply, &vert_props[3]);
this->flags |= CAMF_COLOR;
} else if (equal_strings((char*) "g", prop->name) || equal_strings((char*) "green", prop->name))
setup_property_ply(ply, &vert_props[4]);
else if (equal_strings((char*) "b", prop->name) || equal_strings((char*) "blue", prop->name))
setup_property_ply(ply, &vert_props[5]);
else if (equal_strings((char*) "nx", prop->name)) {
setup_property_ply(ply, &vert_props[6]);
this->flags |= CAMF_NORMALS;
} else if (equal_strings((char*) "ny", prop->name))
setup_property_ply(ply, &vert_props[7]);
else if (equal_strings((char*) "nz", prop->name))
setup_property_ply(ply, &vert_props[8]);
}
for (int i = 0; i < ecount; i++) {
get_element_ply(ply, (void *) &v[i + voffs]);
vf[i + voffs] = 0;
}
this->recolor(rgba, voffs);
}
else if(equal_strings((char*) "face", ename)) {
int importedFaces = 0;
this->reallocBuffers(this->nv, ecount + foffs);
setup_property_ply(ply, &face_props[0]);
tempFace f;
memset(&t[foffs], 0, sizeof(struct capTriangle)*ecount);
for (int j = 0; j < ecount; j++) {
get_element_ply(ply, (void *) &f);
if (f.num == 3) {
unsigned int a = f.verts[0] + voffs, b = f.verts[1] + voffs, c = f.verts[2] + voffs;
if(a >= voffs && a < nv && b >= voffs && b < nv && c >= voffs && c < nv) {
t[j + foffs].a = a;
t[j + foffs].b = b;
t[j + foffs].c = c;
importedFaces++;
}
}
free(f.verts);
}
capDebug::report(0, "%d faces actually imported from PLY", importedFaces);
}
#if 0
else
get_other_element_ply(ply);
#endif
}
close_ply(ply);
free_ply(ply);
return this->loadEnd();
}