Skip to content

Commit

Permalink
Merge pull request Kitware#45 from malaterre/element-spacing-as-double
Browse files Browse the repository at this point in the history
Element spacing as double
  • Loading branch information
jcfr authored Dec 1, 2017
2 parents 55395e3 + 8c1168b commit 9a53c73
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 42 deletions.
102 changes: 85 additions & 17 deletions src/metaImage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h> // for memcpy
#include <stdlib.h> // for atoi
#include <math.h>
#include <algorithm> // for std::min & std::max

#if defined (__BORLANDC__) && (__BORLANDC__ >= 0x0580)
#include <mem.h>
Expand Down Expand Up @@ -156,14 +157,13 @@ MetaImage(MetaImage *_im)
}

//
MetaImage::
MetaImage(int _nDims,
void MetaImage::
InitHelper(int _nDims,
const int * _dimSize,
const float * _elementSpacing,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void *_elementData)
:MetaObject()
{
if(META_DEBUG)
{
Expand Down Expand Up @@ -196,10 +196,46 @@ MetaImage(int _nDims,

}

//
MetaImage::
MetaImage(int _nDims,
const int * _dimSize,
const float * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void *_elementData)
:MetaObject()
{
// Only consider at most 10 element of spacing:
// See MetaObject::InitializeEssential(_nDims)
double tmpElementSpacing[10];
int ndims = std::max( std::min( _nDims, 10 ), 0);
for( int i = 0; i < ndims; ++i )
{
tmpElementSpacing[i] = static_cast<double>(_elementSpacing[i]);
}
InitHelper(_nDims, _dimSize, tmpElementSpacing, _elementType,
_elementNumberOfChannels, _elementData);
}

//
MetaImage::
MetaImage(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void *_elementData)
:MetaObject()
{
InitHelper(_nDims, _dimSize, _elementSpacing, _elementType,
_elementNumberOfChannels, _elementData);
}

//
MetaImage::
MetaImage(int _x, int _y,
float _elementSpacingX, float _elementSpacingY,
double _elementSpacingX, double _elementSpacingY,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels, void *_elementData)
:MetaObject()
Expand All @@ -218,7 +254,7 @@ MetaImage(int _x, int _y,
ds[0] = _x;
ds[1] = _y;

float es[2];
double es[2];
es[0] = _elementSpacingX;
es[1] = _elementSpacingY;

Expand Down Expand Up @@ -247,9 +283,9 @@ MetaImage(int _x, int _y,
//
MetaImage::
MetaImage(int _x, int _y, int _z,
float _elementSpacingX,
float _elementSpacingY,
float _elementSpacingZ,
double _elementSpacingX,
double _elementSpacingY,
double _elementSpacingZ,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void *_elementData)
Expand All @@ -270,7 +306,7 @@ MetaImage(int _x, int _y, int _z,
ds[1] = _y;
ds[2] = _z;

float es[3];
double es[3];
es[0] = _elementSpacingX;
es[1] = _elementSpacingY;
es[2] = _elementSpacingZ;
Expand Down Expand Up @@ -449,10 +485,10 @@ void MetaImage::Clear(void)

m_HeaderSize = 0;

memset(m_SequenceID, 0, 4*sizeof(float));
memset(m_SequenceID, 0, sizeof(m_SequenceID));

m_ElementSizeValid = false;
memset(m_ElementSize, 0, 10*sizeof(float));
memset(m_ElementSize, 0, sizeof(m_ElementSize));

m_ElementType = MET_NONE;

Expand Down Expand Up @@ -504,6 +540,27 @@ InitializeEssential(int _nDims,
int _elementNumberOfChannels,
void * _elementData,
bool _allocElementMemory)
{
// Only consider at most 10 element of spacing:
// See MetaObject::InitializeEssential(_nDims)
double tmpElementSpacing[10];
int ndims = std::max( std::min( _nDims, 10 ), 0);
for( int i = 0; i < ndims; ++i )
{
tmpElementSpacing[i] = static_cast<double>(_elementSpacing[i]);
}
return InitializeEssential(_nDims, _dimSize, tmpElementSpacing, _elementType,
_elementNumberOfChannels, _elementData, _allocElementMemory);
}

bool MetaImage::
InitializeEssential(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void * _elementData,
bool _allocElementMemory)
{
if(META_DEBUG)
{
Expand Down Expand Up @@ -683,27 +740,38 @@ ElementSizeValid(bool _elementSizeValid)
m_ElementSizeValid = _elementSizeValid;
}

const float * MetaImage::
const double * MetaImage::
ElementSize(void) const
{
return m_ElementSize;
}

float MetaImage::
double MetaImage::
ElementSize(int _i) const
{
return m_ElementSize[_i];
}

void MetaImage::
ElementSize(const double *_elementSize)
{
memcpy(m_ElementSize, _elementSize, m_NDims*sizeof(*m_ElementSize));
m_ElementSizeValid = true;
}

void MetaImage::
ElementSize(const float *_elementSize)
{
memcpy(m_ElementSize, _elementSize, m_NDims*sizeof(float));
for(int i = 0; i < m_NDims; ++i)
{
m_ElementSize[i] = static_cast<double>(_elementSize[i]);
}
m_ElementSizeValid = true;
}


void MetaImage::
ElementSize(int _i, float _value)
ElementSize(int _i, double _value)
{
m_ElementSize[_i] = _value;
m_ElementSizeValid = true;
Expand Down Expand Up @@ -2421,7 +2489,7 @@ M_Read(void)
int i;
for(i=0; i<m_NDims; i++)
{
m_ElementSize[i] = (float)(mF->value[i]);
m_ElementSize[i] = mF->value[i];
}
mF = MET_GetFieldRecord("ElementSpacing", &m_Fields);
if(mF && !mF->defined)
Expand Down
54 changes: 42 additions & 12 deletions src/metaImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,24 @@ class METAIO_EXPORT MetaImage : public MetaObject
int _elementNumberOfChannels=1,
void *_elementData=NULL);

MetaImage(int _nDims,
const int * _dimSize,
const double *_elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels=1,
void *_elementData=NULL);

MetaImage(int _x, int _y,
float _elementSpacingX,
float _elementSpacingY,
double _elementSpacingX,
double _elementSpacingY,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels=1,
void *_elementData=NULL);

MetaImage(int _x, int _y, int _z,
float _elementSpacingX,
float _elementSpacingY,
float _elementSpacingZ,
double _elementSpacingX,
double _elementSpacingY,
double _elementSpacingZ,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels=1,
void *_elementData=NULL);
Expand All @@ -105,6 +112,7 @@ class METAIO_EXPORT MetaImage : public MetaObject

void Clear(void) MET_OVERRIDE;

// Legacy for floating point elementSpacing
bool InitializeEssential(int _nDims,
const int * _dimSize,
const float * _elementSpacing,
Expand All @@ -113,6 +121,14 @@ class METAIO_EXPORT MetaImage : public MetaObject
void *_elementData=NULL,
bool _allocElementMemory=true);

bool InitializeEssential(int _nDims,
const int * _dimSize,
const double * _elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels=1,
void *_elementData=NULL,
bool _allocElementMemory=true);

//
//
//
Expand Down Expand Up @@ -155,12 +171,13 @@ class METAIO_EXPORT MetaImage : public MetaObject
// Optional Field
// Physical size (in MM) of each element in the image
// (0 = xSize, 1 = ySize, 2 = zSize)
bool ElementSizeValid(void) const;
void ElementSizeValid(bool _elementSizeValid);
const float * ElementSize(void) const;
float ElementSize(int i) const;
void ElementSize(const float * _pointSize);
void ElementSize(int _i, float _value);
bool ElementSizeValid(void) const;
void ElementSizeValid(bool _elementSizeValid);
const double * ElementSize(void) const;
double ElementSize(int i) const;
void ElementSize(const double * _pointSize);
void ElementSize(const float * _pointSize); // legacy
void ElementSize(int _i, double _value);

MET_ValueEnumType ElementType(void) const;
void ElementType(MET_ValueEnumType _elementType);
Expand Down Expand Up @@ -307,7 +324,7 @@ class METAIO_EXPORT MetaImage : public MetaObject
float m_SequenceID[4];

bool m_ElementSizeValid;
float m_ElementSize[10];
double m_ElementSize[10];

MET_ValueEnumType m_ElementType;

Expand Down Expand Up @@ -377,6 +394,19 @@ class METAIO_EXPORT MetaImage : public MetaObject
METAIO_STL::string M_GetTagValue(const METAIO_STL::string & buffer,
const char* tag) const;

////
//
// PRIVATE
//
////
private:
void InitHelper(int _nDims,
const int * _dimSize,
const double *_elementSpacing,
MET_ValueEnumType _elementType,
int _elementNumberOfChannels,
void *_elementData);

};

#if (METAIO_USE_NAMESPACE)
Expand Down
28 changes: 19 additions & 9 deletions src/metaObject.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -906,20 +906,20 @@ AnatomicalOrientation(int _dim, char _ao)

//
//
const float * MetaObject::
const double * MetaObject::
ElementSpacing(void) const
{
return m_ElementSpacing;
}

float MetaObject::
double MetaObject::
ElementSpacing(int _i) const
{
return m_ElementSpacing[_i];
}

void MetaObject::
ElementSpacing(const float * _elementSpacing)
ElementSpacing(const double * _elementSpacing)
{
int i;
for(i=0; i<m_NDims; i++)
Expand All @@ -929,7 +929,17 @@ ElementSpacing(const float * _elementSpacing)
}

void MetaObject::
ElementSpacing(int _i, float _value)
ElementSpacing(const float * _elementSpacing)
{
int i;
for(i=0; i<m_NDims; i++)
{
m_ElementSpacing[i] = static_cast<double>(_elementSpacing[i]);
}
}

void MetaObject::
ElementSpacing(int _i, double _value)
{
m_ElementSpacing[_i] = _value;
}
Expand Down Expand Up @@ -1057,10 +1067,10 @@ Clear(void)
strcpy(m_ObjectSubTypeName, "");
strcpy(m_Name, "");

memset(m_Offset, 0, 10*sizeof(float));
memset(m_TransformMatrix, 0, 100*sizeof(float));
memset(m_CenterOfRotation, 0, 10*sizeof(float));
memset(m_Color, 0, 4*sizeof(float));
memset(m_Offset, 0, sizeof(m_Offset));
memset(m_TransformMatrix, 0, sizeof(m_TransformMatrix));
memset(m_CenterOfRotation, 0, sizeof(m_CenterOfRotation));
memset(m_Color, 0, sizeof(m_Color));

m_ID = -1;
m_Color[0]=1.0f;
Expand Down Expand Up @@ -1741,7 +1751,7 @@ M_Read(void)
{
for(i=0; i<mF->length && i < 10; i++)
{
m_ElementSpacing[i] = static_cast<float>( mF->value[i] );
m_ElementSpacing[i] = mF->value[i];
if (META_DEBUG)
{
METAIO_STREAM::cout << "metaObject: M_Read: elementSpacing["
Expand Down
9 changes: 5 additions & 4 deletions src/metaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class METAIO_EXPORT MetaObject

MET_DistanceUnitsEnumType m_DistanceUnits; // "DistanceUnits = mm"

float m_ElementSpacing[10]; // "ElementSpacing = " 0,0,0
double m_ElementSpacing[10]; // "ElementSpacing = " 0,0,0

float m_Color[4]; // "Color = " 1.0, 0.0, 0.0, 1.0

Expand Down Expand Up @@ -218,10 +218,11 @@ class METAIO_EXPORT MetaObject
// ElementSpacing(...)
// Optional Field
// Physical Spacing (in same units as position)
const float * ElementSpacing(void) const;
float ElementSpacing(int _i) const;
const double * ElementSpacing(void) const;
double ElementSpacing(int _i) const;
void ElementSpacing(const double * _elementSpacing);
void ElementSpacing(const float * _elementSpacing);
void ElementSpacing(int _i, float _value);
void ElementSpacing(int _i, double _value);

// Name(...)
// Optional Field
Expand Down

0 comments on commit 9a53c73

Please sign in to comment.