[Insight-users] QuaternionTransform: Direction of Rotation

Michael Kuhn michakuhn at gmx . ch
Mon, 11 Aug 2003 09:18:35 -0600


Hi,

I'm still having problems to understand the correct setup of the 
quaternion transform parameters. I quickly set up a program to 
demonstrate my problem:

I'm trying to rotate the point (1, 0, 0) around the z-Axis by angle +phi 
using an affine and a quaternion transform.

The results of these two transforms turn out to be different with 
respect to the direction of rotation. Is there something wrong with the 
way I set up the parameters?

Thanks,

Michael


Below the output of my program and the program code:

Output:
--------

result for affine transform:
0.980067
0.198669
0
result for quaternion transform:
0.980067
-0.198669
0




Program Code
---------------

#include "itkQuaternionRigidTransform.h"
#include "itkAffineTransform.h"


#include <iostream>

int main(int argc, char** argv) {

    typedef itk::QuaternionRigidTransform<double> QuaternionTransformType;
    typedef itk::AffineTransform<double, 3> AffineTransformType;

    QuaternionTransformType::Pointer quaternionTransform =
        QuaternionTransformType::New();

    AffineTransformType::Pointer affineTransform = 
AffineTransformType::New();

    double phi = 0.2;
    
    // rotation around the z-Axis for Affine Transform:

    AffineTransformType::ParametersType
        affineParameters(affineTransform->GetNumberOfParameters());

    affineParameters[0] = cos(phi);
    affineParameters[1] = -sin(phi);
    affineParameters[2] = 0;
    affineParameters[3] = sin(phi);
    affineParameters[4] = cos(phi);
    affineParameters[5] = 0;
    affineParameters[6] = 0;
    affineParameters[7] = 0;
    affineParameters[8] = 1;
    affineParameters[9] = 0;
    affineParameters[10] = 0;
    affineParameters[11] = 0;

    affineTransform->SetParameters(affineParameters);



    // rotation around the z-Axis for Quaternion Transform:

    QuaternionTransformType::ParametersType
        quaternionParameters(quaternionTransform->GetNumberOfParameters());

    quaternionParameters[0] = 0;
    quaternionParameters[1] = 0;
    quaternionParameters[2] = 1 * sin(phi / 2);
    quaternionParameters[3] = cos(phi / 2);
    quaternionParameters[4] = 0;
    quaternionParameters[5] = 0;
    quaternionParameters[6] = 0;

    quaternionTransform->SetParameters(quaternionParameters);


    // Define point (1, 0, 0) and rotate with Affine Transform:
    AffineTransformType::InputPointType inPointAffine;

    inPointAffine[0] = 1;
    inPointAffine[1] = 0;
    inPointAffine[2] = 0;

    AffineTransformType::OutputPointType outPointAffine =
        affineTransform->TransformPoint(inPointAffine);

    // Define point (1, 0, 0) and rotate with Quaternion Transform:
    QuaternionTransformType::InputPointType inPointQuaternion;

    inPointQuaternion[0] = 1;
    inPointQuaternion[1] = 0;
    inPointQuaternion[2] = 0;

    QuaternionTransformType::OutputPointType outPointQuaternion;
    outPointQuaternion =
        quaternionTransform->TransformPoint(inPointQuaternion);

    // compare the two results
    std::cout << "result for affine transform: " << std::endl;
    for (int i = 0; i < 3; i++) {
        std::cout << outPointAffine[i] << std::endl;
    }

    std::cout << "result for quaternion transform: " << std::endl;
    for (int i = 0; i < 3; i++) {
        std::cout << outPointQuaternion[i] << std::endl;
    }

    return 0;
}