mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
added basic CollisionGeometryCache container
This commit is contained in:
parent
af1be8ccd4
commit
9f26836b43
2 changed files with 141 additions and 0 deletions
79
libraries/physics/src/CollisionGeometryCache.cpp
Normal file
79
libraries/physics/src/CollisionGeometryCache.cpp
Normal file
|
@ -0,0 +1,79 @@
|
|||
//
|
||||
// CollisionGeometryCache.cpp
|
||||
// libraries/physcis/src
|
||||
//
|
||||
// Created by Andrew Meadows 2016.07.13
|
||||
// Copyright 2016 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#include <cassert>
|
||||
//#include <glm/gtx/norm.hpp>
|
||||
|
||||
//#include "ShapeFactory.h"
|
||||
#include "CollisionGeometryCache.h"
|
||||
|
||||
int foo = 0;
|
||||
|
||||
GeometryPointer createGeometryFromShape(CollisionGeometryCache::Key key) {
|
||||
return std::make_shared<int>(++foo);
|
||||
}
|
||||
|
||||
CollisionGeometryCache::CollisionGeometryCache() {
|
||||
}
|
||||
|
||||
CollisionGeometryCache::~CollisionGeometryCache() {
|
||||
_geometryMap.clear();
|
||||
_pendingGarbage.clear();
|
||||
}
|
||||
|
||||
GeometryPointer CollisionGeometryCache::getGeometry(CollisionGeometryCache::Key key) {
|
||||
if (!key) {
|
||||
return GeometryPointer();
|
||||
}
|
||||
GeometryPointer geometry = 0;
|
||||
|
||||
CollisionGeometryMap::const_iterator itr = _geometryMap.find(key);
|
||||
if (itr != _geometryMap.end()) {
|
||||
// make geometry and add it to map
|
||||
geometry = createGeometryFromShape(key);
|
||||
if (geometry) {
|
||||
_geometryMap.insert(std::make_pair(key, geometry));
|
||||
}
|
||||
}
|
||||
return geometry;
|
||||
}
|
||||
|
||||
bool CollisionGeometryCache::releaseGeometry(CollisionGeometryCache::Key key) {
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
CollisionGeometryMap::const_iterator itr = _geometryMap.find(key);
|
||||
if (itr != _geometryMap.end()) {
|
||||
assert((*itr).second.use_count() != 1);
|
||||
if ((*itr).second.use_count() == 2) {
|
||||
// we hold all of the references inside the cache so we'll try to delete later
|
||||
_pendingGarbage.push_back(key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CollisionGeometryCache::collectGarbage() {
|
||||
int numShapes = _pendingGarbage.size();
|
||||
for (int i = 0; i < numShapes; ++i) {
|
||||
CollisionGeometryCache::Key key = _pendingGarbage[i];
|
||||
CollisionGeometryMap::const_iterator itr = _geometryMap.find(key);
|
||||
if (itr != _geometryMap.end()) {
|
||||
if ((*itr).second.use_count() == 1) {
|
||||
// we hold the only reference
|
||||
_geometryMap.erase(itr);
|
||||
}
|
||||
}
|
||||
}
|
||||
_pendingGarbage.clear();
|
||||
}
|
||||
|
62
libraries/physics/src/CollisionGeometryCache.h
Normal file
62
libraries/physics/src/CollisionGeometryCache.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
//
|
||||
// CollisionGeometryCache.h
|
||||
// libraries/physcis/src
|
||||
//
|
||||
// Created by Andrew Meadows 2016.07.13
|
||||
// Copyright 2016 High Fidelity, Inc.
|
||||
//
|
||||
// Distributed under the Apache License, Version 2.0.
|
||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
|
||||
#ifndef hifi_CollisionGeometryCache
|
||||
#define hifi_CollisionGeometryCache
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
//#include <btBulletDynamicsCommon.h>
|
||||
//#include <LinearMath/btHashMap.h>
|
||||
|
||||
class btCollisionShape;
|
||||
|
||||
// BEGIN TEST HACK
|
||||
using GeometryPointer = std::shared_ptr<int>;
|
||||
// END TEST HACK
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<btCollisionShape*> {
|
||||
std::size_t operator()(btCollisionShape* key) const {
|
||||
return (hash<void*>()((void*)key));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
class CollisionGeometryCache {
|
||||
public:
|
||||
using Key = btCollisionShape const *;
|
||||
|
||||
CollisionGeometryCache();
|
||||
~CollisionGeometryCache();
|
||||
|
||||
/// \return pointer to geometry
|
||||
GeometryPointer getGeometry(Key key);
|
||||
|
||||
/// \return true if geometry was found and released
|
||||
bool releaseGeometry(Key key);
|
||||
|
||||
/// delete geometries that have zero references
|
||||
void collectGarbage();
|
||||
|
||||
// validation methods
|
||||
uint32_t getNumGeometries() const { return (uint32_t)_geometryMap.size(); }
|
||||
bool hasGeometry(Key key) const { return _geometryMap.find(key) == _geometryMap.end(); }
|
||||
|
||||
private:
|
||||
using CollisionGeometryMap = std::unordered_map<Key, GeometryPointer>;
|
||||
CollisionGeometryMap _geometryMap;
|
||||
std::vector<Key> _pendingGarbage;
|
||||
};
|
||||
|
||||
#endif // hifi_CollisionGeometryCache
|
Loading…
Reference in a new issue