ViSP  2.8.0
vpTriangle.cpp
1 /****************************************************************************
2  *
3  * $Id: vpTriangle.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 {
53  goodTriange = true;
54  init (vpImagePoint(0,0),vpImagePoint(1,0),vpImagePoint(0,1));
55 }
56 
64 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
65 {
66  goodTriange = true;
67 
68  init(iP1,iP2,iP3);
69 }
70 
77 {
78  goodTriange = tri.goodTriange;
79  S1 = tri.S1;
80  uvinv00 = tri.uvinv00;
81  uvinv01 = tri.uvinv01;
82  uvinv10 = tri.uvinv10;
83  uvinv11 = tri.uvinv11;
84  ptempo0 = tri.ptempo0;
85  ptempo1 = tri.ptempo1;
86  area = tri.area;
87 }
88 
93 {
94 }
95 
99 vpTriangle &
101 {
102  goodTriange = tri.goodTriange;
103  S1 = tri.S1;
104  uvinv00 = tri.uvinv00;
105  uvinv01 = tri.uvinv01;
106  uvinv10 = tri.uvinv10;
107  uvinv11 = tri.uvinv11;
108  ptempo0 = tri.ptempo0;
109  ptempo1 = tri.ptempo1;
110  area = tri.area;
111  return *this;
112 };
113 
121 void
122 vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
123 {
124  init(iP1,iP2,iP3);
125 }
126 
127 
128 void
129 vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
130 {
131  apex1 = iP1;
132  apex2 = iP2;
133  apex3 = iP3;
134 
135  vpMatrix uv(2,2);
136  vpMatrix uvinv(2,2);
137 
138  uv[0][0] = iP2.get_i() - iP1.get_i();
139  uv[1][0] = iP3.get_i() - iP1.get_i();
140  uv[0][1] = iP2.get_j() - iP1.get_j();
141  uv[1][1] = iP3.get_j() - iP1.get_j();
142  try
143  {
144  uvinv=uv.inverseByLU();
145  goodTriange = true;
146  }
147  catch(...)
148  {
149  goodTriange = false;
150  std::cout<<"Empty triangle"<<std::endl;
151  }
152 
153  uvinv00=uvinv[0][0];
154  uvinv01=uvinv[0][1];
155  uvinv10=uvinv[1][0];
156  uvinv11=uvinv[1][1];
157  S1 = iP1;
158  area = 0.5 * fabs(uv.det());
159 }
160 
161 
170 bool
171 vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
172 {
173  if(!goodTriange)
174  return false;
175 
176  if(threshold < 0)
177  threshold = 0;
178 
179  ptempo0 = iP.get_i() - S1.get_i();
180  ptempo1 = iP.get_j() - S1.get_j();
181 
182  double p_ds_uv0=ptempo0*uvinv00+ptempo1*uvinv10;
183  double p_ds_uv1=ptempo0*uvinv01+ptempo1*uvinv11;
184 
185  return (p_ds_uv0+p_ds_uv1<1.+threshold && p_ds_uv0>-threshold && p_ds_uv1>-threshold);
186 }
187 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
double get_i() const
Definition: vpImagePoint.h:181
Defines a 2D triangle.
Definition: vpTriangle.h:58
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:171
double get_j() const
Definition: vpImagePoint.h:192
virtual ~vpTriangle()
Definition: vpTriangle.cpp:92
void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:122
vpTriangle & operator=(const vpTriangle &tri)
Definition: vpTriangle.cpp:100
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92