bioprocessing  5.1.2
coustyflowmap.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef BIOPROCESSING_WATERSHED_COUSTYFLOWMAP
12 #define BIOPROCESSING_WATERSHED_COUSTYFLOWMAP
13 
14 //--- bioprocessing ----------------------------------------------------------
15 #include <bioprocessing/graph/pointvertex.h> // bio::PointVertexRef
16 //--- aims -------------------------------------------------------------------
17 #include <aims/vector/vector.h> // Point*
18 //--- cartodata --------------------------------------------------------------
19 #include <cartodata/volume/volume.h> // carto::VolumeRef
20 //--- cartobase --------------------------------------------------------------
21 #include <cartobase/smart/rcptr.h> // carto::rc_ptr
22 #include <cartobase/smart/refwrapper.h> // carto::reference_wrapper
23 //--- std --------------------------------------------------------------------
24 #include <map> // std::map
25 #include <limits> // std::numeric_limits
26 #include <utility> // std::pair
27 //----------------------------------------------------------------------------
28 
29 namespace bio {
30  template <typename V, typename L> class CoustyFlowMap;
31  template <typename V, typename L> class CoustyFlowMapRef;
32 
33  //==========================================================================
34  // FLOW MAP
35  //==========================================================================
47  template <typename V, typename L>
48  class CoustyFlowMap: public std::map<V,L>, public carto::RCObject
49  {
50  };
51 
52  //==========================================================================
53  // FLOW MAP: POINT VERTEX SPECIALIZATION
54  //==========================================================================
57  template <typename L>
58  class CoustyFlowMap<PointVertexRef<Point4dl>,L>
59  {
60  //--- typedef ------------------------------------------------------------
61  public:
63 
64  //--- constructor --------------------------------------------------------
65  public:
67  CoustyFlowMap(): _volume(), _minima() {}
69 
70  //--- iterator -----------------------------------------------------------
71  public:
72  class const_iterator;
73  class iterator;
74  const_iterator begin() const { return const_iterator(_volume,false); }
75  const_iterator end() const { return const_iterator(_volume,true); }
76  iterator begin() { return iterator(_volume,false); }
77  iterator end() { return iterator(_volume,true); }
78 
79  //--- interface ----------------------------------------------------------
80  public:
84  template <typename Iterator>
85  void init( Iterator b, Iterator e )
86  {
87  int j = 0;
88  Iterator i;
89  for( i = b; i != e; ++i )
90  {
91  const typename Iterator::Point & p = i->point();
92  _volume( p[0], p[1], p[2], p[3] ) = labelNone();
93  }
94  }
96  L labelNone() const
97  {
98  return std::numeric_limits<L>::max();
99  }
101  L labelMinus() const
102  {
103  if( std::numeric_limits<L>::min() < 0 )
104  return -1;
105  else
106  return std::numeric_limits<L>::max() - 1;
107  }
109  L label( Vertex v ) const
110  {
111  const typename Vertex::Point & p = v.point();
112  return _volume( p[0], p[1], p[2], p[3] );
113  }
115  void setLabel( Vertex v, L lab )
116  {
117  const typename Vertex::Point & p = v.point();
118  _volume( p[0], p[1], p[2], p[3] ) = lab;
119  }
120  void setMinimum( Vertex v )
121  {
122  const typename Vertex::Point & p = v.point();
123  _minima( p[0], p[1], p[2], p[3] ) = 1;
124  }
126  carto::VolumeRef<L> volume()
127  {
128  return _volume;
129  }
131  size_t size() const { return (size_t) _volume.getSizeX() *
132  (size_t) _volume.getSizeY() *
133  (size_t) _volume.getSizeZ() *
134  (size_t) _volume.getSizeT(); }
135  carto::VolumeRef<L> getMinima()
136  {
137  return _minima;
138  }
139 
140  //--- volume -------------------------------------------------------------
141  public:
143  void setVolume( carto::VolumeRef<L> in )
144  {
145  _volume = in;
146  _minima->reallocate( in.getSizeX(), in.getSizeY(), in.getSizeZ(), in.getSizeT() );
147  }
149  void setDimensions( int sx = 1, int sy = 1, int sz = 1, int st = 1 )
150  {
151  _volume->reallocate( sx, sy, sz, st );
152  _minima->reallocate( sx, sy, sz, st );
153  }
155  void setHeader( const carto::PropertySet & h )
156  {
157  _volume->copyHeaderFrom( h );
158  _minima->copyHeaderFrom( h );
159  }
160 
161  //--- members ------------------------------------------------------------
162  protected:
163  carto::VolumeRef<L> _volume;
164  carto::VolumeRef<L> _minima;
165  };
166 
167 
168  //==========================================================================
169  // FLOW MAP POINT: ITERATORS
170  //==========================================================================
171 
172  template <typename L>
173  class CoustyFlowMap<PointVertexRef<Point4dl>,L>::const_iterator
174  {
175  public:
177  typedef typename Owner::Vertex Vertex;
178  typedef std::pair<Vertex,carto::reference_wrapper<L> > IterType;
179 
180  protected:
181  L & noneref() { return *( (L*) this ); }
182  const_iterator( carto::VolumeRef<L> in, bool end = false ):
183  _current(Vertex(),carto::wrap::ref(noneref())),
184  _volume(in),
185  _dimensions(in.getSizeX(),in.getSizeY(),in.getSizeZ(),in.getSizeT())
186  {
187  if( end )
188  toEnd();
189  else
190  toBegin();
191  }
192  public:
193  const_iterator(): _current(Vertex(),carto::wrap::ref(noneref())) {}
194  const_iterator( const const_iterator & other ): _current(other._current) {}
195  const_iterator( const iterator & other ): _current(other._current) {}
196 
197 
198  const_iterator & operator=( const const_iterator & other )
199  {
200  _current = other._current;
201  _dimensions = other._dimensions;
202  _volume = other._volume;
203  return *this;
204  }
205  const_iterator & operator=( const iterator & other )
206  {
207  _current = other._current;
208  _dimensions = other._dimensions;
209  _volume = other._volume;
210  return *this;
211  }
212  const_iterator & operator++()
213  {
214  if( _current.first.point()[0] < _dimensions[0]-1 )
215  ++( _current.first.point()[0] );
216  else
217  {
218  _current.first.point()[0] = 0;
219  if( _current.first.point()[1] < _dimensions[1]-1 )
220  ++( _current.first.point()[1] );
221  else
222  {
223  _current.first.point()[1] = 0;
224  if( _current.first.point()[2] < _dimensions[2]-1 )
225  ++( _current.first.point()[2] );
226  else
227  {
228  _current.first.point()[2] = 0;
229  if( _current.first.point()[3] < _dimensions[3]-1 )
230  ++( _current.first.point()[3] );
231  else
232  toEnd();
233  }
234  }
235  }
236  computeLabel();
237  return *this;
238  }
239  const_iterator operator++(int)
240  {
241  const_iterator prev = *this;
242  ++(*this);
243  return prev;
244  }
245  const IterType & operator*() const { return _current; }
246  const IterType * operator->() const { return &_current; }
247  bool operator==( const const_iterator & other ) const
248  {
249  return other._current.first == _current.first;
250  }
251  bool operator==( const iterator & other ) const { return other == *this; }
252  bool operator!=( const const_iterator & other ) const { return !( other == *this ); }
253  bool operator!=( const iterator & other ) const { return !( other == *this ); }
254 
255  protected:
256  void toBegin() { _current.first = Vertex(Point4dl(0,0,0,0)); computeLabel(); }
257  void toEnd() { _current.first = Vertex(_dimensions); computeLabel(); }
259  {
260  if( _current.first.point() != _dimensions )
261  _current.second = carto::wrap::ref(
262  _volume( _current.first.point()[0], _current.first.point()[1],
263  _current.first.point()[2], _current.first.point()[3] ) );
264  else
265  _current.second = carto::wrap::ref(noneref());
266  }
268  Point4dl _dimensions;
269  carto::VolumeRef<L> _volume;
270 
271  friend class CoustyFlowMap<Vertex, L>;
272  };
273 
274  template <typename L>
275  class CoustyFlowMap<PointVertexRef<Point4dl>,L>::iterator:
276  public CoustyFlowMap<PointVertexRef<Point4dl>,L>::const_iterator
277  {
278  protected:
279  iterator( carto::VolumeRef<L> in, bool end = false ): const_iterator(in,end) {}
280  public:
281  iterator(): const_iterator() {}
282  iterator( const iterator & other ): const_iterator(other) {}
283 
285  typedef typename Owner::Vertex Vertex;
286  typedef std::pair<Vertex,carto::reference_wrapper<L> > IterType;
287 
288  iterator & operator=( const iterator & other ) { const_iterator::operator=(other); }
289  iterator & operator++() { const_iterator::operator++(); return *this; }
290  iterator operator++(int)
291  {
292  iterator prev = *this;
293  ++(*this);
294  return prev;
295  }
296  IterType & operator*() { return this->_current; }
297  IterType * operator->() { return &(this->_current); }
298 
299  friend class CoustyFlowMap<Vertex,L>;
300  };
301 
302  //==========================================================================
303  // FLOW MAP REF
304  //==========================================================================
309  template <typename V, typename L>
310  class CoustyFlowMapRef: public carto::rc_ptr<CoustyFlowMap<V,L> >
311  {
312  };
313 
314  //==========================================================================
315  // FLOW MAP POINT REF
316  //==========================================================================
321  template <typename L>
322  class CoustyFlowMapRef<PointVertexRef<Point4dl>,L>:
323  public carto::rc_ptr<CoustyFlowMap<PointVertexRef<Point4dl>,L> >
324  {
325  //--- typedef ------------------------------------------------------------
326  protected:
327  typedef carto::rc_ptr<CoustyFlowMap<PointVertexRef<Point4dl>,L> > Base;
329  public:
330  typedef typename Pointed::Vertex Vertex;
331 
332  //--- constructor --------------------------------------------------------
333  public:
336 
337  //--- iterator -----------------------------------------------------------
338  public:
339  typedef typename Pointed::const_iterator const_iterator;
340  typedef typename Pointed::iterator iterator;
341  const_iterator begin() const { return (*this)->begin(); }
342  const_iterator end() const { return (*this)->end(); }
343  iterator begin() { return (*this)->begin(); }
344  iterator end() { return (*this)->end(); }
345 
346  //--- interface ----------------------------------------------------------
347  public:
348  template <typename Iterator>
349  void init( Iterator b, Iterator e ) { return (*this)->init(b,e); }
350  L labelNone() const { return (*this)->labelNone(); }
351  L labelMinus() const { return (*this)->labelMinus(); }
352  L label( Vertex v ) const { return (*this)->label(v); }
353  void setLabel( Vertex v, L lab ) { return (*this)->setLabel(v,lab); }
354  void setMinimum( Vertex v ) { return (*this)->setMinimum(v); }
355  carto::VolumeRef<L> volume() { return (*this)->volume(); }
356  size_t size() const { return (*this)->size(); }
357  carto::VolumeRef<L> getMinima() { return (*this)->getMinima(); }
358  // void setSeeds( aims::BucketMap<Void> & seeds ) { return (*this)->setSeeds(seeds); }
359 
360 
361  //--- volume -------------------------------------------------------------
362  public:
363  void setVolume( carto::VolumeRef<L> in )
364  { return (*this)->setVolume(in); }
365  void setDimensions( int sx = 1, int sy = 1, int sz = 1, int st = 1 )
366  { return (*this)->setDimensions(sx,sy,sz,st); }
367  void setHeader( const carto::PropertySet & h )
368  { return (*this)->setHeader(h); }
369  };
370 
371 } // namespace bio
372 
373 #endif // BIOPROCESSING_WATERSHED_COUSTYFLOWMAP
carto::rc_ptr< CoustyFlowMap< PointVertexRef< Point4dl >, L > > Base
void setDimensions(int sx=1, int sy=1, int sz=1, int st=1)
CoustyFlowMap< PointVertexRef< Point4dl >, L > Pointed
Reference counting pointer to a CoustyFlowMap.
std::pair< Vertex, carto::reference_wrapper< L > > IterType
const_iterator & operator=(const const_iterator &other)
CoustyFlowMap< PointVertexRef< Point4dl >, L > Owner
const_iterator(carto::VolumeRef< L > in, bool end=false)
iterator(carto::VolumeRef< L > in, bool end=false)
std::pair< Vertex, carto::reference_wrapper< L > > IterType
CoustyFlowMap< PointVertexRef< Point4dl >, L > Owner
Flow map specialization for PointVertexRef<Point4dl> (as used by CoustyWatershed)
Definition: coustyflowmap.h:59
PointVertexRef< Point4dl > Vertex
Usable vertex type.
Definition: coustyflowmap.h:62
void setHeader(const carto::PropertySet &h)
Set the underlying volume's header.
size_t size() const
Get the number of voxels/vertices.
void setLabel(Vertex v, L lab)
Set a value ti vertex v.
void setDimensions(int sx=1, int sy=1, int sz=1, int st=1)
Set the underlying volume's dimensions.
L labelMinus() const
Minimumvalue (see CoustyWatershed and Cousty et al 2007)
const_iterator begin() const
iterator pointing to a pair<PointVertexRef,L>
Definition: coustyflowmap.h:74
L label(Vertex v) const
Get the label of vertex v.
CoustyFlowMap()
Default constructor: builds an empty volume.
Definition: coustyflowmap.h:67
void setVolume(carto::VolumeRef< L > in)
Set the underlying volume.
L labelNone() const
None value (see CoustyWatershed and Cousty et al 2007)
Definition: coustyflowmap.h:96
carto::VolumeRef< L > volume()
Get the underlying volume.
void init(Iterator b, Iterator e)
FlowMap initialization For all given vertices, the none value (see labelNone()) is given to the map.
Definition: coustyflowmap.h:85
A flow map is a mapping of the vertices of a graph to a value.
Definition: coustyflowmap.h:49
Reference counting pointer to a PointVertex.
Definition: pointvertex.h:139
Pointed::Point Point
Point.
Definition: pointvertex.h:147
const Point & point() const
constant accessor to the coordinates
Definition: pointvertex.h:217