/src/FreeImage/Source/OpenEXR/Imath/ImathVecAlgo.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 146 lines · 56 code · 30 blank · 60 comment · 2 complexity · e4cb681f650fbcb952677c1922c6b3be MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHVECALGO_H
  35. #define INCLUDED_IMATHVECALGO_H
  36. //-------------------------------------------------------------------------
  37. //
  38. // This file contains algorithms applied to or in conjunction
  39. // with points (Imath::Vec2 and Imath::Vec3).
  40. // The assumption made is that these functions are called much
  41. // less often than the basic point functions or these functions
  42. // require more support classes.
  43. //
  44. //-------------------------------------------------------------------------
  45. #include "ImathVec.h"
  46. #include "ImathLimits.h"
  47. namespace Imath {
  48. //-----------------------------------------------------------------
  49. // Find the projection of vector t onto vector s (Vec2, Vec3, Vec4)
  50. //-----------------------------------------------------------------
  51. template <class Vec> Vec project (const Vec &s, const Vec &t);
  52. //------------------------------------------------
  53. // Find a vector that is perpendicular to s and
  54. // in the same plane as s and t (Vec2, Vec3, Vec4)
  55. //------------------------------------------------
  56. template <class Vec> Vec orthogonal (const Vec &s, const Vec &t);
  57. //-----------------------------------------------
  58. // Find the direction of a ray s after reflection
  59. // off a plane with normal t (Vec2, Vec3, Vec4)
  60. //-----------------------------------------------
  61. template <class Vec> Vec reflect (const Vec &s, const Vec &t);
  62. //--------------------------------------------------------------------
  63. // Find the vertex of triangle (v0, v1, v2) that is closest to point p
  64. // (Vec2, Vec3, Vec4)
  65. //--------------------------------------------------------------------
  66. template <class Vec> Vec closestVertex (const Vec &v0,
  67. const Vec &v1,
  68. const Vec &v2,
  69. const Vec &p);
  70. //---------------
  71. // Implementation
  72. //---------------
  73. template <class Vec>
  74. Vec
  75. project (const Vec &s, const Vec &t)
  76. {
  77. Vec sNormalized = s.normalized();
  78. return sNormalized * (sNormalized ^ t);
  79. }
  80. template <class Vec>
  81. Vec
  82. orthogonal (const Vec &s, const Vec &t)
  83. {
  84. return t - project (s, t);
  85. }
  86. template <class Vec>
  87. Vec
  88. reflect (const Vec &s, const Vec &t)
  89. {
  90. return s - typename Vec::BaseType(2) * (s - project(t, s));
  91. }
  92. template <class Vec>
  93. Vec
  94. closestVertex(const Vec &v0,
  95. const Vec &v1,
  96. const Vec &v2,
  97. const Vec &p)
  98. {
  99. Vec nearest = v0;
  100. typename Vec::BaseType neardot = (v0 - p).length2();
  101. typename Vec::BaseType tmp = (v1 - p).length2();
  102. if (tmp < neardot)
  103. {
  104. neardot = tmp;
  105. nearest = v1;
  106. }
  107. tmp = (v2 - p).length2();
  108. if (tmp < neardot)
  109. {
  110. neardot = tmp;
  111. nearest = v2;
  112. }
  113. return nearest;
  114. }
  115. } // namespace Imath
  116. #endif