PageRenderTime 46ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/cinder/AxisAlignedBox.cpp

https://github.com/mikeedwards/Cinder
C++ | 123 lines | 83 code | 19 blank | 21 comment | 34 complexity | 6cfc57f500d16f27295dc40496d8a9d4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Copyright (c) 2010, The Cinder Project: http://libcinder.org
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  5. the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this list of conditions and
  7. the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
  9. the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
  11. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  12. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  13. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  14. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  15. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  16. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  17. POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. #include "cinder/AxisAlignedBox.h"
  20. #include <limits>
  21. namespace cinder {
  22. AxisAlignedBox3f::AxisAlignedBox3f( const Vec3f &aMin, const Vec3f &aMax )
  23. {
  24. mExtents[0] = Vec3f( aMin.x, aMin.y, aMin.z );
  25. mExtents[1] = Vec3f( aMax.x, aMax.y, aMax.z );
  26. Vec3f extent( aMax.x - aMin.x, aMax.y - aMin.y, aMax.z - aMin.z );
  27. Vec3f mid( ( aMin.x + aMax.x ) / 2.0f, ( aMin.y + aMax.y ) / 2.0f, ( aMin.z + aMax.z ) / 2.0f );
  28. mVerts[0] = Vec3f( -0.5f, -0.5f, 0.5f ) * extent + mid;
  29. mVerts[1] = Vec3f( 0.5f, -0.5f, 0.5f ) * extent + mid;
  30. mVerts[2] = Vec3f( -0.5f, 0.5f, 0.5f ) * extent + mid;
  31. mVerts[3] = Vec3f( 0.5f, 0.5f, 0.5f ) * extent + mid;
  32. mVerts[4] = Vec3f( -0.5f, 0.5f, -0.5f ) * extent + mid;
  33. mVerts[5] = Vec3f( 0.5f, 0.5f, -0.5f ) * extent + mid;
  34. mVerts[6] = Vec3f( -0.5f, -0.5f, -0.5f ) * extent + mid;
  35. mVerts[7] = Vec3f( 0.5f, -0.5f, -0.5f ) * extent + mid;
  36. }
  37. bool AxisAlignedBox3f::intersects( const Ray &ray )
  38. {
  39. float tmin, tmax, tymin, tymax, tzmin, tzmax;
  40. tmin = (mExtents[ray.getSignX()].x - ray.getOrigin().x) * ray.getInverseDirection().x;
  41. tmax = (mExtents[1-ray.getSignX()].x - ray.getOrigin().x) * ray.getInverseDirection().x;
  42. tymin = (mExtents[ray.getSignY()].y - ray.getOrigin().y) * ray.getInverseDirection().y;
  43. tymax = (mExtents[1-ray.getSignY()].y - ray.getOrigin().y) * ray.getInverseDirection().y;
  44. if( ( tmin > tymax ) || ( tymin > tmax) )
  45. return false;
  46. if( tymin > tmin )
  47. tmin = tymin;
  48. if( tymax < tmax )
  49. tmax = tymax;
  50. tzmin = (mExtents[ray.getSignZ()].z - ray.getOrigin().z) * ray.getInverseDirection().z;
  51. tzmax = (mExtents[1 - ray.getSignZ()].z - ray.getOrigin().z) * ray.getInverseDirection().z;
  52. if( ( tmin > tzmax ) || ( tzmin > tmax ) )
  53. return false;
  54. if( tzmin > tmin )
  55. tmin = tzmin;
  56. if( tzmax < tmax )
  57. tmax = tzmax;
  58. return tmin > 0;
  59. }
  60. int AxisAlignedBox3f::intersect( const Ray &ray, float intersections[2] )
  61. {
  62. int i = 0;
  63. if( ray.calcTriangleIntersection( mVerts[2], mVerts[0], mVerts[1], &intersections[i] ) ) { // +Z
  64. i++;
  65. }
  66. else if( ray.calcTriangleIntersection( mVerts[1], mVerts[3], mVerts[2], &intersections[i] ) ) { // +Z
  67. i++;
  68. }
  69. if( ray.calcTriangleIntersection( mVerts[7], mVerts[5], mVerts[3], &intersections[i] ) ) { // +X
  70. i++; if( i > 2 ) return i;
  71. }
  72. else if( ray.calcTriangleIntersection( mVerts[1], mVerts[7], mVerts[3], &intersections[i] ) ) { // +X
  73. i++; if( i > 2 ) return i;
  74. }
  75. if( ray.calcTriangleIntersection( mVerts[3], mVerts[5], mVerts[4], &intersections[i] ) ) { // +Y
  76. i++; if( i > 2 ) return i;
  77. }
  78. else if( ray.calcTriangleIntersection( mVerts[2], mVerts[3], mVerts[4], &intersections[i] ) ) { // +Y
  79. i++; if( i > 2 ) return i;
  80. }
  81. if( ray.calcTriangleIntersection( mVerts[1], mVerts[0], mVerts[7], &intersections[i] ) ) { // -Y
  82. i++; if( i > 2 ) return i;
  83. }
  84. else if( ray.calcTriangleIntersection( mVerts[0], mVerts[6], mVerts[7], &intersections[i] ) ) { // -Y
  85. i++; if( i > 2 ) return i;
  86. }
  87. if( ray.calcTriangleIntersection( mVerts[4], mVerts[0], mVerts[2], &intersections[i] ) ) { // -X
  88. i++; if( i > 2 ) return i;
  89. }
  90. else if( ray.calcTriangleIntersection( mVerts[4], mVerts[6], mVerts[0], &intersections[i] ) ) { // -X
  91. i++; if( i > 2 ) return i;
  92. }
  93. if( ray.calcTriangleIntersection( mVerts[6], mVerts[4], mVerts[5], &intersections[i] ) ) { // -Z
  94. i++;
  95. }
  96. else if( ray.calcTriangleIntersection( mVerts[7], mVerts[6], mVerts[5], &intersections[i] ) ) { // -Z
  97. i++;
  98. }
  99. return i;
  100. }
  101. } // namespace cinder