llplane.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file llplane.h
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewergpl$
  5. *
  6. * Copyright (c) 2001-2009, Linden Research, Inc.
  7. *
  8. * Second Life Viewer Source Code
  9. * The source code in this file ("Source Code") is provided by Linden Lab
  10. * to you under the terms of the GNU General Public License, version 2.0
  11. * ("GPL"), unless you have obtained a separate licensing agreement
  12. * ("Other License"), formally executed by you and Linden Lab. Terms of
  13. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  14. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  15. *
  16. * There are special exceptions to the terms and conditions of the GPL as
  17. * it is applied to this Source Code. View the full text of the exception
  18. * in the file doc/FLOSS-exception.txt in this software distribution, or
  19. * online at
  20. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  21. *
  22. * By copying, modifying or distributing this software, you acknowledge
  23. * that you have read and understood your obligations described above,
  24. * and agree to abide by those obligations.
  25. *
  26. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  27. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  28. * COMPLETENESS OR PERFORMANCE.
  29. * $/LicenseInfo$
  30. */
  31. #ifndef LL_LLPLANE_H
  32. #define LL_LLPLANE_H
  33. #include "llvector4.h"
  34. // A simple way to specify a plane is to give its normal,
  35. // and it's nearest approach to the origin.
  36. //
  37. // Given the equation for a plane : A*x + B*y + C*z + D = 0
  38. // The plane normal = [A, B, C]
  39. // The closest approach = D / sqrtf(A*A + B*B + C*C)
  40. class alignas(16) LLPlane
  41. {
  42. public:
  43. LL_INLINE LLPlane() = default;
  44. LL_INLINE LLPlane(const LLVector3& p0, F32 d)
  45. {
  46. setVec(p0, d);
  47. }
  48. LL_INLINE LLPlane(const LLVector3& p0, const LLVector3& n)
  49. {
  50. setVec(p0, n);
  51. }
  52. LL_INLINE void setVec(const LLVector3& p0, F32 d)
  53. {
  54. mV.set(p0[0], p0[1], p0[2], d);
  55. }
  56. LL_INLINE void setVec(const LLVector3& p0, const LLVector3& n)
  57. {
  58. F32 d = -(p0 * n);
  59. setVec(n, d);
  60. }
  61. LL_INLINE void setVec(const LLVector3& p0, const LLVector3& p1,
  62. const LLVector3& p2)
  63. {
  64. LLVector3 u, v, w;
  65. u = p1 - p0;
  66. v = p2 - p0;
  67. w = u % v;
  68. w.normalize();
  69. F32 d = -(w * p0);
  70. setVec(w, d);
  71. }
  72. LL_INLINE LLPlane& operator=(const LLVector4& v2)
  73. {
  74. mV.set(v2[0], v2[1], v2[2], v2[3]);
  75. return *this;
  76. }
  77. LL_INLINE LLPlane& operator=(const LLVector4a& v2)
  78. {
  79. mV.set(v2[0], v2[1], v2[2], v2[3]);
  80. return *this;
  81. }
  82. LL_INLINE void set(const LLPlane& p2) { mV = p2.mV; }
  83. LL_INLINE F32 dist(const LLVector3& v2) const
  84. {
  85. return mV[0] * v2[0] + mV[1] * v2[1] + mV[2] * v2[2] + mV[3];
  86. }
  87. LL_INLINE LLSimdScalar dot3(const LLVector4a& b) const
  88. {
  89. return mV.dot3(b);
  90. }
  91. // Read-only access a single float in this vector. Do not use in proximity
  92. // to any function call that manipulates the data at the whole vector level
  93. // or you will incur a substantial penalty. Consider using the splat
  94. // functions instead
  95. LL_INLINE F32 operator[](S32 idx) const { return mV[idx]; }
  96. // preferable when index is known at compile time
  97. template<int N> LL_INLINE void getAt(LLSimdScalar& v) const
  98. {
  99. v = mV.getScalarAt<N>();
  100. }
  101. // Reset the vector to 0, 0, 0, 1
  102. LL_INLINE void clear() { mV.set(0.f, 0.f, 0.f, 1.f); }
  103. LL_INLINE void getVector3(LLVector3& vec) const { vec.set(mV[0], mV[1], mV[2]); }
  104. // Retrieve the mask indicating which of the x, y, or z axis are greater
  105. // or equal to zero.
  106. LL_INLINE U8 calcPlaneMask() const
  107. {
  108. return mV.greaterEqual(LLVector4a::getZero()).getGatheredBits() &
  109. LLVector4Logical::MASK_XYZ;
  110. }
  111. // Check if two planes are nearly same
  112. LL_INLINE bool equal(const LLPlane& p) const { return mV.equals4(p.mV); }
  113. private:
  114. LLVector4a mV;
  115. };
  116. #endif // LL_LLPLANE_H