lygia
/v1.1.6
/geometry
/triangle
/distanceSq
)returns the closest sq distance to the surface of a triangle
Dependencies:
Use:
<vec3> closestDistanceSq(<Triangle> tri, <vec3> _pos)
#ifndef FNC_TRIANGLE_DISTANCE_SQ
#define FNC_TRIANGLE_DISTANCE_SQ
float distanceSq(Triangle _tri, vec3 _pos) {
// prepare data
vec3 v21 = _tri.b - _tri.a; vec3 p1 = _pos - _tri.a;
vec3 v32 = _tri.c - _tri.b; vec3 p2 = _pos - _tri.b;
vec3 v13 = _tri.a - _tri.c; vec3 p3 = _pos - _tri.c;
vec3 nor = cross( v21, v13 );
return sqrt(
// inside/outside test
(sign( dot(cross(v21,nor),p1)) +
sign( dot(cross(v32,nor),p2)) +
sign( dot(cross(v13,nor),p3)) < 2.0)
?
// 3 edges
min( min(
lengthSq(v21 * saturate( dot(v21,p1)/lengthSq(v21) ) - p1),
lengthSq(v32 * saturate( dot(v32,p2)/lengthSq(v32) ) - p2) ),
lengthSq(v13 * saturate( dot(v13,p3)/lengthSq(v13) ) - p3) )
:
// 1 face
dot(nor,p1)* dot(nor,p1)/lengthSq(nor)
);
}
#endif
Dependencies:
Use:
<float3> closestDistanceSq(<Triangle> tri, <float3> _pos)
#ifndef FNC_TRIANGLE_DISTANCE_SQ
#define FNC_TRIANGLE_DISTANCE_SQ
float distanceSq(Triangle _tri, float3 _pos) {
// prepare data
float3 v21 = _tri.b - _tri.a; float3 p1 = _pos - _tri.a;
float3 v32 = _tri.c - _tri.b; float3 p2 = _pos - _tri.b;
float3 v13 = _tri.a - _tri.c; float3 p3 = _pos - _tri.c;
float3 nor = cross( v21, v13 );
return sqrt(
// inside/outside test
(sign( dot(cross(v21,nor),p1)) +
sign( dot(cross(v32,nor),p2)) +
sign( dot(cross(v13,nor),p3)) < 2.0)
?
// 3 edges
min( min(
lengthSq(v21 * saturate( dot(v21,p1)/lengthSq(v21) ) - p1),
lengthSq(v32 * saturate( dot(v32,p2)/lengthSq(v32) ) - p2) ),
lengthSq(v13 * saturate( dot(v13,p3)/lengthSq(v13) ) - p3) )
:
// 1 face
dot(nor,p1)* dot(nor,p1)/lengthSq(nor)
);
}
#endif
Dependencies:
lygia
/v1.1.6
/geometry
/triangle
/triangle
.glsl
lygia
/v1.1.6
/math
/sign
.glsl
lygia
/v1.1.6
/math
/cross
.glsl
lygia
/v1.1.6
/math
/clamp
.glsl
lygia
/v1.1.6
/math
/lengthSq
.glsl
lygia
/v1.1.6
/math
/operations
.glsl
Use:
<float3> closestDistanceSq(<Triangle> tri, <float3> _pos)
#ifndef FNC_TRIANGLE_DISTANCE_SQ
#define FNC_TRIANGLE_DISTANCE_SQ
inline __host__ __device__ float distanceSq(const Triangle& _tri, float3 _pos) {
// prepare data
float3 v21 = _tri.b - _tri.a; float3 p1 = _pos - _tri.a;
float3 v32 = _tri.c - _tri.b; float3 p2 = _pos - _tri.b;
float3 v13 = _tri.a - _tri.c; float3 p3 = _pos - _tri.c;
float3 nor = cross( v21, v13 );
return sqrt(
// inside/outside test
(sign( dot(cross(v21,nor),p1)) +
sign( dot(cross(v32,nor),p2)) +
sign( dot(cross(v13,nor),p3)) < 2.0)
?
// 3 edges
min( min(
lengthSq(v21 * clamp( dot(v21,p1)/lengthSq(v21), 0.0f, 1.0f) - p1),
lengthSq(v32 * clamp( dot(v32,p2)/lengthSq(v32), 0.0f, 1.0f) - p2) ),
lengthSq(v13 * clamp( dot(v13,p3)/lengthSq(v13), 0.0f, 1.0f) - p3) )
:
// 1 face
dot(nor,p1)* dot(nor,p1)/lengthSq(nor)
);
}
#endif
LYGIA is dual-licensed under the Prosperity License and the Patron License for sponsors and contributors.
Sponsors and contributors are automatically added to the Patron License and they can ignore the any non-commercial rule of the Prosperity Licensed software (please take a look to the exception).
It's also possible to get a permanent comercial license hook to a single and specific version of LYGIA.
Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository