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