Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 
39 BEGIN_VISP_NAMESPACE
47  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
48  apex2(), apex3()
49 {
50  init(vpImagePoint(0, 0), vpImagePoint(1, 0), vpImagePoint(0, 1));
51 }
52 
61 vpTriangle::vpTriangle(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
62  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
63  apex2(), apex3()
64 {
65  init(iP1, iP2, iP3);
66 }
67 
74  : goodTriange(true), S1(), uvinv00(0), uvinv01(0), uvinv10(0), uvinv11(0), ptempo0(0), ptempo1(0), area(0), apex1(),
75  apex2(), apex3()
76 {
77  *this = tri;
78 }
79 
84 
89 {
90  goodTriange = tri.goodTriange;
91  S1 = tri.S1;
92  uvinv00 = tri.uvinv00;
93  uvinv01 = tri.uvinv01;
94  uvinv10 = tri.uvinv10;
95  uvinv11 = tri.uvinv11;
96  ptempo0 = tri.ptempo0;
97  ptempo1 = tri.ptempo1;
98  area = tri.area;
99  apex1 = tri.apex1;
100  apex2 = tri.apex2;
101  apex3 = tri.apex3;
102  return *this;
103 };
104 
114 {
115  init(iP1, iP2, iP3);
116  return *this;
117 }
118 
119 void vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
120 {
121  ptempo0 = ptempo1 = 0.;
122  apex1 = iP1;
123  apex2 = iP2;
124  apex3 = iP3;
125 
126  vpMatrix uv(2, 2);
127  vpMatrix uvinv(2, 2);
128 
129  uv[0][0] = iP2.get_i() - iP1.get_i();
130  uv[1][0] = iP3.get_i() - iP1.get_i();
131  uv[0][1] = iP2.get_j() - iP1.get_j();
132  uv[1][1] = iP3.get_j() - iP1.get_j();
133  try {
134  uvinv = uv.inverseByLU();
135  goodTriange = true;
136  }
137  catch (...) {
138  goodTriange = false;
139  std::cout << "Empty triangle" << std::endl;
140  }
141 
142  uvinv00 = uvinv[0][0];
143  uvinv01 = uvinv[0][1];
144  uvinv10 = uvinv[1][0];
145  uvinv11 = uvinv[1][1];
146  S1 = iP1;
147  area = 0.5 * fabs(uv.det());
148 }
149 
161 bool vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
162 {
163  if (!goodTriange)
164  return false;
165 
166  if (threshold < 0)
167  threshold = 0;
168 
169  ptempo0 = iP.get_i() - S1.get_i();
170  ptempo1 = iP.get_j() - S1.get_j();
171 
172  double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
173  double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
174 
175  return (p_ds_uv0 + p_ds_uv1 < 1. + threshold && p_ds_uv0 > -threshold && p_ds_uv1 > -threshold);
176 }
177 END_VISP_NAMESPACE
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:169
Defines a 2D triangle.
Definition: vpTriangle.h:54
vpTriangle & operator=(const vpTriangle &tri)
Definition: vpTriangle.cpp:88
vpTriangle & buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:113
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:161
virtual ~vpTriangle()
Definition: vpTriangle.cpp:83