BaseObject.cpp 2.97 KB
Newer Older
Yuwei Xiao's avatar
init  
Yuwei Xiao committed
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
#include "BaseObject.h"
#include <igl/per_face_normals.h>
#include <igl/per_vertex_normals.h>
#include <igl/readOBJ.h>
#include <igl/readOFF.h>

bool BaseObject::loadMesh(const std::string& path) {
	bool succ = false;

	std::ifstream infile(path);
	if (!infile.good()) {
		return false;
	}

	const std::string OFF(".off");
	if (path.compare(path.size() - OFF.size(), OFF.size(), OFF) == 0) {
		succ = igl::readOFF(path, m_mesh.V, m_mesh.F, m_mesh.V_normals);
		if (succ) {
			std::cout << "Reading OFF-file from " << path << " ..."
				<< std::endl;
		}
	}

	const std::string OBJ(".obj");
	if (path.compare(path.size() - OBJ.size(), OBJ.size(), OBJ) == 0) {
		succ = igl::readOBJ(path, m_mesh.V, m_mesh.F);
		if (succ) {
			std::cout << "Reading OBJ-file from " << path << " ..."
				<< std::endl;
			igl::per_vertex_normals(m_mesh.V, m_mesh.F, m_mesh.V_normals);
		}
	}
	m_mesh.C = Eigen::MatrixXd(1, 3);
	m_mesh.C << 255.0 / 255.0, 228.0 / 255.0, 58.0 / 255.0;
	return succ;
}

void BaseObject::setMesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F) {
	m_mesh.V = V;
	m_mesh.F = F;
}

void BaseObject::findAndLoadMesh(const std::string& file) {
	if (loadMesh(file)) return;
	if (loadMesh("data/" + file)) return;
	if (loadMesh("../data/" + file)) return;
	if (loadMesh("../../data/" + file)) return;
	if (loadMesh("../../../data/" + file)) return;
	std::cerr << "Failed to find " << file << std::endl;
}

void BaseObject::reset() {
	setPosition(Eigen::Vector3d::Zero());
	setRotation(Eigen::Matrix3d::Identity());
	resetMembers();
}

void BaseObject::recomputeCOM() {
	Eigen::Vector3d COM = m_mesh.V.colwise().mean();
	m_mesh.V = m_mesh.V.rowwise() - COM.transpose();
}

void BaseObject::setScale(double s) { m_scale = s; }

void BaseObject::setID(int id) { m_id = id; }

void BaseObject::setType(ObjType t) { m_type = t; }

void BaseObject::setPosition(const Eigen::Vector3d& p) { m_position = p; }

void BaseObject::setRotation(const Eigen::Quaterniond& q) {
	m_quat = q;
	m_rot = q.toRotationMatrix();
}

void BaseObject::setRotation(const Eigen::Matrix3d& R) {
	m_rot = R;
	m_quat = R;
}

void BaseObject::setColors(const Eigen::MatrixXd& C) { m_mesh.C = C; }

double BaseObject::getScale() const { return m_scale; }

int BaseObject::getID() const { return m_id; }

ObjType BaseObject::getType() const { return m_type; }

Eigen::Vector3d BaseObject::getPosition() const { return m_position; }

Eigen::Quaterniond BaseObject::getRotation() const { return m_quat; }

Eigen::Matrix3d BaseObject::getRotationMatrix() const { return m_rot; }

Eigen::Vector3d BaseObject::getVertexPosition(int vertexIndex) const {
	return m_mesh.V.row(vertexIndex) * m_scale *
		getRotationMatrix().transpose() +
		getPosition().transpose();
}

void BaseObject::getMesh(Eigen::MatrixXd& V, Eigen::MatrixXi& F) const {
	// get mesh after rotation and translation
	V = (m_mesh.V * m_scale * getRotationMatrix().transpose()).rowwise() +
		getPosition().transpose();
	F = m_mesh.F;
}

void BaseObject::getColors(Eigen::MatrixXd& C) const { C = m_mesh.C; }