Fraxinus  17.12-rc3
An IGT application
cxImageEnveloper.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 "cxImageEnveloper.h"
35 #include "cxImage.h"
36 #include "cxVolumeHelpers.h"
37 #include "vtkImageData.h"
39 #include "cxTypeConversions.h"
40 #include "cxBoundingBox3D.h"
41 #include "cxImageTF3D.h"
42 
43 
44 namespace cx
45 {
47 {
48  return ImageEnveloperPtr(new ImageEnveloper());
49 }
50 
51 ImageEnveloper::ImageEnveloper() : mMaxEnvelopeVoxels(10*1000*1000)
52 {
53 
54 }
55 
57 {
58  mMaxEnvelopeVoxels = maxVoxels;
59 }
60 
61 void ImageEnveloper::setImages(std::vector<ImagePtr> images)
62 {
63  mImages = images;
64 }
65 
67 {
68  ImageParameters box = this->createEnvelopeParametersFromImage(mImages[0]);
69  for(unsigned i = 1; i < mImages.size(); ++i)
70  box = selectParametersWithSmallestExtent(box, this->createEnvelopeParametersFromImage(mImages[i]));
71 
72  box.limitVoxelsKeepBounds(mMaxEnvelopeVoxels);
73 
74  ImagePtr retval = this->createEnvelopeFromParameters(box);
75 
76  return retval;
77 }
78 
79 ImageParameters ImageEnveloper::createEnvelopeParametersFromImage(ImagePtr img)
80 {
81  ImageParameters retval;
82 
83  DoubleBoundingBox3D bb = findEnclosingBoundingBox(mImages, img->get_rMd().inverse());
84 
85  retval.setSpacingKeepDim(this->getMinimumSpacingFromAllImages(img->get_rMd().inverse()));
86  retval.setDimKeepBoundsAlignSpacing(bb.range().array());
87  retval.mParentVolume = img->getUid();
88 
89  Vector3D shift = bb.bottomLeft();
90 
91  retval.m_rMd = img->get_rMd() * createTransformTranslate(shift);
92 
93  return retval;
94 }
95 
96 ImageParameters ImageEnveloper::selectParametersWithSmallestExtent(ImageParameters a, ImageParameters b)
97 {
98  if (a.getVolume() <= b.getVolume())
99  return a;
100  else
101  return b;
102 }
103 
104 ImageParameters ImageEnveloper::selectParametersWithFewestVoxels(ImageParameters a, ImageParameters b)
105 {
106  if (a.getNumVoxels() <= b.getNumVoxels())
107  return a;
108  else
109  return b;
110 }
111 
112 Eigen::Array3d ImageEnveloper::getMinimumSpacingFromAllImages(Transform3D qMr)
113 {
114  Eigen::Array3d retval;
115  retval = this->getTransformedSpacing(mImages[0]->getSpacing(), qMr * mImages[0]->get_rMd());
116  for (unsigned i = 1; i < mImages.size(); ++i)
117  {
118  Eigen::Array3d current = this->getTransformedSpacing(mImages[i]->getSpacing(), qMr * mImages[i]->get_rMd());
119  retval = retval.min(current);
120  }
121  return retval;
122 }
123 
124 Eigen::Array3d ImageEnveloper::getTransformedSpacing(Eigen::Array3d spacing, Transform3D qMd)
125 {
126  Eigen::Array3d retval;
127 
128  //Create spacing vectors in img coord syst (d)
129  Vector3D sx = Vector3D(spacing[0], 0, 0);
130  Vector3D sy = Vector3D(0, spacing[1], 0);
131  Vector3D sz = Vector3D(0, 0, spacing[2]);
132 
133  //Transform to q coord syst
134  sx = qMd.vector(sx);
135  sy = qMd.vector(sy);
136  sz = qMd.vector(sz);
137 
138  //Find spacing for each axis
139  for (unsigned i = 0; i < 3; ++i)
140  {
141  retval[i] = std::max(fabs(sx[i]), fabs(sy[i]));
142  retval[i] = std::max(retval[i], fabs(sz[i]));
143  }
144 
145  return retval;
146 }
147 
148 ImagePtr ImageEnveloper::createEnvelopeFromParameters(ImageParameters box)
149 {
150  int maxRange = this->getMaxScalarRange();
151 
152  vtkImageDataPtr imageData = generateVtkImageDataUnsignedShort(box.getDim(), box.getSpacing(), maxRange, 1);
153 
154  QString uid = QString("envelope_image_%1").arg(box.mParentVolume);
155  ImagePtr retval(new Image(uid, imageData));
156  retval->get_rMd_History()->setRegistration(box.m_rMd);
157  retval->get_rMd_History()->setParentSpace(box.mParentVolume);
158  retval->setAcquisitionTime(QDateTime::currentDateTime());
159  retval->setModality("SC");
160 
161  return retval;
162 }
163 
164 int ImageEnveloper::getMaxScalarRange()
165 {
166  int maxRange = 0;
167  for (unsigned i=0; i<mImages.size(); ++i)
168  maxRange = std::max<int>(maxRange, mImages[i]->getBaseVtkImageData()->GetScalarRange()[1]);
169  return maxRange;
170 }
171 
172 } // namespace cx
vtkImageDataPtr generateVtkImageDataUnsignedShort(Eigen::Array3i dim, Vector3D spacing, const unsigned short initValue, int components)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
Vector3D bottomLeft() const
static ImageEnveloperPtr create()
void limitVoxelsKeepBounds(unsigned long maxVolumeSize)
DoubleBoundingBox3D findEnclosingBoundingBox(std::vector< DataPtr > data, Transform3D qMr)
void setDimKeepBoundsAlignSpacing(Eigen::Array3d bounds)
Eigen::Array3i getDim() const
unsigned long getNumVoxels() const
A volumetric data set.
Definition: cxImage.h:66
Transform3D createTransformTranslate(const Vector3D &translation)
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 void setImages(std::vector< ImagePtr > images)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Eigen::Array3d getSpacing() const
virtual ImagePtr getEnvelopingImage()
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void setMaxEnvelopeVoxels(long maxVoxels)
boost::shared_ptr< class ImageEnveloper > ImageEnveloperPtr
void setSpacingKeepDim(Eigen::Array3d spacing)
Namespace for all CustusX production code.