diff --git a/image_analysis/geom_refinement/GeometryRefiner.cpp b/image_analysis/geom_refinement/GeometryRefiner.cpp index 9544623e5..0eeff8384 100644 --- a/image_analysis/geom_refinement/GeometryRefiner.cpp +++ b/image_analysis/geom_refinement/GeometryRefiner.cpp @@ -590,12 +590,17 @@ GeometryRefinerResult RefineGlobalGeometry(const DiffractionGeometry &nominal_ge // The refined modules' moves: bounded to what a misplaced module plausibly is, and held to // zero in sum by the gauge (ModuleGauge). A 1 px mean shift costs as much there as moving - // every spot by 1 px would, ten times over. + // every spot by 1 px would, ten times over. A module held as given (modules_held) already fixes + // the frame the others are placed in; the gauge would fix it a second time, and the fit, pulled + // between the two, crawls (on an 8-module detector with one module held: 50-100 iterations a + // round instead of 3-7) and settles with modules pressed against their bounds. std::vector moved; for (size_t m = 0; m < modules.size(); ++m) if (refined[m] && problem.HasParameterBlock(move[m].data())) moved.push_back(m); - if (moved.size() >= 2) { + const bool any_held = std::find(settings.modules_held.begin(), settings.modules_held.end(), true) + != settings.modules_held.end(); + if (moved.size() >= 2 && !any_held) { auto *gauge = new ModuleGauge; double lever = 0, total = 0; for (size_t m : moved) diff --git a/tests/ModuleRefinementTest.cpp b/tests/ModuleRefinementTest.cpp index b97d6fd7e..d208627a5 100644 --- a/tests/ModuleRefinementTest.cpp +++ b/tests/ModuleRefinementTest.cpp @@ -123,6 +123,53 @@ TEST_CASE("RefineGlobalGeometry_ModulePlacement") { CHECK(scored.distance_mm == geom.GetDetectorDistance_mm()); } +// A module held as given fixes the frame the others are placed in: their moves come out as they are, with no +// gauge taking their sum away, and the beam stays where the held module puts it. +TEST_CASE("RefineGlobalGeometry_ModulePlacement_OneHeld") { + DiffractionExperiment exp; + exp.IncidentEnergy_keV(WVL_1A_IN_KEV).BeamX_pxl(1000).BeamY_pxl(1000).DetectorDistance_mm(150); + const DiffractionGeometry geom = exp.GetDiffractionGeometry(); + + // Module 0 where it was given, the others moved by amounts that do not sum to zero. + std::vector truth(4); + const double centre[4][2] = {{500, 500}, {1500, 500}, {500, 1500}, {1500, 1500}}; + const double move[4][3] = {{0.0, 0.0, 0.0}, {3.0, 1.0, 0.2}, {2.0, -2.0, -0.1}, {1.5, 2.5, 0.1}}; + for (int m = 0; m < 4; m++) { + truth[m].x0 = centre[m][0]; + truth[m].y0 = centre[m][1]; + truth[m].dx = move[m][0]; + truth[m].dy = move[m][1]; + truth[m].rot_rad = move[m][2] * PI / 180.0; + } + const CrystalLattice latt(50, 60, 70, 90, 90, 90); + const std::vector frames = FramesOnMovedModules(exp, latt, truth); + REQUIRE(frames.size() > 30); + + GeometryRefinerSettings st; + st.crystal_system = gemmi::CrystalSystem::Orthorhombic; + st.num_threads = 2; + st.refine_modules = true; + st.modules_held = {true, false, false, false}; + for (const auto &t: truth) { + ModuleShift start; + start.x0 = t.x0; + start.y0 = t.y0; + st.modules.push_back(start); + } + + const auto placed = RefineGlobalGeometry(geom, latt.GetUnitCell(), frames, st); + REQUIRE(placed.ok); + CHECK(placed.median_residual_px < 0.1); + for (int m = 0; m < 4; m++) { + CHECK(placed.modules[m].dx == Catch::Approx(truth[m].dx).margin(0.1)); + CHECK(placed.modules[m].dy == Catch::Approx(truth[m].dy).margin(0.1)); + CHECK(placed.modules[m].rot_rad * 180.0 / PI == Catch::Approx(move[m][2]).margin(0.01)); + } + CHECK(placed.beam_x_px == Catch::Approx(1000).margin(0.1)); + CHECK(placed.beam_y_px == Catch::Approx(1000).margin(0.1)); + CHECK(placed.distance_mm == Catch::Approx(150).margin(0.05)); +} + // The coarse search ahead of the placement: a module off by several pixels is found near where it is, and the // search gives the same answer on any number of threads. TEST_CASE("ScanModuleShifts_FindsShiftedModule") {