/Framework/DataObjects/test/PeakShapeEllipsoidTest.h

https://github.com/wdzhou/mantid · C Header · 281 lines · 226 code · 46 blank · 9 comment · 0 complexity · c7b4409c35445b64341896170cb78793 MD5 · raw file

  1. #ifndef MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDTEST_H_
  2. #define MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDTEST_H_
  3. #include <cxxtest/TestSuite.h>
  4. #include "MantidDataObjects/PeakShapeEllipsoid.h"
  5. #include "MantidKernel/cow_ptr.h"
  6. #include "MantidKernel/V3D.h"
  7. #include "MantidKernel/Matrix.h"
  8. #include <vector>
  9. #include <json/json.h>
  10. using Mantid::DataObjects::PeakShapeEllipsoid;
  11. using Mantid::Kernel::SpecialCoordinateSystem;
  12. using namespace Mantid;
  13. using namespace Mantid::Kernel;
  14. class PeakShapeEllipsoidTest : public CxxTest::TestSuite {
  15. public:
  16. // This pair of boilerplate methods prevent the suite being created statically
  17. // This means the constructor isn't called when running other tests
  18. static PeakShapeEllipsoidTest *createSuite() {
  19. return new PeakShapeEllipsoidTest();
  20. }
  21. static void destroySuite(PeakShapeEllipsoidTest *suite) { delete suite; }
  22. void test_constructor() {
  23. auto directions = {(V3D(1, 0, 0)), (V3D(0, 1, 0)), (V3D(0, 0, 1))};
  24. const MantidVec abcRadii = {2, 3, 4};
  25. const MantidVec abcInnerRadii = {5, 6, 7};
  26. const MantidVec abcOuterRadii = {8, 9, 10};
  27. const SpecialCoordinateSystem frame = Mantid::Kernel::HKL;
  28. const std::string algorithmName = "foo";
  29. const int algorithmVersion = 3;
  30. // Construct it.
  31. PeakShapeEllipsoid shape(directions, abcRadii, abcInnerRadii, abcOuterRadii,
  32. frame, algorithmName, algorithmVersion);
  33. TS_ASSERT_EQUALS(abcRadii, shape.abcRadii());
  34. TS_ASSERT_EQUALS(abcInnerRadii, shape.abcRadiiBackgroundInner());
  35. TS_ASSERT_EQUALS(abcOuterRadii, shape.abcRadiiBackgroundOuter());
  36. TS_ASSERT_EQUALS(frame, shape.frame());
  37. TS_ASSERT_EQUALS(algorithmName, shape.algorithmName());
  38. TS_ASSERT_EQUALS(algorithmVersion, shape.algorithmVersion());
  39. }
  40. void test_constructor_throws() {
  41. auto directions = {V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)};
  42. auto bad_directions = {V3D(1, 0, 0)};
  43. const MantidVec abcRadii = {2, 3, 4};
  44. const MantidVec bad_abcRadii = {2, 3, 4, 5};
  45. const MantidVec abcInnerRadii = {5, 6, 7};
  46. const MantidVec bad_abcInnerRadii = {5, 6};
  47. const MantidVec abcOuterRadii = {8, 9, 10};
  48. const MantidVec bad_abcOuterRadii = {8, 9, 10, 11};
  49. const SpecialCoordinateSystem frame = Mantid::Kernel::HKL;
  50. TSM_ASSERT_THROWS("Should throw, bad directions",
  51. PeakShapeEllipsoid(bad_directions, abcRadii,
  52. abcInnerRadii, abcOuterRadii, frame),
  53. std::invalid_argument &);
  54. TSM_ASSERT_THROWS("Should throw, bad radii",
  55. PeakShapeEllipsoid(directions, bad_abcRadii,
  56. abcInnerRadii, abcOuterRadii, frame),
  57. std::invalid_argument &);
  58. TSM_ASSERT_THROWS("Should throw, bad inner radii",
  59. PeakShapeEllipsoid(directions, abcRadii,
  60. bad_abcInnerRadii, abcOuterRadii,
  61. frame),
  62. std::invalid_argument &);
  63. TSM_ASSERT_THROWS("Should throw, bad outer radii",
  64. PeakShapeEllipsoid(directions, abcRadii, abcInnerRadii,
  65. bad_abcOuterRadii, frame),
  66. std::invalid_argument &);
  67. }
  68. void test_copy_constructor() {
  69. auto directions = {V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)};
  70. const MantidVec abcRadii = {2, 3, 4};
  71. const MantidVec abcInnerRadii = {5, 6, 7};
  72. const MantidVec abcOuterRadii = {8, 9, 10};
  73. const SpecialCoordinateSystem frame = Mantid::Kernel::HKL;
  74. const std::string algorithmName = "foo";
  75. const int algorithmVersion = 3;
  76. // Construct it.
  77. PeakShapeEllipsoid a(directions, abcRadii, abcInnerRadii, abcOuterRadii,
  78. frame, algorithmName, algorithmVersion);
  79. PeakShapeEllipsoid b(a);
  80. TS_ASSERT_EQUALS(abcRadii, b.abcRadii());
  81. TS_ASSERT_EQUALS(abcInnerRadii, b.abcRadiiBackgroundInner());
  82. TS_ASSERT_EQUALS(abcOuterRadii, b.abcRadiiBackgroundOuter());
  83. TS_ASSERT_EQUALS(frame, b.frame());
  84. TS_ASSERT_EQUALS(algorithmName, b.algorithmName());
  85. TS_ASSERT_EQUALS(algorithmVersion, b.algorithmVersion());
  86. }
  87. void test_assignment() {
  88. PeakShapeEllipsoid a({V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)}, {2, 3, 4},
  89. {5, 6, 7}, {8, 9, 10}, Mantid::Kernel::HKL, "foo", 1);
  90. PeakShapeEllipsoid b({V3D(0, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)}, {1, 3, 4},
  91. {1, 6, 7}, {8, 9, 10}, QLab, "bar", 2);
  92. b = a;
  93. TS_ASSERT_EQUALS(a.abcRadii(), b.abcRadii());
  94. TS_ASSERT_EQUALS(a.abcRadiiBackgroundInner(), b.abcRadiiBackgroundInner());
  95. TS_ASSERT_EQUALS(a.abcRadiiBackgroundOuter(), b.abcRadiiBackgroundOuter());
  96. TS_ASSERT_EQUALS(a.frame(), b.frame());
  97. TS_ASSERT_EQUALS(a.algorithmName(), b.algorithmName());
  98. TS_ASSERT_EQUALS(a.algorithmVersion(), b.algorithmVersion());
  99. }
  100. void test_radius() {
  101. std::vector<double> radius = {1, 2, 3};
  102. PeakShapeEllipsoid shape({V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)}, radius,
  103. radius, radius, Mantid::Kernel::HKL);
  104. TSM_ASSERT_EQUALS("Radius should be taken to be the max of the ABC radii",
  105. 3.0, shape.radius());
  106. TSM_ASSERT_EQUALS(
  107. "Radius should be taken to be the max of the ABC radii", 3.0,
  108. shape.radius(Mantid::Geometry::PeakShape::RadiusType::Radius));
  109. TSM_ASSERT_EQUALS(
  110. "Radius should be taken to be the max of the ABC BackgroundInner radii",
  111. 3.0,
  112. shape.radius(Mantid::Geometry::PeakShape::RadiusType::InnerRadius));
  113. TSM_ASSERT_EQUALS(
  114. "Radius should be taken to be the max of the ABC BackgroundOuter radii",
  115. 3.0,
  116. shape.radius(Mantid::Geometry::PeakShape::RadiusType::OuterRadius));
  117. }
  118. void test_shape_name() {
  119. // Construct it.
  120. PeakShapeEllipsoid shape({V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)},
  121. {2, 3, 4}, {5, 6, 7}, {8, 9, 10},
  122. Mantid::Kernel::HKL, "foo", 1);
  123. TS_ASSERT_EQUALS("ellipsoid", shape.shapeName());
  124. }
  125. void test_toJSON() {
  126. std::vector<V3D> directions = {V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)};
  127. const MantidVec abcRadii = {2, 3, 4};
  128. const MantidVec abcInnerRadii = {5, 6, 7};
  129. const MantidVec abcOuterRadii = {8, 9, 10};
  130. const SpecialCoordinateSystem frame = Mantid::Kernel::HKL;
  131. const std::string algorithmName = "foo";
  132. const int algorithmVersion = 3;
  133. // Construct it.
  134. PeakShapeEllipsoid shape(directions, abcRadii, abcInnerRadii, abcOuterRadii,
  135. frame, algorithmName, algorithmVersion);
  136. const std::string json = shape.toJSON();
  137. Json::Reader reader;
  138. Json::Value output;
  139. TSM_ASSERT("Should parse as JSON", reader.parse(json, output));
  140. TS_ASSERT_EQUALS(directions[0].toString(), output["direction0"].asString());
  141. TS_ASSERT_EQUALS(directions[1].toString(), output["direction1"].asString());
  142. TS_ASSERT_EQUALS(directions[2].toString(), output["direction2"].asString());
  143. TS_ASSERT_EQUALS(algorithmName, output["algorithm_name"].asString());
  144. TS_ASSERT_EQUALS(algorithmVersion, output["algorithm_version"].asInt());
  145. TS_ASSERT_EQUALS(frame, output["frame"].asInt());
  146. TS_ASSERT_EQUALS(abcRadii[0], output["radius0"].asDouble());
  147. TS_ASSERT_EQUALS(abcRadii[1], output["radius1"].asDouble());
  148. TS_ASSERT_EQUALS(abcRadii[2], output["radius2"].asDouble());
  149. TS_ASSERT_EQUALS(abcOuterRadii[0],
  150. output["background_outer_radius0"].asDouble());
  151. TS_ASSERT_EQUALS(abcOuterRadii[1],
  152. output["background_outer_radius1"].asDouble());
  153. TS_ASSERT_EQUALS(abcOuterRadii[2],
  154. output["background_outer_radius2"].asDouble());
  155. }
  156. void test_directionsInSpecificFrameThrowsForMatrixWithInvalidDimensions() {
  157. auto directions = {V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)};
  158. const MantidVec abcRadii = {2, 3, 4};
  159. const MantidVec abcInnerRadii = {5, 6, 7};
  160. const MantidVec abcOuterRadii = {8, 9, 10};
  161. const SpecialCoordinateSystem frame = Mantid::Kernel::QLab;
  162. const std::string algorithmName = "foo";
  163. const int algorithmVersion = 3;
  164. // Construct it.
  165. PeakShapeEllipsoid a(directions, abcRadii, abcInnerRadii, abcOuterRadii,
  166. frame, algorithmName, algorithmVersion);
  167. Mantid::Kernel::Matrix<double> matrix(3, 2);
  168. std::vector<double> column1;
  169. column1.push_back(1.0);
  170. column1.push_back(1.0);
  171. column1.push_back(1.0);
  172. std::vector<double> column2;
  173. column2.push_back(1.0);
  174. column2.push_back(1.0);
  175. column2.push_back(1.0);
  176. matrix.setColumn(0, column1);
  177. matrix.setColumn(1, column2);
  178. TSM_ASSERT_THROWS("Should throw, bad goniometer matrix",
  179. a.getDirectionInSpecificFrame(matrix),
  180. std::invalid_argument &);
  181. }
  182. void test_directionsInSepcificFrame() {
  183. auto directions = {V3D(1, 0, 0), V3D(0, 1, 0), V3D(0, 0, 1)};
  184. const MantidVec abcRadii = {2, 3, 4};
  185. const MantidVec abcInnerRadii = {5, 6, 7};
  186. const MantidVec abcOuterRadii = {8, 9, 10};
  187. const SpecialCoordinateSystem frame = Mantid::Kernel::QLab;
  188. const std::string algorithmName = "foo";
  189. const int algorithmVersion = 3;
  190. // Construct it.
  191. PeakShapeEllipsoid a(directions, abcRadii, abcInnerRadii, abcOuterRadii,
  192. frame, algorithmName, algorithmVersion);
  193. // 90 degree rotation around the z axis
  194. Mantid::Kernel::Matrix<double> matrix(3, 3);
  195. std::vector<double> column1;
  196. column1.push_back(0.0);
  197. column1.push_back(1.0);
  198. column1.push_back(0.0);
  199. std::vector<double> column2;
  200. column2.push_back(-1.0);
  201. column2.push_back(0.0);
  202. column2.push_back(0.0);
  203. std::vector<double> column3;
  204. column3.push_back(0.0);
  205. column3.push_back(0.0);
  206. column3.push_back(1.0);
  207. matrix.setColumn(0, column1);
  208. matrix.setColumn(1, column2);
  209. matrix.setColumn(2, column3);
  210. std::vector<Mantid::Kernel::V3D> directionInNewFrame(3);
  211. TSM_ASSERT_THROWS_NOTHING("Should throw nothing, valid goniometer matrix",
  212. directionInNewFrame =
  213. a.getDirectionInSpecificFrame(matrix));
  214. const double delta = 1e-6;
  215. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[0][0], 0.0,
  216. delta);
  217. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[0][1], 1.0,
  218. delta);
  219. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[0][2], 0.0,
  220. delta);
  221. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[1][0], -1.0,
  222. delta);
  223. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[1][1], 0.0,
  224. delta);
  225. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[1][2], 0.0,
  226. delta);
  227. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[2][0], 0.0,
  228. delta);
  229. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[2][1], 0.0,
  230. delta);
  231. TSM_ASSERT_DELTA("Should be rotated", directionInNewFrame[2][2], 1.0,
  232. delta);
  233. }
  234. };
  235. #endif /* MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDTEST_H_ */