Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemplateTrackerTriangle.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 * Description:
31 * Template tracker.
32 *
33 * Authors:
34 * Amaury Dame
35 * Aurelien Yol
36 */
37#include <visp3/tt/vpTemplateTrackerTriangle.h>
38
44 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
45 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
46{ }
47
52 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
53 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
54{
55 *this = T;
56}
57
62{
65
66 l_t = T.l_t;
67 h_t = T.h_t;
68 C1.x = T.C1.x;
69 C1.y = T.C1.y;
70 C2.x = T.C2.x;
71 C2.y = T.C2.y;
72 C3.x = T.C3.x;
73 C3.y = T.C3.y;
74 // uvinv.resize(2,2);
75 // uvinv=T.uvinv;
76 // p_ds_uv.resize(2);
77 // p_ds_uv=T.p_ds_uv;
78 // ptempo.resize(2);
79 // ptempo=T.ptempo;
81
82 uvinv00 = T.uvinv00;
83 uvinv01 = T.uvinv01;
84 uvinv10 = T.uvinv10;
85 uvinv11 = T.uvinv11;
86
88 area = T.area;
89
90 return (*this);
91}
92
103 const vpColVector &c3)
104 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
105 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
106{
107 init(c1[0], c1[1], c2[0], c2[1], c3[0], c3[1]);
108}
109
113{
115 Ttemp.init(C1.x / 2., C1.y / 2., C2.x / 2., C2.y / 2., C3.x / 2., C3.y / 2.);
116 return Ttemp;
117}
118
124vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
125 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
126 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
127{
128 init(x1, y1, x2, y2, x3, y3);
129}
130
138 const vpImagePoint &c3)
139 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
140 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
141{
142 init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
143}
144
150vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
151 : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false), uvinv00(0.), uvinv01(0.),
152 uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
153{
154 init(x1, y1, x2, y2, x3, y3);
155}
156
166{
167 init(c1[0], c1[1], c2[0], c2[1], c3[0], c3[1]);
168}
169
176{
177 init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
178}
179
186void vpTemplateTrackerTriangle::init(int x1, int y1, int x2, int y2, int x3, int y3)
187{
188 init(static_cast<double>(x1), static_cast<double>(y1), static_cast<double>(x2), static_cast<double>(y2), static_cast<double>(x3), static_cast<double>(y3));
189}
190
197void vpTemplateTrackerTriangle::init(double x1, double y1, double x2, double y2, double x3, double y3)
198{
199 C1.x = x1;
200 C1.y = y1;
201 C2.x = x2;
202 C2.y = y2;
203 C3.x = x3;
204 C3.y = y3;
205
206 double minx, miny, maxx, maxy;
207 // calcul du rectangle minimal contenant le triangle seletionne
208 minx = (x1 < x2) ? x1 : x2;
209 miny = (y1 < y2) ? y1 : y2;
210 minx = (minx < x3) ? minx : x3;
211 miny = (miny < y3) ? miny : y3;
212 maxx = (x1 > x2) ? x1 : x2;
213 maxy = (y1 > y2) ? y1 : y2;
214 maxx = (maxx > x3) ? maxx : x3;
215 maxy = (maxy > y3) ? maxy : y3;
216
217 vpColVector u;
218 vpColVector v;
219 u.resize(2);
220 v.resize(2);
221 vpMatrix uv(2, 2);
222 vpMatrix uvinv(2, 2);
223
224 u[0] = C2.x - C1.x;
225 u[1] = C2.y - C1.y;
226
227 v[0] = C3.x - C1.x;
228 v[1] = C3.y - C1.y;
229
230 uv[0][0] = u[0];
231 uv[1][0] = v[0];
232 uv[0][1] = u[1];
233 uv[1][1] = v[1];
234 try {
235 uvinv = uv.inverseByLU();
236 not_good = false;
237 }
238 catch (...) {
239 not_good = true;
240 std::cout << "Triangle vide" << std::endl;
241 }
242 uvinv00 = uvinv[0][0];
243 uvinv01 = uvinv[0][1];
244 uvinv10 = uvinv[1][0];
245 uvinv11 = uvinv[1][1];
246
247 l_t = maxx - minx;
248 h_t = maxy - miny;
249 minx_temp = minx;
250 miny_temp = miny;
251
252 marge_triangle = 0.00001;
253 area = 0.5 * fabs(uv.det());
254}
255
256// marge ajoutee a zone pour que sommet soit pris en compte
257
263bool vpTemplateTrackerTriangle::inTriangle(const int &i, const int &j) const
264{
265 if (not_good)
266 return false;
267
268 /*ptempo[0]=j-C1.x;
269 ptempo[1]=i-C1.y;
270
271 p_ds_uv=ptempo*uvinv;
272 return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
273
274 double ptempo0 = j - C1.x;
275 double ptempo1 = i - C1.y;
276 double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
277 double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
278 return (p_ds_uv0 + p_ds_uv1 < 1. + marge_triangle && p_ds_uv0 > -marge_triangle && p_ds_uv1 > -marge_triangle);
279}
280
286bool vpTemplateTrackerTriangle::inTriangle(const double &i, const double &j) const
287{
288 if (not_good)
289 return false;
290 /*ptempo[0]=j-C1.x;
291 ptempo[1]=i-C1.y;
292
293 p_ds_uv=ptempo*uvinv;
294 return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
295 double ptempo0 = j - C1.x;
296 double ptempo1 = i - C1.y;
297 double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
298 double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
299 return (p_ds_uv0 + p_ds_uv1 < 1. + marge_triangle && p_ds_uv0 > -marge_triangle && p_ds_uv1 > -marge_triangle);
300}
301
306bool vpTemplateTrackerTriangle::inTriangle(const vpImagePoint &ip) const { return inTriangle(ip.get_i(), ip.get_j()); }
314{
315 c1.set_uv(C1.x, C1.y);
316 c2.set_uv(C2.x, C2.y);
317 c3.set_uv(C3.x, C3.y);
318}
319
325void vpTemplateTrackerTriangle::getCorners(std::vector<vpImagePoint> &c) const
326{
327 c.resize(3);
328 c[0].set_uv(C1.x, C1.y);
329 c[1].set_uv(C2.x, C2.y);
330 c[2].set_uv(C3.x, C3.y);
331}
332
339{
340 c1 = getCorner1();
341 c2 = getCorner2();
342 c3 = getCorner3();
343}
344
350vpColVector vpTemplateTrackerTriangle::getCorner1() const
351{
352 vpColVector c(2);
353 c[0] = C1.x;
354 c[1] = C1.y;
355
356 return c;
357}
363vpColVector vpTemplateTrackerTriangle::getCorner2() const
364{
365 vpColVector c(2);
366 c[0] = C2.x;
367 c[1] = C2.y;
368 return c;
369}
370
376vpColVector vpTemplateTrackerTriangle::getCorner3() const
377{
378 vpColVector c(2);
379 c[0] = C3.x;
380 c[1] = C3.y;
381 return c;
382}
383
389void vpTemplateTrackerTriangle::getSize(double &w, double &h) const
390{
391 w = l_t;
392 h = h_t;
393}
394
399void vpTemplateTrackerTriangle::getSize(int &w, int &h) const
400{
401 w = static_cast<int>(l_t) + 1;
402 h = static_cast<int>(h_t) + 1;
403}
404
409double vpTemplateTrackerTriangle::getMinx() const { return minx_temp - 1; }
414double vpTemplateTrackerTriangle::getMiny() const { return miny_temp - 1; }
419double vpTemplateTrackerTriangle::getMaxx() const { return minx_temp + l_t + 1; }
424double vpTemplateTrackerTriangle::getMaxy() const { return miny_temp + h_t + 1; }
425END_VISP_NAMESPACE
Implementation of column vector and the associated operations.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
double get_j() const
double get_u() const
void set_uv(double u, double v)
double get_i() const
double get_v() const
Implementation of a matrix and operations on matrices.
Definition vpMatrix.h:175
vpMatrix inverseByLU() const
double det(vpDetMethod method=LU_DECOMPOSITION) const
vpTemplateTrackerDPoint C3
Corner 2.
bool inTriangle(const vpImagePoint &ip) const
void getSize(double &w, double &h) const
vpTemplateTrackerDPoint C2
Corner 1.
vpTemplateTrackerTriangle getPyramidDown() const
void init(const vpColVector &c1, const vpColVector &c2, const vpColVector &c3)
vpTemplateTrackerTriangle & operator=(const vpTemplateTrackerTriangle &T)
void getCorners(vpColVector &c1, vpColVector &c2, vpColVector &c3) const