RayZaler 0.1
The free opto-mechanical simulation framework
OpticalElement.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 _OPTICAL_ELEMENT_H
20#define _OPTICAL_ELEMENT_H
21
22#include "Element.h"
23#include <list>
24#include <map>
25#include <string>
26#include <RayTracingEngine.h>
27
28namespace RZ {
29 class MediumBoundary;
30 class OpticalElement;
31
32
34 std::string name;
35 const ReferenceFrame *frame = nullptr;
36 const MediumBoundary *boundary = nullptr;
37 OpticalElement *parent = nullptr;
38
39 std::map<uint32_t, RayBeamStatistics> statistics;
40
41 mutable std::vector<Ray> hits;
42
43 // Haha C++
44 mutable std::vector<Real> locationArray;
45 mutable std::vector<Real> directionArray;
46
47 std::vector<Real> &locations() const;
48 std::vector<Real> &directions() const;
49
50 void clearCache() const;
51 void clearStatistics();
52 };
53
54 struct OpticalPath {
55 std::list<const OpticalSurface *> m_sequence;
56 std::map<std::string, const OpticalSurface *> m_nameToSurface;
57
58 OpticalPath &plug(OpticalElement *, std::string const &name = "");
59 void push(const OpticalSurface *);
60
61 const std::vector<Real> &hits(std::string const &name) const;
62 const std::vector<Real> &directions(std::string const &name) const;
63
64 inline const OpticalSurface *
65 getSurface(std::string const &name) const
66 {
67 auto it = m_nameToSurface.find(name);
68
69 if (it == m_nameToSurface.cend())
70 return nullptr;
71
72 return it->second;
73 }
74
75 inline std::list<std::string>
76 surfaces() const
77 {
78 std::list<std::string> list;
79
80 for (auto &p : m_nameToSurface)
81 list.push_back(p.first);
82
83 return list;
84 }
85 };
86
87 class OpticalElement : public Element {
88 using Element::Element;
89 std::list<OpticalSurface> m_surfaces; // Owned
90 std::map<std::string, OpticalSurface *> m_nameToSurf; // Borrowed
91 std::list<ReferenceFrame *> m_surfaceFrames; // Owned
92 OpticalPath m_internalPath;
93 bool m_recordHits = false;
94
95 protected:
96 void pushOpticalSurface(
97 std::string,
99 const MediumBoundary *);
100
101 void defineOpticalSurface(
102 std::string,
104 const MediumBoundary *);
105
108 std::string const &,
110 Element *parent = nullptr);
111
112 public:
113 inline bool
114 recordHits() const
115 {
116 return m_recordHits;
117 }
118
119 static inline OpticalElement *
120 fromElement(Element *element)
121 {
122 if (element->hasProperty("optical"))
123 return static_cast<OpticalElement *>(element);
124
125 return nullptr;
126 }
127
128 virtual OpticalPath opticalPath(std::string const &name = "") const;
129 OpticalPath plug(OpticalElement *, std::string const &name = "") const;
130
131 const std::list<const OpticalSurface *> &opticalSurfaces() const;
132 std::list<OpticalSurface *> opticalSurfaces();
133 std::list<std::string> surfaceNames() const;
134
135 OpticalSurface *lookupSurface(std::string const &);
136
137 const std::vector<Real> &hits(std::string const &name = "") const;
138 const std::vector<Real> &directions(std::string const &name = "") const;
139
140 virtual void setRecordHits(bool);
141 virtual void clearHits();
142
143 virtual ~OpticalElement() override;
144 };
145
146 RZ_DECLARE_ABSTRACT_ELEMENT(OpticalElement);
147}
148
149#endif // _OPTICAL_ELEMENT_H
Definition: Element.h:393
Definition: Element.h:173
Definition: MediumBoundary.h:35
Definition: OpticalElement.h:87
Definition: ReferenceFrame.h:59
Definition: OpticalElement.h:54
Definition: OpticalElement.h:33