All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
StackAllocatedRayCaster.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Collision.h"
6 #include "VectorMath.h"
7 #include "CollisionGroup.h"
8 #include "ICollisionBvhProvider.h"
9 #include "CollisionBvhRayCaster.h"
10 #include "ArrayAllocator.h"
11 
12 #include <vector>
13 
14 namespace Eegeo
15 {
16  namespace Collision
17  {
18  template <size_t CollisionBvhBufferSize, size_t MaxRayNodeIntersectionResults>
20  {
21  public:
22 #if defined(EEGEO_WIN) && !defined(NDEBUG)
23  typedef std::vector<const CollisionBvh*> CollisionBvhVector;
24  typedef std::vector<RayNodeIntersectionResult> RayNodeIntersectionResultVector;
25 #else
26  typedef std::vector<const CollisionBvh*, Eegeo::Helpers::ArrayAllocator<const CollisionBvh*, CollisionBvhBufferSize> > CollisionBvhVector;
27  typedef std::vector<RayNodeIntersectionResult, Eegeo::Helpers::ArrayAllocator<RayNodeIntersectionResult, MaxRayNodeIntersectionResults> > RayNodeIntersectionResultVector;
28 #endif
29  StackAllocatedRayCaster(const ICollisionBvhProvider& collisionBvhProvider)
30  : m_collisionBvhProvider(collisionBvhProvider)
31  {
32 
33  }
34 
35  RayCasterResult FindFirstRayIntersection(const u32 collidableGroupsMask, const float environmentFlatteningScale, const Eegeo::dv3& rayOrigin, const Eegeo::dv3& rayDirection)
36  {
37  m_collisionBvhBuffer.resize(CollisionBvhBufferSize);
38 
39  const size_t totalCount = m_collisionBvhProvider.CopyCollisionBvhsForGroups(collidableGroupsMask, m_collisionBvhBuffer.data(), m_collisionBvhBuffer.size());
40  Eegeo_ASSERT(totalCount <= m_collisionBvhBuffer.capacity(), "insufficient collisionBvhBuffer size, totalCount %ld", totalCount);
41  m_collisionBvhBuffer.resize(totalCount);
42 
43  CollisionBvhRayCaster<CollisionBvhVector, RayNodeIntersectionResultVector> rayCaster(m_collisionBvhBuffer, m_rayNodeIntersectionResults);
44  return rayCaster.FindFirstRayIntersection(environmentFlatteningScale, rayOrigin, rayDirection);
45  }
46  private:
47  const ICollisionBvhProvider& m_collisionBvhProvider;
48  CollisionBvhVector m_collisionBvhBuffer;
49  RayNodeIntersectionResultVector m_rayNodeIntersectionResults;
50 
51  void * operator new [](size_t) throw() {return 0;}
52  void operator delete [] (void*) {}
53  void * operator new (size_t) throw() {return 0;}
54  void operator delete (void*) {}
55  };
56  }
57 }