include/geometry/PxCustomGeometry.h

File members: include/geometry/PxCustomGeometry.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-2024 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_CUSTOMGEOMETRY_H
#define PX_CUSTOMGEOMETRY_H

#include "geometry/PxGeometry.h"
#include "geometry/PxGeometryHit.h"
#include "geometry/PxGeometryQueryContext.h"
#include "foundation/PxFoundationConfig.h"

#if !PX_DOXYGEN
namespace physx
{
#endif
    class PxContactBuffer;
    class PxRenderOutput;
    class PxMassProperties;

    class PxCustomGeometry : public PxGeometry
    {
    public:
        PX_PHYSX_COMMON_API static PxU32 getUniqueID();

        struct Type
        {
            PX_INLINE Type() : mID(getUniqueID()) {}

            PX_INLINE Type(const Type& t) : mID(t.mID) {}

            PX_INLINE Type& operator = (const Type& t) { mID = t.mID; return *this; }

            PX_INLINE bool operator == (const Type& t) const { return mID == t.mID; }

            PX_INLINE bool operator != (const Type& t) const { return mID != t.mID; }

            PX_INLINE static Type INVALID() { PxU32 z(0); return reinterpret_cast<const Type&>(z); }

        private:
            PxU32 mID;
        };

        struct Callbacks
        {
            virtual Type getCustomType() const = 0;

            virtual PxBounds3 getLocalBounds(const PxGeometry& geometry) const = 0;

            virtual bool generateContacts(const PxGeometry& geom0, const PxGeometry& geom1, const PxTransform& pose0, const PxTransform& pose1,
                const PxReal contactDistance, const PxReal meshContactMargin, const PxReal toleranceLength,
                PxContactBuffer& contactBuffer) const = 0;

            virtual PxU32 raycast(const PxVec3& origin, const PxVec3& unitDir, const PxGeometry& geom, const PxTransform& pose,
                PxReal maxDist, PxHitFlags hitFlags, PxU32 maxHits, PxGeomRaycastHit* rayHits, PxU32 stride, PxRaycastThreadContext* threadContext) const = 0;

            virtual bool overlap(const PxGeometry& geom0, const PxTransform& pose0, const PxGeometry& geom1, const PxTransform& pose1, PxOverlapThreadContext* threadContext) const = 0;

            virtual bool sweep(const PxVec3& unitDir, const PxReal maxDist,
                const PxGeometry& geom0, const PxTransform& pose0, const PxGeometry& geom1, const PxTransform& pose1,
                PxGeomSweepHit& sweepHit, PxHitFlags hitFlags, const PxReal inflation, PxSweepThreadContext* threadContext) const = 0;

            virtual void visualize(const PxGeometry& geometry, PxRenderOutput& out, const PxTransform& absPose, const PxBounds3& cullbox) const = 0;

            virtual void computeMassProperties(const PxGeometry& geometry, PxMassProperties& massProperties) const = 0;

            virtual bool usePersistentContactManifold(const PxGeometry& geometry, PxReal& breakingThreshold) const = 0;

            /* Destructor */
            virtual ~Callbacks() {}
        };

        PX_INLINE PxCustomGeometry() :
            PxGeometry(PxGeometryType::eCUSTOM),
            callbacks(NULL)
        {}

        PX_INLINE PxCustomGeometry(Callbacks& _callbacks) :
            PxGeometry(PxGeometryType::eCUSTOM),
            callbacks(&_callbacks)
        {}

        PX_INLINE PxCustomGeometry(const PxCustomGeometry& that) :
            PxGeometry(that),
            callbacks(that.callbacks)
        {}

        PX_INLINE void operator=(const PxCustomGeometry& that)
        {
            mType = that.mType;
            callbacks = that.callbacks;
        }

        PX_INLINE bool isValid() const;

        PX_INLINE Type getCustomType() const
        {
            return callbacks ? callbacks->getCustomType() : Type::INVALID();
        }

    public:
        Callbacks* callbacks;
    };

    PX_INLINE bool PxCustomGeometry::isValid() const
    {
        return mType == PxGeometryType::eCUSTOM && callbacks != NULL;
    }

#if !PX_DOXYGEN
} // namespace physx
#endif

#define DECLARE_CUSTOM_GEOMETRY_TYPE                                \
    static ::physx::PxCustomGeometry::Type TYPE();                  \
    virtual ::physx::PxCustomGeometry::Type getCustomType() const;

#define IMPLEMENT_CUSTOM_GEOMETRY_TYPE(CLASS)                       \
::physx::PxCustomGeometry::Type CLASS::TYPE()                       \
{                                                                   \
    static ::physx::PxCustomGeometry::Type customType;              \
    return customType;                                              \
}                                                                   \
::physx::PxCustomGeometry::Type CLASS::getCustomType() const        \
{                                                                   \
    return TYPE();                                                  \
}

#endif