|
| 1 | +#include <cassert> |
| 2 | +#include <chrono> |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <memory> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +#include <spectacularAI/vio.hpp> |
| 9 | +#include <spectacularAI/replay.hpp> |
| 10 | + |
| 11 | +const std::string SEPARATOR = "/"; |
| 12 | + |
| 13 | +std::string readFileToString(const std::string &filename) { |
| 14 | + std::ifstream f(filename); |
| 15 | + std::ostringstream oss; |
| 16 | + oss << f.rdbuf(); |
| 17 | + return oss.str(); |
| 18 | +} |
| 19 | + |
| 20 | +bool fileExists(const std::string &filePath) { |
| 21 | + std::ifstream dataFile(filePath); |
| 22 | + return dataFile.is_open(); |
| 23 | +} |
| 24 | + |
| 25 | +int main(int argc, char *argv[]) { |
| 26 | + std::vector<std::string> arguments(argv, argv + argc); |
| 27 | + std::unique_ptr<std::ofstream> outputFile; |
| 28 | + std::string inputFolder; |
| 29 | + std::string userConfigurationYaml; |
| 30 | + bool realtime = false; |
| 31 | + bool print = false; |
| 32 | + for (size_t i = 1; i < arguments.size(); ++i) { |
| 33 | + const std::string &argument = arguments.at(i); |
| 34 | + if (argument == "-o") { |
| 35 | + outputFile = std::make_unique<std::ofstream>(arguments.at(++i)); |
| 36 | + assert(outputFile->is_open()); |
| 37 | + } |
| 38 | + else if (argument == "-i") inputFolder = arguments.at(++i); |
| 39 | + else if (argument == "-c") userConfigurationYaml = readFileToString(arguments.at(++i)); |
| 40 | + else if (argument == "--realtime") realtime = true; |
| 41 | + else if (argument == "--print") print = true; |
| 42 | + } |
| 43 | + if (inputFolder.empty()) { |
| 44 | + std::cout << "Please specify input directory using `-i`."<< std::endl; |
| 45 | + return 0; |
| 46 | + } |
| 47 | + |
| 48 | + std::ostringstream configurationYaml; |
| 49 | + std::string dataConfigurationYamlPath = inputFolder + SEPARATOR + "vio_config.yaml"; |
| 50 | + if (fileExists(dataConfigurationYamlPath)) { |
| 51 | + // Vio::Builder::setConfigurationYAML overwrites the configuration in the data |
| 52 | + // directory, but in most cases that configuration is needed for the best VIO |
| 53 | + // performance, so we concatenate our changes to it. |
| 54 | + configurationYaml << readFileToString(dataConfigurationYamlPath) << std::endl; |
| 55 | + } |
| 56 | + // The `processingQueueSize` option makes the data input wait for VIO to finish to |
| 57 | + // avoid dropping frames. It should not be used in real-time scenarios. |
| 58 | + configurationYaml << "processingQueueSize: 0\n"; |
| 59 | + configurationYaml << userConfigurationYaml; |
| 60 | + |
| 61 | + // The Replay API builder takes as input the main API builder as a way to share |
| 62 | + // configuration methods. |
| 63 | + spectacularAI::Vio::Builder vioBuilder = spectacularAI::Vio::builder() |
| 64 | + .setConfigurationYAML(configurationYaml.str()); |
| 65 | + |
| 66 | + std::unique_ptr<spectacularAI::Replay> replay |
| 67 | + = spectacularAI::Replay::builder(inputFolder, vioBuilder).build(); |
| 68 | + replay->setPlaybackSpeed(realtime ? 1.0 : -1.0); |
| 69 | + |
| 70 | + replay->setOutputCallback([&](spectacularAI::VioOutputPtr vioOutput) { |
| 71 | + if (outputFile) *outputFile << vioOutput->asJson().c_str() << std::endl; |
| 72 | + if (print) std::cout << vioOutput->asJson().c_str() << std::endl; |
| 73 | + }); |
| 74 | + |
| 75 | + const auto t0 = std::chrono::steady_clock::now(); |
| 76 | + |
| 77 | + replay->runReplay(); |
| 78 | + |
| 79 | + const auto time = std::chrono::duration<double, std::milli>( |
| 80 | + std::chrono::steady_clock::now() - t0).count(); |
| 81 | + printf("Replay took %.2fs\n", 1e-3 * time); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments