From 6afcada3aaa802b06279c54f0306db21e19926cb Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@orfeo-toolbox.org>
Date: Thu, 6 Dec 2012 17:14:40 +0100
Subject: [PATCH] ENH: Relaxing the constraints on the number of required
 points by explicitly disabling elevation if not enough points are provided

---
 .../OssimAdapters/otbRPCSolverAdapter.cxx     | 19 ++++++++++++++++---
 .../OssimAdapters/otbRPCSolverAdapter.h       |  7 ++++++-
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.cxx b/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.cxx
index 0789f4b92f..5c14b6e609 100644
--- a/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.cxx
+++ b/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.cxx
@@ -18,6 +18,7 @@
 
 #include "otbRPCSolverAdapter.h"
 #include "otbImageKeywordlist.h"
+#include "otbMacro.h"
 
 #include "ossim/projection/ossimRpcSolver.h"
 #include "ossim/projection/ossimProjection.h"
@@ -68,13 +69,25 @@ RPCSolverAdapter::Solve(const GCPsContainerType& gcpContainer,
     }
 
   // Check for enough points
-  if(sensorPoints.size() < 40)
+  if(sensorPoints.size() < 20)
     {
-    itkGenericExceptionMacro(<<"At least 40 points are required to estimate the 80 parameters of the RPC model, but only "<<sensorPoints.size()<<" were given.");
+    itkGenericExceptionMacro(<<"At least 20 points are required to estimate the 40 parameters of a RPC model without elevation support, and 40 are required to estimate the 80 parameters of a RPC model with elevation support. Only "<<sensorPoints.size()<<" points were given.");
+    }
+
+  // By default, use elevation provided with ground control points
+  bool useElevation = true;
+
+  // If not enough points are given for a proper estimation of RPC
+  // with elevation support, disable elevation. This will result in
+  // all coefficients related to elevation set to zero.
+  if(sensorPoints.size()<40)
+    {
+    otbGenericWarningMacro("Only "<<sensorPoints.size()<<" ground control points are provided, can not estimate a RPC model with elevation support (at least 40 points required). Elevation support will be disabled for RPC estimation. All coefficients related to elevation will be set to zero, and elevation will have no effect on the resulting transform.");
+    useElevation = false; 
     }
   
   // Build the ossim rpc solver
-  ossimRefPtr<ossimRpcSolver> rpcSolver = new ossimRpcSolver(true, false);
+  ossimRefPtr<ossimRpcSolver> rpcSolver = new ossimRpcSolver(useElevation, false);
 
   // Call the solve method
   rpcSolver->solveCoefficients(sensorPoints, geoPoints);
diff --git a/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.h b/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.h
index 3a67d87964..ac4b911358 100644
--- a/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.h
+++ b/Code/UtilitiesAdapters/OssimAdapters/otbRPCSolverAdapter.h
@@ -54,13 +54,18 @@ public:
   /** Solve RPC modelling from a set of GCPs and image bounds.
    *  The estimated RPC model is written in a keywordlist understood
    *  by other OTB and classes (like GenericRSTransform for instance).
+   *  Please note that at least 20 points are required to estimate the
+   *  RPC model. Between 20 and 40 points, the estimated model will
+   *  not provide elevation support, since there are not enough points
+   *  to estimate all the coefficients. Starting at 40 points, a full
+   *  RPC model is estimated.
    */
   static void Solve(const GCPsContainerType& gcpContainer,
                     double& rmsError,
                     ImageKeywordlist& otb_kwl);
 
   /** Solve RPC modelling from a set of GCPs and image bounds. The
-   * estimated RPC model is written to a geom file
+   * estimated RPC model is written to a geom file.
    */
   static bool Solve(const GCPsContainerType& gcpContainer,
                     double& rmsError,
-- 
GitLab