]> Creatis software - openheart.git/blob - Common/vtkOptimizedImageData.h
commit all the files for the first time
[openheart.git] / Common / vtkOptimizedImageData.h
1 /**
2 * Progam made by Olivier Bernard, associate professor
3 * at Institut National des Sciences Appliquees (INSA) Lyon,
4 * CREATIS-LRMN Laboratory,
5 * 69621 Villeurbanne, France,
6 * 31th of march 2011
7 */
8
9 #ifndef __vtkOptimizedImageData_h__
10 #define __vtkOptimizedImageData_h__
11
12 #include <vtkObjectFactory.h>
13 #include <vtkImageData.h>
14 #include <vtkPointData.h>
15
16
17 using namespace std;
18
19 template<class T>
20 class VTK_EXPORT vtkOptimizedImageData : public vtkImageData
21 {
22
23     private:
24
25         T* data;
26         int dimX;
27         int dimY;
28         int dimZ;
29
30     protected:
31
32         vtkOptimizedImageData() : data(NULL) {}
33         ~vtkOptimizedImageData() {}
34
35     public:
36
37         static vtkOptimizedImageData *New()
38         {
39             vtkObject* ret = vtkObjectFactory::CreateInstance("vtkOptimizedImageData");
40             if(ret) { return (vtkOptimizedImageData*)ret; }
41             // If the factory was unable to create the object, then create it here.
42             return (new vtkOptimizedImageData);
43         }
44
45         vtkOptimizedImageData& operator=(vtkImageData &m)
46         {
47             this->DeepCopy(&m);
48             return *this;
49         }
50
51         T& operator()(int x, int y, int z)
52         {
53             if ( !data ) {
54                 data = static_cast<T*>(this->PointData->GetScalars()->GetVoidPointer(0));
55                 dimX = this->GetDimensions()[0];
56                 dimY = this->GetDimensions()[1];
57                 dimZ = this->GetDimensions()[2];
58             }
59             return data[x+y*dimX+z*dimX*dimY];
60 //            if ( (x>=0 && x<dimX) && (y>=0 && y<dimY) && (z>=0 && z<dimZ) )
61 //                return data[x+y*dimX+z*dimX*dimY];
62 //            else
63 //            {
64 //                cout << "Take care, try to access a point out of the volume" << endl;
65 //                return data[0];
66 //            }
67         }
68
69         double operator()(double x, double y, double z) // added by Daniel
70         {
71             if ( !data ) {
72                 data = static_cast<T*>(this->PointData->GetScalars()->GetVoidPointer(0));
73                 dimX = this->GetDimensions()[0];
74                 dimY = this->GetDimensions()[1];
75                 dimZ = this->GetDimensions()[2];
76             }
77
78             int x0,y0,z0;
79             x0=(int) x;
80             y0=(int) y;
81             z0=(int) z;
82
83 //            cout << x << " " << y << " " << z << " " << endl;
84 //            cout << x0 << " " << y0 << " " << z0 << " " << endl;
85
86
87             int x1,y1,z1;
88             x1=x0+1;
89             y1=y0+1;
90             z1=z0+1;
91
92 //            cout << x1 << " " << y1 << " " << z1 << " " << endl;
93
94             double xd,yd,zd;
95             xd=x-x0;
96             yd=y-y0;
97             zd=z-z0;
98
99 //            cout << xd << " " << yd << " " << zd << " " << endl;
100
101             double xdi=1.0-xd;
102             double ydi=1.0-yd;
103             double zdi=1.0-zd;
104
105             double c00, c10, c01, c11;
106             c00 = data[x0+y0*dimX+z0*dimX*dimY]*xdi+data[x1+y0*dimX+z0*dimX*dimY]*xd;
107             c10 = data[x0+y1*dimX+z0*dimX*dimY]*xdi+data[x1+y1*dimX+z0*dimX*dimY]*xd;
108             c01 = data[x0+y0*dimX+z1*dimX*dimY]*xdi+data[x1+y0*dimX+z1*dimX*dimY]*xd;
109             c11 = data[x0+y1*dimX+z1*dimX*dimY]*xdi+data[x1+y1*dimX+z1*dimX*dimY]*xd;
110
111             double c0,c1;
112             c0=c00*ydi+c10*yd;
113             c1=c01*ydi+c11*yd;
114
115             return c0*zdi+c1*zd;
116
117         }
118
119         T& operator()(int x, int y)
120         {
121             if ( !data ) {
122                 data = static_cast<T*>(this->PointData->GetScalars()->GetVoidPointer(0));
123                 dimX = this->GetDimensions()[0];
124                 dimY = this->GetDimensions()[1];
125                 dimZ = 1;
126             }
127             if ( (x>=0 && x<dimX) && (y>=0 && y<dimY) )
128                 return data[x+y*dimX];
129             else
130             {
131                 cout << "Take care, try to access a point out of the volume" << endl;
132                 return data[0];
133             }
134         }
135
136         T& operator()(int k)
137         {
138             if ( !data ) {
139                 data = static_cast<T*>(this->PointData->GetScalars()->GetVoidPointer(0));
140                 dimX = this->GetDimensions()[0];
141                 dimY = this->GetDimensions()[1];
142                 dimZ = this->GetDimensions()[2];
143             }
144             if (k>=0 && k<dimX*dimY*dimZ)
145                 return data[k];
146             else
147             {
148                 cout << "Take care, try to access a point out of the volume" << endl;
149                 return data[0];
150             }
151         }
152
153         void PrintDataPointer()
154         {
155                 cout << "data = " << this->PointData->GetScalars()->GetVoidPointer(0) << endl;
156         }
157
158
159         void Update()
160         {
161                 AllocateScalars();
162             this->Superclass::Update();
163         }
164
165         void PrintInfo();
166
167         /// Added by Daniel
168         void CopyImage(vtkDataObject *src)
169         {
170             this->DeepCopy(src);
171             data = static_cast<T*>(this->PointData->GetScalars()->GetVoidPointer(0));
172             dimX = this->GetDimensions()[0];
173             dimY = this->GetDimensions()[1];
174             dimZ = this->GetDimensions()[2];
175         }
176
177
178 };
179
180
181 #endif