Skip to content

Commit

Permalink
Refs #11020 Add unit to Projection
Browse files Browse the repository at this point in the history
In Horace's python interface this is known as type
  • Loading branch information
Harry Jeffery committed Feb 6, 2015
1 parent b393079 commit 2ab9a6a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 20 deletions.
Expand Up @@ -36,6 +36,12 @@ namespace Geometry {
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/

/// The units for a given dimension
enum ProjectionUnit {
RLU, // r.l.u
INV_ANG // inverse angstroms
};

class DLLExport Projection {
public:
/// Default constructor builds with two dimensions
Expand All @@ -62,14 +68,18 @@ class DLLExport Projection {
float getOffset(size_t nd);
/// Retrieves the axis vector for the given dimension
VMD getAxis(size_t nd);
/// Retrives the unit of the given dimension
ProjectionUnit getUnit(size_t nd);
/// Set the offset for a given dimension
void setOffset(size_t nd, float offset);
/// Set the axis vector for a given dimension
void setAxis(size_t nd, VMD axis);

/// Set the unit for a given dimension
void setUnit(size_t nd, ProjectionUnit unit);
/// Retrives the number of dimensions
size_t getNumDims() const { return m_nd; }

// We're guaranteed to have at least 2 axis
// We're guaranteed to have at least 2 axes
VMD &U() { return m_dimensions[0]; }
VMD &V() { return m_dimensions[1]; }
VMD &W() {
Expand All @@ -86,6 +96,8 @@ class DLLExport Projection {
VMD *m_dimensions;
/// A vector of the offsets for each dimension
float *m_offsets;
/// A vector of the units for each dimension
ProjectionUnit *m_units;
};

} // namespace Geometry
Expand Down
72 changes: 55 additions & 17 deletions Code/Mantid/Framework/Geometry/src/Crystal/Projection.cpp
@@ -1,19 +1,21 @@
#include "MantidGeometry/Crystal/Projection.h"

namespace Mantid
{
namespace Geometry
{
namespace Mantid {
namespace Geometry {

Projection::Projection()
: m_nd(2), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(2), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = VMD(m_nd);
m_dimensions[1] = VMD(m_nd);
m_offsets[0] = 0.0;
m_offsets[1] = 0.0;
m_units[0] = RLU;
m_units[1] = RLU;
}

Projection::Projection(size_t nd) : m_nd(nd) {
Projection::Projection(size_t nd)
: m_nd(nd), m_units(new ProjectionUnit[m_nd]) {
if (m_nd <= 1)
throw std::invalid_argument("nd must be > 1");

Expand All @@ -23,67 +25,85 @@ Projection::Projection(size_t nd) : m_nd(nd) {
for (size_t i = 0; i < m_nd; ++i) {
m_dimensions[i] = VMD(m_nd);
m_offsets[i] = 0.0;
m_units[i] = RLU;
}
}

Projection::Projection(VMD u, VMD v)
: m_nd(2), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(2), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = u;
m_dimensions[1] = v;
m_offsets[0] = 0.0;
m_offsets[1] = 0.0;
m_units[0] = RLU;
m_units[1] = RLU;
}

Projection::Projection(VMD u, VMD v, VMD w)
: m_nd(3), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(3), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = u;
m_dimensions[1] = v;
m_dimensions[2] = w;
for (size_t i = 0; i < m_nd; ++i)
for (size_t i = 0; i < m_nd; ++i) {
m_offsets[i] = 0.0;
m_units[i] = RLU;
}
}

Projection::Projection(VMD u, VMD v, VMD w, VMD x)
: m_nd(4), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(4), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = u;
m_dimensions[1] = v;
m_dimensions[2] = w;
m_dimensions[3] = x;
for (size_t i = 0; i < m_nd; ++i)
for (size_t i = 0; i < m_nd; ++i) {
m_offsets[i] = 0.0;
m_units[i] = RLU;
}
}

Projection::Projection(VMD u, VMD v, VMD w, VMD x, VMD y)
: m_nd(5), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(5), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = u;
m_dimensions[1] = v;
m_dimensions[2] = w;
m_dimensions[3] = x;
m_dimensions[4] = y;
for (size_t i = 0; i < m_nd; ++i)
for (size_t i = 0; i < m_nd; ++i) {
m_offsets[i] = 0.0;
m_units[i] = RLU;
}
}

Projection::Projection(VMD u, VMD v, VMD w, VMD x, VMD y, VMD z)
: m_nd(6), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]) {
: m_nd(6), m_dimensions(new VMD[m_nd]), m_offsets(new float[m_nd]),
m_units(new ProjectionUnit[m_nd]) {
m_dimensions[0] = u;
m_dimensions[1] = v;
m_dimensions[2] = w;
m_dimensions[3] = x;
m_dimensions[4] = y;
m_dimensions[5] = z;
for (size_t i = 0; i < m_nd; ++i)
for (size_t i = 0; i < m_nd; ++i) {
m_offsets[i] = 0.0;
m_units[i] = RLU;
}
}

Projection::Projection(const Projection &other) : m_nd(other.m_nd) {
if (m_nd <= 0)
throw std::invalid_argument("nd must be > 0");
m_dimensions = new VMD[m_nd];
m_offsets = new float[m_nd];
m_units = new ProjectionUnit[m_nd];
for (size_t i = 0; i < m_nd; ++i) {
m_dimensions[i] = other.m_dimensions[i];
m_offsets[i] = other.m_offsets[i];
m_units[i] = other.m_units[i];
}
}

Expand All @@ -92,19 +112,23 @@ Projection &Projection::operator=(const Projection &other) {
m_nd = other.m_nd;
delete[] m_dimensions;
delete[] m_offsets;
delete[] m_units;
m_dimensions = new VMD[m_nd];
m_offsets = new float[m_nd];
m_units = new ProjectionUnit[m_nd];
}
for (size_t i = 0; i < m_nd; ++i) {
m_dimensions[i] = other.m_dimensions[i];
m_offsets[i] = other.m_offsets[i];
m_units[i] = other.m_units[i];
}
return *this;
}

Projection::~Projection() {
delete[] m_dimensions;
delete[] m_offsets;
delete[] m_units;
}

float Projection::getOffset(size_t nd) {
Expand All @@ -121,6 +145,13 @@ VMD Projection::getAxis(size_t nd) {
return m_dimensions[nd];
}

ProjectionUnit Projection::getUnit(size_t nd) {
if (nd >= m_nd)
throw std::invalid_argument("given axis out of range");
else
return m_units[nd];
}

void Projection::setOffset(size_t nd, float offset) {
if (nd >= m_nd)
throw std::invalid_argument("given axis out of range");
Expand All @@ -135,5 +166,12 @@ void Projection::setAxis(size_t nd, VMD axis) {
m_dimensions[nd] = axis;
}

} //Geometry
} //Mantid
void Projection::setUnit(size_t nd, ProjectionUnit unit) {
if (nd >= m_nd)
throw std::invalid_argument("given axis out of range");
else
m_units[nd] = unit;
}

} // Geometry
} // Mantid
22 changes: 21 additions & 1 deletion Code/Mantid/Framework/Geometry/test/ProjectionTest.h
Expand Up @@ -15,6 +15,7 @@ class ProjectionTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(p.getNumDims(), 2);
TS_ASSERT_EQUALS(p.getOffset(0), 0.0);
TS_ASSERT_EQUALS(p.getAxis(0).getNumDims(), 2);
TS_ASSERT_EQUALS(p.getUnit(0), RLU);
}

void test_ndim_constructor() {
Expand Down Expand Up @@ -49,8 +50,10 @@ class ProjectionTest : public CxxTest::TestSuite {
}

void test_throw_invalid_dimension_constructor() {
TS_ASSERT_THROWS_ANYTHING(Projection(0));
TS_ASSERT_THROWS_ANYTHING(Projection(-1));
TS_ASSERT_THROWS_ANYTHING(Projection(0));
TS_ASSERT_THROWS_ANYTHING(Projection(1));
TS_ASSERT_THROWS_NOTHING(Projection(2));
}

void test_throw_out_of_range_access() {
Expand All @@ -62,19 +65,27 @@ class ProjectionTest : public CxxTest::TestSuite {
TS_ASSERT_THROWS_ANYTHING(p.getAxis(-1));
TS_ASSERT_THROWS_NOTHING(p.getAxis(2));
TS_ASSERT_THROWS_ANYTHING(p.getAxis(3));

TS_ASSERT_THROWS_ANYTHING(p.getUnit(-1));
TS_ASSERT_THROWS_NOTHING(p.getUnit(2));
TS_ASSERT_THROWS_ANYTHING(p.getUnit(3));
}

void test_copy_constructor() {
VMD u(1, -1, 0);
VMD v(1, 1, 0);
VMD w(0, 0, 1);
Projection p(u, v, w);
p.setUnit(0, RLU);
p.setUnit(1, INV_ANG);

Projection q(p);

TS_ASSERT_EQUALS(q.getAxis(0), u);
TS_ASSERT_EQUALS(q.getAxis(1), v);
TS_ASSERT_EQUALS(q.getAxis(2), w);
TS_ASSERT_EQUALS(q.getUnit(0), RLU);
TS_ASSERT_EQUALS(q.getUnit(1), INV_ANG);
}

void test_assign() {
Expand Down Expand Up @@ -114,6 +125,15 @@ class ProjectionTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(p.getAxis(2), VMD(7,8,8));
}

void test_setUnit() {
Projection p(3);
p.setUnit(0, INV_ANG);
p.setUnit(1, RLU);
p.setUnit(2, INV_ANG);
TS_ASSERT_EQUALS(p.getUnit(0), INV_ANG);
TS_ASSERT_EQUALS(p.getUnit(1), RLU);
TS_ASSERT_EQUALS(p.getUnit(2), INV_ANG);
}
};

#endif /* MANTID_GEOMETRY_PROJECTIONTEST_H_ */
Expand Up @@ -18,8 +18,10 @@ void export_Projection()
.def("getNumDims", &Projection::getNumDims, "Returns the number of dimensions in the projection")
.def("getOffset", &Projection::getOffset, "Returns the offset for the given dimension", args("dimension"))
.def("getAxis", &Projection::getAxis, "Returns the axis for the given dimension", args("dimension"))
.def("getUnit", &Projection::getUnit, "Returns the unit for the given dimension", args("dimension"))
.def("setOffset", &Projection::setOffset, "Sets the offset for the given dimension", args("dimension", "offset"))
.def("setAxis", &Projection::setAxis, "Sets the axis for the given dimension", args("dimension", "axis"))
.def("setUnit", &Projection::setUnit, "Sets the unit for the given dimension", args("dimension", "unit"))
.def("U", &Projection::U, "Returns a reference to the U axis", return_internal_reference<>())
.def("V", &Projection::V, "Returns a reference to the V axis", return_internal_reference<>())
.def("W", &Projection::W, "Returns a reference to the W axis", return_internal_reference<>())
Expand Down

0 comments on commit 2ab9a6a

Please sign in to comment.