Skip to content

Commit

Permalink
refs #10878. Factory for spherical peaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Jan 12, 2015
1 parent d465449 commit 75043a3
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Code/Mantid/Framework/DataObjects/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@ set ( SRC_FILES
src/Peak.cpp
src/PeakColumn.cpp
src/PeakShapeSpherical.cpp
src/PeakShapeSphericalFactory.cpp
src/PeaksWorkspace.cpp
src/RebinnedOutput.cpp
src/SpecialWorkspace2D.cpp
Expand Down Expand Up @@ -44,7 +45,9 @@ set ( INC_FILES
inc/MantidDataObjects/Peak.h
inc/MantidDataObjects/PeakColumn.h
inc/MantidDataObjects/PeakShape.h
inc/MantidDataObjects/PeakShapeFactory.h
inc/MantidDataObjects/PeakShapeSpherical.h
inc/MantidDataObjects/PeakShapeSphericalFactory.h
inc/MantidDataObjects/PeaksWorkspace.h
inc/MantidDataObjects/RebinnedOutput.h
inc/MantidDataObjects/SpecialWorkspace2D.h
Expand All @@ -68,6 +71,7 @@ set ( TEST_FILES
MementoTableWorkspaceTest.h
OffsetsWorkspaceTest.h
PeakColumnTest.h
PeakShapeSphericalFactoryTest.h
PeakShapeSphericalTest.h
PeakTest.h
PeaksWorkspaceTest.h
Expand Down
@@ -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 &copy; 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_ */
Expand Up @@ -55,7 +55,8 @@ namespace DataObjects
virtual int algorithmVersion() const;
/// Clone the peak shape
virtual PeakShapeSpherical* clone() const;

/// Equals operator
bool operator==(const PeakShapeSpherical& other) const;
/// Peak centre
Mantid::Kernel::VMD centre() const;
/// Peak radius
Expand Down
@@ -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 &copy; 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_ */
7 changes: 7 additions & 0 deletions Code/Mantid/Framework/DataObjects/src/PeakShapeSpherical.cpp
Expand Up @@ -110,6 +110,13 @@ PeakShapeSpherical* PeakShapeSpherical::clone() const
return new PeakShapeSpherical(*this);
}

bool PeakShapeSpherical::operator==(const PeakShapeSpherical &other) const
{
return other.centre() == this->centre() &&
other.radius() == this->radius() &&
other.frame() == this->frame();
}

/**
* @brief Get radius of sphere
* @return radius
Expand Down
@@ -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
@@ -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_ */
10 changes: 10 additions & 0 deletions Code/Mantid/Framework/DataObjects/test/PeakShapeSphericalTest.h
Expand Up @@ -122,7 +122,17 @@ class PeakShapeSphericalTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(algorithmVersion, output["algorithm_version"].asInt());
TS_ASSERT_EQUALS(frame, output["frame"].asInt());
TS_ASSERT_EQUALS(radius, output["radius"].asDouble());
}

void test_equals()
{
TS_ASSERT_EQUALS(PeakShapeSpherical(V3D(0,0,0), 1.0, QSample), PeakShapeSpherical(V3D(0,0,0), 1.0, QSample));

TSM_ASSERT_DIFFERS("Different centre", PeakShapeSpherical(V3D(0,0,0), 1.0, QSample), PeakShapeSpherical(V3D(1,0,0), 1.0, QSample));

TSM_ASSERT_DIFFERS("Different radius", PeakShapeSpherical(V3D(0,0,0), 1.0, QSample), PeakShapeSpherical(V3D(0,0,0), 2.0, QSample));

TSM_ASSERT_DIFFERS("Different frame", PeakShapeSpherical(V3D(0,0,0), 1.0, QSample), PeakShapeSpherical(V3D(0,0,0), 1.0, QLab));
}


Expand Down

0 comments on commit 75043a3

Please sign in to comment.