-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcapmodel.h
312 lines (222 loc) · 6.35 KB
/
capmodel.h
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
//
// Created by mteg on 5/6/18.
//
#ifndef CAPOC2_CAPMODEL_H
#define CAPOC2_CAPMODEL_H
#include <map>
class capModel;
#include "capaffine.h"
#include "capnvm.h"
#include "caprenderer.h"
#include <vector>
#include <cctype>
#include <cmath>
#define CA_HASFLAG(i, flag) (this->vf[i] & (flag))
#define CA_HASFLAG_A(vf, i, flag) ((vf)[i] & (flag))
#define CA_FLAG(i, flag) this->vf[i] |= (flag)
#define CA_FLAG_A(vf, i, flag) vf[i] |= (flag)
#define CA_UNFLAG(i, flag) this->vf[i] &= ~(flag)
#define CA_UNFLAG_A(vf, i, flag) vf[i] &= ~(flag)
struct capVertex
{
/* Vertex coordinates */
float x, y, z;
/* Normal vector */
float nx, ny, nz;
/* Vertex color */
unsigned char r, g, b;
/* Are there any faces using this vertex */
unsigned char isUsed;
};
struct capEdge
{
/* Vertex indices */
unsigned int from, to;
};
struct capEdgePointer
{
/* First edge from this vertex */
unsigned int edgeStart;
/* Number of edges going from this vertex */
unsigned int edgeLen;
};
struct capTriangle
{
/* Vertex indices */
unsigned int a, b, c;
/* Normal of this triangle */
float nx, ny, nz;
};
static inline void vAdd(struct capVertex *v, struct capVertex *n)
{
v->nx += n->nx;
v->ny += n->ny;
v->nz += n->nz;
}
static inline void vNormalize(struct capVertex *n)
{
float l;
if((l = sqrtf(n->nx * n->nx + n->ny * n->ny + n->nz * n->nz)) != 0.0f)
{
n->nx /= l;
n->ny /= l;
n->nz /= l;
}
else
{
n->nx = n->ny = n->nz = 0;
}
}
inline static char *vPopstring(char *in, char **str)
{
if(!in) return NULL;
while(isblank(*in)) in++;
if(!*in) return NULL;
*str = in;
while((!isblank(*in)) && *in) in++;
if(*in)
{
*in = 0;
in++;
return in;
}
else
return in;
}
inline static char *vPopfloat(char *in, float *out)
{
char *str, *eptr;
if(!(in = vPopstring(in, &str)))
return NULL;
*out = strtof(str, &eptr);
if(*eptr) return NULL;
return in;
}
inline static char *vPopint(char *in, int *out)
{
char *str, *eptr;
if(!(in = vPopstring(in, &str)))
return NULL;
*out = (int) strtol(str, &eptr, 0);
if(*eptr) return NULL;
return in;
}
inline static char *vPopbyte(char *in, unsigned char *out)
{
int s;
char * res = vPopint(in, &s);
*out = s;
return res;
}
struct capModel_ball
{
float radius;
float x0, y0, z0;
};
struct capModel_plane
{
float cx, cy, cz;
float nx, ny, nz;
float **m;
float radius;
float dip, dipDir;
};
class capModel {
public:
explicit capModel(capRenderer *renderer);
/* Name of the file and full path this model was loaded from */
char *filename = NULL;
char *fullpath = NULL;
/* Transform bringing this model to unified world coordinates */
capAffineMatrix affineMatrix;
/* Transform bringing this model to camera coordinates */
capAffineMatrix modelviewMatrix;
/* Number of vertices, triangles and edges */
unsigned int nv = 0, nt = 0, ne = 0;
/* Vertices and triangles of this model */
struct capVertex *v = NULL;
struct capTriangle *t = NULL;
/* Vertex flags */
unsigned char *vf = NULL;
#define CAV_MARKED 1
#define CAV_DELETED 2
/* Edges are ordered by 'from'. */
struct capEdge *e = NULL;
/* Edge pointers help listing all neighbors of a particular vertex */
struct capEdgePointer *ep = NULL;
/* If NVM was loaded for this model, here it is! */
capNvm *nvm = NULL;
/* General flags of this model */
#define CAMF_LOADED 1
#define CAMF_POINTS 2
#define CAMF_FACES 4
#define CAMF_ACTIVE 8
#define CAMF_COLOR 16
#define CAMF_EDGES 32
#define CAMF_NORMALS 64
unsigned int flags = 0;
/* Named points of this model */
std::map <std::string, capGenericPoint> savedPoints;
capRenderer *renderer;
capActivePoint cursor;
void *rendererData = NULL;
#define CA_DEC_MAX 16
unsigned int decRate = 0, decSeed = 0; /* decimation rate and decimation seed */
capModel_plane fitPlane;
bool hasPlane = false;
static void trim(char *line);
int saveOff(const char *filename);
int saveSelectionOff(const char *filename);
int loadOff(const char *filename, int append, capColor *rgba);
int loadPly(const char *filename, int append, capColor *rgba);
int loadNvm(const char *filename, int append, capColor *rgba);
int loadFile(const char *filename, int append, capColor *rgba);
int load3d(const char *filename, int append, capColor *rgba);
int readNvm(const char *filename, unsigned int modelId = 0);
void unload();
float getMarkedArea();
float getMarkedArea(unsigned char *flagField);
void walkEdge(int steps);
void ensureEdges();
unsigned char *getField();
unsigned char *getBoundary(const unsigned char *mv);
void selectBoundary();
void expandSelection(float radius, unsigned char *mv);
void subtractSelections(unsigned char *fa, const unsigned char *fb);
unsigned char *popComponent(unsigned char *mv);
void popComponent();
void dumpComponents();
void dumpComponents(const char *filename);
void growSelection(float radius);
unsigned int ballSelect(float radius, float x0, float y0, float z0);
unsigned int ballSelect(capModel_ball b);
capModel_ball ballViewToModel(capModel_ball vb);
bool fitPlaneToSelection();
void dropPlane();
void resetSelection();
void computeNormals();
void reedge();
void recolor(capColor *rgba);
void deleteSelected();
#define CAC_GRAY_UNCOLORABLE 1
#define CAC_DELETE_UNCOLORABLE 2
#define CAC_UNDELETE_COLORABLE 4
int recolorFrom(const char *filename, int flags);
void findTransformTo(capModel *destModel, bool b);
void undeleteAll();
void updateDisplay();
void scale(float sz);
void rotate(float degAngle, int axis);
void translate(float tx, float ty, float tz);
void setPoint(char* name, capGenericPoint p);
void setPoint(char* name, float x, float y, float z);
void deletePoint(char* name);
void seePoint(char* name, capCamera *view, float step = 5.0f);
void selectBehind();
private:
int loadStart(const char *filename, int append);
void reallocBuffers(unsigned int i, unsigned int i1);
void recolor(capColor *pColor, unsigned int i);
int loadEnd();
};
#endif //CAPOC2_CAPMODEL_H