Commit 0113d287 authored by Jan Möbius's avatar Jan Möbius

Merge branch 'stableDistPointTriangle' into 'master'

Stable versions of distPointTriangleSquared and distPointTriangle

added stable versions (for degenerate triangles return the distance to the longest edge) for distPointTriangleSquared and distPointTriangle

See merge request !3
parents 1ca7b894 4fd5c13b
......@@ -41,7 +41,7 @@
/*===========================================================================*\
* *
* $Revision$ *
* $Revision$ *
* $Author$ *
* $Date$ *
* *
......@@ -313,16 +313,16 @@ distPointLineSquared( const Vec& _p,
return d1.sqrnorm();
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template <class Vec>
typename Vec::value_type
distPointTriangleSquared( const Vec& _p,
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint )
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint,
bool _stable)
{
Vec v0v1 = _v1 - _v0;
Vec v0v2 = _v2 - _v0;
......@@ -332,8 +332,23 @@ distPointTriangleSquared( const Vec& _p,
// Check if the triangle is degenerated
if (d < FLT_MIN && d > -FLT_MIN) {
std::cerr << "distPointTriangleSquared: Degenerated triangle !\n";
return -1.0;
if (_stable) {
const double l0 = v0v1.sqrnorm();
const double l1 = v0v2.sqrnorm();
const double l2 = (_v2 - _v1).sqrnorm();
if (l0 > l1 && l0 > l2) {
return distPointLineSquared(_p, _v0, _v1, &_nearestPoint);
}
else if (l1 > l0 && l1 > l2) {
return distPointLineSquared(_p, _v0, _v2, &_nearestPoint);
}
else {
return distPointLineSquared(_p, _v1, _v2, &_nearestPoint);
}
} else {
std::cerr << "distPointTriangleSquared: Degenerated triangle !\n";
return -1.0;
}
}
typename Vec::value_type invD = typename Vec::value_type(1.0) / d;
......@@ -439,10 +454,40 @@ distPointTriangleSquared( const Vec& _p,
return (_nearestPoint - _p).sqrnorm();
}
//-----------------------------------------------------------------------------
template <class Vec>
typename Vec::value_type
distPointTriangleSquared( const Vec& _p,
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint )
{
return distPointTriangleSquared(_p, _v0, _v1, _v2, _nearestPoint, false);
}
//-----------------------------------------------------------------------------
template <class Vec>
typename Vec::value_type
distPointTriangleSquaredStable( const Vec& _p,
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint )
{
return distPointTriangleSquared(_p, _v0, _v1, _v2, _nearestPoint, true);
}
//-----------------------------------------------------------------------------
//
// Modified code of Dave Eberly (www.magic-software.com)
//
......
......@@ -308,6 +308,25 @@ distPointTriangleSquared( const Vec& _p,
const Vec& _v2,
Vec& _nearestPoint );
/** \brief squared distance from point _p to triangle (_v0, _v1, _v2)
*
* In the stable version the distance to the longest edge
* is returned if the triangle is degenerate.
*
* @param _p point to test against triangle
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangle
* @return Computed distance
*/
template <class Vec>
typename Vec::value_type
distPointTriangleSquaredStable( const Vec& _p,
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint );
/// distance from point _p to triangle (_v0, _v1, _v2)
template <class Vec>
typename Vec::value_type
......@@ -318,6 +337,25 @@ distPointTriangle( const Vec& _p,
Vec& _nearestPoint )
{ return sqrt(distPointTriangleSquared(_p, _v0, _v1, _v2, _nearestPoint)); }
/** \brief distance from point _p to triangle (_v0, _v1, _v2)
*
* In the stable version the distance to the longest edge
* is returned if the triangle is degenerate.
*
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangle
* @return Computed distance
*/
template <class Vec>
typename Vec::value_type
distPointTriangleStable( const Vec& _p,
const Vec& _v0,
const Vec& _v1,
const Vec& _v2,
Vec& _nearestPoint )
{ return sqrt(distPointTriangleSquaredStable(_p, _v0, _v1, _v2, _nearestPoint)); }
/** \brief Checks the distance from a point to a plane
*
*
......@@ -542,6 +580,9 @@ int isObtuse(const VectorT& _p0,
/** \brief return squared area of triangle (_v0, _v1, _v2)
*
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangl
*/
template <class Vec>
typename Vec::value_type
......@@ -552,6 +593,9 @@ triangleAreaSquared( const Vec& _v0,
/** \brief return area of triangle (_v0, _v1, _v2)
*
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangl
*/
template <class Vec>
typename Vec::value_type
......@@ -565,6 +609,9 @@ triangleArea( const Vec& _v0,
/** \brief return aspect ratio (length/height) of triangle
*
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangl
*/
template <typename Scalar, int N>
Scalar
......@@ -574,6 +621,9 @@ aspectRatio( const VectorT<Scalar, N>& _v0,
/** \brief return roundness of triangle: 1=equilateral, 0=colinear
*
* @param _v0 First point of triangle
* @param _v1 Second point of triangle
* @param _v2 Third point of triangl
*/
template <typename Scalar, int N>
Scalar
......
......@@ -13,6 +13,7 @@
- increase max supported face size closes #2522
- Geometry
- add fast algorithms for bspline basis function to ACG
- Added more stable dist_point triangle Algorithm Version
- GL
- implicitly bind vbo on upload()
- fix shaders for resolving multisampled textures
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment