CPSC 102 Computer Science II
(Advanced C Programming)

Assignment 5

Objectives:

Redo the C plane_hits function in Objective-C

Description:

  1. Redo Asg 1 in Objective-C by implementing the plane_t object's hits method. Recall that because plane_t subscribes to object_t <intersecting> protocol, it must implement the hits method:
    	- (double) hits: (vec_t *) _pos: (vec_t *) _dir: (vec_t **) _hit: (vec_t **) _N;
    	
  2. The test_hits() function is now object-oriented in the sense that it does not set the obj's last_hit data member to 0 via memset, but rather asks obj to set its last_hit to 0.0: 0.0: 0.0 (internally, object_t asks its last_hit, which is a vec_t to set itself via the latter's set: method).

    UPDATE: Since rewriting the object_find_closest and the hits functions, we don't really care anymore about the object's last_hit data member. It's still there for legacy reasons but may probably be removed without harm.

    void test_hits(object_t *obj, vec_t *pos, vec_t *dir)
    {
            double  dis=0.0;
            vec_t   *hit = [(vec_t *)[vec_t alloc] init];
            vec_t   *N = [(vec_t *)[vec_t alloc] init];
    
      [obj setlast_hit: 0.0: 0.0: 0.0];
    
      dis = [obj hits: pos: [dir unit]: &hit:  &N];
    
      fprintf(stderr,"dis to plane %s %8.3lf \n",[[obj getname] UTF8String],dis);
    
    // old way of doing it
    //[[obj getlast_hit] write: stderr: "hit point"];
    
    // new way
      [hit write: stderr: "hit point"];
    }
    	
  3. The main() function now makes use of Objective-C objects:
    
    int main(int argc, const char *argv[])
    {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
            list_t          *mats = [(list_t *)[list_t alloc] init];
            list_t          *objs = [(list_t *)[list_t alloc] init];
            material_t      *mat = nil;
            object_t        *obj = nil;
    
            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];
    
            int             firsttime=1;
    
            char            token[16];
    
            NSMutableString *str = nil;
    
      NSLog(@"main start");
      NSLog(@"reading stdin");
    
      // input consist of material or object definitions and
      // there can be any number of them in the file
      while(fscanf(stdin,"%s",token)==1) {
    
        if(!strcmp(token,"material")) {
          // create material_t object, read attributes, add to list
          mat = [(material_t *)[material_t alloc] init];
          [mat read: stdin];
          [mats add: mat];
    
          // this test is designed to ensure that list_add
          // pointed current to the material just loaded
          mat = [mats data];
          assert([mat getcookie] == MAT_COOKIE);
          fprintf(stderr,"loaded %s\n",[[mat getname] UTF8String]);
        }
    
        if(!strcmp(token,"plane")) {
          // create plane_t object, read attributes, add to list
          obj = [(plane_t *)[plane_t alloc] init];
          str = [[NSMutableString alloc] initWithUTF8String: token];
          [obj settype: str];
          [obj read: stdin: mats];
          [objs add: obj];
    
          // this test is designed to ensure that list_add
          // pointed current to the material just loaded
          obj = [objs data];
          assert([obj getcookie] == OBJ_COOKIE);
          fprintf(stderr,"loaded %s\n",[[obj getname] UTF8String]);
    
          // test hits function
          [pos set:  4.0:  3.0:  5.0];
          [dir set:  0.0:  0.0: -1.0];
          test_hits(obj,pos,dir);
    
          if(firsttime) {
            // make sure we don't get a hit on a miss..shoot
            // straight down at backwall
            fprintf(stderr,"vertical ray test\n");
            [pos set:  4.0:  3.0:  5.0];
            [dir set:  0.0: -2.1:  0.0];
            test_hits(obj,pos,dir);
            firsttime = 1-firsttime;
          } else {
            // make sure we don't get a hit in +z space
            fprintf(stderr,"positive z test\n");
            [pos set:  4.0:  3.0:  5.0];
            [dir set:  0.0: -2.1: -1.0];
            test_hits(obj,pos,dir);
          }
        }
      }
    
      NSLog(@"done reading stdin");
    
      [mats write: stdout];
      [objs write: stdout];
    
      [str release];
    
      [pool drain];
    
      return 0;
    }
    	
  4. Sample input:
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
       material green
       normal 0 0 1
       point  0 0 -7
    }
    
    plane floor
    {
       material brown
       normal 0 1 1
       point  0 0 -7
    }
    	
  5. Sample output:
    2013-02-12 11:07:53.511 main[59704:903] main start
    2013-02-12 11:07:53.513 main[59704:903] reading stdin
    loaded green
    loaded brown
    loaded wall
    dis to plane wall   12.000 
    hit point 4 3  -7
    vertical ray test
    dis to plane wall   -1.000 
    hit point 0 0 0
    loaded floor
    dis to plane floor   15.000 
    hit point 4 3 -10
    positive z test
    dis to plane floor   -1.000 
    hit point 0 0 0
    2013-02-12 11:07:53.514 main[59704:903] done reading stdin
    material green
    {
       ambient 0 5 0
    }
    
    material brown
    {
       ambient 3 3 0
    }
    
    plane wall
    {
       material green
       normal 0 0 1
       point 0 0  -7
    }
    
    plane floor
    {
       material brown
       normal 0 1 1
       point 0 0  -7
    }
    	

What to hand in:

A tar.gz archive of your asg5/ 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.