ObjectTest.h 37.2 KB
Newer Older
Russell Taylor's avatar
Russell Taylor committed
1
2
3
4
5
6
7
8
9
10
#ifndef MANTID_TESTOBJECT__
#define MANTID_TESTOBJECT__

#include <cxxtest/TestSuite.h>
#include <cmath>
#include <ostream>
#include <vector>
#include <algorithm>
#include <ctime>

11
12
#include <boost/shared_ptr.hpp>

Russell Taylor's avatar
Russell Taylor committed
13
#include "MantidGeometry/V3D.h" 
Nick Draper's avatar
re #843    
Nick Draper committed
14
15
16
17
18
19
20
21
#include "MantidGeometry/Objects/Object.h" 
#include "MantidGeometry/Surfaces/Cylinder.h" 
#include "MantidGeometry/Surfaces/Sphere.h" 
#include "MantidGeometry/Surfaces/Plane.h" 
#include "MantidGeometry/Math/Algebra.h" 
#include "MantidGeometry/Surfaces/SurfaceFactory.h" 
#include "MantidGeometry/Objects/Track.h" 
#include "MantidGeometry/Rendering/GluGeometryHandler.h"
22
#include "MantidGeometry/Objects/BoundingBox.h"
23

Russell Taylor's avatar
Russell Taylor committed
24
25
26
using namespace Mantid;
using namespace Geometry;

27
28
typedef boost::shared_ptr<Object> Object_sptr;

Russell Taylor's avatar
Russell Taylor committed
29
30
31
32
33
34
35
36
class testObject: public CxxTest::TestSuite
{
private:


public:


Nick Draper's avatar
re #843    
Nick Draper committed
37
  void testCreateUnitCube()
Russell Taylor's avatar
Russell Taylor committed
38
  {
39
    Object_sptr geom_obj = createUnitCube();
40
41
    
    TS_ASSERT_EQUALS(geom_obj->str(),"68 -6 5 -4 3 -2 1");
42
43
44
45

    double xmin(0.0), xmax(0.0), ymin(0.0), ymax(0.0), zmin(0.0), zmax(0.0);
    geom_obj->getBoundingBox(xmax, ymax, zmax, xmin, ymax,zmin);

Russell Taylor's avatar
Russell Taylor committed
46
47
  }

Nick Draper's avatar
re #843    
Nick Draper committed
48
  void testIsOnSideCappedCylinder()
Russell Taylor's avatar
Russell Taylor committed
49
  {
50
	Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
51
    //inside
52
53
54
55
56
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,0)),0); //origin
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,2.9,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-2.9,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-2.9)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,2.9)),0);
Russell Taylor's avatar
Russell Taylor committed
57
    //on the side
58
59
60
61
62
63
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,3)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.2,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.2,0,0)),1);
Russell Taylor's avatar
Russell Taylor committed
64
65

    //on the edges
66
67
68
69
70
71
72
73
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.2,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.2,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.2,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.2,0,3)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.2,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.2,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.2,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.2,0,3)),1);
Russell Taylor's avatar
Russell Taylor committed
74
    //out side
75
76
77
78
79
80
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,3.1,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-3.1,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-3.1)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,3.1)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(1.3,0,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(-3.3,0,0)),0);
Russell Taylor's avatar
Russell Taylor committed
81
82
83
84
  }

  void testIsValidCappedCylinder()
  {
85
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
86
    //inside
87
88
89
90
91
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,0)),1); //origin
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,2.9,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-2.9,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-2.9)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,2.9)),1);
Russell Taylor's avatar
Russell Taylor committed
92
    //on the side
93
94
95
96
97
98
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,3)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.2,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.2,0,0)),1);
Russell Taylor's avatar
Russell Taylor committed
99
100

    //on the edges
101
102
103
104
105
106
107
108
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.2,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.2,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.2,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.2,0,3)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.2,3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.2,-3,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.2,0,-3)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.2,0,3)),1);
Russell Taylor's avatar
Russell Taylor committed
109
    //out side
110
111
112
113
114
115
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,3.1,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-3.1,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-3.1)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,3.1)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(1.3,0,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(-3.3,0,0)),0);
Russell Taylor's avatar
Russell Taylor committed
116
117
118
119
  }

  void testIsOnSideSphere()
  {
120
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
121
    //inside
122
123
124
125
126
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,0)),0); //origin
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,4.0,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-4.0,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-4.0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,4.0)),0);
Russell Taylor's avatar
Russell Taylor committed
127
    //on the side
128
129
130
131
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,4.1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-4.1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-4.1)),1);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,4.1)),1);
Russell Taylor's avatar
Russell Taylor committed
132
133

    //out side
134
135
136
137
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,4.2,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,-4.2,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,-4.2)),0);
    TS_ASSERT_EQUALS(geom_obj->isOnSide(V3D(0,0,4.2)),0);
Russell Taylor's avatar
Russell Taylor committed
138
139
140
141
  }

  void testIsValidSphere()
  {
142
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
143
    //inside
144
145
146
147
148
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,0)),1); //origin
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,4.0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-4.0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-4.0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,4.0)),1);
Russell Taylor's avatar
Russell Taylor committed
149
    //on the side
150
151
152
153
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,4.1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-4.1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-4.1)),1);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,4.1)),1);
Russell Taylor's avatar
Russell Taylor committed
154
155

    //out side
156
157
158
159
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,4.2,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,-4.2,0)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,-4.2)),0);
    TS_ASSERT_EQUALS(geom_obj->isValid(V3D(0,0,4.2)),0);
Russell Taylor's avatar
Russell Taylor committed
160
161
162
163
  }

  void testCalcValidTypeSphere()
  {
164
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
165
    //entry on the normal
166
167
168
169
170
171
172
173
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-4.1,0,0),V3D(1,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-4.1,0,0),V3D(-1,0,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(4.1,0,0),V3D(1,0,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(4.1,0,0),V3D(-1,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,-4.1,0),V3D(0,1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,-4.1,0),V3D(0,-1,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,4.1,0),V3D(0,1,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,4.1,0),V3D(0,-1,0)),1);
Russell Taylor's avatar
Russell Taylor committed
174
175

    //a glancing blow
176
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-4.1,0,0),V3D(0,1,0)),0);
Russell Taylor's avatar
Russell Taylor committed
177
    //not quite on the normal
178
179
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-4.1,0,0),V3D(0.5,0.5,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(4.1,0,0),V3D(0.5,0.5,0)),-1);
Russell Taylor's avatar
Russell Taylor committed
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
  void testGetBoundingBoxForSphere()
  {
    Object_sptr geom_obj = createSphere();    
    const double tolerance(1e-10);

    double xmax,ymax,zmax,xmin,ymin,zmin;
    xmax=ymax=zmax=20;
    xmin=ymin=zmin=-20;
    geom_obj->getBoundingBox(xmax,ymax,zmax,xmin,ymin,zmin);
    TS_ASSERT_DELTA(xmax,4.1,tolerance);
    TS_ASSERT_DELTA(ymax,4.1,tolerance);
    TS_ASSERT_DELTA(zmax,4.1,tolerance);
    TS_ASSERT_DELTA(xmin,-4.1,tolerance);
    TS_ASSERT_DELTA(ymin,-4.1,tolerance);
    TS_ASSERT_DELTA(zmin,-4.1,tolerance);

    boost::shared_ptr<BoundingBox> bbox = geom_obj->getBoundingBox();

    TS_ASSERT_DELTA(bbox->xMax(),4.1,tolerance);
    TS_ASSERT_DELTA(bbox->yMax(),4.1,tolerance);
    TS_ASSERT_DELTA(bbox->zMax(),4.1,tolerance);
    TS_ASSERT_DELTA(bbox->xMin(),-4.1,tolerance);
    TS_ASSERT_DELTA(bbox->yMin(),-4.1,tolerance);
    TS_ASSERT_DELTA(bbox->zMin(),-4.1,tolerance);
  }

Russell Taylor's avatar
Russell Taylor committed
208
209
  void testCalcValidTypeCappedCylinder()
  {
210
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
211
    //entry on the normal
212
213
214
215
216
217
218
219
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-3.2,0,0),V3D(1,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-3.2,0,0),V3D(-1,0,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(1.2,0,0),V3D(1,0,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(1.2,0,0),V3D(-1,0,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,-3,0),V3D(0,1,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,-3,0),V3D(0,-1,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,3,0),V3D(0,1,0)),-1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(0,3,0),V3D(0,-1,0)),1);
Russell Taylor's avatar
Russell Taylor committed
220
221

    //a glancing blow
222
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-3.2,0,0),V3D(0,1,0)),0);
Russell Taylor's avatar
Russell Taylor committed
223
    //not quite on the normal
224
225
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(-3.2,0,0),V3D(0.5,0.5,0)),1);
    TS_ASSERT_EQUALS(geom_obj->calcValidType(V3D(1.2,0,0),V3D(0.5,0.5,0)),-1);
Russell Taylor's avatar
Russell Taylor committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  }

  void testInterceptSurfaceSphereZ()
  {
    std::vector<TUnit> expectedResults;
    std::string S41="s 1 1 1 4";         // Sphere at (1,1,1) radius 4

    // First create some surfaces
    std::map<int,Surface*> SphSurMap;
    SphSurMap[41]=new Sphere();
    SphSurMap[41]->setSurface(S41);
    SphSurMap[41]->setName(41);

    // A sphere 
    std::string ObjSphere="-41" ;

242
243
244
    Object_sptr geom_obj = Object_sptr(new Object); 
    geom_obj->setObject(41,ObjSphere);
    geom_obj->populate(SphSurMap);
Russell Taylor's avatar
Russell Taylor committed
245
246
247
248
249
250
251
252


    Track track(V3D(-1,1.5,1),V3D(1,0,0));

    // format = startPoint, endPoint, total distance so far, objectID
    // forward only intercepts means that start point should be track origin
    //expectedResults.push_back(TUnit(V3D(-sqrt(16-0.25)+1,1.5,1),
    expectedResults.push_back(TUnit(V3D(-1,1.5,1),
253
      V3D(sqrt(16-0.25)+1,1.5,1.0),sqrt(15.75)+2,geom_obj->getName()));
Russell Taylor's avatar
Russell Taylor committed
254

255
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
256
257
258
259
260
  }

  void testInterceptSurfaceSphereY()
  {
    std::vector<TUnit> expectedResults;
261
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
262
263
264
    Track track(V3D(0,-10,0),V3D(0,1,0));

    //format = startPoint, endPoint, total distance so far, objectID
265
    expectedResults.push_back(TUnit(V3D(0,-4.1,0),V3D(0,4.1,0),14.1,geom_obj->getName()));
Russell Taylor's avatar
Russell Taylor committed
266

267
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
268
269
270
271
272
  }

  void testInterceptSurfaceSphereX()
  {
    std::vector<TUnit> expectedResults;
273
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
274
    Track track(V3D(-10,0,0),V3D(1,0,0));
275

Russell Taylor's avatar
Russell Taylor committed
276
    //format = startPoint, endPoint, total distance so far, objectID
277
278
    expectedResults.push_back(TUnit(V3D(-4.1,0,0),V3D(4.1,0,0),14.1,geom_obj->getName()));
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
279
280
281
282
283
  }

  void testInterceptSurfaceCappedCylinderY()
  {
    std::vector<TUnit> expectedResults;
284
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
285
    //format = startPoint, endPoint, total distance so far, objectID
286
    expectedResults.push_back(TUnit(V3D(0,-3,0),V3D(0,3,0),13,geom_obj->getName()));
Russell Taylor's avatar
Russell Taylor committed
287
288

    Track track(V3D(0,-10,0),V3D(0,1,0));
289
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
290
291
292
293
294
  }

  void testInterceptSurfaceCappedCylinderX()
  {
    std::vector<TUnit> expectedResults;
295
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
296
297
298
    Track track(V3D(-10,0,0),V3D(1,0,0));

    //format = startPoint, endPoint, total distance so far, objectID
299
    expectedResults.push_back(TUnit(V3D(-3.2,0,0),V3D(1.2,0,0),11.2,geom_obj->getName()));
Russell Taylor's avatar
Russell Taylor committed
300

301
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
302
303
304
305
306
  }

  void testInterceptSurfaceCappedCylinderMiss()
  {
    std::vector<TUnit> expectedResults; //left empty as there are no expected results
307
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
308
309
    Track track(V3D(-10,0,0),V3D(1,1,0));

310
    checkTrackIntercept(geom_obj,track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
311
312
313
314
315
316
317
  }

  void checkTrackIntercept(Track& track, std::vector<TUnit>& expectedResults)
  {
    int index = 0;
    for (Track::LType::const_iterator it = track.begin(); it!=track.end();++it)
    {
318
319
      TS_ASSERT_DELTA(it->distFromStart,expectedResults[index].distFromStart,1e-6);
      TS_ASSERT_DELTA(it->distInsideObject,expectedResults[index].distInsideObject,1e-6);
Russell Taylor's avatar
Russell Taylor committed
320
      TS_ASSERT_EQUALS(it->ObjID,expectedResults[index].ObjID);
321
322
      TS_ASSERT_EQUALS(it->entryPoint,expectedResults[index].entryPoint);
      TS_ASSERT_EQUALS(it->exitPoint,expectedResults[index].exitPoint);
Russell Taylor's avatar
Russell Taylor committed
323
324
325
326
327
      ++index;
    }
    TS_ASSERT_EQUALS(index,static_cast<int>(expectedResults.size()));
  }

328
  void checkTrackIntercept(Object_sptr obj, Track& track, std::vector<TUnit>& expectedResults)
Russell Taylor's avatar
Russell Taylor committed
329
  {
330
    int unitCount = obj->interceptSurface(track);
331
332
    TS_ASSERT_EQUALS(unitCount,expectedResults.size());
    checkTrackIntercept(track,expectedResults);
Russell Taylor's avatar
Russell Taylor committed
333
334
335
336
337
338
339
340
341
342
  }

  void xtestTrackTwoIsolatedCubes()
    /*!
    Test a track going through an object
    */
  {
    std::string ObjA="60001 -60002 60003 -60004 60005 -60006";
    std::string ObjB="80001 -80002 60003 -60004 60005 -60006";
    
343
    createSurfaces(ObjA);
Russell Taylor's avatar
Russell Taylor committed
344
345
346
347
    Object object1=Object();
    object1.setObject(3,ObjA);
    object1.populate(SMap);

348
    createSurfaces(ObjB);
Russell Taylor's avatar
Russell Taylor committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
    Object object2=Object();
    object2.setObject(4,ObjB);
    object2.populate(SMap);

    Track TL(Geometry::V3D(-5,0,0),
      Geometry::V3D(1,0,0));

    // CARE: This CANNOT be called twice
    TS_ASSERT(object1.interceptSurface(TL)!=0)
      TS_ASSERT(object2.interceptSurface(TL)!=0)

      std::vector<TUnit> expectedResults;
    expectedResults.push_back(TUnit(V3D(-1,0,0),V3D(1,0,0),6,3));
    expectedResults.push_back(TUnit(V3D(4.5,0,0),V3D(6.5,0,0),11.5,4));
    checkTrackIntercept(TL,expectedResults);

  }

  void testTrackTwoTouchingCubes()
    /*!
    Test a track going through an object
    */
  {
    std::string ObjA="60001 -60002 60003 -60004 60005 -60006";
    std::string ObjB="60002 -80002 60003 -60004 60005 -60006";

375
    createSurfaces(ObjA);
Russell Taylor's avatar
Russell Taylor committed
376
377
378
379
    Object object1=Object();
    object1.setObject(3,ObjA);
    object1.populate(SMap);

380
    createSurfaces(ObjB);
Russell Taylor's avatar
Russell Taylor committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    Object object2=Object();
    object2.setObject(4,ObjB);
    object2.populate(SMap);

    Track TL(Geometry::V3D(-5,0,0),
      Geometry::V3D(1,0,0));

    // CARE: This CANNOT be called twice
    TS_ASSERT(object1.interceptSurface(TL)!=0)
      TS_ASSERT(object2.interceptSurface(TL)!=0)

      std::vector<TUnit> expectedResults;
    expectedResults.push_back(TUnit(V3D(-1,0,0),V3D(1,0,0),6,3));
    expectedResults.push_back(TUnit(V3D(1,0,0),V3D(6.5,0,0),11.5,4));

    checkTrackIntercept(TL,expectedResults);

  }

  void testTrackCubeWithInternalSphere()
    /*!
    Test a track going through an object
    */
  {
    std::string ObjA="60001 -60002 60003 -60004 60005 -60006 71";
    std::string ObjB="-71";

408
    createSurfaces(ObjA);
Russell Taylor's avatar
Russell Taylor committed
409
410
411
412
    Object object1=Object();
    object1.setObject(3,ObjA);
    object1.populate(SMap);

413
    createSurfaces(ObjB);
Russell Taylor's avatar
Russell Taylor committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
    Object object2=Object();
    object2.setObject(4,ObjB);
    object2.populate(SMap);

    Track TL(Geometry::V3D(-5,0,0),
      Geometry::V3D(1,0,0));

    // CARE: This CANNOT be called twice
    TS_ASSERT(object1.interceptSurface(TL)!=0);
    TS_ASSERT(object2.interceptSurface(TL)!=0);

    std::vector<TUnit> expectedResults;
    expectedResults.push_back(TUnit(V3D(-1,0,0),V3D(-0.8,0,0),4.2,3));
    expectedResults.push_back(TUnit(V3D(-0.8,0,0),V3D(0.8,0,0),5.8,4));
    expectedResults.push_back(TUnit(V3D(0.8,0,0),V3D(1,0,0),6,3));
    checkTrackIntercept(TL,expectedResults);
  }

  void testTrack_CubePlusInternalEdgeTouchSpheres()
    /*!
    Test a track going through an object
    */
  {
    std::string ObjA="60001 -60002 60003 -60004 60005 -60006 72 73";
    std::string ObjB="(-72 : -73)";
   
440
    createSurfaces(ObjA);
Russell Taylor's avatar
Russell Taylor committed
441
442
443
444
    Object object1=Object();
    object1.setObject(3,ObjA);
    object1.populate(SMap);

445
    createSurfaces(ObjB);
Russell Taylor's avatar
Russell Taylor committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    Object object2=Object();
    object2.setObject(4,ObjB);
    object2.populate(SMap);

    Track TL(Geometry::V3D(-5,0,0),
      Geometry::V3D(1,0,0));


    // CARE: This CANNOT be called twice
    TS_ASSERT(object1.interceptSurface(TL)!=0);
    TS_ASSERT(object2.interceptSurface(TL)!=0);

    std::vector<TUnit> expectedResults;
    expectedResults.push_back(TUnit(V3D(-1,0,0),V3D(-0.4,0,0),4.6,4));
    expectedResults.push_back(TUnit(V3D(-0.4,0,0),V3D(0.2,0,0),5.2,3));
    expectedResults.push_back(TUnit(V3D(0.2,0,0),V3D(1,0,0),6,4));
    checkTrackIntercept(TL,expectedResults);
  }

  void testTrack_CubePlusInternalEdgeTouchSpheresMiss()
    /*!
    Test a track missing an object
    */
  {
    std::string ObjA="60001 -60002 60003 -60004 60005 -60006 72 73";
    std::string ObjB="(-72 : -73)";
    
473
    createSurfaces(ObjA);
Russell Taylor's avatar
Russell Taylor committed
474
475
476
477
    Object object1=Object();
    object1.setObject(3,ObjA);
    object1.populate(SMap);

478
    createSurfaces(ObjB);
Russell Taylor's avatar
Russell Taylor committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
    Object object2=Object();
    object2.setObject(4,ObjB);
    object2.populate(SMap);

    Track TL(Geometry::V3D(-5,0,0),
      Geometry::V3D(0,1,0));


    // CARE: This CANNOT be called twice
    TS_ASSERT_EQUALS(object1.interceptSurface(TL),0);
    TS_ASSERT_EQUALS(object2.interceptSurface(TL),0);

    std::vector<TUnit> expectedResults; //left empty as this should miss
    checkTrackIntercept(TL,expectedResults);
  }

  void testFindPointInCube()
    /*!
    Test find point in cube
    */
  {
500
    Object_sptr geom_obj = createUnitCube();
Russell Taylor's avatar
Russell Taylor committed
501
502
    // initial guess in object
    Geometry::V3D pt;
503
    TS_ASSERT_EQUALS(geom_obj->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
504
505
506
507
508
509
    TS_ASSERT_EQUALS(pt,V3D(0,0,0));
    // initial guess not in object, but on x-axis
    std::vector<std::string> planes;
    planes.push_back("px 10"); planes.push_back("px 11");
    planes.push_back("py -0.5"); planes.push_back("py 0.5");
    planes.push_back("pz -0.5"); planes.push_back("pz 0.5");
510
511
    Object_sptr B =createCuboid(planes);
    TS_ASSERT_EQUALS(B->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
512
513
514
515
516
517
    TS_ASSERT_EQUALS(pt,V3D(10,0,0));
    // on y axis
    planes.clear();
    planes.push_back("px -0.5"); planes.push_back("px 0.5");
    planes.push_back("py -22"); planes.push_back("py -21");
    planes.push_back("pz -0.5"); planes.push_back("pz 0.5");
518
519
    Object_sptr C =createCuboid(planes);
    TS_ASSERT_EQUALS(C->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
520
521
522
523
524
525
    TS_ASSERT_EQUALS(pt,V3D(0,-21,0));
    // not on principle axis, now works using getBoundingBox
    planes.clear();
    planes.push_back("px 0.5"); planes.push_back("px 1.5");
    planes.push_back("py -22"); planes.push_back("py -21");
    planes.push_back("pz -0.5"); planes.push_back("pz 0.5");
526
527
    Object_sptr D =createCuboid(planes);
    TS_ASSERT_EQUALS(D->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
528
529
530
531
532
533
534
535
536
537
538
539
    TS_ASSERT_DELTA(pt.X(),1.0,1e-6);
    TS_ASSERT_DELTA(pt.Y(),-21.5,1e-6);
    TS_ASSERT_DELTA(pt.Z(),0.0,1e-6);
    planes.clear();
    // Test non axis aligned (AA) case - getPointInObject works because the object is on a principle axis
    // However, if not on a principle axis then the getBoundingBox fails to find correct minima (maxima are OK)
    // This is related to use of the complement for -ve surfaces and might be avoided by only using +ve surfaces
    // for defining non-AA objects. However, BoundingBox is poor for non-AA and needs improvement if these are
    // common
    planes.push_back("p 1 0 0 -0.5"); planes.push_back("p 1 0 0 0.5");
    planes.push_back("p 0 .70710678118 .70710678118 -1.1"); planes.push_back("p 0 .70710678118 .70710678118 -0.1");
    planes.push_back("p 0 -.70710678118 .70710678118 -0.5"); planes.push_back("p 0 -.70710678118 .70710678118 0.5");
540
541
    Object_sptr E =createCuboid(planes);
    TS_ASSERT_EQUALS(E->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
542
543
544
545
546
547
548
549
550
551
552
    TS_ASSERT_DELTA(pt.X(),0.0,1e-6);
    TS_ASSERT_DELTA(pt.Y(),-0.1414213562373,1e-6);
    TS_ASSERT_DELTA(pt.Z(),0.0,1e-6);
    planes.clear();
    // This test fails to find a point in object, as object not on a principle axis
    // and getBoundingBox does not give a useful result in this case.
    // Object is unit cube located at +-0.5 in x but centred on z=y=-1.606.. and rotated 45deg
    // to these two axes
    planes.push_back("p 1 0 0 -0.5"); planes.push_back("p 1 0 0 0.5");
    planes.push_back("p 0  .70710678118 .70710678118 -2"); planes.push_back("p 0  .70710678118 .70710678118 -1");
    planes.push_back("p 0 -.70710678118 .70710678118 -0.5"); planes.push_back("p 0 -.70710678118 .70710678118 0.5");
553
554
    Object_sptr F =createCuboid(planes);
    TS_ASSERT_EQUALS(F->getPointInObject(pt),0);
Russell Taylor's avatar
Russell Taylor committed
555
    // Test use of defineBoundingBox to explictly set the bounding box, when the automatic method fails
556
    F->defineBoundingBox(0.5,-1/(2.0*sqrt(2.0)),-1.0/(2.0*sqrt(2.0)),
Russell Taylor's avatar
Russell Taylor committed
557
      -0.5,-sqrt(2.0)-1.0/(2.0*sqrt(2.0)),-sqrt(2.0)-1.0/(2.0*sqrt(2.0)));
558
559
560
    TS_ASSERT_EQUALS(F->getPointInObject(pt),1);
    Object_sptr S = createSphere();
    TS_ASSERT_EQUALS(S->getPointInObject(pt),1);
Russell Taylor's avatar
Russell Taylor committed
561
562
563
564
565
566
567
568
569
    TS_ASSERT_EQUALS(pt,V3D(0.0,0.0,0));
  }


  void testSolidAngleSphere()
    /*!
    Test solid angle calculation for a sphere
    */
  {
570
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
571
572
573
574
575
576
    double satol=2e-2; // tolerance for solid angle

    // Solid angle at distance 8.1 from centre of sphere radius 4.1 x/y/z
    // Expected solid angle calculated values from sa=2pi(1-cos(arcsin(R/r))
    // where R is sphere radius and r is distance of observer from sphere centre
    // Intercept for track in reverse direction now worked round
577
578
579
580
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(8.1,0,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(0,8.1,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(0,0,8.1)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(0,0,-8.1)),0.864364,satol);
Russell Taylor's avatar
Russell Taylor committed
581
    // internal point (should be 4pi)
582
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(0,0,0)),4*M_PI,satol);
Russell Taylor's avatar
Russell Taylor committed
583
    // surface point
584
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(4.1,0,0)),2*M_PI,satol);
Russell Taylor's avatar
Russell Taylor committed
585
    // distant points
586
587
588
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(20,0,0)),0.133442,satol);
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(200,0,0)),0.0013204,satol);
    TS_ASSERT_DELTA(geom_obj->rayTraceSolidAngle(V3D(2000,0,0)),1.32025e-5,satol);
Russell Taylor's avatar
Russell Taylor committed
589
590
591
    //
    // test solidAngle interface, which will be main method to solid angle
    //
592
593
594
595
    TS_ASSERT_DELTA(geom_obj->solidAngle(V3D(8.1,0,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->solidAngle(V3D(0,8.1,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->solidAngle(V3D(0,0,8.1)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->solidAngle(V3D(0,0,-8.1)),0.864364,satol);
Russell Taylor's avatar
Russell Taylor committed
596
597
598
599
600
601
602
603
    //
  }

  void testSolidAngleCappedCylinder()
    /*!
    Test solid angle calculation for a capped cylinder
    */
  {
604
    Object_sptr geom_obj = createSmallCappedCylinder();
605
    // Want to test triangulation so setup a geometry handler
606
    boost::shared_ptr<GluGeometryHandler> h = boost::shared_ptr<GluGeometryHandler>(new GluGeometryHandler(geom_obj.get()));
607
    h->setCylinder(V3D(-1.0,0.0,0.0), V3D(1., 0.0, 0.0), 0.005, 0.003);
608
    geom_obj->setGeometryHandler(h);
609
610

    double satol(1e-4); // tolerance for solid angle
Russell Taylor's avatar
Russell Taylor committed
611

612
    // solid angle at point -0.5 from capped cyl -1.0 -0.997 in x, rad 0.005 - approx WISH cylinder
Russell Taylor's avatar
Russell Taylor committed
613
614
    //
    // soild angle of circle radius 3, distance 3 is 2pi(1-cos(t)) where
615
    // t is atan(3/3), should be 0.000317939
616
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-0.5, 0.0, 0.0)), 0.000317939, satol);
617
    // Other end
618
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-1.497, 0.0, 0.0)), 0.000317939, satol);
619

Russell Taylor's avatar
Russell Taylor committed
620
    // No analytic value for side on SA, using hi-res value
621
622
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0, 0, 0.1)), 8.03225e-05, satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0, 0.1, 0)), 8.03225e-05, satol);
623

Russell Taylor's avatar
Russell Taylor committed
624
    // internal point (should be 4pi)
625
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-0.999, 0.0, 0.0)),4*M_PI,satol);
626
627

    // surface points
628
629
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-1.0, 0.0, 0.0)),2*M_PI,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-0.997, 0.0, 0.0)),2*M_PI,satol);
630

Russell Taylor's avatar
Russell Taylor committed
631
632
633
634
635
636
637
638
  }

  void testSolidAngleCubeTriangles()
    /*!
    Test solid angle calculation for a cube using triangles
    - test for using Open Cascade surface triangulation for all solid angles.
    */
  {
639
    Object_sptr geom_obj = createUnitCube();
Russell Taylor's avatar
Russell Taylor committed
640
641
642
643
644
645
    double satol=1e-3; // tolerance for solid angle

    // solid angle at distance 0.5 should be 4pi/6 by symmetry
    //
    // tests for Triangulated cube
    //
646
647
648
649
650
651
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(1.0,0,0)),M_PI*2.0/3.0,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(-1.0,0,0)),M_PI*2.0/3.0,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,1.0,0)),M_PI*2.0/3.0,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,-1.0,0)),M_PI*2.0/3.0,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,1.0)),M_PI*2.0/3.0,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,-1.0)),M_PI*2.0/3.0,satol);
Russell Taylor's avatar
Russell Taylor committed
652
653
654
655
656
657
658
659
660
661

    if(timeTest)
    {
      // block to test time of solid angle methods
      // change false to true to include
      double saRay,saTri;
      V3D observer(1.0,0,0);
      int iter=4000;
      int starttime=clock();
      for (int i=0;i<iter;i++)
662
        saTri=geom_obj->triangleSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
663
664
665
666
667
      int endtime=clock();
      std::cout << std::endl << "Cube tri time=" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
      iter=50;
      starttime=clock();
      for (int i=0;i<iter;i++)
668
        saRay=geom_obj->rayTraceSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
669
670
671
672
673
674
      endtime=clock();
      std::cout << "Cube ray time=" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
    }

  }

675
  void testGetBoundingBoxForCylinder()
Russell Taylor's avatar
Russell Taylor committed
676
677
678
679
    /*!
    Test bounding box for a object capped cylinder
    */
  {
680
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
681
682
683
    double xmax,ymax,zmax,xmin,ymin,zmin;
    xmax=ymax=zmax=100;
    xmin=ymin=zmin=-100;
684
    geom_obj->getBoundingBox(xmax,ymax,zmax,xmin,ymin,zmin);
Russell Taylor's avatar
Russell Taylor committed
685
686
687
688
689
690
691
692
    TS_ASSERT_DELTA(xmax,1.2,0.0001);
    TS_ASSERT_DELTA(ymax,3.0,0.0001);
    TS_ASSERT_DELTA(zmax,3.0,0.0001);
    TS_ASSERT_DELTA(xmin,-3.2,0.0001);
    TS_ASSERT_DELTA(ymin,-3.0,0.0001);
    TS_ASSERT_DELTA(zmin,-3.0,0.0001);
  }

693
  void testdefineBoundingBox()
Russell Taylor's avatar
Russell Taylor committed
694
695
696
697
    /*!
    Test use of defineBoundingBox
    */
  {
698
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
699
700
701
    double xmax,ymax,zmax,xmin,ymin,zmin;
    xmax=1.2;ymax=3.0;zmax=3.0;
    xmin=-3.2;ymin=-3.0;zmin=-3.0;
702
703
704
705
706
707
708
709
710
711
712
713
714

    TS_ASSERT_THROWS_NOTHING(geom_obj->defineBoundingBox(xmax,ymax,zmax,xmin,ymin,zmin));
    
    boost::shared_ptr<BoundingBox> boundBox = geom_obj->getBoundingBox();

    TS_ASSERT_EQUALS(boundBox->xMax(),1.2);
    TS_ASSERT_EQUALS(boundBox->yMax(),3.0);
    TS_ASSERT_EQUALS(boundBox->zMax(),3.0);
    TS_ASSERT_EQUALS(boundBox->xMin(),-3.2);
    TS_ASSERT_EQUALS(boundBox->yMin(),-3.0);
    TS_ASSERT_EQUALS(boundBox->zMin(),-3.0);

    //Inconsistent bounding box
Russell Taylor's avatar
Russell Taylor committed
715
    xmax=1.2;xmin=3.0;
716
    TS_ASSERT_THROWS(geom_obj->defineBoundingBox(xmax,ymax,zmax,xmin,ymin,zmin),std::invalid_argument);
Russell Taylor's avatar
Russell Taylor committed
717
718
719
720
721
722
723

  }
  void testSurfaceTriangulation()
    /*!
    Test triangle solid angle calc
    */
  {
724
    Object_sptr geom_obj = createCappedCylinder();
Russell Taylor's avatar
Russell Taylor committed
725
726
727
    double xmax,ymax,zmax,xmin,ymin,zmin;
    xmax=20;ymax=20.0;zmax=20.0;
    xmin=-20.0;ymin=-20.0;zmin=-20.0;
728
    geom_obj->getBoundingBox(xmax,ymax,zmax,xmin,ymin,zmin);
Russell Taylor's avatar
Russell Taylor committed
729
730
731
732
733
734
735
736
737
738
739
740
    double saTri,saRay;
    V3D observer(4.2,0,0);
    
    double satol=1e-3; // typical result tolerance

    if(timeTest)
    {
      // block to test time of solid angle methods
      // change false to true to include
      int iter=4000;
      int starttime=clock();
      for (int i=0;i<iter;i++)
741
        saTri=geom_obj->triangleSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
742
743
744
745
746
      int endtime=clock();
      std::cout << std::endl << "Cyl tri time=" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
      iter=50;
      starttime=clock();
      for (int i=0;i<iter;i++)
747
        saRay=geom_obj->rayTraceSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
748
749
750
751
      endtime=clock();
      std::cout << "Cyl ray time=" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
    }

752
753
    saTri=geom_obj->triangleSolidAngle(observer);
    saRay=geom_obj->rayTraceSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
754
755
756
757
    TS_ASSERT_DELTA(saTri,1.840302,0.001);
    TS_ASSERT_DELTA(saRay,1.840302,0.01);
    
    observer=V3D(-7.2,0,0);
758
759
    saTri=geom_obj->triangleSolidAngle(observer);
    saRay=geom_obj->rayTraceSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
760
761
762
763
764
    
    TS_ASSERT_DELTA(saTri,1.25663708,0.001);
    TS_ASSERT_DELTA(saRay,1.25663708,0.001);

    // No analytic value for side on SA, using hi-res value
765
766
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,7)),0.7531,0.753*satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,7,0)),0.7531,0.753*satol);
Russell Taylor's avatar
Russell Taylor committed
767

768
    saTri=geom_obj->triangleSolidAngle(V3D(20,0,0));
Russell Taylor's avatar
Russell Taylor committed
769
    TS_ASSERT_DELTA(saTri,0.07850147,satol*0.0785);
770
    saTri=geom_obj->triangleSolidAngle(V3D(200,0,0));
Russell Taylor's avatar
Russell Taylor committed
771
    TS_ASSERT_DELTA(saTri,0.000715295,satol*0.000715);
772
    saTri=geom_obj->triangleSolidAngle(V3D(2000,0,0));
Russell Taylor's avatar
Russell Taylor committed
773
774
775
776
777
778
779
780
    TS_ASSERT_DELTA(saTri,7.08131e-6,satol*7.08e-6);
    
  }
  void testSolidAngleSphereTri()
    /*!
    Test solid angle calculation for a sphere from triangulation
    */
  {
781
    Object_sptr geom_obj = createSphere();
Russell Taylor's avatar
Russell Taylor committed
782
783
784
785
786
787
    double satol=1e-3; // tolerance for solid angle

    // Solid angle at distance 8.1 from centre of sphere radius 4.1 x/y/z
    // Expected solid angle calculated values from sa=2pi(1-cos(arcsin(R/r))
    // where R is sphere radius and r is distance of observer from sphere centre
    // Intercept for track in reverse direction now worked round
788
789
790
791
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(8.1,0,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,8.1,0)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,8.1)),0.864364,satol);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,-8.1)),0.864364,satol);
Russell Taylor's avatar
Russell Taylor committed
792
    // internal point (should be 4pi)
793
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(0,0,0)),4*M_PI,satol);
Russell Taylor's avatar
Russell Taylor committed
794
    // surface point
795
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(4.1,0,0)),2*M_PI,satol);
Russell Taylor's avatar
Russell Taylor committed
796
    // distant points
797
798
799
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(20,0,0)),0.133442,satol*0.133);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(200,0,0)),0.0013204,satol*0.00132);
    TS_ASSERT_DELTA(geom_obj->triangleSolidAngle(V3D(2000,0,0)),1.32025e-5,satol*1.32e-5);
Russell Taylor's avatar
Russell Taylor committed
800
801
802
803
804
805
806
807
808
809

    if(timeTest)
    {
      // block to test time of solid angle methods
      // change false to true to include
      double saTri,saRay;
      int iter=400;
      V3D observer(8.1,0,0);
      int starttime=clock();
      for (int i=0;i<iter;i++)
810
        saTri=geom_obj->triangleSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
811
812
813
814
815
      int endtime=clock();
      std::cout << std::endl << "Sphere tri time =" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
      iter=40;
      starttime=clock();
      for (int i=0;i<iter;i++)
816
        saRay=geom_obj->rayTraceSolidAngle(observer);
Russell Taylor's avatar
Russell Taylor committed
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
      endtime=clock();
      std::cout << "Sphere ray time =" << (endtime-starttime)/(static_cast<double>(CLOCKS_PER_SEC*iter)) << std::endl;
    }

  }

private:

  /// Surface type
  typedef std::map<int,Surface*> STYPE ; 

  /// set timeTest true to get time comparisons of soild angle methods
  const static bool timeTest=false;
  
  STYPE SMap;   ///< Surface Map

833
  Object_sptr createCappedCylinder()
Russell Taylor's avatar
Russell Taylor committed
834
835
836
837
838
839
840
841
842
843
844
845
  {
    std::string C31="cx 3.0";         // cylinder x-axis radius 3
    std::string C32="px 1.2";
    std::string C33="px -3.2";

    // First create some surfaces
    std::map<int,Surface*> CylSurMap;
    CylSurMap[31]=new Cylinder();
    CylSurMap[32]=new Plane();
    CylSurMap[33]=new Plane();

    CylSurMap[31]->setSurface(C31);
846
847
848
849
850
851
852
853
854
855
    CylSurMap[32]->setSurface(C32);
    CylSurMap[33]->setSurface(C33);
    CylSurMap[31]->setName(31);
    CylSurMap[32]->setName(32);
    CylSurMap[33]->setName(33);

    // Capped cylinder (id 21) 
    // using surface ids: 31 (cylinder) 32 (plane (top) ) and 33 (plane (base))
    std::string ObjCapCylinder="-31 -32 33";

856
857
858
859
860
	Object_sptr retVal = Object_sptr(new Object); 
    retVal->setObject(21,ObjCapCylinder);
    retVal->populate(CylSurMap);

	TS_ASSERT(retVal.get())
861
862
863
864
865
866

    return retVal;
  }
  
  // This creates a cylinder to test the solid angle that is more realistic in size
  // for a detector cylinder
867
  Object_sptr createSmallCappedCylinder()
868
869
870
871
872
873
874
875
876
877
878
879
  {
    std::string C31="cx 0.005";         // cylinder x-axis radius 0.005 and height 0.003
    std::string C32="px -0.997";
    std::string C33="px -1.0";

    // First create some surfaces
    std::map<int,Surface*> CylSurMap;
    CylSurMap[31]=new Cylinder();
    CylSurMap[32]=new Plane();
    CylSurMap[33]=new Plane();

    CylSurMap[31]->setSurface(C31);
Russell Taylor's avatar
Russell Taylor committed
880
881
882
883
884
885
886
887
888
889
    CylSurMap[32]->setSurface(C32);
    CylSurMap[33]->setSurface(C33);
    CylSurMap[31]->setName(31);
    CylSurMap[32]->setName(32);
    CylSurMap[33]->setName(33);

    // Capped cylinder (id 21) 
    // using surface ids: 31 (cylinder) 32 (plane (top) ) and 33 (plane (base))
    std::string ObjCapCylinder="-31 -32 33";

890
891
892
    Object_sptr retVal = Object_sptr(new Object); 
    retVal->setObject(21,ObjCapCylinder);
    retVal->populate(CylSurMap);
Russell Taylor's avatar
Russell Taylor committed
893
894
895
896

    return retVal;
  }

897
  Object_sptr createSphere()
Russell Taylor's avatar
Russell Taylor committed
898
899
900
901
902
903
904
905
906
907
908
909
  {
    std::string S41="so 4.1";         // Sphere at origin radius 4.1

    // First create some surfaces
    std::map<int,Surface*> SphSurMap;
    SphSurMap[41]=new Sphere();
    SphSurMap[41]->setSurface(S41);
    SphSurMap[41]->setName(41);

    // A sphere 
    std::string ObjSphere="-41" ;

910
911
912
    Object_sptr retVal = Object_sptr(new Object); 
    retVal->setObject(41,ObjSphere);
    retVal->populate(SphSurMap);
Russell Taylor's avatar
Russell Taylor committed
913
914
915
916
917
918
919
920
921
922

    return retVal;
  }

  void clearSurfMap()
    /*!
    Clears the surface map for a new test
    or destruction.
    */
  {
923
    SMap.clear();
Russell Taylor's avatar
Russell Taylor committed
924
925
926
    return;
  }

927
  void createSurfaces(const std::string& desired)
Russell Taylor's avatar
Russell Taylor committed
928
929
930
931
932
933
934
935
936
937
938
    /*!
    Creates a list of surfaces for used in the objects
    and populates the MObj layers.
    */
  {
    clearSurfMap();

    // PLANE SURFACES:

    typedef std::pair<int,std::string> SCompT;
    std::vector<SCompT> SurfLine;
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
    if (desired.find("60001") != std::string::npos)
      SurfLine.push_back(SCompT(60001,"px -1"));
    if (desired.find("60002") != std::string::npos)
      SurfLine.push_back(SCompT(60002,"px 1"));
    if (desired.find("60003") != std::string::npos)
      SurfLine.push_back(SCompT(60003,"py -2"));
    if (desired.find("60004") != std::string::npos)
      SurfLine.push_back(SCompT(60004,"py 2"));
    if (desired.find("60005") != std::string::npos)
      SurfLine.push_back(SCompT(60005,"pz -3"));
    if (desired.find("60006") != std::string::npos)
      SurfLine.push_back(SCompT(60006,"pz 3"));

    if (desired.find("80001") != std::string::npos)
      SurfLine.push_back(SCompT(80001,"px 4.5"));
    if (desired.find("80002") != std::string::npos)
      SurfLine.push_back(SCompT(80002,"px 6.5"));

    if (desired.find("71") != std::string::npos)
      SurfLine.push_back(SCompT(71,"so 0.8"));
    if (desired.find("72") != std::string::npos)
      SurfLine.push_back(SCompT(72,"s -0.7 0 0 0.3"));
    if (desired.find("73") != std::string::npos)
      SurfLine.push_back(SCompT(73,"s 0.6 0 0 0.4"));
Russell Taylor's avatar
Russell Taylor committed
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983

    std::vector<SCompT>::const_iterator vc;

    // Note that the testObject now manages the "new Plane"
    Geometry::Surface* A;
    for(vc=SurfLine.begin();vc!=SurfLine.end();vc++)
    {  
      A=Geometry::SurfaceFactory::Instance()->processLine(vc->second);
      if (!A)
      {
        std::cerr<<"Failed to process line "<<vc->second<<std::endl;
        exit(1);
      }
      A->setName(vc->first);
      SMap.insert(STYPE::value_type(vc->first,A));
    }

    return;
  }


984
  Object_sptr createUnitCube()
Russell Taylor's avatar
Russell Taylor committed
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  {
    std::string C1="px -0.5";         // cube +/-0.5
    std::string C2="px 0.5";
    std::string C3="py -0.5";
    std::string C4="py 0.5";
    std::string C5="pz -0.5";
    std::string C6="pz 0.5";

    // Create surfaces
    std::map<int,Surface*> CubeSurMap;
    CubeSurMap[1]=new Plane();
    CubeSurMap[2]=new Plane();
    CubeSurMap[3]=new Plane();
    CubeSurMap[4]=new Plane();
    CubeSurMap[5]=new Plane();
    CubeSurMap[6]=new Plane();