lsurf 1.0.0__py3-none-any.whl

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.
Files changed (180) hide show
  1. lsurf/__init__.py +471 -0
  2. lsurf/analysis/__init__.py +107 -0
  3. lsurf/analysis/healpix_utils.py +418 -0
  4. lsurf/analysis/sphere_viz.py +1280 -0
  5. lsurf/cli/__init__.py +48 -0
  6. lsurf/cli/build.py +398 -0
  7. lsurf/cli/config_schema.py +318 -0
  8. lsurf/cli/gui_cmd.py +76 -0
  9. lsurf/cli/interactive.py +850 -0
  10. lsurf/cli/main.py +81 -0
  11. lsurf/cli/run.py +806 -0
  12. lsurf/detectors/__init__.py +266 -0
  13. lsurf/detectors/analysis.py +289 -0
  14. lsurf/detectors/base.py +284 -0
  15. lsurf/detectors/constant_size_rings.py +485 -0
  16. lsurf/detectors/directional.py +45 -0
  17. lsurf/detectors/extended/__init__.py +73 -0
  18. lsurf/detectors/extended/local_sphere.py +353 -0
  19. lsurf/detectors/extended/recording_sphere.py +368 -0
  20. lsurf/detectors/planar.py +45 -0
  21. lsurf/detectors/protocol.py +187 -0
  22. lsurf/detectors/recording_spheres.py +63 -0
  23. lsurf/detectors/results.py +1140 -0
  24. lsurf/detectors/small/__init__.py +79 -0
  25. lsurf/detectors/small/directional.py +330 -0
  26. lsurf/detectors/small/planar.py +401 -0
  27. lsurf/detectors/small/spherical.py +450 -0
  28. lsurf/detectors/spherical.py +45 -0
  29. lsurf/geometry/__init__.py +199 -0
  30. lsurf/geometry/builder.py +478 -0
  31. lsurf/geometry/cell.py +228 -0
  32. lsurf/geometry/cell_geometry.py +247 -0
  33. lsurf/geometry/detector_arrays.py +1785 -0
  34. lsurf/geometry/geometry.py +222 -0
  35. lsurf/geometry/surface_analysis.py +375 -0
  36. lsurf/geometry/validation.py +91 -0
  37. lsurf/gui/__init__.py +51 -0
  38. lsurf/gui/app.py +903 -0
  39. lsurf/gui/core/__init__.py +39 -0
  40. lsurf/gui/core/scene.py +343 -0
  41. lsurf/gui/core/simulation.py +264 -0
  42. lsurf/gui/renderers/__init__.py +40 -0
  43. lsurf/gui/renderers/ray_renderer.py +353 -0
  44. lsurf/gui/renderers/source_renderer.py +505 -0
  45. lsurf/gui/renderers/surface_renderer.py +477 -0
  46. lsurf/gui/views/__init__.py +48 -0
  47. lsurf/gui/views/config_editor.py +3199 -0
  48. lsurf/gui/views/properties.py +257 -0
  49. lsurf/gui/views/results.py +291 -0
  50. lsurf/gui/views/scene_tree.py +180 -0
  51. lsurf/gui/views/viewport_3d.py +555 -0
  52. lsurf/gui/views/visualizations.py +712 -0
  53. lsurf/materials/__init__.py +169 -0
  54. lsurf/materials/base/__init__.py +64 -0
  55. lsurf/materials/base/full_inhomogeneous.py +208 -0
  56. lsurf/materials/base/grid_inhomogeneous.py +319 -0
  57. lsurf/materials/base/homogeneous.py +342 -0
  58. lsurf/materials/base/material_field.py +527 -0
  59. lsurf/materials/base/simple_inhomogeneous.py +418 -0
  60. lsurf/materials/base/spectral_inhomogeneous.py +497 -0
  61. lsurf/materials/implementations/__init__.py +120 -0
  62. lsurf/materials/implementations/data/alpha_values_typical_atmosphere_updated.txt +24 -0
  63. lsurf/materials/implementations/duct_atmosphere.py +390 -0
  64. lsurf/materials/implementations/exponential_atmosphere.py +435 -0
  65. lsurf/materials/implementations/gaussian_lens.py +120 -0
  66. lsurf/materials/implementations/interpolated_data.py +123 -0
  67. lsurf/materials/implementations/layered_atmosphere.py +134 -0
  68. lsurf/materials/implementations/linear_gradient.py +109 -0
  69. lsurf/materials/implementations/linsley_atmosphere.py +764 -0
  70. lsurf/materials/implementations/standard_materials.py +126 -0
  71. lsurf/materials/implementations/turbulent_atmosphere.py +135 -0
  72. lsurf/materials/implementations/us_standard_atmosphere.py +149 -0
  73. lsurf/materials/utils/__init__.py +77 -0
  74. lsurf/materials/utils/constants.py +45 -0
  75. lsurf/materials/utils/device_functions.py +117 -0
  76. lsurf/materials/utils/dispersion.py +160 -0
  77. lsurf/materials/utils/factories.py +142 -0
  78. lsurf/propagation/__init__.py +91 -0
  79. lsurf/propagation/detector_gpu.py +67 -0
  80. lsurf/propagation/gpu_device_rays.py +294 -0
  81. lsurf/propagation/kernels/__init__.py +175 -0
  82. lsurf/propagation/kernels/absorption/__init__.py +61 -0
  83. lsurf/propagation/kernels/absorption/grid.py +240 -0
  84. lsurf/propagation/kernels/absorption/simple.py +232 -0
  85. lsurf/propagation/kernels/absorption/spectral.py +410 -0
  86. lsurf/propagation/kernels/detection/__init__.py +64 -0
  87. lsurf/propagation/kernels/detection/protocol.py +102 -0
  88. lsurf/propagation/kernels/detection/spherical.py +255 -0
  89. lsurf/propagation/kernels/device_functions.py +790 -0
  90. lsurf/propagation/kernels/fresnel/__init__.py +64 -0
  91. lsurf/propagation/kernels/fresnel/protocol.py +97 -0
  92. lsurf/propagation/kernels/fresnel/standard.py +258 -0
  93. lsurf/propagation/kernels/intersection/__init__.py +79 -0
  94. lsurf/propagation/kernels/intersection/annular_plane.py +207 -0
  95. lsurf/propagation/kernels/intersection/bounded_plane.py +205 -0
  96. lsurf/propagation/kernels/intersection/plane.py +166 -0
  97. lsurf/propagation/kernels/intersection/protocol.py +95 -0
  98. lsurf/propagation/kernels/intersection/signed_distance.py +742 -0
  99. lsurf/propagation/kernels/intersection/sphere.py +190 -0
  100. lsurf/propagation/kernels/propagation/__init__.py +85 -0
  101. lsurf/propagation/kernels/propagation/grid.py +527 -0
  102. lsurf/propagation/kernels/propagation/protocol.py +105 -0
  103. lsurf/propagation/kernels/propagation/simple.py +460 -0
  104. lsurf/propagation/kernels/propagation/spectral.py +875 -0
  105. lsurf/propagation/kernels/registry.py +331 -0
  106. lsurf/propagation/kernels/surface/__init__.py +72 -0
  107. lsurf/propagation/kernels/surface/bisection.py +232 -0
  108. lsurf/propagation/kernels/surface/detection.py +402 -0
  109. lsurf/propagation/kernels/surface/reduction.py +166 -0
  110. lsurf/propagation/propagator_protocol.py +222 -0
  111. lsurf/propagation/propagators/__init__.py +101 -0
  112. lsurf/propagation/propagators/detector_handler.py +354 -0
  113. lsurf/propagation/propagators/factory.py +200 -0
  114. lsurf/propagation/propagators/fresnel_handler.py +305 -0
  115. lsurf/propagation/propagators/gpu_gradient.py +566 -0
  116. lsurf/propagation/propagators/gpu_surface_propagator.py +707 -0
  117. lsurf/propagation/propagators/gradient.py +429 -0
  118. lsurf/propagation/propagators/intersection_handler.py +327 -0
  119. lsurf/propagation/propagators/material_propagator.py +398 -0
  120. lsurf/propagation/propagators/signed_distance_handler.py +522 -0
  121. lsurf/propagation/propagators/spectral_gpu_gradient.py +553 -0
  122. lsurf/propagation/propagators/surface_interaction.py +616 -0
  123. lsurf/propagation/propagators/surface_propagator.py +719 -0
  124. lsurf/py.typed +1 -0
  125. lsurf/simulation/__init__.py +70 -0
  126. lsurf/simulation/config.py +164 -0
  127. lsurf/simulation/orchestrator.py +462 -0
  128. lsurf/simulation/result.py +299 -0
  129. lsurf/simulation/simulation.py +262 -0
  130. lsurf/sources/__init__.py +128 -0
  131. lsurf/sources/base.py +264 -0
  132. lsurf/sources/collimated.py +252 -0
  133. lsurf/sources/custom.py +409 -0
  134. lsurf/sources/diverging.py +228 -0
  135. lsurf/sources/gaussian.py +272 -0
  136. lsurf/sources/parallel_from_positions.py +197 -0
  137. lsurf/sources/point.py +172 -0
  138. lsurf/sources/uniform_diverging.py +258 -0
  139. lsurf/surfaces/__init__.py +184 -0
  140. lsurf/surfaces/cpu/__init__.py +50 -0
  141. lsurf/surfaces/cpu/curved_wave.py +463 -0
  142. lsurf/surfaces/cpu/gerstner_wave.py +381 -0
  143. lsurf/surfaces/cpu/wave_params.py +118 -0
  144. lsurf/surfaces/gpu/__init__.py +72 -0
  145. lsurf/surfaces/gpu/annular_plane.py +453 -0
  146. lsurf/surfaces/gpu/bounded_plane.py +390 -0
  147. lsurf/surfaces/gpu/curved_wave.py +483 -0
  148. lsurf/surfaces/gpu/gerstner_wave.py +377 -0
  149. lsurf/surfaces/gpu/multi_curved_wave.py +520 -0
  150. lsurf/surfaces/gpu/plane.py +299 -0
  151. lsurf/surfaces/gpu/recording_sphere.py +587 -0
  152. lsurf/surfaces/gpu/sphere.py +311 -0
  153. lsurf/surfaces/protocol.py +336 -0
  154. lsurf/surfaces/registry.py +373 -0
  155. lsurf/utilities/__init__.py +175 -0
  156. lsurf/utilities/detector_analysis.py +814 -0
  157. lsurf/utilities/fresnel.py +628 -0
  158. lsurf/utilities/interactions.py +1215 -0
  159. lsurf/utilities/propagation.py +602 -0
  160. lsurf/utilities/ray_data.py +532 -0
  161. lsurf/utilities/recording_sphere.py +745 -0
  162. lsurf/utilities/time_spread.py +463 -0
  163. lsurf/visualization/__init__.py +329 -0
  164. lsurf/visualization/absorption_plots.py +334 -0
  165. lsurf/visualization/atmospheric_plots.py +754 -0
  166. lsurf/visualization/common.py +348 -0
  167. lsurf/visualization/detector_plots.py +1350 -0
  168. lsurf/visualization/detector_sphere_plots.py +1173 -0
  169. lsurf/visualization/fresnel_plots.py +1061 -0
  170. lsurf/visualization/ocean_simulation_plots.py +999 -0
  171. lsurf/visualization/polarization_plots.py +916 -0
  172. lsurf/visualization/raytracing_plots.py +1521 -0
  173. lsurf/visualization/ring_detector_plots.py +1867 -0
  174. lsurf/visualization/time_spread_plots.py +531 -0
  175. lsurf-1.0.0.dist-info/METADATA +381 -0
  176. lsurf-1.0.0.dist-info/RECORD +180 -0
  177. lsurf-1.0.0.dist-info/WHEEL +5 -0
  178. lsurf-1.0.0.dist-info/entry_points.txt +2 -0
  179. lsurf-1.0.0.dist-info/licenses/LICENSE +32 -0
  180. lsurf-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,522 @@
1
+ # The Clear BSD License
2
+ #
3
+ # Copyright (c) 2026 Tobias Heibges
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted (subject to the limitations in the disclaimer
8
+ # below) provided that the following conditions are met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright notice,
11
+ # this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above copyright
14
+ # notice, this list of conditions and the following disclaimer in the
15
+ # documentation and/or other materials provided with the distribution.
16
+ #
17
+ # * Neither the name of the copyright holder nor the names of its
18
+ # contributors may be used to endorse or promote products derived from this
19
+ # software without specific prior written permission.
20
+ #
21
+ # NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
22
+ # THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
+ # CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25
+ # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29
+ # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
30
+ # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ # POSSIBILITY OF SUCH DAMAGE.
33
+
34
+ """
35
+ Signed Distance Handler
36
+
37
+ GPU memory management for signed distance and surface normal kernels.
38
+ This module provides high-level interfaces that handle cuda.to_device(),
39
+ copy_to_host(), and kernel launching for the generic signed distance kernels.
40
+
41
+ These functions enable GPU-accelerated intersection detection for ALL surface
42
+ types by computing signed distance values that can be used for bisection.
43
+ """
44
+
45
+ import numpy as np
46
+ from numpy.typing import NDArray
47
+
48
+ from ..kernels.intersection import (
49
+ kernel_signed_distance,
50
+ kernel_surface_normal,
51
+ )
52
+
53
+ # GPU support is optional
54
+ try:
55
+ from numba import cuda
56
+
57
+ _HAS_CUDA = cuda.is_available()
58
+ except ImportError:
59
+ _HAS_CUDA = False
60
+
61
+ class _FakeCuda:
62
+ @staticmethod
63
+ def is_available():
64
+ return False
65
+
66
+ cuda = _FakeCuda() # type: ignore[assignment]
67
+
68
+
69
+ # Maximum number of parameters supported per surface
70
+ # 64 params allows up to 8 wave components for multi-wave surfaces
71
+ # Layout for multi-wave: 8 header params + 8 waves * 8 params each = 72, using 64
72
+ MAX_SURFACE_PARAMS = 64
73
+
74
+
75
+ def compute_signed_distance_gpu(
76
+ positions: NDArray[np.float32],
77
+ geometry_id: int,
78
+ params: tuple | NDArray[np.float32],
79
+ threads_per_block: int = 256,
80
+ ) -> NDArray[np.float32]:
81
+ """
82
+ GPU-accelerated signed distance computation for any surface type.
83
+
84
+ Parameters
85
+ ----------
86
+ positions : ndarray, shape (N, 3)
87
+ Points to compute signed distance for
88
+ geometry_id : int
89
+ Surface geometry type:
90
+ 1 = Plane
91
+ 2 = Sphere
92
+ 3 = Gerstner wave (flat earth)
93
+ 4 = Curved wave (spherical earth)
94
+ 5 = Multi-wave curved (up to 8 waves)
95
+ 7 = Annular plane (ring-shaped detector)
96
+ params : tuple or ndarray
97
+ Surface parameters from surface.get_gpu_parameters()
98
+ Will be padded to 12 elements if shorter.
99
+ threads_per_block : int, optional
100
+ CUDA threads per block (default 256)
101
+
102
+ Returns
103
+ -------
104
+ signed_distances : ndarray, shape (N,)
105
+ Signed distance from each position to the surface.
106
+ Sign convention depends on surface type (typically positive outside).
107
+ """
108
+ # Convert params to array and pad to MAX_SURFACE_PARAMS
109
+ params_arr = _prepare_params(params)
110
+
111
+ if _HAS_CUDA:
112
+ return _compute_signed_distance_cuda(
113
+ positions, geometry_id, params_arr, threads_per_block
114
+ )
115
+ else:
116
+ return _compute_signed_distance_cpu(positions, geometry_id, params_arr)
117
+
118
+
119
+ def compute_surface_normal_gpu(
120
+ positions: NDArray[np.float32],
121
+ geometry_id: int,
122
+ params: tuple | NDArray[np.float32],
123
+ incoming_directions: NDArray[np.float32] | None = None,
124
+ threads_per_block: int = 256,
125
+ ) -> NDArray[np.float32]:
126
+ """
127
+ GPU-accelerated surface normal computation for any surface type.
128
+
129
+ Parameters
130
+ ----------
131
+ positions : ndarray, shape (N, 3)
132
+ Points on (or near) the surface
133
+ geometry_id : int
134
+ Surface geometry type (1=plane, 2=sphere, 3=gerstner, 4=curved_wave)
135
+ params : tuple or ndarray
136
+ Surface parameters from surface.get_gpu_parameters()
137
+ incoming_directions : ndarray, shape (N, 3), optional
138
+ If provided, normals will be flipped to face incoming rays
139
+ threads_per_block : int, optional
140
+ CUDA threads per block (default 256)
141
+
142
+ Returns
143
+ -------
144
+ normals : ndarray, shape (N, 3)
145
+ Unit normal vectors at each position
146
+ """
147
+ params_arr = _prepare_params(params)
148
+
149
+ if _HAS_CUDA:
150
+ normals = _compute_surface_normal_cuda(
151
+ positions, geometry_id, params_arr, threads_per_block
152
+ )
153
+ else:
154
+ normals = _compute_surface_normal_cpu(positions, geometry_id, params_arr)
155
+
156
+ # Optionally flip normals to face incoming rays
157
+ if incoming_directions is not None:
158
+ dot = np.sum(normals * incoming_directions, axis=1)
159
+ flip_mask = dot > 0
160
+ normals[flip_mask] = -normals[flip_mask]
161
+
162
+ return normals
163
+
164
+
165
+ def _prepare_params(params: tuple | NDArray[np.float32]) -> NDArray[np.float32]:
166
+ """Convert params to float32 array, padded to MAX_SURFACE_PARAMS."""
167
+ if isinstance(params, tuple):
168
+ params_arr = np.array(params, dtype=np.float32)
169
+ else:
170
+ params_arr = params.astype(np.float32)
171
+
172
+ # Pad to MAX_SURFACE_PARAMS
173
+ if len(params_arr) < MAX_SURFACE_PARAMS:
174
+ padded = np.zeros(MAX_SURFACE_PARAMS, dtype=np.float32)
175
+ padded[: len(params_arr)] = params_arr
176
+ return padded
177
+ return params_arr[:MAX_SURFACE_PARAMS]
178
+
179
+
180
+ # =============================================================================
181
+ # CUDA Implementations
182
+ # =============================================================================
183
+
184
+
185
+ def _compute_signed_distance_cuda(
186
+ positions: NDArray[np.float32],
187
+ geometry_id: int,
188
+ params: NDArray[np.float32],
189
+ threads_per_block: int,
190
+ ) -> NDArray[np.float32]:
191
+ """CUDA implementation of signed distance computation."""
192
+ num_points = len(positions)
193
+
194
+ # Allocate output
195
+ signed_dist = np.zeros(num_points, dtype=np.float32)
196
+
197
+ # Transfer to GPU
198
+ d_positions = cuda.to_device(positions.astype(np.float32))
199
+ d_signed_dist = cuda.to_device(signed_dist)
200
+ d_params = cuda.to_device(params)
201
+
202
+ # Launch kernel
203
+ blocks = (num_points + threads_per_block - 1) // threads_per_block
204
+ kernel_signed_distance[blocks, threads_per_block](
205
+ d_positions,
206
+ d_signed_dist,
207
+ geometry_id,
208
+ d_params,
209
+ )
210
+
211
+ cuda.synchronize()
212
+ return d_signed_dist.copy_to_host()
213
+
214
+
215
+ def _compute_surface_normal_cuda(
216
+ positions: NDArray[np.float32],
217
+ geometry_id: int,
218
+ params: NDArray[np.float32],
219
+ threads_per_block: int,
220
+ ) -> NDArray[np.float32]:
221
+ """CUDA implementation of surface normal computation."""
222
+ num_points = len(positions)
223
+
224
+ # Allocate output
225
+ normals = np.zeros((num_points, 3), dtype=np.float32)
226
+
227
+ # Transfer to GPU
228
+ d_positions = cuda.to_device(positions.astype(np.float32))
229
+ d_normals = cuda.to_device(normals)
230
+ d_params = cuda.to_device(params)
231
+
232
+ # Launch kernel
233
+ blocks = (num_points + threads_per_block - 1) // threads_per_block
234
+ kernel_surface_normal[blocks, threads_per_block](
235
+ d_positions,
236
+ d_normals,
237
+ geometry_id,
238
+ d_params,
239
+ )
240
+
241
+ cuda.synchronize()
242
+ return d_normals.copy_to_host()
243
+
244
+
245
+ # =============================================================================
246
+ # CPU Fallback Implementations
247
+ # =============================================================================
248
+
249
+
250
+ def _compute_signed_distance_cpu(
251
+ positions: NDArray[np.float32],
252
+ geometry_id: int,
253
+ params: NDArray[np.float32],
254
+ ) -> NDArray[np.float32]:
255
+ """CPU fallback implementation of signed distance computation."""
256
+ import math
257
+
258
+ num_points = len(positions)
259
+ signed_dist = np.zeros(num_points, dtype=np.float32)
260
+
261
+ x = positions[:, 0]
262
+ y = positions[:, 1]
263
+ z = positions[:, 2]
264
+
265
+ if geometry_id == 1: # Plane
266
+ nx, ny, nz = params[0], params[1], params[2]
267
+ px, py, pz = params[3], params[4], params[5]
268
+ signed_dist = nx * (x - px) + ny * (y - py) + nz * (z - pz)
269
+
270
+ elif geometry_id == 2: # Sphere
271
+ cx, cy, cz = params[0], params[1], params[2]
272
+ r = params[3]
273
+ dx = x - cx
274
+ dy = y - cy
275
+ dz = z - cz
276
+ dist = np.sqrt(dx * dx + dy * dy + dz * dz)
277
+ signed_dist = dist - abs(r)
278
+
279
+ elif geometry_id == 3: # Gerstner wave
280
+ A = params[0]
281
+ k = params[1]
282
+ dir_x, dir_y = params[2], params[3]
283
+ ref_z = params[4]
284
+ phase = params[5]
285
+ time = params[6]
286
+
287
+ GRAVITY = 9.81
288
+ omega = math.sqrt(GRAVITY * k)
289
+ phase_val = k * (dir_x * x + dir_y * y) - omega * time + phase
290
+ wave_height = ref_z + A * np.cos(phase_val)
291
+ signed_dist = z - wave_height
292
+
293
+ elif geometry_id == 4: # Curved wave
294
+ cx, cy, cz = params[0], params[1], params[2]
295
+ earth_radius = params[3]
296
+ A = params[4]
297
+ k = params[5]
298
+ dir_x, dir_y = params[6], params[7]
299
+ time = params[8]
300
+
301
+ dx = x - cx
302
+ dy = y - cy
303
+ dz = z - cz
304
+ dist_from_center = np.sqrt(dx * dx + dy * dy + dz * dz)
305
+
306
+ GRAVITY = 9.81
307
+ omega = math.sqrt(GRAVITY * k)
308
+ dot_val = dir_x * x + dir_y * y
309
+ phase_val = k * dot_val - omega * time
310
+ wave_height = A * np.cos(phase_val)
311
+ surface_radius = earth_radius + wave_height
312
+ signed_dist = dist_from_center - surface_radius
313
+
314
+ elif geometry_id == 5: # Multi-wave curved
315
+ cx, cy, cz = params[0], params[1], params[2]
316
+ earth_radius = params[3]
317
+ time = params[4]
318
+ num_waves = int(params[5])
319
+
320
+ dx = x - cx
321
+ dy = y - cy
322
+ dz = z - cz
323
+ dist_from_center = np.sqrt(dx * dx + dy * dy + dz * dz)
324
+
325
+ GRAVITY = 9.81
326
+ total_wave_height = np.zeros(num_points, dtype=np.float64)
327
+ for i in range(num_waves):
328
+ offset = 8 + i * 8
329
+ A = params[offset + 0]
330
+ k = params[offset + 1]
331
+ dir_x = params[offset + 2]
332
+ dir_y = params[offset + 3]
333
+ phase = params[offset + 4]
334
+
335
+ omega = math.sqrt(GRAVITY * k)
336
+ dot_val = dir_x * x + dir_y * y
337
+ phase_val = k * dot_val - omega * time + phase
338
+ total_wave_height += A * np.cos(phase_val)
339
+
340
+ surface_radius = earth_radius + total_wave_height
341
+ signed_dist = dist_from_center - surface_radius
342
+
343
+ elif geometry_id == 7: # Annular plane
344
+ # Signed distance to infinite plane (bounds checking done separately)
345
+ # params: [nx, ny, nz, cx, cy, cz, ...]
346
+ nx, ny, nz = params[0], params[1], params[2]
347
+ cx, cy, cz = params[3], params[4], params[5]
348
+ signed_dist = nx * (x - cx) + ny * (y - cy) + nz * (z - cz)
349
+
350
+ else:
351
+ # Unknown geometry - return inf
352
+ signed_dist = np.full(num_points, np.inf, dtype=np.float32)
353
+
354
+ return signed_dist.astype(np.float32)
355
+
356
+
357
+ def _compute_surface_normal_cpu(
358
+ positions: NDArray[np.float32],
359
+ geometry_id: int,
360
+ params: NDArray[np.float32],
361
+ ) -> NDArray[np.float32]:
362
+ """CPU fallback implementation of surface normal computation."""
363
+ import math
364
+
365
+ num_points = len(positions)
366
+ normals = np.zeros((num_points, 3), dtype=np.float32)
367
+
368
+ x = positions[:, 0]
369
+ y = positions[:, 1]
370
+ z = positions[:, 2]
371
+
372
+ if geometry_id == 1: # Plane
373
+ normals[:, 0] = params[0]
374
+ normals[:, 1] = params[1]
375
+ normals[:, 2] = params[2]
376
+
377
+ elif geometry_id == 2: # Sphere
378
+ cx, cy, cz = params[0], params[1], params[2]
379
+ r = params[3]
380
+ dx = x - cx
381
+ dy = y - cy
382
+ dz = z - cz
383
+ dist = np.sqrt(dx * dx + dy * dy + dz * dz)
384
+ dist = np.maximum(dist, 1e-12)
385
+ normals[:, 0] = dx / dist
386
+ normals[:, 1] = dy / dist
387
+ normals[:, 2] = dz / dist
388
+ if r < 0:
389
+ normals *= -1
390
+
391
+ elif geometry_id == 3: # Gerstner wave
392
+ A = params[0]
393
+ k = params[1]
394
+ dir_x, dir_y = params[2], params[3]
395
+ phase = params[5]
396
+ time = params[6]
397
+
398
+ GRAVITY = 9.81
399
+ omega = math.sqrt(GRAVITY * k)
400
+ phase_val = k * (dir_x * x + dir_y * y) - omega * time + phase
401
+ sin_theta = np.sin(phase_val)
402
+
403
+ dz_dx = -A * k * dir_x * sin_theta
404
+ dz_dy = -A * k * dir_y * sin_theta
405
+
406
+ nx = -dz_dx
407
+ ny = -dz_dy
408
+ nz = np.ones(num_points, dtype=np.float32)
409
+
410
+ norm = np.sqrt(nx * nx + ny * ny + nz * nz)
411
+ normals[:, 0] = nx / norm
412
+ normals[:, 1] = ny / norm
413
+ normals[:, 2] = nz / norm
414
+
415
+ elif geometry_id == 4: # Curved wave
416
+ cx, cy, cz = params[0], params[1], params[2]
417
+ A = params[4]
418
+ k = params[5]
419
+ dir_x, dir_y = params[6], params[7]
420
+ time = params[8]
421
+
422
+ # Radial direction
423
+ dx = x - cx
424
+ dy = y - cy
425
+ dz = z - cz
426
+ dist = np.sqrt(dx * dx + dy * dy + dz * dz)
427
+ dist = np.maximum(dist, 1e-12)
428
+ radial = np.stack([dx / dist, dy / dist, dz / dist], axis=-1)
429
+
430
+ # Wave perturbation (simplified)
431
+ GRAVITY = 9.81
432
+ omega = math.sqrt(GRAVITY * k)
433
+ dot_val = dir_x * x + dir_y * y
434
+ phase_val = k * dot_val - omega * time
435
+ sin_theta = np.sin(phase_val)
436
+ WA = k * A
437
+
438
+ nx_local = dir_x * WA * sin_theta
439
+ ny_local = dir_y * WA * sin_theta
440
+ nz_local = 1.0 - 0.5 * WA * sin_theta
441
+
442
+ # Combine radial with perturbation
443
+ normals[:, 0] = radial[:, 0] + 0.01 * nx_local
444
+ normals[:, 1] = radial[:, 1] + 0.01 * ny_local
445
+ normals[:, 2] = radial[:, 2] + 0.01 * nz_local
446
+
447
+ # Normalize
448
+ norm = np.sqrt(normals[:, 0] ** 2 + normals[:, 1] ** 2 + normals[:, 2] ** 2)
449
+ norm = np.maximum(norm, 1e-12)
450
+ normals /= norm[:, np.newaxis]
451
+
452
+ elif geometry_id == 5: # Multi-wave curved
453
+ cx, cy, cz = params[0], params[1], params[2]
454
+ time = params[4]
455
+ num_waves = int(params[5])
456
+
457
+ # Radial direction
458
+ dx = x - cx
459
+ dy = y - cy
460
+ dz = z - cz
461
+ dist = np.sqrt(dx * dx + dy * dy + dz * dz)
462
+ dist = np.maximum(dist, 1e-12)
463
+ radial = np.stack([dx / dist, dy / dist, dz / dist], axis=-1)
464
+
465
+ # Accumulate wave perturbations
466
+ GRAVITY = 9.81
467
+ nx_local = np.zeros(num_points, dtype=np.float64)
468
+ ny_local = np.zeros(num_points, dtype=np.float64)
469
+ nz_local = np.ones(num_points, dtype=np.float64)
470
+
471
+ for i in range(num_waves):
472
+ offset = 8 + i * 8
473
+ A = params[offset + 0]
474
+ k = params[offset + 1]
475
+ dir_x = params[offset + 2]
476
+ dir_y = params[offset + 3]
477
+ phase = params[offset + 4]
478
+
479
+ omega = math.sqrt(GRAVITY * k)
480
+ dot_val = dir_x * x + dir_y * y
481
+ phase_val = k * dot_val - omega * time + phase
482
+ sin_theta = np.sin(phase_val)
483
+ WA = k * A
484
+
485
+ nx_local += dir_x * WA * sin_theta
486
+ ny_local += dir_y * WA * sin_theta
487
+ nz_local -= 0.5 * WA * sin_theta
488
+
489
+ # Normalize local normal
490
+ local_norm = np.sqrt(nx_local**2 + ny_local**2 + nz_local**2)
491
+ local_norm = np.maximum(local_norm, 1e-12)
492
+ nx_local /= local_norm
493
+ ny_local /= local_norm
494
+ nz_local /= local_norm
495
+
496
+ # Combine radial with perturbation
497
+ normals[:, 0] = radial[:, 0] + 0.01 * nx_local
498
+ normals[:, 1] = radial[:, 1] + 0.01 * ny_local
499
+ normals[:, 2] = radial[:, 2] + 0.01 * nz_local
500
+
501
+ # Normalize
502
+ norm = np.sqrt(normals[:, 0] ** 2 + normals[:, 1] ** 2 + normals[:, 2] ** 2)
503
+ norm = np.maximum(norm, 1e-12)
504
+ normals /= norm[:, np.newaxis]
505
+
506
+ elif geometry_id == 7: # Annular plane
507
+ # Normal is constant everywhere (stored in params)
508
+ normals[:, 0] = params[0]
509
+ normals[:, 1] = params[1]
510
+ normals[:, 2] = params[2]
511
+
512
+ else:
513
+ # Unknown geometry - default to +Z
514
+ normals[:, 2] = 1.0
515
+
516
+ return normals.astype(np.float32)
517
+
518
+
519
+ __all__ = [
520
+ "compute_signed_distance_gpu",
521
+ "compute_surface_normal_gpu",
522
+ ]