14 namespace til {
namespace geo
26 template <
typename T >
34 template <
typename T >
40 template <
typename T >
47 template <
typename T >
54 template <
typename T >
62 template <
typename T >
68 template <
typename T >
69 inline T
angle(T x, T y);
74 template <
typename T >
82 template <
typename T >
94 template <
typename TArray >
129 template <
typename TArray >
130 bool triangle2segment(
const TArray & A,
const TArray & B,
const TArray & C, TArray & X, TArray & Y)
144 template <
typename TArray,
typename TResArray >
184 template <
typename TArray,
typename TResArray >
187 triangle_normal(
const TArray & A,
const TArray & B,
const TArray & C, TResArray & result);
193 template <
typename TNormal,
typename TPo
int >
194 inline void normal2D(
const TPoint & A,
const TPoint & B, TNormal & N)
212 template <
typename TArray >
227 prec_type sAB =
dot(P - A, m_N);
228 prec_type s =
dot(C - A, m_N);
231 prec_type sBC =
dot(P - B, m_N);
234 prec_type sCA =
dot(P - C, m_N);
244 template <
typename TPrec,
typename TPo
int >
290 template <
typename TPrec,
typename TPo
int >
303 if ((b[0]-a[0])*(c[1]-a[1]) - (b[1]-a[1])*(c[0]-a[0]) >= 0)
327 template <
typename TArray,
typename TPrecArray >
340 const TArray & A,
const TArray & B,
347 m_N *= 1/norm<prec_type>(m_N);
356 template <
typename TArray,
typename TPrecArray >
365 const TArray & A,
const TArray & B,
374 std::copy(A.begin(), A.end(), output.begin());
378 std::copy(B.begin(), B.end(), output.begin());
392 template <
typename TArray,
typename TOutputArray >
405 const TArray & A,
const TArray & B,
const TArray & C,
406 TOutputArray & output
422 std::size_t j = (i+1)%3;
423 std::size_t k = (i+2)%3;
424 Array2D a; a[0] = A[j]; a[1] = A[k];
425 Array2D b; b[0] = B[j]; b[1] = B[k];
426 Array2D c; c[0] = C[j]; c[1] = C[k];
427 Array2D p; p[0] = p[j]; p[1] = p[k];
431 prec_type sAB =
dot(p - a, n);
432 prec_type s =
dot(c - a, n);
434 prec_type sBC =
dot(p - b, n);
436 prec_type sCA =
dot(p - c, n);
442 std::copy(B.begin(), B.end(), output.begin());
447 std::copy(A.begin(), A.end(), output.begin());
452 m_projline(P, A, B, output);
459 std::copy(C.begin(), C.end(), output.begin());
464 m_projline(P, B, C, output);
469 m_projline(P, C, A, output);
489 template <
typename TArray >
497 const TArray & A1,
const TArray & B1,
498 const TArray & A2,
const TArray & B2
501 normal2D(A1, B1, m_N);
506 normal2D(A2, B2, m_N);
518 template <
typename TArray >
524 const TArray & A1,
const TArray & B1,
const TArray & C1,
525 const TArray & A2,
const TArray & B2,
const TArray & C2
529 if (m_interseg(A1,B1,A2,B2))
return true;
530 if (m_interseg(A1,C1,A2,B2))
return true;
531 if (m_interseg(B1,C1,A2,B2))
return true;
533 if (m_interseg(A1,B1,A2,C2))
return true;
534 if (m_interseg(A1,C1,A2,C2))
return true;
535 if (m_interseg(B1,C1,A2,C2))
return true;
537 if (m_interseg(A1,B1,B2,C2))
return true;
538 if (m_interseg(A1,C1,B2,C2))
return true;
539 if (m_interseg(B1,C1,B2,C2))
return true;
542 if (m_pointInTriangle(A1, A2, B2, C2))
return true;
543 if (m_pointInTriangle(A2, A1, B1, C1))
return true;
559 template <
typename TArray >
574 const TArray & A1,
const TArray & B1,
const TArray & C1,
575 const TArray & A2,
const TArray & B2,
const TArray & C2
578 const prec_type EPSILON = 128 * std::numeric_limits<prec_type>::epsilon();
581 m_N1 *= 1/norm<prec_type>(m_N1);
583 prec_type dA2 =
dot(m_N1, A2-A1);
584 prec_type dB2 =
dot(m_N1, B2-A1);
585 prec_type dC2 =
dot(m_N1, C2-A1);
591 if (sAB2 && sAC2 && sBC2)
return false;
594 m_N2 *= 1/norm<prec_type>(m_N2);
596 prec_type dA1 =
dot(m_N2, A1-A2);
597 prec_type dB1 =
dot(m_N2, B1-A2);
598 prec_type dC1 =
dot(m_N2, C1-A2);
602 if (sAB1 && sAC1 && sBC1)
return false;
604 m_D = cross<prec_type>(m_N1, m_N2);
608 if (
max(D0, D1, D2) < EPSILON)
615 std::size_t j = (i+1)%3;
616 std::size_t k = (i+2)%3;
618 Array2D a1; a1[0] = A1[j]; a1[1] = A1[k];
619 Array2D b1; b1[0] = B1[j]; b1[1] = B1[k];
620 Array2D c1; c1[0] = C1[j]; c1[1] = C1[k];
622 Array2D a2; a2[0] = A2[j]; a2[1] = A2[k];
623 Array2D b2; b2[0] = B2[j]; b2[1] = B2[k];
624 Array2D c2; c2[0] = C2[j]; c2[1] = C2[k];
626 return m_intersectTriangle2D(a1,b1,c1,a2,b2,c2);
648 prec_type a1 = dot<prec_type>(A1, m_D);
649 prec_type b1 = dot<prec_type>(B1, m_D);
650 prec_type c1 = dot<prec_type>(C1, m_D);
651 prec_type a2 = dot<prec_type>(A2, m_D);
652 prec_type b2 = dot<prec_type>(B2, m_D);
653 prec_type c2 = dot<prec_type>(C2, m_D);
656 prec_type emin, emax;
659 emin = line_coord(a1, b1, dA1, dB1);
660 emax = line_coord(a1, c1, dA1, dC1);
664 emin = line_coord(b1, a1, dB1, dA1);
665 emax = line_coord(b1, c1, dB1, dC1);
669 emin = line_coord(c1, a1, dC1, dA1);
670 emax = line_coord(c1, b1, dC1, dB1);
672 if (emin > emax) std::swap(emin, emax);
675 prec_type fmin, fmax;
678 fmin = line_coord(a2, b2, dA2, dB2);
679 fmax = line_coord(a2, c2, dA2, dC2);
683 fmin = line_coord(b2, a2, dB2, dA2);
684 fmax = line_coord(b2, c2, dB2, dC2);
688 fmin = line_coord(c2, a2, dC2, dA2);
689 fmax = line_coord(c2, b2, dC2, dB2);
691 if (fmin > fmax) std::swap(fmin, fmax);
693 if (fmin > emax || fmax < emin)
return false;
720 prec_type line_coord(prec_type a, prec_type b, prec_type da, prec_type db)
721 {
return a + (b-a) * da / (da + db); }
748 template <
typename TPrec,
typename TPo
int >
namespace for template expressions.
Project a point onto a 3D triangle.
A namespace structure collecting geometrical projection algorithms.
const TExpr< expr::ThirdArgument > _3
Placeholder for the third argument.
TPrec norm2(const numeric_array< T, D > &a, prec< TPrec >)
Return the squared euclidean norm of a.
TArray::value_type prec_type
T law_of_cosines(T a, T b, T c)
Returns the cosine of angle A given the lengths of the triangle edges.
Checks if a 2D point lies within the circumcircle of a 2D triangle.
TResArray::value_type prec_type
TPrec dot(const numeric_array< T1, D > &a1, const numeric_array< T2, D > &a2, prec< TPrec >)
Return the dot product of two vectors.
INLINE double norm(const T &a, const T &b)
< Euclidean norm of 2D vector (a, b)
TArray::value_type prec_type
Belongs to package Box Do not include directly, include til/Box.h instead.
bool triangle_normal(const TArray &A, const TArray &B, const TArray &C, TResArray &result)
Compute the normal of a triangle.
T law_of_tangents(T a, T b, T c, T area)
Returns the tangent of angle A given the area and the lengths of the edges of the triangle...
Approximate an ill-conditionned triangle by a segment.
boost::logic::tribool same_sign(T x, T y, T delta=128 *std::numeric_limits< T >::epsilon())
Returns true iff both arguments have same sign, allowing some degree of imprecision.
void spherical2cartesian(T theta, T phi, T rho, T &x, T &y, T &z)
Converts spherical coordinates into cartesian coordinates.
Compute the normal of a triangle.
Check whether two 2D triangles are intersecting or not.
A namespace structure collecting geometrical object intersection algorithms.
This exeception is thrown whenever a reliable computation of the triangle normal cannot be achieved...
numeric_array< T, D > abs(const numeric_array< T, D > &a)
Absolute value, element-wise.
TPrecArray::value_type prec_type
bool triangle2segment(const TArray &A, const TArray &B, const TArray &C, TArray &X, TArray &Y)
Checks if a 2D point lies within a 2D triangle.
TArray::value_type prec_type
bool is_in_circumcircle(const TPoint &p, const TPoint &a, const TPoint &b, const TPoint &c)
Check whether a point lies within the circumcircle of a given triangle.
A namespace structure collecting geometrical object inclusion algorithms.
TImage::value_type max(const TImage &im)
Returns the maximum intensity of the input image.
T angle(T x, T y, T norm)
Returns the angle between (x,y) and the x-axis, with the norm of (x,y) provided.
TArray::value_type prec_type
void copy(const TImage &in, TImage &out)
Copy one image to another.
numeric_array< prec_type, 2 > Array2D
const TExpr< expr::SecondArgument > _2
Placeholder for the second argument.
const TExpr< expr::FirstArgument > _1
Placeholder for the first argument.
void cartesian2spherical(T x, T y, T z, T &theta, T &phi, T &rho)
Converts cartesian coordinates into spherical coordinates.
Check whether two 2D segments are intersecting or not.
Checks if a 2D point lies within the circumcircle of a triangle, whose vertices are given in a counte...
void loop_xxx(expr::TExpr< Expr > expr, TIterator1 start1, const TIterator1 end1, TIterator2 start2, TIterator3 start3)
T herons_formula(T a, T b, T c)
Heron's formula.
T det(const Matrix3< T > &m)
Check whether two triangles are intersecting or not.
TOutputArray::value_type prec_type
A dummy class used to pass a precision type for computations to some functions.
std::size_t min_index(T x0, T x1, T x2)
Returns i0, where X_i0 is the smallest of all Xi's.
TPrecArray::value_type prec_type