Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpTriangle.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Defines a 2D triangle.
32  *
33  * Author:
34  * Amaury Dame
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpTriangle.h>
40 #include <visp3/core/vpDebug.h>
41 
48  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
49  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
50 {
51  init (vpImagePoint(0,0),vpImagePoint(1,0),vpImagePoint(0,1));
52 }
53 
61 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
62  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
63  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
64 {
65  init(iP1,iP2,iP3);
66 }
67 
74  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0),
75  ptempo0(0), ptempo1(0), area(0), apex1(), apex2(), apex3()
76 {
77  *this = tri;
78 }
79 
84 {
85 }
86 
90 vpTriangle &
92 {
93  goodTriange = tri.goodTriange;
94  S1 = tri.S1;
95  uvinv00 = tri.uvinv00;
96  uvinv01 = tri.uvinv01;
97  uvinv10 = tri.uvinv10;
98  uvinv11 = tri.uvinv11;
99  ptempo0 = tri.ptempo0;
100  ptempo1 = tri.ptempo1;
101  area = tri.area;
102  return *this;
103 };
104 
112 void
113 vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
114 {
115  init(iP1,iP2,iP3);
116 }
117 
118 
119 void
120 vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
121 {
122  ptempo0 = ptempo1 = 0.;
123  apex1 = iP1;
124  apex2 = iP2;
125  apex3 = iP3;
126 
127  vpMatrix uv(2,2);
128  vpMatrix uvinv(2,2);
129 
130  uv[0][0] = iP2.get_i() - iP1.get_i();
131  uv[1][0] = iP3.get_i() - iP1.get_i();
132  uv[0][1] = iP2.get_j() - iP1.get_j();
133  uv[1][1] = iP3.get_j() - iP1.get_j();
134  try
135  {
136  uvinv=uv.inverseByLU();
137  goodTriange = true;
138  }
139  catch(...)
140  {
141  goodTriange = false;
142  std::cout<<"Empty triangle"<<std::endl;
143  }
144 
145  uvinv00=uvinv[0][0];
146  uvinv01=uvinv[0][1];
147  uvinv10=uvinv[1][0];
148  uvinv11=uvinv[1][1];
149  S1 = iP1;
150  area = 0.5 * fabs(uv.det());
151 }
152 
153 
162 bool
163 vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
164 {
165  if(!goodTriange)
166  return false;
167 
168  if(threshold < 0)
169  threshold = 0;
170 
171  ptempo0 = iP.get_i() - S1.get_i();
172  ptempo1 = iP.get_j() - S1.get_j();
173 
174  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
175  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
176 
177  return (p_ds_uv0+p_ds_uv1<1.+threshold && p_ds_uv0>-threshold && p_ds_uv1>-threshold);
178 }
179 
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
double get_i() const
Definition: vpImagePoint.h:199
Defines a 2D triangle.
Definition: vpTriangle.h:55
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:163
double get_j() const
Definition: vpImagePoint.h:210
virtual ~vpTriangle()
Definition: vpTriangle.cpp:83
void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:113
vpTriangle & operator=(const vpTriangle &tri)
Definition: vpTriangle.cpp:91
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88