Mantid
Loading...
Searching...
No Matches
AffineMatrixParameterParser.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 +
8#include <boost/algorithm/string.hpp>
9
10namespace Mantid::DataObjects {
11
13
14//----------------------------------------------------------------------------------------------
15
17 std::string typeName = parameterElement->getChildElement("Type")->innerText();
18 if (AffineMatrixParameter::parameterName() != typeName) {
19 throw std::runtime_error(std::string("AffineMatrixParameterParser cannot parse parameter of type: " + typeName));
20 } else {
21 // Convenience typedefs
22 using VecStrings = std::vector<std::string>;
23 using VecDoubles = std::vector<coord_t>;
24
25 std::string sParameterValue = parameterElement->getChildElement("Value")->innerText();
26
27 VecStrings vecStrRows;
28 VecStrings vecStrCols;
29
30 boost::split(vecStrRows, sParameterValue, boost::is_any_of(";"));
31 size_t nRows = vecStrRows.size();
32
33 auto row_it = vecStrRows.begin();
34 VecStrings::iterator col_it;
35
36 size_t nCols = 0;
37 VecDoubles elements;
38
39 // Process each row and column and extract each element as a double.
40 while (row_it != vecStrRows.end()) {
41 boost::split(vecStrCols, *row_it, boost::is_any_of(","));
42 nCols = vecStrCols.size();
43 col_it = vecStrCols.begin();
44 while (col_it != vecStrCols.end()) {
45 coord_t val = static_cast<coord_t>(std::stof(col_it->c_str()));
46 elements.emplace_back(val);
47 ++col_it;
48 }
49 ++row_it;
50 }
51
52 // Create the Matrix.
53 AffineMatrixType m(nRows, nCols);
54 size_t count = 0;
55 for (size_t i = 0; i < nRows; i++) {
56 for (size_t j = 0; j < nCols; j++) {
57 m[i][j] = elements[count];
58 count++;
59 }
60 }
61
62 // Create the parameter and set the matrix.
63 auto parameter = new AffineMatrixParameter(nRows - 1, nCols - 1);
64 parameter->setMatrix(m);
65
66 // Now return the fully constructed parameter.
67 return parameter;
68 }
69}
70
71//----------------------------------------------------------------------------------------------
72
74 throw std::runtime_error("Cannot set a successor parser on a AffineMatrixParameterParser");
75}
76} // namespace Mantid::DataObjects
int count
counter
Definition: Matrix.cpp:37
AffineMatrixParameter * createParameter(Poco::XML::Element *parameterElement) override
Creates the parameter by reading the xml given.
void setSuccessorParser(ImplicitFunctionParameterParser *) override
Set a successor parser for chain-of-responsibility type reading.
Type to wrap an affine matrix and allow serialization via xml.
static std::string parameterName()
Gets the type parameter name.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27