1use super::math_utils::js_max;
4use super::{Box3, Matrix4, Plane, Vector3};
5
6#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Sphere {
10 pub center: Vector3,
12 pub radius: f64,
14}
15
16impl Default for Sphere {
17 fn default() -> Self {
19 Self {
20 center: Vector3::new(0.0, 0.0, 0.0),
21 radius: -1.0,
22 }
23 }
24}
25
26impl Sphere {
27 pub const IS_SPHERE: bool = true;
29
30 pub const fn new(center: Vector3, radius: f64) -> Self {
32 Self { center, radius }
33 }
34
35 pub fn set(&mut self, center: &Vector3, radius: f64) -> &mut Self {
37 self.center.copy(center);
38 self.radius = radius;
39
40 self
41 }
42
43 pub fn set_from_points(
45 &mut self,
46 points: &[Vector3],
47 optional_center: Option<&Vector3>,
48 ) -> &mut Self {
49 if let Some(optional_center) = optional_center {
50 self.center.copy(optional_center);
51 } else {
52 let mut box_ = Box3::default();
53 self.center = box_.set_from_points(points).get_center();
54 }
55
56 let center = self.center;
57
58 let mut max_radius_sq = 0.0;
59
60 for point in points {
61 max_radius_sq = js_max(max_radius_sq, center.distance_to_squared(point));
62 }
63
64 self.radius = max_radius_sq.sqrt();
65
66 self
67 }
68
69 pub fn copy(&mut self, sphere: &Self) -> &mut Self {
71 self.center.copy(&sphere.center);
72 self.radius = sphere.radius;
73
74 self
75 }
76
77 pub fn is_empty(&self) -> bool {
79 self.radius < 0.0
80 }
81
82 pub fn make_empty(&mut self) -> &mut Self {
84 self.center.set(0.0, 0.0, 0.0);
85 self.radius = -1.0;
86
87 self
88 }
89
90 pub fn contains_point(&self, point: &Vector3) -> bool {
92 point.distance_to_squared(&self.center) <= (self.radius * self.radius)
93 }
94
95 pub fn distance_to_point(&self, point: &Vector3) -> f64 {
97 point.distance_to(&self.center) - self.radius
98 }
99
100 pub fn intersects_sphere(&self, sphere: &Self) -> bool {
102 let radius_sum = self.radius + sphere.radius;
103
104 sphere.center.distance_to_squared(&self.center) <= (radius_sum * radius_sum)
105 }
106
107 pub fn intersects_box(&self, box_: &Box3) -> bool {
109 box_.intersects_sphere(self)
110 }
111
112 pub fn intersects_plane(&self, plane: &Plane) -> bool {
114 plane.distance_to_point(&self.center).abs() <= self.radius
115 }
116
117 pub fn clamp_point(&self, point: &Vector3) -> Vector3 {
119 let delta_length_sq = self.center.distance_to_squared(point);
120
121 let mut target = Vector3::default();
122 target.copy(point);
123
124 if delta_length_sq > (self.radius * self.radius) {
125 target.sub(&self.center).normalize();
126 target.multiply_scalar(self.radius).add(&self.center);
127 }
128
129 target
130 }
131
132 pub fn get_bounding_box(&self) -> Box3 {
134 let mut target = Box3::default();
135
136 if self.is_empty() {
137 target.make_empty();
139 return target;
140 }
141
142 target.set(&self.center, &self.center);
143 target.expand_by_scalar(self.radius);
144
145 target
146 }
147
148 pub fn apply_matrix4(&mut self, matrix: &Matrix4) -> &mut Self {
150 self.center.apply_matrix4(matrix);
151 self.radius *= matrix.get_max_scale_on_axis();
152
153 self
154 }
155
156 pub fn translate(&mut self, offset: &Vector3) -> &mut Self {
158 self.center.add(offset);
159
160 self
161 }
162
163 pub fn expand_by_point(&mut self, point: &Vector3) -> &mut Self {
165 if self.is_empty() {
166 self.center.copy(point);
167
168 self.radius = 0.0;
169
170 return self;
171 }
172
173 let mut v1 = Vector3::default();
174 v1.sub_vectors(point, &self.center);
175
176 let length_sq = v1.length_sq();
177
178 if length_sq > (self.radius * self.radius) {
179 let length = length_sq.sqrt();
182
183 let delta = (length - self.radius) * 0.5;
184
185 self.center.add_scaled_vector(&v1, delta / length);
186
187 self.radius += delta;
188 }
189
190 self
191 }
192
193 pub fn union(&mut self, sphere: &Self) -> &mut Self {
195 if sphere.is_empty() {
196 return self;
197 }
198
199 if self.is_empty() {
200 self.copy(sphere);
201
202 return self;
203 }
204
205 if self.center.equals(&sphere.center) {
206 self.radius = js_max(self.radius, sphere.radius);
207 } else {
208 let mut v2 = Vector3::default();
209 v2.sub_vectors(&sphere.center, &self.center)
210 .set_length(sphere.radius);
211
212 let mut v1 = Vector3::default();
213 let point = *v1.copy(&sphere.center).add(&v2);
214 self.expand_by_point(&point);
215
216 let point = *v1.copy(&sphere.center).sub(&v2);
217 self.expand_by_point(&point);
218 }
219
220 self
221 }
222
223 pub fn equals(&self, sphere: &Self) -> bool {
225 sphere.center.equals(&self.center) && (sphere.radius == self.radius)
226 }
227}