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 for (size_t d = 0; d < outD; d++) {
72 if (m_dimensionToBinFrom[d] >= inD) {
73 throw std::runtime_error("CoordTransformAligned::ctor(): invalid entry in "
74 "dimensionToBinFrom[" +
75 std::to_string(d) + "]. Cannot build the coordinate transformation.");
76 }
77 }
78}
79
80//----------------------------------------------------------------------------------------------
85}
86
87//----------------------------------------------------------------------------------------------
93void CoordTransformAligned::apply(const coord_t *inputVector, coord_t *outVector) const {
94 // For each output dimension
95 for (size_t out = 0; out < outD; ++out) {
96 // Get the coordinate at the dimension in the INPUT workspace corresponding
97 // to this OUTPUT
98 coord_t x = inputVector[m_dimensionToBinFrom[out]];
99 // Convert by taking the origin out then scaling.
100 outVector[out] = (x - m_origin[out]) * m_scaling[out];
101 }
102}
103
104//----------------------------------------------------------------------------------------------
113 // Zeroed-out affine matrix
114 Matrix<coord_t> mat(outD + 1, inD + 1);
115 // Bottom-right corner of the matrix is always 1.
116 mat[outD][inD] = 1.0;
117 // For each output dimension
118 for (size_t out = 0; out < outD; ++out) {
119 // The ROW is the out dimension.
120 size_t row = out;
121 // The COLUMN is the input dimension
122 size_t col = m_dimensionToBinFrom[out];
123 // So place the scaling factor at that spot
124 mat[row][col] = m_scaling[out];
125 // And place the origin (translation amount) at the last column
126 mat[row][inD] = -m_origin[out] * m_scaling[out];
127 }
128 return mat;
129}
130
131//----------------------------------------------------------------------------------------------
137 using namespace Poco::XML;
138
139 AutoPtr<Document> pDoc = new Document;
140 AutoPtr<Element> coordTransformElement = pDoc->createElement("CoordTransform");
141 pDoc->appendChild(coordTransformElement);
142
143 AutoPtr<Element> coordTransformTypeElement = pDoc->createElement("Type");
144 coordTransformTypeElement->appendChild(pDoc->createTextNode("CoordTransformAffine"));
145 coordTransformElement->appendChild(coordTransformTypeElement);
146
147 AutoPtr<Element> paramListElement = pDoc->createElement("ParameterList");
148
149 AutoPtr<Text> formatText = pDoc->createTextNode("%s%s%s");
150 paramListElement->appendChild(formatText);
151
152 coordTransformElement->appendChild(paramListElement);
153
154 std::stringstream xmlstream;
155
156 DOMWriter writer;
157 writer.writeNode(xmlstream, pDoc);
158
159 // Convert the members to parameters
160 InDimParameter inD_param(inD);
161 OutDimParameter outD_param(outD);
162
163 std::string formattedXMLString = boost::str(boost::format(xmlstream.str().c_str()) % inD_param.toXMLString().c_str() %
164 outD_param.toXMLString().c_str());
165 return formattedXMLString;
166}
167
172std::string CoordTransformAligned::id() const { return "CoordTransformAligned"; }
173
174} // 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)