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

_QGAR_GenPolyline.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_GENPOLYLINE_H_INCLUDED__
00029 #define ___QGAR_GENPOLYLINE_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file    _QGAR_GenPolyline.H
00034  * @brief   Header file of class qgar::GenPolyline.
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  18:01
00041  * @since   Qgar 2.2
00042  */
00043 
00044 
00045 // For RCS/CVS use: Do not delete
00046 /* $Id: _QGAR_GenPolyline.H,v 1.5 2005/10/14 17:05:48 masini Exp $ */
00047 
00048 
00049 
00050 // STD
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 GenPolyline _QGAR_GenPolyline.H "qgarlib/primitives.H"
00070  * @ingroup DS_PRIM_GEOM
00071  * @brief Geometrical polyline, 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 polyline is a series of points, implemented as a STL
00094  * <b>deque</b>): Each point represents a vertex and two
00095  * consecutive points represent a segment.
00096  * </li>
00097  *
00098  * <li>
00099  * A polyline must always be provided with at least 2 vertices.
00100  * </li>
00101  *
00102  * <li>
00103  * A polyline is oriented: It is provided with a so-called
00104  * <i>source</i> vertex (first of the series)
00105  * and a so-called <i>target</i> vertex (last of the series).
00106  * </li>
00107  *
00108  * <li>
00109  * A polyline does not define any closed geometrical shape,
00110  * like a polygon. In particular, the source and target vertices
00111  * do not define a segment.
00112  * </li>
00113  *
00114  * <li>
00115  * A polyline has no attribute. For <i>graphical</i> polylines,
00116  * typically resulting from vectorization and provided with attributes
00117  * (thickness, color, outline), see class qgar::GenQgarPolyline.
00118  * </li>
00119  *
00120  * <li>
00121  * Predefined types:
00122  * - qgar::Polyline
00123  * - qgar::IPolyline
00124  * - qgar::FPolyline
00125  * - qgar::DPolyline
00126  * </li>
00127  *
00128  * </ul>
00129  *
00130  *
00131  * @warning
00132  *   <b>The coherence of the data structure of a polyline after
00133  *   modifications of its source and/or its target and/or its
00134  *   other vertices is the user's responsibility!</b>
00135  *
00136  *
00137  * @author  <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00138  * @date    June 24, 2003  18:00
00139  * @since   Qgar 2.1
00140  */
00141 template <class T> class GenPolyline
00142 
00143   : public AbstractGenPrimitive<T>,
00144     public ISerializable
00145 
00146 {
00147 // -------------------------------------------------------------------
00148 // T Y P E   D E F I N I T I O N S
00149 // -------------------------------------------------------------------
00150 public:
00151 
00152 
00153   /** @name Types */
00154   //        =====
00155   //@{
00156 
00157   /**
00158    * @brief Type of the vertices coordinates.
00159    */
00160   typedef T value_type;
00161 
00162   /**
00163    * @brief Reference to qgar::GenPolyline::value_type.
00164    */
00165   typedef value_type& reference;
00166 
00167   /**
00168    * @brief Constant reference to qgar::GenPolyline::value_type.
00169    */
00170   typedef const value_type& const_reference;
00171 
00172   /**
00173    * @brief Pointer to qgar::GenPolyline::value_type.
00174    */
00175   typedef value_type* pointer;
00176 
00177   /**
00178    * @brief Constant pointer to qgar::GenPolyline::value_type.
00179    */
00180   typedef const value_type* const_pointer;
00181 
00182   /**
00183    * @brief Type of the vertices container.
00184    */
00185   typedef std::deque< GenPoint<value_type> > vertices_type;
00186 
00187   /**
00188    * @brief Reference to qgar::GenPolyline::vertices_type.
00189    */
00190   typedef vertices_type& vertices_reference;
00191 
00192   /**
00193    * @brief Constant reference to qgar::GenPolyline::vertices_type.
00194    */
00195   typedef const vertices_type& vertices_const_reference;
00196 
00197   //@}
00198 
00199 
00200 // -------------------------------------------------------------------
00201 // P U B L I C    M E M B E R S
00202 // -------------------------------------------------------------------
00203 public:
00204 
00205 
00206   /** @name Constructors */
00207   //        ============
00208   //@{
00209   /**
00210    * @brief Default constructor.
00211    *
00212    * @todo The created polyline does not conform to the definition
00213    *   of a polyline: The vertices <b>deque</b> is empty whereas
00214    *   it should contain at least 2 elements!
00215    */
00216   GenPolyline();
00217 
00218   /**
00219    * @brief Copy constructor.
00220    *
00221    * @param aPoly  a polyline
00222    */
00223   GenPolyline(const GenPolyline<value_type>& aPoly);
00224 
00225   /**
00226    * @brief Initialize from a Qgar polyline.
00227    *
00228    * The order of the vertices is preserved in the resulting polyline.
00229    *
00230    * @param aQPoly  a Qgar polyline
00231    *
00232    * @warning This kind of conversion must be explicitely
00233    *   specified by the client.
00234    */
00235   explicit GenPolyline(const GenQgarPolyline<value_type>& aQPoly);
00236 
00237   /**
00238    * @brief Initialize from a Qgar segment.
00239    *
00240    * The source (resp. target) of the resulting polyline
00241    * is the source (resp. target) of the segment.
00242    *
00243    * @param aQSeg  a Qgar segment
00244    *
00245    * @warning This kind of conversion must be explicitely
00246    *   specified by the client.
00247    */
00248   explicit GenPolyline(const GenQgarSegment<value_type>& aQSeg);
00249 
00250   /**
00251    * @brief Initialize from a (geometrical) segment.
00252    *
00253    * The source (resp. target) of the resulting polyline
00254    * is the source (resp. target) of the segment.
00255    *
00256    * @param aSeg  a (geometrical) segment
00257    *
00258    * @warning This kind of conversion must be explicitely
00259    *   specified by the client.
00260    */
00261   explicit GenPolyline(const GenSegment<value_type>& aSeg);
00262 
00263   /**
00264    * @brief Initialize from two vertices: source and target.
00265    *
00266    * @param aSource  source vertex 
00267    * @param aTarget  target vertex
00268    */
00269   GenPolyline(const GenPoint<value_type>& aSource,
00270               const GenPoint<value_type>& aTarget);
00271 
00272    /**
00273    * @brief Initialize from a list of at least two vertices.
00274    *
00275    * The order of the given vertices is preserved
00276    * in the resulting polyline.
00277    *
00278    * @param aPtList  a list of at least two vertices
00279    *
00280    * @warning This kind of conversion must be explicitely
00281    *   specified by the client.
00282    *
00283    * @exception qgar::QgarErrorUser
00284    * (cannot create a polyline of less than 2 vertices)
00285    */
00286   explicit
00287   GenPolyline(const std::list< GenPoint<value_type> >& aPtList)
00288     throw(QgarErrorUser);
00289 
00290   //@}
00291 
00292 
00293   /** @name Destructor */
00294   //        ==========
00295   //@{
00296 
00297   /**
00298    * @brief Virtual destructor.
00299    */
00300   virtual ~GenPolyline();
00301 
00302   //@}
00303 
00304 
00305   /**@name Copy */
00306   //       ====
00307   //@{
00308 
00309   virtual GenPolyline<value_type>* clone() const;
00310 
00311   //@}  
00312 
00313 
00314   /** @name Operators */
00315   //        =========
00316   //@{
00317 
00318   /**
00319    * @brief Assignment.
00320    *
00321    * @param aPoly  a polyline
00322    */
00323   inline GenPolyline<value_type>&
00324   operator=(const GenPolyline<value_type>& aPoly);
00325 
00326   //@}
00327 
00328 
00329   /** @name Access to vertices */
00330   //        ==================
00331   //@{
00332 
00333   /**
00334    * @brief Get number of vertices.
00335    */
00336   inline int size() const;
00337 
00338   /**
00339    * @brief Get the vertices.
00340    */
00341   inline vertices_const_reference accessVertices() const;
00342 
00343   /**
00344    * @brief Give non-protected access to the vertices.
00345    *
00346    * Vertices may then be directly modified using appropriate
00347    * transformation function members.
00348    */
00349   inline vertices_reference getVertices();
00350 
00351   /**
00352    * @brief Get a copy of the vertices.
00353    */
00354   inline vertices_type vertices() const;
00355 
00356   //@}
00357 
00358 
00359   /** @name Insert vertices */
00360   //        ===============
00361   //@{
00362 
00363   /**
00364    * @brief Insert a point as new source.
00365    *
00366    * @param aPt  a point
00367    */
00368   GenPolyline<value_type>&
00369   appendSource(const GenPoint<value_type>& aPt);
00370 
00371   /**
00372    * @brief Insert a point as new target.
00373    *
00374    * @param aPt  a point
00375    */
00376   GenPolyline<value_type>&
00377   appendTarget(const GenPoint<value_type>& aPt);
00378 
00379   /**
00380    * @brief Appends a polyline.
00381    *
00382    * @param aPoly  the polyline to be appended
00383    *
00384    * @return a reference to the current polyline
00385    */
00386   GenPolyline<value_type>&
00387   append(const GenPolyline<value_type>& aPoly);
00388 
00389   /**
00390    * @brief Appends a Qgar polyline.
00391    *
00392    * @param aQPoly  the Qgar polyline to be appended
00393    *
00394    * @return a reference to the current polyline
00395    */
00396   GenPolyline<value_type>&
00397   append(const GenQgarPolyline<value_type>& aQPoly);
00398 
00399   /**
00400    * @brief Appends a set of vertices to a polyline.
00401    *
00402    * The given points are inserted after the target point.
00403    * The order of the given vertices is preserved
00404    * in the resulting polyline.
00405    * The last point of the vector becomes the new target.
00406    *
00407    * @param aPtVect  the vector of points to be appended
00408    *
00409    * @return a reference to the current polyline
00410    */
00411   GenPolyline<value_type>&
00412   append(const std::vector< GenPoint<value_type> >& aPtVect);
00413 
00414   //@}
00415 
00416 
00417   /** @name Remove vertices */
00418   //        ===============
00419   //@{
00420 
00421   /**
00422    * @brief Remove a vertex from the polyline.
00423    *
00424    * @param aPt  a point
00425    *
00426    * @exception qgar::QgarErrorUser
00427    * (no removal in a polyline including 2 vertices)
00428    */
00429   GenPolyline<value_type>&
00430   remove(const GenPoint<value_type>& aPt)
00431     throw(QgarErrorUser);
00432 
00433   //@}
00434 
00435 
00436   /** @name Conversions of the vertices */
00437   //        ===========================
00438   //@{
00439 
00440   /**
00441    * @brief Get a vector of points from the vertices.
00442    */
00443   std::vector< GenPoint<value_type> > toPointVector();
00444 
00445   /**
00446    * @brief Get a <b>list</b> of (geometrical) segments
00447    *        from the vertices.
00448    */
00449   std::list< GenSegment<value_type> > toSegList();
00450 
00451   /**
00452    * @brief Get a <b>list</b> of Qgar segments, provided
00453    *   with given attributes, from the vertices.
00454    *
00455    * @param aThickness  thickness (default <b>1</b>)
00456    * @param aColor      color     (default qgar::QGE_COLOR_DEFAULT)
00457    * @param anOutline   outline   (default qgar::QGE_OUTLINE_DEFAULT) 
00458    */
00459   std::list< GenQgarSegment<value_type> > 
00460   toQgarSegList(int        aThickness = 1,
00461                 QGEcolor   aColor     = QGE_COLOR_DEFAULT,
00462                 QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00463 
00464   //@}
00465 
00466 
00467   /**@name Geometry: Translation */
00468   //       =====================
00469   //@{
00470 
00471   /**
00472    * @brief Translate current polyline along X and Y axis.
00473    * @param aTransX  X translation factor
00474    * @param aTransY  Y translation factor
00475    */
00476   virtual void translate(value_type aTransX, value_type aTransY);
00477 
00478   //@}
00479 
00480 
00481   /** @name Serialization/deserialization */
00482   //        =============================
00483   //@{
00484 
00485   /**
00486    * @brief Deserializes the current polyline from an input stream.
00487    *
00488    * A serialized polyline is represented as:
00489 @verbatim
00490 Polyline(<SOURCE>)(<TARGET>)(<VERTICES COUNT>)(<VERTICE 1>)..(<VERTICE n>)
00491 @endverbatim
00492    *
00493    * @param anInStream  the input stream
00494    */
00495   inline virtual std::istream& read(std::istream& anInStream);
00496 
00497   /**
00498    * @brief Serializes the current polyline to an input stream.
00499    *
00500    * A serialized polyline is represented as:
00501 @verbatim
00502 Polyline(<SOURCE>)(<TARGET>)(<VERTICES COUNT>)(<VERTICE 1>)..(<VERTICE n>)
00503 @endverbatim
00504    *
00505    * @param anOutStream  the output stream
00506    */
00507   inline virtual std::ostream& write(std::ostream& anOutStream) const;
00508 
00509   //@}
00510 
00511 
00512 // -------------------------------------------------------------------
00513 // P R O T E C T E D    M E M B E R S
00514 // -------------------------------------------------------------------
00515 protected:
00516 
00517 
00518   /** @name Structure of a polyline */
00519   //        =======================
00520   //@{
00521 
00522   /**
00523    * @brief Vertices.
00524    */
00525   vertices_type _vertices;
00526 
00527   //@}
00528 
00529 
00530   /** @name Updates subsequent to source and target modifications */
00531   //        =====================================================
00532   //@{
00533 
00534   /**
00535    * @brief Update the geometrical structure
00536    *        when the source has been changed.
00537    */
00538   virtual void updateSource();
00539 
00540   /**
00541    * @brief Update the geometrical structure
00542    *        when the target has been changed.
00543    */
00544   virtual void updateTarget();
00545 
00546   /**
00547    * @brief Update the geometrical structure
00548    *        when both source and target have been changed.
00549    */
00550   virtual void updateSourceTarget();
00551 
00552   //@}
00553 
00554 
00555 // -------------------------------------------------------------------
00556 }; // class GenPolyline
00557 
00558 
00559 
00560 
00561 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00562 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00563 // P R E D E F I N E D    P O L Y L I N E    T Y P E S
00564 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00565 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00566 
00567 
00568 /**@name Polyline types */
00569 //       ==============
00570 //@{
00571 
00572 /**
00573  * @brief Polyline with <b>integer</b> coordinates.
00574  * @ingroup DS_PRIM_GEOM
00575  *
00576  * @see qgar::IPolyline
00577  */
00578 typedef GenPolyline<int> Polyline;
00579 
00580 /**
00581  * @brief Polyline with <b>integer</b> coordinates.
00582  * @ingroup DS_PRIM_GEOM
00583  *
00584  * @see qgar::Polyline
00585  */
00586 typedef GenPolyline<int> IPolyline;
00587 
00588 /**
00589  * @brief Polyline with <b>float</b> coordinates.
00590  * @ingroup DS_PRIM_GEOM
00591  */
00592 typedef GenPolyline<float> FPolyline;
00593 
00594 /**
00595  * @brief Polyline with <b>double</b> coordinates.
00596  * @ingroup DS_PRIM_GEOM
00597  */
00598 typedef GenPolyline<double> DPolyline;
00599 
00600 //@}
00601 
00602 
00603 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00604 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00605 
00606 
00607 } // namespace qgar 
00608 
00609 
00610 #endif /* ___QGAR_GENPOLYLINE_H_INCLUDED__ */