Do not const cast CollisionRegion

This commit is contained in:
sabrina-shanman 2018-08-03 13:43:19 -07:00
parent b7d5804edb
commit cf34a2cffd
3 changed files with 22 additions and 24 deletions

View file

@ -13,7 +13,6 @@
#include <glm/gtx/transform.hpp>
#include "ScriptEngineLogging.h"
#include "model-networking/ModelCache.h"
#include "UUIDHasher.h"
QVariantMap CollisionPickResult::toVariantMap() const {
@ -70,14 +69,10 @@ QVariantMap CollisionPickResult::toVariantMap() const {
return variantMap;
}
bool CollisionPick::isShapeInfoReady(CollisionRegion& pick) {
if (pick.shouldComputeShapeInfo()) {
if (!_cachedResource || _cachedResource->getURL() != pick.modelURL) {
_cachedResource = DependencyManager::get<ModelCache>()->getCollisionGeometryResource(pick.modelURL);
}
if (_cachedResource->isLoaded()) {
computeShapeInfo(pick, pick.shapeInfo, _cachedResource);
bool CollisionPick::isShapeInfoReady() {
if (_mathPick.shouldComputeShapeInfo()) {
if (_cachedResource && _cachedResource->isLoaded()) {
computeShapeInfo(_mathPick, *_mathPick.shapeInfo, _cachedResource);
return true;
}
@ -321,12 +316,12 @@ CollisionRegion CollisionPick::getMathematicalPick() const {
}
PickResultPointer CollisionPick::getEntityIntersection(const CollisionRegion& pick) {
if (!isShapeInfoReady(*const_cast<CollisionRegion*>(&pick))) {
if (!isShapeInfoReady()) {
// Cannot compute result
return std::make_shared<CollisionPickResult>();
}
const auto& entityIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_ENTITY, pick.shapeInfo, pick.transform);
const auto& entityIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_ENTITY, *pick.shapeInfo, pick.transform);
return std::make_shared<CollisionPickResult>(pick, entityIntersections, std::vector<ContactTestResult>());
}
@ -335,12 +330,12 @@ PickResultPointer CollisionPick::getOverlayIntersection(const CollisionRegion& p
}
PickResultPointer CollisionPick::getAvatarIntersection(const CollisionRegion& pick) {
if (!isShapeInfoReady(*const_cast<CollisionRegion*>(&pick))) {
if (!isShapeInfoReady()) {
// Cannot compute result
return std::make_shared<CollisionPickResult>();
}
const auto& avatarIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_AVATAR, pick.shapeInfo, pick.transform);
const auto& avatarIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_AVATAR, *pick.shapeInfo, pick.transform);
return std::make_shared<CollisionPickResult>(pick, std::vector<ContactTestResult>(), avatarIntersections);
}

View file

@ -62,6 +62,9 @@ public:
Pick(filter, maxDistance, enabled),
_mathPick(collisionRegion),
_physicsEngine(physicsEngine) {
if (collisionRegion.shouldComputeShapeInfo()) {
_cachedResource = DependencyManager::get<ModelCache>()->getCollisionGeometryResource(collisionRegion.modelURL);
}
}
CollisionRegion getMathematicalPick() const override;
@ -73,7 +76,7 @@ public:
protected:
// Returns true if pick.shapeInfo is valid. Otherwise, attempts to get the shapeInfo ready for use.
bool isShapeInfoReady(CollisionRegion& pick);
bool isShapeInfoReady();
void computeShapeInfo(CollisionRegion& pick, ShapeInfo& shapeInfo, QSharedPointer<GeometryResource> resource);
CollisionRegion _mathPick;

View file

@ -243,7 +243,7 @@ public:
transform.setScale(vec3FromVariant(shape["dimensions"]));
}
shapeInfo.setParams(shapeType, transform.getScale() / 2.0f, modelURL.toString());
shapeInfo->setParams(shapeType, transform.getScale() / 2.0f, modelURL.toString());
}
}
@ -259,7 +259,7 @@ public:
QVariantMap collisionRegion;
QVariantMap shape;
shape["shapeType"] = ShapeInfo::getNameForShapeType(shapeInfo.getType());
shape["shapeType"] = ShapeInfo::getNameForShapeType(shapeInfo->getType());
shape["modelURL"] = modelURL.toString();
shape["dimensions"] = vec3toVariant(transform.getScale());
@ -274,33 +274,33 @@ public:
operator bool() const override {
return !(glm::any(glm::isnan(transform.getTranslation())) ||
glm::any(glm::isnan(transform.getRotation())) ||
shapeInfo.getType() == SHAPE_TYPE_NONE);
shapeInfo->getType() == SHAPE_TYPE_NONE);
}
bool operator==(const CollisionRegion& other) const {
return glm::all(glm::equal(transform.getTranslation(), other.transform.getTranslation())) &&
glm::all(glm::equal(transform.getRotation(), other.transform.getRotation())) &&
glm::all(glm::equal(transform.getScale(), other.transform.getScale())) &&
shapeInfo.getType() == other.shapeInfo.getType() &&
shapeInfo->getType() == other.shapeInfo->getType() &&
modelURL == other.modelURL;
}
bool shouldComputeShapeInfo() const {
if (!(shapeInfo.getType() == SHAPE_TYPE_HULL ||
(shapeInfo.getType() >= SHAPE_TYPE_COMPOUND &&
shapeInfo.getType() <= SHAPE_TYPE_STATIC_MESH)
if (!(shapeInfo->getType() == SHAPE_TYPE_HULL ||
(shapeInfo->getType() >= SHAPE_TYPE_COMPOUND &&
shapeInfo->getType() <= SHAPE_TYPE_STATIC_MESH)
)) {
return false;
}
return !shapeInfo.getPointCollection().size();
return !shapeInfo->getPointCollection().size();
}
// We can't load the model here because it would create a circular dependency, so we delegate that responsibility to the owning CollisionPick
QUrl modelURL;
// We can't compute the shapeInfo here without loading the model first, so we delegate that responsibility to the owning CollisionPick
ShapeInfo shapeInfo;
std::shared_ptr<ShapeInfo> shapeInfo = std::make_shared<ShapeInfo>();
Transform transform;
};
@ -371,7 +371,7 @@ namespace std {
struct hash<CollisionRegion> {
size_t operator()(const CollisionRegion& a) const {
size_t result = 0;
hash_combine(result, a.transform, (int)a.shapeInfo.getType(), qHash(a.modelURL));
hash_combine(result, a.transform, (int)a.shapeInfo->getType(), qHash(a.modelURL));
return result;
}
};