diff --git a/tests/JFJochReaderTest.cpp b/tests/JFJochReaderTest.cpp index 5dc03dcf7..bc07750c1 100644 --- a/tests/JFJochReaderTest.cpp +++ b/tests/JFJochReaderTest.cpp @@ -14,6 +14,8 @@ #include "../reader/MiniCBF.h" #include "../compression/JFJochCompressor.h" +#include +#include #include #include #include @@ -5234,6 +5236,111 @@ TEST_CASE("JFJochReader_JungfrauModules", "[HDF5][Full]") { REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); } +// A pixel of an NXmx module taken to the lab by following the file's own depends_on chain, independently +// of the reader: start at j fast + i slow pixel steps, then apply module_offset, the detector translation and +// the rotations, each in turn up to the root. McStas frame, metres. +static std::array NXmxPixelToLab(HDF5Object &file, const std::string &module, double j, double i) { + auto vec = [&](const std::string &path) { + HDF5DataSet ds(file, path); + std::vector value; + ds.ReadVector(value); + const auto v = ds.ReadAttrVec("vector"); + return std::array{v[0], v[1], v[2], value[0]}; + }; + const auto f = vec(module + "/fast_pixel_direction"), s = vec(module + "/slow_pixel_direction"); + std::array p{}; + for (int k = 0; k < 3; k++) + p[k] = j * f[3] * f[k] + i * s[3] * s[k]; + std::string node = module + "/module_offset"; + while (node != ".") { + HDF5DataSet ds(file, node); + const auto t = vec(node); + if (ds.ReadAttrStr("transformation_type") == "translation") { + for (int k = 0; k < 3; k++) + p[k] += t[3] * t[k]; + } else { + // Rodrigues: p cos a + (u x p) sin a + u (u.p)(1 - cos a); angles in rad, as this writer states them + REQUIRE(ds.ReadAttrStr("units") == "rad"); + const double c = std::cos(t[3]), sn = std::sin(t[3]); + const double dot = t[0] * p[0] + t[1] * p[1] + t[2] * p[2]; + const std::array cross{t[1] * p[2] - t[2] * p[1], t[2] * p[0] - t[0] * p[2], + t[0] * p[1] - t[1] * p[0]}; + for (int k = 0; k < 3; k++) + p[k] = p[k] * c + cross[k] * sn + t[k] * dot * (1 - c); + } + node = ds.ReadAttrStr("depends_on"); + } + return p; +} + +// Modules turned a quarter (fast along +y, slow along -x) beside unturned ones, staggered, mirrored in Y, on +// a tilted detector: every corner pixel of every module, put where the receiver puts it in the image, lies in +// the file's module of the same index, and the file's chain takes it to the lab position DiffractionGeometry +// gives that image pixel. +TEST_CASE("JFJochReader_TurnedModules_LabCorners", "[HDF5][Full]") { + RegisterHDF5Filter(); + using D = DetectorModuleGeometry::Direction; + const std::vector layout = { + {600, 0, D::Xpos, D::Ypos}, {1640, 60, D::Xpos, D::Ypos}, + {600, 550, D::Xpos, D::Ypos}, {1640, 610, D::Xpos, D::Ypos}, + {580, 100, D::Ypos, D::Xneg}, {3200, 150, D::Ypos, D::Xneg}}; + for (const DetectorOrientation orientation: {DetectorOrientation(), DetectorOrientation(true, 1)}) { + DiffractionExperiment x(DetJF(DetectorGeometryModular(layout, true)).ImageOrientation(orientation)); + x.FilePrefix("test_turned_modules").ImagesPerTrigger(1).OverwriteExistingFiles(true); + x.BeamX_pxl(1500).BeamY_pxl(700).DetectorDistance_mm(120) + .PoniRot1_rad(0.02f).PoniRot2_rad(-0.03f).PoniRot3_rad(0.01f) + .IncidentEnergy_keV(WVL_1A_IN_KEV).PixelSigned(false).BitDepthImage(16); + x.FrameTime(std::chrono::microseconds(500), std::chrono::microseconds(10)); + { + StartMessage start_message; + x.FillMessage(start_message); + FileWriter file_set(start_message); + DataMessage message{}; + std::vector image(x.GetPixelsNum(), 0); + message.image = CompressedImage(image, x.GetXPixelsNum(), x.GetYPixelsNum()); + message.number = 0; + REQUIRE_NOTHROW(file_set.WriteHDF5(message)); + EndMessage end_message; + end_message.max_image_number = 1; + file_set.WriteHDF5(end_message); + file_set.Finalize(); + } + { + const auto &geom = x.GetDetectorSetup().GetGeometry(); + const DiffractionGeometry dg = x.GetDiffractionGeometry(); + HDF5ReadOnlyFile file("test_turned_modules_master.h5"); + REQUIRE(geom.GetModulesNum() == 6); + for (int64_t m = 0; m < geom.GetModulesNum(); m++) { + const std::string module = "/entry/instrument/detector/module_" + std::to_string(m); + const auto origin = file.ReadVector(module + "/data_origin"); + const auto size = file.ReadVector(module + "/data_size"); + REQUIRE(origin.size() == 2); + REQUIRE(size.size() == 2); + for (const int64_t col: {int64_t(0), int64_t(CONVERTED_MODULE_COLS - 1)}) + for (const int64_t line: {int64_t(0), int64_t(CONVERTED_MODULE_LINES - 1)}) { + const Coord f = geom.GetFastDirection(m), s = geom.GetSlowDirection(m); + const auto px = static_cast(geom.GetX0(m) + col * f.x + line * s.x); + const auto py = static_cast(geom.GetY0(m) + col * f.y + line * s.y); + // in this module's box of the image (data_origin and data_size are slow, fast) + const double j = px - origin[1], i = py - origin[0]; + CHECK(j >= 0); + CHECK(j < size[1]); + CHECK(i >= 0); + CHECK(i < size[0]); + const auto lab = NXmxPixelToLab(file, module, j, i); + const Coord expected = dg.LabCoord(px, py); // internal frame, mm + CHECK(lab[0] == Catch::Approx(-expected.x / 1000.0).margin(2e-6)); + CHECK(lab[1] == Catch::Approx(-expected.y / 1000.0).margin(2e-6)); + CHECK(lab[2] == Catch::Approx(expected.z / 1000.0).margin(2e-6)); + } + } + } + remove("test_turned_modules_master.h5"); + remove("test_turned_modules_data_000001.h5"); + } + REQUIRE(H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL) == 0); +} + // A detector with modules stacked vertically beside horizontal ones (the CrMX JUNGFRAU 8M layout, mirrored // in Y): the master states the 16 modules as module_0..15 and no whole-image module, each its box of the // image - a vertical module is a box 514 wide and 1030 high - named module_00 ... module_15, and they read