How to I iterate through all triangles in an ofMesh


#1

I want to do intersection testing on all triangles of an ofMesh but the documentation says that ofMesh::getFaces() is not implemented. If I understand correctly, ofMesh::getUniqueFaces() only returns faces that don’t share a vertex/edge - so that doesn’t seem like the right method…(?)… suggestions ?


#2

I would go for:

        intersectsPrimitive(const of3dPrimitive& primitive,  glm::vec3 & baricentricCoords, glm::vec3 & intNormal) {
            // at the beginning, no intersection is found and the distance to the closest surface
            // is set to an high value;
            bool found = false;
            float distanceToTheClosestSurface = numeric_limits<float>::max();

            for (const ofMeshFace& face : primitive.getMesh().getUniqueFaces()) {
                glm::vec3 baricentricCoords;
                bool intersection = glm::intersectRayTriangle(
                                                              origin, direction,
                                                              glm::vec3(primitive.getGlobalTransformMatrix() * glm::vec4(face.getVertex(0), 1.f)),
                                                              glm::vec3(primitive.getGlobalTransformMatrix() * glm::vec4(face.getVertex(1), 1.f)),
                                                              glm::vec3(primitive.getGlobalTransformMatrix() * glm::vec4(face.getVertex(2), 1.f)),
                                                              baricentricCoords);
                // when an intersection is found, it updates the distanceToTheClosestSurface value
                // this value is used to order the new intersections, if a new intersection with a smaller baricenter.z
                // value is found, this one will become the new intersection
                if (intersection) {
                    if (baricentricCoords.z < distanceToTheClosestSurface) {
                        found = true;
                        distanceToTheClosestSurface = baricentricCoords.z;
                        intNormal = face.getFaceNormal();
                    }
                }
            }
            return found;
        };

I have used this method in this addon, and it works quite well. https://github.com/edap/ofxRaycaster


#3

Thank You ! I have tried the glm::intersect methods and they seem to work nicely. I’ll look at your code.