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

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 184 lines · 87 code · 40 blank · 57 comment · 2 complexity · 981e36982a1b5c7ddd7c30387c1859dd 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_IMATHLINE_H
  35. #define INCLUDED_IMATHLINE_H
  36. //-------------------------------------
  37. //
  38. // A 3D line class template
  39. //
  40. //-------------------------------------
  41. #include "ImathVec.h"
  42. #include "ImathLimits.h"
  43. #include "ImathMatrix.h"
  44. namespace Imath {
  45. template <class T>
  46. class Line3
  47. {
  48. public:
  49. Vec3<T> pos;
  50. Vec3<T> dir;
  51. //-------------------------------------------------------------
  52. // Constructors - default is normalized units along direction
  53. //-------------------------------------------------------------
  54. Line3() {}
  55. Line3(const Vec3<T>& point1, const Vec3<T>& point2);
  56. //------------------
  57. // State Query/Set
  58. //------------------
  59. void set(const Vec3<T>& point1,
  60. const Vec3<T>& point2);
  61. //-------
  62. // F(t)
  63. //-------
  64. Vec3<T> operator() (T parameter) const;
  65. //---------
  66. // Query
  67. //---------
  68. T distanceTo(const Vec3<T>& point) const;
  69. T distanceTo(const Line3<T>& line) const;
  70. Vec3<T> closestPointTo(const Vec3<T>& point) const;
  71. Vec3<T> closestPointTo(const Line3<T>& line) const;
  72. };
  73. //--------------------
  74. // Convenient typedefs
  75. //--------------------
  76. typedef Line3<float> Line3f;
  77. typedef Line3<double> Line3d;
  78. //---------------
  79. // Implementation
  80. //---------------
  81. template <class T>
  82. inline Line3<T>::Line3(const Vec3<T> &p0, const Vec3<T> &p1)
  83. {
  84. set(p0,p1);
  85. }
  86. template <class T>
  87. inline void Line3<T>::set(const Vec3<T> &p0, const Vec3<T> &p1)
  88. {
  89. pos = p0; dir = p1-p0;
  90. dir.normalize();
  91. }
  92. template <class T>
  93. inline Vec3<T> Line3<T>::operator()(T parameter) const
  94. {
  95. return pos + dir * parameter;
  96. }
  97. template <class T>
  98. inline T Line3<T>::distanceTo(const Vec3<T>& point) const
  99. {
  100. return (closestPointTo(point)-point).length();
  101. }
  102. template <class T>
  103. inline Vec3<T> Line3<T>::closestPointTo(const Vec3<T>& point) const
  104. {
  105. return ((point - pos) ^ dir) * dir + pos;
  106. }
  107. template <class T>
  108. inline T Line3<T>::distanceTo(const Line3<T>& line) const
  109. {
  110. T d = (dir % line.dir) ^ (line.pos - pos);
  111. return (d >= 0)? d: -d;
  112. }
  113. template <class T>
  114. inline Vec3<T>
  115. Line3<T>::closestPointTo(const Line3<T>& line) const
  116. {
  117. // Assumes the lines are normalized
  118. Vec3<T> posLpos = pos - line.pos ;
  119. T c = dir ^ posLpos;
  120. T a = line.dir ^ dir;
  121. T f = line.dir ^ posLpos ;
  122. T num = c - a * f;
  123. T denom = a*a - 1;
  124. T absDenom = ((denom >= 0)? denom: -denom);
  125. if (absDenom < 1)
  126. {
  127. T absNum = ((num >= 0)? num: -num);
  128. if (absNum >= absDenom * limits<T>::max())
  129. return pos;
  130. }
  131. return pos + dir * (num / denom);
  132. }
  133. template<class T>
  134. std::ostream& operator<< (std::ostream &o, const Line3<T> &line)
  135. {
  136. return o << "(" << line.pos << ", " << line.dir << ")";
  137. }
  138. template<class S, class T>
  139. inline Line3<S> operator * (const Line3<S> &line, const Matrix44<T> &M)
  140. {
  141. return Line3<S>( line.pos * M, (line.pos + line.dir) * M );
  142. }
  143. } // namespace Imath
  144. #endif