All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CollisionBvhNodeIntersectionTests.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 "CollisionBvhNode.h"
8 #include "RayNodeIntersectionResult.h"
9 #include "IntersectionTests.h"
10 #include "CollisionBvh.h"
11 
12 #include <functional>
13 
14 namespace Eegeo
15 {
16  namespace Collision
17  {
18  struct BvhNodeRayIntersectionTest : public std::unary_function<CollisionBvhNode, RayNodeIntersectionResult>
19  {
20  BvhNodeRayIntersectionTest(const CollisionBvh& collisionBvh, Eegeo::v3 rayOrigin, Eegeo::v3 rayDirection, float skewedParamScale)
21  : m_collisionBvh(collisionBvh)
22  , m_rayOrigin(rayOrigin)
23  , m_rayDirection(rayDirection)
24  , m_skewedParamScale(skewedParamScale)
25  {
26 
27  }
28 
29  RayNodeIntersectionResult operator() (const CollisionBvhNode& candidate) const
30  {
31  float t;
32  const Geometry::SingleSphere& sphere = m_collisionBvh.GetSphere(candidate);
33  bool intersects = Geometry::IntersectionTests::SphereIntersectsWithRay(sphere, m_rayOrigin, m_rayDirection, t);
34  t *= m_skewedParamScale;
35  RayNodeIntersectionResult result(m_rayOrigin, m_rayDirection, m_skewedParamScale, m_collisionBvh, candidate, intersects, t);
36  return result;
37  }
38 
39  private:
40  const CollisionBvh& m_collisionBvh;
41  const Eegeo::v3 m_rayOrigin;
42  const Eegeo::v3 m_rayDirection;
43  const float m_skewedParamScale;
44  };
45 
46  struct BvhNodeLineSegmentIntersectionTest : public std::unary_function<CollisionBvhNode, RayNodeIntersectionResult>
47  {
48  BvhNodeLineSegmentIntersectionTest(const CollisionBvh& collisionBvh, Eegeo::v3 rayOrigin, Eegeo::v3 rayDirection, float lineSegmentLength, float skewedParamScale)
49  : m_collisionBvh(collisionBvh)
50  , m_rayOrigin(rayOrigin)
51  , m_rayDirection(rayDirection)
52  , m_lineSegmentLength(lineSegmentLength)
53  , m_skewedParamScale(skewedParamScale)
54  {
55 
56  }
57 
58  RayNodeIntersectionResult operator() (const CollisionBvhNode& candidate) const
59  {
60  float t;
61  const Geometry::SingleSphere& sphere = m_collisionBvh.GetSphere(candidate);
62 
63  bool intersectsRay = Geometry::IntersectionTests::SphereIntersectsWithRay(sphere, m_rayOrigin, m_rayDirection, t);
64  t *= m_skewedParamScale;
65  bool intersectsLineSegment = intersectsRay && (t <= m_lineSegmentLength);
66  RayNodeIntersectionResult result(m_rayOrigin, m_rayDirection, m_skewedParamScale, m_collisionBvh, candidate, intersectsLineSegment, t);
67  return result;
68  }
69 
70  private:
71  const CollisionBvh& m_collisionBvh;
72  const Eegeo::v3 m_rayOrigin;
73  const Eegeo::v3 m_rayDirection;
74  const float m_lineSegmentLength;
75  const float m_skewedParamScale;
76  };
77 
78  struct BvhNodeIntersectionTestPredicate : public std::unary_function<RayNodeIntersectionResult, bool>
79  {
80  bool operator() (const RayNodeIntersectionResult& testResult) const
81  {
82  return testResult.Intersects();
83  }
84  };
85  }
86 }