aimsdata  5.0.5
Neuroimaging data handling
borderfiller.h
Go to the documentation of this file.
1 #ifndef AIMSDATA_BORDER_BORDERFILLER_H
2 #define AIMSDATA_BORDER_BORDERFILLER_H
3 
4 //--- aims -------------------------------------------------------------------
6 #include <aims/math/mathelem.h> // MathUtil
7 #include <aims/resampling/splineresampler.h> // mirrorCoeff
8 //--- cartodata --------------------------------------------------------------
9 #include <cartodata/volume/volume.h> // VolumeRef
10 //----------------------------------------------------------------------------
11 
12 namespace aims
13 {
14 
15  //==========================================================================
16  // BORDER FILLER
17  //==========================================================================
21  template <typename T>
23  {
24  public:
26  static void fillConstant( carto::VolumeRef<T> in, const T & value = 0,
27  bool unreadOnly = false );
35  static void fillMedian( carto::VolumeRef<T> in,
36  Point4dl size = Point4dl(-1,-1,-1,-1),
37  bool unreadOnly = false );
39  static void fillNearest( carto::VolumeRef<T> in, bool unreadOnly = false );
41  static void fillMirror( carto::VolumeRef<T> in, bool unreadOnly = false );
46  };
47 
48  template <typename T>
50  bool unreadOnly )
51  {
53  if (unreadOnly)
55  else
56  vol = in;
57 
58  BorderIterator<T> brd( vol );
59  typename BorderIterator<T>::iterator i;
60 
61  for( i = brd.begin(); i != brd.end(); ++i )
62  *i = value;
63  }
64 
65  template <typename T>
67  bool unreadOnly )
68  {
70  if (unreadOnly)
72  else
73  vol = in;
74 
75  std::vector<int> bs = vol.getBorders();
76  if( size[0] < 0 )
77  size[0] = ( bs[0] >= bs[1] ? bs[0] : bs[1] );
78  if( size[1] < 0 )
79  size[1] = ( bs[2] >= bs[3] ? bs[2] : bs[3] );
80  if( size[2] < 0 )
81  size[2] = ( bs[4] >= bs[5] ? bs[4] : bs[5] );
82  if( size[3] < 0 )
83  size[3] = ( bs[6] >= bs[7] ? bs[6] : bs[7] );
84  BorderIterator<T> brdin( vol, true, size );
85  T med = aims::MathUtil<T>::median( brdin.begin(), brdin.end() );
86 
87  BorderIterator<T> brdout( vol );
88  typename BorderIterator<T>::iterator i;
89  for( i = brdout.begin(); i != brdout.end(); ++i )
90  *i = med;
91  }
92 
93  template <typename T>
95  bool unreadOnly )
96  {
98  if (unreadOnly)
100  else
101  vol = in;
102 
103  BorderIterator<T> brd( vol );
104  typename BorderIterator<T>::iterator i;
105  Point4dl current;
106  for( i = brd.begin(); i != brd.end(); ++i )
107  {
108  current = i.coordinate();
109  if( current[0] < 0 )
110  current[0] = 0;
111  else if( current[0] >= vol.getSizeX() )
112  current[0] = vol.getSizeX() - 1;
113  if( current[1] < 0 )
114  current[1] = 0;
115  else if( current[1] >= vol.getSizeX() )
116  current[1] = vol.getSizeX() - 1;
117  if( current[2] < 0 )
118  current[2] = 0;
119  else if( current[2] >= vol.getSizeX() )
120  current[2] = vol.getSizeX() - 1;
121  if( current[3] < 0 )
122  current[3] = 0;
123  else if( current[3] >= vol.getSizeX() )
124  current[3] = vol.getSizeX() - 1;
125  *i = vol( current[0], current[1], current[2], current[3] );
126  }
127  }
128 
129 
130  template <typename T>
132  bool unreadOnly )
133  {
135  if (unreadOnly)
136  vol = buildUnfilledBorderView(in);
137  else
138  vol = in;
139 
140  BorderIterator<T> brd( vol );
141  typename BorderIterator<T>::iterator i;
142  Point4dl current;
143  for( i = brd.begin(); i != brd.end(); ++i )
144  {
145  current = i.coordinate();
146  current[0] = mirrorCoeff( current[0], vol.getSizeX() );
147  current[1] = mirrorCoeff( current[1], vol.getSizeY() );
148  current[2] = mirrorCoeff( current[2], vol.getSizeZ() );
149  current[3] = mirrorCoeff( current[3], vol.getSizeT() );
150  *i = vol( current[0], current[1], current[2], current[3] );
151  }
152  }
153 
154  template<typename T>
156  const carto::VolumeRef<T> & vol)
157  {
158  if (!vol.isNull()) {
159  const carto::VolumeRef<T> parent1 = vol.refVolume();
160  if (!parent1.isNull()
161  && parent1.allocatorContext().isAllocated()) {
162  const carto::VolumeRef<T> parent2 = parent1.refVolume();
163  if (!parent2.isNull()
164  && !parent2.allocatorContext().isAllocated()) {
165  typedef typename carto::Volume<T>::Position Position;
166  Position pos = vol.posInRefVolume().toVector();
167  Position pos1 = parent1.posInRefVolume().toVector();
168  Position size = vol.getSize();
169  Position size1 = parent1.getSize();
170  Position size2 = parent2.getSize();
171 
172  size_t dims = pos.size();
173 
174  for(size_t d=0; d<dims; ++d) {
175  pos[d] =std::max(pos1[d], 0) - pos1[d];
176  size[d] = std::min(size1[d], size2[d] - pos1[d]) - pos[d];
177  }
178 
179  return carto::VolumeRef<T>(vol.refVolume(), pos, size);
180  }
181  }
182  }
183 
184  return vol;
185 }
186 
187 } // namespace aims
188 
189 #endif // AIMSDATA_BORDER_BORDERFILLER_H
static void fillNearest(carto::VolumeRef< T > in, bool unreadOnly=false)
Each voxel of the border is assigned with the nearest inside voxel.
Definition: borderfiller.h:94
int getSizeX() const
int getSizeY() const
int getSizeZ() const
This class contains static methods to fill the border of a VolumeRef with chosen values.
Definition: borderfiller.h:22
int getSizeT() const
The class for EcatSino data write operation.
Definition: border.h:44
static carto::VolumeRef< T > buildUnfilledBorderView(const carto::VolumeRef< T > &vol)
Build a view that exposes data filled at reading time.
Definition: borderfiller.h:155
bool isNull() const
static void fillMirror(carto::VolumeRef< T > in, bool unreadOnly=false)
The border is filled by mirroring the inside border of same size.
Definition: borderfiller.h:131
static void fillMedian(carto::VolumeRef< T > in, Point4dl size=Point4dl(-1,-1,-1,-1), bool unreadOnly=false)
Fills the border with a "median" value.
Definition: borderfiller.h:66
Point4dl coordinate() const
Returns the coordinates of the current point in the linked volume.
const Position4Di posInRefVolume() const
std::vector< int > getSize() const
Represents the border of a volume.
rc_ptr< Volume< T > > refVolume() const
static T median(Iterator b, Iterator e, T default_value=(T) 0)
Definition: mathelem.h:143
const AllocatorContext & allocatorContext() const
std::vector< int > getBorders() const
std::vector< int > Position
static void fillConstant(carto::VolumeRef< T > in, const T &value=0, bool unreadOnly=false)
Fills the border with a constant value.
Definition: borderfiller.h:49