1 // This is core/vgl/vgl_clip.h
2 #ifndef vgl_clip_h_
3 #define vgl_clip_h_
4 //:
5 // \file
6 // \author fsm
7 // \verbatim
8 //  Modifications
9 //   29 Apr 2002: Amitha Perera: added a polygon clipper (a wrap around for
10 //                               Alan Murt's Generic Polygon Clipper)
11 //   12 Oct 2002: Peter Vanroose: vgl_clip_line_to_box completely re-implemented
12 //   14 Nov 2003: Peter Vanroose: made all functions templated
13 //   27 May 2015: Scott Richardson: added another polygon clipper library (a wrapper
14 //                around Angus Johnson's Clipper library)
15 // \endverbatim
16 
17 #include "vgl_box_2d.h"
18 #include "vgl_line_2d.h"
19 #include "vgl_line_segment_2d.h"
20 #include "vgl_polygon.h"
21 
22 //: Type of polygon "clip" operations.
23 enum vgl_clip_type
24 {
25   vgl_clip_type_intersect,
26   vgl_clip_type_union,
27   vgl_clip_type_difference,
28   vgl_clip_type_xor
29 };
30 
31 
32 //: clips away the portion where ax+by+c<0. return false if nothing left.
33 
34 template <class T>
35 bool vgl_clip_lineseg_to_line(T &x1, T &y1, // line segment start
36                               T &x2, T &y2, // and end.
37                               T a, T b, T c);
38 
39 //: clip line ax+by+c=0 to given box. return false if no intersection.
40 
41 template <class T>
42 bool vgl_clip_line_to_box(T a, T b, T c, // line equation ax+by+c=0.
43                           T x1,T y1,     // coordinates of
44                           T x2,T y2,     // box corners.
45                           T &bx, T &by,  // clipped line
46                           T &ex, T &ey); // segment.
47 
48 
49 //: clip given line to given box, and return resulting line segment
50 // \relatesalso vgl_line_2d
51 // \relatesalso vgl_box_2d
52 
53 template <class T>
54 inline
vgl_clip_line_to_box(vgl_line_2d<T> const & l,vgl_box_2d<T> const & b)55 vgl_line_segment_2d<T> vgl_clip_line_to_box(vgl_line_2d<T> const& l,
56                                             vgl_box_2d<T> const& b)
57 {
58   T sx, sy, ex, ey;
59   bool r = vgl_clip_line_to_box(l.a(), l.b(), l.c(),
60                                 b.min_x(), b.min_y(), b.max_x(), b.max_y(),
61                                 sx, sy, ex, ey);
62   return r ? vgl_line_segment_2d<T>(vgl_point_2d<T>(sx, sy),
63                                     vgl_point_2d<T>(ex, ey))
64            : vgl_line_segment_2d<T>(); // uninitialised when no intersection
65 }
66 
67 //: Clip a polygon against another polygon.
68 // The two polygons poly1 and poly2 are combined with each other.
69 // The operation (intersection, union, etc) is given by parameter op.
70 //
71 // \note The implementation of this code is based on Alan Murta's GPC
72 // library (http://www.cs.man.ac.uk/aig/staff/alan/software/gpc.html)
73 // which is free for non-commercial use.
74 //
75 // In order to be able to use it, make sure to satisfy the copyright notice,
76 // then activate the "BUILD_NONCOMMERCIAL" compiler option.
77 //
78 // \relatesalso vgl_polygon
79 template <class T>
80 vgl_polygon<T>
81 vgl_clip( const vgl_polygon<T>& poly1, const vgl_polygon<T>& poly2,
82           vgl_clip_type op = vgl_clip_type_intersect );
83 
84 //: Clip a polygon against another polygon.
85 //  Same as vgl_clip( const vgl_polygon<T>& poly1, const vgl_polygon<T>& poly2,
86 //                    vgl_clip_type op = vgl_clip_type_intersect );
87 //  but where the fourth parameter is a return flag which is 1 if success,
88 //  or 0 if the operation faced a geometric degeneracy which could not be
89 //  handled. In this case, it might be necessary to perturb the input with
90 //  a tiny amount of random noise and try again.
91 //
92 // \relatesalso vgl_polygon
93 template <class T>
94 vgl_polygon<T>
95 vgl_clip(vgl_polygon<T> const& poly1, vgl_polygon<T> const& poly2, vgl_clip_type op, int *p_retval);
96 
97 #define VGL_CLIP_INSTANTIATE(T) extern "please include vgl/vgl_clip.hxx instead"
98 
99 #endif // vgl_clip_h_
100