ViSP  2.9.0
vpTemplateTrackerTriangle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTemplateTrackerTriangle.cpp 4636 2014-02-04 08:17:58Z 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)
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)
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 
94  return (*this);
95 }
96 
106  const vpColVector &c2,
107  const vpColVector &c3)
108  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
109  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001)
110 {
111  init(c1[0],c1[1],c2[0],c2[1],c3[0],c3[1]);
112 }
117 {
119  Ttemp.init(C1.x/2.,C1.y/2.,C2.x/2.,C2.y/2.,C3.x/2.,C3.y/2.);
120  return Ttemp;
121 }
122 
128 vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(int x1,int y1, int x2,int y2, int x3,int y3)
129  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
130  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001)
131 {
132  init(x1,y1,x2,y2,x3,y3);
133 }
134 
142  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
143  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001)
144 {
145  init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
146 }
147 
153 vpTemplateTrackerTriangle::vpTemplateTrackerTriangle(double x1,double y1, double x2,double y2, double x3,double y3)
154  : minx_temp(0), miny_temp(0), C1(), C2(), C3(), l_t(0), h_t(0), not_good(false),
155  uvinv00(0.), uvinv01(0.), uvinv10(0.), uvinv11(0.), marge_triangle(0.00001)
156 {
157  init(x1,y1,x2,y2,x3,y3);
158 }
168 {
169  init(c1[0],c1[1],c2[0],c2[1],c3[0],c3[1]);
170 }
178 {
179  init(c1.get_u(), c1.get_v(), c2.get_u(), c2.get_v(), c3.get_u(), c3.get_v());
180 }
181 
187 void vpTemplateTrackerTriangle::init(int x1, int y1, int x2,int y2, int x3, int y3)
188 {
189  init((double)x1,(double)y1,(double)x2,(double)y2,(double)x3,(double)y3);
190 }
191 
197 void vpTemplateTrackerTriangle::init(double x1, double y1, double x2,double y2, double x3, double y3)
198 {
199  C1.x=x1;C1.y=y1;
200  C2.x=x2;C2.y=y2;
201  C3.x=x3;C3.y=y3;
202 
203  double minx,miny,maxx,maxy;
204  //calcul du rectangle minimal contenant le triangle seletionn�
205  minx=(x1<x2)?x1:x2;
206  miny=(y1<y2)?y1:y2;
207  minx=(minx<x3)?minx:x3;
208  miny=(miny<y3)?miny:y3;
209  maxx=(x1>x2)?x1:x2;
210  maxy=(y1>y2)?y1:y2;
211  maxx=(maxx>x3)?maxx:x3;
212  maxy=(maxy>y3)?maxy:y3;
213 
214  vpColVector u;
215  vpColVector v;
216  u.resize(2);
217  v.resize(2);
218  vpMatrix uv(2,2);
219  vpMatrix uvinv(2,2);
220 
221  u[0]=C2.x-C1.x;
222  u[1]=C2.y-C1.y;
223 
224  v[0]=C3.x-C1.x;
225  v[1]=C3.y-C1.y;
226 
227  uv[0][0]=u[0];uv[1][0]=v[0];
228  uv[0][1]=u[1];uv[1][1]=v[1];
229  try
230  {
231  uvinv=uv.inverseByLU();
232  not_good=false;
233  }
234  catch(...)
235  {
236  not_good=true;
237  std::cout<<"Triangle vide"<<std::endl;
238 
239  }
240  uvinv00=uvinv[0][0];
241  uvinv01=uvinv[0][1];
242  uvinv10=uvinv[1][0];
243  uvinv11=uvinv[1][1];
244 
245  l_t=maxx-minx;
246  h_t=maxy-miny;
247  minx_temp=minx;
248  miny_temp=miny;
249 
250  marge_triangle = 0.00001;
251 }
252 
253 //marge ajoutee a zone pour que sommet soit pris en compte
254 
260 bool vpTemplateTrackerTriangle::inTriangle(const int &i, const int &j) const
261 {
262  if(not_good)
263  return false;
264 
265  /*ptempo[0]=j-C1.x;
266  ptempo[1]=i-C1.y;
267 
268  p_ds_uv=ptempo*uvinv;
269  return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
270 
271  double ptempo0=j-C1.x;
272  double ptempo1=i-C1.y;
273  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
274  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
275  return (p_ds_uv0+p_ds_uv1<1.+marge_triangle && p_ds_uv0>-marge_triangle && p_ds_uv1>-marge_triangle);
276 }
277 
283 bool vpTemplateTrackerTriangle::inTriangle(const double &i, const double &j) const
284 {
285  if(not_good)
286  return false;
287  /*ptempo[0]=j-C1.x;
288  ptempo[1]=i-C1.y;
289 
290  p_ds_uv=ptempo*uvinv;
291  return (p_ds_uv[0]+p_ds_uv[1]<1. && p_ds_uv[0]>0 && p_ds_uv[1]>0);*/
292  double ptempo0=j-C1.x;
293  double ptempo1=i-C1.y;
294  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
295  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
296  return (p_ds_uv0+p_ds_uv1<1.+marge_triangle && p_ds_uv0>-marge_triangle && p_ds_uv1>-marge_triangle);
297 }
298 
304 {
305  return inTriangle(ip.get_i(), ip.get_j());
306 }
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 
324 void vpTemplateTrackerTriangle::getCorners(std::vector<vpImagePoint> &c) const
325 {
326  c.resize(3);
327  c[0].set_uv(C1.x, C1.y);
328  c[1].set_uv(C2.x, C2.y);
329  c[2].set_uv(C3.x, C3.y);
330 }
331 
339 {
340  c1=getCorner1();
341  c2=getCorner2();
342  c3=getCorner3();
343 }
344 
349 vpColVector vpTemplateTrackerTriangle::getCorner1() const
350 {
351  vpColVector c(2);
352  c[0]=C1.x;
353  c[1]=C1.y;
354 
355  return c;
356 }
361 vpColVector vpTemplateTrackerTriangle::getCorner2() const
362 {
363  vpColVector c(2);
364  c[0]=C2.x;
365  c[1]=C2.y;
366  return c;
367 }
368 
373 vpColVector vpTemplateTrackerTriangle::getCorner3() const
374 {
375  vpColVector c(2);
376  c[0]=C3.x;
377  c[1]=C3.y;
378  return c;
379 }
380 
386 void vpTemplateTrackerTriangle::getSize(double &w,double &h) const
387 {
388  w=l_t;
389  h=h_t;
390 }
396 void vpTemplateTrackerTriangle::getSize(int &w,int &h) const
397 {
398  w=(int)l_t+1;
399  h=(int)h_t+1;
400 }
401 
407 {
408  return minx_temp-1;
409 }
415 {
416  return miny_temp-1;
417 }
423 {
424  return minx_temp+l_t+1;
425 }
431 {
432  return miny_temp+h_t+1;
433 }
434 
435 
436 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
double get_v() const
Definition: vpImagePoint.h:263
vpTemplateTrackerTriangle getPyramidDown() const
double get_i() const
Definition: vpImagePoint.h:194
double get_u() const
Definition: vpImagePoint.h:252
vpTemplateTrackerDPoint C2
Corner 1.
double get_j() const
Definition: vpImagePoint.h:205
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.
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:238
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94