Mantid
Loading...
Searching...
No Matches
DetectorGridDefinition.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 +
8
9#include <cmath>
10#include <stdexcept>
11
12namespace Mantid::Algorithms {
13
23DetectorGridDefinition::DetectorGridDefinition(const double minLatitude, const double maxLatitude,
24 const size_t latitudePoints, const double minLongitude,
25 const double maxLongitude, const size_t longitudePoints)
26 : m_minLatitude(minLatitude), m_maxLatitude(maxLatitude), m_latitudePoints(latitudePoints),
27 m_minLongitude(minLongitude), m_maxLongitude(maxLongitude), m_longitudePoints(longitudePoints) {
28 // prevent pointless edge case to simplify interpolation code
29 if (latitudePoints < 2 || longitudePoints < 2 || minLatitude > maxLatitude || minLongitude > maxLongitude) {
30 throw std::runtime_error("Invalid detector grid definition.");
31 }
32 // The angular ranges might be zero in some cases preventing
33 // the spawning of a real grid. We want to avoid this.
34 const double tiny = 1e-5;
35 const double smallShift = M_PI / 300.0;
36 if (std::abs(m_minLatitude - m_maxLatitude) < tiny) {
37 m_minLatitude -= smallShift;
38 m_maxLatitude += smallShift;
39 }
40 if (std::abs(m_minLongitude - m_maxLongitude) < tiny) {
41 m_minLongitude -= smallShift;
42 m_maxLongitude += smallShift;
43 }
44 m_latitudeStep = (m_maxLatitude - m_minLatitude) / static_cast<double>(m_latitudePoints - 1);
45 m_longitudeStep = (m_maxLongitude - m_minLongitude) / static_cast<double>(m_longitudePoints - 1);
46}
47
52double DetectorGridDefinition::latitudeAt(const size_t row) const {
53 return m_minLatitude + static_cast<double>(row) * m_latitudeStep;
54}
55
60double DetectorGridDefinition::longitudeAt(const size_t column) const {
61 return m_minLongitude + static_cast<double>(column) * m_longitudeStep;
62}
63
69std::array<size_t, 4> DetectorGridDefinition::nearestNeighbourIndices(const double latitude,
70 const double longitude) const {
71 auto row = static_cast<size_t>((latitude - m_minLatitude) / m_latitudeStep);
72 // Check for points at the edges or outside the grid.
73 if (row == m_latitudePoints - 1) {
74 --row;
75 }
76 auto col = static_cast<size_t>((longitude - m_minLongitude) / m_longitudeStep);
77 if (col == m_longitudePoints - 1) {
78 --col;
79 }
80 std::array<size_t, 4> is;
81 std::get<0>(is) = col * m_latitudePoints + row;
82 std::get<1>(is) = std::get<0>(is) + 1;
83 std::get<2>(is) = std::get<0>(is) + m_latitudePoints;
84 std::get<3>(is) = std::get<2>(is) + 1;
85 return is;
86}
87
93size_t DetectorGridDefinition::getDetectorIndex(size_t row, size_t col) {
94 if ((col >= m_longitudePoints) || (row >= m_latitudePoints)) {
95 throw std::runtime_error("DetectorGridDefinition::getDetectorIndex: "
96 "detector requested for out of bounds row or col");
97 }
98 return col * m_latitudePoints + row;
99}
100
107std::pair<size_t, size_t> DetectorGridDefinition::getNearestVertex(const double latitude,
108 const double longitude) const {
109 auto topLeftRow = static_cast<size_t>((latitude - m_minLatitude) / m_latitudeStep);
110 // Check for points at the edges or outside the grid.
111 if (topLeftRow == m_latitudePoints - 1) {
112 --topLeftRow;
113 }
114 auto topLeftCol = static_cast<size_t>((longitude - m_minLongitude) / m_longitudeStep);
115 if (topLeftCol == m_longitudePoints - 1) {
116 --topLeftCol;
117 }
118 return std::pair<size_t, size_t>{topLeftRow, topLeftCol};
119}
120
125
130
131} // namespace Mantid::Algorithms
std::pair< size_t, size_t > getNearestVertex(const double latitude, const double longitude) const
Return the indices to the detector that is immediate neighbour of the supplied lat/long and has lat/l...
DetectorGridDefinition(const double minLatitude, const double maxLatitude, const size_t latitudePoints, const double minLongitude, const double maxLongitude, const size_t longitudeStep)
Initializes a DetectorGridDefinition object.
std::array< size_t, 4 > nearestNeighbourIndices(const double latitude, const double longitude) const
Return the indices to detector surrounding the given point.
double latitudeAt(const size_t row) const
Return the latitude of the given row.
size_t numberRows() const
Return the number of rows in the grid.
size_t getDetectorIndex(size_t row, size_t col)
Return the indices of the detector described by a row and col.
double longitudeAt(const size_t column) const
Return the longitude of the given column.
size_t numberColumns() const
Return the number of columns in the grid.