source: nanovis/trunk/vrmath/Rotation.cpp @ 5722

Last change on this file since 5722 was 3492, checked in by ldelgass, 11 years ago

Fix camera reset for nanovis. Includes refactoring of vector/matrix classes
in nanovis to consolidate into vrmath library. Also add preliminary canonical
view control to clients for testing.

File size: 1.4 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 *
5 * Author: Insoo Woo <iwoo@purdue.edu>
6 */
7
8#include <math.h>
9
10#include <vrmath/Rotation.h>
11#include <vrmath/Vector3f.h>
12#include <vrmath/Quaternion.h>
13
14using namespace vrmath;
15
16void Rotation::set(const Vector3f &vec1, const Vector3f &vec2)
17{
18    if (vec1 == vec2) {
19        x = 0.0f;
20        y = 0.0f;
21        z = 1.0f;
22        angle = 0.0f;
23        return;
24    }
25
26    Vector3f perp = cross(vec1, vec2);
27
28    float ang = atan2(perp.length(), vec1.dot(vec2));
29
30    perp = perp.normalize();
31
32    x = perp.x;
33    y = perp.y;
34    z = perp.z;
35    angle = ang;
36}
37
38void Rotation::set(const Quaternion& quat)
39{
40    //if (quat.w > 1) quat.normalize();
41
42    angle = acos(quat.w) * 2.0f;
43    float scale = sqrt(1 - quat.w * quat.w);
44    if (angle < 1.0e-6f) {
45        x = 1;
46        y = 0;
47        z = 0;
48    } else if (scale < 1.0e-6f) {
49        x = quat.x;
50        y = quat.y;
51        z = quat.z;
52    } else {
53        x = quat.x / scale;
54        y = quat.y / scale;
55        z = quat.z / scale;
56    }
57}
58
59Quaternion Rotation::getQuaternion() const
60{
61    Quaternion result;
62
63    result.w = cos(angle / 2.0);
64    double sinHalfAngle = sin(angle / 2.0);
65    result.x = x * sinHalfAngle;
66    result.y = y * sinHalfAngle;
67    result.z = z * sinHalfAngle;
68
69    return result;
70}
Note: See TracBrowser for help on using the repository browser.