-
Notifications
You must be signed in to change notification settings - Fork 122
/
vtkDataSetToNonOrthogonalDataSetTest.h
361 lines (331 loc) · 11.9 KB
/
vtkDataSetToNonOrthogonalDataSetTest.h
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifndef MANTID_VATES_VTKDATASETTONONORTHOGONALDATASETTEST_H_
#define MANTID_VATES_VTKDATASETTONONORTHOGONALDATASETTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidVatesAPI/vtkDataSetToNonOrthogonalDataSet.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidMDEvents/CoordTransformAffine.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include <vtkDataArray.h>
#include <vtkFieldData.h>
#include <vtkFloatArray.h>
#include <vtkPoints.h>
#include "MantidVatesAPI/vtkRectilinearGrid_Silent.h"
#include <vtkUnstructuredGrid.h>
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::MDEvents;
using namespace Mantid::MDEvents::MDEventsTestHelper;
using namespace Mantid::VATES;
class vtkDataSetToNonOrthogonalDataSetTest : public CxxTest::TestSuite
{
private:
std::string createMantidWorkspace(bool nonUnityTransform,
bool wrongCoords = false,
bool forgetUB = false,
bool forgetWmat = false,
bool forgetAffmat = false,
double scale = 1.0)
{
// Creating an MDEventWorkspace as the content is not germain to the
// information necessary for the non-orthogonal axes
std::string wsName = "simpleWS";
IMDEventWorkspace_sptr ws = makeAnyMDEW<MDEvent<4>, 4>(1, 0.0, 1.0, 1, wsName);
// Set the coordinate system
if (!wrongCoords)
{
ws->setCoordinateSystem(Mantid::Kernel::HKL);
}
else
{
ws->setCoordinateSystem(QSample);
}
// Set the UB matrix
if (!forgetUB)
{
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("SetUB");
alg->initialize();
alg->setRethrows(true);
alg->setProperty("Workspace", wsName);
alg->setProperty("a", 3.643*scale);
alg->setProperty("b", 3.643);
alg->setProperty("c", 5.781);
alg->setProperty("alpha", 90.0);
alg->setProperty("beta", 90.0);
alg->setProperty("gamma", 120.0);
std::vector<double> uVec;
uVec.push_back(1*scale);
uVec.push_back(1);
uVec.push_back(0);
std::vector<double> vVec;
vVec.push_back(0);
vVec.push_back(0);
vVec.push_back(1);
alg->setProperty("u", uVec);
alg->setProperty("v", vVec);
alg->execute();
}
// Create the coordinate transformation information
std::vector<Mantid::coord_t> affMatVals;
affMatVals.push_back(1);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(1);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(1);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(1);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(0);
affMatVals.push_back(1);
CoordTransformAffine affMat(4, 4);
affMat.setMatrix(Matrix<Mantid::coord_t>(affMatVals));
if (!forgetAffmat)
{
ws->setTransformToOriginal(affMat.clone(), 0);
}
// Create the transform (W) matrix
// Need it as a vector
std::vector<double> wMat;
if (!nonUnityTransform)
{
DblMatrix temp(3, 3, true);
wMat = temp.getVector();
}
else
{
wMat.push_back(1);
wMat.push_back(1);
wMat.push_back(0);
wMat.push_back(1);
wMat.push_back(-1);
wMat.push_back(0);
wMat.push_back(0);
wMat.push_back(0);
wMat.push_back(1);
}
if (!forgetWmat)
{
// Create property for W matrix and add it as log to run object
PropertyWithValue<std::vector<double> > *p;
p = new PropertyWithValue<std::vector<double> >("W_MATRIX", wMat);
ws->getExperimentInfo(0)->mutableRun().addProperty(p, true);
}
return wsName;
}
vtkUnstructuredGrid *createSingleVoxelPoints()
{
vtkUnstructuredGrid* ds = vtkUnstructuredGrid::New();
vtkPoints *points = vtkPoints::New();
points->Allocate(8);
points->InsertNextPoint(0,0,0);
points->InsertNextPoint(1,0,0);
points->InsertNextPoint(1,1,0);
points->InsertNextPoint(0,1,0);
points->InsertNextPoint(0,0,1);
points->InsertNextPoint(1,0,1);
points->InsertNextPoint(1,1,1);
points->InsertNextPoint(0,1,1);
ds->SetPoints(points);
return ds;
}
float *getRangeComp(vtkDataSet *ds, std::string fieldname, int size)
{
vtkDataArray *arr = ds->GetFieldData()->GetArray(fieldname.c_str());
vtkFloatArray *farr = vtkFloatArray::SafeDownCast(arr);
float *vals = new float[size];
farr->GetTupleValue(0, vals);
return vals;
}
void checkUnityTransformation(vtkUnstructuredGrid *grid)
{
// This function can be used for both the unscaled and scaled
// unity transformation, since the outcome is identical.
// Now, check some values
/// Get the (1,1,1) point
const double eps = 1.0e-5;
double *point = grid->GetPoint(6);
TS_ASSERT_DELTA(point[0], 1.5, eps);
TS_ASSERT_DELTA(point[1], 1.0, eps);
TS_ASSERT_DELTA(point[2], 0.8660254, eps);
// See if the basis vectors are available
float *xBasis = getRangeComp(grid, "AxisBaseForX", 3);
TS_ASSERT_DELTA(xBasis[0], 1.0, eps);
TS_ASSERT_DELTA(xBasis[1], 0.0, eps);
TS_ASSERT_DELTA(xBasis[2], 0.0, eps);
delete [] xBasis;
float *yBasis = getRangeComp(grid, "AxisBaseForY", 3);
TS_ASSERT_DELTA(yBasis[0], 0.0, eps);
TS_ASSERT_DELTA(yBasis[1], 1.0, eps);
TS_ASSERT_DELTA(yBasis[2], 0.0, eps);
delete [] yBasis;
float *zBasis = getRangeComp(grid, "AxisBaseForZ", 3);
TS_ASSERT_DELTA(zBasis[0], 0.5, eps);
TS_ASSERT_DELTA(zBasis[1], 0.0, eps);
TS_ASSERT_DELTA(zBasis[2], 0.8660254, eps);
delete [] zBasis;
}
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static vtkDataSetToNonOrthogonalDataSetTest *createSuite() { return new vtkDataSetToNonOrthogonalDataSetTest(); }
static void destroySuite( vtkDataSetToNonOrthogonalDataSetTest *suite ) { delete suite; }
void testThrowIfVtkDatasetNull()
{
vtkDataSet *dataset = NULL;
TS_ASSERT_THROWS(vtkDataSetToNonOrthogonalDataSet temp(dataset, ""),
std::runtime_error);
}
void testThrowsIfWorkspaceNameEmpty()
{
vtkUnstructuredGrid *dataset = vtkUnstructuredGrid::New();
TS_ASSERT_THROWS(vtkDataSetToNonOrthogonalDataSet temp(dataset, ""),
std::runtime_error);
dataset->Delete();
}
void testThrowIfVtkDatasetWrongType()
{
vtkRectilinearGrid *grid = vtkRectilinearGrid::New();
vtkDataSetToNonOrthogonalDataSet converter(grid, "name");
TS_ASSERT_THROWS(converter.execute(), std::runtime_error);
grid->Delete();
}
void testSimpleDataset()
{
std::string wsName = createMantidWorkspace(false);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS_NOTHING(converter.execute());
this->checkUnityTransformation(ds);
ds->Delete();
}
void testThrowsSimpleDatasetWrongCoords()
{
std::string wsName = createMantidWorkspace(false, true);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS(converter.execute(), std::invalid_argument);
ds->Delete();
}
void testThrowsSimpleDatasetNoUB()
{
std::string wsName = createMantidWorkspace(false, false, true);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS(converter.execute(), std::invalid_argument);
ds->Delete();
}
void testThrowsSimpleDatasetNoWMatrix()
{
std::string wsName = createMantidWorkspace(false, false, false, true);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS(converter.execute(), std::invalid_argument);
ds->Delete();
}
void testNoThrowsSimpleDataSetNoAffineMatrix()
{
std::string wsName = createMantidWorkspace(false, false, false, false, true);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS_NOTHING(converter.execute());
ds->Delete();
}
void testStaticUseForSimpleDataSet()
{
std::string wsName = createMantidWorkspace(false);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
TS_ASSERT_THROWS_NOTHING(vtkDataSetToNonOrthogonalDataSet::exec(ds,
wsName));
ds->Delete();
}
void testNonUnitySimpleDataset()
{
std::string wsName = createMantidWorkspace(true);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS_NOTHING(converter.execute());
// Now, check some values
/// Get the (1,1,1) point
const double eps = 1.0e-5;
double *point = ds->GetPoint(6);
TS_ASSERT_DELTA(point[0], 1.0, eps);
TS_ASSERT_DELTA(point[1], 1.0, eps);
TS_ASSERT_DELTA(point[2], 1.0, eps);
// See if the basis vectors are available
float *xBasis = getRangeComp(ds, "AxisBaseForX", 3);
TS_ASSERT_DELTA(xBasis[0], 1.0, eps);
TS_ASSERT_DELTA(xBasis[1], 0.0, eps);
TS_ASSERT_DELTA(xBasis[2], 0.0, eps);
delete [] xBasis;
float *yBasis = getRangeComp(ds, "AxisBaseForY", 3);
TS_ASSERT_DELTA(yBasis[0], 0.0, eps);
TS_ASSERT_DELTA(yBasis[1], 1.0, eps);
TS_ASSERT_DELTA(yBasis[2], 0.0, eps);
delete [] yBasis;
float *zBasis = getRangeComp(ds, "AxisBaseForZ", 3);
TS_ASSERT_DELTA(zBasis[0], 0.0, eps);
TS_ASSERT_DELTA(zBasis[1], 0.0, eps);
TS_ASSERT_DELTA(zBasis[2], 1.0, eps);
delete [] zBasis;
ds->Delete();
}
void testScaledSimpleDataset()
{
std::string wsName = createMantidWorkspace(false, false, false, false, false, 2.0);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS_NOTHING(converter.execute());
this->checkUnityTransformation(ds);
ds->Delete();
}
void testScaledNonUnitySimpleDataset()
{
std::string wsName = createMantidWorkspace(true, false, false, false, false, 2.0);
vtkUnstructuredGrid *ds = createSingleVoxelPoints();
vtkDataSetToNonOrthogonalDataSet converter(ds, wsName);
TS_ASSERT_THROWS_NOTHING(converter.execute());
// Now, check some values
/// Get the (1,1,1) point
const double eps = 1.0e-5;
double *point = ds->GetPoint(6);
TS_ASSERT_DELTA(point[0], 0.34534633, eps);
TS_ASSERT_DELTA(point[1], 1.0, eps);
TS_ASSERT_DELTA(point[2], 0.75592895, eps);
// See if the basis vectors are available
float *xBasis = getRangeComp(ds, "AxisBaseForX", 3);
TS_ASSERT_DELTA(xBasis[0], 1.0, eps);
TS_ASSERT_DELTA(xBasis[1], 0.0, eps);
TS_ASSERT_DELTA(xBasis[2], 0.0, eps);
delete [] xBasis;
float *yBasis = getRangeComp(ds, "AxisBaseForY", 3);
TS_ASSERT_DELTA(yBasis[0], 0.0, eps);
TS_ASSERT_DELTA(yBasis[1], 1.0, eps);
TS_ASSERT_DELTA(yBasis[2], 0.0, eps);
delete [] yBasis;
float *zBasis = getRangeComp(ds, "AxisBaseForZ", 3);
TS_ASSERT_DELTA(zBasis[0], -0.65465367, eps);
TS_ASSERT_DELTA(zBasis[1], 0.0, eps);
TS_ASSERT_DELTA(zBasis[2], 0.75592895, eps);
delete [] zBasis;
ds->Delete();
}
};
#endif /* MANTID_VATESAPI_VTKDATASETTONONORTHOGONALDATASETTEST_H_ */