ViSP  2.10.0
vpTemplateTrackerTriangle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerTriangle.cpp 5060 2014-12-12 18:31:03Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Template tracker.
35  *
36  * Authors:
37  * Amaury Dame
38  * Aurelien Yol
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 #include <visp/vpTemplateTrackerTriangle.h>
43 
48  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
49  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
50 {
51 }
52 
57  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
58  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
59 {
60  *this = T;
61 }
62 
67 {
70 
71  l_t=T.l_t;
72  h_t=T.h_t;
73  C1.x=T.C1.x;
74  C1.y=T.C1.y;
75  C2.x=T.C2.x;
76  C2.y=T.C2.y;
77  C3.x=T.C3.x;
78  C3.y=T.C3.y;
79  //uvinv.resize(2,2);
80  //uvinv=T.uvinv;
81  //p_ds_uv.resize(2);
82  //p_ds_uv=T.p_ds_uv;
83  //ptempo.resize(2);
84  //ptempo=T.ptempo;
86 
87  uvinv00=T.uvinv00;
88  uvinv01=T.uvinv01;
89  uvinv10=T.uvinv10;
90  uvinv11=T.uvinv11;
91 
93  area = T.area;
94 
95  return (*this);
96 }
97 
107  const vpColVector &c2,
108  const vpColVector &c3)
109  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
110  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
111 {
112  init(c1[0],c1[1],c2[0],c2[1],c3[0],c3[1]);
113 }
118 {
120  Ttemp.init(C1.x/2.,C1.y/2.,C2.x/2.,C2.y/2.,C3.x/2.,C3.y/2.);
121  return Ttemp;
122 }
123 
129 vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(int x1,int y1, int x2,int y2, int x3,int y3)
130  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
131  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
132 {
133  init(x1,y1,x2,y2,x3,y3);
134 }
135 
143  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
144  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
145 {
146  init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
147 }
148 
154 vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(double x1,double y1, double x2,double y2, double x3,double y3)
155  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
156  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001), area(0)
157 {
158  init(x1,y1,x2,y2,x3,y3);
159 }
169 {
170  init(c1[0],c1[1],c2[0],c2[1],c3[0],c3[1]);
171 }
179 {
180  init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
181 }
182 
188 void vpTemplateTrackerTriangle::init(int x1, int y1, int x2,int y2, int x3, int y3)
189 {
190  init((double)x1,(double)y1,(double)x2,(double)y2,(double)x3,(double)y3);
191 }
192 
198 void vpTemplateTrackerTriangle::init(double x1, double y1, double x2,double y2, double x3, double y3)
199 {
200  C1.x=x1;C1.y=y1;
201  C2.x=x2;C2.y=y2;
202  C3.x=x3;C3.y=y3;
203 
204  double minx,miny,maxx,maxy;
205  //calcul du rectangle minimal contenant le triangle seletionne
206  minx=(x1<x2)?x1:x2;
207  miny=(y1<y2)?y1:y2;
208  minx=(minx<x3)?minx:x3;
209  miny=(miny<y3)?miny:y3;
210  maxx=(x1>x2)?x1:x2;
211  maxy=(y1>y2)?y1:y2;
212  maxx=(maxx>x3)?maxx:x3;
213  maxy=(maxy>y3)?maxy:y3;
214 
215  vpColVector u;
216  vpColVector v;
217  u.resize(2);
218  v.resize(2);
219  vpMatrix uv(2,2);
220  vpMatrix uvinv(2,2);
221 
222  u[0]=C2.x-C1.x;
223  u[1]=C2.y-C1.y;
224 
225  v[0]=C3.x-C1.x;
226  v[1]=C3.y-C1.y;
227 
228  uv[0][0]=u[0];uv[1][0]=v[0];
229  uv[0][1]=u[1];uv[1][1]=v[1];
230  try
231  {
232  uvinv=uv.inverseByLU();
233  not_good=false;
234  }
235  catch(...)
236  {
237  not_good=true;
238  std::cout<<"Triangle vide"<<std::endl;
239 
240  }
241  uvinv00=uvinv[0][0];
242  uvinv01=uvinv[0][1];
243  uvinv10=uvinv[1][0];
244  uvinv11=uvinv[1][1];
245 
246  l_t=maxx-minx;
247  h_t=maxy-miny;
248  minx_temp=minx;
249  miny_temp=miny;
250 
251  marge_triangle = 0.00001;
252  area = 0.5 * fabs(uv.det());
253 }
254 
255 //marge ajoutee a zone pour que sommet soit pris en compte
256 
262 bool vpTemplateTrackerTriangle::inTriangle(const int &i, const int &j) const
263 {
264  if(not_good)
265  return false;
266 
267  /*ptempo[0]=j-C1.x;
268  ptempo[1]=i-C1.y;
269 
270  p_ds_uv=ptempo*uvinv;
271  return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
272 
273  double ptempo0=j-C1.x;
274  double ptempo1=i-C1.y;
275  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
276  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
277  return (p_ds_uv0+p_ds_uv1<1.+marge_triangle && p_ds_uv0>-marge_triangle && p_ds_uv1>-marge_triangle);
278 }
279 
285 bool vpTemplateTrackerTriangle::inTriangle(const double &i, const double &j) const
286 {
287  if(not_good)
288  return false;
289  /*ptempo[0]=j-C1.x;
290  ptempo[1]=i-C1.y;
291 
292  p_ds_uv=ptempo*uvinv;
293  return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
294  double ptempo0=j-C1.x;
295  double ptempo1=i-C1.y;
296  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
297  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
298  return (p_ds_uv0+p_ds_uv1<1.+marge_triangle && p_ds_uv0>-marge_triangle && p_ds_uv1>-marge_triangle);
299 }
300 
306 {
307  return inTriangle(ip.get_i(), ip.get_j());
308 }
316 {
317  c1.set_uv(C1.x, C1.y);
318  c2.set_uv(C2.x, C2.y);
319  c3.set_uv(C3.x, C3.y);
320 }
321 
326 void vpTemplateTrackerTriangle::getCorners(std::vector<vpImagePoint> &c) const
327 {
328  c.resize(3);
329  c[0].set_uv(C1.x, C1.y);
330  c[1].set_uv(C2.x, C2.y);
331  c[2].set_uv(C3.x, C3.y);
332 }
333 
341 {
342  c1=getCorner1();
343  c2=getCorner2();
344  c3=getCorner3();
345 }
346 
351 vpColVector vpTemplateTrackerTriangle::getCorner1() const
352 {
353  vpColVector c(2);
354  c[0]=C1.x;
355  c[1]=C1.y;
356 
357  return c;
358 }
363 vpColVector vpTemplateTrackerTriangle::getCorner2() const
364 {
365  vpColVector c(2);
366  c[0]=C2.x;
367  c[1]=C2.y;
368  return c;
369 }
370 
375 vpColVector vpTemplateTrackerTriangle::getCorner3() const
376 {
377  vpColVector c(2);
378  c[0]=C3.x;
379  c[1]=C3.y;
380  return c;
381 }
382 
388 void vpTemplateTrackerTriangle::getSize(double &w,double &h) const
389 {
390  w=l_t;
391  h=h_t;
392 }
398 void vpTemplateTrackerTriangle::getSize(int &w,int &h) const
399 {
400  w=(int)l_t+1;
401  h=(int)h_t+1;
402 }
403 
409 {
410  return minx_temp-1;
411 }
417 {
418  return miny_temp-1;
419 }
425 {
426  return minx_temp+l_t+1;
427 }
433 {
434  return miny_temp+h_t+1;
435 }
436 
437 
438 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
double get_v() const
Definition: vpImagePoint.h:264
vpTemplateTrackerTriangle getPyramidDown() const
double get_i() const
Definition: vpImagePoint.h:195
double get_u() const
Definition: vpImagePoint.h:253
vpTemplateTrackerDPoint C2
Corner 1.
double get_j() const
Definition: vpImagePoint.h:206
void getCorners(vpColVector &c1, vpColVector &c2, vpColVector &c3) const
void getSize(double &w, double &h) const
bool inTriangle(const vpImagePoint &ip) const
vpTemplateTrackerDPoint C3
Corner 2.
double det(vpDetMethod method=LU_DECOMPOSITION) const
Definition: vpMatrix.cpp:3765
vpTemplateTrackerTriangle & operator=(const vpTemplateTrackerTriangle &T)
void init(const vpColVector &c1, const vpColVector &c2, const vpColVector &c3)
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpMatrix inverseByLU() const
void set_uv(const double u, const double v)
Definition: vpImagePoint.h:239
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:98