Medical Imaging Interaction Toolkit  2018.4.99-9a29ffc6
Medical Imaging Interaction Toolkit
mitkPAVolume.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include "mitkPAVolume.h"
14 #include <mitkImageWriteAccessor.h>
15 #include <mitkImageReadAccessor.h>
16 #include <mutex>
17 
19  unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
20 {
21  MITK_INFO << "Initialized by data*";
22  if (data == nullptr)
23  mitkThrow() << "You may not initialize a mitk::Volume with a nullptr";
25 
26  auto* dimensions = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS];
27  dimensions[0] = yDim;
28  dimensions[1] = xDim;
29  dimensions[2] = zDim;
30  m_XDim = xDim;
31  m_YDim = yDim;
32  m_ZDim = zDim;
33  mitk::PixelType pixelType = mitk::MakeScalarPixelType<double>();
34 
35  m_InternalMitkImage->Initialize(pixelType, NUMBER_OF_SPATIAL_DIMENSIONS, dimensions);
36  m_InternalMitkImage->SetImportVolume(data, Image::ImportMemoryManagementType::CopyMemory);
37 
38  SetSpacing(spacing);
39 
41 
42  delete data;
43 }
44 
46 {
47  MITK_INFO << "Initialized by mitk::Image";
48 
49  if (image.IsNull())
50  mitkThrow() << "You may not initialize a mitk::Volume with a null reference to an mitk image";
51 
52  unsigned int* dimensions = image->GetDimensions();
53  m_YDim = dimensions[0];
54  m_XDim = dimensions[1];
55  m_ZDim = dimensions[2];
56 
58 
60 }
61 
63 {
64  return m_InternalMitkImage->GetGeometry()->GetSpacing()[0];
65 }
66 
67 void mitk::pa::Volume::SetSpacing(double spacing)
68 {
69  const mitk::ScalarType spacingArray[]{ spacing, spacing, spacing };
70  m_InternalMitkImage->SetSpacing(spacingArray);
71 }
72 
74 {
75  m_InternalMitkImage = nullptr;
76 }
77 
78 mitk::pa::Volume::Pointer mitk::pa::Volume::New(double* data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
79 {
80  mitk::pa::Volume::Pointer smartPtr = new mitk::pa::Volume(data, xDim, yDim, zDim, spacing);
81  smartPtr->UnRegister();
82  return smartPtr;
83 }
84 
86 {
87  mitk::pa::Volume::Pointer smartPtr = new mitk::pa::Volume(image);
88  smartPtr->UnRegister();
89  return smartPtr;
90 }
91 
93 {
94  return m_InternalMitkImage;
95 }
96 
97 mitk::pa::Volume::Pointer mitk::pa::Volume::DeepCopy()
98 {
99  long length = GetXDim()*GetYDim()*GetZDim();
100  auto* data = new double[length];
101  memcpy(data, GetData(), length * sizeof(double));
102 
103  return mitk::pa::Volume::New(data, GetXDim(), GetYDim(), GetZDim(), GetSpacing());
104 }
105 
106 double mitk::pa::Volume::GetData(unsigned int x, unsigned int y, unsigned int z)
107 {
108  return m_FastAccessDataPointer[GetIndex(x, y, z)];
109 }
110 
111 void mitk::pa::Volume::SetData(double data, unsigned int x, unsigned int y, unsigned int z)
112 {
113  m_FastAccessDataPointer[GetIndex(x, y, z)] = data;
114 }
115 
117 {
118  return m_XDim;
119 }
120 
122 {
123  return m_YDim;
124 }
125 
127 {
128  return m_ZDim;
129 }
130 
132 {
134  return (double*)imgRead.GetData();
135 }
136 
137 long long mitk::pa::Volume::GetIndex(unsigned int x, unsigned int y, unsigned int z)
138 {
139 #ifdef _DEBUG
140 
141  if (x < 0 || x >(GetXDim() - 1) || y < 0 || y >(GetYDim() - 1) || z < 0 || z >(GetZDim() - 1))
142  {
143  MITK_ERROR << "Index out of bounds at " << x << "|" << y << "|" << z;
144  mitkThrow() << "Index out of bounds exception!";
145  }
146 
147 #endif
148  return ((long long)z) * m_XDim * m_YDim + ((long long)x) * m_YDim + ((long long)y);
149 }
void SetData(double data, unsigned int x, unsigned int y, unsigned int z)
SetData.
unsigned int m_YDim
Definition: mitkPAVolume.h:139
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
double ScalarType
unsigned int m_ZDim
Definition: mitkPAVolume.h:140
void * GetData()
Gives full data access.
Image::Pointer AsMitkImage()
returns the Volume instance as an mitk image
unsigned int GetYDim()
GetYDim.
unsigned int m_XDim
Definition: mitkPAVolume.h:138
unsigned int GetXDim()
GetXDim.
unsigned int GetZDim()
GetZDim.
The Volume class is designed to encapsulate volumetric information and to provide convenience methods...
Definition: mitkPAVolume.h:30
#define mitkThrow()
double * GetData() const
Volume::Pointer DeepCopy()
DeepCopy.
Volume(double *data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
Initialize initializes this volume with the given pointer to the data array. It is assumed...
double * m_FastAccessDataPointer
Definition: mitkPAVolume.h:141
mitk::Image::Pointer image
static Pointer New()
Image::Pointer m_InternalMitkImage
Definition: mitkPAVolume.h:135
void SetSpacing(double spacing)
~Volume() override
const int NUMBER_OF_SPATIAL_DIMENSIONS
Definition: mitkPAVolume.h:133
ImageWriteAccessor class to get locked write-access for a particular image part.
static Volume::Pointer New(double *data, unsigned int xDim, unsigned int yDim, unsigned int zDim, double spacing)
returns smartpointer reference to a new instance of this objects. The given data array will be freed ...
long long GetIndex(unsigned int x, unsigned int y, unsigned int z)
convenience method to enable consistent access to the dat array
Class for defining the data type of pixels.
Definition: mitkPixelType.h:51