-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilitiesFlow.h
138 lines (114 loc) · 3.6 KB
/
UtilitiesFlow.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
#ifndef _UTILITIES_FLOW_H_
#define _UTILITIES_FLOW_H_
#include <Eigen\Dense>
#include "MeshFlow.h"
// Convert Triangle openmesh data structure to Eigen matrices
namespace OpenMesh {
inline void LoadMeshDataToMatrix(OpenMesh::MeshFlow* Mesh, Eigen::MatrixXd& V, Eigen::MatrixXd& VN, Eigen::MatrixXi& F, Eigen::MatrixXd& FN)
{
int nv = Mesh->n_vertices();
int nf = Mesh->n_faces();
V.resize(nv, 3);
VN.resize(nv, 3);
F.resize(nf, 3);
FN.resize(nf, 3);
Mesh->update_normals();
/*Reindex the idx*/
int idx = 0;
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter) {
OpenMesh::VertexHandle v = *viter;
Mesh->Idx(v) = idx;
idx++;
}
/*load vertex data*/
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter) {
OpenMesh::VertexHandle v = *viter;
int idx = Mesh->Idx(v);
OpenMesh::Vec3d point = Mesh->point(v);
OpenMesh::Vec3d normal = Mesh->normal(v);
for (int i = 0; i < 3; i++) {
V(idx, i) = point[i];
VN(idx, i) = normal[i];
}
}
int fidx = 0;
for (OpenMesh::MeshFlow::FaceIter fiter = Mesh->faces_begin(); fiter != Mesh->faces_end(); ++fiter) {
OpenMesh::FaceHandle f = *fiter;
int i = 0;
for (OpenMesh::MeshFlow::FaceVertexIter fviter = Mesh->fv_iter(f); fviter.is_valid(); ++fviter) {
OpenMesh::VertexHandle v = *fviter;
F(fidx, i) = Mesh->Idx(v);
i++;
}
OpenMesh::Vec3d normal = Mesh->normal(f);
for (int j = 0; j < 3; j++) {
FN(fidx, j) = normal[j];
}
fidx++;
}
}
inline void LoadMeshFlatMappingResultToMatrix(OpenMesh::MeshFlow* Mesh, Eigen::MatrixXd& UV)
{
int nv = Mesh->n_vertices();
UV.resize(nv, 2);
/*load vertex data*/
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter) {
OpenMesh::VertexHandle v = *viter;
int idx = Mesh->Idx(v);
OpenMesh::Vec2d uv = Mesh->texcoord2D(v);
for (int i = 0; i < 2; i++) {
UV(idx, i) = uv[i];
}
}
}
inline void LoadMeshSphericalMappingResultToMatrix(OpenMesh::MeshFlow* Mesh, Eigen::MatrixXd& V)
{
int nv = Mesh->n_vertices();
V.resize(nv, 3);
/*load vertex data*/
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter) {
OpenMesh::VertexHandle v = *viter;
int idx = Mesh->Idx(v);
OpenMesh::Vec3d point = Mesh->texcoord3D(v);
for (int i = 0; i < 3; i++) {
V(idx, i) = point[i];
}
}
}
inline void NormalizeMesh(OpenMesh::MeshFlow* Mesh)
{
OpenMesh::MeshFlow::Point s(0, 0, 0);
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter)
{
OpenMesh::VertexHandle v = *viter;
s = s + Mesh->point(v);
}
s = s / Mesh->n_vertices();
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter)
{
OpenMesh::VertexHandle v = *viter;
OpenMesh::MeshFlow::Point p = Mesh->point(v);
p = p - s;
Mesh->set_point(v, p);
}
double d = 0;
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter)
{
OpenMesh::VertexHandle v = *viter;
OpenMesh::MeshFlow::Point p = Mesh->point(v);
for (int k = 0; k < 3; k++)
{
d = (d > fabs(p[k])) ? d : fabs(p[k]);
}
}
for (OpenMesh::MeshFlow::VertexIter viter = Mesh->vertices_begin(); viter != Mesh->vertices_end(); ++viter)
{
OpenMesh::VertexHandle v = *viter;
OpenMesh::MeshFlow::Point p = Mesh->point(v);
p = p / d;
Mesh->set_point(v, p);
}
}
}
#endif // !_Mesh_IO_H_
#pragma once