forked from cvg/Hierarchical-Localization
-
Notifications
You must be signed in to change notification settings - Fork 1
/
result_vl_to_ov.py
150 lines (124 loc) · 5.43 KB
/
result_vl_to_ov.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import numpy as np
from scipy.spatial.transform import Rotation as R
import argparse
from pathlib import Path
def parse_line(line):
# add timeoffset in here!
# (this does not make a big difference, but we want to do it more properly)
timeoffset = 0.002524377913673846
parts = line.split()
timestamp_str = parts[0].split(".")[0] # Extract the timestamp part before '.png'
timestamp = float(timestamp_str[:10] + "." + timestamp_str[10:]) + timeoffset
# Format the timestamp
rotation = np.array([float(parts[i]) for i in range(1, 5)])
translation = np.array([float(parts[i]) for i in range(5, 8)]).reshape([3, 1])
return {"timestamp": timestamp, "q_NtoC": rotation, "p_NinC": translation}
# hamiltonian quaternion convention
def qvec2rotmat(qvec): # TODO: double check this
return np.array(
[
[
1 - 2 * qvec[2] ** 2 - 2 * qvec[3] ** 2,
2 * qvec[1] * qvec[2] - 2 * qvec[0] * qvec[3],
2 * qvec[3] * qvec[1] + 2 * qvec[0] * qvec[2],
],
[
2 * qvec[1] * qvec[2] + 2 * qvec[0] * qvec[3],
1 - 2 * qvec[1] ** 2 - 2 * qvec[3] ** 2,
2 * qvec[2] * qvec[3] - 2 * qvec[0] * qvec[1],
],
[
2 * qvec[3] * qvec[1] - 2 * qvec[0] * qvec[2],
2 * qvec[2] * qvec[3] + 2 * qvec[0] * qvec[1],
1 - 2 * qvec[1] ** 2 - 2 * qvec[2] ** 2,
],
]
)
def rotmat2qvec(rotm): # TODO: double check this
return R.from_matrix(rotm).as_quat()
def q2R(quat):
rotation = R.from_quat([quat[1], quat[2], quat[3], quat[0]])
return rotation.as_matrix()
def read_and_process(input_file):
data = []
bottom = np.array([0.0, 0.0, 0.0, 1.0]).reshape([1, 4])
with open(input_file, "r") as f:
for line in f:
if not line.startswith("#"):
entry = parse_line(line)
quat = entry["q_NtoC"]
R = qvec2rotmat(quat)
t = entry["p_NinC"]
entry["R_NtoC"] = R
m = np.concatenate([np.concatenate([R, t], 1), bottom], 0) # R_WtoC
T_CtoN = np.linalg.inv(m)
entry["T_CtoN"] = T_CtoN
entry["q_CtoN"] = rotmat2qvec(entry["T_CtoN"][0:3, 0:3])
entry["p_CinN"] = entry["T_CtoN"][0:3, 3]
data.append(entry)
data.sort(key=lambda x: x["timestamp"]) # Sort by timestamp
return data
def transform_data(data, q_NtoG, p_NinG, s_NtoG):
for d in data:
d["q_CtoG"] = R.from_matrix(
np.matrix(R.from_quat(q_NtoG).as_matrix())
* np.matrix(R.from_quat(d["q_CtoN"]).as_matrix())
).as_quat()
d["p_CinG"] = np.array(
s_NtoG
* R.from_quat(q_NtoG).as_matrix()
* np.matrix(d["p_CinN"]).reshape([3, 1])
+ p_NinG.reshape([3, 1])
).flatten()
def write_processed_data_inN(output_file, data):
with open(output_file, "w") as f:
f.write("# q_CtoN(Hamilton)\n")
f.write("# p_CinN\n")
f.write("# timestamp tx ty tz qx qy qz qw\n")
for entry in data:
timestamp_str = "{:.9f}".format(entry["timestamp"])
p_CinN_str = " ".join("{:.6f}".format(x) for x in entry["p_CinN"])
q_CtoN_str = " ".join("{:.6f}".format(x) for x in entry["q_CtoN"])
f.write(f"{timestamp_str} {p_CinN_str} {q_CtoN_str}\n")
def write_processed_data_inG(output_file, data):
with open(output_file, "w") as f:
f.write("# q_CtoG(Hamilton)\n")
f.write("# p_CinG\n")
f.write("# timestamp tx ty tz qx qy qz qw\n")
for entry in data:
timestamp_str = "{:.9f}".format(entry["timestamp"])
p_CinG_str = " ".join("{:.6f}".format(x) for x in entry["p_CinG"])
q_CtoG_str = " ".join("{:.6f}".format(x) for x in entry["q_CtoG"])
f.write(f"{timestamp_str} {p_CinG_str} {q_CtoG_str}\n")
def read_transform_data(filename):
with open(filename, "r") as file:
for line in file:
if not line.startswith("#"):
parts = line.split()
# Hamilton
q_NtoG = np.array([float(parts[i]) for i in range(0, 4)])
p_NinG = np.array([float(parts[i]) for i in range(4, 7)])
s_NtoG = float(parts[7])
break
print("q_NtoG:", q_NtoG)
print("p_NinG:", p_NinG)
print("s_NtoG:", s_NtoG)
print()
return np.array(q_NtoG), np.array(p_NinG), np.array(s_NtoG)
if __name__ == "__main__":
# parser
parser = argparse.ArgumentParser()
parser.add_argument("--transform_path", type=Path, required=True)
parser.add_argument("--orig_result_path", type=Path, required=True)
parser.add_argument("--vlresult_path", type=Path, required=True)
parser.add_argument("--ovresult_path", type=Path, required=True)
args = parser.parse_args().__dict__
# read in transform
q_NtoG, p_NinG, s_NtoG = read_transform_data(args["transform_path"])
# read in visual localization result
processed_data = read_and_process(args["orig_result_path"])
transform_data(processed_data, q_NtoG, p_NinG, s_NtoG)
# write visual localization result both in N frame (map frame) and in G frame (vicon frame)
write_processed_data_inN(args["vlresult_path"], processed_data)
write_processed_data_inG(args["ovresult_path"], processed_data)
print("Processed data written done")