-
Notifications
You must be signed in to change notification settings - Fork 31
/
Raytracer.hh
223 lines (193 loc) · 6.19 KB
/
Raytracer.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file geocel/rasterize/Raytracer.hh
//---------------------------------------------------------------------------//
#pragma once
#include "corecel/math/Algorithms.hh"
#include "ImageLineView.hh"
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Trace each pixel along a line.
*
* \tparam GTV Geometry Track View
* \tparam F Calculate the pixel value (result_type) given GTV
*/
template<class GTV, class F>
class Raytracer
{
public:
//!@{
//! \name Type aliases
using result_type = int;
//!@}
public:
// Construct from geo track view, function to calculate ID, and image
inline CELER_FUNCTION
Raytracer(GTV&& geo, F&& calc_id, ImageLineView const& image);
// Calculate the value for the next pixel
inline CELER_FUNCTION result_type operator()(size_type pix);
private:
//// DATA ////
GTV geo_;
F calc_id_;
ImageLineView const& image_;
size_type pixel_{invalid_pixel()}; //!< Current pixel
real_type distance_{0}; //!< Distance to next boundary
int cur_id_{-1}; //!< Current ID
//// HELPER FUNCTIONS ////
// Initialize the geometry at this pixel index
inline CELER_FUNCTION void initialize_at_pixel(size_type pix);
// Find the next step
inline CELER_FUNCTION void find_next_step();
//! Sentinel value for needing the pixel to be reset
static CELER_CONSTEXPR_FUNCTION size_type invalid_pixel()
{
return static_cast<size_type>(-1);
}
//! Stop tracking the pixel if this many crossings occur
static CELER_CONSTEXPR_FUNCTION short int max_crossings_per_pixel()
{
return 32;
}
};
//---------------------------------------------------------------------------//
// DEDUCTION GUIDES
//---------------------------------------------------------------------------//
template<class GTV, class F>
CELER_FUNCTION Raytracer(GTV&&, F&&, ImageLineView const&)
-> Raytracer<GTV, F>;
//---------------------------------------------------------------------------//
// INLINE DEFINITIONS
//---------------------------------------------------------------------------//
/*!
* Construct from geo track view, function to calculate ID, and image.
*/
template<class GTV, class F>
CELER_FUNCTION
Raytracer<GTV, F>::Raytracer(GTV&& geo, F&& calc_id, ImageLineView const& image)
: geo_{celeritas::forward<GTV>(geo)}
, calc_id_{celeritas::forward<F>(calc_id)}
, image_{image}
{
}
//---------------------------------------------------------------------------//
/*!
* Calculate the value for a single pixel.
*/
template<class GTV, class F>
CELER_FUNCTION auto Raytracer<GTV, F>::operator()(size_type pix) -> result_type
{
if (pix != pixel_)
{
this->initialize_at_pixel(pix);
if (pixel_ == invalid_pixel())
{
// Pixel starts outside the geometry
return cur_id_;
}
}
// Remaining distance to the end of the pixel
real_type pix_dist = image_.pixel_width();
// Distance associated with the largest segment so far
real_type max_dist = 0;
int max_id = cur_id_;
// Countdown to limit stuck tracks or overdetailed pixels
auto abort_counter = this->max_crossings_per_pixel();
while (cur_id_ >= 0 && distance_ < pix_dist)
{
// Move to geometry boundary
if (max_id == cur_id_)
{
max_dist += distance_;
}
else if (distance_ > max_dist)
{
max_dist = distance_;
max_id = cur_id_;
}
// Update pixel and boundary distance
pix_dist -= distance_;
distance_ = 0;
// Cross surface and update post-crossing ID
geo_.move_to_boundary();
geo_.cross_boundary();
if (--abort_counter == 0)
{
// Give up and move to end of pixel, reinitialize on the next call
pix_dist = 0;
}
if (pix_dist > 0)
{
// Update next distance and current ID for the remaining pixel
this->find_next_step();
}
}
if (pix_dist > 0)
{
// Move to pixel boundary
distance_ -= pix_dist;
if (pix_dist > max_dist)
{
max_dist = pix_dist;
max_id = cur_id_;
}
}
if (abort_counter != 0)
{
++pixel_;
}
else
{
pixel_ = invalid_pixel();
}
return max_id;
}
//---------------------------------------------------------------------------//
/*!
* Initialize the geometry at the given pixel position.
*/
template<class GTV, class F>
CELER_FUNCTION void Raytracer<GTV, F>::initialize_at_pixel(size_type pix)
{
CELER_EXPECT(pix < image_.max_index());
GeoTrackInitializer init{image_.start_pos(), image_.start_dir()};
axpy(pix * image_.pixel_width(), init.dir, &init.pos);
geo_ = init;
pixel_ = pix;
distance_ = 0;
this->find_next_step();
}
//---------------------------------------------------------------------------//
/*!
* Find the distance to the next step.
*/
template<class GTV, class F>
CELER_FUNCTION void Raytracer<GTV, F>::find_next_step()
{
CELER_EXPECT(pixel_ != invalid_pixel());
CELER_EXPECT(distance_ <= 0);
if (geo_.is_outside())
{
// Skip this pixel since not all navigation engines can trace from
// outside the geometry
pixel_ = invalid_pixel();
distance_ = numeric_limits<real_type>::infinity();
}
else
{
// Search to distances just past the end of the saved pixels (so the
// last point isn't coincident with a boundary)
distance_ = geo_.find_next_step((image_.max_index() + 1 - pixel_)
* image_.pixel_width())
.distance;
}
cur_id_ = this->calc_id_(geo_);
CELER_ENSURE(distance_ > 0);
}
//---------------------------------------------------------------------------//
} // namespace celeritas