Skip to content

Commit 2509e29

Browse files
[SymForce] Add all the camera cals to types_t and define TypeEnum
In order to support adding camera cals to `Values`, we need to add corresponding enums to `type_t`. I didn't want to have a situation where we add new camera cals and have to remember to manually add them to `type_t`, so I set it to add all subclasses of `CameraCal` (note, only immediate children; subclasses of subclasses won't be added because there are no examples currently and I didn't want to go overkill). The logic for coming up with the camera cal enum names was too complicated to want to keep in the jinja file I thought, so I pass it in from the outside. Since we generate the lcm types in both `test/symforce_lcm_codegen_test.py` and `codegen/geo_package_codegen.py`, I put the logic in a new file, `codegen/lcm_types_codegen.py`, which can then be imported into both. ALSO For the same purpose, we also need `StorageOps<T>::EnumType` to be defined for `T` a camera cal type, so I also do that in this PR. GitOrigin-RevId: 25f5827f00b3cd7546e382405e62d0e820551d67
1 parent a093c00 commit 2509e29

File tree

13 files changed

+74
-8
lines changed

13 files changed

+74
-8
lines changed

gen/cpp/sym/ops/atan_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cpp/sym/ops/double_sphere_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cpp/sym/ops/equidistant_epipolar_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cpp/sym/ops/linear_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cpp/sym/ops/polynomial_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/cpp/sym/ops/spherical_camera_cal/storage_ops.h

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lcmtypes/symforce_types.lcm

+6-1
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@ enum type_t : int32_t {
108108
MATRIX99 = 91,
109109

110110
// Camera calibrations
111-
LINEAR_CAMERA_CAL = 92,
111+
ATAN_CAMERA_CAL = 92,
112+
DOUBLE_SPHERE_CAMERA_CAL = 93,
113+
EQUIDISTANT_EPIPOLAR_CAMERA_CAL = 94,
114+
LINEAR_CAMERA_CAL = 95,
115+
POLYNOMIAL_CAMERA_CAL = 96,
116+
SPHERICAL_CAMERA_CAL = 97,
112117
};

symforce/codegen/cpp_templates/ops/CLASS/storage_ops.h.jinja

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#pragma once
22

33
#include <sym/{{ camelcase_to_snakecase(cls.__name__) }}.h>
4-
{% if "geo" in cls.__module__ %}
54
#include {{ lcm_type_t_include_dir }}
6-
{% endif %}
75

86
namespace sym {
97

@@ -22,11 +20,11 @@ struct StorageOps<{{ cls.__name__ }}<ScalarType>> {
2220
static void ToStorage(const T& a, ScalarType* out);
2321
static T FromStorage(const ScalarType* data);
2422

25-
{% if "geo" in cls.__module__ %}
2623
static constexpr type_t TypeEnum() {
27-
return type_t::{{ cls.__name__.upper() }};
24+
return type_t::{{ python_util.camelcase_to_screaming_snakecase(cls.__name__) }};
2825
}
2926

27+
{% if "geo" in cls.__module__ %}
3028
template <typename Generator>
3129
static T Random(Generator& gen) {
3230
return T::Random(gen);

symforce/codegen/geo_package_codegen.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from symforce.codegen import CppConfig
1616
from symforce.codegen import PythonConfig
1717
from symforce.codegen import codegen_util
18+
from symforce.codegen import lcm_types_codegen
1819
from symforce.codegen import template_util
1920

2021
# Default geo types to generate
@@ -316,7 +317,7 @@ def generate(config: CodegenConfig, output_dir: str = None) -> str:
316317
templates.add(
317318
str(pathlib.Path(template_util.LCM_TEMPLATE_DIR, "symforce_types.lcm.jinja")),
318319
str(package_dir / ".." / "lcmtypes" / "lcmtypes" / "symforce_types.lcm"),
319-
{},
320+
lcm_types_codegen.lcm_symforce_types_data(),
320321
)
321322

322323
templates.render()

symforce/codegen/lcm_templates/symforce_types.lcm.jinja

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ enum type_t : int32_t {
3232
{% endfor %}
3333

3434
// Camera calibrations
35-
LINEAR_CAMERA_CAL = {{ k.value }},
35+
{% for name in camera_cal_enum_names %}
36+
{{ name }} = {{ k.value }},
37+
{% set k.value = k.value + 1 %}
38+
{% endfor %}
3639
};

symforce/codegen/lcm_types_codegen.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from symforce import cam
2+
from symforce import python_util
3+
from symforce import typing as T
4+
5+
6+
def lcm_symforce_types_data() -> T.Dict[str, T.Any]:
7+
"""
8+
Returns data for template generation with lcm_templates/symforce_types.lcm.jinja.
9+
"""
10+
enums = [
11+
python_util.camelcase_to_screaming_snakecase(cal_cls.__name__)
12+
for cal_cls in cam.CameraCal.__subclasses__()
13+
]
14+
enums.sort()
15+
return dict(camera_cal_enum_names=enums)

symforce/python_util.py

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ def snakecase_to_camelcase(s: str) -> str:
100100
)
101101

102102

103+
def camelcase_to_screaming_snakecase(s: str) -> str:
104+
"""
105+
Convert CamelCase -> SCREAMING_SNAKE_CASE
106+
"""
107+
return camelcase_to_snakecase(s).upper()
108+
109+
103110
def str_replace_all(s: str, replacements: T.Dict[str, str]) -> str:
104111
"""
105112
Call str.replace(old, new) for every pair (old, new) in replacements

test/symforce_lcm_codegen_test.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22

3+
from symforce.codegen.lcm_types_codegen import lcm_symforce_types_data
34
from symforce.codegen import template_util
45
from symforce.test_util import TestCase
56

@@ -12,7 +13,7 @@ def test_generate_lcm(self) -> None:
1213

1314
template_util.render_template(
1415
os.path.join(template_util.LCM_TEMPLATE_DIR, "symforce_types.lcm.jinja"),
15-
data={},
16+
data=lcm_symforce_types_data(),
1617
output_path=os.path.join(output_dir, "symforce_types.lcm"),
1718
)
1819

0 commit comments

Comments
 (0)