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
use draw::{self, Drawing};
use draw::properties::{spatial, ColorScalar, Draw, Drawn, IntoDrawn, Primitive, Rgba, SetColor, SetDimensions, SetPosition};
use draw::properties::spatial::{dimension, position};
use geom;
use math::{BaseFloat, ElementWise, Point2, Vector2};
use std::{iter, slice};
#[derive(Clone, Debug)]
pub struct Line<S = geom::DefaultScalar> {
line: geom::Line<S>,
spatial: spatial::Properties<S>,
color: Option<Rgba>,
}
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 spatial = Default::default();
let line = geom::Line::new(start, end, half_thickness);
Line { line, spatial, 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.line.half_thickness = half_thickness;
self
}
pub fn start(mut self, start: Point2<S>) -> Self {
self.line.start = start;
self
}
pub fn end(mut self, end: Point2<S>) -> Self {
self.line.end = end;
self
}
pub fn points(self, start: Point2<S>, end: Point2<S>) -> Self {
self.start(start).end(end)
}
}
impl<S> IntoDrawn<S> for Line<S>
where
S: BaseFloat,
{
type Vertices = draw::mesh::vertex::IterFromPoint2s<geom::line::Vertices<S>, S>;
type Indices = iter::Cloned<slice::Iter<'static, usize>>;
fn into_drawn(self, draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
let Line {
line,
spatial,
color,
} = self;
let (maybe_x, maybe_y, maybe_z) = spatial.dimensions.to_scalars(&draw);
assert!(
maybe_z.is_none(),
"z dimension support for ellipse is unimplemented"
);
let mut quad = line.quad_corners();
if maybe_x.is_some() || maybe_y.is_some() {
let rect = line.bounding_rect();
let centroid = line.centroid();
let x_scale = maybe_x.map(|x| x / rect.w()).unwrap_or_else(S::one);
let y_scale = maybe_y.map(|y| y / rect.h()).unwrap_or_else(S::one);
let scale = Vector2 { x: x_scale, y: y_scale };
let (a, b, c, d) = quad.into();
let translate = |v: Point2<S>| centroid + ((v - centroid).mul_element_wise(scale));
let new_a = translate(a);
let new_b = translate(b);
let new_c = translate(c);
let new_d = translate(d);
quad = geom::Quad([new_a, new_b, new_c, new_d]);
}
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 points = quad.vertices();
let vertices = draw::mesh::vertex::IterFromPoint2s::new(points, color);
let indices = geom::quad::TRIANGLE_INDICES.iter().cloned();
(spatial, vertices, indices)
}
}
impl<S> From<geom::Line<S>> for Line<S>
where
S: BaseFloat,
{
fn from(line: geom::Line<S>) -> Self {
let spatial = <_>::default();
let color = <_>::default();
Line { line, spatial, 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> SetPosition<S> for Line<S> {
fn properties(&mut self) -> &mut position::Properties<S> {
SetPosition::properties(&mut self.spatial)
}
}
impl<S> SetDimensions<S> for Line<S> {
fn properties(&mut self) -> &mut dimension::Properties<S> {
SetDimensions::properties(&mut self.spatial)
}
}
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))
}
}