## closestPoint (`lygia`/`geometry`/`triangle`/`closestPoint`)

returns the closest point on the surface of a triangle

Dependencies:

Use:

``````<vec3> closestDistance(<Triangle> tri, <vec3> _pos)
``````

Check it on Github

``````

#ifndef FNC_TRIANGLE_CLOSEST_POINT
#define FNC_TRIANGLE_CLOSEST_POINT

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
vec3 closestPoint(Triangle _tri, vec3 _triNormal, vec3 _pos) {
vec3 ab = _tri.b - _tri.a;
vec3 ac = _tri.c - _tri.a;
vec3 normal = _triNormal;

vec3 p = _pos - dot(_triNormal, _pos - _tri.a) * _triNormal;
vec3 ap = p - _tri.a;

vec3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
vec3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
vec3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
vec3 closestPoint(Triangle _tri, vec3 _pos) {
vec3 ab = _tri.b - _tri.a;
vec3 ac = _tri.c - _tri.a;
vec3 normal = normalize( cross(ac,ab) );

vec3 p = _pos - dot(normal, _pos - _tri.a) * normal;
vec3 ap = p - _tri.a;

vec3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
vec3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
vec3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

#endif
``````

Dependencies:

Use:

``````<float3> closestDistance(<Triangle> tri, <float3> _pos)
``````

Check it on Github

``````

#ifndef FNC_TRIANGLE_CLOSEST_POINT
#define FNC_TRIANGLE_CLOSEST_POINT

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
float3 closestPoint(Triangle _tri, float3 _triNormal, float3 _pos) {
float3 ab = _tri.b - _tri.a;
float3 ac = _tri.c - _tri.a;
float3 normal = _triNormal;

float3 p = _pos - dot(_triNormal, _pos - _tri.a) * _triNormal;
float3 ap = p - _tri.a;

float3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
float3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
float3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
float3 closestPoint(Triangle _tri, float3 _pos) {
float3 ab = _tri.b - _tri.a;
float3 ac = _tri.c - _tri.a;
float3 normal = normalize( cross(ac,ab) );

float3 p = _pos - dot(normal, _pos - _tri.a) * normal;
float3 ap = p - _tri.a;

float3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
float3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
float3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

#endif
``````

Dependencies:

Use:

``````<float3> closestDistance(<Triangle> tri, <float3> _pos)
``````

Check it on Github

``````

#ifndef FNC_TRIANGLE_CLOSEST_POINT
#define FNC_TRIANGLE_CLOSEST_POINT

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
inline __host__ __device__ float3 closestPoint(const Triangle& _tri, const float3& _triNormal, const float3& _pos) {
float3 ab = _tri.b - _tri.a;
float3 ac = _tri.c - _tri.a;
float3 p = _pos - dot(_triNormal, _pos - _tri.a) * _triNormal;
float3 ap = p - _tri.a;

float3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
float3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
float3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

// https://github.com/nmoehrle/libacc/blob/master/primitives.h#L71
inline __host__ __device__ float3 closestPoint(const Triangle& _tri, const float3& _pos) {
float3 ab = _tri.b - _tri.a;
float3 ac = _tri.c - _tri.a;
float3 normal = normalize( cross(ac,ab) );

float3 p = _pos - dot(normal, _pos - _tri.a) * normal;
float3 ap = p - _tri.a;

float3 bcoords = barycentric(ab, ac, ap);

if (bcoords.x < 0.0f) {
float3 bc = _tri.c - _tri.b;
float n = length( bc );
float t = max(0.0f, min( dot(bc, p - _tri.b)/n, n));
return _tri.b + t / n * bc;
}

if (bcoords.y < 0.0f) {
float3 ca = _tri.a - _tri.c;
float n = length( ca );
float t = max(0.0f, min( dot(ca, p - _tri.c)/n, n));
return _tri.c + t / n * ca;
}

if (bcoords.z < 0.0f) {
float n = length( ab );
float t = max(0.0f, min( dot(ab, p - _tri.a)/n, n));
return _tri.a + t / n * ab;
}

return (_tri.a * bcoords.x + _tri.b * bcoords.y + _tri.c * bcoords.z);
}

#endif
``````