Fraxinus  16.5.0-fx-rc1
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxImageParameters.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 #include "cxImageParameters.h"
33 #include "cxLogger.h"
34 #include "cxTypeConversions.h"
35 
36 namespace cx
37 {
39  mDim(Eigen::Array3i(0,0,0)),
40  mSpacing(cx::Vector3D(1,1,1)),
41  mParentVolume(""),
42  m_rMd(cx::Transform3D::Identity())
43 {
44 }
45 
46 ImageParameters::ImageParameters(Eigen::Array3i dim, cx::Vector3D spacing, QString parent, cx::Transform3D rMd) :
47  mDim(dim),
48  mSpacing(spacing),
49  mParentVolume(parent),
50  m_rMd(rMd)
51 {
52 }
53 
54 Eigen::Array3i ImageParameters::getDim() const
55 {
56  return mDim;
57 }
58 
59 Eigen::Array3d ImageParameters::getSpacing() const
60 {
61  return mSpacing;
62 }
63 
65 {
66  return (mDim - 1).cast<double>() * mSpacing;
67 }
68 
70 {
71  return this->getBounds().prod();
72 }
73 
75 {
76  Eigen::Array3d dim = bounds / mSpacing;
77  mDim = round(dim).cast<int>();
78  mDim += 1;
79  this->alignSpacingKeepDim(bounds);
80 }
81 
82 void ImageParameters::alignSpacingKeepDim(Eigen::Array3d bounds)
83 {
84  for (unsigned i = 0; i < 3; ++i)
85  {
86  //Set spacing to 1 if one of the axes is degenerated
87  if((mDim[i] == 1) && similar(bounds[i], 0.0))
88  mSpacing[i] = 1;
89  else
90  mSpacing[i] = bounds[i] / double(mDim[i]-1);
91  }
92 }
93 
94 void ImageParameters::setSpacingKeepDim(Eigen::Array3d spacing)
95 {
96  mSpacing = spacing;
97 }
98 
99 void ImageParameters::setDimFromExtent(Eigen::Array3i extent)
100 {
101  mDim = extent + 1;
102 }
103 
108 void ImageParameters::limitVoxelsKeepBounds(unsigned long maxVoxels)
109 {
110  if (this->getNumVoxels() <= maxVoxels)
111  return;
112 
113  if(mDim.minCoeff() == 1) //At least one of the dimensions == 1
114  {
115  reportError("ImageParameters::limitVoxelsKeepBounds() only work with 3D images");
116  return;
117  }
118  // i: input spacing
119  // s: (output) spacing
120  // b: bounds
121  // e: extent
122  // d: dim
123  //
124  // Relations:
125  // e = d-1
126  // b = s*d
127  //
128  // Ve: The volume of the extent, i.e. volume in voxel space
129  // Ve = e0*e1*e2
130  // Ve = pow(pow(voxelCount,1/3)-1, 3) //convert from dim to extent
131  // Vb: The volume of the bounds, ie in physical space
132  // Vb = b0*b1*b2
133  //
134  // Start with the Extent volume:
135  // Ve = e0*e1*e2 = b0/s0 * b1/s1 * b2/s2 = Vb / (s0*s1*s2)
136  //
137  // Keep the ratios between the spacing components from the input spacing:
138  // f1 = i1/i0 = s1/s0
139  // f2 = i2/i0 = s2/s0
140  // This gives:
141  // s1 = f1*s0
142  // s2 = f2*s0
143  //
144  // Plug into extent volume:
145  // Ve = Vb / (s0^3*f1*f2)
146  // s0^3 = Vb/(Ve*f1*f2)
147  // s0 = pow(Vb/(Ve*f1*f2), 1/3)
148 
149  // init
150  Eigen::Array3d b = this->getBounds();
151  Eigen::Array3d i = this->getSpacing();
152  double f1 = i[1] / i[0];
153  double f2 = i[2] / i[0];
154  double Vb = b.prod();
155  double Ve = pow(pow(double(maxVoxels),1.0/3)-1, 3);
156 
157  // generate spacing array
158  Eigen::Array3d s;
159  s[0] = pow(Vb/(Ve*f1*f2), 1.0/3);
160  s[1] = f1 * s[0];
161  s[2] = f2 * s[0];
162 
163  // set values
164  mSpacing = s;
165  Eigen::Array3i e = (b/s).cast<int>();
166  // extents of 1 gives unstable results. extent 0 is an error.
167 // e = e.max(Eigen::Array3i(1,1,1));
168  this->setDimFromExtent(e);
169  this->alignSpacingKeepDim(b); // change spacing to keep bounds
170 }
171 
177 {
178  Eigen::Array3d b = this->getBounds();
179  double voxelCount = this->getNumVoxels();
180  double Vb = b.prod();
181  double Ve = pow(pow(double(voxelCount),1.0/3)-1, 3);
182  double spacing = pow(Vb/Ve, 1.0/3);
183  Eigen::Array3d s(spacing,spacing,spacing);
184 
185  // set values
186  mSpacing = Eigen::Array3d(spacing,spacing,spacing);
187  Eigen::Array3i e = (b/s).cast<int>();
188  this->setDimFromExtent(e);
189 }
190 
191 void ImageParameters::print(std::ostream& s, vtkIndent indent)
192 {
193  s << indent << "Dim: " << mDim << std::endl;
194  s << indent << "Spacing: " << mSpacing << std::endl;
195  s << indent << "Bounds: " << this->getBounds() << std::endl;
196  s << indent << "NumVoxels: " << this->getNumVoxels() << std::endl;
197  s << indent << "Parent Volume: " << mParentVolume << std::endl;
198  s << indent << "rMd:\n" << m_rMd << std::endl;
199 }
200 
201 
202 }// namespace cx
void reportError(QString msg)
Definition: cxLogger.cpp:92
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void print(std::ostream &s, vtkIndent indent)
void limitVoxelsKeepBounds(unsigned long maxVolumeSize)
void setDimKeepBoundsAlignSpacing(Eigen::Array3d bounds)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Eigen::Array3i getDim() const
unsigned long getNumVoxels() const
Eigen::Array3d getBounds()
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Eigen::Array3d getSpacing() const
Vector3D round(const Vector3D &a)
Definition: cxVector3D.cpp:96
void setSpacingKeepDim(Eigen::Array3d spacing)