obdeect-ray-tracing 0.8.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- obdeect_ray_tracing-0.8.0/CMakeLists.txt +116 -0
- obdeect_ray_tracing-0.8.0/PKG-INFO +57 -0
- obdeect_ray_tracing-0.8.0/README.md +25 -0
- obdeect_ray_tracing-0.8.0/cpp/bench/bench_trace.cpp +341 -0
- obdeect_ray_tracing-0.8.0/cpp/bench/replay.cpp +313 -0
- obdeect_ray_tracing-0.8.0/cpp/bench/replay_compare.hpp +163 -0
- obdeect_ray_tracing-0.8.0/cpp/bindings/README.md +21 -0
- obdeect_ray_tracing-0.8.0/cpp/bindings/core.cpp +543 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/bounds.hpp +65 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/bunches.hpp +343 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/json.hpp +215 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/model.hpp +80 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/model_json.hpp +496 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/obscurers.hpp +763 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/random.hpp +56 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/source.hpp +178 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/surfaces.hpp +931 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/table.hpp +74 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/trace.hpp +707 -0
- obdeect_ray_tracing-0.8.0/cpp/include/obdeect/ray_tracing/vec.hpp +44 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/data/make_polynomial_reference.py +82 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/legacy_asphere.hpp +158 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_asphere_steps.cpp +43 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_bounds.cpp +163 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_kernels.cpp +1236 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_model_json.cpp +210 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_replay_compare.cpp +162 -0
- obdeect_ray_tracing-0.8.0/cpp/tests/test_trace.cpp +752 -0
- obdeect_ray_tracing-0.8.0/pyproject.toml +43 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/__init__.py +18 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/cli.py +275 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/compiler.py +940 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/formats.py +393 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/model.py +668 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/plot.py +77 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/propagation.py +49 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/psf.py +54 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/repository.py +171 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/simtel_tables.py +261 -0
- obdeect_ray_tracing-0.8.0/python/obdeect/ray_tracing/stream.py +648 -0
- obdeect_ray_tracing-0.8.0/python/tests/telescopes.py +187 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_axes_offset.py +225 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_bunches.py +684 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_compiler.py +213 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_docs_versions.py +100 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_formats.py +131 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_model.py +434 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_propagation.py +31 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_release_metadata.py +20 -0
- obdeect_ray_tracing-0.8.0/python/tests/test_stream.py +780 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.20)
|
|
2
|
+
# The version comes from the git tag (setuptools-scm), not from here.
|
|
3
|
+
project(obdeect_ray_tracing LANGUAGES CXX)
|
|
4
|
+
|
|
5
|
+
option(OBDEECT_RAY_TRACING_BUILD_TESTING "Build the C++ tests" ON)
|
|
6
|
+
if(DEFINED SKBUILD_PLATLIB_DIR)
|
|
7
|
+
set(OBDEECT_RAY_TRACING_BUILD_PYTHON_API_DEFAULT ON)
|
|
8
|
+
else()
|
|
9
|
+
set(OBDEECT_RAY_TRACING_BUILD_PYTHON_API_DEFAULT OFF)
|
|
10
|
+
endif()
|
|
11
|
+
option(OBDEECT_RAY_TRACING_BUILD_BENCH "Build the benchmark and replay tools (not run by ctest)" OFF)
|
|
12
|
+
option(OBDEECT_RAY_TRACING_BUILD_PYTHON_API "Build the Python extension" ${OBDEECT_RAY_TRACING_BUILD_PYTHON_API_DEFAULT})
|
|
13
|
+
|
|
14
|
+
# The stream component: the target of the combined build, else the stream sources next to
|
|
15
|
+
# this component (builds from the repository), else an installed package (sdist builds).
|
|
16
|
+
if(NOT TARGET obdeect::stream)
|
|
17
|
+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../stream/CMakeLists.txt)
|
|
18
|
+
set(OBDEECT_STREAM_BUILD_TESTING OFF CACHE BOOL "" FORCE)
|
|
19
|
+
set(OBDEECT_STREAM_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
|
|
20
|
+
set(OBDEECT_STREAM_BUILD_CAPI OFF CACHE BOOL "" FORCE)
|
|
21
|
+
set(OBDEECT_STREAM_BUILD_PYTHON_API OFF CACHE BOOL "" FORCE)
|
|
22
|
+
set(OBDEECT_STREAM_INSTALL OFF CACHE BOOL "" FORCE)
|
|
23
|
+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../stream ${CMAKE_CURRENT_BINARY_DIR}/stream)
|
|
24
|
+
message(STATUS "obdeect ray_tracing: using the stream sources in ../stream")
|
|
25
|
+
else()
|
|
26
|
+
find_package(Python 3.14 COMPONENTS Interpreter)
|
|
27
|
+
if(Python_Interpreter_FOUND AND NOT obdeect_stream_DIR)
|
|
28
|
+
execute_process(COMMAND "${Python_EXECUTABLE}" -m obdeect.stream --cmake-dir
|
|
29
|
+
OUTPUT_VARIABLE obdeect_stream_DIR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
|
|
30
|
+
endif()
|
|
31
|
+
find_package(obdeect_stream CONFIG REQUIRED)
|
|
32
|
+
message(STATUS "obdeect ray_tracing: using the installed stream package ${obdeect_stream_VERSION} (${obdeect_stream_DIR})")
|
|
33
|
+
endif()
|
|
34
|
+
endif()
|
|
35
|
+
|
|
36
|
+
# Header-only, standard-library-only core.
|
|
37
|
+
add_library(obdeect_ray_tracing_core INTERFACE)
|
|
38
|
+
target_include_directories(obdeect_ray_tracing_core INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/cpp/include)
|
|
39
|
+
target_compile_features(obdeect_ray_tracing_core INTERFACE cxx_std_20)
|
|
40
|
+
# Philox core shared with the other components.
|
|
41
|
+
target_link_libraries(obdeect_ray_tracing_core INTERFACE obdeect::stream)
|
|
42
|
+
# Identical floating-point results for any thread count and block size on one
|
|
43
|
+
# platform: no implicit fused multiply-add contraction. Transcendental functions
|
|
44
|
+
# come from the platform's math library, so results may differ between
|
|
45
|
+
# platforms.
|
|
46
|
+
# -fno-math-errno lets GCC inline sqrt without the errno branch and libm call; it
|
|
47
|
+
# changes no result (the tracer never reads errno).
|
|
48
|
+
if(NOT MSVC)
|
|
49
|
+
target_compile_options(obdeect_ray_tracing_core INTERFACE -ffp-contract=off -fno-math-errno)
|
|
50
|
+
endif()
|
|
51
|
+
|
|
52
|
+
# Changes results at the rounding level. Use only on hardware with fused
|
|
53
|
+
# multiply-add; without it std::fma is emulated and very slow. Results differ
|
|
54
|
+
# from builds without it. Default builds, wheels and CI stay without FMA.
|
|
55
|
+
option(OBDEECT_RAY_TRACING_USE_FMA "Use std::fma in the aspheric polynomial evaluation" OFF)
|
|
56
|
+
if(OBDEECT_RAY_TRACING_USE_FMA)
|
|
57
|
+
target_compile_definitions(obdeect_ray_tracing_core INTERFACE OBDEECT_RAY_TRACING_USE_FMA=1)
|
|
58
|
+
# Without -mfma, x86-64 compilers call the math library for std::fma instead of inlining it.
|
|
59
|
+
if(NOT MSVC AND CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|amd64")
|
|
60
|
+
target_compile_options(obdeect_ray_tracing_core INTERFACE -mfma)
|
|
61
|
+
endif()
|
|
62
|
+
endif()
|
|
63
|
+
|
|
64
|
+
function(obdeect_ray_tracing_enable_warnings target)
|
|
65
|
+
if(MSVC)
|
|
66
|
+
target_compile_options(${target} PRIVATE /W4 /WX)
|
|
67
|
+
else()
|
|
68
|
+
target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
|
69
|
+
endif()
|
|
70
|
+
endfunction()
|
|
71
|
+
|
|
72
|
+
if(OBDEECT_RAY_TRACING_BUILD_PYTHON_API)
|
|
73
|
+
find_package(Python 3.14 COMPONENTS Interpreter Development.Module REQUIRED)
|
|
74
|
+
execute_process(COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
|
|
75
|
+
OUTPUT_VARIABLE nanobind_DIR OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
76
|
+
COMMAND_ERROR_IS_FATAL ANY)
|
|
77
|
+
find_package(nanobind CONFIG REQUIRED)
|
|
78
|
+
nanobind_add_module(_core NB_STATIC cpp/bindings/core.cpp)
|
|
79
|
+
target_link_libraries(_core PRIVATE obdeect_ray_tracing_core)
|
|
80
|
+
obdeect_ray_tracing_enable_warnings(_core)
|
|
81
|
+
install(TARGETS _core LIBRARY DESTINATION obdeect/ray_tracing)
|
|
82
|
+
endif()
|
|
83
|
+
|
|
84
|
+
if(OBDEECT_RAY_TRACING_BUILD_TESTING)
|
|
85
|
+
enable_testing()
|
|
86
|
+
foreach(name kernels trace model_json bounds)
|
|
87
|
+
add_executable(test_${name} cpp/tests/test_${name}.cpp)
|
|
88
|
+
target_link_libraries(test_${name} PRIVATE obdeect_ray_tracing_core)
|
|
89
|
+
# check.hpp lives with the stream tests; tests are built from the repository only, never from an sdist.
|
|
90
|
+
target_include_directories(test_${name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../stream/cpp/tests)
|
|
91
|
+
obdeect_ray_tracing_enable_warnings(test_${name})
|
|
92
|
+
add_test(NAME ${name} COMMAND test_${name})
|
|
93
|
+
endforeach()
|
|
94
|
+
# Newton-step counts need the benchmark counters compiled in.
|
|
95
|
+
add_executable(test_asphere_steps cpp/tests/test_asphere_steps.cpp)
|
|
96
|
+
target_link_libraries(test_asphere_steps PRIVATE obdeect_ray_tracing_core)
|
|
97
|
+
target_include_directories(test_asphere_steps PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../stream/cpp/tests)
|
|
98
|
+
target_compile_definitions(test_asphere_steps PRIVATE OBDEECT_RAY_TRACING_BENCH_COUNTERS)
|
|
99
|
+
obdeect_ray_tracing_enable_warnings(test_asphere_steps)
|
|
100
|
+
add_test(NAME asphere_steps COMMAND test_asphere_steps)
|
|
101
|
+
# The replay tool's limit comparison (header in cpp/bench, standard library only).
|
|
102
|
+
add_executable(test_replay_compare cpp/tests/test_replay_compare.cpp)
|
|
103
|
+
target_link_libraries(test_replay_compare PRIVATE obdeect_ray_tracing_core)
|
|
104
|
+
target_include_directories(test_replay_compare PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../stream/cpp/tests)
|
|
105
|
+
target_include_directories(test_replay_compare PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/cpp/bench)
|
|
106
|
+
obdeect_ray_tracing_enable_warnings(test_replay_compare)
|
|
107
|
+
add_test(NAME replay_compare COMMAND test_replay_compare)
|
|
108
|
+
endif()
|
|
109
|
+
|
|
110
|
+
if(OBDEECT_RAY_TRACING_BUILD_BENCH)
|
|
111
|
+
foreach(name bench_trace replay)
|
|
112
|
+
add_executable(${name} cpp/bench/${name}.cpp)
|
|
113
|
+
target_link_libraries(${name} PRIVATE obdeect_ray_tracing_core)
|
|
114
|
+
obdeect_ray_tracing_enable_warnings(${name})
|
|
115
|
+
endforeach()
|
|
116
|
+
endif()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: obdeect-ray-tracing
|
|
3
|
+
Version: 0.8.0
|
|
4
|
+
Summary: Ray tracing for imaging atmospheric Cherenkov telescopes
|
|
5
|
+
Author: obdeect contributors
|
|
6
|
+
License-Expression: BSD-3-Clause
|
|
7
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
8
|
+
Classifier: Programming Language :: C++
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
12
|
+
Requires-Python: >=3.14
|
|
13
|
+
Requires-Dist: numpy>=2.3
|
|
14
|
+
Requires-Dist: obdeect-stream==0.8.0
|
|
15
|
+
Provides-Extra: dev
|
|
16
|
+
Requires-Dist: nanobind>=3.1; extra == "dev"
|
|
17
|
+
Requires-Dist: pre-commit; extra == "dev"
|
|
18
|
+
Requires-Dist: pytest; extra == "dev"
|
|
19
|
+
Requires-Dist: ruff; extra == "dev"
|
|
20
|
+
Provides-Extra: docs
|
|
21
|
+
Requires-Dist: myst-parser; extra == "docs"
|
|
22
|
+
Requires-Dist: pydata-sphinx-theme; extra == "docs"
|
|
23
|
+
Requires-Dist: pyyaml; extra == "docs"
|
|
24
|
+
Requires-Dist: sphinx; extra == "docs"
|
|
25
|
+
Requires-Dist: sphinx-design; extra == "docs"
|
|
26
|
+
Provides-Extra: figures
|
|
27
|
+
Requires-Dist: astropy; extra == "figures"
|
|
28
|
+
Requires-Dist: matplotlib>=3.8; extra == "figures"
|
|
29
|
+
Provides-Extra: plot
|
|
30
|
+
Requires-Dist: matplotlib>=3.8; extra == "plot"
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# obdeect-ray-tracing
|
|
34
|
+
|
|
35
|
+
Optical ray tracer for imaging atmospheric Cherenkov telescopes. The C++20 core uses only the
|
|
36
|
+
standard library. Python compiles optical models from simulation models, traces photons, and
|
|
37
|
+
plots results. simtools uses it as a ray-tracing backend. Status: pre-alpha.
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
pip install obdeect-ray-tracing # this component only (also installs obdeect-stream)
|
|
41
|
+
pip install obdeect # all obdeect components
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Requires Python 3.14 or higher.
|
|
45
|
+
|
|
46
|
+
## Commands
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
obdeect ray-tracing compile --simulation-models ../simulation-models --model MSTN-01 \
|
|
50
|
+
--version 7.0.0 --seed 1 --zenith-deg 20 --output mst.json
|
|
51
|
+
obdeect ray-tracing trace --model mst.json --seed 2 --photons 10000 --field-deg 0.5 0
|
|
52
|
+
obdeect ray-tracing trace-stream --model mst.json --seed 7 -i ground.ps -o camera.ps
|
|
53
|
+
obdeect ray-tracing obscurers ...
|
|
54
|
+
obdeect ray-tracing plot ...
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Full documentation and the repository README: https://github.com/gammasim/obdeect
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# obdeect-ray-tracing
|
|
2
|
+
|
|
3
|
+
Optical ray tracer for imaging atmospheric Cherenkov telescopes. The C++20 core uses only the
|
|
4
|
+
standard library. Python compiles optical models from simulation models, traces photons, and
|
|
5
|
+
plots results. simtools uses it as a ray-tracing backend. Status: pre-alpha.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pip install obdeect-ray-tracing # this component only (also installs obdeect-stream)
|
|
9
|
+
pip install obdeect # all obdeect components
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Requires Python 3.14 or higher.
|
|
13
|
+
|
|
14
|
+
## Commands
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
obdeect ray-tracing compile --simulation-models ../simulation-models --model MSTN-01 \
|
|
18
|
+
--version 7.0.0 --seed 1 --zenith-deg 20 --output mst.json
|
|
19
|
+
obdeect ray-tracing trace --model mst.json --seed 2 --photons 10000 --field-deg 0.5 0
|
|
20
|
+
obdeect ray-tracing trace-stream --model mst.json --seed 7 -i ground.ps -o camera.ps
|
|
21
|
+
obdeect ray-tracing obscurers ...
|
|
22
|
+
obdeect ray-tracing plot ...
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Full documentation and the repository README: https://github.com/gammasim/obdeect
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// Single-thread cost of the tracing stages of an optical model.
|
|
2
|
+
//
|
|
3
|
+
// bench_trace <model.json> <disk_radius_m> <disk_z_m> <start_z_m> [photons=2000000]
|
|
4
|
+
// [classify_losses=1]
|
|
5
|
+
//
|
|
6
|
+
// classify_losses 0 times the trace_photon lines with classify_losses false.
|
|
7
|
+
//
|
|
8
|
+
// Photons are sampled before the timing starts (star on the optical axis at
|
|
9
|
+
// 10 km, 400 nm, seed 3). Times are ns per photon, best of three. Not run by
|
|
10
|
+
// ctest.
|
|
11
|
+
#include "obdeect/ray_tracing/model_json.hpp"
|
|
12
|
+
#include "obdeect/ray_tracing/source.hpp"
|
|
13
|
+
#include "obdeect/ray_tracing/trace.hpp"
|
|
14
|
+
|
|
15
|
+
#include <array>
|
|
16
|
+
#include <chrono>
|
|
17
|
+
#include <cstdio>
|
|
18
|
+
#include <cstdlib>
|
|
19
|
+
#include <exception>
|
|
20
|
+
#include <fstream>
|
|
21
|
+
#include <sstream>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
using namespace obdeect::ray_tracing;
|
|
25
|
+
using clk = std::chrono::steady_clock;
|
|
26
|
+
|
|
27
|
+
namespace {
|
|
28
|
+
|
|
29
|
+
// Best of three runs of body(), in ns per item.
|
|
30
|
+
template <class Body> double best_ns(std::size_t items, const Body &body) {
|
|
31
|
+
double best = 1e30;
|
|
32
|
+
for (int run = 0; run < 3; ++run) {
|
|
33
|
+
const auto start = clk::now();
|
|
34
|
+
body();
|
|
35
|
+
best = std::min(best, std::chrono::duration<double, std::nano>(clk::now() - start).count());
|
|
36
|
+
}
|
|
37
|
+
return best / static_cast<double>(items);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
} // namespace
|
|
41
|
+
|
|
42
|
+
static int run(int argc, char **argv) {
|
|
43
|
+
if (argc < 5) {
|
|
44
|
+
std::fprintf(stderr,
|
|
45
|
+
"usage: %s model.json disk_radius_m disk_z_m start_z_m [photons] "
|
|
46
|
+
"[classify_losses]\n",
|
|
47
|
+
argv[0]);
|
|
48
|
+
return 2;
|
|
49
|
+
}
|
|
50
|
+
std::ifstream file(argv[1]);
|
|
51
|
+
if (!file) {
|
|
52
|
+
std::fprintf(stderr, "cannot read %s\n", argv[1]);
|
|
53
|
+
return 2;
|
|
54
|
+
}
|
|
55
|
+
std::stringstream text;
|
|
56
|
+
text << file.rdbuf();
|
|
57
|
+
const OpticalModel model = load_model(text.str());
|
|
58
|
+
const PointSource source{{0, 0, 1},
|
|
59
|
+
1e4,
|
|
60
|
+
std::strtod(argv[2], nullptr),
|
|
61
|
+
std::strtod(argv[3], nullptr),
|
|
62
|
+
std::strtod(argv[4], nullptr),
|
|
63
|
+
400.0};
|
|
64
|
+
const std::size_t n = argc > 5 ? std::strtoull(argv[5], nullptr, 10) : 2'000'000;
|
|
65
|
+
const bool classify = argc > 6 ? std::strtol(argv[6], nullptr, 10) != 0 : true;
|
|
66
|
+
std::vector<SourcePhoton> photons(n);
|
|
67
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
68
|
+
photons[i] = sample_source(source, i, 3);
|
|
69
|
+
|
|
70
|
+
double checksum = 0.0;
|
|
71
|
+
std::array<std::size_t, kStatusCount> counts{};
|
|
72
|
+
const double trace_ns = best_ns(n, [&] {
|
|
73
|
+
counts.fill(0);
|
|
74
|
+
for (std::size_t i = 0; i < n; ++i) {
|
|
75
|
+
const auto r =
|
|
76
|
+
trace_photon(model, photons[i].ray, 400.0, 0.0, 1.0, i, 3, false, false, true, classify);
|
|
77
|
+
++counts[static_cast<std::size_t>(r.status)];
|
|
78
|
+
checksum += r.position.x + r.time_ns;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
std::printf("trace_photon %8.1f ns", trace_ns);
|
|
82
|
+
for (std::size_t s = 0; s < kStatusCount; ++s)
|
|
83
|
+
if (counts[s])
|
|
84
|
+
std::printf(" %s %.4f", std::string(to_string(static_cast<Status>(s))).c_str(),
|
|
85
|
+
static_cast<double>(counts[s]) / static_cast<double>(n));
|
|
86
|
+
std::printf("\n");
|
|
87
|
+
|
|
88
|
+
bool failed = false;
|
|
89
|
+
std::size_t hits = 0;
|
|
90
|
+
std::vector<char> hit(n);
|
|
91
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
92
|
+
SegmentGrid::counters() = {};
|
|
93
|
+
#endif
|
|
94
|
+
const double primary_ns = best_ns(n, [&] {
|
|
95
|
+
hits = 0;
|
|
96
|
+
for (std::size_t i = 0; i < n; ++i) {
|
|
97
|
+
const auto h = detail::intersect_mirror(model.primary, photons[i].ray, failed);
|
|
98
|
+
hit[i] = h.has_value();
|
|
99
|
+
hits += hit[i];
|
|
100
|
+
if (h)
|
|
101
|
+
checksum += h->hit.t;
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
std::printf("primary intersect %8.1f ns (hit %.4f)\n", primary_ns,
|
|
105
|
+
static_cast<double>(hits) / static_cast<double>(n));
|
|
106
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
107
|
+
// Per primary hit of the timed runs (best_ns runs the body three times).
|
|
108
|
+
if (hits && SegmentGrid::counters().calls)
|
|
109
|
+
std::printf(" %s\n", SegmentGrid::counter_report(3 * hits).c_str());
|
|
110
|
+
#endif
|
|
111
|
+
|
|
112
|
+
if (!model.primary.facets.empty()) {
|
|
113
|
+
std::size_t candidates = 0;
|
|
114
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
115
|
+
model.primary.grid.candidates(photons[i].ray, model.primary.facets.size(),
|
|
116
|
+
[&](std::size_t) { ++candidates; });
|
|
117
|
+
std::printf(" candidate facets per ray %.2f\n",
|
|
118
|
+
static_cast<double>(candidates) / static_cast<double>(n));
|
|
119
|
+
}
|
|
120
|
+
// Photons that miss the primary (no hit above): the primary intersection alone.
|
|
121
|
+
std::vector<std::size_t> misses;
|
|
122
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
123
|
+
if (!hit[i])
|
|
124
|
+
misses.push_back(i);
|
|
125
|
+
if (!misses.empty()) {
|
|
126
|
+
const double miss_ns = best_ns(misses.size(), [&] {
|
|
127
|
+
for (const std::size_t i : misses)
|
|
128
|
+
checksum += detail::intersect_mirror(model.primary, photons[i].ray, failed).has_value();
|
|
129
|
+
});
|
|
130
|
+
std::size_t miss_candidates = 0;
|
|
131
|
+
if (!model.primary.facets.empty())
|
|
132
|
+
for (const std::size_t i : misses)
|
|
133
|
+
model.primary.grid.candidates(photons[i].ray, model.primary.facets.size(),
|
|
134
|
+
[&](std::size_t) { ++miss_candidates; });
|
|
135
|
+
std::printf("primary miss %8.1f ns (%zu rays, %.2f candidate facets per ray)\n",
|
|
136
|
+
miss_ns, misses.size(),
|
|
137
|
+
static_cast<double>(miss_candidates) / static_cast<double>(misses.size()));
|
|
138
|
+
}
|
|
139
|
+
if (model.primary.asphere) {
|
|
140
|
+
for (const char want : {char{1}, char{0}}) {
|
|
141
|
+
std::size_t k = 0;
|
|
142
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
143
|
+
k += hit[i] == want;
|
|
144
|
+
const double ns = best_ns(k, [&] {
|
|
145
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
146
|
+
if (hit[i] == want)
|
|
147
|
+
checksum += detail::intersect_mirror(model.primary, photons[i].ray, failed).has_value();
|
|
148
|
+
});
|
|
149
|
+
std::printf(" asphere %-4s %8.1f ns per ray (%zu rays)\n", want ? "hit" : "miss", ns,
|
|
150
|
+
k);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
std::size_t blocked = 0;
|
|
155
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
156
|
+
Obscurers::counters() = {};
|
|
157
|
+
#endif
|
|
158
|
+
const double obscurer_ns = best_ns(n, [&] {
|
|
159
|
+
blocked = 0;
|
|
160
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
161
|
+
if (const auto b = model.obscurers.nearest(photons[i].ray, Leg::incoming, 1e30)) {
|
|
162
|
+
++blocked;
|
|
163
|
+
checksum += b->t;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
std::printf("obscurers incoming %8.1f ns (blocked %.4f, %zu items)\n", obscurer_ns,
|
|
167
|
+
static_cast<double>(blocked) / static_cast<double>(n),
|
|
168
|
+
model.obscurers.items().size());
|
|
169
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
170
|
+
std::printf("%s\n", Obscurers::counter_report(3 * n).c_str());
|
|
171
|
+
#endif
|
|
172
|
+
// Round-2 measures. Each one collects its inputs first and times only the calls.
|
|
173
|
+
const auto focal_limit = [&](const Ray &r) -> std::optional<double> {
|
|
174
|
+
if (model.focal_surface.asphere) {
|
|
175
|
+
bool f = false;
|
|
176
|
+
if (const auto h = model.focal_surface.asphere->intersect(
|
|
177
|
+
r, kMinDistanceM, [](const Vec3 &) { return true; }, f))
|
|
178
|
+
return h->t;
|
|
179
|
+
return std::nullopt;
|
|
180
|
+
}
|
|
181
|
+
if (r.direction.z == 0.0)
|
|
182
|
+
return std::nullopt;
|
|
183
|
+
const double t = (model.focal_surface.plane_z - r.origin.z) / r.direction.z;
|
|
184
|
+
if (t > kMinDistanceM)
|
|
185
|
+
return t;
|
|
186
|
+
return std::nullopt;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const double responses_ns = best_ns(n, [&] {
|
|
190
|
+
for (std::size_t i = 0; i < n; ++i) {
|
|
191
|
+
const auto r =
|
|
192
|
+
trace_photon(model, photons[i].ray, 400.0, 0.0, 1.0, i, 3, true, false, true, classify);
|
|
193
|
+
checksum += r.weight + r.time_ns;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
std::printf("trace_photon (responses) %8.1f ns\n", responses_ns);
|
|
197
|
+
|
|
198
|
+
// Rays that hit the primary, with their hit records.
|
|
199
|
+
std::vector<detail::MirrorHit> primary_hits;
|
|
200
|
+
std::vector<std::size_t> primary_index;
|
|
201
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
202
|
+
if (const auto h = detail::intersect_mirror(model.primary, photons[i].ray, failed)) {
|
|
203
|
+
primary_hits.push_back(*h);
|
|
204
|
+
primary_index.push_back(i);
|
|
205
|
+
}
|
|
206
|
+
if (!primary_hits.empty()) {
|
|
207
|
+
const double reflect_ns = best_ns(primary_hits.size(), [&] {
|
|
208
|
+
for (std::size_t k = 0; k < primary_hits.size(); ++k) {
|
|
209
|
+
const std::size_t i = primary_index[k];
|
|
210
|
+
const Vec3 d = detail::reflect_at(model.primary, primary_hits[k], photons[i].ray.direction,
|
|
211
|
+
i, 3, Stream::primary_scatter);
|
|
212
|
+
checksum += d.x;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
std::printf("reflect_at per hit %8.1f ns (%zu hits)\n", reflect_ns, primary_hits.size());
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Reflected rays after the primary, with the distance to their next surface.
|
|
219
|
+
std::vector<Ray> after_primary_rays, after_secondary_rays;
|
|
220
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
221
|
+
std::vector<Ray> reflected_primary_rays; // All of them, also those that miss the next surface.
|
|
222
|
+
#endif
|
|
223
|
+
std::vector<double> after_primary_limits, after_secondary_limits;
|
|
224
|
+
for (std::size_t k = 0; k < primary_hits.size(); ++k) {
|
|
225
|
+
const std::size_t i = primary_index[k];
|
|
226
|
+
const auto &mh = primary_hits[k];
|
|
227
|
+
const Vec3 d1 = detail::reflect_at(model.primary, mh, photons[i].ray.direction, i, 3,
|
|
228
|
+
Stream::primary_scatter);
|
|
229
|
+
const Ray r1{mh.hit.point, d1};
|
|
230
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
231
|
+
reflected_primary_rays.push_back(r1);
|
|
232
|
+
#endif
|
|
233
|
+
std::optional<double> limit1;
|
|
234
|
+
std::optional<detail::MirrorHit> m2;
|
|
235
|
+
if (model.secondary) {
|
|
236
|
+
m2 = detail::intersect_mirror(*model.secondary, r1, failed);
|
|
237
|
+
if (m2)
|
|
238
|
+
limit1 = m2->hit.t;
|
|
239
|
+
} else {
|
|
240
|
+
limit1 = focal_limit(r1);
|
|
241
|
+
}
|
|
242
|
+
if (!limit1)
|
|
243
|
+
continue;
|
|
244
|
+
after_primary_rays.push_back(r1);
|
|
245
|
+
after_primary_limits.push_back(*limit1);
|
|
246
|
+
if (!model.secondary)
|
|
247
|
+
continue;
|
|
248
|
+
const Vec3 d2 = detail::reflect_at(*model.secondary, *m2, d1, i, 3, Stream::secondary_scatter);
|
|
249
|
+
const Ray r2{m2->hit.point, d2};
|
|
250
|
+
if (const auto limit2 = focal_limit(r2)) {
|
|
251
|
+
after_secondary_rays.push_back(r2);
|
|
252
|
+
after_secondary_limits.push_back(*limit2);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const auto time_obscurers = [&](const std::vector<Ray> &rays, const std::vector<double> &limits,
|
|
256
|
+
Leg leg, const char *label) {
|
|
257
|
+
if (rays.empty())
|
|
258
|
+
return;
|
|
259
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
260
|
+
Obscurers::counters() = {};
|
|
261
|
+
#endif
|
|
262
|
+
const double ns = best_ns(rays.size(), [&] {
|
|
263
|
+
for (std::size_t j = 0; j < rays.size(); ++j)
|
|
264
|
+
if (const auto b = model.obscurers.nearest(rays[j], leg, limits[j]))
|
|
265
|
+
checksum += b->t;
|
|
266
|
+
});
|
|
267
|
+
std::printf("obscurers %-16s %8.1f ns (%zu rays)\n", label, ns, rays.size());
|
|
268
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
269
|
+
std::printf(" %s\n", Obscurers::counter_report(3 * rays.size()).c_str());
|
|
270
|
+
#endif
|
|
271
|
+
};
|
|
272
|
+
time_obscurers(after_primary_rays, after_primary_limits, Leg::after_primary, "after primary");
|
|
273
|
+
if (model.secondary)
|
|
274
|
+
time_obscurers(after_secondary_rays, after_secondary_limits, Leg::after_secondary,
|
|
275
|
+
"after secondary");
|
|
276
|
+
|
|
277
|
+
// Full trace cost of the photons that miss the primary (geometry only, as the
|
|
278
|
+
// trace_photon line).
|
|
279
|
+
std::vector<std::size_t> missed;
|
|
280
|
+
for (std::size_t i = 0; i < n; ++i)
|
|
281
|
+
if (trace_photon(model, photons[i].ray, 400.0, 0.0, 1.0, i, 3, false).status ==
|
|
282
|
+
Status::missed_primary)
|
|
283
|
+
missed.push_back(i);
|
|
284
|
+
if (!missed.empty()) {
|
|
285
|
+
const double miss_ns = best_ns(missed.size(), [&] {
|
|
286
|
+
for (const std::size_t i : missed)
|
|
287
|
+
checksum += trace_photon(model, photons[i].ray, 400.0, 0.0, 1.0, i, 3, false).path_length_m;
|
|
288
|
+
});
|
|
289
|
+
std::printf("miss cost %8.1f ns (%zu rays)\n", miss_ns, missed.size());
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
#ifdef OBDEECT_RAY_TRACING_BENCH_COUNTERS
|
|
293
|
+
// Newton steps per call of each aspheric surface, for the rays that reach it:
|
|
294
|
+
// photons (primary), all rays reflected by the primary (secondary, or the focal
|
|
295
|
+
// surface without a secondary) and rays after the secondary (focal surface).
|
|
296
|
+
const auto report_steps = [&](const char *label, const std::vector<Ray> &rays, auto intersect) {
|
|
297
|
+
Asphere::counters() = {};
|
|
298
|
+
for (const Ray &r : rays)
|
|
299
|
+
checksum += intersect(r);
|
|
300
|
+
const auto &c = Asphere::counters();
|
|
301
|
+
if (c.calls)
|
|
302
|
+
std::printf(" newton steps per call %-9s %.3f (%llu calls)\n", label,
|
|
303
|
+
static_cast<double>(c.newton_steps) / static_cast<double>(c.calls),
|
|
304
|
+
static_cast<unsigned long long>(c.calls));
|
|
305
|
+
};
|
|
306
|
+
const auto through_mirror = [&](const Mirror &m) {
|
|
307
|
+
return [&](const Ray &r) {
|
|
308
|
+
return detail::intersect_mirror(m, r, failed).has_value() ? 1.0 : 0.0;
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
if (model.primary.asphere) {
|
|
312
|
+
std::vector<Ray> rays;
|
|
313
|
+
for (const auto &photon : photons)
|
|
314
|
+
rays.push_back(photon.ray);
|
|
315
|
+
report_steps("primary", rays, through_mirror(model.primary));
|
|
316
|
+
}
|
|
317
|
+
if (model.secondary && model.secondary->asphere)
|
|
318
|
+
report_steps("secondary", reflected_primary_rays, through_mirror(*model.secondary));
|
|
319
|
+
if (model.focal_surface.asphere)
|
|
320
|
+
report_steps("focal", model.secondary ? after_secondary_rays : reflected_primary_rays,
|
|
321
|
+
[&](const Ray &r) {
|
|
322
|
+
return model.focal_surface.asphere
|
|
323
|
+
->intersect(
|
|
324
|
+
r, kMinDistanceM, [](const Vec3 &) { return true; }, failed)
|
|
325
|
+
.has_value()
|
|
326
|
+
? 1.0
|
|
327
|
+
: 0.0;
|
|
328
|
+
});
|
|
329
|
+
#endif
|
|
330
|
+
std::printf("checksum %.17g\n", checksum);
|
|
331
|
+
return 0;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
int main(int argc, char **argv) {
|
|
335
|
+
try {
|
|
336
|
+
return run(argc, argv);
|
|
337
|
+
} catch (const std::exception &error) {
|
|
338
|
+
std::fprintf(stderr, "error: %s\n", error.what());
|
|
339
|
+
return 1;
|
|
340
|
+
}
|
|
341
|
+
}
|