13 #ifndef __utlRotationMatrixFromVectors_h 14 #define __utlRotationMatrixFromVectors_h 35 template <
class VectorType,
class MatrixType>
39 const double epsilon = 0.000001;
45 v[0] = from[1] * to[2] - from[2] * to[1];
46 v[1] = from[2] * to[0] - from[0] * to[2];
47 v[2] = from[0] * to[1] - from[1] * to[0];
50 e = from[0]*to[0]+from[1]*to[1]+from[2]*to[2];
52 if( f > 1.0 - epsilon )
54 VectorType ut(from), vt(from);
59 x[0] = (from[0] > 0.0) ? from[0] : -from[0];
60 x[1] = (from[1] > 0.0) ? from[1] : -from[1];
61 x[2] = (from[2] > 0.0) ? from[2] : -from[2];
67 x[0] = 1.0; x[1] = x[2] = 0.0;
71 x[2] = 1.0; x[0] = x[1] = 0.0;
78 x[1] = 1.0; x[0] = x[2] = 0.0;
82 x[2] = 1.0; x[0] = x[1] = 0.0;
86 ut[0] = x[0] - from[0]; ut[1] = x[1] - from[1]; ut[2] = x[2] - from[2];
87 vt[0] = x[0] - to[0]; vt[1] = x[1] - to[1]; vt[2] = x[2] - to[2];
89 c1 = 2.0 / (ut[0]*ut[0]+ut[1]*ut[1]+ut[2]*ut[2]);
90 c2 = 2.0 / (vt[0]*vt[0]+vt[1]*vt[1]+vt[2]*vt[2]);
91 c3 = c1 * c2 * (ut[0]*vt[0]+ut[1]*vt[1]+ut[2]*vt[2]);
92 for( i = 0; i < 3; i++ )
94 for( j = 0; j < 3; j++ )
96 mtx(i,j) = -c1 * ut[i] * ut[j] - c2 * vt[i] * vt[j] + c3 * vt[i] * ut[j];
105 double hvx, hvz, hvxy, hvxz, hvyz;
113 mtx(0,0) = e + hvx * v[0];
114 mtx(0,1) = hvxy - v[2];
115 mtx(0,2) = hvxz + v[1];
117 mtx(1,0) = hvxy + v[2];
118 mtx(1,1) = e + h * v[1] * v[1];
119 mtx(1,2) = hvyz - v[0];
121 mtx(2,0) = hvxz - v[1];
122 mtx(2,1) = hvyz + v[0];
123 mtx(2,2) = e + hvz * v[2];
128 template <
class VectorType,
class MatrixType>
132 VectorType from_unit(from);
133 VectorType to_unit(to);
134 double sum_from=0.0, sum_to=0.0;
135 for (
int i = 0; i < 3; i += 1 )
137 sum_from += from_unit[i]*from_unit[i];
138 sum_to += to_unit[i]*to_unit[i];
141 double norm_from = std::sqrt(sum_from);
142 double norm_to = std::sqrt(sum_to);
144 for (
int i = 0; i < 3; i += 1 )
146 from_unit[i] /= norm_from;
147 to_unit[i] /= norm_to;
149 RotationMatrixFromUnitNormVectors<VectorType, MatrixType>(from_unit,to_unit, mat);
150 double factor = norm_to/norm_from;
151 for (
int i = 0; i < 3; ++i )
152 for (
int j = 0; j < 3; ++j )
void RotationMatrixFromVectors(const VectorType &from, const VectorType &to, MatrixType &mat)
#define utlException(cond, expout)
T abs(const T x)
template version of the fabs function
void RotationMatrixFromUnitNormVectors(const VectorType &from, const VectorType &to, MatrixType &mtx)