RulesUnionTest.h 11.5 KB
Newer Older
1
2
3
4
5
6
#ifndef MANTID_RULESUNIONTEST__
#define MANTID_RULESUNIONTEST__
#include <cxxtest/TestSuite.h>
#include <cmath>
#include <vector>
#include "MantidKernel/Logger.h"
7
#include "MantidKernel/make_unique.h"
8
9
#include "MantidKernel/System.h"
#include <cfloat>
10
#include "MantidKernel/V3D.h"
11
#include "MantidGeometry/Surfaces/Quadratic.h"
12
#include "MantidGeometry/Objects/CSGObject.h"
13
14
15
16
17
18
#include "MantidGeometry/Objects/Rules.h"
#include "MantidGeometry/Surfaces/Plane.h"
#include "MantidGeometry/Surfaces/Sphere.h"
#include "MantidGeometry/Surfaces/Cylinder.h"
#include "MantidGeometry/Surfaces/Cone.h"

19
20
#include "boost/make_shared.hpp"

21
22
using namespace Mantid;
using namespace Geometry;
23
using Mantid::Kernel::V3D;
24

25
class RulesUnionTest : public CxxTest::TestSuite {
26
public:
27
28
  void testDefaultConstructor() {
    Union A;
29
30
    TS_ASSERT_EQUALS(A.leaf(0), (Rule *)nullptr);
    TS_ASSERT_EQUALS(A.leaf(1), (Rule *)nullptr);
31
  }
32

33
  void testTwoRuleConstructor() {
34
35
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
36
37
38
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
39
40
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
41
    S1->setKey(P1);
42
    S1->setKeyN(10);
43
    S2->setKey(Sp1);
44
    S2->setKeyN(11);
45
46
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
47
    Union A(std::move(S1), std::move(S2));
48
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
49
50
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
51
  }
52

53
54
  void testThreeRuleConstructor() {
    Union Parent;
55
56
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
57
58
59
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
60
61
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
62
    S1->setKey(P1);
63
    S1->setKeyN(10);
64
    S2->setKey(Sp1);
65
    S2->setKeyN(11);
66
67
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
68
    Union A(&Parent, std::move(S1), std::move(S2));
69
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
70
71
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
72
73
    TS_ASSERT_EQUALS(A.getParent(), &Parent);
  }
74

75
  void testUnionConstructor() {
76
77
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
78
79
80
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
81
82
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
83
    S1->setKey(P1);
84
    S1->setKeyN(10);
85
    S2->setKey(Sp1);
86
    S2->setKeyN(11);
87
88
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
89
    Union A(std::move(S1), std::move(S2));
90
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
91
92
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
93
  }
94

95
  void testClone() {
96
97
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
98
99
100
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
101
102
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
103
    S1->setKey(P1);
104
    S1->setKeyN(10);
105
    S2->setKey(Sp1);
106
    S2->setKeyN(11);
107
108
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
109
    Union A(std::move(S1), std::move(S2));
110
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
111
112
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
113
    auto B = A.clone();
114
    TS_ASSERT_EQUALS(B->display(), "10 : 11");
115
116
    TS_ASSERT_EQUALS(B->leaf(0)->display(), ptrS1->display());
    TS_ASSERT_EQUALS(B->leaf(1)->display(), ptrS2->display());
117
  }
118

119
  void testAssignment() {
120
121
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
122
123
124
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
125
126
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
127
    S1->setKey(P1);
128
    S1->setKeyN(10);
129
    S2->setKey(Sp1);
130
    S2->setKeyN(11);
131
132
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
133
    Union A(std::move(S1), std::move(S2));
134
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
135
136
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
137
  }
138

139
  void testSetLeaves() {
140
141
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
142
143
144
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
145
146
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
147
    S1->setKey(P1);
148
    S1->setKeyN(10);
149
    S2->setKey(Sp1);
150
    S2->setKeyN(11);
151
152
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
153
    Union A;
154
    A.setLeaves(std::move(S1), std::move(S2));
155
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
156
157
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
158
  }
159

160
  void testSetLeaf() {
161
162
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
163
164
165
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
166
167
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
168
    S1->setKey(P1);
169
    S1->setKeyN(10);
170
    S2->setKey(Sp1);
171
    S2->setKeyN(11);
172
173
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
174
    Union A;
175
    A.setLeaf(std::move(S2), 1);
176
    TS_ASSERT_EQUALS(A.leaf(0), (Rule *)nullptr);
177
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
178
    A.setLeaf(std::move(S1), 0);
179
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
180
181
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
182
  }
183

184
  void testFindLeaf() {
185
    SurfPoint S3;
186
187
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
188
189
190
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
191
192
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
193
    S1->setKey(P1);
194
    S1->setKeyN(10);
195
    S2->setKey(Sp1);
196
    S2->setKeyN(11);
197
198
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
199
    Union A;
200
    A.setLeaves(std::move(S1), std::move(S2));
201
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
202
203
204
205
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
    TS_ASSERT_EQUALS(A.findLeaf(ptrS1), 0);
    TS_ASSERT_EQUALS(A.findLeaf(ptrS2), 1);
206
207
    TS_ASSERT_EQUALS(A.findLeaf(&S3), -1);
  }
208

209
  void testFindKey() {
210
211
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
212
213
214
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
215
216
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
217
    S1->setKey(P1);
218
    S1->setKeyN(10);
219
    S2->setKey(Sp1);
220
    S2->setKeyN(11);
221
222
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
223
    Union A;
224
    A.setLeaves(std::move(S1), std::move(S2));
225
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
226
227
228
229
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
    TS_ASSERT_EQUALS(A.findKey(10), ptrS1);
    TS_ASSERT_EQUALS(A.findKey(11), ptrS2);
230
    TS_ASSERT_EQUALS(A.findKey(15), (Rule *)nullptr);
231
  }
232

233
234
  void testIsComplementary() { // Problem: it only detects whether first leaf or
                               // second leaf but not both
235
236
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
237
238
239
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
240
241
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
242
    S1->setKey(P1);
243
    S1->setKeyN(10);
244
    S2->setKey(Sp1);
245
    S2->setKeyN(11);
246
247
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
248
    Union A(std::move(S1), std::move(S2));
249
    TS_ASSERT_EQUALS(A.display(), "10 : 11");
250
251
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
252
    TS_ASSERT_EQUALS(A.isComplementary(), 0);
253
254
255
    auto B = Mantid::Kernel::make_unique<CompObj>();
    auto C = Mantid::Kernel::make_unique<CompObj>();
    A.setLeaf(std::move(B), 1);
256
    TS_ASSERT_EQUALS(A.isComplementary(), -1);
257
    A.setLeaf(std::move(C), 0);
258
259
    TS_ASSERT_EQUALS(A.isComplementary(), 1);
  }
260

261
  void testIsValid() {
262

263
264
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
265
266
267
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
268
269
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
270
    S1->setKey(P1);
271
    S1->setKeyN(10);
272
    S2->setKey(Sp1);
273
    S2->setKeyN(-11);
274
275
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
276
    Union A(std::move(S1), std::move(S2));
277
    TS_ASSERT_EQUALS(A.display(), "10 : -11");
278
279
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
280
281
282
283
284
285
286
    TS_ASSERT_EQUALS(A.isValid(V3D(0.0, 0.0, 0.0)), true); // on surface
    TS_ASSERT_EQUALS(A.isValid(V3D(5.0, 0.0, 0.0)), true); // inside surface
    TS_ASSERT_EQUALS(A.isValid(V3D(-0.1, 0.0, 0.0)),
                     false); // just outside surface
    TS_ASSERT_EQUALS(A.isValid(V3D(10.1, 1.0, 1.0)),
                     true); // on other side of the plane
  }
287

288
  void testIsValidMap() {
289
290
    auto P1 = boost::make_shared<Plane>();
    auto Sp1 = boost::make_shared<Sphere>();
291
292
293
    P1->setSurface("px 5");             // yz plane with x=5
    Sp1->setSurface("s 5.0 0.0 0.0 5"); // a sphere with center (5,0,0) and
                                        // radius 5. this will touch origin
294
295
    auto S1 = Mantid::Kernel::make_unique<SurfPoint>();
    auto S2 = Mantid::Kernel::make_unique<SurfPoint>();
296
    S1->setKey(P1);
297
    S1->setKeyN(10);
298
    S2->setKey(Sp1);
299
    S2->setKeyN(-11);
300
301
    auto ptrS1 = S1.get();
    auto ptrS2 = S2.get();
302
    Union A(std::move(S1), std::move(S2));
303
    TS_ASSERT_EQUALS(A.display(), "10 : -11");
304
305
    TS_ASSERT_EQUALS(A.leaf(0), ptrS1);
    TS_ASSERT_EQUALS(A.leaf(1), ptrS2);
306
307
308
309
310
311
312
313
314
315
316
317
318
    std::map<int, int> input;
    input[5] = 1;
    input[10] = 1;
    input[11] = 1;
    input[15] = 0;
    input[20] = -1;
    TS_ASSERT_EQUALS(A.isValid(input), true);
    input[10] = 0;
    input[11] = 0;
    TS_ASSERT_EQUALS(A.isValid(input), true);
    input[11] = 1;
    TS_ASSERT_EQUALS(A.isValid(input), false);
  }
319
320
321
};

#endif