aimsdata 6.0.0
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
12namespace 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 );
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 );
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 );
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 );
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// std::cout << "buildUnfilledBorderView::unallocated parent found"
166// << std::endl;
167 typedef typename carto::Volume<T>::Position Position;
168 Position pos = vol.posInRefVolume().toVector();
169 Position pos1 = parent1.posInRefVolume().toVector();
170 Position size = vol.getSize();
171 Position size1 = parent1.getSize();
172 Position size2 = parent2.getSize();
173
174 size_t dims = pos.size();
175
176 for(size_t d=0; d<dims; ++d) {
177 pos[d] =std::max(pos1[d], 0) - pos1[d];
178 size[d] = std::min(size1[d], size2[d] - pos1[d]) - pos[d];
179 }
180
181// std::cout << "buildUnfilledBorderView::border view position ["
182// << pos[0] << ", " << pos[1] << ", " << pos[2] << "]"
183// << std::endl;
184// std::cout << "buildUnfilledBorderView::border view size ["
185// << size[0] << ", " << size[1] << ", " << size[2]
186// << "]" << std::endl;
187 return carto::VolumeRef<T>(vol.refVolume(), pos, size);
188 }
189 }
190 }
191
192 return vol;
193 }
194
195} // namespace aims
196
197#endif // AIMSDATA_BORDER_BORDERFILLER_H
This class contains static methods to fill the border of a VolumeRef with chosen values.
static void fillNearest(carto::VolumeRef< T > in, bool unreadOnly=false)
Each voxel of the border is assigned with the nearest inside voxel.
static void fillConstant(carto::VolumeRef< T > in, const T &value=0, bool unreadOnly=false)
Fills the border with a constant value.
static carto::VolumeRef< T > buildUnfilledBorderView(const carto::VolumeRef< T > &vol)
Build a view that exposes data filled at reading time.
static void fillMedian(carto::VolumeRef< T > in, Point4dl size=Point4dl(-1,-1,-1,-1), bool unreadOnly=false)
Fills the border with a "median" value.
static void fillMirror(carto::VolumeRef< T > in, bool unreadOnly=false)
The border is filled by mirroring the inside border of same size.
Point4dl coordinate() const
Returns the coordinates of the current point in the linked volume.
Represents the border of a volume.
static T median(Iterator b, Iterator e, T default_value=(T) 0)
Definition mathelem.h:143
int getSizeZ() const
std::vector< int > getBorders() const
int getSizeT() const
rc_ptr< Volume< T > > refVolume() const
std::vector< int > getSize() const
int getSizeY() const
int getSizeX() const
const AllocatorContext & allocatorContext() const
const Position4Di posInRefVolume() const
const std::vector< int > & toVector() const
bool isNull() const
The class for EcatSino data write operation.
AimsVector< int64_t, 4 > Point4dl