LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiPolygonPoints.h

00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  *
00026  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00027  * file .......: ltiPolygonPoints.h
00028  * classes ....: lti::PolygonPoints
00029  * description.: class for description of an objects contour
00030  *               as a linear polygon
00031  * authors ....: Ruediger Weiler
00032  * organization: LTI, RWTH Aachen
00033  * creation ...: 06.12.2000
00034  * revisions ..: $Id: ltiPolygonPoints.h,v 1.6 2006/02/08 11:39:35 ltilib Exp $
00035  */
00036 
00037 #ifndef _LTIPOLYGONPOINTS_H
00038 #define _LTIPOLYGONPOINTS_H
00039 
00040 #include <list>
00041 #include "ltiMath.h"
00042 #include "ltiTypes.h"
00043 #include "ltiPointList.h"
00044 
00045 namespace lti {
00046 
00047   //declared in ltiContour.h
00048   class borderPoints;
00049   class ioPoints;
00050 
00051   /**
00052    * Contour classes: polygonPoints.
00053    *
00054    * For the explanation of the contour description in this class, see
00055    * following image:
00056    *
00057    * \code
00058    *   -- 00000000001111111111222222222233
00059    *   -- 01234567890123456789012345678901
00060    *   00 --------------------------------
00061    *   01 --------------------------------
00062    *   02 --------------------------------
00063    *   03 --------Xooo-------Soo----------
00064    *   04 -------o++++Xoo----E++o---------
00065    *   05 -------o+++++++X---o+++X--------
00066    *   06 ------X+++++++o-----o+o---------
00067    *   07 -------o+++++++XoooX++o---------
00068    *   08 ---------X++++++++++++X---------
00069    *   09 --------o+++++++++++++++o-------
00070    *   10 --------o+++++++++++++++o-------
00071    *   11 -------X++++++++++++++Xo--------
00072    *   12 ------o++++++++++++++o----------
00073    *   13 ------o++++++++++++++oX---------
00074    *   14 -----X++++++++++++++++++o-------
00075    *   15 -----o+++++++++++++++++++o------
00076    *   16 ----o+++++++++++++++++++Xo------
00077    *   17 ---X++++++++++++++++++oo--------
00078    *   18 ----ooooooooX++++++++X----------
00079    *   19 -------------oooooXoo-----------
00080    *   20 --------------------------------
00081    *   21 --------------------------------
00082    *   22 --------------------------------
00083    *   23 --------------------------------
00084    * \endcode
00085    *
00086    *  "-" means the background, "+" represents the inner part of the object,
00087    *  "o" indicates a borderPoint,  "X" is a polygonPoint, the
00088    *  borderPoint "S" is start point of the polygonPointList and "E" the end
00089    *  point of the list.
00090    *
00091    *  This class cast given borderPoints into a polygon.  The polygon
00092    *  has usually less points than the borderPoints, and thus it saves
00093    *  memory. The disadvantage is that you loose details of the
00094    *  object when you do the cast. Two parameters minLength and
00095    *  maxDistance control the number of points of the resulting
00096    *  polygon.
00097    *
00098    *  Between two points in the polygon there must be more than
00099    *  minLength points of the borderPoints, and the polygon must be
00100    *  closer to the borderPoints as maxDistance.
00101    *
00102    *  It is also possible to get the convex hull of a set of points
00103    *  (given in a pointList or in an ioPoints list) calling the respective
00104    *  castFrom() methods.
00105    *
00106    *  @see lti::borderPoints, lti::ioPoints, lti::pointList
00107    *
00108    *  @ingroup gAggregate
00109    *  @ingroup gShape
00110    */
00111   template<class T>
00112   class tpolygonPoints : public tpointList<T> {
00113   public:
00114     /**
00115      * default constructor creates an empty border-point-list
00116      */
00117     tpolygonPoints<T>();
00118 
00119     /**
00120      * create this pointList as a copy of another pointList
00121      * @param other the pointList to be copied.
00122      */
00123     tpolygonPoints(const tpolygonPoints<T>& other);
00124 
00125     /**
00126      * destructor
00127      */
00128     virtual ~tpolygonPoints();
00129 
00130     /**
00131      * returns the name of this class: "polygonPoints"
00132      */
00133     const char* getTypeName() const {return "tpolygonPoints";};
00134 
00135     /**
00136      * alias for approximate()
00137      */
00138     tpolygonPoints<T>& castFrom(const borderPoints& theBorderPoints,
00139                                 const int minLength=-1,
00140                                 const double maxDistance=1.0,
00141                                 const bool closed=true,
00142                                 const bool searchMaxDist=false);
00143 
00144     /**
00145      * creates the smallest convex polygon that contains all points in
00146      * the given io-points list.  (see computeConvexHull()).
00147      *
00148      * This method eliminates the points present twice in the list before
00149      * computing the convex hull.
00150      */
00151     tpolygonPoints<T>& castFrom(const ioPoints& thePointList);
00152 
00153     /**
00154      * This is an alias for computeConvexHull()
00155      */
00156     tpolygonPoints<T>& castFrom(const tpointList<T>& thePointList);
00157 
00158     /**
00159      * assigment operator (alias for copy(other)).
00160      * @param other the pointList to be copied
00161      * @return a reference to the actual pointList
00162      */
00163     inline tpolygonPoints<T>& operator=(const tpolygonPoints<T>& other) {
00164       copy(other);
00165       return *this;
00166     };
00167 
00168     /**
00169      * Compute the 2 x area  of this polygon.
00170      * 
00171      * The reason to return twice the area and not the area itself is very
00172      * simple:  For integer point types, twice the area is always an integer 
00173      * value, but the area itself is not. 
00174      *
00175      * If the area is positive, the polygon has a clockwise direction.
00176      * Otherwise it will be negative.
00177      */
00178     T areaX2() const;
00179 
00180     /**
00181      * Compute if this polygon has a clockwise direction or not.
00182      */
00183     bool clockwise() const;
00184 
00185     /**
00186      * Creates the smallest convex polygon that contains all points
00187      * in the given point list.
00188      *
00189      * The list of points \b must be a set, i.e. the same point is not
00190      * allowed to be twice in the list.  (This usually is not the case
00191      * in lti::ioPoints lists, see castFrom(const ioPoints&)).
00192      *
00193      * For more information on the algorithm used here see:
00194      *
00195      * M. de Berg, et.al. Computational Geometry, Algorithms and Applications.
00196      * 2nd. edition, Springer, 2000, pp. 6ff
00197      *
00198      * @param thePointList a set of points (the same point ist not allowed to
00199      *                     be twice in the list).
00200      * @return a reference to this object.
00201      */
00202     tpolygonPoints<T>& convexHullFrom(const tpointList<T>& thePointList);
00203 
00204     /**
00205      * Approximate the given border points as a polygon.
00206      *
00207      * At the end of the approximation process this polygon-points-list
00208      * contains the coordinates of the vertices.
00209      *
00210      * @param theBorderPoints list with the border points to be approximated
00211      *                        with a polygon.  The elements of this list must
00212      *                        be adjacent.
00213      * @param minStep minimal "distance" between vertices of the polygon.
00214      *                  (if 0 or negative, only the maxDistance parameter
00215      *                   will be considered).
00216      *                  The "distance" used here is \b NOT the
00217      *                  euclidean distance between the vertices but
00218      *                  the number of elements between both vertices
00219      *                  in the border list.  These is usually
00220      *                  therefore a city-block distance or a
00221      *                  8-neighborhood distance depending on the
00222      *                  border points description used.
00223      * @param maxDistance specify the maximal allowed \b euclidean distance
00224      *                  between the border points and the approximated polygon.
00225      *                  (if negative, each "minLength" pixel of the
00226      *                    border points will be taken).
00227      * @param closed if true (default), only the found vertices will be
00228      *                    in the list.  If false,  the last point of the list
00229      *                    will be adjacent to the first point in the list.
00230      * @param searchMaxDist if true, the first two vertices will be computed
00231      *                    as the two points in the border with the maximal
00232      *                    distance.  If false, the first point in the
00233      *                    list will be a vertex and the border point with
00234      *                    the maximal distance to it too.  This faster
00235      *                    method is the default.  It provides usually
00236      *                    good results.
00237      *
00238      * @return a reference to this polygon-points instance.
00239      */
00240     tpolygonPoints<T>& approximate(const borderPoints& theBorderPoints,
00241                                    const int minStep=-1,
00242                                    const double maxDistance=1,
00243                                    const bool closed=false,
00244                                    const bool searchMaxDist=false);
00245 
00246     /**
00247      * Approximate the given border points as a polygon, but ensure
00248      * that the given points are vertices.  The given list of points
00249      * \b must fulfill following two conditions:
00250      * -# It must have the same sequence than theBorderPoints.
00251      * -# The points must be contained by the theBorderPoints
00252      *
00253      * If one of these condition is not met, the algorithm will take too much
00254      * time trying to figure out what happend and to recover.  
00255      *
00256      * The first condition ensures an efficient search for the
00257      * vertices in the border points list.
00258      *
00259      * At the end of the approximation process this polygon-points-list
00260      * contains the coordinates of the vertices.
00261      *
00262      * @param theBorderPoints list with the border points to be approximated
00263      *                        with a polygon.  The elements of this list must
00264      *                        be adjacent.
00265      * @param forcedVertices list of vertices that must be included in
00266      *                        the polygon representation.  This list must be a 
00267      *                        subset and respect  the same ordering as in
00268      *                        theBorderPoints
00269      * @param minStep minimal "distance" between vertices of the polygon.
00270      *                  (if 0 or negative, only the maxDistance parameter
00271      *                   will be considered).
00272      *                  The "distance" used here is \b NOT the
00273      *                  euclidean distance between the vertices but
00274      *                  the number of elements between both vertices
00275      *                  in the border list.  These is usually
00276      *                  therefore a city-block distance or a
00277      *                  8-neighborhood distance depending on the
00278      *                  border points description used.
00279      * @param maxDistance specify the maximal allowed \b euclidean distance
00280      *                  between the border points and the approximated polygon.
00281      *                  (if negative, each "minLength" pixel of the
00282      *                    border points will be taken).
00283      * @param closed if true (default), only the found vertices will be
00284      *                    in the list.  If false,  the last point of the list
00285      *                    will be adjacent to the first point in the list.
00286      * @param searchMaxDist if true, the first two vertices will be computed
00287      *                    as the two points in the border with the maximal
00288      *                    distance.  If false, the first point in the
00289      *                    list will be a vertex and the border point with
00290      *                    the maximal distance to it too.  This faster
00291      *                    method is the default.  It provides usually
00292      *                    good results.
00293      * @return a reference to this polygon-points instance.
00294      */
00295     tpolygonPoints<T>& approximate(const borderPoints& theBorderPoints,
00296                                    const pointList& forcedVertices,
00297                                    const int minStep=-1,
00298                                    const double maxDistance=1,
00299                                    const bool closed=false,
00300                                    const bool searchMaxDist=false);
00301 
00302     /**
00303      * invert the direction of the polygon points
00304      */
00305     void invert();
00306 
00307     /**
00308      * Combine two polygons.  The result is the union of the points of
00309      * both polygons.  If both given polygons do not overlap, the result
00310      * will be the first polygon.
00311      *
00312      * @param polygon1 first polygon
00313      * @param polygon2 second polygon
00314      * @param minLength minimal distance between vertices of the polygon.
00315      * @param maxDistance specify the maximal allowed distance between the
00316      *                    border points and the approximated polygon.
00317      *                    If set to 0, every minLength points will be
00318      *                    taken as vertice of the polygon.
00319      * @return a reference to this object
00320      *
00321      */
00322     tpolygonPoints<T>& clipPoly(const tpolygonPoints<T>& polygon1,
00323                                 const tpolygonPoints<T>& polygon2,
00324                                 const int& minLength = 4,
00325                                 const double& maxDistance = 0.8);
00326 
00327   protected:
00328     /**
00329      * used in the approximation of a border points with a polygon.
00330      * Implements the Ramer (or Duda Hart) method.  See also
00331      * Haralick and Shapiro.  Computer and Robot Vision vol. 1 Addison Wesley,
00332      * 1992.  pp. 563ff
00333      */
00334     void fitAndSplit(const vector< point >& vct,
00335                      const int idx1,
00336                      const int idx2,
00337                      const double maxDistance,
00338                      vector<ubyte>& flags);
00339   };
00340 
00341   /**
00342    * Polygon Points of Integer Points
00343    */
00344   class polygonPoints : public tpolygonPoints<int> {
00345   public:
00346     polygonPoints() : tpolygonPoints<int>() {};
00347     polygonPoints(const polygonPoints& other)
00348       : tpolygonPoints<int>(other) {};
00349   };
00350 
00351 }//end namespace lti
00352 #endif

Generated on Sat Apr 10 15:25:59 2010 for LTI-Lib by Doxygen 1.6.1