53 #ifndef GEO_ALGORITHMS_HH
54 #define GEO_ALGORITHMS_HH
60 #include <ACG/Math/VectorT.hh>
64 #include "../Math/Matrix3x3T.hh"
76 template<
typename Scalar>
86 template<
typename Scalar>
94 return circumCenter(_v0, _v1, _v2, _v3, cc) ? (cc-_v0).sqrnorm() : FLT_MAX;
99 template<
typename Scalar>
121 template<
typename Scalar>
142 template<
typename Scalar>
148 bool _degree =
true);
159 template <
typename Scalar>
180 template<
typename Vec>
187 typename Vec::value_type& _t,
188 typename Vec::value_type& _u,
189 typename Vec::value_type& _v );
204 template<
typename Vec>
210 typename Vec::value_type& _t0,
211 typename Vec::value_type& _t1 );
217 template<
typename Scalar>
224 return (d1[0]*d2[1]-d1[1]*d2[0]);
229 template<
typename Scalar>
240 template<
typename Scalar>
251 template<
typename Scalar>
276 typename Vec::value_type
296 typename Vec::value_type
306 typename Vec::value_type
307 distPointTriangleSquared(
const Vec& _p,
311 Vec& _nearestPoint );
325 typename Vec::value_type
330 Vec& _nearestPoint );
334 typename Vec::value_type
340 {
return sqrt(distPointTriangleSquared(_p, _v0, _v1, _v2, _nearestPoint)); }
353 typename Vec::value_type
369 template <
typename VectorT ,
typename ValueT >
385 template<
typename Scalar>
393 bool _fastApprox=
false );
397 template<
typename Scalar>
425 template <
typename VectorT >
437 template <
typename VectorT >
455 template<
typename Scalar>
466 template<
typename Scalar>
475 return ((bary[0]>0.0) && (bary[1]>0.0) && (bary[2]>0.0));
477 std::cerr <<
"point in triangle error\n";
482 template<
typename Scalar>
498 template<
typename Scalar>
508 template<
typename Scalar>
518 template<
typename Scalar>
526 template<
typename Scalar>
537 template<
typename Scalar>
546 template<
typename Scalar>
554 template<
typename Scalar>
570 template<
class VectorT>
590 typename Vec::value_type
603 typename Vec::value_type
618 template <
typename Scalar,
int N>
630 template <
typename Scalar,
int N>
638 template<
typename Vector>
640 const auto delta = ((p2-p1)|(x-p1)) / (p2-p1).sqrnorm();
645 }
else if (delta >= 1) {
648 }
else if (delta != delta) {
650 return (x-p1).sqrnorm() < (x-p2).sqrnorm() ? p1 : p2;
653 return (1 - delta) * p1 + delta * p2;
657 template<
typename Vector>
659 constexpr
double thresh = 1e-8;
661 const auto n = ((b - a) % (c - a));
663 if ((b-a).sqrnorm() < thresh || (c-a).sqrnorm() < thresh || n.sqrnorm() < thresh) {
666 std::array<ACG::Vec3d, 2> max_segment = {a, b};
667 double max_len = (b-a).sqrnorm();
668 if ((c-a).sqrnorm() > max_len)
669 max_segment = {a, c};
670 if ((c-b).sqrnorm() > max_len)
671 max_segment = {b, c};
674 return closestPointLineSegment(p, max_segment[0], max_segment[1]);
677 const auto abd = Matrix3x3d::fromColumns(a-c, b-c, n).inverse() * (p - c);
678 const bool alpha = (abd[0] >= 0.0),
679 beta = (abd[1] >= 0.0),
680 gamma = (1.0-abd[0]-abd[1] >= 0.0);
682 if (alpha && beta && gamma) {
685 return abd[0] * a + abd[1] * b + (1.0 - abd[0] - abd[1]) * c;
689 return closestPointLineSegment(p, b, c);
693 return closestPointLineSegment(p, a, c);
697 return closestPointLineSegment(p, a, b);
699 throw std::logic_error(
"This cannot happen.");
707 #if defined(INCLUDE_TEMPLATES) && !defined(GEO_ALGORITHMS_C)
708 #define GEO_ALGORITHMS_TEMPLATES
709 #include "Algorithms.cc"
712 #endif // GEO_ALGORITHMS_HH defined
Scalar minRadiusSquared(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return squared radius of min. enclosing circle of triangle (_v0,_v1,_v2)
Scalar minRadius(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return radius of min. enclosing circle of triangle (_v0,_v1,_v2)
Scalar circumRadius(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, const VectorT< Scalar, 3 > &_v3)
return radius of circumcircle of tetrahedron (_v0,_v1,_v2,_v3)
Namespace providing different geometric functions concerning angles.
Scalar roundness(const VectorT< Scalar, N > &_v0, const VectorT< Scalar, N > &_v1, const VectorT< Scalar, N > &_v2)
return roundness of triangle: 1=equilateral, 0=colinear
Vec::value_type distPointTriangleSquaredStable(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
squared distance from point _p to triangle (_v0, _v1, _v2)
bool triangleIntersection(const Vec &_o, const Vec &_dir, const Vec &_v0, const Vec &_v1, const Vec &_v2, typename Vec::value_type &_t, typename Vec::value_type &_u, typename Vec::value_type &_v)
Intersect a ray and a triangle.
bool edgeConvexPolygonIntersection(std::vector< VectorT< Scalar, 3 > > _polygon_points, VectorT< Scalar, 3 > _v0, VectorT< Scalar, 3 > _v1, VectorT< Scalar, 3 > &_result)
Get intersection point of a ray and a convex polygon.
Vec::value_type triangleAreaSquared(const Vec &_v0, const Vec &_v1, const Vec &_v2)
return squared area of triangle (_v0, _v1, _v2)
VectorT projectToPlane(const VectorT &_porigin, const VectorT &_pnormal, const VectorT &_point)
projects a point to a plane
bool circumCenter(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, VectorT< Scalar, 3 > &_result)
return circumcenter of triangle (_v0,_v1,_v2)
Scalar circumRadiusSquared(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2)
return squared radius of circumcircle of triangle (_v0,_v1,_v2)
bool isCW(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2)
are 3 vertices in clockwise order? in 2D
Scalar distLineLineSquared(const VectorT< Scalar, 3 > &_v00, const VectorT< Scalar, 3 > &_v01, const VectorT< Scalar, 3 > &_v10, const VectorT< Scalar, 3 > &_v11, VectorT< Scalar, 3 > *_min_v0, VectorT< Scalar, 3 > *_min_v1, bool _fastApprox)
squared distance of lines (_v00, _v01) and (_v10, _v11)
bool rotationOfTwoVectors(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, VectorT< Scalar, 3 > &_axis, Scalar &_angle, bool _degree)
Get rotation axis and signed angle of rotation between two vectors.
bool minSphere(const VectorT< Scalar, 3 > &_v0, const VectorT< Scalar, 3 > &_v1, const VectorT< Scalar, 3 > &_v2, VectorT< Scalar, 3 > &_center, Scalar &_radius)
construct min. enclosing sphere
VectorT< Scalar, 3 > perpendicular(const VectorT< Scalar, 3 > &v)
find a vector that's perpendicular to _v
Vec::value_type distPointTriangle(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
distance from point _p to triangle (_v0, _v1, _v2)
int isObtuse(const VectorT &_p0, const VectorT &_p1, const VectorT &_p2)
Scalar pointLineOrientation(const VectorT< Scalar, 2 > &_p, const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1)
orientation of point _p w.r.t. line through _v0,_v1 in 2D
ValueT distPointPlane(const VectorT &_porigin, const VectorT &_pnormal, const VectorT &_point)
Checks the distance from a point to a plane.
Scalar aspectRatio(const VectorT< Scalar, N > &_v0, const VectorT< Scalar, N > &_v1, const VectorT< Scalar, N > &_v2)
return aspect ratio (length/height) of triangle
Vec::value_type triangleArea(const Vec &_v0, const Vec &_v1, const Vec &_v2)
return area of triangle (_v0, _v1, _v2)
Scalar distLineLine(const VectorT< Scalar, 3 > &_v00, const VectorT< Scalar, 3 > &_v01, const VectorT< Scalar, 3 > &_v10, const VectorT< Scalar, 3 > &_v11, VectorT< Scalar, 3 > *_min_v0=0, VectorT< Scalar, 3 > *_min_v1=0)
distance of lines (_v00, _v01) and (_v10, _v11)
bool axisAlignedBBIntersection(const Vec &_o, const Vec &_dir, const Vec &_bbmin, const Vec &_bbmax, typename Vec::value_type &tmin, typename Vec::value_type &tmax)
Intersect a ray and an axis aligned bounding box.
Vec::value_type distPointLine(const Vec &_p, const Vec &_v0, const Vec &_v1, Vec *_min_v=0)
Compute distance from point to line segment.
bool isInTriangle(const VectorT< Scalar, 2 > &_p, const VectorT< Scalar, 2 > &_u, const VectorT< Scalar, 2 > &_v, const VectorT< Scalar, 2 > &_w)
is point _p in triangle (_v0,_v1,_v2) in 2D
Vec::value_type distPointLineSquared(const Vec &_p, const Vec &_v0, const Vec &_v1, Vec *_min_v)
squared distance from point _p to line segment (_v0,_v1)
bool baryCoord(const VectorT< Scalar, 3 > &_p, const VectorT< Scalar, 3 > &_u, const VectorT< Scalar, 3 > &_v, const VectorT< Scalar, 3 > &_w, VectorT< Scalar, 3 > &_result)
bool lineIntersection(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2, const VectorT< Scalar, 2 > &_v3, Scalar &_t1, Scalar &_t2)
intersect two line segments (_v0,_v1) and (_v2,_v3)
Vec::value_type distPointTriangleStable(const Vec &_p, const Vec &_v0, const Vec &_v1, const Vec &_v2, Vec &_nearestPoint)
distance from point _p to triangle (_v0, _v1, _v2)
bool isCCW(const VectorT< Scalar, 2 > &_v0, const VectorT< Scalar, 2 > &_v1, const VectorT< Scalar, 2 > &_v2)
are 3 vertices in counterclockwise order? in 2D
VectorT projectToEdge(const VectorT &_start, const VectorT &_end, const VectorT &_point)