rust_3d/is_buildable_2d.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//! IsBuildable2D trait used for types which are positioned in 2D space and can be constructed
24
25use core::str::FromStr;
26use std::hash::Hash;
27
28use crate::*;
29
30//------------------------------------------------------------------------------
31
32/// IsBuildable2D is a trait used for types which are positioned in 2D space and can be constructed
33pub trait IsBuildable2D:
34 Sized + Is2D + IsBuildableND + Eq + PartialEq + Ord + PartialOrd + Hash
35{
36 /// Should build an object from x and y coordinates
37 fn new(x: f64, y: f64) -> Self;
38 /// Should use the coordinates of another as its own
39 fn from<P>(&mut self, other: &P)
40 where
41 P: Is2D;
42
43 /// Uses the coordinates of other to create a new
44 #[inline(always)]
45 fn new_from<P>(other: &P) -> Self
46 where
47 P: Is2D,
48 {
49 Self::new(other.x(), other.y())
50 }
51 /// Returns this with normalized values
52 fn normalized(&self) -> Result<Self> {
53 let l = self.abs();
54 if l.get() == 0.0 {
55 return Err(ErrorKind::NormalizeVecWithoutLength);
56 }
57 Ok(Self::new(self.x() / l.get(), self.y() / l.get()))
58 }
59 /// Returns a new object with 0/0 as coordinates
60 #[inline(always)]
61 fn zero() -> Self {
62 Self::new(0.0, 0.0)
63 }
64 /// Applies a matrix to this
65 fn multiply_m(&self, m: &Matrix3) -> Self {
66 let x = self.x() * m.data[0][0] + self.y() * m.data[0][1] + m.data[0][2];
67 let y = self.x() * m.data[1][0] + self.y() * m.data[1][1] + m.data[1][2];
68 Self::new(x, y)
69 }
70 /// Creates this from a "x y" string. E.g. "4.3 17.29"
71 fn parse(text: &str) -> Result<Self> {
72 let mut words = text.split(" ").skip_empty_string();
73
74 let x = f64::from_str(words.next().ok_or(ErrorKind::ParseError)?)?;
75 let y = f64::from_str(words.next().ok_or(ErrorKind::ParseError)?)?;
76
77 if words.next().is_none() {
78 Ok(Self::new(x, y))
79 } else {
80 Err(ErrorKind::ParseError)
81 }
82 }
83
84 /// Returns the center between this and other
85 fn center<P>(&self, other: &P) -> Self
86 where
87 P: Is2D,
88 {
89 Self::new(0.5 * (self.x() + other.x()), 0.5 * (self.y() + other.y()))
90 }
91}