Rerun C++ SDK
Loading...
Searching...
No Matches
demo_utils.hpp
1#pragma once
2
3// Utilities used in examples.
4
5#include <algorithm>
6#include <cmath>
7#include <vector>
8
9#include "components/color.hpp"
10#include "components/position3d.hpp"
11
12namespace rerun {
13 namespace demo {
14 constexpr float PI = 3.14159265358979323846264338327950288f;
15 constexpr float TAU = 6.28318530717958647692528676655900577f;
16
17 /// A linear interpolator that bounces between `a` and `b` as `t` goes above `1.0`.
18 inline float bounce_lerp(float a, float b, float t) {
19 auto tf = t - floorf(t);
20 if (static_cast<int32_t>(t) % 2 == 0) {
21 return (1.0f - tf) * a + tf * b;
22 } else {
23 return tf * a + (1.0f - tf) * b;
24 }
25 }
26
27 /// Linearly interpolates from `a` through `b` in `n` steps, returning the intermediate result at
28 /// each step.
29 template <typename T>
30 inline std::vector<T> linspace(T start, T end, size_t num) {
31 std::vector<T> linspaced(num);
32 std::generate(linspaced.begin(), linspaced.end(), [&, i = 0]() mutable {
33 return static_cast<T>(
34 start + static_cast<T>(i++) * (end - start) / static_cast<T>(num - 1)
35 );
36 });
37 return linspaced;
38 }
39
40 /// Given a range `from`-`to`, linearly interpolates between them in `n` steps along
41 /// three axes each, returning the intermediate result at each step.
42 template <typename T, typename Elem>
43 std::vector<T> grid3d(Elem from, Elem to, size_t n) {
44 std::vector<T> output;
45 for (Elem z : linspace(from, to, n)) {
46 for (Elem y : linspace(from, to, n)) {
47 for (Elem x : linspace(from, to, n)) {
48 output.emplace_back(
49 static_cast<Elem>(x),
50 static_cast<Elem>(y),
51 static_cast<Elem>(z)
52 );
53 }
54 }
55 }
56 return output;
57 }
58
59 /// Create a spiral of points with colors along the Z axis.
60 ///
61 /// * `num_points`: Total number of points.
62 /// * `radius`: The radius of the spiral.
63 /// * `angular_step`: The factor applied between each step along the trigonometric circle.
64 /// * `angular_offset`: Offsets the starting position on the trigonometric circle.
65 /// * `z_step`: The factor applied between each step along the Z axis.
66 void color_spiral(
67 size_t num_points, float radius, float angular_step, float angular_offset, float z_step,
68 std::vector<components::Position3D>& out_points,
69 std::vector<components::Color>& out_colors
70 );
71
72 /// Returns sRGB polynomial approximation from Turbo color map, assuming `t` is normalized.
73 rerun::components::Color colormap_turbo_srgb(float t);
74 } // namespace demo
75} // namespace rerun
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:20
Component: An RGBA color with unmultiplied/separate alpha, in sRGB gamma space with linear alpha.
Definition color.hpp:28