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
53 : GridDetector(base, map) {
54 init();
55}
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
126//-------------------------------------------------------------------------------------------------
136
137//-------------------------------------------------------------------------------------------------
167void RectangularDetector::initialize(std::shared_ptr<IObject> shape, int xpixels, double xstart, double xstep,
168 int ypixels, double ystart, double ystep, int idstart, bool idfillbyfirst_y,
169 int idstepbyrow, int idstep) {
170
172 idfillbyfirst_y ? "yxz" : "xyz", idstepbyrow, idstep);
173}
174
175//------------------------------------------------------------------------------------------------
186 std::deque<IComponent_const_sptr> & /*searchQueue*/) const {
188 V3D basePoint;
189
191 V3D vertical;
192
194 V3D horizontal;
195
196 basePoint = getAtXY(0, 0)->getPos();
197 horizontal = getAtXY(xpixels() - 1, 0)->getPos() - basePoint;
198 vertical = getAtXY(0, ypixels() - 1)->getPos() - basePoint;
199
200 // The beam direction
201 V3D beam = testRay.direction();
202
203 // From: http://en.wikipedia.org/wiki/Line-plane_intersection (taken on May 4,
204 // 2011),
205 // We build a matrix to solve the linear equation:
206 Matrix<double> mat(3, 3);
207 mat.setColumn(0, beam * -1.0);
208 mat.setColumn(1, horizontal);
209 mat.setColumn(2, vertical);
210 mat.Invert();
211
212 // Multiply by the inverted matrix to find t,u,v
213 V3D tuv = mat * (testRay.startPoint() - basePoint);
214 // std::cout << tuv << "\n";
215
216 // Intersection point
217 V3D intersec = beam;
218 intersec *= tuv[0];
219
220 // t = coordinate along the line
221 // u,v = coordinates along horizontal, vertical
222 // (and correct for it being between 0, xpixels-1). The +0.5 is because the
223 // base point is at the CENTER of pixel 0,0.
224 double u = (double(xpixels() - 1) * tuv[1] + 0.5);
225 double v = (double(ypixels() - 1) * tuv[2] + 0.5);
226
227 // std::cout << u << ", " << v << "\n";
228
229 // In indices
230 auto xIndex = int(u);
231 auto yIndex = int(v);
232
233 // Out of range?
234 if (xIndex < 0)
235 return;
236 if (yIndex < 0)
237 return;
238 if (xIndex >= xpixels())
239 return;
240 if (yIndex >= ypixels())
241 return;
242
243 // TODO: Do I need to put something smart here for the first 3 parameters?
244 auto comp = getAtXY(xIndex, yIndex);
245 testRay.addLink(intersec, intersec, 0.0, *(comp->shape()), comp->getComponentID());
246}
247
254void RectangularDetector::getTextureSize(int &xsize, int &ysize) const {
255 xsize = getOneTextureSize(this->xpixels());
256 ysize = getOneTextureSize(this->ypixels());
257}
258
261void RectangularDetector::setTextureID(unsigned int textureID) { m_textureID = textureID; }
262
264unsigned int RectangularDetector::getTextureID() const { return m_textureID; }
265
267
269 return componentVisitor.registerRectangularBank(*this);
270}
271
272//-------------------------------------------------------------------------------------------------
273//-------------------------------------------------------------------------------------------------
274// ------------ END OF IObjComponent methods ----------------
275//-------------------------------------------------------------------------------------------------
276//-------------------------------------------------------------------------------------------------
277
288std::ostream &operator<<(std::ostream &os, const RectangularDetector &ass) {
289 ass.printSelf(os);
290 os << "************************\n";
291 os << "Number of children :" << ass.nelements() << '\n';
292 ass.printChildren(os);
293 return os;
294}
295
296} // namespace Mantid::Geometry
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.
Definition: GridDetector.h:34
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.
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 idfillbyfirst_y() const
Returns the idfillbyfirst_y.
int xpixels() const
Returns the number of pixels in the X direction.
int idstep() const
Returns the idstep.
int idstart() const
Returns the idstart.
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
Kernel::V3D getRelativePosAtXYZ(int x, int y, int z) const
Returns the position of the center of the pixel at x,y, relative to the center of the GridDetector,...
int ypixels() const
Returns the number of pixels in the Y direction.
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 idstepbyrow() const
Returns the idstepbyrow.
base class for Geometric IComponent
Definition: IComponent.h:51
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.
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.
Definition: PointGroup.cpp:312
int32_t detid_t
Typedef for a detector ID.
Definition: SpectrumInfo.h:21