I am new to C++ (in C# this is child's play) I would like to set the kernel used in a method at runtime depending on configuration. The method in question is
enum PoseSolverType
{
EIGHT_POINT_RELATIVE,
THREE_POINT_UPRIGHT_RELATIVE
};
std::pair<double, double> SphericalRelativePoseSolver::executeRobustEstimationAcransac(
const Mat& xSphericalStatic, const Mat& xSphericalDynamic, std::vector<uint32_t>& inliers, Mat3& E, double acRansacMaxThreshold)
{
// First define the AContrario angular error adaptor.
using KernelType = openMVG::robust::ACKernelAdaptor_AngularRadianError<EightPointRelativePoseSolver, AngularError, Mat3>;
//using KernelType = openMVG::robust::ACKernelAdaptor_AngularRadianError<kernel::ThreePointUprightRelativePoseSolver, AngularError, Mat3>;
KernelType kernel(xSphericalStatic, xSphericalDynamic);
// Robust estimation of the essential matrix and its precision.
const double precision = D2R(acRansacMaxThreshold);
return openMVG::robust::ACRANSAC(kernel, inliers, 1024, &E, precision, true);
}
I see we could do
std::pair<double, double> SphericalRelativePoseSolver::executeRobustEstimationAcransac(
const Mat& xSphericalStatic, const Mat& xSphericalDynamic, std::vector<uint32_t>& inliers, Mat3& E, double acRansacMaxThreshold, PoseSolverType solverType)
{
using KernelType = openMVG::robust::ACKernelAdaptor_AngularRadianError<
typename std::conditional<**<useSolverTypeEnumSwitchHere>**, openMVG::essential::kernel::ThreePointUprightRelativePoseSolver, openMVG::EightPointRelativePoseSolver>::type>,
openMVG::AngularError,
Mat3>;
KernelType kernel(xSphericalStatic, xSphericalDynamic);
// Robust estimation of the essential matrix and its precision.
const double precision = D2R(acRansacMaxThreshold);
return openMVG::robust::ACRANSAC(kernel, inliers, 1024, &E, precision, true);
}
but std::conditional
only works at compile-time. How can I use a switch in this code to change the solver at run-time?
I see this C++11 std::conditional at runtime? question, but the implementations do not seem to neatly trasfer to this case.
bool
flag), then call one or the other usingif
else
. If there are many types, there are some clever ways to avoid writing a longif
chain, but for two types I wouldn't bother.std::function
or even a lambda to select either you eight point solver or your 3 point solver. This is so simple that I am sure that I miss the point here.