8#include "MantidBeamline/ComponentInfo.h"
9#include "MantidBeamline/ComponentType.h"
21#include <Eigen/Geometry>
33const Eigen::Vector3d undoRotation(
const Eigen::Vector3d &point,
const Beamline::ComponentInfo &compInfo,
34 const size_t componentIndex) {
35 auto unRotateTransform = Eigen::Affine3d(compInfo.rotation(componentIndex).inverse());
36 return unRotateTransform * point;
45const Kernel::V3D toShapeFrame(
const Kernel::V3D &point,
const Beamline::ComponentInfo &compInfo,
46 const size_t componentIndex) {
48 undoRotation(
Kernel::toVector3d(point) - compInfo.position(componentIndex), compInfo, componentIndex));
62 std::unique_ptr<Beamline::ComponentInfo> componentInfo,
63 std::shared_ptr<
const std::vector<Mantid::Geometry::IComponent *>> componentIds,
64 std::shared_ptr<
const std::unordered_map<Geometry::IComponent const *, size_t>> componentIdToIndexMap,
65 std::shared_ptr<std::vector<std::shared_ptr<const Geometry::IObject>>> shapes,
66 std::shared_ptr<ParameterInfo> parameterInfo)
67 : m_componentInfo(
std::move(componentInfo)), m_componentIds(
std::move(componentIds)),
68 m_compIDToIndex(
std::move(componentIdToIndexMap)), m_shapes(
std::move(shapes)),
69 m_parameterInfo(
std::move(parameterInfo)) {
72 throw std::invalid_argument(
"Inconsistent ID and Mapping input containers "
73 "for Geometry::ComponentInfo");
76 throw std::invalid_argument(
"Inconsistent ID and base "
77 "Beamline::ComponentInfo sizes for "
78 "Geometry::ComponentInfo");
96 : m_componentInfo(other.m_componentInfo->cloneWithoutDetectorInfo()), m_componentIds(other.m_componentIds),
97 m_compIDToIndex(other.m_compIDToIndex), m_shapes(other.m_shapes), m_parameterInfo(other.m_parameterInfo) {}
119 if (!(type == Beamline::ComponentType::Structured || type == Beamline::ComponentType::Rectangular ||
120 parentType == Beamline::ComponentType::Grid))
121 throw std::runtime_error(
"ComponentType is not Structured or Rectangular "
122 "in ComponentInfo::quadrilateralComponent.");
125 const auto &innerRangeComp =
m_componentInfo->children(componentIndex);
126 corners.
nX = innerRangeComp.size();
129 corners.
nY = firstCol.size();
132 corners.
topLeft = firstCol.back();
154 const auto *shapeAtIndex = (*m_shapes)[componentIndex].get();
155 return shapeAtIndex !=
nullptr && shapeAtIndex->hasValidShape();
239 const auto range =
m_componentInfo->detectorRangeInSubtree(componentIndex);
240 return range.begin() < range.end();
268 std::unordered_map<std::shared_ptr<const IObject>, std::vector<size_t>> result;
270 for (
size_t i = 0; i < shapes.size(); ++i) {
271 const auto ¤tShape = shapes[i];
272 if (currentShape !=
nullptr && currentShape->hasValidShape())
273 result[currentShape].emplace_back(i);
300 throw std::runtime_error(
"ComponentType is not Rectangular or Grid in ComponentInfo::pixelGridComponent.");
310std::tuple<int, int, int> xyzFillFirstZ(
const Beamline::PixelGridComponent &grid,
int col,
int id) {
311 if (grid.idFillOrder[1] ==
'y') {
312 int row = (
id / grid.idStepByRow) % grid.nY;
313 int layer = (
id / grid.idStepByRow) / grid.nY;
314 return {layer, row, col};
316 int row = (
id / grid.idStepByRow) % grid.nX;
317 int layer = (
id / grid.idStepByRow) / grid.nX;
318 return {row, layer, col};
321std::tuple<int, int, int> xyzFillFirstY(
const Beamline::PixelGridComponent &grid,
int col,
int id) {
322 if (grid.idFillOrder[1] ==
'z') {
323 int row = (
id / grid.idStepByRow) % grid.nZ;
324 int layer = (
id / grid.idStepByRow) / grid.nZ;
325 return {layer, col, row};
327 int row = (
id / grid.idStepByRow) % grid.nX;
328 int layer = (
id / grid.idStepByRow) / grid.nX;
329 return {row, col, layer};
332std::tuple<int, int, int> xyzFillFirstX(
const Beamline::PixelGridComponent &grid,
int col,
int id) {
333 if (grid.idFillOrder[1] ==
'y') {
334 int row = (
id / grid.idStepByRow) % grid.nY;
335 int layer = (
id / grid.idStepByRow) / grid.nY;
336 return {col, row, layer};
338 int row = (
id / grid.idStepByRow) % grid.nZ;
339 int layer = (
id / grid.idStepByRow) / grid.nZ;
340 return {col, layer, row};
355 const int id = detectorID - grid.idStart;
356 if (grid.idStepByRow == 0 || grid.idStep == 0)
358 const int col = (
id % grid.idStepByRow) / grid.idStep;
360 if (grid.idFillOrder[0] ==
'z')
361 return xyzFillFirstZ(grid, col,
id);
362 if (grid.idFillOrder[0] ==
'y')
363 return xyzFillFirstY(grid, col,
id);
364 return xyzFillFirstX(grid, col,
id);
374 if ((scaleFactorAtIndex -
Kernel::V3D(1.0, 1.0, 1.0)).norm() < 1e-12)
379 return shape(componentIndex).
solidAngle(paramsWithRelativeObserver, scaleFactorAtIndex);
402 const bool excludeMonitors)
const {
422 const bool excludeMonitors)
const {
436 const bool excludeMonitors)
const {
441 if (excludeMonitors) {
455 const auto &s = this->
shape(index);
461 absoluteBB.
xMin() *= scaleFactorAtIndex[0];
462 absoluteBB.
xMax() *= scaleFactorAtIndex[0];
463 absoluteBB.
yMin() *= scaleFactorAtIndex[1];
464 absoluteBB.
yMax() *= scaleFactorAtIndex[1];
465 absoluteBB.
zMin() *= scaleFactorAtIndex[2];
466 absoluteBB.
zMax() *= scaleFactorAtIndex[2];
469 .rotateBB(absoluteBB.
xMin(), absoluteBB.
yMin(), absoluteBB.
zMin(), absoluteBB.
xMax(), absoluteBB.
yMax(),
474 absoluteBB.
xMin() += localPos[0];
475 absoluteBB.
xMax() += localPos[0];
476 absoluteBB.
yMin() += localPos[1];
477 absoluteBB.
yMax() += localPos[1];
478 absoluteBB.
zMin() += localPos[2];
479 absoluteBB.
zMax() += localPos[2];
483 std::vector<Kernel::V3D> coordSystem;
487 absoluteBB.
realign(&coordSystem);
505 const bool excludeMonitors)
const {
513 auto parentFlag = Beamline::ComponentType::Generic;
520 }
else if (compFlag == Beamline::ComponentType::Unstructured) {
521 for (
const auto &childIndex : this->
children(componentIndex)) {
524 }
else if (compFlag == Beamline::ComponentType::Grid) {
525 for (
const auto &childIndex : this->
children(componentIndex)) {
528 }
else if (compFlag == Beamline::ComponentType::Rectangular || compFlag == Beamline::ComponentType::Structured ||
529 parentFlag == Beamline::ComponentType::Grid) {
531 }
else if (compFlag == Beamline::ComponentType::OutlineComposite) {
545 m_componentInfo->setScanInterval({interval.first.totalNanoseconds(), interval.second.totalNanoseconds()});
561 constexpr size_t noBankParent{0};
568 if (indexChildren.empty()) {
572 if (
name(indexChildren[0]).starts_with(bankPart)) {
576 for (
size_t const child : indexChildren) {
578 if (j != noBankParent) {
588 std::vector<size_t> ancestry{componentIndex};
591 ancestry.emplace_back(
index);
594 for (
auto it = ancestry.crbegin(); it != ancestry.crend(); ++it) {
595 if (it != ancestry.crbegin()) {
617 size_t current =
root();
618 size_t segmentStart = 0;
620 while (segmentStart <=
fullName.size()) {
621 const auto separator =
fullName.find(
'/', segmentStart);
623 fullName.substr(segmentStart, separator == std::string::npos ? std::string::npos : separator - segmentStart);
626 if (
name(current) != segment) {
631 auto const &candidates =
children(current);
633 std::find_if(candidates.cbegin(), candidates.cend(), [&](
size_t child) { return name(child) == segment; });
634 if (found == candidates.cend()) {
639 if (separator == std::string::npos) {
642 segmentStart = separator + 1;
649 std::vector<T> result;
654 result.emplace_back(param->value<T>());
664 result = recursive ?
static_cast<bool>(
m_parameterInfo->getRecursive(*
this, componentIndex,
name))
671 std::set<std::string> result;
675 if (recursive &&
hasParent(componentIndex)) {
677 result.insert(parentNames.cbegin(), parentNames.cend());
692 const T &
value,
const std::string *
const pDescription,
693 const std::string &pVisible) {
695 auto typedParameter = std::dynamic_pointer_cast<ParameterType<T>>(parameter);
696 if (!typedParameter) {
697 throw std::invalid_argument(
"ComponentInfo: parameter type '" + type +
"' does not match the value supplied for '" +
700 typedParameter->setValue(
value);
702 parameter->setDescription(*pDescription);
708 const std::string &
value,
const std::string *
const pDescription,
709 const std::string &pVisible) {
713 parameter->fromString(
value);
715 parameter->setDescription(*pDescription);
721 const std::string *
const pDescription,
const std::string &pVisible) {
726 const std::string *
const pDescription,
const std::string &pVisible) {
731 const std::string *
const pDescription,
const std::string &pVisible) {
736 const std::string *
const pDescription,
const std::string &pVisible) {
741 const std::string *
const pDescription) {
746 const std::string *
const pDescription) {
751 const std::string &fittingFunction,
const std::string &
value,
752 const std::string *
const pDescription,
const std::string &pVisible) {
754 parameter->fromString(
value);
756 parameter->setDescription(*pDescription);
773 bool recursive)
const {
774 return getParameter<double>(componentIndex,
name, recursive);
778 bool recursive)
const {
779 return getParameter<int>(componentIndex,
name, recursive);
783 bool recursive)
const {
784 return getParameter<bool>(componentIndex,
name, recursive);
788 bool recursive)
const {
789 return getParameter<std::string>(componentIndex,
name, recursive);
794 throw std::runtime_error(
"Parameters are not available in component=" + this->
name(componentIndex));
797 throw std::runtime_error(
"Fitting parameter=" +
name +
798 " could not be extracted from component=" + this->
name(componentIndex));
800 const auto &fitParam = parameter->value<
FitParameter>();
803 throw std::runtime_error(
"Unable to get lookup table for parameter=" +
name +
804 " from component=" + this->
name(componentIndex));
812 const size_t n =
size();
816 const size_t componentIdsMem =
sizeof(std::vector<Geometry::IComponent *>) +
n *
sizeof(
Geometry::IComponent *);
819 const size_t compIDToIndexMem =
sizeof(std::unordered_map<Geometry::IComponent const *, size_t>) +
823 const size_t shapesMem =
824 m_shapes ?
sizeof(*m_shapes) +
m_shapes->capacity() *
sizeof(std::shared_ptr<const Geometry::IObject>) : 0;
825 return sizeof(*this) + beamlineMem + componentIdsMem + compIDToIndexMem + shapesMem;
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
double xMax() const
Return the maximum value of X.
double zMin() const
Return the minimum value of Z.
std::vector< Kernel::V3D > const & getCoordSystem() const
returns the coordinate system to which BB is alighned to;
double zMax() const
Return the maximum value of Z.
double yMax() const
Return the maximum value of Y.
bool isAxisAligned() const
Check if it is normal axis aligned bounding box or not.
double xMin() const
Return the minimum value of X.
double yMin() const
Return the minimum value of Y.
void grow(const BoundingBox &other)
Grow the bounding box so that it also encompasses the given box.
void realign(std::vector< Kernel::V3D > const *const pCS=nullptr)
reallign the BB according to new coordinate system, provided earlier or specified as parameter;
ComponentInfo : Provides a component centric view on to the instrument.
std::vector< int > getIntParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
bool hasParent(const size_t componentIndex) const
ParameterInfo & mutableParameterInfo()
The store, created on first write if this ComponentInfo was built without one (a bare,...
const ComponentInfoIterator< const ComponentInfo > cend()
bool hasDetectorInfo() const
BoundingBox componentBoundingBox(const size_t index, const BoundingBox *reference, const bool excludeMonitors=false) const
Calculates the absolute bounding box for the leaf item at index.
size_t findBankParent(size_t index, const std::string &bankPart) const
void clearParameter(const size_t componentIndex, const std::string &name)
Remove every parameter of this name from this component.
std::string fullName(const size_t componentIndex) const
The fully-qualified name of a component ("instrument/bank1/pixel3")
std::unordered_map< std::shared_ptr< const Geometry::IObject >, std::vector< size_t > > shapeToComponentIndices() const
Build a map from each unique shape to the list of component indices that share it.
size_t indexOfAny(const std::string &name) const
void setRotation(size_t componentIndex, const Kernel::Quat &newRotation)
bool hasDetectors(const size_t componentIndex) const
bool hasEquivalentSource(const ComponentInfo &other) const
void addQuat(const size_t componentIndex, const std::string &name, const Kernel::Quat &value, const std::string *const pDescription=nullptr)
Add or replace a named quaternion parameter at a specific component index.
void addFittingParameter(const size_t componentIndex, const std::string &name, const std::string &fittingFunction, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add a fitting parameter.
double solidAngle(const size_t componentIndex, const Geometry::SolidAngleParams ¶ms) const
size_t parent(const size_t componentIndex) const
Kernel::Quat rotation(const size_t componentIndex) const
bool isGridDetector(size_t const componentIndex) const
void growBoundingBoxAsOutline(size_t index, const Geometry::BoundingBox *reference, Geometry::BoundingBox &mutableBB, const bool excludeMonitors=false) const
Grow the bounding box on the basis that the component described by index has an outline,...
BoundingBox boundingBox(const size_t componentIndex, const BoundingBox *reference=nullptr, const bool excludeMonitors=false) const
Compute the bounding box for the component with componentIndex taking into account all sub components...
std::shared_ptr< std::vector< std::shared_ptr< const Geometry::IObject > > > m_shapes
Shapes for each component.
void scaleComponent(const size_t componentIndex, const Kernel::V3D &newScaling)
std::vector< std::string > getStringParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
std::shared_ptr< const std::unordered_map< Geometry::IComponent const *, size_t > > m_compIDToIndex
Map of component ids to indexes.
Kernel::V3D position(const size_t componentIndex) const
const std::vector< size_t > & children(size_t componentIndex) const
Kernel::V2D sideBySideViewPosition(const size_t componentIndex) const
std::vector< size_t > componentsInSubtree(size_t componentIndex) const
bool hasParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
Returns empty (or false) if the ComponentInfo was built without parameters or the parameter is unset.
void addTypedParameter(const size_t componentIndex, const std::string &type, const std::string &name, const T &value, const std::string *const pDescription, const std::string &pVisible)
Shared implementation of the typed add*() methods.
void merge(const ComponentInfo &other)
bool hasValidShape(const size_t componentIndex) const
std::vector< double > getNumberParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
double getFittingParameter(const size_t componentIndex, const std::string &name, double xvalue) const
void addString(const size_t componentIndex, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add or replace a named string parameter at a specific component index.
std::vector< bool > getBoolParameter(const size_t componentIndex, const std::string &name, bool recursive=true) const
Beamline::PixelGridComponent pixelGridComponent(const size_t componentIndex) const
Kernel::V3D sourcePosition() const
size_t indexOfOrInvalid(Geometry::IComponent const *id) const
The index for a component ID, or invalidIndex if it is not part of this instrument.
std::shared_ptr< ParameterInfo > m_parameterInfo
The instrument's named parameters, keyed by component index.
std::vector< T > getParameter(size_t componentIndex, const std::string &name, bool recursive) const
ComponentInfoIterator< ComponentInfo > begin()
bool hasEquivalentSample(const ComponentInfo &other) const
void setScaleFactor(const size_t componentIndex, const Kernel::V3D &scaleFactor)
ComponentInfoIterator< ComponentInfo > end()
std::unique_ptr< Beamline::ComponentInfo > m_componentInfo
Pointer to the actual ComponentInfo object (non-wrapping part).
std::vector< size_t > detectorsInSubtree(size_t componentIndex) const
bool isDetector(const size_t componentIndex) const
bool uniqueName(const std::string &name) const
void growBoundingBoxAsRectuangularBank(size_t index, const Geometry::BoundingBox *reference, Geometry::BoundingBox &mutableBB, const bool excludeMonitors=false) const
Grow the bounding box on the basis that the component described by index is a regular grid in a trape...
std::set< std::string > getParameterNames(const size_t componentIndex, bool recursive=true) const
std::unique_ptr< ComponentInfo > cloneWithoutDetectorInfo() const
Clone current instance but not the DetectorInfo non-owned parts.
const ComponentInfoIterator< const ComponentInfo > cbegin()
std::shared_ptr< const std::vector< Geometry::IComponent * > > m_componentIds
Collection of component ids.
void addInt(const size_t componentIndex, const std::string &name, int value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add or replace a named integer parameter at a specific component index.
size_t detectorIndexAtXYZ(const size_t componentIndex, const int x, const int y, const int z) const
Kernel::V3D relativePosition(const size_t componentIndex) const
std::tuple< int, int, int > xyzForDetectorID(const size_t componentIndex, const detid_t detectorID) const
Given the component index of a Rectangular/Grid bank, and a detector ID within it,...
size_t indexOf(Geometry::IComponent const *id) const
const std::string & name(const size_t componentIndex) const
size_t getMemorySize() const
Return memory used by the component info, in bytes.
Kernel::Quat relativeRotation(const size_t componentIndex) const
void setPosition(size_t componentIndex, const Kernel::V3D &newPosition)
void addDouble(const size_t componentIndex, const std::string &name, double value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add or replace a named double parameter at a specific component index.
static constexpr size_t invalidIndex
Returned by indexOfOrInvalid() for a component ID that is not part of this instrument.
Kernel::V3D samplePosition() const
const Geometry::IObject & shape(const size_t componentIndex) const
void setScanInterval(const std::pair< Types::Core::DateAndTime, Types::Core::DateAndTime > &interval)
const ParameterInfo::ComponentParameters & parameters(const size_t componentIndex) const
All parameters on one component, non-recursive, ordered by name.
void addBool(const size_t componentIndex, const std::string &name, bool value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add or replace a named boolean parameter at a specific component index.
size_t indexOfFullName(const std::string &fullName) const
The component with this fully-qualified name, or invalidIndex if there is none.
QuadrilateralComponent quadrilateralComponent(const size_t componentIndex) const
Beamline::ComponentType componentType(const size_t componentIndex) const
void addV3D(const size_t componentIndex, const std::string &name, const Kernel::V3D &value, const std::string *const pDescription=nullptr)
Add or replace a named vector parameter at a specific component index.
Kernel::V3D scaleFactor(const size_t componentIndex) const
ComponentInfo(const ComponentInfo &other)
Private copy constructor. Do not make public.
void addParameter(const size_t componentIndex, const std::string &type, const std::string &name, const std::string &value, const std::string *const pDescription=nullptr, const std::string &pVisible="true")
Add or replace a named parameter at a specific component index.
Store information about a fitting parameter such as its value if it is constrained or tied.
double getValue(const double &at) const
get paramter value
base class for Geometric IComponent
IObject : Interface for geometry objects.
virtual double solidAngle(const SolidAngleParams ¶ms) const =0
static std::shared_ptr< Parameter > create(const std::string &className, const std::string &name, const std::string &visible="true")
Creates an instance of a parameter.
ParameterInfo : the named parameters of an instrument, addressed by component index.
bool addFittingParameter(size_t const componentIndex, std::shared_ptr< Parameter > const ¶meter, std::string const &fittingFunction)
Add a fitting parameter, deduplicating by (name, function) rather than by name alone so that two func...
void add(size_t const componentIndex, std::shared_ptr< Parameter > const ¶meter)
Add a parameter, replacing any existing parameter of the same name on the same component.
std::multimap< std::string, std::shared_ptr< Parameter >, Kernel::CaseInsensitiveStringComparator > ComponentParameters
Parameters of a single component, ordered by name.
static const std::string & pQuat()
static const std::string & pBool()
static const std::string & pV3D()
static const std::string & pDouble()
static const std::string & pString()
static const std::string & pInt()
const SolidAngleParams copyWithNewObserver(Kernel::V3D newObserver) const
const Kernel::V3D & observer() const
Exception thrown when an attempt is made to dereference a null pointer.
Implements a 2-dimensional vector embedded in a 3D space, i.e.
std::shared_ptr< Parameter > Parameter_sptr
Typedef for the shared pointer.
ComponentInfoIterator< const ComponentInfo > ComponentInfoConstIt
ComponentInfoIterator< ComponentInfo > ComponentInfoIt
Kernel::Quat toQuat(const Eigen::Quaterniond &quat)
Converts Eigen::Quaterniond to Kernel::Quat.
Kernel::V2D toV2D(const Eigen::Vector2d &vec)
Converts Eigen::Vector2d to Kernel::V2D.
Eigen::Vector3d toVector3d(const Kernel::V3D &vec)
Converts Kernel::V3D to Eigen::Vector3d.
Kernel::V3D toV3D(const Eigen::Vector3d &vec)
This header provides conversion helpers between vector and rotation types in MantidKernel and equival...
Eigen::Quaterniond toQuaterniond(const Kernel::Quat &quat)
Converts Kernel::Quat to Eigen::Quaterniond.
int32_t detid_t
Typedef for a detector ID.