ImathSphere.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_IMATHSPHERE_H
  35. #define INCLUDED_IMATHSPHERE_H
  36. //-------------------------------------
  37. //
  38. // A 3D sphere class template
  39. //
  40. //-------------------------------------
  41. #include "ImathVec.h"
  42. #include "ImathBox.h"
  43. #include "ImathLine.h"
  44. #include "ImathNamespace.h"
  45. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  46. template <class T>
  47. class Sphere3
  48. {
  49. public:
  50. Vec3<T> center;
  51. T radius;
  52. //---------------
  53. // Constructors
  54. //---------------
  55. Sphere3() : center(0,0,0), radius(0) {}
  56. Sphere3(const Vec3<T> &c, T r) : center(c), radius(r) {}
  57. //-------------------------------------------------------------------
  58. // Utilities:
  59. //
  60. // s.circumscribe(b) sets center and radius of sphere s
  61. // so that the s tightly encloses box b.
  62. //
  63. // s.intersectT (l, t) If sphere s and line l intersect, then
  64. // intersectT() computes the smallest t,
  65. // t >= 0, so that l(t) is a point on the
  66. // sphere. intersectT() then returns true.
  67. //
  68. // If s and l do not intersect, intersectT()
  69. // returns false.
  70. //
  71. // s.intersect (l, i) If sphere s and line l intersect, then
  72. // intersect() calls s.intersectT(l,t) and
  73. // computes i = l(t).
  74. //
  75. // If s and l do not intersect, intersect()
  76. // returns false.
  77. //
  78. //-------------------------------------------------------------------
  79. void circumscribe(const Box<Vec3<T> > &box);
  80. bool intersect(const Line3<T> &l, Vec3<T> &intersection) const;
  81. bool intersectT(const Line3<T> &l, T &t) const;
  82. };
  83. //--------------------
  84. // Convenient typedefs
  85. //--------------------
  86. typedef Sphere3<float> Sphere3f;
  87. typedef Sphere3<double> Sphere3d;
  88. //---------------
  89. // Implementation
  90. //---------------
  91. template <class T>
  92. void Sphere3<T>::circumscribe(const Box<Vec3<T> > &box)
  93. {
  94. center = T(0.5) * (box.min + box.max);
  95. radius = (box.max - center).length();
  96. }
  97. template <class T>
  98. bool Sphere3<T>::intersectT(const Line3<T> &line, T &t) const
  99. {
  100. bool doesIntersect = true;
  101. Vec3<T> v = line.pos - center;
  102. T B = T(2.0) * (line.dir ^ v);
  103. T C = (v ^ v) - (radius * radius);
  104. // compute discriminant
  105. // if negative, there is no intersection
  106. T discr = B*B - T(4.0)*C;
  107. if (discr < 0.0)
  108. {
  109. // line and Sphere3 do not intersect
  110. doesIntersect = false;
  111. }
  112. else
  113. {
  114. // t0: (-B - sqrt(B^2 - 4AC)) / 2A (A = 1)
  115. T sqroot = Math<T>::sqrt(discr);
  116. t = (-B - sqroot) * T(0.5);
  117. if (t < 0.0)
  118. {
  119. // no intersection, try t1: (-B + sqrt(B^2 - 4AC)) / 2A (A = 1)
  120. t = (-B + sqroot) * T(0.5);
  121. }
  122. if (t < 0.0)
  123. doesIntersect = false;
  124. }
  125. return doesIntersect;
  126. }
  127. template <class T>
  128. bool Sphere3<T>::intersect(const Line3<T> &line, Vec3<T> &intersection) const
  129. {
  130. T t;
  131. if (intersectT (line, t))
  132. {
  133. intersection = line(t);
  134. return true;
  135. }
  136. else
  137. {
  138. return false;
  139. }
  140. }
  141. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  142. #endif // INCLUDED_IMATHSPHERE_H