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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use draw::{self, Drawing};
use draw::properties::{spatial, ColorScalar, Draw, Drawn, IntoDrawn, Primitive, Rgba, SetColor, SetOrientation, SetPosition};
use draw::properties::spatial::{orientation, position};
use geom;
use math::{BaseFloat, Point2};
#[derive(Clone, Debug)]
pub struct Line<S = geom::DefaultScalar> {
position: position::Properties<S>,
orientation: orientation::Properties<S>,
capped: geom::line::Capped<S>,
color: Option<Rgba>,
}
pub const DEFAULT_ROUND_RESOLUTION: usize = 25;
impl<S> Line<S>
where
S: BaseFloat,
{
pub fn new(start: Point2<S>, end: Point2<S>, half_thickness: S) -> Self {
let color = Default::default();
let position = Default::default();
let orientation = Default::default();
let line = geom::Line::new(start, end, half_thickness);
let cap = geom::line::Cap::Butt;
let capped = geom::line::Capped { line, cap };
Line { position, orientation, capped, color }
}
pub fn thickness(self, thickness: S) -> Self {
self.half_thickness(thickness / (S::one() + S::one()))
}
pub fn half_thickness(mut self, half_thickness: S) -> Self {
self.capped.line.half_thickness = half_thickness.abs();
self
}
pub fn start(mut self, start: Point2<S>) -> Self {
self.capped.line.start = start;
self
}
pub fn end(mut self, end: Point2<S>) -> Self {
self.capped.line.end = end;
self
}
pub fn points(self, start: Point2<S>, end: Point2<S>) -> Self {
self.start(start).end(end)
}
pub fn caps_round(self) -> Self {
self.caps_round_with_resolution(DEFAULT_ROUND_RESOLUTION)
}
pub fn caps_round_with_resolution(mut self, resolution: usize) -> Self {
self.capped.cap = geom::line::Cap::Round { resolution };
self
}
pub fn caps_square(mut self) -> Self {
self.capped.cap = geom::line::Cap::Square;
self
}
}
impl<S> IntoDrawn<S> for Line<S>
where
S: BaseFloat,
{
type Vertices = draw::mesh::vertex::IterFromPoint2s<geom::line::CappedVertices<S>, S>;
type Indices = geom::polygon::TriangleIndices;
fn into_drawn(self, draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
let Line {
capped,
position,
orientation,
color,
} = self;
let color = color
.or_else(|| {
draw.theme(|theme| {
theme
.color
.primitive
.get(&draw::theme::Primitive::Line)
.map(|&c| c)
})
})
.unwrap_or(draw.theme(|t| t.color.default));
let dimensions = Default::default();
let spatial = spatial::Properties { dimensions, position, orientation };
let points = capped.vertices();
let indices = geom::polygon::triangle_indices(points.len());
let vertices = draw::mesh::vertex::IterFromPoint2s::new(points, color);
(spatial, vertices, indices)
}
}
impl<S> From<geom::Line<S>> for Line<S>
where
S: BaseFloat,
{
fn from(line: geom::Line<S>) -> Self {
let cap = <_>::default();
let capped = geom::line::Capped { line, cap };
capped.into()
}
}
impl<S> From<geom::line::Capped<S>> for Line<S>
where
S: BaseFloat,
{
fn from(capped: geom::line::Capped<S>) -> Self {
let position = <_>::default();
let orientation = <_>::default();
let color = <_>::default();
Line { capped, position, orientation, color }
}
}
impl<S> Default for Line<S>
where
S: BaseFloat,
{
fn default() -> Self {
let zero = S::zero();
let fifty = S::from(50.0).unwrap();
let half_thickness = S::one();
let left = -fifty;
let right = fifty;
let a = Point2 { x: left, y: zero };
let b = Point2 { x: right, y: zero };
Line::new(a, b, half_thickness)
}
}
impl<S> SetOrientation<S> for Line<S> {
fn properties(&mut self) -> &mut orientation::Properties<S> {
SetOrientation::properties(&mut self.orientation)
}
}
impl<S> SetPosition<S> for Line<S> {
fn properties(&mut self) -> &mut position::Properties<S> {
SetPosition::properties(&mut self.position)
}
}
impl<S> SetColor<ColorScalar> for Line<S> {
fn rgba_mut(&mut self) -> &mut Option<Rgba> {
SetColor::rgba_mut(&mut self.color)
}
}
impl<S> From<Line<S>> for Primitive<S> {
fn from(prim: Line<S>) -> Self {
Primitive::Line(prim)
}
}
impl<S> Into<Option<Line<S>>> for Primitive<S> {
fn into(self) -> Option<Line<S>> {
match self {
Primitive::Line(prim) => Some(prim),
_ => None,
}
}
}
impl<'a, S> Drawing<'a, Line<S>, S>
where
S: BaseFloat,
{
pub fn thickness(self, thickness: S) -> Self {
self.map_ty(|ty| ty.thickness(thickness))
}
pub fn half_thickness(self, half_thickness: S) -> Self {
self.map_ty(|ty| ty.half_thickness(half_thickness))
}
pub fn start(self, start: Point2<S>) -> Self {
self.map_ty(|ty| ty.start(start))
}
pub fn end(self, end: Point2<S>) -> Self {
self.map_ty(|ty| ty.end(end))
}
pub fn points(self, start: Point2<S>, end: Point2<S>) -> Self {
self.map_ty(|ty| ty.points(start, end))
}
pub fn caps_round(self) -> Self {
self.map_ty(|ty| ty.caps_round())
}
pub fn caps_round_with_resolution(self, resolution: usize) -> Self {
self.map_ty(|ty| ty.caps_round_with_resolution(resolution))
}
pub fn caps_square(self) -> Self {
self.map_ty(|ty| ty.caps_square())
}
}