Skip to content

Commit

Permalink
wip: update rotate method in Camera
Browse files Browse the repository at this point in the history
  • Loading branch information
hanson-hschang committed Sep 22, 2024
1 parent 99f0d6b commit 1c11e33
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/bsr/_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def location(self, location: np.ndarray) -> None:
sky=self._sky,
)

@property
def orientation(self) -> np.ndarray:
"""
Return the current orientation of the camera.
"""
return np.array(self._camera.matrix_world)[:3, :3]

@property
def look_at(self) -> Optional[np.ndarray]:
"""
Expand Down Expand Up @@ -170,12 +177,40 @@ def compute_matrix_world(

direction = direction / np.linalg.norm(direction)
right = np.cross(direction, sky)
right = right / np.linalg.norm(right)
up = np.cross(right, direction)

return np.array(
[[*right, 0.0], [*up, 0.0], [*(-direction), 0.0], [*location, 1.0]]
)

def rotate(self, angle: float) -> None:
"""
Rotate the camera around the look-at-axis.
Parameters
----------
angle : float
The angle (degree) to rotate the camera.
"""
assert isinstance(angle, (int, float)), "angle must be a number"
angle_rad = np.deg2rad(angle)
rotation_matrix = np.array(
[
[np.cos(angle_rad), -np.sin(angle_rad), 0],
[np.sin(angle_rad), np.cos(angle_rad), 0],
[0, 0, 1],
]
)
orientation = self.orientation @ rotation_matrix
self._camera.matrix_world = np.array(
[
[*orientation[:, 0], 0.0],
[*orientation[:, 1], 0.0],
[*orientation[:, 2], 0.0],
[*self.location, 1.0],
]
)

def set_resolution(self, width: int, height: int) -> None:
"""
Set the resolution of the camera.
Expand Down

0 comments on commit 1c11e33

Please sign in to comment.