RayZaler 0.1
The free opto-mechanical simulation framework
RayTracingEngine.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 _RAYTRACING_ENGINE_H
20#define _RAYTRACING_ENGINE_H
21
22#include <sys/time.h>
23#include <cassert>
24
25#include "RayBeam.h"
26
27namespace RZ {
28 class ReferenceFrame;
29 class OpticalSurface;
30
31 enum RayTracingStageProgressType {
32 PROGRESS_TYPE_TRACE, // Tracing rays to capture surface
33 PROGRESS_TYPE_TRANSFER, // Transfering exit rays
34 PROGRESS_TYPE_KIRCHHOFF, // Integrating wavefronts
35 PROGRESS_TYPE_CONFIG, // Reconfigure model
36 };
37
39 public:
40 virtual void stageProgress(
41 RayTracingStageProgressType,
42 std::string const &,
43 unsigned int num,
44 unsigned int total);
45 virtual void rayProgress(uint64_t num, uint64_t total);
46
47 virtual uint64_t rayNotifyInterval() const;
48 virtual bool cancelled() const;
49 };
50
52 std::list<Ray> m_rays;
53 bool m_raysDirty = false;
54
55 RayBeam *m_beam = nullptr;
56 bool m_beamDirty = true;
57 bool m_notificationPendig = false;
58
59 std::string m_stageName;
60 size_t m_currStage = 0;
61 size_t m_numStages = 0;
62
63 void toBeam(); // From m_rays to beam->origins and beam->directions
64 void toRays(bool keepPruned = false); // From beam->destinations and beam->directions to rays
65
66 RayTracingProcessListener *m_listener = nullptr; // Always borrowed
67
68 struct timeval m_start;
69
70 protected:
71 virtual void cast(const OpticalSurface *, RayBeam *) = 0;
72 virtual void transmit(const OpticalSurface *, RayBeam *) = 0;
73
74 void rayProgress(uint64_t num, uint64_t total);
75
76 public:
77 inline RayBeam *
78 beam() const
79 {
80 return m_beam;
81 }
82
83 inline void
84 setCurrentStage(std::string const &name, size_t current, size_t num)
85 {
86 m_stageName = name;
87 m_currStage = current;
88 m_numStages = num;
89 }
90
91 virtual RayBeam *makeBeam(bool nonSeq = false);
92 RayBeam *ensureMainBeam();
93
95 virtual ~RayTracingEngine();
96
97 // This sets the event listener, to receive periodic updates
98 RayTracingProcessListener *listener() const;
99 void setListener(RayTracingProcessListener *);
100
101 // This clears all the rays
102 void clear();
103
104 // This sets a beam that was initialized from somewhere else
105 void setMainBeam(RayBeam *);
106
107 // This adds a new ray to the list
108 void pushRay(
109 Point3 const &origin,
110 Vec3 const &direction,
111 Real length = 0,
112 uint32_t id = 0);
113 void pushRays(std::list<Ray> const &);
114
115 // Intersect with this surface. It needs to check if the beam is up to
116 // date, and recreated it with toBeam() if necessary.
117 // Then, it will call to cast() and compute beam->destinations and lengths
118 void castTo(const OpticalSurface *, RayBeam *beam = nullptr);
119
120 // Refresh ray origins
121 void updateOrigins();
122
123 // Clear m_ray, process the beam and extract unpruned rays
124 void transmitThrough(const OpticalSurface *surface);
125 void transmitThroughIntercepted(); // Equivalent to transmitThrough(nullptr)
126
127 // Clear m_ray, process the beam, set random targets
128 // Return the output rays, after transfer
129 std::list<Ray> const &getRays(bool keepPruned = false);
130
131 // Mark start of elapsed time counter
132 void tick();
133
134 // Get elapsed time
135 void setStartTime(struct timeval const &tv);
136 struct timeval lastTick() const;
137
138 uint64_t tack() const;
139
140 // Get if global progress notifications are pending
141 bool notificationPending() const;
142 void clearPendingNotifications();
143 bool cancelled() const;
144
145 void stageProgress(
146 RayTracingStageProgressType,
147 std::string const &,
148 unsigned int num,
149 unsigned int total);
150
151 };
152}
153
154#endif // _RAYTRACING_ENGINE_H
155
Definition: RayTracingEngine.h:51
Definition: RayTracingEngine.h:38
Definition: OpticalElement.h:33
Definition: RayBeam.h:88
Definition: Vector.h:66