CPSC 102-001 Computer Science II
(Advanced C Programming)

Fall 2010
09:05-09:55MW Daniel 415
Lab 12

Objectives:
To implement and exercise the model_t object

What to hand in:
A tar.gz archive of your lab12/ 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 .cpp source)
  4. object code (do a make clean before tar)
How to hand in:
See sendlab notes

Description:
  1. Following the class notes, define the model_t class in the model.h interface, providing the following class member functions:
      // constructors (overloaded)
      model_t();
    
      // copy constructor
      model_t(const model_t& rhs);
    
      // destructors (default ok)
      ~model_t();
    
      // assignment operator (does nothing, just return *this)
      const model_t& operator=(const model_t& rhs);
    
      // friend i/o
      friend std::ostream& operator<<(std::ostream& s, model_t& rhs);
      friend std::ostream& operator<<(std::ostream& s, model_t *rhs)
            { return(s << (*rhs)); }
    
      friend std::istream& operator>>(std::istream& s, model_t& rhs);
      friend std::istream& operator>>(std::istream& s, model_t *rhs)
            { return(s >> (*rhs)); }
    
      // members
      object_t* find_closest(vec_t&,vec_t&,object_t*,double&);
    
      // data members
      private:
      list_t  mats;
      list_t  objs;
    		

  2. The two i/o member functions (a.k.a. methods) operator<< and operator>> must be implemented in the model_t implementation mocdel.cpp
  3. The search member function find_closest() must also be implemented in the model_t implementation mocdel.cpp; this function calls the polymorphic obj->hits() function (which must be provided by the object_t and plane_t objects)
  4. Your code should be such that the following main() routine works unaltered:
    int main(int argc, char *argv[])
    {
            model_t         model;
            object_t        *obj;
            vec_t           pos(4.0,  3.0,  5.0);
            vec_t           dir(0.0,  0.0, -1.0);
            double          dist;
            std::ifstream   model_ifs;
            std::ifstream   hits_ifs;
    
      if(argc != 3) {
        std::cerr << "Usage " << argv[0] << "  " << std::endl;
        return 1;
      }
    
      model_ifs.open(argv[1],std::ifstream::in);
      model_ifs >> model;
      model_ifs.close();
      std::cout << model;
    
      hits_ifs.open(argv[2],std::ifstream::in);
      while((hits_ifs >> dir).good()) {
    
        std::cerr << std::endl << std::endl;
    
        std::cerr << "Ray direction: " << dir << std::endl;
        dir = dir.norm();
    
        obj = model.find_closest(pos,dir,(object_t *)NULL,dist);
        if(dist > 0) {
          std::cerr << "Hit: " << obj->getname();
    
          std::cerr << " Dist = " << dist;
          std::cerr << " Loc = " << obj->getlast_hit() << std::endl;
        } else
          std::cerr << " Dist = " << dist << std::endl;
    
        std::cerr << std::endl << std::endl;
        obj = model.find_closest(pos,dir,(object_t *)obj,dist);
    
        if(dist > 0) {
          std::cerr << "Hit: " << obj->getname();
    
          std::cerr << " Dist = " << dist;
          std::cerr << " Loc = " << obj->getlast_hit() << std::endl;
        } else
          std::cerr << " Dist = " << dist << std::endl;
      }
      hits_ifs.close();
    }
    		
  5. Sample input (model.txt):
    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
    }
    		
  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
    loaded green
    loaded yellow
    loaded gray
    loaded leftwall
    loaded rightwall
    loaded floor
    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
    }
    
    
    
    Ray direction: -0.5 -0.1 -1
    Hit: leftwall Dist = 7.63298 Loc = 0.6 2.32 -1.8
    
    
    Hit: floor Dist = 33.6749 Loc = -11 0 -25
    
    
    Ray direction: 0.5 -0.1 -1
    Hit: rightwall Dist = 7.63298 Loc = 7.4 2.32 -1.8
    
    
    Hit: floor Dist = 33.6749 Loc = 19 0 -25
    
    
    Ray direction: 0 -0.5 -1
    Hit: floor Dist = 6.7082 Loc = 4 0 -1
    
    
    Hit: leftwall Dist = 19.0066 Loc = 4 -5.5 -12
    
    
    Ray direction: 1 1 1
     Dist = -1
    
    
     Dist = -1