-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSphere.cpp
131 lines (106 loc) · 2.9 KB
/
Sphere.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
#include "Sphere.h"
#include "Constants.h"
#include "Vector.h"
#include "Ray.h"
#include "ShadeRec.h"
const RTdouble Sphere::kEpsilon = RT_EPSILON;
Sphere::Sphere(): GeometryObject(), c(), r(RT_ZERO) {
}
Sphere::Sphere(const Point &point, const RTdouble &radius): GeometryObject(), c(point), r(radius) {
}
void Sphere::setCenter(const RTdouble &cor) {
c = Point(cor);
}
void Sphere::setCenter(const Point &p) {
c = p;
}
void Sphere::setRadius(const RTdouble &len) {
r = (RTdouble)len;
}
bool Sphere::hit(const Ray &ray, double &min_t, ShadeRec &sr) const {
// (p - c) * (p - c) - r^2 = 0
// p = o + t * d
// (d * d)t^2 + [2(o-c)*d]t + (o-c)(o-c) - r^2 = 0
// A = d * d
// B = 2 * (o-c) * d
// C = (o-c)^2 - r^2
// delta = B^2 - 4*A*C
// t_1 = (-B + sqrt(delta)) / (2 * a)
// t_2 = (-B - sqrt(delta)) / (2 * a)
//ray.print("ray");
//(*this).print("sphere");
RTdouble t;
Vector co = ray.o - c;
RTdouble A = ray.d * ray.d,
B = (RTdouble)2. * co * ray.d,
C = co * co - r * r,
delta = B * B - (RTdouble)4. * A * C;
if( delta < RT_ZERO) {
return false;
}
else {
RTdouble root = sqrt(delta);
t = (-B - root) / ((RTdouble)2. * A); // smaller solution
if( t > kEpsilon) { // ray hits the sphere in [0,+inf)
min_t = t;
sr.normal = (co + t * ray.d) / r;
sr.local_hit_point = ray.o + t * ray.d;
return true;
}
t = (-B + root) / ((RTdouble)2. * A); // greater solution
// obvious farther than the smaller one
if( t > kEpsilon) { // ray hits the sphere in [0,+inf)
min_t = t;
sr.normal = (co + t * ray.d) / r;
sr.local_hit_point = ray.o + t * ray.d;
return true;
}
}
return false; // both t < 0
}
bool Sphere::shadowHit(const Ray &ray, RTdouble &min_t) const {
if(!shadows) {
return false;
}
RTdouble t;
Vector co = ray.o - c;
RTdouble A = ray.d * ray.d,
B = (RTdouble)2. * co * ray.d,
C = co * co - r * r,
delta = B * B - (RTdouble)4. * A * C;
if( delta < RT_ZERO) {
return false;
}
else {
RTdouble root = sqrt(delta);
t = (-B - root) / ((RTdouble)2. * A); // smaller solution
if( t > kEpsilon) { // ray hits the sphere in [0,+inf)
min_t = t;
return true;
}
t = (-B + root) / ((RTdouble)2. * A); // greater solution
// obvious farther than the smaller one
if( t > kEpsilon) { // ray hits the sphere in [0,+inf)
min_t = t;
return true;
}
}
return false;
}
void Sphere::print(const string &prompt) const {
printf("Info of Sphere %s>\n", prompt.c_str());
c.print("center of Sphere " + prompt);
printf("radius of Sphere %s is %.2lf\n", prompt.c_str(), r);
}
RGBColor Sphere::getColor() const {
return color;
}
void Sphere::setColor(const RGBColor &t_color) {
color = t_color;
}
Material * Sphere::getMaterial() const {
return material;
}
void Sphere::setMaterial(Material * mat) {
material = mat;
}