glm::dot returning a vector

1.9k views Asked by At

I am trying to implement Tomas Moller's triangle-triangle intersection test (http://fileadmin.cs.lth.se/cs/Personal/Tomas_Akenine-Moller/pubs/tritri.pdf).

At the moment I am up to filtering out all the cases where the distance from one of the triangle vertices to the plane on which the other triangle lies is not zero, and all the distances have the same sign. In my checkForCollision function I have a line where I compute the dot product of one triangle's normal and the other's vertices. However Visual Studio is telling me that:

Error   7   error C2440: '=' : cannot convert from 'glm::detail::tvec3<glm::mediump_float>' to `'float'`

Here is my code:

struct triangle
{
    glm::vec3 vertex0, vertex1, vertex2;

    triangle(glm::vec3 vert1, glm::vec3 vert2, glm::vec3 vert3)
    {
        vertex0 = vert1;
        vertex1 = vert2;
        vertex2 = vert3;
    }
};

struct triangleDistance
{
    float vertex0, vertex1, vertex2;
};

int sign(const double inputNumber)
{
    if (inputNumber >= 0) return 1;
    else return -1;
}

bool signIsTheSame(const triangleDistance inputDistance)
{
    return (sign(inputDistance.vertex0) == sign(inputDistance.vertex1) &&
        sign(inputDistance.vertex1) == sign(inputDistance.vertex2));
}

bool noneAreZero(const triangleDistance inputDistance)
{
    return (inputDistance.vertex0 == 0 || inputDistance.vertex1 == 0 || inputDistance.vertex2 == 0);
}

glm::vec3 computeTriangleNormal(const triangle inputTriangle)
{
    glm::vec3 crossTerm1, crossTerm2;
    return glm::normalize(glm::cross((inputTriangle.vertex1 - inputTriangle.vertex0),
        (inputTriangle.vertex2 - inputTriangle.vertex0)));
}

bool checkForCollision(const triangle triangle1, const triangle triangle2)
{
    glm::vec3 triangle2Normal = computeTriangleNormal(triangle2);
    glm::vec3 triangle2d = -triangle2Normal * triangle2.vertex0;

    triangleDistance triangle1Distance;

    triangle1Distance.vertex0 = glm::dot(triangle2Normal, triangle1.vertex0) + triangle2d;
    triangle1Distance.vertex1 = glm::dot(triangle2Normal, triangle1.vertex1) + triangle2d;
    triangle1Distance.vertex2 = glm::dot(triangle2Normal, triangle1.vertex2) + triangle2d;

    return (signIsTheSame(triangle1Distance) && noneAreZero(triangle1Distance));

    return false;
}
1

There are 1 answers

0
Richard Walters On BEST ANSWER

I think you may have misinterpreted the triangle-triangle collision test algorithm. The dot product of two vectors is a scalar. It does not make sense to add a scalar to a vector, ergo the compiler error.

I think the problem lies in your understanding of d_2 -- from the paper you site:

d_2 = - N_2 * V_2_0

Where:

  • d_2 is the scalar in the plane equation (N_2 * X + d_2 = 0) -- think of it as the shortest distance from the plane to the origin of your coordinate system.
  • N_2 is the normal vector of the plane of triangle 2
  • The ' * ' is actually a dot, as in dot product.
  • V_2_0 is vertex 0 of triangle 2

In your code, however, d_2 is triangle2d which you have interpreted to be a vector and calculated as a per-component vector product:

glm::vec3 triangle2d = -triangle2Normal * triangle2.vertex0;

Instead, consider that d_2 really should be a scalar and calculated as the dot product of the triangle 2 normal with one of its vertices:

float triangle2d = glm::dot(-triangle2Normal, triangle2.vertex0);