ViSP  2.10.0
vpTriangle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTriangle.cpp 4632 2014-02-03 17:06:40Z 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  *
34  * Description:
35  * Defines a 2D triangle.
36  *
37  * Author:
38  * Amaury Dame
39  * Nicolas Melchior
40  *
41  *****************************************************************************/
42 
43 #include <visp/vpTriangle.h>
44 #include <visp/vpDebug.h>
45 
52  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
53  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
54 {
55  init (vpImagePoint(0,0),vpImagePoint(1,0),vpImagePoint(0,1));
56 }
57 
65 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
66  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
67  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
68 {
69  init(iP1,iP2,iP3);
70 }
71 
78  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
79  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
80 {
81  *this = tri;
82 }
83 
88 {
89 }
90 
94 vpTriangle &
96 {
97  goodTriange = tri.goodTriange;
98  S1 = tri.S1;
99  uvinv00 = tri.uvinv00;
100  uvinv01 = tri.uvinv01;
101  uvinv10 = tri.uvinv10;
102  uvinv11 = tri.uvinv11;
103  ptempo0 = tri.ptempo0;
104  ptempo1 = tri.ptempo1;
105  area = tri.area;
106  return *this;
107 };
108 
116 void
117 vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
118 {
119  init(iP1,iP2,iP3);
120 }
121 
122 
123 void
124 vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
125 {
126  ptempo0 = ptempo1 = 0.;
127  apex1 = iP1;
128  apex2 = iP2;
129  apex3 = iP3;
130 
131  vpMatrix uv(2,2);
132  vpMatrix uvinv(2,2);
133 
134  uv[0][0] = iP2.get_i() - iP1.get_i();
135  uv[1][0] = iP3.get_i() - iP1.get_i();
136  uv[0][1] = iP2.get_j() - iP1.get_j();
137  uv[1][1] = iP3.get_j() - iP1.get_j();
138  try
139  {
140  uvinv=uv.inverseByLU();
141  goodTriange = true;
142  }
143  catch(...)
144  {
145  goodTriange = false;
146  std::cout<<"Empty triangle"<<std::endl;
147  }
148 
149  uvinv00=uvinv[0][0];
150  uvinv01=uvinv[0][1];
151  uvinv10=uvinv[1][0];
152  uvinv11=uvinv[1][1];
153  S1 = iP1;
154  area = 0.5 * fabs(uv.det());
155 }
156 
157 
166 bool
167 vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
168 {
169  if(!goodTriange)
170  return false;
171 
172  if(threshold < 0)
173  threshold = 0;
174 
175  ptempo0 = iP.get_i() - S1.get_i();
176  ptempo1 = iP.get_j() - S1.get_j();
177 
178  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
179  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
180 
181  return (p_ds_uv0+p_ds_uv1<1.+threshold && p_ds_uv0>-threshold && p_ds_uv1>-threshold);
182 }
183 
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
double get_i() const
Definition: vpImagePoint.h:195
Defines a 2D triangle.
Definition: vpTriangle.h:58
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:167
double get_j() const
Definition: vpImagePoint.h:206
virtual ~vpTriangle()
Definition: vpTriangle.cpp:87
void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:117
vpTriangle & operator=(const vpTriangle &tri)
Definition: vpTriangle.cpp:95
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:93