Unit Math_utils

///////////////////////////////////////////////////////////////////////////// // Assorted routines // ŠJohn Biddiscombe // Rutherford Appleton Laboratory, UK // j.biddiscombe@rl.ac.uk // // /////////////////////////////////////////////////////////////////////////////

Classes

Functions

allocate_matrix -
allocate_points - ///////////////////////////////////////////////////////////////////////////// Memory /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Memory /////////////////////////////////////////////////////////////////////////////
angle -
aPoint3D - ///////////////////////////////////////////////////////////////////////////// General point 3D stuff /////////////////////////////////////////////////////////////////////////////
BoolToStr -
CreateTransformation - Matrix order : For reference start with a point at ( cos(30),sin(30),0 ) rotate by 30 degrees - shifts point to (1,0,0) then translate by (10,0,0) shifts to (11,0,0) then rotate by -45 degrees goes to (7.
cross -
deallocate_matrix -
deallocate_points -
dist2D -
dist3D -
dmax -
dmin -
dot - ///////////////////////////////////////////////////////////////////////////// Vector 3D stuff /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Vector 3D stuff /////////////////////////////////////////////////////////////////////////////
float_out - ///////////////////////////////////////////////////////////////////////////// String /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// String /////////////////////////////////////////////////////////////////////////////
imax -
imin -
mag -
MatrixMultiply -
max_bound - ///////////////////////////////////////////////////////////////////////////// Bounds /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Bounds /////////////////////////////////////////////////////////////////////////////
min_bound -
normalize -
p1_eq_p2_2D -
p1_eq_p2_3D -
p1_minus_p2 -
p1_plus_p2 -
p1_x_n -
Point3DToStr - result := FloatToStr(f);
powerxy - ///////////////////////////////////////////////////////////////////////////// Assorted /////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
ranbit - /////////////////////////////////////////////////////////////////////////////
RotationAxis -
ScaleMatrix -
setup_tables - ///////////////////////////////////////////////////////////////////////////// Lookup tables /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Lookup tables /////////////////////////////////////////////////////////////////////////////
set_accuracy -
sq_dist2D -
sq_dist3D -
sq_mag3D -
TransformPoint -
TranslateMatrix -
update_transformations -
XRotateMatrix - ///////////////////////////////////////////////////////////////////////////// Rotations for Insert/Block drawing /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Rotations for Insert/Block drawing /////////////////////////////////////////////////////////////////////////////
YRotateMatrix -
ZRotateMatrix -

Types

coord
Matrix
pM
pMatrix
Point2D
Point3D
pointlist
pPoint3D
ppointlist
pTriangle
pTriangleList
table
Triangle
TriangleList

Constants

cube
cube_edges
identity
origin3D
tablesize
tablesized2
tablesizep1
WCS_X
WCS_Y
WCS_Z

Variables

cosine
lookups_done
sigmoids
sine
theta_d


Functions


function allocate_matrix : pMatrix;


function allocate_points(n:integer) : ppointlist;

///////////////////////////////////////////////////////////////////////////// Memory /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Memory /////////////////////////////////////////////////////////////////////////////


function angle(p1,p2,p3:Point3D; do_3D:boolean) : double;


function aPoint3D(a,b,c:double) : Point3D;

///////////////////////////////////////////////////////////////////////////// General point 3D stuff /////////////////////////////////////////////////////////////////////////////

function BoolToStr(b:boolean) : string;


function CreateTransformation(Ax,Ay,Az:Point3D) : Matrix;

Matrix order : For reference start with a point at ( cos(30),sin(30),0 ) rotate by 30 degrees - shifts point to (1,0,0) then translate by (10,0,0) shifts to (11,0,0) then rotate by -45 degrees goes to (7.77, 7.77 ,0) 7.77 = Sqrt(11^2 /2 ) NOTE THE ORDER OF MATRIX OPERATIONS ! test := aPoint3D( cos(degtorad(30)) , sin(degtorad(30)) , 0); mat := ZRotateMatrix( cos(degtorad(30)) , sin(degtorad(30)) ); mat := MatrixMultiply( mat , TranslateMatrix(aPoint3D(10,0,0)) ); mat := MatrixMultiply( mat , ZRotateMatrix( cos(degtorad(-45)) , sin(degtorad(-45)) ) ); test := TransformPoint(mat,test);

function cross(p1,p2:Point3D) : Point3D;


procedure deallocate_matrix(var m:pMatrix);


procedure deallocate_points(var pts:ppointlist; n:integer);


function dist2D(p1,p2:Point3D) : double;


function dist3D(p1,p2:Point3D) : double;


function dmax(a,b:double) : double;


function dmin(a,b:double) : double;


function dot(p1,p2:Point3D) : double;

///////////////////////////////////////////////////////////////////////////// Vector 3D stuff /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Vector 3D stuff /////////////////////////////////////////////////////////////////////////////


function float_out(f:double) : string;

///////////////////////////////////////////////////////////////////////////// String /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// String /////////////////////////////////////////////////////////////////////////////


function imax(a,b:integer) : integer;


function imin(a,b:integer) : integer;


function mag(p1:Point3D) : double;


function MatrixMultiply(matrix1,matrix2:Matrix) : Matrix;


procedure max_bound(var bounds:Point3D; point:Point3D);

///////////////////////////////////////////////////////////////////////////// Bounds /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Bounds /////////////////////////////////////////////////////////////////////////////


procedure min_bound(var bounds:Point3D; point:Point3D);


function normalize(p1:Point3D) : Point3D;


function p1_eq_p2_2D(p1,p2:Point3D) : boolean;


function p1_eq_p2_3D(p1,p2:Point3D) : boolean;


function p1_minus_p2(p1,p2:Point3D) : Point3D;


function p1_plus_p2 (p1,p2:Point3D) : Point3D;


function p1_x_n(p1:Point3D; n:double) : Point3D;


function Point3DToStr(p:Point3D) : string;

result := FloatToStr(f);

function powerxy(x,y:double):double;

///////////////////////////////////////////////////////////////////////////// Assorted /////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////


function ranbit : integer;

/////////////////////////////////////////////////////////////////////////////

function RotationAxis(A:Point3D; angle:double) : Matrix;


function ScaleMatrix(p:Point3D) : Matrix;


procedure setup_tables;

///////////////////////////////////////////////////////////////////////////// Lookup tables /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Lookup tables /////////////////////////////////////////////////////////////////////////////


function set_accuracy(factor:double; p:Point3D) : Point3D;


function sq_dist2D(p1,p2:Point3D) : double;


function sq_dist3D(p1,p2:Point3D) : double;


function sq_mag3D(p1:Point3D) : double;


function TransformPoint(TM:Matrix; p:Point3D) : Point3D;


function TranslateMatrix(p:Point3D) : Matrix;


function update_transformations(OCS_WCS,OCS:pMatrix) : pMatrix;


function XRotateMatrix(cos_a,sin_a:double) : Matrix;

///////////////////////////////////////////////////////////////////////////// Rotations for Insert/Block drawing /////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////// Rotations for Insert/Block drawing /////////////////////////////////////////////////////////////////////////////


function YRotateMatrix(cos_a,sin_a:double) : Matrix;


function ZRotateMatrix(cos_a,sin_a:double) : Matrix;


Types


coord = array[0..2] of double;

Matrix = record
val : array[0..3,0..3] of double;
end;

pM      = pMatrix

pMatrix = ^Matrix

Point2D = record
x : Double;
y : Double;
end;

Point3D = record
x : double;
y : double;
z : double;
end;

pointlist     = array[0..0] of Point3D;

pPoint3D      = ^Point3D

ppointlist    = ^pointlist

pTriangle     = ^Triangle

pTriangleList = ^TriangleList

table = array[0..tablesize] of double;

Triangle = record
p1 : Point3D;
p2 : Point3D;
p3 : Point3D;
end;

TriangleList  = array[0..0] of Triangle;

Constants

cube = ( (0,0,0), (1,0,0), (1,1,0), (0,1,0), (0,0,1), (1,0,1), (1,1,1), (0,1,1))

cube_edges = ( (0,1), (1,2), (2,3), (3,0), (4,0), (5,1), (6,2), (7,3), (4,5), (5,6), (6,7), (7,4))

identity = (val:((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)))

origin3D = (x:0; y:0; z:0)

tablesize = 16383

tablesized2 = tablesizep1 div 2

tablesizep1 = tablesize+1

WCS_X = (x:1; y:0; z:0)

WCS_Y = (x:0; y:1; z:0)

WCS_Z = (x:0; y:0; z:1)


Variables

cosine : table

lookups_done : boolean

lookup tables

sigmoids : table

sine : table

theta_d : array[1..10] of integer