8 #ifndef META_OCEAN_GEOMETRY_ESTIMATOR_H
9 #define META_OCEAN_GEOMETRY_ESTIMATOR_H
133 template <EstimatorType tEstimator>
134 static constexpr
bool needSigma();
141 template <EstimatorType tEstimator>
142 static constexpr
bool isStandardEstimator();
149 static inline bool isStandardEstimator(
const EstimatorType estimator);
166 template <EstimatorType tEstimator>
187 template <EstimatorType tEstimator>
188 static inline Scalar robustErrorSquare(
const Scalar sqrValue,
const Scalar sqrSigma = 0);
207 template <EstimatorType tEstimator>
226 template <EstimatorType tEstimator>
227 static inline Scalar robustWeightSquare(
const Scalar sqrValue,
const Scalar sqrSigma = 0);
246 template <EstimatorType tEstimator>
247 static inline Scalar determineSigma(
const Scalar* errors,
const size_t number,
const size_t modelParameters);
257 static inline Scalar determineSigma(
const Scalar* errors,
const size_t number,
const size_t modelParameters,
const EstimatorType estimator);
268 template <EstimatorType tEstimator>
269 static inline Scalar determineSigma(
const Scalar* errors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters);
280 static inline Scalar determineSigma(
const Scalar* errors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters,
const EstimatorType estimator);
290 template <EstimatorType tEstimator>
291 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters);
301 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters,
const EstimatorType estimator);
312 template <EstimatorType tEstimator>
313 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters);
324 static inline Scalar determineSigmaSquare(
const Scalar* sqrErrors,
const unsigned int* indices,
const size_t numberIndices,
const size_t modelParameters,
const EstimatorType estimator);
334 template <Estimator::EstimatorType tEstimator>
335 static inline Scalar determineRobustError(
const Scalar* sqrErrors,
const size_t number,
const size_t modelParameters);
341 static constexpr
Scalar maximalWeight();
347 static constexpr
Scalar invMaximalWeight();
375 template <EstimatorType tEstimator>
376 static inline Scalar sigmaTuningContstant();
379 template <Estimator::EstimatorType tEstimator>
390 return needSigma<ET_SQUARE>();
393 return needSigma<ET_LINEAR>();
396 return needSigma<ET_HUBER>();
399 return needSigma<ET_TUKEY>();
402 return needSigma<ET_CAUCHY>();
408 ocean_assert(
false &&
"Invalid estimator!");
412 template <Estimator::EstimatorType tEstimator>
427 template <Estimator::EstimatorType tEstimator>
430 ocean_assert(
false &&
"Invalid estimator type!");
438 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
442 return value * value *
Scalar(0.5);
448 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
461 ocean_assert(sigma > 0);
465 if (absValue <= sigma)
467 return value * value *
Scalar(0.5);
470 return sigma * (absValue - sigma *
Scalar(0.5));
479 ocean_assert(sigma > 0);
481 const Scalar sqrSigma6 = sigma * sigma *
Scalar(1.0 / 6.0);
487 return sqrSigma6 * (
Scalar(1) - tmp * tmp * tmp);
498 ocean_assert(sigma > 0);
508 return robustError<ET_SQUARE>(value);
511 return robustError<ET_LINEAR>(value);
514 return robustError<ET_HUBER>(value, sigma);
517 return robustError<ET_TUKEY>(value, sigma);
520 return robustError<ET_CAUCHY>(value, sigma);
526 ocean_assert(
false &&
"Invalid estimator!");
527 return robustError<ET_SQUARE>(value);
530 template <Estimator::EstimatorType tEstimator>
533 ocean_assert(
false &&
"Invalid estimator type!");
539 inline Scalar Estimator::robustErrorSquare<Estimator::ET_SQUARE>(
const Scalar sqrValue,
const Scalar sqrSigma)
541 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
545 return sqrValue *
Scalar(0.5);
549 inline Scalar Estimator::robustErrorSquare<Estimator::ET_LINEAR>(
const Scalar sqrValue,
const Scalar sqrSigma)
551 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
559 inline Scalar Estimator::robustErrorSquare<Estimator::ET_HUBER>(
const Scalar sqrValue,
const Scalar sqrSigma)
564 ocean_assert(sqrSigma > 0);
566 if (sqrValue <= sqrSigma)
568 return sqrValue *
Scalar(0.5);
575 inline Scalar Estimator::robustErrorSquare<Estimator::ET_TUKEY>(
const Scalar sqrValue,
const Scalar sqrSigma)
580 ocean_assert(sqrSigma > 0);
582 if (sqrValue <= sqrSigma)
586 return sqrSigma *
Scalar(1.0 / 6.0) * (
Scalar(1) - tmp * tmp * tmp);
589 return sqrSigma *
Scalar(1.0 / 6.0);
593 inline Scalar Estimator::robustErrorSquare<Estimator::ET_CAUCHY>(
const Scalar sqrValue,
const Scalar sqrSigma)
597 ocean_assert(sqrSigma > 0);
607 return robustErrorSquare<ET_SQUARE>(sqrValue);
610 return robustErrorSquare<ET_LINEAR>(sqrValue);
613 return robustErrorSquare<ET_HUBER>(sqrValue, sqrSigma);
616 return robustErrorSquare<ET_TUKEY>(sqrValue, sqrSigma);
619 return robustErrorSquare<ET_CAUCHY>(sqrValue, sqrSigma);
625 ocean_assert(
false &&
"Invalid estimator!");
626 return robustErrorSquare<ET_SQUARE>(sqrValue);
629 template <Estimator::EstimatorType tEstimator>
632 ocean_assert(
false &&
"Invalid estimator!");
640 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
648 ocean_assert_and_suppress_unused(sigma ==
Scalar(0), sigma);
652 return std::min(
Scalar(1) / absValue, maximalWeight());
658 ocean_assert(sigma > 0);
662 if (absValue <= sigma)
667 return std::min(sigma / absValue, maximalWeight());
673 ocean_assert(sigma > 0);
677 if (absValue > sigma)
688 ocean_assert(sigma > 0);
698 return robustWeight<ET_SQUARE>(value);
701 return robustWeight<ET_LINEAR>(value);
704 return robustWeight<ET_HUBER>(value, sigma);
707 return robustWeight<ET_TUKEY>(value, sigma);
710 return robustWeight<ET_CAUCHY>(value, sigma);
716 ocean_assert(
false &&
"Invalid estimator!");
717 return robustWeight<ET_SQUARE>(value);
720 template <Estimator::EstimatorType tEstimator>
723 ocean_assert(
false &&
"Invalid estimator!");
728 inline Scalar Estimator::robustWeightSquare<Estimator::ET_SQUARE>(
const Scalar ,
const Scalar sqrSigma)
730 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
736 inline Scalar Estimator::robustWeightSquare<Estimator::ET_LINEAR>(
const Scalar sqrValue,
const Scalar sqrSigma)
738 ocean_assert(sqrValue >= 0);
739 ocean_assert_and_suppress_unused(sqrSigma ==
Scalar(0), sqrSigma);
750 inline Scalar Estimator::robustWeightSquare<Estimator::ET_HUBER>(
const Scalar sqrValue,
const Scalar sqrSigma)
752 ocean_assert(sqrValue >= 0 && sqrSigma > 0);
754 if (sqrValue <= sqrSigma)
759 return std::min(
Numeric::sqrt(sqrSigma / sqrValue), maximalWeight());
763 inline Scalar Estimator::robustWeightSquare<Estimator::ET_TUKEY>(
const Scalar sqrValue,
const Scalar sqrSigma)
765 ocean_assert(sqrSigma > 0);
767 if (sqrValue > sqrSigma)
776 inline Scalar Estimator::robustWeightSquare<Estimator::ET_CAUCHY>(
const Scalar sqrValue,
const Scalar sqrSigma)
778 ocean_assert(sqrSigma > 0);
788 return robustWeightSquare<ET_SQUARE>(sqrValue);
791 return robustWeightSquare<ET_LINEAR>(sqrValue);
794 return robustWeightSquare<ET_HUBER>(sqrValue, sqrSigma);
797 return robustWeightSquare<ET_TUKEY>(sqrValue, sqrSigma);
800 return robustWeightSquare<ET_CAUCHY>(sqrValue, sqrSigma);
806 ocean_assert(
false &&
"Invalid estimator!");
807 return robustWeightSquare<ET_SQUARE>(sqrValue);
810 template <Estimator::EstimatorType tEstimator>
813 ocean_assert(number > 0);
817 if (number <= modelParameters)
819 return max(
Numeric::eps(), sigmaTuningContstant<tEstimator>() *
Scalar(1.4826) * median);
834 return determineSigma<ET_HUBER>(errors, number, modelParameters);
837 return determineSigma<ET_TUKEY>(errors, number, modelParameters);
840 return determineSigma<ET_CAUCHY>(errors, number, modelParameters);
846 ocean_assert(
false &&
"Invalid estimator!");
850 template <Estimator::EstimatorType tEstimator>
853 ocean_assert(errors !=
nullptr);
854 ocean_assert(indices !=
nullptr);
855 ocean_assert(numberIndices > 0);
856 ocean_assert(modelParameters >= 1);
859 Scalars subsetErrors(numberIndices);
860 for (
size_t n = 0; n < numberIndices; ++n)
862 subsetErrors[n] = errors[indices[n]];
867 if (numberIndices <= modelParameters)
869 return max(
Numeric::eps(), sigmaTuningContstant<tEstimator>() *
Scalar(1.4826) * median);
872 return max(
Numeric::eps(), sigmaTuningContstant<tEstimator>() *
Scalar(1.4826) * (1 +
Scalar(5) /
Scalar(numberIndices - modelParameters)) * median);
884 return determineSigma<ET_HUBER>(errors, indices, numberIndices, modelParameters);
887 return determineSigma<ET_TUKEY>(errors, indices, numberIndices, modelParameters);
890 return determineSigma<ET_CAUCHY>(errors, indices, numberIndices, modelParameters);
896 ocean_assert(
false &&
"Invalid estimator!");
900 template <Estimator::EstimatorType tEstimator>
903 ocean_assert(sqrErrors !=
nullptr);
904 ocean_assert(number > 0);
908 if (number <= modelParameters)
925 return determineSigmaSquare<ET_HUBER>(sqrErrors, number, modelParameters);
928 return determineSigmaSquare<ET_TUKEY>(sqrErrors, number, modelParameters);
931 return determineSigmaSquare<ET_CAUCHY>(sqrErrors, number, modelParameters);
937 ocean_assert(
false &&
"Invalid estimator!");
941 template <Estimator::EstimatorType tEstimator>
944 ocean_assert(sqrErrors !=
nullptr);
945 ocean_assert(indices !=
nullptr);
946 ocean_assert(numberIndices > 0);
949 Scalars subsetSqrErrors(numberIndices);
950 for (
size_t n = 0; n < numberIndices; ++n)
952 subsetSqrErrors[n] = sqrErrors[indices[n]];
957 if (numberIndices <= modelParameters)
974 return determineSigmaSquare<ET_HUBER>(sqrErrors, indices, numberIndices, modelParameters);
977 return determineSigmaSquare<ET_TUKEY>(sqrErrors, indices, numberIndices, modelParameters);
980 return determineSigmaSquare<ET_CAUCHY>(sqrErrors, indices, numberIndices, modelParameters);
986 ocean_assert(
false &&
"Invalid estimator!");
990 template <Estimator::EstimatorType tEstimator>
993 ocean_assert(sqrErrors !=
nullptr && number >= 1);
994 ocean_assert(modelParameters >= 1);
997 const Scalar sqrSigma = needSigma<tEstimator>() ?
Numeric::sqr(determineSigmaSquare<tEstimator>(sqrErrors, number, modelParameters)) : 0;
1001 for (
size_t n = 0; n < number; ++n)
1003 robustError += sqrErrors[n] * robustWeightSquare<tEstimator>(sqrErrors[n], sqrSigma);
1020 template <Estimator::EstimatorType tEstimator>
1023 ocean_assert(
false &&
"Invalid estimator type!");
1028 inline Scalar Estimator::sigmaTuningContstant<Estimator::ET_HUBER>()
1034 inline Scalar Estimator::sigmaTuningContstant<Estimator::ET_TUKEY>()
1040 inline Scalar Estimator::sigmaTuningContstant<Estimator::ET_CAUCHY>()
This class implements robust estimator functions.
Definition: Estimator.h:27
static Scalar robustWeight(const Scalar value, const Scalar sigma=0)
Returns the weight in relation to a error for a given residual error and a specified estimator.
Definition: Estimator.h:630
static Scalar robustWeightSquare(const Scalar sqrValue, const Scalar sqrSigma=0)
Returns the weight in relation to a squared error for a given residual error and a specified estimato...
Definition: Estimator.h:721
static std::string translateEstimatorType(const EstimatorType estimatorType)
Translates a given estimator type into a readable string.
static constexpr Scalar invMaximalWeight()
Returns the inverse maximal weight for any estimator which is used to clamp extremely high weights (f...
Definition: Estimator.h:1015
static Scalar determineSigmaSquare(const Scalar *sqrErrors, const size_t number, const size_t modelParameters)
Determines the squared sigma for a specific set of squared residual errors and a specified estimator.
Definition: Estimator.h:901
static Scalar determineRobustError(const Scalar *sqrErrors, const size_t number, const size_t modelParameters)
Determines the overall robust error for set of given squared errors, a specified estimator and the di...
Definition: Estimator.h:991
static constexpr Scalar maximalWeight()
Returns the maximal weight for any estimator which is used to clamp extremely high weights (for tiny ...
Definition: Estimator.h:1010
static EstimatorType translateEstimatorType(const std::string &estimatorType)
Translates a readable name of an estimator type to it's value.
static Scalar sigmaTuningContstant()
Returns the tuning constant allowing to determine a 95 percent efficiency on the standard normal dist...
Definition: Estimator.h:1021
static constexpr bool needSigma()
Returns whether an estimator needs a standard deviation for computation.
Definition: Estimator.h:380
static const EstimatorTypes & estimatorTypes()
Returns all existing valid estimator types.
std::vector< EstimatorType > EstimatorTypes
Definition of a vector holding estimator types.
Definition: Estimator.h:124
EstimatorType
Definition of individual robust estimator types.
Definition: Estimator.h:34
@ ET_HUBER
The Huber estimator type.
Definition: Estimator.h:84
@ ET_TUKEY
The Tukey estimator.
Definition: Estimator.h:102
@ ET_SQUARE
The standard square error estimator (L2).
Definition: Estimator.h:52
@ ET_LINEAR
The linear estimator (L1).
Definition: Estimator.h:66
@ ET_INVALID
An invalid estimator type.
Definition: Estimator.h:38
@ ET_CAUCHY
The Cauchy estimator.
Definition: Estimator.h:118
static constexpr bool isStandardEstimator()
Returns whether an estimator is the standard square error estimator.
Definition: Estimator.h:413
static Scalar robustError(const Scalar value, const Scalar sigma=0)
Returns the robust error of a residual error for a specified estimator.
Definition: Estimator.h:428
static Scalar robustErrorSquare(const Scalar sqrValue, const Scalar sqrSigma=0)
Returns the robust error of a squared residual error for a specified estimator.
Definition: Estimator.h:531
static Scalar determineSigma(const Scalar *errors, const size_t number, const size_t modelParameters)
Determines the sigma for a specific set of residual errors and a specified estimator.
Definition: Estimator.h:811
static constexpr T weakEps()
Returns a weak epsilon.
static T log(const T value)
Returns the natural logarithm of a given value (the logarithm to the base e).
Definition: Numeric.h:1655
static T abs(const T value)
Returns the absolute value of a given value.
Definition: Numeric.h:1220
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static constexpr T eps()
Returns a small epsilon.
static constexpr T sqr(const T value)
Returns the square of a given value.
Definition: Numeric.h:1495
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
@ ET_INVALID
An invalid event type.
Definition: TrackerEvent.h:37
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15