source: trunk/packages/vizservers/nanovis/vrmath/vrRotation.cpp @ 2841

Last change on this file since 2841 was 2840, checked in by ldelgass, 13 years ago

Use trace macros in image loader. Formatting fixes in libs

File size: 1.2 KB
RevLine 
[2798]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
[2096]2#include <vrmath/vrRotation.h>
3#include <vrmath/vrVector3f.h>
4#include <vrmath/vrQuaternion.h>
5
6void vrRotation::set(const vrVector3f &vec1, const vrVector3f &vec2)
7{
[2840]8    if (vec1 == vec2) {
9        x = 0.0f;
10        y = 0.0f;
11        z = 1.0f;
12        angle = 0.0f;
13        return;
14    }
[2096]15
[2840]16    vrVector3f cross;
17    cross.cross(vec1, vec2);
[2096]18
[2840]19    float ang = atan2( cross.length(), vec1.dot(vec2) );
[2096]20
[2840]21    cross.normalize();
[2096]22
[2840]23    x = cross.x;
24    y = cross.y;
25    z = cross.z;
26    angle = ang;
[1699]27}
28
29void vrRotation::set(const vrQuaternion& quat)
30{
[2840]31    //if (quat.w > 1) quat.normalize();
[1699]32
[2840]33    angle = acos(quat.w) * 2.0f;
34    float scale = sqrt(1 - quat.w * quat.w);
35    if (scale < 0.001) {
36        x = quat.x;
37        y = quat.y;
38        z = quat.z;
39    } else {
40        x = quat.x / scale;
41        y = quat.y / scale;
42        z = quat.z / scale;
43    }
44    /*
45    // http://gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation#Quaternion_to_axis-angle
46    float scale = sqrt(quat.x * quat.x + quat.y * quat.y + quat.z * quat.z);
47    x = quat.x / scale;
48    y = quat.y / scale;
49    z = quat.z / scale;
50    */
[1699]51}
Note: See TracBrowser for help on using the repository browser.