tests and fixes for vector implementation
authorRoland Hieber <rohieb@yoda.(none)>
Wed, 13 Jan 2010 02:23:05 +0000 (03:23 +0100)
committerRoland Hieber <rohieb@yoda.(none)>
Wed, 13 Jan 2010 02:23:05 +0000 (03:23 +0100)
MicroTrace.cxx
Vec3f.cxx
Vec3f.hxx

index d0b1d6e..feb1eeb 100644 (file)
@@ -1,4 +1,5 @@
 #include <string>
+#include <iostream>
 
 #include "Vec3f.hxx"
 //#include "Sphere.hxx"
 #include "Image.hxx"
 #include "PerspectiveCamera.hxx"
 
-void RenderFrame(Camera &camera,
-                const std::string& fileName)
-{
+void RenderFrame(Camera &camera, const std::string& fileName) {
   /* scene objects */
 
   /*
-    Sphere s1(Vec3f(-2,1.7,0),2);
-    Sphere s2(Vec3f(1,-1,1),2.2);
-    Sphere s3(Vec3f(3,0.8,-2),2);*/
+   Sphere s1(Vec3f(-2,1.7,0),2);
+   Sphere s2(Vec3f(1,-1,1),2.2);
+   Sphere s3(Vec3f(3,0.8,-2),2);*/
   //InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0));
 
   /*
-    Triangle t1(Vec3f(-2,3.7,0),Vec3f(1,2,1),Vec3f(3,2.8,-2)); 
-    Triangle t2(Vec3f(3,2,3),Vec3f(3,2,-3),Vec3f(-3,2,-3));    
-  */
-  Image img(camera.resX(),camera.resY()); // image array
-  Ray ray;                            // primary ray
-  
-  for(int y=0;y<camera.resY();y++)
-    for (int x=0;x<camera.resX();x++) 
-      {
-       
-       /* Initialize your ray here */
-       
-       camera.InitRay(x+0.5,y+0.5,ray); // initialize ray
-       
-       Vec3f col = Vec3f(0,0,0); // background color
-       
-       /*      
-               if (s1.Intersect(ray))
-               col = Vec3f(1,0,0);
-               if (s2.Intersect(ray))
-               col = Vec3f(0,1,0);
-               if (s3.Intersect(ray))
-               col = Vec3f(0,0,1);
-               if (p1.Intersect(ray))
-               col = Vec3f(1,1,0);
-               if (t1.Intersect(ray))
-               col = Vec3f(0,1,1);
-               if (t2.Intersect(ray))
-               col = Vec3f(1,1,1);
-               
-               img(x,y) = col; // store pixel color
-       */
-      }
+   Triangle t1(Vec3f(-2,3.7,0),Vec3f(1,2,1),Vec3f(3,2.8,-2));
+   Triangle t2(Vec3f(3,2,3),Vec3f(3,2,-3),Vec3f(-3,2,-3));
+   */
+  Image img(camera.resX(), camera.resY()); // image array
+  Ray ray; // primary ray
+
+  for(int y = 0; y < camera.resY(); y++)
+    for(int x = 0; x < camera.resX(); x++) {
+
+      /* Initialize your ray here */
+
+      camera.InitRay(x + 0.5, y + 0.5, ray); // initialize ray
+
+      Vec3f col = Vec3f(0, 0, 0); // background color
+
+      /*
+       if (s1.Intersect(ray))
+       col = Vec3f(1,0,0);
+       if (s2.Intersect(ray))
+       col = Vec3f(0,1,0);
+       if (s3.Intersect(ray))
+       col = Vec3f(0,0,1);
+       if (p1.Intersect(ray))
+       col = Vec3f(1,1,0);
+       if (t1.Intersect(ray))
+       col = Vec3f(0,1,1);
+       if (t2.Intersect(ray))
+       col = Vec3f(1,1,1);
+
+       img(x,y) = col; // store pixel color
+       */
+    }
   img.WritePPM(fileName); // write final image
 }
 
 #define RESX 640 // image x-resolution
 #define RESY 480 // image y-resolution
+using namespace std;
 
-int main(int, char**)
-{
-  /* render three images with different camera settings */
+int main(int, char**) {
+  // test vector implementation
 
-  PerspectiveCamera c1(Vec3f(0,0,10),Vec3f(0,0,-1),Vec3f(0,1,0),60,RESX,RESY);
-  RenderFrame(c1,"perspective1.ppm");
+  Vec3f bar(1, 4, 5), foo(3, 2, 1);
+  cout << "Using example vector bar=" << bar << ", foo=" << foo << endl;
+  cout << "bar | foo = " << (bar | foo) << ", should be 16" << endl;
+  cout << "bar | bar = " << (bar | bar) << ", should be 42" << endl;
+  cout << "foo | foo = " << (foo | foo) << ", should be 14" << endl;
+  cout << "bar % foo = " << (bar % foo) << ", should be (-6,14,-10)" << endl;
+  cout << "bar % bar = " << (bar % bar) << ", should be (0,0,0)" << endl;
+  cout << "foo % foo = " << (foo % foo) << ", should be (0,0,0)" << endl;
+  cout << "bar.norm() = " << bar.norm() << ", should be 6.48" << endl;
+  cout << "foo.norm() = " << foo.norm() << ", should be 3.74" << endl;
+  cout << "bar*5 = " << (bar * 5) << ", should be (5,20,25)" << endl;
+  cout << "bar/5 = " << (bar / 5) << ", should be (0.2,0.8,1)" << endl;
+  cout << "bar + foo = " << (bar + foo) << ", should be (4,6,6)" << endl;
+  cout << "bar - foo = " << (bar - foo) << ", should be (-2,2,4)" << endl;
+  cout << "foo - bar = " << (foo - bar) << ", should be (2,-2,-4)" << endl;
+  cout << "bar * foo = " << (bar * foo) << ", should be (3,8,5)" << endl;
+  cout << "bar / foo = " << (bar / foo) << ", should be (0.33,2,5)" << endl;
+  cout << "foo / bar = " << (foo / bar) << ", should be (3,0.5,0.2)" << endl;
+
+  cout << "bar *= 4: " << (bar *= 4) << ", should be (4,16,20)" << endl;
+  cout << "bar /= 2: " << (bar /= 2) << ", should be (2,8,10)" << endl;
+  cout << "bar += foo: " << (bar += foo) << ", should be (5,10,11)" << endl;
+  cout << "bar -= Vec3f(5,6,3): " << (bar -= Vec3f(5, 6, 3))
+    << ", should be (0,4,8)" << endl;
+
+  cout << "bar[0] = " << bar[0] << ", should be 0" << endl;
+  cout << "bar[1] = " << bar[1] << ", should be 4" << endl;
+  cout << "bar[2] = " << bar[2] << ", should be 8" << endl;
+  cout << "foo[0] = " << foo[0] << ", should be 3" << endl;
+  cout << "foo[1] = " << foo[1] << ", should be 2" << endl;
+  cout << "foo[2] = " << foo[2] << ", should be 1" << endl;
+
+  bar.normalize();
+  cout << "bar.normalize(): " << bar << ", should be (0,0.44,0.89)" << endl;
+  foo.normalize();
+  cout << "foo.normalize(): " << foo << ", should be (0.80,0.53,0.26)" << endl;
+  bar = foo;
+  cout << "bar := foo: bar = " << bar << ", should be (0.80,0.53,0.26)" << endl;
+
+  /* render three images with different camera settings */
+  /*
+   PerspectiveCamera c1(Vec3f(0, 0, 10), Vec3f(0, 0, -1), Vec3f(0, 1, 0), 60,
+   RESX, RESY);
+   RenderFrame(c1, "perspective1.ppm");
 
-  PerspectiveCamera c2(Vec3f(-8,3,8),Vec3f(1,-.1,-1),Vec3f(0,1,0),45,RESX,RESY);
-  RenderFrame(c2,"perspective2.ppm");
+   PerspectiveCamera c2(Vec3f(-8, 3, 8), Vec3f(1, -.1, -1), Vec3f(0, 1, 0), 45,
+   RESX, RESY);
+   RenderFrame(c2, "perspective2.ppm");
 
-  PerspectiveCamera c3(Vec3f(-8,3,8),Vec3f(1,-.1,-1),Vec3f(1,1,0),45,RESX,RESY);
-  RenderFrame(c3,"perspective3.ppm");
+   PerspectiveCamera c3(Vec3f(-8, 3, 8), Vec3f(1, -.1, -1), Vec3f(1, 1, 0), 45,
+   RESX, RESY);
+   RenderFrame(c3, "perspective3.ppm");*/
 }
index 8dfa3d7..b68f9e8 100644 (file)
--- a/Vec3f.cxx
+++ b/Vec3f.cxx
@@ -6,139 +6,139 @@ Vec3f::Vec3f() {
 }
 
 Vec3f::Vec3f(float x, float y, float z) {
-       m_values[0] = x;
-       m_values[0] = y;
-       m_values[0] = z;
+  m_values[0] = x;
+  m_values[1] = y;
+  m_values[2] = z;
 }
 
 Vec3f::~Vec3f() {
 }
 
 Vec3f::Vec3f(const Vec3f& o) {
-       for (size_t i = 0; i < DIM; i++) {
-               m_values[i] = o.m_values[i];
-       }
+  for(size_t i = 0; i < DIM; i++) {
+    m_values[i] = o.m_values[i];
+  }
 }
 
 Vec3f& Vec3f::operator=(const Vec3f& o) {
-       for (size_t i = 0; i < DIM; i++) {
-               m_values[i] = o.m_values[i];
-       }
-       return *this;
+  for(size_t i = 0; i < DIM; i++) {
+    m_values[i] = o.m_values[i];
+  }
+  return *this;
 }
 
 float Vec3f::operator|(const Vec3f& o) {
 
-       return dot(o);
+  return dot(o);
 }
 
 float Vec3f::dot(const Vec3f& o) {
-       float prod = 0;
-       for (size_t i = 0; i < DIM; i++) {
-               prod += m_values[i] * o.m_values[i];
-       }
-       return prod;
+  float prod = 0;
+  for(size_t i = 0; i < DIM; i++) {
+    prod += m_values[i] * o.m_values[i];
+  }
+  return prod;
 }
 
 Vec3f Vec3f::operator%(const Vec3f& o) {
-       return cross(o);
+  return cross(o);
 }
 
 Vec3f Vec3f::cross(const Vec3f& o) {
-       return Vec3f(m_values[1] * o.m_values[2] - m_values[2] * o.m_values[1],
-               m_values[2] * o.m_values[0] - m_values[0] * o.m_values[2], m_values[0]
-                       * o.m_values[1] - m_values[1] * o.m_values[0]);
+  return Vec3f(m_values[1] * o.m_values[2] - m_values[2] * o.m_values[1],
+    m_values[2] * o.m_values[0] - m_values[0] * o.m_values[2], m_values[0]
+      * o.m_values[1] - m_values[1] * o.m_values[0]);
 }
 
 void Vec3f::normalize() {
-
+  *this /= norm();
 }
 
 float Vec3f::norm() const {
-       return sqrt(m_values[0] * m_values[0] + m_values[1] * m_values[1]
-               + m_values[2] * m_values[2]);
+  return sqrt(m_values[0] * m_values[0] + m_values[1] * m_values[1]
+    + m_values[2] * m_values[2]);
 }
 
 Vec3f Vec3f::operator*(const float t) const {
-       Vec3f v(*this);
-       return v *= t;
+  Vec3f v(*this);
+  return v *= t;
 }
 
 Vec3f& Vec3f::operator*=(const float t) {
-    for (unsigned int i = 0; i < 3; ++i) {
-        m_values[i] *= f;
-    }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] *= t;
+  }
+  return *this;
 }
 
 Vec3f Vec3f::operator/(const float t) const {
-       Vec3f v(*this);
-    return v /= f;
+  Vec3f v(*this);
+  return v /= t;
 }
 
 Vec3f& Vec3f::operator/=(const float t) {
-    for (unsigned int i = 0; i < 3; ++i) {
-        m_values[i] /= f;
-    }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] /= t;
+  }
+  return *this;
 }
 
 // example implementation of a standard operator
 Vec3f Vec3f::operator+(const Vec3f& o) const {
-       Vec3f v(*this);
-       return v += o;
+  Vec3f v(*this);
+  return v += o;
 }
 
 // example implementation of an in-place operator
 Vec3f& Vec3f::operator+=(const Vec3f& o) {
-       for (unsigned int i = 0; i < 3; ++i) {
-               m_values[i] += o.m_values[i];
-       }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] += o.m_values[i];
+  }
+  return *this;
 }
 
 Vec3f Vec3f::operator-(const Vec3f& o) const {
-    Vec3f v(*this);
-    return v -= o;
+  Vec3f v(*this);
+  return v -= o;
 }
 
 Vec3f& Vec3f::operator-=(const Vec3f& o) {
-    for (unsigned int i = 0; i < 3; ++i) {
-        m_values[i] -= o.m_values[i];
-    }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] -= o.m_values[i];
+  }
+  return *this;
 }
 
 Vec3f Vec3f::operator*(const Vec3f& o) const {
-    Vec3f v(*this);
-    return v *= o;
+  Vec3f v(*this);
+  return v *= o;
 }
 
 Vec3f& Vec3f::operator*=(const Vec3f& o) {
-    for (unsigned int i = 0; i < 3; ++i) {
-        m_values[i] *= o.m_values[i];
-    }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] *= o.m_values[i];
+  }
+  return *this;
 }
 
 Vec3f Vec3f::operator/(const Vec3f& o) const {
-    Vec3f v(*this);
-    return v /= o;
+  Vec3f v(*this);
+  return v /= o;
 }
 
 Vec3f& Vec3f::operator/=(const Vec3f& o) {
-    for (unsigned int i = 0; i < 3; ++i) {
-        m_values[i] /= o.m_values[i];
-    }
-       return *this;
+  for(unsigned int i = 0; i < 3; ++i) {
+    m_values[i] /= o.m_values[i];
+  }
+  return *this;
 }
 
 float Vec3f::operator[](unsigned int i) const {
-       assert(i < DIM);
-       return m_values[i];
+  assert(i < DIM);
+  return m_values[i];
 }
 
 float& Vec3f::operator[](unsigned int i) {
-       assert(i < DIM);
-       return m_values[i];
+  assert(i < DIM);
+  return m_values[i];
 }
index 7f0c77d..908a8fc 100644 (file)
--- a/Vec3f.hxx
+++ b/Vec3f.hxx
@@ -70,14 +70,19 @@ public:
        //! element access functions ( read-only and read-write )
        float operator[](unsigned int i) const;
        float& operator[](unsigned int i);
-
+/*
+       inline std::string& operator std::string() {
+         std::string s = "(" + v[0] << "," << v[1] << "," << v[2] << ")");
+         return std::string(
+       }
+*/
 private:
        float m_values[3];
 };
 
 inline std::ostream& operator<<(std::ostream& o, const Vec3f& v) {
-       o << "(" << v[0] << "," << v[1] << "," << v[2] << ")";
-       return o;
+  o << "(" << v[0] << "," << v[1] << "," << v[2] << ")";
+  return o;
 }
 
 #endif
This page took 0.047269 seconds and 4 git commands to generate.