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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::draw;
use crate::geom;
use crate::geom::graph::node;
use crate::math::BaseFloat;
use std::cell::RefCell;
use std::ops;
pub mod color;
pub mod primitive;
pub mod spatial;
use self::spatial::dimension;
pub use self::color::{IntoRgba, SetColor};
pub use self::primitive::{Ellipse, Line, Primitive, Quad, Rect, Tri};
pub use self::spatial::dimension::SetDimensions;
pub use self::spatial::orientation::SetOrientation;
pub use self::spatial::position::SetPosition;
pub type ColorScalar = color::DefaultScalar;
pub type Rgba = color::DefaultRgba;
pub type Drawn<S, V, I> = (spatial::Properties<S>, V, I);
#[derive(Debug)]
pub struct Draw<'a, S = geom::scalar::Default>
where
S: 'a + BaseFloat,
{
state: RefCell<&'a mut draw::State<S>>,
}
#[derive(Debug)]
pub struct VerticesFromRanges {
pub ranges: draw::IntermediaryVertexDataRanges,
pub fill_color: Option<draw::mesh::vertex::Color>,
}
#[derive(Debug)]
pub struct IndicesFromRange {
pub range: ops::Range<usize>,
}
impl<'a, S> Draw<'a, S>
where
S: BaseFloat,
{
pub fn new(state: &'a mut draw::State<S>) -> Self {
Draw {
state: RefCell::new(state),
}
}
pub fn untransformed_dimension_of<F>(&self, n: &node::Index, point_axis: &F) -> Option<S>
where
F: Fn(&draw::mesh::vertex::Point<S>) -> S,
{
self.state
.borrow_mut()
.untransformed_dimension_of(n, point_axis)
}
pub fn untransformed_x_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().untransformed_x_dimension_of(n)
}
pub fn untransformed_y_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().untransformed_y_dimension_of(n)
}
pub fn untransformed_z_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().untransformed_z_dimension_of(n)
}
pub fn dimension_of<F>(&mut self, n: &node::Index, point_axis: &F) -> Option<S>
where
F: Fn(&draw::mesh::vertex::Point<S>) -> S,
{
self.state.borrow_mut().dimension_of(n, point_axis)
}
pub fn x_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().x_dimension_of(n)
}
pub fn y_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().y_dimension_of(n)
}
pub fn z_dimension_of(&self, n: &node::Index) -> Option<S> {
self.state.borrow_mut().z_dimension_of(n)
}
pub fn theme<F, T>(&self, get: F) -> T
where
F: FnOnce(&draw::Theme) -> T,
{
let state = self.state.borrow();
get(&state.theme)
}
}
pub trait Vertices<S>: Sized {
fn next(&mut self, data: &draw::IntermediaryMesh<S>) -> Option<draw::mesh::Vertex<S>>;
fn into_iter(self, data: &draw::IntermediaryMesh<S>) -> IterVertices<Self, S> {
IterVertices {
vertices: self,
data,
}
}
}
pub trait Indices: Sized {
fn next(&mut self, intermediary_indices: &[usize]) -> Option<usize>;
fn into_iter(self, intermediary_indices: &[usize]) -> IterIndices<Self> {
IterIndices {
indices: self,
intermediary_indices,
}
}
}
pub trait IntoDrawn<S>
where
S: BaseFloat,
{
type Vertices: Vertices<S>;
type Indices: Indices;
fn into_drawn(self, _: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices>;
}
pub struct IterVertices<'a, V, S: 'a> {
vertices: V,
data: &'a draw::IntermediaryMesh<S>,
}
pub struct IterIndices<'a, I> {
indices: I,
intermediary_indices: &'a [usize],
}
impl<S> dimension::Properties<S>
where
S: BaseFloat,
{
pub fn to_scalars(&self, draw: &Draw<S>) -> (Option<S>, Option<S>, Option<S>) {
const EXPECT_DIMENSION: &'static str = "no raw dimension for node";
let x = self.x.as_ref().map(|x| {
x.to_scalar(|n| {
draw.untransformed_x_dimension_of(n)
.expect(EXPECT_DIMENSION)
})
});
let y = self.y.as_ref().map(|y| {
y.to_scalar(|n| {
draw.untransformed_y_dimension_of(n)
.expect(EXPECT_DIMENSION)
})
});
let z = self.z.as_ref().map(|z| {
z.to_scalar(|n| {
draw.untransformed_z_dimension_of(n)
.expect(EXPECT_DIMENSION)
})
});
(x, y, z)
}
}
impl<S, I> Vertices<S> for I
where
I: Iterator<Item = draw::mesh::Vertex<S>>,
{
fn next(&mut self, _data: &draw::IntermediaryMesh<S>) -> Option<draw::mesh::Vertex<S>> {
self.next()
}
}
impl<I> Indices for I
where
I: Iterator<Item = usize>,
{
fn next(&mut self, _intermediary_indices: &[usize]) -> Option<usize> {
self.next()
}
}
impl<'a, V, S> Iterator for IterVertices<'a, V, S>
where
V: Vertices<S>,
{
type Item = draw::mesh::Vertex<S>;
fn next(&mut self) -> Option<Self::Item> {
let IterVertices {
ref mut vertices,
data,
} = *self;
Vertices::next(vertices, data)
}
}
impl<'a, I> Iterator for IterIndices<'a, I>
where
I: Indices,
{
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
let IterIndices {
ref mut indices,
intermediary_indices,
} = *self;
Indices::next(indices, intermediary_indices)
}
}
impl<S> Vertices<S> for VerticesFromRanges
where
S: BaseFloat,
{
fn next(&mut self, mesh: &draw::IntermediaryMesh<S>) -> Option<draw::mesh::Vertex<S>> {
let VerticesFromRanges {
ref mut ranges,
fill_color,
} = *self;
let point = Iterator::next(&mut ranges.points);
let color = Iterator::next(&mut ranges.colors);
let tex_coords = Iterator::next(&mut ranges.tex_coords);
let point = match point {
None => return None,
Some(point_ix) => *mesh
.vertex_data
.points
.get(point_ix)
.expect("no point for point index in IntermediaryMesh"),
};
let color = color
.map(|color_ix| {
*mesh
.vertex_data
.colors
.get(color_ix)
.expect("no color for color index in IntermediaryMesh")
})
.or(fill_color)
.expect("no color for vertex");
let tex_coords = tex_coords
.map(|tex_coords_ix| {
*mesh
.vertex_data
.tex_coords
.get(tex_coords_ix)
.expect("no tex_coords for tex_coords index in IntermediaryMesh")
})
.unwrap_or_else(draw::mesh::vertex::default_tex_coords);
Some(draw::mesh::vertex::new(point, color, tex_coords))
}
}
impl Indices for IndicesFromRange {
fn next(&mut self, intermediary_indices: &[usize]) -> Option<usize> {
Iterator::next(&mut self.range).map(|ix| {
*intermediary_indices
.get(ix)
.expect("index into `intermediary_indices` is out of range")
})
}
}