-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMesh.cpp
69 lines (54 loc) · 1.44 KB
/
Mesh.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
#include "Mesh.h"
#include "MeshIO.h"
Mesh::Mesh()
{
}
Mesh::Mesh(const Mesh& mesh)
{
*this = mesh;
}
bool Mesh::read(const std::string& fileName)
{
std::ifstream in(fileName.c_str());
if (!in.is_open()) {
std::cerr << "Error: Could not open file for reading" << std::endl;
return false;
}
bool readSuccessful = false;
if ((readSuccessful = MeshIO::read(in, *this))) {
normalize();
}
return readSuccessful;
}
bool Mesh::write(const std::string& fileName)
{
std::ofstream out(fileName.c_str());
if (!out.is_open()) {
std::cerr << "Error: Could not open file for writing" << std::endl;
return false;
}
MeshIO::write(out, *this);
return false;
}
void Mesh::normalize()
{
// compute center of mass
Eigen::Vector3d cm = Eigen::Vector3d::Zero();
for (VertexCIter v = vertices.begin(); v != vertices.end(); v++) {
cm += v->position;
}
cm /= (double)vertices.size();
// translate to origin
for (VertexIter v = vertices.begin(); v != vertices.end(); v++) {
v->position -= cm;
}
// determine radius
double rMax = 0;
for (VertexCIter v = vertices.begin(); v != vertices.end(); v++) {
rMax = std::max(rMax, v->position.norm());
}
// rescale to unit sphere
for (VertexIter v = vertices.begin(); v != vertices.end(); v++) {
v->position /= rMax;
}
}