/
vtkMDHexFactory.cpp
303 lines (252 loc) · 9.37 KB
/
vtkMDHexFactory.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidKernel/CPUTimer.h"
#include "MantidMDEvents/MDEventFactory.h"
#include "MantidVatesAPI/vtkMDHexFactory.h"
#include "MantidVatesAPI/Common.h"
#include "MantidVatesAPI/ProgressAction.h"
#include <vtkCellData.h>
#include <vtkFloatArray.h>
#include <vtkHexahedron.h>
#include <vtkPoints.h>
#include <vtkUnstructuredGrid.h>
#include "MantidKernel/ReadLock.h"
using namespace Mantid::API;
using namespace Mantid::MDEvents;
using namespace Mantid::Geometry;
using Mantid::Kernel::CPUTimer;
using Mantid::Kernel::ReadLock;
namespace Mantid
{
namespace VATES
{
/*Constructor
@Param thresholdRange : Threshold range strategy
@scalarName : Name for scalar signal array.
*/
vtkMDHexFactory::vtkMDHexFactory(ThresholdRange_scptr thresholdRange, const std::string& scalarName, const size_t maxDepth) :
m_thresholdRange(thresholdRange), m_scalarName(scalarName), m_maxDepth(maxDepth), m_time(0)
{
}
/// Destructor
vtkMDHexFactory::~vtkMDHexFactory()
{
}
//-------------------------------------------------------------------------------------------------
/* Generate the vtkDataSet from the objects input MDEventWorkspace (of a given type an dimensionality 3+)
*
* @param ws: workspace to draw from
* @return a fully constructed vtkUnstructuredGrid containing geometric and scalar data.
*/
template<typename MDE, size_t nd>
void vtkMDHexFactory::doCreate(typename MDEventWorkspace<MDE, nd>::sptr ws) const
{
bool VERBOSE = true;
CPUTimer tim;
// Acquire a scoped read-only lock to the workspace (prevent segfault from algos modifying ws)
ReadLock lock(*ws);
// First we get all the boxes, up to the given depth; with or wo the slice function
std::vector<API::IMDNode *> boxes;
if (this->slice)
ws->getBox()->getBoxes(boxes, m_maxDepth, true, this->sliceImplicitFunction);
else
ws->getBox()->getBoxes(boxes, m_maxDepth, true);
vtkIdType numBoxes = boxes.size();
vtkIdType imageSizeActual = 0;
if (VERBOSE) std::cout << tim << " to retrieve the " << numBoxes << " boxes down to depth " << m_maxDepth << std::endl;
// Create 8 points per box.
vtkPoints *points = vtkPoints::New();
points->Allocate(numBoxes * 8);
points->SetNumberOfPoints(numBoxes * 8);
// One scalar per box
vtkFloatArray * signals = vtkFloatArray::New();
signals->Allocate(numBoxes);
signals->SetName(m_scalarName.c_str());
signals->SetNumberOfComponents(1);
//signals->SetNumberOfValues(numBoxes);
// To cache the signal
float * signalArray = new float[numBoxes];
// True for boxes that we will use
bool * useBox = new bool[numBoxes];
memset(useBox, 0, sizeof(bool)*numBoxes);
// Create the data set
vtkUnstructuredGrid * visualDataSet = vtkUnstructuredGrid::New();
this->dataSet = visualDataSet;
visualDataSet->Allocate(numBoxes);
vtkIdList * hexPointList = vtkIdList::New();
hexPointList->SetNumberOfIds(8);
// This can be parallelized
// cppcheck-suppress syntaxError
PRAGMA_OMP( parallel for schedule (dynamic) )
for (int ii=0; ii<int(boxes.size()); ii++)
{
// Get the box here
size_t i = size_t(ii);
API::IMDNode * box = boxes[i];
Mantid::signal_t signal_normalized= box->getSignalNormalized();
if (!isSpecial( signal_normalized ) && m_thresholdRange->inRange(signal_normalized))
{
// Cache the signal and using of it
signalArray[i] = float(signal_normalized);
useBox[i] = true;
//Get the coordinates.
size_t numVertexes = 0;
coord_t * coords;
// If slicing down to 3D, specify which dimensions to keep.
if (this->slice)
coords = box->getVertexesArray(numVertexes, 3, this->sliceMask);
else
coords = box->getVertexesArray(numVertexes);
if (numVertexes == 8)
{
//Iterate through all coordinates. Candidate for speed improvement.
for(size_t v = 0; v < numVertexes; v++)
{
coord_t * coord = coords + v*3;
// Set the point at that given ID
points->SetPoint(i*8 + v, coord[0], coord[1], coord[2]);
std::string msg;
}
} // valid number of vertexes returned
// Free memory
delete [] coords;
}
} // For each box
if (VERBOSE) std::cout << tim << " to create the necessary points." << std::endl;
//Add points
visualDataSet->SetPoints(points);
for (size_t i=0; i<boxes.size(); i++)
{
if (useBox[i])
{
// The bare point ID
vtkIdType pointIds = i * 8;
//Add signal
signals->InsertNextValue(signalArray[i]);
hexPointList->SetId(0, pointIds + 0); //xyx
hexPointList->SetId(1, pointIds + 1); //dxyz
hexPointList->SetId(2, pointIds + 3); //dxdyz
hexPointList->SetId(3, pointIds + 2); //xdyz
hexPointList->SetId(4, pointIds + 4); //xydz
hexPointList->SetId(5, pointIds + 5); //dxydz
hexPointList->SetId(6, pointIds + 7); //dxdydz
hexPointList->SetId(7, pointIds + 6); //xdydz
//Add cells
visualDataSet->InsertNextCell(VTK_HEXAHEDRON, hexPointList);
double bounds[6];
visualDataSet->GetCellBounds(imageSizeActual, bounds);
if(bounds[0] < -10 || bounds[2] < -10 ||bounds[4]< -10)
{
std::string msg = "";
}
imageSizeActual++;
}
} // for each box.
delete[] signalArray;
delete[] useBox;
//Shrink to fit
signals->Squeeze();
visualDataSet->Squeeze();
//Add scalars
visualDataSet->GetCellData()->SetScalars(signals);
if (VERBOSE) std::cout << tim << " to create " << imageSizeActual << " hexahedrons." << std::endl;
}
//-------------------------------------------------------------------------------------------------
/*
Generate the vtkDataSet from the objects input IMDEventWorkspace
@param progressUpdating: Reporting object to pass progress information up the stack.
@Return a fully constructed vtkUnstructuredGrid containing geometric and scalar data.
*/
vtkDataSet* vtkMDHexFactory::create(ProgressAction& progressUpdating) const
{
this->dataSet = tryDelegatingCreation<IMDEventWorkspace, 3>(m_workspace, progressUpdating, false);
if(this->dataSet != NULL)
{
return this->dataSet;
}
else
{
IMDEventWorkspace_sptr imdws = this->castAndCheck<IMDEventWorkspace, 3>(m_workspace, false);
size_t nd = imdws->getNumDims();
if (nd > 3)
{
// Slice from >3D down to 3D
this->slice = true;
this->sliceMask = new bool[nd];
this->sliceImplicitFunction = new MDImplicitFunction();
// Make the mask of dimensions
// TODO: Smarter mapping
for (size_t d=0; d<nd; d++)
this->sliceMask[d] = (d<3);
// Define where the slice is in 4D
// TODO: Where to slice? Right now is just 0
std::vector<coord_t> point(nd, 0);
point[3] = coord_t(m_time); //Specifically for 4th/time dimension.
// Define two opposing planes that point in all higher dimensions
std::vector<coord_t> normal1(nd, 0);
std::vector<coord_t> normal2(nd, 0);
for (size_t d=3; d<nd; d++)
{
normal1[d] = +1.0;
normal2[d] = -1.0;
}
// This creates a 0-thickness region to slice in.
sliceImplicitFunction->addPlane( MDPlane(normal1, point) );
sliceImplicitFunction->addPlane( MDPlane(normal2, point) );
//coord_t pointA[4] = {0, 0, 0, -1.0};
//coord_t pointB[4] = {0, 0, 0, +2.0};
}
else
{
// Direct 3D, so no slicing
this->slice = false;
}
progressUpdating.eventRaised(0.1);
// Macro to call the right instance of the
CALL_MDEVENT_FUNCTION(this->doCreate, imdws);
progressUpdating.eventRaised(1.0);
// Clean up
if (this->slice)
{
delete[] this->sliceMask;
delete this->sliceImplicitFunction;
}
// The macro does not allow return calls, so we used a member variable.
return this->dataSet;
}
}
/*
Initalize the factory with the workspace. This allows top level decision on what factory to use, but allows presenter/algorithms to pass in the
dataobjects (workspaces) to run against at a later time. If workspace is not an IMDEventWorkspace, attempts to use any run-time successor set.
@Param ws : Workspace to use.
*/
void vtkMDHexFactory::initialize(Mantid::API::Workspace_sptr ws)
{
IMDEventWorkspace_sptr imdws = doInitialize<IMDEventWorkspace, 3>(ws, false);
m_workspace = imdws;
//Setup range values according to whatever strategy object has been injected.
m_thresholdRange->setWorkspace(ws);
m_thresholdRange->calculate();
}
/// Validate the current object.
void vtkMDHexFactory::validate() const
{
if(!m_workspace)
{
throw std::runtime_error("Invalid vtkMDHexFactory. Workspace is null");
}
}
/** Sets the recursion depth to a specified level in the workspace.
*/
void vtkMDHexFactory::setRecursionDepth(size_t depth)
{
m_maxDepth = depth;
}
/*
Set the time value.
*/
void vtkMDHexFactory::setTime(double time)
{
m_time = time;
}
}
}