Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C++ Custom Component example #4309

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 23 additions & 50 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,15 @@ fn fill_arrow_array_builder_method(
}
}

fn to_data_cell_method(obj: &Object, objects: &Objects, hpp_includes: &mut Includes) -> Method {
fn to_arrow_method(
obj: &Object,
objects: &Objects,
hpp_includes: &mut Includes,
declarations: &mut ForwardDecls,
) -> Method {
hpp_includes.insert_system("memory"); // std::shared_ptr
hpp_includes.insert_rerun("data_cell.hpp");
hpp_includes.insert_rerun("result.hpp");
declarations.insert("arrow", ForwardDecl::Class(format_ident!("Array")));

let todo_pool = quote_comment("TODO(andreas): Allow configuring the memory pool.");

Expand All @@ -1227,12 +1232,15 @@ fn to_data_cell_method(obj: &Object, objects: &Objects, hpp_includes: &mut Inclu
let namespace_ident = obj.namespace_ident();

Method {
docs: format!("Creates a Rerun DataCell from an array of `rerun::{namespace_ident}::{type_ident}` components.").into(),
docs: format!(
"Serializes an array of `rerun::{namespace_ident}::{type_ident}` into an arrow array."
)
.into(),
declaration: MethodDeclaration {
is_static: true,
return_type: quote! { Result<rerun::DataCell> },
return_type: quote! { Result<std::shared_ptr<arrow::Array>> },
name_and_parameters: quote! {
to_data_cell(const #namespace_ident::#type_ident* instances, size_t num_instances)
to_arrow(const #namespace_ident::#type_ident* instances, size_t num_instances)
},
},
definition_body: quote! {
Expand All @@ -1252,18 +1260,7 @@ fn to_data_cell_method(obj: &Object, objects: &Objects, hpp_includes: &mut Inclu
}
std::shared_ptr<arrow::Array> array;
ARROW_RETURN_NOT_OK(builder->Finish(&array));
#NEWLINE_TOKEN
#NEWLINE_TOKEN
// Lazily register component type.
static const Result<ComponentTypeHandle> component_type = ComponentType(Name, datatype).register_component();
RR_RETURN_NOT_OK(component_type.error);
#NEWLINE_TOKEN
#NEWLINE_TOKEN
DataCell cell;
cell.num_instances = num_instances;
cell.array = std::move(array);
cell.component_type = component_type.value;
return cell;
return array;
},
inline: false,
}
Expand All @@ -1279,49 +1276,25 @@ fn archetype_serialize(type_ident: &Ident, obj: &Object, hpp_includes: &mut Incl
let push_batches = obj.fields.iter().map(|field| {
let field_name = format_ident!("{}", field.name);
let field_accessor = quote!(archetype.#field_name);
let field_type = quote_fqname_as_type_path(
hpp_includes,
field
.typ
.fqname()
.expect("Archetypes only have components and vectors of components."),
);

let emplace_back = quote! {
let push_back = quote! {
RR_RETURN_NOT_OK(result.error);
cells.emplace_back(std::move(result.value));
cells.push_back(std::move(result.value));
};


// TODO(andreas): Introducing MonoCollection will remove the need for distinguishing these two cases.
if field.typ.is_plural() {
if field.is_nullable {
quote! {
if (#field_accessor.has_value()) {
auto result = Loggable<#field_type>::to_data_cell(#field_accessor.value().data(), #field_accessor.value().size());
#emplace_back
}
}
} else {
quote! {
{
auto result = Loggable<#field_type>::to_data_cell(#field_accessor.data(), #field_accessor.size());
#emplace_back
}
}
}
} else if field.is_nullable {
if field.is_nullable {
quote! {
if (#field_accessor.has_value()) {
auto result = Loggable<#field_type>::to_data_cell(&#field_accessor.value(), 1);
#emplace_back
auto result = DataCell::from_loggable(#field_accessor.value());
#push_back
}
}
} else {
quote! {
{
auto result = Loggable<#field_type>::to_data_cell(&#field_accessor, 1);
#emplace_back
auto result = DataCell::from_loggable(#field_accessor);
#push_back
}
}
}
Expand All @@ -1345,7 +1318,7 @@ fn archetype_serialize(type_ident: &Ident, obj: &Object, hpp_includes: &mut Incl
#(#push_batches)*
{
auto indicator = #type_ident::IndicatorComponent();
auto result = Loggable<#type_ident::IndicatorComponent>::to_data_cell(&indicator, 1);
auto result = DataCell::from_loggable(indicator);
RR_RETURN_NOT_OK(result.error);
cells.emplace_back(std::move(result.value));
}
Expand Down Expand Up @@ -2170,7 +2143,7 @@ fn quote_loggable_hpp_and_cpp(
let methods = vec![
arrow_data_type_method(obj, objects, hpp_includes, cpp_includes, hpp_declarations),
fill_arrow_array_builder_method(obj, cpp_includes, hpp_declarations, objects),
to_data_cell_method(obj, objects, hpp_includes),
to_arrow_method(obj, objects, hpp_includes, hpp_declarations),
];

let loggable_type_name = quote! { Loggable<#namespace_ident::#type_ident> };
Expand Down
92 changes: 92 additions & 0 deletions docs/code-examples/custom_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/// Demonstrates how to implement custom archetypes and components, and extend existing ones.

#include <rerun.hpp>
#include <rerun/demo_utils.hpp>

/// A custom component that is backed by a builtin `rerun::Float32` scalar `rerun::Datatype`.
/// Using a rerun type allows us to re-use the existing arrow serialization code.
struct Confidence {
rerun::Float32 value;
};

/// A custom component bundle that extends Rerun's builtin `rerun::Points3D` archetype with extra
/// `rerun::Component`s.
struct CustomPoints3D {
static constexpr const char IndicatorName[] = "user.CustomPoints3DIndicator";
using IndicatorComponent = rerun::components::IndicatorComponent<IndicatorName>;

rerun::Points3D points;
// Using a rerun::Collection is not strictly necessary, you could also use an std::vector for example,
// but useful for avoiding allocations since `rerun::Collection` can borrow data from other containers.
std::optional<rerun::Collection<Confidence>> confidences;
};

template <>
struct rerun::AsComponents<CustomPoints3D> {
static Result<std::vector<DataCell>> serialize(const CustomPoints3D& archetype) {
auto cells = AsComponents<rerun::Points3D>::serialize(archetype.points).value_or_throw();

// Add a custom indicator component.
CustomPoints3D::IndicatorComponent indicator;
cells.push_back(DataCell::from_loggable(indicator).value_or_throw());

// Add custom confidence components if present.
if (archetype.confidences) {
cells.push_back(DataCell::from_loggable(*archetype.confidences).value_or_throw());
}

return cells;
}
};

// ---

template <>
struct rerun::Loggable<Confidence> {
static constexpr const char Name[] = "user.Confidence";

static const std::shared_ptr<arrow::DataType>& arrow_datatype() {
return rerun::Loggable<rerun::Float32>::arrow_datatype();
}

// TODO(#4257) should take a rerun::Collection instead of pointer and size.
static rerun::Result<std::shared_ptr<arrow::Array>> to_arrow(
const Confidence* instances, size_t num_instances
) {
return rerun::Loggable<rerun::Float32>::to_arrow(
reinterpret_cast<const rerun::Float32*>(instances),
num_instances
);
}
};

// ---

int main() {
const auto rec = rerun::RecordingStream("rerun_example_custom_data");
rec.spawn().exit_on_failure();

auto grid =
rerun::demo::grid<rerun::Position3D, float>({-5.0f, -5.0f, -5.0f}, {5.0f, 5.0f, 5.0f}, 3);

rec.log(
"left/my_confident_point_cloud",
CustomPoints3D{
rerun::Points3D(grid),
Confidence{42.0f},
}
);

std::vector<Confidence> confidences;
for (auto i = 0; i < 27; ++i) {
confidences.emplace_back(Confidence{static_cast<float>(i)});
}

rec.log(
"right/my_polarized_point_cloud",
CustomPoints3D{
rerun::Points3D(grid),
confidences,
}
);
}
8 changes: 3 additions & 5 deletions rerun_cpp/src/rerun/archetypes/annotation_context.cpp

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

51 changes: 15 additions & 36 deletions rerun_cpp/src/rerun/archetypes/arrows3d.cpp

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

20 changes: 7 additions & 13 deletions rerun_cpp/src/rerun/archetypes/asset3d.cpp

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

7 changes: 3 additions & 4 deletions rerun_cpp/src/rerun/archetypes/bar_chart.cpp

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

Loading
Loading