diff --git a/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.cpp b/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.cpp index 1dfae5dab9..c24003f240 100644 --- a/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.cpp +++ b/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.cpp @@ -1,160 +1,157 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkPhotoacoustic3dVolume.h" #include -mitk::Photoacoustic3dVolume::Photoacoustic3dVolume() +mitk::Photoacoustic3dVolume::Photoacoustic3dVolume(double* data, int xDim, int yDim, int zDim): + m_XDim(xDim), + m_YDim(yDim), + m_ZDim(zDim) { - m_State = UNINITIALIZED; + if(data == nullptr) + mitkThrow() << "You may not initialize a mitk::Photoacoustic3dVolume with a nullptr"; + + m_VolumeData = data; + m_State = INITIALIZED; } mitk::Photoacoustic3dVolume::~Photoacoustic3dVolume() { static std::mutex destructorMutex; destructorMutex.lock(); if(m_VolumeData!= nullptr) { delete[] m_VolumeData; m_VolumeData = nullptr; } m_State = UNINITIALIZED; destructorMutex.unlock(); } mitk::Photoacoustic3dVolume::Pointer mitk::Photoacoustic3dVolume::DeepCopy() { AssertState(INITIALIZED); - mitk::Photoacoustic3dVolume::Pointer copy = mitk::Photoacoustic3dVolume::New(); long length = m_XDim*m_YDim*m_ZDim; double* data = new double[length]; memcpy(data, m_VolumeData, length*sizeof(double)); - copy->Initialize(data, m_XDim, m_YDim, m_ZDim); - return copy; -} - -void mitk::Photoacoustic3dVolume::Initialize(double* data, int xDim, int yDim, int zDim) -{ - AssertState(UNINITIALIZED); - if(data == nullptr) - mitkThrow() << "You may not initialize a mitk::Photoacoustic3dVolume with a nullptr"; - m_VolumeData = data; - m_XDim = xDim; - m_YDim = yDim; - m_ZDim = zDim; - m_State = INITIALIZED; + return mitk::Photoacoustic3dVolume::New(data, m_XDim, m_YDim, m_ZDim); } mitk::Image::Pointer mitk::Photoacoustic3dVolume::ConvertToMitkImage() { AssertState(INITIALIZED); mitk::Image::Pointer resultImage = mitk::Image::New(); mitk::PixelType TPixel = mitk::MakeScalarPixelType(); - unsigned int* dimensionsOfImage = new unsigned int[3]; - // Copy dimensions + unsigned int* dimensionsOfImage = new unsigned int[NUMBER_OF_SPATIAL_DIMENSIONS]; dimensionsOfImage[0] = m_YDim; dimensionsOfImage[1] = m_XDim; dimensionsOfImage[2] = m_ZDim; MITK_DEBUG << "Initializing image..."; - resultImage->Initialize(TPixel, 3, dimensionsOfImage, 1); + resultImage->Initialize(TPixel, NUMBER_OF_SPATIAL_DIMENSIONS, dimensionsOfImage, 1); resultImage->SetImportVolume(m_VolumeData); MITK_DEBUG << "Initializing image...[Done]"; return resultImage; } double mitk::Photoacoustic3dVolume::GetData(int x, int y, int z) { - return m_VolumeData[z * m_XDim * m_YDim + x * m_YDim + y]; + AssertState(INITIALIZED); + return m_VolumeData[GetIndex(x, y, z)]; } void mitk::Photoacoustic3dVolume::SetData(double data, int x, int y, int z) { - m_VolumeData[z * m_XDim * m_YDim + x * m_YDim + y] = data; + AssertState(INITIALIZED); + m_VolumeData[GetIndex(x, y, z)] = data; } int mitk::Photoacoustic3dVolume::GetXDim() { + AssertState(INITIALIZED); return m_XDim; } int mitk::Photoacoustic3dVolume::GetYDim() { + AssertState(INITIALIZED); return m_YDim; } int mitk::Photoacoustic3dVolume::GetZDim() { + AssertState(INITIALIZED); return m_ZDim; } mitk::Photoacoustic3dVolume::Pointer mitk::Photoacoustic3dVolume::CreateDifferenceImage(mitk::Photoacoustic3dVolume::Pointer other, bool relative) { AssertState(INITIALIZED); - mitk::Photoacoustic3dVolume::Pointer result = mitk::Photoacoustic3dVolume::New(); - double* resultArray = (double*) malloc(m_XDim*m_YDim*m_ZDim*sizeof(double)); for(int z=0; zGetData(x, y, z); + resultArray[GetIndex(x, y, z)] = GetData(x, y, z) / other->GetData(x, y, z); else - resultArray[z * m_XDim * m_YDim + x * m_YDim + y] = GetData(x, y, z) - other->GetData(x, y, z); + resultArray[GetIndex(x, y, z)] = GetData(x, y, z) - other->GetData(x, y, z); } } } - result->Initialize(resultArray, m_XDim, m_YDim, m_ZDim); - - return result; + return New(resultArray, m_XDim, m_YDim, m_ZDim); } mitk::Photoacoustic3dVolume::Pointer mitk::Photoacoustic3dVolume::ThresholdImage(double threshold) { AssertState(INITIALIZED); - mitk::Photoacoustic3dVolume::Pointer result = mitk::Photoacoustic3dVolume::New(); - double* resultArray = (double*) malloc(m_XDim*m_YDim*m_ZDim*sizeof(double)); for(int z=0; zthreshold?1:0; + if(GetData(x, y, z)>threshold) + resultArray[GetIndex(x, y, z)] = 1; + else + resultArray[GetIndex(x, y, z)] = 0; } } } - result->Initialize(resultArray, m_XDim, m_YDim, m_ZDim); + return New(resultArray, m_XDim, m_YDim, m_ZDim); +} - return result; +int mitk::Photoacoustic3dVolume::GetIndex(int x, int y, int z) +{ + return z * m_XDim * m_YDim + x * m_YDim + y; } diff --git a/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.h b/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.h index 3969aa8649..456e8740ec 100644 --- a/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.h +++ b/Modules/PhotoacousticSimulation/src/Algorithms/mitkPhotoacoustic3dVolume.h @@ -1,132 +1,134 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKPHOTOACOUSTIC3dVOLUME_H #define MITKPHOTOACOUSTIC3dVOLUME_H #include "MitkPhotoacousticSimulationExports.h" //Includes for smart pointer usage #include "mitkCommon.h" #include "itkLightObject.h" #include "mitkPhotoacousticStatefulObject.h" #include namespace mitk { class MITKPHOTOACOUSTICSIMULATION_EXPORT Photoacoustic3dVolume : public itk::LightObject, public mitk::PhotoacousticStatefulObject { public: mitkClassMacroItkParent(mitk::Photoacoustic3dVolume, itk::LightObject) - itkFactorylessNewMacro(Self) - - /** - * @brief Initialize initializes this volume with the given pointer to the data array. - * It is assumed, that the array is of dimension xDim|yDim|zDim. - * The Photoacoustic3DVolume will handle memory management of the array. - * - * @param data a pointer to the data array - * @param xDim x dimension of the data - * @param yDim y dimension of the data - * @param zDim z dimension of the data - */ - void Initialize(double* data, int xDim, int yDim, int zDim); + mitkNewMacro4Param(Self, double*, int, int, int) /** * @brief ConvertToMitkImage * Creates a new mitk::Image and copies the data array into it. * * @return a mitk::Image::Pointer containing a copy (!) of the data of this array. */ mitk::Image::Pointer ConvertToMitkImage(); /** * @brief GetData. Returns data at wanted position. For performance reasons, this method will not check, * if the specified position it within the array. Please use the GetXDim(), GetYDim() and GetZDim() methods * to check for yourself if necessary. * * @param x * @param y * @param z * @return the data contained within the data array held by this Photoacoustic3dVolume at * positon x|y|z. */ double GetData(int x, int y, int z); /** * @brief SetData * @param data * @param x * @param y * @param z */ void SetData(double data, int x, int y, int z); /** * @brief GetXDim * @return size of x dimension of this Photoacoustic3dVolume */ int GetXDim(); /** * @brief GetYDim * @return size of y dimension of this Photoacoustic3dVolume */ int GetYDim(); /** * @brief GetZDim * @return size of z dimension of this Photoacoustic3dVolume */ int GetZDim(); /** * @brief DeepCopy * @return a deep copy of this Photoacoustic3dVolume. the old volume remains intact and memory is NOT shared * between the objects. */ mitk::Photoacoustic3dVolume::Pointer DeepCopy(); /** * @brief CreateDifferenceImage creates a difference image of this volume and another. * @param other * @param relative if true, the images will be divided, if false they will be subtracted. * @return a new volume. The data of the original images is not affected. */ mitk::Photoacoustic3dVolume::Pointer CreateDifferenceImage(mitk::Photoacoustic3dVolume::Pointer other, bool relative = false); /** * @brief ThresholdImage applies a binary threshold filter to this image. * @param threshold * @return a new binary volume. The data of the current instance will not be affected. */ mitk::Photoacoustic3dVolume::Pointer ThresholdImage(double threshold); protected: - Photoacoustic3dVolume(); + /** + * @brief Initialize initializes this volume with the given pointer to the data array. + * It is assumed, that the array is of dimension xDim|yDim|zDim. + * The Photoacoustic3DVolume will handle memory management of the array. + * + * @param data a pointer to the data array + * @param xDim x dimension of the data + * @param yDim y dimension of the data + * @param zDim z dimension of the data + */ + Photoacoustic3dVolume(double* data, int xDim, int yDim, int zDim); virtual ~Photoacoustic3dVolume(); + const int NUMBER_OF_SPATIAL_DIMENSIONS = 3; + double* m_VolumeData; int m_XDim; int m_YDim; int m_ZDim; + + int GetIndex(int x, int y, int z); }; } #endif // MITKPHOTOACOUSTIC3dVOLUME_H