Fix a bug in TrustRegionMinimizer.

LocalParameterization::Plus is allowed to return false when for some
reason the operation cannot be performed. Uptil now, this would
cause the TrustRegionMinimizer to terminate with a numerical failure.

This is not correct behaviour. Just like CostFunction::Evaluate,
returning false from LocalParameterization::Plus should result
in the current step to be rejected rather than the entire optimization
coming to a halt.

LineSearchMinimizer has also been updated to use the same pattern.

Thanks to Michael Vitus for reporting this.

Change-Id: I1351966bc6db3bf6cd46387b78d4e52a9df51696
diff --git a/internal/ceres/line_search_minimizer.cc b/internal/ceres/line_search_minimizer.cc
index 2cc89fa..5533e20 100644
--- a/internal/ceres/line_search_minimizer.cc
+++ b/internal/ceres/line_search_minimizer.cc
@@ -69,6 +69,9 @@
 // use.
 const double kEpsilon = 1e-12;
 
+// TODO(sameeragarwal): I think there is a small bug here, in that if
+// the evaluation fails, then the state can contain garbage. Look at
+// this more carefully.
 bool Evaluate(Evaluator* evaluator,
               const Vector& x,
               LineSearchMinimizer::State* state) {
@@ -310,9 +313,10 @@
         WallTimeInSeconds() - iteration_start_time;
 
     // TODO(sameeragarwal): Collect stats.
-    if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data()) ||
-        !Evaluate(evaluator, x_plus_delta, &current_state)) {
-      LOG(WARNING) << "Evaluation failed.";
+    if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data())) {
+      LOG(WARNING) << "x_plus_delta = Plus(x, delta) failed. ";
+    } else if (!Evaluate(evaluator, x_plus_delta, &current_state)) {
+      LOG(WARNING) << "Step failed to evaluate. ";
     } else {
       x = x_plus_delta;
     }