-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtriangle.cpp
More file actions
53 lines (45 loc) · 1.59 KB
/
triangle.cpp
File metadata and controls
53 lines (45 loc) · 1.59 KB
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
#include "all.h"
#include <cmath>
namespace HYF
{
int Triangle::Intersect(Ray& p_Ray, double& p_Dist)
{
vector3 dir = p_Ray.getDirection();
vector3 ori = p_Ray.getOrigin();
double distance = - DOT( (ori - vertex[0]), N ) / DOT( dir, N );
if ((distance < EPS) || (distance > 1e6)) return MISS;
vector3 interPoint = ori + distance * dir;
double u,v;
vector3 diru = vertex[1] - vertex[0];
NORMALIZE(diru);
vector3 Hu1 = vertex[0] + diru * DOT(diru,(vertex[2] - vertex[0]));
vector3 Hu2 = vertex[0] + diru * DOT(diru,(interPoint - vertex[0]));
u = (interPoint - Hu2).x / (vertex[2] - Hu1).x;
if(u < -EPS) return MISS;
vector3 dirv = vertex[2] - vertex[0];
NORMALIZE(dirv);
vector3 Hv1 = vertex[0] + dirv * DOT(dirv,(vertex[1] - vertex[0]));
vector3 Hv2 = vertex[0] + dirv * DOT(dirv,(interPoint - vertex[0]));
v = (interPoint - Hv2).x / (vertex[1] - Hv1).x;
if(v < -EPS) return MISS;
if(u+v > 1.0+EPS) return MISS;
if(p_Dist > distance) p_Dist = distance;
else return MISS;
return HIT;
}
bool Triangle::H_IntersectBox( BoundingBox& box)
{
BoundingBox a = getBoundingBox();
return a.Intersect(box);
}
BoundingBox Triangle::getBoundingBox()
{
vector3 mina = vector3(std::min((std::min(vertex[0].x,vertex[1].x)),vertex[2].x),
std::min((std::min(vertex[0].y,vertex[1].y)),vertex[2].y),
std::min((std::min(vertex[0].z,vertex[1].z)),vertex[2].z));
vector3 maxa = vector3(std::max((std::max(vertex[0].x,vertex[1].x)),vertex[2].x),
std::max((std::max(vertex[0].y,vertex[1].y)),vertex[2].y),
std::max((std::max(vertex[0].z,vertex[1].z)),vertex[2].z));
return BoundingBox(mina,maxa - mina);
}
};