Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a function to find the triangle containing a point #20

Open
mllobera opened this issue Dec 14, 2023 · 0 comments
Open

Adding a function to find the triangle containing a point #20

mllobera opened this issue Dec 14, 2023 · 0 comments

Comments

@mllobera
Copy link

mllobera commented Dec 14, 2023

Based on what I have seen in your code I was wondering about the possibility of inserting the following functions into triangulator.cpp and *.h in order to extend your code to be able to get the location of the triangle containing a point. I do no have much of C++ experience hence why I am not making a pull request.

Here are the functions,

// Private Function to check if a point is inside a triangle using barycentric coordinates
bool PointInTriangle(const glm::ivec2 p, const glm::ivec2 a, const glm::ivec2 b, const glm::ivec2 c) const {
    auto sign = [](const glm::ivec2 p1, const glm::ivec2 p2, const glm::ivec2 p3) {
        return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
    };

    bool d1 = sign(p, a, b) < 0.0f;
    bool d2 = sign(p, b, c) < 0.0f;
    bool d3 = sign(p, c, a) < 0.0f;

    return ((d1 == d2) && (d2 == d3));
}

// Public  function to triangle contianing point pt
int locatePoint(const glm::ivec2 pt) const {
    for (int i = 0; i < m_Queue.size(); ++i) {
        const int t = m_Queue[i];
        const int e0 = t * 3;
        const int e1 = e0 + 1;
        const int e2 = e0 + 2;

        const int p0 = m_Triangles[e0];
        const int p1 = m_Triangles[e1];
        const int p2 = m_Triangles[e2];

        const glm::ivec2 a = m_Points[p0];
        const glm::ivec2 b = m_Points[p1];
        const glm::ivec2 c = m_Points[p2];

        if (PointInTriangle(pt, a, b, c)) {
            return t;
        }
    }
    return -1;  // Point is not inside any triangle
}

Thank you for considering these.

@mllobera mllobera changed the title Adding a Adding a function to find the triangle containing a point Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant