-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.pi
More file actions
140 lines (110 loc) · 3.24 KB
/
vec3.pi
File metadata and controls
140 lines (110 loc) · 3.24 KB
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
use raytracepl::utils::*;
pub struct Vec3 {
pub x:f64;
pub y:f64;
pub z:f64;
}
pub fn default() Vec3 {
return Vec3{};
}
pub fn new(x:f64, y:f64, z:f64) Vec3 {
return Vec3{x:x,y:y,z:z};
}
pub fn random() Vec3 {
return Vec3{x:random_f64(),y:random_f64(),z:random_f64()};
}
pub fn random_in_unit_sphere() Vec3 {
while true {
let p = random_range(-1.0, 1.0);
if p.length_squared() < 1.0 {
return p;
}
}
return Vec3{};
}
pub fn ranndom_unit_vector() Vec3 {
return random_in_unit_sphere().unit_vector();
}
pub fn random_on_hemisphere(normal:Vec3) Vec3 {
let on_unit_sphere = random_in_unit_sphere();
if on_unit_sphere.dot(normal) > 0.0 {
return on_unit_sphere;
} else {
return on_unit_sphere.neg();
}
}
pub fn random_range(min:f64, max:f64) Vec3 {
return Vec3{
x:random_f64_range(min,max),
y:random_f64_range(min,max),
z:random_f64_range(min,max)
};
}
pub fn random_in_unit_disk() Vec3 {
while true {
let p = Vec3{x:random_f64_range(-1.0, 1.0), y:random_f64_range(-1.0, 1.0), z:0.0};
if p.length_squared() < 1.0 {
return p;
}
}
return Vec3{};
}
impl Vec3 {
pub fn near_zero() bool {
let s = 1e-8;
return fabs(self.x) < s && fabs(self.y) < s && fabs(self.z) < s;
}
pub fn length_squared() f64 {
return self.x*self.x + self.y*self.y + self.z*self.z;
}
pub fn length() f64 {
return sqrt_64(self.length_squared());
}
pub fn add(other:Vec3) Vec3 {
return Vec3{x:self.x+other.x, y:self.y+other.y, z:self.z+other.z};
}
pub fn sub(other:Vec3) Vec3 {
return Vec3{x:self.x-other.x, y:self.y-other.y, z:self.z-other.z};
}
pub fn neg() Vec3 {
return Vec3{x:-self.x, y:-self.y, z:-self.z};
}
pub fn mul(i:f64) Vec3 {
return Vec3{x:self.x*i, y:self.y*i, z:self.z*i};
}
pub fn div(i:f64) Vec3 {
return Vec3{x:self.x/i, y:self.y/i, z:self.z/i};
}
pub fn eq(other:*Vec3) bool {
return self.x == other.x && self.y == other.y && self.z == other.z;
}
pub fn to_str() string {
let s = itoa_pl(self.x as i64);
s.append(" ");
s.append(itoa_pl(self.y as i64));
s.append(" ");
s.append(itoa_pl(self.z as i64));
return s;
}
pub fn mul_vec(other:Vec3) Vec3 {
return Vec3{x:self.x*other.x, y:self.y*other.y, z:self.z*other.z};
}
pub fn dot(other:Vec3) f64 {
return self.x*other.x + self.y*other.y + self.z*other.z;
}
pub fn cross(other:Vec3) Vec3 {
return Vec3{x:self.y*other.z - self.z*other.y, y:self.z*other.x - self.x*other.z, z:self.x*other.y - self.y*other.x};
}
pub fn unit_vector() Vec3 {
return self.div(self.length());
}
pub fn reflect(n:Vec3) Vec3 {
return self.sub(n.mul(2.0*self.dot(n)));
}
pub fn refract(n:Vec3, etai_over_etat:f64) Vec3 {
let cos_theta = fmin(self.neg().dot(n), 1.0);
let r_out_perp = self.add(n.mul(cos_theta)).mul(etai_over_etat);
let r_out_parallel = n.mul(sqrt_64(fabs(1.0 - r_out_perp.length_squared()))).neg();
return r_out_perp.add(r_out_parallel);
}
}