-
Notifications
You must be signed in to change notification settings - Fork 0
/
arena.py
61 lines (46 loc) · 1.5 KB
/
arena.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import os
from os import path
import pickle
from camera import file_to_camera
arena_shape = (99, 70, 40)
def camera_to_world(r, t, point):
point = point - t
m = np.linalg.inv(np.matrix(r))
return m * point
def world_to_camera(r, t, point):
point = point + t
m = np.matrix(r)
return m * point
def load_arena(dir, timestamp):
matrices_files = [file for file in os.listdir(dir) if file.find(timestamp) != -1 and file.find('.data') != -1]
matrices = {}
for file in matrices_files:
print('loading matrix from file:', file)
f = open(path.join(dir, file), 'rb')
m = pickle.load(f)
matrices[file_to_camera(file)] = m
f.close()
return Arena(matrices)
def in_arena(p, noise = 10):
if len(p) == 0:
return False
x, y, z = p[0], p[1], p[2]
x_max, y_max, z_max = arena_shape
if min(x, y, z) < -1 * noise:
return False
if x > x_max + noise or y > y_max + noise or z > z_max + noise:
return False
return True
class Arena:
def __init__(self, cameras_matrices):
self.cameras_matrices = cameras_matrices
def get_camera_matrices(self, camera):
return self.cameras_matrices[camera]
def translate_point_to_world(self, camera, p):
if camera not in self.cameras_matrices:
print('camera is not initialized in arena')
return
r, t = self.cameras_matrices[camera]
m = np.linalg.inv(np.matrix(r))
return m * (p - t)