From 9b35cf5ed55a19f4aa90b7392d2b5472a0cd646f Mon Sep 17 00:00:00 2001 From: Michael Wedel <michael.wedel@psi.ch> Date: Thu, 23 Apr 2015 10:34:19 +0200 Subject: [PATCH] Refs #10702. Extracting crystal system names into PointGroup.h This way it's easier to convert between strings/enum in different places. --- .../CurveFitting/src/PawleyFunction.cpp | 21 +-------- .../inc/MantidGeometry/Crystal/PointGroup.h | 8 ++++ .../Geometry/src/Crystal/PointGroup.cpp | 46 +++++++++++++++++++ .../Framework/Geometry/test/PointGroupTest.h | 32 +++++++++++++ 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/Code/Mantid/Framework/CurveFitting/src/PawleyFunction.cpp b/Code/Mantid/Framework/CurveFitting/src/PawleyFunction.cpp index 01230f49b7e..a64b5232ad4 100644 --- a/Code/Mantid/Framework/CurveFitting/src/PawleyFunction.cpp +++ b/Code/Mantid/Framework/CurveFitting/src/PawleyFunction.cpp @@ -183,26 +183,7 @@ void PawleyParameterFunction::setProfileFunction( */ void PawleyParameterFunction::setCrystalSystem(const std::string &crystalSystem) { - std::string crystalSystemLC = boost::algorithm::to_lower_copy(crystalSystem); - - if (crystalSystemLC == "cubic") { - m_crystalSystem = PointGroup::Cubic; - } else if (crystalSystemLC == "tetragonal") { - m_crystalSystem = PointGroup::Tetragonal; - } else if (crystalSystemLC == "hexagonal") { - m_crystalSystem = PointGroup::Hexagonal; - } else if (crystalSystemLC == "trigonal") { - m_crystalSystem = PointGroup::Trigonal; - } else if (crystalSystemLC == "orthorhombic") { - m_crystalSystem = PointGroup::Orthorhombic; - } else if (crystalSystemLC == "monoclinic") { - m_crystalSystem = PointGroup::Monoclinic; - } else if (crystalSystemLC == "triclinic") { - m_crystalSystem = PointGroup::Triclinic; - } else { - throw std::invalid_argument("Not a valid crystal system: '" + - crystalSystem + "'."); - } + m_crystalSystem = Geometry::getCrystalSystemFromString(crystalSystem); createCrystalSystemParameters(m_crystalSystem); } diff --git a/Code/Mantid/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h b/Code/Mantid/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h index f6ed56e7ba3..37c7e43c074 100644 --- a/Code/Mantid/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h +++ b/Code/Mantid/Framework/Geometry/inc/MantidGeometry/Crystal/PointGroup.h @@ -75,6 +75,14 @@ typedef std::multimap<PointGroup::CrystalSystem, PointGroup_sptr> PointGroupCrystalSystemMap; MANTID_GEOMETRY_DLL PointGroupCrystalSystemMap getPointGroupsByCrystalSystem(); +MANTID_GEOMETRY_DLL +std::string +getCrystalSystemAsString(const PointGroup::CrystalSystem &crystalSystem); + +MANTID_GEOMETRY_DLL +PointGroup::CrystalSystem +getCrystalSystemFromString(const std::string &crystalSystem); + } // namespace Mantid } // namespace Geometry diff --git a/Code/Mantid/Framework/Geometry/src/Crystal/PointGroup.cpp b/Code/Mantid/Framework/Geometry/src/Crystal/PointGroup.cpp index 3a521aea6cb..138b973085d 100644 --- a/Code/Mantid/Framework/Geometry/src/Crystal/PointGroup.cpp +++ b/Code/Mantid/Framework/Geometry/src/Crystal/PointGroup.cpp @@ -3,6 +3,7 @@ #include <set> #include <boost/make_shared.hpp> +#include <boost/algorithm/string.hpp> #include <iostream> #include "MantidGeometry/Crystal/PointGroupFactory.h" @@ -182,5 +183,50 @@ PointGroupCrystalSystemMap getPointGroupsByCrystalSystem() { return map; } +/// Return a human-readable string for the given crystal system +std::string +getCrystalSystemAsString(const PointGroup::CrystalSystem &crystalSystem) { + switch (crystalSystem) { + case PointGroup::Cubic: + return "Cubic"; + case PointGroup::Tetragonal: + return "Tetragonal"; + case PointGroup::Hexagonal: + return "Hexagonal"; + case PointGroup::Trigonal: + return "Trigonal"; + case PointGroup::Orthorhombic: + return "Orthorhombic"; + case PointGroup::Monoclinic: + return "Monoclinic"; + default: + return "Triclinic"; + } +} + +PointGroup::CrystalSystem +getCrystalSystemFromString(const std::string &crystalSystem) { + std::string crystalSystemLC = boost::algorithm::to_lower_copy(crystalSystem); + + if (crystalSystemLC == "cubic") { + return PointGroup::Cubic; + } else if (crystalSystemLC == "tetragonal") { + return PointGroup::Tetragonal; + } else if (crystalSystemLC == "hexagonal") { + return PointGroup::Hexagonal; + } else if (crystalSystemLC == "trigonal") { + return PointGroup::Trigonal; + } else if (crystalSystemLC == "orthorhombic") { + return PointGroup::Orthorhombic; + } else if (crystalSystemLC == "monoclinic") { + return PointGroup::Monoclinic; + } else if (crystalSystemLC == "triclinic") { + return PointGroup::Triclinic; + } else { + throw std::invalid_argument("Not a valid crystal system: '" + + crystalSystem + "'."); + } +} + } // namespace Mantid } // namespace Geometry diff --git a/Code/Mantid/Framework/Geometry/test/PointGroupTest.h b/Code/Mantid/Framework/Geometry/test/PointGroupTest.h index 31a1f945dd5..0fd1f3159f4 100644 --- a/Code/Mantid/Framework/Geometry/test/PointGroupTest.h +++ b/Code/Mantid/Framework/Geometry/test/PointGroupTest.h @@ -193,6 +193,38 @@ public: checkPointGroupPerformance(pg); } + void testCrystalSystemNames() + { + TS_ASSERT_EQUALS(getCrystalSystemFromString("Cubic"), PointGroup::Cubic); + TS_ASSERT_EQUALS(getCrystalSystemFromString("cubic"), PointGroup::Cubic); + TS_ASSERT_EQUALS(getCrystalSystemFromString("CUBIC"), PointGroup::Cubic); + TS_ASSERT_EQUALS(getCrystalSystemFromString("CuBiC"), PointGroup::Cubic); + + TS_ASSERT_EQUALS(getCrystalSystemFromString("Tetragonal"), PointGroup::Tetragonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString("Hexagonal"), PointGroup::Hexagonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString("Trigonal"), PointGroup::Trigonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString("Orthorhombic"), PointGroup::Orthorhombic); + TS_ASSERT_EQUALS(getCrystalSystemFromString("Monoclinic"), PointGroup::Monoclinic); + TS_ASSERT_EQUALS(getCrystalSystemFromString("Triclinic"), PointGroup::Triclinic); + + TS_ASSERT_THROWS(getCrystalSystemFromString("DoesNotExist"), std::invalid_argument); + + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Cubic)), + PointGroup::Cubic); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Tetragonal)), + PointGroup::Tetragonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Hexagonal)), + PointGroup::Hexagonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Trigonal)), + PointGroup::Trigonal); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Orthorhombic)), + PointGroup::Orthorhombic); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Monoclinic)), + PointGroup::Monoclinic); + TS_ASSERT_EQUALS(getCrystalSystemFromString(getCrystalSystemAsString(PointGroup::Triclinic)), + PointGroup::Triclinic); + } + private: void checkPointGroupPerformance(const PointGroup_sptr &pointGroup) { -- GitLab