Make examples independent of ceres internals.
Change-Id: I6b6913e067a86fea713646218c8da1439d349d74
diff --git a/examples/bal_problem.cc b/examples/bal_problem.cc
index 5733f46..c118f88 100644
--- a/examples/bal_problem.cc
+++ b/examples/bal_problem.cc
@@ -35,7 +35,6 @@
#include <string>
#include <vector>
#include "Eigen/Core"
-#include "ceres/random.h"
#include "ceres/rotation.h"
#include "glog/logging.h"
@@ -45,6 +44,25 @@
typedef Eigen::Map<Eigen::VectorXd> VectorRef;
typedef Eigen::Map<const Eigen::VectorXd> ConstVectorRef;
+inline double RandDouble() {
+ double r = static_cast<double>(rand());
+ return r / RAND_MAX;
+}
+
+// Box-Muller algorithm for normal random number generation.
+// http://en.wikipedia.org/wiki/Box-Muller_transform
+inline double RandNormal() {
+ double x1, x2, w;
+ do {
+ x1 = 2.0 * RandDouble() - 1.0;
+ x2 = 2.0 * RandDouble() - 1.0;
+ w = x1 * x1 + x2 * x2;
+ } while ( w >= 1.0 || w == 0.0 );
+
+ w = sqrt((-2.0 * log(w)) / w);
+ return x1 * w;
+}
+
template<typename T>
void FscanfOrDie(FILE* fptr, const char* format, T* value) {
int num_scanned = fscanf(fptr, format, value);
diff --git a/examples/bundle_adjuster.cc b/examples/bundle_adjuster.cc
index 78dbd01..8fff9f0 100644
--- a/examples/bundle_adjuster.cc
+++ b/examples/bundle_adjuster.cc
@@ -60,7 +60,6 @@
#include "bal_problem.h"
#include "ceres/ceres.h"
-#include "ceres/random.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "snavely_reprojection_error.h"
@@ -305,7 +304,7 @@
BALProblem bal_problem(filename, FLAGS_use_quaternions);
Problem problem;
- SetRandomState(FLAGS_random_seed);
+ srand(FLAGS_random_seed);
bal_problem.Normalize();
bal_problem.Perturb(FLAGS_rotation_sigma,
FLAGS_translation_sigma,
diff --git a/examples/nist.cc b/examples/nist.cc
index 7504e51..ec8c56d 100644
--- a/examples/nist.cc
+++ b/examples/nist.cc
@@ -74,7 +74,6 @@
#include <iostream>
#include <fstream>
#include "ceres/ceres.h"
-#include "ceres/split.h"
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "Eigen/Core"
@@ -95,16 +94,39 @@
" nonmonotic steps");
DEFINE_double(initial_trust_region_radius, 1e4, "Initial trust region radius");
+namespace ceres {
+namespace examples {
+
using Eigen::Dynamic;
using Eigen::RowMajor;
typedef Eigen::Matrix<double, Dynamic, 1> Vector;
typedef Eigen::Matrix<double, Dynamic, Dynamic, RowMajor> Matrix;
+void SplitStringUsingChar(const string& full,
+ const char delim,
+ vector<string>* result) {
+ back_insert_iterator< vector<string> > it(*result);
+
+ const char* p = full.data();
+ const char* end = p + full.size();
+ while (p != end) {
+ if (*p == delim) {
+ ++p;
+ } else {
+ const char* start = p;
+ while (++p != end && *p != delim) {
+ // Skip to the next occurence of the delimiter.
+ }
+ *it++ = string(start, p - start);
+ }
+ }
+}
+
bool GetAndSplitLine(std::ifstream& ifs, std::vector<std::string>* pieces) {
pieces->clear();
char buf[256];
ifs.getline(buf, 256);
- ceres::SplitStringUsing(std::string(buf), " ", pieces);
+ SplitStringUsingChar(std::string(buf), ' ', pieces);
return true;
}
@@ -504,9 +526,12 @@
std::cout << "Total : " << easy_success + medium_success + hard_success << "/54\n";
}
+} // namespace examples
+} // namespace ceres
+
int main(int argc, char** argv) {
google::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
- SolveNISTProblems();
+ ceres::examples::SolveNISTProblems();
return 0;
};