rust_3d/
line_segment_3d.rs

1/*
2Copyright 2017 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//! LineSegment3D, a line segment within 3D space
24
25use std::fmt;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31#[derive(Debug, PartialEq, PartialOrd, Eq, Clone, Hash)]
32/// LineSegment3D, a line segment within 3D space
33pub struct LineSegment3D {
34    pub start: Point3D,
35    pub end: Point3D,
36}
37
38impl LineSegment3D {
39    /// Creates a new LineSegment3D from a start and end point
40    pub fn new(start: Point3D, end: Point3D) -> Self {
41        LineSegment3D { start, end }
42    }
43}
44
45impl IsMovable3D for LineSegment3D {
46    fn move_by(&mut self, x: f64, y: f64, z: f64) {
47        self.start.move_by(x, y, z);
48        self.end.move_by(x, y, z);
49    }
50}
51
52impl HasLength for LineSegment3D {
53    fn length(&self) -> f64 {
54        dist_3d(&self.start, &self.end)
55    }
56}
57
58impl HasBoundingBox3DMaybe for LineSegment3D {
59    fn bounding_box_maybe(&self) -> Result<BoundingBox3D> {
60        BoundingBox3D::from_iterator([&self.start, &self.end].iter().map(|x| *x))
61    }
62}
63
64impl HasCenterOfGravity3D for LineSegment3D {
65    fn center_of_gravity(&self) -> Result<Point3D> {
66        Ok(center_3d(&self.start, &self.end))
67    }
68}
69
70impl IsScalable for LineSegment3D {
71    fn scale(&mut self, factor: Positive) {
72        if let Ok(c) = self.bounding_box_maybe().map(|x| x.center_bb()) {
73            self.start.increase_distance_to_by(&c, factor);
74            self.end.increase_distance_to_by(&c, factor);
75        }
76    }
77}
78
79impl IsMatrix4Transformable for LineSegment3D {
80    fn transformed(&self, m: &Matrix4) -> Self {
81        let mut new = self.clone();
82        new.transform(m);
83        new
84    }
85
86    fn transform(&mut self, m: &Matrix4) {
87        self.start.transform(m);
88        self.end.transform(m);
89    }
90}
91
92impl fmt::Display for LineSegment3D {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        write!(
95            f,
96            "({}, {}, {} -> {}, {}, {})",
97            self.start.x(),
98            self.start.y(),
99            self.start.z(),
100            self.end.x(),
101            self.end.y(),
102            self.end.z()
103        )
104    }
105}