Skip to content

Commit

Permalink
C++ as_components style & splatting (#3258)
Browse files Browse the repository at this point in the history
### What

* Part of: #2919 
* Fixes #3256
* just like in Rust it's actually `as_component_lists`, we return a
vector of type erased component arrays that know how to serialize
themselves, dubbed `AnonymousComponentList` for now.
* I'm not happy with naming and internals of `AnonymousComponentList`.
This smells very much like we should have actual inheritance somewhere,
but it's a bit tricky since we want to extract pointer out of arbitrary
list types, giving rise to the type aware `ComponentList`. Overall the
approach here tries to mimic Rust but ultimately misses the target,
since, well this is not Rust. To be revisited, maybe it is just a naming
issue!
* Fixes #3255
* indicators are regular components now, their handling in
`as_component_lists` is very slightly dubious since they're not backed
by data but ofc indicator components don't have any data to begin with 🤷
* Fixes #3040
* First step towards #3050


Trigger of this PR was the inability to deal with mono components like
`DrawOrder` -> need splatting -> don't want to implement splatting
separately for components & archetype -> implement as_components-style

Quick throw-away test for splatting radius on the random point demo:
<img width="943" alt="image"
src="https://github.com/rerun-io/rerun/assets/1220815/d256b281-34c7-4d6c-a143-b4d604a13b75">



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

- [PR Build Summary](https://build.rerun.io/pr/3258)
- [Docs
preview](https://rerun.io/preview/f732aad18515476d81d57b89108e08f787729ae6/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/f732aad18515476d81d57b89108e08f787729ae6/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://ref.rerun.io/dev/bench/)
- [Wasm size tracking](https://ref.rerun.io/dev/sizes/)
  • Loading branch information
Wumpf authored Sep 8, 2023
1 parent 146f704 commit 1488bd5
Show file tree
Hide file tree
Showing 114 changed files with 901 additions and 1,426 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.

93 changes: 36 additions & 57 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ impl QuotedObject {
});
}

methods.push(archetype_to_data_cells(
methods.push(archetype_as_component_batches(
&type_ident,
obj,
&mut hpp_includes,
&mut cpp_includes,
Expand Down Expand Up @@ -1221,89 +1222,53 @@ fn component_to_data_cell_method(
}
}

fn archetype_to_data_cells(
fn archetype_as_component_batches(
type_ident: &Ident,
obj: &Object,
hpp_includes: &mut Includes,
cpp_includes: &mut Includes,
) -> Method {
hpp_includes.insert_rerun("data_cell.hpp");
hpp_includes.insert_rerun("result.hpp");
hpp_includes.insert_rerun("arrow.hpp");
hpp_includes.insert_rerun("component_batch.hpp");
cpp_includes.insert_rerun("indicator_component.hpp");
hpp_includes.insert_system("vector"); // std::vector

// TODO(andreas): Splats need to be handled separately.

let num_fields = quote_integer(obj.fields.len());
let push_cells = obj.fields.iter().map(|field| {
let field_type_fqname = match &field.typ {
Type::Vector { elem_type } => elem_type.fqname().unwrap(),
Type::Object(fqname) => fqname,
_ => unreachable!(
"Archetypes are not expected to have any fields other than objects and vectors"
),
};
let field_type = quote_fqname_as_type_path(cpp_includes, field_type_fqname);
let push_batches = obj.fields.iter().map(|field| {
let field_name = format_ident!("{}", field.name);

if field.is_nullable {
let to_data_cell = if field.typ.is_plural() {
quote!(#field_type::to_data_cell(value.data(), value.size()))
} else {
quote!(#field_type::to_data_cell(&value, 1))
};
quote! {
if (#field_name.has_value()) {
const auto& value = #field_name.value();
const auto result = #to_data_cell;
if (result.is_err()) {
return result.error;
}
cells.emplace_back(std::move(result.value));
comp_batches.emplace_back(#field_name.value());
}
}
} else {
let to_data_cell = if field.typ.is_plural() {
quote!(#field_type::to_data_cell(#field_name.data(), #field_name.size()))
} else {
quote!(#field_type::to_data_cell(&#field_name, 1))
};
quote! {
{
const auto result = #to_data_cell;
if (result.is_err()) {
return result.error;
}
cells.emplace_back(std::move(result.value));
}
comp_batches.emplace_back(#field_name);
}
}
});

let indicator_fqname = format!("rerun.components.{}Indicator", obj.name);
Method {
docs: "Creates a list of Rerun DataCell from this archetype.".into(),
docs: "Collections all component lists into a list of component collections. \
*Attention:* The returned vector references this instance and does not take ownership of any data. \
Adding any new components to this archetype will invalidate the returned component lists!".into(),
declaration: MethodDeclaration {
is_static: false,
return_type: quote!(Result<std::vector<rerun::DataCell>>),
name_and_parameters: quote!(to_data_cells() const),
return_type: quote!(std::vector<AnonymousComponentBatch>),
name_and_parameters: quote!(as_component_batches() const),
},
definition_body: quote! {
std::vector<rerun::DataCell> cells;
cells.reserve(#num_fields);
std::vector<AnonymousComponentBatch> comp_batches;
comp_batches.reserve(#num_fields);
#NEWLINE_TOKEN
#NEWLINE_TOKEN
#(#push_cells)*
{
const auto result =
create_indicator_component(#indicator_fqname, num_instances());
if (result.is_err()) {
return result.error;
}
cells.emplace_back(std::move(result.value));
}
#(#push_batches)*
comp_batches.emplace_back(ComponentBatch<components::IndicatorComponent<#type_ident::INDICATOR_COMPONENT_NAME>>(nullptr, num_instances()));
#NEWLINE_TOKEN
#NEWLINE_TOKEN
return cells;
return comp_batches;
},
inline: false,
}
Expand Down Expand Up @@ -1762,11 +1727,25 @@ fn quote_constants_header_and_cpp(
#NEWLINE_TOKEN
#NEWLINE_TOKEN
#comment
static const char* NAME
static const char NAME[]
});
cpp.push(quote!(const char* #obj_type_ident::NAME = #legacy_fqname));
cpp.push(quote!(const char #obj_type_ident::NAME[] = #legacy_fqname));
}
ObjectKind::Archetype => {
let indicator_fqname =
format!("{}Indicator", obj.fqname).replace("archetypes", "components");
let comment = quote_doc_comment("Name of the indicator component, used to identify the archetype when converting to a list of components.");
hpp.push(quote! {
#NEWLINE_TOKEN
#NEWLINE_TOKEN
#comment
static const char INDICATOR_COMPONENT_NAME[]
});
cpp.push(
quote!(const char #obj_type_ident::INDICATOR_COMPONENT_NAME[] = #indicator_fqname),
);
}
ObjectKind::Archetype | ObjectKind::Datatype => {}
ObjectKind::Datatype => {}
}

(hpp, cpp)
Expand Down
3 changes: 2 additions & 1 deletion examples/cpp/minimal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ int main(int argc, char** argv) {
// Log points with the components api - this is the advanced way of logging components in a
// fine-grained matter. It supports passing various types of containers.
rrc::Text c_style_array[3] = {rrc::Text("hello"), rrc::Text("friend"), rrc::Text("yo")};
rr_stream.log_components(
rr_stream.log_component_batches(
"2d/points",
3,
std::vector{rrc::Point2D(0.0f, 0.0f), rrc::Point2D(1.0f, 3.0f), rrc::Point2D(5.0f, 5.0f)},
std::array{rrc::Color(0xFF0000FF), rrc::Color(0x00FF00FF), rrc::Color(0x0000FFFF)},
c_style_array
Expand Down
36 changes: 15 additions & 21 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.

12 changes: 10 additions & 2 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.

89 changes: 19 additions & 70 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.

12 changes: 10 additions & 2 deletions rerun_cpp/src/rerun/archetypes/arrows3d.hpp

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

Loading

0 comments on commit 1488bd5

Please sign in to comment.