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