core/vgl/vgl_clip.h
Go to the documentation of this file.
00001 // This is core/vgl/vgl_clip.h
00002 #ifndef vgl_clip_h_
00003 #define vgl_clip_h_
00004 //:
00005 // \file
00006 // \author fsm
00007 // \verbatim
00008 //  Modifications
00009 //   29 Apr 2002: Amitha Perera: added a polygon clipper (a wrap around for
00010 //                               Alan Murt's Generic Polygon Clipper)
00011 //   12 Oct 2002: Peter Vanroose: vgl_clip_line_to_box completely re-implemented
00012 //   14 Nov 2003: Peter Vanroose: made all functions templated
00013 // \endverbatim
00014 
00015 #include <vgl/vgl_box_2d.h>
00016 #include <vgl/vgl_line_2d.h>
00017 #include <vgl/vgl_line_segment_2d.h>
00018 #include <vgl/vgl_polygon.h>
00019 
00020 //: Type of polygon "clip" operations.
00021 enum vgl_clip_type
00022 {
00023   vgl_clip_type_intersect,
00024   vgl_clip_type_union,
00025   vgl_clip_type_difference,
00026   vgl_clip_type_xor
00027 };
00028 
00029 
00030 //: clips away the portion where ax+by+c<0. return false if nothing left.
00031 
00032 template <class T>
00033 bool vgl_clip_lineseg_to_line(T &x1, T &y1, // line segment start
00034                               T &x2, T &y2, // and end.
00035                               T a, T b, T c);
00036 
00037 //: clip line ax+by+c=0 to given box. return false if no intersection.
00038 
00039 template <class T>
00040 bool vgl_clip_line_to_box(T a, T b, T c, // line equation ax+by+c=0.
00041                           T x1,T y1,     // coordinates of
00042                           T x2,T y2,     // box corners.
00043                           T &bx, T &by,  // clipped line
00044                           T &ex, T &ey); // segment.
00045 
00046 
00047 //: clip given line to given box, and return resulting line segment
00048 // \relatesalso vgl_line_2d
00049 // \relatesalso vgl_box_2d
00050 
00051 template <class T>
00052 inline
00053 vgl_line_segment_2d<T> vgl_clip_line_to_box(vgl_line_2d<T> const& l,
00054                                             vgl_box_2d<T> const& b)
00055 {
00056   T sx, sy, ex, ey;
00057   bool r = vgl_clip_line_to_box(l.a(), l.b(), l.c(),
00058                                 b.min_x(), b.min_y(), b.max_x(), b.max_y(),
00059                                 sx, sy, ex, ey);
00060   return r ? vgl_line_segment_2d<T>(vgl_point_2d<T>(sx, sy),
00061                                     vgl_point_2d<T>(ex, ey))
00062            : vgl_line_segment_2d<T>(); // uninitialised when no intersection
00063 }
00064 
00065 //: Clip a polygon against another polygon.
00066 // The two polygons poly1 and poly2 are combined with each other.
00067 // The operation (intersection, union, etc) is given by parameter op.
00068 //
00069 // \note The implementation of this code is based on Alan Murta's GPC
00070 // library (http://www.cs.man.ac.uk/aig/staff/alan/software/gpc.html)
00071 // which is free for non-commercial use.
00072 //
00073 // In order to be able to use it, make sure to satisfy the copyright notice,
00074 // then activate the "BUILD_NONCOMMERCIAL" compiler option.
00075 //
00076 // \relatesalso vgl_polygon
00077 template <class T>
00078 vgl_polygon<T>
00079 vgl_clip( const vgl_polygon<T>& poly1, const vgl_polygon<T>& poly2,
00080           vgl_clip_type op = vgl_clip_type_intersect );
00081 
00082 //: Clip a polygon against another polygon.
00083 //  Same as vgl_clip( const vgl_polygon<T>& poly1, const vgl_polygon<T>& poly2,
00084 //                    vgl_clip_type op = vgl_clip_type_intersect );
00085 //  but where the fourth parameter is a return flag which is 1 if success,
00086 //  or 0 if the operation faced a geometric degeneracy which could not be
00087 //  handled. In this case, it might be necessary to perturb the input with
00088 //  a tiny amount of random noise and try again.
00089 //
00090 // \relatesalso vgl_polygon
00091 template <class T>
00092 vgl_polygon<T>
00093 vgl_clip(vgl_polygon<T> const& poly1, vgl_polygon<T> const& poly2, vgl_clip_type op, int *p_retval);
00094 
00095 #define VGL_CLIP_INSTANTIATE(T) extern "please include vgl/vgl_clip.txx instead"
00096 
00097 #endif // vgl_clip_h_