-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rectangle.h
79 lines (57 loc) · 2.32 KB
/
Rectangle.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
#ifndef RAYTRACER_RECTANGLE_H
#define RAYTRACER_RECTANGLE_H
#include "Hitable.h"
class RECTXY : public Hitable {
public:
double x0, x1, y0, y1, k;
Material *mat_ptr;
RECTXY() {}
RECTXY(double x_0, double x_1, double y_0, double y_1, double kt, Material *mat) :
x0(x_0), x1(x_1), y0(y_0), y1(y_1), k(kt), mat_ptr(mat) {}
bool hit(const Ray &r, double t_min, double t_max, HitRecord &rec) const override;
bool bounding_box(AxisAlignedBoundingBox &box) const override{
box = AxisAlignedBoundingBox(Vec3(x0, y0, k - 0.0001), Vec3(x1, y1, k + 0.0001));
return true;
}
};
class RECTXZ : public Hitable {
public:
double x0, x1, z0, z1, k;
Material *mat_ptr;
RECTXZ() {}
RECTXZ(double x_0, double x_1, double z_0, double z_1, double kt, Material *mat) :
x0(x_0), x1(x_1), z0(z_0), z1(z_1), k(kt), mat_ptr(mat) {}
bool hit(const Ray &r, double t_min, double t_max, HitRecord &rec) const override;
bool bounding_box(AxisAlignedBoundingBox &box) const override{
box = AxisAlignedBoundingBox(Vec3(x0, k - 0.0001, z0), Vec3(x1, k + 0.0001, z1));
return true;
}
double pdfValue(const Vec3& o, const Vec3& v) const override {
HitRecord hrec;
if(this->hit(Ray(o, v), 0.001, DBL_MAX, hrec)) {
double area = (x1 - x0) * (z1 - z0);
double distance_squared = hrec.t * hrec.t * v.squared_length();
double cosine = fabs(dot(v, hrec.normal) / v.length());
return distance_squared / (cosine * area);
}
else return 0;
}
Vec3 random(const Vec3& o) const override {
Vec3 randomPoint = Vec3(x0 + randNum01() * (x1 - x0), k, z0 + randNum01() * (z1 - z0));
return randomPoint - o;
}
};
class RECTYZ : public Hitable {
public:
double y0, y1, z0, z1, k;
Material *mat_ptr;
RECTYZ() {}
RECTYZ(double y_0, double y_1, double z_0, double z_1, double kt, Material *mat) :
y0(y_0), y1(y_1), z0(z_0), z1(z_1), k(kt), mat_ptr(mat) {}
bool hit(const Ray &r, double t_min, double t_max, HitRecord &rec) const override;
bool bounding_box(AxisAlignedBoundingBox &box) const override{
box = AxisAlignedBoundingBox(Vec3(k - 0.0001, y0, z0), Vec3(k + 0.0001, y1, z1));
return true;
}
};
#endif //RAYTRACER_RECTANGLE_H