Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpRBConvergenceMetric.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
31#include <visp3/rbt/vpRBConvergenceMetric.h>
32
33#ifdef VISP_HAVE_NLOHMANN_JSON
34#include VISP_NLOHMANN_JSON(json_fwd.hpp)
35
36std::shared_ptr<vpRBConvergenceMetric> vpRBConvergenceMetric::loadFromJSON(const nlohmann::json &j)
37{
38 const std::string key = j.at("type");
39 double renderThreshold = j.value("renderThreshold", 0.0);
40 double convergenceThreshold = j.value("convergenceThreshold", 0.0);
41 unsigned int numPoints = j.value("samples", 0);
42 unsigned int seed = j.value("seed", 42);
43
44 if (key == "reprojection") {
45 return std::make_shared<vpRBConvergenceReprojectionMetric>(renderThreshold, convergenceThreshold, numPoints, seed);
46 }
47 else if (key == "add") {
48 return std::make_shared<vpRBConvergenceADDMetric>(renderThreshold, convergenceThreshold, numPoints, seed);
49 }
50
51 throw vpException(vpException::badValue, "Tried to parse an incorrect convergence metric type: %s", key.c_str());
52
53}
54#endif
55
56
57vpRBConvergenceMetric::vpRBConvergenceMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed)
58 : m_seed(seed), m_map(numPoints, 0.0, 0.0, 0.0, 0.0), m_random(seed),
59 m_rerenderThreshold(renderThreshold), m_convergedThreshold(convergedThreshold)
60{
61 m_indices.resize(numPoints, 1);
62 for (unsigned int i = 0; i < numPoints; ++i) {
63 m_indices[i][0] = i;
64 }
65}
66
68{
69 m_random.setSeed(m_seed, 0x123465789ULL);
70 vpTranslationVector minAxes, maxAxes;
71 renderer.get3DExtents(minAxes, maxAxes);
72
73 vpMatrix oX(m_map.getNumMaxPoints(), 3);
74 for (unsigned int i = 0; i < oX.getRows(); ++i) {
75 for (unsigned int j = 0; j < 3; ++j) {
76 oX[i][j] = m_random() * (maxAxes[j] - minAxes[j]) + minAxes[j];
77 }
78 }
79 vpArray2D<int> empty;
80 std::vector<int> removed;
81 unsigned int added;
82 vpMatrix normals;
83 m_map.updatePoints(empty, oX, normals, removed, added);
84 if (added != m_map.getNumMaxPoints()) {
85 throw vpException(vpException::dimensionError, "Something went wrong when inserting bb points into the map");
86 }
87}
88
89vpRBConvergenceADDMetric::vpRBConvergenceADDMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed) : vpRBConvergenceMetric(renderThreshold, convergedThreshold, numPoints, seed)
90{ }
91
92vpRBConvergenceReprojectionMetric::vpRBConvergenceReprojectionMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed) : vpRBConvergenceMetric(renderThreshold, convergedThreshold, numPoints, seed)
93{ }
94
96{
97 vpMatrix X1, X2;
98
99 m_map.project(cTo1, X1);
100 m_map.project(cTo2, X2);
101 if (X1.getRows() == 0) {
102 throw vpException(vpException::badValue, "Points were not sampled from the object");
103 }
104 double error = 0.0;
105
106 for (unsigned int i = 0; i < X1.getRows(); ++i) {
107 double d = sqrtf(vpMath::sqr(X1[i][0] - X2[i][0]) + vpMath::sqr(X1[i][1] - X2[i][1]) + vpMath::sqr(X1[i][2] - X2[i][2]));
108 if (!vpMath::isNaN(d)) {
109 error += d;
110 }
111 }
112
113 return error / static_cast<double>(X1.getRows());
114}
115
117{
118 vpMatrix X1, X2;
119 vpMatrix xs1, xs2;
120 vpMatrix uv1, uv2;
121 m_map.project(cam, m_indices, cTo1, X1, xs1, uv1);
122 m_map.project(cam, m_indices, cTo2, X2, xs2, uv2);
123 if (X1.getRows() == 0) {
124 throw vpException(vpException::badValue, "Points were not sampled from the object");
125 }
126 double error = 0.0;
127 for (unsigned int i = 0; i < uv1.getRows(); ++i) {
128 double d = sqrtf((uv1[i][0] - uv2[i][0]) + vpMath::sqr(uv1[i][1] - uv2[i][1]));
129 if (!vpMath::isNaN(d)) {
130 error += d;
131 }
132 }
133 return error / static_cast<double>(uv1.getRows());
134}
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition vpArray2D.h:146
unsigned int getRows() const
Definition vpArray2D.h:433
Generic class defining intrinsic camera parameters.
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
@ dimensionError
Bad dimension.
Definition vpException.h:71
Implementation of an homogeneous matrix and operations on such kind of matrices.
static bool isNaN(double value)
Definition vpMath.cpp:101
static double sqr(double x)
Definition vpMath.h:203
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
Single object focused renderer.
void get3DExtents(vpTranslationVector &minValues, vpTranslationVector &maxValues)
double operator()(const vpCameraParameters &, const vpHomogeneousMatrix &cTo1, const vpHomogeneousMatrix &cTo2) VP_OVERRIDE
vpRBConvergenceADDMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed)
vpRBConvergenceMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed)
static std::shared_ptr< vpRBConvergenceMetric > loadFromJSON(const nlohmann::json &j)
void sampleObject(vpObjectCentricRenderer &renderer)
vpRBConvergenceReprojectionMetric(double renderThreshold, double convergedThreshold, unsigned int numPoints, unsigned int seed)
double operator()(const vpCameraParameters &cam, const vpHomogeneousMatrix &cTo1, const vpHomogeneousMatrix &cTo2) VP_OVERRIDE
Class that consider the case of a translation vector.