summaryrefslogtreecommitdiff
path: root/examples/pybullet/gym/pybullet_envs/deep_mimic/mocap/camera.py
blob: d98993b61f00a8e2239b0617938fcf8d3ffe2f14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import numpy as np

from quaternion import qrot, qinverse


def normalize_screen_coordinates(X, w, h):
  assert X.shape[-1] == 2
  # Normalize so that [0, w] is mapped to [-1, 1], while preserving the aspect ratio
  return X / w * 2 - [1, h / w]


def image_coordinates(X, w, h):
  assert X.shape[-1] == 2
  # Reverse camera frame normalization
  return (X + [1, h / w]) * w / 2


def world_to_camera(X, R, t):
  Rt = qinverse(R)
  Q = np.tile(Rt, (*X.shape[:-1], 1))
  V = X - t
  return qrot(Q, V)


def camera_to_world(X, R, t):
  Q = np.tile(R, (*X.shape[:-1], 1))
  V = X
  return qrot(Q, V) + t