include/PxIsosurfaceExtraction.h

File members: include/PxIsosurfaceExtraction.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_ISOSURFACE_EXTRACTION_H
#define PX_ISOSURFACE_EXTRACTION_H
#include "cudamanager/PxCudaContext.h"
#include "cudamanager/PxCudaContextManager.h"

#include "foundation/PxSimpleTypes.h"
#include "foundation/PxVec4.h"
#include "PxParticleSystem.h"

#include "PxSparseGridParams.h"

#include "foundation/PxArray.h"
#include "PxParticleGpu.h"

#if !PX_DOXYGEN
namespace physx
{
#endif

#if PX_SUPPORT_GPU_PHYSX

    struct PxIsosurfaceGridFilteringType
    {
        enum Enum
        {
            eNONE = 0,
            eSMOOTH = 1,
            eGROW = 2,
            eSHRINK = 3
        };
    };

    struct PxIsosurfaceParams
    {
        PX_INLINE PxIsosurfaceParams()
        {
            particleCenterToIsosurfaceDistance = 0.2f;
            numMeshSmoothingPasses = 4;
            numMeshNormalSmoothingPasses = 4;
            gridFilteringFlags = 0;
            gridSmoothingRadius = 0.2f;
        }

        PX_INLINE void clearFilteringPasses()
        {
            gridFilteringFlags = 0;
        }

        PX_INLINE PxU64 addGridFilteringPass(PxIsosurfaceGridFilteringType::Enum operation)
        {
            for (PxU32 i = 0; i < 32; ++i)
            {
                PxIsosurfaceGridFilteringType::Enum o;
                if (!getGridFilteringPass(i, o))
                {
                    setGridFilteringPass(i, operation);
                    return i;
                }
            }
            return 0xFFFFFFFFFFFFFFFF;
        }

        PX_INLINE void setGridFilteringPass(PxU32 passIndex, PxIsosurfaceGridFilteringType::Enum operation)
        {
            PX_ASSERT(passIndex < 32u);
            PxU32 shift = passIndex * 2;
            gridFilteringFlags &= ~(PxU64(3) << shift);
            gridFilteringFlags |= PxU64(operation) << shift;
        }

        PX_INLINE bool getGridFilteringPass(PxU32 passIndex, PxIsosurfaceGridFilteringType::Enum& operation) const
        {
            PxU32 shift = passIndex * 2;
            PxU64 v = gridFilteringFlags >> shift;
            v &= 3; //Extract last 2 bits
            operation = PxIsosurfaceGridFilteringType::Enum(v);
            return operation != PxIsosurfaceGridFilteringType::eNONE;
        }

        PxReal  particleCenterToIsosurfaceDistance;
        PxU32   numMeshSmoothingPasses;
        PxU32   numMeshNormalSmoothingPasses;
        PxU64   gridFilteringFlags;
        PxReal  gridSmoothingRadius;
    };

    class PxIsosurfaceExtractor
    {
    public:
        virtual PxIsosurfaceParams getIsosurfaceParams() const = 0;

        virtual void setIsosurfaceParams(const PxIsosurfaceParams& params) = 0;

        virtual PxU32 getNumVertices() const = 0;

        virtual PxU32 getNumTriangles() const = 0;

        virtual PxU32 getMaxVertices() const = 0;

        virtual PxU32 getMaxTriangles() const = 0;

        virtual void setMaxVerticesAndTriangles(PxU32 maxNumVertices, PxU32 maxNumTriangles) = 0;

        virtual PxU32 getMaxParticles() const = 0;

        virtual void setMaxParticles(PxU32 maxParticles) = 0;

        virtual void release() = 0;

        virtual void extractIsosurface(PxVec4* deviceParticlePos, const PxU32 numParticles, CUstream stream, PxU32* phases = NULL, PxU32 validPhaseMask = PxParticlePhaseFlag::eParticlePhaseFluid,
            PxU32* activeIndices = NULL, PxVec4* anisotropy1 = NULL, PxVec4* anisotropy2 = NULL, PxVec4* anisotropy3 = NULL, PxReal anisotropyFactor = 1.0f) = 0;

        virtual void setResultBufferHost(PxVec4* vertices, PxU32* triIndices, PxVec4* normals = NULL) = 0;

        virtual void setResultBufferDevice(PxVec4* vertices, PxU32* triIndices, PxVec4* normals) = 0;

        virtual void setEnabled(bool enabled) = 0;

        virtual bool isEnabled() const = 0;

        virtual ~PxIsosurfaceExtractor() {}
    };

    class PxSparseGridIsosurfaceExtractor : public PxIsosurfaceExtractor
    {
        virtual PxSparseGridParams getSparseGridParams() const = 0;

        virtual void setSparseGridParams(const PxSparseGridParams& params) = 0;
    };

    class PxIsosurfaceCallback : public PxParticleSystemCallback
    {
    public:
        void initialize(PxIsosurfaceExtractor* isosurfaceExtractor, PxU32 validPhaseMask = PxParticlePhaseFlag::eParticlePhaseFluid)
        {
            mIsosurfaceExtractor = isosurfaceExtractor;
            mValidPhaseMask = validPhaseMask;
        }

        virtual void onPostSolve(const PxGpuMirroredPointer<PxGpuParticleSystem>& gpuParticleSystem, CUstream stream)
        {
            mIsosurfaceExtractor->extractIsosurface(reinterpret_cast<PxVec4*>(gpuParticleSystem.mHostPtr->mUnsortedPositions_InvMass),
                gpuParticleSystem.mHostPtr->mCommonData.mMaxParticles, stream, gpuParticleSystem.mHostPtr->mUnsortedPhaseArray, mValidPhaseMask);
        }

        virtual void onBegin(const PxGpuMirroredPointer<PxGpuParticleSystem>& /*gpuParticleSystem*/, CUstream /*stream*/) { }

        virtual void onAdvance(const PxGpuMirroredPointer<PxGpuParticleSystem>& /*gpuParticleSystem*/, CUstream /*stream*/) { }

    private:
        PxIsosurfaceExtractor* mIsosurfaceExtractor;
        PxU32 mValidPhaseMask;
    };

#endif

#if !PX_DOXYGEN
} // namespace physx
#endif

#endif