www.pudn.com > Gimcrack-v0.0051-Source.zip > math.cpp


#include "math.h" 
#include "vector.h" 
#include "triangle.h" 
#include "obb.h" 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
void Swap(float & a, float & b) 
{ 
    float t; 
    t = a; 
    a = b; 
    b = t; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
/* 
float Clamp(float value, float min, float max) 
{ 
	if (value > max) 
		value -= (max-min); 
	 
	if (value < min) 
		value += (max-min); 
	 
	if ((value > max) || (value < min)) 
		value = Clamp(value, min, max); 
   
	return value; 
} 
*/ 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
float NormalizeAngle(float value) 
{ 
	if( value > 360 ) 
		value -= 360; 
	 
	if( value < 0 ) 
		value += 360; 
	 
	if( (value > 360) || (value < 0) ) 
		value = NormalizeAngle(value); 
   
	return value; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
static float Abs( float scalar ) 
{ 
	if( scalar >= 0 ) 
		return scalar; 
	else 
		return -scalar; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
GcVector3 GcMath::ClaculateNormal(GcPoint3 p1, GcPoint3 p2, GcPoint3 p3) 
{ 
	GcVector3 temp1; 
	GcVector3 temp2; 
 
	// Extract the vectors from the point 
	temp1 = p1 - p2; 
	temp2 = p1 - p3; 
 
	// Calculate the normal 
	temp1.Normalize(temp2); 
 
	return temp1; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
void GcMath::ProjectTriangle(const GcVector3 & V, const GcTriangle & triangle,  
							 float & min, float & max) 
{ 
    float dot; 
	 
	// Take one point of the triangle and project onto V 
	// For starting, we assume this to be the max and min until proven otherwise 
	min = V.DotProduct( triangle.a ); 
    max = min; 
 
    	 
	// Project the next point of the triangle onto V 
	dot = V.DotProduct( triangle.b ); 
     
	// Is it a new max/min ? 
	if( dot < min ) 
        min = dot; 
    else if( dot > max ) 
        max = dot; 
 
     
	// Project the last point of the triangle onto V 
	dot = V.DotProduct( triangle.c ); 
     
	// Is it a new max/min ? 
	if( dot < min ) 
        min = dot; 
    else if( dot > max ) 
        max = dot; 
} 
 
//////////////////////////////////////////////////////////////////////////////////////////// 
 
// FIXA CONST 
void GcMath::ProjectOBB( const GcVector3 & V, GcOBB & obb, float & min, float & max ) 
{ 
//    GcOBB FANJAVLAHELVETE = obb; 
	GcVector3 center = obb.GetWorldTranslation(); 
	GcVector3 extent = obb.Extents(); 
	GcVector3 distance; 
	 
	// Project the OBB center onto the given vector 
	// This gives us a reference point on the vector 
	float obbCenterOnV = V.DotProduct( center ); 
     
	 
	 
 
	 
	// Project each axis onto V so we get a radius of the OBB on V 
	distance.x = extent.x * ( V.DotProduct( obb.Axis(0) ) ); 
	distance.y = extent.y * ( V.DotProduct( obb.Axis(1) ) ); 
	distance.z = extent.z * ( V.DotProduct( obb.Axis(2) ) ); 
 
	float radius = fabs( distance.x ) + fabs( distance.y ) + fabs( distance.z ); 
     
	min = obbCenterOnV - radius; 
    max = obbCenterOnV + radius; 
} 
 
////////////////////////////////////////////////////////////////////////////////////////////