Skip to content

Commit

Permalink
Save/Load config (#98)
Browse files Browse the repository at this point in the history
* dump config

* load config from dump directory
  • Loading branch information
koide3 authored Oct 29, 2024
1 parent 58188aa commit d686103
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
29 changes: 12 additions & 17 deletions include/glim/util/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,30 @@ class Config {
*/
void save(const std::string& path) const;

private:
protected:
std::any config;
};


/**
* @brief Global configuration class to bootstrap the root path of the configuration files
* @brief Global configuration class to bootstrap the root path of the configuration files
*/
class GlobalConfig : public Config {
private:
GlobalConfig(const std::string& global_config_path) : Config(global_config_path) {}
virtual ~GlobalConfig() override {}

public:
static GlobalConfig* instance(const std::string& config_path = std::string()) {
if (inst == nullptr) {
inst = new GlobalConfig(config_path + "/config.json");
inst->override_param("global", "config_path", config_path);
}
return inst;
}

static std::string get_config_path(const std::string& config_name) {
auto config = instance();
const std::string directory = config->param<std::string>("global", "config_path", ".");
const std::string filename = config->param<std::string>("global", config_name, config_name + ".json");
return directory + "/" + filename;
}
static GlobalConfig* instance(const std::string& config_path = std::string(), bool override_path = false);

static std::string get_config_path(const std::string& config_name);

/**
* @brief Dump all involved config parameters
* @param path Destination path
*/
void dump(const std::string& path);

private:
static GlobalConfig* inst;
};

Expand Down
3 changes: 3 additions & 0 deletions src/glim/mapping/global_mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,9 @@ void GlobalMapping::save(const std::string& path) {

submaps[i]->save((boost::format("%s/%06d") % path % i).str());
}

logger->info("saving config");
GlobalConfig::instance()->dump(path + "/config");
}

std::vector<Eigen::Vector4d> GlobalMapping::export_points() {
Expand Down
41 changes: 41 additions & 0 deletions src/glim/util/config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <glim/util/config.hpp>

#include <boost/filesystem.hpp>
#include <glim/util/config_impl.hpp>

namespace glim {
Expand Down Expand Up @@ -50,4 +51,44 @@ DEFINE_CONFIG_IO_SPECIALIZATION(Eigen::Isometry3d)

DEFINE_CONFIG_IO_SPECIALIZATION(std::vector<Eigen::Isometry3d>)

GlobalConfig* GlobalConfig::instance(const std::string& config_path, bool override_path) {
if (inst == nullptr || override_path) {
if (inst) {
delete inst;
}

inst = new GlobalConfig(config_path + "/config.json");
inst->override_param("global", "config_path", config_path);
}
return inst;
}

std::string GlobalConfig::get_config_path(const std::string& config_name) {
auto config = instance();
const std::string directory = config->param<std::string>("global", "config_path", ".");
const std::string filename = config->param<std::string>("global", config_name, config_name + ".json");
return directory + "/" + filename;
}

void GlobalConfig::dump(const std::string& path) {
spdlog::debug("dumping config to {} (config_path={})", path, param<std::string>("global", "config_path", "."));
boost::filesystem::create_directories(path);
this->save(path + "/config.json");

const auto& json = std::any_cast<const nlohmann::json&>(config);
for (const auto& param : json["global"].items()) {
const std::string config_name = param.key();
const std::string config_file = param.value();
if (config_name == "config_path" || config_name == "config_ext") {
continue;
}

spdlog::debug("dumping {} : {}", config_name, config_file);
const Config conf(get_config_path(config_name));
conf.save(path + "/" + config_file);
}

spdlog::debug("dumping global config done");
}

} // namespace glim
12 changes: 11 additions & 1 deletion src/glim/viewer/offline_viewer.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <glim/viewer/offline_viewer.hpp>

#include <boost/filesystem.hpp>
#include <gtsam_points/config.hpp>
#include <gtsam_points/optimizers/linearization_hook.hpp>
#include <gtsam_points/cuda/nonlinear_factor_set_gpu_create.hpp>
#include <glim/util/config.hpp>

#include <spdlog/spdlog.h>
#include <portable-file-dialogs.h>
Expand Down Expand Up @@ -84,6 +86,14 @@ void OfflineViewer::main_menu() {

if (!map_path.empty()) {
recent_files.push(map_path);

if (boost::filesystem::exists(map_path + "/config")) {
logger->info("Use config from {}", map_path + "/config");
GlobalConfig::instance(map_path + "/config", true);
} else {
logger->warn("No config found in {}", map_path);
}

progress_modal->open<std::shared_ptr<GlobalMapping>>("open", [this, map_path](guik::ProgressInterface& progress) { return load_map(progress, map_path); });
}
}
Expand Down Expand Up @@ -143,7 +153,7 @@ std::shared_ptr<GlobalMapping> OfflineViewer::load_map(guik::ProgressInterface&
const auto result = pfd::message("Confirm", "Do optimization?", pfd::choice::yes_no).result();
params.enable_optimization = (result == pfd::button::ok) || (result == pfd::button::yes);

std::cout << "enable_optimization:" << params.enable_optimization << std::endl;
logger->info("enable_optimization={}", params.enable_optimization);
std::shared_ptr<glim::GlobalMapping> global_mapping(new glim::GlobalMapping(params));
if (!global_mapping->load(path)) {
logger->error("failed to load {}", path);
Expand Down

0 comments on commit d686103

Please sign in to comment.