protected void BuildScaleTable()
{
m_Intersections = new double[INTERSECTIONS_TABLE_SIZE,INTERSECTIONS_TABLE_SIZE];
// Use Newton Raphson method to compute the interesection
// for the equation f(x) = tan(a.x).tan(x) - b = 0
//
// The derivative of f(x) is:
// f'(x) = a.tan(x)/cosĀ²(a.x) + tan(a.x)/cosĀ²(x)
//
int SumIterations = 0;
for ( int Y=0; Y < INTERSECTIONS_TABLE_SIZE; Y++ )
{
double PhiD = Y * 0.5 * Math.PI / (INTERSECTIONS_TABLE_SIZE-1);
double b = 1.0 / Math.Cos( PhiD );
for ( int X=0; X < INTERSECTIONS_TABLE_SIZE; X++ )
{
double SlopeAngle = X * 0.5 * Math.PI / (INTERSECTIONS_TABLE_SIZE-1);
double a = Math.Tan( SlopeAngle );
int IterationsCount;
double Intersection = SolveNewton( a, b, out IterationsCount );
m_Intersections[X,Y] = (float) Intersection;
SumIterations += IterationsCount;
}
}
double AverageIterationsCount = (double) SumIterations / (INTERSECTIONS_TABLE_SIZE*INTERSECTIONS_TABLE_SIZE); // Get average iterations per texel
// Save the result
using ( FileStream S = new FileInfo( "Intersections" + INTERSECTIONS_TABLE_SIZE + "x" + INTERSECTIONS_TABLE_SIZE + ".double" ).Create() )
using ( BinaryWriter Writer = new BinaryWriter( S ) )
{
for ( int Y=0; Y < INTERSECTIONS_TABLE_SIZE; Y++ )
for ( int X=0; X < INTERSECTIONS_TABLE_SIZE; X++ )
Writer.Write( m_Intersections[X,Y] );
}
using ( FileStream S = new FileInfo( "Intersections" + INTERSECTIONS_TABLE_SIZE + "x" + INTERSECTIONS_TABLE_SIZE + ".float" ).Create() )
using ( BinaryWriter Writer = new BinaryWriter( S ) )
{
for ( int Y=0; Y < INTERSECTIONS_TABLE_SIZE; Y++ )
for ( int X=0; X < INTERSECTIONS_TABLE_SIZE; X++ )
Writer.Write( 1.0f / (float) m_Intersections[X,Y] );
}
}