CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  if (input->GetOutput()->GetNumberOfScalarComponents() == 3) // color
84  {
85  // split the image into the components, apply the lut, then merge.
86 
87  vtkImageAppendComponentsPtr merger = vtkImageAppendComponentsPtr::New();
88 
89  for (int i = 0; i < 3; ++i)
90  {
91  vtkImageMapToColorsPtr compWindowLevel = vtkImageMapToColorsPtr::New();
92  compWindowLevel->SetInputConnection(input->GetOutputPort());
93  compWindowLevel->SetActiveComponent(i);
94  compWindowLevel->SetLookupTable(lut);
95  if (i==2) //TODO the only thing missing here is the alpha channel. Should be able to pass that on from the last pipe.
96  {
97  compWindowLevel->SetOutputFormatToLuminanceAlpha();
98  }
99  else
100  {
101  compWindowLevel->SetOutputFormatToLuminance();
102  }
103 
104  merger->AddInputConnection(compWindowLevel->GetOutputPort());
105  }
106 
107  mRedirecter->SetInputConnection(merger->GetOutputPort());
108  }
109  else // grayscale
110  {
111  vtkImageMapToColorsPtr windowLevel = vtkImageMapToColorsPtr::New();
112  windowLevel->SetOutputFormatToRGBA();
113  windowLevel->SetInputConnection(input->GetOutputPort());
114  windowLevel->SetLookupTable(lut);
115  mRedirecter->SetInputConnection(windowLevel->GetOutputPort());
116  }
117  }
118  else // no image
119  {
120  mRedirecter->SetInputData(mDummyImage);
121  }
122 
123  mRedirecter->Update();
124 }
125 
127 {
128  return mRedirecter;
129 }
130 
132 {
133  return mRedirecter->GetOutput();
134 }
135 
140 
141 
143 {
144  mMatrixAxes = vtkMatrix4x4Ptr::New();
145 
146  mReslicer = vtkImageReslicePtr::New();
147  mReslicer->SetInterpolationModeToLinear();
148  mReslicer->SetOutputDimensionality(2);
149  mReslicer->SetResliceAxes(mMatrixAxes);
150  //mReslicer->SetAutoCropOutput(false); //faster update rate
151  mReslicer->AutoCropOutputOn(); // fix used in 2.0.9, but slower update rate
152 
153  mImageWithLUTProxy.reset(new ApplyLUTToImage2DProxy());
154 
155  mRedirecter = vtkImageChangeInformationPtr::New();
156 }
157 
159 {
160 }
161 
162 void SlicedImageProxy::setOutputFormat(Vector3D origin, Eigen::Array3i dim, Vector3D spacing)
163 {
164  mReslicer->SetOutputOrigin(origin.data());
165  mReslicer->SetOutputExtent(0, dim[0], 0, dim[1], 0, 0);
166  // this looks like the correct way, but gives incorrect output (the way it is used)
167  // TODO investigate
168 // mReslicer->SetOutputExtent(0, dim[0]-1, 0, dim[1]-1, 0, 0);
169  mReslicer->SetOutputSpacing(spacing.data());
170 }
171 
173 {
174  if (mSlicer)
175  {
176  disconnect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
177  }
178  mSlicer = slicer;
179  if (mSlicer)
180  {
181  connect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
182  update();
183  }
184 }
185 
186 void SlicedImageProxy::transferFunctionsChangedSlot()
187 {
188  mReslicer->SetInputData(mImage->getBaseVtkImageData());
189  mReslicer->SetBackgroundLevel(mImage->getMin());
190  mImageWithLUTProxy->setInput(mRedirecter, mImage->getLookupTable2D()->getOutputLookupTable());
191 }
192 
193 void SlicedImageProxy::updateRedirecterSlot()
194 {
195  mRedirecter->SetInputConnection(mReslicer->GetOutputPort());
196  update();
197 }
198 
200 {
201  if (mImage)
202  {
203  disconnect(mImage.get(), SIGNAL(transferFunctionsChanged()), this, SLOT(transferFunctionsChangedSlot()));
204  disconnect(mImage.get(), SIGNAL(transformChanged()), this, SLOT(transformChangedSlot()));
205  disconnect(mImage.get(), SIGNAL(vtkImageDataChanged()), this, SLOT(updateRedirecterSlot()));
206  }
207 
208  mImage = image;
209 
210  if (mImage)
211  {
212  connect(mImage.get(), SIGNAL(transferFunctionsChanged()), this, SLOT(transferFunctionsChangedSlot()));
213  connect(mImage.get(), SIGNAL(transformChanged()), this, SLOT(transformChangedSlot()));
214  connect(mImage.get(), SIGNAL(vtkImageDataChanged()), this, SLOT(updateRedirecterSlot()));
215  }
216 
217  if (mImage)
218  {
219  this->updateRedirecterSlot();
220  this->transferFunctionsChangedSlot();
221  }
222  else // no image
223  {
224  mImageWithLUTProxy->setInput(vtkImageAlgorithmPtr(), vtkLookupTablePtr());
225  }
226 
227  this->update();
228 }
229 
231 {
232  return mImage;
233 }
234 
236 {
237  return mImageWithLUTProxy->getOutput();
238 }
239 
241 {
242  return mImageWithLUTProxy->getOutputPort();
243 }
244 
246 {
247  return mRedirecter->GetOutput();
248 }
249 
251 {
252  return mRedirecter;
253 }
254 
256 {
257  if (!mImage)
258  return;
259 
260  Transform3D rMs = Transform3D::Identity();
261  if (mSlicer)
262  rMs = mSlicer->get_sMr().inv();
263  Transform3D iMr = mImage->get_rMd().inv();
264  Transform3D M = iMr * rMs;
265 
266  mMatrixAxes->DeepCopy(M.getVtkMatrix());
267 }
268 
269 void SlicedImageProxy::transformChangedSlot()
270 {
271  update();
272 }
273 
274 void SlicedImageProxy::printSelf(std::ostream & os, Indent indent)
275 {
276  //os << indent << "PlaneType: " << mType << std::endl;
277  os << indent << "mImage: " << (mImage ? mImage->getUid() : "NULL") << std::endl;
278  os << indent << "mSlicer: " << (mSlicer ? mSlicer.get() : 0) << std::endl;
279  if (mSlicer)
280  {
281  mSlicer->printSelf(os, indent.stepDown());
282  }
283  os << indent << "mReslicer->GetOutput(): " << mReslicer->GetOutput() << std::endl;
284  os << indent << "mReslicer->GetInput() : " << mReslicer->GetInput() << std::endl;
285  Transform3D test(mReslicer->GetResliceAxes());
286  os << indent << "resliceaxes: " << std::endl;
287  test.put(os, indent.getIndent() + 3);
288  os << std::endl;
289  //os << indent << "rMs_debug: " << std::endl;
290  //rMs_debug.put(os, indent.getIndent()+3);
291 
292 }
293 
294 //---------------------------------------------------------
295 }//end namespace
296 //---------------------------------------------------------
static vtkImageDataPtr createDummyImageData(int axisSize, int maxVoxelValue)
Create a moc object of vtkImageData.
Definition: cxImage.cpp:829
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
Helper class for applying sscLUT2D to an image.
void setInputData(vtkImageDataPtr image, vtkLookupTablePtr lut)