clipping 0.1.1

Efficient clipping of arbitrary polygons
Documentation
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use std::ptr;
use std::fmt;

use rand;

#[derive(Debug)]
/// Node in a circular doubly linked list
struct Vertex{
    pt: [f64; 2],
    next: *mut Vertex,
    prev: *mut Vertex,
    neighbour: *mut Vertex,
    entry: bool,
    alpha: f64,
    inter: bool,
    checked: bool
}

impl Vertex{

    pub fn new(pt: [f64; 2], alpha: f64, inter: bool, entry: bool, checked: bool)->*mut Self{
        let vertex = Box::new(Self{
            pt: pt,
            next: ptr::null_mut(),
            prev: ptr::null_mut(),
            neighbour: ptr::null_mut(),
            entry: entry,
            alpha: alpha,
            inter: inter,
            checked: checked
        });
        let ptr = Box::into_raw(vertex);

        unsafe{
            ptr.as_mut().unwrap().next = ptr;
            ptr.as_mut().unwrap().prev = ptr;
        }

        ptr
    }

    pub fn clone(vertex: *mut Vertex)->*mut Self{
        unsafe{
            Self::new(
                vertex.as_ref().unwrap().pt,
                vertex.as_ref().unwrap().alpha,
                vertex.as_ref().unwrap().inter,
                vertex.as_ref().unwrap().entry,
                vertex.as_ref().unwrap().checked
            )
        }
    }

    /// Test if the vertex is inside the polygon
    pub fn is_inside(&self, poly: &CPolygon)->bool{
        let mut w = 0;

        let infinity = [-100000. - (rand::random::<f64>() % 100.), -100000. - (rand::random::<f64>() % 100.)];

        for q in poly.iter(){
            unsafe{
                let pt_q = q.as_ref().unwrap().pt;
                let pt_q_next = poly.next(q.as_ref().unwrap().next).as_ref().unwrap().pt;

                match intersect(self.pt, infinity, pt_q, pt_q_next){
                    Some(_) => if !q.as_ref().unwrap().inter{
                        w += 1;
                    },
                    None => {}
                }
            }
        }

        w % 2 != 0
    }

    pub fn set_checked(&mut self){
        self.checked = true;
        if !self.neighbour.is_null(){
            unsafe{
                if !self.neighbour.as_ref().unwrap().checked{
                    self.neighbour.as_mut().unwrap().set_checked();
                }
            }
        }
    }
}

impl fmt::Display for Vertex{
    fn fmt(&self, f: &mut fmt::Formatter)->fmt::Result{
        unsafe{
            let pt = self.pt;
            let npt = self.next.as_ref().unwrap().pt;
            let ppt = self.prev.as_ref().unwrap().pt;

            write!(f, "{:?} <-> {:?} <-> {:?}", ppt, pt, npt)
        }
    }
}

pub struct CPolygon{
    fst: *mut Vertex
}

impl CPolygon{

    /// Create a new empty polygon
    pub fn new()->Self{
        Self{
            fst: ptr::null_mut()
        }
    }

    /// Create a polygon from a list of points
    pub fn from_vec(vec: &Vec<[f64; 2]>)->Self{
        let mut poly = Self::new();

        for v in vec{
            let vertex = Vertex::new(*v, 0., false, true, false);
            poly.add(vertex);
        }

        poly
    }

    fn iter(&self)->CPolyIter{
        CPolyIter::new(self)
    }

    /// Add a vertex at the end of the polygon
    fn add(&mut self, vertex: *mut Vertex){
        if self.fst.is_null(){
            self.fst = vertex;
        }else{
            unsafe{
                let next = self.fst;
                let prev = next.as_ref().unwrap().prev;

                next.as_mut().unwrap().prev = vertex;
                vertex.as_mut().unwrap().next = next;
                vertex.as_mut().unwrap().prev = prev;
                prev.as_mut().unwrap().next = vertex;
            }
        }
    }

    /// Insert and sort a vertex between a specified pair of vertices
    fn insert(&mut self, vertex: *mut Vertex, start: *mut Vertex, end: *mut Vertex){
        unsafe{
            let mut curr = start;

            while curr != end && curr.as_ref().unwrap().alpha < vertex.as_ref().unwrap().alpha{
                curr = curr.as_ref().unwrap().next;
            }

            vertex.as_mut().unwrap().next = curr;
            let prev = curr.as_ref().unwrap().prev;
            vertex.as_mut().unwrap().prev = prev;
            prev.as_mut().unwrap().next = vertex;
            curr.as_mut().unwrap().prev = vertex;
        }
    }

    /// Return the next non intersecting vertex after the one specified
    fn next(&self, v: *mut Vertex)->*mut Vertex{
        unsafe{
            let mut c = v;
            while c.as_ref().unwrap().inter{
                c = c.as_ref().unwrap().next;
            }
            c
        }
    }

    /// Return the first unchecked intersection point in the polygon
    fn first_intersect(&self)->*mut Vertex{
        let mut ve = ptr::null_mut();

        for v in self.iter(){
            ve = v;
            unsafe{
                if v.as_ref().unwrap().inter && !v.as_ref().unwrap().checked{
                    break;
                }
            }
        }

        ve
    }

    /// Return the polygon points as a list of points, clear consecutive equals points
    pub fn points(&self)->Vec<[f64; 2]>{
        let mut poly = vec!();

        for vertex in self.iter(){
            unsafe{
                let pt = vertex.as_ref().unwrap().pt;
                if vertex == self.fst{
                    poly.push(pt);
                }else{
                    if vertex == self.fst.as_ref().unwrap().prev{
                        if self.fst.as_ref().unwrap().pt != pt{
                            poly.push(pt);
                        }
                    }else{
                        let prev = vertex.as_ref().unwrap().prev.as_ref().unwrap().pt;

                        if prev != pt{
                            poly.push(pt);
                        }
                    }
                }
            }
        }

        poly
    }

    /// Check if an unchecked intersection remain in the polygon
    fn unprocessed(&self)->bool{
        for v in self.iter(){
            unsafe{
                if v.as_ref().unwrap().inter && !v.as_ref().unwrap().checked{
                    return true;
                }
            }
        }

        false
    }

    fn phase_one(&mut self, poly: &mut CPolygon){
        for s in self.iter(){
            unsafe{
                if !s.as_ref().unwrap().inter{
                    for c in poly.iter(){
                        if !c.as_ref().unwrap().inter{
                            let s_pt = s.as_ref().unwrap().pt;
                            let s_next_pt = self.next(s.as_ref().unwrap().next).as_ref().unwrap().pt;
                            let c_pt = c.as_ref().unwrap().pt;
                            let c_next_pt = poly.next(c.as_ref().unwrap().next).as_ref().unwrap().pt;

                            match intersect(s_pt, s_next_pt, c_pt, c_next_pt){
                                Some((i, alpha_s, alpha_c)) => {
                                    let mut is = Vertex::new(i, alpha_s, true, false, false);
                                    let mut ic = Vertex::new(i, alpha_c, true, false, false);
                                    is.as_mut().unwrap().neighbour = ic;
                                    ic.as_mut().unwrap().neighbour = is;

                                    let s_next = self.next(s.as_ref().unwrap().next);
                                    self.insert(is, s, s_next);

                                    let c_next = poly.next(c.as_ref().unwrap().next);
                                    poly.insert(ic, c, c_next);
                                },
                                None => {}
                            }
                        }
                    }
                }
            }
        }
    }

    fn phase_two(&mut self, poly: &mut CPolygon, mut s_entry: bool, mut c_entry: bool){
        unsafe{
            s_entry ^= self.fst.as_ref().unwrap().is_inside(poly);
            for s in self.iter(){
                if s.as_ref().unwrap().inter{
                    s.as_mut().unwrap().entry = s_entry;
                    s_entry = !s_entry;
                }
            }

            c_entry ^= poly.fst.as_ref().unwrap().is_inside(self);
            for c in poly.iter(){
                if c.as_ref().unwrap().inter{
                    c.as_mut().unwrap().entry = c_entry;
                    c_entry = !c_entry;
                }
            }
        }
    }

    fn phase_three(&mut self)->Vec<Vec<[f64; 2]>>{
        let mut list = vec!();

        while self.unprocessed(){
            unsafe{
                let mut curr = self.first_intersect();
                let mut clipped = CPolygon::new();
                clipped.add(Vertex::clone(curr));

                loop{
                    curr.as_mut().unwrap().set_checked();
                    if curr.as_ref().unwrap().entry{
                        loop {
                            curr = curr.as_ref().unwrap().next;
                            clipped.add(Vertex::clone(curr));
                            if curr.as_ref().unwrap().inter{
                                break;
                            }
                        }
                    }else{
                        loop {
                            curr = curr.as_ref().unwrap().prev;
                            clipped.add(Vertex::clone(curr));
                            if curr.as_ref().unwrap().inter{
                                break;
                            }
                        }
                    }

                    curr = curr.as_ref().unwrap().neighbour;
                    if curr.as_ref().unwrap().checked{
                        break;
                    }
                }

                list.push(clipped.points());
            }
        }

        if list.is_empty(){
            let mut clipped = vec!();
            for s in self.iter(){
                unsafe{
                    clipped.push(s.as_ref().unwrap().pt);
                }
            }
            list.push(clipped);
        }

        list
    }

    pub fn clip(&mut self, poly: &mut CPolygon, s_entry: bool, c_entry: bool)->Vec<Vec<[f64; 2]>>{
        self.phase_one(poly);
        self.phase_two(poly, s_entry, c_entry);
        self.phase_three()
    }

    pub fn union(&mut self, poly: &mut CPolygon)->Vec<Vec<[f64; 2]>>{
        self.clip(poly, false, false)
    }

    pub fn intersection(&mut self, poly: &mut CPolygon)->Vec<Vec<[f64; 2]>>{
        self.clip(poly, true, true)
    }

    pub fn difference(&mut self, poly: &mut CPolygon)->Vec<Vec<[f64; 2]>>{
        self.clip(poly, false, true)
    }
}

impl Drop for CPolygon{
    fn drop(&mut self){
        let mut curr = self.fst;

        if !curr.is_null(){
            loop{
                unsafe{
                    let next = curr.as_ref().unwrap().next;
                    Box::from_raw(curr);
                    curr = next;
                    if curr == self.fst{
                        break;
                    }
                }
            }
        }
    }
}

struct CPolyIter{
    first: *mut Vertex,
    curr: *mut Vertex,
    fst: bool
}

impl CPolyIter{

    pub fn new(poly: &CPolygon)->Self{
        Self{
            first: poly.fst,
            curr: poly.fst,
            fst: true
        }
    }
}

impl Iterator for CPolyIter{
    type Item = *mut Vertex;

    fn next(&mut self)->Option<Self::Item>{
        if self.fst{
            if self.curr.is_null(){
                None
            }else{
                let i = self.curr;
                unsafe{
                    self.curr = self.curr.as_ref().unwrap().next;
                }
                self.fst = false;
                Some(i)
            }
        }else{
            if self.curr == self.first{
                None
            }else{
                let i = self.curr;
                unsafe{
                    self.curr = self.curr.as_ref().unwrap().next;
                }
                Some(i)
            }
        }
    }
}


/// Test the intersection of two line and get the intersection point and alphas
fn intersect(s1: [f64; 2], s2: [f64; 2], c1: [f64; 2], c2: [f64; 2])->Option<([f64; 2], f64, f64)>{
    let den = (c2[1] - c1[1]) * (s2[0] - s1[0]) - (c2[0] - c1[0]) * (s2[1] - s1[1]);

    if den == 0.{
        return None;
    }

    let us = ((c2[0] - c1[0]) * (s1[1] - c1[1]) - (c2[1] - c1[1]) * (s1[0] - c1[0])) / den;
    let uc = ((s2[0] - s1[0]) * (s1[1] - c1[1]) - (s2[1] - s1[1]) * (s1[0] - c1[0])) / den;

    if (us == 0. || us == 1.) && (0. <= uc && uc <= 1.) ||
        (uc == 0. || uc == 1.) && (0. <= us && us <= 1.){
        // degenerate case
        return None;
    }else if (0. < us && us < 1.) && (0. < uc && uc < 1.){
        let x = s1[0] + us * (s2[0] - s1[0]);
        let y = s1[1] + us * (s2[1] - s1[1]);

        let pt = [x, y];
        return Some((pt, us, uc));
    }

    None
}