From df1eeb3271a5da7a78011e6b9090f2641e645a64 Mon Sep 17 00:00:00 2001 From: Alex Liberzon Date: Fri, 5 Jan 2024 22:37:24 +0200 Subject: [PATCH] glass is not a class anymore, it's a numpy array --- openptv_python/calibration.py | 98 ++++++++++++++++++------------- openptv_python/imgcoord.py | 3 + openptv_python/multimed.py | 15 ++--- openptv_python/orientation.py | 72 +++++++++++------------ openptv_python/ray_tracing.py | 2 +- tests/test_calibration_binding.py | 13 ++-- tests/test_calibration_class.py | 8 +-- tests/test_epipolar.py | 35 ++++++----- tests/test_img_coord.py | 3 +- tests/test_imgcoord.py | 11 +++- tests/test_multimedia_r_nlay.py | 10 +++- tests/test_ray_tracing.py | 13 ++-- tests/test_trans_cam_point.py | 12 ++-- 13 files changed, 166 insertions(+), 129 deletions(-) diff --git a/openptv_python/calibration.py b/openptv_python/calibration.py index 478663e..bb59f8e 100644 --- a/openptv_python/calibration.py +++ b/openptv_python/calibration.py @@ -1,11 +1,13 @@ """Calibration data structures and functions.""" import pathlib -from typing import List, Optional +from typing import Optional import numpy as np from numba import njit +from openptv_python.vec_utils import vec_set + @njit def rotation_matrix(phi: float, omega: float, kappa: float) -> np.ndarray: @@ -50,11 +52,19 @@ def set_rotation_matrix(self, dm: np.ndarray) -> None: """Set the rotation matrix of the camera.""" self.dm = dm - def set_pos(self, pos: List[float]) -> None: + def set_pos(self, pos: np.ndarray) -> None: """Set the position of the camera.""" + pos = np.array(pos, dtype = np.float64) + + if pos.shape != (3,): + raise ValueError( + "Illegal array argument " + + str(pos) + + " for x, y, z. Expected array/list of 3 numbers" + ) self.x0, self.y0, self.z0 = pos - def set_angles(self, angles: List[float]) -> None: + def set_angles(self, angles: np.ndarray) -> None: """Set the angles of the camera.""" self.omega, self.phi, self.kappa = angles @@ -91,18 +101,29 @@ def set_back_focal_distance(self, cc: float) -> None: self.cc = cc -class Glass: - """Glass data structure.""" +# class Glass: +# """Glass data structure.""" + +# def __init__(self, vec_x=0.0, vec_y=0.0, vec_z=1.0): +# self.vec_x = vec_x +# self.vec_y = vec_y +# self.vec_z = vec_z - def __init__(self, vec_x=0.0, vec_y=0.0, vec_z=1.0): - self.vec_x = vec_x - self.vec_y = vec_y - self.vec_z = vec_z +# def set_glass_vec(self, vec: np.ndarray) -> None: +# """Set the glass vector.""" +# self.vec_x, self.vec_y, self.vec_z = vec - def set_glass_vec(self, vec: np.ndarray) -> None: - """Set the glass vector.""" - self.vec_x, self.vec_y, self.vec_z = vec +Glass_dtype = np.dtype( + [ + ("vec_x", np.float64), + ("vec_y", np.float64), + ("vec_z", np.float64), + ] +) +def default_glass_vec() -> np.ndarray: + """Return default glass vector.""" + return vec_set(0.0, 0.0, 1.0) class ap_52: """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 if int_par is None: int_par = Interior() if glass_par is None: - glass_par = Glass() + glass_par = default_glass_vec() if added_par is None: added_par = ap_52() if mmlut is None: @@ -187,8 +208,8 @@ def from_file(cls, ori_file: str, add_file: str): with open(ori_file, "r", encoding="utf-8") as fp: # Exterior - ret.set_pos([float(x) for x in fp.readline().split()]) - ret.set_angles([float(x) for x in fp.readline().split()]) + ret.set_pos(np.array([float(x) for x in fp.readline().split()])) + ret.set_angles(np.array([float(x) for x in fp.readline().split()])) # ret.ext_par.set_pos(np.fromstring(fp.readline(), dtype=float, sep="\t")) # 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): # Glass # skip fp.readline() - ret.glass_par.set_glass_vec( - np.array([float(x) for x in fp.readline().split()])) + ret.glass_par = np.array([float(x) for x in fp.readline().split()]) # double-check that we have the correct rotation matrix # self.ext_par.rotation_matrix() @@ -261,34 +281,30 @@ def set_rotation_matrix(self, dm: np.ndarray) -> None: raise ValueError("Illegal argument for exterior rotation matrix") self.ext_par.set_rotation_matrix(dm) - def set_pos(self, x_y_z_np: List[float]) -> None: + def set_pos(self, x_y_z_np: np.ndarray) -> None: """ Set exterior position. Parameter: x_y_z_np - numpy array of 3 elements for x, y, z. """ - if len(x_y_z_np) != 3: - raise ValueError( - "Illegal array argument " - + str(x_y_z_np) - + " for x, y, z. Expected array/list of 3 numbers" - ) self.ext_par.set_pos(x_y_z_np) def get_pos(self): """Return array of 3 elements representing exterior's x, y, z.""" return np.r_[self.ext_par.x0, self.ext_par.y0, self.ext_par.z0] - def set_angles(self, o_p_k_np: List[float]) -> None: + def set_angles(self, o_p_k_np: np.ndarray) -> None: """ Set angles (omega, phi, kappa) and recalculates Dmatrix accordingly. Parameter: o_p_k_np - array of 3 elements. """ - if len(o_p_k_np) != 3: + o_p_k_np = np.array(o_p_k_np, dtype=np.float64) + + if o_p_k_np.shape != (3,): raise ValueError( f"Illegal array argument {o_p_k_np} for " - "omega, phi, kappa. Expected array/list of 3 numbers" + "omega, phi, kappa. Expected array or list of 3 float" ) self.ext_par.set_angles(o_p_k_np) @@ -394,14 +410,16 @@ def set_glass_vec(self, gvec: np.ndarray): --------- gvec - a 3-element array, the glass vector. """ - if len(gvec) != 3: - raise ValueError("Expected a 3-element list") + gvec = np.array(gvec, dtype=np.float64) + + if gvec.shape != (3,): + raise ValueError("Expected a 3-element list or array") - self.glass_par.set_glass_vec(gvec) + self.glass_par = gvec - def get_glass_vec(self): - """Return the glass vector, a 3-element array.""" - return [self.glass_par.vec_x, self.glass_par.vec_y, self.glass_par.vec_z] + def get_glass_vec(self) -> np.ndarray: + """Return the glass vector, a 3-element array of Glass_dtype.""" + return self.glass_par def set_added_par(self, listpar: np.ndarray | list): """Set added par from an numpy array of parameters.""" @@ -423,7 +441,7 @@ def copy(self, new_copy): def write_ori( ext_par: Exterior, int_par: Interior, - glass: Glass, + glass: np.ndarray, added_par: ap_52, filename: str, add_file: Optional[str], @@ -438,7 +456,7 @@ def write_ori( fp.write(f"{row[0]:.7f} {row[1]:.7f} {row[2]:.7f}\n") fp.write(f"\n{int_par.xh:.4f} {int_par.yh:.4f}\n{int_par.cc:.4f}\n") fp.write( - f"\n{glass.vec_x:.15f} {glass.vec_y:.15f} {glass.vec_z:.15f}\n") + f"\n{glass[0]:.15f} {glass[1]:.15f} {glass[2]:.15f}\n") if add_file is None: return success @@ -491,7 +509,7 @@ def compare_interior(i1: Interior, i2: Interior) -> bool: return i1.xh == i2.xh and i1.yh == i2.yh and i1.cc == i2.cc -def compare_glass(g1: Glass, g2: Glass): +def compare_glass(g1: np.ndarray, g2: np.ndarray) -> bool: """Compare `Glass` parameters. objects that need to be compared. The function then returns `1` if all @@ -500,14 +518,14 @@ def compare_glass(g1: Glass, g2: Glass): Args: ---- - g1 (_type_): _description_ - g2 (_type_): _description_ + g1 (Glass_dtype): vector pointing from the 3D origin to the surface of the glass + g2 (Glass_dtype): another vector for comparison Returns ------- - _type_: _description_ + bool: True if vectors are identical, False otherwise """ - return g1.vec_x == g2.vec_x and g1.vec_y == g2.vec_y and g1.vec_z == g2.vec_z + return np.array_equal(g1, g2) def compare_calibration(c1: Calibration, c2: Calibration) -> bool: diff --git a/openptv_python/imgcoord.py b/openptv_python/imgcoord.py index c59eafd..cba9a76 100644 --- a/openptv_python/imgcoord.py +++ b/openptv_python/imgcoord.py @@ -27,6 +27,9 @@ def flat_image_coord( ------- _type_: _description_ """ + if orig_pos.shape != (3,): + raise ValueError("orig_pos must be a 3D vector") + cal_t = Calibration(mmlut = cal.mmlut) # This block calculate 3D position in an imaginary air-filled space, diff --git a/openptv_python/multimed.py b/openptv_python/multimed.py index 506d747..75e9f77 100644 --- a/openptv_python/multimed.py +++ b/openptv_python/multimed.py @@ -4,7 +4,7 @@ import numpy as np from numba import njit -from .calibration import Calibration, Exterior, Glass +from .calibration import Calibration, Exterior from .parameters import ( ControlPar, MultimediaPar, @@ -116,7 +116,7 @@ def fast_multimed_r_nlay( def trans_cam_point( - ex: Exterior, mm: MultimediaPar, glass: Glass, pos: np.ndarray + ex: Exterior, mm: MultimediaPar, glass_dir: np.ndarray, pos: np.ndarray ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, float]: """Transform the camera and point coordinates to the glass coordinates. @@ -128,14 +128,13 @@ def trans_cam_point( pos_t, cross_p, cross_c = trans_cam_point(ex, mm, glass, pos, ex_t) """ origin = np.array([ex.x0, ex.y0, ex.z0], dtype=np.float64) - glass_dir = np.array([glass.vec_x, glass.vec_y, glass.vec_z], dtype=np.float64) pos = pos.astype(np.float64) return fast_trans_cam_point( origin, mm.d[0], glass_dir, pos) -@njit(fastmath=True) +# @njit(fastmath=True) def fast_trans_cam_point( primary_point: np.ndarray, d: float, @@ -175,7 +174,7 @@ def fast_trans_cam_point( def back_trans_point( pos_t: np.ndarray, mm: MultimediaPar, - glass: Glass, + glass: np.ndarray, cross_p: np.ndarray, cross_c: np.ndarray, ) -> np.ndarray: @@ -194,12 +193,10 @@ def back_trans_point( ------- A numpy array representing the position of the point in the camera coordinate system. """ - glass_direction = np.array([glass.vec_x, glass.vec_y, glass.vec_z], dtype=np.float64) - - return fast_back_trans_point(glass_direction, mm.d[0], cross_c, cross_p, pos_t) + return fast_back_trans_point(glass, mm.d[0], cross_c, cross_p, pos_t) @njit -def fast_back_trans_point(glass_direction: np.ndarray, d: float, cross_c, cross_p, pos_t) -> np.ndarray: +def fast_back_trans_point(glass_direction: np.recarray, d: float, cross_c, cross_p, pos_t) -> np.ndarray: """Run numba faster version of back projection.""" # Calculate the glass direction vector diff --git a/openptv_python/orientation.py b/openptv_python/orientation.py index b8419f4..e2e4465 100644 --- a/openptv_python/orientation.py +++ b/openptv_python/orientation.py @@ -125,7 +125,7 @@ def weighted_dumbbell_precision( dtot += tmp if pt % 2 == 1: - dist = np.linalg.norm(res[0] - res[1]) + dist = float(np.linalg.norm(res[0] - res[1])) len_err_tot += 1.0 - float( db_length / dist if dist > db_length else dist / db_length ) @@ -275,18 +275,18 @@ def orient( else: numbers = 16 - glass_dir = vec_set(cal.glass_par.vec_x, cal.glass_par.vec_y, cal.glass_par.vec_z) + glass_dir = cal.glass_par nGl = vec_norm(glass_dir) - e1_x = 2 * cal.glass_par.vec_z - 3 * cal.glass_par.vec_x - e1_y = 3 * cal.glass_par.vec_x - 1 * cal.glass_par.vec_z - e1_z = 1 * cal.glass_par.vec_y - 2 * cal.glass_par.vec_y + e1_x = 2 * cal.glass_par[2] - 3 * cal.glass_par[0] + e1_y = 3 * cal.glass_par[0] - 1 * cal.glass_par[2] + e1_z = 1 * cal.glass_par[1] - 2 * cal.glass_par[1] tmp_vec = vec_set(e1_x, e1_y, e1_z) e1 = unit_vector(tmp_vec) - e2_x = e1_y * cal.glass_par.vec_z - e1_z * cal.glass_par.vec_x - e2_y = e1_z * cal.glass_par.vec_x - e1_x * cal.glass_par.vec_z - e2_z = e1_x * cal.glass_par.vec_y - e1_y * cal.glass_par.vec_y + e2_x = e1_y * cal.glass_par[2] - e1_z * cal.glass_par[0] + e2_y = e1_z * cal.glass_par[0] - e1_x * cal.glass_par[2] + e2_z = e1_x * cal.glass_par[1] - e1_y * cal.glass_par[1] tmp_vec = vec_set(e2_x, e2_y, e2_z) e2 = unit_vector(tmp_vec) @@ -309,9 +309,9 @@ def orient( ] # backup for changing back and forth - safety_x = cal.glass_par.vec_x - safety_y = cal.glass_par.vec_y - safety_z = cal.glass_par.vec_z + safety_x = cal.glass_par[0] + safety_y = cal.glass_par[1] + safety_z = cal.glass_par[2] itnum = 0 stopflag = False @@ -391,46 +391,46 @@ def orient( # xp, yp = 0.0, 0.0 # xc, yc = fix[i][0], fix[i][1] # al, be, ga = cal.alpha, cal.beta, cal.gamma - # safety_x, safety_y, safety_z = cal.glass_par.vec_x, cal.glass_par.vec_y, cal.glass_par.vec_z + # safety_x, safety_y, safety_z = cal.glass_par[0], cal.glass_par[1], cal.glass_par[2] # nGl = cal.glass_par.n / cal.air_par.n cal.int_par.cc -= dm # al += dm - cal.glass_par.vec_x += e1[0] * nGl * dm - cal.glass_par.vec_y += e1[1] * nGl * dm - cal.glass_par.vec_z += e1[2] * nGl * dm + cal.glass_par[0] += e1[0] * nGl * dm + cal.glass_par[1] += e1[1] * nGl * dm + cal.glass_par[2] += e1[2] * nGl * dm xpd, ypd = img_coord(fix[i], cal, cpar.mm) X[n][16] = (xpd - xp) / dm X[n + 1][16] = (ypd - yp) / dm # al -= dm - cal.glass_par.vec_x = safety_x - cal.glass_par.vec_y = safety_y - cal.glass_par.vec_z = safety_z + cal.glass_par[0] = safety_x + cal.glass_par[1] = safety_y + cal.glass_par[2] = safety_z # be += dm - cal.glass_par.vec_x += e2[0] * nGl * dm - cal.glass_par.vec_y += e2[1] * nGl * dm - cal.glass_par.vec_z += e2[2] * nGl * dm + cal.glass_par[0] += e2[0] * nGl * dm + cal.glass_par[1] += e2[1] * nGl * dm + cal.glass_par[2] += e2[2] * nGl * dm xpd, ypd = img_coord(fix[i], cal, cpar.mm) X[n][17] = (xpd - xp) / dm X[n + 1][17] = (ypd - yp) / dm # be -= dm - cal.glass_par.vec_x = safety_x - cal.glass_par.vec_y = safety_y - cal.glass_par.vec_z = safety_z + cal.glass_par[0] = safety_x + cal.glass_par[1] = safety_y + cal.glass_par[2] = safety_z # ga += dm - cal.glass_par.vec_x += cal.glass_par.vec_x * nGl * dm - cal.glass_par.vec_y += cal.glass_par.vec_y * nGl * dm - cal.glass_par.vec_z += cal.glass_par.vec_z * nGl * dm + cal.glass_par[0] += cal.glass_par[0] * nGl * dm + cal.glass_par[1] += cal.glass_par[1] * nGl * dm + cal.glass_par[2] += cal.glass_par[2] * nGl * dm xpd, ypd = img_coord(fix[i], cal, cpar.mm) X[n][18] = (xpd - xp) / dm X[n + 1][18] = (ypd - yp) / dm # ga -= dm - cal.glass_par.vec_x = safety_x - cal.glass_par.vec_y = safety_y - cal.glass_par.vec_z = safety_z + cal.glass_par[0] = safety_x + cal.glass_par[1] = safety_y + cal.glass_par[2] = safety_z y[n] = xc - xp y[n + 1] = yc - yp @@ -543,12 +543,12 @@ def orient( cal.added_par.she += beta[15] if flags.interfflag: - cal.glass_par.vec_x += e1[0] * nGl * beta[16] - cal.glass_par.vec_y += e1[1] * nGl * beta[16] - cal.glass_par.vec_z += e1[2] * nGl * beta[16] - cal.glass_par.vec_x += e2[0] * nGl * beta[17] - cal.glass_par.vec_y += e2[1] * nGl * beta[17] - cal.glass_par.vec_z += e2[2] * nGl * beta[17] + cal.glass_par[0] += e1[0] * nGl * beta[16] + cal.glass_par[1] += e1[1] * nGl * beta[16] + cal.glass_par[2] += e1[2] * nGl * beta[16] + cal.glass_par[0] += e2[0] * nGl * beta[17] + cal.glass_par[1] += e2[1] * nGl * beta[17] + cal.glass_par[2] += e2[2] * nGl * beta[17] # def compute_residuals(X, y, beta, n_obs, numbers, NPAR, XPX, P, cal, cal_in, stopflag): # Xbeta = np.zeros((n_obs, 1)) diff --git a/openptv_python/ray_tracing.py b/openptv_python/ray_tracing.py index 5755330..961de1a 100644 --- a/openptv_python/ray_tracing.py +++ b/openptv_python/ray_tracing.py @@ -50,7 +50,7 @@ def ray_tracing( _type_: _description_ """ primary_point = np.r_[cal.ext_par.x0, cal.ext_par.y0, cal.ext_par.z0] - glass = np.r_[cal.glass_par.vec_x, cal.glass_par.vec_y, cal.glass_par.vec_z] + glass = cal.glass_par return fast_ray_tracing( x, y, diff --git a/tests/test_calibration_binding.py b/tests/test_calibration_binding.py index 2899985..5c016c9 100644 --- a/tests/test_calibration_binding.py +++ b/tests/test_calibration_binding.py @@ -9,7 +9,6 @@ from openptv_python.calibration import ( Calibration, Exterior, - Glass, Interior, ap_52, compare_addpar, @@ -46,7 +45,7 @@ def test_full_instantiate(self): Ext = Exterior(x0=1.0, y0=3.0, z0=5.0, omega=2.0, phi=4.0, kappa=6.0) In = Interior(xh=3.0, yh=9.0, cc=15.0) - G = Glass(vec_x=glass[0], vec_y=glass[1], vec_z=glass[2]) + G = np.array(glass) addpar = ap_52( k1=rad_dist[0], k2=rad_dist[1], @@ -70,7 +69,7 @@ def test_full_instantiate(self): np.testing.assert_array_equal(rad_dist, cal.get_radial_distortion()) np.testing.assert_array_equal(decent, cal.get_decentering()) np.testing.assert_array_equal(affine, cal.get_affine()) - np.testing.assert_array_equal(glass, cal.get_glass_vec()) + np.testing.assert_array_equal(G, cal.get_glass_vec()) def test_calibration_instantiation(self): """Filling a calibration object by reading ori files.""" @@ -94,7 +93,7 @@ def test_calibration_instantiation(self): def test_set_pos(self): """Set exterior position, only for admissible values.""" # test set_pos() by passing a np array of 3 elements - new_np = [111.1111, 222.2222, 333.3333] + new_np = np.r_[111.1111, 222.2222, 333.3333] self.cal.set_pos(new_np) # test getting position and assert that position is equal to set position @@ -154,11 +153,11 @@ def test_set_decentering(self): def test_set_glass(self): """Set glass vector, only for admissible values.""" - new_gv = [1.0, 2.0, 3.0] - self.cal.set_glass_vec(new_gv) + new_gv = np.array([1.0, 2.0, 3.0]) + self.cal.glass_par = new_gv np.testing.assert_array_equal(new_gv, self.cal.get_glass_vec()) - self.assertRaises(ValueError, self.cal.set_glass_vec, [0,0]) + self.assertRaises(ValueError, self.cal.set_glass_vec, [0, 0]) self.assertRaises(ValueError, self.cal.set_glass_vec, [1]) diff --git a/tests/test_calibration_class.py b/tests/test_calibration_class.py index e5bc0d4..1d5fb22 100644 --- a/tests/test_calibration_class.py +++ b/tests/test_calibration_class.py @@ -6,7 +6,6 @@ from openptv_python.calibration import ( Calibration, Exterior, - Glass, Interior, ap_52, mm_lut, @@ -28,10 +27,7 @@ def test_interior_initialization(self): def test_glass_initialization(self): """Test glass parameters initialization.""" - glass = self.cal.glass_par - assert glass.vec_x == 0.0 - assert glass.vec_y == 0.0 - assert glass.vec_z == 1.0 + assert (np.all(self.cal.glass_par == np.array([0.0, 0.0, 1.0]))) def test_ap_52_initialization(self): """Test ap_52 parameters initialization.""" @@ -59,7 +55,7 @@ def test_calibration_initialization(self): """Test calibration parameters initialization.""" assert isinstance(self.cal.ext_par, Exterior) assert isinstance(self.cal.int_par, Interior) - assert isinstance(self.cal.glass_par, Glass) + assert isinstance(self.cal.glass_par, np.ndarray) assert isinstance(self.cal.added_par, ap_52) assert isinstance(self.cal.mmlut, mm_lut) diff --git a/tests/test_epipolar.py b/tests/test_epipolar.py index a465a68..58d0a1c 100644 --- a/tests/test_epipolar.py +++ b/tests/test_epipolar.py @@ -13,7 +13,12 @@ import numpy as np -from openptv_python.calibration import Calibration, Exterior, Glass, Interior, ap_52 +from openptv_python.calibration import ( + Calibration, + Exterior, + Interior, + ap_52, +) from openptv_python.epi import ( Candidate_dtype, Coord2d_dtype, @@ -43,7 +48,6 @@ def test_two_cameras(self): orig_cal = Calibration().from_file(ori_tmpl, add_file) - cam_num = 3 ori_tmpl = f"tests/testing_folder/calibration/sym_cam{cam_num}.tif.ori" proj_cal = Calibration().from_file(ori_tmpl, add_file) @@ -76,18 +80,20 @@ def test_two_cameras(self): line = epipolar_curve( mid - np.r_[100.0, 0.0], orig_cal, proj_cal, 5, cpar, vpar ) - np.testing.assert_array_equal(np.argsort(line[:, 0]), np.arange(5)[::-1]) + np.testing.assert_array_equal( + np.argsort(line[:, 0]), np.arange(5)[::-1]) self.assertTrue(np.all(abs(line[:, 1] - mid[1]) < 1e-6)) def test_epi_mm_2D(self): """Test the epi_mm_2D function.""" test_Ex = Exterior(0.0, 0.0, 100.0, 0.0, 0.0, 0.0) test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0, 0.0, 50.0) + test_G = np.array((0.0, 0.0, 50.0)) test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) test_cal = Calibration(test_Ex, test_I, test_G, test_addp) - test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) + test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [ + 5.0, 0.0, 0.0], 1.33) test_vpar = VolumePar( [-250.0, 250.0], @@ -120,18 +126,19 @@ def test_epi_mm(self): """Test the epi_mm function.""" test_Ex = Exterior(10.0, 0.0, 100.0, 0.0, -0.01, 0.0) test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0, 0.0, 50.0) + test_G = np.array((0.0, 0.0, 50.0)) test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) test_cal_1 = Calibration(test_Ex, test_I, test_G, test_addp) test_Ex_2 = Exterior(-10.0, 0.0, 100.0, 0.0, 0.01, 0.0) test_cal_2 = Calibration(test_Ex_2, test_I, test_G, test_addp) - test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) + test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [ + 5.0, 0.0, 0.0], 1.33) test_vpar = VolumePar( - [-250.0, 250.0], [-50.0, -50.0], [50.0, 50.0], \ - 0.01, 0.3, 0.3, 0.01, 1.0, 33 + [-250.0, 250.0], [-50.0, -50.0], [50.0, 50.0], + 0.01, 0.3, 0.3, 0.01, 1.0, 33 ) x = 10.0 @@ -150,7 +157,7 @@ def test_epi_mm_perpendicular(self): """Test the epi_mm function.""" test_Ex = Exterior(0.0, 0.0, 100.0, 0.0, 0.0, 0.0) test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0, 0.0, 50.0) + test_G = np.array((0.0, 0.0, 50.0)) test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) test_cal_1 = Calibration(test_Ex, test_I, test_G, test_addp) @@ -219,8 +226,6 @@ def test_find_candidate(self): test_crd.x = np.array([0.1, 0.2, 0.4, 0.7, 1.2, 0.0, 10.4]) test_crd.y = np.array([0.1, 0.8, -1.1, -0.1, 0.3, 0.0, 0.1]) - - # parameters of the particle for which we look for the candidates n = 10 nx = 3 @@ -229,10 +234,11 @@ def test_find_candidate(self): test_Ex = Exterior(0.0, 0.0, 100.0, 0.0, 0.0, 0.0) test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0, 0.0, 50.0) + test_G = np.array((0.0, 0.0, 50.0)) test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) test_cal = Calibration(test_Ex, test_I, test_G, test_addp) - test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) + test_mm = MultimediaPar(1, 1.0, [1.49, 0.0, 0.0], [ + 5.0, 0.0, 0.0], 1.33) test_vpar = VolumePar( [-250.0, 250.0], [-100.0, -100.0], @@ -289,7 +295,6 @@ def test_find_candidate(self): expected.corr = np.array([1156.0, 784.0, 421.0, 676.0, 264.0]) expected.tol = np.array([0.0, 0.424264, 0.565685, 0.636396, 0.0]) - self.assertTrue(len(test_cand) == len(expected)) for t, e in zip(test_cand, expected): self.assertTrue(t.pnr == e.pnr) diff --git a/tests/test_img_coord.py b/tests/test_img_coord.py index d498074..e8af025 100644 --- a/tests/test_img_coord.py +++ b/tests/test_img_coord.py @@ -44,8 +44,9 @@ def test_img_coord_typecheck(self): ) def test_image_coord_regress(self): + """Test image coordinates for a simple case.""" self.calibration.set_pos(np.array([0, 0, 40])) - self.calibration.set_angles(np.array([0, 0, 0])) + self.calibration.set_angles([0, 0, 0]) self.calibration.set_primary_point(np.array([0, 0, 10])) self.calibration.set_glass_vec(np.array([0, 0, 20])) self.calibration.set_radial_distortion(np.array([0, 0, 0])) diff --git a/tests/test_imgcoord.py b/tests/test_imgcoord.py index 62771ff..0eb65c5 100644 --- a/tests/test_imgcoord.py +++ b/tests/test_imgcoord.py @@ -1,6 +1,13 @@ import unittest -from openptv_python.calibration import Calibration, Exterior, Glass, Interior, ap_52 +import numpy as np + +from openptv_python.calibration import ( + Calibration, + Exterior, + Interior, + ap_52, +) from openptv_python.imgcoord import flat_image_coord from openptv_python.parameters import MultimediaPar from openptv_python.vec_utils import vec_set @@ -15,7 +22,7 @@ def test_flat_centered_cam(self): cal = Calibration( ext_par=Exterior(0, 0, 40, 0, 0, 0, [[1, 0, 0], [0, 1, 0], [0, 0, 1]]), int_par=Interior(0, 0, 10), - glass_par=Glass(0, 0, 20), + glass_par=np.array((0., 0., 20.)), added_par=ap_52(0, 0, 0, 0, 0, 1, 0), ) mm = MultimediaPar( # All in air, simplest case. diff --git a/tests/test_multimedia_r_nlay.py b/tests/test_multimedia_r_nlay.py index 7197f57..710fb01 100644 --- a/tests/test_multimedia_r_nlay.py +++ b/tests/test_multimedia_r_nlay.py @@ -2,7 +2,13 @@ import numpy as np -from openptv_python.calibration import Calibration, Exterior, Glass, Interior, ap_52 +from openptv_python.calibration import ( + Calibration, + Exterior, + Glass_dtype, + Interior, + ap_52, +) from openptv_python.multimed import multimed_r_nlay from openptv_python.parameters import MultimediaPar @@ -23,7 +29,7 @@ def test_multimedia_r_nlay(self): ) test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0001, 0.00001, 1.0) + test_G = np.array([(0.0001, 0.00001, 1.0)], dtype=Glass_dtype).view(np.recarray) test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) test_cal = Calibration( test_Ex, test_I, test_G, test_addp diff --git a/tests/test_ray_tracing.py b/tests/test_ray_tracing.py index b5360e2..c6e2f2d 100644 --- a/tests/test_ray_tracing.py +++ b/tests/test_ray_tracing.py @@ -3,7 +3,12 @@ import numpy as np -from openptv_python.calibration import Calibration, Exterior, Glass, Interior, ap_52 +from openptv_python.calibration import ( + Calibration, + Exterior, + Interior, + ap_52, +) from openptv_python.parameters import ControlPar from openptv_python.parameters import MultimediaPar as mm_np from openptv_python.ray_tracing import ray_tracing @@ -43,7 +48,7 @@ def test_ray_tracing_bard(self): test_I = Interior(0.0, 0.0, 100.0) # The glass parameters - test_G = Glass(0.0001, 0.00001, 1.0) + test_G = np.array((0.0001, 0.00001, 1.0)) # The addp parameters test_addp = ap_52(0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) @@ -55,8 +60,8 @@ def test_ray_tracing_bard(self): test_mm = mm_np(3, 1.0, [1.49, 0.0, 0.0], [5.0, 0.0, 0.0], 1.33) # Expected output values - expected_X = [110.406944, 88.325788, 0.988076] - expected_a = [0.387960, 0.310405, -0.867834] + expected_X = np.array([110.406944, 88.325788, 0.988076]) + expected_a = np.array([0.387960, 0.310405, -0.867834]) # Call the ray_tracing() function actual_X, actual_a = ray_tracing(x, y, test_cal, test_mm) diff --git a/tests/test_trans_cam_point.py b/tests/test_trans_cam_point.py index e9d43cb..8a3e517 100644 --- a/tests/test_trans_cam_point.py +++ b/tests/test_trans_cam_point.py @@ -2,7 +2,7 @@ import numpy as np -from openptv_python.calibration import Exterior, Glass +from openptv_python.calibration import Exterior from openptv_python.multimed import back_trans_point, trans_cam_point from openptv_python.parameters import MultimediaPar from openptv_python.vec_utils import vec_norm, vec_set @@ -34,7 +34,7 @@ def test_back_trans_point(self): ) # test_I = Interior(0.0, 0.0, 100.0) - test_G = Glass(0.0001, 0.00001, 1.0) + test_G = np.array((0.0001, 0.00001, 1.0)) # test_addp = ap_52(0., 0., 0., 0., 0., 1., 0.) # test_cal = Calibration(test_Ex, test_I, test_G, test_addp) @@ -77,7 +77,7 @@ def test_trans_cam_point(self): dm=np.array([[1.0, 0.2, -0.3], [0.2, 1.0, 0.0], [-0.3, 0.0, 1.0]]), ) - test_G = Glass(0.0, 0.0, 50.0) + test_G = np.array((0.0, 0.0, 50.0)) # ap_52 test_addp = {0., 0., 0., 0., 0., 1., 0.}; # Calibration test_cal = {test_Ex, test_I, test_G, test_addp}; @@ -86,11 +86,11 @@ def test_trans_cam_point(self): Ex_t = Exterior() pos_t, cross_p, cross_c, Ex_t.z0 = trans_cam_point(test_Ex, test_mm, test_G, pos) - self.assertTrue(np.allclose(pos_t, np.r_[sep_norm, 0.0, -test_G.vec_z])) - self.assertTrue(np.allclose(cross_p, np.r_[pos[0], pos[1], test_G.vec_z])) + self.assertTrue(np.allclose(pos_t, np.r_[sep_norm, 0.0, -test_G[2]])) + self.assertTrue(np.allclose(cross_p, np.r_[pos[0], pos[1], test_G[2]])) self.assertTrue( np.allclose( - cross_c, np.r_[test_Ex.x0, test_Ex.y0, test_G.vec_z + test_mm.d[0]] + cross_c, np.r_[test_Ex.x0, test_Ex.y0, test_G[2] + test_mm.d[0]] ) )