CustusX  15.3.4-beta
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  mReslicer->SetOutputSpacing(spacing.data());
167 }
168 
170 {
171  if (mSlicer)
172  {
173  disconnect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
174  }
175  mSlicer = slicer;
176  if (mSlicer)
177  {
178  connect(mSlicer.get(), SIGNAL(transformChanged(Transform3D)), this, SLOT(transformChangedSlot()));
179  update();
180  }
181 }
182 
183 void SlicedImageProxy::transferFunctionsChangedSlot()
184 {
185  mReslicer->SetInputData(mImage->getBaseVtkImageData());
186  mReslicer->SetBackgroundLevel(mImage->getMin());
187  mImageWithLUTProxy->setInput(mRedirecter, mImage->getLookupTable2D()->getOutputLookupTable());
188 }
189 
190 void SlicedImageProxy::updateRedirecterSlot()
191 {
192  // if input is 2D - use directly
193  if (mImage->getBaseVtkImageData()->GetDimensions()[2]==1)
194  mRedirecter->SetInputData(mImage->getBaseVtkImageData());
195  else
196  mRedirecter->SetInputConnection(mReslicer->GetOutputPort());
197 
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:824
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)
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 vtkImageChangeInformation > vtkImageChangeInformationPtr
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)