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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright (c) 2023 Xu Shaohua <shaohua@biofan.org>. All rights reserved.
// Use of this source is governed by Lesser General Public License that can be found
// in the LICENSE file.
use std::ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign};
use crate::core::scalar::{Scalar, SCALAR_NEARLY_ZERO};
#[derive(Debug, Clone, PartialEq)]
pub struct Point3 {
x: Scalar,
y: Scalar,
z: Scalar,
}
impl Point3 {
#[must_use]
#[inline]
pub const fn from(x: Scalar, y: Scalar, z: Scalar) -> Self {
Self { x, y, z }
}
#[must_use]
#[inline]
pub const fn x(&self) -> Scalar {
self.x
}
#[must_use]
#[inline]
pub const fn y(&self) -> Scalar {
self.y
}
#[must_use]
#[inline]
pub const fn z(&self) -> Scalar {
self.z
}
#[inline]
pub fn set(&mut self, x: Scalar, y: Scalar, z: Scalar) {
self.x = x;
self.y = y;
self.z = z;
}
/// Return the Euclidian distance from (0,0,0) to (x,y,z)
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn from_length(x: Scalar, y: Scalar, z: Scalar) -> Scalar {
let mag_sq = get_length_squared(x, y, z);
if mag_sq.is_finite() {
mag_sq.sqrt()
} else {
let xx = f64::from(x);
let yy = f64::from(y);
let zz = f64::from(z);
(xx.mul_add(xx, yy.mul_add(yy, zz * zz))).sqrt() as f32
}
}
/// Return the Euclidian distance from (0,0,0) to the point
#[must_use]
#[inline]
pub fn length(&self) -> Scalar {
Self::from_length(self.x, self.y, self.z)
}
/// Set the point (vector) to be unit-length in the same direction as it
/// already points.
///
/// If the point has a degenerate length (i.e., nearly 0) then set it to (0,0,0)
/// and return false; otherwise return true.
#[must_use]
pub fn normalize(&mut self) -> bool {
// We have to worry about 2 tricky conditions:
// 1. underflow of magSq (compared against nearlyzero^2)
// 2. overflow of magSq (compared w/ isfinite)
//
// If we underflow, we return false. If we overflow, we compute again using
// doubles, which is much slower (3x in a desktop test) but will not overflow.
let mut mag_sq = 0.0;
if is_length_nearly_zero(self.x, self.y, self.z, &mut mag_sq) {
self.set(0.0, 0.0, 0.0);
return false;
}
// sqrtf does not provide enough precision; since sqrt takes a double,
// there's no additional penalty to storing invScale in a double
let inv_scale = if mag_sq.is_finite() {
f64::from(mag_sq)
} else {
// our magSq step overflowed to infinity, so use doubles instead.
// much slower, but needed when x, y or z is very large, otherwise we
// divide by inf. and return (0,0,0) vector.
let xx = f64::from(self.x);
let yy = f64::from(self.y);
let zz = f64::from(self.z);
xx.mul_add(xx, yy.mul_add(yy, zz * zz))
};
// using a float instead of a double for scale loses too much precision
#[allow(clippy::cast_possible_truncation)]
let scale = (1.0 / inv_scale.sqrt()) as f32;
self.scale(scale);
if !self.x.is_finite() || !self.y.is_finite() || !self.z.is_finite() {
self.set(0.0, 0.0, 0.0);
false
} else {
true
}
}
/// Return a new point whose X, Y and Z coordinates are scaled.
#[must_use]
#[inline]
pub fn make_scale(&self, scale: Scalar) -> Self {
Self {
x: self.x * scale,
y: self.y * scale,
z: self.z * scale,
}
}
/// Scale the point's coordinates by scale.
#[inline]
pub fn scale(&mut self, scale: Scalar) {
self.x *= scale;
self.y *= scale;
self.z *= scale;
}
/// Returns true if x, y, and z are measurable values (other than infinites and NaN).
#[must_use]
#[inline]
pub fn is_finite(&self) -> bool {
let mut accum = 0.0;
accum *= self.x;
accum *= self.y;
accum *= self.z;
// accum is either NaN or it is finite (zero).
debug_assert!(accum == 0.0 || accum.is_nan());
// value==value will be true iff value is not NaN
accum.is_nan()
}
/// Returns the dot product of a and b, treating them as 3D vectors
#[must_use]
#[inline]
pub fn dot_product(a: &Self, b: &Self) -> Scalar {
a.x.mul_add(b.x, a.y.mul_add(b.y, a.z * b.z))
}
/// Returns the dot product of self and other, treating them as 3D vectors
#[must_use]
#[inline]
pub fn dot(&self, other: &Self) -> Scalar {
Self::dot_product(self, other)
}
/// Returns the cross product of a and b, treating them as 3D vectors
#[must_use]
#[inline]
pub fn cross_product(a: &Self, b: &Self) -> Self {
Self {
x: a.y.mul_add(b.z, -a.z * b.y),
y: a.z.mul_add(b.x, -a.x * b.z),
z: a.x.mul_add(b.y, -a.y * b.x),
}
}
/// Returns the cross product of self and other point, treating them as 3D vectors
#[must_use]
pub fn cross(&self, other: &Self) -> Self {
Self::cross_product(self, other)
}
}
pub type Vector3 = Point3;
pub type Color3f = Point3;
/// Return a new point whose X, Y and Z coordinates are the negative of the original point's
impl Neg for &Point3 {
type Output = Point3;
fn neg(self) -> Self::Output {
Self::Output {
x: -self.x,
y: -self.y,
z: -self.z,
}
}
}
/// Returns a new point whose coordinates are the sum of a and b (a + b)
impl Add<&Self> for &Point3 {
type Output = Point3;
fn add(self, other: &Self) -> Self::Output {
Self::Output {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
/// Add other's coordinates to the point's
impl AddAssign<&Self> for Point3 {
fn add_assign(&mut self, other: &Self) {
self.x += other.x;
self.y += other.y;
self.z += other.z;
}
}
/// Returns a new point whose coordinates are the difference between a and b (i.e., a - b)
impl Sub<&Self> for &Point3 {
type Output = Point3;
fn sub(self, other: &Self) -> Self::Output {
Self::Output {
x: self.x - other.x,
y: self.y - other.y,
z: self.z - other.z,
}
}
}
/// Subtract other's coordinates from the point's
impl SubAssign<&Self> for Point3 {
fn sub_assign(&mut self, other: &Self) {
self.x -= other.x;
self.y -= other.y;
self.z -= other.z;
}
}
impl Mul<Scalar> for &Point3 {
type Output = Point3;
fn mul(self, scale: Scalar) -> Self::Output {
Self::Output {
x: self.x * scale,
y: self.y * scale,
z: self.y * scale,
}
}
}
impl Mul<&Point3> for Scalar {
type Output = Point3;
fn mul(self, p: &Point3) -> Self::Output {
Point3 {
x: self * p.x,
y: self * p.y,
z: self * p.z,
}
}
}
// Returns the square of the Euclidian distance to (x,y,z).
fn get_length_squared(x: f32, y: f32, z: f32) -> f32 {
x.mul_add(x, y.mul_add(y, z * z))
}
// Calculates the square of the Euclidian distance to (x,y,z) and stores it in
// *lengthSquared. Returns true if the distance is judged to be "nearly zero".
//
// This logic is encapsulated in a helper method to make it explicit that we
// always perform this check in the same manner, to avoid inconsistencies
// (see http://code.google.com/p/skia/issues/detail?id=560 ).
fn is_length_nearly_zero(x: f32, y: f32, z: f32, length_squared: &mut f32) -> bool {
*length_squared = get_length_squared(x, y, z);
*length_squared <= (SCALAR_NEARLY_ZERO * SCALAR_NEARLY_ZERO)
}