Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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
40 namespace tutorial
41 {
46 class vpTutoParabolaModel
47 {
48 public:
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 
137  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)
138  {
139  const unsigned int nbPts = static_cast<unsigned int>(pts.size());
140  const unsigned int nbCoeffs = degree + 1;
141  std::vector<VISP_NAMESPACE_ADDRESSING vpImagePoint> normalizedPts;
142  // Normalization to avoid numerical instability
143  for (const auto &pt: pts) {
144  normalizedPts.push_back(VISP_NAMESPACE_ADDRESSING vpImagePoint(pt.get_i() / height, pt.get_j() / width));
145  }
146  A.resize(nbPts, nbCoeffs, 1.); // Contains the u^i
147  b.resize(nbPts, 1); // Contains the v coordinates
148  for (unsigned int i = 0; i < nbPts; ++i) {
149  double u = normalizedPts[i].get_u();
150  double v = normalizedPts[i].get_v();
151  for (unsigned int j = 0; j < nbCoeffs; ++j) {
152  A[i][j] = std::pow(u, j);
153  }
154  b[i][0] = v;
155  }
156  }
158 
159  friend std::ostream &operator<<(std::ostream &os, const vpTutoParabolaModel &model)
160  {
161  os << "Highest degree = " << model.m_degree << std::endl;
162  os << "Coeffs = [ " << model.m_coeffs.transpose() << " ]" << std::endl;
163  return os;
164  }
165 
166 private:
167  unsigned int m_degree;
168  unsigned int m_height;
169  unsigned int m_width;
170  VISP_NAMESPACE_ADDRESSING vpColVector m_coeffs;
171 };
172 }
173 #endif
174 #endif
175 #endif
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169