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

_QGAR_GenQgarSegment.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_GENQGARSEGMENT_H_INCLUDED__
00029 #define ___QGAR_GENQGARSEGMENT_H_INCLUDED__
00030 
00031 
00032 /**
00033  * @file     _QGAR_GenQgarSegment.H
00034  * @brief    Header file of class qgar::GenQgarSegment.
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 15, 2004  14:14
00041  * @since    Qgar 2.2
00042  */
00043 
00044 
00045 // For RCS/CVS use: Do not delete
00046 /* $Id: _QGAR_GenQgarSegment.H,v 1.6 2005/10/14 17:05:48 masini Exp $ */
00047 
00048 
00049 
00050 // STD
00051 #include <iosfwd>  // Avoid including classes when not necessary
00052 // QGAR
00053 #include <qgarlib/ISerializable.H>
00054 #include <qgarlib/math.H>
00055 
00056 
00057 
00058 namespace qgar
00059 {
00060 
00061 
00062 /**
00063  * @class GenQgarSegment _QGAR_GenQgarSegment.H "qgarlib/primitives.H"
00064  * @ingroup DS_PRIM_QGAR
00065  * @brief Graphical segment, so-called <i>Qgar segment</i>,
00066  *   with coordinates of type <b>T</b>.
00067  *
00068  * It typically results from vectorization.
00069  *
00070  *
00071  * <ul>
00072  *
00073  * <li>
00074 @verbatim
00075    O
00076     +---------------> X
00077     |\    |
00078     | \ <-'
00079     |  \    angle (in radians, unless specified)
00080     |   \
00081     |
00082     V
00083 
00084     Y
00085 @endverbatim
00086  * The origin of the coordinate system is at top left corner
00087  * and angles are clockwise from the X axis.
00088  * </li>
00089  *
00090  * <li>
00091 @verbatim
00092    O
00093     +---------------> X
00094     |
00095     |        + source
00096     |       /
00097     |      /
00098     |     v
00099     V    + target
00100 
00101     Y
00102 @endverbatim
00103  * A Qgar segment is oriented: It is provided with a so-called
00104  * <i>source point</i> and a so-called <i>target point</i>,
00105  * which determine the orientation of the vector representing
00106  * the segment in geometrical computations.
00107  * </li>
00108  *
00109  * <li>
00110  * Predefined types:
00111  * qgar::QgarSegment,
00112  * qgar::IQgarSegment,
00113  * qgar::FQgarSegment,
00114  * qgar::DQgarSegment.
00115  * </li>
00116  *
00117  * <li>
00118  * A Qgar segment is provided with attributes: thickness, color,
00119  * and outline. When such features are useless, see class
00120  * qgar::GenSegment (for geometrical segments).
00121  * </li>
00122  *
00123  * </ul>
00124  *
00125  *
00126  * @author   <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a>
00127  * @date     July 2, 2001  18:07
00128  * @since    Qgar 2.1
00129  */
00130 template <class T>
00131 class GenQgarSegment
00132 
00133   : public AbstractGenQgarPrimitive<T>,
00134     public ISerializable
00135 
00136 {
00137 // -------------------------------------------------------------------
00138 // T Y P E   D E F I N I T I O N S
00139 // -------------------------------------------------------------------
00140 public:
00141 
00142 
00143   /** @name Types */
00144   //        =====
00145   //@{
00146 
00147   /**
00148    * @brief Type of the source and target coordinates.
00149    */
00150   typedef T value_type;
00151 
00152   /**
00153    * @brief Reference to qgar::GenQgarSegment::value_type.
00154    */
00155   typedef value_type& reference;
00156 
00157   /**
00158    * @brief Constant reference to qgar::GenQgarSegment::value_type.
00159    */
00160   typedef const value_type& const_reference;
00161 
00162   /**
00163    * @brief Pointer to qgar::GenQgarSegment::value_type.
00164    */
00165   typedef value_type* pointer;
00166 
00167   /**
00168    * @brief Constant pointer to qgar::GenQgarSegment::value_type.
00169    */
00170   typedef const value_type* const_pointer;
00171 
00172   //@}
00173 
00174 
00175 // -------------------------------------------------------------------
00176 // P U B L I C    M E M B E R S
00177 // -------------------------------------------------------------------
00178 public:
00179 
00180 
00181   /** @name Constructors */
00182   //        ============
00183   //@{
00184 
00185   /**
00186    * @brief Default constructor.
00187    *
00188    * Zero-length segment located at <b>(0, 0)</b>.
00189    *
00190    * @param aThickness  thickness (default <b>1</b>)
00191    * @param aColor      color (default qgar::QGE_COLOR_DEFAULT)
00192    * @param anOutline   outline (default qgar::QGE_OUTLINE_DEFAULT) 
00193    *
00194    * @todo The created Qgar segment does not conform
00195    *       to the definition of a segment!
00196    */
00197   GenQgarSegment(int        aThickness = 1,
00198                  QGEcolor   aColor     = QGE_COLOR_DEFAULT,              
00199                  QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00200 
00201   /**
00202    * @brief Copy constructor.
00203    */
00204   GenQgarSegment(const GenQgarSegment<value_type>& aQSeg);
00205 
00206   /**
00207    * @brief Initialize from a geometrical segment.
00208    *
00209    * @param aSeg       a segment
00210    * @param aThickness thickness (default <b>1</b>)
00211    * @param aColor     color (default qgar::QGE_COLOR_DEFAULT)
00212    * @param anOutline  outline (default qgar::QGE_OUTLINE_DEFAULT)
00213    */
00214   explicit GenQgarSegment(const GenSegment<value_type>& aSeg,
00215                           int        aThickness = 1,
00216                           QGEcolor   aColor     = QGE_COLOR_DEFAULT,
00217                           QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00218 
00219   /**
00220    * @brief Initialize from source and target points.
00221    *
00222    * @param aSource    source point
00223    * @param aTarget    target point
00224    * @param aThickness thickness (default <b>1</b>)
00225    * @param aColor     color (default qgar::QGE_COLOR_DEFAULT)
00226    * @param anOutline  outline (default qgar::QGE_OUTLINE_DEFAULT)
00227    */
00228   GenQgarSegment(const GenPoint<value_type>& aSource,
00229                  const GenPoint<value_type>& aTarget,
00230                  int        aThickness = 1,
00231                  QGEcolor   aColor     = QGE_COLOR_DEFAULT,
00232                  QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00233 
00234   /**
00235    * @brief Initialize from coordinates.
00236    *
00237    * @param aXSource   X coordinate of the source point
00238    * @param aYSource   Y coordinate of the source point
00239    * @param aXTarget   X coordinate of the target point
00240    * @param aYTarget   X coordinate of the target point
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   GenQgarSegment(value_type aXSource,
00246                  value_type aYSource,
00247                  value_type aXTarget,
00248                  value_type aYTarget,
00249                  int        aThickness = 1,
00250                  QGEcolor   aColor     = QGE_COLOR_DEFAULT,
00251                  QGEoutline anOutline  = QGE_OUTLINE_DEFAULT);
00252 
00253   //@}
00254 
00255 
00256   /** @name Destructor */
00257   //        ==========
00258   //@{
00259 
00260   /**
00261    * @brief Virtual destructor.
00262    */
00263   virtual ~GenQgarSegment();
00264 
00265   //@}
00266 
00267 
00268   /** @name Copy */
00269   //        ====
00270   //@{
00271 
00272   virtual GenQgarSegment<value_type>* clone() const;
00273 
00274   //@}
00275 
00276 
00277   /** @name Access to geometrical features */
00278   //        ==============================
00279   //@{
00280 
00281   /**
00282    * @brief Get the structure implementing
00283    * the geometrical aspect of a Qgar segment.
00284    */
00285   inline const GenSegment<value_type>& accessGeomStructure() const;
00286 
00287   /**
00288    * @brief Get length of the segment.
00289    */
00290   inline double length() const;
00291 
00292   /**
00293    * @brief Get squared length of the segment.
00294    */
00295   inline double sqr_length() const;
00296 
00297   /**
00298    * @brief Distance between point <b>(0,0)</b> and its perpendicular
00299    * projection onto the line supporting the current segment.
00300    *
00301    * Always positive.
00302    */
00303   inline double rho() const;
00304 
00305   /**
00306    * @brief Angle between the X axis and the segment,
00307    *   in [0, 2PI[ radians.
00308    *
00309 @verbatim
00310                                            ----->/
00311  O                             O          /     /
00312   +------------------> X        +---------|-------------> X
00313   |    \    |                   |         |   /  |
00314   |     \ <-' angle             |   angle \_ /__/
00315   |      \                      |           /
00316   |       + source              |          + target
00317   |        \                    |         ^
00318   |         \                   |        /
00319   |          v                  |       /
00320   v           + target          v      + source
00321 
00322   Y                             Y
00323 @endverbatim
00324    *
00325    * @warning The current segment is assimilited to a vector
00326    *   joining its source to its target.
00327    */
00328   inline double theta() const;
00329 
00330   /**
00331    * @brief Same as qgar::GenQgarSegment::theta.
00332    */
00333   inline double angle() const;
00334 
00335   /**
00336    * @brief Angle between the X axis and the segment, in [0, PI[ radians.
00337    *
00338 @verbatim
00339  O                           O
00340   +------------------> X      +-----------------> X
00341   |    \    |                 |    \    |
00342   |     \ <-' angle           |     \ <-' angle
00343   |      \                    |      \
00344   |       + source            |       + target
00345   |        \                  |        \
00346   |         \                 |         \
00347   |          \                |          \
00348   v           + target        v           + source
00349 
00350   Y                           Y
00351 @endverbatim
00352    *
00353    * @warning The current segment is not assimilited to a vector.
00354    */
00355   inline double slope() const;
00356 
00357   /**
00358    * @brief Same as qgar::GenQgarSegment::theta,
00359    * but the result is given in <b>[0,360[</b> degrees.
00360    */
00361   inline double thetaDegrees() const;
00362 
00363   /**
00364    * @brief Same as qgar::GenQgarSegment::theta,
00365    * but the result is given in <b>[0,360[</b> degrees.
00366    */
00367   inline double angleDegrees() const;
00368 
00369   /**
00370    * @brief Same as qgar::GenQgarSegment::slope,
00371    * but the result is given in <b>[0,180[</b> degrees.
00372    */
00373   inline double slopeDegrees() const;
00374 
00375   //@}
00376 
00377   
00378   /**@name Geometrical predicates */
00379   //       ======================
00380   //@{
00381 
00382   /**
00383    * @brief Return <b>true</b> if the given point approximately
00384    *   belongs to the current Qgar segment.
00385    *
00386    * Here, ''to belong'' means to be at a distance smaller or equal
00387    * to the given threshold from some point between the source
00388    * and the target of the segment.
00389    *
00390    * @param c      a point
00391    * @param aDist  threshold used to decide whether the given point
00392    *   belongs to the segment (default qgar::Math::epsilonD)
00393    */
00394   inline bool
00395   contains(const GenPoint<T>& c, double aDist = Math::epsilonD());
00396 
00397   //@}
00398 
00399   
00400   /** @name Operators */
00401   //        =========
00402   //@{
00403 
00404   /**
00405    * @brief Assignment.
00406    *
00407    * @param aQSeg  a Qgar segment
00408    */
00409   GenQgarSegment<value_type>&
00410   operator=(const GenQgarSegment<value_type>& aQSeg);
00411 
00412   /**
00413    * @brief Same as function qgar::GenQgarSegment::eq
00414    */
00415   inline bool
00416   operator==(const GenQgarSegment<value_type>& aQSeg) const;
00417 
00418   /**
00419    * @brief Same as function qgar::GenQgarSegment::notEq.
00420    */
00421   inline bool
00422   operator!=(const GenQgarSegment<value_type>& aQSeg) const;
00423 
00424   //@}
00425 
00426 
00427   /**@name Functional operators */
00428   //       ====================
00429   //@{
00430 
00431    /**
00432    * @brief Equality.
00433    *
00434    * @param aQSeg  a segment
00435    *
00436    * @return <b>true</b> if the current segment
00437    * and the given segment have the same coordinates,
00438    * regardless other features.
00439    */
00440   inline bool eq(const GenQgarSegment<value_type>& aQSeg) const;
00441 
00442    /**
00443    * @brief Equality.
00444    *
00445    * @param aQSeg  a segment
00446    *
00447    * @return <b>true</b> if the current segment
00448    * and the given segment have the same coordinates and attributes.
00449    */
00450   bool equal(const GenQgarSegment<value_type>& aQSeg) const;
00451 
00452   /**
00453    * @brief Inequality.
00454    *
00455    * @param aQSeg a segment
00456    *
00457    * @return <b>true</b> if the coordinates
00458    * of the current segment and of the given segment are different,
00459    * regardless other features.
00460    */
00461   inline bool notEq(const GenQgarSegment<value_type>& aQSeg) const;
00462 
00463   /**
00464    * @brief Inequality.
00465    *
00466    * @param aQSeg a segment
00467    *
00468    * @return <b>true</b> if the coordinates or attributes
00469    * of the current segment and of the given segment are different.
00470    */
00471   bool notEqual(const GenQgarSegment<value_type>& aQSeg) const;
00472 
00473   //@}
00474 
00475 
00476   /** @name Serialization/deserialization */
00477   //        =============================
00478   //@{
00479 
00480   /**
00481    * @brief Deserializes the current segment from an input stream.
00482    *
00483    * A serialized Qgar segment is represented as:
00484 @verbatim
00485 QgarSegment(<GEOMETRICAL STRUCTURE>)(<THICKNESS>)(<COLOR>)(<OUTLINE>)
00486 @endverbatim
00487    *
00488    * @param anInStream the input stream
00489    */
00490   virtual std::istream& read(std::istream& anInStream);
00491 
00492 
00493   /**
00494    * @brief Serializes an object to an input stream.
00495    *
00496    * A serialized Qgar segment is represented as:
00497 @verbatim
00498 QgarSegment(<GEOMETRICAL STRUCTURE>)(<THICKNESS>)(<COLOR>)(<OUTLINE>)
00499 @endverbatim
00500    *
00501    * @param anOutStream  the output stream
00502    */
00503   virtual std::ostream& write(std::ostream& anOutStream) const;
00504 
00505   //@}
00506 
00507 
00508 // -------------------------------------------------------------------
00509 // P R O T E C T E D    M E M B E R S
00510 // -------------------------------------------------------------------
00511 protected:
00512 
00513 
00514   /** @name Geometrical structure of a Qgar segment */
00515   //        =======================================
00516   //@{
00517 
00518   /**
00519    * @brief Structure implementing the geometrical aspect.
00520    */
00521   GenSegment<value_type> _geomStructure;
00522 
00523   //@}
00524 
00525 
00526   /**@name Non-protected access to the geometrical aspect */
00527   //       ==============================================
00528   //@{
00529 
00530   /**
00531    * @brief Return the geometrical aspect.
00532    *
00533    * It may then be modified using appropriate
00534    * transformation function members.
00535    */
00536   inline AbstractGenPrimitive<value_type>& getGeomStructure();
00537 
00538   //@}
00539 
00540 
00541 // -------------------------------------------------------------------
00542 }; // class GenQgarSegment
00543 
00544 
00545 
00546 
00547 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00548 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00549 // P R E D E F I N E D    Q G A R    S E G M E N T    T Y P E S
00550 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00551 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00552 
00553 
00554 /** @name Qgar Segments */
00555 //        =============
00556 //@{
00557 
00558 /**
00559  * @ingroup DS_PRIM_QGAR
00560  * @brief Qgar segment with <b>integer</b> coordinates.
00561  *
00562  * @see qgar::IQgarSegment
00563  */
00564 typedef GenQgarSegment<int> QgarSegment;
00565 
00566 /**
00567  * @ingroup DS_PRIM_QGAR
00568  * @brief Qgar segment with <b>integer</b> coordinates.
00569  *
00570  * @see qgar::QgarSegment
00571  */
00572 typedef GenQgarSegment<int> IQgarSegment;
00573 
00574 /**
00575  * @ingroup DS_PRIM_QGAR
00576  * @brief Qgar segment with <b>float</b> coordinates.
00577  */
00578 typedef GenQgarSegment<float> FQgarSegment;
00579 
00580 /**
00581  * @ingroup DS_PRIM_QGAR
00582  * @brief Qgar segment with <b>double</b> coordinates.
00583  */
00584 typedef GenQgarSegment<double> DQgarSegment;
00585 
00586 //@}
00587 
00588 
00589 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00590 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
00591 
00592 
00593 } // namespace qgar
00594 
00595 
00596 #endif /* ___QGAR_GENQGARSEGMENT_H_INCLUDED__ */