-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrttriangle.cpp
189 lines (147 loc) · 4.19 KB
/
rttriangle.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "rttriangle.h"
#include <cmath>
RTTriangle::RTTriangle(RTPoint &p1, RTPoint &p2, RTPoint &p3) :
RTObject(),
p1(p1), p2(p2), p3(p3)
{
RTVector normal = this->getNormalOfPlane();
setNormal1(normal);
setNormal2(normal);
setNormal3(normal);
}
RTTriangle::RTTriangle(const RTTriangle &cpy) :
RTObject(),
p1(cpy.getP1()),
p2(cpy.getP2()),
p3(cpy.getP3()),
normal1(cpy.getNormal1()),
normal2(cpy.getNormal2()),
normal3(cpy.getNormal3())
{
}
RTTriangle::RTTriangle()
{
this->p1 = RTPoint(0,0,0);
this->p2 = RTPoint(0,0,0);
this->p3 = RTPoint(0,0,0);
RTVector normal = this->getNormalOfPlane();
setNormal1(normal);
setNormal2(normal);
setNormal3(normal);
}
bool RTTriangle::intersect(RTRay &ray, double &t)
{
RTVector normal = this->getNormalOfPlane();
// check if the ray intersect the plane
double plane_t = ( (RTVector)( ray.getPos() - this->p1 ) ).dot( normal );
plane_t = - plane_t / ray.getDir().dot( normal );
// the ray does not hit the plane
if(plane_t<0)
return false;
RTVector vecPos(ray.getPos().getX(), ray.getPos().getY(), ray.getPos().getZ());
RTVector vecP = vecPos + ray.getDir() * plane_t;
RTPoint p(vecP.getX(), vecP.getY(), vecP.getZ());
// the ray does hit the plane, now we can
// check if the hit point of the plane is
// also into the triangle
double a = ( (RTVector)( ( p2 - p1 ) * ( p - p1 ) ) ).dot(normal);
double b = ( (RTVector)( ( p3 - p2 ) * ( p - p2 ) ) ).dot(normal);
double c = ( (RTVector)( ( p1 - p3 ) * ( p - p3 ) ) ).dot(normal);
if(a>=0 && b>=0 && c>=0)
{
t = plane_t;
return true;
} else {
return false;
}
}
RTVector RTTriangle::normalOfHitPoint(RTVector hit)
{
RTPoint ph(hit.getX(), hit.getY(), hit.getZ());
double aT = TriangleArea(p1, p2, p3);
double aB = TriangleArea(p1, ph, p3);
double aC = TriangleArea(p1, ph, p2);
double aA = aT - aB - aC;
double c1 = aA/aT;
double c2 = aB/aT;
double c3 = aC/aT;
double nX = normal1.getX()*c1 + normal2.getX()*c2 + normal3.getX()*c3;
double nY = normal1.getY()*c1 + normal2.getY()*c2 + normal3.getY()*c3;
double nZ = normal1.getZ()*c1 + normal2.getZ()*c2 + normal3.getZ()*c3;
RTVector normal(nX, nY, nZ);
return normal;
}
RTVector RTTriangle::getNormalOfPlane() const
{
// calculate normal using counterclockwise
RTVector n1 = ( this->getP2() - this->getP1() );
RTVector n2 = ( this->getP3() - this->getP1() );
RTVector n = n1 * n2;
return n;
}
RTPoint RTTriangle::getP1() const
{
return p1;
}
void RTTriangle::setP1(const RTPoint &value)
{
p1 = value;
}
RTPoint RTTriangle::getP2() const
{
return p2;
}
void RTTriangle::setP2(const RTPoint &value)
{
p2 = value;
}
RTPoint RTTriangle::getP3() const
{
return p3;
}
void RTTriangle::setP3(const RTPoint &value)
{
p3 = value;
}
RTVector RTTriangle::getNormal1() const
{
return normal1;
}
void RTTriangle::setNormal1(const RTVector &value)
{
normal1 = value;
}
RTVector RTTriangle::getNormal2() const
{
return normal2;
}
void RTTriangle::setNormal2(const RTVector &value)
{
normal2 = value;
}
RTVector RTTriangle::getNormal3() const
{
return normal3;
}
void RTTriangle::setNormal3(const RTVector &value)
{
normal3 = value;
}
double TriangleArea( const RTPoint &A, const RTPoint &B, const RTPoint &C )
{
// Calculate the side lengths
double a = std::sqrt( std::pow( ( A.getX() - B.getX() ) , 2) +
std::pow( ( A.getY() - B.getY() ) , 2) +
std::pow( ( A.getZ() - B.getZ() ) , 2) );
double b = std::sqrt( std::pow( ( A.getX() - C.getX() ) , 2) +
std::pow( ( A.getY() - C.getY() ) , 2) +
std::pow( ( A.getZ() - C.getZ() ) , 2) );
double c = std::sqrt( std::pow( ( B.getX() - C.getX() ) , 2) +
std::pow( ( B.getY() - C.getY() ) , 2) +
std::pow( ( B.getZ() - C.getZ() ) , 2) );
// Calculate half the perimeter
double s = (1/2)*(a+b+c);
// Calculate total Area
double area = std::sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}