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
use draw::{self, Drawing};
use draw::properties::{spatial, ColorScalar, Draw, Drawn, IntoDrawn, Primitive, Rgba, SetColor, SetDimensions, SetOrientation, SetPosition};
use draw::properties::spatial::{dimension, orientation, position};
use geom;
use math::{BaseFloat, Vector2};
#[derive(Clone, Debug)]
pub struct Ellipse<S = geom::DefaultScalar> {
spatial: spatial::Properties<S>,
color: Option<Rgba>,
resolution: Option<usize>,
}
impl<S> Ellipse<S>
where
S: BaseFloat,
{
pub fn radius(self, radius: S) -> Self {
let side = radius * (S::one() + S::one());
self.w_h(side, side)
}
pub fn resolution(mut self, resolution: usize) -> Self {
self.resolution = Some(resolution);
self
}
}
impl<S> IntoDrawn<S> for Ellipse<S>
where
S: BaseFloat,
{
type Vertices = draw::mesh::vertex::IterFromPoint2s<geom::ellipse::TriangleVertices<S>, S>;
type Indices = geom::ellipse::TriangleIndices;
fn into_drawn(self, draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
let Ellipse {
spatial,
color,
resolution,
} = 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"
);
const DEFAULT_RESOLUTION: usize = 50;
let default_w = || S::from(100.0).unwrap();
let default_h = || S::from(100.0).unwrap();
let w = maybe_x.unwrap_or_else(default_w);
let h = maybe_y.unwrap_or_else(default_h);
let rect = geom::Rect::from_wh(Vector2 { x: w, y: h });
let resolution = resolution.unwrap_or(DEFAULT_RESOLUTION);
let color = color
.or_else(|| {
draw.theme(|theme| {
theme
.color
.primitive
.get(&draw::theme::Primitive::Ellipse)
.map(|&c| c)
})
})
.unwrap_or(draw.theme(|t| t.color.default));
let ellipse = geom::Ellipse::new(rect, resolution);
let (points, indices) = ellipse.triangle_indices();
let vertices = draw::mesh::vertex::IterFromPoint2s::new(points, color);
(spatial, vertices, indices)
}
}
impl<S> Default for Ellipse<S> {
fn default() -> Self {
let spatial = Default::default();
let color = Default::default();
let resolution = Default::default();
Ellipse {
spatial,
color,
resolution,
}
}
}
impl<S> SetOrientation<S> for Ellipse<S> {
fn properties(&mut self) -> &mut orientation::Properties<S> {
SetOrientation::properties(&mut self.spatial)
}
}
impl<S> SetPosition<S> for Ellipse<S> {
fn properties(&mut self) -> &mut position::Properties<S> {
SetPosition::properties(&mut self.spatial)
}
}
impl<S> SetDimensions<S> for Ellipse<S> {
fn properties(&mut self) -> &mut dimension::Properties<S> {
SetDimensions::properties(&mut self.spatial)
}
}
impl<S> SetColor<ColorScalar> for Ellipse<S> {
fn rgba_mut(&mut self) -> &mut Option<Rgba> {
SetColor::rgba_mut(&mut self.color)
}
}
impl<S> From<Ellipse<S>> for Primitive<S> {
fn from(prim: Ellipse<S>) -> Self {
Primitive::Ellipse(prim)
}
}
impl<S> Into<Option<Ellipse<S>>> for Primitive<S> {
fn into(self) -> Option<Ellipse<S>> {
match self {
Primitive::Ellipse(prim) => Some(prim),
_ => None,
}
}
}
impl<'a, S> Drawing<'a, Ellipse<S>, S>
where
S: BaseFloat,
{
pub fn radius(self, radius: S) -> Self {
self.map_ty(|ty| ty.radius(radius))
}
pub fn resolution(self, resolution: usize) -> Self {
self.map_ty(|ty| ty.resolution(resolution))
}
}