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_GENPOINT_H_INCLUDED__ 00029 #define ___QGAR_GENPOINT_H_INCLUDED__ 00030 00031 00032 /** 00033 * @file _QGAR_GenPoint.H 00034 * @brief Header file of class qgar::GenPoint. 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 15:55 00041 * @since Qgar 2.2 00042 */ 00043 00044 00045 // For RCS/CVS use: Do not delete 00046 /* $Id: _QGAR_GenPoint.H,v 1.5 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 00055 00056 00057 namespace qgar 00058 { 00059 00060 00061 /** 00062 * @class GenPoint _QGAR_GenPoint.H "qgarlib/primitives.H" 00063 * @ingroup DS_POINT 00064 * @brief Point with coordinates of type <b>T</b>. 00065 * 00066 * <b>This class is not supposed to be derived: The destructor 00067 * (as any other function) is not virtual.</b> 00068 * 00069 * <ul> 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 * and angles are clockwise from the X axis. 00085 * </li> 00086 * <li> 00087 * Predefined types: 00088 * - qgar::Point 00089 * - qgar::IPoint 00090 * - qgar::FPoint 00091 * - qgar::DPoint 00092 * </li> 00093 * </ul> 00094 * 00095 * 00096 * @warning 00097 * Most of the functions performing geometrical transformations work 00098 * with points and primitives having <b>double</b> coordinates only. 00099 * 00100 * 00101 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Gérald Masini">Gérald Masini</a> 00102 * @date July 4, 2001 14:07 00103 * @since Qgar 1.0 00104 */ 00105 template <class T> class GenPoint 00106 { 00107 // ------------------------------------------------------------------- 00108 // T Y P E D E F I N I T I O N S 00109 // ------------------------------------------------------------------- 00110 public: 00111 00112 00113 /** @name Types */ 00114 // ===== 00115 //@{ 00116 00117 /** 00118 * @brief Type of the coordinates. 00119 */ 00120 typedef T value_type; 00121 00122 /** 00123 * @brief Reference to qgar::GenPoint::value_type. 00124 */ 00125 typedef value_type& reference; 00126 00127 /** 00128 * @brief Constant reference to qgar::GenPoint::value_type. 00129 */ 00130 typedef const value_type& const_reference; 00131 00132 /** 00133 * @brief Pointer to qgar::GenPoint::value_type. 00134 */ 00135 typedef value_type* pointer; 00136 00137 /** 00138 * @brief Constant pointer to qgar::GenPoint::value_type. 00139 */ 00140 typedef const value_type* const_pointer; 00141 00142 //@} 00143 00144 00145 // ------------------------------------------------------------------- 00146 // P U B L I C M E M B E R S 00147 // ------------------------------------------------------------------- 00148 public: 00149 00150 00151 /** @name Constructors */ 00152 // ============ 00153 //@{ 00154 00155 /** 00156 * @brief Default constructor: Create a point at <b>(0,0)</b>. 00157 */ 00158 GenPoint(); 00159 00160 /** 00161 * @brief Copy constructor. 00162 */ 00163 GenPoint(const GenPoint<value_type>& aPt); 00164 00165 /** 00166 * @brief Conversion of a point of a type different 00167 * from the effective type. 00168 * 00169 * @param aPt a point 00170 * 00171 * @warning This kind of conversion must be explicitely 00172 * specified by the client. 00173 */ 00174 template <class U> 00175 explicit GenPoint(const GenPoint<U>& aPt); 00176 00177 /** 00178 * @brief Initialize from coordinates. 00179 */ 00180 GenPoint(value_type aX, value_type aY); 00181 00182 //@} 00183 00184 00185 /** @name Destructor */ 00186 // ========== 00187 //@{ 00188 00189 /** 00190 * @brief Non-virtual destructor (see the class header). 00191 */ 00192 ~GenPoint(); 00193 00194 //@} 00195 00196 00197 /** @name Access to coordinates */ 00198 // ===================== 00199 //@{ 00200 00201 /** 00202 * @brief Get X coordinate. 00203 */ 00204 inline value_type x() const; 00205 00206 /** 00207 * @brief Get Y coordinate. 00208 */ 00209 inline value_type y() const; 00210 00211 /** 00212 * @brief Get radius (<i>rho</i>) in polar coordinates. 00213 */ 00214 inline double rho() const; 00215 00216 /** 00217 * @brief Get angle (<i>theta</i>) in polar coordinates. 00218 */ 00219 inline double theta() const; 00220 00221 //@} 00222 00223 00224 /** @name Assign coordinates */ 00225 // ================== 00226 //@{ 00227 00228 /** 00229 * @brief Set X coordinate. 00230 * 00231 * @param aX new X coordinate 00232 */ 00233 inline void setX(value_type aX); 00234 00235 /** 00236 * @brief Set Y coordinate. 00237 * 00238 * @param aY new Y coordinate 00239 */ 00240 inline void setY(value_type aY); 00241 00242 /** 00243 * @brief Set both X and Y coordinates. 00244 * 00245 * @param aX new X coordinate 00246 * @param aY new Y coordinate 00247 */ 00248 inline void setXY(value_type aX, value_type aY); 00249 00250 //@} 00251 00252 00253 // =================================================================== 00254 /** @name Operators 00255 * 00256 * @warning Using stand-alone versions of operators 00257 * (<b>operator+</b>, <b>operator-</b>) is much less efficient then 00258 * using assignment versions (<b>operator+=</b>, <b>operator-=<b/>). 00259 * For example, an expression like: 00260 @verbatim 00261 res = a + b - c; 00262 @endverbatim 00263 * uses 2 temporary objects, one for each call to <b>operator+</b> 00264 * and <b>operator-</b>. These considerations also apply to functional 00265 * operators (see the corresponding section). To preserve efficiency, 00266 * the code should be written in this way: 00267 @verbatim 00268 res = a; 00269 res += b; // no temporary needed 00270 res -= c; // no temporary needed 00271 @endverbatim 00272 */ 00273 // =================================================================== 00274 //@{ 00275 00276 /** 00277 * @brief Assignment. 00278 */ 00279 inline GenPoint<value_type>& 00280 operator=(const GenPoint<value_type>& aPt); 00281 00282 /** 00283 * @brief Same as function qgar::GenPoint::plus. 00284 */ 00285 inline const GenPoint<value_type> 00286 operator+(const GenPoint<value_type>& aPt) const; 00287 00288 /** 00289 * @brief Same as function qgar::GenPoint::plusEqual. 00290 */ 00291 inline GenPoint<value_type>& 00292 operator+=(const GenPoint<value_type>& aPt); 00293 00294 /** 00295 * @brief Same as function qgar::GenPoint::minus. 00296 */ 00297 inline const GenPoint<value_type> 00298 operator-(const GenPoint<value_type>& aPt) const; 00299 00300 /** 00301 * @brief Same as function qgar::GenPoint::minusEqual. 00302 */ 00303 inline GenPoint<value_type>& 00304 operator-=(const GenPoint<value_type>& aPt); 00305 00306 /** 00307 * @brief Same as function qgar::GenPoint::eq. 00308 */ 00309 inline bool 00310 operator==(const GenPoint<value_type>& aPt) const; 00311 00312 /** 00313 * @brief Same as function qgar::GenPoint::notEq. 00314 */ 00315 inline bool 00316 operator!=(const GenPoint<value_type>& aPt) const; 00317 00318 //@} 00319 00320 00321 // =================================================================== 00322 /** @name Functional operators 00323 * 00324 * @warning Using stand-alone versions of operators 00325 * (<b>plus</b>, <b>minus</b>) is much less efficient then 00326 * using assignment versions (<b>plusEqual</b>, <b>minusEqual</b>). 00327 * For example, an expression like: 00328 @verbatim 00329 res = a.plus(b).minus(c); 00330 @endverbatim 00331 * uses 2 temporary objects, one for each call to <b>plus</b> 00332 * and <b>minus</b>. These considerations also apply to operators 00333 * (see the corresponding section). To preserve efficiency, the code 00334 * should be written in this way: 00335 @verbatim 00336 res = a; 00337 res.plusEqual(b); // no temporary needed 00338 res.minusEqual(c); // no temporary needed 00339 @endverbatim 00340 * 00341 * Operators implementation uses Scott Meyers' tips from 00342 * [<a href="Bibliography.html#Meyer-1996">Meyer, 1996</a>]: 00343 * item #22, pages 107-110. 00344 */ 00345 // =================================================================== 00346 //@{ 00347 00348 /** 00349 * @brief Add coordinates of the given point to those 00350 * of the current point, and return them as a new point. 00351 * 00352 * @param aPt a point 00353 */ 00354 const GenPoint<value_type> plus(const GenPoint<value_type>& aPt) const; 00355 00356 /** 00357 * @brief Add coordinates of the given point to those 00358 * of the current point. 00359 * 00360 * The current point is modified. 00361 * 00362 * @param aPt a point 00363 */ 00364 GenPoint<value_type>& plusEqual(const GenPoint<value_type>& aPt); 00365 00366 /** 00367 * @brief Substract coordinates of the given point to those 00368 * of the current point, and return them as a new point. 00369 * 00370 * @param aPt a point 00371 */ 00372 const GenPoint<value_type> minus(const GenPoint<value_type>& aPt) const; 00373 00374 /** 00375 * @brief Substract coordinates of the given point to those 00376 * of the current point. 00377 * 00378 * The current point is modified. 00379 * 00380 * @param aPt a point 00381 */ 00382 GenPoint<value_type>& minusEqual(const GenPoint<value_type>& aPt); 00383 00384 /** 00385 * @brief Equality. 00386 * 00387 * Return <b>true</b> if the current point and the given point 00388 * have the same coordinates. 00389 * 00390 * @param aPt a point 00391 */ 00392 bool eq(const GenPoint<value_type>& aPt) const; 00393 00394 /** 00395 * @brief Inequality. 00396 * 00397 * Return <b>true</b> if the coordinates of the current point 00398 * and of the given point are different. 00399 * 00400 * @param aPt a point 00401 */ 00402 bool notEq(const GenPoint<value_type>& aPt) const; 00403 00404 //@} 00405 00406 00407 /** @name Geometry: projection */ 00408 // ==================== 00409 //@{ 00410 00411 /** 00412 * @brief Orthogonal projection of the current point 00413 * onto the line supporting the given segment. 00414 * 00415 * @param aSeg a segment 00416 * 00417 * @warning This function applies only to points and 00418 * segments having coordinates of type <b>double</b>. 00419 */ 00420 inline void project(const GenSegment<value_type>& aSeg); 00421 00422 /** 00423 * @brief Orthogonal projection of the current point 00424 * onto the line supporting the given Qgar segment. 00425 * 00426 * @param aQSeg a Qgar segment 00427 * 00428 * @warning This function applies only to points and 00429 * Qgar segments having coordinates of type <b>double</b>. 00430 */ 00431 inline void project(const GenQgarSegment<value_type>& aQSeg); 00432 00433 //@} 00434 00435 00436 /** @name Geometry: translation */ 00437 // ===================== 00438 //@{ 00439 00440 /** 00441 * @brief Translate current point along X and Y axis. 00442 * 00443 * @param aTransX X translation factor 00444 * @param aTransY Y translation factor 00445 */ 00446 void translate(value_type aTransX, value_type aTransY); 00447 00448 //@} 00449 00450 00451 // /** @name Geometry: rotation */ 00452 // // ================== 00453 // //@{ 00454 00455 // /** 00456 // * @brief Rotate by a given angle, using the origin 00457 // * of the coordinates system as the rotation center. 00458 // * @param anAngle an angle 00459 // * 00460 // * @warning This function applies only to points 00461 // * having coordinates of type <b>double</b>. 00462 // */ 00463 // void rotate(double anAngle); 00464 00465 // /** 00466 // * @brief Rotate by a given angle, 00467 // * using the given point as the rotation center. 00468 // * @param anAngle an angle 00469 // * @param aPt a point 00470 // * 00471 // * @warning This function applies only to points 00472 // * having coordinates of type <b>double</b>. 00473 // */ 00474 // void rotate(double anAngle, const GenPoint<T>& aPt); 00475 00476 // /** 00477 // * @brief Rotate by \f$ \pi \f$/2, using the origin 00478 // * of the coordinates system as the rotation center. 00479 // * 00480 // * @warning This function applies only to points 00481 // * having coordinates of type <b>double</b>. 00482 // */ 00483 // void rotate90(); 00484 00485 // /** 00486 // * @brief Rotate \f$ \pi \f$/2, 00487 // * using the given point as the rotation center. 00488 // * @param aPt a point 00489 // * 00490 // * @warning This function applies only to points 00491 // * having coordinates of type <b>double</b>. 00492 // */ 00493 // void rotate90(const GenPoint<T>& aPt); 00494 00495 // /** 00496 // * @brief Rotate by \f$ \pi \f$, using the origin 00497 // * of the coordinates system as the rotation center. 00498 // * 00499 // * @warning This function applies only to points 00500 // * having coordinates of type <b>double</b>. 00501 // */ 00502 // inline void rotate180(); 00503 00504 // /** 00505 // * @brief Rotate by \f$ \pi \f$/2, 00506 // * using the given point as the rotation center. 00507 // * @param aPt a point 00508 // * 00509 // * @warning This function applies only to points 00510 // * having coordinates of type <b>double</b>. 00511 // */ 00512 // inline void rotate180(const GenPoint<T>& aPt); 00513 00514 00515 // /** 00516 // * @brief Rotate by a 3\f$ \pi \f$/2, using the origin 00517 // * of the coordinates system as the rotation center. 00518 // * 00519 // * @warning This function applies only to points 00520 // * having coordinates of type <b>double</b>. 00521 // */ 00522 // void rotate270(); 00523 00524 // /** 00525 // * @brief Rotate by 3\f$ \pi \f$/2, 00526 // * using the given point as the rotation center. 00527 // * @param aPt a point 00528 // * 00529 // * @warning This function applies only to points 00530 // * having coordinates of type <b>double</b>. 00531 // */ 00532 // void rotate270(const GenPoint<T>& aPt); 00533 00534 // //@} 00535 00536 00537 // /** @name Geometry: homothety */ 00538 // // =================== 00539 // //@{ 00540 00541 // /** 00542 // * @brief Homothety with respect to the origin of the 00543 // * coordiantes system, using the given dilation factor. 00544 // * @param aDilFactor dilation factor 00545 // * 00546 // * @warning This function applies only to points 00547 // * having coordinates of type <b>double</b>. 00548 // */ 00549 // void homothety(double aDilFactor); 00550 00551 // /** 00552 // * @brief Homothety with respect to the given point, 00553 // * using the given dilation factor. 00554 // * @param aDilFactor dilation factor 00555 // * @param aPt a point 00556 // * 00557 // * @warning This function applies only to points 00558 // * having coordinates of type <b>double</b>. 00559 // */ 00560 // void homothety(double aDilFactor, const GenPoint<T>& aPt); 00561 00562 // //@} 00563 00564 00565 // /** @name Geometry: symmetry */ 00566 // // ================== 00567 // //@{ 00568 00569 // /** 00570 // * @brief Apply a central symmetry to the current point, using 00571 // * the origin of the coordinates system as the symmetry center. 00572 // */ 00573 // void symmetry(); 00574 00575 // /** 00576 // * @brief Apply a central symmetry to the current point, using 00577 // * the given point as the symmetry center. 00578 // * @param aPt a point, center of the symmetry 00579 // */ 00580 // void symmetry(const GenPoint<T>& aPt); 00581 00582 // /** 00583 // * @brief Apply a mirror symmetry to the current point, using 00584 // * the line supporting the given segment for the symmetry. 00585 // * @param aSeg segment supporting the line used for the symmetry 00586 // * 00587 // * @warning This function applies only to points and segments 00588 // * having coordinates of type <b>double</b>. 00589 // */ 00590 // void symmetry(const GenSegment<T>& aSeg); 00591 00592 // //@} 00593 00594 00595 // ------------------------------------------------------------------- 00596 // P R O T E C T E D M E M B E R S 00597 // ------------------------------------------------------------------- 00598 protected: 00599 00600 00601 /** @name Representation of a point */ 00602 // ========================= 00603 //@{ 00604 00605 /** 00606 * @brief X coordinate. 00607 */ 00608 value_type _x; 00609 00610 /** 00611 * @brief Y coordinate. 00612 */ 00613 value_type _y; 00614 00615 //@} 00616 00617 00618 // ------------------------------------------------------------------- 00619 00620 }; // class GenPoint 00621 00622 00623 00624 00625 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00626 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00627 // S E R I A L I Z A T I O N / D E S E R I A L I Z A T I O N 00628 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00629 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00630 00631 00632 // =============================================================== 00633 /** 00634 * @name Point serialization/deserialization operators 00635 * 00636 * @warning Class qgar::GenPoint does not derive from class 00637 * qgar::ISerializable to get a smaller implementation of a point, 00638 * and thus to save memory space when dealing with (lots of) 00639 * points; if points were serializable, they would be implemented 00640 * using an extra pointer to the table for virtual functions 00641 * (the destructor in this case). 00642 */ 00643 // =============================================================== 00644 //@{ 00645 00646 /** 00647 * @ingroup IO_SERIAL 00648 * 00649 * @brief Deserialize a point (read it from an input stream). 00650 * 00651 * A serialized point is represented as: 00652 @verbatim 00653 Point ( <X> ) ( <Y> ) 00654 @endverbatim 00655 * 00656 * @param anInStream input stream 00657 * @param aPt deserialized point 00658 * 00659 * @see qgar::qgReadObjName, qgar::qgReadObjData 00660 */ 00661 template <class T> 00662 inline std::istream& 00663 operator>>(std::istream& anInStream, GenPoint<T>& aPt); 00664 00665 00666 /** 00667 * @ingroup IO_SERIAL 00668 * 00669 * @brief Serialize a point (write it into an output stream). 00670 * 00671 * A serialized point is represented as: 00672 @verbatim 00673 Point ( <X> ) ( <Y> ) 00674 @endverbatim 00675 * 00676 * @param anOutStream output stream 00677 * @param aPt point to serialize 00678 */ 00679 template <class T> 00680 inline std::ostream& 00681 operator<<(std::ostream& anOutStream, const GenPoint<T>& aPt); 00682 00683 //@} 00684 00685 00686 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00687 // SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS 00688 00689 00690 00691 00692 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00693 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00694 // P R E D E F I N E D P O I N T T Y P E S 00695 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00696 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00697 00698 00699 /**@name Point types */ 00700 // =========== 00701 //@{ 00702 00703 /** 00704 * @brief Points with integer coordinates. 00705 * @ingroup DS_PRIM_GEOM 00706 * 00707 * @see qgar::IPoint 00708 */ 00709 typedef GenPoint<int> Point; 00710 00711 /** 00712 * @brief Points with integer coordinates. 00713 * @ingroup DS_PRIM_GEOM 00714 * 00715 * @see qgar::Point 00716 */ 00717 typedef GenPoint<int> IPoint; 00718 00719 /** 00720 * @brief Points with coordinates of type <b>float</b>. 00721 * @ingroup DS_PRIM_GEOM 00722 */ 00723 typedef GenPoint<float> FPoint; 00724 00725 /** 00726 * @brief Points with coordinates of type <b>double</b>. 00727 * @ingroup DS_PRIM_GEOM 00728 */ 00729 typedef GenPoint<double> DPoint; 00730 00731 //@} 00732 00733 00734 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00735 // TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 00736 00737 00738 } // namespace qgar 00739 00740 00741 #endif /* ___QGAR_GENPOINT_H_INCLUDED__ */