CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxResampleImageFilter.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 #include "cxResampleImageFilter.h"
34 
35 #include <QApplication>
36 
37 #include "cxImageAlgorithms.h"
38 #include "cxImage.h"
39 #include "vtkImageData.h"
41 #include "cxDoubleProperty.h"
42 #include "cxPatientModelService.h"
43 #include "cxVisServices.h"
44 
45 namespace cx
46 {
47 
49  FilterImpl(services)
50 {
51 }
52 
54 {
55  return "Resample";
56 }
57 
59 {
60  return "ResampleImageFilter";
61 }
62 
64 {
65  return "<html>"
66  "<h3>Resample.</h3>"
67  "<p><i>Resample the volume into the space of the reference volume. Also crop to the same volume.</i></p>"
68  "</html>";
69 }
70 
72 {
73  return DoubleProperty::initialize("Margin", "",
74  "mm Margin added to ref image bounding box",
75  5.0, DoubleRange(0, 50, 1), 1, root);
76 }
77 
79 {
80  mOptionsAdapters.push_back(this->getMarginOption(mOptions));
81 }
82 
84 {
86 
87  temp = StringPropertySelectImage::New(mServices->patient());
88  temp->setValueName("Input");
89  temp->setHelp("Select input to be resampled");
90  mInputTypes.push_back(temp);
91 
92  temp = StringPropertySelectImage::New(mServices->patient());
93  temp->setValueName("Reference");
94  temp->setHelp("Select reference. Resample input into this coordinate system and bounding box");
95  mInputTypes.push_back(temp);
96 }
97 
99 {
101 
102  temp = StringPropertySelectData::New(mServices->patient());
103  temp->setValueName("Output");
104  temp->setHelp("Output thresholded binary image");
105  mOutputTypes.push_back(temp);
106 }
107 
108 //bool ResampleImageFilter::preProcess()
109 //{
110 // return FilterImpl::preProcess();
111 //}
112 
119 {
120  ImagePtr input = this->getCopiedInputImage(0);
121  ImagePtr reference = this->getCopiedInputImage(1);
122  if (!input || !reference)
123  return false;
124 
125  DoublePropertyPtr marginOption = this->getMarginOption(mCopiedOptions);
126  double margin = marginOption->getValue();
127 
128  Transform3D refMi = reference->get_rMd().inv() * input->get_rMd();
129  ImagePtr oriented = resampleImage(mServices->patient(), input, refMi);//There is an error with the transfer functions in this image
130 
131  Transform3D orient_M_ref = oriented->get_rMd().inv() * reference->get_rMd();
132  DoubleBoundingBox3D bb_crop = transform(orient_M_ref, reference->boundingBox());
133 
134  // increase bb size by margin
135  bb_crop[0] -= margin;
136  bb_crop[1] += margin;
137  bb_crop[2] -= margin;
138  bb_crop[3] += margin;
139  bb_crop[4] -= margin;
140  bb_crop[5] += margin;
141 
142  oriented->setCroppingBox(bb_crop);
143 
144  ImagePtr cropped = cropImage(mServices->patient(), oriented);
145 
146  QString uid = input->getUid() + "_resample%1";
147  QString name = input->getName() + " resample%1";
148 
149  ImagePtr resampled = resampleImage(mServices->patient(), cropped, Vector3D(reference->getBaseVtkImageData()->GetSpacing()), uid, name);
150 
151  // important! move thread affinity to main thread - ensures signals/slots is still called correctly
152  resampled->moveThisAndChildrenToThread(QApplication::instance()->thread());
153 
154  mRawResult = resampled;
155  return true;
156 }
157 
159 {
160  if (!mRawResult)
161  return false;
162 
163  ImagePtr output = mRawResult;
164  mRawResult.reset();
165  mServices->patient()->insertData(output);
166 
167  // set output
168  mOutputTypes.front()->setValue(output->getUid());
169  return true;
170 }
171 
172 
173 
174 } // namespace cx
175 
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
ImagePtr resampleImage(PatientModelServicePtr dataManager, ImagePtr image, Transform3D qMd)
std::vector< SelectDataStringPropertyBasePtr > mInputTypes
Definition: cxFilterImpl.h:94
QDomElement mCopiedOptions
Definition: cxFilterImpl.h:101
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
vtkImageDataPtr cropImage(vtkImageDataPtr input, IntBoundingBox3D cropbox)
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:53
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
DoublePropertyPtr getMarginOption(QDomElement root)
virtual QString getName() const
std::vector< PropertyPtr > mOptionsAdapters
Definition: cxFilterImpl.h:96
VisServicesPtr mServices
Definition: cxFilterImpl.h:103
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
virtual QString getType() const
ImagePtr getCopiedInputImage(int index=0)
QDomElement mOptions
Definition: cxFilterImpl.h:97
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
virtual QString getHelp() const
boost::shared_ptr< class DoubleProperty > DoublePropertyPtr
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
std::vector< SelectDataStringPropertyBasePtr > mOutputTypes
Definition: cxFilterImpl.h:95
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
ResampleImageFilter(VisServicesPtr services)