Back to index

radiance  4R0+20100331
Defines | Typedefs | Functions
vect.h File Reference
#include <stdio.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define X   0
#define Y   1
#define Z   2

Typedefs

typedef float Vector [3]
typedef float Matrix [4][4]

Functions

void vect_init (Vector v, float x, float y, float z)
void vect_copy (Vector v1, Vector v2)
int vect_equal (Vector v1, Vector v2)
void vect_add (Vector v1, Vector v2, Vector v3)
void vect_sub (Vector v1, Vector v2, Vector v3)
void vect_scale (Vector v1, Vector v2, float k)
float vect_mag (Vector v)
void vect_normalize (Vector v)
float vect_dot (Vector v1, Vector v2)
void vect_cross (Vector v1, Vector v2, Vector v3)
void vect_min (Vector v1, Vector v2, Vector v3)
void vect_max (Vector v1, Vector v2, Vector v3)
float vect_angle (Vector v1, Vector v2)
void vect_print (FILE *f, Vector v, int dec, char sep)
void vect_rotate (Vector v1, Vector v2, int axis, float angle)
void vect_axis_rotate (Vector v1, Vector v2, Vector axis, float angle)
void mat_identity (Matrix mat)
void mat_copy (Matrix mat1, Matrix mat2)
void mat_rotate (Matrix mat1, Matrix mat2, int axis, float angle)
void mat_axis_rotate (Matrix mat1, Matrix mat2, Vector axis, float angle)
void mat_mult (Matrix mat1, Matrix mat2, Matrix mat3)
void vect_transform (Vector v1, Vector v2, Matrix mat)
void mat_decode (Matrix mat, Vector scale, Vector shear, Vector rotate, Vector transl)
float mat_inv (Matrix mat1, Matrix mat2)

Define Documentation

#define X   0

Definition at line 13 of file vect.h.

#define Y   1

Definition at line 14 of file vect.h.

#define Z   2

Definition at line 15 of file vect.h.


Typedef Documentation

typedef float Matrix[4][4]

Definition at line 18 of file vect.h.

typedef float Vector[3]

Definition at line 11 of file vect.h.


Function Documentation

void mat_axis_rotate ( Matrix  mat1,
Matrix  mat2,
Vector  axis,
float  angle 
)

Definition at line 295 of file vect.c.

{
    float  cosa, sina;
    Matrix mat;

    cosa = cos ((PI/180.0) * angle);
    sina = sin ((PI/180.0) * angle);

    mat[0][0] = (axis[X] * axis[X]) + ((1.0 - (axis[X] * axis[X]))*cosa);
    mat[0][1] = (axis[X] * axis[Y] * (1.0 - cosa)) - (axis[Z] * sina);
    mat[0][2] = (axis[X] * axis[Z] * (1.0 - cosa)) + (axis[Y] * sina);
    mat[0][3] = 0.0;

    mat[1][0] = (axis[X] * axis[Y] * (1.0 - cosa)) + (axis[Z] * sina);
    mat[1][1] = (axis[Y] * axis[Y]) + ((1.0 - (axis[Y] * axis[Y])) * cosa);
    mat[1][2] = (axis[Y] * axis[Z] * (1.0 - cosa)) - (axis[X] * sina);
    mat[1][3] = 0.0;

    mat[2][0] = (axis[X] * axis[Z] * (1.0 - cosa)) - (axis[Y] * sina);
    mat[2][1] = (axis[Y] * axis[Z] * (1.0 - cosa)) + (axis[X] * sina);
    mat[2][2] = (axis[Z] * axis[Z]) + ((1.0 - (axis[Z] * axis[Z])) * cosa);
    mat[2][3] = 0.0;

    mat[3][0] = mat[3][1] = mat[3][2] = mat[3][3] = 0.0;

    mat_mult (mat1, mat2, mat);
}

Here is the call graph for this function:

void mat_copy ( Matrix  mat1,
Matrix  mat2 
)

Definition at line 247 of file vect.c.

{
    int i, j;

    for (i = 0; i < 4; i++)
       for (j = 0; j < 4; j++)
           mat1[i][j] = mat2[i][j];
}

Here is the caller graph for this function:

void mat_decode ( Matrix  mat,
Vector  scale,
Vector  shear,
Vector  rotate,
Vector  transl 
)

Definition at line 353 of file vect.c.

{
    int i;
    Vector row[3], temp;

    for (i = 0; i < 3; i++)
       transl[i] = mat[3][i];

    for (i = 0; i < 3; i++) {
       row[i][X] = mat[i][0];
       row[i][Y] = mat[i][1];
       row[i][Z] = mat[i][2];
    }

    scale[X] = vect_mag (row[0]);
    vect_normalize (row[0]);

    shear[X] = vect_dot (row[0], row[1]);
    row[1][X] = row[1][X] - shear[X]*row[0][X];
    row[1][Y] = row[1][Y] - shear[X]*row[0][Y];
    row[1][Z] = row[1][Z] - shear[X]*row[0][Z];

    scale[Y] = vect_mag (row[1]);
    vect_normalize (row[1]);

    if (scale[Y] != 0.0)
       shear[X] /= scale[Y];

    shear[Y] = vect_dot (row[0], row[2]);
    row[2][X] = row[2][X] - shear[Y]*row[0][X];
    row[2][Y] = row[2][Y] - shear[Y]*row[0][Y];
    row[2][Z] = row[2][Z] - shear[Y]*row[0][Z];

    shear[Z] = vect_dot (row[1], row[2]);
    row[2][X] = row[2][X] - shear[Z]*row[1][X];
    row[2][Y] = row[2][Y] - shear[Z]*row[1][Y];
    row[2][Z] = row[2][Z] - shear[Z]*row[1][Z];

    scale[Z] = vect_mag (row[2]);
    vect_normalize (row[2]);

    if (scale[Z] != 0.0) {
       shear[Y] /= scale[Z];
       shear[Z] /= scale[Z];
    }

    vect_cross (temp, row[1], row[2]);
    if (vect_dot (row[0], temp) < 0.0) {
       for (i = 0; i < 3; i++) {
           scale[i]  *= -1.0;
           row[i][X] *= -1.0;
           row[i][Y] *= -1.0;
           row[i][Z] *= -1.0;
       }
    }

    if (row[0][Z] < -1.0) row[0][Z] = -1.0;
    if (row[0][Z] > +1.0) row[0][Z] = +1.0;

    rotate[Y] = asin(-row[0][Z]);

    if (fabs(cos(rotate[Y])) > EPSILON) {
       rotate[X] = atan2 (row[1][Z], row[2][Z]);
       rotate[Z] = atan2 (row[0][Y], row[0][X]);
    }
    else {
       rotate[X] = atan2 (row[1][X], row[1][Y]);
       rotate[Z] = 0.0;
    }

    /* Convert the rotations to degrees */
    rotate[X] = (180.0/PI)*rotate[X];
    rotate[Y] = (180.0/PI)*rotate[Y];
    rotate[Z] = (180.0/PI)*rotate[Z];
}

Here is the call graph for this function:

Here is the caller graph for this function:

void mat_identity ( Matrix  mat)

Definition at line 234 of file vect.c.

{
    int i, j;

    for (i = 0; i < 4; i++)
       for (j = 0; j < 4; j++)
           mat[i][j] = 0.0;

    for (i = 0; i < 4; i++)
       mat[i][i] = 1.0;
}

Here is the caller graph for this function:

float mat_inv ( Matrix  mat1,
Matrix  mat2 
)

Definition at line 434 of file vect.c.

{
    int i, j;
    float det;

    if (mat1 != mat2) {
       for (i = 0; i < 4; i++)
           for (j = 0; j < 4; j++)
              mat1[i][j] = mat2[i][j];
    }

    det = det4x4 (mat1);

    if (fabs (det) < EPSILON)
       return 0.0;

    adjoint (mat1);

    for (i = 0; i < 4; i++)
       for(j = 0; j < 4; j++)
           mat1[i][j] = mat1[i][j] / det;

    return det;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void mat_mult ( Matrix  mat1,
Matrix  mat2,
Matrix  mat3 
)

Definition at line 325 of file vect.c.

{
    float sum;
    int   i, j, k;
    Matrix result;

    for (i = 0; i < 4; i++) {
       for (j = 0; j < 4; j++) {
           sum = 0.0;

           for (k = 0; k < 4; k++)
              sum = sum + mat2[i][k] * mat3[k][j];

           result[i][j] = sum;
       }
    }

    for (i = 0; i < 4; i++)
       for (j = 0; j < 4; j++)
           mat1[i][j] = result[i][j];
}

Here is the caller graph for this function:

void mat_rotate ( Matrix  mat1,
Matrix  mat2,
int  axis,
float  angle 
)

Definition at line 258 of file vect.c.

{
    Matrix mat;
    float  cosa, sina;

    cosa = cos ((PI/180.0) * angle);
    sina = sin ((PI/180.0) * angle);

    mat_identity (mat);

    switch (axis) {
       case X:
           mat[1][1] = cosa;
           mat[1][2] = sina;
           mat[2][1] = -sina;
           mat[2][2] = cosa;
           break;

       case Y:
           mat[0][0] = cosa;
           mat[0][2] = -sina;
           mat[2][0] = sina;
           mat[2][2] = cosa;
           break;

       case Z:
           mat[0][0] = cosa;
           mat[0][1] = sina;
           mat[1][0] = -sina;
           mat[1][1] = cosa;
           break;
    }

    mat_mult (mat1, mat2, mat);
}

Here is the call graph for this function:

void vect_add ( Vector  v1,
Vector  v2,
Vector  v3 
)

Definition at line 47 of file vect.c.

{
    v1[X] = v2[X] + v3[X];
    v1[Y] = v2[Y] + v3[Y];
    v1[Z] = v2[Z] + v3[Z];
}

Here is the caller graph for this function:

float vect_angle ( Vector  v1,
Vector  v2 
)

Definition at line 118 of file vect.c.

{
    float  mag1, mag2, angle, cos_theta;

    mag1 = vect_mag(v1);
    mag2 = vect_mag(v2);

    if (mag1 * mag2 == 0.0)
       angle = 0.0;
    else {
       cos_theta = vect_dot(v1,v2) / (mag1 * mag2);

       if (cos_theta <= -1.0)
           angle = 180.0;
       else if (cos_theta >= +1.0)
           angle = 0.0;
       else
           angle = (180.0/PI) * acos(cos_theta);
    }

    return angle;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void vect_axis_rotate ( Vector  v1,
Vector  v2,
Vector  axis,
float  angle 
)

Definition at line 191 of file vect.c.

{
    float  cosa, sina;
    Matrix mat;

    cosa = cos ((PI/180.0) * angle);
    sina = sin ((PI/180.0) * angle);

    mat[0][0] = (axis[X] * axis[X]) + ((1.0 - (axis[X] * axis[X]))*cosa);
    mat[0][1] = (axis[X] * axis[Y] * (1.0 - cosa)) - (axis[Z] * sina);
    mat[0][2] = (axis[X] * axis[Z] * (1.0 - cosa)) + (axis[Y] * sina);
    mat[0][3] = 0.0;

    mat[1][0] = (axis[X] * axis[Y] * (1.0 - cosa)) + (axis[Z] * sina);
    mat[1][1] = (axis[Y] * axis[Y]) + ((1.0 - (axis[Y] * axis[Y])) * cosa);
    mat[1][2] = (axis[Y] * axis[Z] * (1.0 - cosa)) - (axis[X] * sina);
    mat[1][3] = 0.0;

    mat[2][0] = (axis[X] * axis[Z] * (1.0 - cosa)) - (axis[Y] * sina);
    mat[2][1] = (axis[Y] * axis[Z] * (1.0 - cosa)) + (axis[X] * sina);
    mat[2][2] = (axis[Z] * axis[Z]) + ((1.0 - (axis[Z] * axis[Z])) * cosa);
    mat[2][3] = 0.0;

    mat[3][0] = mat[3][1] = mat[3][2] = mat[3][3] = 0.0;

    vect_transform (v1, v2, mat);
}

Here is the call graph for this function:

void vect_copy ( Vector  v1,
Vector  v2 
)

Definition at line 30 of file vect.c.

{
    v1[X] = v2[X];
    v1[Y] = v2[Y];
    v1[Z] = v2[Z];
}

Here is the caller graph for this function:

void vect_cross ( Vector  v1,
Vector  v2,
Vector  v3 
)

Definition at line 94 of file vect.c.

{
    v1[X] = (v2[Y] * v3[Z]) - (v2[Z] * v3[Y]);
    v1[Y] = (v2[Z] * v3[X]) - (v2[X] * v3[Z]);
    v1[Z] = (v2[X] * v3[Y]) - (v2[Y] * v3[X]);
}

Here is the caller graph for this function:

float vect_dot ( Vector  v1,
Vector  v2 
)

Definition at line 88 of file vect.c.

{
    return (v1[X]*v2[X] + v1[Y]*v2[Y] + v1[Z]*v2[Z]);
}

Here is the caller graph for this function:

int vect_equal ( Vector  v1,
Vector  v2 
)

Definition at line 38 of file vect.c.

{
    if (v1[X] == v2[X] && v1[Y] == v2[Y] && v1[Z] == v2[Z])
       return 1;
    else
       return 0;
}

Here is the caller graph for this function:

void vect_init ( Vector  v,
float  x,
float  y,
float  z 
)

Definition at line 22 of file vect.c.

{
    v[X] = x;
    v[Y] = y;
    v[Z] = z;
}

Here is the caller graph for this function:

float vect_mag ( Vector  v)

Definition at line 71 of file vect.c.

{
    float mag = sqrt(v[X]*v[X] + v[Y]*v[Y] + v[Z]*v[Z]);

    return mag;
}

Here is the caller graph for this function:

void vect_max ( Vector  v1,
Vector  v2,
Vector  v3 
)

Definition at line 109 of file vect.c.

{
    v1[X] = (v2[X] > v3[X]) ? v2[X] : v3[X];
    v1[Y] = (v2[Y] > v3[Y]) ? v2[Y] : v3[Y];
    v1[Z] = (v2[Z] > v3[Z]) ? v2[Z] : v3[Z];
}

Here is the caller graph for this function:

void vect_min ( Vector  v1,
Vector  v2,
Vector  v3 
)

Definition at line 101 of file vect.c.

{
    v1[X] = (v2[X] < v3[X]) ? v2[X] : v3[X];
    v1[Y] = (v2[Y] < v3[Y]) ? v2[Y] : v3[Y];
    v1[Z] = (v2[Z] < v3[Z]) ? v2[Z] : v3[Z];
}

Here is the caller graph for this function:

void vect_normalize ( Vector  v)

Definition at line 79 of file vect.c.

{
    float mag = vect_mag (v);

    if (mag > 0.0)
       vect_scale (v, v, 1.0/mag);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void vect_print ( FILE *  f,
Vector  v,
int  dec,
char  sep 
)

Definition at line 142 of file vect.c.

{
    char fstr[] = "%.4f, %.4f, %.4f";

    if (dec < 0) dec = 0;
    if (dec > 9) dec = 9;

    fstr[2]  = '0' + dec;
    fstr[8]  = '0' + dec;
    fstr[14] = '0' + dec;

    fstr[4]  = sep;
    fstr[10] = sep;

    fprintf (f, fstr, v[X], v[Y], v[Z]);
}

Here is the caller graph for this function:

void vect_rotate ( Vector  v1,
Vector  v2,
int  axis,
float  angle 
)

Definition at line 161 of file vect.c.

{
    float  cosa, sina;

    cosa = cos ((PI/180.0) * angle);
    sina = sin ((PI/180.0) * angle);

    switch (axis) {
       case X:
           v1[X] =  v2[X];
           v1[Y] =  v2[Y] * cosa + v2[Z] * sina;
           v1[Z] =  v2[Z] * cosa - v2[Y] * sina;
           break;

       case Y:
           v1[X] = v2[X] * cosa - v2[Z] * sina;
           v1[Y] = v2[Y];
           v1[Z] = v2[Z] * cosa + v2[X] * sina;
           break;

       case Z:
           v1[X] = v2[X] * cosa + v2[Y] * sina;
           v1[Y] = v2[Y] * cosa - v2[X] * sina;
           v1[Z] = v2[Z];
           break;
    }
}

Here is the call graph for this function:

void vect_scale ( Vector  v1,
Vector  v2,
float  k 
)

Definition at line 63 of file vect.c.

{
    v1[X] = k * v2[X];
    v1[Y] = k * v2[Y];
    v1[Z] = k * v2[Z];
}

Here is the caller graph for this function:

void vect_sub ( Vector  v1,
Vector  v2,
Vector  v3 
)

Definition at line 55 of file vect.c.

{
    v1[X] = v2[X] - v3[X];
    v1[Y] = v2[Y] - v3[Y];
    v1[Z] = v2[Z] - v3[Z];
}

Here is the caller graph for this function:

void vect_transform ( Vector  v1,
Vector  v2,
Matrix  mat 
)

Definition at line 221 of file vect.c.

{
    Vector tmp;

    tmp[X] = (v2[X] * mat[0][0]) + (v2[Y] * mat[1][0]) + (v2[Z] * mat[2][0]) + mat[3][0];
    tmp[Y] = (v2[X] * mat[0][1]) + (v2[Y] * mat[1][1]) + (v2[Z] * mat[2][1]) + mat[3][1];
    tmp[Z] = (v2[X] * mat[0][2]) + (v2[Y] * mat[1][2]) + (v2[Z] * mat[2][2]) + mat[3][2];

    vect_copy (v1, tmp);
}

Here is the call graph for this function:

Here is the caller graph for this function: