Mantid
Loading...
Searching...
No Matches
CoordTransformAligned.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9
10using namespace Mantid::Kernel;
11using namespace Mantid::API;
12
13namespace Mantid::DataObjects {
14
15//----------------------------------------------------------------------------------------------
30CoordTransformAligned::CoordTransformAligned(const size_t inD, const size_t outD, const size_t *dimensionToBinFrom,
31 const coord_t *origin, const coord_t *scaling)
32 : CoordTransform(inD, outD), m_dimensionToBinFrom(outD), m_origin(outD), m_scaling(outD) {
33 if (!origin || !scaling || !dimensionToBinFrom)
34 throw std::runtime_error("CoordTransformAligned::ctor(): at least one of "
35 "the input arrays is a NULL pointer.");
36 for (size_t d = 0; d < outD; d++) {
37 m_dimensionToBinFrom[d] = dimensionToBinFrom[d];
38 if (m_dimensionToBinFrom[d] >= inD) {
39 throw std::runtime_error("CoordTransformAligned::ctor(): invalid entry in "
40 "dimensionToBinFrom[" +
41 std::to_string(d) + "]. Cannot build the coordinate transformation.");
42 }
43 m_origin[d] = origin[d];
44 m_scaling[d] = scaling[d];
45 }
46}
47
48//----------------------------------------------------------------------------------------------
63CoordTransformAligned::CoordTransformAligned(const size_t inD, const size_t outD,
64 std::vector<size_t> dimensionToBinFrom, std::vector<coord_t> origin,
65 std::vector<coord_t> scaling)
66 : CoordTransform(inD, outD), m_dimensionToBinFrom(std::move(dimensionToBinFrom)), m_origin(std::move(origin)),
67 m_scaling(std::move(scaling)) {
68 if (m_dimensionToBinFrom.size() != outD || m_origin.size() != outD || m_scaling.size() != outD)
69 throw std::runtime_error("CoordTransformAligned::ctor(): at least one of "
70 "the input vectors is the wrong size.");
71 const auto it = std::find_if(m_dimensionToBinFrom.cbegin(), m_dimensionToBinFrom.cbegin() + outD,
72 [inD](const size_t i) { return i >= inD; });
73 if (it != m_dimensionToBinFrom.cbegin() + outD) {
74 const size_t d = std::distance(m_dimensionToBinFrom.cbegin(), it);
75 throw std::runtime_error("CoordTransformAligned::ctor(): invalid entry in "
76 "dimensionToBinFrom[" +
77 std::to_string(d) + "]. Cannot build the coordinate transformation.");
78 }
79}
80
81//----------------------------------------------------------------------------------------------
87
88//----------------------------------------------------------------------------------------------
94void CoordTransformAligned::apply(const coord_t *inputVector, coord_t *outVector) const {
95 // For each output dimension
96 for (size_t out = 0; out < outD; ++out) {
97 // Get the coordinate at the dimension in the INPUT workspace corresponding
98 // to this OUTPUT
99 coord_t x = inputVector[m_dimensionToBinFrom[out]];
100 // Convert by taking the origin out then scaling.
101 outVector[out] = (x - m_origin[out]) * m_scaling[out];
102 }
103}
104
105//----------------------------------------------------------------------------------------------
114 // Zeroed-out affine matrix
115 Matrix<coord_t> mat(outD + 1, inD + 1);
116 // Bottom-right corner of the matrix is always 1.
117 mat[outD][inD] = 1.0;
118 // For each output dimension
119 for (size_t out = 0; out < outD; ++out) {
120 // The ROW is the out dimension.
121 size_t row = out;
122 // The COLUMN is the input dimension
123 size_t col = m_dimensionToBinFrom[out];
124 // So place the scaling factor at that spot
125 mat[row][col] = m_scaling[out];
126 // And place the origin (translation amount) at the last column
127 mat[row][inD] = -m_origin[out] * m_scaling[out];
128 }
129 return mat;
130}
131
132//----------------------------------------------------------------------------------------------
138 using namespace Poco::XML;
139
140 AutoPtr<Document> pDoc = new Document;
141 AutoPtr<Element> coordTransformElement = pDoc->createElement("CoordTransform");
142 pDoc->appendChild(coordTransformElement);
143
144 AutoPtr<Element> coordTransformTypeElement = pDoc->createElement("Type");
145 coordTransformTypeElement->appendChild(pDoc->createTextNode("CoordTransformAffine"));
146 coordTransformElement->appendChild(coordTransformTypeElement);
147
148 AutoPtr<Element> paramListElement = pDoc->createElement("ParameterList");
149
150 AutoPtr<Text> formatText = pDoc->createTextNode("%s%s%s");
151 paramListElement->appendChild(formatText);
152
153 coordTransformElement->appendChild(paramListElement);
154
155 std::stringstream xmlstream;
156
157 DOMWriter writer;
158 writer.writeNode(xmlstream, pDoc);
159
160 // Convert the members to parameters
161 InDimParameter inD_param(inD);
162 OutDimParameter outD_param(outD);
163
164 std::string formattedXMLString = boost::str(boost::format(xmlstream.str().c_str()) % inD_param.toXMLString().c_str() %
165 outD_param.toXMLString().c_str());
166 return formattedXMLString;
167}
168
173std::string CoordTransformAligned::id() const { return "CoordTransformAligned"; }
174
175} // namespace Mantid::DataObjects
Unique SingleValueParameter Declaration for InputNDimensions.
size_t inD
Input number of dimensions.
size_t outD
Output number of dimensions.
Unique type declaration for which dimensions are used in the input workspace.
std::string toXMLString() const override
Serialize the coordinate transform.
std::vector< coord_t > m_scaling
Scaling from the input to the output dimension, sized [outD].
std::string id() const override
Coordinate transform id.
std::vector< size_t > m_dimensionToBinFrom
For each dimension in the output, index in the input workspace of which dimension it is.
Mantid::Kernel::Matrix< coord_t > makeAffineMatrix() const override
Create an equivalent affine transformation matrix out of the parameters of this axis-aligned transfor...
std::vector< coord_t > m_origin
Offset (minimum) position in each of the output dimensions, sized [outD].
CoordTransformAligned(const size_t inD, const size_t outD, const size_t *dimensionToBinFrom, const coord_t *origin, const coord_t *scaling)
Constructor.
void apply(const coord_t *inputVector, coord_t *outVector) const override
Apply the coordinate transformation.
CoordTransform * clone() const override
Virtual cloner.
Numerical Matrix class.
Definition Matrix.h:42
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition MDTypes.h:27
STL namespace.
std::string to_string(const wide_integer< Bits, Signed > &n)