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
//! Safe rust wrapper around the subset of libtess2 I personally need.
//!
//! Your vertices are ordered clockwise when using the clipping functions.

extern crate tess2_sys;
extern crate itertools;

use tess2_sys::*;
use itertools::Itertools;
use std::mem;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Vertex {
    pub x: f32,
    pub y: f32,
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct Triangles {
    pub vertices: Vec<Vertex>,
    pub indices: Vec<u32>,
}

struct Tessellator {
    tess: *mut TESStesselator,
}

enum Orientation {
    Clockwise,
    CounterClockwise,
}

impl Tessellator {
    fn new() -> Self {
        Self { tess: unsafe { tessNewTess(0 as *mut TESSalloc) } }
    }

    fn add_poly(&mut self, poly: &[Vertex], orientation: Orientation) -> Result<(), String> {
        use std::os::raw::c_void;

        if poly.len() < 3 {
            return Err(String::from("A polygon must have at least 3 vertices."));
        }

        let format = |v: &Vertex| vec![v.x, v.y];
        let formatted_vertices: Vec<f32> = match orientation {
            Orientation::Clockwise => poly.iter().flat_map(format).collect(),
            Orientation::CounterClockwise => poly.iter().rev().flat_map(format).collect(),
        };

        unsafe {
            tessAddContour(self.tess,
                           2,
                           (&formatted_vertices[0] as *const f32) as *const c_void,
                           mem::size_of_val(&formatted_vertices[0]) as i32 * 2,
                           poly.len() as i32);
        }

        Ok(())
    }

    fn tessellate(&mut self, rule: TessWindingRule) -> Result<Triangles, String> {
        unsafe {
            use std::slice;
            if tessTesselate(self.tess,
                             rule,
                             TessElementType::TESS_POLYGONS,
                             3,
                             2,
                             0 as *mut TESSreal) != 1 {
                return Err(String::from("Triangulation failed."));
            }

            let raw_triangle_count = tessGetElementCount(self.tess);
            if raw_triangle_count < 1 {
                return Err(String::from("Triangulation failed to yield triangles."));
            };
            let triangle_count = raw_triangle_count as usize;

            let vertex_buffer = slice::from_raw_parts(tessGetVertices(self.tess),
                                                      tessGetVertexCount(self.tess) as usize * 2);
            let triangle_buffer = slice::from_raw_parts(tessGetElements(self.tess),
                                                        triangle_count * 3);

            let xs = vertex_buffer.iter().step(2);
            let ys = vertex_buffer.iter().skip(1).step(2);
            let verts = xs.zip(ys);

            Ok(Triangles {
                   vertices: verts.map(|(x, y)| Vertex { x: *x, y: *y }).collect(),
                   indices: triangle_buffer.iter().map(|i| *i as u32).collect(),
               })
        }
    }
}

impl Drop for Tessellator {
    fn drop(&mut self) {
        unsafe { tessDeleteTess(self.tess) }
    }
}

/// Tessellates the union of the given simple polygon paths.
pub fn fill_union(polies: &[&[Vertex]]) -> Result<Triangles, String> {
    let mut tess = Tessellator::new();
    for poly in polies {
        tess.add_poly(poly, Orientation::Clockwise)?;
    }
    tess.tessellate(TessWindingRule::TESS_WINDING_NONZERO)
}

/// Tessellates the intersection of the given simple polygon paths. To tessellate
/// many, call this function again on the resulting triangles; may become expensive.
pub fn fill_intersection(a: &[Vertex], b: &[Vertex]) -> Result<Triangles, String> {
    let mut tess = Tessellator::new();
    tess.add_poly(a, Orientation::Clockwise)?;
    tess.add_poly(b, Orientation::Clockwise)?;
    tess.tessellate(TessWindingRule::TESS_WINDING_ABS_GEQ_TWO)
}

pub fn fill_difference(base: &[Vertex], subtract: &[&[Vertex]]) -> Result<Triangles, String> {
    let mut tess = Tessellator::new();
    tess.add_poly(base, Orientation::Clockwise)?;
    for sub in subtract {
        tess.add_poly(sub, Orientation::CounterClockwise)?;
    }
    tess.tessellate(TessWindingRule::TESS_WINDING_POSITIVE)
}

/// Fill tessellate a simple polygon path.
pub fn fill(poly: &[Vertex]) -> Result<Triangles, String> {
    fill_union(&[poly])
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn it_works() {
        assert_eq!(fill(&[Vertex { x: 0.0, y: 0.0 },
                          Vertex { x: 1.0, y: 0.0 },
                          Vertex { x: 1.0, y: 1.0 },
                          Vertex { x: 0.0, y: 1.0 }])
                           .expect("triangulation"),
                   Triangles {
                       vertices: vec![Vertex { x: 0.0, y: 1.0 },
                                      Vertex { x: 1.0, y: 0.0 },
                                      Vertex { x: 1.0, y: 1.0 },
                                      Vertex { x: 0.0, y: 0.0 }],
                       indices: vec![0, 1, 2, 1, 0, 3],
                   });
    }

    #[test]
    fn intersection() {
        assert_eq!(fill_intersection(&[Vertex { x: 0.0, y: 0.0 },
                                       Vertex { x: 1.0, y: 0.0 },
                                       Vertex { x: 1.0, y: 1.0 },
                                       Vertex { x: 0.0, y: 1.0 }],
                                     &[Vertex { x: 0.25, y: 0.25 },
                                       Vertex { x: 0.75, y: 0.25 },
                                       Vertex { x: 0.75, y: 0.75 },
                                       Vertex { x: 0.25, y: 0.75 }])
                           .expect("triangulation"),
                   Triangles {
                       vertices: vec![Vertex { x: 0.25, y: 0.75 },
                                      Vertex { x: 0.75, y: 0.25 },
                                      Vertex { x: 0.75, y: 0.75 },
                                      Vertex { x: 0.25, y: 0.25 }],
                       indices: vec![0, 1, 2, 1, 0, 3],
                   });
    }

    #[test]
    fn union() {
        assert_eq!(fill_union(&[&[Vertex { x: 0.0, y: 0.0 },
                                  Vertex { x: 2.0, y: 4.0 },
                                  Vertex { x: 4.0, y: 0.0 }],
                                &[Vertex { x: 0.5, y: 0.0 },
                                  Vertex { x: 2.0, y: 2.0 },
                                  Vertex { x: 3.5, y: 0.0 }]])
                           .expect("triangulation"),
                   Triangles {
                       vertices: vec![Vertex { x: 2.0, y: 2.0 },
                                      Vertex { x: 4.0, y: 0.0 },
                                      Vertex { x: 3.5, y: 0.0 },
                                      Vertex { x: 2.0, y: 4.0 },
                                      Vertex { x: 0.5, y: 0.0 },
                                      Vertex { x: 0.0, y: 0.0 }],
                       indices: vec![0, 1, 2, 0, 3, 1, 4, 3, 0, 3, 4, 5, 2, 4, 0],
                   });
    }

    #[test]
    fn difference() {
        assert_eq!(fill_difference(&[Vertex { x: 0.0, y: 0.0 },
                                     Vertex { x: 2.0, y: 4.0 },
                                     Vertex { x: 4.0, y: 0.0 }],
                                   &[&[Vertex { x: 0.5, y: 0.0 },
                                       Vertex { x: 2.0, y: 2.0 },
                                       Vertex { x: 3.5, y: 0.0 }]])
                           .expect("triangulation"),
                   Triangles {
                       vertices: vec![Vertex { x: 2.0, y: 2.0 },
                                      Vertex { x: 4.0, y: 0.0 },
                                      Vertex { x: 3.5, y: 0.0 },
                                      Vertex { x: 2.0, y: 4.0 },
                                      Vertex { x: 0.5, y: 0.0 },
                                      Vertex { x: 0.0, y: 0.0 }],
                       indices: vec![0, 1, 2, 0, 3, 1, 4, 3, 0, 3, 4, 5],
                   });
    }
}