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

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