PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/v2math.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 126 lines | 76 code | 20 blank | 30 comment | 3 complexity | 89109fcf64fea7eee0b46a5511af5f8f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v2math.cpp
  3. * @brief LLVector2 class implementation.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. //#include "vmath.h"
  28. #include "v2math.h"
  29. #include "v3math.h"
  30. #include "v4math.h"
  31. #include "m4math.h"
  32. #include "m3math.h"
  33. #include "llquaternion.h"
  34. // LLVector2
  35. LLVector2 LLVector2::zero(0,0);
  36. // Non-member functions
  37. // Sets all values to absolute value of their original values
  38. // Returns TRUE if data changed
  39. BOOL LLVector2::abs()
  40. {
  41. BOOL ret = FALSE;
  42. if (mV[0] < 0.f) { mV[0] = -mV[0]; ret = TRUE; }
  43. if (mV[1] < 0.f) { mV[1] = -mV[1]; ret = TRUE; }
  44. return ret;
  45. }
  46. F32 angle_between(const LLVector2& a, const LLVector2& b)
  47. {
  48. LLVector2 an = a;
  49. LLVector2 bn = b;
  50. an.normVec();
  51. bn.normVec();
  52. F32 cosine = an * bn;
  53. F32 angle = (cosine >= 1.0f) ? 0.0f :
  54. (cosine <= -1.0f) ? F_PI :
  55. acos(cosine);
  56. return angle;
  57. }
  58. BOOL are_parallel(const LLVector2 &a, const LLVector2 &b, float epsilon)
  59. {
  60. LLVector2 an = a;
  61. LLVector2 bn = b;
  62. an.normVec();
  63. bn.normVec();
  64. F32 dot = an * bn;
  65. if ( (1.0f - fabs(dot)) < epsilon)
  66. {
  67. return TRUE;
  68. }
  69. return FALSE;
  70. }
  71. F32 dist_vec(const LLVector2 &a, const LLVector2 &b)
  72. {
  73. F32 x = a.mV[0] - b.mV[0];
  74. F32 y = a.mV[1] - b.mV[1];
  75. return (F32) sqrt( x*x + y*y );
  76. }
  77. F32 dist_vec_squared(const LLVector2 &a, const LLVector2 &b)
  78. {
  79. F32 x = a.mV[0] - b.mV[0];
  80. F32 y = a.mV[1] - b.mV[1];
  81. return x*x + y*y;
  82. }
  83. F32 dist_vec_squared2D(const LLVector2 &a, const LLVector2 &b)
  84. {
  85. F32 x = a.mV[0] - b.mV[0];
  86. F32 y = a.mV[1] - b.mV[1];
  87. return x*x + y*y;
  88. }
  89. LLVector2 lerp(const LLVector2 &a, const LLVector2 &b, F32 u)
  90. {
  91. return LLVector2(
  92. a.mV[VX] + (b.mV[VX] - a.mV[VX]) * u,
  93. a.mV[VY] + (b.mV[VY] - a.mV[VY]) * u );
  94. }
  95. LLSD LLVector2::getValue() const
  96. {
  97. LLSD ret;
  98. ret[0] = mV[0];
  99. ret[1] = mV[1];
  100. return ret;
  101. }
  102. void LLVector2::setValue(LLSD& sd)
  103. {
  104. mV[0] = (F32) sd[0].asReal();
  105. mV[1] = (F32) sd[1].asReal();
  106. }