IB-0.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
IBSubImageGrabber.h
Go to the documentation of this file.
1 /*////////////////////////////////////////////////////////////////////////////
2  Copyright 2018 Istituto Nazionale di Fisica Nucleare
3 
4  Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
5  the European Commission - subsequent versions of the EUPL (the "Licence").
6  You may not use this work except in compliance with the Licence.
7 
8  You may obtain a copy of the Licence at:
9 
10  https://joinup.ec.europa.eu/software/page/eupl
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
14  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  Licence for the specific language governing permissions and limitations under
16  the Licence.
18 
19 
20 
21 #ifndef IBSUBIMAGEGRABBER_H
22 #define IBSUBIMAGEGRABBER_H
23 
24 #include "Math/VoxImage.h"
25 
26 using namespace uLib;
27 
28 struct Box { // better put this elsewhere so a "BoxSelector" can use it
29  Vector3i Begins;
30  Vector3i Ends;
31 };
32 
33 template<class T> // T = VoxImage<>
34 class IBSubImageGrabber
35 {
36 public:
37  IBSubImageGrabber() {}
38  IBSubImageGrabber(T& image) : m_Image(&image) {}
39  IBSubImageGrabber(T* image) : m_Image(image) {}
40 
41  inline void SetImage(T& image) { this->m_Image = &image; }
42  inline void SetImage(T* image) { this->m_Image = image ; }
43 
44  template<class K> // return VoxImage<> Type
45  K GrabRegion(Vector3i start, Vector3i stop);
46 
47  template<class K>
48  K GrabRegion(HPoint3f center, HVector3f size);
49 
50  template<class K>
51  K GrabRegion(Box& voxBox);
52 
53 private:
54  T * m_Image;
55 };
56 
57 // specializations
58 
59 // TODO checks on T for type and on box border inside T
60 // TODO maybe move these function on the VoxImage<T> prototype and get rid of this class?
61 template<class T>
62 template<class K>
63 K IBSubImageGrabber<T>::GrabRegion(Box& voxBox)
64 {
65  Vector3i sizer = (voxBox.Ends - voxBox.Begins) + Vector3i(1,1,1);
66  K region(sizer);
67  Vector3f spacing = this->m_Image->GetSpacing();
68  region.SetSpacing(spacing);
69  region.SetDataOrder(this->m_Image->GetDataOrder());
70  Vector3f region_position = this->m_Image->GetPosition();
71  Vector3f shift(spacing(0)*voxBox.Begins(0),
72  spacing(1)*voxBox.Begins(1),
73  spacing(2)*voxBox.Begins(2));
74  region_position += shift;
75  region.SetPosition(region_position);
76 
77  for(int i=0; i<sizer(0); ++i) {
78  for (int j=0; j<sizer(1); ++j) {
79  for (int k=0; k<sizer(2); ++k) {
80  Vector3i box_position(i,j,k);
81  Vector3i img_position = box_position + voxBox.Begins;
82  region.SetValue(box_position, m_Image->GetValue(img_position));
83  }
84  }
85  }
86  return region;
87 }
88 
89 template<class T>
90 template<class K>
91 K IBSubImageGrabber<T>::GrabRegion(Vector3i start, Vector3i stop)
92 {
93  Box box;
94  box.Begins = start;
95  box.Ends = stop;
96  return this->GrabRegion<K>(box);
97 }
98 
99 template<class T>
100 template<class K>
101 K IBSubImageGrabber<T>::GrabRegion(HPoint3f center, HVector3f size)
102 {
103  Box box;
104  box.Begins = this->m_Image->Find(center-size);
105  box.Ends = this->m_Image->Find(center+size);
106  return this->GrabRegion<K>(box);
107 }
108 
109 #endif // IBSUBIMAGEGRABBER_H