Fraxinus  17.12
An IGT application
cxSlicedImageProxy.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxSlicedImageProxy.h"
35 
36 #include <vtkImageReslice.h>
37 #include <vtkImageMapToWindowLevelColors.h>
38 #include <vtkWindowLevelLookupTable.h>
39 #include <vtkImageData.h>
40 #include <vtkMatrix4x4.h>
41 #include <vtkImageAlgorithm.h>
42 #include <vtkImageChangeInformation.h>
43 #include <vtkImageExtractComponents.h>
44 #include <vtkImageAppendComponents.h>
45 
46 #include "cxImage.h"
47 #include "cxSliceProxy.h"
48 #include "cxImageLUT2D.h"
49 #include "cxTypeConversions.h"
50 
51 
52 namespace cx
53 {
54 
55 
57 {
58  mDummyImage = Image::createDummyImageData(1, 0);
59 
60  mRedirecter = vtkSmartPointer<vtkImageChangeInformation>::New(); // used for forwarding only.
61  mRedirecter->SetInputData(mDummyImage);
62 }
63 
65 {
66 
67 }
68 
70 {
71  vtkImageChangeInformationPtr redirecter = vtkImageChangeInformationPtr::New();
72  redirecter->SetInputData(image);
73  redirecter->Update();
74  this->setInput(redirecter, lut);
75 }
76 
78 {
79  input->Update();
80 
81  if (input->GetOutput())
82  {
83  int numScalarComponents = input->GetOutput()->GetNumberOfScalarComponents();
84  if ( numScalarComponents >= 3) // color
85  {
86  // split the image into the components, apply the lut, then merge.
87 
88  vtkImageAppendComponentsPtr merger = vtkImageAppendComponentsPtr::New();
89 
90  for (int i = 0; i < numScalarComponents; ++i)
91  {
92  vtkImageMapToColorsPtr compWindowLevel = vtkImageMapToColorsPtr::New();
93  compWindowLevel->SetInputConnection(input->GetOutputPort());
94  compWindowLevel->SetActiveComponent(i);
95  compWindowLevel->SetLookupTable(lut);
96 
97  if (i==2 && numScalarComponents==3)
98  {
99  compWindowLevel->SetOutputFormatToLuminanceAlpha();
100  }
101  else
102  {
103  compWindowLevel->SetOutputFormatToLuminance();
104  }
105 
106  merger->AddInputConnection(compWindowLevel->GetOutputPort());
107  }
108 
109  mRedirecter->SetInputConnection(merger->GetOutputPort());
110  }
111  else // grayscale
112  {
113  vtkImageMapToColorsPtr windowLevel = vtkImageMapToColorsPtr::New();
114  windowLevel->SetOutputFormatToRGBA();
115  windowLevel->SetInputConnection(input->GetOutputPort());
116  windowLevel->SetLookupTable(lut);
117  mRedirecter->SetInputConnection(windowLevel->GetOutputPort());
118  }
119  }
120  else // no image
121  {
122  mRedirecter->SetInputData(mDummyImage);
123  }
124 
125  mRedirecter->Update();
126 }
127 
129 {
130  return mRedirecter;
131 }
132 
134 {
135  return mRedirecter->GetOutput();
136 }
137 
142 
143 
145 {
146  mMatrixAxes = vtkMatrix4x4Ptr::New();
147 
148  mReslicer = vtkImageReslicePtr::New();
149  mReslicer->SetInterpolationModeToLinear();
150  mReslicer->SetOutputDimensionality(2);
151  mReslicer->SetResliceAxes(mMatrixAxes);
152  //mReslicer->SetAutoCropOutput(false); //faster update rate
153  mReslicer->AutoCropOutputOn(); // fix used in 2.0.9, but slower update rate
154 
155  mImageWithLUTProxy.reset(new ApplyLUTToImage2DProxy());
156 
157  mRedirecter = vtkImageChangeInformationPtr::New();
158 }
159 
161 {
162 }
163 
164 void SlicedImageProxy::setOutputFormat(Vector3D origin, Eigen::Array3i dim, Vector3D spacing)
165 {
166  mReslicer->SetOutputOrigin(origin.data());
167  mReslicer->SetOutputExtent(0, dim[0], 0, dim[1], 0, 0);
168  // this looks like the correct way, but gives incorrect output (the way it is used)
169  // TODO investigate
170 // mReslicer->SetOutputExtent(0, dim[0]-1, 0, dim[1]-1, 0, 0);
171  mReslicer->SetOutputSpacing(spacing.data());
172 }
173 
175 {
176  if (mSlicer)
177  {
178  disconnect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
179  }
180  mSlicer = slicer;
181  if (mSlicer)
182  {
183  connect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
184  update();
185  }
186 }
187 
188 void SlicedImageProxy::transferFunctionsChangedSlot()
189 {
190  mReslicer->SetInputData(mImage->getBaseVtkImageData());
191  mReslicer->SetBackgroundLevel(mImage->getMin());
192  mImageWithLUTProxy->setInput(mRedirecter, mImage->getLookupTable2D()->getOutputLookupTable());
193 }
194 
195 void SlicedImageProxy::updateRedirecterSlot()
196 {
197  mRedirecter->SetInputConnection(mReslicer->GetOutputPort());
198  update();
199 }
200 
202 {
203  if (mImage)
204  {
205  disconnect(mImage.get(), SIGNAL(transferFunctionsChanged()), this, SLOT(transferFunctionsChangedSlot()));
206  disconnect(mImage.get(), SIGNAL(transformChanged()), this, SLOT(transformChangedSlot()));
207  disconnect(mImage.get(), SIGNAL(vtkImageDataChanged()), this, SLOT(updateRedirecterSlot()));
208  }
209 
210  mImage = image;
211 
212  if (mImage)
213  {
214  connect(mImage.get(), SIGNAL(transferFunctionsChanged()), this, SLOT(transferFunctionsChangedSlot()));
215  connect(mImage.get(), SIGNAL(transformChanged()), this, SLOT(transformChangedSlot()));
216  connect(mImage.get(), SIGNAL(vtkImageDataChanged()), this, SLOT(updateRedirecterSlot()));
217  }
218 
219  if (mImage)
220  {
221  this->updateRedirecterSlot();
222  this->transferFunctionsChangedSlot();
223  }
224  else // no image
225  {
226  mImageWithLUTProxy->setInput(vtkImageAlgorithmPtr(), vtkLookupTablePtr());
227  }
228 
229  this->update();
230 }
231 
233 {
234  return mImage;
235 }
236 
238 {
239  return mImageWithLUTProxy->getOutput();
240 }
241 
243 {
244  return mImageWithLUTProxy->getOutputPort();
245 }
246 
248 {
249  return mRedirecter->GetOutput();
250 }
251 
253 {
254  return mRedirecter;
255 }
256 
258 {
259  if (!mImage)
260  return;
261 
262  Transform3D rMs = Transform3D::Identity();
263  if (mSlicer)
264  rMs = mSlicer->get_sMr().inv();
265  Transform3D iMr = mImage->get_rMd().inv();
266  Transform3D M = iMr * rMs;
267 
268  mMatrixAxes->DeepCopy(M.getVtkMatrix());
269 }
270 
271 void SlicedImageProxy::transformChangedSlot()
272 {
273  update();
274 }
275 
276 void SlicedImageProxy::printSelf(std::ostream & os, Indent indent)
277 {
278  //os << indent << "PlaneType: " << mType << std::endl;
279  os << indent << "mImage: " << (mImage ? mImage->getUid() : "NULL") << std::endl;
280  os << indent << "mSlicer: " << (mSlicer ? mSlicer.get() : 0) << std::endl;
281  if (mSlicer)
282  {
283  mSlicer->printSelf(os, indent.stepDown());
284  }
285  os << indent << "mReslicer->GetOutput(): " << mReslicer->GetOutput() << std::endl;
286  os << indent << "mReslicer->GetInput() : " << mReslicer->GetInput() << std::endl;
287  Transform3D test(mReslicer->GetResliceAxes());
288  os << indent << "resliceaxes: " << std::endl;
289  test.put(os, indent.getIndent() + 3);
290  os << std::endl;
291  //os << indent << "rMs_debug: " << std::endl;
292  //rMs_debug.put(os, indent.getIndent()+3);
293 
294 }
295 
296 //---------------------------------------------------------
297 }//end namespace
298 //---------------------------------------------------------
static vtkImageDataPtr createDummyImageData(int axisSize, int maxVoxelValue)
Create a moc object of vtkImageData.
Definition: cxImage.cpp:838
int getIndent() const
Definition: cxIndent.cpp:44
vtkImageDataPtr getOutput()
output 2D sliced image
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
void setOutputFormat(Vector3D origin, Eigen::Array3i dim, Vector3D spacing)
vtkSmartPointer< vtkImageChangeInformation > vtkImageChangeInformationPtr
Definition: cxImage.cpp:67
vtkImageAlgorithmPtr getOutputPort()
output 2D sliced image
void printSelf(std::ostream &os, Indent indent)
vtkImageAlgorithmPtr getOutputPortWithoutLUT()
vtkSmartPointer< class vtkImageMapToColors > vtkImageMapToColorsPtr
ImagePtr getImage() const
vtkSmartPointer< class vtkImageAlgorithm > vtkImageAlgorithmPtr
vtkSmartPointer< class vtkImageAppendComponents > vtkImageAppendComponentsPtr
void setInput(vtkImageAlgorithmPtr input, vtkLookupTablePtr lut)
Formatting class for debug printing of the ssc library.
Definition: cxIndent.h:49
void setSliceProxy(SliceProxyInterfacePtr slicer)
vtkSmartPointer< class vtkLookupTable > vtkLookupTablePtr
void setImage(ImagePtr image)
Indent stepDown() const
Definition: cxIndent.cpp:48
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
vtkImageAlgorithmPtr getOutputPort()
output 2D sliced image
vtkImageDataPtr getOutputWithoutLUT()
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
boost::shared_ptr< class SliceProxyInterface > SliceProxyInterfacePtr
void setInputData(vtkImageDataPtr image, vtkLookupTablePtr lut)
Namespace for all CustusX production code.