TerraLib 4.1
E:/Projetos_Primeiro_Semestre_2012/TerraView/terralib/src/terralib/kernel/TeGeometry.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 TerraLib - a library for developing GIS applications.
00003 Copyright  2001-2007 INPE and Tecgraf/PUC-Rio.
00004 
00005 This code is part of the TerraLib library.
00006 This library is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU Lesser General Public
00008 License as published by the Free Software Foundation; either
00009 version 2.1 of the License, or (at your option) any later version.
00010 
00011 You should have received a copy of the GNU Lesser General Public
00012 License along with this library.
00013 
00014 The authors reassure the license terms regarding the warranties.
00015 They specifically disclaim any warranties, including, but not limited to,
00016 the implied warranties of merchantability and fitness for a particular purpose.
00017 The library provided hereunder is on an "as is" basis, and the authors have no
00018 obligation to provide maintenance, support, updates, enhancements, or modifications.
00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
00020 indirect, special, incidental, or consequential damages arising out of the use
00021 of this library and its documentation.
00022 *************************************************************************************/
00027 #ifndef __TERRALIB_INTERNAL_GEOMETRY_H
00028 #define __TERRALIB_INTERNAL_GEOMETRY_H
00029 
00030 #if defined(_MSC_VER) /* MSVC Compiler */
00031 #pragma warning(disable: 4786)
00032 #endif
00033 
00034 #include "TeDefines.h"
00035 #include "TeBox.h"
00036 #include "TeCoord2D.h"
00037 #include "TeComposite.h"
00038 #include "TeMeasure.h"
00039 #include "TeUtils.h"
00040 #include "TeDataTypes.h"
00041 
00042 #include <string>
00043 #include <iostream>
00044 
00045 
00046 using namespace std;
00047 
00049 
00056 class TL_DLL TeGeometry
00057 {
00058 public:
00059 
00061            TeGeometry(): box_ ( TeMAXFLOAT, TeMAXFLOAT, -TeMAXFLOAT, -TeMAXFLOAT ),
00062                       geomId_  ( 0 ), objectId_ (""), srid_(-1) {}
00063 
00065            TeGeometry ( const TeGeometry& other ) 
00066            {
00067                       box_    = other.box_;
00068                       geomId_ = other.geomId_;
00069                       objectId_ = other.objectId_;
00070                       srid_ = other.srid_;
00071            }
00072            
00074            virtual ~TeGeometry() {}
00075 
00077            void setBox ( const TeBox & box )
00078            { box_  = box; }
00079            
00081            const TeBox& box () const 
00082            { return box_; }
00083 
00085            TeBox& box ()
00086            { return box_; }
00087 
00089            int geomId() const
00090            { return geomId_; }
00091 
00093            void geomId( int id )
00094            { geomId_ = id; }
00095 
00097            virtual string objectId() const
00098            { return objectId_; }
00099 
00101            virtual void objectId ( const string& id )
00102            { objectId_ = id; }
00103 
00105            virtual int srid() const
00106            { return srid_; }
00107 
00109            virtual void srid(const int& srid)
00110            { srid_ = srid; }
00111 
00113            virtual unsigned int size() const
00114            { return 0; }
00115 
00117            ostream& operator<<(ostream& os)
00118            {
00119                       os << Te2String(geomId_);
00120                       return os;
00121            }
00122 
00124            virtual bool isRing() const
00125            { return false; }
00126 
00128            virtual TeGeomRep elemType() const 
00129            { return TeGEOMETRYNONE; }
00130            
00131 protected:
00132 
00133            TeBox      box_;                 
00134            int                   geomId_;   
00135            string     objectId_; 
00136            int                   srid_;                
00137 };
00138 
00139 
00141 
00144 class TL_DLL TeGeometryNone:  public TeGeometry
00145 {
00146 public: 
00148            TeGeomRep elemType() const { return TeGEOMETRYNONE; }
00149 
00151            void clear () { return; }
00152 };
00153 
00155 class TL_DLL TeVector : public TeGeometry
00156 {
00157 };
00158 
00160 
00163 template <class T>
00164 class TeGeomSingle : public TeVector
00165 {
00166 public:
00167 
00169            typedef  T value_type;
00170 
00172            TeGeomSingle<T>() {}
00173 
00175            TeGeomSingle<T> (const T& elem ): elem_ ( elem  ) 
00176            {          updateBox ( box_, elem ); }
00177 
00179            TeGeomSingle ( const TeGeomSingle& other ) : TeVector()
00180            {
00181                       box_    = other.box_;
00182                       geomId_ = other.geomId_;
00183                       objectId_ = other.objectId_;
00184                       elem_ = other.elem_;
00185            }
00186 
00188            TeGeomSingle& operator = ( const TeGeomSingle& other )
00189            {
00190                       box_    = other.box_;
00191                       geomId_ = other.geomId_;
00192                       objectId_ = other.objectId_;
00193                       elem_ = other.elem_;
00194                       return *this;
00195            }
00196 
00198            virtual ~TeGeomSingle<T>() {}
00199 
00201            void add ( T& elem )
00202            {
00203                       elem_ = elem;
00204                       box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT); // invalidates its box
00205                       updateBox ( box_, elem );
00206            }
00207 
00209            T&         location ()
00210            {          return elem_; }
00211 
00213            const T& location () const
00214            {          return elem_; }
00215 
00217            T& elem ()
00218            {          return elem_; }
00219 
00221            const T& elem () const
00222            {          return elem_; }
00223 
00225            T& operator [] ( int /* i */) 
00226            {          return elem_; }
00227 
00229            bool operator== (const TeGeomSingle& other) const
00230            {          return elem_ == other.elem(); }
00231 
00233            int size() { return 1; }
00234 
00235 protected:
00236            T          elem_;
00237 
00238 };
00239 
00240 
00241 
00243 
00252 template <class T>  
00253 class TeGeomComposite: public TeVector
00254 {
00255 public:
00256 
00258            TeGeomComposite()
00259            {
00260                       pImpl_ = new TeComposite<T>;
00261                       pImpl_->attach();
00262            }
00263 
00265            virtual ~TeGeomComposite()
00266            {          pImpl_->detach();     }
00267 
00268 
00270            TeGeomComposite ( const TeGeomComposite& other ) : TeVector()
00271            {
00272                       pImpl_ = other.pImpl_;
00273                       pImpl_->attach();
00274                       box_    = other.box_;
00275                       geomId_ = other.geomId_;
00276                       objectId_ = other.objectId_;
00277            }
00278 
00280            TeGeomComposite& operator= ( const TeGeomComposite& other )
00281            {
00282                       if ( this != &other )
00283                       {          
00284                                  other.pImpl_->attach();
00285                                  pImpl_->detach();
00286                                  pImpl_  = other.pImpl_;
00287                                  box_    = other.box_;
00288                                  geomId_ = other.geomId_;
00289                                  objectId_ = other.objectId_;
00290                       }
00291                       return *this;
00292            }
00293 
00295            virtual string objectId() const
00296            { return objectId_; }
00297 
00299            virtual void objectId (const string& id )
00300            { 
00301                       objectId_ = id; 
00302                       typename TeComposite<T>::iterator it = pImpl_->begin();
00303                       while (it != pImpl_->end())
00304                       {
00305                                  it->objectId(id);
00306                                  ++it;
00307                       }
00308            }
00309 
00311            void copyElements ( const TeGeomComposite& other )
00312            {
00313                       geomId_ = other.geomId_;
00314                       objectId_ = other.objectId_;
00315 
00316                       for (unsigned int i = 0; i < other.pImpl_->size(); i++)
00317                                  add (other.pImpl_->operator[](i));
00318            }
00319 
00321            bool operator== (const TeGeomComposite& other) const
00322            {          
00323                       if ( this->size() != other.size() )
00324                                  return false;
00325 
00326                       for (unsigned int i = 0; i < other.pImpl_->size(); i++)
00327                                  if ( ! ( pImpl_->operator[]( i ) == other.pImpl_->operator[]( i ) ) )
00328                                             return false;
00329       
00330                       return true; 
00331            }
00332 
00334            void add ( const T& elem )
00335            {          
00336                       pImpl_->add ( elem );
00337                       updateBox ( box_, elem );
00338            }
00339 
00341            bool erase ( int i )
00342            {          
00343                       bool status = pImpl_->erase (i);
00344 
00345                       if (status)           // recalculates the box
00346                       {
00347                                  box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT);
00348                                  for (unsigned int j = 0; j < pImpl_->size(); j++)
00349                                             updateBox(box_,pImpl_->operator[](j));
00350                       }
00351                       return status;
00352            }
00353 
00355            bool erase ( T& object )
00356            {          
00357                       bool status = pImpl_->erase ( object ); 
00358                       if (status)           // recalculates the box
00359                       {
00360                                  box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT);
00361                                  for (unsigned int j = 0; j < pImpl_->size(); j++)
00362                                             updateBox(box_,pImpl_->operator[](j));
00363                       }
00364                       return status;
00365            }
00366 
00368            typename TeComposite<T>::iterator erase(typename TeComposite<T>::iterator it)
00369            {          
00370                       typename TeComposite<T>::iterator res = pImpl_->erase(it); 
00371                       box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT);
00372                       for (unsigned int j = 0; j < pImpl_->size(); j++)
00373                                  updateBox(box_,pImpl_->operator[](j));
00374                       return res;
00375            }
00376 
00378            void clear ()
00379            {          
00380                       pImpl_->clear ();                // remove all elements
00381                       box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT); // invalidates its box
00382            }
00383            
00385            unsigned int size() const
00386            {          return ( (unsigned int) pImpl_->size() ); }
00387 
00389            void reserve(int nelem)
00390            { pImpl_->reserve(nelem); }
00391 
00393            T& operator [] ( int i ) const
00394            {          return pImpl_->operator[] ( i ); }
00395 
00397            T& first() const
00398            {          return pImpl_->operator[] ( 0 ); }
00399            
00401            T& last() const
00402            {          return pImpl_->operator[] ( pImpl_->size()-1 ); }
00403            
00405            bool empty () const
00406            {          return pImpl_->empty (); }
00407 
00409            typedef typename TeComposite<T>::iterator iterator;
00410 
00412 //         typedef typename T value_type;
00413            typedef  T value_type;
00414 
00416            typename TeComposite<T>::iterator begin()
00417            { return pImpl_->begin(); }
00418 
00420            typename TeComposite<T>::iterator const begin() const
00421            { return pImpl_->begin(); }
00422 
00424            typename TeComposite<T>::iterator end()
00425            { return pImpl_->end(); }
00426 
00428            typename TeComposite<T>::iterator const end() const
00429            { return pImpl_->end(); }
00430 
00432            typedef typename TeComposite<T>::reverse_iterator reverse_iterator;
00433 
00435            typename TeComposite<T>::reverse_iterator rbegin()
00436            { return pImpl_->rbegin(); }
00437 
00439            typename TeComposite<T>::reverse_iterator rend()
00440            { return pImpl_->rend(); }
00441 
00442 protected:
00443 
00445            TeComposite<T> * pImpl_; 
00446 };
00447 
00449 
00452 class TL_DLL TeLine2D : public TeGeomComposite<TeCoord2D>
00453 {
00454 public:
00456            bool isRing() const;  
00457 
00459            string objectId() const
00460            { return objectId_; }
00461            
00463            void objectId (const string& id )
00464            {  objectId_ = id; }
00465 
00467            TeGeomRep elemType() const { return TeLINES; }
00468 };
00469 
00471 
00474 class TL_DLL TeLineSet: public TeGeomComposite<TeLine2D>
00475 {
00476 public:
00478            TeGeomRep elemType() const { return TeLINES; }
00479 
00481            void copyElements (const TeLineSet& other ); 
00482 }; 
00483 
00485 
00492 class TL_DLL TeLinearRing : public TeLine2D  {
00493 public:
00494 
00496            TeLinearRing() : TeLine2D() {}
00497 
00499            TeLinearRing ( TeLine2D& line );
00500 };
00501 
00502 
00504 
00508 class TL_DLL TePolygon: public TeGeomComposite<TeLinearRing>  
00509 {
00510 public:
00511            
00513            TeGeomRep elemType() const { return TePOLYGONS; }
00514 
00516            void copyElements ( const TePolygon& other );
00517 };
00518 
00520 class TL_DLL TePolygonSet: public TeGeomComposite<TePolygon> 
00521 {
00522 public:
00524            TeGeomRep elemType() const { return TePOLYGONS; }
00525 
00527            void copyElements ( const TePolygonSet& other );
00528 };
00529 
00530            
00532 class TL_DLL TePoint : public TeGeomSingle<TeCoord2D>
00533 {
00534 public:
00536            TePoint(const double& x = 0., const double& y = 0. ):
00537                       TeGeomSingle<TeCoord2D> ( )
00538            {
00539                                  elem_ = TeCoord2D(x,y);
00540                                  setBox(TeBox(x,y,x,y)); // the box of a point is the point itself
00541            }
00542 
00544            TePoint(const TeCoord2D& c):
00545                       TeGeomSingle<TeCoord2D> ( )
00546            {
00547                                  elem_ = c;
00548                                  setBox(TeBox(c.x(),c.y(),c.x(),c.y())); // the box of a point is the point itself
00549            }
00550 
00552            TeGeomRep elemType() const { return TePOINTS; }
00553 
00555            string objectId() const
00556            { return objectId_; }
00557            
00559            void objectId (const string& id )
00560            {  objectId_ = id; }
00561 
00562 };
00563 
00565 class TL_DLL TePointSet: public  TeGeomComposite<TePoint> 
00566 {
00567 public:
00569            TeGeomRep elemType() const { return TePOINTS; }
00570 };
00571 
00573 class TL_DLL TeText: public TeGeomSingle<TeCoord2D>
00574 {
00575 public:
00577 
00580            TeText(const string& txt="" ):
00581                       TeGeomSingle<TeCoord2D> ( ),
00582                       angle_(0),
00583                       height_(0),
00584                       textValue_(txt),
00585                       alignmentVert_(0),
00586                       alignmentHoriz_(0)
00587            {
00588                       elem_ = TeCoord2D(0,0);
00589                       setBox(TeBox(0.0,0.0,0.0,0.0));
00590            }
00591 
00593 
00598            TeText( TeCoord2D& location, const string& txt="" ):
00599                       TeGeomSingle<TeCoord2D> ( location ),
00600                       angle_(0),
00601                       height_(0),
00602                       textValue_(txt),
00603                       alignmentVert_(0),
00604                       alignmentHoriz_(0)
00605            {
00606                       setBox(TeBox(location,location));
00607            }
00608 
00610            TeText(const TeText& other ) : TeGeomSingle<TeCoord2D>()
00611            {
00612                       angle_ = other.angle_;
00613                       height_ = other.height_;
00614                       textValue_ = other.textValue_;
00615                       alignmentVert_ = other.alignmentVert_;
00616                       alignmentHoriz_ = other. alignmentHoriz_;
00617                       setBox(other.box());
00618                       elem_ = other.elem_;
00619                       geomId_ = other.geomId_;
00620                       objectId_ = other.objectId_;
00621            }
00622 
00624            TeText& operator= ( const TeText& other )
00625            {
00626                       if ( this != &other )
00627                       {          
00628                                  angle_ = other.angle_;
00629                                  height_ = other.height_;
00630                                  textValue_ = other.textValue_;
00631                                  alignmentVert_ = other.alignmentVert_;
00632                                  alignmentHoriz_ = other. alignmentHoriz_;
00633                                  setBox(other.box());
00634                                  elem_ = other.elem_;
00635                                  geomId_ = other.geomId_;
00636                                  objectId_ = other.objectId_;
00637                       }
00638                       return *this;
00639            }
00640 
00642            bool operator== (const TeText& tx) const 
00643            {
00644                       return (angle_ == tx.angle_ &&
00645                                  height_ == tx.height_ &&
00646                                  textValue_ == tx.textValue_ &&
00647                                  alignmentVert_ == tx.alignmentVert_ &&
00648                                  alignmentHoriz_ == tx.alignmentHoriz_ &&
00649                                  elem_ == tx.elem_ &&
00650                                  geomId_ == tx.geomId_ &&
00651                                  objectId_ == tx.objectId_);
00652            }
00653 
00655            void setLocation(const TeCoord2D& l)
00656            {          elem_ = l; setBox(TeBox(l,l));   }
00657 
00659            string textValue () const
00660            { return textValue_; }
00661            
00663            void setTextValue (const string &text) 
00664            { textValue_ = text; }
00665 
00667            double angle () const
00668            { return angle_; }
00669 
00671            void setAngle (double angle) 
00672            { angle_ = angle; }
00673 
00675            double height () const
00676            { return height_ ; }
00677 
00679            void setHeight (double height) 
00680            { height_ = height; }
00681 
00683            double alignmentVert () const
00684            { return alignmentVert_ ; }
00685 
00687            void setAlignmentVert (double alig) 
00688            { alignmentVert_ = alig; }
00689 
00691            double alignmentHoriz () const
00692            { return alignmentHoriz_ ; }
00693 
00695            void setAlignmentHoriz (double alig) 
00696            { alignmentHoriz_ = alig; }
00697 
00699            TeGeomRep elemType() const { return TeTEXT; }
00700 
00701 private:
00702            double angle_;        
00703            double height_;
00704            string textValue_;
00705            double alignmentVert_;
00706            double alignmentHoriz_;
00707 };
00708 
00709 
00711 class TL_DLL TeTextSet : public TeGeomComposite<TeText> 
00712 {
00713 public:
00715            TeGeomRep elemType() const { return TeTEXT; }
00716 
00717 };
00718 
00719 
00721 class TL_DLL TeNode: public TeGeomSingle<TeCoord2D>
00722 {
00723 public:
00725            bool operator== (const TeNode& node) const
00726            {
00727                       TeCoord2D p1 = elem_;
00728                       TeCoord2D p2 = node.elem_;
00729                       return p1==p2;
00730            }
00731 
00733            ostream& operator<<(ostream& os)
00734            {
00735                       os << Te2String(geomId_);
00736                       return os;
00737            }
00738 
00740            TeGeomRep elemType() const { return TeNODES; }
00741 
00742 };
00743 
00745 TL_DLL ostream& operator<<(ostream& os, TeNode& N);
00746 
00748 class TL_DLL TeNodeSet : public TeGeomComposite<TeNode> 
00749 
00750 {
00751 public:
00753            TeGeomRep elemType() const { return TeNODES; }
00754 
00755 };
00756 
00758 class TL_DLL TeArc : public TeVector 
00759 {
00760 public:
00761 
00763            TeArc(): ifrom_ (-1), ito_ (-1){}
00764 
00766 
00770            TeArc(TeNode& from, TeNode& to)
00771            {
00772                       from_ = from;
00773                       to_ = to;
00774                       updateBox ( box_, from );
00775                       updateBox ( box_, to );
00776            }
00777 
00779 
00783            TeArc(int from, int to): ifrom_ (from), ito_ (to) {}
00784 
00785 // -- Methods
00786 
00788            TeNode& fromNode () 
00789            { return from_; }
00790 
00792            TeNode& toNode () 
00793            { return to_; }
00794 
00796            int fromId () const
00797            { return ifrom_; }
00798 
00800            void fromId (int i) 
00801            { ifrom_ = i; }
00802 
00804            int toId () const
00805            { return ito_; }
00806 
00808            void toId (int i)
00809            { ito_ = i; }
00810            
00812            void setNodes (TeNode& from, TeNode& to)
00813            { 
00814                       from_ = from;
00815                       to_ = to;
00816                       updateBox ( box_, from );
00817                       updateBox ( box_, to );
00818            }
00819 
00821            TeGeomRep elemType() const { return TeARCS; }
00822 
00824            bool operator== (const TeArc& other) const
00825            {
00826                       if((from_ == other.from_) &&
00827                   (to_ == other.to_) &&
00828                          (ifrom_ == other.ifrom_) &&
00829            (ito_ != other.ito_))
00830                                  return true;
00831 
00832                       return false;
00833            }
00834 
00835 private:
00836 
00837            TeNode     from_, to_;
00838            int                   ifrom_, ito_;
00839 };
00840 
00842 TL_DLL ostream& operator<<(ostream& os, const TeArc& N);
00843 
00844 
00846 class TL_DLL TeArcSet: public TeGeomComposite <TeArc> 
00847 {
00848 public:
00850            TeGeomRep elemType() const { return TeARCS; }
00851 };
00852 
00854 class TL_DLL TeSample: public TeGeomSingle<TeCoord2D>, public TeMeasure
00855 {
00856 public:
00858 
00862            TeSample ( const TeCoord2D& location, double measure = 0.  ):
00863                       TeGeomSingle<TeCoord2D> ( location ), TeMeasure ( measure ) {}
00864 
00865            TeGeomRep elemType() const { return TeSAMPLES; }
00866 };
00867 
00869 class TL_DLL TeSampleSet: public TeGeomComposite<TeSample>
00870 {
00871 public:
00873            TeGeomRep elemType() const { return TeSAMPLES; }
00874 };
00875 
00877 class TL_DLL TeContourLine: public TeLine2D, public TeMeasure
00878 {
00879 public:
00881 
00885            TeContourLine ( TeLine2D& line, double measure = 0. )
00886                       : TeLine2D ( line ), TeMeasure ( measure )
00887            {}
00888 };
00889 
00890 
00892 class TL_DLL TeContourLineSet: public  TeGeomComposite <TeContourLine> 
00893 {
00894 public:
00896            TeGeomRep elemType() const { return TeSAMPLES; }
00897 };
00898 
00899 
00901 class TL_DLL TeCell : public TeVector 
00902 {
00903            int column_;          
00904            int line_;            
00905 
00906 public:
00908            TeCell():
00909              column_(-1),
00910              line_(-1) {}
00911 
00912            TeCell(TeBox& box, int col, int lin):
00913              column_(col),
00914              line_(lin) { setBox(box); }
00915 
00917            int        column () const
00918            { return column_; }
00919 
00921            void column (int column) 
00922            { column_ =  column; }
00923 
00925            int        line () const
00926            { return line_; }
00927 
00929            void line (int line) 
00930            { line_ =  line; }
00931 
00933            TeGeomRep elemType() const { return TeCELLS; }
00934 
00936            bool operator== (const TeCell& other) const
00937            {
00938                       if((column_ == other.column_) &&
00939                   (line_ == other.line_))
00940                                  return true;
00941 
00942                       return false;
00943            }
00944 };
00945 
00947 class TL_DLL TeCellSet: public  TeGeomComposite<TeCell> 
00948 {
00949            double     resX_;     
00950            double     resY_;     
00951 
00952 public:
00953 
00955            TeCellSet() : resX_(0.), resY_(0.) 
00956            {}
00957 
00959            double resX () const
00960            { return resX_; }
00961 
00963            double resY () const
00964            { return resY_; }
00965 
00967            void resX (double reX) 
00968            { resX_ = reX; }
00969 
00971 
00972            void resY (double reY) 
00973            { resY_ = reY; }
00974 
00976            TeGeomRep elemType() const { return TeCELLS; }
00977 
00978 };
00979 
00980 
00984 TL_DLL TePointSet makePointSet( const TeLinearRing& lr );
00985 
00986 
00990 TL_DLL TePointSet makePointSet( const TePolygon& p );
00991 
00992 #endif
00993 
00994 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines