2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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 General Public License for more details.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include "math_vector.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#define LERP_DELTA 1e-6
|
|
|
|
|
|
|
|
idVec3 vec_zero( 0.0f, 0.0f, 0.0f );
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Bounds boundsZero;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
float idVec3::toYaw( void ) {
|
|
|
|
float yaw;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
if ( ( y == 0 ) && ( x == 0 ) ) {
|
|
|
|
yaw = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-11-04 03:51:54 +00:00
|
|
|
yaw = atan2( y, x ) * 180 / M_PI;
|
|
|
|
if ( yaw < 0 ) {
|
|
|
|
yaw += 360;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return yaw;
|
|
|
|
}
|
|
|
|
|
|
|
|
float idVec3::toPitch( void ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
float forward;
|
|
|
|
float pitch;
|
|
|
|
|
2007-11-04 03:51:54 +00:00
|
|
|
if ( ( x == 0 ) && ( y == 0 ) ) {
|
|
|
|
if ( z > 0 ) {
|
|
|
|
pitch = 90;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-11-04 03:51:54 +00:00
|
|
|
pitch = 270;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-11-04 03:51:54 +00:00
|
|
|
forward = ( float )idSqrt( x * x + y * y );
|
|
|
|
pitch = atan2( z, forward ) * 180 / M_PI;
|
|
|
|
if ( pitch < 0 ) {
|
|
|
|
pitch += 360;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
angles_t idVec3::toAngles( void ) {
|
|
|
|
float forward;
|
|
|
|
float yaw;
|
|
|
|
float pitch;
|
|
|
|
|
|
|
|
if ( ( x == 0 ) && ( y == 0 ) ) {
|
|
|
|
yaw = 0;
|
|
|
|
if ( z > 0 ) {
|
|
|
|
pitch = 90;
|
|
|
|
} else {
|
|
|
|
pitch = 270;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
yaw = atan2( y, x ) * 180 / M_PI;
|
|
|
|
if ( yaw < 0 ) {
|
|
|
|
yaw += 360;
|
|
|
|
}
|
|
|
|
|
|
|
|
forward = ( float )idSqrt( x * x + y * y );
|
|
|
|
pitch = atan2( z, forward ) * 180 / M_PI;
|
|
|
|
if ( pitch < 0 ) {
|
|
|
|
pitch += 360;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return angles_t( -pitch, yaw, 0 );
|
|
|
|
}
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
idVec3 LerpVector( idVec3 &w1, idVec3 &w2, const float t ) {
|
|
|
|
float omega, cosom, sinom, scale0, scale1;
|
|
|
|
|
|
|
|
cosom = w1 * w2;
|
|
|
|
if ( ( 1.0 - cosom ) > LERP_DELTA ) {
|
|
|
|
omega = acos( cosom );
|
|
|
|
sinom = sin( omega );
|
|
|
|
scale0 = sin( ( 1.0 - t ) * omega ) / sinom;
|
|
|
|
scale1 = sin( t * omega ) / sinom;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-11-04 03:51:54 +00:00
|
|
|
scale0 = 1.0 - t;
|
|
|
|
scale1 = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( w1 * scale0 + w2 * scale1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
idVec3::string
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This is just a convenience function
|
|
|
|
for printing vectors
|
|
|
|
=============
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
char *idVec3::string( void ) {
|
2012-03-17 20:01:54 +00:00
|
|
|
static int index = 0;
|
|
|
|
static char str[ 8 ][ 36 ];
|
|
|
|
char *s;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
// use an array so that multiple toString's won't collide
|
|
|
|
s = str[ index ];
|
2012-03-17 20:01:54 +00:00
|
|
|
index = ( index + 1 ) & 7;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
sprintf( s, "%.2f %.2f %.2f", x, y, z );
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|