Skip to content
Snippets Groups Projects
Commit 3e45549a authored by Simon Heybrock's avatar Simon Heybrock
Browse files

Re #18659. Improved performance of Instrument::makeLegacyParameterMap.

parent f480d3cd
No related merge requests found
...@@ -1266,36 +1266,51 @@ boost::shared_ptr<ParameterMap> Instrument::makeLegacyParameterMap() const { ...@@ -1266,36 +1266,51 @@ boost::shared_ptr<ParameterMap> Instrument::makeLegacyParameterMap() const {
constexpr double L = 1000.0; constexpr double L = 1000.0;
constexpr double safety_factor = 2.0; constexpr double safety_factor = 2.0;
const double imag_norm_max = sin(d_max / (2.0 * L * safety_factor)); const double imag_norm_max = sin(d_max / (2.0 * L * safety_factor));
const IComponent *oldParent{nullptr};
Eigen::Quaterniond invParentRot;
Eigen::Affine3d transformation;
for (size_t i = 0; i < m_detectorInfo->size(); ++i) { for (size_t i = 0; i < m_detectorInfo->size(); ++i) {
const auto &det = std::get<1>(baseInstr.m_detectorCache[i]); const auto &det = std::get<1>(baseInstr.m_detectorCache[i]);
if (m_detectorInfo->isMasked(i)) { if (m_detectorInfo->isMasked(i)) {
pmap->forceUnsafeSetMasked(det.get(), true); pmap->forceUnsafeSetMasked(det.get(), true);
} }
// Compare detector position with position based on pmap. Note that we // Obtain parent position/rotation/scale.
// previously stripped the DetectorInfo from pmap so we do not get positions const auto parent = det->getParent();
// from the DetectorInfo but only those directly stored in pmap. if (parent.get() != oldParent) {
const auto parDet = ParComponentFactory::create(det, pmap.get()); oldParent = parent.get();
auto relPos = m_detectorInfo->position(i); const auto parParent = ParComponentFactory::create(parent, pmap.get());
auto relRot = m_detectorInfo->rotation(i); const auto parentPos = toVector3d(parParent->getPos());
if (const auto parent = parDet->getParent()) { invParentRot = toQuaterniond(parParent->getRotation()).conjugate();
relPos -= toVector3d(parent->getPos()); transformation = invParentRot;
const auto invParentRot = transformation.translate(-parentPos);
toQuaterniond(parent->getRotation()).conjugate(); // Special case: scaling for RectangularDetectorPixel
relPos = invParentRot * relPos; if (boost::dynamic_pointer_cast<const RectangularDetectorPixel>(det)) {
// Note the unusual order, see Component.cpp const auto *panel = det->getParent()->getParent().get();
relRot = invParentRot * relRot; Eigen::Vector3d scale(1, 1, 1);
if (auto scalex = pmap->get(panel, "scalex"))
scale[0] = 1.0 / scalex->value<double>();
if (auto scaley = pmap->get(panel, "scaley"))
scale[1] = 1.0 / scaley->value<double>();
transformation.prescale(scale);
}
} }
// Undo parent transformation to obtain relative position/rotation.
auto relPos = transformation * m_detectorInfo->position(i);
auto relRot = invParentRot * m_detectorInfo->rotation(i);
// Tolerance 1e-9 m as in Beamline::DetectorInfo::isEquivalent. // Tolerance 1e-9 m as in Beamline::DetectorInfo::isEquivalent.
if ((relPos - toVector3d(parDet->getRelativePos())).norm() >= 1e-9) { if ((relPos - toVector3d(det->getRelativePos())).norm() >= 1e-9) {
if (boost::dynamic_pointer_cast<const RectangularDetectorPixel>(parDet)) if (boost::dynamic_pointer_cast<const RectangularDetectorPixel>(det))
throw std::runtime_error("Cannot create legacy ParameterMap: Position " throw std::runtime_error("Cannot create legacy ParameterMap: Position "
"parameters for RectangularDetectorPixel are " "parameters for RectangularDetectorPixel are "
"not supported"); "not supported");
pmap->addV3D(det->getComponentID(), ParameterMap::pos(), toV3D(relPos)); pmap->addV3D(det->getComponentID(), ParameterMap::pos(), toV3D(relPos));
} }
if ((relRot * toQuaterniond(parDet->getRelativeRot()).conjugate()) if ((relRot * toQuaterniond(det->getRelativeRot()).conjugate())
.vec() .vec()
.norm() >= imag_norm_max) .norm() >= imag_norm_max)
pmap->addQuat(det->getComponentID(), ParameterMap::rot(), toQuat(relRot)); pmap->addQuat(det->getComponentID(), ParameterMap::rot(), toQuat(relRot));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment