8 #ifndef META_OCEAN_GEOMETRY_NON_LINEAR_UNIVERSAL_OPTIMIZATION_DENSE_H
9 #define META_OCEAN_GEOMETRY_NON_LINEAR_UNIVERSAL_OPTIMIZATION_DENSE_H
30 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize = tModelSize>
122 template <Estimator::EstimatorType tEstimator>
174 static bool optimizeUniversalModel(
const Model& model,
const size_t numberElements,
const ValueCallback& valueCallback,
const ErrorCallback& errorCallback,
const ModelTransformationCallback& modelTransformationCallback,
const ModelAdjustmentCallback& modelAdjustmentCallback,
Model& optimizedModel,
const unsigned int iterations = 5u,
const Estimator::EstimatorType estimator =
Estimator::ET_SQUARE,
Scalar lambda =
Scalar(0.001),
const Scalar lambdaFactor =
Scalar(5),
Scalar* initialError =
nullptr,
Scalar* finalError =
nullptr,
Scalars* intermediateErrors =
nullptr);
177 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
180 numberElements_(numberElements),
181 valueCallback_(valueCallback),
182 errorCallback_(errorCallback),
183 modelTransformationCallback_(modelTransformationCallback),
184 modelAdjustmentCallback_(modelAdjustmentCallback)
193 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
196 ocean_assert(valueCallback_);
197 ocean_assert(modelTransformationCallback_);
199 jacobian.
resize(tResultDimension * numberElements_, tModelSize);
206 modelTransformationCallback_(model_, externalModel);
210 for (
size_t a = 0; a < tModelSize; ++a)
212 Model internalModel = model_;
213 internalModel[a] += eps;
215 modelTransformationCallback_(internalModel, externalEpsModels[a]);
219 for (
size_t n = 0; n < numberElements_; ++n)
222 valueCallback_(externalModel, n, result);
224 for (
size_t m = 0; m < tModelSize; ++m)
227 valueCallback_(externalEpsModels[m], n, epsResult);
230 for (
size_t d = 0; d < tResultDimension; ++d)
232 jacobian[n * tResultDimension + d][m] = (epsResult[d] - result[d]) * invEps;
238 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
241 ocean_assert(deltas.
rows() == tModelSize);
243 for (
size_t n = 0; n < tModelSize; ++n)
245 const Scalar& delta = deltas(n);
246 candidateModel_[n] = model_[n] - delta;
249 if (modelAdjustmentCallback_)
251 modelAdjustmentCallback_(candidateModel_);
255 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
256 template <Estimator::EstimatorType tEstimator>
259 ocean_assert(errorCallback_);
260 ocean_assert(modelTransformationCallback_);
262 OCEAN_SUPPRESS_UNUSED_WARNING(invertedCovariances);
263 ocean_assert(invertedCovariances ==
nullptr);
266 weightedErrorVector.
resize(tResultDimension * numberElements_, 1u);
270 modelTransformationCallback_(candidateModel_, externalModel);
275 if constexpr (!Estimator::isStandardEstimator<tEstimator>())
277 sqrErrors.reserve(numberElements_);
280 for (
size_t n = 0; n < numberElements_; ++n)
283 if (!errorCallback_(externalModel, n, weightedErrorPointer))
288 if constexpr (Estimator::isStandardEstimator<tEstimator>())
294 ocean_assert(!Estimator::isStandardEstimator<tEstimator>());
300 if constexpr (Estimator::isStandardEstimator<tEstimator>())
303 ocean_assert(!weightVector);
305 ocean_assert(numberElements_ > 0);
306 return sqrError /=
Scalar(numberElements_);
311 weightVector.
resize(tResultDimension * numberElements_, 1u);
313 ocean_assert(sqrErrors.size() == numberElements_);
319 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
322 model_ = candidateModel_;
325 template <
unsigned int tModelSize,
unsigned int tResultDimension,
unsigned int tExternalModelSize>
326 bool NonLinearUniversalOptimizationDense<tModelSize, tResultDimension, tExternalModelSize>::optimizeUniversalModel(
const Model& model,
const size_t numberElements,
const ValueCallback& valueCallback,
const ErrorCallback& errorCallback,
const ModelTransformationCallback& modelTransformationCallback,
const ModelAdjustmentCallback& modelAdjustmentCallback,
Model& optimizedModel,
const unsigned int iterations,
const Estimator::EstimatorType estimator,
Scalar lambda,
const Scalar lambdaFactor,
Scalar* initialError,
Scalar* finalError,
Scalars* intermediateErrors)
328 ocean_assert(&model != &optimizedModel);
329 optimizedModel = model;
331 UniversalOptimizationProvider provider(optimizedModel, numberElements, valueCallback, errorCallback, modelTransformationCallback, modelAdjustmentCallback);
332 return NonLinearOptimization::denseOptimization<UniversalOptimizationProvider>(provider, iterations, estimator, lambda, lambdaFactor, initialError, finalError,
nullptr, intermediateErrors);
This class implements a container for callback functions.
Definition: Callback.h:3456
EstimatorType
Definition of individual robust estimator types.
Definition: Estimator.h:34
@ ET_SQUARE
The standard square error estimator (L2).
Definition: Estimator.h:52
This class implements the base optimization provider.
Definition: NonLinearOptimization.h:293
This class implements the basic functions for least square or robust optimization algorithms for non ...
Definition: NonLinearOptimization.h:34
This class implements a dense universal optimization provider for universal models and measurement/da...
Definition: NonLinearUniversalOptimizationDense.h:88
const ModelAdjustmentCallback modelAdjustmentCallback_
The optional callback function allowing to adjust a model before it is accepted or rejected.
Definition: NonLinearUniversalOptimizationDense.h:151
void determineJacobian(Matrix &jacobian) const
Determines the jacobian matrix for the current camera.
Definition: NonLinearUniversalOptimizationDense.h:194
Model & model_
Universal model that will be optimized.
Definition: NonLinearUniversalOptimizationDense.h:133
const ErrorCallback errorCallback_
The error calculation callback function.
Definition: NonLinearUniversalOptimizationDense.h:145
void acceptCorrection()
Accepts the current pose candidate as better model.
Definition: NonLinearUniversalOptimizationDense.h:320
Scalar determineRobustError(Matrix &weightedErrorVector, Matrix &weightVector, const Matrix *invertedCovariances) const
Determines the robust error of the current candidate pose.
Definition: NonLinearUniversalOptimizationDense.h:257
void applyCorrection(const Matrix &deltas)
Applies the model correction and stores the new model as candidate.
Definition: NonLinearUniversalOptimizationDense.h:239
Model candidateModel_
Universal model that stores the most recent optimization result as candidate.
Definition: NonLinearUniversalOptimizationDense.h:136
const size_t numberElements_
The number of measurement elements that are used to optimize the model.
Definition: NonLinearUniversalOptimizationDense.h:139
const ModelTransformationCallback modelTransformationCallback_
The Callback function allowing to transform the model into an external model before the value and err...
Definition: NonLinearUniversalOptimizationDense.h:148
UniversalOptimizationProvider(Model &model, const size_t numberElements, const ValueCallback &valueCallback, const ErrorCallback &errorCallback, const ModelTransformationCallback &modelTransformationCallback, const ModelAdjustmentCallback &modelAdjustmentCallback)
Creates a new universal optimization object.
Definition: NonLinearUniversalOptimizationDense.h:178
const ValueCallback valueCallback_
The value calculation callback function.
Definition: NonLinearUniversalOptimizationDense.h:142
This class implements an optimization for universal dense problems with one model (optimization probl...
Definition: NonLinearUniversalOptimizationDense.h:32
Callback< bool, const ExternalModel &, const size_t, Result & > ErrorCallback
Definition of a callback function for dense error calculation.
Definition: NonLinearUniversalOptimizationDense.h:65
static bool optimizeUniversalModel(const Model &model, const size_t numberElements, const ValueCallback &valueCallback, const ErrorCallback &errorCallback, const ModelTransformationCallback &modelTransformationCallback, const ModelAdjustmentCallback &modelAdjustmentCallback, Model &optimizedModel, const unsigned int iterations=5u, const Estimator::EstimatorType estimator=Estimator::ET_SQUARE, Scalar lambda=Scalar(0.001), const Scalar lambdaFactor=Scalar(5), Scalar *initialError=nullptr, Scalar *finalError=nullptr, Scalars *intermediateErrors=nullptr)
Optimizes a universal model by minimizing the error the model produces.
Definition: NonLinearUniversalOptimizationDense.h:326
StaticBuffer< Scalar, tResultDimension > Result
Definition of a model result.
Definition: NonLinearUniversalOptimizationDense.h:48
Callback< void, Model & > ModelAdjustmentCallback
Definition of a dense model adjustment function.
Definition: NonLinearUniversalOptimizationDense.h:80
StaticBuffer< Scalar, tExternalModelSize > ExternalModel
Definition of an external model.
Definition: NonLinearUniversalOptimizationDense.h:43
StaticBuffer< Scalar, tModelSize > Model
Definition of a model.
Definition: NonLinearUniversalOptimizationDense.h:38
Callback< void, const ExternalModel &, const size_t, Result & > ValueCallback
Definition of a callback function for dense value calculation.
Definition: NonLinearUniversalOptimizationDense.h:56
Callback< void, Model &, ExternalModel & > ModelTransformationCallback
Definition of a dense model transformation function.
Definition: NonLinearUniversalOptimizationDense.h:73
const T * data() const
Returns a pointer to the internal values.
Definition: Matrix.h:798
size_t rows() const
Returns the count of rows.
Definition: Matrix.h:692
void resize(const size_t rows, const size_t columns)
Resizes this matrix.
static constexpr T weakEps()
Returns a weak epsilon.
static T summedSqr(const T *values, const size_t number)
Returns the summed squares of a given values.
Definition: Numeric.h:1514
static constexpr T maxValue()
Returns the max scalar value.
Definition: Numeric.h:3244
const T * data() const
Returns the buffer data pointer.
Definition: StaticBuffer.h:240
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition: Math.h:144
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15