include/cooking/PxConvexMeshDesc.h

File members: include/cooking/PxConvexMeshDesc.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_CONVEX_MESH_DESC_H
#define PX_CONVEX_MESH_DESC_H
#include "foundation/PxVec3.h"
#include "foundation/PxFlags.h"
#include "common/PxCoreUtilityTypes.h"
#include "geometry/PxConvexMesh.h"
#include "PxSDFDesc.h"

#if !PX_DOXYGEN
namespace physx
{
#endif
struct PxConvexFlag
{
    enum Enum
    {
        e16_BIT_INDICES     =   (1<<0),

        eCOMPUTE_CONVEX     =   (1<<1),

        eCHECK_ZERO_AREA_TRIANGLES      =   (1<<2),

        eQUANTIZE_INPUT = (1 << 3),

        eDISABLE_MESH_VALIDATION = (1 << 4),

        ePLANE_SHIFTING = (1 << 5),

        eFAST_INERTIA_COMPUTATION = (1 << 6),

        eGPU_COMPATIBLE PX_DEPRECATED = (1 << 7),

        eSHIFT_VERTICES = (1 << 8)
    };
};

typedef PxFlags<PxConvexFlag::Enum,PxU16> PxConvexFlags;
PX_FLAGS_OPERATORS(PxConvexFlag::Enum,PxU16)

class PxConvexMeshDesc
{
public:

    PxBoundedData points;

    PxBoundedData polygons;

    PxBoundedData indices;

    PxConvexFlags flags;

    PxU16 vertexLimit;

    PxU16 polygonLimit;

    PxU16 quantizedCount;

    PxSDFDesc* sdfDesc;

    PX_INLINE PxConvexMeshDesc();
    PX_INLINE void setToDefault();

    PX_INLINE bool isValid() const;
};

PX_INLINE PxConvexMeshDesc::PxConvexMeshDesc()  //constructor sets to default
: vertexLimit(255), polygonLimit(255), quantizedCount(255), sdfDesc(NULL)
{

}

PX_INLINE void PxConvexMeshDesc::setToDefault()
{
    *this = PxConvexMeshDesc();
}

PX_INLINE bool PxConvexMeshDesc::isValid() const
{
    // Check geometry
    if(points.count < 3 ||  //at least 1 trig's worth of points
        (points.count > 0xffff && flags & PxConvexFlag::e16_BIT_INDICES))
        return false;
    if(!points.data)
        return false;
    if(points.stride < sizeof(PxVec3))  //should be at least one point's worth of data
        return false;
    if (quantizedCount < 4)
        return false;

    // Check topology
    if(polygons.data)
    {
        if(polygons.count < 4) // we require 2 neighbors for each vertex - 4 polygons at least
            return false;

        if(!indices.data) // indices must be provided together with polygons
            return false;

        PxU32 limit = (flags & PxConvexFlag::e16_BIT_INDICES) ? sizeof(PxU16) : sizeof(PxU32);
        if(indices.stride < limit)
            return false;

        limit = sizeof(PxHullPolygon);
        if(polygons.stride < limit)
            return false;
    }
    else
    {
        // We can compute the hull from the vertices
        if(!(flags & PxConvexFlag::eCOMPUTE_CONVEX))
            return false;   // If the mesh is convex and we're not allowed to compute the hull,
                            // you have to provide it completely (geometry & topology).
    }

    if((flags & PxConvexFlag::ePLANE_SHIFTING) &&  vertexLimit < 4)
    {
        return false;
    }

    if (!(flags & PxConvexFlag::ePLANE_SHIFTING) && vertexLimit < 8)
    {
        return false;
    }

    if(vertexLimit > 255)
    {
        return false;
    }

    if (polygonLimit < 4)
    {
        return false;
    }

    if (polygonLimit > 255)
    {
        return false;
    }

    if (sdfDesc && !sdfDesc->isValid())
    {
        return false;
    }

    return true;
}

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif