// Copyright (2019-2022) Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-or-later #include "DetectorGeometry.h" #include "JFJochException.h" #include "Definitions.h" DetectorGeometry::DetectorGeometry(const std::vector &in_modules) : modules(in_modules) { if (modules.empty()) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Module number must be positive"); width = 0; height = 0; for (auto &m: modules) { width = std::max(width, m.GetMaxX()+1); height = std::max(height, m.GetMaxY()+1); } } DetectorGeometry::DetectorGeometry(int32_t nmodules, int32_t horizontal_stacking, int32_t gap_x, int32_t gap_y, bool mirror_y) { if (horizontal_stacking <= 0) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Horizontal stacking must be positive"); if (nmodules <= 0) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Module number must be positive"); if (gap_x < 0) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Gap x has to be non-negative"); if (gap_y < 0) throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Gap y has to be non-negative"); if (nmodules < horizontal_stacking) horizontal_stacking = nmodules; width = horizontal_stacking * CONVERTED_MODULE_COLS + (horizontal_stacking - 1) * gap_x; int64_t conv_lines = nmodules / horizontal_stacking + (nmodules % horizontal_stacking > 0 ? 1 : 0); height = conv_lines * CONVERTED_MODULE_LINES + (conv_lines - 1) * gap_y; for (int module = 0; module < nmodules; module++) { int64_t module_x = module % horizontal_stacking; int64_t module_y = module / horizontal_stacking; int64_t x0 = module_x * (CONVERTED_MODULE_COLS + gap_x); int64_t y0 = module_y * (CONVERTED_MODULE_LINES + gap_y); DetectorModuleGeometry::Direction fast = DetectorModuleGeometry::Direction::Xpos; DetectorModuleGeometry::Direction slow; if (mirror_y) { y0 = height - y0 - 1; slow = DetectorModuleGeometry::Direction::Yneg; } else slow = DetectorModuleGeometry::Direction::Ypos; modules.emplace_back(x0, y0, fast, slow); } } DetectorGeometry::operator JFJochProtoBuf::DetectorGeometry() const { JFJochProtoBuf::DetectorGeometry ret; ret.set_height_pxl(height); ret.set_width_pxl(width); for (auto &m: modules) { auto mpbf = ret.add_module_geometry(); mpbf->set_pixel0(m.GetPixel0_X() + width * m.GetPixel0_Y()); mpbf->set_fast_direction_step(GetDirectionStep(m.GetFastAxis())); mpbf->set_slow_direction_step(GetDirectionStep(m.GetSlowAxis())); } return ret; } int64_t DetectorGeometry::GetDirectionStep(DetectorModuleGeometry::Direction direction) const { switch (direction) { case DetectorModuleGeometry::Direction::Xneg: return -1; case DetectorModuleGeometry::Direction::Xpos: return 1; case DetectorModuleGeometry::Direction::Yneg: return -width; case DetectorModuleGeometry::Direction::Ypos: return width; default: return 0; } } int64_t DetectorGeometry::GetModulesNum() const { return modules.size(); }