Skip to content

Commit

Permalink
C++ basic Archetype & Component logging (#2874)
Browse files Browse the repository at this point in the history
* Part of #2647
* Next step after #2820

### What

Introduces APIs for component and archetype logging as well as the
necessary methods in codegen to do so. A very basic example is included
in `main.cpp`.
Largely unrelated to the rest:
* Makes C++ tagged union types copy-able
* `to_arrow_datatype` constructs/allocates the datatype lazily exactly
once (using C++'s crazy local `static` variable guarantees)

Commit by commit review possible.


![image](https://github.com/rerun-io/rerun/assets/1220815/8e7fdf3a-4fe7-45b1-a835-463b13fc34d8)

Next steps:
* Add roundtrip tests
* Add C++ to ci (roundtrip, linting, etc.)
* Add codegen custom code injection
* Improve API usability in varous places, including #2873 
* add other tests
* serialize unions
* serialize datatypes nested in unions, structs and lists
* more testing & roundtripping

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2874) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2874)
- [Docs
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp-codegen%2Farchetype-builder-and-logging/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp-codegen%2Farchetype-builder-and-logging/examples)
  • Loading branch information
Wumpf committed Jul 31, 2023
1 parent 93df421 commit bf27efd
Show file tree
Hide file tree
Showing 120 changed files with 3,359 additions and 398 deletions.
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

459 changes: 396 additions & 63 deletions crates/re_types_builder/src/codegen/cpp/mod.rs

Large diffs are not rendered by default.

86 changes: 34 additions & 52 deletions examples/cpp/minimal/main.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#include <loguru.hpp>
#include <rerun.hpp>

#include <array>
#include <components/point2d.hpp>

arrow::Result<std::shared_ptr<arrow::Table>> points2(size_t num_points, const float* xy) {
arrow::MemoryPool* pool = arrow::default_memory_pool();

ARROW_ASSIGN_OR_RAISE(auto builder, rr::components::Point2D::new_arrow_array_builder(pool));
ARROW_RETURN_NOT_OK(rr::components::Point2D::fill_arrow_array_builder(
builder.get(),
(const rr::components::Point2D*)
xy, // TODO(andreas): Hack to get Points2D C-style array in an easy fashion
num_points
));

std::shared_ptr<arrow::Array> array;
ARROW_RETURN_NOT_OK(builder->Finish(&array));

auto name = "points"; // Unused, but should be the name of the field in the archetype
auto schema =
arrow::schema({arrow::field(name, rr::components::Point2D::to_arrow_datatype(), false)});

return arrow::Table::Make(schema, {array});
}

int main(int argc, char** argv) {
loguru::g_preamble_uptime = false;
loguru::g_preamble_thread = false;
Expand All @@ -33,37 +13,39 @@ int main(int argc, char** argv) {

auto rr_stream = rr::RecordingStream{"c-example-app", "127.0.0.1:9876"};

// Points3D.
{
float xyz[9] = {0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 5.0, 5.0, 5.0};
auto points = rr::points3(3, xyz).ValueOrDie(); // TODO(andreas): phase this out.
auto buffer = rr::ipc_from_table(*points).ValueOrDie();

const rr::DataCell data_cells[1] = {rr::DataCell{
.component_name = "rerun.point3d",
.num_bytes = static_cast<size_t>(buffer->size()),
.bytes = buffer->data(),
}};

uint32_t num_instances = 3;
rr_stream.log_data_row("3d/points", num_instances, 1, data_cells);
}

// Points2D.
{
float xy[6] = {0.0, 0.0, 1.0, 3.0, 5.0, 5.0};
auto points = points2(3, xy).ValueOrDie();
auto buffer = rr::ipc_from_table(*points).ValueOrDie();

const rr::DataCell data_cells[1] = {rr::DataCell{
.component_name = "rerun.point2d",
.num_bytes = static_cast<size_t>(buffer->size()),
.bytes = buffer->data(),
}};

uint32_t num_instances = 3;
rr_stream.log_data_row("2d/points", num_instances, 1, data_cells);
}
rr_stream.log_archetype(
"3d/points",
rr::archetypes::Points3D({
rr::datatypes::Point3D{1.0, 2.0, 3.0},
rr::datatypes::Point3D{4.0, 5.0, 6.0},
})
.with_radii({0.42, 0.43})
.with_colors({0xAA0000CC, 0x00BB00DD})
.with_labels({std::string("hello"), std::string("friend")})
.with_class_ids({126, 127})
.with_keypoint_ids({2, 3})
.with_instance_keys({66, 666})
);

rr::components::Label c_style_array[3] = {
rr::components::Label("hello"),
rr::components::Label("friend"),
rr::components::Label("yo"),
};
rr_stream.log_components(
"2d/points",
std::vector{
rr::components::Point2D(rr::datatypes::Point2D{0.0, 0.0}),
rr::components::Point2D(rr::datatypes::Point2D{1.0, 3.0}),
rr::components::Point2D(rr::datatypes::Point2D{5.0, 5.0}),
},
std::array{
rr::components::Color(0xFF0000FF),
rr::components::Color(0x00FF00FF),
rr::components::Color(0x0000FFFF),
},
c_style_array
);

// Test some type instantiation
auto tls = rr::datatypes::TranslationRotationScale3D{};
Expand Down

0 comments on commit bf27efd

Please sign in to comment.