-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaarect.h
202 lines (147 loc) · 6.17 KB
/
aarect.h
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef AARECT_H
#define AARECT_H
#include "rtweekend.h"
#include "hittable.h"
class xy_rect : public hittable {
// rectangle between x0,x1 & y0,y1 at z = k
public:
xy_rect() {}
xy_rect( double _x0, double _x1, double _y0, double _y1, double _k, shared_ptr<material> mat)
: x0(_x0), x1(_x1), y0(_y0), y1(_y1), k{_k}, mp(mat), area((x1 - x0) * (y1 - y0)) {};
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
virtual bool bounding_box(double time0, double time1, aabb& output_box) const override {
// The bounding box must have non-zero width in each dimension, so pad the Z dimension
// in a small amount
output_box = aabb(point3(x0, y0, k - 0.0001), point3(x1, y1, k + 0.0001));
return true;
}
virtual vec3 random(const point3& origin) const override{
vec3 o_prime = point3(random_double(x0,x1), random_double(y0, y1), k);
return o_prime - origin;
}
virtual double pdf_value(const point3& origin, const vec3& v) const override {
hit_record rec;
if(!this->hit(ray(origin, v), 0.001, infinity, rec)){
return 0;
}
double dist_squared = rec.t * rec.t * v.length_squared();
double cosine = fabs(dot(v, rec.normal)) / v.length();
return dist_squared / (cosine * area);
}
public:
shared_ptr<material> mp;
double x0, x1, y0, y1, k;
double area;
};
class xz_rect : public hittable {
public:
xz_rect() {}
xz_rect( double _x0, double _x1, double _z0, double _z1, double _k, shared_ptr<material> mat)
: x0(_x0), x1(_x1), z0(_z0), z1(_z1), k{_k}, mp(mat), area((x1 - x0) * (z1 - z0)) {};
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
virtual bool bounding_box(double time0, double time1, aabb& output_box) const override {
// The bounding box must have non-zero width in each dimension, so pad the normal direction
// a small amount
output_box = aabb(point3(x0, k - 0.0001, z0), point3(x1, k + 0.0001, z1));
return true;
}
virtual double pdf_value(const point3& origin, const vec3& v) const override {
hit_record rec;
if (!this->hit(ray(origin, v), 0.001, infinity, rec))
return 0;
auto distance_squared = rec.t * rec.t * v.length_squared();
auto cosine = fabs(dot(v, rec.normal) / v.length());
return distance_squared / (cosine * area);
}
virtual vec3 random(const point3& origin) const override {
auto random_point = point3(random_double(x0,x1), k, random_double(z0,z1));
return random_point - origin;
}
public:
shared_ptr<material> mp;
double x0, x1, z0, z1, k;
double area;
};
class yz_rect : public hittable {
public:
yz_rect() {}
yz_rect( double _y0, double _y1, double _z0, double _z1, double _k, shared_ptr<material> mat)
: y0(_y0), y1(_y1), z0(_z0), z1(_z1), k{_k}, mp(mat), area((y1 - y0) * (z1 - z0)) {};
virtual bool hit(const ray& r, double t_min, double t_max, hit_record& rec) const override;
virtual bool bounding_box(double time0, double time1, aabb& output_box) const override {
// The bounding box must have non-zero width in each dimension, so pad the Z dimension
// in a small amount
output_box = aabb(point3(k - 0.0001, y0, z0), point3(k + 0.0001, y1, z1));
return true;
}
virtual vec3 random(const vec3& o) const override{
vec3 o_prime = vec3(k, random_double(y0,y1), random_double(z0,z1));
return o_prime - o;
}
virtual double value(const point3& origin, const vec3& v){
hit_record rec;
if(!this->hit(ray(origin, v), 0.001, infinity, rec)){
return 0;
}
double dist_squared = rec.t * rec.t * v.length_squared();
double cosine = fabs(dot(v, rec.normal)) / v.length();
return dist_squared / (cosine * area);
}
public:
shared_ptr<material> mp;
double y0, y1, z0, z1, k;
double area;
};
bool xy_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k - r.origin().z()) / r.direction().z();
if(t < t_min || t > t_max){
return false;
}
auto x = r.origin().x() + t * r.direction().x();
auto y = r.origin().y() + t * r.direction().y();
if(x < x0 || x > x1 || y < y0 || y > y1)
return false;
rec.u = (x - x0) / (x1 - x0);
rec.v = (y - y0) / (y1 - y0);
rec.t = t;
auto outward_normal = vec3(0,0,1);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mp;
rec.p = r.at(t);
return true;
}
bool xz_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k-r.origin().y()) / r.direction().y();
if (t < t_min || t > t_max)
return false;
auto x = r.origin().x() + t*r.direction().x();
auto z = r.origin().z() + t*r.direction().z();
if (x < x0 || x > x1 || z < z0 || z > z1)
return false;
rec.u = (x-x0)/(x1-x0);
rec.v = (z-z0)/(z1-z0);
rec.t = t;
auto outward_normal = vec3(0, 1, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mp;
rec.p = r.at(t);
return true;
}
bool yz_rect::hit(const ray& r, double t_min, double t_max, hit_record& rec) const {
auto t = (k-r.origin().x()) / r.direction().x();
if (t < t_min || t > t_max)
return false;
auto y = r.origin().y() + t*r.direction().y();
auto z = r.origin().z() + t*r.direction().z();
if (y < y0 || y > y1 || z < z0 || z > z1)
return false;
rec.u = (y-y0)/(y1-y0);
rec.v = (z-z0)/(z1-z0);
rec.t = t;
auto outward_normal = vec3(1, 0, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mp;
rec.p = r.at(t);
return true;
}
#endif