00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef __TERRALIB_INTERNAL_GEOMETRY_H
00028 #define __TERRALIB_INTERNAL_GEOMETRY_H
00029
00030 #if defined(_MSC_VER)
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
00048
00049
00050
00051
00052
00053
00054
00055
00056 class TL_DLL TeGeometry
00057 {
00058 public:
00059
00060
00061 TeGeometry(): box_ ( TeMAXFLOAT, TeMAXFLOAT, -TeMAXFLOAT, -TeMAXFLOAT ),
00062 geomId_ ( 0 ), objectId_ (""), srid_(-1) {}
00063
00064
00065 TeGeometry ( const TeGeometry& other )
00066 {
00067 box_ = other.box_;
00068 geomId_ = other.geomId_;
00069 objectId_ = other.objectId_;
00070 srid_ = other.srid_;
00071 }
00072
00073
00074 virtual ~TeGeometry() {}
00075
00076
00077 void setBox ( const TeBox & box )
00078 { box_ = box; }
00079
00080
00081 const TeBox& box () const
00082 { return box_; }
00083
00084
00085 TeBox& box ()
00086 { return box_; }
00087
00088
00089 int geomId() const
00090 { return geomId_; }
00091
00092
00093 void geomId( int id )
00094 { geomId_ = id; }
00095
00096
00097 virtual string objectId() const
00098 { return objectId_; }
00099
00100
00101 virtual void objectId ( const string& id )
00102 { objectId_ = id; }
00103
00104
00105 virtual int srid() const
00106 { return srid_; }
00107
00108
00109 virtual void srid(const int& srid)
00110 { srid_ = srid; }
00111
00112
00113 virtual unsigned int size() const
00114 { return 0; }
00115
00116
00117 ostream& operator<<(ostream& os)
00118 {
00119 os << Te2String(geomId_);
00120 return os;
00121 }
00122
00123
00124 virtual bool isRing() const
00125 { return false; }
00126
00127
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
00140
00141
00142
00143
00144 class TL_DLL TeGeometryNone: public TeGeometry
00145 {
00146 public:
00147
00148 TeGeomRep elemType() const { return TeGEOMETRYNONE; }
00149
00150
00151 void clear () { return; }
00152 };
00153
00154
00155 class TL_DLL TeVector : public TeGeometry
00156 {
00157 };
00158
00159
00160
00161
00162
00163 template <class T>
00164 class TeGeomSingle : public TeVector
00165 {
00166 public:
00167
00168
00169 typedef T value_type;
00170
00171
00172 TeGeomSingle<T>() {}
00173
00174
00175 TeGeomSingle<T> (const T& elem ): elem_ ( elem )
00176 { updateBox ( box_, elem ); }
00177
00178
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
00187
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
00197
00198 virtual ~TeGeomSingle<T>() {}
00199
00200
00201 void add ( T& elem )
00202 {
00203 elem_ = elem;
00204 box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT);
00205 updateBox ( box_, elem );
00206 }
00207
00208
00209 T& location ()
00210 { return elem_; }
00211
00212
00213 const T& location () const
00214 { return elem_; }
00215
00216
00217 T& elem ()
00218 { return elem_; }
00219
00220
00221 const T& elem () const
00222 { return elem_; }
00223
00224
00225 T& operator [] ( int )
00226 { return elem_; }
00227
00228
00229 bool operator== (const TeGeomSingle& other) const
00230 { return elem_ == other.elem(); }
00231
00232
00233 int size() { return 1; }
00234
00235 protected:
00236 T elem_;
00237
00238 };
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 template <class T>
00253 class TeGeomComposite: public TeVector
00254 {
00255 public:
00256
00257
00258 TeGeomComposite()
00259 {
00260 pImpl_ = new TeComposite<T>;
00261 pImpl_->attach();
00262 }
00263
00264
00265 virtual ~TeGeomComposite()
00266 { pImpl_->detach(); }
00267
00268
00269
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
00279
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
00294
00295 virtual string objectId() const
00296 { return objectId_; }
00297
00298
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
00310
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
00320
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
00333
00334 void add ( const T& elem )
00335 {
00336 pImpl_->add ( elem );
00337 updateBox ( box_, elem );
00338 }
00339
00340
00341 bool erase ( int i )
00342 {
00343 bool status = pImpl_->erase (i);
00344
00345 if (status)
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
00354
00355 bool erase ( T& object )
00356 {
00357 bool status = pImpl_->erase ( object );
00358 if (status)
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
00367
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
00377
00378 void clear ()
00379 {
00380 pImpl_->clear ();
00381 box_ = TeBox(TeMAXFLOAT, TeMAXFLOAT,-TeMAXFLOAT,-TeMAXFLOAT);
00382 }
00383
00384
00385 unsigned int size() const
00386 { return ( (unsigned int) pImpl_->size() ); }
00387
00388
00389 void reserve(int nelem)
00390 { pImpl_->reserve(nelem); }
00391
00392
00393 T& operator [] ( int i ) const
00394 { return pImpl_->operator[] ( i ); }
00395
00396
00397 T& first() const
00398 { return pImpl_->operator[] ( 0 ); }
00399
00400
00401 T& last() const
00402 { return pImpl_->operator[] ( pImpl_->size()-1 ); }
00403
00404
00405 bool empty () const
00406 { return pImpl_->empty (); }
00407
00408
00409 typedef typename TeComposite<T>::iterator iterator;
00410
00411
00412
00413 typedef T value_type;
00414
00415
00416 typename TeComposite<T>::iterator begin()
00417 { return pImpl_->begin(); }
00418
00419
00420 typename TeComposite<T>::iterator const begin() const
00421 { return pImpl_->begin(); }
00422
00423
00424 typename TeComposite<T>::iterator end()
00425 { return pImpl_->end(); }
00426
00427
00428 typename TeComposite<T>::iterator const end() const
00429 { return pImpl_->end(); }
00430
00431
00432 typedef typename TeComposite<T>::reverse_iterator reverse_iterator;
00433
00434
00435 typename TeComposite<T>::reverse_iterator rbegin()
00436 { return pImpl_->rbegin(); }
00437
00438
00439 typename TeComposite<T>::reverse_iterator rend()
00440 { return pImpl_->rend(); }
00441
00442 protected:
00443
00444
00445 TeComposite<T> * pImpl_;
00446 };
00447
00448
00449
00450
00451
00452 class TL_DLL TeLine2D : public TeGeomComposite<TeCoord2D>
00453 {
00454 public:
00455
00456 bool isRing() const;
00457
00458
00459 string objectId() const
00460 { return objectId_; }
00461
00462
00463 void objectId (const string& id )
00464 { objectId_ = id; }
00465
00466
00467 TeGeomRep elemType() const { return TeLINES; }
00468 };
00469
00470
00471
00472
00473
00474 class TL_DLL TeLineSet: public TeGeomComposite<TeLine2D>
00475 {
00476 public:
00477
00478 TeGeomRep elemType() const { return TeLINES; }
00479
00480
00481 void copyElements (const TeLineSet& other );
00482 };
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 class TL_DLL TeLinearRing : public TeLine2D {
00493 public:
00494
00495
00496 TeLinearRing() : TeLine2D() {}
00497
00498
00499 TeLinearRing ( TeLine2D& line );
00500 };
00501
00502
00503
00504
00505
00506
00507
00508 class TL_DLL TePolygon: public TeGeomComposite<TeLinearRing>
00509 {
00510 public:
00511
00512
00513 TeGeomRep elemType() const { return TePOLYGONS; }
00514
00515
00516 void copyElements ( const TePolygon& other );
00517 };
00518
00519
00520 class TL_DLL TePolygonSet: public TeGeomComposite<TePolygon>
00521 {
00522 public:
00523
00524 TeGeomRep elemType() const { return TePOLYGONS; }
00525
00526
00527 void copyElements ( const TePolygonSet& other );
00528 };
00529
00530
00531
00532 class TL_DLL TePoint : public TeGeomSingle<TeCoord2D>
00533 {
00534 public:
00535
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));
00541 }
00542
00543
00544 TePoint(const TeCoord2D& c):
00545 TeGeomSingle<TeCoord2D> ( )
00546 {
00547 elem_ = c;
00548 setBox(TeBox(c.x(),c.y(),c.x(),c.y()));
00549 }
00550
00551
00552 TeGeomRep elemType() const { return TePOINTS; }
00553
00554
00555 string objectId() const
00556 { return objectId_; }
00557
00558
00559 void objectId (const string& id )
00560 { objectId_ = id; }
00561
00562 };
00563
00564
00565 class TL_DLL TePointSet: public TeGeomComposite<TePoint>
00566 {
00567 public:
00568
00569 TeGeomRep elemType() const { return TePOINTS; }
00570 };
00571
00572
00573 class TL_DLL TeText: public TeGeomSingle<TeCoord2D>
00574 {
00575 public:
00576
00577
00578
00579
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
00592
00593
00594
00595
00596
00597
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
00609
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
00623
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
00641
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
00654
00655 void setLocation(const TeCoord2D& l)
00656 { elem_ = l; setBox(TeBox(l,l)); }
00657
00658
00659 string textValue () const
00660 { return textValue_; }
00661
00662
00663 void setTextValue (const string &text)
00664 { textValue_ = text; }
00665
00666
00667 double angle () const
00668 { return angle_; }
00669
00670
00671 void setAngle (double angle)
00672 { angle_ = angle; }
00673
00674
00675 double height () const
00676 { return height_ ; }
00677
00678
00679 void setHeight (double height)
00680 { height_ = height; }
00681
00682
00683 double alignmentVert () const
00684 { return alignmentVert_ ; }
00685
00686
00687 void setAlignmentVert (double alig)
00688 { alignmentVert_ = alig; }
00689
00690
00691 double alignmentHoriz () const
00692 { return alignmentHoriz_ ; }
00693
00694
00695 void setAlignmentHoriz (double alig)
00696 { alignmentHoriz_ = alig; }
00697
00698
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
00710
00711 class TL_DLL TeTextSet : public TeGeomComposite<TeText>
00712 {
00713 public:
00714
00715 TeGeomRep elemType() const { return TeTEXT; }
00716
00717 };
00718
00719
00720
00721 class TL_DLL TeNode: public TeGeomSingle<TeCoord2D>
00722 {
00723 public:
00724
00725 bool operator== (const TeNode& node) const
00726 {
00727 TeCoord2D p1 = elem_;
00728 TeCoord2D p2 = node.elem_;
00729 return p1==p2;
00730 }
00731
00732
00733 ostream& operator<<(ostream& os)
00734 {
00735 os << Te2String(geomId_);
00736 return os;
00737 }
00738
00739
00740 TeGeomRep elemType() const { return TeNODES; }
00741
00742 };
00743
00744
00745 TL_DLL ostream& operator<<(ostream& os, TeNode& N);
00746
00747
00748 class TL_DLL TeNodeSet : public TeGeomComposite<TeNode>
00749
00750 {
00751 public:
00752
00753 TeGeomRep elemType() const { return TeNODES; }
00754
00755 };
00756
00757
00758 class TL_DLL TeArc : public TeVector
00759 {
00760 public:
00761
00762
00763 TeArc(): ifrom_ (-1), ito_ (-1){}
00764
00765
00766
00767
00768
00769
00770 TeArc(TeNode& from, TeNode& to)
00771 {
00772 from_ = from;
00773 to_ = to;
00774 updateBox ( box_, from );
00775 updateBox ( box_, to );
00776 }
00777
00778
00779
00780
00781
00782
00783 TeArc(int from, int to): ifrom_ (from), ito_ (to) {}
00784
00785
00786
00787
00788 TeNode& fromNode ()
00789 { return from_; }
00790
00791
00792 TeNode& toNode ()
00793 { return to_; }
00794
00795
00796 int fromId () const
00797 { return ifrom_; }
00798
00799
00800 void fromId (int i)
00801 { ifrom_ = i; }
00802
00803
00804 int toId () const
00805 { return ito_; }
00806
00807
00808 void toId (int i)
00809 { ito_ = i; }
00810
00811
00812 void setNodes (TeNode& from, TeNode& to)
00813 {
00814 from_ = from;
00815 to_ = to;
00816 updateBox ( box_, from );
00817 updateBox ( box_, to );
00818 }
00819
00820
00821 TeGeomRep elemType() const { return TeARCS; }
00822
00823
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
00841
00842 TL_DLL ostream& operator<<(ostream& os, const TeArc& N);
00843
00844
00845
00846 class TL_DLL TeArcSet: public TeGeomComposite <TeArc>
00847 {
00848 public:
00849
00850 TeGeomRep elemType() const { return TeARCS; }
00851 };
00852
00853
00854 class TL_DLL TeSample: public TeGeomSingle<TeCoord2D>, public TeMeasure
00855 {
00856 public:
00857
00858
00859
00860
00861
00862 TeSample ( const TeCoord2D& location, double measure = 0. ):
00863 TeGeomSingle<TeCoord2D> ( location ), TeMeasure ( measure ) {}
00864
00865 TeGeomRep elemType() const { return TeSAMPLES; }
00866 };
00867
00868
00869 class TL_DLL TeSampleSet: public TeGeomComposite<TeSample>
00870 {
00871 public:
00872
00873 TeGeomRep elemType() const { return TeSAMPLES; }
00874 };
00875
00876
00877 class TL_DLL TeContourLine: public TeLine2D, public TeMeasure
00878 {
00879 public:
00880
00881
00882
00883
00884
00885 TeContourLine ( TeLine2D& line, double measure = 0. )
00886 : TeLine2D ( line ), TeMeasure ( measure )
00887 {}
00888 };
00889
00890
00891
00892 class TL_DLL TeContourLineSet: public TeGeomComposite <TeContourLine>
00893 {
00894 public:
00895
00896 TeGeomRep elemType() const { return TeSAMPLES; }
00897 };
00898
00899
00900
00901 class TL_DLL TeCell : public TeVector
00902 {
00903 int column_;
00904 int line_;
00905
00906 public:
00907
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
00916
00917 int column () const
00918 { return column_; }
00919
00920
00921 void column (int column)
00922 { column_ = column; }
00923
00924
00925 int line () const
00926 { return line_; }
00927
00928
00929 void line (int line)
00930 { line_ = line; }
00931
00932
00933 TeGeomRep elemType() const { return TeCELLS; }
00934
00935
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
00946
00947 class TL_DLL TeCellSet: public TeGeomComposite<TeCell>
00948 {
00949 double resX_;
00950 double resY_;
00951
00952 public:
00953
00954
00955 TeCellSet() : resX_(0.), resY_(0.)
00956 {}
00957
00958
00959 double resX () const
00960 { return resX_; }
00961
00962
00963 double resY () const
00964 { return resY_; }
00965
00966
00967 void resX (double reX)
00968 { resX_ = reX; }
00969
00970
00971
00972 void resY (double reY)
00973 { resY_ = reY; }
00974
00975
00976 TeGeomRep elemType() const { return TeCELLS; }
00977
00978 };
00979
00980
00981
00982
00983
00984 TL_DLL TePointSet makePointSet( const TeLinearRing& lr );
00985
00986
00987
00988
00989
00990 TL_DLL TePointSet makePointSet( const TePolygon& p );
00991
00992 #endif
00993
00994