[vtkusers] Transformation matrix for transforming one vector to another

Donny Zimmerman donny.zimmerman at willowpointsoftware.com
Sat Dec 31 14:35:01 EST 2011


Thanks David. That works beautifully!!!

-----Original Message-----
From: David Gobbi [mailto:david.gobbi at gmail.com] 
Sent: Saturday, December 31, 2011 10:01 AM
To: Donny Zimmerman
Cc: vtkusers at vtk.org
Subject: Re: [vtkusers] Transformation matrix for transforming one vector to
another

On Sat, Dec 31, 2011 at 8:24 AM, Donny Zimmerman
<donny.zimmerman at willowpointsoftware.com> wrote:
> If I have a plane perpendicular to one vector say {0, 1000, 0} how would I
> get the transformation matrix to transform it to be perpendicular to
vector
> {975, -250, 600}. I would like to use the RotateX, RotateY, RotateZ and
> translate functions of vtkTransform to accomplish this. Any help?

This fits into the "advanced math" category, you can't do it with
any of the basic transform operations.  The solution relies on
quaternions.  Below is some code that generates the 3x3 rotation
matrix.  Finding the translation and building a 4x4 matrix is left as
an exercise for the reader.

  // Compute rotation matrix between two vectors
  double vec[3];
  vtkMath::Cross(normal1, normal2, vec);
  double costheta = vtkMath::Dot(normal1, normal2);
  double sintheta = vtkMath::Norm(vec);
  double theta = atan2(sintheta, costheta);
  if (sintheta != 0)
    {
    vec[0] /= sintheta;
    vec[1] /= sintheta;
    vec[2] /= sintheta;
    }
  // convert to quaternion
  costheta = cos(0.5*theta);
  sintheta = sin(0.5*theta);
  double quat[4];
  quat[0] = costheta;
  quat[1] = vec[0]*sintheta;
  quat[2] = vec[1]*sintheta;
  quat[3] = vec[2]*sintheta;
  // convert to matrix
  double mat[3][3];
  vtkMath::QuaternionToMatrix3x3(quat, mat);

-- David




More information about the vtkusers mailing list