forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffineMatrixParameter.cpp
167 lines (151 loc) · 5.99 KB
/
AffineMatrixParameter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataObjects/AffineMatrixParameter.h"
namespace Mantid::DataObjects {
//----------------------------------------------------------------------------------------------
/** Constructor
*
* @param outD the number of output dimensions
* @param inD the nubmer of input dimensions
*/
AffineMatrixParameter::AffineMatrixParameter(size_t outD, size_t inD) : m_affineMatrix(outD + 1, inD + 1) {
m_isValid = false;
m_affineMatrix.identityMatrix();
size_t nx = m_affineMatrix.numRows();
size_t ny = m_affineMatrix.numCols();
// big chunk of memory holding the whole matrix
m_rawMem = new coord_t[nx * ny];
// array of pointers (one per column)
m_rawMatrix = new coord_t *[nx];
for (size_t i = 0; i < nx; i++)
m_rawMatrix[i] = m_rawMem + (i * ny);
// Copy into the raw matrix (for speed)
copyRawMatrix();
}
//----------------------------------------------------------------------------------------------
/// Destructor
AffineMatrixParameter::~AffineMatrixParameter() {
// delete array of pointers to rows
delete[] m_rawMatrix;
m_rawMatrix = nullptr;
// delete large mem block holding the matrix
delete[] m_rawMem;
m_rawMem = nullptr;
}
//----------------------------------------------------------------------------------------------
/// Copy elements from affinematrix into raw array.
void AffineMatrixParameter::copyRawMatrix() {
for (size_t x = 0; x < m_affineMatrix.numRows(); ++x)
for (size_t y = 0; y < m_affineMatrix.numCols(); ++y)
m_rawMatrix[x][y] = m_affineMatrix[x][y];
}
//----------------------------------------------------------------------------------------------
/** Gets copy of internal affine matrix.
*
* @return A copy of the underlying affine matrix.
*/
AffineMatrixType AffineMatrixParameter::getAffineMatrix() const { return m_affineMatrix; }
//----------------------------------------------------------------------------------------------
/** Get the matrix in its raw array form.
*
* @return the matrix as an array.
*/
coord_t **AffineMatrixParameter::getRawMatrix() { return m_rawMatrix; }
//----------------------------------------------------------------------------------------------
/** Get the name of the parameter
*
* @return Parameter name.
*/
std::string AffineMatrixParameter::getName() const { return AffineMatrixParameter::parameterName(); }
//----------------------------------------------------------------------------------------------
/** Serialize the Affine Matrix Parameter
*
* @return the object as serialized. Xml in a std::string.
*/
std::string AffineMatrixParameter::toXMLString() const {
std::vector<coord_t> elements = this->m_affineMatrix.getVector();
const size_t size = elements.size();
std::string parameterValue;
for (size_t i = 1; i <= size; i++) {
std::stringstream sstream;
sstream << elements[i - 1];
parameterValue.append(sstream.str());
sstream.clear();
if (i % m_affineMatrix.numCols() == 0) {
if (i != size) {
parameterValue.append(";");
}
} else {
parameterValue.append(",");
}
}
return parameterXMLTemplate(parameterValue);
}
//----------------------------------------------------------------------------------------------
/** Clone the parameter.
*
* @return Cloned parameter.
*/
AffineMatrixParameter *AffineMatrixParameter::clone() const {
return new AffineMatrixParameter(m_affineMatrix.numRows() - 1, m_affineMatrix.numCols() - 1);
}
//----------------------------------------------------------------------------------------------
/** Getter for the valid status.
*
* @return The valid status. i.e. has setMatrix been called.
*/
bool AffineMatrixParameter::isValid() const { return m_isValid; }
//----------------------------------------------------------------------------------------------
/** Assignemnt operator.
* @param other : another affine matrix to assign from.
* @return ref to assigned object
*/
AffineMatrixParameter &AffineMatrixParameter::operator=(const AffineMatrixParameter &other) {
if ((other.m_affineMatrix.numCols() != this->m_affineMatrix.numCols()) ||
(other.m_affineMatrix.numRows() != this->m_affineMatrix.numRows())) {
throw std::runtime_error("Cannot make assignemnts between "
"AffineMatrixParameter when the matrixes are of "
"different sizes.");
}
if (this != &other) {
this->m_affineMatrix = other.m_affineMatrix;
this->m_isValid = other.m_isValid;
copyRawMatrix();
}
return *this;
}
//----------------------------------------------------------------------------------------------
/** Copy constructor
* @param other : another affine matrix to copy from.
*/
AffineMatrixParameter::AffineMatrixParameter(const AffineMatrixParameter &other)
: m_affineMatrix(other.m_affineMatrix) {
m_isValid = other.m_isValid;
size_t nx = m_affineMatrix.numRows();
size_t ny = m_affineMatrix.numCols();
m_rawMem = new coord_t[nx * ny];
m_rawMatrix = new coord_t *[nx];
for (size_t i = 0; i < nx; i++)
m_rawMatrix[i] = m_rawMem + (i * ny);
copyRawMatrix();
}
//----------------------------------------------------------------------------------------------
/** Setter for the internal affine matrix.
*
* @param newMatrix : new matrix to use.
*/
void AffineMatrixParameter::setMatrix(const AffineMatrixType &newMatrix) {
if (newMatrix.numRows() != this->m_affineMatrix.numRows())
throw std::runtime_error("setMatrix(): Number of rows must match!");
if (newMatrix.numCols() != this->m_affineMatrix.numCols())
throw std::runtime_error("setMatrix(): Number of columns must match!");
m_affineMatrix = newMatrix;
// Copy into the raw matrix (for speed)
copyRawMatrix();
this->m_isValid = true;
}
} // namespace Mantid::DataObjects