DGtal 0.3.0

ImageContainerBySTLVector.h

Go to the documentation of this file.
00001 
00017 #pragma once
00018 
00031 #if defined(ImageContainerBySTLVector_RECURSES)
00032 #error Recursive header files inclusion detected in ImageContainerBySTLVector.h
00033 #else // defined(ImageContainerBySTLVector_RECURSES)
00034 
00035 #define ImageContainerBySTLVector_RECURSES
00036 
00037 #if !defined ImageContainerBySTLVector_h
00038 
00039 #define ImageContainerBySTLVector_h
00040 
00042 // Inclusions
00043 #include <iostream>
00044 #include <vector>
00045 #include "DGtal/base/Common.h"
00046 #include "DGtal/kernel/images/CValue.h"
00047 #include "DGtal/kernel/domains/CBoundedDomain.h"
00048 #include "DGtal/kernel/IntegerTraits.h"
00049 #include "DGtal/io/DGtalBoard.h"
00050 
00052 
00053 namespace DGtal
00054 {
00055 
00057   // class ImageContainerBySTLVector
00058 
00070   template <typename TDomain, typename TValue>
00071   class ImageContainerBySTLVector: public vector<TValue>
00072   {
00073   public:
00074 
00075     BOOST_CONCEPT_ASSERT(( CValue<TValue> ));
00076     BOOST_CONCEPT_ASSERT(( CBoundedDomain<TDomain> ));
00077                         
00078     typedef TValue Value;
00079     typedef TDomain Domain;
00080 
00081     // static constants
00082     static const typename Domain::Dimension dimension = Domain::dimension;
00083     
00084     typedef typename Domain::Point Point;
00085     typedef typename Domain::Vector Vector;
00086     typedef typename Domain::Dimension Dimension;
00087     typedef typename Domain::Integer Integer;
00088     typedef typename Domain::Size Size;
00089     typedef typename vector<Value>::iterator Iterator;
00090     typedef typename vector<Value>::const_iterator ConstIterator;
00091 
00092     ImageContainerBySTLVector(const Point &aPointA,
00093                               const Point &aPointB );
00094 
00095     ~ImageContainerBySTLVector();
00096 
00103     Value operator()(const Point &aPoint) const;
00104 
00112     Value operator()(ConstIterator &it) const
00113     {
00114       return (*it);
00115     };
00116 
00117 
00125     Value operator()(Iterator &it) const
00126     {
00127       return (*it);
00128     };
00129 
00136     void setValue(const Point &aPoint, const Value &aValue);
00137 
00144     void setValue(Iterator &it, const Value &aValue)
00145     {
00146       (*it) = aValue;
00147     }
00148 
00153     void selfDisplay ( std::ostream & out ) const;
00154 
00155 
00159     bool isValid() const
00160     {
00161       return (this != NULL);
00162     }
00163 
00169     Vector extent() const;
00170 
00174     Point lowerBound() const
00175     {
00176       return myLowerBound;
00177     };
00178 
00182     Point upperBound() const
00183     {
00184       return myUpperBound;
00185     };
00186 
00190     Domain domain() const
00191     {
00192       return Domain(myLowerBound, myUpperBound);
00193     }
00194     
00196 
00202     class SpanIterator
00203     {
00204 
00205       friend class ImageContainerBySTLVector<Domain, Value>;
00206 
00207     public:
00208 
00209       typedef std::bidirectional_iterator_tag iterator_category; 
00210       typedef Value value_type;
00211       typedef ptrdiff_t difference_type;
00212       typedef Value* pointer;
00213       typedef Value& reference;
00214 
00222       SpanIterator( const Point & p ,
00223                     const Dimension aDim ,
00224                     ImageContainerBySTLVector<Domain, Value> *aMap ) :  myMap ( aMap ), myDimension ( aDim )
00225       {
00226         myPos = aMap->linearized(p);
00227 
00228         //We compute the myShift quantity
00229         myShift = 1;
00230         for (unsigned int k = 0; k < myDimension  ; k++)
00231           myShift *= (aMap->myUpperBound.at(k) - aMap->myLowerBound.at(k) + 1);
00232         
00233         //trace.warning() <<"SpanIter("<<p<<" "<<aDim<<") Pos="<<myPos<<" shift="<<myShift<<std::endl;
00234       }
00235 
00236 
00242       inline 
00243       void setValue(const Value aVal)
00244       {
00245         (*myMap)[ myPos ] = aVal;
00246       }
00247 
00253       inline
00254       const Value & operator*() 
00255       {
00256         return (*myMap)[ myPos ];
00257       }
00258 
00264       inline
00265       bool operator== ( const SpanIterator &it ) const
00266       {
00267         return ( myPos == it.myPos );
00268       }
00269 
00275       inline
00276       bool operator!= ( const SpanIterator &it ) const
00277       {
00278         return ( myPos != it.myPos );
00279       }
00280 
00285       inline
00286       void next()
00287       {
00288         myPos += myShift;
00289       }
00290 
00295       inline
00296       void prev()
00297       {
00298         ASSERT((long int) myPos - myShift > 0);
00299         myPos -= myShift;
00300       }
00301 
00306       inline
00307       SpanIterator &operator++()
00308       {
00309         this->next();
00310         return *this;
00311       }
00312 
00317       inline
00318       SpanIterator &operator++ ( int )
00319       {
00320         SpanIterator tmp = *this;
00321         ++*this;
00322         return tmp;
00323       }
00324 
00329       inline
00330       SpanIterator &operator--()
00331       {
00332         this->prev();
00333         return *this;
00334       }
00335 
00340       inline
00341       SpanIterator &operator-- ( int )
00342       {
00343         SpanIterator tmp = *this;
00344         --*this;
00345         return tmp;
00346       }
00347 
00348     private:
00350       Size myPos;
00351 
00353       ImageContainerBySTLVector<Domain, Value> *myMap;
00354 
00356       Dimension  myDimension;
00357 
00359       Size myShift;
00360 
00361     };
00362 
00369     void setValue(SpanIterator &it, const Value &aValue)
00370     {
00371       it.setValue(aValue);
00372     }
00373 
00374 
00384     SpanIterator spanBegin(const Point &aPoint, const Dimension aDimension)
00385     {
00386       return SpanIterator ( aPoint, aDimension, this);
00387     }
00388 
00399     SpanIterator spanEnd(const Point &aPoint, const Dimension aDimension)
00400     {
00401       Point tmp = aPoint;
00402       tmp.at( aDimension ) = myUpperBound.at( aDimension ) + 1;
00403       return SpanIterator( tmp, aDimension, this);
00404     }
00405 
00412     Value operator()(SpanIterator &it)
00413     {
00414       return (*it);
00415     };
00416 
00417 
00418 
00419   private:
00420 
00426     Size linearized(const Point &aPoint) const;
00427 
00428     Point myLowerBound;
00429     Point myUpperBound;
00430 
00431     // ------------- realization CDrawableWithDGtalBoard --------------------
00432   private:
00433 
00437     struct DefaultDrawStyle : public DrawableWithDGtalBoard
00438     {
00439       virtual void selfDraw( DGtalBoard & aBoard ) const
00440       {
00441         aBoard.setPenColorRGBi(60, 60, 60);
00442         aBoard.setLineStyle(DGtalBoard::Shape::SolidStyle);
00443       }
00444     };
00445 
00446   public:
00447 
00452     DrawableWithDGtalBoard* defaultStyle() const;
00453 
00457     std::string styleName() const;
00458 
00459 
00467     template<typename Colormap>
00468     void selfDraw(DGtalBoard & board, const Value & minValue, const Value & maxValue ) const;
00469 
00470   };
00471 
00478   template <typename Domain, typename V>
00479   inline
00480   std::ostream&
00481   operator<< ( std::ostream & out, const ImageContainerBySTLVector<Domain, V> & object )
00482   {
00483     object.selfDisplay ( out );
00484     return out;
00485   }
00486 
00487 } // namespace DGtal
00488 
00489 
00491 // Includes inline functions
00492 #include "DGtal/kernel/images/ImageContainerBySTLVector.ih"
00493 
00494 //                                                                           //
00496 
00497 #endif // !defined ImageContainerBySTLVector_h
00498 
00499 #undef ImageContainerBySTLVector_RECURSES
00500 #endif // else defined(ImageContainerBySTLVector_RECURSES)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines