Skip to content

Commit

Permalink
ENH: Added option to set scaling factor for image intensity for 3ddos…
Browse files Browse the repository at this point in the history
…e file reader

SlicerRt#73
  • Loading branch information
anna-ilina committed Oct 26, 2017
1 parent 4f9d00c commit 0276e44
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,50 +58,47 @@ vtkSlicerDosxyzNrc3dDoseFileReaderLogic::~vtkSlicerDosxyzNrc3dDoseFileReaderLogi
{
}



//----------------------------------------------------------------------------
bool vtkSlicerDosxyzNrc3dDoseFileReaderLogic::AreEqualWithTolerance(double a, double b)
{
return fabs(a - b) < MAX_TOLERANCE_SPACING;
}



//----------------------------------------------------------------------------
void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//


//----------------------------------------------------------------------------
void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* filename)
void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* filename, float intensityScalingFactor/*=1.0*/)
{
ifstream readFileStream(filename);
if (!readFileStream)
{
vtkErrorMacro("LoadDosxyzNrc3dDoseFile: The specified file could not be opened.");
return;
}

ifstream readFileStream(filename); //todo test that this doesn't crash when filename is null

if (!readFileStream){
vtkErrorMacro("LoadDosxyzNrc3dDoseFile: The specified file could not be opened.");
return;
if (intensityScalingFactor == 0)
{
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Invalid scaling factor of 0 found, setting default value of 1");
intensityScalingFactor = 1.0;
}

int size[3] = { 0, 0, 0 };
double spacing[3] = { 0, 0, 0 };
double origin[3] = { 0, 0, 0 };
std::string title = "";

// read in block 1 (number of voxels in x, y, z directions)
readFileStream >> size[0] >> size[1] >> size[2];
int numTotalVoxels = size[0] * size[1] * size[2];


if (size[0] <= 0 || size[1] <= 0 || size[2] <= 0)
{
vtkErrorMacro("LoadDosxyzNrc3dDoseFile: Number of voxels in X, Y, or Z direction must be greater than zero." << "numVoxelsX " << size[0] << ", numVoxelsY " << size[1] << ", numVoxelsZ " << size[2]);
vtkErrorMacro("LoadDosxyzNrc3dDoseFile: Number of voxels in X, Y, or Z direction must be greater than zero." << "numVoxelsX " << size[0] << ", numVoxelsY " << size[1] << ", numVoxelsZ " << size[2]);
return;
}


std::vector<double> voxelBoundariesX(size[0] + 1);
std::vector<double> voxelBoundariesY(size[1] + 1);
Expand All @@ -114,12 +111,11 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
double initialVoxelSpacing = 0;
double currentVoxelSpacing = 0;


// read in block 2 (voxel boundaries, cm, in x direction)
while (!readFileStream.eof() && counter < size[0] + 1)
{
readFileStream >> voxelBoundariesX[counter];
voxelBoundariesX[counter] = voxelBoundariesX[counter] * 10.0; // convert from cm to mm
voxelBoundariesX[counter] = voxelBoundariesX[counter] * 10.0; // convert from cm to mm
if (counter == 1)
{
initialVoxelSpacing = fabs(voxelBoundariesX[counter] - voxelBoundariesX[counter - 1]);
Expand All @@ -128,20 +124,20 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
else if (counter > 1)
{
currentVoxelSpacing = abs(voxelBoundariesX[counter] - voxelBoundariesX[counter - 1]);
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false){
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in X direction.")
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false)
{
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in X direction.");
}
}
counter += 1;
}


// read in block 3 (voxel boundaries, cm, in y direction)
counter = 0;
while (!readFileStream.eof() && counter < size[1] + 1)
{
readFileStream >> voxelBoundariesY[counter];
voxelBoundariesY[counter] = voxelBoundariesY[counter] * 10.0; // convert from cm to mm
voxelBoundariesY[counter] = voxelBoundariesY[counter] * 10.0; // convert from cm to mm
if (counter == 1)
{
initialVoxelSpacing = fabs(voxelBoundariesY[counter] - voxelBoundariesY[counter - 1]);
Expand All @@ -150,8 +146,9 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
else if (counter > 1)
{
currentVoxelSpacing = abs(voxelBoundariesY[counter] - voxelBoundariesY[counter - 1]);
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false){
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in Y direction.")
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false)
{
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in Y direction.");
}
}
counter += 1;
Expand All @@ -162,7 +159,7 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
while (!readFileStream.eof() && counter < size[2] + 1)
{
readFileStream >> voxelBoundariesZ[counter];
voxelBoundariesZ[counter] = voxelBoundariesZ[counter] * 10.0; // convert from cm to mm
voxelBoundariesZ[counter] = voxelBoundariesZ[counter] * 10.0; // convert from cm to mm
if (counter == 1)
{
initialVoxelSpacing = fabs(voxelBoundariesZ[counter] - voxelBoundariesZ[counter - 1]);
Expand All @@ -171,8 +168,9 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
else if (counter > 1)
{
currentVoxelSpacing = abs(voxelBoundariesZ[counter] - voxelBoundariesZ[counter - 1]);
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false){
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in Z direction.")
if (AreEqualWithTolerance(initialVoxelSpacing, currentVoxelSpacing) == false)
{
vtkWarningMacro("LoadDosxyzNrc3dDoseFile: Voxels have uneven spacing in Z direction.");
}
}
counter += 1;
Expand All @@ -196,13 +194,13 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
vtkErrorMacro("LoadDosxyzNrc3dDoseFile: The end of file was reached earlier than specified.");
}
readFileStream >> currentValue;
currentValue = currentValue * intensityScalingFactor;
(*floatPtr) = currentValue;
++floatPtr;
}
}
}


// create volume node for dose values
vtkSmartPointer<vtkMRMLScalarVolumeNode> dosxyzNrc3dDoseVolumeNode = vtkSmartPointer<vtkMRMLScalarVolumeNode>::New();
dosxyzNrc3dDoseVolumeNode->SetScene(this->GetMRMLScene());
Expand Down Expand Up @@ -230,12 +228,7 @@ void vtkSlicerDosxyzNrc3dDoseFileReaderLogic::LoadDosxyzNrc3dDoseFile(char* file
dosxyzNrc3dDoseVolumeDisplayNode->SetAndObserveColorNodeID("vtkMRMLColorTableNodeGrey");
dosxyzNrc3dDoseVolumeNode->SetAndObserveDisplayNodeID(dosxyzNrc3dDoseVolumeDisplayNode->GetID());


//todo: read in block 6 (error values array; relative errors)
//todo: read error values in another volume node
//todo: do I also need voxelCenters?
//todo: read in block 6 (relative errors) of .3ddose file in another volume node

readFileStream.close();
}


Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class VTK_SLICER_DOSXYZNRC3DDOSEFILEREADER_LOGIC_EXPORT vtkSlicerDosxyzNrc3dDose

/// Load DosxyzNrc3dDose volume from file
/// \param filename Path and filename of the DosxyzNrc3dDose file
void LoadDosxyzNrc3dDoseFile(char* filename);
void LoadDosxyzNrc3dDoseFile(char* filename, float intensityScalingFactor=1.0);

/// Determine if two numbers are equal within a small tolerance (0.001)
static bool AreEqualWithTolerance(double a, double b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,29 @@
</rect>
</property>
<property name="windowTitle">
<string>Volume Options</string>
<string>DosxyzNrc3dDoseFileReader Options</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QCheckBox" name="UseImageIntensityScaleAndOffsetCheckBox">
<widget class="QLabel" name="ScalingFactorLabel">
<property name="toolTip">
<string>Scaling factor applied on the dose values</string>
</property>
<property name="text">
<string>Scaling factor:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="ScalingFactorLineEdit">
<property name="toolTip">
<string/>
</property>
<property name="text">
<string>Use Image Intensity Scale and Image Intensity Offset</string>
<string>1.0</string>
</property>
</widget>
</item>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

/// Qt includes
#include <QFileInfo>
#include <QDebug>

// CTK includes
#include <ctkFlowLayout.h>
Expand Down Expand Up @@ -48,11 +49,13 @@ qSlicerDosxyzNrc3dDoseFileReaderOptionsWidget::qSlicerDosxyzNrc3dDoseFileReaderO

ctkFlowLayout::replaceLayout(this);

connect(d->UseImageIntensityScaleAndOffsetCheckBox, SIGNAL(toggled(bool)),
this, SLOT(updateProperties()));
connect(d->ScalingFactorLineEdit, SIGNAL(textChanged(QString)), this, SLOT(updateProperties()));

// Image instensity scaling factor is 1.0 by default
float defaultScalingFactorValue = 1.0;
QString defaultScalingFactorString = QString::number(defaultScalingFactorValue);
d->ScalingFactorLineEdit->setText(defaultScalingFactorString);

// Image intensity scale and offset turned off by default
d->UseImageIntensityScaleAndOffsetCheckBox->setChecked(false);
}

//-----------------------------------------------------------------------------
Expand All @@ -65,5 +68,13 @@ void qSlicerDosxyzNrc3dDoseFileReaderOptionsWidget::updateProperties()
{
Q_D(qSlicerDosxyzNrc3dDoseFileReaderOptionsWidget);

d->Properties["imageIntensityScaleAndOffset"] = d->UseImageIntensityScaleAndOffsetCheckBox->isChecked();
bool ok = false;
float scalingFactor = d->ScalingFactorLineEdit->text().toFloat(&ok);
if (ok == false)
{
qCritical() << Q_FUNC_INFO << ": Unable to parse scaling factor parameter " << d->ScalingFactorLineEdit->text() << ". Using default value 1.0";
scalingFactor = 1.0;
}

d->Properties["scalingFactor"] = scalingFactor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ bool qSlicerDosxyzNrc3dDoseFileReaderPlugin::load(const IOProperties& properties
QString fileName = properties["fileName"].toString();
Q_ASSERT(d->Logic);

bool useImageIntensityScaleAndOffsetFromFile = properties["imageIntensityScaleAndOffset"].toBool();
d->Logic->LoadDosxyzNrc3dDoseFile(fileName.toLatin1().data());
float intensityScalingFactor = properties["scalingFactor"].toFloat();
d->Logic->LoadDosxyzNrc3dDoseFile(fileName.toLatin1().data(), intensityScalingFactor);

this->setLoadedNodes(QStringList());

Expand Down

0 comments on commit 0276e44

Please sign in to comment.