Mantid
Loading...
Searching...
No Matches
ComponentInfo.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidBeamline/ComponentType.h"
12#include "MantidKernel/Quat.h"
13#include "MantidKernel/V3D.h"
16#include "MantidPythonInterface/geometry/ComponentInfoPythonIterator.h"
17
18#include <boost/python/class.hpp>
19#include <boost/python/copy_const_reference.hpp>
20#include <boost/python/dict.hpp>
21#include <boost/python/enum.hpp>
22#include <boost/python/reference_existing_object.hpp>
23#include <boost/python/return_value_policy.hpp>
24
25using Mantid::Beamline::ComponentType;
30using Mantid::PythonInterface::ComponentInfoPythonIterator;
33using namespace boost::python;
34
35namespace {
36ComponentInfoPythonIterator make_pyiterator(ComponentInfo &componentInfo) {
37 return ComponentInfoPythonIterator(componentInfo);
38}
39
40// Accept a bare observer position so this reads as a drop-in for the legacy
41// IDetector::solidAngle(observer), constructing SolidAngleParams internally.
42double solidAngle(const ComponentInfo &self, const size_t index, const V3D &observer) {
43 return self.solidAngle(index, SolidAngleParams(observer));
44}
45
46dict shapeToComponentIndices(const ComponentInfo &componentInfo) {
47 dict result;
48 const auto shapeMap = componentInfo.shapeToComponentIndices();
49 for (const auto &shape : shapeMap) {
50 const auto csgObject = std::dynamic_pointer_cast<const Mantid::Geometry::CSGObject>(shape.first);
51 if (csgObject != nullptr) {
52 result[csgObject->getShapeXML()] = shape.second;
53 }
54 }
55 return result;
56}
57
58} // namespace
59
60// Function pointers to help resolve ambiguity
62
64
65void (ComponentInfo::*setPosition)(const size_t, const Mantid::Kernel::V3D &) = &ComponentInfo::setPosition;
66
67void (ComponentInfo::*setRotation)(const size_t, const Mantid::Kernel::Quat &) = &ComponentInfo::setRotation;
68
69// Export ComponentInfo
71 enum_<ComponentType>("ComponentType")
72 .value("Generic", ComponentType::Generic)
73 .value("Infinite", ComponentType::Infinite)
74 .value("Grid", ComponentType::Grid)
75 .value("Rectangular", ComponentType::Rectangular)
76 .value("Structured", ComponentType::Structured)
77 .value("Unstructured", ComponentType::Unstructured)
78 .value("Detector", ComponentType::Detector)
79 .value("OutlineComposite", ComponentType::OutlineComposite);
80
81 class_<ComponentInfo, boost::noncopyable>("ComponentInfo", no_init)
82
83 .def("__iter__", make_pyiterator)
84
85 .def("__len__", &ComponentInfo::size, arg("self"), "Returns the number of components.")
86
87 .def("size", &ComponentInfo::size, arg("self"), "Returns the number of components.")
88
89 .def("isDetector", &ComponentInfo::isDetector, (arg("self"), arg("index")),
90 "Checks if the component is a detector.")
91
92 .def("detectorsInSubtree", &ComponentInfo::detectorsInSubtree, return_value_policy<VectorToNumpy>(),
93 (arg("self"), arg("index")),
94 "Returns a list of detectors in the subtree for the component "
95 "identified by 'index'.")
96
97 .def("componentsInSubtree", &ComponentInfo::componentsInSubtree, return_value_policy<VectorToNumpy>(),
98 (arg("self"), arg("index")),
99 "Returns a list of components in the subtree for the component "
100 "identified by 'index'.")
101
102 .def("position", position, (arg("self"), arg("index")),
103 "Returns the absolute position of the component identified by "
104 "'index'.")
105
106 .def("rotation", rotation, (arg("self"), arg("index")),
107 "Returns the absolute rotation of the component identified by "
108 "'index'.")
109
110 .def("relativePosition", &ComponentInfo::relativePosition, (arg("self"), arg("index")),
111 "Returns the absolute relative position of the component identified "
112 "by 'index'.")
113
114 .def("relativeRotation", &ComponentInfo::relativeRotation, (arg("self"), arg("index")),
115 "Returns the absolute relative rotation of the component identified "
116 "by 'index'.")
117
118 .def("setPosition", setPosition, (arg("self"), arg("index"), arg("newPosition")),
119 "Set the absolute position of the component identified by 'index'.")
120
121 .def("setRotation", setRotation, (arg("self"), arg("index"), arg("newRotation")),
122 "Set the absolute rotation of the component identified by 'index'.")
123
124 .def("hasSource", &ComponentInfo::hasSource, arg("self"), "Returns True if a source is present.")
125
126 .def("hasEquivalentSource", &ComponentInfo::hasEquivalentSource, arg("self"), arg("other"),
127 "Returns True is both beamlines either lack a Source or "
128 "have a Source at the same position.")
129
130 .def("hasSample", &ComponentInfo::hasSample, arg("self"), "Returns True if a sample is present.")
131
132 .def("hasEquivalentSample", &ComponentInfo::hasEquivalentSample, arg("self"), arg("other"),
133 "Returns True is both beamlines either lack a Sample or "
134 "have a Sample at the same position.")
135
136 .def("source", &ComponentInfo::source, arg("self"), "Returns the source component index.")
137
138 .def("sample", &ComponentInfo::sample, arg("self"), "Returns the sample component index.")
139
140 .def("sourcePosition", &ComponentInfo::sourcePosition, arg("self"), "Returns the source position.")
141
142 .def("samplePosition", &ComponentInfo::samplePosition, arg("self"), "Returns the sample position.")
143
144 .def("hasParent", &ComponentInfo::hasParent, (arg("self"), arg("index")),
145 "Returns True only if the component identified by 'index' has a "
146 "parent component.")
147
148 .def("parent", &ComponentInfo::parent, (arg("self"), arg("index")),
149 "Returns the parent component of the component identified by "
150 "'index'.")
151
152 .def("children", &ComponentInfo::children, (arg("self"), arg("index")),
153 return_value_policy<VectorRefToNumpy<WrapReadOnly>>(),
154 "Returns a list of child components for the component identified by "
155 "'index'.")
156
157 .def("name", &ComponentInfo::name, (arg("self"), arg("index")), return_value_policy<copy_const_reference>(),
158 "Returns the name of the component identified by 'index'.")
159
160 .def("l1", &ComponentInfo::l1, arg("self"), "Returns the l1 value.")
161
162 .def("scaleFactor", &ComponentInfo::scaleFactor, (arg("self"), arg("index")),
163 "Returns the scale factor for the component identified by 'index'.")
164
165 .def("setScaleFactor", &ComponentInfo::setScaleFactor, (arg("self"), arg("index"), arg("scaleFactor")),
166 "Set the scale factor of the component identifed by 'index'.")
167
168 .def("hasValidShape", &ComponentInfo::hasValidShape, (arg("self"), arg("index")),
169 "Returns True if the component identified by 'index' has a valid "
170 "shape.")
171
172 .def("shape", &ComponentInfo::shape, (arg("self"), arg("index")),
173 return_value_policy<reference_existing_object>(),
174 "Returns the shape of the component identified by 'index'.")
175
176 .def("solidAngle", &solidAngle, (arg("self"), arg("index"), arg("observer")),
177 "Returns the solid angle of the component identified by 'index' as "
178 "seen from the observer position.")
179
180 .def("componentType", &ComponentInfo::componentType, (arg("self"), arg("index")),
181 "Returns the ComponentType of the component identified by 'index'.")
182
183 .def("indexOfAny", &ComponentInfo::indexOfAny, (arg("self"), arg("name")),
184 "Returns the index of any component matching name. Raises "
185 "ValueError if name not found")
186
187 .def("uniqueName", &ComponentInfo::uniqueName, (arg("self"), arg("name")),
188 "Returns True if the name is a unique single occurance. Zero occurances yields False.")
189
190 .def("root", &ComponentInfo::root, arg("self"), "Returns the index of the root component")
191 .def("getMemorySize", &ComponentInfo::getMemorySize, arg("self"),
192 "Return the memory footprint of the component info in bytes.")
193 .def("shapeToComponentIndices", &shapeToComponentIndices, arg("self"),
194 "Returns a mapping of shapes to the indices of components with that shape.");
195}
std::map< DeltaEMode::Type, std::string > index
SpectrumInfoPythonIterator make_pyiterator(SpectrumInfo &spectrumInfo)
void export_ComponentInfo()
Mantid::Kernel::Quat(ComponentInfo::* rotation)(const size_t) const
void(ComponentInfo::* setPosition)(const size_t, const Mantid::Kernel::V3D &)
void(ComponentInfo::* setRotation)(const size_t, const Mantid::Kernel::Quat &)
Mantid::Kernel::V3D(ComponentInfo::* position)(const size_t) const
ComponentInfo : Provides a component centric view on to the instrument.
bool hasParent(const size_t componentIndex) const
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
bool hasEquivalentSource(const ComponentInfo &other) const
double solidAngle(const size_t componentIndex, const Geometry::SolidAngleParams &params) const
size_t parent(const size_t componentIndex) const
const std::vector< size_t > & children(size_t componentIndex) const
std::vector< size_t > componentsInSubtree(size_t componentIndex) const
bool hasValidShape(const size_t componentIndex) const
bool hasEquivalentSample(const ComponentInfo &other) const
void setScaleFactor(const size_t componentIndex, const Kernel::V3D &scaleFactor)
std::vector< size_t > detectorsInSubtree(size_t componentIndex) const
bool isDetector(const size_t componentIndex) const
bool uniqueName(const std::string &name) const
Kernel::V3D relativePosition(const size_t componentIndex) 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
const Geometry::IObject & shape(const size_t componentIndex) const
Beamline::ComponentType componentType(const size_t componentIndex) const
Kernel::V3D scaleFactor(const size_t componentIndex) const
Class for quaternions.
Definition Quat.h:39
Class for 3D vectors.
Definition V3D.h:34
Helper class which provides the Collimation Length for SANS instruments.
Implements a return value policy that returns a numpy array from a rerence to a std::vector.