Skip to content

Commit

Permalink
impl: StrFormat and StrSplit
Browse files Browse the repository at this point in the history
  • Loading branch information
feixh committed Sep 4, 2019
1 parent a003e53 commit 49c7af1
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 58 deletions.
11 changes: 11 additions & 0 deletions common/traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Type traits and TMP facilities.
// Author: Xiaohan Fei ([email protected])
#include <type_traits>

namespace xivo {
// type traits utilities
template <class T>
using plain = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

} // namespace xivo

16 changes: 14 additions & 2 deletions common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// stl
#include <iostream>
// I/O
#include "absl/strings/str_format.h"
#include "json/json.h"

#include "utils.h"
Expand Down Expand Up @@ -110,7 +109,7 @@ Json::Value LoadJson(const std::string &filename) {
return out;
} else {
throw std::runtime_error(
absl::StrFormat("failed to read file %s", filename));
StrFormat("failed to read file %s", filename));
}
}

Expand All @@ -130,4 +129,17 @@ void SaveJson(const Json::Value &value, const std::string &filename) {
out.close();
}

std::vector<std::string> StrSplit(const std::string &str, char delimiter) {
std::vector<std::string> splits;
size_t curr = 0;
size_t next = str.find(delimiter, curr);
while (next != std::string::npos) {
splits.push_back(str.substr(curr, next - curr));
curr = next + 1;
next = str.find(delimiter, curr);
}
splits.push_back(str.substr(curr));
return std::move(splits);
}

} // namespace xivo
34 changes: 34 additions & 0 deletions common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Author: Xiaohan Fei ([email protected])
#pragma once
#include "alias.h"
#include "traits.h"

// stl
#include <chrono>
Expand Down Expand Up @@ -274,4 +275,37 @@ void WriteMatrixToFile(const std::string &filename,
ostream.close();
}

namespace impl {

// implementation details
template <typename T>
std::enable_if_t<std::is_same<plain<T>, std::string>::value, const char*>
StrArgPass(T &arg) {
// std::cout << __PRETTY_FUNCTION__ << std::endl;
return arg.c_str();
}

template <typename T>
std::enable_if_t<!std::is_same<plain<T>, std::string>::value, T>
StrArgPass(T&& arg) {
// std::cout << __PRETTY_FUNCTION__ << std::endl;
return arg;
}

} // namespace impl


constexpr int kStrBufSize = 512;
template <typename... Args>
std::string StrFormat(const char *format, Args... args) {
char buf[kStrBufSize];
if (snprintf(&buf[0], kStrBufSize, format, impl::StrArgPass(args)...) < 0) {
LOG(FATAL) << "failed to format string!!!";
}
return buf;
}

std::vector<std::string> StrSplit(const std::string &str, char delimiter);


} // namespace xivo
7 changes: 3 additions & 4 deletions src/app/legacy/evaluate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <memory>
#include <tuple>

#include "absl/strings/str_format.h"
#include "gflags/gflags.h"
#include "glog/logging.h"

Expand Down Expand Up @@ -73,8 +72,8 @@ int main(int argc, char **argv) {
ComputeRPE(traj_est, traj_gt, FLAGS_RPE_interval, FLAGS_resolution);
LOG(INFO) << "RPE computed";

std::cout << absl::StreamFormat("ATE=%0.4f meters\n", ate);
std::cout << absl::StreamFormat(
std::cout << StreamFormat("ATE=%0.4f meters\n", ate);
std::cout << StreamFormat(
"RPE @ %0.4f ms=[%0.4f meters, %0.4f degrees]\n",
1000.0 * FLAGS_RPE_interval, rpe_pos, rpe_rot / M_PI * 180);

Expand All @@ -86,7 +85,7 @@ int main(int argc, char **argv) {
;
for (auto msg : traj_est) {
auto aligned_g = g_est_gt.inv() * msg.g_;
ostream << absl::StrFormat("%ld", msg.ts_) << " "
ostream << StrFormat("%ld", msg.ts_) << " "
<< aligned_g.T().transpose() << " "
<< aligned_g.R().log().transpose() << std::endl;
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/legacy/vio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "unistd.h"
#include <algorithm>

#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "opencv2/highgui/highgui.hpp"
Expand Down Expand Up @@ -86,7 +84,7 @@ int main(int argc, char **argv) {
////////////////////////////////////////
if (verbose) {
est_proc->Wait();
std::cout << absl::StrFormat("%ld", est_proc->ts().count()) << " "
std::cout << StrFormat("%ld", est_proc->ts().count()) << " "
<< est_proc->gsb().translation().transpose() << std::endl;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/vio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include "unistd.h"
#include <algorithm>

#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "opencv2/highgui/highgui.hpp"
Expand Down Expand Up @@ -90,7 +88,7 @@ int main(int argc, char **argv) {
}

traj_est.emplace_back(est->ts(), est->gsb());
ostream << absl::StrFormat("%ld", est->ts().count()) << " "
ostream << StrFormat("%ld", est->ts().count()) << " "
<< est->gsb().translation().transpose() << " "
<< est->gsb().rotation().log().transpose() << std::endl;

Expand Down
4 changes: 0 additions & 4 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,4 @@ struct Observation {

using Obs = Observation;

// type traits utilities
template <class T>
using plain = typename std::remove_cv<typename std::remove_reference<T>::type>::type;

} // namespace xivo
13 changes: 6 additions & 7 deletions src/estimator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <tuple>

#include "Eigen/QR"
#include "absl/strings/str_format.h"
#include "glog/logging.h"

#include "estimator.h"
Expand Down Expand Up @@ -615,7 +614,7 @@ bool Estimator::GoodTimestamp(const timestamp_t &now) {
auto curr_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(curr_time_);
if (now_ms < curr_ms) {
LOG(WARNING) << absl::StrFormat("now=%ld ms < curr=%ld ms", now_ms.count(),
LOG(WARNING) << StrFormat("now=%ld ms < curr=%ld ms", now_ms.count(),
curr_ms.count());
return false;
} else {
Expand Down Expand Up @@ -722,7 +721,7 @@ void Estimator::AddGroupToState(GroupPtr g) {
P_.block(0, offset + 3, err_.size(), 3) =
P_.block(0, Index::Tsb, err_.size(), 3);

VLOG(0) << absl::StrFormat("group #%d inserted @ %d/%d", g->id(), index,
VLOG(0) << StrFormat("group #%d inserted @ %d/%d", g->id(), index,
kMaxGroup);
} else {
throw std::runtime_error("Failed to find slot in state for group.");
Expand Down Expand Up @@ -761,15 +760,15 @@ void Estimator::AddFeatureToState(FeaturePtr f) {
P_.block<1, 2>(offset + 2, offset) *= damping;
P_(offset + 2, offset + 2) *= (damping * damping);

VLOG(0) << absl::StrFormat("feature #%d inserted @ %d/%d", f->id(), index,
VLOG(0) << StrFormat("feature #%d inserted @ %d/%d", f->id(), index,
kMaxFeature);
} else {
throw std::runtime_error("Failed to find slot in state for feature.");
}
}

void Estimator::PrintErrorStateNorm() {
VLOG(0) << absl::StrFormat(
VLOG(0) << StrFormat(
"|Wsb|=%0.8f, |Tsb|=%0.8f, |Vsb|=%0.8f, "
"|bg|=%0.8f, |ba|=%0.8f, |Wbc|=%0.8f, |Tbc|=%0.8f, |Wg|=%0.8f\n",
err_.segment<3>(Index::Wsb).norm(), err_.segment<3>(Index::Tsb).norm(),
Expand All @@ -780,7 +779,7 @@ void Estimator::PrintErrorStateNorm() {
#ifndef NDEBUG
CHECK(gsel_[g->sind()]) << "instate group not actually instate";
#endif
VLOG(0) << absl::StrFormat(
VLOG(0) << StrFormat(
"g#%d |W|=%0.8f, |T|=%0.8f\n", g->id(),
err_.segment<3>(kGroupBegin + 6 * g->sind()).norm(),
err_.segment<3>(kGroupBegin + 6 * g->sind() + 3).norm());
Expand All @@ -789,7 +788,7 @@ void Estimator::PrintErrorStateNorm() {
#ifndef NDEBUG
CHECK(fsel_[f->sind()]) << "instate feature not yet instate";
#endif
VLOG(0) << absl::StrFormat(
VLOG(0) << StrFormat(
"f#%d |X|=%0.8f\n", f->id(),
err_.segment<3>(kFeatureBegin + 3 * f->sind()).norm());
}
Expand Down
1 change: 0 additions & 1 deletion src/estimator_process.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 3rdparty
#include "opencv2/highgui/highgui.hpp"
// xivo
#include "absl/strings/str_format.h"
#include "estimator_process.h"
#include "tracker.h"
#include "visualize.h"
Expand Down
5 changes: 2 additions & 3 deletions src/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "mm.h"
#include "param.h"

#include "absl/strings/str_format.h"
#include "glog/logging.h"

namespace xivo {
Expand Down Expand Up @@ -262,7 +261,7 @@ bool Feature::RefineDepth(const SE3 &gbc,
break;
}

VLOG_IF(0, iter > 0) << absl::StrFormat("iter=%d; |res|:%0.4f->%0.4f", iter,
VLOG_IF(0, iter > 0) << StrFormat("iter=%d; |res|:%0.4f->%0.4f", iter,
res_norm0, res_norm);

// Vec3 delta = H.ldlt().solve(b);
Expand All @@ -278,7 +277,7 @@ bool Feature::RefineDepth(const SE3 &gbc,
}

if (res_norm0 > options.max_res_norm) {
VLOG(0) << absl::StrFormat("feature #%d; status=%d; |res|=%f\n", id_,
VLOG(0) << StrFormat("feature #%d; status=%d; |res|=%f\n", id_,
as_integer(status_), res_norm0);
return false;
}
Expand Down
1 change: 0 additions & 1 deletion src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <iostream>
#include <memory>

#include "absl/strings/str_format.h"
#include "glog/logging.h"

namespace xivo {
Expand Down
26 changes: 12 additions & 14 deletions src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <iostream>
#include <sstream>

#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "glog/logging.h"

#include "message_types.h"
Expand All @@ -23,7 +21,7 @@ DataLoader::DataLoader(const std::string &image_dir,
std::getline(is, line); // get rid of the header
while (is >> line) {
if (line.front() != '#') {
std::vector<std::string> content = absl::StrSplit(line, ',');
std::vector<std::string> content = StrSplit(line, ',');
auto ts{timestamp_t(std::stoll(content[0]))};
std::string image_path = image_dir + "/data/" + content[1];
entries_.emplace_back(std::make_unique<msg::Image>(ts, image_path));
Expand All @@ -40,7 +38,7 @@ DataLoader::DataLoader(const std::string &image_dir,
std::getline(is, line); // get rid of the header
while (is >> line) {
if (line.front() != '#') {
std::vector<std::string> content = absl::StrSplit(line, ',');
std::vector<std::string> content = StrSplit(line, ',');
auto ts{timestamp_t(std::stoll(content[0]))};
Vec3 gyro;
Vec3 accel;
Expand Down Expand Up @@ -68,7 +66,7 @@ DataLoader::LoadGroundTruthState(const std::string &state_dir) {
std::getline(is, line); // get rid of the header
while (is >> line) {
if (line.front() != '#') {
std::vector<std::string> content = absl::StrSplit(line, ',');
std::vector<std::string> content = StrSplit(line, ',');
auto ts{timestamp_t(std::stoull(content[0]))};
// pose frame -> world frame
Vec3 T;
Expand All @@ -85,7 +83,7 @@ DataLoader::LoadGroundTruthState(const std::string &state_dir) {
poses_.emplace_back(ts, gsb);
}
}
LOG(INFO) << absl::StreamFormat("%d ground truth poses in total loaded",
LOG(INFO) << StrFormat("%d ground truth poses in total loaded",
poses_.size());
} else {
LOG(FATAL) << "failed to load ground-truth state csv @ " << state_data;
Expand All @@ -102,23 +100,23 @@ GetDirs(const std::string dataset, const std::string root,

if (dataset_type == "tumvi") {
std::string image_dir =
absl::StrFormat("%s/dataset-%s_512_16/mav0/cam%d/", root, seq, cam_id);
StrFormat("%s/dataset-%s_512_16/mav0/cam%d/", root, seq, cam_id);
std::string imu_dir =
absl::StrFormat("%s/dataset-%s_512_16/mav0/imu0/", root, seq);
StrFormat("%s/dataset-%s_512_16/mav0/imu0/", root, seq);
std::string mocap_dir =
absl::StrFormat("%s/dataset-%s_512_16/mav0/mocap0/", root, seq);
StrFormat("%s/dataset-%s_512_16/mav0/mocap0/", root, seq);
return std::make_tuple(image_dir, imu_dir, mocap_dir);
} else if (dataset_type == "euroc") {
std::string image_dir =
absl::StrFormat("%s/%s/mav0/cam%d/", root, seq, cam_id);
std::string imu_dir = absl::StrFormat("%s/%s/mav0/imu0/", root, seq);
StrFormat("%s/%s/mav0/cam%d/", root, seq, cam_id);
std::string imu_dir = StrFormat("%s/%s/mav0/imu0/", root, seq);
std::string mocap_dir =
absl::StrFormat("%s/%s/mav0/state_groundtruth_estimate0/", root, seq);
StrFormat("%s/%s/mav0/state_groundtruth_estimate0/", root, seq);
return std::make_tuple(image_dir, imu_dir, mocap_dir);
} else if (dataset_type == "xivo") {
std::string image_dir =
absl::StrFormat("%s/%s/cam0/", root, seq);
std::string imu_dir = absl::StrFormat("%s/%s/imu0/", root, seq);
StrFormat("%s/%s/cam0/", root, seq);
std::string imu_dir = StrFormat("%s/%s/imu0/", root, seq);
std::string mocap_dir = "";
return std::make_tuple(image_dir, imu_dir, mocap_dir);
} else {
Expand Down
1 change: 0 additions & 1 deletion src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <iostream>
#include <unordered_set>

#include "absl/strings/str_format.h"
#include "glog/logging.h"

#include "estimator.h"
Expand Down
3 changes: 1 addition & 2 deletions src/mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "feature.h"
#include "group.h"

#include "absl/strings/str_format.h"
#include "glog/logging.h"

namespace xivo {
Expand All @@ -14,7 +13,7 @@ MemoryManagerPtr MemoryManager::Create(int max_features, int max_groups) {
if (!instance_) {
instance_ = std::unique_ptr<MemoryManager>(
new MemoryManager(max_features, max_groups));
LOG(INFO) << absl::StrFormat(
LOG(INFO) << StrFormat(
"MemoryManager instance created with %d features and %d groups",
max_features, max_groups);
} else {
Expand Down
2 changes: 0 additions & 2 deletions src/tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// Author: Xiaohan Fei ([email protected])
#include <fstream>

#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "glog/logging.h"
#include "opencv2/video/video.hpp"
#include "opencv2/xfeatures2d.hpp"
Expand Down
3 changes: 1 addition & 2 deletions src/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <iostream>
#include <unordered_set>

#include "absl/strings/str_format.h"
#include "glog/logging.h"

#ifdef USE_GPERFTOOLS
Expand Down Expand Up @@ -252,7 +251,7 @@ Estimator::OnePointRANSAC(const std::vector<FeaturePtr> &mh_inliers) {
g->RestoreState();
}
}
auto str = absl::StrFormat("#hyp tested=%d: li_inliers/mh_inliers=%d/%d",
auto str = StrFormat("#hyp tested=%d: li_inliers/mh_inliers=%d/%d",
n_hyp, max_inliers.size(), mh_inliers.size());

LOG(INFO) << str;
Expand Down
Loading

0 comments on commit 49c7af1

Please sign in to comment.