Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTutoParabolaModel.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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#ifndef VP_PARABOLA_MODEL_H
32#define VP_PARABOLA_MODEL_H
33
34#include <visp3/core/vpConfig.h>
35#include <visp3/core/vpColVector.h>
36#include <visp3/core/vpMatrix.h>
37
38#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
39#ifndef DOXYGEN_SHOULD_SKIP_THIS
40namespace tutorial
41{
46class vpTutoParabolaModel
47{
48public:
49 inline vpTutoParabolaModel(const unsigned int &degree, const unsigned int &height, const unsigned int &width)
50 : m_degree(degree)
51 , m_height(static_cast<unsigned int>(height))
52 , m_width(static_cast<unsigned int>(width))
53 , m_coeffs(degree + 1, 0.)
54 { }
55
64 inline vpTutoParabolaModel(const VISP_NAMESPACE_ADDRESSING vpColVector &coeffs, const unsigned int &height, const unsigned int &width)
65 : m_degree(coeffs.size() - 1)
66 , m_height(static_cast<unsigned int>(height))
67 , m_width(static_cast<unsigned int>(width))
68 , m_coeffs(coeffs)
69 { }
70
79 inline vpTutoParabolaModel(const VISP_NAMESPACE_ADDRESSING vpMatrix &coeffs, const unsigned int &height, const unsigned int &width)
80 : m_degree(coeffs.getRows() - 1)
81 , m_height(static_cast<unsigned int>(height))
82 , m_width(static_cast<unsigned int>(width))
83 , m_coeffs(coeffs.getCol(0))
84 { }
85
92 inline double eval(const double &u) const
93 {
94 double normalizedU = u / m_width;
95 double v = 0.;
96 for (unsigned int i = 0; i <= m_degree; ++i) {
97 v += m_coeffs[i] * std::pow(normalizedU, i);
98 }
99 v *= m_height;
100 return v;
101 }
102
108 inline VISP_NAMESPACE_ADDRESSING vpColVector toVpColVector() const
109 {
110 return m_coeffs;
111 }
112
113 inline vpTutoParabolaModel &operator=(const vpTutoParabolaModel &other)
114 {
115 m_degree = other.m_degree;
116 m_height = other.m_height;
117 m_width = other.m_width;
118 m_coeffs = other.m_coeffs;
119 return *this;
120 }
121
136 static void fillSystem(const unsigned int &degree, const double &height, const double &width, const std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> &pts, VISP_NAMESPACE_ADDRESSING vpMatrix &A, VISP_NAMESPACE_ADDRESSING vpMatrix &b)
137 {
138 const unsigned int nbPts = static_cast<unsigned int>(pts.size());
139 const unsigned int nbCoeffs = degree + 1;
140 std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> normalizedPts;
141 // Normalization to avoid numerical instability
142 for (const auto &pt: pts) {
143 normalizedPts.push_back(VISP_NAMESPACE_ADDRESSING vpImagePoint(pt.get_i() / height, pt.get_j() / width));
144 }
145 A.resize(nbPts, nbCoeffs, 1.); // Contains the u^i
146 b.resize(nbPts, 1); // Contains the v coordinates
147 for (unsigned int i = 0; i < nbPts; ++i) {
148 double u = normalizedPts[i].get_u();
149 double v = normalizedPts[i].get_v();
150 for (unsigned int j = 0; j < nbCoeffs; ++j) {
151 A[i][j] = std::pow(u, j);
152 }
153 b[i][0] = v;
154 }
155 }
157
158 friend std::ostream &operator<<(std::ostream &os, const vpTutoParabolaModel &model)
159 {
160 os << "Highest degree = " << model.m_degree << std::endl;
161 os << "Coeffs = [ " << model.m_coeffs.transpose() << " ]" << std::endl;
162 return os;
163 }
164
165private:
166 unsigned int m_degree;
167 unsigned int m_height;
168 unsigned int m_width;
169 VISP_NAMESPACE_ADDRESSING vpColVector m_coeffs;
170};
171}
172#endif
173#endif
174#endif