hits()
model_t
object
model_t
object in the model.h
interface, providing
the following class data members:
list_t *mats;
list_t *objs;
and member functions:
// - instance methods
// constructors (overloaded)
- (id) init;
// accessor/mutator
- (list_t *) objs;
- (list_t *) mats;
// operators
// members
- (void) read: (FILE *) _in;
- (void) write: (FILE *) _out;
read:
and write:
must be implemented in the model_t
implementation mocdel.m
find_closest()
must be implemented as a protocol and so
must be implemented in the model_t
implementation mocdel.m
; this
function calls the polymorphic [obj hits: ...]
function (which must be provided by the
object_t
and plane_t
objects)
[obj hits: ...]
, find_closest()
should make sure that the object obj
conforms to the
intersecting
protocol (use conformsToProtocol
built-in member function)
main()
routine works unaltered:
int main(int argc, 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];
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:"];
obj = [model find_closest: pos: dir: nil: &dis];
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: obj: &dis];
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;
}
material green { ambient 0 5 0 } material yellow { ambient 6 5 0 } material gray { ambient 4 4 4 } plane leftwall { material green normal 3 0 1 point 0 0 0 } plane rightwall { material yellow normal -3 0 1 point 8 0 0 } plane floor { material gray normal 0 1 0 point 0 0 0 }
-0.5 -0.1 -1 0.5 -0.1 -1 0 -0.5 -1.0 1 1 1
2013-01-17 13:38:18.324 main[65437:903] main start 2013-01-17 13:38:18.326 main[65437:903] reading stdin material green { ambient 0 5 0 } material yellow { ambient 6 5 0 } material gray { ambient 4 4 4 } plane leftwall { material green normal 3 0 1 point 0 0 0 } plane rightwall { material yellow normal -3 0 1 point 8 0 0 } floor { material gray normal 0 1 0 point 0 0 0 } Ray direction: -0 -0 -1 Hit: leftwall Dist = 7.633 Loc = 1 2 -2 Hit: floor Dist = 33.675 Loc = -11 0 -25 Ray direction: 0 -0 -1 Hit: rightwall Dist = 7.633 Loc = 7 2 -2 Hit: floor Dist = 33.675 Loc = 19 0 -25 Ray direction: 0 -0 -1 Hit: floor Dist = 6.708 Loc = 4 0 -1 Hit: leftwall Dist = 19.007 Loc = 4 -6 -12 Ray direction: 1 1 1 Dist = -1.000 Dist = -1.000
tar.gz
archive of your lab11/ directory, including:
README
file containing
Makefile
.h
headers and .c
source)
make clean
before tar
)
submit
notes.