Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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 
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 
105 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
115 void vpTriangle::buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
116 {
117  build(iP1, iP2, iP3);
118 }
119 #endif
120 
130 {
131  init(iP1, iP2, iP3);
132  return *this;
133 }
134 
135 void vpTriangle::init(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
136 {
137  ptempo0 = ptempo1 = 0.;
138  apex1 = iP1;
139  apex2 = iP2;
140  apex3 = iP3;
141 
142  vpMatrix uv(2, 2);
143  vpMatrix uvinv(2, 2);
144 
145  uv[0][0] = iP2.get_i() - iP1.get_i();
146  uv[1][0] = iP3.get_i() - iP1.get_i();
147  uv[0][1] = iP2.get_j() - iP1.get_j();
148  uv[1][1] = iP3.get_j() - iP1.get_j();
149  try {
150  uvinv = uv.inverseByLU();
151  goodTriange = true;
152  }
153  catch (...) {
154  goodTriange = false;
155  std::cout << "Empty triangle" << std::endl;
156  }
157 
158  uvinv00 = uvinv[0][0];
159  uvinv01 = uvinv[0][1];
160  uvinv10 = uvinv[1][0];
161  uvinv11 = uvinv[1][1];
162  S1 = iP1;
163  area = 0.5 * fabs(uv.det());
164 }
165 
177 bool vpTriangle::inTriangle(const vpImagePoint &iP, double threshold)
178 {
179  if (!goodTriange)
180  return false;
181 
182  if (threshold < 0)
183  threshold = 0;
184 
185  ptempo0 = iP.get_i() - S1.get_i();
186  ptempo1 = iP.get_j() - S1.get_j();
187 
188  double p_ds_uv0 = ptempo0 * uvinv00 + ptempo1 * uvinv10;
189  double p_ds_uv1 = ptempo0 * uvinv01 + ptempo1 * uvinv11;
190 
191  return (p_ds_uv0 + p_ds_uv1 < 1. + threshold && p_ds_uv0 > -threshold && p_ds_uv1 > -threshold);
192 }
193 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
VP_DEPRECATED void buildFrom(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:115
bool inTriangle(const vpImagePoint &iP, double threshold=0.00001)
Definition: vpTriangle.cpp:177
vpTriangle & build(const vpImagePoint &iP1, const vpImagePoint &iP2, const vpImagePoint &iP3)
Definition: vpTriangle.cpp:129
virtual ~vpTriangle()
Definition: vpTriangle.cpp:83