Skip to content

Commit df1eeb3

Browse files
committed
glass is not a class anymore, it's a numpy array
1 parent 3253438 commit df1eeb3

13 files changed

Lines changed: 166 additions & 129 deletions

openptv_python/calibration.py

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Calibration data structures and functions."""
22

33
import pathlib
4-
from typing import List, Optional
4+
from typing import Optional
55

66
import numpy as np
77
from numba import njit
88

9+
from openptv_python.vec_utils import vec_set
10+
911

1012
@njit
1113
def rotation_matrix(phi: float, omega: float, kappa: float) -> np.ndarray:
@@ -50,11 +52,19 @@ def set_rotation_matrix(self, dm: np.ndarray) -> None:
5052
"""Set the rotation matrix of the camera."""
5153
self.dm = dm
5254

53-
def set_pos(self, pos: List[float]) -> None:
55+
def set_pos(self, pos: np.ndarray) -> None:
5456
"""Set the position of the camera."""
57+
pos = np.array(pos, dtype = np.float64)
58+
59+
if pos.shape != (3,):
60+
raise ValueError(
61+
"Illegal array argument "
62+
+ str(pos)
63+
+ " for x, y, z. Expected array/list of 3 numbers"
64+
)
5565
self.x0, self.y0, self.z0 = pos
5666

57-
def set_angles(self, angles: List[float]) -> None:
67+
def set_angles(self, angles: np.ndarray) -> None:
5868
"""Set the angles of the camera."""
5969
self.omega, self.phi, self.kappa = angles
6070

@@ -91,18 +101,29 @@ def set_back_focal_distance(self, cc: float) -> None:
91101
self.cc = cc
92102

93103

94-
class Glass:
95-
"""Glass data structure."""
104+
# class Glass:
105+
# """Glass data structure."""
106+
107+
# def __init__(self, vec_x=0.0, vec_y=0.0, vec_z=1.0):
108+
# self.vec_x = vec_x
109+
# self.vec_y = vec_y
110+
# self.vec_z = vec_z
96111

97-
def __init__(self, vec_x=0.0, vec_y=0.0, vec_z=1.0):
98-
self.vec_x = vec_x
99-
self.vec_y = vec_y
100-
self.vec_z = vec_z
112+
# def set_glass_vec(self, vec: np.ndarray) -> None:
113+
# """Set the glass vector."""
114+
# self.vec_x, self.vec_y, self.vec_z = vec
101115

102-
def set_glass_vec(self, vec: np.ndarray) -> None:
103-
"""Set the glass vector."""
104-
self.vec_x, self.vec_y, self.vec_z = vec
116+
Glass_dtype = np.dtype(
117+
[
118+
("vec_x", np.float64),
119+
("vec_y", np.float64),
120+
("vec_z", np.float64),
121+
]
122+
)
105123

124+
def default_glass_vec() -> np.ndarray:
125+
"""Return default glass vector."""
126+
return vec_set(0.0, 0.0, 1.0)
106127

107128
class ap_52:
108129
"""Additional parameters for distortion correction."""
@@ -153,7 +174,7 @@ def __init__(self, ext_par=None, int_par=None, glass_par=None, added_par=None, m
153174
if int_par is None:
154175
int_par = Interior()
155176
if glass_par is None:
156-
glass_par = Glass()
177+
glass_par = default_glass_vec()
157178
if added_par is None:
158179
added_par = ap_52()
159180
if mmlut is None:
@@ -187,8 +208,8 @@ def from_file(cls, ori_file: str, add_file: str):
187208

188209
with open(ori_file, "r", encoding="utf-8") as fp:
189210
# Exterior
190-
ret.set_pos([float(x) for x in fp.readline().split()])
191-
ret.set_angles([float(x) for x in fp.readline().split()])
211+
ret.set_pos(np.array([float(x) for x in fp.readline().split()]))
212+
ret.set_angles(np.array([float(x) for x in fp.readline().split()]))
192213

193214
# ret.ext_par.set_pos(np.fromstring(fp.readline(), dtype=float, sep="\t"))
194215
# ret.ext_par.set_angles(np.fromstring(fp.readline(), dtype=float, sep="\t"))
@@ -215,8 +236,7 @@ def from_file(cls, ori_file: str, add_file: str):
215236
# Glass
216237
# skip
217238
fp.readline()
218-
ret.glass_par.set_glass_vec(
219-
np.array([float(x) for x in fp.readline().split()]))
239+
ret.glass_par = np.array([float(x) for x in fp.readline().split()])
220240

221241
# double-check that we have the correct rotation matrix
222242
# self.ext_par.rotation_matrix()
@@ -261,34 +281,30 @@ def set_rotation_matrix(self, dm: np.ndarray) -> None:
261281
raise ValueError("Illegal argument for exterior rotation matrix")
262282
self.ext_par.set_rotation_matrix(dm)
263283

264-
def set_pos(self, x_y_z_np: List[float]) -> None:
284+
def set_pos(self, x_y_z_np: np.ndarray) -> None:
265285
"""
266286
Set exterior position.
267287
268288
Parameter: x_y_z_np - numpy array of 3 elements for x, y, z.
269289
"""
270-
if len(x_y_z_np) != 3:
271-
raise ValueError(
272-
"Illegal array argument "
273-
+ str(x_y_z_np)
274-
+ " for x, y, z. Expected array/list of 3 numbers"
275-
)
276290
self.ext_par.set_pos(x_y_z_np)
277291

278292
def get_pos(self):
279293
"""Return array of 3 elements representing exterior's x, y, z."""
280294
return np.r_[self.ext_par.x0, self.ext_par.y0, self.ext_par.z0]
281295

282-
def set_angles(self, o_p_k_np: List[float]) -> None:
296+
def set_angles(self, o_p_k_np: np.ndarray) -> None:
283297
"""
284298
Set angles (omega, phi, kappa) and recalculates Dmatrix accordingly.
285299
286300
Parameter: o_p_k_np - array of 3 elements.
287301
"""
288-
if len(o_p_k_np) != 3:
302+
o_p_k_np = np.array(o_p_k_np, dtype=np.float64)
303+
304+
if o_p_k_np.shape != (3,):
289305
raise ValueError(
290306
f"Illegal array argument {o_p_k_np} for "
291-
"omega, phi, kappa. Expected array/list of 3 numbers"
307+
"omega, phi, kappa. Expected array or list of 3 float"
292308
)
293309
self.ext_par.set_angles(o_p_k_np)
294310

@@ -394,14 +410,16 @@ def set_glass_vec(self, gvec: np.ndarray):
394410
---------
395411
gvec - a 3-element array, the glass vector.
396412
"""
397-
if len(gvec) != 3:
398-
raise ValueError("Expected a 3-element list")
413+
gvec = np.array(gvec, dtype=np.float64)
414+
415+
if gvec.shape != (3,):
416+
raise ValueError("Expected a 3-element list or array")
399417

400-
self.glass_par.set_glass_vec(gvec)
418+
self.glass_par = gvec
401419

402-
def get_glass_vec(self):
403-
"""Return the glass vector, a 3-element array."""
404-
return [self.glass_par.vec_x, self.glass_par.vec_y, self.glass_par.vec_z]
420+
def get_glass_vec(self) -> np.ndarray:
421+
"""Return the glass vector, a 3-element array of Glass_dtype."""
422+
return self.glass_par
405423

406424
def set_added_par(self, listpar: np.ndarray | list):
407425
"""Set added par from an numpy array of parameters."""
@@ -423,7 +441,7 @@ def copy(self, new_copy):
423441
def write_ori(
424442
ext_par: Exterior,
425443
int_par: Interior,
426-
glass: Glass,
444+
glass: np.ndarray,
427445
added_par: ap_52,
428446
filename: str,
429447
add_file: Optional[str],
@@ -438,7 +456,7 @@ def write_ori(
438456
fp.write(f"{row[0]:.7f} {row[1]:.7f} {row[2]:.7f}\n")
439457
fp.write(f"\n{int_par.xh:.4f} {int_par.yh:.4f}\n{int_par.cc:.4f}\n")
440458
fp.write(
441-
f"\n{glass.vec_x:.15f} {glass.vec_y:.15f} {glass.vec_z:.15f}\n")
459+
f"\n{glass[0]:.15f} {glass[1]:.15f} {glass[2]:.15f}\n")
442460

443461
if add_file is None:
444462
return success
@@ -491,7 +509,7 @@ def compare_interior(i1: Interior, i2: Interior) -> bool:
491509
return i1.xh == i2.xh and i1.yh == i2.yh and i1.cc == i2.cc
492510

493511

494-
def compare_glass(g1: Glass, g2: Glass):
512+
def compare_glass(g1: np.ndarray, g2: np.ndarray) -> bool:
495513
"""Compare `Glass` parameters.
496514
497515
objects that need to be compared. The function then returns `1` if all
@@ -500,14 +518,14 @@ def compare_glass(g1: Glass, g2: Glass):
500518
501519
Args:
502520
----
503-
g1 (_type_): _description_
504-
g2 (_type_): _description_
521+
g1 (Glass_dtype): vector pointing from the 3D origin to the surface of the glass
522+
g2 (Glass_dtype): another vector for comparison
505523
506524
Returns
507525
-------
508-
_type_: _description_
526+
bool: True if vectors are identical, False otherwise
509527
"""
510-
return g1.vec_x == g2.vec_x and g1.vec_y == g2.vec_y and g1.vec_z == g2.vec_z
528+
return np.array_equal(g1, g2)
511529

512530

513531
def compare_calibration(c1: Calibration, c2: Calibration) -> bool:

openptv_python/imgcoord.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def flat_image_coord(
2727
-------
2828
_type_: _description_
2929
"""
30+
if orig_pos.shape != (3,):
31+
raise ValueError("orig_pos must be a 3D vector")
32+
3033
cal_t = Calibration(mmlut = cal.mmlut)
3134

3235
# This block calculate 3D position in an imaginary air-filled space,

openptv_python/multimed.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55
from numba import njit
66

7-
from .calibration import Calibration, Exterior, Glass
7+
from .calibration import Calibration, Exterior
88
from .parameters import (
99
ControlPar,
1010
MultimediaPar,
@@ -116,7 +116,7 @@ def fast_multimed_r_nlay(
116116

117117

118118
def trans_cam_point(
119-
ex: Exterior, mm: MultimediaPar, glass: Glass, pos: np.ndarray
119+
ex: Exterior, mm: MultimediaPar, glass_dir: np.ndarray, pos: np.ndarray
120120
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, float]:
121121
"""Transform the camera and point coordinates to the glass coordinates.
122122
@@ -128,14 +128,13 @@ def trans_cam_point(
128128
pos_t, cross_p, cross_c = trans_cam_point(ex, mm, glass, pos, ex_t)
129129
"""
130130
origin = np.array([ex.x0, ex.y0, ex.z0], dtype=np.float64)
131-
glass_dir = np.array([glass.vec_x, glass.vec_y, glass.vec_z], dtype=np.float64)
132131
pos = pos.astype(np.float64)
133132

134133
return fast_trans_cam_point(
135134
origin, mm.d[0], glass_dir, pos)
136135

137136

138-
@njit(fastmath=True)
137+
# @njit(fastmath=True)
139138
def fast_trans_cam_point(
140139
primary_point: np.ndarray,
141140
d: float,
@@ -175,7 +174,7 @@ def fast_trans_cam_point(
175174
def back_trans_point(
176175
pos_t: np.ndarray,
177176
mm: MultimediaPar,
178-
glass: Glass,
177+
glass: np.ndarray,
179178
cross_p: np.ndarray,
180179
cross_c: np.ndarray,
181180
) -> np.ndarray:
@@ -194,12 +193,10 @@ def back_trans_point(
194193
-------
195194
A numpy array representing the position of the point in the camera coordinate system.
196195
"""
197-
glass_direction = np.array([glass.vec_x, glass.vec_y, glass.vec_z], dtype=np.float64)
198-
199-
return fast_back_trans_point(glass_direction, mm.d[0], cross_c, cross_p, pos_t)
196+
return fast_back_trans_point(glass, mm.d[0], cross_c, cross_p, pos_t)
200197

201198
@njit
202-
def fast_back_trans_point(glass_direction: np.ndarray, d: float, cross_c, cross_p, pos_t) -> np.ndarray:
199+
def fast_back_trans_point(glass_direction: np.recarray, d: float, cross_c, cross_p, pos_t) -> np.ndarray:
203200
"""Run numba faster version of back projection."""
204201
# Calculate the glass direction vector
205202

0 commit comments

Comments
 (0)