Vector.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../precompiled.h"
  21. #pragma hdrstop
  22. idVec2 vec2_origin( 0.0f, 0.0f );
  23. idVec3 vec3_origin( 0.0f, 0.0f, 0.0f );
  24. idVec4 vec4_origin( 0.0f, 0.0f, 0.0f, 0.0f );
  25. idVec5 vec5_origin( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f );
  26. idVec6 vec6_origin( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f );
  27. idVec6 vec6_infinity( idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY, idMath::INFINITY );
  28. //===============================================================
  29. //
  30. // idVec2
  31. //
  32. //===============================================================
  33. /*
  34. =============
  35. idVec2::ToString
  36. =============
  37. */
  38. const char *idVec2::ToString( int precision ) const {
  39. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  40. }
  41. /*
  42. =============
  43. Lerp
  44. Linearly inperpolates one vector to another.
  45. =============
  46. */
  47. void idVec2::Lerp( const idVec2 &v1, const idVec2 &v2, const float l ) {
  48. if ( l <= 0.0f ) {
  49. (*this) = v1;
  50. } else if ( l >= 1.0f ) {
  51. (*this) = v2;
  52. } else {
  53. (*this) = v1 + l * ( v2 - v1 );
  54. }
  55. }
  56. //===============================================================
  57. //
  58. // idVec3
  59. //
  60. //===============================================================
  61. /*
  62. =============
  63. idVec3::ToYaw
  64. =============
  65. */
  66. float idVec3::ToYaw( void ) const {
  67. float yaw;
  68. if ( ( y == 0.0f ) && ( x == 0.0f ) ) {
  69. yaw = 0.0f;
  70. } else {
  71. yaw = RAD2DEG( atan2( y, x ) );
  72. if ( yaw < 0.0f ) {
  73. yaw += 360.0f;
  74. }
  75. }
  76. return yaw;
  77. }
  78. /*
  79. =============
  80. idVec3::ToPitch
  81. =============
  82. */
  83. float idVec3::ToPitch( void ) const {
  84. float forward;
  85. float pitch;
  86. if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
  87. if ( z > 0.0f ) {
  88. pitch = 90.0f;
  89. } else {
  90. pitch = 270.0f;
  91. }
  92. } else {
  93. forward = ( float )idMath::Sqrt( x * x + y * y );
  94. pitch = RAD2DEG( atan2( z, forward ) );
  95. if ( pitch < 0.0f ) {
  96. pitch += 360.0f;
  97. }
  98. }
  99. return pitch;
  100. }
  101. /*
  102. =============
  103. idVec3::ToAngles
  104. =============
  105. */
  106. idAngles idVec3::ToAngles( void ) const {
  107. float forward;
  108. float yaw;
  109. float pitch;
  110. if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
  111. yaw = 0.0f;
  112. if ( z > 0.0f ) {
  113. pitch = 90.0f;
  114. } else {
  115. pitch = 270.0f;
  116. }
  117. } else {
  118. yaw = RAD2DEG( atan2( y, x ) );
  119. if ( yaw < 0.0f ) {
  120. yaw += 360.0f;
  121. }
  122. forward = ( float )idMath::Sqrt( x * x + y * y );
  123. pitch = RAD2DEG( atan2( z, forward ) );
  124. if ( pitch < 0.0f ) {
  125. pitch += 360.0f;
  126. }
  127. }
  128. return idAngles( -pitch, yaw, 0.0f );
  129. }
  130. /*
  131. =============
  132. idVec3::ToPolar
  133. =============
  134. */
  135. idPolar3 idVec3::ToPolar( void ) const {
  136. float forward;
  137. float yaw;
  138. float pitch;
  139. if ( ( x == 0.0f ) && ( y == 0.0f ) ) {
  140. yaw = 0.0f;
  141. if ( z > 0.0f ) {
  142. pitch = 90.0f;
  143. } else {
  144. pitch = 270.0f;
  145. }
  146. } else {
  147. yaw = RAD2DEG( atan2( y, x ) );
  148. if ( yaw < 0.0f ) {
  149. yaw += 360.0f;
  150. }
  151. forward = ( float )idMath::Sqrt( x * x + y * y );
  152. pitch = RAD2DEG( atan2( z, forward ) );
  153. if ( pitch < 0.0f ) {
  154. pitch += 360.0f;
  155. }
  156. }
  157. return idPolar3( idMath::Sqrt( x * x + y * y + z * z ), yaw, -pitch );
  158. }
  159. /*
  160. =============
  161. idVec3::ToMat3
  162. =============
  163. */
  164. idMat3 idVec3::ToMat3( void ) const {
  165. idMat3 mat;
  166. float d;
  167. mat[0] = *this;
  168. d = x * x + y * y;
  169. if ( !d ) {
  170. mat[1][0] = 1.0f;
  171. mat[1][1] = 0.0f;
  172. mat[1][2] = 0.0f;
  173. } else {
  174. d = idMath::InvSqrt( d );
  175. mat[1][0] = -y * d;
  176. mat[1][1] = x * d;
  177. mat[1][2] = 0.0f;
  178. }
  179. mat[2] = Cross( mat[1] );
  180. return mat;
  181. }
  182. /*
  183. =============
  184. idVec3::ToString
  185. =============
  186. */
  187. const char *idVec3::ToString( int precision ) const {
  188. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  189. }
  190. /*
  191. =============
  192. Lerp
  193. Linearly inperpolates one vector to another.
  194. =============
  195. */
  196. void idVec3::Lerp( const idVec3 &v1, const idVec3 &v2, const float l ) {
  197. if ( l <= 0.0f ) {
  198. (*this) = v1;
  199. } else if ( l >= 1.0f ) {
  200. (*this) = v2;
  201. } else {
  202. (*this) = v1 + l * ( v2 - v1 );
  203. }
  204. }
  205. /*
  206. =============
  207. SLerp
  208. Spherical linear interpolation from v1 to v2.
  209. Vectors are expected to be normalized.
  210. =============
  211. */
  212. #define LERP_DELTA 1e-6
  213. void idVec3::SLerp( const idVec3 &v1, const idVec3 &v2, const float t ) {
  214. float omega, cosom, sinom, scale0, scale1;
  215. if ( t <= 0.0f ) {
  216. (*this) = v1;
  217. return;
  218. } else if ( t >= 1.0f ) {
  219. (*this) = v2;
  220. return;
  221. }
  222. cosom = v1 * v2;
  223. if ( ( 1.0f - cosom ) > LERP_DELTA ) {
  224. omega = acos( cosom );
  225. sinom = sin( omega );
  226. scale0 = sin( ( 1.0f - t ) * omega ) / sinom;
  227. scale1 = sin( t * omega ) / sinom;
  228. } else {
  229. scale0 = 1.0f - t;
  230. scale1 = t;
  231. }
  232. (*this) = ( v1 * scale0 + v2 * scale1 );
  233. }
  234. /*
  235. =============
  236. ProjectSelfOntoSphere
  237. Projects the z component onto a sphere.
  238. =============
  239. */
  240. void idVec3::ProjectSelfOntoSphere( const float radius ) {
  241. float rsqr = radius * radius;
  242. float len = Length();
  243. if ( len < rsqr * 0.5f ) {
  244. z = sqrt( rsqr - len );
  245. } else {
  246. z = rsqr / ( 2.0f * sqrt( len ) );
  247. }
  248. }
  249. //===============================================================
  250. //
  251. // idVec4
  252. //
  253. //===============================================================
  254. /*
  255. =============
  256. idVec4::ToString
  257. =============
  258. */
  259. const char *idVec4::ToString( int precision ) const {
  260. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  261. }
  262. /*
  263. =============
  264. Lerp
  265. Linearly inperpolates one vector to another.
  266. =============
  267. */
  268. void idVec4::Lerp( const idVec4 &v1, const idVec4 &v2, const float l ) {
  269. if ( l <= 0.0f ) {
  270. (*this) = v1;
  271. } else if ( l >= 1.0f ) {
  272. (*this) = v2;
  273. } else {
  274. (*this) = v1 + l * ( v2 - v1 );
  275. }
  276. }
  277. //===============================================================
  278. //
  279. // idVec5
  280. //
  281. //===============================================================
  282. /*
  283. =============
  284. idVec5::ToString
  285. =============
  286. */
  287. const char *idVec5::ToString( int precision ) const {
  288. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  289. }
  290. /*
  291. =============
  292. idVec5::Lerp
  293. =============
  294. */
  295. void idVec5::Lerp( const idVec5 &v1, const idVec5 &v2, const float l ) {
  296. if ( l <= 0.0f ) {
  297. (*this) = v1;
  298. } else if ( l >= 1.0f ) {
  299. (*this) = v2;
  300. } else {
  301. x = v1.x + l * ( v2.x - v1.x );
  302. y = v1.y + l * ( v2.y - v1.y );
  303. z = v1.z + l * ( v2.z - v1.z );
  304. s = v1.s + l * ( v2.s - v1.s );
  305. t = v1.t + l * ( v2.t - v1.t );
  306. }
  307. }
  308. //===============================================================
  309. //
  310. // idVec6
  311. //
  312. //===============================================================
  313. /*
  314. =============
  315. idVec6::ToString
  316. =============
  317. */
  318. const char *idVec6::ToString( int precision ) const {
  319. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  320. }
  321. //===============================================================
  322. //
  323. // idVecX
  324. //
  325. //===============================================================
  326. float idVecX::temp[VECX_MAX_TEMP+4];
  327. float * idVecX::tempPtr = (float *) ( ( (int) idVecX::temp + 15 ) & ~15 );
  328. int idVecX::tempIndex = 0;
  329. /*
  330. =============
  331. idVecX::ToString
  332. =============
  333. */
  334. const char *idVecX::ToString( int precision ) const {
  335. return idStr::FloatArrayToString( ToFloatPtr(), GetDimension(), precision );
  336. }