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
|
//
// Created by Keuin on 2022/4/12.
//
#ifndef RT_SPHERE_H
#define RT_SPHERE_H
#include "object.h"
#include "hitlist.h"
#include "viewport.h"
#include "timer.h"
#include "bitmap.h"
#include "material.h"
#include "ray.h"
#include "vec.h"
#include <cstdlib>
#include <memory>
#include <limits>
#include <vector>
#include <iostream>
#include <cstdint>
class material;
class sphere : public object {
vec3d center;
double radius;
const class material *materi;
public:
sphere() = delete;
sphere(const vec3d center, double radius, const class material &materi)
: center(center), radius(radius), materi(&materi) {}
~sphere() override = default;
vec3d normal_vector(const vec3d &where) const override {
// We assume the point is on surface, speeding up the normalization
return (where - center) / radius;
}
const class material &get_material() const override {
return *materi;
}
bool hit(const ray3d &r, double &t, double t1, double t2) const override {
// Ray: {Source, Direction, time}
// Sphere: {Center, radius}
// sphere hit formula: |Source + Direction * time - Center| = radius
// |(Sx + Dx * t - Cx, Sy + Dy * t - Cy, Sz + Dz * t - Cz)| = radius
const auto c2s = r.source() - center; // center to source
// A = D dot D
const double a = r.direction().mod2();
// H = (S - C) dot D
const auto h = dot(c2s, r.direction());
// B = 2H ( not used in our optimized routine )
// C = (S - C) dot (S - C) - radius^2
const double c = c2s.mod2() - radius * radius;
// 4delta = H^2 - AC
// delta_q = H^2 - AC (quarter delta)
const double delta_q = h * h - a * c;
bool hit = false;
if (delta_q >= 0) {
// return the root in range [t1, t2]
// t = ( -H +- sqrt{ delta_q } ) / A
double root;
root = (-h - sqrt(delta_q)) / a;
if (root >= t1 && root <= t2) {
hit = true;
t = root;
} else {
root = (-h + sqrt(delta_q)) / a;
if (root >= t1 && root <= t2) {
hit = true;
t = root;
}
}
}
return hit;
}
bool is_on(const vec3d &p) const override {
return ((p - center).mod2() - radius * radius) < 1e-10;
}
pixel8b color() const override {
return pixel8b::from_normalized(1.0, 0.0, 0.0);
}
};
#endif //RT_SPHERE_H
|