llvector2.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * @file llvector2.cpp
  3. * @brief LLVector2 class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llvector2.h"
  34. #include "llvector3.h"
  35. #include "llvector4.h"
  36. #include "llmatrix4.h"
  37. #include "llmatrix3.h"
  38. #include "llquaternion.h"
  39. // LLVector2
  40. LLVector2 LLVector2::zero(0.f, 0.f);
  41. // Non-member functions
  42. // Sets all values to absolute value of their original values. Returns true if
  43. // data changed.
  44. bool LLVector2::abs()
  45. {
  46. bool ret = false;
  47. if (mV[0] < 0.f)
  48. {
  49. mV[0] = -mV[0];
  50. ret = true;
  51. }
  52. if (mV[1] < 0.f)
  53. {
  54. mV[1] = -mV[1];
  55. ret = true;
  56. }
  57. return ret;
  58. }
  59. F32 angle_between(const LLVector2& a, const LLVector2& b)
  60. {
  61. LLVector2 an = a;
  62. LLVector2 bn = b;
  63. an.normalize();
  64. bn.normalize();
  65. F32 cosine = an * bn;
  66. F32 angle = cosine >= 1.f ? 0.f
  67. : (cosine <= -1.f ? F_PI : acosf(cosine));
  68. return angle;
  69. }
  70. bool are_parallel(const LLVector2& a, const LLVector2& b, float epsilon)
  71. {
  72. LLVector2 an = a;
  73. LLVector2 bn = b;
  74. an.normalize();
  75. bn.normalize();
  76. F32 dot = an * bn;
  77. return 1.f - fabsf(dot) < epsilon;
  78. }
  79. F32 dist_vec(const LLVector2& a, const LLVector2& b)
  80. {
  81. F32 x = a.mV[0] - b.mV[0];
  82. F32 y = a.mV[1] - b.mV[1];
  83. return sqrtf(x * x + y * y);
  84. }
  85. F32 dist_vec_squared(const LLVector2& a, const LLVector2& b)
  86. {
  87. F32 x = a.mV[0] - b.mV[0];
  88. F32 y = a.mV[1] - b.mV[1];
  89. return x * x + y * y;
  90. }
  91. F32 dist_vec_squared2D(const LLVector2& a, const LLVector2& b)
  92. {
  93. F32 x = a.mV[0] - b.mV[0];
  94. F32 y = a.mV[1] - b.mV[1];
  95. return x * x + y * y;
  96. }
  97. LLVector2 lerp(const LLVector2& a, const LLVector2& b, F32 u)
  98. {
  99. return LLVector2(a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
  100. a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u);
  101. }