rust_3d/
is_3d.rs

1/*
2Copyright 2016 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! Is3D trait used for types which are positioned within the 3D space
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// Is3D is a trait used for types which are positioned within the 3D space
30pub trait Is3D: IsND {
31    /// Should return the x-coordinate
32    fn x(&self) -> f64;
33    /// Should return the y-coordinate
34    fn y(&self) -> f64;
35    /// Should return the z-coordinate
36    fn z(&self) -> f64;
37
38    /// Returns the position as x,y,z array
39    #[inline(always)]
40    fn xyz(&self) -> [f64; 3] {
41        [self.x(), self.y(), self.z()]
42    }
43    /// Returns the components of the position as array
44    #[inline(always)]
45    fn xy(&self) -> [f64; 2] {
46        [self.x(), self.y()]
47    }
48    /// Returns the components of the position as array
49    #[inline(always)]
50    fn xz(&self) -> [f64; 2] {
51        [self.x(), self.z()]
52    }
53    /// Returns the components of the position as array
54    #[inline(always)]
55    fn yz(&self) -> [f64; 2] {
56        [self.y(), self.z()]
57    }
58    /// Calculates the dot product with another Is3D
59    #[inline(always)]
60    fn dot(&self, other: &dyn Is3D) -> f64 {
61        self.x() * other.x() + self.y() * other.y() + self.z() * other.z()
62    }
63    /// The absolute / length of this position
64    #[inline(always)]
65    fn abs(&self) -> NonNegative {
66        NonNegative::new(((self.x()).powi(2) + (self.y()).powi(2) + (self.z()).powi(2)).sqrt())
67            .unwrap()
68    }
69    /// Calculates the angle to the other Is3D in radians
70    fn rad_to(&self, other: &dyn Is3D) -> Result<Rad> {
71        if self.abs().get() == 0.0 || other.abs().get() == 0.0 {
72            return Err(ErrorKind::CantCalculateAngleIfZeroLength);
73        }
74        Ok(Rad {
75            val: (self.dot(other) / (self.abs() * other.abs()).get()).acos(),
76        })
77    }
78    /// Transforms the position in a "x y z" string. E.g. "3.72 5.99 1.01"
79    fn to_str(&self) -> String {
80        let sx: String = self.x().to_string();
81        let sy: String = self.y().to_string();
82        let sz: String = self.z().to_string();
83
84        sx + " " + &sy + " " + &sz
85    }
86}
87
88impl<P> HasDistanceTo<P> for dyn Is3D
89where
90    P: Is3D,
91{
92    fn sqr_distance(&self, other: &P) -> NonNegative {
93        NonNegative::new(
94            (self.x() - other.x()).powi(2)
95                + (self.y() - other.y()).powi(2)
96                + (self.z() - other.z()).powi(2),
97        )
98        .unwrap()
99    }
100}