Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Framework/Geometry/src/Instrument/Goniometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ void Goniometer::saveNexus(::NeXus::File *file, const std::string &group) const
file->writeData("num_axes", int(motors.size()));
for (size_t i = 0; i < motors.size(); i++)
motors[i].saveNexus(file, "axis" + Strings::toString(i));
DblMatrix rMatrix = getR();
// Flatten DblMatrix for saving
std::vector<double> matrixData = rMatrix.getVector();
file->writeData("rotation_matrix", matrixData);
file->closeGroup();
}

Expand All @@ -390,10 +394,19 @@ void Goniometer::loadNexus(::NeXus::File *file, const std::string &group) {
file->readData("num_axes", num_axes);
motors.clear();
motors.reserve(num_axes);
for (int i = 0; i < num_axes; i++) {
GoniometerAxis newAxis;
newAxis.loadNexus(file, "axis" + Strings::toString(i));
motors.emplace_back(newAxis);
// If a rotation matrix is available and no rotation axes are provided load should behave like initFromR
if (file->hasData("rotation_matrix") && (num_axes < 1)) {
std::vector<double> matrixData;
file->readData("rotation_matrix", matrixData);
DblMatrix rotMat(matrixData);
setR(rotMat);
initFromR = true; // set as initFromR to prevent overwrite on R recalc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor -but I think the naming convention for such member variable (if that is the right term?) is m_initFromR - obviously this wasn't introduced in this PR (apologies for missing it before), but it helps to make the scope of the variable clear!

} else {
for (int i = 0; i < num_axes; i++) {
GoniometerAxis newAxis;
newAxis.loadNexus(file, "axis" + Strings::toString(i));
motors.emplace_back(newAxis);
}
}
file->closeGroup();
// Refresh cached values
Expand Down
19 changes: 18 additions & 1 deletion Framework/Geometry/test/GoniometerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class GoniometerTest : public CxxTest::TestSuite {
}

/** Save and load to NXS file */
void test_nexus() {
void test_nexus_from_set_axes() {
NexusTestHelper th(true);
th.createFile("GoniometerTest.nxs");

Expand All @@ -264,6 +264,23 @@ class GoniometerTest : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(G2.getR(), G.getR());
}

void test_nexus_from_set_matrix() {
NexusTestHelper th(true);
th.createFile("GoniometerTest2.nxs");

Mantid::Kernel::DblMatrix rotation_x{{1, 0, 0, 0, 0, -1, 0, 1, 0}}; // 90 degrees around x axis
Goniometer G(rotation_x);
TS_ASSERT_EQUALS(G.getR().equals(rotation_x), true);
G.saveNexus(th.file.get(), "goniometer");

// Reload from the file
th.reopenFile();
Goniometer G2;
G2.loadNexus(th.file.get(), "goniometer");
// Rotation matrices should be the same after loading
TS_ASSERT_EQUALS(G2.getR(), G.getR());
}

void test_equals_when_identical() {
Mantid::Kernel::DblMatrix rotation_x{{1, 0, 0, 0, 0, -1, 0, 1, 0}}; // 90 degrees around x axis
Goniometer a(rotation_x);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- ``saveNexus`` and ``loadNexus`` methods of :py:obj:`~mantid.geometry.Goniometer` object now directly save/load a copy of the rotation matrix. Previously, upon saving/loading, the :py:obj:`~mantid.geometry.Goniometer` would only deal with the rotation axes, leading to loss of rotation information if the :py:obj:`~mantid.geometry.Goniometer` had been defined using ``setR`` method, or similar approach.