rust_3d/
is_editable_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//! IsEditable3D trait used for types which are positioned in 3D space and their position can be changed
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29/// IsEditable3D is a trait used for types which are positioned in 3D space and their position can be changed
30pub trait IsEditable3D: Is3D + IsEditableND {
31    /// Should set the position in x
32    fn set_x(&mut self, val: f64);
33    /// Should set the position in y
34    fn set_y(&mut self, val: f64);
35    /// Should set the position in z
36    fn set_z(&mut self, val: f64);
37
38    /// Sets the position from x, y and z values
39    #[inline(always)]
40    fn set_xyz(&mut self, x: f64, y: f64, z: f64) {
41        self.set_x(x);
42        self.set_y(y);
43        self.set_z(z);
44    }
45    /// Updates the position with x and y values
46    #[inline(always)]
47    fn set_xy(&mut self, x: f64, y: f64) {
48        self.set_x(x);
49        self.set_y(y);
50    }
51    /// Updates the position with x and z values
52    #[inline(always)]
53    fn set_xz(&mut self, x: f64, z: f64) {
54        self.set_x(x);
55        self.set_z(z);
56    }
57    /// Updates the position with y and z values
58    #[inline(always)]
59    fn set_yz(&mut self, y: f64, z: f64) {
60        self.set_y(y);
61        self.set_z(z);
62    }
63    /// Increases distance towards other by factor
64    fn increase_distance_to_by<P>(&mut self, other: &P, factor: Positive)
65    where
66        P: Is3D,
67    {
68        let x = other.x() + factor.get() * (self.x() - other.x());
69        let y = other.y() + factor.get() * (self.y() - other.y());
70        let z = other.z() + factor.get() * (self.z() - other.z());
71
72        self.set_xyz(x, y, z);
73    }
74
75    /// Adds the coordinates of other onto this. x = x + other.x ...
76    fn add<P>(&mut self, other: &P)
77    where
78        P: Is3D,
79    {
80        let x = self.x() + other.x();
81        let y = self.y() + other.y();
82        let z = self.z() + other.z();
83        self.set_x(x);
84        self.set_y(y);
85        self.set_z(z);
86    }
87
88    /// Subtracts the coordinates of other from this. x = x - other.x ...
89    fn subtract<P>(&mut self, other: &P)
90    where
91        P: Is3D,
92    {
93        let x = self.x() - other.x();
94        let y = self.y() - other.y();
95        let z = self.z() - other.z();
96        self.set_x(x);
97        self.set_y(y);
98        self.set_z(z);
99    }
100
101    /// Scales the coordinates by applying a factor to all of them
102    fn scale_pos(&mut self, val: f64) {
103        let x = val * self.x();
104        let y = val * self.y();
105        let z = val * self.z();
106        self.set_x(x);
107        self.set_y(y);
108        self.set_z(z);
109    }
110}