/Framework/Geometry/test/RectangularDetectorTest.h

https://github.com/wdzhou/mantid · C Header · 247 lines · 182 code · 41 blank · 24 comment · 0 complexity · 5d069acc1655eaf3118b96770ab29aa1 MD5 · raw file

  1. #ifndef RECTANGULAR_DETECTOR_TEST_H
  2. #define RECTANGULAR_DETECTOR_TEST_H
  3. #include <cxxtest/TestSuite.h>
  4. #include <cmath>
  5. #include <string>
  6. #include "MantidGeometry/Instrument/RectangularDetector.h"
  7. #include "MantidKernel/V3D.h"
  8. #include "MantidGeometry/Objects/BoundingBox.h"
  9. #include "MantidKernel/Quat.h"
  10. #include "MantidTestHelpers/ComponentCreationHelper.h"
  11. #include "MantidGeometry/Objects/ShapeFactory.h"
  12. using namespace Mantid;
  13. using namespace Mantid::Geometry;
  14. using Mantid::Kernel::V3D;
  15. using Mantid::Kernel::Quat;
  16. class RectangularDetectorTest : public CxxTest::TestSuite {
  17. public:
  18. void testEmptyConstructor() {
  19. RectangularDetector q;
  20. TS_ASSERT_EQUALS(q.nelements(), 0);
  21. TS_ASSERT_THROWS(q[0], std::runtime_error);
  22. TS_ASSERT_EQUALS(q.getName(), "");
  23. TS_ASSERT(!q.getParent());
  24. TS_ASSERT_EQUALS(q.getRelativePos(), V3D(0, 0, 0));
  25. TS_ASSERT_EQUALS(q.getRelativeRot(), Quat(1, 0, 0, 0));
  26. // as there is no parent GetPos should equal getRelativePos
  27. TS_ASSERT_EQUALS(q.getRelativePos(), q.getPos());
  28. }
  29. void testNameValueConstructor() {
  30. RectangularDetector q("Name");
  31. TS_ASSERT_EQUALS(q.nelements(), 0);
  32. TS_ASSERT_THROWS(q[0], std::runtime_error);
  33. TS_ASSERT_EQUALS(q.getName(), "Name");
  34. TS_ASSERT(!q.getParent());
  35. TS_ASSERT_EQUALS(q.getRelativePos(), V3D(0, 0, 0));
  36. TS_ASSERT_EQUALS(q.getRelativeRot(), Quat(1, 0, 0, 0));
  37. // as there is no parent GetPos should equal getRelativePos
  38. TS_ASSERT_EQUALS(q.getRelativePos(), q.getPos());
  39. }
  40. void testNameParentValueConstructor() {
  41. CompAssembly *parent = new CompAssembly("Parent");
  42. parent->setPos(1, 2, 3);
  43. // name and parent
  44. RectangularDetector *q = new RectangularDetector("Child", parent);
  45. q->setPos(1, 1, 1);
  46. TS_ASSERT_EQUALS(q->getName(), "Child");
  47. TS_ASSERT_EQUALS(q->nelements(), 0);
  48. TS_ASSERT_THROWS((*q)[0], std::runtime_error);
  49. // check the parent
  50. TS_ASSERT(q->getParent());
  51. TS_ASSERT_EQUALS(q->getParent()->getName(), parent->getName());
  52. // 1,1,1 is added to (1,2,3)
  53. TS_ASSERT_EQUALS(q->getPos(), V3D(2, 3, 4));
  54. TS_ASSERT_EQUALS(q->getRelativeRot(), Quat(1, 0, 0, 0));
  55. // Now test the parametrized version of that
  56. ParameterMap_sptr pmap(new ParameterMap());
  57. RectangularDetector pq(q, pmap.get());
  58. TS_ASSERT_EQUALS(pq.getPos(), V3D(2, 3, 4));
  59. TS_ASSERT_EQUALS(pq.getRelativeRot(), Quat(1, 0, 0, 0));
  60. delete parent;
  61. }
  62. void testCorrectNameComparison() {
  63. // Test allowed names
  64. TS_ASSERT(RectangularDetector::compareName("RectangularDetector"));
  65. TS_ASSERT(RectangularDetector::compareName("rectangularDetector"));
  66. TS_ASSERT(RectangularDetector::compareName("rectangulardetector"));
  67. TS_ASSERT(RectangularDetector::compareName("rectangular_detector"));
  68. // Test fail on incorrect names
  69. TS_ASSERT(!RectangularDetector::compareName("Rectangular Detector"));
  70. TS_ASSERT(!RectangularDetector::compareName("Rectangular"));
  71. TS_ASSERT(!RectangularDetector::compareName("Detector"));
  72. }
  73. void testFullConstructor() {
  74. boost::shared_ptr<Geometry::Object> cuboidShape =
  75. ComponentCreationHelper::createCuboid(0.5);
  76. RectangularDetector *det = new RectangularDetector("MyRectangle");
  77. det->setPos(1000., 2000., 3000.);
  78. // Initialize with these parameters
  79. det->initialize(cuboidShape, 100, -50.0, 1.0, 200, -100.0, 1.0, 1000000,
  80. true, 1000);
  81. do_test_on(det);
  82. // --- Now make a parametrized version ----
  83. ParameterMap_sptr pmap(new ParameterMap());
  84. RectangularDetector *parDet = new RectangularDetector(det, pmap.get());
  85. do_test_on(parDet);
  86. delete det;
  87. delete parDet;
  88. }
  89. /** Test on a rectangular detector that will be
  90. * repeated on an un-moved pRectangularDetectorPixelarametrized version.
  91. */
  92. void do_test_on(RectangularDetector *det) {
  93. TS_ASSERT_EQUALS(det->xpixels(), 100);
  94. TS_ASSERT_EQUALS(det->xstart(), -50);
  95. TS_ASSERT_EQUALS(det->xstep(), 1.0);
  96. TS_ASSERT_EQUALS(det->xsize(), 100.0);
  97. TS_ASSERT_EQUALS(det->ypixels(), 200);
  98. TS_ASSERT_EQUALS(det->ystart(), -100);
  99. TS_ASSERT_EQUALS(det->ystep(), 1.0);
  100. TS_ASSERT_EQUALS(det->ysize(), 200.0);
  101. // Go out of bounds
  102. TS_ASSERT_THROWS(det->getAtXY(-1, 0), std::runtime_error);
  103. TS_ASSERT_THROWS(det->getAtXY(0, -1), std::runtime_error);
  104. TS_ASSERT_THROWS(det->getAtXY(100, 0), std::runtime_error);
  105. TS_ASSERT_THROWS(det->getAtXY(0, 205), std::runtime_error);
  106. // Check some ids
  107. TS_ASSERT_EQUALS(det->getAtXY(0, 0)->getID() - 1000000, 0);
  108. TS_ASSERT_EQUALS(det->getAtXY(0, 12)->getID() - 1000000, 12);
  109. TS_ASSERT_EQUALS(det->getAtXY(0, 112)->getID() - 1000000, 112);
  110. TS_ASSERT_EQUALS(det->getAtXY(1, 12)->getID() - 1000000, 1012);
  111. TS_ASSERT_EQUALS(det->getDetectorIDAtXY(0, 0), 1000000);
  112. TS_ASSERT_EQUALS(det->getDetectorIDAtXY(0, 12), 1000012);
  113. TS_ASSERT_EQUALS(det->getDetectorIDAtXY(0, 112), 1000112);
  114. TS_ASSERT_EQUALS(det->getDetectorIDAtXY(1, 12), 1001012);
  115. std::pair<int, int> xy;
  116. int x;
  117. int y;
  118. TS_ASSERT_THROWS_NOTHING(xy = det->getXYForDetectorID(1000000));
  119. x = xy.first;
  120. y = xy.second;
  121. TS_ASSERT_EQUALS(x, 0);
  122. TS_ASSERT_EQUALS(y, 0);
  123. TS_ASSERT_THROWS_NOTHING(xy = det->getXYForDetectorID(1000000 + 12));
  124. x = xy.first;
  125. y = xy.second;
  126. TS_ASSERT_EQUALS(x, 0);
  127. TS_ASSERT_EQUALS(y, 12);
  128. TS_ASSERT_THROWS_NOTHING(xy = det->getXYForDetectorID(1000000 + 112));
  129. x = xy.first;
  130. y = xy.second;
  131. TS_ASSERT_EQUALS(x, 0);
  132. TS_ASSERT_EQUALS(y, 112);
  133. TS_ASSERT_THROWS_NOTHING(xy = det->getXYForDetectorID(1000000 + 3012));
  134. x = xy.first;
  135. y = xy.second;
  136. TS_ASSERT_EQUALS(x, 3);
  137. TS_ASSERT_EQUALS(y, 12);
  138. // Check some positions
  139. TS_ASSERT_EQUALS(det->getAtXY(0, 0)->getPos(),
  140. V3D(1000 - 50., 2000 - 100., 3000.));
  141. TS_ASSERT_EQUALS(det->getAtXY(1, 0)->getPos(),
  142. V3D(1000 - 50. + 1., 2000 - 100., 3000.));
  143. TS_ASSERT_EQUALS(det->getAtXY(1, 1)->getPos(),
  144. V3D(1000 - 50. + 1., 2000 - 100. + 1., 3000.));
  145. // Name
  146. TS_ASSERT_EQUALS(det->getAtXY(1, 2)->getName(), "MyRectangle(1,2)");
  147. TS_ASSERT_EQUALS(det->getChild(1)->getName(), "MyRectangle(x=1)");
  148. BoundingBox box;
  149. det->getBoundingBox(box);
  150. TS_ASSERT_DELTA(box.xMin(), 949.5, 1e-08);
  151. TS_ASSERT_DELTA(box.yMin(), 1899.5, 1e-08);
  152. TS_ASSERT_DELTA(box.zMin(), 2999.5, 1e-08);
  153. TS_ASSERT_DELTA(box.xMax(), 1049.5, 1e-08);
  154. TS_ASSERT_DELTA(box.yMax(), 2099.5, 1e-08);
  155. TS_ASSERT_DELTA(box.zMax(), 3000.5, 1e-08);
  156. // Pull out a component and check that
  157. boost::shared_ptr<Detector> pixelDet = det->getAtXY(1, 2);
  158. box = BoundingBox();
  159. pixelDet->getBoundingBox(box);
  160. TS_ASSERT_DELTA(box.xMin(), 950.5, 1e-08);
  161. TS_ASSERT_DELTA(box.yMin(), 1901.5, 1e-08);
  162. TS_ASSERT_DELTA(box.zMin(), 2999.5, 1e-08);
  163. TS_ASSERT_DELTA(box.xMax(), 951.5, 1e-08);
  164. TS_ASSERT_DELTA(box.yMax(), 1902.5, 1e-08);
  165. TS_ASSERT_DELTA(box.zMax(), 3000.5, 1e-08);
  166. }
  167. /** Create a parametrized RectangularDetector with a parameter that
  168. * resizes it.
  169. */
  170. void testResizingParameter() {
  171. boost::shared_ptr<Geometry::Object> cuboidShape =
  172. ComponentCreationHelper::createCuboid(0.5);
  173. RectangularDetector *det = new RectangularDetector("MyRectangle");
  174. det->setPos(1000., 2000., 3000.);
  175. det->initialize(cuboidShape, 100, -50.0, 1.0, 200, -100.0, 1.0, 1000000,
  176. true, 1000);
  177. // --- Now make a parametrized version ----
  178. ParameterMap_sptr pmap(new ParameterMap());
  179. RectangularDetector *parDet = new RectangularDetector(det, pmap.get());
  180. pmap->addDouble(det, "scalex", 12);
  181. pmap->addDouble(det, "scaley", 23);
  182. // Sizes and steps are scaled by these factors
  183. TS_ASSERT_DELTA(parDet->xstep(), 12, 1e-5);
  184. TS_ASSERT_DELTA(parDet->ystep(), 23, 1e-5);
  185. TS_ASSERT_DELTA(parDet->xstart(), -50 * 12, 1e-5);
  186. TS_ASSERT_DELTA(parDet->ystart(), -100 * 23, 1e-5);
  187. TS_ASSERT_DELTA(parDet->xsize(), 100 * 12, 1e-5);
  188. TS_ASSERT_DELTA(parDet->ysize(), 200 * 23, 1e-5);
  189. V3D pos = parDet->getRelativePosAtXY(1, 1);
  190. TS_ASSERT_EQUALS(pos, V3D((-50 + 1) * 12., (-100 + 1) * 23., 0.));
  191. // Check some positions
  192. std::cout << parDet->getAtXY(0, 0)->getPos() << '\n';
  193. std::cout << parDet->getAtXY(1, 0)->getPos() << '\n';
  194. std::cout << parDet->getAtXY(1, 1)->getPos() << '\n';
  195. TS_ASSERT_EQUALS(parDet->getAtXY(0, 0)->getPos(),
  196. V3D(1000 - (50) * 12., 2000 - (100 * 23.), 3000.));
  197. TS_ASSERT_EQUALS(parDet->getAtXY(1, 0)->getPos(),
  198. V3D(1000 + (-50. + 1) * 12., 2000 - (100 * 23.), 3000.));
  199. TS_ASSERT_EQUALS(
  200. parDet->getAtXY(1, 1)->getPos(),
  201. V3D(1000 + (-50. + 1) * 12., 2000 + (-100. + 1) * 23., 3000.));
  202. delete det;
  203. delete parDet;
  204. }
  205. };
  206. #endif