| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 20 | */ |
| 21 | |
| 22 | #include "math_vector.h" |
| 23 | #include <assert.h> |
| 24 | #include <math.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <time.h> |
| 30 | #include <ctype.h> |
| 31 | |
| 32 | #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h |
| 33 | |
| 34 | #define LERP_DELTA 1e-6 |
| 35 | |
| 36 | idVec3 vec_zero( 0.0f, 0.0f, 0.0f ); |
| 37 | |
| 38 | Bounds boundsZero; |
| 39 | |
| 40 | float idVec3::toYaw( void ) { |
| 41 | float yaw; |
| 42 | |
| 43 | if ( ( y == 0 ) && ( x == 0 ) ) { |
| 44 | yaw = 0; |
| 45 | } |
| 46 | else { |
| 47 | yaw = atan2( y, x ) * 180 / M_PI; |
| 48 | if ( yaw < 0 ) { |
| 49 | yaw += 360; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return yaw; |
| 54 | } |
| 55 | |
| 56 | float idVec3::toPitch( void ) { |
| 57 | float forward; |
| 58 | float pitch; |
| 59 | |
| 60 | if ( ( x == 0 ) && ( y == 0 ) ) { |
| 61 | if ( z > 0 ) { |
| 62 | pitch = 90; |
| 63 | } |
| 64 | else { |
| 65 | pitch = 270; |
| 66 | } |
| 67 | } |
| 68 | else { |
| 69 | forward = ( float )idSqrt( x * x + y * y ); |
| 70 | pitch = atan2( z, forward ) * 180 / M_PI; |
| 71 | if ( pitch < 0 ) { |
| 72 | pitch += 360; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return pitch; |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | angles_t idVec3::toAngles( void ) { |
| 81 | float forward; |
| 82 | float yaw; |
| 83 | float pitch; |
| 84 | |
| 85 | if ( ( x == 0 ) && ( y == 0 ) ) { |
| 86 | yaw = 0; |
| 87 | if ( z > 0 ) { |
| 88 | pitch = 90; |
| 89 | } else { |
| 90 | pitch = 270; |
| 91 | } |
| 92 | } else { |
| 93 | yaw = atan2( y, x ) * 180 / M_PI; |
| 94 | if ( yaw < 0 ) { |
| 95 | yaw += 360; |
| 96 | } |
| 97 | |
| 98 | forward = ( float )idSqrt( x * x + y * y ); |
| 99 | pitch = atan2( z, forward ) * 180 / M_PI; |
| 100 | if ( pitch < 0 ) { |
| 101 | pitch += 360; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return angles_t( -pitch, yaw, 0 ); |
| 106 | } |
| 107 | */ |
| 108 | |
| 109 | idVec3 LerpVector( idVec3 &w1, idVec3 &w2, const float t ) { |
| 110 | float omega, cosom, sinom, scale0, scale1; |
| 111 | |
| 112 | cosom = w1 * w2; |
| 113 | if ( ( 1.0 - cosom ) > LERP_DELTA ) { |
| 114 | omega = acos( cosom ); |
| 115 | sinom = sin( omega ); |
| 116 | scale0 = sin( ( 1.0 - t ) * omega ) / sinom; |
| 117 | scale1 = sin( t * omega ) / sinom; |
| 118 | } |
| 119 | else { |
| 120 | scale0 = 1.0 - t; |
| 121 | scale1 = t; |
| 122 | } |
| 123 | |
| 124 | return ( w1 * scale0 + w2 * scale1 ); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | ============= |
| 129 | idVec3::string |
| 130 | |
| 131 | This is just a convenience function |
| 132 | for printing vectors |
| 133 | ============= |
| 134 | */ |
| 135 | char *idVec3::string( void ) { |
| 136 | static int index = 0; |
| 137 | static char str[ 8 ][ 36 ]; |
| 138 | char *s; |
| 139 | |
| 140 | // use an array so that multiple toString's won't collide |
| 141 | s = str[ index ]; |
| 142 | index = ( index + 1 ) & 7; |
| 143 | |
| 144 | sprintf( s, "%.2f %.2f %.2f", x, y, z ); |
| 145 | |
| 146 | return s; |
| 147 | } |