1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/*
* Copyright (C) 2014 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_MATH_TRIANGLE_HH_
#define IGNITION_MATH_TRIANGLE_HH_
#include <set>
#include <ignition/math/Helpers.hh>
#include <ignition/math/Line2.hh>
#include <ignition/math/Vector2.hh>
#include <ignition/math/config.hh>
namespace ignition
{
namespace math
{
inline namespace IGNITION_MATH_VERSION_NAMESPACE
{
/// \class Triangle Triangle.hh ignition/math/Triangle.hh
/// \brief Triangle class and related functions.
template<typename T>
class Triangle
{
/// \brief Default constructor
public: Triangle() = default;
/// \brief Constructor
/// \param[in] _pt1 First point that defines the triangle.
/// \param[in] _pt2 Second point that defines the triangle.
/// \param[in] _pt3 Third point that defines the triangle.
public: Triangle(const math::Vector2<T> &_pt1,
const math::Vector2<T> &_pt2,
const math::Vector2<T> &_pt3)
{
this->Set(_pt1, _pt2, _pt3);
}
/// \brief Set one vertex of the triangle.
/// \param[in] _index Index of the point to set, where
/// 0 == first vertex, 1 == second vertex, and 2 == third vertex.
/// The index is clamped to the range [0, 2].
/// \param[in] _pt Value of the point to set.
public: void Set(const unsigned int _index, const math::Vector2<T> &_pt)
{
this->pts[clamp(_index, 0u, 2u)] = _pt;
}
/// \brief Set all vertices of the triangle.
/// \param[in] _pt1 First point that defines the triangle.
/// \param[in] _pt2 Second point that defines the triangle.
/// \param[in] _pt3 Third point that defines the triangle.
public: void Set(const math::Vector2<T> &_pt1,
const math::Vector2<T> &_pt2,
const math::Vector2<T> &_pt3)
{
this->pts[0] = _pt1;
this->pts[1] = _pt2;
this->pts[2] = _pt3;
}
/// \brief Get whether this triangle is valid, based on triangle
/// inequality: the sum of the lengths of any two sides must be greater
/// than the length of the remaining side.
/// \return True if the triangle inequality holds
public: bool Valid() const
{
T a = this->Side(0).Length();
T b = this->Side(1).Length();
T c = this->Side(2).Length();
return (a+b) > c && (b+c) > a && (c+a) > b;
}
/// \brief Get a line segment for one side of the triangle.
/// \param[in] _index Index of the side to retreive, where
/// 0 == Line2(pt1, pt2),
/// 1 == Line2(pt2, pt3),
/// 2 == Line2(pt3, pt1)
/// The index is clamped to the range [0, 2]
/// \return Line segment of the requested side.
public: Line2<T> Side(const unsigned int _index) const
{
if (_index == 0)
return Line2<T>(this->pts[0], this->pts[1]);
else if (_index == 1)
return Line2<T>(this->pts[1], this->pts[2]);
else
return Line2<T>(this->pts[2], this->pts[0]);
}
/// \brief Check if this triangle completely contains the given line
/// segment.
/// \param[in] _line Line to check.
/// \return True if the line's start and end points are both inside
/// this triangle.
public: bool Contains(const Line2<T> &_line) const
{
return this->Contains(_line[0]) && this->Contains(_line[1]);
}
/// \brief Get whether this triangle contains the given point.
/// \param[in] _pt Point to check.
/// \return True if the point is inside or on the triangle.
public: bool Contains(const math::Vector2<T> &_pt) const
{
// Compute vectors
math::Vector2<T> v0 = this->pts[2] -this->pts[0];
math::Vector2<T> v1 = this->pts[1] -this->pts[0];
math::Vector2<T> v2 = _pt - this->pts[0];
// Compute dot products
double dot00 = v0.Dot(v0);
double dot01 = v0.Dot(v1);
double dot02 = v0.Dot(v2);
double dot11 = v1.Dot(v1);
double dot12 = v1.Dot(v2);
// Compute barycentric coordinates
double invDenom = 1.0 / (dot00 * dot11 - dot01 * dot01);
double u = (dot11 * dot02 - dot01 * dot12) * invDenom;
double v = (dot00 * dot12 - dot01 * dot02) * invDenom;
// Check if point is in triangle
return (u >= 0) && (v >= 0) && (u + v <= 1);
}
/// \brief Get whether the given line intersects this triangle.
/// \param[in] _line Line to check.
/// \param[out] _ipt1 Return value of the first intersection point,
/// only valid if the return value of the function is true.
/// \param[out] _ipt2 Return value of the second intersection point,
/// only valid if the return value of the function is true.
/// \return True if the given line intersects this triangle.
public: bool Intersects(const Line2<T> &_line,
math::Vector2<T> &_ipt1,
math::Vector2<T> &_ipt2) const
{
if (this->Contains(_line))
{
_ipt1 = _line[0];
_ipt2 = _line[1];
return true;
}
Line2<T> line1(this->pts[0], this->pts[1]);
Line2<T> line2(this->pts[1], this->pts[2]);
Line2<T> line3(this->pts[2], this->pts[0]);
math::Vector2<T> pt;
std::set<math::Vector2<T> > points;
if (line1.Intersect(_line, pt))
points.insert(pt);
if (line2.Intersect(_line, pt))
points.insert(pt);
if (line3.Intersect(_line, pt))
points.insert(pt);
if (points.empty())
{
return false;
}
else if (points.size() == 1)
{
auto iter = points.begin();
_ipt1 = *iter;
if (this->Contains(_line[0]))
_ipt2 = _line[0];
else
{
_ipt2 = _line[1];
}
}
else
{
auto iter = points.begin();
_ipt1 = *(iter++);
_ipt2 = *iter;
}
return true;
}
/// \brief Get the length of the triangle's perimeter.
/// \return Sum of the triangle's line segments.
public: T Perimeter() const
{
return this->Side(0).Length() + this->Side(1).Length() +
this->Side(2).Length();
}
/// \brief Get the area of this triangle.
/// \return Triangle's area.
public: double Area() const
{
double s = this->Perimeter() / 2.0;
T a = this->Side(0).Length();
T b = this->Side(1).Length();
T c = this->Side(2).Length();
// Heron's formula
// http://en.wikipedia.org/wiki/Heron%27s_formula
return sqrt(s * (s-a) * (s-b) * (s-c));
}
/// \brief Get one of points that define the triangle.
/// \param[in] _index The index, where 0 == first vertex,
/// 1 == second vertex, and 2 == third vertex.
/// The index is clamped to the range [0, 2]
/// \return The point specified by _index.
public: math::Vector2<T> operator[](const size_t _index) const
{
return this->pts[clamp(_index, IGN_ZERO_SIZE_T, IGN_TWO_SIZE_T)];
}
/// The points of the triangle
private: math::Vector2<T> pts[3];
};
/// Integer specialization of the Triangle class.
typedef Triangle<int> Trianglei;
/// Double specialization of the Triangle class.
typedef Triangle<double> Triangled;
/// Float specialization of the Triangle class.
typedef Triangle<float> Trianglef;
}
}
}
#endif
|