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 a C++ example of BarChart with added ergonomics #3837

Merged
merged 13 commits into from
Oct 13, 2023
15 changes: 12 additions & 3 deletions crates/re_types_builder/src/codegen/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,10 @@ fn quote_variable(
Type::Int32 => quote! { std::optional<int32_t> #name },
Type::Int64 => quote! { std::optional<int64_t> #name },
Type::Bool => quote! { std::optional<bool> #name },
Type::Float16 => quote! { std::optional<uint16_t> #name },
Type::Float16 => {
includes.insert_rerun("half.hpp");
quote! { std::optional<rerun::half> #name }
}
Type::Float32 => quote! { std::optional<float> #name },
Type::Float64 => quote! { std::optional<double> #name },
Type::String => {
Expand Down Expand Up @@ -1928,7 +1931,10 @@ fn quote_variable(
Type::Int32 => quote! { int32_t #name },
Type::Int64 => quote! { int64_t #name },
Type::Bool => quote! { bool #name },
Type::Float16 => quote! { uint16_t #name },
Type::Float16 => {
includes.insert_rerun("half.hpp");
quote! { rerun::half #name }
}
Type::Float32 => quote! { float #name },
Type::Float64 => quote! { double #name },
Type::String => {
Expand Down Expand Up @@ -1966,7 +1972,10 @@ fn quote_element_type(includes: &mut Includes, typ: &ElementType) -> TokenStream
ElementType::Int32 => quote! { int32_t },
ElementType::Int64 => quote! { int64_t },
ElementType::Bool => quote! { bool },
ElementType::Float16 => quote! { uint16_t },
ElementType::Float16 => {
includes.insert_rerun("half.hpp");
quote! { rerun::half }
}
ElementType::Float32 => quote! { float },
ElementType::Float64 => quote! { double },
ElementType::String => {
Expand Down
14 changes: 6 additions & 8 deletions docs/code-examples/annotation_context_rects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,28 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_annotation_context_rects");
auto rec = rerun::RecordingStream("rerun_example_annotation_context_rects");
rec.connect("127.0.0.1:9876").throw_on_failure();

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

// Log a batch of 2 rectangles with different class IDs
rec.log(
"detections",
rr::Boxes2D::from_mins_and_sizes(
rerun::Boxes2D::from_mins_and_sizes(
{{-2.0f, -2.0f}, {0.0f, 0.f}},
{{3.0f, 3.0f}, {2.0f, 2.0f}}
).with_class_ids({1, 2})
);

// Log an extra rect to set the view bounds
rec.log("bounds", rr::Boxes2D::from_half_sizes({{2.5f, 2.5f}}));
rec.log("bounds", rerun::Boxes2D::from_half_sizes({{2.5f, 2.5f}}));
}
12 changes: 5 additions & 7 deletions docs/code-examples/arrow3d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
#include <cmath>
#include <numeric>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_arrow3d");
auto rec = rerun::RecordingStream("rerun_example_arrow3d");
rec.connect("127.0.0.1:9876").throw_on_failure();

std::vector<rr::components::Position3D> origins;
std::vector<rr::components::Vector3D> vectors;
std::vector<rr::components::Color> colors;
std::vector<rerun::components::Position3D> origins;
std::vector<rerun::components::Vector3D> vectors;
std::vector<rerun::components::Color> colors;

for (int i = 0; i < 100; ++i) {
origins.push_back({0, 0, 0});
Expand All @@ -28,6 +26,6 @@ int main() {

rec.log(
"arrows",
rr::Arrows3D::from_vectors(vectors).with_origins(origins).with_colors(colors)
rerun::Arrows3D::from_vectors(vectors).with_origins(origins).with_colors(colors)
);
}
8 changes: 3 additions & 5 deletions docs/code-examples/asset3d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <string>
#include <vector>

namespace rr = rerun;

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv, argv + argc);

Expand All @@ -19,9 +17,9 @@ int main(int argc, char* argv[]) {

std::string path = args[1];

auto rec = rr::RecordingStream("rerun_example_asset3d_simple");
auto rec = rerun::RecordingStream("rerun_example_asset3d_simple");
rec.connect("127.0.0.1:9876").throw_on_failure();

rec.log("world", rr::ViewCoordinates::RIGHT_HAND_Z_UP); // Set an up-axis
rec.log("world/asset", rr::Asset3D::from_file(path));
rec.log("world", rerun::ViewCoordinates::RIGHT_HAND_Z_UP); // Set an up-axis
rec.log("world/asset", rerun::Asset3D::from_file(path));
}
10 changes: 10 additions & 0 deletions docs/code-examples/bar_chart.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Log a batch of 3D arrows.

#include <rerun.hpp>

int main() {
auto rec = rerun::RecordingStream("rerun_example_bar_chart");
rec.connect("127.0.0.1:9876").throw_on_failure();

rec.log("bar_chart", rerun::BarChart::i64({8, 4, 0, 9, 1, 4, 1, 6, 9, 0}));
}
8 changes: 3 additions & 5 deletions docs/code-examples/box2d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_box2d");
auto rec = rerun::RecordingStream("rerun_example_box2d");
rec.connect("127.0.0.1:9876").throw_on_failure();

rec.log("simple", rr::Boxes2D::from_mins_and_sizes({{-1.f, -1.f}}, {{2.f, 2.f}}));
rec.log("simple", rerun::Boxes2D::from_mins_and_sizes({{-1.f, -1.f}}, {{2.f, 2.f}}));

// Log an extra rect to set the view bounds
rec.log("bounds", rr::Boxes2D::from_sizes({{4.f, 3.f}}));
rec.log("bounds", rerun::Boxes2D::from_sizes({{4.f, 3.f}}));
}
21 changes: 10 additions & 11 deletions docs/code-examples/box3d_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_box3d_batch");
auto rec = rerun::RecordingStream("rerun_example_box3d_batch");
rec.connect("127.0.0.1:9876").throw_on_failure();

rec.log(
"batch",
rr::Boxes3D::from_centers_and_half_sizes(
rerun::Boxes3D::from_centers_and_half_sizes(
{{2.0f, 0.0f, 0.0f}, {-2.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 2.0f}},
{{2.0f, 2.0f, 1.0f}, {1.0f, 1.0f, 0.5f}, {2.0f, 0.5f, 1.0f}}
)
.with_rotations({
rr::datatypes::Quaternion::IDENTITY,
rr::datatypes::Quaternion(0.0f, 0.0f, 0.382683f, 0.923880f), // 45 degrees around Z
rr::datatypes::RotationAxisAngle(
rerun::datatypes::Quaternion::IDENTITY,
rerun::datatypes::Quaternion(0.0f, 0.0f, 0.382683f, 0.923880f), // 45 degrees around
// Z
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
rerun::datatypes::RotationAxisAngle(
{0.0f, 1.0f, 0.0f},
rr::datatypes::Angle::degrees(30.0f)
rerun::datatypes::Angle::degrees(30.0f)
),
})
.with_radii({0.025f})
.with_colors({
rr::datatypes::Rgba32(255, 0, 0),
rr::datatypes::Rgba32(0, 255, 0),
rr::datatypes::Rgba32(0, 0, 255),
rerun::datatypes::Rgba32(255, 0, 0),
rerun::datatypes::Rgba32(0, 255, 0),
rerun::datatypes::Rgba32(0, 0, 255),
})
.with_labels({"red", "green", "blue"})
);
Expand Down
6 changes: 2 additions & 4 deletions docs/code-examples/box3d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_box3d_simple");
auto rec = rerun::RecordingStream("rerun_example_box3d_simple");
rec.connect("127.0.0.1:9876").throw_on_failure();

rec.log("simple", rr::Boxes3D::from_half_sizes({{2.f, 2.f, 1.0f}}));
rec.log("simple", rerun::Boxes3D::from_half_sizes({{2.f, 2.f, 1.0f}}));
}
21 changes: 9 additions & 12 deletions docs/code-examples/clear_recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,36 @@
#include <cmath>
#include <numeric>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_clear_recursive");
auto rec = rerun::RecordingStream("rerun_example_clear_recursive");
rec.connect("127.0.0.1:9876").throw_on_failure();

std::vector<rr::components::Vector3D> vectors = {
std::vector<rerun::components::Vector3D> vectors = {
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
};
std::vector<rr::components::Position3D> origins = {
std::vector<rerun::components::Position3D> origins = {
{-0.5, 0.5, 0.0},
{0.5, 0.5, 0.0},
{0.5, -0.5, 0.0},
{-0.5, -0.5, 0.0},
};
std::vector<rr::components::Color> colors = {
{200, 0, 0},
{0, 200, 0},
{0, 0, 200},
{200, 0, 200}};
std::vector<rerun::components::Color> colors =
{{200, 0, 0}, {0, 200, 0}, {0, 0, 200}, {200, 0, 200}};

// Log a handful of arrows.
for (int i = 0; i < vectors.size(); ++i) {
auto entity_path = "arrows/" + std::to_string(i);
rec.log(
entity_path.c_str(),
rr::Arrows3D::from_vectors(vectors[i]).with_origins(origins[i]).with_colors(colors[i])
rerun::Arrows3D::from_vectors(vectors[i])
.with_origins(origins[i])
.with_colors(colors[i])
);
}

// Now clear all of them at once.
rec.log("arrows", rr::Clear::RECURSIVE);
rec.log("arrows", rerun::Clear::RECURSIVE);
}
21 changes: 9 additions & 12 deletions docs/code-examples/clear_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,39 @@
#include <cmath>
#include <numeric>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_clear_simple");
auto rec = rerun::RecordingStream("rerun_example_clear_simple");
rec.connect("127.0.0.1:9876").throw_on_failure();

std::vector<rr::components::Vector3D> vectors = {
std::vector<rerun::components::Vector3D> vectors = {
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
};
std::vector<rr::components::Position3D> origins = {
std::vector<rerun::components::Position3D> origins = {
{-0.5, 0.5, 0.0},
{0.5, 0.5, 0.0},
{0.5, -0.5, 0.0},
{-0.5, -0.5, 0.0},
};
std::vector<rr::components::Color> colors = {
{200, 0, 0},
{0, 200, 0},
{0, 0, 200},
{200, 0, 200}};
std::vector<rerun::components::Color> colors =
{{200, 0, 0}, {0, 200, 0}, {0, 0, 200}, {200, 0, 200}};

// Log a handful of arrows.
for (int i = 0; i < vectors.size(); ++i) {
auto entity_path = "arrows/" + std::to_string(i);
rec.log(
entity_path.c_str(),
rr::Arrows3D::from_vectors(vectors[i]).with_origins(origins[i]).with_colors(colors[i])
rerun::Arrows3D::from_vectors(vectors[i])
.with_origins(origins[i])
.with_colors(colors[i])
);
}

// Now clear them, one by one on each tick.
for (int i = 0; i < vectors.size(); ++i) {
auto entity_path = "arrows/" + std::to_string(i);
rec.log(entity_path.c_str(), rr::Clear::FLAT);
rec.log(entity_path.c_str(), rerun::Clear::FLAT);
}
}
16 changes: 7 additions & 9 deletions docs/code-examples/line_segments2d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_line_segments2d");
auto rec = rerun::RecordingStream("rerun_example_line_segments2d");
rec.connect("127.0.0.1:9876").throw_on_failure();

// TODO(#3202): I want to do this!
// std::vector<std::vector<rr::datatypes::Vec2D>> points = {
// std::vector<std::vector<rerun::datatypes::Vec2D>> points = {
// {{0.f, 0.f}, {2.f, 1.f}},
// {{4.f, -1.f}, {6.f, 0.f}},
// };
// rec.log("segments", rr::LineStrips2D(points));
// rec.log("segments", rerun::LineStrips2D(points));

std::vector<rr::datatypes::Vec2D> points1 = {{0.f, 0.f}, {2.f, 1.f}};
std::vector<rr::datatypes::Vec2D> points2 = {{4.f, -1.f}, {6.f, 0.f}};
rec.log("segments", rr::LineStrips2D({points1, points2}));
std::vector<rerun::datatypes::Vec2D> points1 = {{0.f, 0.f}, {2.f, 1.f}};
std::vector<rerun::datatypes::Vec2D> points2 = {{4.f, -1.f}, {6.f, 0.f}};
rec.log("segments", rerun::LineStrips2D({points1, points2}));

// Log an extra rect to set the view bounds
rec.log("bounds", rr::Boxes2D::from_centers_and_sizes({{3.0f, 0.0f}}, {{8.0f, 6.0f}}));
rec.log("bounds", rerun::Boxes2D::from_centers_and_sizes({{3.0f, 0.0f}}, {{8.0f, 6.0f}}));
}
18 changes: 8 additions & 10 deletions docs/code-examples/line_segments3d_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

#include <rerun.hpp>

namespace rr = rerun;

int main() {
auto rec = rr::RecordingStream("rerun_example_line_segments3d");
auto rec = rerun::RecordingStream("rerun_example_line_segments3d");
rec.connect("127.0.0.1:9876").throw_on_failure();

// TODO(#3202): I want to do this!
// std::vector<std::vector<rr::datatypes::Vec3D>> points = {
// std::vector<std::vector<rerun::datatypes::Vec3D>> points = {
// {{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}},
// {{1.f, 0.f, 0.f}, {1.f, 0.f, 1.f}},
// {{1.f, 1.f, 0.f}, {1.f, 1.f, 1.f}},
// {{0.f, 1.f, 0.f}, {0.f, 1.f, 1.f}},
// };
// rec.log("segments", rr::LineStrips3D(points));
// rec.log("segments", rerun::LineStrips3D(points));

std::vector<rr::datatypes::Vec3D> points1 = {{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}};
std::vector<rr::datatypes::Vec3D> points2 = {{1.f, 0.f, 0.f}, {1.f, 0.f, 1.f}};
std::vector<rr::datatypes::Vec3D> points3 = {{1.f, 1.f, 0.f}, {1.f, 1.f, 1.f}};
std::vector<rr::datatypes::Vec3D> points4 = {{0.f, 1.f, 0.f}, {0.f, 1.f, 1.f}};
rec.log("segments", rr::LineStrips3D({points1, points2, points3, points4}));
std::vector<rerun::datatypes::Vec3D> points1 = {{0.f, 0.f, 0.f}, {0.f, 0.f, 1.f}};
std::vector<rerun::datatypes::Vec3D> points2 = {{1.f, 0.f, 0.f}, {1.f, 0.f, 1.f}};
std::vector<rerun::datatypes::Vec3D> points3 = {{1.f, 1.f, 0.f}, {1.f, 1.f, 1.f}};
std::vector<rerun::datatypes::Vec3D> points4 = {{0.f, 1.f, 0.f}, {0.f, 1.f, 1.f}};
rec.log("segments", rerun::LineStrips3D({points1, points2, points3, points4}));
}
Loading