1+ from typing import Optional
2+
13import numpy as np
24from numpy .typing import NDArray
35
46
5- def calc_camera_vectors (
7+ def camera_vectors_from_element_placement (
68 elem_placement : NDArray [np .float_ ],
79) -> tuple [NDArray [np .float_ ], NDArray [np .float_ ], NDArray [np .float_ ]]:
810 """
@@ -15,19 +17,37 @@ def calc_camera_vectors(
1517 Camera position, direction and up vectors
1618 """
1719 target_position = elem_placement [:3 , 3 ]
18- camera_position = target_position + np .array ((5 , 5 , 5 ))
19- camera_direction = unit_vector (camera_position - target_position )
20+ return camera_vectors_from_target_position (target_position )
21+
22+
23+ def camera_vectors_from_target_position (
24+ target_position : NDArray [np .float_ ], offset : Optional [NDArray [np .float_ ]] = None
25+ ) -> tuple [NDArray [np .float_ ], NDArray [np .float_ ], NDArray [np .float_ ]]:
26+ """
27+ Calculate the vectors of a camera pointing to a target point.
28+
29+ Args:
30+ target_position: point the camera is pointing to.
31+ camera_offset: offset of the camera from the target point.
32+
33+ Returns:
34+ Camera position, direction and up vectors
35+ """
36+ camera_offset = np .array ((5 , 5 , 5 )) if offset is None else offset
37+ camera_position = target_position + camera_offset
38+ camera_direction = unit_vector (- camera_offset ) # pylint: disable=invalid-unary-operand-type
2039 camera_right = unit_vector (np .cross (np .array ([0.0 , 0.0 , 1.0 ]), camera_direction ))
2140 camera_up = unit_vector (np .cross (camera_direction , camera_right ))
22- rotation_transform = np .eye (4 )
23- rotation_transform [0 , :3 ] = camera_right
24- rotation_transform [1 , :3 ] = camera_up
25- rotation_transform [2 , :3 ] = camera_direction
26- translation_transform = np .eye (4 )
27- translation_transform [:3 , - 1 ] = - camera_position
28- look_at_transform = np .matmul (rotation_transform , translation_transform )
29- mat = np .linalg .inv (look_at_transform )
30- return camera_position , - mat [:3 , 2 ], mat [:3 , 1 ]
41+ return camera_position , camera_direction , camera_up
42+ # rotation_transform = np.eye(4)
43+ # rotation_transform[0, :3] = camera_right
44+ # rotation_transform[1, :3] = camera_up
45+ # rotation_transform[2, :3] = camera_direction
46+ # translation_transform = np.eye(4)
47+ # translation_transform[:3, -1] = -camera_position
48+ # look_at_transform = np.matmul(rotation_transform, translation_transform)
49+ # mat = np.linalg.inv(look_at_transform)
50+ # return camera_position, -mat[:3, 2], mat[:3, 1]
3151
3252
3353def unit_vector (v : NDArray [np .float_ ]) -> NDArray [np .float_ ]:
0 commit comments