NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxtestUtilities.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 "cxtestUtilities.h"
34 
35 #include "vtkImageData.h"
36 #include "cxImage.h"
37 #include "cxVolumeHelpers.h"
38 #include "cxTypeConversions.h"
39 #include "cxDataLocations.h"
40 
41 #ifdef CX_WINDOWS
42  #include <Windows.h>
43 #endif
44 
45 namespace cxtest
46 {
47 /*
48 // --------------------------------------------------------
49 TestDataStorage* TestDataStorage::mInstance = NULL; ///< static member
50 // --------------------------------------------------------
51 void TestDataStorage::shutdown()
52 {
53  delete mInstance;
54  mInstance = NULL;
55 }
56 TestDataStorage* TestDataStorage::getInstance()
57 {
58  if (!mInstance)
59  mInstance = new TestDataStorage();
60  return mInstance;
61 }
62 // --------------------------------------------------------
63 // --------------------------------------------------------
64 // --------------------------------------------------------
65 */
66 
67 QString Utilities::getDataRoot(QString suffix)
68 {
69  QString root = cx::DataLocations::getTestDataPath();
70  if (suffix.isEmpty())
71  return root;
72  else
73  return QString("%1/%2").arg(root).arg(suffix);
74 }
75 
76 
77 vtkImageDataPtr Utilities::create3DVtkImageData(Eigen::Array3i dim, const unsigned int voxelValue)
78 {
79  return cx::generateVtkImageData(dim, cx::Vector3D(1,1,1), voxelValue);
80 }
81 
82 cx::ImagePtr Utilities::create3DImage(Eigen::Array3i dim, const unsigned int voxelValue)
83 {
84  return create3DImage(dim, cx::Vector3D(1,1,1), voxelValue);
85 }
86 
87 cx::ImagePtr Utilities::create3DImage(Eigen::Array3i dim, cx::Vector3D spacing, const unsigned int voxelValue)
88 {
89  vtkImageDataPtr vtkImageData = cx::generateVtkImageData(dim, spacing, voxelValue);
90  QString unique_string = qstring_cast(reinterpret_cast<long>(vtkImageData.GetPointer()));
91  QString imagesUid = QString("TESTUID_%2_%1").arg(unique_string);
92  cx::ImagePtr image(new cx::Image(imagesUid, vtkImageData));
93 
94  return image;
95 }
96 
97 
98 std::vector<cx::ImagePtr> Utilities::create3DImages(unsigned int count, Eigen::Array3i dim, const unsigned int voxelValue)
99 {
100  std::vector<cx::ImagePtr> retval;
101  for (unsigned i=0; i<count; ++i)
102  {
103  cx::ImagePtr image = cxtest::Utilities::create3DImage(dim, voxelValue);
104  retval.push_back(image);
105  }
106  return retval;
107 }
108 
109 unsigned int Utilities::getNumberOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component)
110 {
111  if (!image)
112  return 0;
113 
114  unsigned char* ptr = reinterpret_cast<unsigned char*>(image->GetScalarPointer());
115  unsigned int pixelCount = 0;
116  for (unsigned i = 0; i < image->GetDimensions()[0]*image->GetDimensions()[1]*image->GetDimensions()[2]; ++i)
117  {
118  if (ptr[i*image->GetNumberOfScalarComponents()+component] > threshold)
119  ++pixelCount;
120  }
121  return pixelCount;
122 }
123 
125 {
126  return getNumberOfVoxelsAboveThreshold(image, 0);
127 }
128 
129 double Utilities::getFractionOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component)
130 {
131  unsigned int hits = getNumberOfVoxelsAboveThreshold(image, threshold, component);
132  Eigen::Array3i dim(image->GetDimensions());
133  unsigned int totalPixels = dim[0]*dim[1]*dim[2];
134  if (totalPixels==0)
135  return -1;
136  return double(hits)/double(totalPixels);
137 }
138 
139 void Utilities::sleep_sec(int seconds)
140 {
141 #ifndef CX_WINDOWS
142  sleep(seconds); //seconds
143 #else
144  Sleep(seconds*1000); //milliseconds
145 #endif
146 }
147 
148 } /* namespace cxtest */
QString qstring_cast(const T &val)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
static std::vector< cx::ImagePtr > create3DImages(unsigned int imageCount, Eigen::Array3i dim=Eigen::Array3i(3, 3, 3), const unsigned int voxelValue=100)
static cx::ImagePtr create3DImage(Eigen::Array3i dim=Eigen::Array3i(3, 3, 3), const unsigned int voxelValue=100)
static void sleep_sec(int seconds)
static double getFractionOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component=0)
A volumetric data set.
Definition: cxImage.h:66
static QString getTestDataPath()
return path to test data folder
vtkImageDataPtr generateVtkImageData(Eigen::Array3i dim, Vector3D spacing, const unsigned char initValue, int components)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
static unsigned int getNumberOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component=0)
static QString getDataRoot(QString suffix="")
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
static unsigned int getNumberOfNonZeroVoxels(vtkImageDataPtr image)
static vtkImageDataPtr create3DVtkImageData(Eigen::Array3i dim=Eigen::Array3i(3, 3, 3), const unsigned int voxelValue=100)