1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::{CodePair, DxfError, DxfResult};
/// Represents a simple point in Cartesian space.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Point {
/// The X value of the point.
pub x: f64,
/// The Y value of the point.
pub y: f64,
/// The Z value of the point.
pub z: f64,
}
#[macro_export]
///Generates a point in the xyz plane based on the following pattern:
///```
///use dxf::point;
///use dxf::Point;
/////Generate a point at (1,2,3)
///let p1 = point!(1.0,2.0,3.0);
/////Generate a point at (1,2,0)
///let p2 = point!(1.0,2.0);
/////Generate a point at (1,0,0)
///let p3 = point!(1.0);
///```
macro_rules! point {
($x:expr, $y:expr, $z:expr) => {
Point {
x: $x as f64,
y: $y as f64,
z: $z as f64,
}
};
($x:expr, $y:expr) => {
Point {
x: $x as f64,
y: $y as f64,
z: 0.0,
}
};
($x:expr) => {
Point {
x: $x as f64,
y: 0.0,
z: 0.0,
}
};
}
impl Point {
/// Creates a new `Point` with the specified values.
pub fn new(x: f64, y: f64, z: f64) -> Point {
Point { x, y, z }
}
/// Returns a point representing the origin of (0, 0, 0).
pub fn origin() -> Point {
Point::new(0.0, 0.0, 0.0)
}
pub(crate) fn set(&mut self, pair: &CodePair) -> DxfResult<()> {
match pair.code {
10 => self.x = pair.assert_f64()?,
20 => self.y = pair.assert_f64()?,
30 => self.z = pair.assert_f64()?,
_ => {
return Err(DxfError::UnexpectedCodePair(
pair.clone(),
String::from("expected code [10, 20, 30] for point"),
))
}
}
Ok(())
}
pub fn tuple(&self) -> (f64, f64, f64) {
(self.x, self.y, self.z)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Tests whether tuple conversion works as intended and Point doesn't get consumed.
#[test]
fn tuple_conversion_case() {
let p = Point::new(1.0, 1.0, 1.0);
let t: (f64, f64, f64) = p.tuple();
dbg!(&p);
dbg!(&t);
assert_eq!(t, p.tuple())
}
#[test]
fn test_point_macro() {
let p = point!(1, 2, 3.5);
assert_eq!(p.x, 1.0);
assert_eq!(p.y, 2.0);
assert_eq!(p.z, 3.5);
}
}