Skip to content

Commit

Permalink
C++ annotation context (#2948)
Browse files Browse the repository at this point in the history
### What


* Fixes #2794
* Part of #2919 

Small codegen fix for nested array serialization was required.
Adds example & test & roundtrip test for annotation context to C++

Sneaked in: Fixes a few annoyances with VSCode Cpp/CMake config.

<img width="1016" alt="image"
src="https://github.com/rerun-io/rerun/assets/1220815/31a764bc-cb51-4420-a75f-0da0eb6bfd39">


### 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/2948) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2948)
- [Docs
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fannotation-context/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aandreas%2Fcpp%2Fannotation-context/examples)
  • Loading branch information
Wumpf authored Aug 10, 2023
1 parent 481e852 commit cdca754
Show file tree
Hide file tree
Showing 37 changed files with 478 additions and 65 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@
"--config",
"rerun_py/pyproject.toml"
],
"cmake.buildDirectory": "${workspaceRoot}/build/",
"C_Cpp.autoAddFileAssociations": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ namespace rerun.archetypes;
/// \rs ```ignore
/// \rs \include:../../../../../docs/code-examples/annotation_context_rects.rs
/// \rs ```
///
/// \cpp ## Example
/// \cpp
/// \cpp ```
// \cpp TODO(#2786): implement Rect2D cpp example and link it here instead.
/// \cpp \include:../../../../../docs/code-examples/annotation_context_arrows_v2.cpp
/// \cpp ```
table AnnotationContext (
"attr.rust.derive": "Eq, PartialEq",
order: 100
Expand Down
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.

10 changes: 6 additions & 4 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,9 +1304,9 @@ fn quote_append_field_to_builder(
quote! {
auto #value_builder = static_cast<arrow::#value_builder_type*>(#builder->value_builder());
#NEWLINE_TOKEN #NEWLINE_TOKEN
ARROW_RETURN_NOT_OK(#builder->AppendValues(num_elements));
static_assert(sizeof(elements[0].#field_name) == sizeof(elements[0]));
ARROW_RETURN_NOT_OK(#value_builder->AppendValues(#field_ptr_accessor, num_elements * #num_items_per_value, nullptr));
ARROW_RETURN_NOT_OK(#builder->AppendValues(num_elements));
}
} else {
let value_reserve_factor = match &field.typ {
Expand Down Expand Up @@ -1348,8 +1348,8 @@ fn quote_append_field_to_builder(
for (auto elem_idx = 0; elem_idx < num_elements; elem_idx += 1) {
const auto& element = elements[elem_idx];
if (element.#field_name.has_value()) {
#append_value
ARROW_RETURN_NOT_OK(#builder->Append());
#append_value
} else {
ARROW_RETURN_NOT_OK(#builder->AppendNull());
}
Expand All @@ -1360,8 +1360,8 @@ fn quote_append_field_to_builder(
#setup
for (auto elem_idx = 0; elem_idx < num_elements; elem_idx += 1) {
const auto& element = elements[elem_idx];
#append_value
ARROW_RETURN_NOT_OK(#builder->Append());
#append_value
}
}
}
Expand Down Expand Up @@ -1477,7 +1477,9 @@ fn quote_append_single_value_to_builder(
let fqname = quote_fqname_as_type_path(includes, fqname);
let field_ptr_accessor = quote_field_ptr_access(typ, value_access);
quote! {
ARROW_RETURN_NOT_OK(#fqname::fill_arrow_array_builder(#value_builder, #field_ptr_accessor, #num_items_per_element));
if (#field_ptr_accessor) {
ARROW_RETURN_NOT_OK(#fqname::fill_arrow_array_builder(#value_builder, #field_ptr_accessor, #num_items_per_element));
}
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions docs/code-examples/annotation_context_arrows_v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Log an annotation context to assign a label and color to each class

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rr_stream = rr::RecordingStream("annotation_context_rects");
rr_stream.connect("127.0.0.1:9876");

// Log an annotation context to assign a label and color to each class
rr_stream.log(
"/",
rr::archetypes::AnnotationContext({
rr::datatypes::AnnotationInfo(1, "red", rr::components::Color(255, 0, 0)),
rr::datatypes::AnnotationInfo(2, "green", rr::components::Color(0, 255, 0)),
})
);

// Log a batch of 2 arrows with different `class_ids`
rr_stream.log(
"arrows",
rr::archetypes::Arrows3D({{1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}}).with_class_ids({1, 2})
);
}
31 changes: 31 additions & 0 deletions rerun_cpp/src/rerun/archetypes/annotation_context.hpp

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

2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/components/affix_fuzzer11.cpp

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

2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/components/affix_fuzzer12.cpp

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

2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/components/affix_fuzzer13.cpp

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

12 changes: 7 additions & 5 deletions rerun_cpp/src/rerun/components/affix_fuzzer16.cpp

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

14 changes: 9 additions & 5 deletions rerun_cpp/src/rerun/components/affix_fuzzer17.cpp

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

14 changes: 9 additions & 5 deletions rerun_cpp/src/rerun/components/affix_fuzzer18.cpp

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

14 changes: 9 additions & 5 deletions rerun_cpp/src/rerun/components/affix_fuzzer7.cpp

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

16 changes: 9 additions & 7 deletions rerun_cpp/src/rerun/components/annotation_context.cpp

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

12 changes: 12 additions & 0 deletions rerun_cpp/src/rerun/components/annotation_context.hpp

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

31 changes: 31 additions & 0 deletions rerun_cpp/src/rerun/components/annotation_context_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <utility>
#include "annotation_context.hpp"

// Uncomment for better auto-complete while editing the extension.
// #define EDIT_EXTENSION

namespace rerun {
namespace components {
#ifdef EDIT_EXTENSION
struct AnnotationContextExt {
std::vector<rerun::datatypes::ClassDescriptionMapElem> class_map;

#define AnnotationContext AnnotationContextExt

// [CODEGEN COPY TO HEADER START]

AnnotationContext(
std::initializer_list<rerun::datatypes::ClassDescription> class_descriptions
) {
class_map.reserve(class_descriptions.size());
for (const auto& class_description : class_descriptions) {
class_map.emplace_back(std::move(class_description));
}
}

// [CODEGEN COPY TO HEADER END]
};
#endif

} // namespace components
} // namespace rerun
12 changes: 7 additions & 5 deletions rerun_cpp/src/rerun/components/line_strip2d.cpp

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

12 changes: 7 additions & 5 deletions rerun_cpp/src/rerun/components/line_strip3d.cpp

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

Loading

0 comments on commit cdca754

Please sign in to comment.