Skip to content

Commit 4d1018b

Browse files
committed
fix nan error in raytracing when using honor_grid
1 parent 03de066 commit 4d1018b

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

fteikpy/_fteik/_ray2d.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,21 @@ def _ray2d(z, x, zgrad, xgrad, zend, xend, zsrc, xsrc, stepsize, max_step, honor
5050
if honor_grid:
5151
fac = shrink(pcur, delta, lower, upper)
5252
pcur -= fac * delta
53+
pcur[0] = min(max(pcur[0], z[0]), z[-1])
54+
pcur[1] = min(max(pcur[1], x[0]), x[-1])
5355

5456
if fac < 1.0:
57+
# Handle precision issues due to fac
58+
pcur[0] = numpy.round(pcur[0], 8)
59+
pcur[1] = numpy.round(pcur[1], 8)
60+
5561
i = numpy.searchsorted(z, pcur[0], side="right") - 1
5662
j = numpy.searchsorted(x, pcur[1], side="right") - 1
5763
lower[0] = z[max(i - 1, 0)] if pcur[0] == z[i] else z[i]
5864
lower[1] = x[max(j - 1, 0)] if pcur[1] == x[j] else x[j]
5965
upper[0] = z[i + 1]
6066
upper[1] = x[j + 1]
6167

62-
# Handle precision issues due to fac
63-
pcur[0] = numpy.round(pcur[0], 8)
64-
pcur[1] = numpy.round(pcur[1], 8)
65-
6668
if (pcur != ray[count - 1]).any():
6769
ray[count] = pcur.copy()
6870
count += 1
@@ -75,6 +77,9 @@ def _ray2d(z, x, zgrad, xgrad, zend, xend, zsrc, xsrc, stepsize, max_step, honor
7577

7678
else:
7779
pcur -= delta
80+
pcur[0] = min(max(pcur[0], z[0]), z[-1])
81+
pcur[1] = min(max(pcur[1], x[0]), x[-1])
82+
7883
ray[count] = pcur.copy()
7984
count += 1
8085

fteikpy/_fteik/_ray3d.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ def _ray3d(
7474
if honor_grid:
7575
fac = shrink(pcur, delta, lower, upper)
7676
pcur -= fac * delta
77+
pcur[0] = min(max(pcur[0], z[0]), z[-1])
78+
pcur[1] = min(max(pcur[1], x[0]), x[-1])
79+
pcur[2] = min(max(pcur[2], y[0]), y[-1])
7780

7881
if fac < 1.0:
82+
# Handle precision issues due to fac
83+
pcur[0] = numpy.round(pcur[0], 8)
84+
pcur[1] = numpy.round(pcur[1], 8)
85+
pcur[2] = numpy.round(pcur[2], 8)
86+
7987
i = numpy.searchsorted(z, pcur[0], side="right") - 1
8088
j = numpy.searchsorted(x, pcur[1], side="right") - 1
8189
k = numpy.searchsorted(y, pcur[2], side="right") - 1
@@ -86,11 +94,6 @@ def _ray3d(
8694
upper[1] = x[j + 1]
8795
upper[2] = y[k + 1]
8896

89-
# Handle precision issues due to fac
90-
pcur[0] = numpy.round(pcur[0], 8)
91-
pcur[1] = numpy.round(pcur[1], 8)
92-
pcur[2] = numpy.round(pcur[2], 8)
93-
9497
if (pcur != ray[count - 1]).any():
9598
ray[count] = pcur.copy()
9699
count += 1
@@ -103,6 +106,10 @@ def _ray3d(
103106

104107
else:
105108
pcur -= delta
109+
pcur[0] = min(max(pcur[0], z[0]), z[-1])
110+
pcur[1] = min(max(pcur[1], x[0]), x[-1])
111+
pcur[2] = min(max(pcur[2], y[0]), y[-1])
112+
106113
ray[count] = pcur.copy()
107114
count += 1
108115

0 commit comments

Comments
 (0)