Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpTriangle.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Defines a 2D triangle.
33  *
34  * Author:
35  * Amaury Dame
36  * Nicolas Melchior
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpDebug.h>
41 #include <visp3/core/vpTriangle.h>
42 
50  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
51  apex2(), apex3()
52 {
53  init(vpImagePoint(0, 0), vpImagePoint(1, 0), vpImagePoint(0, 1));
54 }
55 
64 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
65  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
66  apex2(), apex3()
67 {
68  init(iP1, iP2, iP3);
69 }
70 
77  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
78  apex2(), apex3()
79 {
80  *this = tri;
81 }
82 
87 
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  apex1 = tri.apex1;
103  apex2 = tri.apex2;
104  apex3 = tri.apex3;
105  return *this;
106 };
107 
116 void vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
117 {
118  init(iP1, iP2, iP3);
119 }
120 
121 void vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
122 {
123  ptempo0 = ptempo1 = 0.;
124  apex1 = iP1;
125  apex2 = iP2;
126  apex3 = iP3;
127 
128  vpMatrix uv(2, 2);
129  vpMatrix uvinv(2, 2);
130 
131  uv[0][0] = iP2.get_i() - iP1.get_i();
132  uv[1][0] = iP3.get_i() - iP1.get_i();
133  uv[0][1] = iP2.get_j() - iP1.get_j();
134  uv[1][1] = iP3.get_j() - iP1.get_j();
135  try {
136  uvinv = uv.inverseByLU();
137  goodTriange = true;
138  } catch (...) {
139  goodTriange = false;
140  std::cout << "Empty triangle" << std::endl;
141  }
142 
143  uvinv00 = uvinv[0][0];
144  uvinv01 = uvinv[0][1];
145  uvinv10 = uvinv[1][0];
146  uvinv11 = uvinv[1][1];
147  S1 = iP1;
148  area = 0.5 * fabs(uv.det());
149 }
150 
162 bool vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
163 {
164  if (!goodTriange)
165  return false;
166 
167  if (threshold < 0)
168  threshold = 0;
169 
170  ptempo0 = iP.get_i() - S1.get_i();
171  ptempo1 = iP.get_j() - S1.get_j();
172 
173  double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
174  double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
175 
176  return (p_ds_uv0 + p_ds_uv1 < 1. + threshold && p_ds_uv0 > -threshold && p_ds_uv1 > -threshold);
177 }
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
double get_i() const
Definition: vpImagePoint.h:204
Defines a 2D triangle.
Definition: vpTriangle.h:58
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:162
double get_j() const
Definition: vpImagePoint.h:215
virtual ~vpTriangle()
Definition: vpTriangle.cpp:86
double det(vpDetMethod method=LU_DECOMPOSITION) const
Definition: vpMatrix.cpp:4906
vpMatrix inverseByLU() const
void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:116
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