forked from salvipeter/geo-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cc
167 lines (146 loc) · 4.7 KB
/
object.cc
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
#include "object.hh"
#ifdef USE_JET_FITTING
#include "jet-wrapper.hh"
#endif
Object::Object(std::string filename) : filename(filename) {
}
Object::~Object() {
}
const BaseMesh &Object::baseMesh() const {
return mesh;
}
void Object::draw(const Visualization &vis) const {
glPolygonMode(GL_FRONT_AND_BACK,
!vis.show_solid && vis.show_wireframe ? GL_LINE : GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1, 1);
if (vis.show_solid || vis.show_wireframe) {
if (vis.type == VisType::PLAIN)
glColor3d(1.0, 1.0, 1.0);
else if (vis.type == VisType::ISOPHOTES) {
glBindTexture(GL_TEXTURE_2D, vis.current_isophote_texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_2D);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
} else if (vis.type == VisType::SLICING) {
glBindTexture(GL_TEXTURE_1D, vis.slicing_texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_1D);
}
for (auto f : mesh.faces()) {
glBegin(GL_POLYGON);
for (auto v : f.vertices()) {
if (vis.type == VisType::MEAN)
glColor3dv(vis.colorMap(vis.mean_min, vis.mean_max, mesh.data(v).mean).data());
else if (vis.type == VisType::SLICING)
glTexCoord1d(mesh.point(v) | vis.slicing_dir * vis.slicing_scaling);
glNormal3dv(mesh.normal(v).data());
glVertex3dv(mesh.point(v).data());
}
glEnd();
}
if (vis.type == VisType::ISOPHOTES) {
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
} else if (vis.type == VisType::SLICING)
glDisable(GL_TEXTURE_1D);
}
if (vis.show_solid && vis.show_wireframe) {
glPolygonMode(GL_FRONT, GL_LINE);
glColor3d(0.0, 0.0, 0.0);
glDisable(GL_LIGHTING);
for (auto f : mesh.faces()) {
glBegin(GL_POLYGON);
for (auto v : f.vertices())
glVertex3dv(mesh.point(v).data());
glEnd();
}
glEnable(GL_LIGHTING);
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void Object::updateBaseMesh() {
updateBaseMesh(false, false);
}
void Object::updateBaseMesh(bool own_normal, bool own_mean) {
mesh.request_face_normals();
mesh.request_vertex_normals();
mesh.update_face_normals();
#ifdef USE_JET_FITTING
if (own_normal && own_mean) {
// no need for jet fitting
for (auto v : mesh.vertices()) {
mesh.set_normal(v, normal(v));
mesh.data(v).mean = meanCurvature(v);
}
return;
}
mesh.update_vertex_normals();
std::vector<Vector> points;
for (auto v : mesh.vertices())
points.push_back(mesh.point(v));
auto nearest = JetWrapper::Nearest(points, 20);
for (auto v : mesh.vertices()) {
auto jet = JetWrapper::fit(mesh.point(v), nearest, 2);
bool reverse = (mesh.normal(v) | jet.normal) > 0;
if (own_normal)
mesh.set_normal(v, normal(v));
else
mesh.set_normal(v, -jet.normal);
if (own_mean)
mesh.data(v).mean = meanCurvature(v);
else
mesh.data(v).mean = (jet.k_min + jet.k_max) / 2;
if (reverse && !own_normal)
mesh.set_normal(v, -mesh.normal(v));
if (reverse && !own_mean)
mesh.data(v).mean *= -1;
}
#else // !USE_JET_FITTING
std::ignore = std::tie(own_normal, own_mean);
for (auto v : mesh.vertices()) {
mesh.set_normal(v, normal(v));
mesh.data(v).mean = meanCurvature(v);
}
#endif // USE_JET_FITTING
}
Vector Object::normal(BaseMesh::VertexHandle vh) const {
auto v = OpenMesh::make_smart(vh, &mesh);
Vector n(0.0, 0.0, 0.0);
for (auto h : v.incoming_halfedges()) {
if (h.is_boundary())
continue;
auto in_vec = mesh.calc_edge_vector(h);
auto out_vec = mesh.calc_edge_vector(h.next());
double w = in_vec.sqrnorm() * out_vec.sqrnorm();
n += (in_vec % out_vec) / (w == 0.0 ? 1.0 : w);
}
double len = n.length();
if (len != 0.0)
n /= len;
return n;
}
double Object::meanCurvature(BaseMesh::VertexHandle vh) const {
auto v = OpenMesh::make_smart(vh, &mesh);
// Compute triangle strip area
double vertex_area = 0;
for (auto f : v.faces())
vertex_area += mesh.calc_sector_area(f.halfedge());
vertex_area /= 3.0;
// Compute mean values using dihedral angles
double mean = 0;
for (auto h : v.incoming_halfedges()) {
auto vec = mesh.calc_edge_vector(h);
double angle = mesh.calc_dihedral_angle(h); // signed; returns 0 at the boundary
mean += angle * vec.norm();
}
return mean / (4 * vertex_area);
}
bool Object::valid() const {
return mesh.n_vertices() > 0;
}