Lab 9: object material_t

Objectives:

To port the material_t object to Objective-C.

Assignment

  1. Following the class notes, define the material_t class in the material.h interface, providing the following class data members:
      int       cookie;
      NSString *name;
      pixel_t  *ambient;
      pixel_t  *diffuse;
      pixel_t  *specular;
    	
    and member functions:
      // constructors (overloaded)
    - (id) init;
    
      // accessor/mutator
    - (NSString *) setname: (NSString *)_name;
    - (pixel_t *)  setambn: (pixel_t *)_ambn;
    - (pixel_t *)  setdiff: (pixel_t *)_diff;
    - (pixel_t *)  setspec: (pixel_t *)_spec;
    - (NSString *) getname;
    - (pixel_t *)  getambn;
    - (pixel_t *)  getdiff;
    - (pixel_t *)  getspec;
    
      // operators
    
      // members
    - (bool) matches: (NSString *) _name;
    - (void) read: (FILE *) _file;
    - (void) write: (FILE *) _file;
    	
  2. Your code should be such that the following main() routine works unaltered:
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
            pixel_t         *p1 = [(pixel_t *)[pixel_t alloc] init: 3.0: 4.0: 5.0];
            pixel_t         *p2 = [(pixel_t *)[pixel_t alloc] init];
            pixel_t         *p3 = nil, *p4 = [p1 copy];
    
            char            entity[16];
    
            list_t          *mats = [(list_t *)[list_t alloc] init];
            material_t      *mat = nil;
    
            NSMutableString *str = [[NSMutableString alloc] init];
    
      NSLog(@"main start");
    
    //[p1 set: 3.0:  4.0:  5.0];
      [p2 set: 4.0: -1.0:  2.0];
    
      [p1 write: stderr: "p1 is:"];
      [p2 write: stderr: "p2 is:"];
    
      // p3 allocated by dif operator, which we retain (must release later)
      p3 = [[p2 dif: p1] retain];
    
      [p3 write: stderr: "p2 - p1 is:"];
    
      // example of "transient" memory use where we don't retain memory
      [[p2 dif: p1] write: stderr: "p2 - p1 is:"];
    
      [p1 release];
      [p2 release];
      [p3 release];
    
      [p4 write: stderr: "p4 is:"];
      [p4 release];
    
      NSLog(@"reading stdin");
    
      // input should consist of only material definitions but
      // there can be any number of them in the file
      while(fscanf(stdin,"%s",entity)==1) {
        mat = [(material_t *)[material_t alloc] init];
        [mat read: stdin];
        [mats add: mat];
      }
    
      NSLog(@"done reading stdin");
    
      NSLog(@"printing list");
      [mats write: stdout];
    
      NSLog(@"==== searches ====");
      // this chunk uses a temporary NSString object with which to search
      // calls material_t method matches to test for equivalence
      [str setString: @"yellow"];
      NSLog(@"looking for %s",[str UTF8String]);
      [mats reset];
      while(![mats end]) {
        if([[mats data] matches: str]) {
          [[mats data] write: stderr];
          break;
        }
        [mats next];
      }
    
      // this chunk uses a temporary NSString object with which to search
      // does not call material_t matches method, does the test here
      [str setString: @"green"];
      NSLog(@"looking for %s",[str UTF8String]);
      [mats reset];
      while(![mats end]) {
        if([[[mats data] getname] isEqualToString: str]) {
          [[mats data] write: stderr];
          break;
        }
        [mats next];
      }
    
      // these chunks uses the list_t getbyname method
      [str setString: @"yellow"];
      NSLog(@"looking for %s",[str UTF8String]);
      [[mats getbyname: str] write: stderr];
    
      [str setString: @"chartreuse"];
      NSLog(@"looking for %s",[str UTF8String]);
      [[mats getbyname: str] write: stderr];
    
      [str release];
    
      [pool drain];
    
      return 0;
    	
  3. Sample input:
    material blue
    {
       ambient 0 0 5
       diffuse 0 0 5
    }
    
    material green
    {
       ambient 0 6 0
       diffuse 0 7 0
      specular 1 1 1
    }
    
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    	
  4. Sample output (note that the timestamps will differ):
    2013-01-17 11:46:21.762 main[64927:903] main start
    p1 is: 3 4 5
    p2 is: 4-1 2
    p2 - p1 is: 1-5-3
    p2 - p1 is: 1-5-3
    p4 is: 3 4 5
    2013-01-17 11:46:21.764 main[64927:903] reading stdin
    2013-01-17 11:46:21.764 main[64927:903] done reading stdin
    2013-01-17 11:46:21.765 main[64927:903] printing list
    material blue
    {
       ambient 0 0 5
       diffuse 0 0 5
    }
    
    material green
    {
       ambient 0 6 0
       diffuse 0 7 0
      specular 1 1 1
    }
    
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    }
    
    2013-01-17 11:46:21.766 main[64927:903] ==== searches ====
    2013-01-17 11:46:21.766 main[64927:903] looking for yellow
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    }
    
    2013-01-17 11:46:21.766 main[64927:903] looking for green
    material green
    {
       ambient 0 6 0
       diffuse 0 7 0
      specular 1 1 1
    }
    
    2013-01-17 11:46:21.767 main[64927:903] looking for yellow
    material yellow
    {
       ambient 7 6 0
       diffuse 7 7 0
    }
    
    2013-01-17 11:46:21.767 main[64927:903] looking for chartreuse
    	

Turn in

Turn in all of your code, in one tar.gz archive of your lab9/ 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.