]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/splines/math_vector.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / splines / math_vector.cpp
diff --git a/libs/splines/math_vector.cpp b/libs/splines/math_vector.cpp
new file mode 100644 (file)
index 0000000..7237de5
--- /dev/null
@@ -0,0 +1,143 @@
+/*\r
+Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
+For a list of contributors, see the accompanying CONTRIBUTORS file.\r
+\r
+This file is part of GtkRadiant.\r
+\r
+GtkRadiant is free software; you can redistribute it and/or modify\r
+it under the terms of the GNU General Public License as published by\r
+the Free Software Foundation; either version 2 of the License, or\r
+(at your option) any later version.\r
+\r
+GtkRadiant is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+GNU General Public License for more details.\r
+\r
+You should have received a copy of the GNU General Public License\r
+along with GtkRadiant; if not, write to the Free Software\r
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
+*/\r
+\r
+#include "math_vector.h"\r
+#include <assert.h>\r
+#include <math.h>\r
+#include <stdio.h>\r
+#include <stdarg.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include <time.h>\r
+#include <ctype.h>\r
+\r
+#define M_PI           3.14159265358979323846  // matches value in gcc v2 math.h\r
+\r
+#define LERP_DELTA 1e-6\r
+\r
+idVec3 vec_zero( 0.0f, 0.0f, 0.0f );\r
+\r
+Bounds boundsZero;\r
+\r
+float idVec3::toYaw( void ) {\r
+       float yaw;\r
+       \r
+       if ( ( y == 0 ) && ( x == 0 ) ) {\r
+               yaw = 0;\r
+       } else {\r
+               yaw = atan2( y, x ) * 180 / M_PI;\r
+               if ( yaw < 0 ) {\r
+                       yaw += 360;\r
+               }\r
+       }\r
+\r
+       return yaw;\r
+}\r
+\r
+float idVec3::toPitch( void ) {\r
+       float   forward;\r
+       float   pitch;\r
+       \r
+       if ( ( x == 0 ) && ( y == 0 ) ) {\r
+               if ( z > 0 ) {\r
+                       pitch = 90;\r
+               } else {\r
+                       pitch = 270;\r
+               }\r
+       } else {\r
+               forward = ( float )idSqrt( x * x + y * y );\r
+               pitch = atan2( z, forward ) * 180 / M_PI;\r
+               if ( pitch < 0 ) {\r
+                       pitch += 360;\r
+               }\r
+       }\r
+\r
+       return pitch;\r
+}\r
+\r
+/*\r
+angles_t idVec3::toAngles( void ) {\r
+       float forward;\r
+       float yaw;\r
+       float pitch;\r
+       \r
+       if ( ( x == 0 ) && ( y == 0 ) ) {\r
+               yaw = 0;\r
+               if ( z > 0 ) {\r
+                       pitch = 90;\r
+               } else {\r
+                       pitch = 270;\r
+               }\r
+       } else {\r
+               yaw = atan2( y, x ) * 180 / M_PI;\r
+               if ( yaw < 0 ) {\r
+                       yaw += 360;\r
+               }\r
+\r
+               forward = ( float )idSqrt( x * x + y * y );\r
+               pitch = atan2( z, forward ) * 180 / M_PI;\r
+               if ( pitch < 0 ) {\r
+                       pitch += 360;\r
+               }\r
+       }\r
+\r
+       return angles_t( -pitch, yaw, 0 );\r
+}\r
+*/\r
+\r
+idVec3 LerpVector( idVec3 &w1, idVec3 &w2, const float t ) {\r
+       float omega, cosom, sinom, scale0, scale1;\r
+\r
+       cosom = w1 * w2;\r
+       if ( ( 1.0 - cosom ) > LERP_DELTA ) {\r
+               omega = acos( cosom );\r
+               sinom = sin( omega );\r
+               scale0 = sin( ( 1.0 - t ) * omega ) / sinom;\r
+               scale1 = sin( t * omega ) / sinom;\r
+       } else {\r
+               scale0 = 1.0 - t;\r
+               scale1 = t;\r
+       }\r
+\r
+       return ( w1 * scale0 + w2 * scale1 );\r
+}\r
+\r
+/*\r
+=============\r
+idVec3::string\r
+\r
+This is just a convenience function\r
+for printing vectors\r
+=============\r
+*/\r
+char *idVec3::string( void ) {\r
+       static  int             index = 0;\r
+       static  char    str[ 8 ][ 36 ];\r
+       char    *s;\r
+\r
+       // use an array so that multiple toString's won't collide\r
+       s = str[ index ];\r
+       index = (index + 1)&7;\r
+\r
+       sprintf( s, "%.2f %.2f %.2f", x, y, z );\r
+\r
+       return s;\r
+}\r