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//-------------------------------------------------------------
14#include "MantidKernel/V3D.h"
15#include <deque>
16#include <iterator>
17#include <utility>
18
19namespace Mantid::Geometry {
20
21using Kernel::V3D;
22
23//-------------------------------------------------------------
24// Public member functions
25//-------------------------------------------------------------
26
34InstrumentRayTracer::InstrumentRayTracer(Instrument_const_sptr instrument) : m_instrument(std::move(instrument)) {
35 if (!m_instrument) {
36 std::ostringstream lexer;
37 lexer << "Cannot create a InstrumentRayTracer, invalid instrument given. "
38 "Input = "
39 << m_instrument.get() << "\n";
40 throw std::invalid_argument(lexer.str());
41 }
42 if (!m_instrument->getSource()) {
43 std::string errorMsg = "Cannot create InstrumentRayTracer, instrument has "
44 "no defined source.\n";
45 throw std::invalid_argument(errorMsg);
46 }
47}
48
57void InstrumentRayTracer::trace(const V3D &dir) const {
58 // Define the track with the source position and the given direction.
59 m_resultsTrack.reset(m_instrument->getSource()->getPos(), dir);
60 // m_resultsTrack.reset(m_instrument->getSample()->getPos() +
61 // V3D(1.0,0.0,0.0), dir);
62 // The intersection results are accumulated within the ray object
64}
65
75 // Define the track with the sample position and the given direction.
76 m_resultsTrack.reset(m_instrument->getSample()->getPos(), dir);
77 // The intersection results are accumulated within the ray object
79}
80
90
91//----------------------------------------------------------------------------------
97 Links results = this->getResults();
98
99 // Go through all results
100 Links::const_iterator resultItr = results.begin();
101 for (; resultItr != results.end(); ++resultItr) {
102 IComponent_const_sptr component = m_instrument->getComponentByID(resultItr->componentID);
103 IDetector_const_sptr det = std::dynamic_pointer_cast<const IDetector>(component);
104 if (det) {
105 if (!m_instrument->isMonitor(det->getID())) {
106 return det;
107 }
108 } // (is a detector)
109 } // each ray tracer result
110 return IDetector_const_sptr();
111}
112
113//-------------------------------------------------------------
114// Private member functions
115//-------------------------------------------------------------
124 // Go through the instrument tree and see if we get any hits by
125 // (a) first testing the bounding box and if we're inside that then
126 // (b) test the lower components.
127 std::deque<IComponent_const_sptr> nodeQueue;
128
129 // Start at the root of the tree
130 nodeQueue.emplace_back(m_instrument);
131
133 while (!nodeQueue.empty()) {
134 node = nodeQueue.front();
135 nodeQueue.pop_front();
136 BoundingBox bbox;
137 auto it = m_boxCache.find(node->getComponentID());
138 if (it != m_boxCache.end()) {
139 bbox = it->second;
140 } else {
141 node->getBoundingBox(bbox);
142 std::lock_guard<std::mutex> lock(m_mutex);
143 m_boxCache[node->getComponentID()] = bbox;
144 }
145
146 // Quick test. If this suceeds moved on to test the children
147 if (bbox.doesLineIntersect(testRay)) {
148 if (ICompAssembly_const_sptr assembly = std::dynamic_pointer_cast<const ICompAssembly>(node)) {
149 assembly->testIntersectionWithChildren(testRay, nodeQueue);
150 } else {
151 throw Kernel::Exception::NotImplementedError("Implement non-comp assembly interactions");
152 }
153 }
154 }
155}
156
158// * Perform a quick check as to whether the ray passes through the component
159// * @param component :: The test component
160// */
161// bool InstrumentRayTracer::quickIntersectCheck(std::shared_ptr<IComponent>
162// component, const Track & testRay) const
163// {
164//
165// }
166//
167// /**
168// * Perform a proper intersection test of the physical object and accumulate
169// the results if necessary
170// * @param testRay :: An input/output parameter that defines the track and
171// accumulates the
172// * intersection results
173// */
174// void slowIntersectCheck(std::shared_ptr<IComponent> component, Track &
175// testRay) const;
176} // namespace Mantid::Geometry
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.
Links getResults() const
Get the results of the intersection tests that have been updated since the previous call to trace.
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...
void traceFromSample(const Kernel::V3D &dir) const
Trace a given track from the sample position in the given direction.
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 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.
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:167
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.