1use core::{
2 cmp::Ordering,
3 f64::consts::PI,
4 fmt::Debug,
5 ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
6};
7use libm::{atan, atan2, fabs, fmod, log, sin, sinh, sqrt};
8use serde::{Deserialize, Serialize};
9
10use crate::*;
12
13#[derive(Serialize, Deserialize, Debug, Clone, Default)]
15#[repr(C)]
16pub struct VectorPoint<M: Clone = MValue> {
17 pub x: f64,
19 pub y: f64,
21 pub z: Option<f64>,
23 #[serde(skip_serializing_if = "Option::is_none")]
25 pub m: Option<M>,
26 #[serde(skip)]
28 pub t: Option<f64>,
29}
30impl VectorPoint<MValue> {
31 pub fn from_xy(x: f64, y: f64) -> Self {
33 Self { x, y, z: None, m: None, t: None }
34 }
35
36 pub fn from_xyz(x: f64, y: f64, z: f64) -> Self {
38 Self { x, y, z: Some(z), m: None, t: None }
39 }
40}
41impl<M: Clone> VectorPoint<M> {
42 pub fn new(x: f64, y: f64, z: Option<f64>, m: Option<M>) -> Self {
44 Self { x, y, z, m, t: None }
45 }
46
47 pub fn new_xy(x: f64, y: f64, m: Option<M>) -> Self {
49 Self { x, y, z: None, m, t: None }
50 }
51
52 pub fn new_xyz(x: f64, y: f64, z: f64, m: Option<M>) -> Self {
54 Self { x, y, z: Some(z), m, t: None }
55 }
56
57 pub fn project(&mut self, bbox: Option<&mut BBox3D>) {
59 let y = self.y;
60 let x = self.x;
61 let sin = sin((y * PI) / 180.);
62 let y2 = 0.5 - (0.25 * log((1. + sin) / (1. - sin))) / PI;
63 self.x = x / 360. + 0.5;
64 self.y = y2.clamp(0., 1.);
65
66 if let Some(bbox) = bbox {
67 bbox.extend_from_point(self)
68 };
69 }
70
71 pub fn unproject(&mut self) {
73 let lon = (self.x - 0.5) * 360.;
74 let y2 = 0.5 - self.y;
75 let lat = atan(sinh(PI * (y2 * 2.))).to_degrees();
76
77 self.x = lon;
78 self.y = lat;
79 }
80
81 pub fn is_empty(&self) -> bool {
83 let zero = f64::default();
84 self.x == zero && self.y == zero && (self.z.is_none() || self.z.unwrap() == zero)
85 }
86
87 pub fn face(&self, f: u8) -> f64 {
89 if f == 0 {
90 self.x
91 } else if f == 1 {
92 self.y
93 } else {
94 self.z.unwrap_or(0.)
95 }
96 }
97
98 pub fn modulo(self, modulus: f64) -> Self {
100 let modulus = fabs(modulus); Self {
102 x: fmod(self.x, modulus),
103 y: fmod(self.y, modulus),
104 z: self.z.map(|z| fmod(z, modulus)),
105 m: self.m,
106 t: None,
107 }
108 }
109
110 pub fn angle<M2: Clone>(&self, b: &VectorPoint<M2>) -> f64 {
114 atan2(self.cross(b).norm(), self.dot(b))
115 }
116
117 pub fn cross<M2: Clone>(&self, b: &VectorPoint<M2>) -> Self {
119 if let (Some(z), Some(bz)) = (self.z, b.z) {
120 Self::new_xyz(
121 self.y * bz - z * b.y,
122 z * b.x - self.x * bz,
123 self.x * b.y - self.y * b.x,
124 None,
125 )
126 } else {
127 Self::new_xy(self.x * b.y - self.y * b.x, self.y * b.x - self.x * b.y, None)
128 }
129 }
130
131 pub fn dot<M2: Clone>(&self, b: &VectorPoint<M2>) -> f64 {
133 if let (Some(z), Some(bz)) = (self.z, b.z) {
134 self.x * b.x + self.y * b.y + z * bz
135 } else {
136 self.x * b.x + self.y * b.y
137 }
138 }
139
140 pub fn abs(&self) -> Self {
142 Self::new(fabs(self.x), fabs(self.y), self.z.map(fabs), None)
143 }
144
145 pub fn invert(&self) -> Self {
147 -self
148 }
149
150 pub fn len(&self) -> f64 {
152 self.norm()
153 }
154
155 pub fn norm(&self) -> f64 {
157 sqrt(self.norm2())
158 }
159
160 pub fn norm2(&self) -> f64 {
162 self.dot(self)
163 }
164
165 pub fn normalize(&mut self) {
167 let len = self.len();
168 if len > 0.0 {
169 self.x /= len;
170 self.y /= len;
171 if let Some(z) = self.z {
172 self.z = Some(z / len);
173 }
174 }
175 }
176
177 pub fn distance<M2: Clone>(&self, b: &VectorPoint<M2>) -> f64 {
179 let d: VectorPoint<M> = self - b;
180 d.len()
181 }
182
183 pub fn largest_abs_component(&self) -> u8 {
185 let tmp = self.abs();
186 let tmp_z = tmp.z.unwrap_or(-2.);
187 if tmp.x > tmp.y {
188 if tmp.x > tmp_z {
189 0
190 } else {
191 2
192 }
193 } else if tmp.y > tmp_z {
194 1
195 } else {
196 2
197 }
198 }
199
200 pub fn intermediate<M2: Clone>(&self, b: &VectorPoint<M2>, t: f64) -> Self {
202 if let (Some(z), Some(bz)) = (self.z, b.z) {
203 Self::new_xyz(
204 self.x + ((b.x - self.x) * (1.0 - t)),
205 self.y + ((b.y - self.y) * (1.0 - t)),
206 z + ((bz - z) * (1.0 - t)),
207 None,
208 )
209 } else {
210 Self::new_xy(
211 self.x + ((b.x - self.x) * (1.0 - t)),
212 self.y + ((b.y - self.y) * (1.0 - t)),
213 None,
214 )
215 }
216 }
217
218 pub fn perpendicular(&self) -> Self {
220 if let Some(z) = self.z {
221 let ref_point = if fabs(self.x) > fabs(z) {
222 Self::new_xyz(0., 0., 1., None)
223 } else {
224 Self::new_xyz(1., 0., 0., None)
225 };
226 let cross = self.cross(&ref_point);
227 Self::new_xyz(-self.y, self.x, cross.z.unwrap(), None)
228 } else {
229 Self::new_xy(-self.y, self.x, None)
230 }
231 }
232}
233impl<M: Clone> From<Point> for VectorPoint<M> {
234 fn from(p: Point) -> Self {
235 Self { x: p.0, y: p.1, z: None, m: None, t: None }
236 }
237}
238impl<M: Clone> From<&Point> for VectorPoint<M> {
239 fn from(p: &Point) -> Self {
240 Self { x: p.0, y: p.1, z: None, m: None, t: None }
241 }
242}
243impl<M: Clone> From<Point3D> for VectorPoint<M> {
244 fn from(p: Point3D) -> Self {
245 Self { x: p.0, y: p.1, z: Some(p.2), m: None, t: None }
246 }
247}
248impl<M: Clone> From<&Point3D> for VectorPoint<M> {
249 fn from(p: &Point3D) -> Self {
250 Self { x: p.0, y: p.1, z: Some(p.2), m: None, t: None }
251 }
252}
253impl<M1: Clone, M2: Clone> Add<&VectorPoint<M2>> for &VectorPoint<M1> {
254 type Output = VectorPoint<M1>;
255 fn add(self, other: &VectorPoint<M2>) -> Self::Output {
256 VectorPoint {
257 x: self.x + other.x,
258 y: self.y + other.y,
259 z: match (self.z, other.z) {
261 (Some(z1), Some(z2)) => Some(z1 + z2),
262 _ => None, },
264 m: None, t: None, }
267 }
268}
269impl<M1: Clone, M2: Clone> AddAssign<&VectorPoint<M2>> for VectorPoint<M1> {
270 fn add_assign(&mut self, other: &VectorPoint<M2>) {
271 self.x += other.x;
272 self.y += other.y;
273 if let (Some(z), Some(other_z)) = (self.z, other.z) {
274 self.z = Some(z + other_z);
275 }
276 }
277}
278impl<M: Clone> Add<f64> for &VectorPoint<M> {
279 type Output = VectorPoint<M>;
280 fn add(self, other: f64) -> Self::Output {
281 VectorPoint {
282 x: self.x + other,
283 y: self.y + other,
284 z: self.z.map(|z| z + other),
285 m: None,
286 t: None,
287 }
288 }
289}
290impl<M: Clone> AddAssign<f64> for VectorPoint<M> {
291 fn add_assign(&mut self, other: f64) {
292 self.x += other;
293 self.y += other;
294 if let Some(z) = self.z {
295 self.z = Some(z + other);
296 }
297 }
298}
299impl<M1: Clone, M2: Clone> Sub<&VectorPoint<M2>> for &VectorPoint<M1> {
301 type Output = VectorPoint<M1>;
302 fn sub(self, other: &VectorPoint<M2>) -> Self::Output {
303 VectorPoint {
304 x: self.x - other.x,
305 y: self.y - other.y,
306 z: match (self.z, other.z) {
307 (Some(z1), Some(z2)) => Some(z1 - z2),
308 _ => None, },
310 m: None, t: None, }
313 }
314}
315impl<M1: Clone, M2: Clone> SubAssign<&VectorPoint<M2>> for VectorPoint<M1> {
316 fn sub_assign(&mut self, other: &VectorPoint<M2>) {
317 self.x -= other.x;
318 self.y -= other.y;
319 if let (Some(z), Some(other_z)) = (self.z, other.z) {
320 self.z = Some(z - other_z);
321 }
322 }
323}
324impl<M: Clone> Sub<f64> for &VectorPoint<M> {
325 type Output = VectorPoint<M>;
326 fn sub(self, other: f64) -> Self::Output {
327 VectorPoint {
328 x: self.x - other,
329 y: self.y - other,
330 z: self.z.map(|z| z - other),
331 m: None,
332 t: None,
333 }
334 }
335}
336impl<M: Clone> SubAssign<f64> for VectorPoint<M> {
337 fn sub_assign(&mut self, other: f64) {
338 self.x -= other;
339 self.y -= other;
340 if let Some(z) = self.z {
341 self.z = Some(z - other);
342 }
343 }
344}
345impl<M: Clone> Neg for &VectorPoint<M> {
347 type Output = VectorPoint<M>;
348 fn neg(self) -> Self::Output {
349 VectorPoint { x: -self.x, y: -self.y, z: self.z.map(|z| -z), m: None, t: None }
350 }
351}
352impl<M1: Clone, M2: Clone> Div<&VectorPoint<M2>> for &VectorPoint<M1> {
354 type Output = VectorPoint<M1>;
355 fn div(self, other: &VectorPoint<M2>) -> Self::Output {
356 VectorPoint {
357 x: self.x / other.x,
358 y: self.y / other.y,
359 z: match (self.z, other.z) {
360 (Some(z1), Some(z2)) => Some(z1 / z2),
361 _ => None, },
363 m: None, t: None, }
366 }
367}
368impl<M1: Clone, M2: Clone> DivAssign<&VectorPoint<M2>> for VectorPoint<M1> {
369 fn div_assign(&mut self, other: &VectorPoint<M2>) {
370 self.x /= other.x;
371 self.y /= other.y;
372 if let (Some(z), Some(other_z)) = (self.z, other.z) {
373 self.z = Some(z / other_z);
374 }
375 }
376}
377impl<M: Clone> Div<f64> for &VectorPoint<M> {
378 type Output = VectorPoint<M>;
379 fn div(self, other: f64) -> Self::Output {
380 VectorPoint {
381 x: self.x / other,
382 y: self.y / other,
383 z: self.z.map(|z| z / other),
384 m: None,
385 t: None,
386 }
387 }
388}
389impl<M: Clone> DivAssign<f64> for VectorPoint<M> {
390 fn div_assign(&mut self, other: f64) {
391 self.x /= other;
392 self.y /= other;
393 if let Some(z) = self.z {
394 self.z = Some(z / other);
395 }
396 }
397}
398impl<M1: Clone, M2: Clone> Mul<&VectorPoint<M2>> for &VectorPoint<M1> {
400 type Output = VectorPoint<M1>;
401 fn mul(self, other: &VectorPoint<M2>) -> Self::Output {
402 VectorPoint {
403 x: self.x * other.x,
404 y: self.y * other.y,
405 z: match (self.z, other.z) {
406 (Some(z1), Some(z2)) => Some(z1 * z2),
407 _ => None, },
409 m: None, t: None, }
412 }
413}
414impl<M1: Clone, M2: Clone> MulAssign<&VectorPoint<M2>> for VectorPoint<M1> {
415 fn mul_assign(&mut self, other: &VectorPoint<M2>) {
416 self.x *= other.x;
417 self.y *= other.y;
418 if let (Some(z), Some(other_z)) = (self.z, other.z) {
419 self.z = Some(z * other_z);
420 }
421 }
422}
423impl<M: Clone> Mul<f64> for &VectorPoint<M> {
424 type Output = VectorPoint<M>;
425 fn mul(self, other: f64) -> Self::Output {
426 VectorPoint {
427 x: self.x * other,
428 y: self.y * other,
429 z: self.z.map(|z| z * other),
430 m: None,
431 t: None,
432 }
433 }
434}
435impl<M: Clone> MulAssign<f64> for VectorPoint<M> {
436 fn mul_assign(&mut self, other: f64) {
437 self.x *= other;
438 self.y *= other;
439 if let Some(z) = self.z {
440 self.z = Some(z * other);
441 }
442 }
443}
444impl<M: Clone> Rem<f64> for VectorPoint<M> {
445 type Output = VectorPoint<M>;
446
447 fn rem(self, modulus: f64) -> Self::Output {
448 self.modulo(modulus)
449 }
450}
451impl<M: Clone> RemAssign<f64> for VectorPoint<M> {
452 fn rem_assign(&mut self, modulus: f64) {
453 let modulus = fabs(modulus);
454 self.x = fmod(self.x, modulus);
455 self.y = fmod(self.y, modulus);
456 if let Some(z) = self.z {
457 self.z = Some(fmod(z, modulus));
458 }
459 }
460}
461impl<M: Clone> Eq for VectorPoint<M> {}
462impl<M: Clone> Ord for VectorPoint<M> {
463 fn cmp(&self, other: &VectorPoint<M>) -> Ordering {
464 match self.x.partial_cmp(&other.x) {
465 Some(Ordering::Equal) => {}
466 other => return other.unwrap_or(Ordering::Greater), }
468 match self.y.partial_cmp(&other.y) {
469 Some(Ordering::Equal) => {}
470 other => return other.unwrap_or(Ordering::Greater), }
472 match self.z.partial_cmp(&other.z) {
473 Some(order) => order,
474 None => Ordering::Equal, }
476 }
477}
478impl<M: Clone> PartialEq for VectorPoint<M> {
479 fn eq(&self, other: &VectorPoint<M>) -> bool {
480 self.x == other.x && self.y == other.y && self.z == other.z
481 }
482}
483impl<M: Clone> PartialOrd for VectorPoint<M> {
484 fn partial_cmp(&self, other: &VectorPoint<M>) -> Option<Ordering> {
485 Some(self.cmp(other))
486 }
487}