-
Notifications
You must be signed in to change notification settings - Fork 122
/
MDEWLoadingPresenter.cpp
235 lines (216 loc) · 7.45 KB
/
MDEWLoadingPresenter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "MantidVatesAPI/MDEWLoadingPresenter.h"
#include "MantidVatesAPI/MDLoadingView.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidGeometry/MDGeometry/NullImplicitFunction.h"
#include "MantidVatesAPI/RebinningKnowledgeSerializer.h"
#include "MantidVatesAPI/MetadataToFieldData.h"
#include "MantidVatesAPI/VatesXMLDefinitions.h"
#include "MantidVatesAPI/Common.h"
#include <boost/algorithm/string.hpp>
#include <vtkFieldData.h>
#include <vtkDataSet.h>
namespace Mantid
{
namespace VATES
{
/// Constructor
MDEWLoadingPresenter::MDEWLoadingPresenter(MDLoadingView* view) :
m_view(view),
m_isSetup(false),
m_time(-1),
m_recursionDepth(0),
m_loadInMemory(false),
m_firstLoad(true)
{
Mantid::API::FrameworkManager::Instance();
}
/// Destructor
MDEWLoadingPresenter::~MDEWLoadingPresenter()
{
}
/*
Extract the geometry and function information
@param eventWs : event workspace to get the information from.
*/
void MDEWLoadingPresenter::extractMetadata(Mantid::API::IMDEventWorkspace_sptr eventWs)
{
using namespace Mantid::Geometry;
MDGeometryBuilderXML<NoDimensionPolicy> refresh;
xmlBuilder= refresh; //Reassign.
std::vector<MDDimensionExtents<coord_t> > ext = eventWs->getMinimumExtents(5);
std::vector<IMDDimension_sptr> dimensions;
size_t nDimensions = eventWs->getNumDims();
for (size_t d=0; d<nDimensions; d++)
{
IMDDimension_const_sptr inDim = eventWs->getDimension(d);
coord_t min = ext[d].getMin();
coord_t max = ext[d].getMax();
if (min > max)
{
min = 0.0;
max = 1.0;
}
//std::cout << "dim " << d << min << " to " << max << std::endl;
axisLabels.push_back(makeAxisTitle(inDim));
MDHistoDimension_sptr dim(new MDHistoDimension(inDim->getName(), inDim->getName(), inDim->getUnits(), min, max, inDim->getNBins()));
dimensions.push_back(dim);
}
//Configuring the geometry xml builder allows the object panel associated with this reader to later
//determine how to display all geometry related properties.
if(nDimensions > 0)
{
xmlBuilder.addXDimension( dimensions[0] );
}
if(nDimensions > 1)
{
xmlBuilder.addYDimension( dimensions[1] );
}
if(nDimensions > 2)
{
xmlBuilder.addZDimension( dimensions[2] );
}
if(nDimensions > 3)
{
tDimension = dimensions[3];
xmlBuilder.addTDimension(tDimension);
}
m_isSetup = true;
}
/**
Method determines whether loading/re-loading is necessary.
*/
bool MDEWLoadingPresenter::shouldLoad()
{
double viewTime = m_view->getTime();
size_t viewDepth = m_view->getRecursionDepth();
bool viewLoadInMemory = m_view->getLoadInMemory();
bool bExecute = false;
if(m_time != viewTime)
{
bExecute = false; //Time has changed. This DOES NOT require reloading.
}
if(m_recursionDepth != viewDepth)
{
bExecute = false; //Recursion depth has changed. This is a vtkDataSet factory concern.
}
if(m_loadInMemory != viewLoadInMemory)
{
bExecute = true; //Must reload with memory/file option.
}
if(m_firstLoad)
{
bExecute = true; //First time round. should execute underlying algorithm.
}
// Save state.
m_time = viewTime;
m_recursionDepth = viewDepth;
m_loadInMemory = viewLoadInMemory;
m_firstLoad = false;
//Return decision.
return bExecute;
}
/**
Determines wheter the file can be loaded based on it's extension.
@param filename containing the extension
@param expectedExtension expected extension for the file to have
@return TRUE, only if the extension is approved.
*/
bool MDEWLoadingPresenter::canLoadFileBasedOnExtension(const std::string& filename, const std::string& expectedExtension) const
{
// Quick check based on extension.
const size_t startExtension = filename.find_last_of('.');
const size_t endExtension = filename.length();
std::string extension = filename.substr(startExtension, endExtension - startExtension);
boost::algorithm::to_lower(extension);
boost::algorithm::trim(extension);
return extension == expectedExtension;
}
/*
Append the geometry and function information onto the outgoing vtkDataSet.
@param visualDataSet : outgoing dataset on which to append metadata.
@param wsName : name of the workspace.
*/
void MDEWLoadingPresenter::appendMetadata(vtkDataSet* visualDataSet, const std::string& wsName)
{
using namespace Mantid::API;
vtkFieldData* outputFD = vtkFieldData::New();
//Serialize metadata
RebinningKnowledgeSerializer serializer(LocationNotRequired);
serializer.setWorkspaceName(wsName);
serializer.setGeometryXML(xmlBuilder.create());
serializer.setImplicitFunction( Mantid::Geometry::MDImplicitFunction_sptr(new Mantid::Geometry::NullImplicitFunction()));
std::string xmlString = serializer.createXMLString();
//Add metadata to dataset.
MetadataToFieldData convert;
convert(outputFD, xmlString, XMLDefinitions::metaDataId().c_str());
visualDataSet->SetFieldData(outputFD);
outputFD->Delete();
}
/**
* Set the axis labels from the current dimensions
* @param visualDataSet: The VTK dataset to update
*/
void MDEWLoadingPresenter::setAxisLabels(vtkDataSet *visualDataSet)
{
vtkFieldData* fieldData = visualDataSet->GetFieldData();
setAxisLabel("AxisTitleForX", axisLabels[0], fieldData);
setAxisLabel("AxisTitleForY", axisLabels[1], fieldData);
setAxisLabel("AxisTitleForZ", axisLabels[2], fieldData);
}
/**
Gets the geometry in a string format.
@return geometry string ref.
@throw runtime_error if execute has not been run first.
*/
const std::string& MDEWLoadingPresenter::getGeometryXML() const
{
if(!m_isSetup)
{
throw std::runtime_error("Have not yet run extractMetaData!");
}
return xmlBuilder.create();
}
/**
@return boolean indicating whether the T dimension is available.
@throw runtime_error if execute has not been run first.
*/
bool MDEWLoadingPresenter::hasTDimensionAvailable() const
{
if(!m_isSetup)
{
throw std::runtime_error("Have not yet run ::extractMetaData!");
}
return xmlBuilder.hasTDimension() && !xmlBuilder.hasIntegratedTDimension();
}
/*
@return timestep values.
@throw runtime_error if execute has not been run first.
*/
std::vector<double> MDEWLoadingPresenter::getTimeStepValues() const
{
if(!m_isSetup)
{
throw std::runtime_error("Have not yet run ::extractMetaData!");
}
std::vector<double> result;
for(size_t i = 0; i < tDimension->getNBins(); i++)
{
result.push_back(tDimension->getX(i));
}
return result;
}
/**
* Create a label for the "time" coordinate
* @return the "time" coordinate label
* @throw runtime_error if execute has not been run first.
*/
std::string MDEWLoadingPresenter::getTimeStepLabel() const
{
if (!m_isSetup)
{
throw std::runtime_error("Have not yet run ::extractMetaData!");
}
return tDimension->getName() + " (" + tDimension->getUnits().ascii() + ")";
}
}
}