-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreferenceshape.cpp
executable file
·60 lines (48 loc) · 1.53 KB
/
referenceshape.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
#include "referenceshape.hpp"
using namespace std;
ReferenceSphere::ReferenceSphere(const Vec3& _center, double _radius) : center(_center), radius(_radius) {
}
// assume mesh is a discretized sphere; find center and radius using LSQ
ReferenceSphere::ReferenceSphere(const Mesh& mesh) {
Mat3x3 M(0);
Vec3 b(0);
for (size_t i=0; i<mesh.nodes.size(); i++) {
Vec3 pi = mesh.nodes[i]->x;
for (size_t j=0; j<mesh.nodes.size(); j++) {
Vec3 pj = mesh.nodes[j]->x;
for (int k=0; k<3; k++) M.col(k) += pi[k]*(pj-pi);
b += 0.5*pi*(norm2(pj)-norm2(pi));
}
}
center = M.inv() * b;
double R2 = 0;
for (size_t i=0; i<mesh.nodes.size(); i++) {
R2 += norm2(center - mesh.nodes[i]->x);
}
radius = sqrt(R2/mesh.nodes.size());
}
Vec3 ReferenceSphere::closest_point(const Vec3& p) {
return center + radius * normalize(p - center);
}
bool ReferenceSphere::raycast(Vec3& p, const Vec3& dir) {
p = normalize(p-center) * radius + center;
return true;
}
ReferenceLinear::ReferenceLinear(const Mesh& mesh) {
}
Vec3 ReferenceLinear::closest_point(const Vec3& p) {
return p;
}
bool ReferenceLinear::raycast(Vec3& p, const Vec3& dir) {
return true;
}
ReferenceMesh::ReferenceMesh(const Mesh& mesh, const string& filename) {
cout << "Reference mesh lookup not implemented here." << endl;
exit(1);
}
Vec3 ReferenceMesh::closest_point(const Vec3& p) {
return p;
}
bool ReferenceMesh::raycast(Vec3& p, const Vec3& dir) {
return false;
}