11"""Calibration data structures and functions."""
22
33import pathlib
4- from typing import List , Optional
4+ from typing import Optional
55
66import numpy as np
77from numba import njit
88
9+ from openptv_python .vec_utils import vec_set
10+
911
1012@njit
1113def 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
107128class 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):
423441def 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
513531def compare_calibration (c1 : Calibration , c2 : Calibration ) -> bool :
0 commit comments