Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #10878. Factory for spherical peaks.
- Loading branch information
1 parent
d465449
commit 75043a3
Showing
8 changed files
with
261 additions
and
1 deletion.
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
57 changes: 57 additions & 0 deletions
57
Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/PeakShapeFactory.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,57 @@ | ||
#ifndef MANTID_DATAOBJECTS_PEAKSHAPEFACTORY_H_ | ||
#define MANTID_DATAOBJECTS_PEAKSHAPEFACTORY_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include <boost/shared_ptr.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataObjects | ||
{ | ||
|
||
// Forward declaration | ||
class PeakShape; | ||
|
||
/** PeakShapeFactory : Factory for creating peak shapes | ||
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 PeakShapeFactory | ||
{ | ||
public: | ||
/// Destructor | ||
virtual ~PeakShapeFactory(){}; | ||
/// Make the product | ||
virtual PeakShape* create(const std::string& source) const = 0; | ||
/// Set the successor factory. create will be called on that if this instance is not suitable. | ||
virtual void setSuccessor(boost::shared_ptr<const PeakShapeFactory> successorFactory) = 0; | ||
}; | ||
|
||
/// Helper typedef | ||
typedef boost::shared_ptr<PeakShapeFactory> PeakShapeFactory_sptr; | ||
/// Helper typedef | ||
typedef boost::shared_ptr<const PeakShapeFactory> PeakShapeFactory_const_sptr; | ||
|
||
|
||
} // namespace DataObjects | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAOBJECTS_PEAKSHAPEFACTORY_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
59 changes: 59 additions & 0 deletions
59
Code/Mantid/Framework/DataObjects/inc/MantidDataObjects/PeakShapeSphericalFactory.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,59 @@ | ||
#ifndef MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORY_H_ | ||
#define MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORY_H_ | ||
|
||
#include "MantidKernel/System.h" | ||
#include "MantidDataObjects/PeakShapeFactory.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace DataObjects | ||
{ | ||
|
||
// Forward declare | ||
class PeakShape; | ||
|
||
/** PeakShapeSphericalFactory : Factory for spherical peak shapes for de-serializing from JSON. | ||
* | ||
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 PeakShapeSphericalFactory : public PeakShapeFactory | ||
{ | ||
public: | ||
/// Constructor | ||
PeakShapeSphericalFactory(); | ||
/// Destructor | ||
virtual ~PeakShapeSphericalFactory(); | ||
/// Make product | ||
PeakShape* create(const std::string &source) const; | ||
|
||
private: | ||
/// Set a successor should this factory be unsuitable | ||
void setSuccessor(PeakShapeFactory_const_sptr successorFactory); | ||
/// Successor factory | ||
PeakShapeFactory_const_sptr m_successor; | ||
|
||
}; | ||
|
||
|
||
} // namespace DataObjects | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORY_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
72 changes: 72 additions & 0 deletions
72
Code/Mantid/Framework/DataObjects/src/PeakShapeSphericalFactory.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,72 @@ | ||
#include "MantidDataObjects/PeakShapeSphericalFactory.h" | ||
#include "MantidDataObjects/PeakShapeSpherical.h" | ||
#include "MantidAPI/SpecialCoordinateSystem.h" | ||
#include <jsoncpp/json/json.h> | ||
#include <MantidKernel/VMD.h> | ||
|
||
namespace Mantid { | ||
namespace DataObjects { | ||
|
||
using namespace Mantid::API; | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Constructor | ||
*/ | ||
PeakShapeSphericalFactory::PeakShapeSphericalFactory() {} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** Destructor | ||
*/ | ||
PeakShapeSphericalFactory::~PeakShapeSphericalFactory() {} | ||
|
||
/** | ||
* @brief PeakShapeSphericalFactory::create : Creational method | ||
* @param source : Source JSON | ||
* @return PeakShape object | ||
*/ | ||
PeakShape* PeakShapeSphericalFactory::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 == "spherical") { | ||
|
||
const std::string algorithmName( root["algorithm_name"].asString() ); | ||
const int algorithmVersion( root["algorithm_version"].asInt() ); | ||
const SpecialCoordinateSystem frame( static_cast<SpecialCoordinateSystem> ( root["frame"].asInt() ) ); | ||
const double radius( root["radius"].asDouble() ); | ||
Json::Value centre( root["centre"] ); | ||
Kernel::VMD centre_nd( centre.size() ); | ||
for (Json::ArrayIndex i = 0; i < centre.size(); ++i) { | ||
centre_nd[i] = centre[i].asFloat(); | ||
} | ||
product = new PeakShapeSpherical(centre_nd, radius, 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 { | ||
if (m_successor) { | ||
product = m_successor->create(source); | ||
} else { | ||
throw std::invalid_argument( | ||
"PeakShapeSphericalFactory:: Source JSON for the peak shape is not valid: " + source); | ||
} | ||
} | ||
return product; | ||
} | ||
|
||
void PeakShapeSphericalFactory::setSuccessor( | ||
PeakShapeFactory_const_sptr successorFactory) { | ||
m_successor = successorFactory; | ||
} | ||
|
||
} // namespace DataObjects | ||
} // namespace Mantid |
50 changes: 50 additions & 0 deletions
50
Code/Mantid/Framework/DataObjects/test/PeakShapeSphericalFactoryTest.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,50 @@ | ||
#ifndef MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORYTEST_H_ | ||
#define MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORYTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
|
||
#include "MantidDataObjects/PeakShapeSphericalFactory.h" | ||
#include "MantidDataObjects/PeakShapeSpherical.h" | ||
#include "MantidKernel/VMD.h" | ||
#include "MantidAPI/SpecialCoordinateSystem.h" | ||
|
||
using namespace Mantid::DataObjects; | ||
using namespace Mantid::Kernel; | ||
using namespace Mantid::API; | ||
|
||
class PeakShapeSphericalFactoryTest : 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 PeakShapeSphericalFactoryTest *createSuite() { return new PeakShapeSphericalFactoryTest(); } | ||
static void destroySuite( PeakShapeSphericalFactoryTest *suite ) { delete suite; } | ||
|
||
|
||
// More tests needed TODO | ||
|
||
void test_create() | ||
{ | ||
const V3D centre(1,1,1); | ||
const double radius = 2; | ||
const SpecialCoordinateSystem frame = HKL; | ||
const std::string algorithmName = "foo"; | ||
const int algorithmVersion = 3; | ||
|
||
// Make a source shape | ||
PeakShapeSpherical sourceShape(centre, radius, frame, algorithmName, algorithmVersion); | ||
|
||
PeakShapeSphericalFactory factory; | ||
PeakShape* productShape = factory.create(sourceShape.toJSON()); | ||
|
||
PeakShapeSpherical* sphericalShapeProduct = dynamic_cast<PeakShapeSpherical*>(productShape); | ||
TS_ASSERT(sphericalShapeProduct); | ||
|
||
TS_ASSERT_EQUALS(sourceShape, *sphericalShapeProduct); | ||
} | ||
|
||
|
||
}; | ||
|
||
|
||
#endif /* MANTID_DATAOBJECTS_PEAKSHAPESPHERICALFACTORYTEST_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