MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlSubImageBox.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_SUB_IMAGE_BOX_H
14#define ML_SUB_IMAGE_BOX_H
15
18
19// ML-includes
20#include "mlInitSystemML.h"
21#include "mlImageVector.h"
22
24
25//-------------------------------------------------------------------------
51//-------------------------------------------------------------------------
52template <typename intT> class TSubImageBox
53{
54
55public:
58
64
70
71 //------------------------------------------------------
74 //------------------------------------------------------
77 inline TSubImageBox()
78 : v1(0)
79 , v2(-1)
80 {}
81
86 : v1(vector1)
87 , v2(vector2)
88 {}
89
92 inline TSubImageBox(const VectorType extent)
93 : v1(0)
94 , v2(extent - 1)
95 {}
97
98
99 //------------------------------------------------------
102 //------------------------------------------------------
104 bool operator==(const TSubImageBox<intT>& box) const
105 {
106 return (v1 == box.v1) && (v2 == box.v2);
107 }
108
110 bool operator!=(const TSubImageBox<intT>& box) const
111 {
112 return (v1 != box.v1) || (v2 != box.v2);
113 }
114
118 bool isEmpty() const
119 {
120 return (v1.x > v2.x) || (v1.y > v2.y) || (v1.z > v2.z) || (v1.c > v2.c) || (v1.t > v2.t) || (v1.u > v2.u);
121 }
122
126 {
127 if (isEmpty()) {
128 return 0;
129 }
130 else {
131 return VectorType::compMul((v2 - v1) + VectorType(1));
132 }
133 }
134
138 {
139 if (isEmpty()) {
140 return VectorType(0);
141 }
142 else {
143 return (v2 - v1) + VectorType(1);
144 }
145 }
146
147
150 void correct();
152
153
154 //------------------------------------------------------
157 //------------------------------------------------------
162 [[nodiscard]]
164
169
173 {
174 v1.set(0);
175 v2.set(-1);
176 }
177
179 [[nodiscard]]
181 {
182 return TSubImageBox<intT>::intersect(*this, box);
183 }
184
186 [[nodiscard]]
188 {
189 return TSubImageBox<intT>::merge(*this, box);
190 }
191
194 VectorType clamp(const VectorType& position) const;
195
197 inline bool contains(const VectorType& position) const
198 {
199 return (position >= v1) && (position <= v2);
200 }
201
204 {
205 v1 = v1 + offsetVector;
206 v2 = v2 + offsetVector;
207 }
208
213 {
214 if (box.isEmpty()) {
215 for (int c = 0; c < 8; c++) {
216 corners[c] = ImageVector(0);
217 }
218 }
219 else {
220 corners[0] = box.v1;
221 corners[1] = ImageVector(box.v1.x, box.v1.y, box.v2.z, 0, 0, 0);
222 corners[2] = ImageVector(box.v1.x, box.v2.y, box.v1.z, 0, 0, 0);
223 corners[3] = ImageVector(box.v1.x, box.v2.y, box.v2.z, 0, 0, 0);
224 corners[4] = ImageVector(box.v2.x, box.v1.y, box.v1.z, 0, 0, 0);
225 corners[5] = ImageVector(box.v2.x, box.v1.y, box.v2.z, 0, 0, 0);
226 corners[6] = ImageVector(box.v2.x, box.v2.y, box.v1.z, 0, 0, 0);
227 corners[7] = box.v2;
228 }
229 }
230
232};
233
235
236//-----------------------------------------------------------------------------------
237// Stream output for std::ostream
238//-----------------------------------------------------------------------------------
239namespace std
240{
241
243 template <typename intT> inline ostream& operator<<(ostream& s, const ML_NAMESPACE::TSubImageBox<intT>& box)
244 {
245 return s << box.v1 << " " << box.v2;
246 }
247
248} // namespace std
249
250
252
255
256//------------------------------------------------------
257// IMPLEMENTATION: Now only internal code follows
258//------------------------------------------------------
259
260// Swaps all components where v1.* > v2.*. This is, an empty vector becomes non-empty
261// if corresponding components are not equal.
262template <typename intT> void TSubImageBox<intT>::correct()
263{
264 // Move lesser components into v1 and greater components into v2
265 VectorType V1(v1);
266 VectorType V2(v2);
267 if (V1.x < V2.x) {
268 v1.x = V1.x;
269 v2.x = V2.x;
270 }
271 else {
272 v1.x = V2.x;
273 v2.x = V1.x;
274 }
275 if (V1.y < V2.y) {
276 v1.y = V1.y;
277 v2.y = V2.y;
278 }
279 else {
280 v1.y = V2.y;
281 v2.y = V1.y;
282 }
283 if (V1.z < V2.z) {
284 v1.z = V1.z;
285 v2.z = V2.z;
286 }
287 else {
288 v1.z = V2.z;
289 v2.z = V1.z;
290 }
291 if (V1.c < V2.c) {
292 v1.c = V1.c;
293 v2.c = V2.c;
294 }
295 else {
296 v1.c = V2.c;
297 v2.c = V1.c;
298 }
299 if (V1.t < V2.t) {
300 v1.t = V1.t;
301 v2.t = V2.t;
302 }
303 else {
304 v1.t = V2.t;
305 v2.t = V1.t;
306 }
307 if (V1.u < V2.u) {
308 v1.u = V1.u;
309 v2.u = V2.u;
310 }
311 else {
312 v1.u = V2.u;
313 v2.u = V1.u;
314 }
315}
316
317// Returns the overlapping region of subimage regions \p loc1 and \p loc2.
318// A result with (v1.x>v2.x || ... || v1.z>v1.z) is interpreted as empty.
319// See also isEmpty(). This is especially true if that already
320// holds for \p loc1 or \p loc2.
321template <typename intT>
323{
325 result.v1 = VectorType::compMax(loc1.v1, loc2.v1);
326 result.v2 = VectorType::compMin(loc1.v2, loc2.v2);
327 return result;
328}
329
333template <typename intT>
335{
336 if (loc1.isEmpty()) {
337 return loc2;
338 }
339 else if (loc2.isEmpty()) {
340 return loc1;
341 }
342 else {
343 // Both regions are not empty; determine surrounding box
344 // in all dimensions.
345 return TSubImageBox<intT>(VectorType::compMin(loc1.v1, loc2.v1), VectorType::compMax(loc1.v2, loc2.v2));
346 }
347}
348
349
350// Clamps the position \p pos to the nearest position inside the SubImageBox.
351// If the SubImageBox is empty, then the result vector contains undefined values.
352template <typename intT> TImageVector<intT> TSubImageBox<intT>::clamp(const VectorType& pos) const
353{
354 return VectorType::compMax(VectorType::compMin(pos, v2), v1);
355}
356
358
359#endif // of __mlSubImageBox_H
ImageVector is the 6D TVector specialization used by the ML for all image indexing.
void set(const ComponentType v=0)
Sets all components to v or - if v is not specified - to 0.
This class defines a rectangular subimage region of standard ML dimensions.
TSubImageBox()
Constructor: creates an empty subimage region by setting v1 to (0,...,0) and v2 to (-1,...
void makeEmpty()
Makes box empty by setting v1 to (0,...,0) and v2 to (-1,...,-1), i.e., constructor state is restored...
TSubImageBox< intT > merge(const TSubImageBox< intT > &box) const
Member function version to merge this with a given box.
void translate(const VectorType &offsetVector)
Shifts the entire box by an offset given by offsetVector.
VectorType v1
Corner v1 of the subimage region (included in region).
TSubImageBox< intT > intersect(const TSubImageBox< intT > &box) const
Member function version to intersect this with a given box.
static TSubImageBox< intT > intersect(const TSubImageBox< intT > &box1, const TSubImageBox< intT > &box2)
Returns the overlapping region of subimage regions box1 and box2.
TImageVector< intT > VectorType
The vector type used for corner members v1 and v2.
static void get3DCorners(const TSubImageBox< intT > &box, VectorType corners[8])
Returns the corners of the 3D box extents of box in corners.
bool operator!=(const TSubImageBox< intT > &box) const
Returns false if box has identical components; otherwise, it returns true.
void correct()
Swaps all components where v1.
TSubImageBox(const VectorType &vector1, const VectorType &vector2)
Constructor: creates a subimage from two passed vectors vector1 and vector2.
bool contains(const VectorType &position) const
Returns true if position is inside the SubImageBox.
VectorType v2
Corner v2 of the subimage region (also included in region!).
VectorType clamp(const VectorType &position) const
Clamps the position to the nearest position inside the SubImageBox.
bool operator==(const TSubImageBox< intT > &box) const
Returns true if box has identical components; otherwise, it returns false.
static TSubImageBox< intT > merge(const TSubImageBox< intT > &box1, const TSubImageBox< intT > &box2)
Returns the region containing both subimage regions box1 and box2.
intT getNumVoxels() const
Returns number of voxels in the subimage region, i.e., the product of all extents if this is not empt...
TSubImageBox(const VectorType extent)
Constructor: creates a subimage from an extent, i.e., v1 is set to (0,...,0) and v2 is set to (extent...
bool isEmpty() const
Returns true if subimage region is empty, i.e., if any of the components of v1 is greater than the co...
VectorType getExtent() const
Returns the extents of the subimage region.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
STL namespace.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.