1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
21 #include "clitkCommon.h"
22 #include <vtkImageData.h>
23 #include <vtkImageReslice.h>
26 #include <vtkGeneralTransform.h>
27 #include <vtkTransform.h>
28 #include <vtkGridTransform.h>
30 //--------------------------------------------------------------------
31 vvImage::vvImage():mGeneralTransform(vtkSmartPointer<vtkGeneralTransform>::New()),
32 mTransform (vtkSmartPointer<vtkTransform>::New()),
33 mGridTransform (vtkSmartPointer<vtkGridTransform>::New())
35 mGeneralTransform->Concatenate(mTransform);
36 mGeneralTransform->Concatenate(mGridTransform);
39 //--------------------------------------------------------------------
42 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
50 //--------------------------------------------------------------------
53 DD("vvImage got deleted!");
56 //--------------------------------------------------------------------
58 //--------------------------------------------------------------------
61 for (unsigned int i = 0; i < mVtkImages.size(); i++)
62 mVtkImageReslice[i]->GetInput()->Delete();
65 mVtkImageReslice.resize(0);
69 //--------------------------------------------------------------------
71 //--------------------------------------------------------------------
72 void vvImage::SetImage(std::vector< vtkImageData* > images)
75 for (unsigned int i = 0; i < images.size(); i++)
78 //--------------------------------------------------------------------
81 //--------------------------------------------------------------------
82 void vvImage::AddImage(vtkImageData* image)
84 mVtkImageReslice.push_back(vtkSmartPointer<vtkImageReslice>::New());
86 mVtkImageReslice.back()->SetInterpolationModeToLinear();
87 mVtkImageReslice.back()->AutoCropOutputOn();
88 mVtkImageReslice.back()->SetBackgroundColor(-1000,-1000,-1000,1);
89 mVtkImageReslice.back()->SetResliceTransform(mGeneralTransform);
90 mVtkImageReslice.back()->SetInput(0, image);
91 mVtkImageReslice.back()->Update();
93 mVtkImages.push_back( mVtkImageReslice.back()->GetOutput(0) );
95 //--------------------------------------------------------------------
97 //--------------------------------------------------------------------
98 int vvImage::GetNumberOfSpatialDimensions()
100 int dim=GetNumberOfDimensions();
101 if (IsTimeSequence())
106 //--------------------------------------------------------------------
108 //--------------------------------------------------------------------
109 int vvImage::GetNumberOfDimensions() const
111 if (mVtkImages.size()) {
114 mVtkImages[0]->GetWholeExtent(extent);
115 if (extent[5] - extent[4] >= 1)
117 if (mVtkImages.size() > 1)
123 //--------------------------------------------------------------------
125 //--------------------------------------------------------------------
126 void vvImage::GetScalarRange(double* range)
128 assert(mVtkImages.size());
129 double * temp = mVtkImages[0]->GetScalarRange();
132 for (unsigned int i=1; i<mVtkImages.size(); i++) {
133 temp = mVtkImages[i]->GetScalarRange();
134 if (temp[0] < range[0]) range[0]=temp[0];
135 if (temp[1] > range[1]) range[1]=temp[1];
138 //--------------------------------------------------------------------
140 //--------------------------------------------------------------------
141 std::string vvImage::GetScalarTypeAsITKString()
143 // WARNING VTK pixel type different from ITK Pixel type
144 std::string vtktype = mVtkImages[0]->GetScalarTypeAsString();
145 if (vtktype == "unsigned char") return "unsigned_char";
146 if (vtktype == "unsigned short") return "unsigned_short";
147 if (vtktype == "unsigned int") return "unsigned_int";
150 //--------------------------------------------------------------------
152 //--------------------------------------------------------------------
153 int vvImage::GetNumberOfScalarComponents()
155 return mVtkImages[0]->GetNumberOfScalarComponents();
157 //--------------------------------------------------------------------
159 //--------------------------------------------------------------------
160 int vvImage::GetScalarSize()
162 return mVtkImages[0]->GetScalarSize();
164 //--------------------------------------------------------------------
166 //--------------------------------------------------------------------
167 std::vector<double> vvImage::GetSpacing()
169 std::vector<double> spacing;
170 int dim = this->GetNumberOfDimensions();
171 for (int i = 0; i < dim; i++) {
173 spacing.push_back(mTimeSpacing);
175 spacing.push_back(mVtkImages[0]->GetSpacing()[i]);
179 //--------------------------------------------------------------------
180 std::vector<double> vvImage::GetOrigin() const
182 std::vector<double> origin;
183 int dim = this->GetNumberOfDimensions();
184 for (int i = 0; i < dim; i++) {
186 origin.push_back(mTimeOrigin);
188 origin.push_back(mVtkImages[0]->GetOrigin()[i]);
192 //--------------------------------------------------------------------
194 //--------------------------------------------------------------------
195 std::vector<int> vvImage::GetSize()
197 std::vector<int> size0;
198 int dim = this->GetNumberOfDimensions();
199 for (int i = 0; i < dim; i++) {
201 size0.push_back(mVtkImages.size());
203 size0.push_back(mVtkImages[0]->GetDimensions()[i]);
207 //--------------------------------------------------------------------
209 //--------------------------------------------------------------------
210 unsigned long vvImage::GetActualMemorySize()
212 unsigned long size = 0;
213 for (unsigned int i = 0; i < mVtkImages.size(); i++) {
214 size += mVtkImages[i]->GetActualMemorySize();
218 //--------------------------------------------------------------------
221 //--------------------------------------------------------------------
222 bool vvImage::IsTimeSequence()
224 return mVtkImages.size()>1;
226 //--------------------------------------------------------------------
229 //--------------------------------------------------------------------
230 const std::vector<vtkImageData*>& vvImage::GetVTKImages()
234 //--------------------------------------------------------------------
236 //--------------------------------------------------------------------
237 vtkImageData* vvImage::GetFirstVTKImageData()
239 return mVtkImages[0];
241 //--------------------------------------------------------------------
243 //--------------------------------------------------------------------
244 bool vvImage::IsScalarTypeInteger()
246 assert(mVtkImages.size()> 0);
247 int t = mVtkImages[0]->GetScalarType();
248 return IsScalarTypeInteger(t);
250 //--------------------------------------------------------------------
252 //--------------------------------------------------------------------
253 bool vvImage::IsScalarTypeInteger(int t)
255 if ((t == VTK_BIT) ||
257 (t == VTK_UNSIGNED_CHAR) ||
259 (t == VTK_UNSIGNED_SHORT) ||
261 (t == VTK_UNSIGNED_INT) ||
263 (t == VTK_UNSIGNED_LONG)) {
270 //--------------------------------------------------------------------
272 //--------------------------------------------------------------------
273 vtkSmartPointer<vtkTransform> vvImage::GetTransform()
277 //--------------------------------------------------------------------
279 //--------------------------------------------------------------------
280 vtkSmartPointer<vtkGridTransform> vvImage::GetGridTransform()
282 return mGridTransform;
284 //--------------------------------------------------------------------
286 //--------------------------------------------------------------------
287 void vvImage::UpdateReslice()
289 for (unsigned int i=0; i<mVtkImageReslice.size(); i++) {
290 mVtkImageReslice[i]->Update();
293 //--------------------------------------------------------------------
296 //--------------------------------------------------------------------
297 vtkImageData * CopyAndCastToFloatFrom(vtkImageData * input)
299 vtkSmartPointer<vtkImageData> p = vtkSmartPointer<vtkImageData>::New();
300 p->SetExtent(input->GetExtent ()); // Only first ! could not be 4D
301 p->SetScalarTypeToFloat();
302 p->AllocateScalars();
303 p->CopyAndCastFrom(input, input->GetExtent());
306 //--------------------------------------------------------------------
309 #endif // VVIMAGE_CXX