CPSC 102 Computer Science II
(Advanced C Programming)

Assignment 6

Objectives:

(Redoing Lab 5 and Asg 2 in Objective-C)

To implement and exercise the camera object, moving object_find_closest to model object

Description:

  1. Following the code pattern developed so far, namely functions to initialize, load, print, and access objects, write the following functions:
    - (id) init;
    - (void) read: (FILE *) _file;
    - (void) write: (FILE *) _file;
    	
    for the camera_t object:
    @interface camera_t: NSObject   // Class: Parent
    {
      int       cookie;
      NSString *name;
      int       pixel_dim[2];
      double    world_dim[2];
      vec_t    *view_point;
    }
    	
  2. Don't forget to include a pointer to the camera object in the model object, i.e.,
    @interface model_t: NSObject         // Class: Parent
    { 
      camera_t *cam;
      list_t   *mats;
      list_t   *objs;
    }
    	
  3. The model_t object implements the searching @protocol:
    @protocol searching
    - (object_t *) find_closest: (vec_t *) pos: (vec_t *) dir:
                                (double *) dis:
                                (vec_t **) hit: (vec_t **) N;
    @end
    	
  4. Your code should be such that the following main.m routine works unaltered:
    #import <Foundation/Foundation.h>
    
    #import <stdio.h>
    #import <stdlib.h>
    #import <string.h>
    #import <assert.h>
    #import <math.h>
    
    #import <vector.h>
    #import <pixel.h>
    #import <list.h>
    #import <camera.h>
    #import <material.h>
    #import <object.h>
    #import <plane.h>
    #import <model.h>
    
    //---- prototypes ----
    int main(int argc, const char *argv[]);
    
    //---- program section ----
    
    int main(int argc, const char *argv[])
    {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
            model_t         *model = [(model_t *)[model_t alloc] init];
            FILE            *model_file=NULL, *hits_file=NULL;
    
            vec_t           *pos = [(vec_t *)[vec_t alloc] init: 4.0: 3.0:  5.0];
            vec_t           *dir = [(vec_t *)[vec_t alloc] init: 0.0: 0.0: -1.0];
    
            vec_t           *hit = [(vec_t *)[vec_t alloc] init];
            vec_t           *N = [(vec_t *)[vec_t alloc] init];
    
            double          dis = 0.0;
    
            object_t        *obj = nil;
    
      NSLog(@"main start");
      NSLog(@"reading stdin");
    
      if(argc != 3) {
        fprintf(stderr,"Usage: %s  \n",argv[0]);
        exit(1);
      }
    
      // open model file
      if((model_file = fopen(argv[1],"r")) == NULL) {
         printf("Error opening input file: %s\n",argv[1]);
         return;
      }
    
      [model read: model_file];
    
      fclose(model_file);
    
      [model write: stdout];
    
      // open hits file
      if((hits_file = fopen(argv[2],"r")) == NULL) {
         printf("Error opening input file: %s\n",argv[2]);
         return;
      }
    
      while([dir read: hits_file]) {
        dir = [dir unit];
    
        [dir write: stderr: "Ray direction:"];
    
        dis = 0.0;
        obj = [model find_closest: pos: dir: &dis: &hit: &N];
    
        if(dis > 0) {
          fprintf(stderr,"Hit: %-12s ",[[obj getname] UTF8String]);
    
          fprintf(stderr,"Dist = %8.3lf ",dis);
          [[obj getlast_hit] write: stderr: "Loc = "];
        } else
          fprintf(stderr,"Dist = %8.3lf \n", dis);
    
        fprintf(stderr, "\n\n");
    
        obj = [model find_closest: pos: dir: &dis: &hit: &N];
        if(dis > 0) {
          fprintf(stderr,"Hit: %-12s ",[[obj getname] UTF8String]);
    
          fprintf(stderr,"Dist = %8.3lf ",dis);
          [[obj getlast_hit] write: stderr: "Loc = "];
        } else
          fprintf(stderr,"Dist = %8.3lf \n", dis);
      }
    
      fclose(hits_file);
    
    
      [pool drain];
    
      return 0;
    }
    	
  5. Sample input:
    camera cam1
    {
       pixeldim 640 480
       worlddim 8   6
       viewpoint 4 3 6
    }
    
    material green
    {
       ambient 0 5 0
    }
    
    material yellow
    {
       ambient  5 4 0
       diffuse  4 4 0
      specular  1 1 1
    }
    
    plane leftwall
    {
       material green
       normal 3 0 1
       point  0 0 0
    }
    
    plane rightwall
    {
       material yellow
       normal -3 0 1
       point   8 0 0
    }
    
    material gray
    {
       ambient 2 2 2
    }
    
    plane floor
    {
       material gray
       normal 0 1 0
       point  0 -0.2 0
    }
    	
  6. Sample input (hittest.txt):
    -0.5 -0.1 -1
     0.5 -0.1 -1
     0  -0.5 -1.0
     1   1   1
    	
  7. Sample output (note that the timestamps will differ):
    2013-02-12 11:51:32.078 main[60168:903] main start
    2013-02-12 11:51:32.080 main[60168:903] reading stdin
    camera cam1
    {
       pixeldim 640 480
       worlddim   8   6
       viewpoint   4.000   3.000   6.000
    }
    
    material green
    {
       ambient 0 5 0
    }
    
    material yellow
    {
       ambient 5 4 0
       diffuse 4 4 0
      specular 1 1 1
    }
    
    material gray
    {
       ambient 2 2 2
    }
    
    plane leftwall
    {
       material green
       normal   3.000   0.000   1.000
       point   0.000   0.000   0.000
    }
    
    plane rightwall
    {
       material yellow
       normal    -3.000   0.000   1.000
       point   8.000   0.000   0.000
    }
    
    plane floor
    {
       material gray
       normal   0.000   1.000   0.000
       point   0.000    -0.200   0.000
    }
    
    Ray direction:    -0.445    -0.089    -0.891
    Hit: leftwall     Dist =    7.633 Loc =    0.600   2.320    -1.800
    
    
    Hit: leftwall     Dist =   15.266 Loc =    0.600   2.320    -1.800
    Ray direction:   0.445    -0.089    -0.891
    Hit: rightwall    Dist =    7.633 Loc =    7.400   2.320    -1.800
    
    
    Hit: rightwall    Dist =   15.266 Loc =    7.400   2.320    -1.800
    Ray direction:   0.000    -0.447    -0.894
    Hit: floor        Dist =    7.155 Loc =    4.000    -0.200    -1.400
    
    
    Hit: floor        Dist =   14.311 Loc =    4.000    -0.200    -1.400
    Ray direction:   0.577   0.577   0.577
    Dist =    0.000 
    
    
    Dist =    0.000 
    	

What to hand in:

A tar.gz archive of your asg6/ directory, including:
  1. A README file containing
    1. Course id--section no
    2. Name
    3. Assignment 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.