Skip to content

Commit

Permalink
Use Rerun C++ code conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Feb 8, 2024
1 parent 4127be5 commit bf0e064
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 77 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ FetchContent_MakeAvailable(vrslib)

find_package(fmt REQUIRED)

add_executable(rerun_vrs_example src/main.cpp src/FramePlayer.cpp src/IMUPlayer.cpp)
add_executable(rerun_vrs_example src/main.cpp src/frame_player.cpp src/imu_player.cpp)
target_link_libraries(rerun_vrs_example rerun_sdk vrslib vrs_utils fmt)
target_include_directories(rerun_vrs_example PRIVATE src)
34 changes: 17 additions & 17 deletions src/FramePlayer.cpp → src/frame_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <vrs/DataLayout.h>
#include <vrs/utils/PixelFrame.h>

#include "FramePlayer.h"
#include "frame_player.hpp"

namespace rerun_vrs {

Expand All @@ -32,55 +32,55 @@ namespace rerun_vrs {
};

FramePlayer::FramePlayer(vrs::StreamId id, std::shared_ptr<const rerun::RecordingStream> rec)
: id_{id}, rec_{rec}, entityPath_{rerun::new_entity_path({id.getName()})} {}
: _id{id}, _rec{rec}, _entity_path{rerun::new_entity_path({id.getName()})} {}

bool FramePlayer::onDataLayoutRead(
const vrs::CurrentRecord& record, size_t blockIndex, vrs::DataLayout& layout
const vrs::CurrentRecord& record, size_t block_index, vrs::DataLayout& layout
) {
if (!enabled_) {
if (!_enabled) {
return false;
}

std::ostringstream buffer;
layout.printLayoutCompact(buffer);
const auto& layout_str = buffer.str();

rec_->set_time_seconds("timestamp", record.timestamp);
_rec->set_time_seconds("timestamp", record.timestamp);

if (record.recordType == vrs::Record::Type::CONFIGURATION) {
// NOTE this is meta data from the sensor that doesn't change over time and only comes
// in once in the beginning
rec_->log_timeless(entityPath_ + "/configuration", rerun::TextDocument(layout_str));
_rec->log_timeless(_entity_path + "/configuration", rerun::TextDocument(layout_str));
}

if (record.recordType == vrs::Record::Type::DATA) {
auto& config = getExpectedLayout<FrameNumberDataLayout>(layout, blockIndex);
auto& config = getExpectedLayout<FrameNumberDataLayout>(layout, block_index);
uint64_t frame_number;
if (config.frameNumber.get(frame_number)) {
rec_->set_time_sequence("frame_number", frame_number);
_rec->set_time_sequence("frame_number", frame_number);
}

// this is meta data per record and changes over time
rec_->log(entityPath_ + "/data", rerun::TextDocument(layout_str));
_rec->log(_entity_path + "/data", rerun::TextDocument(layout_str));
}

return true;
}

bool FramePlayer::onImageRead(
const vrs::CurrentRecord& record, size_t /*blockIndex*/,
const vrs::ContentBlock& contentBlock
const vrs::CurrentRecord& record, size_t /*block_index*/,
const vrs::ContentBlock& content_block
) {
std::shared_ptr<vrs::utils::PixelFrame> frame;
bool frameValid = vrs::utils::PixelFrame::readFrame(frame, record.reader, contentBlock);
bool frame_valid = vrs::utils::PixelFrame::readFrame(frame, record.reader, content_block);

if (frameValid) {
if (frame_valid) {
// Log image to Rerun
// NOTE Rerun assumes row major ordering for Images (i.e., TensorData) without any stride.
// Right now we don't check this properly, and just assume that there is no extra padding
// per pixel and / or per row.
rec_->log(
entityPath_,
_rec->log(
_entity_path,
rerun::Image(
{frame->getHeight(),
frame->getWidth(),
Expand All @@ -90,9 +90,9 @@ namespace rerun_vrs {
);
} else {
std::cout << "Failed reading image with format \""
<< contentBlock.image().getImageFormatAsString() << "\". Disabling player."
<< content_block.image().getImageFormatAsString() << "\". Disabling player."
<< std::endl;
enabled_ = false;
_enabled = false;
}
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/FramePlayer.h → src/frame_player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ namespace rerun_vrs {
public:
explicit FramePlayer(vrs::StreamId id, std::shared_ptr<const rerun::RecordingStream> rec);

bool onDataLayoutRead(const vrs::CurrentRecord& r, size_t blockIndex, vrs::DataLayout&)
bool onDataLayoutRead(const vrs::CurrentRecord& r, size_t block_index, vrs::DataLayout&)
override;
bool onImageRead(const vrs::CurrentRecord& r, size_t blockIndex, const vrs::ContentBlock&)
bool onImageRead(const vrs::CurrentRecord& r, size_t block_index, const vrs::ContentBlock&)
override;

private:
std::shared_ptr<const rerun::RecordingStream> rec_;
vrs::StreamId id_;
std::string entityPath_;
bool enabled_{true};
std::shared_ptr<const rerun::RecordingStream> _rec;
vrs::StreamId _id;
std::string _entity_path;
bool _enabled{true};
};

} // namespace rerun_vrs
62 changes: 31 additions & 31 deletions src/IMUPlayer.cpp → src/imu_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
#include <vrs/DataLayout.h>
#include <vrs/utils/PixelFrame.h>

#include "IMUPlayer.h"
#include "imu_player.hpp"

namespace rerun_vrs {

struct IMUConfigurationDataLayout : public vrs::AutoDataLayout {
vrs::DataPieceValue<vrs::Bool> hasAccelerometer_{"has_accelerometer"};
vrs::DataPieceValue<vrs::Bool> hasGyroscope_{"has_gyroscope"};
vrs::DataPieceValue<vrs::Bool> hasMagnetometer_{"has_magnetometer"};
vrs::DataPieceValue<vrs::Bool> _has_accelerometer{"has_accelerometer"};
vrs::DataPieceValue<vrs::Bool> _has_gyroscope{"has_gyroscope"};
vrs::DataPieceValue<vrs::Bool> _has_magnetometer{"has_magnetometer"};

vrs::AutoDataLayoutEnd endLayout;
};
Expand All @@ -44,79 +44,79 @@ namespace rerun_vrs {
};

IMUPlayer::IMUPlayer(vrs::StreamId id, std::shared_ptr<const rerun::RecordingStream> rec)
: id_{id}, rec_{rec}, entityPath_{rerun::new_entity_path({id.getName()})} {}
: _id{id}, _rec{rec}, _entity_path{rerun::new_entity_path({id.getName()})} {}

bool IMUPlayer::onDataLayoutRead(
const vrs::CurrentRecord& record, size_t blockIndex, vrs::DataLayout& layout
) {
if (!enabled_) {
if (!_enabled) {
return false;
}

std::ostringstream buffer;
layout.printLayoutCompact(buffer);
const auto& layout_str = buffer.str();

rec_->set_time_seconds("timestamp", record.timestamp);
_rec->set_time_seconds("timestamp", record.timestamp);

if (record.recordType == vrs::Record::Type::CONFIGURATION) {
// NOTE this is meta data from the sensor that doesn't change over time and only comes
// in once in the beginning
rec_->log_timeless(entityPath_ + "/configuration", rerun::TextDocument(layout_str));
_rec->log_timeless(_entity_path + "/configuration", rerun::TextDocument(layout_str));

// read properties required for logging
auto& config = getExpectedLayout<IMUConfigurationDataLayout>(layout, blockIndex);
hasAccelerometer_ = config.hasAccelerometer_.get();
hasGyroscope_ = config.hasGyroscope_.get();
hasMagnetometer_ = config.hasMagnetometer_.get();
_has_accelerometer = config._has_accelerometer.get();
_has_gyroscope = config._has_gyroscope.get();
_has_magnetometer = config._has_magnetometer.get();
}

if (record.recordType == vrs::Record::Type::DATA) {
auto& data = getExpectedLayout<IMUDataDataLayout>(layout, blockIndex);

if (hasAccelerometer_) {
if (_has_accelerometer) {
std::array<float, 3> accelMSec2;
if (data.accelMSec2.get(accelMSec2.data(), accelMSec2.size())) {
logAccelerometer(accelMSec2);
log_accelerometer(accelMSec2);
}
}
if (hasGyroscope_) {
if (_has_gyroscope) {
std::array<float, 3> gyroRadSec;
if (data.gyroRadSec.get(gyroRadSec.data(), gyroRadSec.size())) {
logGyroscope(gyroRadSec);
log_gyroscope(gyroRadSec);
}
}
if (hasMagnetometer_) {
if (_has_magnetometer) {
std::array<float, 3> magTesla;
if (data.magTesla.get(magTesla.data(), magTesla.size())) {
logMagnetometer(magTesla);
log_magnetometer(magTesla);
}
}
}

return false;
}

void IMUPlayer::logAccelerometer(const std::array<float, 3>& accelMSec2) {
rec_->log(entityPath_ + "/accelerometer", rerun::Arrows3D::from_vectors({accelMSec2}));
rec_->log(entityPath_ + "/accelerometer/x", rerun::TimeSeriesScalar(accelMSec2[0]));
rec_->log(entityPath_ + "/accelerometer/y", rerun::TimeSeriesScalar(accelMSec2[1]));
rec_->log(entityPath_ + "/accelerometer/z", rerun::TimeSeriesScalar(accelMSec2[2]));
void IMUPlayer::log_accelerometer(const std::array<float, 3>& accelMSec2) {
_rec->log(_entity_path + "/accelerometer", rerun::Arrows3D::from_vectors({accelMSec2}));
_rec->log(_entity_path + "/accelerometer/x", rerun::TimeSeriesScalar(accelMSec2[0]));
_rec->log(_entity_path + "/accelerometer/y", rerun::TimeSeriesScalar(accelMSec2[1]));
_rec->log(_entity_path + "/accelerometer/z", rerun::TimeSeriesScalar(accelMSec2[2]));
}

void IMUPlayer::logGyroscope(const std::array<float, 3>& gyroRadSec) {
rec_->log(entityPath_ + "/gyroscope/x", rerun::TimeSeriesScalar(gyroRadSec[0]));
rec_->log(entityPath_ + "/gyroscope/y", rerun::TimeSeriesScalar(gyroRadSec[1]));
rec_->log(entityPath_ + "/gyroscope/z", rerun::TimeSeriesScalar(gyroRadSec[2]));
void IMUPlayer::log_gyroscope(const std::array<float, 3>& gyroRadSec) {
_rec->log(_entity_path + "/gyroscope/x", rerun::TimeSeriesScalar(gyroRadSec[0]));
_rec->log(_entity_path + "/gyroscope/y", rerun::TimeSeriesScalar(gyroRadSec[1]));
_rec->log(_entity_path + "/gyroscope/z", rerun::TimeSeriesScalar(gyroRadSec[2]));
}

void IMUPlayer::logMagnetometer(const std::array<float, 3>& magTesla) {
rec_->log(entityPath_ + "/magnetometer/x", rerun::TimeSeriesScalar(magTesla[0]));
rec_->log(entityPath_ + "/magnetometer/y", rerun::TimeSeriesScalar(magTesla[1]));
rec_->log(entityPath_ + "/magnetometer/z", rerun::TimeSeriesScalar(magTesla[2]));
void IMUPlayer::log_magnetometer(const std::array<float, 3>& magTesla) {
_rec->log(_entity_path + "/magnetometer/x", rerun::TimeSeriesScalar(magTesla[0]));
_rec->log(_entity_path + "/magnetometer/y", rerun::TimeSeriesScalar(magTesla[1]));
_rec->log(_entity_path + "/magnetometer/z", rerun::TimeSeriesScalar(magTesla[2]));
}

bool mightContainIMUData(const vrs::StreamId& id) {
bool might_contain_imu_data(const vrs::StreamId& id) {
return id.getTypeId() == vrs::RecordableTypeId::SlamImuData ||
id.getTypeId() == vrs::RecordableTypeId::SlamMagnetometerData ||
id.getTypeId() == vrs::RecordableTypeId::ImuRecordableClass;
Expand Down
24 changes: 12 additions & 12 deletions src/IMUPlayer.h → src/imu_player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ namespace rerun_vrs {
override;

private:
void logAccelerometer(const std::array<float, 3>& accelMSec2);
void logGyroscope(const std::array<float, 3>& gyroRadSec);
void logMagnetometer(const std::array<float, 3>& magTesla);

std::shared_ptr<const rerun::RecordingStream> rec_;
vrs::StreamId id_;
std::string entityPath_;
bool enabled_{true};
bool hasAccelerometer_;
bool hasGyroscope_;
bool hasMagnetometer_;
void log_accelerometer(const std::array<float, 3>& accelMSec2);
void log_gyroscope(const std::array<float, 3>& gyroRadSec);
void log_magnetometer(const std::array<float, 3>& magTesla);

std::shared_ptr<const rerun::RecordingStream> _rec;
vrs::StreamId _id;
std::string _entity_path;
bool _enabled{true};
bool _has_accelerometer;
bool _has_gyroscope;
bool _has_magnetometer;
};

bool mightContainIMUData(const vrs::StreamId& id);
bool might_contain_imu_data(const vrs::StreamId& id);

} // namespace rerun_vrs
20 changes: 10 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <vrs/StreamPlayer.h>
#include <rerun.hpp>

#include "FramePlayer.h"
#include "IMUPlayer.h"
#include "frame_player.hpp"
#include "imu_player.hpp"

int main(int argc, const char* argv[]) {
std::cout << "Rerun SDK Version:" << rerun::version_string() << std::endl;
Expand All @@ -21,26 +21,26 @@ int main(int argc, const char* argv[]) {
return 0;
}

std::string vrsPath(argv[1]);
std::string vrs_path(argv[1]);

vrs::RecordFileReader reader;
if (reader.openFile(vrsPath) == 0) {
if (reader.openFile(vrs_path) == 0) {
// for each type of stream there is one player that logs the read data to
// Rerun
std::vector<std::unique_ptr<vrs::StreamPlayer>> streamPlayers;
std::vector<std::unique_ptr<vrs::StreamPlayer>> stream_players;

const std::set<vrs::StreamId>& streamIds = reader.getStreams();
for (auto id : streamIds) {
std::cout << id.getName() << " (" << id.getTypeName() << ")"
<< ": ";
if (reader.mightContainImages(id)) {
std::cout << "Handled by FramePlayer" << std::endl;
streamPlayers.emplace_back(std::make_unique<rerun_vrs::FramePlayer>(id, rec));
reader.setStreamPlayer(id, streamPlayers.back().get());
} else if (rerun_vrs::mightContainIMUData(id)) {
stream_players.emplace_back(std::make_unique<rerun_vrs::FramePlayer>(id, rec));
reader.setStreamPlayer(id, stream_players.back().get());
} else if (rerun_vrs::might_contain_imu_data(id)) {
std::cout << "Handled by IMUPlayer" << std::endl;
streamPlayers.emplace_back(std::make_unique<rerun_vrs::IMUPlayer>(id, rec));
reader.setStreamPlayer(id, streamPlayers.back().get());
stream_players.emplace_back(std::make_unique<rerun_vrs::IMUPlayer>(id, rec));
reader.setStreamPlayer(id, stream_players.back().get());
} else {
std::cout << "No player available. Skipped." << std::endl;
}
Expand Down

0 comments on commit bf0e064

Please sign in to comment.