Manta Interactive Ray Tracer Development Mailing List

Text archives Help


[MANTA] r285 - branches/newPointVector/Core/Geometry


Chronological Thread 
  • From: sparker@sci.utah.edu
  • To: rtrt@sci.utah.edu
  • Subject: [MANTA] r285 - branches/newPointVector/Core/Geometry
  • Date: Wed, 11 May 2005 00:42:19 -0600 (MDT)

Author: sparker
Date: Wed May 11 00:42:19 2005
New Revision: 285

Modified:
   branches/newPointVector/Core/Geometry/AffineTransform.cc
   branches/newPointVector/Core/Geometry/AffineTransform.h
   branches/newPointVector/Core/Geometry/PointVector.h
Log:
Completed/cleaned up Moller/Hughes rotate  code


Modified: branches/newPointVector/Core/Geometry/AffineTransform.cc
==============================================================================
--- branches/newPointVector/Core/Geometry/AffineTransform.cc    (original)
+++ branches/newPointVector/Core/Geometry/AffineTransform.cc    Wed May 11 
00:42:19 2005
@@ -82,67 +82,54 @@
   void AffineTransformT<T>::rotate(const VectorT<T, 3>& from_,
                                    const VectorT<T, 3>& to_)
   {
-
-#if 0
-        // Compute an axis ortho to the two vectors.
-        VectorT<T,3> axis = Cross( from_, to_ );
-        
-        // Don't assume that to and from are normalized.
-        VectorT<T,3> from = from.normal();
-        VectorT<T,3> to   = to.normal();
-
-        T sinth = axis.length();
-        T costh = Dot( from, to );
-
-        // Check to make sure the rotation isn't too small.
-        if ((SCIRun::Abs(sinth) < 1.0e-9) || (SCIRun::Abs(costh) < 1.0e-9))
-               return;
-
-        
-        T theta = atan2( sinth, costh );
-        
-        if (SCIRun::Abs(theta) > 1.0e-9)
-               rotate( axis, theta );
-
-        // Otherwise no rotation.
-#endif
-         VectorT<T, 3> v = Cross(from_, to_);
-         T e = Dot(from_, to_);
-         T f = SCIRun::Abs(e);
-         if(f > 1.0-1.e-9){
-           return;
-#if 0
-           Vector x(-SCIRun::Abs(from_.x()), -SCIRun::Abs(from_.y()), 
-SCIRun::Abs(from_.z()));
-           if(x.x() < x.y()){
-             if(x.x() < x.z()){
-               x[0] = 1; x[1] = x[2] = 0;
-             } else {
-               x[2] = 1; x[0] = x[1] = 0;
-             }
-           } else {
-             if(x.y() < x.z()){
-               x[1] = 1; x[0] = x[2] = 0;
-             } else {
-               x[2] = 1; x[0] = x[1] = 0;
-             }
-           }
-#endif
-         } else {
-           T mtx[3][3];
-           T h = 1.0/(1.0 + e);      /* optimization by Gottfried Chen */
-           mtx[0][0] = e + h * v[0] * v[0];
-           mtx[0][1] = h * v[0] * v[1] - v[2];
-           mtx[0][2] = h * v[0] * v[2] + v[1];
-           
-           mtx[1][0] = h * v[0] * v[1] + v[2];
-           mtx[1][1] = e + h * v[1] * v[1];
-           mtx[1][2] = h * v[1] * v[2] - v[0];
-           
-           mtx[2][0] = h * v[0] * v[2] - v[1];
-           mtx[2][1] = h * v[1] * v[2] + v[0];
-           mtx[2][2] = e + h * v[2] * v[2];
-           pre_multiply(mtx);
-         }
+    // Modified from Moller/Hughes, Journal of Graphics Tools Vol. 4, No. 4
+    // "Efficiently Building a Matrix to Rotate One Vector to Another"
+    
+    // Don't assume that to and from are normalized.
+    VectorT<T,3> from = from.normal();
+    VectorT<T,3> to   = to.normal();
+
+    VectorT<T, 3> v = Cross(from, to);
+    T e = Dot(from, to);
+    if(e > 1.0-1.e-9){
+      // No rotation
+      return;
+    } else if (e < -1.0+1.e-9){
+      // 180 degree rotation
+      VectorT<T, 3> x = v.absoluteValue();
+      int largest = x.indexOfMaxComponent();
+      x = VectorT<T, 3>(0, 0, 0);
+      x[largest] = 1;
+
+      VectorT<T, 3> u = x-from;
+      VectorT<T, 3> v = x-to;
+      T c1 = 2/Dot(u, u);
+      T c2 = 2/Dot(v, v);
+      T c3 = c1 * c2 * Dot(u, v);
+      T mtx[3][3];
+      for(int i = 0; i < 3; i++){
+        for(int j = 0; j < 3; j++){
+          mtx[i][j] = -c1 * u[i] * u[j] + c2 * v[i] * v[j] + c3 * v[i] * 
u[j];
+        }
+        mtx[i][i] += 1.0;
+      }
+      pre_multiply(mtx);
+    } else {
+      T h = 1.0/(1.0 + e);      /* optimization by Gottfried Chen */
+      T mtx[3][3];
+      mtx[0][0] = e + h * v[0] * v[0];
+      mtx[0][1] = h * v[0] * v[1] - v[2];
+      mtx[0][2] = h * v[0] * v[2] + v[1];
+      
+      mtx[1][0] = h * v[0] * v[1] + v[2];
+      mtx[1][1] = e + h * v[1] * v[1];
+      mtx[1][2] = h * v[1] * v[2] - v[0];
+      
+      mtx[2][0] = h * v[0] * v[2] - v[1];
+      mtx[2][1] = h * v[1] * v[2] + v[0];
+      mtx[2][2] = e + h * v[2] * v[2];
+      pre_multiply(mtx);
+    }
   }
 
   template<typename T>
@@ -350,40 +337,40 @@
   
   template<typename T>
   VectorT<T, 3> operator*(const AffineTransformT<T>& trans, const VectorT<T, 
3>& vec) {
-        VectorT<T,3> result;
-        for (int i=0;i<3;++i) {
-          result[i]  = trans(i,0) * vec[0];
-          result[i] += trans(i,1) * vec[1];
-               result[i] += trans(i,2) * vec[2];
-               // vec[3] == 0
-        }
-        return result;
+    VectorT<T,3> result;
+    for (int i=0;i<3;++i) {
+      result[i]  = trans(i,0) * vec[0];
+      result[i] += trans(i,1) * vec[1];
+      result[i] += trans(i,2) * vec[2];
+      // vec[3] == 0
+    }
+    return result;
   }
 
   // Multiply by the transpose of the AffineTransformation, useful for
   // computations involving an inverse transpose.
   template<typename T>
   PointT<T, 3> transpose_mult(const AffineTransformT<T>& trans, const 
PointT<T, 3>& point) {
-        PointT<T,3> result;
-        for (int i=0;i<3;++i) {
-          result[i]  = trans(0,i) * point[0];
-          result[i] += trans(1,i) * point[1];
-               result[i] += trans(2,i) * point[2];
-               result[i] += trans(3,i); // point[3] == 1
-        }
-        return result;
+    PointT<T,3> result;
+    for (int i=0;i<3;++i) {
+      result[i]  = trans(0,i) * point[0];
+      result[i] += trans(1,i) * point[1];
+      result[i] += trans(2,i) * point[2];
+      result[i] += trans(3,i); // point[3] == 1
+    }
+    return result;
   }
   
   template<typename T>
   VectorT<T, 3> transpose_mult(const AffineTransformT<T>& trans, const 
VectorT<T, 3>& vec) {
-        VectorT<T,3> result;
-        for (int i=0;i<3;++i) {
-          result[i]  = trans(0,i) * vec[0];
-          result[i] += trans(1,i) * vec[1];
-               result[i] += trans(2,i) * vec[2];
-               // vec[3] == 0
-        }
-        return result;  
+    VectorT<T,3> result;
+    for (int i=0;i<3;++i) {
+      result[i]  = trans(0,i) * vec[0];
+      result[i] += trans(1,i) * vec[1];
+      result[i] += trans(2,i) * vec[2];
+      // vec[3] == 0
+    }
+    return result;      
   }
 
   template<typename T>
@@ -399,11 +386,19 @@
 
 
   // Static Instances.
-  template PointT <Real,3> operator*(const AffineTransformT<Real> &trans, 
const PointT <Real,3> &point);
-  template VectorT<Real,3> operator*(const AffineTransformT<Real> &trans, 
const VectorT<Real,3> &vec);
-  template PointT <Real,3> transpose_mult(const AffineTransformT<Real> 
&trans, const PointT <Real,3> &point);
-  template VectorT<Real,3> transpose_mult(const AffineTransformT<Real> 
&trans, const VectorT<Real,3> &vec);
-  template class AffineTransformT<Real>;
-  template std::ostream& operator<<(std::ostream& os, const 
AffineTransformT<Real>& trans);
+  template PointT <double,3> operator*(const AffineTransformT<double> 
&trans, const PointT <double,3> &point);
+  template VectorT<double,3> operator*(const AffineTransformT<double> 
&trans, const VectorT<double,3> &vec);
+  template PointT <double,3> transpose_mult(const AffineTransformT<double> 
&trans, const PointT <double,3> &point);
+  template VectorT<double,3> transpose_mult(const AffineTransformT<double> 
&trans, const VectorT<double,3> &vec);
+  template class AffineTransformT<double>;
+  template std::ostream& operator<<(std::ostream& os, const 
AffineTransformT<double>& trans);
+
+
+  template PointT <float,3> operator*(const AffineTransformT<float> &trans, 
const PointT <float,3> &point);
+  template VectorT<float,3> operator*(const AffineTransformT<float> &trans, 
const VectorT<float,3> &vec);
+  template PointT <float,3> transpose_mult(const AffineTransformT<float> 
&trans, const PointT <float,3> &point);
+  template VectorT<float,3> transpose_mult(const AffineTransformT<float> 
&trans, const VectorT<float,3> &vec);
+  template class AffineTransformT<float>;
+  template std::ostream& operator<<(std::ostream& os, const 
AffineTransformT<float>& trans);
 
 }

Modified: branches/newPointVector/Core/Geometry/AffineTransform.h
==============================================================================
--- branches/newPointVector/Core/Geometry/AffineTransform.h     (original)
+++ branches/newPointVector/Core/Geometry/AffineTransform.h     Wed May 11 
00:42:19 2005
@@ -4,6 +4,7 @@
 
 #include <MantaTypes.h>
 #include <Core/Geometry/PointVector.h>
+#include <iosfwd>
 
 namespace Manta {
   /**
@@ -64,7 +65,9 @@
     void invert();
 
     // Const Accessors used by global multiplication operators.
-    const T &operator() (unsigned int r, unsigned int c) const { return 
mat[r][c]; }
+    const T &operator() (unsigned int r, unsigned int c) const { 
+      return mat[r][c];
+    }
 
     // Post-multiplication
     AffineTransformT<T> operator*(const AffineTransformT<T>& m) const;

Modified: branches/newPointVector/Core/Geometry/PointVector.h
==============================================================================
--- branches/newPointVector/Core/Geometry/PointVector.h (original)
+++ branches/newPointVector/Core/Geometry/PointVector.h Wed May 11 00:42:19 
2005
@@ -51,9 +51,9 @@
     const T &operator[](int i) const {
       return data[i];
     }
-        T &operator[] ( int i ) {
-               return data[i];
-        }
+    T &operator[] ( int i ) {
+      return data[i];
+    }
     VectorT<T, Dim> operator+(const VectorT<T, Dim>& v) const {
       return VectorT<T, Dim>(data[0]+v.data[0], data[1]+v.data[1], 
data[2]+v.data[2]);
     }
@@ -125,6 +125,20 @@
       using SCIRun::Max;
       return Max(data[0], data[1], data[2]);
     }
+    int indexOfMinComponent() const {
+      int idx = 0;
+      for(int i=1;i<Dim;i++)
+        if(data[i] < data[idx])
+          idx = i;
+      return idx;
+    }
+    int indexOfMaxComponent() const {
+      int idx = 0;
+      for(int i=1;i<Dim;i++)
+        if(data[i] > data[idx])
+          idx = i;
+      return idx;
+    }
 
     VectorT<T, Dim> inverse() const {
       return VectorT<T, Dim>(1./data[0], 1./data[1], 1./data[2]);
@@ -142,7 +156,7 @@
     }
 
   private:
-    double data[Dim];
+    T data[Dim];
   };
 
 
@@ -187,9 +201,9 @@
     T operator[](int i) const {
       return data[i];
     }
-        T &operator[](int i) {
-               return data[i];
-        }
+    T &operator[](int i) {
+      return data[i];
+    }
 
     VectorT<T, Dim> operator-(const PointT<T, Dim>& p) const {
       return VectorT<T, Dim>(data[0]-p.data[0], data[1]-p.data[1], 
data[2]-p.data[2]);




  • [MANTA] r285 - branches/newPointVector/Core/Geometry, sparker, 05/11/2005

Archive powered by MHonArc 2.6.16.

Top of page