LYGIA Shader Library

barycentric (lygia/v1.1.6/geometry/triangle/barycentric)

returns the centroid of a triangle

Dependencies:

Use:

<vec3> centroid(<Triangle> tri)

Check it on Github



#ifndef FNC_TRIANGLE_BARYCENTRIC
#define FNC_TRIANGLE_BARYCENTRIC

vec3 barycentric(vec3 _a, vec3 _b, vec3 _c ) {
    /* Derived from the book "Real-Time Collision Detection"
     * by Christer Ericson published by Morgan Kaufmann in 2005 */
    float daa = dot(_a, _a);
    float dab = dot(_a, _b);
    float dbb = dot(_b, _b);
    float dca = dot(_c, _a);
    float dcb = dot(_c, _b);
    float denom = daa * dbb - dab * dab;
    float y = (dbb * dca - dab * dcb) / denom;
    float z = (daa * dcb - dab * dca) / denom;
    return make_vec3( 1.0f - y - z, y, z);
}

vec3 barycentric(Triangle _tri) { return barycentric(_tri.a, _tri.b, _tri.c); }

vec3 barycentric(Triangle _tri, vec3 _pos) {
    vec3 f0 = _tri.a - _pos;
    vec3 f1 = _tri.b - _pos;
    vec3 f2 = _tri.c - _pos;

    return make_vec3( length(cross(f1, f2)),                      // p1's triangle area / a
                        length(cross(f2, f0)),                      // p2's triangle area / a 
                        length(cross(f0, f1)) ) / area(_tri) ;      // p3's triangle area / a
}

#endif

Dependencies:

Use:

<float3> centroid(<Triangle> tri)

Check it on Github



#ifndef FNC_TRIANGLE_BARYCENTRIC
#define FNC_TRIANGLE_BARYCENTRIC

float3 barycentric(float3 _a, float3 _b, float3 _c ) {
    /* Derived from the book "Real-Time Collision Detection"
     * by Christer Ericson published by Morgan Kaufmann in 2005 */
    float daa = dot(_a, _a);
    float dab = dot(_a, _b);
    float dbb = dot(_b, _b);
    float dca = dot(_c, _a);
    float dcb = dot(_c, _b);
    float denom = daa * dbb - dab * dab;
    float y = (dbb * dca - dab * dcb) / denom;
    float z = (daa * dcb - dab * dca) / denom;
    return make_float3( 1.0f - y - z, y, z);
}

float3 barycentric(Triangle _tri) { return barycentric(_tri.a, _tri.b, _tri.c); }

float3 barycentric(Triangle _tri, float3 _pos) {
    float3 f0 = _tri.a - _pos;
    float3 f1 = _tri.b - _pos;
    float3 f2 = _tri.c - _pos;

    return make_float3( length(cross(f1, f2)),                      // p1's triangle area / a
                        length(cross(f2, f0)),                      // p2's triangle area / a 
                        length(cross(f0, f1)) ) / area(_tri) ;      // p3's triangle area / a
}

#endif

Dependencies:

Use:

<float3> centroid(<Triangle> tri)

Check it on Github



#ifndef FNC_TRIANGLE_BARYCENTRIC
#define FNC_TRIANGLE_BARYCENTRIC

inline __host__ __device__ float3 barycentric(const float3& _a, const float3& _b, const float3& _c ) {
    /* Derived from the book "Real-Time Collision Detection"
     * by Christer Ericson published by Morgan Kaufmann in 2005 */
    float daa = dot(_a, _a);
    float dab = dot(_a, _b);
    float dbb = dot(_b, _b);
    float dca = dot(_c, _a);
    float dcb = dot(_c, _b);
    float denom = daa * dbb - dab * dab;
    float y = (dbb * dca - dab * dcb) / denom;
    float z = (daa * dcb - dab * dca) / denom;
    return make_float3( 1.0f - y - z, y, z);
}

inline __host__ __device__ float3 barycentric(const Triangle& _tri) { return barycentric(_tri.a, _tri.b, _tri.c); }

inline __host__ __device__ float3 barycentric(const Triangle& _tri, const float3& _pos) {
    const float3 f0 = _tri.a - _pos;
    const float3 f1 = _tri.b - _pos;
    const float3 f2 = _tri.c - _pos;

    return make_float3( length(cross(f1, f2)),                      // p1's triangle area / a
                        length(cross(f2, f0)),                      // p2's triangle area / a 
                        length(cross(f0, f1)) ) / area(_tri) ;      // p3's triangle area / a
}

#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.

Get the latest news and releases

Sign up for the news letter bellow, joing the LYGIA's channel on Discord or follow the Github repository