-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtsphere.cpp
115 lines (92 loc) · 2.3 KB
/
rtsphere.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
#include "rtsphere.h"
#include <cstdio>
#include <cmath>
RTSphere::RTSphere() : RTObject()
{
this->radius = 1.0;
this->objTag=SPHERE;
}
RTSphere::RTSphere(const RTSphere &sph) : RTObject(sph.getBrdf())
{
this->radius = sph.getRadius();
this->center = sph.center;
this->objTag=SPHERE;
}
RTSphere::RTSphere(RTPoint ¢er, double radius, RTBRDF *brdf) : RTObject(brdf)
{
this->center = center;
this->radius = radius;
this->objTag=SPHERE;
}
RTPoint RTSphere::getCenter() const
{
return center;
}
void RTSphere::setCenter(const RTPoint &value)
{
center = value;
}
double RTSphere::getRadius() const
{
return radius;
}
void RTSphere::setRadius(double value)
{
radius = value;
}
bool RTSphere::intersect(RTRay &ray, double &t)
{
double a, b, c, delta, t0, t1;
// calculates the A term
a = std::pow( ray.getDir().getX() , 2.0 ) +
std::pow( ray.getDir().getY() , 2.0 ) +
std::pow( ray.getDir().getZ(), 2.0 );
// calculates the B term
b = 2*(
ray.getDir().getX() * ( ray.getPos().getX() - center.getX() ) +
ray.getDir().getY() * ( ray.getPos().getY() - center.getY() ) +
ray.getDir().getZ() * ( ray.getPos().getZ() - center.getZ() )
);
// calculates the C term
c = std::pow( ray.getPos().getX() - center.getX() , 2.0 )
+ std::pow( ray.getPos().getY() - center.getY() , 2.0 )
+ std::pow( ray.getPos().getZ() - center.getZ() , 2.0 )
- std::pow( radius, 2.0 );
// a*t² + b*t + c = 0
// calculates the delta
delta = b*b - 4*a*c;
// No results
if(delta<0)
return false;
//numeric precision
if(b<0){
t0 = (( -b + std::sqrt(delta) ) / 2*a)/a;
t1 = c/(( -b + std::sqrt(delta) ) / 2*a);
}else{
t0 = (( -b - std::sqrt(delta) ) / 2*a)/a;
t1 = c/(( -b - std::sqrt(delta) ) / 2*a);
}
if(t0>t1){
float temp=t0;
t0=t1;
t1=temp;
}
if (t1 < 0)
return false;
if (t0 < 0)
{
t = t1;
return true;
}
else
{
t = t0;
return true;
}
}
RTVector RTSphere::normalOfHitPoint(RTVector hit)
{
RTVector normal =(hit-(this->center*1.0))/this->radius;
normal.normalize();
return normal;
}