All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CollisionBvhGatherer.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 
6 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
7 
8 #pragma once
9 
10 #include "Collision.h"
11 #include "VectorMath.h"
12 #include "CollisionGroup.h"
13 #include "ICollisionBvhProvider.h"
14 #include "CollisionBvhRayCaster.h"
15 
16 
17 #include <vector>
18 #include <algorithm>
19 
20 namespace Eegeo
21 {
22  namespace Collision
23  {
25  {
26  public:
27  typedef std::vector<const CollisionBvh*> CollisionBvhVector;
28 
29 
30  CollisionBvhGatherer(const ICollisionBvhProvider& collisionBvhProvider)
31  : m_collisionBvhProvider(collisionBvhProvider)
32  {
33 
34  }
35 
36  template <typename TPredicate, typename TOutIter>
37  TOutIter Gather(const u32 collidableGroupsMask, TOutIter outIter, TPredicate predicate)
38  {
39  const size_t totalCount = m_collisionBvhProvider.CountCollisionBvhsForGroups(collidableGroupsMask);
40  CollisionBvhVector collisionBvhBuffer;
41  collisionBvhBuffer.resize(totalCount);
42  m_collisionBvhProvider.CopyCollisionBvhsForGroups(collidableGroupsMask, collisionBvhBuffer.data(), collisionBvhBuffer.size());
43 
44  return std::remove_copy_if(collisionBvhBuffer.begin(), collisionBvhBuffer.end(), outIter, std::not1(predicate));
45  }
46  private:
47  const ICollisionBvhProvider& m_collisionBvhProvider;
48 
49  };
50 
51  struct ContainsCollisionMaterial : public std::unary_function<const Collision::CollisionBvh*, bool>
52  {
53  ContainsCollisionMaterial(std::string collisionMaterialId)
54  : m_collisionMaterialId(collisionMaterialId)
55  {}
56 
57  bool operator() (const Collision::CollisionBvh* pCollisionBvh) const
58  {
59  return pCollisionBvh->GetCollisionMaterialTable().Contains(m_collisionMaterialId);
60  }
61  private:
62  std::string m_collisionMaterialId;
63  };
64 
65  struct ContainsAnyOfCollisionMaterials : public std::unary_function<const Collision::CollisionBvh*, bool>
66  {
67  ContainsAnyOfCollisionMaterials(const std::vector<std::string>& collisionMaterialIds)
68  : m_collisionMaterialIds(collisionMaterialIds)
69  {}
70 
71  bool operator() (const Collision::CollisionBvh* pCollisionBvh) const
72  {
73  Eegeo_ASSERT(pCollisionBvh != NULL);
74  const CollisionMaterialTable& table = pCollisionBvh->GetCollisionMaterialTable();
75  for (std::vector<std::string>::const_iterator iter = m_collisionMaterialIds.begin(); iter != m_collisionMaterialIds.end(); ++iter)
76  {
77  const std::string& materialId = *iter;
78  if (table.Contains(materialId))
79  {
80  return true;
81  }
82  }
83  return false;
84  }
85  private:
86  const std::vector<std::string>& m_collisionMaterialIds;
87  };
88  }
89 }