Newer
Older
#include "MantidVatesAPI/vtkMDHistoLineFactory.h"
#include "MantidVatesAPI/Common.h"
#include "MantidVatesAPI/ProgressAction.h"
#include "MantidVatesAPI/vtkNullUnstructuredGrid.h"
Owen Arnold
committed
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkFloatArray.h"
#include "vtkLine.h"
#include <vector>
Janik Zikovsky
committed
#include "MantidAPI/IMDWorkspace.h"
#include "MantidAPI/NullCoordTransform.h"
#include "MantidDataObjects/MDHistoWorkspace.h"
#include "MantidKernel/ReadLock.h"
Janik Zikovsky
committed
using Mantid::API::IMDWorkspace;
using Mantid::DataObjects::MDHistoWorkspace;
using Mantid::API::NullCoordTransform;
Owen Arnold
committed
namespace
{
Mantid::Kernel::Logger g_log("vtkMDHistoLineFactory");
}
Owen Arnold
committed
namespace Mantid
{
namespace VATES
{
vtkMDHistoLineFactory::vtkMDHistoLineFactory(ThresholdRange_scptr thresholdRange, const VisualNormalization normaliztionOption) : m_normalizationOption(normaliztionOption),
m_thresholdRange(thresholdRange)
Owen Arnold
committed
{
}
/**
Assigment operator
@param other : vtkMDHistoLineFactory to assign to this instance from.
@return ref to assigned current instance.
*/
vtkMDHistoLineFactory& vtkMDHistoLineFactory::operator=(const vtkMDHistoLineFactory& other)
{
if(this != &other)
{
this->m_normalizationOption = other.m_normalizationOption;
this->m_thresholdRange = other.m_thresholdRange;
this->m_workspace = other.m_workspace;
}
return *this;
}
/**
Copy Constructor
@param other : instance to copy from.
*/
vtkMDHistoLineFactory::vtkMDHistoLineFactory(const vtkMDHistoLineFactory& other)
Owen Arnold
committed
{
this->m_normalizationOption = other.m_normalizationOption;
this->m_thresholdRange = other.m_thresholdRange;
Owen Arnold
committed
this->m_workspace = other.m_workspace;
}
/**
Create the vtkStructuredGrid from the provided workspace
@param progressUpdating: Reporting object to pass progress information up the stack.
@return fully constructed vtkDataSet.
*/
vtkSmartPointer<vtkDataSet>
vtkMDHistoLineFactory::create(ProgressAction &progressUpdating) const {
auto product = tryDelegatingCreation<MDHistoWorkspace, 1>(
m_workspace, progressUpdating);
if (product != nullptr) {
Owen Arnold
committed
}
else
{
g_log.warning() << "Factory " << this->getFactoryTypeName() << " is being used. You are viewing data with less than three dimensions in the VSI. \n";
Mantid::Kernel::ReadLock lock(*m_workspace);
Owen Arnold
committed
const int nBinsX = static_cast<int>( m_workspace->getXDimension()->getNBins() );
const coord_t maxX = m_workspace-> getXDimension()->getMaximum();
const coord_t minX = m_workspace-> getXDimension()->getMinimum();
Owen Arnold
committed
coord_t incrementX = (maxX - minX) / coord_t(nBinsX-1);
Owen Arnold
committed
const int imageSize = nBinsX;
vtkNew<vtkPoints> points;
Owen Arnold
committed
points->Allocate(static_cast<int>(imageSize));
vtkNew<vtkFloatArray> signal;
Owen Arnold
committed
signal->Allocate(imageSize);
signal->SetName(vtkDataSetFactory::ScalarName.c_str());
Owen Arnold
committed
signal->SetNumberOfComponents(1);
UnstructuredPoint unstructPoint;
const int nPointsX = nBinsX;
Column column(nPointsX);
NullCoordTransform transform;
//Mantid::API::CoordTransform* transform = m_workspace->getTransformFromOriginal();
Owen Arnold
committed
Mantid::coord_t in[3];
Mantid::coord_t out[3];
double progressFactor = 0.5/double(nBinsX);
double progressOffset = 0.5;
Owen Arnold
committed
//Loop through dimensions
for (int i = 0; i < nPointsX; i++)
{
progressUpdating.eventRaised(progressFactor * double(i));
in[0] = minX + static_cast<coord_t>(i) * incrementX; //Calculate increment in x;
float signalScalar = static_cast<float>(m_workspace->getSignalNormalizedAt(i));
Owen Arnold
committed
if (!std::isfinite(signalScalar) ||
!m_thresholdRange->inRange(signalScalar)) {
//Flagged so that topological and scalar data is not applied.
unstructPoint.isSparse = true;
}
else
{
if (i < (nBinsX -1))
Owen Arnold
committed
{
signal->InsertNextValue(static_cast<float>(signalScalar));
Owen Arnold
committed
}
unstructPoint.isSparse = false;
}
Owen Arnold
committed
transform.apply(in, out);
Owen Arnold
committed
unstructPoint.pointId = points->InsertNextPoint(out);
column[i] = unstructPoint;
}
Owen Arnold
committed
points->Squeeze();
signal->Squeeze();
auto visualDataSet = vtkSmartPointer<vtkUnstructuredGrid>::New();
Owen Arnold
committed
visualDataSet->Allocate(imageSize);
visualDataSet->SetPoints(points.GetPointer());
visualDataSet->GetCellData()->SetScalars(signal.GetPointer());
Owen Arnold
committed
for (int i = 0; i < nBinsX - 1; i++)
{
progressUpdating.eventRaised((progressFactor * double(i)) + progressOffset);
//Only create topologies for those cells which are not sparse.
if (!column[i].isSparse)
{
vtkLine* line = vtkLine::New();
line->GetPointIds()->SetId(0, column[i].pointId);
line->GetPointIds()->SetId(1, column[i + 1].pointId);
visualDataSet->InsertNextCell(VTK_LINE, line->GetPointIds());
}
Owen Arnold
committed
}
visualDataSet->Squeeze();
// Hedge against empty data sets
if (visualDataSet->GetNumberOfPoints() <= 0)
{
vtkNullUnstructuredGrid nullGrid;
visualDataSet = nullGrid.createNullData();
}
vtkSmartPointer<vtkDataSet> dataset = visualDataSet;
return dataset;
Owen Arnold
committed
}
}
void vtkMDHistoLineFactory::initialize(Mantid::API::Workspace_sptr wspace_sptr)
Owen Arnold
committed
{
m_workspace = this->doInitialize<MDHistoWorkspace, 1>(wspace_sptr);
//Setup range values according to whatever strategy object has been injected.
m_thresholdRange->setWorkspace(wspace_sptr);
m_thresholdRange->calculate();
Owen Arnold
committed
}
void vtkMDHistoLineFactory::validate() const
Owen Arnold
committed
{
if(NULL == m_workspace.get())
{
throw std::runtime_error("IMDWorkspace is null");
}
}
vtkMDHistoLineFactory::~vtkMDHistoLineFactory()
Owen Arnold
committed
{
}
}