CustusX  2023.01.05-dev+develop.0da12
An IGT application
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxtestUtilities.h"
13 
14 #include "vtkImageData.h"
15 #include "cxImage.h"
16 #include "cxVolumeHelpers.h"
17 #include "cxTypeConversions.h"
18 #include "cxDataLocations.h"
19 
20 #ifdef CX_WINDOWS
21  #include <Windows.h>
22 #endif
23 
24 namespace cxtest
25 {
26 /*
27 // --------------------------------------------------------
28 TestDataStorage* TestDataStorage::mInstance = NULL; ///< static member
29 // --------------------------------------------------------
30 void TestDataStorage::shutdown()
31 {
32  delete mInstance;
33  mInstance = NULL;
34 }
35 TestDataStorage* TestDataStorage::getInstance()
36 {
37  if (!mInstance)
38  mInstance = new TestDataStorage();
39  return mInstance;
40 }
41 // --------------------------------------------------------
42 // --------------------------------------------------------
43 // --------------------------------------------------------
44 */
45 
46 QString Utilities::getDataRoot(QString suffix)
47 {
48  QString root = cx::DataLocations::getTestDataPath();
49  if (suffix.isEmpty())
50  return root;
51  else
52  return QString("%1/%2").arg(root).arg(suffix);
53 }
54 
55 
56 vtkImageDataPtr Utilities::create3DVtkImageData(Eigen::Array3i dim, const unsigned int voxelValue)
57 {
58  return cx::generateVtkImageData(dim, cx::Vector3D(1,1,1), voxelValue);
59 }
60 
61 cx::ImagePtr Utilities::create3DImage(Eigen::Array3i dim, const unsigned int voxelValue)
62 {
63  return create3DImage(dim, cx::Vector3D(1,1,1), voxelValue);
64 }
65 
66 cx::ImagePtr Utilities::create3DImage(Eigen::Array3i dim, cx::Vector3D spacing, const unsigned int voxelValue)
67 {
68  vtkImageDataPtr vtkImageData = cx::generateVtkImageData(dim, spacing, voxelValue);
69  QString unique_string = qstring_cast(reinterpret_cast<long>(vtkImageData.GetPointer()));
70  QString imagesUid = QString("TESTUID_%2_%1").arg(unique_string);
71  cx::ImagePtr image(new cx::Image(imagesUid, vtkImageData));
72 
73  return image;
74 }
75 
76 
77 std::vector<cx::ImagePtr> Utilities::create3DImages(unsigned int count, Eigen::Array3i dim, const unsigned int voxelValue)
78 {
79  std::vector<cx::ImagePtr> retval;
80  for (unsigned i=0; i<count; ++i)
81  {
82  cx::ImagePtr image = cxtest::Utilities::create3DImage(dim, voxelValue);
83  retval.push_back(image);
84  }
85  return retval;
86 }
87 
88 unsigned int Utilities::getNumberOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component)
89 {
90  if (!image)
91  return 0;
92 
93  unsigned char* ptr = reinterpret_cast<unsigned char*>(image->GetScalarPointer());
94  unsigned int pixelCount = 0;
95  for (unsigned i = 0; i < image->GetDimensions()[0]*image->GetDimensions()[1]*image->GetDimensions()[2]; ++i)
96  {
97  if (ptr[i*image->GetNumberOfScalarComponents()+component] > threshold)
98  ++pixelCount;
99  }
100  return pixelCount;
101 }
102 
104 {
105  return getNumberOfVoxelsAboveThreshold(image, 0);
106 }
107 
108 double Utilities::getFractionOfVoxelsAboveThreshold(vtkImageDataPtr image, int threshold, int component)
109 {
110  unsigned int hits = getNumberOfVoxelsAboveThreshold(image, threshold, component);
111  Eigen::Array3i dim(image->GetDimensions());
112  unsigned int totalPixels = dim[0]*dim[1]*dim[2];
113  if (totalPixels==0)
114  return -1;
115  return double(hits)/double(totalPixels);
116 }
117 
118 void Utilities::sleep_sec(int seconds)
119 {
120 #ifndef CX_WINDOWS
121  sleep(seconds); //seconds
122 #else
123  Sleep(seconds*1000); //milliseconds
124 #endif
125 }
126 
127 } /* namespace cxtest */
QString qstring_cast(const T &val)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
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:45
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:42
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)