-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.cpp
143 lines (136 loc) · 4.33 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
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
#include "mesh.hpp"
#include <iostream>
Mesh::Mesh(const std::vector<Point3D>& verts,
const std::vector< std::vector<int> >& faces)
: m_verts(verts),
m_faces(faces)
{
std::cerr << "Computing bounds for a polygon mesh\n";
// determine a bounding box/sphere
// whichever has a smaller volume
double minx, maxx, miny, maxy, minz, maxz;
double maxdd;
Point3D c;
for (std::vector<Point3D>::const_iterator I = m_verts.begin(); I != m_verts.end(); ++I) {
Point3D p = *I;
if (I == m_verts.begin()) {
minx = p[0];
miny = p[1];
minz = p[2];
maxx = p[0];
maxy = p[1];
maxz = p[2];
maxdd = 0;
c = p;
}
minx = std::min(p[0], minx);
miny = std::min(p[1], miny);
minz = std::min(p[2], minz);
maxx = std::max(p[0], maxx);
maxy = std::max(p[1], maxy);
maxz = std::max(p[2], maxz);
std::vector<Point3D>::const_iterator J = I;
for (++J; J != m_verts.end(); ++J) {
Point3D p2 = *J;
double dist = (p2 - p).length2();
if (dist > maxdd) {
maxdd = dist;
c = p + 0.5 * (p2 - p);
}
}
}
// for now use a sphere
double r = sqrt(maxdd)/2.0;
double spherevolume = 1.33 * M_PI * pow(r, 3.0);
Point3D min = Point3D(minx, miny, minz);
Point3D max = Point3D(maxx, maxy, maxz);
Vector3D sz = max-min;
// volume = w h d
double boxvolume = sz[0] * sz[1] * sz[2];
if (spherevolume < boxvolume) {
std::cerr << "Sphere is smaller\n";
m_bounds = new NonhierSphere(c, r);
} else {
std::cerr << "Box is smaller\n";
m_bounds = new NonhierBox(min, sz);
}
}
std::ostream& operator<<(std::ostream& out, const Mesh& mesh)
{
std::cerr << "mesh({";
for (std::vector<Point3D>::const_iterator I = mesh.m_verts.begin(); I != mesh.m_verts.end(); ++I) {
if (I != mesh.m_verts.begin()) std::cerr << ",\n ";
std::cerr << *I;
}
std::cerr << "},\n\n {";
for (std::vector<Mesh::Face>::const_iterator I = mesh.m_faces.begin(); I != mesh.m_faces.end(); ++I) {
if (I != mesh.m_faces.begin()) std::cerr << ",\n ";
std::cerr << "[";
for (Mesh::Face::const_iterator J = I->begin(); J != I->end(); ++J) {
if (J != I->begin()) std::cerr << ", ";
std::cerr << *J;
}
std::cerr << "]";
}
std::cerr << "});" << std::endl;
return out;
}
RayHit* Mesh::raycast(Point3D from, Vector3D ray, Material* m_material, bool backfaces)
{
// use bounding box or sphere first
RayHit* hit = m_bounds->raycast(from, ray, m_material, true); // if inside the bounding box, hitting the back of a face should still count
//return hit;
if (hit == NULL) return hit;
free(hit);
hit = NULL;
for (std::vector<Mesh::Face>::const_iterator I = m_faces.begin(); I != m_faces.end(); ++I) {
Mesh::Face::const_iterator J = I->begin();
Point3D A = m_verts[*J];
++J;
Point3D B = m_verts[*J];
++J;
Point3D C = m_verts[*J];
Vector3D norm = (B-A).cross(C-B);
//norm.normalize();
// back face cull, norm and ray should be facing each other
if (norm.dot(ray) < 0 || backfaces)
{
// intersect the plane
double t = ((A - from).dot(norm))/(ray.dot(norm));
if (t > 0 && (hit == NULL || t < hit->dist)) {
Point3D P = from + t * ray;
A = m_verts[I->back()]; // if face is ABCD, do edge DA first then AB BC CD
bool in = true;
for (J = I->begin(); J != I->end(); ++J) {
B = m_verts[*J];
// consider edge BC
Vector3D edgenorm = (B-A).cross(norm);
edgenorm.normalize();
if (edgenorm.dot(B-P) < -0.01)
{
in = false;
break;
}
A = B;
}
if (in) {
/*
std::cerr << P << " is in [";
for (J = I->begin(); J != I->end(); ++J) {
if (J != I->begin()) std::cerr << ", ";
std::cerr << m_verts[*J];
}
std::cerr << "]\n";
*/
// this will memleak if unused (or even used) hits arent freed
if (hit != NULL) {
free(hit);
}
hit = new RayHit(t, P, m_material, norm); //this norm is not yet normalized and the distance is not accurate yet
}
}
}
}
//return NULL;
return hit;
}