ImathLine.h 4.7 KB

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