LYGIA Shader Library

contain (lygia/geometry/triangle/contain)

Does the position lie within the triangle

Dependencies:

Use:

<vec3> contain(<Triangle> tri, <vec3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_CONTAIN
#define FNC_TRIANGLE_CONTAIN

bool contain(Triangle _tri, vec3 _pos) { 
    // Move the triangle so that the point becomes the
    // triangles origin
    vec3 local_a = _tri.a - _pos;
    vec3 local_b = _tri.b - _pos;
    vec3 local_c = _tri.c - _pos;

    // The point should be moved too, so they are both
    // relative, but because we don't use p in the
    // equation anymore, we don't need it!
    // p -= p;

    // Compute the normal vectors for triangles:
    // u = normal of PBC
    // v = normal of PCA
    // w = normal of PAB

    vec3 u = cross(local_b, local_c);
    vec3 v = cross(local_c, local_a);
    vec3 w = cross(local_a, local_b);

    // Test to see if the normals are facing the same direction.
    // If yes, the point is inside, otherwise it isn't.
    return dot(u, v) >= 0.0 && dot(u, w) >= 0.0;
}

#endif

Dependencies:

Use:

<float3> contain(<Triangle> tri, <float3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_CONTAIN
#define FNC_TRIANGLE_CONTAIN

bool contain(Triangle _tri, float3 _pos) { 
    // Move the triangle so that the point becomes the
    // triangles origin
    float3 local_a = _tri.a - _pos;
    float3 local_b = _tri.b - _pos;
    float3 local_c = _tri.c - _pos;

    // The point should be moved too, so they are both
    // relative, but because we don't use p in the
    // equation anymore, we don't need it!
    // p -= p;

    // Compute the normal vectors for triangles:
    // u = normal of PBC
    // v = normal of PCA
    // w = normal of PAB

    float3 u = cross(local_b, local_c);
    float3 v = cross(local_c, local_a);
    float3 w = cross(local_a, local_b);

    // Test to see if the normals are facing the same direction.
    // If yes, the point is inside, otherwise it isn't.
    return dot(u, v) >= 0.0f && dot(u, w) >= 0.0f;
}

#endif

Dependencies:

Use:

<float3> contain(<Triangle> tri, <float3> _pos)

Check it on Github



#ifndef FNC_TRIANGLE_CONTAIN
#define FNC_TRIANGLE_CONTAIN

inline __host__ __device__ bool contain(const Triangle& _tri, const float3& _pos) { 
    // Move the triangle so that the point becomes the
    // triangles origin
    float3 local_a = _tri.a - _pos;
    float3 local_b = _tri.b - _pos;
    float3 local_c = _tri.c - _pos;

    // The point should be moved too, so they are both
    // relative, but because we don't use p in the
    // equation anymore, we don't need it!
    // p -= p;

    // Compute the normal vectors for triangles:
    // u = normal of PBC
    // v = normal of PCA
    // w = normal of PAB

    float3 u = cross(local_b, local_c);
    float3 v = cross(local_c, local_a);
    float3 w = cross(local_a, local_b);

    // Test to see if the normals are facing the same direction.
    // If yes, the point is inside, otherwise it isn't.
    return dot(u, v) >= 0.0f && dot(u, w) >= 0.0f;
}

#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