_Implicit Surfaces and Real-Time Graphics_
by Kyle Lussier

Listing One
LSurface Components;    // Given Instance in Object Database
// Pre-Allocated Temporaries for Calculations
static float    x1, y1, z1, x2, y2, z2, Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8, Tmp9;
float LSurface::MGSM(float x, float y, float z)
{
    // Initialization of Threshold and Number of Components
    float   Total=Components.GetThreshold();
    int N=Components.GetSize();
    // Loop through all components
    while(N--) {
        Ci=&Components[N];  // Get Pointer to Current Component
        // Check For Any Angular Rotation
        if( !((Ci->Theta_X==0) && (Ci->Theta_Y==0) && (Ci->Theta_Z==0)))
            {
            // There is Angular Rotation So Use Angular Model

            // Perform Translation of Component
            Tmp1=x+Ci->Tx;  
            Tmp2=y+Ci->Ty; 
            Tmp3=z+Ci->Tz;  
// Calculate Temporaries for Rotation
            Tmp4=Tmp2*Ci->cos_z;    
            Tmp5=Tmp1*Ci->sin_z;
            Tmp6=Ci->cos_z*Tmp1;    
            Tmp7=Ci->sin_z*Tmp2;
            Tmp8=Ci->cos_y*Tmp3;

            // Perform Rotation
            x1=(Ci->cos_y*(Tmp6-Tmp7)+Ci->sin_y*Tmp3);
            y1=(Ci->sin_x*(Ci->sin_y*Tmp6- Ci->sin_y*Tmp7-Tmp8) + Ci->cos_x*(Tmp5+Tmp4));
            z1=(Ci->cos_x*(-Ci->sin_y*Tmp6+Ci->sin_y*Tmp7+Tmp8) + Ci->sin_x*(Tmp5+Tmp4));
            // Perform Stretching
            x2=x1*Ci->Sx;       
            y2=y1*Ci->Sy;
            z2=z1*CI->Sz;
            // Calculate Divisor
            Tmp9=(x2*x2+y2*y2+z2*z2);
            Total-=( Ci->Strength / (Tmp9< 0.000001? 0.000001 : Tmp9));
        }
        else {
            // There is No Angular Rotation (x1), So Use Simple Form

            // Translate and Stretch at the Same Time
            x2=(x+Ci->Tx)*Ci->Sx;   
            y2=(y+Ci->Ty)*Ci->Sy;
            y2=(z+Ci->Tz)*Ci->Sz;

            // Calculate Divisor
            Tmp9=(x2*x2+y2*y2+z2*z2);

            // Apply to Threshold
            Total-=(Ci->Strength / (Tmp9< 0.000001? 0.000001 : Tmp9));
        };
    };
    return Total; 
}





