/
TriMesh.h
executable file
·166 lines (141 loc) · 3.66 KB
/
TriMesh.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
#ifndef TRIMESH_H
#define TRIMESH_H
/*
Szymon Rusinkiewicz
Princeton University
TriMesh.h
Class for triangle meshes.
*/
#include "Vec.h"
#include "Color.h"
#include <vector>
#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
class TriMesh {
protected:
static bool read_helper(const char *filename, TriMesh *mesh);
public:
// Types
struct Face {
int v[3];
Face() {}
Face(const int &v0, const int &v1, const int &v2)
{ v[0] = v0; v[1] = v1; v[2] = v2; }
Face(const int *v_)
{ v[0] = v_[0]; v[1] = v_[1]; v[2] = v_[2]; }
int &operator[] (int i) { return v[i]; }
const int &operator[] (int i) const { return v[i]; }
operator const int * () const { return &(v[0]); }
operator const int * () { return &(v[0]); }
operator int * () { return &(v[0]); }
int indexof(int v_) const
{
return (v[0] == v_) ? 0 :
(v[1] == v_) ? 1 :
(v[2] == v_) ? 2 : -1;
}
};
struct BBox {
point min, max;
point center() const { return 0.5f * (min+max); }
vec size() const { return max - min; }
bool valid;
BBox() : valid(false)
{}
};
struct BSphere {
point center;
float r;
bool valid;
BSphere() : valid(false)
{}
};
// Enums
enum tstrip_rep { TSTRIP_LENGTH, TSTRIP_TERM };
enum { GRID_INVALID = -1 };
// The basics: vertices and faces
std::vector<point> vertices;
std::vector<Face> faces;
// Triangle strips
std::vector<int> tstrips;
// Grid, if present
std::vector<int> grid;
int grid_width, grid_height;
// Other per-vertex properties
std::vector<Color> colors;
std::vector<float> confidences;
std::vector<unsigned> flags;
unsigned flag_curr;
// Computed per-vertex properties
std::vector<vec> normals;
std::vector<vec> pdir1, pdir2;
std::vector<float> curv1, curv2;
std::vector< Vec<4,float> > dcurv;
std::vector<vec> cornerareas;
std::vector<float> pointareas;
// Bounding structures
BBox bbox;
BSphere bsphere;
// Connectivity structures:
// For each vertex, all neighboring vertices
std::vector< std::vector<int> > neighbors;
// For each vertex, all neighboring faces
std::vector< std::vector<int> > adjacentfaces;
// For each face, the three faces attached to its edges
// (for example, across_edge[3][2] is the number of the face
// that's touching the edge opposite vertex 2 of face 3)
std::vector<Face> across_edge;
// Compute all this stuff...
void need_tstrips();
void convert_strips(tstrip_rep rep);
void unpack_tstrips();
void triangulate_grid();
void need_faces()
{
if (!faces.empty())
return;
if (!tstrips.empty())
unpack_tstrips();
else if (!grid.empty())
triangulate_grid();
}
void need_normals();
void need_pointareas();
void need_curvatures();
void need_dcurv();
void need_bbox();
void need_bsphere();
void need_neighbors();
void need_adjacentfaces();
void need_across_edge();
// Input and output
static TriMesh *read(const char *filename);
void write(const char *filename);
// Statistics
// XXX - Add stuff here
float feature_size();
// Useful queries
// XXX - Add stuff here
bool is_bdy(int v)
{
if (neighbors.empty()) need_neighbors();
if (adjacentfaces.empty()) need_adjacentfaces();
return neighbors[v].size() != adjacentfaces[v].size();
}
vec trinorm(int f)
{
if (faces.empty()) need_faces();
return ::trinorm(vertices[faces[f][0]], vertices[faces[f][1]],
vertices[faces[f][2]]);
}
// Debugging printout, controllable by a "verbose"ness parameter
static int verbose;
static void set_verbose(int);
static int dprintf(const char *format, ...);
// Constructor
TriMesh() : grid_width(-1), grid_height(-1), flag_curr(0)
{}
};
bool PolyDataToTriMesh(vtkSmartPointer<vtkPolyData> polydata, TriMesh *mesh);
#endif