Computer Science 102

Lab 6

In this lab you will augment your vector library with some additional functions. A sample main.c and sample output file are provided for you. These are not guaranteed to represent a complete test of the components. You must use the following matrix structure:

typedef struct matrix_type

{

vec_t row[3];

} mat_t;

Functions that you must provide are:

/**/

/* Compute the outer product of two input vectors */

static inline void vec_cross(

vec_t *v1, /* Left input vector */

vec_t *v2, /* Right input vector */

vec_t *v3); /* Output vector */

V x W = (vy wz - vz wy, vz wx - vx wz , vx wy - vy wx)

/**/

/* project a vector onto a plane */

static inline void vec_project(

vec_t *n, /* plane normal */

vec_t *v, /* input vector */

vec_t *p); /* projected vector */

The projection of a vector V onto a plane with normal N is given by:

P = V - (N dot V) N

/**/

/* reflect a vector from a surface plane */

static inline void vec_project(

vec_t *n, /* surface normal */

vec_t *w, /* incoming ray vector */

vec_t *v); /* reflected vector */

If U is a unit vector in the reverse direction of the incoming ray and N is the surface normal, then the direction of the reflected ray V is

V = 2 N (U dot N) – U

You must reverse the input vector W to create the necessary vector U

/* Apply transform matrix to vector */

static inline void vec_xform(

mat_t *m,

vec_t *v1,

vec_t *v2);

For i = 0, 1, 2, the ith element of v2 is the dot product of the ith row of m with v1.

/* Compute the transpose of a matrix */

static inline void mat_transpose(

mat_t *m1,

mat_t *m2);

The transpose of a three by three matrix is also three by the matrix. Its elements are given by a simple rule:

transpose[i][j] = original[j][i]

T

1.0 3.0 2.0 1.0 1.0 -2.0

1.0 2.0 -2.0 = 3.0 2.0 0.0

-2.0 0.0 1.0 2.0 -2.0 1.0

In this lab you will submit a single file, vector.h that includes both your original vector functions and the new ones constructed as part of this lab.

sendlab.102.labsection# lab# vector.h

Mike Westall

2007-01-17T09:19:32