Beginning of using ObjectList and OctTreeNode in storing a mesh's
Beginning of using ObjectList and OctTreeNode in storing a mesh's
triangles.

--- a/AxisAlignedBox.cpp
+++ b/AxisAlignedBox.cpp
@@ -43,7 +43,7 @@
     _maxPoint.setZ(p.z());
 }
 
-Vector AxisAlignedBox::intersection(Ray ray) const
+IntersectionInformation AxisAlignedBox::intersection(Ray ray) const
 { 
   std::vector<Vector> intersections;
   
@@ -146,7 +146,13 @@
 	}
     }
 
-  return smallestInter;
+  if(smallestDistance==-1)
+    return IntersectionInformation();
+  else
+    {
+      IntersectionInformation info(ray,smallestInter,(Object*)this);
+      return info;
+    }
 }
 
 void AxisAlignedBox::fillIntersectionInformation(IntersectionInformation *info) const
@@ -209,7 +215,6 @@
   applyNormalTransforms(info);
   applyColorInformation(info);
 
-
   info->setComplete(true);
 }
 

--- a/AxisAlignedBox.h
+++ b/AxisAlignedBox.h
@@ -10,7 +10,7 @@
   AxisAlignedBox(Vector minPoint,Vector maxPoint);
 
   void fillIntersectionInformation(IntersectionInformation *info) const;
-  Vector intersection(Ray ray) const;  
+  IntersectionInformation intersection(Ray ray) const;  
 
   Vector minPoint() const;
   Vector maxPoint() const;

--- a/IntersectionInformation.cpp
+++ b/IntersectionInformation.cpp
@@ -4,29 +4,32 @@
 #include <iostream>
 
 IntersectionInformation::IntersectionInformation()
-  : _object(0),_complete(false)
+  : _object(0),_complete(false),_data(0)
 { }
 
-IntersectionInformation::IntersectionInformation(Object *obj)
-  : _object(obj),_complete(false)
+IntersectionInformation::IntersectionInformation(Ray ray,Vector xyz,Object *obj)
+  : _ray(ray),_xyz(xyz),_object(obj),_complete(false),_data(0)
 { }
 
-bool IntersectionInformation::intersects()
+bool IntersectionInformation::intersects() const
 {
   return _object!=0;
 }
 
-Object* IntersectionInformation::object()
+Object* IntersectionInformation::object() const
 {
   return _object;
 }
+
+Ray IntersectionInformation::ray() const
+{ return _ray; }
 
 void IntersectionInformation::setComplete(bool complete)
 {
   _complete=complete;
 }
 
-bool IntersectionInformation::complete()
+bool IntersectionInformation::complete() const
 {
   return _complete;
 }
@@ -42,13 +45,13 @@
   _xyz=xyz; 
 }
 
-Vector IntersectionInformation::xyz()
+Vector IntersectionInformation::xyz() const
 { return _xyz; }
 
 void IntersectionInformation::setUvw(Vector uvw)
 { _uvw=uvw; }
 
-Vector IntersectionInformation::uvw()
+Vector IntersectionInformation::uvw() const
 { return _uvw; }
 
 void IntersectionInformation::setNormal(Vector normal)
@@ -57,24 +60,30 @@
   _normal.normalize();
 }
 
-Vector IntersectionInformation::normal()
+Vector IntersectionInformation::normal() const
 { return _normal; }
 
 void IntersectionInformation::setColor(Color color)
 { _color=color; }
 
-Color IntersectionInformation::color()
+Color IntersectionInformation::color() const
 { return _color; }
 
 void IntersectionInformation::setTangentU(Vector tangentU)
 { _tangentU=tangentU; }
 
-Vector IntersectionInformation::tangentU()
+Vector IntersectionInformation::tangentU() const
 { return _tangentU; }
 
 void IntersectionInformation::setTangentV(Vector tangentV)
 { _tangentV=tangentV; }
 
-Vector IntersectionInformation::tangentV()
+Vector IntersectionInformation::tangentV() const
 { return _tangentV; }
 
+void* IntersectionInformation::data() const
+{ return _data; }
+
+void IntersectionInformation::setData(void *data)
+{ _data=data; }
+

--- a/IntersectionInformation.h
+++ b/IntersectionInformation.h
@@ -11,32 +11,37 @@
 {
 public:
   IntersectionInformation();
-  IntersectionInformation(Object *obj);
+  IntersectionInformation(Ray ray,Vector xyz,Object *obj);
 
-  bool intersects();
+  bool intersects() const;
 
   void setComplete(bool complete);
-  bool complete();
+  bool complete() const;
   void calculateComplete();
   
-  Object* object();
+  Ray ray() const;
+
+  Object* object() const;
 
   void setXyz(Vector xyz);
-  Vector xyz();
+  Vector xyz() const;
 
   void setUvw(Vector uvw);
-  Vector uvw();
+  Vector uvw() const;
 
   void setNormal(Vector normal);
-  Vector normal();
+  Vector normal() const;
 
   void setColor(Color color);
-  Color color();
+  Color color() const;
 
   void setTangentU(Vector tangentU);
-  Vector tangentU();
+  Vector tangentU() const;
   void setTangentV(Vector tangentV);
-  Vector tangentV();
+  Vector tangentV() const;
+
+  void* data() const;
+  void setData(void *data);
 
 private:
   Vector _xyz;
@@ -48,8 +53,10 @@
 
   Color _color;
 
+  Ray _ray;
   Object *_object;
   bool _complete;
+  void *_data;
 };
 
 #endif // INTERSECTIONINFORMATION_H_

file:a/Mesh.cpp -> file:b/Mesh.cpp
--- a/Mesh.cpp
+++ b/Mesh.cpp
@@ -1,5 +1,4 @@
 #include "Mesh.h"
-
 #include "config.h"
 
 #include <fstream>
@@ -16,54 +15,29 @@
   loadFile(filename);
 }
 
-Vector Mesh::intersection(Ray ray) const
+IntersectionInformation Mesh::intersection(Ray ray) const
 {
-  double distanceToClosest=-1;
-  Vector point;
+  IntersectionInformation triinfo=_triangles.intersection(ray);
 
-  if(_triangles.size()>6)
+  if(triinfo.intersects())
     {
-      Vector bbtest=_boundingBox.intersection(ray);
-      if(bbtest.isNull()) return Vector();
+      IntersectionInformation info(ray,
+				   triinfo.xyz(),
+				   (Object*)this);
+      info.setData(triinfo.object());
+      return info;
     }
-  
-  for(std::vector<Triangle>::const_iterator iter=_triangles.begin();
-      iter!=_triangles.end();
-      iter++)
-    {
-      Vector currentIntersection=iter->intersection(ray);
-      if(!currentIntersection.isNull())
-	{
-	  double distance=ray.distanceToPoint(currentIntersection);
-	  if(distance<distanceToClosest ||
-	     distanceToClosest == -1)
-	    {
-	      distanceToClosest=distance;
-	      point=currentIntersection;
-	    }
-	}
-    }
-
-  return point;
+  else
+    return IntersectionInformation();
 }
 
 void Mesh::fillIntersectionInformation(IntersectionInformation *info) const
 {
-  Vector point=info->xyz();
-  
-  for(std::vector<Triangle>::const_iterator iter=_triangles.begin();
-      iter!=_triangles.end();
-      iter++)
-    {
-      if(iter->contains(point))
-	{
-	  iter->fillIntersectionInformation(info);
-	  applyNormalTransforms(info);
-	  applyColorInformation(info);
-	  info->setComplete(true);
-	  return;
-	}
-    }
+  Triangle *triangle=(Triangle*)info->data();
+  triangle->fillIntersectionInformation(info);
+  applyNormalTransforms(info);
+  applyColorInformation(info);
+  info->setComplete(true);
 }
 
 Vector Mesh::minPoint() const
@@ -118,17 +92,17 @@
 
 	  for(unsigned int i=1;i<localVertices.size()-1;i++)
 	    {
-	      Triangle t(localVertices[0],
-			 localVertices[i],
-			 localVertices[i+1]);
+	      Triangle *t=new Triangle(localVertices[0],
+				       localVertices[i],
+				       localVertices[i+1]);
 
 	      /*std::cout << t.area() << std::endl;
 	      if(t.area()<EPSILON)
 		{
 		  std::cerr << "Ignoring zero area triangle" << std::endl;
 		}*/
-	      if(t.area()>EPSILON)
-		_triangles.push_back(t);
+	      if(t->area()>EPSILON)
+		_triangles.addObject(t);
 	    }
 	}
       else if(field=="vt")
@@ -141,6 +115,9 @@
     }
 
   fh.close();
+
+  //_octTree=new OctTreeNode(_triangles);
+  _triangles.optimize();
 }
 
 Vertex Mesh::stringToVertex(std::string str)

file:a/Mesh.h -> file:b/Mesh.h
--- a/Mesh.h
+++ b/Mesh.h
@@ -9,6 +9,8 @@
 #include "Vertex.h"
 #include "Triangle.h"
 #include "AxisAlignedBox.h"
+#include "OctTreeNode.h"
+#include "ObjectList.h"
 
 /**
  * An object that uses the points and edges defined in a
@@ -20,15 +22,17 @@
   Mesh(std::string filename);
 
   void fillIntersectionInformation(IntersectionInformation *info) const;
-  Vector intersection(Ray ray) const; 
+  IntersectionInformation intersection(Ray ray) const; 
 
   Vector minPoint() const;
   Vector maxPoint() const;
 
 private:
   std::vector<Vector> _points;
-  std::vector<Triangle> _triangles;
+  ObjectList _triangles;
   std::vector<Vector> _textureVertices;
+
+  OctTreeNode *_octTree;
 
   AxisAlignedBox _boundingBox;
 

file:a/Object.h -> file:b/Object.h
--- a/Object.h
+++ b/Object.h
@@ -14,7 +14,7 @@
   Material* material() const;
   
   virtual void fillIntersectionInformation(IntersectionInformation *info) const =0;
-  virtual Vector intersection(Ray ray) const =0;
+  virtual IntersectionInformation intersection(Ray ray) const =0;
   virtual Vector minPoint() const =0;
   virtual Vector maxPoint() const =0;
 

--- a/ObjectList.cpp
+++ b/ObjectList.cpp
@@ -39,3 +39,6 @@
   return _octTree->intersection(ray);
 }
 
+std::vector<Object*> ObjectList::objects()
+{ return _objects; }
+

--- a/ObjectList.h
+++ b/ObjectList.h
@@ -16,6 +16,8 @@
   ObjectList();
   ~ObjectList();
 
+  std::vector<Object*> objects();
+
   void addObject(Object *obj);
   void optimize();
 

--- a/OctTreeNode.cpp
+++ b/OctTreeNode.cpp
@@ -98,15 +98,15 @@
   // Find the object that this ray intersects with
   double smallestDistance=-1;
   Object *closestObject=0;
-  Vector closestIntersection;
+  IntersectionInformation closestIntersection;
   for(std::vector<Object *>::const_iterator iter=_objects.begin();
       iter!=_objects.end();
       iter++)
     {
-      Vector currentIntersection=(*iter)->intersection(ray);
-      if(!currentIntersection.isNull())
+      IntersectionInformation currentIntersection=(*iter)->intersection(ray);
+      if(currentIntersection.intersects())
 	{
-	  double distanceToIntersection=ray.distanceToPoint(currentIntersection);
+	  double distanceToIntersection=ray.distanceToPoint(currentIntersection.xyz());
 	  //printf("distanceToIntersection\t=\t%f\n",distanceToIntersection);
 	  if(closestObject==0 || distanceToIntersection<smallestDistance)
 	    {
@@ -119,20 +119,13 @@
   
   //std::cout << "closestObject=" << closestObject << std::endl;
   
-  if(!closestObject)
-    return IntersectionInformation();
-  else
-    {
-      IntersectionInformation info(closestObject);
-      info.setXyz(closestIntersection);
-      return info;
-    }
+  return closestIntersection;
 }
 
 IntersectionInformation OctTreeNode::nodeIntersection(Ray ray) const
 {
-  Vector bbtest=_boundingBox.intersection(ray);
-  if(bbtest.isNull()) return IntersectionInformation();
+  IntersectionInformation bbtest=_boundingBox.intersection(ray);
+  if(!bbtest.intersects()) return IntersectionInformation();
   
   // Find the object that this ray intersects with
   double smallestDistance=-1;

--- a/PointLight.cpp
+++ b/PointLight.cpp
@@ -45,7 +45,7 @@
   Ray lightRay(pos,l);
 
   IntersectionInformation info=objects.intersection(lightRay);
-  //std::cout << "xyz = " << (std::string)intersection.xyz() << std::endl;
+  //std::cout << "xyz = " << (std::string)info.xyz() << std::endl;
   if(!info.intersects())
     return 0; // No intersection == no occlusion
 

file:a/Scene.cpp -> file:b/Scene.cpp
--- a/Scene.cpp
+++ b/Scene.cpp
@@ -275,6 +275,8 @@
 	{
 	  double power=1; // The power of the light. 0 if there are non-transparent objects in between
 	  Object *occludedBy=(*iter)->occludedBy(_objects,xyz);
+
+	  //std::cout << "occludedBy=" << occludedBy << std::endl;
 	  if(occludedBy)
 	    power*=occludedBy->material()->T();
 

file:a/Sphere.cpp -> file:b/Sphere.cpp
--- a/Sphere.cpp
+++ b/Sphere.cpp
@@ -9,7 +9,7 @@
   : Object(),_center(center),_radius(radius)
 { }
 
-Vector Sphere::intersection(Ray ray) const
+IntersectionInformation Sphere::intersection(Ray ray) const
 {
   // The itersection of a sphere and ray works by calculation the
   // value of the parameter of t (parametrizes ray) for which the
@@ -42,7 +42,7 @@
   // The thing under the square root is negative == no intersection!
   //printf("d\t=\t%f\n",d);
   if(d<0)
-    return Vector();
+    return IntersectionInformation();
 
   // two intersections
   double t1=(-b+sqrt(d))/(2*a);
@@ -57,11 +57,11 @@
   //std::cout << "v2\t=\t" << (std::string)v2 << std::endl;
   
   if((t1<t2 || t2<=EPSILON) && v1!=ray.start() && t1>EPSILON)
-    return v1;
+    return IntersectionInformation(ray,v1,(Object*)this);
   else if((t2<t1 || t1<=EPSILON) && v2!=ray.start() && t2>EPSILON)
-    return v2;
+    return IntersectionInformation(ray,v2,(Object*)this);
   else
-    return Vector();
+    return IntersectionInformation();
 
 }
 

file:a/Sphere.h -> file:b/Sphere.h
--- a/Sphere.h
+++ b/Sphere.h
@@ -11,7 +11,7 @@
   Sphere(Vector center,double radius);
 
   void fillIntersectionInformation(IntersectionInformation *info) const;
-  Vector intersection(Ray ray) const;
+  IntersectionInformation intersection(Ray ray) const;
 
   Vector minPoint() const;
   Vector maxPoint() const;

--- a/Triangle.cpp
+++ b/Triangle.cpp
@@ -21,7 +21,7 @@
 	 );*/
 }
 
-Vector Triangle::intersection(Ray ray) const
+IntersectionInformation Triangle::intersection(Ray ray) const
 {
   //std::cout << "Triangle::intersection(ray)" << std::endl;
   // The triangle intersection with ray calculation works
@@ -32,18 +32,18 @@
 
   double denominator=_normal.dot(ray.direction());
   if(fabs(denominator)<EPSILON) // Ray parallel to plane, of course no intersection
-    return Vector();
+    return IntersectionInformation();
 
   double t=_normal.dot(_v1.xyz()-ray.start())/denominator;
   if(t<=EPSILON) // Intersection is behind ray start
-    return Vector();
+    return IntersectionInformation();
 
   Vector inter=ray.eval(t);
 
   if(contains(inter))
-    return inter;
+    return IntersectionInformation(ray,inter,(Object*)this);
   else
-    return Vector();
+    return IntersectionInformation();
 }
 
 bool Triangle::contains(Vector point) const

file:a/Triangle.h -> file:b/Triangle.h
--- a/Triangle.h
+++ b/Triangle.h
@@ -11,7 +11,7 @@
   Triangle(Vertex v1,Vertex v2,Vertex v3);
 
   void fillIntersectionInformation(IntersectionInformation *info) const;
-  Vector intersection(Ray ray) const;
+  IntersectionInformation intersection(Ray ray) const;
 
   Vector minPoint() const;
   Vector maxPoint() const;

--- a/tests/axisalignedboxtest1.txt
+++ b/tests/axisalignedboxtest1.txt
@@ -19,6 +19,6 @@
 point_light   5 5 5 -5 0 0
 point_light   5 5 5 5 0 0
 
-trace_depth 6
+trace_depth 0
 render axisalignedboxtest1.ppm