Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Triangle.hh
1 #ifndef _TRIANGLE_HH__
2 #define _TRIANGLE_HH__
3 
4 #include <Base/Geometry/HomgPoint2D.hh>
5 
6 
7 namespace BIAS {
8 
9  /**
10  * @brief Represents a 2D triangle and allows some computations with it.
11  *
12  * @author rwulff 04/2010
13  */
14  class BIASGeometryBase_EXPORT Triangle {
15 
16  public:
17 
18  /**
19  * Constructor.
20  */
21  Triangle(const HomgPoint2D &a, const HomgPoint2D &b, const HomgPoint2D &c);
22 
23 
24  /**
25  * @brief Checks if the given point is inside this triangle.
26  *
27  * @param p
28  * the point to check
29  *
30  * @return true iff the given point is inside this triangle
31  */
32  inline bool IsPointInTriangle(const HomgPoint2D &p) {
33  return ArePointsOnSameSide(p, a_, b_, c_)
34  && ArePointsOnSameSide(p, b_, c_, a_)
35  && ArePointsOnSameSide(p, c_, a_, b_);
36  }
37 
38 
39  /**
40  * @brief Returns the barycentric coordinates of the given point with
41  * respect to this triangle.
42  *
43  * @param p
44  * the point
45  * @param lambdaA
46  * the barycentric coordinate for the first corner of this triangle
47  * @param lambdaB
48  * the barycentric coordinate for the second corner of this triangle
49  * @param lambdaC
50  * the barycentric coordinate for the thrid corner of this triangle
51  */
52  void GetBarycentricCoords(const HomgPoint2D &p,
53  double &lambdaA,
54  double &lambdaB,
55  double &lambdaC);
56 
57 
58  /**
59  * @brief Sets the corners of this triangle.
60  */
61  inline void SetCorners(const HomgPoint2D &a,
62  const HomgPoint2D &b,
63  const HomgPoint2D &c) {
64  a_ = a;
65  b_ = b;
66  c_ = c;
67  }
68 
69 
70  /**
71  * @brief Returns the corners of this triangle.
72  */
73  inline void GetCorners(HomgPoint2D &a,
74  HomgPoint2D &b,
75  HomgPoint2D &c) {
76  a = a_;
77  b = b_;
78  c = c_;
79  }
80 
81 
82 
83  private:
84 
85  // the three corners of this triangle
86  BIAS::HomgPoint2D a_, b_, c_;
87 
88 
89  /**
90  * Hidden default constructor.
91  */
92  Triangle() {};
93 
94 
95  /**
96  * @brief Helper method for IsPointInTriangle().
97  *
98  * Checks if p1 and p2 are on the same side of the vector (b - a).
99  */
100  inline bool ArePointsOnSameSide(HomgPoint2D p1, HomgPoint2D p2,
101  HomgPoint2D a, HomgPoint2D b) {
102  const HomgPoint2D cp1 = (b - a).CrossProduct(p1 - a);
103  const HomgPoint2D cp2 = (b - a).CrossProduct(p2 - a);
104 
105  return cp1.ScalarProduct(cp2) >= 0;
106  }
107 
108  }; // end of class
109 } // end of namespace
110 #endif // _TRIANGLE_HH__
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
Represents a 2D triangle and allows some computations with it.
Definition: Triangle.hh:14
void GetCorners(HomgPoint2D &a, HomgPoint2D &b, HomgPoint2D &c)
Returns the corners of this triangle.
Definition: Triangle.hh:73
void SetCorners(const HomgPoint2D &a, const HomgPoint2D &b, const HomgPoint2D &c)
Sets the corners of this triangle.
Definition: Triangle.hh:61
bool IsPointInTriangle(const HomgPoint2D &p)
Checks if the given point is inside this triangle.
Definition: Triangle.hh:32
class BIASGeometryBase_EXPORT HomgPoint2D