2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "math_quaternion.h"
23 #include "math_matrix.h"
25 void toQuat( idVec3 &src, quat_t &dst ) {
32 void toQuat( angles_t &src, quat_t &dst ) {
35 toMatrix( src, temp );
39 void toQuat( mat3_t &src, quat_t &dst ) {
46 static int next[ 3 ] = { 1, 2, 0 };
48 trace = src[ 0 ][ 0 ] + src[ 1 ][ 1 ] + src[ 2 ][ 2 ];
50 s = ( float )sqrt( trace + 1.0f );
54 dst.x = ( src[ 2 ][ 1 ] - src[ 1 ][ 2 ] ) * s;
55 dst.y = ( src[ 0 ][ 2 ] - src[ 2 ][ 0 ] ) * s;
56 dst.z = ( src[ 1 ][ 0 ] - src[ 0 ][ 1 ] ) * s;
59 if ( src[ 1 ][ 1 ] > src[ 0 ][ 0 ] ) {
62 if ( src[ 2 ][ 2 ] > src[ i ][ i ] ) {
69 s = ( float )sqrt( ( src[ i ][ i ] - ( src[ j ][ j ] + src[ k ][ k ] ) ) + 1.0f );
74 dst.w = ( src[ k ][ j ] - src[ j ][ k ] ) * s;
75 dst[ j ] = ( src[ j ][ i ] + src[ i ][ j ] ) * s;
76 dst[ k ] = ( src[ k ][ i ] + src[ i ][ k ] ) * s;