Convert entityIntersections/avatarIntersections lists in

CollisionPickResult to shared pointers
This commit is contained in:
sabrina-shanman 2018-08-13 17:26:20 -07:00
parent 61d12923ea
commit aa4a6b2eae
4 changed files with 34 additions and 30 deletions

View file

@ -15,8 +15,8 @@
#include "ScriptEngineLogging.h" #include "ScriptEngineLogging.h"
#include "UUIDHasher.h" #include "UUIDHasher.h"
void buildObjectIntersectionsMap(IntersectionType intersectionType, const std::vector<ContactTestResult>& objectIntersections, std::unordered_map<QUuid, QVariantMap>& intersections, std::unordered_map<QUuid, QVariantList>& collisionPointPairs) { void buildObjectIntersectionsMap(IntersectionType intersectionType, const std::shared_ptr<std::vector<ContactTestResult>> objectIntersections, std::unordered_map<QUuid, QVariantMap>& intersections, std::unordered_map<QUuid, QVariantList>& collisionPointPairs) {
for (auto& objectIntersection : objectIntersections) { for (auto& objectIntersection : *objectIntersections) {
auto at = intersections.find(objectIntersection.foundID); auto at = intersections.find(objectIntersection.foundID);
if (at == intersections.end()) { if (at == intersections.end()) {
QVariantMap intersectingObject; QVariantMap intersectingObject;
@ -308,16 +308,17 @@ CollisionRegion CollisionPick::getMathematicalPick() const {
return _mathPick; return _mathPick;
} }
void CollisionPick::filterIntersections(std::vector<ContactTestResult>& intersections) const { void CollisionPick::filterIntersections(std::shared_ptr<std::vector<ContactTestResult>> intersections) const {
const QVector<QUuid>& ignoreItems = getIgnoreItems(); const QVector<QUuid>& ignoreItems = getIgnoreItems();
const QVector<QUuid>& includeItems = getIncludeItems(); const QVector<QUuid>& includeItems = getIncludeItems();
bool isWhitelist = includeItems.size(); bool isWhitelist = includeItems.size();
for (int i = 0; i < intersections.size(); i++) { int n = (int)intersections->size();
auto& intersection = intersections[i]; for (int i = 0; i < n; i++) {
auto& intersection = (*intersections)[i];
const QUuid& id = intersection.foundID; const QUuid& id = intersection.foundID;
if (ignoreItems.contains(id) || (isWhitelist && !includeItems.contains(id))) { if (ignoreItems.contains(id) || (isWhitelist && !includeItems.contains(id))) {
intersections[i] = intersections[intersections.size()-1]; intersection = (*intersections)[--n];
intersections.pop_back(); intersections->pop_back();
} }
} }
} }
@ -325,29 +326,29 @@ void CollisionPick::filterIntersections(std::vector<ContactTestResult>& intersec
PickResultPointer CollisionPick::getEntityIntersection(const CollisionRegion& pick) { PickResultPointer CollisionPick::getEntityIntersection(const CollisionRegion& pick) {
if (!isShapeInfoReady()) { if (!isShapeInfoReady()) {
// Cannot compute result // Cannot compute result
return std::make_shared<CollisionPickResult>(pick.toVariantMap(), CollisionPickResult::LOAD_STATE_NOT_LOADED, std::vector<ContactTestResult>(), std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pick.toVariantMap(), CollisionPickResult::LOAD_STATE_NOT_LOADED, std::make_shared<std::vector<ContactTestResult>>(), std::make_shared<std::vector<ContactTestResult>>());
} }
auto& entityIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_ENTITY, *pick.shapeInfo, pick.transform); auto& entityIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_ENTITY, *pick.shapeInfo, pick.transform);
filterIntersections(entityIntersections); filterIntersections(entityIntersections);
return std::make_shared<CollisionPickResult>(pick, CollisionPickResult::LOAD_STATE_LOADED, entityIntersections, std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pick, CollisionPickResult::LOAD_STATE_LOADED, entityIntersections, std::make_shared<std::vector<ContactTestResult>>());
} }
PickResultPointer CollisionPick::getOverlayIntersection(const CollisionRegion& pick) { PickResultPointer CollisionPick::getOverlayIntersection(const CollisionRegion& pick) {
return std::make_shared<CollisionPickResult>(pick.toVariantMap(), isShapeInfoReady() ? CollisionPickResult::LOAD_STATE_LOADED : CollisionPickResult::LOAD_STATE_NOT_LOADED, std::vector<ContactTestResult>(), std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pick.toVariantMap(), isShapeInfoReady() ? CollisionPickResult::LOAD_STATE_LOADED : CollisionPickResult::LOAD_STATE_NOT_LOADED, std::make_shared<std::vector<ContactTestResult>>(), std::make_shared<std::vector<ContactTestResult>>());
} }
PickResultPointer CollisionPick::getAvatarIntersection(const CollisionRegion& pick) { PickResultPointer CollisionPick::getAvatarIntersection(const CollisionRegion& pick) {
if (!isShapeInfoReady()) { if (!isShapeInfoReady()) {
// Cannot compute result // Cannot compute result
return std::make_shared<CollisionPickResult>(pick.toVariantMap(), CollisionPickResult::LOAD_STATE_NOT_LOADED, std::vector<ContactTestResult>(), std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pick.toVariantMap(), CollisionPickResult::LOAD_STATE_NOT_LOADED, std::make_shared<std::vector<ContactTestResult>>(), std::make_shared<std::vector<ContactTestResult>>());
} }
auto& avatarIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_AVATAR, *pick.shapeInfo, pick.transform); auto& avatarIntersections = _physicsEngine->getCollidingInRegion(MOTIONSTATE_TYPE_AVATAR, *pick.shapeInfo, pick.transform);
filterIntersections(avatarIntersections); filterIntersections(avatarIntersections);
return std::make_shared<CollisionPickResult>(pick, CollisionPickResult::LOAD_STATE_LOADED, std::vector<ContactTestResult>(), avatarIntersections); return std::make_shared<CollisionPickResult>(pick, CollisionPickResult::LOAD_STATE_LOADED, std::make_shared<std::vector<ContactTestResult>>(), avatarIntersections);
} }
PickResultPointer CollisionPick::getHUDIntersection(const CollisionRegion& pick) { PickResultPointer CollisionPick::getHUDIntersection(const CollisionRegion& pick) {
return std::make_shared<CollisionPickResult>(pick.toVariantMap(), isShapeInfoReady() ? CollisionPickResult::LOAD_STATE_LOADED : CollisionPickResult::LOAD_STATE_NOT_LOADED, std::vector<ContactTestResult>(), std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pick.toVariantMap(), isShapeInfoReady() ? CollisionPickResult::LOAD_STATE_LOADED : CollisionPickResult::LOAD_STATE_NOT_LOADED, std::make_shared<std::vector<ContactTestResult>>(), std::make_shared<std::vector<ContactTestResult>>());
} }

View file

@ -24,10 +24,14 @@ public:
CollisionPickResult() {} CollisionPickResult() {}
CollisionPickResult(const QVariantMap& pickVariant) : PickResult(pickVariant) {} CollisionPickResult(const QVariantMap& pickVariant) : PickResult(pickVariant) {}
CollisionPickResult(const CollisionRegion& searchRegion, LoadState loadState, const std::vector<ContactTestResult>& entityIntersections, const std::vector<ContactTestResult>& avatarIntersections) : CollisionPickResult(const CollisionRegion& searchRegion,
LoadState loadState,
std::shared_ptr<std::vector<ContactTestResult>> entityIntersections = std::make_shared<std::vector<ContactTestResult>>(),
std::shared_ptr<std::vector<ContactTestResult>> avatarIntersections = std::make_shared<std::vector<ContactTestResult>>()
) :
PickResult(searchRegion.toVariantMap()), PickResult(searchRegion.toVariantMap()),
loadState(loadState), loadState(loadState),
intersects(entityIntersections.size() || avatarIntersections.size()), intersects(entityIntersections->size() || avatarIntersections->size()),
entityIntersections(entityIntersections), entityIntersections(entityIntersections),
avatarIntersections(avatarIntersections) { avatarIntersections(avatarIntersections) {
} }
@ -41,8 +45,8 @@ public:
LoadState loadState { LOAD_STATE_UNKNOWN }; LoadState loadState { LOAD_STATE_UNKNOWN };
bool intersects { false }; bool intersects { false };
std::vector<ContactTestResult> entityIntersections; std::shared_ptr<std::vector<ContactTestResult>> entityIntersections { std::make_shared<std::vector<ContactTestResult>>() };
std::vector<ContactTestResult> avatarIntersections; std::shared_ptr<std::vector<ContactTestResult>> avatarIntersections { std::make_shared<std::vector<ContactTestResult>>() };
QVariantMap toVariantMap() const override; QVariantMap toVariantMap() const override;
@ -52,14 +56,14 @@ public:
PickResultPointer compareAndProcessNewResult(const PickResultPointer& newRes) override { PickResultPointer compareAndProcessNewResult(const PickResultPointer& newRes) override {
const std::shared_ptr<CollisionPickResult> newCollisionResult = std::static_pointer_cast<CollisionPickResult>(newRes); const std::shared_ptr<CollisionPickResult> newCollisionResult = std::static_pointer_cast<CollisionPickResult>(newRes);
for (ContactTestResult& entityIntersection : newCollisionResult->entityIntersections) { for (ContactTestResult& entityIntersection : *(newCollisionResult->entityIntersections)) {
entityIntersections.push_back(entityIntersection); entityIntersections->push_back(entityIntersection);
} }
for (ContactTestResult& avatarIntersection : newCollisionResult->avatarIntersections) { for (ContactTestResult& avatarIntersection : *(newCollisionResult->avatarIntersections)) {
avatarIntersections.push_back(avatarIntersection); avatarIntersections->push_back(avatarIntersection);
} }
intersects = entityIntersections.size() || avatarIntersections.size(); intersects = entityIntersections->size() || avatarIntersections->size();
if (newCollisionResult->loadState == LOAD_STATE_NOT_LOADED || loadState == LOAD_STATE_UNKNOWN) { if (newCollisionResult->loadState == LOAD_STATE_NOT_LOADED || loadState == LOAD_STATE_UNKNOWN) {
loadState = (LoadState)newCollisionResult->loadState; loadState = (LoadState)newCollisionResult->loadState;
} }
@ -81,7 +85,7 @@ public:
CollisionRegion getMathematicalPick() const override; CollisionRegion getMathematicalPick() const override;
PickResultPointer getDefaultResult(const QVariantMap& pickVariant) const override { PickResultPointer getDefaultResult(const QVariantMap& pickVariant) const override {
return std::make_shared<CollisionPickResult>(pickVariant, CollisionPickResult::LOAD_STATE_UNKNOWN, std::vector<ContactTestResult>(), std::vector<ContactTestResult>()); return std::make_shared<CollisionPickResult>(pickVariant, CollisionPickResult::LOAD_STATE_UNKNOWN, std::make_shared<std::vector<ContactTestResult>>(), std::make_shared<std::vector<ContactTestResult>>());
} }
PickResultPointer getEntityIntersection(const CollisionRegion& pick) override; PickResultPointer getEntityIntersection(const CollisionRegion& pick) override;
PickResultPointer getOverlayIntersection(const CollisionRegion& pick) override; PickResultPointer getOverlayIntersection(const CollisionRegion& pick) override;
@ -92,7 +96,7 @@ protected:
// Returns true if pick.shapeInfo is valid. Otherwise, attempts to get the shapeInfo ready for use. // Returns true if pick.shapeInfo is valid. Otherwise, attempts to get the shapeInfo ready for use.
bool isShapeInfoReady(); bool isShapeInfoReady();
void computeShapeInfo(CollisionRegion& pick, ShapeInfo& shapeInfo, QSharedPointer<GeometryResource> resource); void computeShapeInfo(CollisionRegion& pick, ShapeInfo& shapeInfo, QSharedPointer<GeometryResource> resource);
void filterIntersections(std::vector<ContactTestResult>& intersections) const; void filterIntersections(std::shared_ptr<std::vector<ContactTestResult>> intersections) const;
CollisionRegion _mathPick; CollisionRegion _mathPick;
PhysicsEnginePointer _physicsEngine; PhysicsEnginePointer _physicsEngine;

View file

@ -871,7 +871,6 @@ struct AllContactsCallback : public btCollisionWorld::ContactResultCallback {
btCollisionWorld::ContactResultCallback(), btCollisionWorld::ContactResultCallback(),
desiredObjectType(desiredObjectType), desiredObjectType(desiredObjectType),
collisionObject(), collisionObject(),
contacts(),
myAvatarCollisionObject(myAvatarCollisionObject) { myAvatarCollisionObject(myAvatarCollisionObject) {
const btCollisionShape* collisionShape = ObjectMotionState::getShapeManager()->getShape(shapeInfo); const btCollisionShape* collisionShape = ObjectMotionState::getShapeManager()->getShape(shapeInfo);
@ -898,7 +897,7 @@ struct AllContactsCallback : public btCollisionWorld::ContactResultCallback {
MotionStateType desiredObjectType; MotionStateType desiredObjectType;
btCollisionObject collisionObject; btCollisionObject collisionObject;
std::vector<ContactTestResult> contacts; std::shared_ptr<std::vector<ContactTestResult>> contacts = std::make_shared<std::vector<ContactTestResult>>();
btCollisionObject* myAvatarCollisionObject; btCollisionObject* myAvatarCollisionObject;
btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0, int partId0, int index0, const btCollisionObjectWrapper* colObj1, int partId1, int index1) override { btScalar addSingleResult(btManifoldPoint& cp, const btCollisionObjectWrapper* colObj0, int partId0, int index0, const btCollisionObjectWrapper* colObj1, int partId1, int index1) override {
@ -917,7 +916,7 @@ struct AllContactsCallback : public btCollisionWorld::ContactResultCallback {
// TODO: Give MyAvatar a motion state so we don't have to do this // TODO: Give MyAvatar a motion state so we don't have to do this
if (desiredObjectType == MOTIONSTATE_TYPE_AVATAR && myAvatarCollisionObject && myAvatarCollisionObject == otherBody) { if (desiredObjectType == MOTIONSTATE_TYPE_AVATAR && myAvatarCollisionObject && myAvatarCollisionObject == otherBody) {
contacts.emplace_back(Physics::getSessionUUID(), bulletToGLM(penetrationPoint), bulletToGLM(otherPenetrationPoint)); contacts->emplace_back(Physics::getSessionUUID(), bulletToGLM(penetrationPoint), bulletToGLM(otherPenetrationPoint));
return 0; return 0;
} }
@ -933,7 +932,7 @@ struct AllContactsCallback : public btCollisionWorld::ContactResultCallback {
} }
// This is the correct object type. Add it to the list. // This is the correct object type. Add it to the list.
contacts.emplace_back(candidate->getObjectID(), bulletToGLM(penetrationPoint), bulletToGLM(otherPenetrationPoint)); contacts->emplace_back(candidate->getObjectID(), bulletToGLM(penetrationPoint), bulletToGLM(otherPenetrationPoint));
return 0; return 0;
} }
@ -944,7 +943,7 @@ protected:
} }
}; };
std::vector<ContactTestResult> PhysicsEngine::getCollidingInRegion(MotionStateType desiredObjectType, const ShapeInfo& regionShapeInfo, const Transform& regionTransform) const { std::shared_ptr<std::vector<ContactTestResult>> PhysicsEngine::getCollidingInRegion(MotionStateType desiredObjectType, const ShapeInfo& regionShapeInfo, const Transform& regionTransform) const {
// TODO: Give MyAvatar a motion state so we don't have to do this // TODO: Give MyAvatar a motion state so we don't have to do this
btCollisionObject* myAvatarCollisionObject = nullptr; btCollisionObject* myAvatarCollisionObject = nullptr;
if (desiredObjectType == MOTIONSTATE_TYPE_AVATAR && _myAvatarController) { if (desiredObjectType == MOTIONSTATE_TYPE_AVATAR && _myAvatarController) {

View file

@ -126,7 +126,7 @@ public:
void setShowBulletConstraintLimits(bool value); void setShowBulletConstraintLimits(bool value);
// Function for getting colliding ObjectMotionStates in the world of specified type // Function for getting colliding ObjectMotionStates in the world of specified type
std::vector<ContactTestResult> getCollidingInRegion(MotionStateType desiredObjectType, const ShapeInfo& regionShapeInfo, const Transform& regionTransform) const; std::shared_ptr<std::vector<ContactTestResult>> getCollidingInRegion(MotionStateType desiredObjectType, const ShapeInfo& regionShapeInfo, const Transform& regionTransform) const;
private: private:
QList<EntityDynamicPointer> removeDynamicsForBody(btRigidBody* body); QList<EntityDynamicPointer> removeDynamicsForBody(btRigidBody* body);