Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #10904. Factory for elliptical peak shapes
- Loading branch information
1 parent
2387ce5
commit 78da658
Showing
8 changed files
with
266 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/PeakShapeEllipsoidFactory.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORY_H_ | ||
#define MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORY_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "PeakShapeFactory.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataObjects | ||
{ | ||
|
||
/** PeakShapeEllipsoidFactory : TODO: DESCRIPTION | ||
Copyright © 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge National Laboratory & European Spallation Source | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
class DLLExport PeakShapeEllipsoidFactory : public PeakShapeFactory | ||
{ | ||
public: | ||
PeakShapeEllipsoidFactory(); | ||
virtual ~PeakShapeEllipsoidFactory(); | ||
|
||
// PeakShapeFactory interface | ||
|
||
PeakShape *create(const std::string &source) const; | ||
void setSuccessor(boost::shared_ptr<const PeakShapeFactory> successorFactory); | ||
|
||
private: | ||
/// Successor factory | ||
PeakShapeFactory_const_sptr m_successor; | ||
}; | ||
|
||
|
||
} // namespace DataObjects | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORY_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
Code/Mantid/Framework/DataObjects/src/PeakShapeEllipsoidFactory.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "MantidDataObjects/PeakShapeEllipsoidFactory.h" | ||
#include "MantidDataObjects/PeakShapeEllipsoid.h" | ||
#include "MantidAPI/SpecialCoordinateSystem.h" | ||
|
||
#include <jsoncpp/json/json.h> | ||
#include <stdexcept> | ||
|
||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
|
||
namespace Mantid { | ||
namespace DataObjects { | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Constructor | ||
*/ | ||
PeakShapeEllipsoidFactory::PeakShapeEllipsoidFactory() {} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Destructor | ||
*/ | ||
PeakShapeEllipsoidFactory::~PeakShapeEllipsoidFactory() {} | ||
|
||
/** | ||
* @brief Create the PeakShape | ||
* @param source : source JSON | ||
* @return PeakShape via this factory or it's successors | ||
*/ | ||
PeakShape *PeakShapeEllipsoidFactory::create(const std::string &source) const { | ||
Json::Reader reader; | ||
Json::Value root; | ||
PeakShape *product = NULL; | ||
if (reader.parse(source, root)) { | ||
const std::string shape = root["shape"].asString(); | ||
if (shape == PeakShapeEllipsoid::ellipsoidShapeName()) { | ||
|
||
const std::string algorithmName(root["algorithm_name"].asString()); | ||
const int algorithmVersion(root["algorithm_version"].asInt()); | ||
const SpecialCoordinateSystem frame( | ||
static_cast<SpecialCoordinateSystem>(root["frame"].asInt())); | ||
std::vector<double> abcRadii, abcRadiiBackgroundInner, | ||
abcRadiiBackgroundOuter; | ||
abcRadii.push_back(root["radius0"].asDouble()); | ||
abcRadii.push_back(root["radius1"].asDouble()); | ||
abcRadii.push_back(root["radius2"].asDouble()); | ||
abcRadiiBackgroundInner.push_back( | ||
root["background_inner_radius0"].asDouble()); | ||
abcRadiiBackgroundInner.push_back( | ||
root["background_inner_radius1"].asDouble()); | ||
abcRadiiBackgroundInner.push_back( | ||
root["background_inner_radius2"].asDouble()); | ||
abcRadiiBackgroundOuter.push_back( | ||
root["background_outer_radius0"].asDouble()); | ||
abcRadiiBackgroundOuter.push_back( | ||
root["background_outer_radius1"].asDouble()); | ||
abcRadiiBackgroundOuter.push_back( | ||
root["background_outer_radius2"].asDouble()); | ||
|
||
std::vector<V3D> directions(3); | ||
directions[0].fromString(root["direction0"].asString()); | ||
directions[1].fromString(root["direction1"].asString()); | ||
directions[2].fromString(root["direction2"].asString()); | ||
|
||
product = new PeakShapeEllipsoid( | ||
directions, abcRadii, abcRadiiBackgroundInner, | ||
abcRadiiBackgroundOuter, frame, algorithmName, algorithmVersion); | ||
|
||
} else { | ||
if (m_successor) { | ||
product = m_successor->create(source); | ||
} else { | ||
throw std::invalid_argument("PeakShapeSphericalFactory:: No successor " | ||
"factory able to process : " + | ||
source); | ||
} | ||
} | ||
|
||
} else { | ||
|
||
throw std::invalid_argument("PeakShapeSphericalFactory:: Source JSON for " | ||
"the peak shape is not valid: " + | ||
source); | ||
} | ||
return product; | ||
} | ||
|
||
/** | ||
* @brief Set successor | ||
* @param successorFactory : successor | ||
*/ | ||
void PeakShapeEllipsoidFactory::setSuccessor( | ||
boost::shared_ptr<const PeakShapeFactory> successorFactory) { | ||
this->m_successor = successorFactory; | ||
} | ||
|
||
|
||
} // namespace DataObjects | ||
} // namespace Mantid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Code/Mantid/Framework/DataObjects/test/PeakShapeEllipsoidFactoryTest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#ifndef MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORYTEST_H_ | ||
#define MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORYTEST_H_ | ||
|
||
#ifdef _WIN32 | ||
#pragma warning(disable : 4251) | ||
#endif | ||
|
||
#include <cxxtest/TestSuite.h> | ||
#include <gmock/gmock.h> | ||
#include <jsoncpp/json/json.h> | ||
#include <boost/assign/list_of.hpp> | ||
|
||
#include "MantidDataObjects/PeakShapeEllipsoid.h" | ||
#include "MantidKernel/VMD.h" | ||
#include "MantidKernel/cow_ptr.h" | ||
#include "MantidAPI/SpecialCoordinateSystem.h" | ||
#include "MockObjects.h" | ||
|
||
#include "MantidDataObjects/PeakShapeEllipsoidFactory.h" | ||
|
||
using namespace Mantid; | ||
using namespace Mantid::DataObjects; | ||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
using namespace boost::assign; | ||
|
||
class PeakShapeEllipsoidFactoryTest : public CxxTest::TestSuite { | ||
public: | ||
// This pair of boilerplate methods prevent the suite being created statically | ||
// This means the constructor isn't called when running other tests | ||
static PeakShapeEllipsoidFactoryTest *createSuite() { | ||
return new PeakShapeEllipsoidFactoryTest(); | ||
} | ||
static void destroySuite(PeakShapeEllipsoidFactoryTest *suite) { | ||
delete suite; | ||
} | ||
|
||
void test_invalid_json_with_no_successor() { | ||
PeakShapeEllipsoidFactory factory; | ||
TS_ASSERT_THROWS(factory.create(""), std::invalid_argument &); | ||
} | ||
|
||
void test_use_successor_when_different_shape_found() { | ||
using namespace testing; | ||
|
||
// We expect it to try to use the deletate factory. If it cannot process the | ||
// json. | ||
MockPeakShapeFactory *delegate = new MockPeakShapeFactory; | ||
EXPECT_CALL(*delegate, create(_)).Times(1); | ||
|
||
PeakShapeEllipsoidFactory factory; | ||
factory.setSuccessor(PeakShapeFactory_const_sptr(delegate)); | ||
|
||
// Minimal valid JSON for describing the shape. | ||
Json::Value root; | ||
root["shape"] = "square"; | ||
Json::StyledWriter writer; | ||
const std::string str_json = writer.write(root); | ||
|
||
factory.create(str_json); | ||
|
||
TS_ASSERT(Mock::VerifyAndClearExpectations(delegate)); | ||
} | ||
|
||
void test_create() { | ||
|
||
auto directions = list_of(V3D(1, 0, 0))(V3D(0, 1, 0))(V3D(0, 0, 1)) | ||
.convert_to_container<std::vector<V3D>>(); | ||
const MantidVec abcRadii = list_of(2)(3)(4); | ||
const MantidVec abcInnerRadii = list_of(5)(6)(7); | ||
const MantidVec abcOuterRadii = list_of(8)(9)(10); | ||
const SpecialCoordinateSystem frame = HKL; | ||
const std::string algorithmName = "foo"; | ||
const int algorithmVersion = 3; | ||
|
||
// Make a source shape | ||
PeakShapeEllipsoid sourceShape(directions, abcRadii, abcInnerRadii, | ||
abcOuterRadii, frame, algorithmName, | ||
algorithmVersion); | ||
|
||
PeakShapeEllipsoidFactory factory; | ||
PeakShape *productShape = factory.create(sourceShape.toJSON()); | ||
|
||
PeakShapeEllipsoid *ellipsoidShapeProduct = | ||
dynamic_cast<PeakShapeEllipsoid *>(productShape); | ||
TS_ASSERT(ellipsoidShapeProduct); | ||
|
||
TS_ASSERT_EQUALS(sourceShape, *ellipsoidShapeProduct); | ||
delete productShape; | ||
} | ||
}; | ||
|
||
#endif /* MANTID_DATAOBJECTS_PEAKSHAPEELLIPSOIDFACTORYTEST_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters