RayZaler 0.1
The free opto-mechanical simulation framework
Detector.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _DETECTOR_H
20#define _DETECTOR_H
21
22#include <OpticalElement.h>
23#include <vector>
24
25namespace RZ {
26 class ReferenceFrame;
27 class RotatedFrame;
28 class MediumBoundary;
29
31 std::vector<uint32_t> m_photons;
32 std::vector<Complex> m_amplitude;
33 Real m_width;
34 Real m_height;
35
36 Real m_pxWidth = 15e-6;
37 Real m_pxHeight = 15e-6;
38
39 uint32_t m_maxCounts = 0;
40 Real m_maxEnergy = 0;
41
42 unsigned int m_cols;
43 unsigned int m_rows;
44 unsigned int m_stride;
45
46 void recalculate();
47
48 public:
49 inline bool
50 hit(Real x, Real y, Complex amplitude)
51 {
52 int row, col;
53 size_t ndx;
54 Real E;
55
56 col = floor((x + .5 * m_width) / m_pxWidth);
57 row = floor((y + .5 * m_height) / m_pxHeight);
58
59 if (col < 0 || col >= m_cols || row < 0 || row >= m_rows)
60 return false;
61
62 ndx = col + row * m_stride;
63 ++m_photons[ndx];
64 m_amplitude[ndx] += amplitude;
65
66 E = (m_amplitude[ndx] * std::conj(m_amplitude[ndx])).real();
67
68 if (m_photons[ndx] > m_maxCounts)
69 m_maxCounts = m_photons[ndx];
70
71 if (E > m_maxEnergy)
72 m_maxEnergy = E;
73
74 return true;
75 }
76
77 inline uint32_t
78 maxCounts() const
79 {
80 return m_maxCounts;
81 }
82
83 inline Real
84 maxEnergy() const
85 {
86 return m_maxEnergy;
87 }
88
89 DetectorStorage(unsigned int cols, unsigned int rows, Real width, Real height);
90
91 void setPixelDimensions(Real, Real);
92 void setResolution(unsigned, unsigned);
93
94 void clear();
95 bool savePNG(std::string const &) const;
96 bool saveRawData(std::string const &) const;
97 bool saveAmplitude(std::string const &) const;
98
99 unsigned int cols() const;
100 unsigned int rows() const;
101 unsigned int stride() const;
102 const uint32_t *data() const;
103 const Complex *amplitude() const;
104 };
105
107 DetectorStorage *m_storage; // Borrowed
108
109 public:
111 virtual ~DetectorBoundary() = default;
112 virtual void cast(RayBeam &) const;
113 virtual std::string name() const;
114 };
115
116 class Detector : public OpticalElement {
117 RotatedFrame *m_detectorSurface = nullptr;
118 DetectorStorage *m_storage = nullptr; // Owned
119 MediumBoundary *m_boundary = nullptr;
120
121 Real m_pxWidth = 15e-6;
122 Real m_pxHeight = 15e-6;
123
124 Real m_width;
125 Real m_height;
126
127 bool m_flip = false;
128 unsigned int m_rows = 512;
129 unsigned int m_cols = 512;
130
131 void recalcModel();
132
133 protected:
134 virtual bool propertyChanged(std::string const &, PropertyValue const &) override;
135
136 public:
137 Detector(
139 std::string const &,
141 Element *parent = nullptr);
142 virtual ~Detector() override;
143
144 virtual void nativeMaterialOpenGL(std::string const &) override;
145 virtual void renderOpenGL() override;
146
147 void clear();
148 virtual bool savePNG(std::string const &) const;
149 virtual bool saveRawData(std::string const &) const;
150 virtual bool saveAmplitude(std::string const &) const;
151
152 unsigned int cols() const;
153 unsigned int rows() const;
154 Real pxWidth() const;
155 Real pxHeight() const;
156 Real width() const;
157 Real height() const;
158 unsigned int stride() const;
159 const uint32_t *data() const;
160 const Complex *amplitude() const;
161
162 uint32_t maxCounts() const;
163 Real maxEnergy() const;
164 };
165
166 RZ_DECLARE_OPTICAL_ELEMENT(Detector);
167}
168
169#endif // _DETECTOR_H
Definition: Detector.h:106
Definition: Detector.h:116
Definition: Detector.h:30
Definition: Element.h:393
Definition: Element.h:173
Definition: MediumBoundary.h:35
Definition: OpticalElement.h:87
Definition: Element.h:58
Definition: ReferenceFrame.h:59
Definition: RotatedFrame.h:25
Definition: RayBeam.h:88