summaryrefslogtreecommitdiff
path: root/graphics/asymptote/quaternion.cc
blob: 7f3eaf5f65b3d0eeae3e36df4ccd6618ac9b9441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/***********************************************************************

  quaternion.cc - A quaternion class

  -------------------------------------------------------------------

  GLUI User Interface Toolkit (LGPL)
  Copyright (c) 1998 Paul Rademacher

  WWW:    http://sourceforge.net/projects/glui/
  Forums: http://sourceforge.net/forum/?group_id=92496

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

************************************************************************

  Feb 1998, Paul Rademacher (rademach@cs.unc.edu)
  Oct 2003, Nigel Stewart - GLUI Code Cleaning
  
************************************************************************/

#include "quaternion.h"
#include <cmath>

/******************************************* constructors **************/

quat::quat()
{
    *this = quat_identity();
}

quat::quat(const float x, const float y, const float z, const float w)
{
    v.set( x, y, z );
    s = w;
}

quat::quat(const vec3 &_v, const float _s)
{
    set( _v, _s );
}

quat::quat(const float _s, const vec3 &_v)
{
    set( _v, _s );
}

quat::quat(const float *d)
{
    v[0] = d[0];
    v[1] = d[1];
    v[2] = d[2];
    s    = d[3];
}

quat::quat(const double *d)
{
    v[0] = (float) d[0];
    v[1] = (float) d[1];
    v[2] = (float) d[2];
    s    = (float) d[3];
}

quat::quat(const quat &q)
{
    v = q.v;
    s = q.s;
}

void quat::set(const vec3 &_v, const float _s)
{
    v = _v;
    s = _s;
}

quat &quat::operator=(const quat &q)
{ 
    v = q.v;  
    s = q.s; 
    return *this; 
}

/******** quat friends ************/

quat operator + (const quat &a, const quat &b)
{
    return quat( a.s+b.s, a.v+b.v );
}

quat operator - (const quat &a, const quat &b)
{
    return quat( a.s-b.s, a.v-b.v );
}

quat operator - (const quat &a )
{
    return quat( -a.s, -a.v );
}

quat operator * ( const quat &a, const quat &b)
{
    return quat( a.s*b.s - a.v*b.v, a.s*b.v + b.s*a.v + (a.v^b.v) );
}

quat operator * ( const quat &a, const float t)
{
    return quat( a.v * t, a.s * t );
}

quat operator * ( const float t, const quat &a )
{
    return quat( a.v * t, a.s * t );
}

mat4 quat::to_mat4() const
{
    float xs, ys, zs, wx, wy, wz, xx, xy, xz, yy, yz, zz;

    float t  = 2.0f / (v*v + s*s);

    xs = v[VX]*t;   ys = v[VY]*t;   zs = v[VZ]*t;
    wx = s*xs;      wy = s*ys;      wz = s*zs;
    xx = v[VX]*xs;  xy = v[VX]*ys;  xz = v[VX]*zs;
    yy = v[VY]*ys;  yz = v[VY]*zs;  zz = v[VZ]*zs;

    mat4 matrix( 
           1.0f-(yy+zz), xy+wz,        xz-wy,        0.0f,
           xy-wz,        1.0f-(xx+zz), yz+wx,        0.0f,
           xz+wy,        yz-wx,        1.0f-(xx+yy), 0.0f,
           0.0f,         0.0f,         0.0f,         1.0f );

    return matrix;
}

/************************************************* quat_identity() *****/
/* Returns quaternion identity element                                 */

quat quat_identity() 
{
    return quat( vec3( 0.0, 0.0, 0.0 ), 1.0 );
}

/************************************************ quat_slerp() ********/
/* Quaternion spherical interpolation                                 */

quat quat_slerp(const quat &from, const quat &to, float t)
{
    quat to1;
    float omega, cosom, sinom, scale0, scale1;

    /* calculate cosine */
    cosom = from.v * to.v + from.s + to.s;

    /* Adjust signs (if necessary) */
    if ( cosom < 0.0 ) 
    {
        cosom = -cosom;
        to1 = -to;
    }
    else
    {
        to1 = to;
    }

    /* Calculate coefficients */
    if ((1.0 - cosom) > FUDGE ) 
    {
        /* standard case (slerp) */
        omega =  (float) acos( cosom );
        sinom =  (float) sin( omega );
        scale0 = (float) sin((1.0 - t) * omega) / sinom;
        scale1 = (float) sin(t * omega) / sinom;
    }
    else 
    {
        /* 'from' and 'to' are very close - just do linear interpolation */
        scale0 = 1.0f - t;
        scale1 = t;      
    }

    return scale0 * from + scale1 * to1;
}

/********************************************** set_angle() ************/
/* set rot angle (degrees)                                             */

void quat::set_angle(float f)
{
    vec3 axis = get_axis();

    static const double halfradians=acos(-1.0)/360.0;
    f *= halfradians;
    
    s = (float) cos( f );

    v = axis * (float) sin( f );
}

/********************************************** scale_angle() ************/
/* scale rot angle (degrees)                                             */

void quat::scale_angle(float f)
{
    set_angle( f * get_angle() );
}

/********************************************** get_angle() ************/
/* get rot angle (degrees).  Assumes s is between -1 and 1             */

float quat::get_angle() const
{
    static const double degrees2=360.0/acos(-1.0);
    return (float) (acos( s ) * degrees2);
}

/********************************************* get_axis() **************/

vec3 quat::get_axis() const
{
    float scale = (float) sin( acos( s ) );

    if ( scale < FUDGE && scale > -FUDGE )
        return vec3( 0.0, 0.0, 0.0 );
    else
        return  v / scale;
}

/******************************************* quat::print() ************/

void quat::print(FILE *dest, const char *name) const
{
    fprintf( dest, "%s:   v:<%3.2f %3.2f %3.2f>  s:%3.2f\n", 
        name, v[0], v[1], v[2], s );
}