forked from rerun-io/rerun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_view_coordinate_defs.py
executable file
·346 lines (260 loc) · 12.8 KB
/
generate_view_coordinate_defs.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
"""
Generates the permutations of view coordinate name definitions.
This will modify the different archetype extensions to include the appropriate constants.
"""
# TODO(#2388): This script can potentially go away or be significantly reduced.
from __future__ import annotations
import argparse
import itertools
import os
from dataclasses import dataclass
from typing import Iterable
BEGIN_MARKER = "<BEGIN_GENERATED:{}>"
END_MARKER = "<END_GENERATED:{}>"
SCRIPT_PATH = os.path.relpath(__file__, os.getcwd()).replace("\\", "/")
################################################################################
@dataclass
class ViewCoordinates:
name: str
x: str
y: str
z: str
def docstring(coords: ViewCoordinates) -> str:
# TODO(emilk): warn about left-handed coordinate systems
return f"X={coords.x}, Y={coords.y}, Z={coords.z}"
def generate_view_permutations() -> Iterable[ViewCoordinates]:
D1 = ["Up", "Down"]
D2 = ["Left", "Right"]
D3 = ["Forward", "Back"]
for i in D1:
for j in D2:
for k in D3:
for x, y, z in itertools.permutations([i, j, k]):
name = f"{x[0]}{y[0]}{z[0]}"
yield ViewCoordinates(name, x, y, z)
def generate_up_handed_permutations() -> Iterable[ViewCoordinates]:
return [
ViewCoordinates(name="RIGHT_HAND_X_UP", x="Up", y="Right", z="Forward"),
ViewCoordinates(name="RIGHT_HAND_X_DOWN", x="Down", y="Right", z="Back"),
ViewCoordinates(name="RIGHT_HAND_Y_UP", x="Right", y="Up", z="Back"),
ViewCoordinates(name="RIGHT_HAND_Y_DOWN", x="Right", y="Down", z="Forward"),
ViewCoordinates(name="RIGHT_HAND_Z_UP", x="Right", y="Forward", z="Up"),
ViewCoordinates(name="RIGHT_HAND_Z_DOWN", x="Right", y="Back", z="Down"),
ViewCoordinates(name="LEFT_HAND_X_UP", x="Up", y="Right", z="Back"),
ViewCoordinates(name="LEFT_HAND_X_DOWN", x="Down", y="Right", z="Forward"),
ViewCoordinates(name="LEFT_HAND_Y_UP", x="Right", y="Up", z="Forward"),
ViewCoordinates(name="LEFT_HAND_Y_DOWN", x="Right", y="Down", z="Back"),
ViewCoordinates(name="LEFT_HAND_Z_UP", x="Right", y="Back", z="Up"),
ViewCoordinates(name="LEFT_HAND_Z_DOWN", x="Right", y="Forward", z="Down"),
]
################################################################################
# Rust Archetype
RUST_ARCHETYPE_EXTENSION_FILE = "crates/re_types/src/archetypes/view_coordinates_ext.rs"
def rust_arch_decl(coords: ViewCoordinates) -> str:
return f'define_coordinates!("{docstring(coords)}", {coords.name} => ({coords.x}, {coords.y}, {coords.z}));\n'
def gen_rust_arch_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --rust`\n")
lines.extend(rust_arch_decl(v) for v in generate_view_permutations())
lines.extend(rust_arch_decl(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
return lines
################################################################################
# Rust Component
RUST_COMPONENT_EXTENSION_FILE = "crates/re_types/src/components/view_coordinates_ext.rs"
def rust_cmp_decl(coords: ViewCoordinates) -> str:
return f'define_coordinates!("{docstring(coords)}", {coords.name} => ({coords.x}, {coords.y}, {coords.z}));\n'
def gen_rust_cmp_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --rust`\n")
lines.extend(rust_cmp_decl(v) for v in generate_view_permutations())
lines.extend(rust_cmp_decl(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
return lines
################################################################################
# Python Archetype
PYTHON_ARCHETYPE_EXTENSION_FILE = "rerun_py/rerun_sdk/rerun/archetypes/view_coordinates_ext.py"
def py_arch_decl(coords: ViewCoordinates) -> str:
return f"{coords.name}: ViewCoordinates = None # type: ignore[assignment]\n"
def gen_py_arch_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"# {BEGIN_MARKER.format(key)}\n")
lines.append(f"# This section is generated by running `{SCRIPT_PATH} --python`\n")
lines.append("# The following declarations are replaced in `deferred_patch_class`.\n")
lines.extend(py_arch_decl(v) for v in generate_view_permutations())
lines.extend(py_arch_decl(v) for v in generate_up_handed_permutations())
lines.append(f"# {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
def py_arch_def(coords: ViewCoordinates) -> str:
return f"cls.{coords.name} = Component.{coords.name}\n"
def gen_py_arch_def() -> list[str]:
key = "definitions"
lines = []
lines.append(f"# {BEGIN_MARKER.format(key)}\n")
lines.append(f"# This section is generated by running `{SCRIPT_PATH} --python`\n")
lines.extend(py_arch_def(v) for v in generate_view_permutations())
lines.extend(py_arch_def(v) for v in generate_up_handed_permutations())
lines.append(f"# {END_MARKER.format(key)}\n")
lines = [" " * 8 + line for line in lines]
return lines
################################################################################
# Python Component
PYTHON_COMPONENT_EXTENSION_FILE = "rerun_py/rerun_sdk/rerun/components/view_coordinates_ext.py"
def py_cmp_decl(coords: ViewCoordinates) -> str:
return f'{coords.name}: ViewCoordinates = None # type: ignore[assignment]\n """{docstring(coords)}"""\n\n'
def gen_py_cmp_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"# {BEGIN_MARKER.format(key)}\n")
lines.append(f"# This section is generated by running `{SCRIPT_PATH} --python`\n")
lines.append("# The following declarations are replaced in `deferred_patch_class`.\n")
lines.extend(py_cmp_decl(v) for v in generate_view_permutations())
lines.extend(py_cmp_decl(v) for v in generate_up_handed_permutations())
lines.append(f"# {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
def py_cmp_def(coords: ViewCoordinates) -> str:
return f"cls.{coords.name} = cls([cls.ViewDir.{coords.x}, cls.ViewDir.{coords.y}, cls.ViewDir.{coords.z}])\n"
def gen_py_cmp_def() -> list[str]:
key = "definitions"
lines = []
lines.append(f"# {BEGIN_MARKER.format(key)}\n")
lines.append(f"# This section is generated by running `{SCRIPT_PATH} --python`\n")
lines.extend(py_cmp_def(v) for v in generate_view_permutations())
lines.extend(py_cmp_def(v) for v in generate_up_handed_permutations())
lines.append(f"# {END_MARKER.format(key)}\n")
lines = [" " * 8 + line for line in lines]
return lines
################################################################################
# CPP Archetype
CPP_ARCHETYPE_EXTENSION_FILE = "rerun_cpp/src/rerun/archetypes/view_coordinates_ext.cpp"
def cpp_arch_decl(coords: ViewCoordinates) -> str:
return (
f"/// {docstring(coords)}\nRERUN_SDK_EXPORT static const rerun::archetypes::ViewCoordinates {coords.name};\n\n"
)
def gen_cpp_arch_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --cpp`\n")
lines.extend(cpp_arch_decl(v) for v in generate_view_permutations())
lines.extend(cpp_arch_decl(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
def cpp_arch_def(coords: ViewCoordinates) -> str:
return (
f"const ViewCoordinates ViewCoordinates::{coords.name} = ViewCoordinates(\n"
+ f"rerun::components::ViewCoordinates::{coords.name}\n"
+ ");\n"
)
def gen_cpp_arch_def() -> list[str]:
key = "definitions"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --cpp`\n")
lines.extend(cpp_arch_def(v) for v in generate_view_permutations())
lines.extend(cpp_arch_def(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
################################################################################
# CPP Component
CPP_COMPONENT_EXTENSION_FILE = "rerun_cpp/src/rerun/components/view_coordinates_ext.cpp"
def cpp_cmp_decl(coords: ViewCoordinates) -> str:
return (
f"/// {docstring(coords)}\nRERUN_SDK_EXPORT static const rerun::components::ViewCoordinates {coords.name};\n\n"
)
def gen_cpp_cmp_decl() -> list[str]:
key = "declarations"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --cpp`\n")
lines.extend(cpp_cmp_decl(v) for v in generate_view_permutations())
lines.extend(cpp_cmp_decl(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
def cpp_cmp_def(coords: ViewCoordinates) -> str:
return (
f"const ViewCoordinates ViewCoordinates::{coords.name} = ViewCoordinates(\n"
+ f"rerun::components::ViewCoordinates::{coords.x}, rerun::components::ViewCoordinates::{coords.y}, rerun::components::ViewCoordinates::{coords.z}\n"
+ ");\n"
)
def gen_cpp_cmp_def() -> list[str]:
key = "definitions"
lines = []
lines.append(f"// {BEGIN_MARKER.format(key)}\n")
lines.append(f"// This section is generated by running `{SCRIPT_PATH} --cpp`\n")
lines.extend(cpp_cmp_def(v) for v in generate_view_permutations())
lines.extend(cpp_cmp_def(v) for v in generate_up_handed_permutations())
lines.append(f"// {END_MARKER.format(key)}\n")
lines = [" " * 4 + line for line in lines]
return lines
################################################################################
def show_preview(lines: list[str]) -> None:
print("".join(lines))
def patch_file(filename: str, lines: list[str], key: str) -> None:
contents = open(filename, encoding="utf8").readlines()
start_line = next((i for i, line in enumerate(contents) if BEGIN_MARKER.format(key) in line), None)
end_line = next((i for i, line in enumerate(contents) if END_MARKER.format(key) in line), None)
if (start_line is None) or (end_line is None):
raise Exception("Could not find the generated section in the file.")
new_contents = contents[:start_line] + lines + contents[end_line + 1 :]
open(filename, "w", encoding="utf8").writelines(new_contents)
################################################################################
def process_file(preview: bool, filename: str, decl_lines: list[str] | None, def_lines: list[str] | None) -> None:
if preview:
if def_lines is not None:
print(f"Preview of {filename}: definitions")
show_preview(def_lines)
if decl_lines is not None:
print(f"Preview of {filename}: declarations")
show_preview(decl_lines)
else:
if def_lines is not None:
patch_file(filename, def_lines, "definitions")
if decl_lines is not None:
patch_file(filename, decl_lines, "declarations")
def main() -> None:
parser = argparse.ArgumentParser(description="Modify the ViewCoordinate archetypes.")
parser.add_argument(
"--rust",
action="store_true",
default=False,
help="Generate the rust code for the view coordinates.",
)
parser.add_argument(
"--python",
action="store_true",
default=False,
help="Generate the python code for the view coordinates.",
)
parser.add_argument(
"--cpp",
action="store_true",
default=False,
help="Generate the cpp code for the view coordinates.",
)
parser.add_argument(
"--preview", action="store_true", default=False, help="Just print the preview of the generated sections"
)
args = parser.parse_args()
if args.rust:
process_file(args.preview, RUST_ARCHETYPE_EXTENSION_FILE, gen_rust_arch_decl(), None)
process_file(args.preview, RUST_COMPONENT_EXTENSION_FILE, gen_rust_cmp_decl(), None)
if args.python:
process_file(args.preview, PYTHON_ARCHETYPE_EXTENSION_FILE, gen_py_arch_decl(), gen_py_arch_def())
process_file(args.preview, PYTHON_COMPONENT_EXTENSION_FILE, gen_py_cmp_decl(), gen_py_cmp_def())
if args.cpp:
process_file(args.preview, CPP_ARCHETYPE_EXTENSION_FILE, gen_cpp_arch_decl(), gen_cpp_arch_def())
process_file(args.preview, CPP_COMPONENT_EXTENSION_FILE, gen_cpp_cmp_decl(), gen_cpp_cmp_def())
if __name__ == "__main__":
main()