Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
vpTemplateTrackerWarpAffine.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Template tracker.
33  *
34  * Authors:
35  * Amaury Dame
36  * Aurelien Yol
37  *
38 *****************************************************************************/
39 #include <visp3/tt/vpTemplateTrackerWarpAffine.h>
40 
45 
53 {
54  p_down[0] = p[0];
55  p_down[1] = p[1];
56  p_down[2] = p[2];
57  p_down[3] = p[3];
58  p_down[4] = p[4] / 2.;
59  p_down[5] = p[5] / 2.;
60 }
61 
69 {
70  p_up[0] = p[0];
71  p_up[1] = p[1];
72  p_up[2] = p[2];
73  p_up[3] = p[3];
74  p_up[4] = p[4] * 2.;
75  p_up[5] = p[5] * 2.;
76 }
77 
86 void vpTemplateTrackerWarpAffine::getdW0(const int &v, const int &u, const double &dv, const double &du, double *dIdW)
87 {
88  dIdW[0] = u * du;
89  dIdW[1] = u * dv;
90  dIdW[2] = v * du;
91  dIdW[3] = v * dv;
92  dIdW[4] = du;
93  dIdW[5] = dv;
94 }
95 
107 void vpTemplateTrackerWarpAffine::getdWdp0(const int &v, const int &u, double *dIdW)
108 {
109  dIdW[0] = u;
110  dIdW[1] = 0;
111  dIdW[2] = v;
112  dIdW[3] = 0;
113  dIdW[4] = 1.;
114  dIdW[5] = 0;
115 
116  dIdW[6] = 0;
117  dIdW[7] = u;
118  dIdW[8] = 0;
119  dIdW[9] = v;
120  dIdW[10] = 0;
121  dIdW[11] = 1.;
122 }
123 
133 void vpTemplateTrackerWarpAffine::warpX(const int &v1, const int &u1, double &v2, double &u2, const vpColVector &p)
134 {
135  u2 = (1 + p[0]) * u1 + p[2] * v1 + p[4];
136  v2 = p[1] * u1 + (1 + p[3]) * v1 + p[5];
137 }
138 
147 {
148  X2[0] = (1.0 + p[0]) * X1[0] + p[2] * X1[1] + p[4];
149  X2[1] = p[1] * X1[0] + (1.0 + p[3]) * X1[1] + p[5];
150 }
151 
162 {
163  double u = X[0];
164  double v = X[1];
165 
166  dM[0][0] = u;
167  dM[0][1] = 0;
168  dM[0][2] = v;
169  dM[0][3] = 0;
170  dM[0][4] = 1;
171  dM[0][5] = 0;
172  dM[1][0] = 0;
173  dM[1][1] = u;
174  dM[1][2] = 0;
175  dM[1][3] = v;
176  dM[1][4] = 0;
177  dM[1][5] = 1;
178 }
179 
188  const double *dwdp0, vpMatrix &dM)
189 {
190  for (unsigned int i = 0; i < nbParam; i++) {
191  dM[0][i] = (1. + p[0]) * dwdp0[i] + p[2] * dwdp0[i + nbParam];
192  dM[1][i] = p[1] * dwdp0[i] + (1. + p[3]) * dwdp0[i + nbParam];
193  }
194 }
195 
204 {
205  X2[0] = (1 + p[0]) * X1[0] + p[2] * X1[1] + p[4];
206  X2[1] = p[1] * X1[0] + (1 + p[3]) * X1[1] + p[5];
207 }
208 
216 {
217  double u = p[4];
218  double v = p[5];
219  double r_00 = 1 + p[0], r_01 = p[2];
220  double r_10 = p[1], r_11 = 1 + p[3];
221  double det = r_00 * r_11 - r_01 * r_10;
222  if (std::fabs(det) < std::numeric_limits<double>::epsilon()) {
223  throw(vpException(vpException::fatalError, "In vpTemplateTrackerWarpAffine::getParamInverse() "
224  "cannot inverse 2-by-2 matrix. Matrix determinant is 0."));
225  }
226 
227  double ri_11 = r_00 / det;
228  double ri_00 = r_11 / det;
229  double ri_01 = -r_01 / det;
230  double ri_10 = -r_10 / det;
231 
232  p_inv[0] = ri_00 - 1;
233  p_inv[1] = ri_10;
234  p_inv[2] = ri_01;
235  p_inv[3] = ri_11 - 1;
236  p_inv[4] = -(ri_00 * u + ri_01 * v);
237  p_inv[5] = -(ri_10 * u + ri_11 * v);
238 }
239 
249 {
250  double r1_00 = 1 + p1[0], r1_01 = p1[2];
251  double r1_10 = p1[1], r1_11 = 1 + p1[3];
252  double r2_00 = 1 + p2[0], r2_01 = p2[2];
253  double r2_10 = p2[1], r2_11 = 1 + p2[3];
254  double u1 = p1[4];
255  double v1 = p1[5];
256  double u2 = p2[4];
257  double v2 = p2[5];
258 
259  p12[0] = r1_00 * r2_00 + r1_01 * r2_10 - 1.;
260  p12[1] = r1_10 * r2_00 + r1_11 * r2_10;
261  p12[2] = r1_00 * r2_01 + r1_01 * r2_11;
262  p12[3] = r1_10 * r2_01 + r1_11 * r2_11 - 1.;
263  p12[4] = r1_00 * u2 + r1_01 * v2 + u1;
264  p12[5] = r1_10 * u2 + r1_11 * v2 + v1;
265 }
Implementation of column vector and the associated operations.
Definition: vpColVector.h:167
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:152
void pRondp(const vpColVector &p1, const vpColVector &p2, vpColVector &p12) const
void getParamPyramidDown(const vpColVector &p, vpColVector &p_down)
void getdW0(const int &v, const int &u, const double &dv, const double &du, double *dIdW)
void warpX(const vpColVector &X1, vpColVector &X2, const vpColVector &p)
void getParamInverse(const vpColVector &p, vpColVector &p_inv) const
void dWarp(const vpColVector &X, const vpColVector &, const vpColVector &, vpMatrix &dM)
void getParamPyramidUp(const vpColVector &p, vpColVector &p_up)
void warpXInv(const vpColVector &X1, vpColVector &X2, const vpColVector &p)
void dWarpCompo(const vpColVector &, const vpColVector &, const vpColVector &p, const double *dwdp0, vpMatrix &dM)
void getdWdp0(const int &v, const int &u, double *dIdW)
unsigned int nbParam
Number of parameters used to model warp transformation.