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
use crate::draw::mesh::vertex::{IntoPoint, IntoVertex};
use crate::draw::properties::spatial::{self, orientation, position};
use crate::draw::properties::{Draw, Drawn, IntoDrawn, Primitive, SetOrientation, SetPosition};
use crate::draw::{self, Drawing};
use crate::geom::line::join::miter;
use crate::geom::{self, pt2, Point2};
use crate::math::BaseFloat;
use crate::mesh::vertex::{WithColor, WithTexCoords};
use std::iter;

/// A polyline prior to being initialised.
#[derive(Clone, Debug, Default)]
pub struct Vertexless;

/// Properties related to drawing a **Polyline**.
#[derive(Clone, Debug)]
pub struct Polyline<S = geom::scalar::Default> {
    position: position::Properties<S>,
    orientation: orientation::Properties<S>,
    vertex_data_ranges: draw::IntermediaryVertexDataRanges,
}

impl Vertexless {
    /// Draw a polyline whose path is defined by the given list of vertices.
    pub(crate) fn vertices<S, I>(
        self,
        mesh: &mut draw::IntermediaryMesh<S>,
        half_thickness: S,
        vertices: I,
    ) -> Polyline<S>
    where
        S: BaseFloat,
        I: IntoIterator,
        I::Item: IntoVertex<S>,
    {
        let mut vertex_data_ranges = draw::IntermediaryVertexDataRanges::default();
        vertex_data_ranges.points.start = mesh.vertex_data.points.len();
        vertex_data_ranges.colors.start = mesh.vertex_data.colors.len();
        vertex_data_ranges.tex_coords.start = mesh.vertex_data.tex_coords.len();

        fn v_to_pt2<S>(v: draw::mesh::Vertex<S>) -> Point2<S>
        where
            S: BaseFloat,
        {
            pt2(v.x, v.y)
        }

        // For each vertex in the given sequence, generate the miter point pairs. Colour and
        // texture each pair of points by using the colour and texture coords of the original
        // vertex.
        let mut v_iter = vertices.into_iter().map(IntoVertex::into_vertex);
        let mut a = None;
        let mut v = v_iter.next();
        let mut b = v.clone().map(v_to_pt2);
        loop {
            let next_v = v_iter.next();
            let next = next_v.clone().map(v_to_pt2);
            let [l, r] = match miter::next_pair(half_thickness, &mut a, &mut b, next) {
                None => break,
                Some(pair) => pair,
            };

            // `v` should always be `Some` if `Some` next miter pair was yielded.
            let WithTexCoords {
                tex_coords,
                vertex: WithColor { color, .. },
            } = v.clone().expect("no vertex for the next miter pair");

            // A function for pushing the left and right miter points.
            let mut push_point = |point| {
                mesh.vertex_data.points.push(point);
                mesh.vertex_data.colors.push(color);
                mesh.vertex_data.tex_coords.push(tex_coords);
            };
            push_point(l.into_point());
            push_point(r.into_point());

            // Update the vertex used for texturing and colouring the next miter pair.
            if next_v.is_some() {
                v = next_v;
            }
        }

        vertex_data_ranges.points.end = mesh.vertex_data.points.len();
        vertex_data_ranges.colors.end = mesh.vertex_data.colors.len();
        vertex_data_ranges.tex_coords.end = mesh.vertex_data.tex_coords.len();
        Polyline::new(vertex_data_ranges)
    }
}

impl<S> Polyline<S>
where
    S: BaseFloat,
{
    // Initialise a new `Polyline` with its ranges into the intermediary mesh, ready for drawing.
    fn new(vertex_data_ranges: draw::IntermediaryVertexDataRanges) -> Self {
        let orientation = Default::default();
        let position = Default::default();
        Polyline {
            orientation,
            position,
            vertex_data_ranges,
        }
    }
}

impl<'a, S> Drawing<'a, Vertexless, S>
where
    S: BaseFloat,
{
    /// Describe the polyline with some "half_thickness" of the line and the given sequence of
    /// vertices.
    pub fn vertices<I>(self, half_thickness: S, vertices: I) -> Drawing<'a, Polyline<S>, S>
    where
        I: IntoIterator,
        I::Item: IntoVertex<S>,
    {
        self.map_ty_with_vertices(|ty, mesh| ty.vertices(mesh, half_thickness, vertices))
    }
}

impl<S> IntoDrawn<S> for Vertexless
where
    S: BaseFloat,
{
    type Vertices = iter::Empty<draw::mesh::Vertex<S>>;
    type Indices = iter::Empty<usize>;
    fn into_drawn(self, _draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
        let properties = Default::default();
        let vertices = iter::empty();
        let indices = iter::empty();
        (properties, vertices, indices)
    }
}

impl<S> IntoDrawn<S> for Polyline<S>
where
    S: BaseFloat,
{
    type Vertices = draw::properties::VerticesFromRanges;
    type Indices = geom::tri::FlattenIndices<miter::TriangleIndices>;
    fn into_drawn(self, _draw: Draw<S>) -> Drawn<S, Self::Vertices, Self::Indices> {
        let Polyline {
            orientation,
            position,
            vertex_data_ranges,
        } = self;
        let dimensions = spatial::dimension::Properties::default();
        let spatial = spatial::Properties {
            dimensions,
            orientation,
            position,
        };
        let n_points = (vertex_data_ranges.points.end - vertex_data_ranges.points.start) / 2;
        let vertices = draw::properties::VerticesFromRanges {
            ranges: vertex_data_ranges,
            fill_color: None,
        };
        let index_tris = miter::TriangleIndices::new(n_points);
        let indices = geom::tri::flatten_index_tris(index_tris);
        (spatial, vertices, indices)
    }
}

impl<S> SetOrientation<S> for Polyline<S> {
    fn properties(&mut self) -> &mut orientation::Properties<S> {
        SetOrientation::properties(&mut self.orientation)
    }
}

impl<S> SetPosition<S> for Polyline<S> {
    fn properties(&mut self) -> &mut position::Properties<S> {
        SetPosition::properties(&mut self.position)
    }
}

impl<S> From<Vertexless> for Primitive<S> {
    fn from(prim: Vertexless) -> Self {
        Primitive::PolylineVertexless(prim)
    }
}

impl<S> From<Polyline<S>> for Primitive<S> {
    fn from(prim: Polyline<S>) -> Self {
        Primitive::Polyline(prim)
    }
}

impl<S> Into<Option<Vertexless>> for Primitive<S> {
    fn into(self) -> Option<Vertexless> {
        match self {
            Primitive::PolylineVertexless(prim) => Some(prim),
            _ => None,
        }
    }
}

impl<S> Into<Option<Polyline<S>>> for Primitive<S> {
    fn into(self) -> Option<Polyline<S>> {
        match self {
            Primitive::Polyline(prim) => Some(prim),
            _ => None,
        }
    }
}