include/cooking/PxCooking.h

File members: include/cooking/PxCooking.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_COOKING_H
#define PX_COOKING_H
#include "common/PxPhysXCommonConfig.h"
#include "common/PxTolerancesScale.h"
#include "cooking/Pxc.h"

#include "cooking/PxConvexMeshDesc.h"
#include "cooking/PxTriangleMeshDesc.h"
#include "cooking/PxTetrahedronMeshDesc.h"
#include "cooking/PxMidphaseDesc.h"
#include "cooking/PxBVHDesc.h"
#include "geometry/PxTriangleMesh.h"
#include "geometry/PxTetrahedronMesh.h"
#include "geometry/PxBVH.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

class PxInsertionCallback;
class PxFoundation;
class PxAllocatorCallback;
class PxHeightFieldDesc;

struct PxConvexMeshCookingResult
{
    enum Enum
    {
        eSUCCESS,

        eZERO_AREA_TEST_FAILED,

        ePOLYGONS_LIMIT_REACHED,

        eFAILURE,

        eNON_GPU_COMPATIBLE
    };
};

struct PxConvexMeshCookingType
{
    enum Enum
    {
        eQUICKHULL
    };
};

struct PxTriangleMeshCookingResult
{
    enum Enum
    {
        eSUCCESS    = 0,

        eLARGE_TRIANGLE,

        eEMPTY_MESH,

        eFAILURE
    };
};

struct PxMeshPreprocessingFlag
{
    enum Enum
    {
        eWELD_VERTICES  =   1 << 0,

        eDISABLE_CLEAN_MESH =   1 << 1,

        eDISABLE_ACTIVE_EDGES_PRECOMPUTE    =   1 << 2,

        eFORCE_32BIT_INDICES    =   1 << 3,

        eENABLE_VERT_MAPPING    =   1 << 4,

        eENABLE_INERTIA = 1 << 5
    };
};

typedef PxFlags<PxMeshPreprocessingFlag::Enum,PxU32> PxMeshPreprocessingFlags;

struct PxCookingParams
{
    float       areaTestEpsilon;

    float       planeTolerance;

    PxConvexMeshCookingType::Enum convexMeshCookingType;

    bool        suppressTriangleMeshRemapTable;

    bool        buildTriangleAdjacencies;

    bool        buildGPUData;

    PxTolerancesScale scale;

    PxMeshPreprocessingFlags    meshPreprocessParams;

    PxReal      meshWeldTolerance;

    PxReal      meshAreaMinLimit;

    PxReal      meshEdgeLengthMaxLimit;

    PxMidphaseDesc midphaseDesc;

    PxU32   gaussMapLimit;

    PxReal maxWeightRatioInTet;

    PxCookingParams(const PxTolerancesScale& sc):
        areaTestEpsilon                 (0.06f*sc.length*sc.length),
        planeTolerance                  (0.0007f),
        convexMeshCookingType           (PxConvexMeshCookingType::eQUICKHULL),
        suppressTriangleMeshRemapTable  (false),
        buildTriangleAdjacencies        (false),
        buildGPUData                    (false),
        scale                           (sc),
        meshPreprocessParams            (0),
        meshWeldTolerance               (0.0f),
        meshAreaMinLimit                (0.0f),
        meshEdgeLengthMaxLimit          (500.0f),
        gaussMapLimit                   (32),
        maxWeightRatioInTet             (FLT_MAX)
    {
    }
};

#if !PX_DOXYGEN
} // namespace physx
#endif

// Immediate cooking

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxInsertionCallback* PxGetStandaloneInsertionCallback();

// ==== BVH ====

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookBVH(const physx::PxBVHDesc& desc, physx::PxOutputStream& stream);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxBVH* PxCreateBVH(const physx::PxBVHDesc& desc, physx::PxInsertionCallback& insertionCallback);

PX_FORCE_INLINE physx::PxBVH* PxCreateBVH(const physx::PxBVHDesc& desc)
{
    return PxCreateBVH(desc, *PxGetStandaloneInsertionCallback());
}

// ==== Heightfield ====

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookHeightField(const physx::PxHeightFieldDesc& desc, physx::PxOutputStream& stream);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxHeightField* PxCreateHeightField(const physx::PxHeightFieldDesc& desc, physx::PxInsertionCallback& insertionCallback);

PX_FORCE_INLINE physx::PxHeightField* PxCreateHeightField(const physx::PxHeightFieldDesc& desc)
{
    return PxCreateHeightField(desc, *PxGetStandaloneInsertionCallback());
}

// ==== Convex meshes ====

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookConvexMesh(const physx::PxCookingParams& params, const physx::PxConvexMeshDesc& desc, physx::PxOutputStream& stream, physx::PxConvexMeshCookingResult::Enum* condition=NULL);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxConvexMesh* PxCreateConvexMesh(const physx::PxCookingParams& params, const physx::PxConvexMeshDesc& desc, physx::PxInsertionCallback& insertionCallback, physx::PxConvexMeshCookingResult::Enum* condition=NULL);

PX_FORCE_INLINE physx::PxConvexMesh* PxCreateConvexMesh(const physx::PxCookingParams& params, const physx::PxConvexMeshDesc& desc)
{
    return PxCreateConvexMesh(params, desc, *PxGetStandaloneInsertionCallback());
}

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxValidateConvexMesh(const physx::PxCookingParams& params, const physx::PxConvexMeshDesc& desc);

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxComputeHullPolygons(const physx::PxCookingParams& params, const physx::PxSimpleTriangleMesh& mesh, physx::PxAllocatorCallback& inCallback, physx::PxU32& nbVerts, physx::PxVec3*& vertices,
                                                        physx::PxU32& nbIndices, physx::PxU32*& indices, physx::PxU32& nbPolygons, physx::PxHullPolygon*& hullPolygons);

// ==== Triangle meshes ====

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxValidateTriangleMesh(const physx::PxCookingParams& params, const physx::PxTriangleMeshDesc& desc);

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookTriangleMesh(const physx::PxCookingParams& params, const physx::PxTriangleMeshDesc& desc, physx::PxOutputStream& stream, physx::PxTriangleMeshCookingResult::Enum* condition=NULL);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxTriangleMesh* PxCreateTriangleMesh(const physx::PxCookingParams& params, const physx::PxTriangleMeshDesc& desc, physx::PxInsertionCallback& insertionCallback, physx::PxTriangleMeshCookingResult::Enum* condition=NULL);

PX_FORCE_INLINE physx::PxTriangleMesh*  PxCreateTriangleMesh(const physx::PxCookingParams& params, const physx::PxTriangleMeshDesc& desc)
{
    return PxCreateTriangleMesh(params, desc, *PxGetStandaloneInsertionCallback());
}

// ==== Tetrahedron & deformable volume meshes ====

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookTetrahedronMesh(const physx::PxCookingParams& params, const physx::PxTetrahedronMeshDesc& meshDesc, physx::PxOutputStream& stream);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxTetrahedronMesh* PxCreateTetrahedronMesh(const physx::PxCookingParams& params, const physx::PxTetrahedronMeshDesc& meshDesc, physx::PxInsertionCallback& insertionCallback);

PX_FORCE_INLINE physx::PxTetrahedronMesh*   PxCreateTetrahedronMesh(const physx::PxCookingParams& params, const physx::PxTetrahedronMeshDesc& meshDesc)
{
    return PxCreateTetrahedronMesh(params, meshDesc, *PxGetStandaloneInsertionCallback());
}

PX_C_EXPORT PX_PHYSX_COOKING_API    bool PxCookDeformableVolumeMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc, physx::PxOutputStream& stream);

PX_DEPRECATED PX_FORCE_INLINE bool PxCookSoftBodyMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc, physx::PxOutputStream& stream)
{
    return PxCookDeformableVolumeMesh(params, simulationMeshDesc, collisionMeshDesc, simulationDataDesc, stream);
}

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxDeformableVolumeMesh* PxCreateDeformableVolumeMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc, physx::PxInsertionCallback& insertionCallback);

PX_DEPRECATED PX_FORCE_INLINE physx::PxDeformableVolumeMesh* PxCreateSoftBodyMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc, physx::PxInsertionCallback& insertionCallback)
{
    return PxCreateDeformableVolumeMesh(params, simulationMeshDesc, collisionMeshDesc, simulationDataDesc, insertionCallback);
}

PX_FORCE_INLINE physx::PxDeformableVolumeMesh* PxCreateDeformableVolumeMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc)
{
    return PxCreateDeformableVolumeMesh(params, simulationMeshDesc, collisionMeshDesc, simulationDataDesc, *PxGetStandaloneInsertionCallback());
}

PX_DEPRECATED PX_FORCE_INLINE   physx::PxDeformableVolumeMesh* PxCreateSoftBodyMesh(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc, const physx::PxTetrahedronMeshDesc& collisionMeshDesc,
    const physx::PxDeformableVolumeSimulationDataDesc& simulationDataDesc)
{
    return PxCreateDeformableVolumeMesh(params, simulationMeshDesc, collisionMeshDesc, simulationDataDesc, *PxGetStandaloneInsertionCallback());
}

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxCollisionMeshMappingData* PxComputeModelsMapping(const physx::PxCookingParams& params,
    physx::PxTetrahedronMeshData& simulationMesh, const physx::PxTetrahedronMeshData& collisionMesh,
    const physx::PxDeformableVolumeCollisionData& collisionData, const physx::PxBoundedData* vertexToTet = NULL);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxCollisionTetrahedronMeshData* PxComputeCollisionData(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& collisionMeshDesc);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxSimulationTetrahedronMeshData* PxComputeSimulationData(const physx::PxCookingParams& params,
    const physx::PxTetrahedronMeshDesc& simulationMeshDesc);

PX_C_EXPORT PX_PHYSX_COOKING_API    physx::PxDeformableVolumeMesh*  PxAssembleDeformableVolumeMesh(physx::PxTetrahedronMeshData& simulationMesh,
    physx::PxDeformableVolumeSimulationData& simulationData, physx::PxTetrahedronMeshData& collisionMesh, physx::PxDeformableVolumeCollisionData& collisionData,
    physx::PxCollisionMeshMappingData& mappingData, physx::PxInsertionCallback& insertionCallback);

PX_DEPRECATED PX_FORCE_INLINE   physx::PxDeformableVolumeMesh* PxAssembleSoftBodyMesh(physx::PxTetrahedronMeshData& simulationMesh,
    physx::PxDeformableVolumeSimulationData& simulationData, physx::PxTetrahedronMeshData& collisionMesh, physx::PxDeformableVolumeCollisionData& collisionData,
    physx::PxCollisionMeshMappingData& mappingData, physx::PxInsertionCallback& insertionCallback)
{
    return PxAssembleDeformableVolumeMesh(simulationMesh, simulationData, collisionMesh, collisionData, mappingData, insertionCallback);
}

PX_DEPRECATED PX_FORCE_INLINE   physx::PxDeformableVolumeMesh* PxAssembleSoftBodyMesh_Sim(physx::PxSimulationTetrahedronMeshData& simulationMesh,
    physx::PxCollisionTetrahedronMeshData& collisionMesh, physx::PxCollisionMeshMappingData& mappingData, physx::PxInsertionCallback& insertionCallback)
{
    return PxAssembleDeformableVolumeMesh(*simulationMesh.getMesh(), *simulationMesh.getData(),
        *collisionMesh.getMesh(), *collisionMesh.getData(), mappingData, insertionCallback);
}

#endif