-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
shape_3d.rs
142 lines (119 loc) · 3.12 KB
/
shape_3d.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::{Shape, Shape2d};
/// A 3-dimensional shape
#[derive(Clone, Debug)]
#[repr(C)]
pub enum Shape3d {
/// The difference of two 3-dimensional shapes
Difference(Box<Difference>),
/// A sweep of 2-dimensional shape along the z-axis
Sweep(Sweep),
/// A transformed 3-dimensional shape
Transform(Box<Transform>),
/// The union of two 3-dimensional shapes
Union(Box<Union>),
}
impl From<Shape3d> for Shape {
fn from(shape: Shape3d) -> Self {
Self::Shape3d(shape.into())
}
}
/// The difference of two 3-dimensional shapes
///
/// # Limitations
///
/// This operation is not supported right now. Using it in a model, will result
/// in the host application to crash.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Difference {
/// The first of the shapes
pub a: Shape3d,
/// The second of the shapes
pub b: Shape3d,
}
impl From<Difference> for Shape {
fn from(shape: Difference) -> Self {
Self::Shape3d(Shape3d::Difference(Box::new(shape)))
}
}
impl From<Difference> for Shape3d {
fn from(shape: Difference) -> Self {
Self::Difference(Box::new(shape))
}
}
/// A transformed 3-dimensional shape
///
/// # Limitations
///
/// Transformations are currently limited to a rotation, followed by a
/// translation.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Transform {
/// The shape being rotated
pub shape: Shape3d,
/// The axis of the rotation
pub axis: [f64; 3],
/// The angle of the rotation
pub angle: f64,
/// The offset of the translation
pub offset: [f64; 3],
}
impl From<Transform> for Shape {
fn from(shape: Transform) -> Self {
Self::Shape3d(Shape3d::Transform(Box::new(shape)))
}
}
impl From<Transform> for Shape3d {
fn from(shape: Transform) -> Self {
Self::Transform(Box::new(shape))
}
}
/// A sweep of a 2-dimensional shape along the z-axis
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Sweep {
/// The 2-dimensional shape being swept
pub shape: Shape2d,
/// The length of the sweep
pub length: f64,
}
impl From<Sweep> for Shape {
fn from(shape: Sweep) -> Self {
Self::Shape3d(Shape3d::Sweep(shape))
}
}
impl From<Sweep> for Shape3d {
fn from(shape: Sweep) -> Self {
Self::Sweep(shape)
}
}
/// The union of two 3-dimensional shapes
///
/// # Limitations
///
/// Support for unions is somewhat limited right now. A union of 2 distinct
/// shapes doesn't really create a new shape, but just an aggregation of the
/// two original shapes.
///
/// This means, for example, that generating the triangle mesh of the union does
/// not result in a proper triangle mesh, but rather the two, possibly
/// intersecting, triangle meshes of the original shapes.
#[derive(Clone, Debug)]
#[repr(C)]
pub struct Union {
/// The first of the shapes
pub a: Shape3d,
/// The second of the shapes
pub b: Shape3d,
}
impl From<Union> for Shape {
fn from(shape: Union) -> Self {
Self::Shape3d(Shape3d::Union(Box::new(shape)))
}
}
impl From<Union> for Shape3d {
fn from(shape: Union) -> Self {
Self::Union(Box::new(shape))
}
}