Fawkes API  Fawkes Development Version
triangle.h
1 
2 /***************************************************************************
3  * triangle.h - triangle related utility methods
4  *
5  * Created: Sat Jul 11 18:04:19 2015
6  * Copyright 2015 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_MATH_TRIANGLE_H_
25 #define __UTILS_MATH_TRIANGLE_H_
26 
27 namespace fawkes {
28 #if 0 /* just to make Emacs auto-indent happy */
29 }
30 #endif
31 
32 /** Calculate triangle area.
33  * @param p0 first point of triangle
34  * @param p1 second point of triangle
35  * @param p2 third point of triangle
36  * @return area of triangle
37  */
38 double
39 triangle_area(const Eigen::Vector2f &p0, const Eigen::Vector2f &p1,
40  const Eigen::Vector2f &p2)
41 {
42  return 1.f/2.f*(-p1[1]*p2[0] + p0[1]*(-p1[0] + p2[0]) + p0[0]*(p1[1] - p2[1]) + p1[0]*p2[1]);
43 }
44 
45 
46 /** Check if a triangle contains a point.
47  * A point is also considered to be contained if it is on the boundary
48  * of the triangle.
49  * @param p0 first point of triangle
50  * @param p1 second point of triangle
51  * @param p2 third point of triangle
52  * @param p point to check with respect to the given triangle
53  * @return true if the point is within or on the triangle boundaries
54  */
55 bool
56 triangle_contains(const Eigen::Vector2f &p0, const Eigen::Vector2f &p1,
57  const Eigen::Vector2f &p2, const Eigen::Vector2f &p)
58 {
59  double area_2 = 2. * triangle_area(p0, p1, p2);
60 
61  double s =
62  1./area_2*(p0[1]*p2[0] - p0[0]*p2[1] + (p2[1] - p0[1])*p[0] + (p0[0] - p2[0])*p[1]);
63  if (s < 0) return false;
64 
65  double t =
66  1./area_2*(p0[0]*p1[1] - p0[1]*p1[0] + (p0[1] - p1[1])*p[0] + (p1[0] - p0[0])*p[1]);
67  if (t < 0) return false;
68 
69  return s+t <= 1.;
70 }
71 
72 } // end namespace fawkes
73 
74 #endif
Fawkes library namespace.
double triangle_area(const Eigen::Vector2f &p0, const Eigen::Vector2f &p1, const Eigen::Vector2f &p2)
Calculate triangle area.
Definition: triangle.h:39
bool triangle_contains(const Eigen::Vector2f &p0, const Eigen::Vector2f &p1, const Eigen::Vector2f &p2, const Eigen::Vector2f &p)
Check if a triangle contains a point.
Definition: triangle.h:56