include/PxQueryReport.h

File members: include/PxQueryReport.h

// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2023 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#ifndef PX_QUERY_REPORT_H
#define PX_QUERY_REPORT_H
#include "foundation/PxVec3.h"
#include "foundation/PxFlags.h"
#include "foundation/PxAssert.h"
#include "geometry/PxGeometryHit.h"
#include "geometry/PxGeometryQueryContext.h"
#include "PxPhysXConfig.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

class PxShape;
class PxRigidActor;

struct PxActorShape
{
    PX_INLINE PxActorShape() : actor(NULL), shape(NULL) {}
    PX_INLINE PxActorShape(PxRigidActor* a, PxShape* s) : actor(a), shape(s) {}

    PxRigidActor*   actor;
    PxShape*        shape;
};

// Extends geom hits with Px object pointers
struct PxRaycastHit : PxGeomRaycastHit, PxActorShape    {};
struct PxOverlapHit : PxGeomOverlapHit, PxActorShape    {};
struct PxSweepHit : PxGeomSweepHit, PxActorShape        {};

typedef bool PxAgain;

template<typename HitType>
struct PxHitCallback : PxQueryThreadContext
{
    HitType     block;
    bool        hasBlock;

    HitType*    touches;

    PxU32       maxNbTouches;

    PxU32       nbTouches;

    PxHitCallback(HitType* aTouches, PxU32 aMaxNbTouches)
        : hasBlock(false), touches(aTouches), maxNbTouches(aMaxNbTouches), nbTouches(0)
    {}

    virtual PxAgain processTouches(const HitType* buffer, PxU32 nbHits) = 0;

    virtual void finalizeQuery() {}

    virtual ~PxHitCallback() {}

    PX_FORCE_INLINE bool hasAnyHits() { return (hasBlock || (nbTouches > 0)); }
};

template<typename HitType>
struct PxHitBuffer : public PxHitCallback<HitType>
{
    PxHitBuffer(HitType* aTouches = NULL, PxU32 aMaxNbTouches = 0) : PxHitCallback<HitType>(aTouches, aMaxNbTouches) {}

    PX_INLINE PxU32             getNbAnyHits() const                { return getNbTouches() + PxU32(this->hasBlock); }
    PX_INLINE const HitType&    getAnyHit(const PxU32 index) const  { PX_ASSERT(index < getNbTouches() + PxU32(this->hasBlock));
                                                                        return index < getNbTouches() ? getTouches()[index] : this->block; }

    PX_INLINE PxU32             getNbTouches() const                { return this->nbTouches; }
    PX_INLINE const HitType*    getTouches() const                  { return this->touches; }
    PX_INLINE const HitType&    getTouch(const PxU32 index) const   { PX_ASSERT(index < getNbTouches()); return getTouches()[index]; }
    PX_INLINE PxU32             getMaxNbTouches() const             { return this->maxNbTouches; }

    virtual ~PxHitBuffer() {}

protected:
    // stops after the first callback
    virtual PxAgain processTouches(const HitType* buffer, PxU32 nbHits) { PX_UNUSED(buffer); PX_UNUSED(nbHits); return false; }
};

typedef PxHitCallback<PxRaycastHit> PxRaycastCallback;

typedef PxHitCallback<PxOverlapHit> PxOverlapCallback;

typedef PxHitCallback<PxSweepHit> PxSweepCallback;

typedef PxHitBuffer<PxRaycastHit> PxRaycastBuffer;

typedef PxHitBuffer<PxOverlapHit> PxOverlapBuffer;

typedef PxHitBuffer<PxSweepHit> PxSweepBuffer;

template <int N>
struct PxRaycastBufferN : public PxHitBuffer<PxRaycastHit>
{
    PxRaycastHit hits[N];
    PxRaycastBufferN() : PxHitBuffer<PxRaycastHit>(hits, N) {}
};

template <int N>
struct PxOverlapBufferN : public PxHitBuffer<PxOverlapHit>
{
    PxOverlapHit hits[N];
    PxOverlapBufferN() : PxHitBuffer<PxOverlapHit>(hits, N) {}
};

template <int N>
struct PxSweepBufferN : public PxHitBuffer<PxSweepHit>
{
    PxSweepHit hits[N];
    PxSweepBufferN() : PxHitBuffer<PxSweepHit>(hits, N) {}
};

struct PxQueryCache
{
    PX_INLINE PxQueryCache() : shape(NULL), actor(NULL), faceIndex(0xffffffff) {}

    PX_INLINE PxQueryCache(PxShape* s, PxU32 findex) : shape(s), actor(NULL), faceIndex(findex) {}

    PxShape*        shape;
    PxRigidActor*   actor;
    PxU32           faceIndex;
};

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif