Search code examples
mathgeometryprojectionnormalize

Converting 3D triangle to 2D in orthonormal basis


I have a function that does this conversion. But I'm not sure how its doing. Can anyone please explain it and verify that its correct?

/**
     * \brief Computes the coordinates of the vertices of a triangle
     * in a local 2D orthonormal basis of the triangle's plane.
     * \param[in] p0 , p1 , p2 the 3D coordinates of the vertices of 
     *   the triangle
     * \param[out] z0 , z1 , z2 the 2D coordinates of the vertices of
     *   the triangle
     */
    static void project_triangle(
        const vec3& p0, 
        const vec3& p1, 
        const vec3& p2,
        vec2& z0,
        vec2& z1,
        vec2& z2
    ) {
        vec3 X = p1 - p0;
        X.normalize(); // normalized by dividing x,y,z with length of the vector
        vec3 Z = cross(X,(p2 - p0));
        Z.normalize();
        vec3 Y = cross(Z,X);  //cross product
        const vec3& O = p0;

        double x0 = 0;
        double y0 = 0;
        double x1 = (p1 - O).length();
        double y1 = 0;
        double x2 = dot((p2 - O),X);
        double y2 = dot((p2 - O),Y);        

        z0 = vec2(x0,y0);
        z1 = vec2(x1,y1);
        z2 = vec2(x2,y2);        
    }

Solution

  • Vectors X,Y,Z form orthonormal basis, where X coincides with p0-p1, Y lies in triangle plane, Z is normal to this plane.

    Then p0 maps to coordinate origin in 2D plane, p1 maps on OX axis, then p2 coordinates are calculated through projections of p0-p2 vector on X and Y basis vectors.