/*
A word about the 3D library. Even though this library supports
three dimensions, the matrices are 4x4 for the following reason.
With normal 3 dimensional vectors, translation is an ADDITION,
and rotation is a MULTIPLICATION. A vector {x,y,z} is represented
as a 4-tuple {x,y,z,1}. It is then possible to define a 4x4
matrix such that multiplying the vector by the matrix translates
the vector. This allows combinations of translation and rotation
to be obtained in a single matrix by multiplying a translation
matrix and a rotation matrix together. Note that in the code,
vectors have three components; since the fourth component is
always 1, that value is not included in the vector variable to
save space, but the routines make use of the fourth component
(see vec_mult()). Similarly, the fourth column of EVERY matrix is
always
0
0
0
1
but currently the C version of a matrix includes this even though
it could be left out of the data structure and assumed in the
routines. Vectors are ROW vectors, and are always multiplied with
matrices FROM THE LEFT (e.g. vector*matrix). Also note the order
of indices of a matrix is matrix[row][column], and in usual C
fashion, numbering starts with 0.

TRANSLATION MATRIX = 1 0 0 0
0 1 0 0
0 0 1 0
Tx Ty Tz 1

SCALE MATRIX = Sx 0 0 0
0 Sy 0 0
0 0 Sz 0
0 0 0 1

Rotation about x axis i degrees:
ROTX(é) = 1 0 0 0
0 cosé siné 0
0 -siné cosé 0
0 0 0 1

Rotation about y axis i degrees:
ROTY(é) = cosé 0 -siné 0
0 1 0 0
siné 0 cosé 0
0 0 0 1

Rotation about z axis i degrees:
ROTZ(é) = cosé siné 0 0
-siné cosé 0 0
0 0 1 0
0 0 0 1

-- Tim Wegner April 22, 1989
*/

#include
#include
#include "fractint.h"
extern int overflow;

/* initialize a matrix and set to identity matrix
(all 0's, 1's on diagonal) */
void identity(MATRIX m)
{
int i,j;
for(i=0;i for(j=0;j if(i==j)
m[j][i] = 1.0;
else
m[j][i] = 0.0;
}

/* Multiply two matrices */
void mat_mul(MATRIX mat1, MATRIX mat2, MATRIX mat3)
{
/* result stored in MATRIX new to avoid problems
in case parameter mat3 == mat2 or mat 1 */
MATRIX new;
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
new[j][i] = mat1[j]*mat2[i]+
mat1[j]*mat2[i]+
mat1[j]*mat2[i]+
mat1[j]*mat2[i];

/* should replace this with memcpy */
for(i=0;i<4;i++)
for(j=0;j<4;j++)
mat3[j][i] = new[j][i];

}

/* multiply a matrix by a scalar */
void scale (double sx, double sy, double sz, MATRIX m)
{
MATRIX scale;
identity(scale);
scale = sx;
scale = sy;
scale = sz;
mat_mul(m,scale,m);
}

/* rotate about X axis */
void xrot (double theta, MATRIX m)
{
MATRIX rot;
double sintheta,costheta;
sintheta = sin(theta);
costheta = cos(theta);
identity(rot);
rot = costheta;
rot = -sintheta;
rot = sintheta;
rot = costheta;
mat_mul(m,rot,m);
}

/* rotate about Y axis */
void yrot (double theta, MATRIX m)
{
MATRIX rot;
double sintheta,costheta;
sintheta = sin(theta);
costheta = cos(theta);
identity(rot);
rot = costheta;
rot = sintheta;
rot = -sintheta;
rot = costheta;
mat_mul(m,rot,m);
}

/* rotate about Z axis */
void zrot (double theta, MATRIX m)
{
MATRIX rot;
double sintheta,costheta;
sintheta = sin(theta);
costheta = cos(theta);
identity(rot);
rot = costheta;
rot = -sintheta;
rot = sintheta;
rot = costheta;
mat_mul(m,rot,m);
}

/* translate */
void trans (double tx, double ty, double tz, MATRIX m)
{
MATRIX trans;
identity(trans);
trans = tx;
trans = ty;
trans = tz;
mat_mul(m,trans,m);
}

/* cross product - useful because cross is perpendicular to v and w */
int cross_product (VECTOR v, VECTOR w, VECTOR cross)
{
VECTOR tmp;
tmp = v*w - w*v;
tmp = w*v - v*w;
tmp = v*w - w*v;
cross = tmp;
cross = tmp;
cross = tmp;
return(0);
}
/* cross product integer arguments (not fudged) */
int icross_product (IVECTOR v, IVECTOR w, IVECTOR cross)
{
IVECTOR tmp;
tmp = v*w - w*v;
tmp = w*v - v*w;
tmp = v*w - w*v;
cross = tmp;
cross = tmp;
cross = tmp;
return(0);
}

/* normalize a vector to length 1 */
normalize_vector(VECTOR v)
{
double vlength;
vlength = dot_product(v,v);

/* bailout if zero vlength */
if(vlength < FLT_MIN || vlength > FLT_MAX)
return(-1);
vlength = sqrt(vlength);
if(vlength < FLT_MIN)
return(-1);

v /= vlength;
v /= vlength;
v /= vlength;
return(0);
}

/* multiply source vector s by matrix m, result in target t */
/* used to apply transformations to a vector */
int vmult(s,m,t)
VECTOR s,t;
MATRIX m;
{
VECTOR tmp;
int i,j;
for(j=0;j {
tmp[j] = 0.0;
for(i=0;i tmp[j] += s[i]*m[i][j];
/* vector is really four dimensional with last component always 1 */
tmp[j] += m[j];
}
/* set target = tmp. Necessary to use tmp in case source = target */
for(i=0;i t[i] = tmp[i];
return(0);
}

/* perspective projection of vector v with respect to viewpont vector view */
perspective(VECTOR v)
{
extern VECTOR view;
double denom;
denom = view - v;

if(denom >= 0.0)
{
v = bad_value; /* clipping will catch these values */
v = bad_value; /* so they won't plot values BEHIND viewer */
return(-1);
}
v = (v*view - view*v)/denom;
v = (v*view - view*v)/denom;

/* calculation of z if needed later */
/* v = v/denom;*/
return(0);
}

/* long version of vmult and perspective combined for speed */
longvmultpersp(s, m, t0, t, lview, bitshift)
LVECTOR s; /* source vector */
LMATRIX m; /* transformation matrix */
LVECTOR t0; /* after transformation, before persp */
LVECTOR t; /* target vector */
LVECTOR lview; /* perspective viewer coordinates */
int bitshift; /* fixed point conversion bitshift */
{
LVECTOR tmp;
int i,j, k;
overflow = 0;
k = CMAX-1; /* shorten the math if non-perspective and non-illum */
if (lview == 0 && t0 == 0) k--;

for(j=0;j {
tmp[j] = 0;
for(i=0;i tmp[j] += multiply(s[i],m[i][j],bitshift);
/* vector is really four dimensional with last component always 1 */
tmp[j] += m[j];
}
if(t0) /* first component of t0 used as flag */
{
/* faster than for loop, if less general */
t0 = tmp;
t0 = tmp;
t0 = tmp;
}
if (lview != 0) /* perspective 3D */
{

LVECTOR tmpview;
long denom;

denom = lview - tmp;
if (denom >= 0) /* bail out if point is "behind" us */
{
t = t< t = t;
t = t;
return(-1);
}

/* doing math in this order helps prevent overflow */
tmpview = divide(lview,denom,bitshift);
tmpview = divide(lview,denom,bitshift);
tmpview = divide(lview,denom,bitshift);

tmp = multiply(tmp, tmpview, bitshift) -
multiply(tmpview, tmp, bitshift);

tmp = multiply(tmp, tmpview, bitshift) -
multiply(tmpview, tmp, bitshift);

/* z coordinate if needed */

/* tmp = divide(lview,denom); */
}

/* set target = tmp. Necessary to use tmp in case source = target */
/* faster than for loop, if less general */
t = tmp;
t = tmp;
t = tmp;
return(overflow);
}

/* Long version of perspective. Because of use of fixed point math, there
is danger of overflow and underflow */
longpersp(LVECTOR lv, LVECTOR lview, int bitshift)
{
LVECTOR tmpview;
long denom;
overflow = 0;
denom = lview - lv;
if (denom >= 0) /* bail out if point is "behind" us */
{
lv = lv< lv = lv;
lv = lv;
return(-1);
}

/* doing math in this order helps prevent overflow */
tmpview = divide(lview,denom,bitshift);
tmpview = divide(lview,denom,bitshift);
tmpview = divide(lview,denom,bitshift);

lv = multiply(lv, tmpview, bitshift) -
multiply(tmpview, lv, bitshift);

lv = multiply(lv, tmpview, bitshift) -
multiply(tmpview, lv, bitshift);

/* z coordinate if needed */
/* lv = divide(lview,denom); */
return(overflow);
}

int longvmult(LVECTOR s,LMATRIX m,LVECTOR t,int bitshift)
{
LVECTOR tmp;
int i,j, k;
overflow = 0;
k = CMAX-1;

for(j=0;j {
tmp[j] = 0;
for(i=0;i tmp[j] += multiply(s[i],m[i][j],bitshift);
/* vector is really four dimensional with last component always 1 */
tmp[j] += m[j];
}

/* set target = tmp. Necessary to use tmp in case source = target */
/* faster than for loop, if less general */
t = tmp;
t = tmp;
t = tmp;
return(overflow);
}
