Nathan S
Nathan S

Reputation: 9

C# Calucate Surface Normal Inverse Instead of Boolean Subtraction

I need to do a boolean subtraction between two models in C#. One of the meshs will be entirely within the other mesh, so I was hoping to reverse the normals for the one model and add the two models together. I am at a loss on how to reverse the normals though.

This is how I'm calculating a surface normal:

//creates surface normals
    Vector3D CalculateSurfaceNormal(Point3D p1, Point3D p2, Point3D p3)
    {
        Vector3D v1 = new Vector3D(0, 0, 0);             
        Vector3D v2 = new Vector3D(0, 0, 0);
        Vector3D normal = new Vector3D(0, 0, 0);

        // Finds The Vector Between 2 Points By Subtracting
        // The x,y,z Coordinates From One Point To Another.

        // Calculate The Vector From Point 2 To Point 1
        v1.X = p1.X - p2.X;                  
        v1.Y = p1.Y - p2.Y;                  
        v1.Z = p1.Z - p2.Z;                  
        // Calculate The Vector From Point 3 To Point 2
        v2.X = p2.X - p3.X;                  
        v2.Y = p2.Y - p3.Y;                  
        v2.Z = p2.Z - p3.Z;                  

        // Compute The Cross Product To Give Us A Surface Normal
        normal.X = v1.Y * v2.Z - v1.Z * v2.Y;   // Cross Product For Y - Z
        normal.Y = v1.Z * v2.X - v1.X * v2.Z;   // Cross Product For X - Z
        normal.Z = v1.X * v2.Y - v1.Y * v2.X;   // Cross Product For X - Y

        normal.Normalize();

        return normal;
    }

Is there a simple way to invert the surface normal?

Upvotes: 1

Views: 318

Answers (1)

Orin Tresnjak
Orin Tresnjak

Reputation: 123

To flip a normal just negate it (i.e. by negating each component). This will reverse the direction it points in.

normal.X = -normal.X
normal.Y = -normal.Y
normal.Z = -normal.Z

A vector type will likely have an overloaded unary negation operator for this, so if you've got that you might be able to do simply

normal = -normal

If you're in an environment with backface culling, you'll want to reverse the winding of the vertices of each triangle (which will naturally flip the resulting computed normal). To do this just flip the order of any two vertices of the triangle.

Upvotes: 2

Related Questions