22#include <fmt/format.h>
32template <
typename Derived>
33std::array<float, 3> toStdArray3f(
const Eigen::MatrixBase<Derived>& vec3) {
34 return {
static_cast<float>(vec3[0]),
static_cast<float>(vec3[1]),
static_cast<float>(vec3[2])};
37template <
typename Derived>
38rerun::Position3D toRerunPosition3D(
const Eigen::MatrixBase<Derived>& vec3) {
39 return rerun::Position3D(vec3[0], vec3[1], vec3[2]);
42template <
typename Derived>
43rerun::HalfSize3D toRerunHalfSizes3D(
const Eigen::MatrixBase<Derived>& vec3) {
44 return rerun::HalfSize3D(vec3[0], vec3[1], vec3[2]);
55 data.
centers.reserve(collisionGeometry.size());
56 data.
halfSizes.reserve(collisionGeometry.size());
59 for (
const auto& cg : collisionGeometry) {
66 : skeletonState.
jointState.at(cg.parent).transform;
67 const Transform tf = parentTransform * cg.transformation;
71 data.
halfSizes.emplace_back(cg.ellipsoidRadii * parentTransform.
scale);
82 data.
centers.reserve(collisionGeometry.size());
83 data.
halfSizes.reserve(collisionGeometry.size());
86 for (
const auto& cg : collisionGeometry) {
93 : skeletonState.
jointState.at(cg.parent).transform;
94 const Transform tf = parentTransform * cg.transformation;
97 data.
halfSizes.emplace_back(cg.boxHalfExtents * parentTransform.
scale);
107 const rerun::RecordingStream& rec,
108 const std::string& streamName,
110 std::optional<rerun::Color> color) {
111 auto rerunMesh = rerun::Mesh3D(mesh.
vertices).with_triangle_indices(mesh.
faces);
112 if (color.has_value()) {
113 rerunMesh = std::move(rerunMesh).with_albedo_factor(rerun::AlbedoFactor(color.value()));
115 rerunMesh = std::move(rerunMesh).with_vertex_colors(mesh.
colors);
119 rerunMesh = std::move(rerunMesh).with_vertex_normals(mesh.
normals);
122 safeLog(rec, streamName, rerunMesh);
126 const rerun::RecordingStream& rec,
127 const std::string& streamName,
130 const rerun::Color kGrey(180, 180, 180);
132 std::vector<std::string> labels;
133 std::vector<std::vector<std::array<float, 3>>> lines;
134 labels.reserve(names.size());
135 lines.reserve(names.size());
136 for (
size_t iJoint = 0; iJoint < jointStates.size(); ++iJoint) {
137 if (iJoint >= skeleton.
joints.size()) {
140 const size_t parentIdx = skeleton.
joints[iJoint].parent;
141 if (parentIdx !=
kInvalidIndex && parentIdx < jointStates.size()) {
143 {toStdArray3f(jointStates[parentIdx].transform.translation),
144 toStdArray3f(jointStates[iJoint].transform.translation)});
145 labels.push_back(names[iJoint]);
149 streamName +
"/" + names[iJoint],
152 rerun::datatypes::Mat3x3(jointStates[iJoint].transform.toRotationMatrix().data()))
154 rerun::datatypes::Vec3D(jointStates[iJoint].transform.translation.data()))
155#
if !defined(RERUN_VERSION_GE) || !RERUN_VERSION_GE(0, 24, 0)
156 .with_axis_length(rerun::components::AxisLength(10))
163 rerun::LineStrips3D(lines).with_radii(0.2f).with_colors(kGrey).with_labels(labels));
167 const rerun::RecordingStream& rec,
168 const std::string& streamName,
169 std::span<const Marker> markers) {
170 const rerun::Color kPurple(120, 120, 255);
171 std::vector<rerun::Position3D> points3d;
172 std::vector<std::string> labels;
173 points3d.reserve(markers.size());
174 labels.reserve(markers.size());
176 for (
const auto& marker : markers) {
177 if (marker.occluded) {
180 points3d.push_back(toRerunPosition3D(marker.pos));
181 labels.push_back(marker.name);
188 rerun::Points3D(points3d).with_radii(0.5f).with_colors(kPurple).with_labels(labels));
192 const rerun::RecordingStream& rec,
193 const std::string& streamName,
196 if (locators.empty()) {
200 const rerun::Color kGreen(100, 255, 100);
201 std::vector<std::string> labels;
202 std::vector<rerun::Position3D> points3d;
203 std::vector<rerun::Color> colors;
204 points3d.reserve(locatorState.
position.size());
205 labels.reserve(locatorState.
position.size());
206 colors.reserve(locatorState.
position.size());
208 for (
size_t i = 0; i < locatorState.
position.size(); ++i) {
209 const auto& locator = locatorState.
position[i];
210 points3d.push_back(toRerunPosition3D(locator));
211 labels.push_back(locators[i].name);
212 rerun::Color color{};
213 if (locators.at(i).name.find(
"Floor_") != std::string::npos) {
216 float intensity = 255.0f * locators[i].weight * 0.6f;
217 color = rerun::Color(
218 static_cast<uint8_t
>(intensity),
219 static_cast<uint8_t
>(intensity),
220 static_cast<uint8_t
>(intensity * 0.5f));
222 colors.push_back(color);
229 rerun::Points3D(points3d).with_radii(0.5f).with_colors(colors).with_labels(labels));
233 const rerun::RecordingStream& rec,
234 const std::string& streamName,
235 const std::map<std::string, size_t>& locatorLookup,
237 std::span<const Marker> markers,
238 const float kPositionErrorThreshold) {
239 if (locatorLookup.empty()) {
243 std::vector<std::string> labels;
244 std::vector<std::vector<std::array<float, 3>>> lines;
245 std::vector<rerun::components::Color> colors;
246 std::vector<rerun::components::Radius> radius;
247 lines.reserve(markers.size());
248 labels.reserve(markers.size());
249 colors.reserve(markers.size());
250 radius.reserve(markers.size());
252 const rerun::components::Color kGreenColor(50, 255, 128);
253 const rerun::components::Color kRedColor(255, 100, 100);
254 const rerun::components::Radius kDefaultRadius(0.1f);
255 const rerun::components::Radius kLargeRadius(0.5f);
257 for (
const auto& marker : markers) {
258 if (!marker.occluded && (locatorLookup.count(marker.name) != 0)) {
259 const auto locatorIdx = locatorLookup.at(marker.name);
260 const auto locator = locatorState.
position.at(locatorIdx);
262 lines.push_back({toStdArray3f(marker.pos), toStdArray3f(locator)});
263 labels.push_back(marker.name);
266 const auto error = (marker.pos.cast<
float>() - locator).squaredNorm();
267 if (error > kPositionErrorThreshold) {
268 colors.push_back(kRedColor);
269 radius.push_back(kLargeRadius);
271 colors.push_back(kGreenColor);
272 radius.push_back(kDefaultRadius);
279 rerun::LineStrips3D(lines).with_labels(labels).with_colors(colors).with_radii(radius));
284 const rerun::RecordingStream& rec,
285 const std::string& worldPrefix,
286 const std::string& posePrefix,
287 std::span<const std::string> names,
288 const Eigen::VectorXf& params) {
290 const size_t nParams = params.size();
292 for (
size_t iParam = 0; iParam < nParams; ++iParam) {
293 if (names[iParam].find(
"root") != std::string::npos) {
294 safeLog(rec, fmt::format(
"{}/{}", worldPrefix, names[iParam]),
makeScalar(params[iParam]));
296 safeLog(rec, fmt::format(
"{}/{}", posePrefix, names[iParam]),
makeScalar(params[iParam]));
302 const rerun::RecordingStream& rec,
303 const std::string& worldPrefix,
304 const std::string& posePrefix,
305 std::span<const std::string> names,
306 const Eigen::VectorXf& params) {
309 for (
size_t iJoint = 0; iJoint < names.size(); ++iJoint) {
313 if (names[iJoint].find(
"world") != std::string::npos ||
314 names[iJoint].find(
"root") != std::string::npos) {
315 safeLog(rec, fmt::format(
"{}/{}", worldPrefix, channelName),
makeScalar(params[paramIdx]));
317 safeLog(rec, fmt::format(
"{}/{}", posePrefix, channelName),
makeScalar(params[paramIdx]));
324 const rerun::RecordingStream& rec,
325 const std::string& worldPrefix,
326 const std::string& posePrefix,
327 std::span<const std::string> names) {
328 for (
const auto& name : names) {
329 if (name.find(
"root") != std::string::npos) {
338 const rerun::RecordingStream& rec,
339 const std::string& worldPrefix,
340 const std::string& posePrefix,
341 std::span<const std::string> names) {
342 for (
const auto& name : names) {
344 const std::string channelName = name +
"_" + jointParameterName;
345 if (name.find(
"world") != std::string::npos || name.find(
"root") != std::string::npos) {
348 fmt::format(
"{}/{}", worldPrefix, channelName),
353 fmt::format(
"{}/{}", posePrefix, channelName),
361 const rerun::RecordingStream& rec,
362 const std::string& worldPrefix,
363 const std::string& posePrefix,
364 std::span<const std::string> names,
366 std::span<const int64_t> frameIndices,
367 std::span<const double> times) {
368 const auto nFrames =
static_cast<size_t>(
allParams.cols());
376 rerun::Collection<int64_t>::borrow(frameIndices.data(), frameIndices.size()),
377 rerun::SortingStatus::Sorted);
380 rerun::Collection<double>::borrow(times.data(), times.size()),
381 rerun::SortingStatus::Sorted);
382 const std::vector<rerun::TimeColumn> timeColumns = {
383 std::move(frameIndexColumn), std::move(timeColumn)};
385 const size_t nParams = std::min(
static_cast<size_t>(
allParams.rows()), names.size());
386 for (
size_t iParam = 0; iParam < nParams; ++iParam) {
388 std::vector<double> scalarValues(nFrames);
389 for (
size_t iFrame = 0; iFrame < nFrames; ++iFrame) {
390 scalarValues[iFrame] =
static_cast<double>(
allParams(iParam, iFrame));
395 if (names[iParam].find(
"root") != std::string::npos) {
397 fmt::format(
"{}/{}", worldPrefix, names[iParam]), timeColumns, scalarColumns);
399 rec.send_columns(fmt::format(
"{}/{}", posePrefix, names[iParam]), timeColumns, scalarColumns);
405 const rerun::RecordingStream& rec,
406 const std::string& worldPrefix,
407 const std::string& posePrefix,
408 std::span<const std::string> names,
409 const Eigen::MatrixXf& allJointParams,
410 std::span<const int64_t> frameIndices,
411 std::span<const double> times) {
412 const auto nFrames =
static_cast<size_t>(allJointParams.cols());
420 rerun::Collection<int64_t>::borrow(frameIndices.data(), frameIndices.size()),
421 rerun::SortingStatus::Sorted);
424 rerun::Collection<double>::borrow(times.data(), times.size()),
425 rerun::SortingStatus::Sorted);
426 const std::vector<rerun::TimeColumn> timeColumns = {
427 std::move(frameIndexColumn), std::move(timeColumn)};
430 const size_t nJoints =
432 for (
size_t iJoint = 0; iJoint < nJoints; ++iJoint) {
438 std::vector<double> scalarValues(nFrames);
439 for (
size_t iFrame = 0; iFrame < nFrames; ++iFrame) {
440 scalarValues[iFrame] =
static_cast<double>(allJointParams(paramIdx, iFrame));
445 if (names[iJoint].find(
"world") != std::string::npos ||
446 names[iJoint].find(
"root") != std::string::npos) {
448 fmt::format(
"{}/{}", worldPrefix, channelName), timeColumns, scalarColumns);
450 rec.send_columns(fmt::format(
"{}/{}", posePrefix, channelName), timeColumns, scalarColumns);
457 const rerun::RecordingStream& rec,
458 const std::string& streamName,
463 collisionState.
update(skeletonState, collisionGeometry);
466 const auto n = collisionGeometry.size();
467 std::vector<axel::BoundingBoxf> aabbs(n);
468 for (
size_t i = 0; i < n; ++i) {
469 auto& aabb = aabbs[i];
470 aabb.id =
static_cast<axel::Index
>(i);
476 bvh.setBoundingBoxes(aabbs);
477 const auto& bvs = bvh.getPrimitives();
480 std::vector<rerun::Position3D> centers;
481 std::vector<rerun::HalfSize3D> halfSizes;
482 centers.reserve(bvs.size());
483 halfSizes.reserve(bvs.size());
484 for (
const auto& bv : bvs) {
485 centers.push_back(toRerunPosition3D(bv.center()));
486 halfSizes.push_back(toRerunHalfSizes3D((bv.max() - bv.min()) / 2.0f));
494 rerun::Boxes3D::from_centers_and_half_sizes(centers, halfSizes)
496 .with_colors(rerun::Color(64, 128, 64)));
500 const rerun::RecordingStream& rec,
501 const std::string& streamName,
504 std::vector<rerun::Position3D> capsuleTranslations;
505 std::vector<rerun::Quaternion> capsuleQuaternions;
506 std::vector<float> capsuleLengths;
507 std::vector<float> capsuleRadii;
509 capsuleTranslations.reserve(collisionGeometry.size());
510 capsuleQuaternions.reserve(collisionGeometry.size());
511 capsuleLengths.reserve(collisionGeometry.size());
512 capsuleRadii.reserve(collisionGeometry.size());
514 for (
const auto& cg : collisionGeometry) {
520 : skeletonState.
jointState.at(cg.parent).transform;
521 const Transform tf = parentTransform * cg.transformation;
524 capsuleTranslations.push_back(toRerunPosition3D(tf.
translation));
525 capsuleQuaternions.emplace_back(rerun::Quaternion::from_xyzw(q.x(), q.y(), q.z(), q.w()));
529 capsuleLengths.emplace_back(cg.length * tf.
scale);
531 capsuleRadii.emplace_back(cg.radius.maxCoeff() * parentTransform.
scale);
534 if (!capsuleLengths.empty()) {
537 streamName +
"/capsules",
538 rerun::Capsules3D::from_lengths_and_radii(capsuleLengths, capsuleRadii)
539 .with_translations(capsuleTranslations)
540 .with_quaternions(capsuleQuaternions)
541 .with_colors(rerun::Color(128, 64, 64))
542 .with_fill_mode(rerun::FillMode::Solid));
545 const auto ellipsoidLogData =
547 if (!ellipsoidLogData.halfSizes.empty()) {
548 std::vector<rerun::Position3D> ellipsoidCenters;
549 std::vector<rerun::HalfSize3D> ellipsoidHalfSizes;
550 std::vector<rerun::Quaternion> ellipsoidQuaternions;
551 ellipsoidCenters.reserve(ellipsoidLogData.centers.size());
552 ellipsoidHalfSizes.reserve(ellipsoidLogData.halfSizes.size());
553 ellipsoidQuaternions.reserve(ellipsoidLogData.quaternions.size());
555 for (
size_t i = 0; i < ellipsoidLogData.halfSizes.size(); ++i) {
556 const Quaternionf& q = ellipsoidLogData.quaternions[i];
557 ellipsoidCenters.push_back(toRerunPosition3D(ellipsoidLogData.centers[i]));
558 ellipsoidHalfSizes.push_back(toRerunHalfSizes3D(ellipsoidLogData.halfSizes[i]));
559 ellipsoidQuaternions.emplace_back(rerun::Quaternion::from_xyzw(q.x(), q.y(), q.z(), q.w()));
564 streamName +
"/ellipsoids",
565 rerun::Ellipsoids3D::from_centers_and_half_sizes(ellipsoidCenters, ellipsoidHalfSizes)
566 .with_quaternions(ellipsoidQuaternions)
567 .with_colors(rerun::Color(128, 64, 64))
568 .with_fill_mode(rerun::FillMode::Solid));
572 if (!boxLogData.halfSizes.empty()) {
573 std::vector<rerun::Position3D> boxCenters;
574 std::vector<rerun::HalfSize3D> boxHalfSizes;
575 std::vector<rerun::Quaternion> boxQuaternions;
576 boxCenters.reserve(boxLogData.centers.size());
577 boxHalfSizes.reserve(boxLogData.halfSizes.size());
578 boxQuaternions.reserve(boxLogData.quaternions.size());
580 for (
size_t i = 0; i < boxLogData.halfSizes.size(); ++i) {
582 boxCenters.push_back(toRerunPosition3D(boxLogData.centers[i]));
583 boxHalfSizes.push_back(toRerunHalfSizes3D(boxLogData.halfSizes[i]));
584 boxQuaternions.emplace_back(rerun::Quaternion::from_xyzw(q.x(), q.y(), q.z(), q.w()));
589 streamName +
"/boxes",
590 rerun::Boxes3D::from_centers_and_half_sizes(boxCenters, boxHalfSizes)
591 .with_quaternions(boxQuaternions)
592 .with_colors(rerun::Color(128, 64, 64))
593 .with_fill_mode(rerun::FillMode::Solid));
596 logBvh(rec, streamName +
"/bvh", collisionGeometry, skeletonState);
600 const rerun::RecordingStream& rec,
601 const std::string& charStreamName,
604 const rerun::Color& color) {
605 if (characterState.
meshState !=
nullptr) {
615 if (
const auto& collision = character.
collision) {
617 rec, charStreamName +
"/collision_geometry", *collision, characterState.
skeletonState);
CollisionBoxLogData makeCollisionBoxLogData(const CollisionGeometry &collisionGeometry, const SkeletonState &skeletonState)
Extracts world-space box collision geometry from the current skeleton state.
CollisionEllipsoidLogData makeCollisionEllipsoidLogData(const CollisionGeometry &collisionGeometry, const SkeletonState &skeletonState)
Extracts world-space ellipsoid collision geometry from the current skeleton state.
void logModelParams(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names, const Eigen::VectorXf ¶ms)
TransformT< float > Transform
CollisionGeometryT< float > CollisionGeometry
SkeletonT< float > Skeleton
CollisionGeometryStateT< float > CollisionGeometryState
Quaternion< float > Quaternionf
rerun::TimeColumn makeSequenceTimeColumn(std::string timelineName, rerun::Collection< int64_t > values, rerun::SortingStatus sortingStatus=rerun::SortingStatus::Unknown)
void logMarkers(const rerun::RecordingStream &rec, const std::string &streamName, std::span< const Marker > markers)
CharacterStateT< float > CharacterState
auto makeSeriesLineWithName(const std::string &name)
rerun::TimeColumn makeDurationSecondsTimeColumn(std::string timelineName, rerun::Collection< double > values, rerun::SortingStatus sortingStatus=rerun::SortingStatus::Unknown)
ParameterSet allParams()
Creates a ParameterSet with all parameters set to active.
JointStateListT< float > JointStateList
void logJoints(const rerun::RecordingStream &rec, const std::string &streamName, const Skeleton &skeleton, const JointStateList &jointStates)
void logCharacter(const rerun::RecordingStream &rec, const std::string &charStreamName, const Character &character, const CharacterState &characterState, const rerun::Color &color)
void logJointParamNames(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names)
constexpr std::array< const char *, kParametersPerJoint > kJointParameterNames
void logMesh(const rerun::RecordingStream &rec, const std::string &streamName, const Mesh &mesh, std::optional< rerun::Color > color)
void logModelParamsColumns(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names, const Eigen::MatrixXf &allParams, std::span< const int64_t > frameIndices, std::span< const double > times)
Log model parameters for all frames at once using send_columns.
void logBvh(const rerun::RecordingStream &rec, const std::string &streamName, const CollisionGeometry &collisionGeometry, const SkeletonState &skeletonState)
void logMarkerLocatorCorrespondence(const rerun::RecordingStream &rec, const std::string &streamName, const std::map< std::string, size_t > &locatorLookup, const LocatorState &locatorState, std::span< const Marker > markers, const float kPositionErrorThreshold)
SkeletonStateT< float > SkeletonState
void updateAabb(axel::BoundingBox< T > &aabb, const Vector3< T > &originA, const Vector3< T > &direction, const Vector2< T > &radii)
Updates an axis-aligned bounding box to encompass a tapered capsule.
void logCollisionGeometry(const rerun::RecordingStream &rec, const std::string &streamName, const CollisionGeometry &collisionGeometry, const SkeletonState &skeletonState)
constexpr size_t kParametersPerJoint
void logModelParamNames(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names)
rerun::Collection< rerun::ComponentColumn > makeScalarColumns(std::vector< double > values)
void logJointParams(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names, const Eigen::VectorXf ¶ms)
void logJointParamsColumns(const rerun::RecordingStream &rec, const std::string &worldPrefix, const std::string &posePrefix, std::span< const std::string > names, const Eigen::MatrixXf &allJointParams, std::span< const int64_t > frameIndices, std::span< const double > times)
Log joint parameters for all frames at once using send_columns.
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
void safeLog(const rerun::RecordingStream &rec, std::string_view entityPath, const T &archetype)
Safe wrapper for RecordingStream::log that skips empty archetypes.
constexpr T pi() noexcept
Returns the mathematical constant pi.
void logLocators(const rerun::RecordingStream &rec, const std::string &streamName, const LocatorList &locators, const LocatorState &locatorState)
void safeLogStatic(const rerun::RecordingStream &rec, std::string_view entityPath, const T &archetype)
Safe wrapper for RecordingStream::log_static that skips empty archetypes.
std::vector< Locator > LocatorList
A collection of locators attached to a skeleton.
Mesh_u meshState
Skinned mesh; null when constructed/updated with updateMesh=false or when the reference character has...
SkeletonState skeletonState
LocatorState locatorState
A character model with skeletal structure, mesh, and optional components.
LocatorList locators
Points of interest attached to joints.
Skeleton skeleton
Skeletal structure defining the character's joints and hierarchy.
CollisionGeometry_u collision
Collision volumes for the character.
void update(const SkeletonStateT< T > &skeletonState, const CollisionGeometry &collisionGeometry)
Updates the state based on a given skeleton state and collision geometry.
Tracks the current world positions of locators in a skeleton.
std::vector< Vector3f > position
World-space positions of all locators, updated when the skeleton moves.
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
std::vector< Eigen::Vector3< T > > normals
List of vertex normals (same size as vertices when computed).
std::vector< Eigen::Vector3i > faces
List of triangular faces defined by vertex indices.
std::vector< Eigen::Vector3b > colors
List of per-vertex RGB colors (0-255 for each channel).
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...
std::vector< std::string > getJointNames() const
World-space box collision primitive data prepared for Rerun box logging.
std::vector< Quaternionf > quaternions
std::vector< Vector3f > halfSizes
std::vector< Vector3f > centers
World-space ellipsoid collision primitive data prepared for Rerun ellipsoid logging.
std::vector< Quaternionf > quaternions
std::vector< Vector3f > halfSizes
std::vector< Vector3f > centers