triangulate/outputs/formats/
deindexed_list.rs

1use std::marker::PhantomData;
2
3use crate::{ListFormat, PolygonList, TriangulationError, List, ListBuilder};
4
5use super::GenericList;
6
7/// A [ListFormat] which outputs the triangle list by their actual vertex values, not their indices
8#[derive(Debug)]
9pub struct DeindexedListFormat<'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>>
10where P::Vertex: Clone {
11    list: L,
12    _phantom: PhantomData<&'p P>,
13}
14
15impl<'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>> DeindexedListFormat<'p, P, L> 
16where P::Vertex: Clone {
17    /// Create a deindexed format which stores its output in the given [List]
18    pub fn new(list: L) -> Self {
19        Self { list, _phantom: PhantomData, }
20    }
21}
22
23impl <'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>> ListFormat<'p, P> for DeindexedListFormat<'p, P, L> 
24where P::Vertex: Clone {
25    type Builder = DeindexedListBuilder<'p, P, L>;
26
27    fn initialize(self, polygon_list: &'p P) -> Result<Self::Builder, <Self::Builder as ListBuilder<'p, P>>::Error> {
28        DeindexedListBuilder::new(self.list, polygon_list)
29    }
30}
31
32pub struct DeindexedListBuilder<'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>> 
33where P::Vertex: Clone {
34    list: GenericList<L, P::Vertex>,
35    polygon_list: &'p P,
36}
37
38impl<'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>> DeindexedListBuilder<'p, P, L> 
39where P::Vertex: Clone {
40    fn new(list: L, polygon_list: &'p P) -> Result<Self, <Self as ListBuilder<'p, P>>::Error> {
41        let list = GenericList::new(list);
42        let l = Self {
43            list,
44            polygon_list,
45        };
46
47        Ok(l)
48    }
49}
50
51impl<'p, P: PolygonList<'p> + ?Sized, L: List<P::Vertex>> ListBuilder<'p, P> for DeindexedListBuilder<'p, P, L> 
52where P::Vertex: Clone {
53    type Output = L;
54    type Error = std::convert::Infallible;
55
56    fn add_triangle(&mut self, vi0: P::Index, vi1: P::Index, vi2: P::Index) -> Result<(), Self::Error> {
57        let (v0, v1, v2) = (self.polygon_list.get_vertex(vi0).clone(), self.polygon_list.get_vertex(vi1).clone(), self.polygon_list.get_vertex(vi2).clone());
58        self.list.new_triangle(v0, v1, v2);
59        Ok(())
60    }
61    
62    fn build(self) -> Result<Self::Output, Self::Error> {
63        Ok(self.list.build())
64    }
65
66    fn fail(self, _error: &TriangulationError<Self::Error>) {
67        self.list.fail();
68    }
69}