Lab 6: vec_xform() and mat_xpose()

Objectives

Augment the libvec.a library with matrix.o, which is the object file obtained by compiling matrix.c.

Assignment

  1. The matrix.h header file contains:
    
    typedef double mtx_t[3][3];
    
    void vec_xform(mtx_t,vec_t,vec_t);
    void mat_xpose(mtx_t m1,mtx_t m2);
    	
  2. Write the corresponding matrix.c implementation file that provides the functionality the above Application Program Interface (API) specifies.
  3. You code should be such that the following main.c file works unaltered:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>
    #include <math.h>
    
    #include "vector.h"
    #include "matrix.h"
    #include "pixel.h"
    #include "list.h"
    #include "material.h"
    #include "object.h"
    #include "model.h"
    
    int main()
    {
            vec_t   v1 = {1.0, 0.0, 1.0};
            vec_t   v2 = {1.0, 1.0, 1.0};
            vec_t   v7 = {2.0, 1.0, 0.5};
            vec_t   v3;
            vec_t   v4;
            vec_t   v5;
            mtx_t   m1;
            mtx_t   m2;
    
      // convert initial vectors to unit vecs and print
      vec_unit(v1, v1);
      vec_unit(v2, v2);
    
      vec_print(stderr, "v1 ", v1);
      vec_print(stderr, "v2 ", v2);
      fprintf(stderr, "\n");
    
      vec_cross(v2, v7, v7);
      fprintf(stderr, "\n   Cross product\n");
      vec_print(stderr, "v7 ", v7);
    
      // reflect -v7 off plane whose normal is v2
      vec_unit(v7, v7);
      vec_reflect(v2, v7, v7);
      fprintf(stderr, "\n   Reflected vector\n");
      vec_print(stderr,"v7 ", v7);
    
      // project v1 onto plane whose normal is v2
      vec_project(v2, v1, v5);
      vec_unit(v5, v5);
      fprintf(stderr, "\n   Projected vector\n");
      vec_print(stderr, "v5 ", v5);
      fprintf(stderr, "\n");
    
      // now create a matrix that will rotate v2 into the z-axis
      // v1 into the x = 0 plane with positive y component
      vec_cross(v1, v2, m1[0]);
      vec_unit(m1[0], m1[0]);  // DON"T FORGET ME
    
      vec_copy(v2, m1[2]);
      vec_cross(m1[2], m1[0], m1[1]);
    
      fprintf(stderr,"   Rotation Matrix \n");
      vec_print(stderr, "r0 ", m1[0]);
      vec_print(stderr, "r1 ", m1[1]);
      vec_print(stderr, "r2 ", m1[2]);
      fprintf(stderr, "\n");
    
      // verify that it worked
      vec_xform(m1, v1, v3);
      vec_xform(m1, v2, v4);
    
      fprintf(stderr, "   Transformed v1 v2\n");
      vec_print(stderr, "v3 ", v3);
      vec_print(stderr, "v4 ", v4);
      fprintf(stderr, "\n");
    
      // build the inverse of the rotation
      mat_xpose(m1, m2);
    
      fprintf(stderr,"   Transpose Matrix \n");
      vec_print(stderr, "r0 ", m2[0]);
      vec_print(stderr, "r1 ", m2[1]);
      vec_print(stderr, "r2 ", m2[2]);
      fprintf(stderr,"\n");
    
      // and verify that it all worked
      vec_xform(m2, v3, v3);
      vec_xform(m2, v4, v4);
    
      fprintf(stderr, "   Inverse transform\n");
      vec_print(stderr, "v3 ", v3);
      vec_print(stderr, "v4 ", v4);
      fprintf(stderr, "\n");
    
      return(0);
    }
    	
  4. Sample output:
    v1    0.707   0.000   0.707
    v2    0.577   0.577   0.577
    
    
       Cross product
    v7   -0.289   0.866  -0.577
    
       Reflected vector
    v7   -0.267   0.802  -0.535
    
       Projected vector
    v5    0.408  -0.816   0.408
    
       Rotation Matrix 
    r0   -0.707   0.000   0.707
    r1    0.408  -0.816   0.408
    r2    0.577   0.577   0.577
    
       Transformed v1 v2
    v3    0.000   0.577   0.816
    v4    0.000   0.000   1.000
    
       Transpose Matrix 
    r0   -0.707   0.408   0.577
    r1    0.000  -0.816   0.577
    r2    0.707   0.408   0.577
    
       Inverse transform
    v3    0.707  -0.000   0.707
    v4    0.577   0.577   0.577
    	

Turn in

Turn in all of your code, in one tar.gz archive of your lab06/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Lab description
    4. Brief solution description (e.g., program design, description of algorithm, etc., however appropriate).
    5. Lessons learned, identified interesting features of your program
    6. Any special usage instructions
  2. Makefile
  3. source code (.h headers and .c source)
  4. object code (do a make clean before tar)

How to hand in

See submit notes.