Mantid
Loading...
Searching...
No Matches
RectangularDetector.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 +
19#include "MantidKernel/Matrix.h"
20#include <algorithm>
21#include <boost/regex.hpp>
22#include <memory>
23#include <ostream>
24#include <stdexcept>
25#include <utility>
26
27namespace {
34int getOneTextureSize(int desired) {
35 int size = 2;
36 while (desired > size) {
37 size = size * 2;
38 }
39 return size;
40}
41} // namespace
42
43namespace Mantid::Geometry {
44
45using Kernel::Matrix;
46using Kernel::V3D;
47
56
66RectangularDetector::RectangularDetector(const std::string &n, IComponent *reference) : GridDetector(n, reference) {
67 init();
68 m_textureID = 0;
69 this->setName(n);
71}
72
73bool RectangularDetector::compareName(const std::string &proposedMatch) {
74 static const boost::regex exp("RectangularDetector|rectangularDetector|"
75 "rectangulardetector|rectangular_detector");
76
77 return boost::regex_match(proposedMatch, exp);
78}
79
85
86//-------------------------------------------------------------------------------------------------
97std::shared_ptr<Detector> RectangularDetector::getAtXY(const int X, const int Y) const {
98 return GridDetector::getAtXYZ(X, Y, 0);
99}
100
101//-------------------------------------------------------------------------------------------------
113}
114
115//-------------------------------------------------------------------------------------------------
121std::pair<int, int> RectangularDetector::getXYForDetectorID(const int detectorID) const {
122 auto xyz = GridDetector::getXYZForDetectorID(detectorID);
123 return std::pair<int, int>(std::get<0>(xyz), std::get<1>(xyz));
124}
125
126V3D RectangularDetector::getPosAtXY(const int x, const int y) const { return GridDetector::getPosAtXYZ(x, y, 0); }
127
128//-------------------------------------------------------------------------------------------------
138
139void RectangularDetector::getBoundingBoxAtXY(const int x, const int y, BoundingBox &box) const {
141}
142
143bool RectangularDetector::inBoundsXY(int x, int y) const { return GridDetector::inBoundsXYZ(x, y, 0); }
144
145//-------------------------------------------------------------------------------------------------
175void RectangularDetector::initialize(std::shared_ptr<IObject> shape, int xpixels, double xstart, double xstep,
176 int ypixels, double ystart, double ystep, int idstart, bool idfillbyfirst_y,
177 int idstepbyrow, int idstep) {
178
180 idfillbyfirst_y ? "yxz" : "xyz", idstepbyrow, idstep);
181}
182
183//------------------------------------------------------------------------------------------------
194 std::deque<IComponent_const_sptr> & /*searchQueue*/) const {
196 V3D basePoint = getPosAtXY(0, 0);
197
199 V3D vertical = getPosAtXY(0, ypixels() - 1) - basePoint;
200
202 V3D horizontal = getPosAtXY(xpixels() - 1, 0) - basePoint;
203
204 // The beam direction
205 V3D beam = testRay.direction();
206
207 // From: http://en.wikipedia.org/wiki/Line-plane_intersection (taken on May 4,
208 // 2011),
209 // We build a matrix to solve the linear equation:
210 Matrix<double> mat(3, 3);
211 mat.setColumn(0, beam * -1.0);
212 mat.setColumn(1, horizontal);
213 mat.setColumn(2, vertical);
214 mat.Invert();
215
216 // Multiply by the inverted matrix to find t,u,v
217 V3D tuv = mat * (testRay.startPoint() - basePoint);
218 // std::cout << tuv << "\n";
219
220 // Intersection point
221 V3D intersec = beam;
222 intersec *= tuv[0];
223
224 // t = coordinate along the line
225 // u,v = coordinates along horizontal, vertical
226 // (and correct for it being between 0, xpixels-1). The +0.5 is because the
227 // base point is at the CENTER of pixel 0,0.
228 double u = (double(xpixels() - 1) * tuv[1] + 0.5);
229 double v = (double(ypixels() - 1) * tuv[2] + 0.5);
230
231 // std::cout << u << ", " << v << "\n";
232
233 // In indices
234 auto xIndex = int(u);
235 auto yIndex = int(v);
236
237 // Out of range?
238 if (xIndex < 0)
239 return;
240 if (yIndex < 0)
241 return;
242 if (xIndex >= xpixels())
243 return;
244 if (yIndex >= ypixels())
245 return;
246
247 // TODO: Do I need to put something smart here for the first 3 parameters?
248 auto comp = getAtXY(xIndex, yIndex);
249 testRay.addLink(intersec, intersec, 0.0, *(comp->shape()), comp->getComponentID());
250}
251
258void RectangularDetector::getTextureSize(int &xsize, int &ysize) const {
259 xsize = getOneTextureSize(this->xpixels());
260 ysize = getOneTextureSize(this->ypixels());
261}
262
265void RectangularDetector::setTextureID(unsigned int textureID) { m_textureID = textureID; }
266
268unsigned int RectangularDetector::getTextureID() const { return m_textureID; }
269
271
273 return componentVisitor.registerRectangularBank(*this);
274}
275
276//-------------------------------------------------------------------------------------------------
277//-------------------------------------------------------------------------------------------------
278// ------------ END OF IObjComponent methods ----------------
279//-------------------------------------------------------------------------------------------------
280//-------------------------------------------------------------------------------------------------
281
292std::ostream &operator<<(std::ostream &os, const RectangularDetector &ass) {
293 ass.printSelf(os);
294 os << "************************\n";
295 os << "Number of children :" << ass.nelements() << '\n';
296 ass.printChildren(os);
297 return os;
298}
299
300} // namespace Mantid::Geometry
A simple structure that defines an axis-aligned cuboid shaped bounding box for a geometrical object.
Definition BoundingBox.h:33
void printChildren(std::ostream &) const override
Print information about all children.
int nelements() const override
Return the number of elements in the assembly.
ComponentVisitor : Visitor for IComponents.
virtual size_t registerRectangularBank(const ICompAssembly &bank)=0
Handles rendering of all object Geometry.
GridDetector is a type of CompAssembly, an assembly of components.
double ystart() const
Returns the start position in the Y direction.
std::tuple< int, int, int > getXYZForDetectorID(const detid_t detectorID) const
Given a detector ID, return the X,Y,Z coords into the grid detector.
const std::shared_ptr< const IObject > shape() const override
Returns the shape of the Object.
std::shared_ptr< Detector > getAtXYZ(const int x, const int y, const int z) const
Return a pointer to the component in the assembly at the (X,Y) pixel position.
int const & ypixels() const
Returns the number of pixels in the Y direction.
Kernel::V3D getPosAtXYZ(int const x, int const y, int const z) const
Returns the position of the center of the pixel at x,y, relative to the center of the GridDetector,...
double xsize() const
Size in X of the detector.
void initialize(std::shared_ptr< IObject > shape, int xpixels, double xstart, double xstep, int ypixels, double ystart, double ystep, int zpixels, double zstart, double zstep, int idstart, const std::string &idFillOrder, int idstepbyrow, int idstep=1)
Create all the detector pixels of this grid detector.
bool const & idfillbyfirst_y() const
returns if IDs are filled along y direction first
double ystep() const
Returns the step size in the Y direction.
double xstep() const
Returns the step size in the X direction.
void init()
initialize members to bare defaults
int const & xpixels() const
Returns the number of pixels in the X direction.
int const & idstepbyrow() const
Returns the idstepbyrow.
double xstart() const
Returns the start position in the X direction.
detid_t getDetectorIDAtXYZ(const int x, const int y, const int z) const
Return the detector ID corresponding to the component in the assembly at the (X,Y) pixel position.
int const & idstart() const
Returns the idstart.
void getBoundingBoxAtXYZ(int const x, int const y, int const z, BoundingBox &box) const
bool inBoundsXYZ(const int x, const int y, const int z) const
int const & idstep() const
Returns the idstep.
Kernel::V3D getRelativePosAtXYZ(int const x, int const y, int const z) const
Returns the position of the center of the pixel at x,y, relative to the center of the GridDetector,...
base class for Geometric IComponent
Definition IComponent.h:53
virtual void printSelf(std::ostream &) const =0
Prints a text representation of itself.
virtual void setName(const std::string &)=0
Set the IComponent name.
void setGeometryHandler(GeometryHandler *h)
Reset the current geometry handler.
RectangularDetector is a type of CompAssembly, an assembly of components.
void setTextureID(unsigned int textureID)
Set the texture ID to use when rendering the RectangularDetector.
const Kernel::Material material() const override
Returns the material of the detector.
unsigned int getTextureID() const
Return the texture ID to be used in plotting .
void getTextureSize(int &xsize, int &ysize) const
Return the number of pixels to make a texture in, given the desired pixel size.
RectangularDetector()
Empty constructor.
RectangularDetector * clone() const override
Make a clone of the present component.
Kernel::V3D getPosAtXY(const int x, const int y) const
void getBoundingBoxAtXY(const int x, const int y, BoundingBox &box) const
std::pair< int, int > getXYForDetectorID(const int detectorID) const
Given a detector ID, return the X,Y coords into the rectangular detector.
unsigned int m_textureID
Texture ID to use in rendering.
detid_t getDetectorIDAtXY(const int X, const int Y) const
Return the detector ID corresponding to the component in the assembly at the (X,Y) pixel position.
std::shared_ptr< Detector > getAtXY(const int X, const int Y) const
Return a pointer to the component in the assembly at the (X,Y) pixel position.
Kernel::V3D getRelativePosAtXY(int x, int y) const
Returns the position of the center of the pixel at x,y, relative to the center of the RectangularDete...
void initialize(std::shared_ptr< IObject > shape, int xpixels, double xstart, double xstep, int ypixels, double ystart, double ystep, int idstart, bool idfillbyfirst_y, int idstepbyrow, int idstep=1)
Create all the detector pixels of this rectangular detector.
static bool compareName(const std::string &proposedMatch)
Matches name to Structured Detector.
virtual size_t registerContents(class ComponentVisitor &componentVisitor) const override
void testIntersectionWithChildren(Track &testRay, std::deque< IComponent_const_sptr > &searchQueue) const override
Test the intersection of the ray with the children of the component assembly, for InstrumentRayTracer...
Defines a track as a start point and a direction.
Definition Track.h:165
const Kernel::V3D & startPoint() const
Returns the starting point.
Definition Track.h:191
int addLink(const Kernel::V3D &firstPoint, const Kernel::V3D &secondPoint, const double distanceAlongTrack, const IObject &obj, const ComponentID compID=nullptr)
Adds a link to the track.
Definition Track.cpp:152
const Kernel::V3D & direction() const
Returns the direction as a unit vector.
Definition Track.h:193
A material is defined as being composed of a given element, defined as a PhysicalConstants::NeutronAt...
Definition Material.h:50
Numerical Matrix class.
Definition Matrix.h:42
T Invert()
LU inversion routine.
Definition Matrix.cpp:924
void setColumn(const size_t nCol, const std::vector< T > &newCol)
Definition Matrix.cpp:675
Class for 3D vectors.
Definition V3D.h:34
MANTID_GEOMETRY_DLL std::ostream & operator<<(std::ostream &stream, const PointGroup &self)
Returns a streamed representation of the PointGroup object.
int32_t detid_t
Typedef for a detector ID.