solution for 4.1b,c
[MicroTrace.git] / Vec3f.cxx
index b68f9e8..7c39435 100644 (file)
--- a/Vec3f.cxx
+++ b/Vec3f.cxx
 
 #include "Vec3f.hxx"
 
-Vec3f::Vec3f() {
+Vec3f::Vec3f()
+{
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] = 0.0f;
+    }
 }
 
-Vec3f::Vec3f(float x, float y, float z) {
+Vec3f::Vec3f(float x, float y, float z)
+{
   m_values[0] = x;
   m_values[1] = y;
   m_values[2] = z;
 }
 
-Vec3f::~Vec3f() {
+Vec3f::~Vec3f()
+{
 }
 
-Vec3f::Vec3f(const Vec3f& o) {
-  for(size_t i = 0; i < DIM; i++) {
-    m_values[i] = o.m_values[i];
-  }
+Vec3f::Vec3f(const Vec3f& o)
+{
+  for(int i = 0; i < 3; ++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];
-  }
+Vec3f& 
+Vec3f::operator=(const Vec3f& o)
+{
+  if(this != &o)
+    {
+      for(int i = 0; i < 3; ++i)
+       {
+         m_values[i] = o.m_values[i];
+       } 
+    }
   return *this;
 }
 
-float Vec3f::operator|(const Vec3f& 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;
-}
-
-Vec3f Vec3f::operator%(const Vec3f& 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]);
-}
-
-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]);
-}
-
-Vec3f Vec3f::operator*(const float t) const {
+float 
+Vec3f::operator|(const Vec3f& o) const
+{
+  return this->dot(o);
+}
+
+float 
+Vec3f::dot(const Vec3f& o) const
+{
+  float dot = 0.0f;
+  for(int i = 0; i < 3; ++i)
+    {
+      dot += m_values[i]*o.m_values[i];
+    }
+  return dot;
+}
+  
+Vec3f 
+Vec3f::operator%(const Vec3f& o) const
+{
+  return this->cross(o);
+}
+
+Vec3f 
+Vec3f::cross(const Vec3f& o) const
+{
+  float x = m_values[1]*o.m_values[2] - m_values[2]*o.m_values[1];
+  float y = m_values[2]*o.m_values[0] - m_values[0]*o.m_values[2];
+  float z = m_values[0]*o.m_values[1] - m_values[1]*o.m_values[0];
+  
+  return Vec3f(x,y,z);
+}
+  
+void 
+Vec3f::normalize()
+{
+  float norm = this->norm();
+  *this *= 1.0f/norm;
+}
+
+float 
+Vec3f::norm() const
+{
+  float n = this->dot(*this);
+  return sqrtf(n);
+}
+
+Vec3f 
+Vec3f::operator*(const float t) const
+{
   Vec3f v(*this);
   return v *= t;
 }
 
-Vec3f& Vec3f::operator*=(const float t) {
-  for(unsigned int i = 0; i < 3; ++i) {
-    m_values[i] *= t;
-  }
+Vec3f& 
+Vec3f::operator*=(const float t)
+{
+  for(int i = 0; i < 3; ++i)
+    {
+      m_values[i] *= t;
+    }
   return *this;
 }
 
-Vec3f Vec3f::operator/(const float t) const {
+Vec3f 
+Vec3f::operator/(const float t) const
+{
   Vec3f v(*this);
   return v /= t;
 }
 
-Vec3f& Vec3f::operator/=(const float t) {
-  for(unsigned int i = 0; i < 3; ++i) {
-    m_values[i] /= t;
-  }
+Vec3f& 
+Vec3f::operator/=(const float t)
+{
+  for(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 
+Vec3f::operator+(const Vec3f& o) const
+{
   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];
-  }
+Vec3f& 
+Vec3f::operator+=(const Vec3f& o)
+{
+  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 
+Vec3f::operator-(const Vec3f& o) const
+{
   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];
-  }
+Vec3f& 
+Vec3f::operator-=(const Vec3f& o)
+{
+  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 
+Vec3f::operator*(const Vec3f& o) const
+{
   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];
-  }
+Vec3f& 
+Vec3f::operator*=(const Vec3f& o)
+{
+  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 
+Vec3f::operator/(const Vec3f& o) const
+{
   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];
-  }
+Vec3f& 
+Vec3f::operator/=(const Vec3f& o)
+{
+  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);
+float 
+Vec3f::operator[](unsigned int i) const
+{
+  assert( i < 3 );
   return m_values[i];
 }
 
-float& Vec3f::operator[](unsigned int i) {
-  assert(i < DIM);
+float& 
+Vec3f::operator[](unsigned int i)
+{
+  assert(i < 3);
   return m_values[i];
 }
+
+void
+Vec3f::clamp()
+{
+  for(unsigned int i = 0; i < 3; ++i)
+    {
+      if(m_values[i] < 0.0)
+       m_values[i] = 0.0;
+      if(m_values[i] > 1.0)
+       m_values[i] = 1.0;
+    }
+}
This page took 0.032494 seconds and 4 git commands to generate.