Randomly perturb the bundle adjustment problem. 1. Add the ability to perturb the camera pose and the point positions using user specified parameters. 2. Re-order the flags. 3. Minor name correction. 4. Added Box-Mueller generator to random.h Change-Id: I2c9ce74c237f5bde9a7299cc71b205d1ca9bc742
diff --git a/internal/ceres/random.h b/internal/ceres/random.h index 769e0b4..cd7a0ea 100644 --- a/internal/ceres/random.h +++ b/internal/ceres/random.h
@@ -27,19 +27,41 @@ // POSSIBILITY OF SUCH DAMAGE. // // Author: keir@google.com (Keir Mierle) +// sameeragarwal@google.com (Sameer Agarwal) #ifndef CERES_INTERNAL_RANDOM_H_ #define CERES_INTERNAL_RANDOM_H_ +#include <cmath> +#include <cstdlib> + namespace ceres { -inline double RandDouble() { - double r = rand(); - return r / RAND_MAX; +inline void SetRandomState(int state) { + srandom(state); } inline int Uniform(int n) { - return rand() % n; + return random() % n; +} + +inline double RandDouble() { + double r = static_cast<double>(random()); + 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; } } // namespace ceres