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

C++: Allow creating Image/Tensor/DepthImage/SegmentationImage directly from shape & pointer #4345

Merged
merged 5 commits into from
Nov 27, 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
4 changes: 4 additions & 0 deletions crates/re_types/definitions/rerun/archetypes/depth_image.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace rerun.archetypes;
/// The shape of the `TensorData` must be mappable to an `HxW` tensor.
/// Each pixel corresponds to a depth value in units specified by `meter`.
///
/// \cpp Since the underlying `rerun::datatypes::TensorData` uses `rerun::Collection` internally,
/// \cpp data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
/// \cpp If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
///
/// \example depth_image_simple !api title="Simple example" image="https://static.rerun.io/depth_image_simple/9598554977873ace2577bddd79184ac120ceb0b0/1200w.png"
/// \example depth_image_3d title="Depth to 3D example" image="https://static.rerun.io/depth_image_3d/f78674bdae0eb25786c6173307693c5338f38b87/1200w.png"
table DepthImage (
Expand Down
4 changes: 4 additions & 0 deletions crates/re_types/definitions/rerun/archetypes/image.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ namespace rerun.archetypes;
/// Leading and trailing unit-dimensions are ignored, so that
/// `1x640x480x3x1` is treated as a `640x480x3` RGB image.
///
/// \cpp Since the underlying `rerun::datatypes::TensorData` uses `rerun::Collection` internally,
/// \cpp data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
/// \cpp If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
///
/// \example image_simple image="https://static.rerun.io/image_simple/06ba7f8582acc1ffb42a7fd0006fad7816f3e4e4/1200w.png"
table Image (
"attr.rust.derive": "PartialEq",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ namespace rerun.archetypes;
/// \py See also [`AnnotationContext`][rerun.archetypes.AnnotationContext] to associate each class with a color and a label.
/// \rs See also [`AnnotationContext`][crate::archetypes::AnnotationContext] to associate each class with a color and a label.
///
/// \cpp Since the underlying `rerun::datatypes::TensorData` uses `rerun::Collection` internally,
/// \cpp data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
/// \cpp If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
///
/// \example segmentation_image_simple title="Simple segmentation image" image="https://static.rerun.io/segmentation_image_simple/eb49e0b8cb870c75a69e2a47a2d202e5353115f6/1200w.png"
table SegmentationImage (
"attr.rust.derive": "PartialEq",
Expand Down
4 changes: 4 additions & 0 deletions crates/re_types/definitions/rerun/archetypes/tensor.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace rerun.archetypes;

/// A generic n-dimensional Tensor.
///
/// \cpp Since the underlying `rerun::datatypes::TensorData` uses `rerun::Collection` internally,
/// \cpp data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
/// \cpp If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
///
/// \example tensor_simple title="Simple Tensor" image="https://static.rerun.io/tensor_simple/baacb07712f7b706e3c80e696f70616c6c20b367/1200w.png"
table Tensor (
"attr.rust.derive": "PartialEq"
Expand Down
3 changes: 2 additions & 1 deletion crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,8 @@ fn lines_from_docs(docs: &Docs) -> Vec<String> {
if let Some(title) = title {
lines.push(format!("### {title}"));
} else {
lines.push(format!("### `{name}`:"));
// Other languages put the name in backticks but doxygen doesn't support this on headings.
lines.push(format!("### {name}:"));
}

if let Some(image) = image {
Expand Down
5 changes: 1 addition & 4 deletions docs/code-examples/depth_image_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,5 @@ int main() {
)
);

rec.log(
"world/camera/depth",
rerun::DepthImage({HEIGHT, WIDTH}, std::move(data)).with_meter(10000.0)
);
rec.log("world/camera/depth", rerun::DepthImage({HEIGHT, WIDTH}, data).with_meter(10000.0));
}
2 changes: 1 addition & 1 deletion docs/code-examples/depth_image_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ int main() {
std::fill_n(data.begin() + y * WIDTH + 100, 180, static_cast<uint16_t>(45000));
}

rec.log("depth", rerun::DepthImage({HEIGHT, WIDTH}, std::move(data)).with_meter(10000.0));
rec.log("depth", rerun::DepthImage({HEIGHT, WIDTH}, data).with_meter(10000.0));
}
2 changes: 1 addition & 1 deletion docs/code-examples/image_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ int main() {
}
}

rec.log("image", rerun::Image({HEIGHT, WIDTH, 3}, std::move(data)));
rec.log("image", rerun::Image({HEIGHT, WIDTH, 3}, data));
}
2 changes: 1 addition & 1 deletion docs/code-examples/segmentation_image_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ int main() {
})
);

rec.log("image", rerun::SegmentationImage({HEIGHT, WIDTH}, std::move(data)));
rec.log("image", rerun::SegmentationImage({HEIGHT, WIDTH}, data));
}
29 changes: 24 additions & 5 deletions rerun_cpp/src/rerun/archetypes/depth_image.hpp

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

20 changes: 19 additions & 1 deletion rerun_cpp/src/rerun/archetypes/depth_image_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@ namespace rerun::archetypes {

/// New depth image from height/width and tensor buffer.
///
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2.
/// Sets the dimension names to "height" and "width" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2.
/// \param buffer
/// The tensor buffer containing the depth image data.
DepthImage(Collection<datatypes::TensorDimension> shape, datatypes::TensorBuffer buffer)
: DepthImage(datatypes::TensorData(std::move(shape), std::move(buffer))) {}

/// New depth image from tensor data.
///
/// \param data_
/// The tensor buffer containing the depth image data.
/// Sets the dimension names to "height" and "width" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2.
explicit DepthImage(components::TensorData data_);

/// New depth image from dimensions and pointer to depth image data.
///
/// Type must be one of the types supported by `rerun::datatypes::TensorData`.
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2.
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// Determines the number of elements expected to be in `data`.
/// \param data_
/// Target of the pointer must outlive the archetype.
template <typename TElement>
explicit DepthImage(Collection<datatypes::TensorDimension> shape, const TElement* data_)
: DepthImage(datatypes::TensorData(std::move(shape), data_)) {}

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand Down
32 changes: 27 additions & 5 deletions rerun_cpp/src/rerun/archetypes/image.hpp

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

25 changes: 22 additions & 3 deletions rerun_cpp/src/rerun/archetypes/image_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@ namespace rerun::archetypes {

/// New Image from height/width/channel and tensor buffer.
///
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2 or 3.
Image(Collection<datatypes::TensorDimension> shape, datatypes::TensorBuffer buffer)
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2 or 3.
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// \param buffer
/// The tensor buffer containing the image data.
explicit Image(Collection<datatypes::TensorDimension> shape, datatypes::TensorBuffer buffer)
: Image(datatypes::TensorData(std::move(shape), std::move(buffer))) {}

/// New depth image from tensor data.
///
/// \param data_
/// The tensor buffer containing the image data.
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2 or 3.
explicit Image(rerun::components::TensorData data_);

/// New image from dimensions and pointer to image data.
///
/// Type must be one of the types supported by `rerun::datatypes::TensorData`.
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2 or 3.
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// Determines the number of elements expected to be in `data`.
/// \param data_
/// Target of the pointer must outlive the archetype.
template <typename TElement>
explicit Image(Collection<datatypes::TensorDimension> shape, const TElement* data_)
: Image(datatypes::TensorData(std::move(shape), data_)) {}

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand Down
2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/archetypes/line_strips2d.hpp

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

28 changes: 26 additions & 2 deletions rerun_cpp/src/rerun/archetypes/segmentation_image.hpp

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

20 changes: 19 additions & 1 deletion rerun_cpp/src/rerun/archetypes/segmentation_image_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,35 @@ namespace rerun::archetypes {

/// New segmentation image from height/width and tensor buffer.
///
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2.
/// Sets the dimension names to "height" and "width" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2.
/// \param buffer
/// The tensor buffer containing the segmentation image data.
SegmentationImage(Collection<datatypes::TensorDimension> shape, datatypes::TensorBuffer buffer)
: SegmentationImage(datatypes::TensorData(std::move(shape), std::move(buffer))) {}

/// New segmentation image from tensor data.
///
/// \param data_
/// The tensor buffer containing the segmentation image data.
/// Sets the dimension names to "height" and "width" if they are not specified.
/// Calls `Error::handle()` if the shape is not rank 2.
explicit SegmentationImage(components::TensorData data_);

/// New segmentation image from dimensions and pointer to segmentation image data.
///
/// Type must be one of the types supported by `rerun::datatypes::TensorData`.
/// \param shape
/// Shape of the image. Calls `Error::handle()` if the shape is not rank 2.
/// Sets the dimension names to "height", "width" and "channel" if they are not specified.
/// Determines the number of elements expected to be in `data`.
/// \param data_
/// Target of the pointer must outlive the archetype.
template <typename TElement>
explicit SegmentationImage(Collection<datatypes::TensorDimension> shape, const TElement* data_)
: SegmentationImage(datatypes::TensorData(std::move(shape), data_)) {}

// </CODEGEN_COPY_TO_HEADER>
#endif

Expand Down
Loading
Loading