Skip to content

Commit

Permalink
Refs #11830. Added stream operators for SymmetryOperation
Browse files Browse the repository at this point in the history
Python can now also handle lists of SymmetryOperations.
  • Loading branch information
Michael Wedel committed May 22, 2015
1 parent ed7805d commit f2dda9d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
Expand Up @@ -170,6 +170,11 @@ class MANTID_GEOMETRY_DLL SymmetryOperation {
std::string m_identifier;
};

MANTID_GEOMETRY_DLL std::ostream &operator<<(
std::ostream &stream, const SymmetryOperation &operation);
MANTID_GEOMETRY_DLL std::istream &operator>>(std::istream &stream,
SymmetryOperation &operation);

MANTID_GEOMETRY_DLL V3R getWrappedVector(const V3R &vector);
MANTID_GEOMETRY_DLL Kernel::V3D getWrappedVector(const Kernel::V3D &vector);

Expand Down
26 changes: 26 additions & 0 deletions Code/Mantid/Framework/Geometry/src/Crystal/SymmetryOperation.cpp
Expand Up @@ -271,5 +271,31 @@ Kernel::V3D getWrappedVector(const Kernel::V3D &vector) {
return wrappedVector;
}

/// Stream output operator, writes the identifier to stream.
std::ostream &operator<<(std::ostream &stream,
const SymmetryOperation &operation) {
stream << "[" + operation.identifier() + "]";

return stream;
}

/// Reads identifier from stream and tries to parse as a symbol.
std::istream &operator>>(std::istream &stream, SymmetryOperation &operation) {
std::string identifier;
std::getline(stream, identifier);

size_t i = identifier.find_first_of('[');
size_t j = identifier.find_last_of(']');

if (i == std::string::npos || j == std::string::npos) {
throw std::runtime_error(
"Cannot construct SymmetryOperation from identifier: " + identifier);
}

operation = SymmetryOperation(identifier.substr(i + 1, j - i - 1));

return stream;
}

} // namespace Geometry
} // namespace Mantid
33 changes: 33 additions & 0 deletions Code/Mantid/Framework/Geometry/test/SymmetryOperationTest.h
Expand Up @@ -6,6 +6,7 @@
#include "MantidGeometry/Crystal/SymmetryOperation.h"
#include "MantidGeometry/Crystal/SymmetryOperationSymbolParser.h"

#include "MantidKernel/Exception.h"
#include "MantidKernel/V3D.h"

#include <boost/make_shared.hpp>
Expand Down Expand Up @@ -276,6 +277,38 @@ class SymmetryOperationTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(fourFoldZ^4, identity);
}

void testStreamOperatorOut()
{
SymmetryOperation mirror("x,-y,z");

std::stringstream stream;
stream << mirror;

TS_ASSERT_EQUALS(stream.str(), "[x,-y,z]");
}

void testStreamOperatorIn()
{
std::stringstream stream;
stream << "[x,-y,z]";

SymmetryOperation mirror;
TS_ASSERT_DIFFERS(mirror.identifier(), "x,-y,z");

TS_ASSERT_THROWS_NOTHING(stream >> mirror);
TS_ASSERT_EQUALS(mirror.identifier(), "x,-y,z");

// no []
std::stringstream invalidBrackets;
invalidBrackets << "x,-y,z";
TS_ASSERT_THROWS(invalidBrackets >> mirror, std::runtime_error);

// invalid string
std::stringstream invalid;
invalid << "[someString]";
TS_ASSERT_THROWS(invalid >> mirror, Exception::ParseError);
}

private:
V3D applyOrderTimes(const SymmetryOperation &symOp, const V3D &vector)
{
Expand Down
Expand Up @@ -22,17 +22,6 @@ namespace {

return pythonSymOps;
}

boost::python::list getSymmetryOperations(Group &self) {
const std::vector<SymmetryOperation> &symOps = self.getSymmetryOperations();

boost::python::list pythonSymOps;
for (auto it = symOps.begin(); it != symOps.end(); ++it) {
pythonSymOps.append(*it);
}

return pythonSymOps;
}
}

// clang-format off
Expand All @@ -43,9 +32,11 @@ void export_Group()
.value("Orthogonal", Group::Orthogonal)
.value("Hexagonal", Group::Hexagonal);

class_<Group, boost::noncopyable>("Group", no_init)
class_<Group>("Group")
.def(init<std::string>("Construct a group from the provided initializer string."))
.def(init<std::vector<SymmetryOperation> >("Construct a group from the provided symmetry operation list."))
.def("getOrder", &Group::order, "Returns the order of the group.")
.def("getCoordinateSystem", &Group::getCoordinateSystem, "Returns the type of coordinate system to distinguish groups with hexagonal system definition.")
.def("getSymmetryOperations", &getSymmetryOperations, "Returns the symmetry operations contained in the group.")
.def("getSymmetryOperations", &Group::getSymmetryOperations, "Returns the symmetry operations contained in the group.")
.def("getSymmetryOperationStrings", &getSymmetryOperationStrings, "Returns the x,y,z-strings for the contained symmetry operations.");
}
Expand Up @@ -2,6 +2,7 @@
#include "MantidGeometry/Crystal/SymmetryOperation.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToV3D.h"
#include "MantidPythonInterface/kernel/Converters/PyObjectToMatrix.h"
#include "MantidPythonInterface/kernel/StlExportDefinitions.h"

#include <boost/python/class.hpp>
#include <boost/python/enum.hpp>
Expand All @@ -10,6 +11,7 @@
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::SymmetryOperation;
using Mantid::PythonInterface::std_vector_exporter;

using namespace boost::python;

Expand Down Expand Up @@ -40,5 +42,7 @@ void export_SymmetryOperation()
.def("transformCoordinates", &applyToCoordinates, "Returns transformed coordinates. For transforming HKLs, use transformHKL.")
.def("transformHKL", &applyToVector, "Returns transformed HKLs. For transformation of coordinates use transformCoordinates.")
.def("apply", &applyToVector, "An alias for transformHKL.");

std_vector_exporter<Mantid::Geometry::SymmetryOperation>::wrap("std_vector_symmetryoperation");
}

0 comments on commit f2dda9d

Please sign in to comment.