Mantid
Loading...
Searching...
No Matches
InstrumentRayTracer.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 +
7//-------------------------------------------------------------
8// Includes
9//-------------------------------------------------------------
18#include "MantidKernel/V3D.h"
19#include <deque>
20#include <iterator>
21#include <utility>
22
23namespace Mantid::Geometry {
24
25using Kernel::V3D;
26
27//-------------------------------------------------------------
28// Public member functions
29//-------------------------------------------------------------
30
38InstrumentRayTracer::InstrumentRayTracer(Instrument_const_sptr instrument) : m_instrument(std::move(instrument)) {
39 if (!m_instrument) {
40 std::ostringstream lexer;
41 lexer << "Cannot create a InstrumentRayTracer, invalid instrument given. "
42 "Input = "
43 << m_instrument.get() << "\n";
44 throw std::invalid_argument(lexer.str());
45 }
46 if (!m_instrument->getSource()) {
47 std::string errorMsg = "Cannot create InstrumentRayTracer, instrument has "
48 "no defined source.\n";
49 throw std::invalid_argument(errorMsg);
50 }
51}
52
54
63void InstrumentRayTracer::trace(const V3D &dir) const {
64 // Define the track with the source position and the given direction.
65 m_resultsTrack.reset(m_instrument->getSource()->getPos(), dir);
66 // m_resultsTrack.reset(m_instrument->getSample()->getPos() +
67 // V3D(1.0,0.0,0.0), dir);
68 // The intersection results are accumulated within the ray object
70}
71
81 // Define the track with the sample position and the given direction.
82 m_resultsTrack.reset(m_instrument->getSample()->getPos(), dir);
83 // The intersection results are accumulated within the ray object
85}
86
96
97//----------------------------------------------------------------------------------
103 Links results = this->getResults();
104 const auto &compInfo = componentInfo();
105 const auto &detInfo = detectorInfo();
106
107 // Go through all results
108 Links::const_iterator resultItr = results.begin();
109 for (; resultItr != results.end(); ++resultItr) {
110 const size_t index = compInfo.indexOf(resultItr->componentID);
111 if (compInfo.isDetector(index) && !detInfo.isMonitor(index)) {
112 return m_instrument->getDetector(detInfo.detid(index));
113 } // (is a detector)
114 } // each ray tracer result
115 return IDetector_const_sptr();
116}
117
118//-------------------------------------------------------------
119// Private member functions
120//-------------------------------------------------------------
121
129
131 if (m_instrument->isParametrized())
132 return m_instrument->getParameterMap()->componentInfo();
134 return *m_ownedComponentInfo;
135}
136
138 if (m_instrument->isParametrized())
139 return m_instrument->getParameterMap()->detectorInfo();
141 return *m_ownedDetectorInfo;
142}
143
152 // Go through the instrument tree and see if we get any hits by
153 // (a) first testing the bounding box and if we're inside that then
154 // (b) test the lower components.
155 std::deque<IComponent_const_sptr> nodeQueue;
156
157 // Start at the root of the tree
158 nodeQueue.emplace_back(m_instrument);
159
161 while (!nodeQueue.empty()) {
162 node = nodeQueue.front();
163 nodeQueue.pop_front();
164 BoundingBox bbox;
165 auto it = m_boxCache.find(node->getComponentID());
166 if (it != m_boxCache.end()) {
167 bbox = it->second;
168 } else {
169 node->getBoundingBox(bbox);
170 std::lock_guard<std::mutex> lock(m_mutex);
171 m_boxCache[node->getComponentID()] = bbox;
172 }
173
174 // Quick test. If this suceeds moved on to test the children
175 if (bbox.doesLineIntersect(testRay)) {
176 if (ICompAssembly_const_sptr assembly = std::dynamic_pointer_cast<const ICompAssembly>(node)) {
177 assembly->testIntersectionWithChildren(testRay, nodeQueue);
178 } else {
179 throw Kernel::Exception::NotImplementedError("Implement non-comp assembly interactions");
180 }
181 }
182 }
183}
184
186// * Perform a quick check as to whether the ray passes through the component
187// * @param component :: The test component
188// */
189// bool InstrumentRayTracer::quickIntersectCheck(std::shared_ptr<IComponent>
190// component, const Track & testRay) const
191// {
192//
193// }
194//
195// /**
196// * Perform a proper intersection test of the physical object and accumulate
197// the results if necessary
198// * @param testRay :: An input/output parameter that defines the track and
199// accumulates the
200// * intersection results
201// */
202// void slowIntersectCheck(std::shared_ptr<IComponent> component, Track &
203// testRay) const;
204} // namespace Mantid::Geometry
std::map< DeltaEMode::Type, std::string > index
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition BoundingBox.h:33
bool doesLineIntersect(const Track &track) const
Does a specified track intersect the bounding box.
ComponentInfo : Provides a component centric view on to the instrument.
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
const ComponentInfo & componentInfo() const
Links getResults() const
Get the results of the intersection tests that have been updated since the previous call to trace.
const DetectorInfo & detectorInfo() const
Track m_resultsTrack
Accumulate results in this Track object, aids performance.
Instrument_const_sptr m_instrument
Pointer to the instrument.
IDetector_const_sptr getDetectorResult() const
Gets the results of the trace, then returns the first detector (that is NOT a monitor) found in the r...
std::unique_ptr< DetectorInfo > m_ownedDetectorInfo
void traceFromSample(const Kernel::V3D &dir) const
Trace a given track from the sample position in the given direction.
std::unique_ptr< ComponentInfo > m_ownedComponentInfo
boost::unordered_map< IComponent *, BoundingBox > m_boxCache
Map of component id -> bounding box.
InstrumentRayTracer()
Default constructor.
std::mutex m_mutex
Mutex to lock box cache.
void ensureOwnedInfoIsBuilt() const
ComponentInfo/DetectorInfo for m_instrument, built and owned here only if m_instrument is not paramet...
void trace(const Kernel::V3D &dir) const
Trace a given track from the instrument source in the given direction and compile a list of results t...
void fireRay(Track &testRay) const
Fire the given track at the instrument.
std::pair< std::unique_ptr< ComponentInfo >, std::unique_ptr< DetectorInfo > > makeWrappers() const
Defines a track as a start point and a direction.
Definition Track.h:165
void clearIntersectionResults()
Clear the current set of intersection results.
Definition Track.cpp:55
LType::const_iterator cbegin() const
Returns an interator to the start of the set of links (const version)
Definition Track.h:206
LType::const_iterator cend() const
Returns an interator to one-past-the-end of the set of links (const version)
Definition Track.h:209
void reset(const Kernel::V3D &startPoint, const Kernel::V3D &direction)
Set a starting point and direction.
Definition Track.cpp:45
Marks code as not implemented yet.
Definition Exception.h:138
Class for 3D vectors.
Definition V3D.h:34
std::shared_ptr< const ICompAssembly > ICompAssembly_const_sptr
Shared pointer to a const ICompAssembly.
std::shared_ptr< const IComponent > IComponent_const_sptr
Typdef of a shared pointer to a const IComponent.
Definition IComponent.h:165
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
Definition IDetector.h:102
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
Track::LType Links
Typedef for object intersections.
STL namespace.