Skip to content

Commit 87898ca

Browse files
committed
better numba
1 parent 27fbcaf commit 87898ca

2 files changed

Lines changed: 31 additions & 74 deletions

File tree

openptv_python/multimed.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def multimed_r_nlay(cal: Calibration, mm: MultimediaPar, pos: np.ndarray) -> flo
5656
return mmf
5757

5858

59-
@njit
59+
@njit(fastmath=True, cache=True, nogil=True)
6060
def fast_multimed_r_nlay(
6161
nlay: int,
6262
n1: float,
@@ -134,7 +134,7 @@ def trans_cam_point(
134134
origin, mm.d[0], glass_dir, pos)
135135

136136

137-
@njit(fastmath=True)
137+
@njit(fastmath=True, cache=True, nogil=True)
138138
def fast_trans_cam_point(
139139
primary_point: np.ndarray,
140140
d: float,
@@ -195,7 +195,7 @@ def back_trans_point(
195195
"""
196196
return fast_back_trans_point(glass, mm.d[0], cross_c, cross_p, pos_t)
197197

198-
@njit
198+
@njit(fastmath=True, cache=True, nogil=True)
199199
def fast_back_trans_point(glass_direction: np.ndarray, d: float, cross_c, cross_p, pos_t) -> np.ndarray:
200200
"""Run numba faster version of back projection."""
201201
# Calculate the glass direction vector
@@ -226,7 +226,7 @@ def fast_back_trans_point(glass_direction: np.ndarray, d: float, cross_c, cross_
226226

227227
return pos
228228

229-
@njit
229+
@njit(fastmath=True, cache=True, nogil=True)
230230
def move_along_ray(glob_z: float, vertex: np.ndarray, direct: np.ndarray) -> np.ndarray:
231231
"""Move along the ray to the global z plane.
232232

openptv_python/trafo.py

Lines changed: 27 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def fast_arr_metric_to_pixel(
144144

145145
return pixel
146146

147-
147+
@njit(fastmath=True, cache=True, nogil=True)
148148
def distort_brown_affine(x: float,
149149
y: float,
150150
ap: np.recarray,
@@ -153,72 +153,29 @@ def distort_brown_affine(x: float,
153153
if x == 0 and y == 0:
154154
return 0, 0
155155

156-
tmp = fast_distort_brown_affine(x, y, ap.k1, ap.k2, ap.k3,
157-
ap.p1, ap.p2, ap.she, ap.scx)
158-
return tmp[0], tmp[1]
159-
160-
# print(f"x {x}, y {y}")
161-
162-
163-
# @njit(float64[:]
164-
# (float64,float64,float64,float64,float64,
165-
# float64,float64,float64,float64))
166-
def fast_distort_brown_affine(
167-
x: float,
168-
y: float,
169-
k1: float,
170-
k2: float,
171-
k3: float,
172-
p1: float,
173-
p2: float,
174-
she: float,
175-
scx: float,
176-
) -> np.ndarray:
177-
"""Distort a point using the Brown affine model."""
178156
r = sqrt(x**2 + y**2)
179157

180158
x += (
181-
x * (k1 * r**2 + k2 * r**4 + k3 * r**6)
182-
+ p1 * (r**2 + 2 * x**2)
183-
+ 2 * p2 * x * y
159+
x * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
160+
+ ap.p1 * (r**2 + 2 * x**2)
161+
+ 2 * ap.p2 * x * y
184162
)
185163
y += (
186-
y * (k1 * r**2 + k2 * r**4 + k3 * r**6)
187-
+ p2 * (r**2 + 2 * y**2)
188-
+ 2 * p1 * x * y
164+
y * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
165+
+ ap.p2 * (r**2 + 2 * y**2)
166+
+ 2 * ap.p1 * x * y
189167
)
190168

191-
# print(f"x {x}, y {y}")
192-
# print(f"ap.she {ap.she} ap.scx {ap.scx}")
193-
194-
x1 = scx * x - sin(she) * y
195-
y1 = cos(she) * y
196-
197-
# print(f"x1 {x1}, y1 {y1}")
198-
199-
return np.array([x1, y1])
169+
x1 = ap.scx * x - sin(ap.she) * y
170+
y1 = cos(ap.she) * y
200171

172+
return x1, y1
201173

174+
@njit(fastmath=True, cache=True, nogil=True)
202175
def correct_brown_affine(
203176
x: float, y: float, ap: np.recarray, tol: float = 1e-5
204177
) -> Tuple[float, float]:
205178
"""Correct a distorted point using the Brown affine model."""
206-
return fast_correct_brown_affine(x, y, ap.k1, ap.k2, ap.k3, ap.p1, ap.p2, ap.she, ap.scx, tol)
207-
208-
209-
@njit
210-
def fast_correct_brown_affine(
211-
x: float,
212-
y: float,
213-
k1: float,
214-
k2: float,
215-
k3: float,
216-
p1: float,
217-
p2: float,
218-
she: float,
219-
scx: float,
220-
tol: float = 1e-5) -> Tuple[float, float]:
221-
"""Correct a distorted point using the Brown affine model."""
222179
r, rq, xq, yq = 0.0, 0.0, x, y
223180
itnum = 0
224181

@@ -230,17 +187,17 @@ def fast_correct_brown_affine(
230187
while True:
231188
r = rq
232189
xq = (
233-
(x + yq * np.sin(she)) / scx
234-
- xq * (k1 * r**2 + k2 * r**4 + k3 * r**6)
235-
- p1 * (r**2 + 2 * xq**2)
236-
- 2 * p2 * xq * yq
190+
(x + yq * np.sin(ap.she)) / ap.scx
191+
- xq * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
192+
- ap.p1 * (r**2 + 2 * xq**2)
193+
- 2 * ap.p2 * xq * yq
237194
)
238195

239196
yq = (
240-
y / np.cos(she)
241-
- yq * (k1 * r**2 + k2 * r**4 + k3 * r**6)
242-
- p2 * (r**2 + 2 * yq**2)
243-
- 2 * p1 * xq * yq
197+
y / np.cos(ap.she)
198+
- yq * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
199+
- ap.p2 * (r**2 + 2 * yq**2)
200+
- 2 * ap.p1 * xq * yq
244201
)
245202

246203
rq = np.sqrt(xq**2 + yq**2)
@@ -255,17 +212,17 @@ def fast_correct_brown_affine(
255212

256213
r = rq
257214
x1 = (
258-
(x + yq * np.sin(she)) / scx
259-
- xq * (k1 * r**2 + k2 * r**4 + k3 * r**6)
260-
- p1 * (r**2 + 2 * xq**2)
261-
- 2 * p2 * xq * yq
215+
(x + yq * np.sin(ap.she)) / ap.scx
216+
- xq * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
217+
- ap.p1 * (r**2 + 2 * xq**2)
218+
- 2 * ap.p2 * xq * yq
262219
)
263220

264221
y1 = (
265-
y / np.cos(she)
266-
- yq * (k1 * r**2 + k2 * r**4 + k3 * r**6)
267-
- p2 * (r**2 + 2 * yq**2)
268-
- 2 * p1 * xq * yq
222+
y / np.cos(ap.she)
223+
- yq * (ap.k1 * r**2 + ap.k2 * r**4 + ap.k3 * r**6)
224+
- ap.p2 * (r**2 + 2 * yq**2)
225+
- 2 * ap.p1 * xq * yq
269226
)
270227

271228
return x1, y1

0 commit comments

Comments
 (0)