2
0

quadTreeTracer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _QUADTREETRACER_H_
  23. #define _QUADTREETRACER_H_
  24. #include "platform/platform.h"
  25. #include "math/mPoint3.h"
  26. #include "scene/sceneObject.h"
  27. /// Helper class to perform a fast, recursive ray cast against a set of
  28. /// hierarchical bounding boxes.
  29. ///
  30. /// This class assumes that it is working on a unit quadtree (ie, one that
  31. /// extends from 0..1 in the XY dimensions. Z scale is unaffected).
  32. ///
  33. /// Node indexing is done TGE Terrain style - 0 is the largest level of the
  34. /// quadtree, while coordinates are always in the full range of the quadtree
  35. /// (in a 6 deep tree, 0..63, for instance). This allows the quadtree descent
  36. /// to be very fast!
  37. class QuadTreeTracer
  38. {
  39. protected:
  40. struct StackNode
  41. {
  42. Point2I squarePos;
  43. U32 level;
  44. };
  45. struct RayStackNode : StackNode
  46. {
  47. F32 startT;
  48. F32 endT;
  49. };
  50. U32 mTreeDepth;
  51. QuadTreeTracer(U32 treeDepth)
  52. : mTreeDepth(treeDepth)
  53. {
  54. }
  55. /// Children better implement these! They return min/max height bounds
  56. /// of the specified square.
  57. virtual const F32 getSquareMin(const U32 &level, const Point2I &pos) const = 0;
  58. virtual const F32 getSquareMax(const U32 &level, const Point2I &pos) const = 0;
  59. /// And this does checks on leaf nodes.
  60. virtual bool castLeafRay(const Point2I pos, const Point3F &start, const Point3F &end, const F32 &startT, const F32 &endT, RayInfo *info) = 0;
  61. /// Helper function to calculate intercepts.
  62. inline const F32 calcIntercept(const F32 vStart, const F32 invDeltaV, const F32 intercept) const
  63. {
  64. return (intercept - vStart) * invDeltaV;
  65. }
  66. public:
  67. /// Size of a quadtree of depth.
  68. static inline const U32 getNodeCount(const U32 depth)
  69. {
  70. return 0x55555555 & ((1 << depth*2) - 1);
  71. }
  72. /// Index of a node at given position in a quadtree.
  73. static inline const U32 getNodeIndex(const U32 level, const Point2I pos)
  74. {
  75. //AssertFatal(level < mTreeDepth, "QuadTreeTracer::getNodeIndex - out of range level!)
  76. AssertFatal(pos.x < BIT(level) && pos.x >= 0 , "QuadTreeTracer::getNodeIndex - out of range x for level!");
  77. AssertFatal(pos.y < BIT(level) && pos.y >= 0 , "QuadTreeTracer::getNodeIndex - out of range y for level!");
  78. const U32 base = getNodeCount(level);
  79. return base + (pos.x << level) + pos.y;
  80. }
  81. /// Cast a ray against a quadtree of hierarchical bounding boxes.
  82. ///
  83. /// This method assumes the quadtree extends from (0..1) along the
  84. /// X and Y axes. Z is unscaled. You may need to adjust the points
  85. /// you pass into this method to get the proper results.
  86. bool castRay(const Point3F &start, const Point3F &end, RayInfo *info);
  87. };
  88. #endif