Main MRPT website > C++ reference
MRPT logo
lightweight_geom_data.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef LIGHTWEIGHT_GEOM_DATA_H
00029 #define LIGHTWEIGHT_GEOM_DATA_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/utils/stl_extensions.h>
00033 #include <mrpt/utils/TPixelCoord.h>
00034 
00035 #include <mrpt/math/math_frwds.h>  // Fordward declarations
00036 
00037 
00038 
00039 namespace mrpt  {
00040         namespace poses {
00041                 template <class DERIVEDCLASS> class CPoseOrPoint;
00042                 class CPoint2D;
00043                 class CPoint3D;
00044                 class CPose2D;
00045                 class CPose3D;
00046         }
00047         namespace utils { class CStream; }
00048 }
00049 
00050 namespace mrpt  {
00051 namespace math  {
00052         using namespace mrpt::utils; // For "square"
00053 
00054         struct TPoint2D;
00055         struct TPose2D;
00056         struct TPoint3D;
00057         struct TPose3D;
00058         struct TPose3DQuat;
00059 
00060         /** \addtogroup geometry_grp
00061           * @{ */
00062 
00063 
00064         //Pragma defined to ensure no structure packing
00065 #pragma pack(push,1)
00066         //Set of typedefs for lightweight geometric items.
00067         /**
00068           * Lightweight 2D point. Allows coordinate access using [] operator.
00069           * \sa mrpt::poses::CPoint2D
00070           */
00071 //#define TOBJECTS_USE_UNIONS
00072         struct BASE_IMPEXP TPoint2D     {
00073                 /**
00074                   * X coordinate.
00075                   */
00076                 double x;
00077                 /**
00078                   * Y coordinate.
00079                   */
00080                 double y;
00081                 /**
00082                   * Constructor from TPose2D, discarding phi.
00083                   * \sa TPose2D
00084                   */
00085                 explicit TPoint2D(const TPose2D &p);
00086                 /**
00087                   * Constructor from TPoint3D, discarding z.
00088                   * \sa TPoint3D
00089                   */
00090                 explicit TPoint2D(const TPoint3D &p);
00091                 /**
00092                   * Constructor from TPose3D, discarding z and the angular coordinates.
00093                   * \sa TPose3D
00094                   */
00095                 explicit TPoint2D(const TPose3D &p);
00096                 /**
00097                   * Constructor from CPoseOrPoint, perhaps losing 3D information
00098                   * \sa CPoseOrPoint,CPoint3D,CPose2D,CPose3D
00099                   */
00100                 template <class DERIVEDCLASS>
00101                 explicit TPoint2D(const mrpt::poses::CPoseOrPoint<DERIVEDCLASS> &p) :x(p.x()),y(p.y())  {}
00102 
00103                 /** Implicit transformation constructor from TPixelCoordf */
00104                 inline TPoint2D(const mrpt::utils::TPixelCoordf &p) :x(p.x),y(p.y)      {}
00105 
00106                 /** Implicit constructor from CPoint2D  */
00107                 TPoint2D(const mrpt::poses::CPoint2D &p);
00108                 /**
00109                   * Constructor from coordinates.
00110                   */
00111                 inline TPoint2D(double xx,double yy):x(xx),y(yy)        {}
00112                 /**
00113                   * Default fast constructor. Initializes to garbage.
00114                   */
00115                 inline TPoint2D()       {}
00116                 /**
00117                   * Unsafe coordinate access using operator[]. Intended for loops.
00118                   */
00119                 inline double &operator[](size_t i)     {
00120                         return (&x)[i];
00121                 }
00122                 /**
00123                   * Unsafe coordinate access using operator[]. Intended for loops.
00124                   */
00125                 inline const double &operator[](size_t i) const {
00126                         return (&x)[i];
00127                 }
00128                 /**
00129                   * Transformation into vector.
00130                   */
00131                 inline void getAsVector(vector_double &v) const {
00132                         v.resize(2);
00133                         v[0]=x; v[1]=y;
00134                 }
00135 
00136                 bool operator<(const TPoint2D &p) const;
00137 
00138                 inline TPoint2D &operator+=(const TPoint2D &p)  {
00139                         x+=p.x;
00140                         y+=p.y;
00141                         return *this;
00142                 }
00143 
00144                 inline TPoint2D &operator-=(const TPoint2D &p)  {
00145                         x-=p.x;
00146                         y-=p.y;
00147                         return *this;
00148                 }
00149 
00150                 inline TPoint2D &operator*=(double d)   {
00151                         x*=d;
00152                         y*=d;
00153                         return *this;
00154                 }
00155 
00156                 inline TPoint2D &operator/=(double d)   {
00157                         x/=d;
00158                         y/=d;
00159                         return *this;
00160                 }
00161 
00162                 inline TPoint2D operator+(const TPoint2D &p) const      {
00163                         TPoint2D r(*this);
00164                         return r+=p;
00165                 }
00166 
00167                 inline TPoint2D operator-(const TPoint2D &p) const      {
00168                         TPoint2D r(*this);
00169                         return r-=p;
00170                 }
00171 
00172                 inline TPoint2D operator*(double d) const       {
00173                         TPoint2D r(*this);
00174                         return r*=d;
00175                 }
00176 
00177                 inline TPoint2D operator/(double d) const       {
00178                         TPoint2D r(*this);
00179                         return r/=d;
00180                 }
00181                  /** Returns a human-readable textual representation of the object (eg: "[0.02 1.04]" )
00182                    * \sa fromString
00183                    */
00184                 void asString(std::string &s) const { s = mrpt::format("[%f %f]",x,y); }
00185                 inline std::string asString() const { std::string s; asString(s); return s; }
00186 
00187                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04]" )
00188                    * \sa asString
00189                    * \exception std::exception On invalid format
00190                    */
00191                  void fromString(const std::string &s);
00192                  static size_t size() { return 2; }
00193         };
00194 
00195         /**
00196           * Lightweight 2D pose. Allows coordinate access using [] operator.
00197           * \sa mrpt::poses::CPose2D
00198           */
00199         struct BASE_IMPEXP TPose2D      {
00200                 /**
00201                   * X coordinate.
00202                   */
00203                 double x;
00204                 /**
00205                   * Y coordinate.
00206                   */
00207                 double y;
00208                 /**
00209                   * Phi coordinate.
00210                   */
00211                 double phi;
00212                 /**
00213                   * Implicit constructor from TPoint2D. Zeroes the phi coordinate.
00214                   * \sa TPoint2D
00215                   */
00216                 TPose2D(const TPoint2D &p);
00217                 /**
00218                   * Constructor from TPoint3D, losing information. Zeroes the phi coordinate.
00219                   * \sa TPoint3D
00220                   */
00221                 explicit TPose2D(const TPoint3D &p);
00222                 /**
00223                   * Constructor from TPose3D, losing information. The phi corresponds to the original pose's yaw.
00224                   * \sa TPose3D
00225                   */
00226                 explicit TPose2D(const TPose3D &p);
00227                 /**
00228                   * Implicit constructor from heavyweight type.
00229                   * \sa mrpt::poses::CPose2D
00230                   */
00231                 TPose2D(const mrpt::poses::CPose2D &p);
00232                 /**
00233                   * Constructor from coordinates.
00234                   */
00235                 inline TPose2D(double xx,double yy,double pphi):x(xx),y(yy),phi(pphi)   {}
00236                 /**
00237                   * Default fast constructor. Initializes to garbage.
00238                   */
00239                 inline TPose2D()        {}
00240                 /**
00241                   * Unsafe coordinate access using operator[]. Intended for loops.
00242                   */
00243                 inline double &operator[](size_t i)     {
00244                         return (&x)[i];
00245                 }
00246                 /**
00247                   * Unsafe coordinate access using operator[]. Intended for loops.
00248                   */
00249                 inline const double &operator[](size_t i) const {
00250                         return (&x)[i];
00251                 }
00252                 /**
00253                   * Transformation into vector.
00254                   */
00255                 inline void getAsVector(vector_double &v) const {
00256                         v.resize(3);
00257                         v[0]=x; v[1]=y; v[2]=phi;
00258                 }
00259                  /** Returns a human-readable textual representation of the object (eg: "[x y yaw]", yaw in degrees)
00260                    * \sa fromString
00261                    */
00262                 void asString(std::string &s) const { s = mrpt::format("[%f %f %f]",x,y,RAD2DEG(phi)); }
00263                 inline std::string asString() const { std::string s; asString(s); return s; }
00264 
00265                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04 -45.0]" )
00266                    * \sa asString
00267                    * \exception std::exception On invalid format
00268                    */
00269                  void fromString(const std::string &s);
00270                  static size_t size() { return 3; }
00271         };
00272 
00273         /** Lightweight 3D point (float version).
00274           * \sa mrpt::poses::CPoint3D, mrpt::math::TPoint3D
00275           */
00276         struct BASE_IMPEXP TPoint3Df
00277         {
00278                 float x;
00279                 float y;
00280                 float z;
00281 
00282                 inline TPoint3Df() { }
00283                 inline TPoint3Df(const float xx,const float yy,const float zz) : x(xx), y(yy),z(zz) { }
00284                 inline TPoint3Df & operator +=(const TPoint3Df &p) { x+=p.x; y+=p.y; z+=p.z; return *this; }
00285                 inline TPoint3Df   operator *(const float s) { return TPoint3Df(x*s,y*s,z*s); }
00286         };
00287 
00288         /**
00289           * Lightweight 3D point. Allows coordinate access using [] operator.
00290           * \sa mrpt::poses::CPoint3D, mrpt::math::TPoint3Df
00291           */
00292         struct BASE_IMPEXP TPoint3D     {
00293                 double x; //!< X coordinate
00294                 double y; //!< Y coordinate
00295                 double z; //!< Z coordinate
00296 
00297                 /** Constructor from coordinates.  */
00298                 inline TPoint3D(double xx,double yy,double zz):x(xx),y(yy),z(zz)        {}
00299                 /** Default fast constructor. Initializes to garbage. */
00300                 inline TPoint3D()       {}
00301                 /** Explicit constructor from coordinates.  */
00302                 explicit inline TPoint3D(const TPoint3Df &p):x(p.x),y(p.y),z(p.z) {}
00303 
00304                 /** Implicit constructor from TPoint2D. Zeroes the z.
00305                   * \sa TPoint2D
00306                   */
00307                 TPoint3D(const TPoint2D &p);
00308                 /**
00309                   * Constructor from TPose2D, losing information. Zeroes the z.
00310                   * \sa TPose2D
00311                   */
00312                 explicit TPoint3D(const TPose2D &p);
00313                 /**
00314                   * Constructor from TPose3D, losing information.
00315                   * \sa TPose3D
00316                   */
00317                 explicit TPoint3D(const TPose3D &p);
00318                 /**
00319                   * Implicit constructor from heavyweight type.
00320                   * \sa mrpt::poses::CPoint3D
00321                   */
00322                 TPoint3D(const mrpt::poses::CPoint3D &p);
00323                 /**
00324                   * Constructor from heavyweight 3D pose.
00325                   * \sa mrpt::poses::CPose3D.
00326                   */
00327                 explicit TPoint3D(const mrpt::poses::CPose3D &p);
00328                 /**
00329                   * Unsafe coordinate access using operator[]. Intended for loops.
00330                   */
00331                 inline double &operator[](size_t i)     {
00332                         return (&x)[i];
00333                 }
00334                 /**
00335                   * Unsafe coordinate access using operator[]. Intended for loops.
00336                   */
00337                 inline const double &operator[](size_t i) const {
00338                         return (&x)[i];
00339                 }
00340                 /**
00341                   * Point-to-point distance.
00342                   */
00343                 inline double distanceTo(const TPoint3D &p) const       {
00344                         return sqrt(square(p.x-x)+square(p.y-y)+square(p.z-z));
00345                 }
00346                 /**
00347                   * Point-to-point distance, squared.
00348                   */
00349                 inline double sqrDistanceTo(const TPoint3D &p) const    {
00350                         return square(p.x-x)+square(p.y-y)+square(p.z-z);
00351                 }
00352                 /**
00353                   * Point norm.
00354                   */
00355                 inline double norm() const      {
00356                         return sqrt(square(x)+square(y)+square(z));
00357                 }
00358                 /**
00359                   * Point scale.
00360                   */
00361                 inline TPoint3D &operator*=(const double f) {
00362                         x*=f;y*=f;z*=f;
00363                         return *this;
00364                 }
00365                 /**
00366                   * Transformation into vector.
00367                   */
00368                 void getAsVector(vector_double &v) const {
00369                         v.resize(3);
00370                         v[0]=x; v[1]=y; v[2]=z;
00371                 }
00372                 /**
00373                   * Translation.
00374                   */
00375                 inline TPoint3D &operator+=(const TPoint3D &p)  {
00376                         x+=p.x;
00377                         y+=p.y;
00378                         z+=p.z;
00379                         return *this;
00380                 }
00381                 /**
00382                   * Difference between points.
00383                   */
00384                 inline TPoint3D &operator-=(const TPoint3D &p)  {
00385                         x-=p.x;
00386                         y-=p.y;
00387                         z-=p.z;
00388                         return *this;
00389                 }
00390                 /**
00391                   * Points addition.
00392                   */
00393                 inline TPoint3D operator+(const TPoint3D &p) const      {
00394                         return TPoint3D(x+p.x,y+p.y,z+p.z);
00395                 }
00396                 /**
00397                   * Points substraction.
00398                   */
00399                 inline TPoint3D operator-(const TPoint3D &p) const      {
00400                         return TPoint3D(x-p.x,y-p.y,z-p.z);
00401                 }
00402 
00403                 inline TPoint3D operator*(double d) const       {
00404                         return TPoint3D(x*d,y*d,z*d);
00405                 }
00406 
00407                 inline TPoint3D operator/(double d) const       {
00408                         return TPoint3D(x/d,y/d,z/d);
00409                 }
00410 
00411                 bool operator<(const TPoint3D &p) const;
00412 
00413                  /** Returns a human-readable textual representation of the object (eg: "[0.02 1.04 -0.8]" )
00414                    * \sa fromString
00415                    */
00416                 void asString(std::string &s) const { s = mrpt::format("[%f %f %f]",x,y,z); }
00417                 inline std::string asString() const { std::string s; asString(s); return s; }
00418 
00419                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04 -0.8]" )
00420                    * \sa asString
00421                    * \exception std::exception On invalid format
00422                    */
00423                  void fromString(const std::string &s);
00424                  static size_t size() { return 3; }
00425         };
00426 
00427         /**
00428           * Lightweight 3D pose (three spatial coordinates, plus three angular coordinates). Allows coordinate access using [] operator.
00429           * \sa mrpt::poses::CPose3D
00430           */
00431         struct BASE_IMPEXP TPose3D      {
00432                 /**
00433                   * X coordinate.
00434                   */
00435                 double x;
00436                 /**
00437                   * Y coordinate.
00438                   */
00439                 double y;
00440                 /**
00441                   * Z coordinate.
00442                   */
00443                 double z;
00444                 /**
00445                   * Yaw coordinate (rotation angle over Z axis).
00446                   */
00447                 double yaw;
00448                 /**
00449                   * Pitch coordinate (rotation angle over Y axis).
00450                   */
00451                 double pitch;
00452                 /**
00453                   * Roll coordinate (rotation angle over X coordinate).
00454                   */
00455                 double roll;
00456                 /**
00457                   * Implicit constructor from TPoint2D. Zeroes all the unprovided information.
00458                   * \sa TPoint2D
00459                   */
00460                 TPose3D(const TPoint2D &p);
00461                 /**
00462                   * Implicit constructor from TPose2D. Gets the yaw from the 2D pose's phi, zeroing all the unprovided information.
00463                   * \sa TPose2D
00464                   */
00465                 TPose3D(const TPose2D &p);
00466                 /**
00467                   * Implicit constructor from TPoint3D. Zeroes angular information.
00468                   * \sa TPoint3D
00469                   */
00470                 TPose3D(const TPoint3D &p);
00471                 /**
00472                   * Implicit constructor from heavyweight type.
00473                   * \sa mrpt::poses::CPose3D
00474                   */
00475                 TPose3D(const mrpt::poses::CPose3D &p);
00476                 /**
00477                   * Constructor from coordinates.
00478                   */
00479                 TPose3D(double _x,double _y,double _z,double _yaw,double _pitch,double _roll):x(_x),y(_y),z(_z),yaw(_yaw),pitch(_pitch),roll(_roll)     {}
00480                 /**
00481                   * Default fast constructor. Initializes to garbage.
00482                   */
00483                 inline TPose3D()        {}
00484                 /**
00485                   * Unsafe coordinate access using operator[]. Intended for loops.
00486                   */
00487                 inline double &operator[](size_t i)     {
00488                         return (&x)[i];
00489                 }
00490                 /**
00491                   * Unsafe coordinate access using operator[]. Intended for loops.
00492                   */
00493                 inline const double &operator[](size_t i) const {
00494                         return (&x)[i];
00495                 }
00496                 /**
00497                   * Pose's spatial coordinates norm.
00498                   */
00499                 double norm() const {
00500                         return sqrt(square(x)+square(y)+square(z));
00501                 }
00502                 /**
00503                   * Gets the pose as a vector of doubles.
00504                   */
00505                 void getAsVector(vector_double &v) const {
00506                         v.resize(6);
00507                         v[0]=x; v[1]=y; v[2]=z; v[3]=yaw; v[4]=pitch; v[5]=roll;
00508                 }
00509                  /** Returns a human-readable textual representation of the object (eg: "[x y z yaw pitch roll]", angles in degrees.)
00510                    * \sa fromString
00511                    */
00512                 void asString(std::string &s) const { s = mrpt::format("[%f %f %f %f %f %f]",x,y,z,RAD2DEG(yaw),RAD2DEG(pitch),RAD2DEG(roll)); }
00513                 inline std::string asString() const { std::string s; asString(s); return s; }
00514 
00515                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04 -0.8]" )
00516                    * \sa asString
00517                    * \exception std::exception On invalid format
00518                    */
00519                  void fromString(const std::string &s);
00520                  static size_t size() { return 6; }
00521         };
00522 
00523         /** Lightweight 3D pose (three spatial coordinates, plus a quaternion ). Allows coordinate access using [] operator.
00524           * \sa mrpt::poses::CPose3DQuat
00525           */
00526         struct BASE_IMPEXP TPose3DQuat  {
00527                 double x;       //!< Translation in x
00528                 double y;       //!< Translation in y
00529                 double z;       //!< Translation in z
00530                 double qr;  //!< Quaternion part, r
00531                 double qx;  //!< Quaternion part, x
00532                 double qy;  //!< Quaternion part, y
00533                 double qz;  //!< Quaternion part, z
00534 
00535                 /** Constructor from coordinates. */
00536                 inline TPose3DQuat(double _x,double _y,double _z,double _qr,double _qx, double _qy, double _qz):x(_x),y(_y),z(_z),qr(_qr),qx(_qx),qy(_qy),qz(_qz) {  }
00537                 /** Default fast constructor. Initializes to garbage. */
00538                 inline TPose3DQuat()    {}
00539                 /** Constructor from a CPose3DQuat */
00540                 TPose3DQuat(const mrpt::poses::CPose3DQuat &p);
00541 
00542                 /** Unsafe coordinate access using operator[]. Intended for loops. */
00543                 inline double &operator[](size_t i)     {
00544                         return (&x)[i];
00545                 }
00546                 /** Unsafe coordinate access using operator[]. Intended for loops. */
00547                 inline const double &operator[](size_t i) const {
00548                         return (&x)[i];
00549                 }
00550                 /** Pose's spatial coordinates norm. */
00551                 double norm() const {
00552                         return sqrt(square(x)+square(y)+square(z));
00553                 }
00554                 /** Gets the pose as a vector of doubles. */
00555                 void getAsVector(vector_double &v) const {
00556                         v.resize(7);
00557                         for (size_t i=0;i<7;i++) v[i]=(*this)[i];
00558                 }
00559                  /** Returns a human-readable textual representation of the object as "[x y z qr qx qy qz]"
00560                    * \sa fromString
00561                    */
00562                 void asString(std::string &s) const { s = mrpt::format("[%f %f %f %f %f %f %f]",x,y,z,qr,qx,qy,qz); }
00563                 inline std::string asString() const { std::string s; asString(s); return s; }
00564 
00565                  /** Set the current object value from a string generated by 'asString' (eg: "[0.02 1.04 -0.8 1.0 0.0 0.0 0.0]" )
00566                    * \sa asString
00567                    * \exception std::exception On invalid format
00568                    */
00569                  void fromString(const std::string &s);
00570                  static size_t size() { return 7; }
00571         };
00572 #pragma pack(pop)
00573 
00574         // Text streaming functions:
00575         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPoint2D & p);
00576         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPoint3D & p);
00577         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose2D & p);
00578         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose3D & p);
00579         std::ostream BASE_IMPEXP & operator << (std::ostream& o, const TPose3DQuat & p);
00580 
00581 
00582         /**
00583           * Unary minus operator for 3D points.
00584           */
00585         inline TPoint3D operator-(const TPoint3D &p1)   {
00586                 return TPoint3D(-p1.x,-p1.y,-p1.z);
00587         }
00588         /**
00589           * Exact comparison between 2D points.
00590           */
00591         inline bool operator==(const TPoint2D &p1,const TPoint2D &p2)   {
00592                 return (p1.x==p2.x)&&(p1.y==p2.y);
00593         }
00594         /**
00595           * Exact comparison between 2D points.
00596           */
00597         inline bool operator!=(const TPoint2D &p1,const TPoint2D &p2)   {
00598                 return (p1.x!=p2.x)||(p1.y!=p2.y);
00599         }
00600         /**
00601           * Exact comparison between 3D points.
00602           */
00603         inline bool operator==(const TPoint3D &p1,const TPoint3D &p2)   {
00604                 return (p1.x==p2.x)&&(p1.y==p2.y)&&(p1.z==p2.z);
00605         }
00606         /**
00607           * Exact comparison between 3D points.
00608           */
00609         inline bool operator!=(const TPoint3D &p1,const TPoint3D &p2)   {
00610                 return (p1.x!=p2.x)||(p1.y!=p2.y)||(p1.z!=p2.z);
00611         }
00612         /**
00613           * Exact comparison between 2D poses, taking possible cycles into account.
00614           */
00615         inline bool operator==(const TPose2D &p1,const TPose2D &p2)     {
00616                 return (p1.x==p2.x)&&(p1.y==p2.y)&&(mrpt::math::wrapTo2Pi(p1.phi)==mrpt::math::wrapTo2Pi(p2.phi));
00617         }
00618         /**
00619           * Exact comparison between 2D poses, taking possible cycles into account.
00620           */
00621         inline bool operator!=(const TPose2D &p1,const TPose2D &p2)     {
00622                 return (p1.x!=p2.x)||(p1.y!=p2.y)||(mrpt::math::wrapTo2Pi(p1.phi)!=mrpt::math::wrapTo2Pi(p2.phi));
00623         }
00624         /**
00625           * Exact comparison between 3D poses, taking possible cycles into account.
00626           */
00627         inline bool operator==(const TPose3D &p1,const TPose3D &p2)     {
00628                 return (p1.x==p2.x)&&(p1.y==p2.y)&&(p1.z==p2.z)&&(mrpt::math::wrapTo2Pi(p1.yaw)==mrpt::math::wrapTo2Pi(p2.yaw))&&(mrpt::math::wrapTo2Pi(p1.pitch)==mrpt::math::wrapTo2Pi(p2.pitch))&&(mrpt::math::wrapTo2Pi(p1.roll)==mrpt::math::wrapTo2Pi(p2.roll));
00629         }
00630         /**
00631           * Exact comparison between 3D poses, taking possible cycles into account.
00632           */
00633         inline bool operator!=(const TPose3D &p1,const TPose3D &p2)     {
00634                 return (p1.x!=p2.x)||(p1.y!=p2.y)||(p1.z!=p2.z)||(mrpt::math::wrapTo2Pi(p1.yaw)!=mrpt::math::wrapTo2Pi(p2.yaw))||(mrpt::math::wrapTo2Pi(p1.pitch)!=mrpt::math::wrapTo2Pi(p2.pitch))||(mrpt::math::wrapTo2Pi(p1.roll)!=mrpt::math::wrapTo2Pi(p2.roll));
00635         }
00636         //Forward declarations
00637         struct BASE_IMPEXP TSegment3D;
00638         struct BASE_IMPEXP TLine3D;
00639         class BASE_IMPEXP TPolygon3D;
00640         struct BASE_IMPEXP TObject3D;
00641 
00642         //Pragma defined to ensure no structure packing
00643 #pragma pack(push,1)
00644         /**
00645           * 2D segment, consisting of two points.
00646           * \sa TSegment3D,TLine2D,TPolygon2D,TPoint2D
00647           */
00648         struct BASE_IMPEXP TSegment2D   {
00649         public:
00650                 /**
00651                   * Origin point.
00652                   */
00653                 TPoint2D point1;
00654                 /**
00655                   * Destiny point.
00656                   */
00657                 TPoint2D point2;
00658                 /**
00659                   * Segment length.
00660                   */
00661                 double length() const;
00662                 /**
00663                   * Distance to point.
00664                   */
00665                 double distance(const TPoint2D &point) const;
00666                 /**
00667                   * Distance with sign to point (sign indicates which side the point is).
00668                   */
00669                 double signedDistance(const TPoint2D &point) const;
00670                 /**
00671                   * Check whether a point is inside a segment.
00672                   */
00673                 bool contains(const TPoint2D &point) const;
00674                 /**
00675                   * Unsafe point access using [] operator, intended for loops.
00676                   */
00677                 inline TPoint2D &operator[](size_t i)   {
00678                         return (&point1)[i];
00679                 }
00680                 /**
00681                   * Unsafe point access using [] operator, intended for loops.
00682                   */
00683                 inline const TPoint2D &operator[](size_t i) const       {
00684                         return (&point1)[i];
00685                 }
00686                 /**
00687                   * Project into 3D space, setting the z to 0.
00688                   */
00689                 void generate3DObject(TSegment3D &s) const;
00690                 /**
00691                   * Segment's central point.
00692                   */
00693                 inline void getCenter(TPoint2D &p) const        {
00694                         p.x=(point1.x+point2.x)/2;
00695                         p.y=(point1.y+point2.y)/2;
00696                 }
00697                 /**
00698                   * Constructor from both points.
00699                   */
00700                 TSegment2D(const TPoint2D &p1,const TPoint2D &p2):point1(p1),point2(p2) {}
00701                 /**
00702                   * Fast default constructor. Initializes to garbage.
00703                   */
00704                 TSegment2D()    {}
00705                 /**
00706                   * Explicit constructor from 3D object, discarding the z.
00707                   */
00708                 explicit TSegment2D(const TSegment3D &s);
00709 
00710                 bool operator<(const TSegment2D &s) const;
00711         };
00712         /**
00713           * 3D segment, consisting of two points.
00714           * \sa TSegment2D,TLine3D,TPlane,TPolygon3D,TPoint3D
00715           */
00716         struct BASE_IMPEXP TSegment3D   {
00717         public:
00718                 /**
00719                   * Origin point.
00720                   */
00721                 TPoint3D point1;
00722                 /**
00723                   * Destiny point.
00724                   */
00725                 TPoint3D point2;
00726                 /**
00727                   * Segment length.
00728                   */
00729                 double length() const;
00730                 /**
00731                   * Distance to point.
00732                   */
00733                 double distance(const TPoint3D &point) const;
00734                 /**
00735                   * Check whether a point is inside the segment.
00736                   */
00737                 bool contains(const TPoint3D &point) const;
00738                 /**
00739                   * Unsafe point access using [] operator, intended for loops.
00740                   */
00741                 inline TPoint3D &operator[](size_t i)   {
00742                         return (&point1)[i];
00743                 }
00744                 /**
00745                   * Unsafe point access using [] operator, intended for loops.
00746                   */
00747                 inline const TPoint3D &operator[](size_t i) const       {
00748                         return (&point1)[i];
00749                 }
00750                 /**
00751                   * Projection into 2D space, discarding the z.
00752                   */
00753                 inline void generate2DObject(TSegment2D &s) const       {
00754                         s=TSegment2D(*this);
00755                 }
00756                 /**
00757                   * Segment's central point.
00758                   */
00759                 inline void getCenter(TPoint3D &p) const        {
00760                         p.x=(point1.x+point2.x)/2;
00761                         p.y=(point1.y+point2.y)/2;
00762                         p.z=(point1.z+point2.z)/2;
00763                 }
00764                 /**
00765                   * Constructor from both points.
00766                   */
00767                 TSegment3D(const TPoint3D &p1,const TPoint3D &p2):point1(p1),point2(p2) {}
00768                 /**
00769                   * Fast default constructor. Initializes to garbage.
00770                   */
00771                 TSegment3D()    {}
00772                 /**
00773                   * Constructor from 2D object. Sets the z to zero.
00774                   */
00775                 TSegment3D(const TSegment2D &s):point1(s.point1),point2(s.point2)       {}
00776 
00777                 bool operator<(const TSegment3D &s) const;
00778         };
00779 #pragma pack(pop)
00780 
00781         inline bool operator==(const TSegment2D &s1,const TSegment2D &s2)       {
00782                 return (s1.point1==s2.point1)&&(s1.point2==s2.point2);
00783         }
00784 
00785         inline bool operator!=(const TSegment2D &s1,const TSegment2D &s2)       {
00786                 return (s1.point1!=s1.point1)||(s1.point2!=s2.point2);
00787         }
00788 
00789         inline bool operator==(const TSegment3D &s1,const TSegment3D &s2)       {
00790                 return (s1.point1==s2.point1)&&(s1.point2==s2.point2);
00791         }
00792 
00793         inline bool operator!=(const TSegment3D &s1,const TSegment3D &s2)       {
00794                 return (s1.point1!=s1.point1)||(s1.point2!=s2.point2);
00795         }
00796 
00797         /**
00798           * 2D line without bounds, represented by its equation \f$Ax+By+C=0\f$.
00799           * \sa TLine3D,TSegment2D,TPolygon2D,TPoint2D
00800           */
00801         struct BASE_IMPEXP TLine2D      {
00802         public:
00803                 /**
00804                   * Line coefficients, stored as an array: \f$\left[A,B,C\right]\f$.
00805                   */
00806                 double coefs[3];
00807                 /**
00808                   * Evaluate point in the line's equation.
00809                   */
00810                 double evaluatePoint(const TPoint2D &point) const;
00811                 /**
00812                   * Check whether a point is inside the line.
00813                   */
00814                 bool contains(const TPoint2D &point) const;
00815                 /**
00816                   * Distance from a given point.
00817                   */
00818                 double distance(const TPoint2D &point) const;
00819                 /**
00820                   * Distance with sign from a given point (sign indicates side).
00821                   */
00822                 double signedDistance(const TPoint2D &point) const;
00823                 /**
00824                   * Get line's normal vector.
00825                   */
00826                 void getNormalVector(double (&vector)[2]) const;
00827                 /**
00828                   * Unitarize line's normal vector.
00829                   */
00830                 void unitarize();
00831                 /**
00832                   * Get line's normal vector after unitarizing line.
00833                   */
00834                 inline void getUnitaryNormalVector(double (&vector)[2]) {
00835                         unitarize();
00836                         getNormalVector(vector);
00837                 }
00838                 /**
00839                   * Get line's director vector.
00840                   */
00841                 void getDirectorVector(double (&vector)[2]) const;
00842                 /**
00843                   * Unitarize line and then get director vector.
00844                   */
00845                 inline void getUnitaryDirectorVector(double (&vector)[2])       {
00846                         unitarize();
00847                         getDirectorVector(vector);
00848                 }
00849                 /**
00850                   * Project into 3D space, setting the z to 0.
00851                   */
00852                 void generate3DObject(TLine3D &l) const;
00853                 /**
00854                   * Get a pose2D whose X axis corresponds to the line.
00855                   * \sa mrpt::poses::CPose2D.
00856                   */
00857                 void getAsPose2D(mrpt::poses::CPose2D &outPose) const;
00858                 /**
00859                   * Get a pose2D whose X axis corresponds to the line, forcing the base point to one given.
00860                   * \throw logic_error if the point is not inside the line.
00861                   * \sa mrpt::poses::CPose2D.
00862                   */
00863                 void getAsPose2DForcingOrigin(const TPoint2D &origin,mrpt::poses::CPose2D &outPose) const;
00864                 /**
00865                   * Constructor from two points, through which the line will pass.
00866                   * \throw logic_error if both points are the same
00867                   */
00868                 TLine2D(const TPoint2D &p1,const TPoint2D &p2) throw(std::logic_error);
00869                 /**
00870                   * Constructor from a segment.
00871                   */
00872                 explicit TLine2D(const TSegment2D &s);
00873                 /**
00874                   * Fast default constructor. Initializes to garbage.
00875                   */
00876                 TLine2D()       {}
00877                 /**
00878                   * Constructor from line's coefficients.
00879                   */
00880                 inline TLine2D(double A,double B,double C)      {
00881                         coefs[0]=A;
00882                         coefs[1]=B;
00883                         coefs[2]=C;
00884                 }
00885                 /**
00886                   * Construction from 3D object, discarding the Z.
00887                   * \throw std::logic_error if the line is normal to the XY plane.
00888                   */
00889                 explicit TLine2D(const TLine3D &l);
00890         };
00891 
00892         /**
00893           * 3D line, represented by a base point and a director vector.
00894           * \sa TLine2D,TSegment3D,TPlane,TPolygon3D,TPoint3D
00895           */
00896         struct BASE_IMPEXP TLine3D      {
00897         public:
00898                 /**
00899                   * Base point.
00900                   */
00901                 TPoint3D pBase;
00902                 /**
00903                   * Director vector.
00904                   */
00905                 double director[3];
00906                 /**
00907                   * Check whether a point is inside the line.
00908                   */
00909                 bool contains(const TPoint3D &point) const;
00910                 /**
00911                   * Distance between the line and a point.
00912                   */
00913                 double distance(const TPoint3D &point) const;
00914                 /**
00915                   * Unitarize director vector.
00916                   */
00917                 void unitarize();
00918                 /**
00919                   * Get director vector.
00920                   */
00921                 inline void getDirectorVector(double (&vector)[3]) const        {
00922                         for (size_t i=0;i<3;i++) vector[i]=director[i];
00923                 }
00924                 /**
00925                   * Unitarize and then get director vector.
00926                   */
00927                 inline void getUnitaryDirectorVector(double (&vector)[3])       {
00928                         unitarize();
00929                         getDirectorVector(vector);
00930                 }
00931                 /**
00932                   * Project into 2D space, discarding the Z coordinate.
00933                   * \throw std::logic_error if the line's director vector is orthogonal to the XY plane.
00934                   */
00935                 inline void generate2DObject(TLine2D &l) const  {
00936                         l=TLine2D(*this);
00937                 }
00938                 /**
00939                   * Constructor from two points, through which the line will pass.
00940                   * \throw std::logic_error if both points are the same.
00941                   */
00942                 TLine3D(const TPoint3D &p1,const TPoint3D &p2) throw(std::logic_error);
00943                 /**
00944                   * Constructor from 3D segment.
00945                   */
00946                 explicit TLine3D(const TSegment3D &s);
00947                 /**
00948                   * Fast default constructor. Initializes to garbage.
00949                   */
00950                 TLine3D()       {}
00951                 /**
00952                   * Implicit constructor from 2D object. Zeroes the z.
00953                   */
00954                 TLine3D(const TLine2D &l);
00955         };
00956 
00957         /**
00958           * 3D Plane, represented by its equation \f$Ax+By+Cz+D=0\f$
00959           * \sa TSegment3D,TLine3D,TPolygon3D,TPoint3D
00960           */
00961         struct BASE_IMPEXP TPlane       {
00962         public:
00963                 /**
00964                   * Plane coefficients, stored as an array: \f$\left[A,B,C,D\right]\f$
00965                   */
00966                 double coefs[4];
00967                 /**
00968                   * Evaluate a point in the plane's equation.
00969                   */
00970                 double evaluatePoint(const TPoint3D &point) const;
00971                 /**
00972                   * Check whether a point is contained into the plane.
00973                   */
00974                 bool contains(const TPoint3D &point) const;
00975                 /**
00976                   * Check whether a segment is fully contained into the plane.
00977                   */
00978                 inline bool contains(const TSegment3D &segment) const   {
00979                         return contains(segment.point1)&&contains(segment.point2);
00980                 }
00981                 /**
00982                   * Check whether a line is fully contained into the plane.
00983                   */
00984                 bool contains(const TLine3D &line) const;
00985                 /**
00986                   * Distance to 3D point.
00987                   */
00988                 double distance(const TPoint3D &point) const;
00989                 /**
00990                   * Distance to 3D line. Will be zero if the line is not parallel to the plane.
00991                   */
00992                 double distance(const TLine3D &line) const;
00993                 /**
00994                   * Get plane's normal vector.
00995                   */
00996                 void getNormalVector(double (&vec)[3]) const;
00997                 /**
00998                   * Unitarize normal vector.
00999                   */
01000                 void unitarize();
01001                 /**
01002                   * Unitarize, then get normal vector.
01003                   */
01004                 inline void getUnitaryNormalVector(double (&vec)[3])    {
01005                         unitarize();
01006                         getNormalVector(vec);
01007                 }
01008                 /**
01009                   * Gets a pose whose XY plane corresponds to this plane.
01010                   */
01011                 void getAsPose3D(mrpt::poses::CPose3D &outPose);
01012                 /**
01013                   * Gets a pose whose XY plane corresponds to this plane.
01014                   */
01015                 inline void getAsPose3D(mrpt::poses::CPose3D &outPose) const    {
01016                         TPlane p=*this;
01017                         p.getAsPose3D(outPose);
01018                 }
01019                 /**
01020                   * Gets a pose whose XY plane corresponds to this, forcing an exact point as its spatial coordinates.
01021                   * \throw std::logic_error if the point is not inside the plane.
01022                   */
01023                 void getAsPose3DForcingOrigin(const TPoint3D &newOrigin,mrpt::poses::CPose3D &pose);
01024                 /**
01025                   * Gets a pose whose XY plane corresponds to this, forcing an exact point as its spatial coordinates.
01026                   * \throw std::logic_error if the point is not inside the plane.
01027                   */
01028                 inline void getAsPose3DForcingOrigin(const TPoint3D &newOrigin,mrpt::poses::CPose3D &pose) const        {
01029                         TPlane p=*this;
01030                         p.getAsPose3DForcingOrigin(newOrigin,pose);
01031                 }
01032                 /**
01033                   * Gets a plane which contains these three points.
01034                   * \throw std::logic_error if the points are linearly dependants.
01035                   */
01036                 TPlane(const TPoint3D &p1,const TPoint3D &p2,const TPoint3D &p3) throw(std::logic_error);
01037                 /**
01038                   * Gets a plane which contains this point and this line.
01039                   * \throw std::logic_error if the point is inside the line.
01040                   */
01041                 TPlane(const TPoint3D &p1,const TLine3D &r2) throw(std::logic_error);
01042                 /**
01043                   * Gets a plane which contains the two lines.
01044                   * \throw std::logic_error if the lines do not cross.
01045                   */
01046                 TPlane(const TLine3D &r1,const TLine3D &r2) throw(std::logic_error);
01047                 /**
01048                   * Fast default constructor. Initializes to garbage.
01049                   */
01050                 TPlane()        {}
01051                 /**
01052                   * Constructor from plane coefficients.
01053                   */
01054                 inline TPlane(double A,double B,double C,double D)      {
01055                         coefs[0]=A;
01056                         coefs[1]=B;
01057                         coefs[2]=C;
01058                         coefs[3]=D;
01059                 }
01060                 /**
01061                   * Constructor from an array of coefficients.
01062                   */
01063                 inline TPlane(const double (&vec)[4])   {
01064                         for (size_t i=0;i<4;i++) coefs[i]=vec[i];
01065                 }
01066         };
01067 
01068         typedef TPlane TPlane3D;
01069 
01070         /**
01071           * 2D polygon, inheriting from std::vector<TPoint2D>.
01072           * \sa TPolygon3D,TSegment2D,TLine2D,TPoint2D, CPolygon
01073           */
01074         class BASE_IMPEXP TPolygon2D:public std::vector<TPoint2D>       {
01075         public:
01076                 /**
01077                   * Distance to a point.
01078                   */
01079                 double distance(const TPoint2D &point) const;
01080                 /**
01081                   * Check whether a point is inside the polygon.
01082                   */
01083                 bool contains(const TPoint2D &point) const;
01084                 /**
01085                   * Gets as set of segments, instead of points.
01086                   */
01087                 void getAsSegmentList(std::vector<TSegment2D> &v) const;
01088                 /**
01089                   * Projects into 3D space, zeroing the z.
01090                   */
01091                 void generate3DObject(TPolygon3D &p) const;
01092                 /**
01093                   * Polygon's central point.
01094                   */
01095                 void getCenter(TPoint2D &p) const;
01096                 /**
01097                   * Checks whether is convex.
01098                   */
01099                 bool isConvex() const;
01100                 /**
01101                   * Erase repeated vertices.
01102                   * \sa removeRedundantVertices
01103                   */
01104                 void removeRepeatedVertices();
01105                 /**
01106                   * Erase every redundant vertex from the polygon, saving space.
01107                   * \sa removeRepeatedVertices
01108                   */
01109                 void removeRedundantVertices();
01110                 /**
01111                   * Gets plot data, ready to use on a 2D plot.
01112                   * \sa mrpt::gui::CDisplayWindowPlots
01113                   */
01114                 void getPlotData(std::vector<double> &x,std::vector<double> &y) const;
01115                 /**
01116                   * Default constructor.
01117                   */
01118                 TPolygon2D():std::vector<TPoint2D>()    {}
01119                 /**
01120                   * Constructor for a given number of vertices, intializing them as garbage.
01121                   */
01122                 explicit TPolygon2D(size_t N):std::vector<TPoint2D>(N)  {}
01123                 /**
01124                   * Implicit constructor from a vector of 2D points.
01125                   */
01126                 TPolygon2D(const std::vector<TPoint2D> &v):std::vector<TPoint2D>(v)     {}
01127                 /**
01128                   * Constructor from a 3D object.
01129                   */
01130                 explicit TPolygon2D(const TPolygon3D &p);
01131                 /**
01132                   * Static method to create a regular polygon, given its size and radius.
01133                   * \throw std::logic_error if radius is near zero or the number of edges is less than three.
01134                   */
01135                 static void createRegularPolygon(size_t numEdges,double radius,TPolygon2D &poly);
01136                 /**
01137                   * Static method to create a regular polygon from its size and radius. The center will correspond to the given pose.
01138                   * \throw std::logic_error if radius is near zero or the number of edges is less than three.
01139                   */
01140                 static inline void createRegularPolygon(size_t numEdges,double radius,TPolygon2D &poly,const mrpt::poses::CPose2D &pose);
01141         };
01142 
01143         /**
01144           * 3D polygon, inheriting from std::vector<TPoint3D>
01145           * \sa TPolygon2D,TSegment3D,TLine3D,TPlane,TPoint3D
01146           */
01147         class BASE_IMPEXP TPolygon3D:public std::vector<TPoint3D>       {
01148         public:
01149                 /**
01150                   * Distance to point.
01151                   */
01152                 double distance(const TPoint3D &point) const;
01153                 /**
01154                   * Check whether a point is inside the polygon.
01155                   */
01156                 bool contains(const TPoint3D &point) const;
01157                 /**
01158                   * Gets as set of segments, instead of set of points.
01159                   */
01160                 void getAsSegmentList(std::vector<TSegment3D> &v) const;
01161                 /**
01162                   * Gets a plane which contains the polygon. Returns false if the polygon is skew and cannot be fit inside a plane.
01163                   */
01164                 bool getPlane(TPlane &p) const;
01165                 /**
01166                   * Gets the best fitting plane, disregarding whether the polygon actually fits inside or not.
01167                   * \sa getBestFittingPlane
01168                   */
01169                 void getBestFittingPlane(TPlane &p) const;
01170                 /**
01171                   * Projects into a 2D space, discarding the z.
01172                   * \get getPlane,isSkew
01173                   */
01174                 inline void generate2DObject(TPolygon2D &p) const       {
01175                         p=TPolygon2D(*this);
01176                 }
01177                 /**
01178                   * Get polygon's central point.
01179                   */
01180                 void getCenter(TPoint3D &p) const;
01181                 /**
01182                   * Check whether the polygon is skew. Returns true if there doesn't exist a plane in which the polygon can fit.
01183                   * \sa getBestFittingPlane
01184                   */
01185                 bool isSkew() const;
01186                 /**
01187                   * Remove polygon's repeated vertices.
01188                   */
01189                 void removeRepeatedVertices();
01190                 /**
01191                   * Erase every redundant vertex, thus saving space.
01192                   */
01193                 void removeRedundantVertices();
01194                 /**
01195                   * Default constructor. Creates a polygon with no vertices.
01196                   */
01197                 TPolygon3D():std::vector<TPoint3D>()    {}
01198                 /**
01199                   * Constructor for a given size. Creates a polygon with a fixed number of vertices, which are initialized to garbage.
01200                   */
01201                 explicit TPolygon3D(size_t N):std::vector<TPoint3D>(N)  {}
01202                 /**
01203                   * Implicit constructor from a 3D points vector.
01204                   */
01205                 TPolygon3D(const std::vector<TPoint3D> &v):std::vector<TPoint3D>(v)     {}
01206                 /**
01207                   * Constructor from a 2D object. Zeroes the z.
01208                   */
01209                 TPolygon3D(const TPolygon2D &p);
01210                 /**
01211                   * Static method to create a regular polygon, given its size and radius.
01212                   * \throw std::logic_error if number of edges is less than three, or radius is near zero.
01213                   */
01214                 static void createRegularPolygon(size_t numEdges,double radius,TPolygon3D &poly);
01215                 /**
01216                   * Static method to create a regular polygon, given its size and radius. The center will be located on the given pose.
01217                   * \throw std::logic_error if number of edges is less than three, or radius is near zero.
01218                   */
01219                 static inline void createRegularPolygon(size_t numEdges,double radius,TPolygon3D &poly,const mrpt::poses::CPose3D &pose);
01220         };
01221 
01222         /**
01223           * Object type identifier for TPoint2D or TPoint3D.
01224           * \sa TObject2D,TObject3D
01225           */
01226         const unsigned char GEOMETRIC_TYPE_POINT=0;
01227         /**
01228           * Object type identifier for TSegment2D or TSegment3D.
01229           * \sa TObject2D,TObject3D
01230           */
01231         const unsigned char GEOMETRIC_TYPE_SEGMENT=1;
01232         /**
01233           * Object type identifier for TLine2D or TLine3D.
01234           * \sa TObject2D,TObject3D
01235           */
01236         const unsigned char GEOMETRIC_TYPE_LINE=2;
01237         /**
01238           * Object type identifier for TPolygon2D or TPolygon3D.
01239           * \sa TObject2D,TObject3D
01240           */
01241         const unsigned char GEOMETRIC_TYPE_POLYGON=3;
01242         /**
01243           * Object type identifier for TPlane.
01244           * \sa TObject3D
01245           */
01246         const unsigned char GEOMETRIC_TYPE_PLANE=4;
01247         /**
01248           * Object type identifier for empty TObject2D or TObject3D.
01249           * \sa TObject2D,TObject3D
01250           */
01251         const unsigned char GEOMETRIC_TYPE_UNDEFINED=255;
01252 
01253         /**
01254           * Standard type for storing any lightweight 2D type. Do not inherit from this class.
01255           * \sa TPoint2D,TSegment2D,TLine2D,TPolygon2D
01256           */
01257 #ifdef TOBJECTS_USE_UNIONS
01258         struct BASE_IMPEXP TObject2D    {
01259         private:
01260                 /**
01261                   * Object type identifier.
01262                   */
01263                 unsigned char type;
01264                 /**
01265                   * Union type storing pointers to every allowed type.
01266                   */
01267                 union   {
01268                         TPoint2D *point;
01269                         TSegment2D *segment;
01270                         TLine2D *line;
01271                         TPolygon2D *polygon;
01272                 }       data;
01273                 /**
01274                   * Destroys the object, releasing the pointer to the content (if any).
01275                   */
01276                 void destroy()  {
01277                         switch(type)    {
01278                                 case GEOMETRIC_TYPE_POINT:
01279                                         delete data.point;
01280                                         break;
01281                                 case GEOMETRIC_TYPE_SEGMENT:
01282                                         delete data.segment;
01283                                         break;
01284                                 case GEOMETRIC_TYPE_LINE:
01285                                         delete data.line;
01286                                         break;
01287                                 case GEOMETRIC_TYPE_POLYGON:
01288                                         delete data.polygon;
01289                                         break;
01290                         }
01291                         type=GEOMETRIC_TYPE_UNDEFINED;
01292                 }
01293         public:
01294                 /**
01295                   * Implicit constructor from point.
01296                   */
01297                 TObject2D(const TPoint2D &p):type(GEOMETRIC_TYPE_POINT) {
01298                         data.point=new TPoint2D(p);
01299                 }
01300                 /**
01301                   * Implicit constructor from segment.
01302                   */
01303                 TObject2D(const TSegment2D &s):type(GEOMETRIC_TYPE_SEGMENT)     {
01304                         data.segment=new TSegment2D(s);
01305                 }
01306                 /**
01307                   * Implicit constructor from line.
01308                   */
01309                 TObject2D(const TLine2D &r):type(GEOMETRIC_TYPE_LINE)   {
01310                         data.line=new TLine2D(r);
01311                 }
01312                 /**
01313                   * Implicit constructor from polygon.
01314                   */
01315                 TObject2D(const TPolygon2D &p):type(GEOMETRIC_TYPE_POLYGON)     {
01316                         data.polygon=new TPolygon2D(p);
01317                 }
01318                 /**
01319                   * Implicit constructor from polygon.
01320                   */
01321                 TObject2D():type(GEOMETRIC_TYPE_UNDEFINED)      {}
01322                 /**
01323                   * Object destruction.
01324                   */
01325                 ~TObject2D()    {
01326                         destroy();
01327                 }
01328                 /**
01329                   * Checks whether content is a point.
01330                   */
01331                 inline bool isPoint() const     {
01332                         return type==GEOMETRIC_TYPE_POINT;
01333                 }
01334                 /**
01335                   * Checks whether content is a segment.
01336                   */
01337                 inline bool isSegment() const   {
01338                         return type==GEOMETRIC_TYPE_SEGMENT;
01339                 }
01340                 /**
01341                   * Checks whether content is a line.
01342                   */
01343                 inline bool isLine() const      {
01344                         return type==GEOMETRIC_TYPE_LINE;
01345                 }
01346                 /**
01347                   * Checks whether content is a polygon.
01348                   */
01349                 inline bool isPolygon() const   {
01350                         return type==GEOMETRIC_TYPE_POLYGON;
01351                 }
01352                 /**
01353                   * Gets content type.
01354                   */
01355                 inline unsigned char getType() const    {
01356                         return type;
01357                 }
01358                 /**
01359                   * Gets the content as a point, returning false if the type is inadequate.
01360                   */
01361                 inline bool getPoint(TPoint2D &p) const {
01362                         if (isPoint())  {
01363                                 p=*(data.point);
01364                                 return true;
01365                         }       else return false;
01366                 }
01367                 /**
01368                   * Gets the content as a segment, returning false if the type is inadequate.
01369                   */
01370                 inline bool getSegment(TSegment2D &s) const     {
01371                         if (isSegment())        {
01372                                 s=*(data.segment);
01373                                 return true;
01374                         }       else return false;
01375                 }
01376                 /**
01377                   * Gets the content as a line, returning false if the type is inadequate.
01378                   */
01379                 inline bool getLine(TLine2D &r) const   {
01380                         if (isLine())   {
01381                                 r=*(data.line);
01382                                 return true;
01383                         }       else return false;
01384                 }
01385                 /**
01386                   * Gets the content as a polygon, returning false if the type is inadequate.
01387                   */
01388                 inline bool getPolygon(TPolygon2D &p) const     {
01389                         if (isPolygon())        {
01390                                 p=*(data.polygon);
01391                                 return true;
01392                         }       else return false;
01393                 }
01394                 /**
01395                   * Assign another TObject2D. Pointers are not shared.
01396                   */
01397                 void operator=(const TObject2D &obj)    {
01398                         if (this==&obj) return;
01399                         destroy();
01400                         switch (type=obj.type)  {
01401                                 case GEOMETRIC_TYPE_POINT:
01402                                         data.point=new TPoint2D(*(obj.data.point));
01403                                         break;
01404                                 case GEOMETRIC_TYPE_SEGMENT:
01405                                         data.segment=new TSegment2D(*(obj.data.segment));
01406                                         break;
01407                                 case GEOMETRIC_TYPE_LINE:
01408                                         data.line=new TLine2D(*(obj.data.line));
01409                                         break;
01410                                 case GEOMETRIC_TYPE_POLYGON:
01411                                         data.polygon=new TPolygon2D(*(obj.data.polygon));
01412                                         break;
01413                         }
01414                 }
01415                 /**
01416                   * Assign a point to this object.
01417                   */
01418                 inline void operator=(const TPoint2D &p)        {
01419                         destroy();
01420                         type=GEOMETRIC_TYPE_POINT;
01421                         data.point=new TPoint2D(p);
01422                 }
01423                 /**
01424                   * Assign a segment to this object.
01425                   */
01426                 inline void operator=(const TSegment2D &s)      {
01427                         destroy();
01428                         type=GEOMETRIC_TYPE_SEGMENT;
01429                         data.segment=new TSegment2D(s);
01430                 }
01431                 /**
01432                   * Assign a line to this object.
01433                   */
01434                 inline void operator=(const TLine2D &l) {
01435                         destroy();
01436                         type=GEOMETRIC_TYPE_LINE;
01437                         data.line=new TLine2D(l);
01438                 }
01439                 /**
01440                   * Assign a polygon to this object.
01441                   */
01442                 inline void operator=(const TPolygon2D &p)      {
01443                         destroy();
01444                         type=GEOMETRIC_TYPE_POLYGON;
01445                         data.polygon=new TPolygon2D(p);
01446                 }
01447                 /**
01448                   * Project into 3D space.
01449                   */
01450                 void generate3DObject(TObject3D &obj) const;
01451                 /**
01452                   * Constructor from another TObject2D.
01453                   */
01454                 TObject2D(const TObject2D &obj):type(GEOMETRIC_TYPE_UNDEFINED)  {
01455                         operator=(obj);
01456                 }
01457                 /**
01458                   * Static method to retrieve all the points in a vector of TObject2D.
01459                   */
01460                 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts);
01461                 /**
01462                   * Static method to retrieve all the segments in a vector of TObject2D.
01463                   */
01464                 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms);
01465                 /**
01466                   * Static method to retrieve all the lines in a vector of TObject2D.
01467                   */
01468                 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins);
01469                 /**
01470                   * Static method to retrieve all the polygons in a vector of TObject2D.
01471                   */
01472                 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys);
01473                 /**
01474                   * Static method to retrieve all the points in a vector of TObject2D, returning the remainder objects in another parameter.
01475                   */
01476                 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts,std::vector<TObject2D> &remainder);
01477                 /**
01478                   * Static method to retrieve all the segments in a vector of TObject2D, returning the remainder objects in another parameter.
01479                   */
01480                 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms,std::vector<TObject2D> &remainder);
01481                 /**
01482                   * Static method to retrieve all the lines in a vector of TObject2D, returning the remainder objects in another parameter.
01483                   */
01484                 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins,std::vector<TObject2D> &remainder);
01485                 /**
01486                   * Static method to retrieve all the polygons in a vector of TObject2D, returning the remainder objects in another parameter.
01487                   */
01488                 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys,std::vector<TObject2D> &remainder);
01489         };
01490         /**
01491           * Standard object for storing any 3D lightweight object. Do not inherit from this class.
01492           * \sa TPoint3D,TSegment3D,TLine3D,TPlane,TPolygon3D
01493           */
01494         struct BASE_IMPEXP TObject3D    {
01495         private:
01496                 /**
01497                   * Object type identifier.
01498                   */
01499                 unsigned char type;
01500                 /**
01501                   * Union containing pointer to actual data.
01502                   */
01503                 union   {
01504                         TPoint3D *point;
01505                         TSegment3D *segment;
01506                         TLine3D *line;
01507                         TPolygon3D *polygon;
01508                         TPlane *plane;
01509                 }       data;
01510                 /**
01511                   * Destroys the object and releases the pointer, if any.
01512                   */
01513                 void destroy()  {
01514                         switch (type)   {
01515                                 case GEOMETRIC_TYPE_POINT:
01516                                         delete data.point;
01517                                         break;
01518                                 case GEOMETRIC_TYPE_SEGMENT:
01519                                         delete data.segment;
01520                                         break;
01521                                 case GEOMETRIC_TYPE_LINE:
01522                                         delete data.line;
01523                                         break;
01524                                 case GEOMETRIC_TYPE_POLYGON:
01525                                         delete data.polygon;
01526                                         break;
01527                                 case GEOMETRIC_TYPE_PLANE:
01528                                         delete data.plane;
01529                                         break;
01530                                 case GEOMETRIC_TYPE_UNDEFINED:
01531                                         break;
01532                                 default:
01533                                         THROW_EXCEPTION("Invalid TObject2D object");
01534                         }
01535                         type=GEOMETRIC_TYPE_UNDEFINED;
01536                 }
01537         public:
01538                 /**
01539                   * Constructor from point.
01540                   */
01541                 TObject3D(const TPoint3D &p):type(GEOMETRIC_TYPE_POINT) {
01542                         data.point=new TPoint3D(p);
01543                 }
01544                 /**
01545                   * Constructor from segment.
01546                   */
01547                 TObject3D(const TSegment3D &s):type(GEOMETRIC_TYPE_SEGMENT)     {
01548                         data.segment=new TSegment3D(s);
01549                 }
01550                 /**
01551                   * Constructor from line.
01552                   */
01553                 TObject3D(const TLine3D &r):type(GEOMETRIC_TYPE_LINE)   {
01554                         data.line=new TLine3D(r);
01555                 }
01556                 /**
01557                   * Constructor from polygon.
01558                   */
01559                 TObject3D(const TPolygon3D &p):type(GEOMETRIC_TYPE_POLYGON)     {
01560                         data.polygon=new TPolygon3D(p);
01561                 }
01562                 /**
01563                   * Constructor from plane.
01564                   */
01565                 TObject3D(const TPlane &p):type(GEOMETRIC_TYPE_PLANE)   {
01566                         data.plane=new TPlane(p);
01567                 }
01568                 /**
01569                   * Empty constructor.
01570                   */
01571                 TObject3D():type(GEOMETRIC_TYPE_UNDEFINED)      {}
01572                 /**
01573                   * Destructor.
01574                   */
01575                 ~TObject3D()    {
01576                         destroy();
01577                 }
01578                 /**
01579                   * Checks whether content is a point.
01580                   */
01581                 inline bool isPoint() const     {
01582                         return type==GEOMETRIC_TYPE_POINT;
01583                 }
01584                 /**
01585                   * Checks whether content is a segment.
01586                   */
01587                 inline bool isSegment() const   {
01588                         return type==GEOMETRIC_TYPE_SEGMENT;
01589                 }
01590                 /**
01591                   * Checks whether content is a line.
01592                   */
01593                 inline bool isLine() const      {
01594                         return type==GEOMETRIC_TYPE_LINE;
01595                 }
01596                 /**
01597                   * Checks whether content is a polygon.
01598                   */
01599                 inline bool isPolygon() const   {
01600                         return type==GEOMETRIC_TYPE_POLYGON;
01601                 }
01602                 /**
01603                   * Checks whether content is a plane.
01604                   */
01605                 inline bool isPlane() const     {
01606                         return type==GEOMETRIC_TYPE_PLANE;
01607                 }
01608                 /**
01609                   * Gets object type.
01610                   */
01611                 inline unsigned char getType() const    {
01612                         return type;
01613                 }
01614                 /**
01615                   * Gets the content as a point, returning false if the type is not adequate.
01616                   */
01617                 inline bool getPoint(TPoint3D &p) const {
01618                         if (isPoint())  {
01619                                 p=*(data.point);
01620                                 return true;
01621                         }       else return false;
01622                 }
01623                 /**
01624                   * Gets the content as a segment, returning false if the type is not adequate.
01625                   */
01626                 inline bool getSegment(TSegment3D &s) const     {
01627                         if (isSegment())        {
01628                                 s=*(data.segment);
01629                                 return true;
01630                         }       else return false;
01631                 }
01632                 /**
01633                   * Gets the content as a line, returning false if the type is not adequate.
01634                   */
01635                 inline bool getLine(TLine3D &r) const   {
01636                         if (isLine())   {
01637                                 r=*(data.line);
01638                                 return true;
01639                         }       else return false;
01640                 }
01641                 /**
01642                   * Gets the content as a polygon, returning false if the type is not adequate.
01643                   */
01644                 inline bool getPolygon(TPolygon3D &p) const     {
01645                         if (isPolygon())        {
01646                                 p=*(data.polygon);
01647                                 return true;
01648                         }       else return false;
01649                 }
01650                 /**
01651                   * Gets the content as a plane, returning false if the type is not adequate.
01652                   */
01653                 inline bool getPlane(TPlane &p) const   {
01654                         if (isPlane())  {
01655                                 p=*(data.plane);
01656                                 return true;
01657                         }       else return false;
01658                 }
01659                 /**
01660                   * Assigns another object, creating a new pointer if needed.
01661                   */
01662                 void operator=(const TObject3D &obj)    {
01663                         if (this==&obj) return;
01664                         destroy();
01665                         switch (type=obj.type)  {
01666                                 case GEOMETRIC_TYPE_POINT:
01667                                         data.point=new TPoint3D(*(obj.data.point));
01668                                         break;
01669                                 case GEOMETRIC_TYPE_SEGMENT:
01670                                         data.segment=new TSegment3D(*(obj.data.segment));
01671                                         break;
01672                                 case GEOMETRIC_TYPE_LINE:
01673                                         data.line=new TLine3D(*(obj.data.line));
01674                                         break;
01675                                 case GEOMETRIC_TYPE_POLYGON:
01676                                         data.polygon=new TPolygon3D(*(obj.data.polygon));
01677                                         break;
01678                                 case GEOMETRIC_TYPE_PLANE:
01679                                         data.plane=new TPlane(*(obj.data.plane));
01680                                         break;
01681                                 case GEOMETRIC_TYPE_UNDEFINED:
01682                                         break;
01683                                 default:
01684                                         THROW_EXCEPTION("Invalid TObject3D object");
01685                         }
01686                 }
01687                 /**
01688                   * Assigns a point to this object.
01689                   */
01690                 inline void operator=(const TPoint3D &p)        {
01691                         destroy();
01692                         type=GEOMETRIC_TYPE_POINT;
01693                         data.point=new TPoint3D(p);
01694                 }
01695                 /**
01696                   * Assigns a segment to this object.
01697                   */
01698                 inline void operator=(const TSegment3D &s)      {
01699                         destroy();
01700                         type=GEOMETRIC_TYPE_SEGMENT;
01701                         data.segment=new TSegment3D(s);
01702                 }
01703                 /**
01704                   * Assigns a line to this object.
01705                   */
01706                 inline void operator=(const TLine3D &l) {
01707                         destroy();
01708                         type=GEOMETRIC_TYPE_LINE;
01709                         data.line=new TLine3D(l);
01710                 }
01711                 /**
01712                   * Assigns a polygon to this object.
01713                   */
01714                 inline void operator=(const TPolygon3D &p)      {
01715                         destroy();
01716                         type=GEOMETRIC_TYPE_POLYGON;
01717                         data.polygon=new TPolygon3D(p);
01718                 }
01719                 /**
01720                   * Assigns a plane to this object.
01721                   */
01722                 inline void operator=(const TPlane &p)  {
01723                         destroy();
01724                         type=GEOMETRIC_TYPE_PLANE;
01725                         data.plane=new TPlane(p);
01726                 }
01727                 /**
01728                   * Projects into 2D space.
01729                   * \throw std::logic_error if the 3D object loses its properties when projecting into 2D space (for example, it's a plane or a vertical line).
01730                   */
01731                 inline void generate2DObject(TObject2D &obj) const      {
01732                         switch (type)   {
01733                                 case GEOMETRIC_TYPE_POINT:
01734                                         obj=TPoint2D(*(data.point));
01735                                         break;
01736                                 case GEOMETRIC_TYPE_SEGMENT:
01737                                         obj=TSegment2D(*(data.segment));
01738                                         break;
01739                                 case GEOMETRIC_TYPE_LINE:
01740                                         obj=TLine2D(*(data.line));
01741                                         break;
01742                                 case GEOMETRIC_TYPE_POLYGON:
01743                                         obj=TPolygon2D(*(data.polygon));
01744                                         break;
01745                                 case GEOMETRIC_TYPE_PLANE:
01746                                         throw std::logic_error("Too many dimensions");
01747                                 default:
01748                                         obj=TObject2D();
01749                                         break;
01750                         }
01751                 }
01752                 /**
01753                   * Constructs from another object.
01754                   */
01755                 TObject3D(const TObject3D &obj):type(GEOMETRIC_TYPE_UNDEFINED)  {
01756                         operator=(obj);
01757                 }
01758                 /**
01759                   * Static method to retrieve every point included in a vector of objects.
01760                   */
01761                 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts);
01762                 /**
01763                   * Static method to retrieve every segment included in a vector of objects.
01764                   */
01765                 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms);
01766                 /**
01767                   * Static method to retrieve every line included in a vector of objects.
01768                   */
01769                 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins);
01770                 /**
01771                   * Static method to retrieve every plane included in a vector of objects.
01772                   */
01773                 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns);
01774                 /**
01775                   * Static method to retrieve every polygon included in a vector of objects.
01776                   */
01777                 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys);
01778                 /**
01779                   * Static method to retrieve every point included in a vector of objects, returning the remaining objects in another argument.
01780                   */
01781                 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts,std::vector<TObject3D> &remainder);
01782                 /**
01783                   * Static method to retrieve every segment included in a vector of objects, returning the remaining objects in another argument.
01784                   */
01785                 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms,std::vector<TObject3D> &remainder);
01786                 /**
01787                   * Static method to retrieve every line included in a vector of objects, returning the remaining objects in another argument.
01788                   */
01789                 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins,std::vector<TObject3D> &remainder);
01790                 /**
01791                   * Static method to retrieve every plane included in a vector of objects, returning the remaining objects in another argument.
01792                   */
01793                 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns,std::vector<TObject3D> &remainder);
01794                 /**
01795                   * Static method to retrieve every polygon included in a vector of objects, returning the remaining objects in another argument.
01796                   */
01797                 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TObject3D> &remainder);
01798         };
01799 #else
01800         struct BASE_IMPEXP TObject2D    {
01801         private:
01802                 /**
01803                   * Object type identifier.
01804                   */
01805                 unsigned char type;
01806                 /**
01807                   * Union type storing pointers to every allowed type.
01808                   */
01809                 struct  {
01810                         TPoint2D point;
01811                         TSegment2D segment;
01812                         TLine2D line;
01813                         TPolygon2D *polygon;
01814                 }       data;
01815                 /**
01816                   * Destroys the object, releasing the pointer to the content (if any).
01817                   */
01818                 inline void destroy()   {
01819                         if (type==GEOMETRIC_TYPE_POLYGON) delete data.polygon;
01820                         type=GEOMETRIC_TYPE_UNDEFINED;
01821                 }
01822         public:
01823                 /**
01824                   * Implicit constructor from point.
01825                   */
01826                 inline TObject2D(const TPoint2D &p):type(GEOMETRIC_TYPE_POINT)  {
01827                         data.point=p;
01828                 }
01829                 /**
01830                   * Implicit constructor from segment.
01831                   */
01832                 inline TObject2D(const TSegment2D &s):type(GEOMETRIC_TYPE_SEGMENT)      {
01833                         data.segment=s;
01834                 }
01835                 /**
01836                   * Implicit constructor from line.
01837                   */
01838                 inline TObject2D(const TLine2D &r):type(GEOMETRIC_TYPE_LINE)    {
01839                         data.line=r;
01840                 }
01841                 /**
01842                   * Implicit constructor from polygon.
01843                   */
01844                 inline TObject2D(const TPolygon2D &p):type(GEOMETRIC_TYPE_POLYGON)      {
01845                         data.polygon=new TPolygon2D(p);
01846                 }
01847                 /**
01848                   * Implicit constructor from polygon.
01849                   */
01850                 TObject2D():type(GEOMETRIC_TYPE_UNDEFINED)      {}
01851                 /**
01852                   * Object destruction.
01853                   */
01854                 ~TObject2D()    {
01855                         destroy();
01856                 }
01857                 /**
01858                   * Checks whether content is a point.
01859                   */
01860                 inline bool isPoint() const     {
01861                         return type==GEOMETRIC_TYPE_POINT;
01862                 }
01863                 /**
01864                   * Checks whether content is a segment.
01865                   */
01866                 inline bool isSegment() const   {
01867                         return type==GEOMETRIC_TYPE_SEGMENT;
01868                 }
01869                 /**
01870                   * Checks whether content is a line.
01871                   */
01872                 inline bool isLine() const      {
01873                         return type==GEOMETRIC_TYPE_LINE;
01874                 }
01875                 /**
01876                   * Checks whether content is a polygon.
01877                   */
01878                 inline bool isPolygon() const   {
01879                         return type==GEOMETRIC_TYPE_POLYGON;
01880                 }
01881                 /**
01882                   * Gets content type.
01883                   */
01884                 inline unsigned char getType() const    {
01885                         return type;
01886                 }
01887                 /**
01888                   * Gets the content as a point, returning false if the type is inadequate.
01889                   */
01890                 inline bool getPoint(TPoint2D &p) const {
01891                         if (isPoint())  {
01892                                 p=data.point;
01893                                 return true;
01894                         }       else return false;
01895                 }
01896                 /**
01897                   * Gets the content as a segment, returning false if the type is inadequate.
01898                   */
01899                 inline bool getSegment(TSegment2D &s) const     {
01900                         if (isSegment())        {
01901                                 s=data.segment;
01902                                 return true;
01903                         }       else return false;
01904                 }
01905                 /**
01906                   * Gets the content as a line, returning false if the type is inadequate.
01907                   */
01908                 inline bool getLine(TLine2D &r) const   {
01909                         if (isLine())   {
01910                                 r=data.line;
01911                                 return true;
01912                         }       else return false;
01913                 }
01914                 /**
01915                   * Gets the content as a polygon, returning false if the type is inadequate.
01916                   */
01917                 inline bool getPolygon(TPolygon2D &p) const     {
01918                         if (isPolygon())        {
01919                                 p=*(data.polygon);
01920                                 return true;
01921                         }       else return false;
01922                 }
01923                 /**
01924                   * Assign another TObject2D. Pointers are not shared.
01925                   */
01926                 void operator=(const TObject2D &obj)    {
01927                         if (this==&obj) return;
01928                         destroy();
01929                         switch (type=obj.type)  {
01930                                 case GEOMETRIC_TYPE_POINT:
01931                                         data.point=obj.data.point;
01932                                         break;
01933                                 case GEOMETRIC_TYPE_SEGMENT:
01934                                         data.segment=obj.data.segment;
01935                                         break;
01936                                 case GEOMETRIC_TYPE_LINE:
01937                                         data.line=obj.data.line;
01938                                         break;
01939                                 case GEOMETRIC_TYPE_POLYGON:
01940                                         data.polygon=new TPolygon2D(*(obj.data.polygon));
01941                                         break;
01942                         }
01943                 }
01944                 /**
01945                   * Assign a point to this object.
01946                   */
01947                 inline void operator=(const TPoint2D &p)        {
01948                         destroy();
01949                         type=GEOMETRIC_TYPE_POINT;
01950                         data.point=p;
01951                 }
01952                 /**
01953                   * Assign a segment to this object.
01954                   */
01955                 inline void operator=(const TSegment2D &s)      {
01956                         destroy();
01957                         type=GEOMETRIC_TYPE_SEGMENT;
01958                         data.segment=s;
01959                 }
01960                 /**
01961                   * Assign a line to this object.
01962                   */
01963                 inline void operator=(const TLine2D &l) {
01964                         destroy();
01965                         type=GEOMETRIC_TYPE_LINE;
01966                         data.line=l;
01967                 }
01968                 /**
01969                   * Assign a polygon to this object.
01970                   */
01971                 inline void operator=(const TPolygon2D &p)      {
01972                         destroy();
01973                         type=GEOMETRIC_TYPE_POLYGON;
01974                         data.polygon=new TPolygon2D(p);
01975                 }
01976                 /**
01977                   * Project into 3D space.
01978                   */
01979                 void generate3DObject(TObject3D &obj) const;
01980                 /**
01981                   * Constructor from another TObject2D.
01982                   */
01983                 TObject2D(const TObject2D &obj):type(GEOMETRIC_TYPE_UNDEFINED)  {
01984                         operator=(obj);
01985                 }
01986                 /**
01987                   * Static method to retrieve all the points in a vector of TObject2D.
01988                   */
01989                 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts);
01990                 /**
01991                   * Static method to retrieve all the segments in a vector of TObject2D.
01992                   */
01993                 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms);
01994                 /**
01995                   * Static method to retrieve all the lines in a vector of TObject2D.
01996                   */
01997                 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins);
01998                 /**
01999                   * Static method to retrieve all the polygons in a vector of TObject2D.
02000                   */
02001                 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys);
02002                 /**
02003                   * Static method to retrieve all the points in a vector of TObject2D, returning the remainder objects in another parameter.
02004                   */
02005                 static void getPoints(const std::vector<TObject2D> &objs,std::vector<TPoint2D> &pnts,std::vector<TObject2D> &remainder);
02006                 /**
02007                   * Static method to retrieve all the segments in a vector of TObject2D, returning the remainder objects in another parameter.
02008                   */
02009                 static void getSegments(const std::vector<TObject2D> &objs,std::vector<TSegment2D> &sgms,std::vector<TObject2D> &remainder);
02010                 /**
02011                   * Static method to retrieve all the lines in a vector of TObject2D, returning the remainder objects in another parameter.
02012                   */
02013                 static void getLines(const std::vector<TObject2D> &objs,std::vector<TLine2D> &lins,std::vector<TObject2D> &remainder);
02014                 /**
02015                   * Static method to retrieve all the polygons in a vector of TObject2D, returning the remainder objects in another parameter.
02016                   */
02017                 static void getPolygons(const std::vector<TObject2D> &objs,std::vector<TPolygon2D> &polys,std::vector<TObject2D> &remainder);
02018         };
02019         /**
02020           * Standard object for storing any 3D lightweight object. Do not inherit from this class.
02021           * \sa TPoint3D,TSegment3D,TLine3D,TPlane,TPolygon3D
02022           */
02023         struct BASE_IMPEXP TObject3D    {
02024         private:
02025                 /**
02026                   * Object type identifier.
02027                   */
02028                 unsigned char type;
02029                 /**
02030                   * Union containing pointer to actual data.
02031                   */
02032                 struct  {
02033                         TPoint3D point;
02034                         TSegment3D segment;
02035                         TLine3D line;
02036                         TPolygon3D *polygon;
02037                         TPlane plane;
02038                 }       data;
02039                 /**
02040                   * Destroys the object and releases the pointer, if any.
02041                   */
02042                 void destroy()  {
02043                         if (type==GEOMETRIC_TYPE_POLYGON) delete data.polygon;
02044                         type=GEOMETRIC_TYPE_UNDEFINED;
02045                 }
02046         public:
02047                 /**
02048                   * Constructor from point.
02049                   */
02050                 TObject3D(const TPoint3D &p):type(GEOMETRIC_TYPE_POINT) {
02051                         data.point=p;
02052                 }
02053                 /**
02054                   * Constructor from segment.
02055                   */
02056                 TObject3D(const TSegment3D &s):type(GEOMETRIC_TYPE_SEGMENT)     {
02057                         data.segment=s;
02058                 }
02059                 /**
02060                   * Constructor from line.
02061                   */
02062                 TObject3D(const TLine3D &r):type(GEOMETRIC_TYPE_LINE)   {
02063                         data.line=r;
02064                 }
02065                 /**
02066                   * Constructor from polygon.
02067                   */
02068                 TObject3D(const TPolygon3D &p):type(GEOMETRIC_TYPE_POLYGON)     {
02069                         data.polygon=new TPolygon3D(p);
02070                 }
02071                 /**
02072                   * Constructor from plane.
02073                   */
02074                 TObject3D(const TPlane &p):type(GEOMETRIC_TYPE_PLANE)   {
02075                         data.plane=p;
02076                 }
02077                 /**
02078                   * Empty constructor.
02079                   */
02080                 TObject3D():type(GEOMETRIC_TYPE_UNDEFINED)      {}
02081                 /**
02082                   * Destructor.
02083                   */
02084                 ~TObject3D()    {
02085                         destroy();
02086                 }
02087                 /**
02088                   * Checks whether content is a point.
02089                   */
02090                 inline bool isPoint() const     {
02091                         return type==GEOMETRIC_TYPE_POINT;
02092                 }
02093                 /**
02094                   * Checks whether content is a segment.
02095                   */
02096                 inline bool isSegment() const   {
02097                         return type==GEOMETRIC_TYPE_SEGMENT;
02098                 }
02099                 /**
02100                   * Checks whether content is a line.
02101                   */
02102                 inline bool isLine() const      {
02103                         return type==GEOMETRIC_TYPE_LINE;
02104                 }
02105                 /**
02106                   * Checks whether content is a polygon.
02107                   */
02108                 inline bool isPolygon() const   {
02109                         return type==GEOMETRIC_TYPE_POLYGON;
02110                 }
02111                 /**
02112                   * Checks whether content is a plane.
02113                   */
02114                 inline bool isPlane() const     {
02115                         return type==GEOMETRIC_TYPE_PLANE;
02116                 }
02117                 /**
02118                   * Gets object type.
02119                   */
02120                 inline unsigned char getType() const    {
02121                         return type;
02122                 }
02123                 /**
02124                   * Gets the content as a point, returning false if the type is not adequate.
02125                   */
02126                 inline bool getPoint(TPoint3D &p) const {
02127                         if (isPoint())  {
02128                                 p=data.point;
02129                                 return true;
02130                         }       else return false;
02131                 }
02132                 /**
02133                   * Gets the content as a segment, returning false if the type is not adequate.
02134                   */
02135                 inline bool getSegment(TSegment3D &s) const     {
02136                         if (isSegment())        {
02137                                 s=data.segment;
02138                                 return true;
02139                         }       else return false;
02140                 }
02141                 /**
02142                   * Gets the content as a line, returning false if the type is not adequate.
02143                   */
02144                 inline bool getLine(TLine3D &r) const   {
02145                         if (isLine())   {
02146                                 r=data.line;
02147                                 return true;
02148                         }       else return false;
02149                 }
02150                 /**
02151                   * Gets the content as a polygon, returning false if the type is not adequate.
02152                   */
02153                 inline bool getPolygon(TPolygon3D &p) const     {
02154                         if (isPolygon())        {
02155                                 p=*(data.polygon);
02156                                 return true;
02157                         }       else return false;
02158                 }
02159                 /**
02160                   * Gets the content as a plane, returning false if the type is not adequate.
02161                   */
02162                 inline bool getPlane(TPlane &p) const   {
02163                         if (isPlane())  {
02164                                 p=data.plane;
02165                                 return true;
02166                         }       else return false;
02167                 }
02168                 /**
02169                   * Assigns another object, creating a new pointer if needed.
02170                   */
02171                 void operator=(const TObject3D &obj)    {
02172                         if (this==&obj) return;
02173                         destroy();
02174                         switch (type=obj.type)  {
02175                                 case GEOMETRIC_TYPE_POINT:
02176                                         data.point=obj.data.point;
02177                                         break;
02178                                 case GEOMETRIC_TYPE_SEGMENT:
02179                                         data.segment=obj.data.segment;
02180                                         break;
02181                                 case GEOMETRIC_TYPE_LINE:
02182                                         data.line=obj.data.line;
02183                                         break;
02184                                 case GEOMETRIC_TYPE_POLYGON:
02185                                         data.polygon=new TPolygon3D(*(obj.data.polygon));
02186                                         break;
02187                                 case GEOMETRIC_TYPE_PLANE:
02188                                         data.plane=obj.data.plane;
02189                                         break;
02190                                 case GEOMETRIC_TYPE_UNDEFINED:
02191                                         break;
02192                                 default:
02193                                         THROW_EXCEPTION("Invalid TObject3D object");
02194                         }
02195                 }
02196                 /**
02197                   * Assigns a point to this object.
02198                   */
02199                 inline void operator=(const TPoint3D &p)        {
02200                         destroy();
02201                         type=GEOMETRIC_TYPE_POINT;
02202                         data.point=p;
02203                 }
02204                 /**
02205                   * Assigns a segment to this object.
02206                   */
02207                 inline void operator=(const TSegment3D &s)      {
02208                         destroy();
02209                         type=GEOMETRIC_TYPE_SEGMENT;
02210                         data.segment=s;
02211                 }
02212                 /**
02213                   * Assigns a line to this object.
02214                   */
02215                 inline void operator=(const TLine3D &l) {
02216                         destroy();
02217                         type=GEOMETRIC_TYPE_LINE;
02218                         data.line=l;
02219                 }
02220                 /**
02221                   * Assigns a polygon to this object.
02222                   */
02223                 inline void operator=(const TPolygon3D &p)      {
02224                         destroy();
02225                         type=GEOMETRIC_TYPE_POLYGON;
02226                         data.polygon=new TPolygon3D(p);
02227                 }
02228                 /**
02229                   * Assigns a plane to this object.
02230                   */
02231                 inline void operator=(const TPlane &p)  {
02232                         destroy();
02233                         type=GEOMETRIC_TYPE_PLANE;
02234                         data.plane=p;
02235                 }
02236                 /**
02237                   * Projects into 2D space.
02238                   * \throw std::logic_error if the 3D object loses its properties when projecting into 2D space (for example, it's a plane or a vertical line).
02239                   */
02240                 inline void generate2DObject(TObject2D &obj) const      {
02241                         switch (type)   {
02242                                 case GEOMETRIC_TYPE_POINT:
02243                                         obj=TPoint2D(data.point);
02244                                         break;
02245                                 case GEOMETRIC_TYPE_SEGMENT:
02246                                         obj=TSegment2D(data.segment);
02247                                         break;
02248                                 case GEOMETRIC_TYPE_LINE:
02249                                         obj=TLine2D(data.line);
02250                                         break;
02251                                 case GEOMETRIC_TYPE_POLYGON:
02252                                         obj=TPolygon2D(*(data.polygon));
02253                                         break;
02254                                 case GEOMETRIC_TYPE_PLANE:
02255                                         throw std::logic_error("Too many dimensions");
02256                                 default:
02257                                         obj=TObject2D();
02258                                         break;
02259                         }
02260                 }
02261                 /**
02262                   * Constructs from another object.
02263                   */
02264                 TObject3D(const TObject3D &obj):type(GEOMETRIC_TYPE_UNDEFINED)  {
02265                         operator=(obj);
02266                 }
02267                 /**
02268                   * Static method to retrieve every point included in a vector of objects.
02269                   */
02270                 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts);
02271                 /**
02272                   * Static method to retrieve every segment included in a vector of objects.
02273                   */
02274                 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms);
02275                 /**
02276                   * Static method to retrieve every line included in a vector of objects.
02277                   */
02278                 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins);
02279                 /**
02280                   * Static method to retrieve every plane included in a vector of objects.
02281                   */
02282                 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns);
02283                 /**
02284                   * Static method to retrieve every polygon included in a vector of objects.
02285                   */
02286                 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys);
02287                 /**
02288                   * Static method to retrieve every point included in a vector of objects, returning the remaining objects in another argument.
02289                   */
02290                 static void getPoints(const std::vector<TObject3D> &objs,std::vector<TPoint3D> &pnts,std::vector<TObject3D> &remainder);
02291                 /**
02292                   * Static method to retrieve every segment included in a vector of objects, returning the remaining objects in another argument.
02293                   */
02294                 static void getSegments(const std::vector<TObject3D> &objs,std::vector<TSegment3D> &sgms,std::vector<TObject3D> &remainder);
02295                 /**
02296                   * Static method to retrieve every line included in a vector of objects, returning the remaining objects in another argument.
02297                   */
02298                 static void getLines(const std::vector<TObject3D> &objs,std::vector<TLine3D> &lins,std::vector<TObject3D> &remainder);
02299                 /**
02300                   * Static method to retrieve every plane included in a vector of objects, returning the remaining objects in another argument.
02301                   */
02302                 static void getPlanes(const std::vector<TObject3D> &objs,std::vector<TPlane> &plns,std::vector<TObject3D> &remainder);
02303                 /**
02304                   * Static method to retrieve every polygon included in a vector of objects, returning the remaining objects in another argument.
02305                   */
02306                 static void getPolygons(const std::vector<TObject3D> &objs,std::vector<TPolygon3D> &polys,std::vector<TObject3D> &remainder);
02307         };
02308 
02309 #endif
02310 
02311 
02312         //Streaming functions
02313         /**
02314           * TPoint2D binary input.
02315           */
02316         BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPoint2D &o);
02317         /**
02318           * TPoint2D binary output.
02319           */
02320         BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPoint2D &o);
02321 
02322         /**
02323           * TPoint3D binary input.
02324           */
02325         BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPoint3D &o);
02326         /**
02327           * TPoint3D binary output.
02328           */
02329         BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPoint3D &o);
02330 
02331         /**
02332           * TPose2D binary input.
02333           */
02334         BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPose2D &o);
02335         /**
02336           * TPose2D binary output.
02337           */
02338         BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPose2D &o);
02339 
02340         /**
02341           * TPose3D binary input.
02342           */
02343         BASE_IMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,mrpt::math::TPose3D &o);
02344         /**
02345           * TPose3D binary output.
02346           */
02347         BASE_IMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const mrpt::math::TPose3D &o);
02348 
02349         /**
02350           * TSegment2D binary input.
02351           */
02352         inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TSegment2D &s)     {
02353                 return in>>s.point1>>s.point2;
02354         }
02355         /**
02356           * TSegment2D binary output.
02357           */
02358         inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TSegment2D &s)      {
02359                 return out<<s.point1<<s.point2;
02360         }
02361 
02362         /**
02363           * TLine2D binary input.
02364           */
02365         inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TLine2D &l)        {
02366                 return in>>l.coefs[0]>>l.coefs[1]>>l.coefs[2];
02367         }
02368         /**
02369           * TLine2D binary output.
02370           */
02371         inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TLine2D &l) {
02372                 return out<<l.coefs[0]<<l.coefs[1]<<l.coefs[2];
02373         }
02374 
02375         /**
02376           * TObject2D binary input.
02377           */
02378         BASE_IMPEXP mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TObject2D &o);
02379         /**
02380           * TObject2D binary input.
02381           */
02382         BASE_IMPEXP mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TObject2D &o);
02383 
02384         /**
02385           * TSegment3D binary input.
02386           */
02387         inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TSegment3D &s)     {
02388                 return in>>s.point1>>s.point2;
02389         }
02390         /**
02391           * TSegment3D binary output.
02392           */
02393         inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TSegment3D &s)      {
02394                 return out<<s.point1<<s.point2;
02395         }
02396 
02397         /**
02398           * TLine3D binary input.
02399           */
02400         inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TLine3D &l)        {
02401                 return in>>l.pBase>>l.director[0]>>l.director[1]>>l.director[2];
02402         }
02403         /**
02404           * TLine3D binary output.
02405           */
02406         inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TLine3D &l) {
02407                 return out<<l.pBase<<l.director[0]<<l.director[1]<<l.director[2];
02408         }
02409 
02410         /**
02411           * TPlane binary input.
02412           */
02413         inline mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TPlane &p) {
02414                 return in>>p.coefs[0]>>p.coefs[1]>>p.coefs[2]>>p.coefs[3];
02415         }
02416         /**
02417           * TPlane binary output.
02418           */
02419         inline mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TPlane &p)  {
02420                 return out<<p.coefs[0]<<p.coefs[1]<<p.coefs[2]<<p.coefs[3];
02421         }
02422 
02423         /**
02424           * TObject3D binary input.
02425           */
02426         BASE_IMPEXP mrpt::utils::CStream &operator>>(mrpt::utils::CStream &in,mrpt::math::TObject3D &o);
02427         /**
02428           * TObject3D binary output.
02429           */
02430         BASE_IMPEXP mrpt::utils::CStream &operator<<(mrpt::utils::CStream &out,const mrpt::math::TObject3D &o);
02431 
02432 
02433         /** @} */ // end of grouping
02434 
02435         }       //end of namespace math
02436 
02437         namespace utils
02438         {
02439                 using namespace ::mrpt::math;
02440 
02441                 // Specialization must occur in the same namespace
02442                 MRPT_DECLARE_TTYPENAME(TPoint2D)
02443                 MRPT_DECLARE_TTYPENAME(TPoint3D)
02444                 MRPT_DECLARE_TTYPENAME(TPose2D)
02445                 MRPT_DECLARE_TTYPENAME(TPose3D)
02446                 MRPT_DECLARE_TTYPENAME(TSegment2D)
02447                 MRPT_DECLARE_TTYPENAME(TLine2D)
02448                 MRPT_DECLARE_TTYPENAME(TPolygon2D)
02449                 MRPT_DECLARE_TTYPENAME(TObject2D)
02450                 MRPT_DECLARE_TTYPENAME(TSegment3D)
02451                 MRPT_DECLARE_TTYPENAME(TLine3D)
02452                 MRPT_DECLARE_TTYPENAME(TPlane)
02453                 MRPT_DECLARE_TTYPENAME(TPolygon3D)
02454                 MRPT_DECLARE_TTYPENAME(TObject3D)
02455 
02456         } // end of namespace utils
02457 
02458 }       //end of namespace
02459 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011