All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CollisionBvh.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 "CollisionMaterialIndexLookupPair.h"
9 #include "CollisionGroup.h"
10 #include "CollisionMaterialTable.h"
11 #include "AllVertexTypes.h"
12 #include <vector>
13 #include <string>
14 
15 namespace Eegeo
16 {
17  namespace Collision
18  {
20  {
21  public:
22  CollisionBvh();
23 
24  CollisionBvh(const Eegeo::dv3& originEcef,
25  const v3& quantizationRelativeOrigin,
26  float quantizationScale,
27  float minAltitude,
28  float maxAltitude,
29  CollisionGroup::Type collisionGroup,
30  bool counterClockwiseWinding,
31  const std::vector<Rendering::VertexTypes::ShortPositionVertex>* pPoints,
32  const std::vector<u16>* pTriListIndices,
33  const std::vector<CollisionBvhNode>* pNodes,
34  const std::vector<CollisionMaterialIndexLookupPair>* pMaterialIndexLookupPairs,
35  const CollisionMaterialTable* pCollisionMaterialTable);
36 
37  ~CollisionBvh();
38 
39  const dv3& OriginEcef() const { return m_originEcef; }
40  const v3& QuantizationRelativeOrigin() const { return m_quantizationRelativeOrigin; }
41  float QuantizationScale() const { return m_quantizationScale; }
42  CollisionGroup::Type CollisionGroup() const { return m_collisionGroup; }
43  bool IsCounterClockwiseWinding() const { return m_counterClockwiseWinding; }
44  const std::vector<Rendering::VertexTypes::ShortPositionVertex>& QuantizedPoints() const { return *m_pPoints; }
45  const std::vector<u16>& TriListIndices() const { return *m_pTriListIndices; }
46  const std::vector<CollisionBvhNode>& Nodes() const { return *m_pNodes; }
47  const std::vector<CollisionMaterialIndexLookupPair>& MaterialIndexLookupPairs() const { return *m_pMaterialIndexLookupPairs; }
48  int FindMeshIndexForTriangle(int triangleIndex) const;
49  const float MinAltitude() const { return m_minAltitude; }
50  const float MaxAltitude() const { return m_maxAltitude; }
51 
52  inline v3 GetInflatedPoint(int vertexIndex) const;
53 
54  inline Eegeo::Geometry::SingleSphere GetSphere(const CollisionBvhNode& node) const;
55 
56  std::string CollisionMaterialIdForIndex(int materialIndex) const { return (materialIndex < 0) ? std::string() : m_pCollisionMaterialTable->IdAtIndex(materialIndex); }
57 
58  std::vector<u16> GatherTriListIndicesForMaterialIndex(const int materialIndex) const;
59 
60  const CollisionMaterialTable& GetCollisionMaterialTable() const { return *m_pCollisionMaterialTable; }
61  private:
62  const dv3 m_originEcef;
63  const v3 m_quantizationRelativeOrigin;
64  const float m_quantizationScale;
65  const float m_minAltitude;
66  const float m_maxAltitude;
67  const CollisionGroup::Type m_collisionGroup;
68  bool m_counterClockwiseWinding;
69  const std::vector<Rendering::VertexTypes::ShortPositionVertex>* m_pPoints;
70  const std::vector<u16>* m_pTriListIndices;
71  const std::vector<CollisionBvhNode>* m_pNodes;
72  const std::vector<CollisionMaterialIndexLookupPair>* m_pMaterialIndexLookupPairs;
73  const CollisionMaterialTable* m_pCollisionMaterialTable;
74  };
75 
76  inline v3 CollisionBvh::GetInflatedPoint(int vertexIndex) const
77  {
78  Eegeo_ASSERT_SLOW(vertexIndex >= 0 && vertexIndex < m_pPoints->size(), "vertex index out of range");
79 
80  const Rendering::VertexTypes::ShortPositionVertex& q = (*m_pPoints)[vertexIndex];
81  v3 p(q.x, q.y, q.z);
82  p *= m_quantizationScale;
83  p += m_quantizationRelativeOrigin;
84  return p;
85  }
86 
87  inline Eegeo::Geometry::SingleSphere CollisionBvh::GetSphere(const CollisionBvhNode& node) const
88  {
90  const Eegeo::Rendering::VertexTypes::ShortPositionVertex& qc = node.QuantizedSphereCentre();
91  sphere.centre.x = qc.x;
92  sphere.centre.y = qc.y;
93  sphere.centre.z = qc.z;
94  sphere.centre *= m_quantizationScale;
95  sphere.centre += m_quantizationRelativeOrigin;
96  sphere.radius = node.QuantizedSphereRadius()*m_quantizationScale;
97  return sphere;
98  }
99 
100  }
101 }