Visual Servoing Platform  version 3.6.1 under development (2024-04-18)
vpTriangle.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
36 #include <visp3/core/vpDebug.h>
37 #include <visp3/core/vpTriangle.h>
38 
46  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
47  apex2(), apex3()
48 {
49  init(vpImagePoint(0, 0), vpImagePoint(1, 0), vpImagePoint(0, 1));
50 }
51 
60 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
61  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
62  apex2(), apex3()
63 {
64  init(iP1, iP2, iP3);
65 }
66 
73  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
74  apex2(), apex3()
75 {
76  *this = tri;
77 }
78 
83 
88 {
89  goodTriange = tri.goodTriange;
90  S1 = tri.S1;
91  uvinv00 = tri.uvinv00;
92  uvinv01 = tri.uvinv01;
93  uvinv10 = tri.uvinv10;
94  uvinv11 = tri.uvinv11;
95  ptempo0 = tri.ptempo0;
96  ptempo1 = tri.ptempo1;
97  area = tri.area;
98  apex1 = tri.apex1;
99  apex2 = tri.apex2;
100  apex3 = tri.apex3;
101  return *this;
102 };
103 
112 void vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
113 {
114  init(iP1, iP2, iP3);
115 }
116 
117 void vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
118 {
119  ptempo0 = ptempo1 = 0.;
120  apex1 = iP1;
121  apex2 = iP2;
122  apex3 = iP3;
123 
124  vpMatrix uv(2, 2);
125  vpMatrix uvinv(2, 2);
126 
127  uv[0][0] = iP2.get_i() - iP1.get_i();
128  uv[1][0] = iP3.get_i() - iP1.get_i();
129  uv[0][1] = iP2.get_j() - iP1.get_j();
130  uv[1][1] = iP3.get_j() - iP1.get_j();
131  try {
132  uvinv = uv.inverseByLU();
133  goodTriange = true;
134  }
135  catch (...) {
136  goodTriange = false;
137  std::cout << "Empty triangle" << std::endl;
138  }
139 
140  uvinv00 = uvinv[0][0];
141  uvinv01 = uvinv[0][1];
142  uvinv10 = uvinv[1][0];
143  uvinv11 = uvinv[1][1];
144  S1 = iP1;
145  area = 0.5 * fabs(uv.det());
146 }
147 
159 bool vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
160 {
161  if (!goodTriange)
162  return false;
163 
164  if (threshold < 0)
165  threshold = 0;
166 
167  ptempo0 = iP.get_i() - S1.get_i();
168  ptempo1 = iP.get_j() - S1.get_j();
169 
170  double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
171  double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
172 
173  return (p_ds_uv0 + p_ds_uv1 < 1. + threshold && p_ds_uv0 > -threshold && p_ds_uv1 > -threshold);
174 }
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
double get_j() const
Definition: vpImagePoint.h:125
double get_i() const
Definition: vpImagePoint.h:114
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
Defines a 2D triangle.
Definition: vpTriangle.h:53
vpTriangle & operator=(const vpTriangle &tri)
Definition: vpTriangle.cpp:87
void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:112
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:159
virtual ~vpTriangle()
Definition: vpTriangle.cpp:82