Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

_QGAR_GenPolygon.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------+
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library is distributed in the hope that it will be useful,     |
00010  | but WITHOUT ANY WARRANTY; without even the implied warranty of      |
00011  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 #ifndef ___QGAR_GENPOLYGON_H_INCLUDED__
00029 #define ___QGAR_GENPOLYGON_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file    _QGAR_GenPolygon.H
00034  * @brief   Header file of class qgar::GenPolygon.
00035  *
00036  * @warning <b>Not to be used as include file!</b>
00037  * <br>When working with primitives, use file primitives.H.
00038  *
00039  * @author  <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00040  * @date    December 14, 2004  16:16
00041  * @since   Qgar 2.2
00042  */
00043 
00044 
00045 // For RCS/CVS use: Do not delete
00046 /* $Id: _QGAR_GenPolygon.H,v 1.5 2005/10/14 17:05:48 masini Exp $ */
00047 
00048 
00049 
00050 // STL
00051 #include <deque>
00052 #include <iosfwd>  // Avoid including classes when not necessary
00053 #include <list>
00054 #include <vector>
00055 // QGAR
00056 #include <qgarlib/ISerializable.H>
00057 namespace qgar
00058 {
00059   // Avoid #include's when not necessary
00060   class QgarErrorUser;
00061 }
00062 
00063 
00064 
00065 namespace qgar
00066 {
00067 
00068 /**
00069  * @class GenPolygon _QGAR_GenPolygon.H "qgarlib/primitives.H"
00070  * @ingroup DS_PRIM_GEOM
00071  * @brief Geometrical polygon, with coordinates of type <b>T</b>.
00072  *
00073  * <ul>
00074  *
00075  * <li>
00076 @verbatim
00077    O
00078     +---------------> X
00079     |\    |
00080     | \ <-'
00081     |  \    angle (in radians, unless specified)
00082     |   \
00083     |
00084     V
00085 
00086     Y
00087 @endverbatim
00088  * The origin of the coordinate system is at top left corner.
00089  * Angles are clockwise from the X axis.
00090  * </li>
00091  *
00092  * <li>
00093  * A polygon is a series of points (implemented as a STL <b>deque</b>)
00094  * defining a closed geometrical shape: Each point represents
00095  * a vertex and two consecutive points represent a side.
00096  * </li>
00097  *
00098  * <li>
00099  * A polygon must always be provided with at least 3 vertices.
00100  * </li>
00101  *
00102  * <li>
00103  * A polygon is oriented: It is provided with a so-called
00104  * <i>source</i> vertex (first of the series) and a so-called
00105  * <i>target</i> vertex (last of the series).
00106  * </li>
00107  *
00108  * <li>
00109  * A polygon has no attribute. For <i>graphical</i> polygons,
00110  * provided with attributes (thickness, color, outline),
00111  * see class qgar::GenQgarPolygon.
00112  * </li>
00113  *
00114  * <li>
00115  * Predefined types:
00116  * qgar::Polygon,
00117  * qgar::IPolygon,
00118  * qgar::FPolygon,
00119  * qgar::DPolygon.
00120  * </li>
00121  *
00122  * </ul>
00123  *
00124  *
00125  * @warning
00126  * <ul>
00127  * <li>
00128  * A polygon should not be provided with crossing sides. Otherwise,
00129  * the correctness of the effects and results of certain operations,
00130  * like qgar::GenPolygon::area, is not guaranteed. In particular:
00131 @verbatim
00132   1 +    + 3                               1 +     + 5
00133     |\  /|                                   |\ 6 /|
00134     | \/ |      must be represented as       | \_/ |  
00135     | /\ |                                   | / \ |
00136     |/  \|                                   |/ 3 \| 
00137   2 +    + 4                               2 +     + 4
00138 @endverbatim
00139  * </li>
00140  * <li>
00141  * <b>The coherence of the data structure of a polygon after
00142  * modifications of its source and/or its target and/or its
00143  * other vertices is the user's responsibility!</b>
00144  * </li>
00145  * </ul>
00146  *
00147  *
00148  * @author  <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00149  * @date    March 23, 2004  15:15
00150  * @since   Qgar 2.0
00151  */
00152 template <class T> class GenPolygon
00153 
00154   : public AbstractGenPrimitive<T>,
00155     public ISerializable
00156 
00157 {
00158 // -------------------------------------------------------------------
00159 // T Y P E   D E F I N I T I O N S
00160 // -------------------------------------------------------------------
00161 public:
00162 
00163 
00164   /** @name Types */
00165   //        =====
00166   //@{
00167 
00168   /**
00169    * @brief Type of the vertices coordinates.
00170    */
00171   typedef T value_type;
00172 
00173   /**
00174    * @brief Reference to qgar::GenPolygon::value_type.
00175    */
00176   typedef value_type& reference;
00177 
00178   /**
00179    * @brief Constant reference to qgar::GenPolygon::value_type.
00180    */
00181   typedef const value_type& const_reference;
00182 
00183   /**
00184    * @brief Pointer to qgar::GenPolygon::value_type.
00185    */
00186   typedef value_type* pointer;
00187 
00188   /**
00189    * @brief Constant pointer to qgar::GenPolygon::value_type.
00190    */
00191   typedef const value_type* const_pointer;
00192 
00193   /**
00194    * @brief Type of the vertices container.
00195    */
00196   typedef std::deque< GenPoint<value_type> > vertices_type;
00197 
00198   /**
00199    * @brief Reference to qgar::GenPolygon::vertices_type.
00200    */
00201   typedef vertices_type& vertices_reference;
00202 
00203   /**
00204    * @brief Constant reference to qgar::GenPolygon::vertices_type.
00205    */
00206   typedef const vertices_type& vertices_const_reference;
00207 
00208   //@}
00209 
00210 
00211 // -------------------------------------------------------------------
00212 // P U B L I C    M E M B E R S
00213 // -------------------------------------------------------------------
00214 public:
00215 
00216 
00217   /** @name Constructors */
00218   //        ============
00219   //@{
00220 
00221   /**
00222    * @brief Default constructor.
00223    *
00224    * @todo The created polygon does not conform to the definition
00225    *   of a polygon: The vertices <b>deque</b> is empty whereas
00226    *   it should contain at least 3 elements!
00227    */
00228   GenPolygon();
00229 
00230   /**
00231    * @brief Copy constructor.
00232    *
00233    * @param aPoly  a polygon
00234    */
00235   GenPolygon(const GenPolygon<value_type>& aPoly);
00236 
00237   /**
00238    * @brief Initialize from a Qgar polygon.
00239    *
00240    * @param aQPoly  a Qgar polygon
00241    *
00242    * @warning This kind of conversion must be explicitely
00243    *   specified by the client.
00244    */
00245   explicit GenPolygon(const GenQgarPolygon<value_type>& aQPoly);
00246 
00247    /**
00248    * @brief Initialize from a STL list of at least 3 points.
00249    *
00250    * @param aPtList  a STL list of at least 3 points
00251    *
00252    * @warning This kind of conversion must be explicitely
00253    *   specified by the client.
00254    *
00255    * @exception qgar::QgarErrorUser
00256    * (cannot create a polygon of less than 3 vertices)
00257    */
00258   explicit GenPolygon(const std::list< GenPoint<value_type> >& aPtList)
00259     throw(QgarErrorUser);
00260 
00261   //@}
00262 
00263 
00264   /** @name Destructor */
00265   //        ==========
00266   //@{
00267 
00268   /**
00269    * @brief Virtual destructor.
00270    */
00271   virtual ~GenPolygon();
00272 
00273   //@}
00274 
00275 
00276   /**@name Copy */
00277   //       ====
00278   //@{
00279 
00280   /**
00281    * @brief Perform a deep copy.
00282    */  
00283   virtual GenPolygon<value_type>* clone() const;
00284 
00285   //@}
00286 
00287 
00288   /** @name Operators */
00289   //        =========
00290   //@{
00291 
00292   /**
00293    * @brief Assignment.
00294    *
00295    * @param aPoly  a polygon
00296    */
00297   inline GenPolygon<value_type>&
00298   operator=(const GenPolygon<value_type>& aPoly);
00299 
00300   //@}
00301 
00302 
00303   /** @name Area */
00304   //        ====
00305   //@{
00306 
00307   /**
00308    * @brief Get signed area.
00309    */
00310   double signedArea();
00311 
00312   /**
00313    * @brief Get area.
00314    */
00315   inline double area();
00316 
00317   //@}
00318 
00319 
00320   /** @name Access to vertices */
00321   //        ==================
00322   //@{
00323 
00324   /**
00325    * @brief Get number of vertices.
00326    */
00327   inline int size() const;
00328 
00329   /**
00330    * @brief Get vertices.
00331    */
00332   inline vertices_const_reference accessVertices() const;
00333 
00334   /**
00335    * @brief Give non-protected access to the vertices.
00336    *
00337    * Vertices may then be directly modified using appropriate
00338    * transformation function members.
00339    */
00340   inline vertices_reference getVertices();
00341 
00342   /**
00343    * @brief Get a copy of the vertices.
00344    */
00345   inline vertices_type vertices() const;
00346 
00347   //@}
00348 
00349 
00350   /** @name Insert vertices */
00351   //        ===============
00352   //@{
00353 
00354   /**
00355    * @brief Insert a point as new source.
00356    *
00357    * @param aPt  a point
00358    */
00359   GenPolygon<value_type>&
00360   appendSource(const GenPoint<value_type>& aPt);
00361 
00362   /**
00363    * @brief Insert a point as new target.
00364    *
00365    * @param aPt  a point
00366    */
00367   GenPolygon<value_type>&
00368   appendTarget(const GenPoint<value_type>& aPt);
00369 
00370   /**
00371    * @brief Appends a set of vertices to a Qgar polygon.
00372    *
00373    * The given vertices are inserted after the target vertex.
00374    * The order of the given vertices is preserved
00375    * in the resulting polyline.
00376    * The last vertex of the vector becomes the new target.
00377    *
00378    * @param aPtVect  STL vector of vertices to be appended
00379    * @return a reference on the polygon.
00380    */
00381   GenPolygon<value_type>&
00382   append(const std::vector<GenPoint<value_type> >& aPtVect);
00383 
00384   //@}
00385 
00386 
00387   /** @name Remove vertices */
00388   //        ===============
00389   //@{
00390 
00391   /**
00392    * @brief Remove a vertex from the polygon.
00393    *
00394    * @param aPt  a point
00395    *
00396    * @exception qgar::QgarErrorUser
00397    * (no removal in a polygon including 3 vertices)
00398    */
00399   GenPolygon<value_type>& remove(const GenPoint<value_type>& aPt)
00400     throw(QgarErrorUser);
00401 
00402   //@}
00403 
00404 
00405   /** @name Conversions of the vertices */
00406   //        ===========================
00407   //@{
00408 
00409   /**
00410    * @brief Get a vector of points from the vertices.
00411    */
00412   std::vector< GenPoint<value_type> > toPointVector();
00413 
00414   /**
00415    * @brief Get a STL list of (geometrical) segments from the vertices.
00416    */
00417   std::list< GenSegment<value_type> > toSegList();
00418 
00419   /**
00420    * @brief Get a STL list of Qgar segments, provided with given
00421    *   attributes, from the vertices.
00422    *
00423    * @param aThickness  thickness (default <b>1</b>)
00424    * @param aColor      color     (default qgar::QGE_COLOR_DEFAULT)
00425    * @param anOutline   outline   (default qgar::QGE_OUTLINE_DEFAULT) 
00426    */
00427   std::list< GenQgarSegment<value_type> > 
00428   toQgarSegList(int        aThickness = 1,
00429                 QGEcolor   aColor     = QGE_COLOR_DEFAULT,
00430                 QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00431 
00432   //@}
00433 
00434 
00435   /**@name Geometry: Translation */
00436   //       =====================
00437   //@{
00438 
00439   /**
00440    * @brief Translate current segment along X and Y axis.
00441    *
00442    * @param aTransX  X translation factor
00443    * @param aTransY  Y translation factor
00444    */
00445   virtual void translate(value_type aTransX, value_type aTransY);
00446 
00447   //@}
00448 
00449 
00450   /** @name Serialization/deserialization */
00451   //        =============================
00452   //@{
00453 
00454   /**
00455    * @brief Deserializes the current polygon from an input stream.
00456    *
00457    * A serialized polygon is represented by:
00458 @verbatim
00459 Polygon(<SOURCE>)(<TARGET>)(<VERTICES COUNT>)(<VERTICE 1>)..(<VERTICE n>)
00460 @endverbatim
00461    *
00462    * @param anInStream  the input stream
00463    */
00464   virtual std::istream& read(std::istream& anInStream);
00465 
00466   /**
00467    * @brief Serializes the current polygon to an input stream.
00468    *
00469    * A serialized polygon is represented by:
00470 @verbatim
00471 Polygon(<SOURCE>)(<TARGET>)(<VERTICES COUNT>)(<VERTICE 1>)..(<VERTICE n>)
00472 @endverbatim
00473    *
00474    * @param anOutStream  the output stream
00475    */
00476   virtual std::ostream& write(std::ostream& anOutStream) const;
00477 
00478   //@}
00479 
00480 
00481 // -------------------------------------------------------------------
00482 // P R O T E C T E D    M E M B E R S
00483 // -------------------------------------------------------------------
00484 protected:
00485 
00486 
00487   /** @name Structure of a polygon */
00488   //        =======================
00489   //@{
00490 
00491   /**
00492    * @brief Vertices.
00493    */
00494   vertices_type _vertices;
00495 
00496   //@}
00497 
00498 
00499   /** @name Updates subsequent to source and target modifications */
00500   //        =====================================================
00501   //@{
00502 
00503   /**
00504    * @brief Update the geometrical structure
00505    *   when the source has been changed.
00506    */
00507   virtual void updateSource();
00508 
00509   /**
00510    * @brief Update the geometrical structure
00511    *   when the target has been changed.
00512    */
00513   virtual void updateTarget();
00514 
00515   /**
00516    * @brief Update the geometrical structure
00517    *   when both source and target have been changed.
00518    */
00519   virtual void updateSourceTarget();
00520 
00521   //@}
00522 
00523 
00524 // -------------------------------------------------------------------
00525 }; // class GenPolygon
00526 
00527 
00528 
00529 
00530 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00531 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00532 // P R E D E F I N E D    P O L Y L I N E    T Y P E S
00533 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00534 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00535 
00536 
00537 /**@name Polygon types */
00538 //       =============
00539 //@{
00540 
00541 /**
00542  * @brief Polygon with <b>integer</b> coordinates.
00543  * @ingroup DS_PRIM_GEOM
00544  *
00545  * @see qgar::IPolygon
00546  */
00547 typedef GenPolygon<int> Polygon;
00548 
00549 /**
00550  * @brief Polygon with <b>integer</b> coordinates.
00551  * @ingroup DS_PRIM_GEOM
00552  *
00553  * @see qgar::Polygon
00554  */
00555 typedef GenPolygon<int> IPolygon;
00556 
00557 /**
00558  * @brief Polygon with <b>float</b> coordinates.
00559  * @ingroup DS_PRIM_GEOM
00560  */
00561 typedef GenPolygon<float> FPolygon;
00562 
00563 /**
00564  * @brief Polygon with <b>double</b> coordinates.
00565  * @ingroup DS_PRIM_GEOM
00566  */
00567 typedef GenPolygon<double> DPolygon;
00568 
00569 //@}
00570 
00571 
00572 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00573 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00574 
00575 
00576 } // namespace qgar 
00577 
00578 
00579 #endif /* ___QGAR_GENPOLYGON_H_INCLUDED__ */