diff options
| author | Bryan Galdrikian <[email protected]> | 2017-02-21 12:07:59 -0800 |
|---|---|---|
| committer | Bryan Galdrikian <[email protected]> | 2017-02-21 12:07:59 -0800 |
| commit | 446ce137c6823ba9eff273bdafdaf266287c7c98 (patch) | |
| tree | d20aab3e2ed08d7b3ca71c2f40db6a93ea00c459 /NvBlast/sdk/extensions | |
| download | blast-1.0.0-beta.tar.xz blast-1.0.0-beta.zip | |
first commitv1.0.0-beta
Diffstat (limited to 'NvBlast/sdk/extensions')
111 files changed, 20905 insertions, 0 deletions
diff --git a/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringBondGenerator.h b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringBondGenerator.h new file mode 100644 index 0000000..68767eb --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringBondGenerator.h @@ -0,0 +1,136 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGBONDGENERATOR_H +#define NVBLASTEXTAUTHORINGBONDGENERATOR_H + +#include "NvBlastExtAuthoringTypes.h" +#include "NvBlastExtAuthoringFractureTool.h" +#include "NvBlastTypes.h" +#include "../cooking/PxCooking.h" +#include <PxPlane.h> +#include <NvBlastExtAuthoringCollisionBuilder.h> +struct NvBlastBondDesc; +struct NvBlastChunkDesc; +struct NvBlastBond; + +using namespace physx; + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class TriangleProcessor; +struct PlaneChunkIndexer; + +/** + Bond interface generation configuration + EXACT - common surface will be searched + AVERAGE - Inerface is approximated by projections or intersecitons with midplane +*/ +struct BondGenerationConfig +{ + enum BondGenMode { EXACT, AVERAGE }; + BondGenMode bondMode; +}; + + +struct PlaneChunkIndexer +{ + PlaneChunkIndexer(int32_t chunkId, int32_t trId, physx::PxPlane pl) : chunkId(chunkId), trId(trId), plane(pl) {} + int32_t chunkId; + int32_t trId; + physx::PxPlane plane; +}; + + +/** + Tool for gathering bond information from provided mesh geometry +*/ + +class BlastBondGenerator +{ +public: + + BlastBondGenerator(physx::PxCooking* cooking, physx::PxPhysicsInsertionCallback* insertionCallback) : mPxCooking(cooking), mPxInsertionCallback(insertionCallback){}; + + /** + This method based on marking triangles during fracture process, so can be used only with internally fractured meshes. + \param[in] tool FractureTool which contains chunks representation, tool->finalizeFracturing() should be called before. + \param[in] chunkIsSupport Array of flags, if true - chunk is support. Array size should be equal to chunk count in tool. + \param[out] resultBondDescs Array of created bond descriptors. + \param[out] resultChunkDescriptors Array of created chunk descriptors. + \return 0 if success + */ + int32_t buildDescFromInternalFracture(FractureTool* tool, const std::vector<bool>& chunkIsSupport, std::vector<NvBlastBondDesc>& resultBondDescs, std::vector<NvBlastChunkDesc>& resultChunkDescriptors); + + + /** + Creates bond description between two meshes + \param[in] meshA Array of triangles of mesh A. + \param[in] meshB Array of triangles of mesh B. + \param[out] resultBond Result bond description. + \param[in] conf Bond creation mode. + \return 0 if success + */ + int32_t createBondBetweenMeshes(const std::vector<Triangle>& meshA, const std::vector<Triangle>& meshB, NvBlastBond& resultBond, BondGenerationConfig conf = BondGenerationConfig()); + + /** + Creates bond description between number of meshes + \param[in] geometry Array of arrays of triangles for each chunk. + \param[out] resultBond Array of result bonds. + \param[in] overlaps Array of pairs - indexes of chunks, for which bond should be created. + \param[in] cfg Bond creation mode. + \return 0 if success + */ + int32_t createBondBetweenMeshes(const std::vector<std::vector<Triangle> >& geometry, std::vector<NvBlastBondDesc>& resultBond, const std::vector<std::pair<uint32_t, uint32_t> >& overlaps, BondGenerationConfig cfg); + + + /** + Creates bond description for prefractured meshes, when there is no info about which chunks should be connected with bond. + \param[in] geometry Array of arrays of triangles for each chunk. + \param[in] chunkIsSupport Array of flags, if true - chunk is support. Array size should be equal to chunk count in tool. + \param[out] resultBondDescs Array of result bonds. + \param[in] conf Bond creation mode. + \return 0 if success + */ + int32_t bondsFromPrefractured(const std::vector<std::vector<Triangle>>& geometry, const std::vector<bool>& chunkIsSupport, std::vector<NvBlastBondDesc>& resultBondDescs, BondGenerationConfig conf = BondGenerationConfig()); + +private: + float processWithMidplanes(TriangleProcessor* trProcessor, const std::vector<physx::PxVec3>& chunk1Points, const std::vector<physx::PxVec3>& chunk2Points, + const std::vector<physx::PxVec3>& hull1p,const std::vector<physx::PxVec3>& hull2p, physx::PxVec3& normal, physx::PxVec3& centroid); + + int32_t createFullBondListAveraged(const std::vector<std::vector<Triangle>>& chunksGeometry, const std::vector<bool>& supportFlags, std::vector<NvBlastBondDesc>& mResultBondDescs, BondGenerationConfig conf); + int32_t createFullBondListExact(const std::vector<std::vector<Triangle>>& chunksGeometry, const std::vector<bool>& supportFlags, std::vector<NvBlastBondDesc>& mResultBondDescs, BondGenerationConfig conf); + int32_t createFullBondListExactInternal(const std::vector<std::vector<Triangle>>& chunksGeometry, std::vector < PlaneChunkIndexer >& planeTriangleMapping , std::vector<NvBlastBondDesc>& mResultBondDescs); + int32_t createBondForcedInternal(const std::vector<PxVec3>& hull0, const std::vector<PxVec3>& hull1,const CollisionHull& cHull0, const CollisionHull& cHull1,PxBounds3 bound0, PxBounds3 bound1, NvBlastBond& resultBond, float overlapping); + + void buildGeometryCache(const std::vector<std::vector<Triangle> >& geometry); + void resetGeometryCache(); + + physx::PxCooking* mPxCooking; + physx::PxPhysicsInsertionCallback* mPxInsertionCallback; + + + std::vector<std::vector<Triangle> > mGeometryCache; + + std::vector<PlaneChunkIndexer> mPlaneCache; + std::vector<CollisionHull> mCHullCache; + std::vector<std::vector<physx::PxVec3> > mHullsPointsCache; + std::vector<physx::PxBounds3 > mBoundsCache; +}; + +} // namespace Blast +} // namespace Nv + +#endif // NVBLASTEXTAUTHORINGBONDGENERATOR_H
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringCollisionBuilder.h b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringCollisionBuilder.h new file mode 100644 index 0000000..b3e143a --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringCollisionBuilder.h @@ -0,0 +1,123 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGCOLLISIONBUILDER_H +#define NVBLASTEXTAUTHORINGCOLLISIONBUILDER_H + +#include "NvBlastTypes.h" +#include <vector> +#include <PxVec3.h> + +namespace physx +{ + class PxCooking; + class PxPhysicsInsertionCallback; + class PxVec3; + class PxConvexMesh; +} + + +namespace Nv +{ +namespace Blast +{ + +/** + Collision hull geometry format. +*/ +struct CollisionHull +{ + /** + Collision hull polygon format. + */ + struct HullPolygon + { + // Polygon base plane + float mPlane[4]; + // Number vertices in polygon + uint16_t mNbVerts; + // First index in CollisionHull.indices array for this polygon + uint16_t mIndexBase; + }; + ///** + + CollisionHull(){}; + + std::vector<physx::PxVec3> points; + std::vector<uint32_t> indices; + std::vector<HullPolygon> polygonData; +}; + + +/** + ConvexMeshBuilder provides routine to build collision hulls from array of vertices. + Collision hull is built as convex hull of provided point set. + If due to some reason building of convex hull is failed, collision hull is built as bounding box of vertex set. +*/ +class ConvexMeshBuilder +{ +public: + + /** + Constructor should be provided with PxCoocking and PxPhysicsInsertionCallback objects. + */ + ConvexMeshBuilder(physx::PxCooking* cooking, physx::PxPhysicsInsertionCallback* insertionCallback) : mInsertionCallback(insertionCallback), mCooking(cooking) {} + + /** + Method creates CollisionHull from provided array of vertices. + \param[in] vertexData Vertex array of some object, for which collision geometry should be built + \param[out] output Reference on CollisionHull object in which generated geometry should be saved + */ + void buildCollisionGeometry(const std::vector<physx::PxVec3>& vertexData, CollisionHull& output); + + /** + Method creates PxConvexMesh from provided array of vertices. + \param[in] vertexData Vertex array of some object, for which collision geometry should be built + + \return pointer to the PxConvexMesh object if it was built successfully, 'nullptr' otherwise. + */ + physx::PxConvexMesh* buildConvexMesh(std::vector<physx::PxVec3>& vertexData); + + + /** + Method creates PxConvexMesh from provided ConvexHull geometry + \param[in] hull ConvexHull geometry + + \return pointer to the PxConvexMesh object if it was built successfully, 'nullptr' otherwise. + */ + physx::PxConvexMesh* buildConvexMesh(CollisionHull& hull); + + + /** + Convex geometry trimming. + Using slicing with noised slicing surface can result in intersecting collision geometry. + It leads to unstable behaviour of rigid body simulation. + This method trims all intersecting parts of collision geometry. + As a drawback, trimming collision geometry can lead to penetrating render meshes during simulation. + + + \param[in] in ConvexHull geometry which should be clipped. + \param[in] chunkDepth Array of depth levels of convex hulls corresponding chunks. + + */ + + void trimCollisionGeometry(std::vector<CollisionHull>& in, const std::vector<uint32_t>& chunkDepth); + + +private: + physx::PxPhysicsInsertionCallback* mInsertionCallback; + physx::PxCooking* mCooking; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTAUTHORINGCOLLISIONBUILDER_H diff --git a/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringFractureTool.h b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringFractureTool.h new file mode 100644 index 0000000..528ffbc --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringFractureTool.h @@ -0,0 +1,435 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTAUTHORINGFRACTURETOOL_H +#define NVBLASTAUTHORINGFRACTURETOOL_H + +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastTypes.h" + + +namespace Nv +{ +namespace Blast +{ + +class SpatialAccelerator; +class ChunkPostProcessor; + + +/* + Chunk data, chunk with chunkId == 0 is always source mesh. +*/ +struct ChunkInfo +{ + Mesh* meshData; + int32_t parent; + int32_t chunkId; + bool isLeaf; +}; + + +/* + Slicing fracturing configuration + + + default: + x_slices = 1; + y_slices = 1; + z_slices = 1; + + offset_variations = 0.f; + angle_variations = 0.f; + noiseAmplitude = 0.f; + noiseFrequency = 1.f; + noiseOctaveNumber = 1; + surfaceResolution = 1; +*/ +struct SlicingConfiguration +{ + /** + Number of slices in each direction + */ + int32_t x_slices, y_slices, z_slices; + + /** + Offset variation, value in [0, 1] + */ + float offset_variations; + /** + Angle variation, value in [0, 1] + */ + float angle_variations; + + + /** + Noisy slicing configutaion: + + Amplitude of cutting surface noise. If it is 0 - noise is disabled. + */ + float noiseAmplitude; + /** + Frequencey of cutting surface noise. + */ + float noiseFrequency; + /** + Octave number in slicing surface noise. + */ + uint32_t noiseOctaveNumber; + /** + Cutting surface resolution. + */ + int32_t surfaceResolution; + + + SlicingConfiguration() + { + reset(); + } + /** + Set default params. + */ + void reset() + { + x_slices = 1; + y_slices = 1; + z_slices = 1; + + offset_variations = 0.f; + angle_variations = 0.f; + noiseAmplitude = 0.f; + noiseFrequency = 1.f; + noiseOctaveNumber = 1; + surfaceResolution = 1; + } + +}; + + + +/** + Class for voronoi sites generation inside supplied mesh. +*/ +class VoronoiSitesGenerator +{ +public: + + /** + Voronoi sites should not be generated outside of the fractured mesh, so VoronoiSitesGenerator + should be supplied with fracture mesh. + \param[in] mesh Fracture mesh + \param[in] rnd User supplied random value generator. + \return + */ + VoronoiSitesGenerator(Mesh* mesh, RandomGeneratorBase* rnd); + ~VoronoiSitesGenerator(); + + /** + Set base fracture mesh + */ + void setBaseMesh(Mesh* m); + + /** + Returns reference on vector of generated voronoi sites. + */ + std::vector<physx::PxVec3>& getVoronoiSites(); + + /** + Add site in particular point + \param[in] site Site coordinates + */ + void addSite(const physx::PxVec3& site); + /** + Uniformly generate sites inside the mesh + \param[in] numberOfSites Number of generated sites + */ + void uniformlyGenerateSitesInMesh(const uint32_t numberOfSites); + + /** + Generate sites in clustered fashion + \param[in] numberOfClusters Number of generated clusters + \param[in] sitesPerCluster Number of sites in each cluster + \param[in] clusterRadius Voronoi cells cluster radius + */ + void clusteredSitesGeneration(const uint32_t numberOfClusters, const uint32_t sitesPerCluster, float clusterRadius); + + /** + Radial pattern of sites generation + \param[in] center Center of generated pattern + \param[in] normal Normal to plane in which sites are generated + \param[in] radius Pattern radius + \param[in] angularSteps Number of angular steps + \param[in] radialSteps Number of radial steps + \param[in] angleOffset Angle offset at each radial step + \param[in] variability Randomness of sites distribution + */ + void radialPattern(const physx::PxVec3& center, const physx::PxVec3& normal, float radius, int32_t angularSteps, int32_t radialSteps, float angleOffset = 0.0f, float variability = 0.0f); + + /** + Generate sites inside sphere + \param[in] count Count of generated sites + \param[in] radius Radius of sphere + \param[in] center Center of sphere + */ + void generateInSphere(const uint32_t count, const float radius, const physx::PxVec3& center); + /** + Set stencil mesh. With stencil mesh sites are generated only inside both of fracture and stencil meshes. + \param[in] stencil Stencil mesh. + */ + void setStencil(Mesh* stencil); + /** + Removes stencil mesh + */ + void clearStencil(); + + /** + Deletes sites inside supplied sphere + \param[in] radius Radius of sphere + \param[in] center Center of sphere + \param[in] eraserProbability Probability of removing some particular site + */ + void deleteInSphere(const float radius, const physx::PxVec3& center, const float eraserProbability = 1); + +private: + std::vector<physx::PxVec3> mGeneratedSites; + Mesh* mMesh; + Mesh* mStencil; + RandomGeneratorBase* mRnd; + SpatialAccelerator* mAccelerator; +}; + + + +/** + FractureTool class provides methods to fracture provided mesh and generate Blast asset data +*/ +class FractureTool +{ + +public: + + /** + FractureTool can log asset creation info if logCallback is provided. + */ + FractureTool(NvBlastLog logCallback = nullptr) + { + mPlaneIndexerOffset = 1; + mChunkIdCounter = 0; + mRemoveIslands = false; + mLoggingCallback = logCallback; + } + + ~FractureTool() + { + reset(); + } + + /** + Reset FractureTool state. + */ + void reset(); + + + /** + Set input mesh wich will be fractured, FractureTool will be reseted. + */ + void setSourceMesh(Mesh* mesh); + + /** + Get chunk mesh in polygonal representation + */ + Mesh getChunkMesh(int32_t chunkId); + + /** + Input mesh is scaled and transformed internally to fit unit cube centered in origin. + Method provides offset vector and scale parameter; + */ + void getTransformation(physx::PxVec3& offset, float& scale); + + + /** + Fractures specified chunk with voronoi method. + \param[in] chunkId Chunk to fracture + \param[in] cellPoints Array of voronoi sites + \param[in] replaceChunk if 'true', newly generated chunks will replace source chunk, if 'false', newly generated chunks will be at next depth level, source chunk will be parent for them. + Case replaceChunk == true && chunkId == 0 considered as wrong input parameters + \return If 0, fracturing is successful. + */ + int32_t voronoiFracturing(uint32_t chunkId, const std::vector<physx::PxVec3>& cellPoints, bool replaceChunk); + + /** + Fractures specified chunk with voronoi method. Cells can be scaled along x,y,z axes. + \param[in] chunkId Chunk to fracture + \param[in] cellPoints Array of voronoi sites + \param[in] cellPoints Array of voronoi sites + \param[in] scale Voronoi cells scaling factor + \param[in] replaceChunk if 'true', newly generated chunks will replace source chunk, if 'false', newly generated chunks will be at next depth level, source chunk will be parent for them. + Case replaceChunk == true && chunkId == 0 considered as wrong input parameters + \return If 0, fracturing is successful. + */ + int32_t voronoiFracturing(uint32_t chunkId, const std::vector<physx::PxVec3>& cellPoints, const physx::PxVec3& scale, bool replaceChunk); + + + /** + Fractures specified chunk with slicing method. + \param[in] chunkId Chunk to fracture + \param[in] conf Slicing parameters, see SlicingConfiguration. + \param[in] replaceChunk if 'true', newly generated chunks will replace source chunk, if 'false', newly generated chunks will be at next depth level, source chunk will be parent for them. + Case replaceChunk == true && chunkId == 0 considered as wrong input parameters + \param[in] rnd User supplied random number generator + + \return If 0, fracturing is successful. + */ + int32_t slicing(uint32_t chunkId, SlicingConfiguration conf, bool replaceChunk, RandomGeneratorBase* rnd); + + + /** + Creates resulting fractured mesh geometry from intermediate format + */ + void finalizeFracturing(); + + /** + Get chunk information + */ + const std::vector<ChunkInfo>& getChunkList(); + + + /** + Tesselate interior surfaces + \param[in] averageEdgeLength - Average length of edge on internal surface. + */ + void tesselate(float averageEdgeLength); + + /** + Apply noise to interior surfaces. Must be called only after tesselation! + \param[in] amplitude Amplitude of noise + \param[in] frequency Frequency of noise + \param[in] octaves Number of noise octaves + \param[in] falloff - damping of noise around of external surface + \param[in] relaxIterations - number of smoothing iterations before applying noise + \param[in] relaxFactor - amount of smoothing before applying noise. + \param[in] seed Random seed value + */ + void applyNoise(float amplitude, float frequency, int32_t octaves, float falloff, int32_t relaxIterations, float relaxFactor, int32_t seed = 0); + + /** + Get percentage of mesh overlap. + percentage computed as volume(intersection(meshA , meshB)) / volume (meshA) + \param[in] meshA Mesh A + \param[in] meshB Mesh B + \return mesh overlap percentage + */ + static float getMeshOverlap(Mesh& meshA, Mesh& meshB); + + /** + Get chunk base mesh + \param[in] chunkIndex Chunk index + \param[out] output Array of triangles to be filled + */ + void getBaseMesh(int32_t chunkIndex, std::vector<Triangle>& output); + + /** + Get chunk mesh with noise + \param[in] chunkIndex Chunk index + \param[out] output Array of triangles to be filled + */ + void getNoisedMesh(int32_t chunkIndex, std::vector<Triangle>& output); + + + /** + Return index of chunk with specified chunkId + \param[in] chunkId Chunk ID + \return Chunk index in internal buffer, if not exist -1 is returned. + */ + int32_t getChunkIndex(int32_t chunkId); + + /** + Return id of chunk with specified index. + \param[in] chunkIndex Chunk index + \return Chunk id or -1 if there is no such chunk. + */ + int32_t getChunkId(int32_t chunkIndex); + + /** + Return depth level of the given chunk + \param[in] chunkId Chunk ID + \return Chunk depth or -1 if there is no such chunk. + */ + int32_t getChunkDepth(int32_t chunkId); + + /** + Return array of chunks IDs with given depth. + \param[in] depth Chunk depth + \return Array of chunk IDs + */ + std::vector<int32_t> getChunksIdAtDepth(uint32_t depth); + + + /** + Get result geometry without noise as vertex and index buffers, where index buffers contain series of triplets + which represent triangles. + \param[out] vertexBuffer Array of vertices to be filled + \param[out] indexBuffer Array of arrays of indices to be filled + */ + void getBufferedBaseMeshes(std::vector<Vertex>& vertexBuffer, std::vector<std::vector<uint32_t> >& indexBuffer); + + /** + Get result geometry after tesselation and application of noise as vertex and index buffers, where index buffers contain series of triplets + which represent triangles. + \param[out] vertexBuffer Array of vertices to be filled + \param[out] indexBuffer Array of arrays of indices to be filled + */ + void getBufferedNoiseMeshes(std::vector<Vertex>& vertexBuffer, std::vector<std::vector<uint32_t> >& indexBuffer); + + /** + Set automatic islands removing. May cause instabilities. + \param[in] isRemoveIslands Flag whether remove or not islands. + */ + void setRemoveIslands(bool isRemoveIslands); + + /** + Try find islands and remove them on some specifical chunk. If chunk has childs, island removing can lead to wrong results! Apply it before further chunk splitting. + \param[in] chunkId Chunk ID which should be checked for islands + \return Number of found islands is returned + */ + int32_t islandDetectionAndRemoving(int32_t chunkId); + +private: + void eraseChunk(int32_t chunkId); + bool isAncestorForChunk(int32_t ancestorId, int32_t chunkId); + void deleteAllChildsOfChunk(int32_t chunkId); + int32_t slicingNoisy(uint32_t chunkId, SlicingConfiguration conf, bool replaceChunk, RandomGeneratorBase* rnd); + +protected: + /** + Mesh scaled to unite-cube and translated to the origin + */ + float mScaleFactor; + physx::PxVec3 mOffset; + + /* Chunk mesh wrappers */ + std::vector<ChunkPostProcessor*> mChunkPostprocessors; + + + + int32_t mPlaneIndexerOffset; + int32_t mChunkIdCounter; + std::vector<ChunkInfo> mChunkData; + + bool mRemoveIslands; + + NvBlastLog mLoggingCallback; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTAUTHORINGFRACTURETOOL_H diff --git a/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringMesh.h b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringMesh.h new file mode 100644 index 0000000..2b1806a --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringMesh.h @@ -0,0 +1,174 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTAUTHORINGMESH_H +#define NVBLASTAUTHORINGMESH_H + +#include "NvBlastExtAuthoringTypes.h" +#include <vector> + + +namespace Nv +{ +namespace Blast +{ + +/** + Class for internal mesh representation +*/ +class Mesh +{ +public: + + /** + Constructs mesh object from array of triangles. + \param[in] position Array of vertex positions + \param[in] normals Array of vertex normals + \param[in] uv Array of vertex uv coordinates + \param[in] verticesCount Vertices count + \param[in] indices Array of vertex indices. Indices contain vertex index triplets which form a mesh triangle. + \param[in] indicesCount Indices count (should be equal to numberOfTriangles * 3) + */ + Mesh(physx::PxVec3* position, physx::PxVec3* normals, physx::PxVec2* uv, uint32_t verticesCount, uint32_t* indices, uint32_t indicesCount); + + /** + Constructs mesh object from array of facets. + \param[in] vertices Array of vertices + \param[in] edges Array of edges + \param[in] facets Array of facets + \param[in] posCount Vertices count + \param[in] edgesCount Edges count + \param[in] facetsCount Facets count + */ + Mesh(Vertex* vertices, Edge* edges, Facet* facets, uint32_t posCount, uint32_t edgesCount, uint32_t facetsCount); + + ~Mesh(); + + /** + Return true if mesh is valid + */ + bool isValid(); + + /** + Return pointer on vertices array + */ + Vertex* getVertices(); + + /** + Return pointer on edges array + */ + Edge* getEdges(); + + /** + Return pointer on facets array + */ + Facet* getFacetsBuffer(); + + /** + Return pointer on specified facet + */ + Facet* getFacet(int32_t facet); + + /** + Return edges count + */ + uint32_t getEdgesCount(); + + /** + Return vertices count + */ + uint32_t getVerticesCount(); + + /** + Return facet count + */ + uint32_t getFacetCount(); + + /** + Return reference on mesh bounding box. + */ + physx::PxBounds3& getBoundingBox(); + + /** + Recalculate bounding box + */ + void recalculateBoundingBox(); + + /** + Compute mesh volume. Can be used only for triangulated meshes. + Return mesh volume. If mesh is not triangulated return 0. + */ + float getMeshVolume(); + +private: + std::vector<Vertex> mVertices; + std::vector<Edge> mEdges; + std::vector<Facet> mFacets; + physx::PxBounds3 mBounds; +}; + + +/** + Helper functions +*/ + +/** + Set cutting box at some particular position. + \param[in] point Cutting face center + \param[in] normal Cutting face normal + \param[in] mesh Cutting box mesh + \param[in] size Cutting box size + \param[in] id Cutting box ID +*/ +void setCuttingBox(const physx::PxVec3& point, const physx::PxVec3& normal, Mesh* mesh, float size, int32_t id); +/** + Create cutting box at some particular position. + \param[in] point Cutting face center + \param[in] normal Cutting face normal + \param[in] size Cutting box size + \param[in] id Cutting box ID +*/ +Mesh* getCuttingBox(const physx::PxVec3& point, const physx::PxVec3& normal, float size, int32_t id); + +/** + Create box at some particular position. + \param[in] point Cutting face center + \param[in] size Cutting box size +*/ +Mesh* getBigBox(const physx::PxVec3& point, float size); + +/** + Create slicing box with noisy cutting surface. + \param[in] point Cutting face center + \param[in] normal Cutting face normal + \param[in] size Cutting box size + \param[in] jaggedPlaneSize Noisy surface size + \param[in] resolution Noisy surface resolution + \param[in] id Cutting box ID + \param[in] amplitude Noise amplitude + \param[in] frequency Noise frequency + \param[in] octaves Noise octaves + \param[in] seed Random generator seed, used for noise generation. +*/ +Mesh* getNoisyCuttingBoxPair(const physx::PxVec3& point, const physx::PxVec3& normal, float size, float jaggedPlaneSize, uint32_t resolution, int32_t id, float amplitude, float frequency, int32_t octaves, int32_t seed); + + +/** + Inverses normals of cutting box and sets indices. + \param[in] mesh Cutting box mesh + \param[in] id Cutting box ID +*/ +void inverseNormalAndSetIndices(Mesh* mesh, int32_t id); + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTAUTHORINGMESH_H diff --git a/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringTypes.h b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringTypes.h new file mode 100644 index 0000000..de28866 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/include/NvBlastExtAuthoringTypes.h @@ -0,0 +1,128 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTAUTHORINGTYPES_H +#define NVBLASTAUTHORINGTYPES_H + +#include <PxVec3.h> +#include <PxVec2.h> +#include <PxBounds3.h> +#include <algorithm> +#include "NvBlastTypes.h" + +#define NOT_VALID_VERTEX INT32_MAX + +namespace Nv +{ +namespace Blast +{ + +/** + Edge representation +*/ +struct Edge +{ + uint32_t s, e; + Edge() : s(NOT_VALID_VERTEX), e(NOT_VALID_VERTEX){} + Edge(int s, int e) : s(s), e(e) {} + bool operator<(const Edge& b) const + { + if (s == b.s) + return e < b.e; + else + return s < b.s; + } +}; + +/** + Mesh vertex representation +*/ +struct Vertex +{ + physx::PxVec3 p; // Position + physx::PxVec3 n; // Normal + physx::PxVec2 uv[1]; // UV-coordinates array, currently supported only one UV coordinate. +}; + +/** + Mesh triangle representation +*/ +struct Triangle +{ + Triangle() {}; + Triangle(Vertex a, Vertex b, Vertex c) : a(a), b(b), c(c) {}; + Vertex a, b, c; + int32_t userInfo; + physx::PxVec3 getNormal() + { + return ((b.p - a.p).cross(c.p - a.p)); + } +}; + + +/** + Index based triangle +*/ +struct TriangleIndexed +{ + TriangleIndexed() {}; + TriangleIndexed(uint32_t a, uint32_t b, uint32_t c) : ea(a), eb(b), ec(c) {}; + + uint32_t getOpposite(uint32_t a, uint32_t b) + { + if (ea != a && ea != b) + return ea; + if (eb != a && eb != b) + return eb; + if (ec != a && ec != b) + return ec; + return NOT_VALID_VERTEX; + } + + bool isContainEdge(uint32_t a, uint32_t b) + { + return (a == ea || a == eb || a == ec) && (b == ea || b == eb || b == ec); + } + + uint32_t ea, eb, ec; + int32_t userInfo; +}; + +/** + Mesh facet representation +*/ +struct Facet +{ + int32_t firstEdgeNumber; + uint32_t edgesCount; + int32_t userData; + Facet(int32_t fEdge = 0, uint32_t eCount = 0, int32_t userData = 0) : firstEdgeNumber(fEdge), edgesCount(eCount), userData(userData) {} +}; + +/** +Abstract base class for user-defined random value generator. +*/ +class RandomGeneratorBase +{ +public: + // Generates uniformly distributed value in [0, 1] range. + virtual float getRandomValue() = 0; + // Seeds random value generator + virtual void seed(int32_t seed) = 0; + virtual ~RandomGeneratorBase() {}; +}; + + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTAUTHORINGTYPES_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.cpp new file mode 100644 index 0000000..73c59b8 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.cpp @@ -0,0 +1,1004 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtApexSharedParts.h" + +#include "PxMat44.h" +#include "PxBounds3.h" +#include "PxFoundation.h" +#include "PxPhysics.h" +#include "PsVecMath.h" +#include <vector> + +using namespace physx; +using namespace physx::shdfnd::aos; + + +namespace Nv +{ +namespace Blast +{ + +PX_NOALIAS PX_FORCE_INLINE BoolV PointOutsideOfPlane4(const Vec3VArg _a, const Vec3VArg _b, const Vec3VArg _c, const Vec3VArg _d) +{ + // this is not 0 because of the following scenario: + // All the points lie on the same plane and the plane goes through the origin (0,0,0). + // On the Wii U, the math below has the problem that when point A gets projected on the + // plane cumputed by A, B, C, the distance to the plane might not be 0 for the mentioned + // scenario but a small positive or negative value. This can lead to the wrong boolean + // results. Using a small negative value as threshold is more conservative but safer. + const Vec4V zero = V4Load(-1e-6); + + const Vec3V ab = V3Sub(_b, _a); + const Vec3V ac = V3Sub(_c, _a); + const Vec3V ad = V3Sub(_d, _a); + const Vec3V bd = V3Sub(_d, _b); + const Vec3V bc = V3Sub(_c, _b); + + const Vec3V v0 = V3Cross(ab, ac); + const Vec3V v1 = V3Cross(ac, ad); + const Vec3V v2 = V3Cross(ad, ab); + const Vec3V v3 = V3Cross(bd, bc); + + const FloatV signa0 = V3Dot(v0, _a); + const FloatV signa1 = V3Dot(v1, _a); + const FloatV signa2 = V3Dot(v2, _a); + const FloatV signd3 = V3Dot(v3, _a); + + const FloatV signd0 = V3Dot(v0, _d); + const FloatV signd1 = V3Dot(v1, _b); + const FloatV signd2 = V3Dot(v2, _c); + const FloatV signa3 = V3Dot(v3, _b); + + const Vec4V signa = V4Merge(signa0, signa1, signa2, signa3); + const Vec4V signd = V4Merge(signd0, signd1, signd2, signd3); + return V4IsGrtrOrEq(V4Mul(signa, signd), zero);//same side, outside of the plane +} + +PX_NOALIAS PX_FORCE_INLINE Vec3V closestPtPointSegment(const Vec3VArg a, const Vec3VArg b) +{ + const FloatV zero = FZero(); + const FloatV one = FOne(); + + //Test degenerated case + const Vec3V ab = V3Sub(b, a); + const FloatV denom = V3Dot(ab, ab); + const Vec3V ap = V3Neg(a);//V3Sub(origin, a); + const FloatV nom = V3Dot(ap, ab); + const BoolV con = FIsEq(denom, zero); + const FloatV tValue = FClamp(FDiv(nom, denom), zero, one); + const FloatV t = FSel(con, zero, tValue); + + return V3Sel(con, a, V3ScaleAdd(ab, t, a)); +} + +PX_NOALIAS PX_FORCE_INLINE Vec3V closestPtPointSegment(const Vec3VArg Q0, const Vec3VArg Q1, const Vec3VArg A0, const Vec3VArg A1, + const Vec3VArg B0, const Vec3VArg B1, PxU32& size, Vec3V& closestA, Vec3V& closestB) +{ + const Vec3V a = Q0; + const Vec3V b = Q1; + + const BoolV bTrue = BTTTT(); + const FloatV zero = FZero(); + const FloatV one = FOne(); + + //Test degenerated case + const Vec3V ab = V3Sub(b, a); + const FloatV denom = V3Dot(ab, ab); + const Vec3V ap = V3Neg(a);//V3Sub(origin, a); + const FloatV nom = V3Dot(ap, ab); + const BoolV con = FIsEq(denom, zero); + + if (BAllEq(con, bTrue)) + { + size = 1; + closestA = A0; + closestB = B0; + return Q0; + } + + const Vec3V v = V3Sub(A1, A0); + const Vec3V w = V3Sub(B1, B0); + const FloatV tValue = FClamp(FDiv(nom, denom), zero, one); + const FloatV t = FSel(con, zero, tValue); + + const Vec3V tempClosestA = V3ScaleAdd(v, t, A0); + const Vec3V tempClosestB = V3ScaleAdd(w, t, B0); + closestA = tempClosestA; + closestB = tempClosestB; + return V3Sub(tempClosestA, tempClosestB); +} + +PX_NOALIAS Vec3V closestPtPointSegmentTesselation(const Vec3VArg Q0, const Vec3VArg Q1, const Vec3VArg A0, const Vec3VArg A1, + const Vec3VArg B0, const Vec3VArg B1, PxU32& size, Vec3V& closestA, Vec3V& closestB) +{ + const FloatV half = FHalf(); + + const FloatV targetSegmentLengthSq = FLoad(10000.f);//100 unit + + Vec3V q0 = Q0; + Vec3V q1 = Q1; + Vec3V a0 = A0; + Vec3V a1 = A1; + Vec3V b0 = B0; + Vec3V b1 = B1; + + for (;;) + { + const Vec3V midPoint = V3Scale(V3Add(q0, q1), half); + const Vec3V midA = V3Scale(V3Add(a0, a1), half); + const Vec3V midB = V3Scale(V3Add(b0, b1), half); + + const Vec3V v = V3Sub(midPoint, q0); + const FloatV sqV = V3Dot(v, v); + if (FAllGrtr(targetSegmentLengthSq, sqV)) + break; + //split the segment into half + const Vec3V tClos0 = closestPtPointSegment(q0, midPoint); + const FloatV sqDist0 = V3Dot(tClos0, tClos0); + + const Vec3V tClos1 = closestPtPointSegment(q1, midPoint); + const FloatV sqDist1 = V3Dot(tClos1, tClos1); + //const BoolV con = FIsGrtr(sqDist0, sqDist1); + if (FAllGrtr(sqDist0, sqDist1)) + { + //segment [m, q1] + q0 = midPoint; + a0 = midA; + b0 = midB; + } + else + { + //segment [q0, m] + q1 = midPoint; + a1 = midA; + b1 = midB; + } + + } + + return closestPtPointSegment(q0, q1, a0, a1, b0, b1, size, closestA, closestB); +} + +PX_NOALIAS Vec3V closestPtPointTriangleTesselation(const Vec3V* PX_RESTRICT Q, const Vec3V* PX_RESTRICT A, const Vec3V* PX_RESTRICT B, const PxU32* PX_RESTRICT indices, PxU32& size, Vec3V& closestA, Vec3V& closestB) +{ + size = 3; + const FloatV zero = FZero(); + const FloatV eps = FEps(); + const FloatV half = FHalf(); + const BoolV bTrue = BTTTT(); + const FloatV four = FLoad(4.f); + const FloatV sixty = FLoad(100.f); + + const PxU32 ind0 = indices[0]; + const PxU32 ind1 = indices[1]; + const PxU32 ind2 = indices[2]; + + const Vec3V a = Q[ind0]; + const Vec3V b = Q[ind1]; + const Vec3V c = Q[ind2]; + + Vec3V ab_ = V3Sub(b, a); + Vec3V ac_ = V3Sub(c, a); + Vec3V bc_ = V3Sub(b, c); + + const FloatV dac_ = V3Dot(ac_, ac_); + const FloatV dbc_ = V3Dot(bc_, bc_); + if (FAllGrtrOrEq(eps, FMin(dac_, dbc_))) + { + //degenerate + size = 2; + return closestPtPointSegment(Q[ind0], Q[ind1], A[ind0], A[ind1], B[ind0], B[ind1], size, closestA, closestB); + } + + Vec3V ap = V3Neg(a); + Vec3V bp = V3Neg(b); + Vec3V cp = V3Neg(c); + + FloatV d1 = V3Dot(ab_, ap); // snom + FloatV d2 = V3Dot(ac_, ap); // tnom + FloatV d3 = V3Dot(ab_, bp); // -sdenom + FloatV d4 = V3Dot(ac_, bp); // unom = d4 - d3 + FloatV d5 = V3Dot(ab_, cp); // udenom = d5 - d6 + FloatV d6 = V3Dot(ac_, cp); // -tdenom + /* FloatV unom = FSub(d4, d3); + FloatV udenom = FSub(d5, d6);*/ + + FloatV va = FNegScaleSub(d5, d4, FMul(d3, d6));//edge region of BC + FloatV vb = FNegScaleSub(d1, d6, FMul(d5, d2));//edge region of AC + FloatV vc = FNegScaleSub(d3, d2, FMul(d1, d4));//edge region of AB + + //check if p in vertex region outside a + const BoolV con00 = FIsGrtrOrEq(zero, d1); // snom <= 0 + const BoolV con01 = FIsGrtrOrEq(zero, d2); // tnom <= 0 + const BoolV con0 = BAnd(con00, con01); // vertex region a + if (BAllEq(con0, bTrue)) + { + //size = 1; + closestA = A[ind0]; + closestB = B[ind0]; + return Q[ind0]; + } + + //check if p in vertex region outside b + const BoolV con10 = FIsGrtrOrEq(d3, zero); + const BoolV con11 = FIsGrtrOrEq(d3, d4); + const BoolV con1 = BAnd(con10, con11); // vertex region b + if (BAllEq(con1, bTrue)) + { + /*size = 1; + indices[0] = ind1;*/ + closestA = A[ind1]; + closestB = B[ind1]; + return Q[ind1]; + } + + + //check if p in vertex region outside of c + const BoolV con20 = FIsGrtrOrEq(d6, zero); + const BoolV con21 = FIsGrtrOrEq(d6, d5); + const BoolV con2 = BAnd(con20, con21); // vertex region c + if (BAllEq(con2, bTrue)) + { + closestA = A[ind2]; + closestB = B[ind2]; + return Q[ind2]; + } + + //check if p in edge region of AB + const BoolV con30 = FIsGrtrOrEq(zero, vc); + const BoolV con31 = FIsGrtrOrEq(d1, zero); + const BoolV con32 = FIsGrtrOrEq(zero, d3); + const BoolV con3 = BAnd(con30, BAnd(con31, con32)); + + if (BAllEq(con3, bTrue)) + { + //size = 2; + //p in edge region of AB, split AB + return closestPtPointSegmentTesselation(Q[ind0], Q[ind1], A[ind0], A[ind1], B[ind0], B[ind1], size, closestA, closestB); + } + + //check if p in edge region of BC + const BoolV con40 = FIsGrtrOrEq(zero, va); + const BoolV con41 = FIsGrtrOrEq(d4, d3); + const BoolV con42 = FIsGrtrOrEq(d5, d6); + const BoolV con4 = BAnd(con40, BAnd(con41, con42)); + + if (BAllEq(con4, bTrue)) + { + //p in edge region of BC, split BC + return closestPtPointSegmentTesselation(Q[ind1], Q[ind2], A[ind1], A[ind2], B[ind1], B[ind2], size, closestA, closestB); + } + + //check if p in edge region of AC + const BoolV con50 = FIsGrtrOrEq(zero, vb); + const BoolV con51 = FIsGrtrOrEq(d2, zero); + const BoolV con52 = FIsGrtrOrEq(zero, d6); + const BoolV con5 = BAnd(con50, BAnd(con51, con52)); + + if (BAllEq(con5, bTrue)) + { + //p in edge region of AC, split AC + return closestPtPointSegmentTesselation(Q[ind0], Q[ind2], A[ind0], A[ind2], B[ind0], B[ind2], size, closestA, closestB); + } + + size = 3; + + Vec3V q0 = Q[ind0]; + Vec3V q1 = Q[ind1]; + Vec3V q2 = Q[ind2]; + Vec3V a0 = A[ind0]; + Vec3V a1 = A[ind1]; + Vec3V a2 = A[ind2]; + Vec3V b0 = B[ind0]; + Vec3V b1 = B[ind1]; + Vec3V b2 = B[ind2]; + + for (;;) + { + + const Vec3V ab = V3Sub(q1, q0); + const Vec3V ac = V3Sub(q2, q0); + const Vec3V bc = V3Sub(q2, q1); + + const FloatV dab = V3Dot(ab, ab); + const FloatV dac = V3Dot(ac, ac); + const FloatV dbc = V3Dot(bc, bc); + + const FloatV fMax = FMax(dab, FMax(dac, dbc)); + const FloatV fMin = FMin(dab, FMin(dac, dbc)); + + const Vec3V w = V3Cross(ab, ac); + + const FloatV area = V3Length(w); + const FloatV ratio = FDiv(FSqrt(fMax), FSqrt(fMin)); + if (FAllGrtr(four, ratio) && FAllGrtr(sixty, area)) + break; + + //calculate the triangle normal + const Vec3V triNormal = V3Normalize(w); + + PX_ASSERT(V3AllEq(triNormal, V3Zero()) == 0); + + + //split the longest edge + if (FAllGrtrOrEq(dab, dac) && FAllGrtrOrEq(dab, dbc)) + { + //split edge q0q1 + const Vec3V midPoint = V3Scale(V3Add(q0, q1), half); + const Vec3V midA = V3Scale(V3Add(a0, a1), half); + const Vec3V midB = V3Scale(V3Add(b0, b1), half); + + const Vec3V v = V3Sub(midPoint, q2); + const Vec3V n = V3Normalize(V3Cross(v, triNormal)); + + const FloatV d = FNeg(V3Dot(n, midPoint)); + const FloatV dp = FAdd(V3Dot(n, q0), d); + const FloatV sum = FMul(d, dp); + + if (FAllGrtr(sum, zero)) + { + //q0 and origin at the same side, split triangle[q0, m, q2] + q1 = midPoint; + a1 = midA; + b1 = midB; + } + else + { + //q1 and origin at the same side, split triangle[m, q1, q2] + q0 = midPoint; + a0 = midA; + b0 = midB; + } + + } + else if (FAllGrtrOrEq(dac, dbc)) + { + //split edge q0q2 + const Vec3V midPoint = V3Scale(V3Add(q0, q2), half); + const Vec3V midA = V3Scale(V3Add(a0, a2), half); + const Vec3V midB = V3Scale(V3Add(b0, b2), half); + + const Vec3V v = V3Sub(midPoint, q1); + const Vec3V n = V3Normalize(V3Cross(v, triNormal)); + + const FloatV d = FNeg(V3Dot(n, midPoint)); + const FloatV dp = FAdd(V3Dot(n, q0), d); + const FloatV sum = FMul(d, dp); + + if (FAllGrtr(sum, zero)) + { + //q0 and origin at the same side, split triangle[q0, q1, m] + q2 = midPoint; + a2 = midA; + b2 = midB; + } + else + { + //q2 and origin at the same side, split triangle[m, q1, q2] + q0 = midPoint; + a0 = midA; + b0 = midB; + } + } + else + { + //split edge q1q2 + const Vec3V midPoint = V3Scale(V3Add(q1, q2), half); + const Vec3V midA = V3Scale(V3Add(a1, a2), half); + const Vec3V midB = V3Scale(V3Add(b1, b2), half); + + const Vec3V v = V3Sub(midPoint, q0); + const Vec3V n = V3Normalize(V3Cross(v, triNormal)); + + const FloatV d = FNeg(V3Dot(n, midPoint)); + const FloatV dp = FAdd(V3Dot(n, q1), d); + const FloatV sum = FMul(d, dp); + + if (FAllGrtr(sum, zero)) + { + //q1 and origin at the same side, split triangle[q0, q1, m] + q2 = midPoint; + a2 = midA; + b2 = midB; + } + else + { + //q2 and origin at the same side, split triangle[q0, m, q2] + q1 = midPoint; + a1 = midA; + b1 = midB; + } + + + } + } + + //P must project inside face region. Compute Q using Barycentric coordinates + ab_ = V3Sub(q1, q0); + ac_ = V3Sub(q2, q0); + ap = V3Neg(q0); + bp = V3Neg(q1); + cp = V3Neg(q2); + + d1 = V3Dot(ab_, ap); // snom + d2 = V3Dot(ac_, ap); // tnom + d3 = V3Dot(ab_, bp); // -sdenom + d4 = V3Dot(ac_, bp); // unom = d4 - d3 + d5 = V3Dot(ab_, cp); // udenom = d5 - d6 + d6 = V3Dot(ac_, cp); // -tdenom + + va = FNegScaleSub(d5, d4, FMul(d3, d6));//edge region of BC + vb = FNegScaleSub(d1, d6, FMul(d5, d2));//edge region of AC + vc = FNegScaleSub(d3, d2, FMul(d1, d4));//edge region of AB + + const FloatV toRecipD = FAdd(va, FAdd(vb, vc)); + const FloatV denom = FRecip(toRecipD);//V4GetW(recipTmp); + const Vec3V v0 = V3Sub(a1, a0); + const Vec3V v1 = V3Sub(a2, a0); + const Vec3V w0 = V3Sub(b1, b0); + const Vec3V w1 = V3Sub(b2, b0); + + const FloatV t = FMul(vb, denom); + const FloatV w = FMul(vc, denom); + const Vec3V vA1 = V3Scale(v1, w); + const Vec3V vB1 = V3Scale(w1, w); + const Vec3V tempClosestA = V3Add(a0, V3ScaleAdd(v0, t, vA1)); + const Vec3V tempClosestB = V3Add(b0, V3ScaleAdd(w0, t, vB1)); + closestA = tempClosestA; + closestB = tempClosestB; + return V3Sub(tempClosestA, tempClosestB); +} + +PX_NOALIAS Vec3V closestPtPointTetrahedronTesselation(Vec3V* PX_RESTRICT Q, Vec3V* PX_RESTRICT A, Vec3V* PX_RESTRICT B, PxU32& size, Vec3V& closestA, Vec3V& closestB) +{ + const FloatV eps = FEps(); + const Vec3V zeroV = V3Zero(); + PxU32 tempSize = size; + + FloatV bestSqDist = FLoad(PX_MAX_REAL); + const Vec3V a = Q[0]; + const Vec3V b = Q[1]; + const Vec3V c = Q[2]; + const Vec3V d = Q[3]; + const BoolV bTrue = BTTTT(); + const BoolV bFalse = BFFFF(); + + //degenerated + const Vec3V ad = V3Sub(d, a); + const Vec3V bd = V3Sub(d, b); + const Vec3V cd = V3Sub(d, c); + const FloatV dad = V3Dot(ad, ad); + const FloatV dbd = V3Dot(bd, bd); + const FloatV dcd = V3Dot(cd, cd); + const FloatV fMin = FMin(dad, FMin(dbd, dcd)); + if (FAllGrtr(eps, fMin)) + { + size = 3; + PxU32 tempIndices[] = { 0, 1, 2 }; + return closestPtPointTriangleTesselation(Q, A, B, tempIndices, size, closestA, closestB); + } + + Vec3V _Q[] = { Q[0], Q[1], Q[2], Q[3] }; + Vec3V _A[] = { A[0], A[1], A[2], A[3] }; + Vec3V _B[] = { B[0], B[1], B[2], B[3] }; + + PxU32 indices[3] = { 0, 1, 2 }; + + const BoolV bIsOutside4 = PointOutsideOfPlane4(a, b, c, d); + + if (BAllEq(bIsOutside4, bFalse)) + { + //origin is inside the tetrahedron, we are done + return zeroV; + } + + Vec3V result = zeroV; + Vec3V tempClosestA, tempClosestB; + + if (BAllEq(BGetX(bIsOutside4), bTrue)) + { + + PxU32 tempIndices[] = { 0, 1, 2 }; + PxU32 _size = 3; + + result = closestPtPointTriangleTesselation(_Q, _A, _B, tempIndices, _size, tempClosestA, tempClosestB); + + const FloatV sqDist = V3Dot(result, result); + bestSqDist = sqDist; + + indices[0] = tempIndices[0]; + indices[1] = tempIndices[1]; + indices[2] = tempIndices[2]; + + tempSize = _size; + closestA = tempClosestA; + closestB = tempClosestB; + } + + if (BAllEq(BGetY(bIsOutside4), bTrue)) + { + + PxU32 tempIndices[] = { 0, 2, 3 }; + + PxU32 _size = 3; + + const Vec3V q = closestPtPointTriangleTesselation(_Q, _A, _B, tempIndices, _size, tempClosestA, tempClosestB); + + const FloatV sqDist = V3Dot(q, q); + const BoolV con = FIsGrtr(bestSqDist, sqDist); + if (BAllEq(con, bTrue)) + { + result = q; + bestSqDist = sqDist; + indices[0] = tempIndices[0]; + indices[1] = tempIndices[1]; + indices[2] = tempIndices[2]; + + tempSize = _size; + closestA = tempClosestA; + closestB = tempClosestB; + } + } + + if (BAllEq(BGetZ(bIsOutside4), bTrue)) + { + + PxU32 tempIndices[] = { 0, 3, 1 }; + PxU32 _size = 3; + + const Vec3V q = closestPtPointTriangleTesselation(_Q, _A, _B, tempIndices, _size, tempClosestA, tempClosestB); + + const FloatV sqDist = V3Dot(q, q); + const BoolV con = FIsGrtr(bestSqDist, sqDist); + if (BAllEq(con, bTrue)) + { + result = q; + bestSqDist = sqDist; + indices[0] = tempIndices[0]; + indices[1] = tempIndices[1]; + indices[2] = tempIndices[2]; + tempSize = _size; + closestA = tempClosestA; + closestB = tempClosestB; + } + + } + + if (BAllEq(BGetW(bIsOutside4), bTrue)) + { + + PxU32 tempIndices[] = { 1, 3, 2 }; + PxU32 _size = 3; + + const Vec3V q = closestPtPointTriangleTesselation(_Q, _A, _B, tempIndices, _size, tempClosestA, tempClosestB); + + const FloatV sqDist = V3Dot(q, q); + const BoolV con = FIsGrtr(bestSqDist, sqDist); + + if (BAllEq(con, bTrue)) + { + result = q; + bestSqDist = sqDist; + + indices[0] = tempIndices[0]; + indices[1] = tempIndices[1]; + indices[2] = tempIndices[2]; + + tempSize = _size; + closestA = tempClosestA; + closestB = tempClosestB; + } + } + + A[0] = _A[indices[0]]; A[1] = _A[indices[1]]; A[2] = _A[indices[2]]; + B[0] = _B[indices[0]]; B[1] = _B[indices[1]]; B[2] = _B[indices[2]]; + Q[0] = _Q[indices[0]]; Q[1] = _Q[indices[1]]; Q[2] = _Q[indices[2]]; + + + size = tempSize; + return result; +} + +PX_NOALIAS PX_FORCE_INLINE Vec3V doTesselation(Vec3V* PX_RESTRICT Q, Vec3V* PX_RESTRICT A, Vec3V* PX_RESTRICT B, + const Vec3VArg support, const Vec3VArg supportA, const Vec3VArg supportB, PxU32& size, Vec3V& closestA, Vec3V& closestB) +{ + switch (size) + { + case 1: + { + closestA = supportA; + closestB = supportB; + return support; + } + case 2: + { + return closestPtPointSegmentTesselation(Q[0], support, A[0], supportA, B[0], supportB, size, closestA, closestB); + } + case 3: + { + + PxU32 tempIndices[3] = { 0, 1, 2 }; + return closestPtPointTriangleTesselation(Q, A, B, tempIndices, size, closestA, closestB); + } + case 4: + { + return closestPtPointTetrahedronTesselation(Q, A, B, size, closestA, closestB); + } + default: + PX_ASSERT(0); + } + return support; +} + + + + +enum Status +{ + STATUS_NON_INTERSECT, + STATUS_CONTACT, + STATUS_DEGENERATE, +}; + +struct Output +{ + /// Get the normal to push apart in direction from A to B + PX_FORCE_INLINE Vec3V getNormal() const { return V3Normalize(V3Sub(mClosestB, mClosestA)); } + Vec3V mClosestA; ///< Closest point on A + Vec3V mClosestB; ///< Closest point on B + FloatV mDistSq; +}; + +struct ConvexV +{ + void calcExtent(const Vec3V& dir, PxF32& minOut, PxF32& maxOut) const + { + // Expand + const Vec4V x = Vec4V_From_FloatV(V3GetX(dir)); + const Vec4V y = Vec4V_From_FloatV(V3GetY(dir)); + const Vec4V z = Vec4V_From_FloatV(V3GetZ(dir)); + + const Vec4V* src = mAovVertices; + const Vec4V* end = src + mNumAovVertices * 3; + + // Do first step + Vec4V max = V4MulAdd(x, src[0], V4MulAdd(y, src[1], V4Mul(z, src[2]))); + Vec4V min = max; + src += 3; + // Do the rest + for (; src < end; src += 3) + { + const Vec4V dot = V4MulAdd(x, src[0], V4MulAdd(y, src[1], V4Mul(z, src[2]))); + max = V4Max(dot, max); + min = V4Min(dot, min); + } + FStore(V4ExtractMax(max), &maxOut); + FStore(V4ExtractMin(min), &minOut); + } + Vec3V calcSupport(const Vec3V& dir) const + { + // Expand + const Vec4V x = Vec4V_From_FloatV(V3GetX(dir)); + const Vec4V y = Vec4V_From_FloatV(V3GetY(dir)); + const Vec4V z = Vec4V_From_FloatV(V3GetZ(dir)); + + PX_ALIGN(16, static const PxF32 index4const[]) = { 0.0f, 1.0f, 2.0f, 3.0f }; + Vec4V index4 = *(const Vec4V*)index4const; + PX_ALIGN(16, static const PxF32 delta4const[]) = { 4.0f, 4.0f, 4.0f, 4.0f }; + const Vec4V delta4 = *(const Vec4V*)delta4const; + + const Vec4V* src = mAovVertices; + const Vec4V* end = src + mNumAovVertices * 3; + + // Do first step + Vec4V max = V4MulAdd(x, src[0], V4MulAdd(y, src[1], V4Mul(z, src[2]))); + Vec4V maxIndex = index4; + index4 = V4Add(index4, delta4); + src += 3; + // Do the rest + for (; src < end; src += 3) + { + const Vec4V dot = V4MulAdd(x, src[0], V4MulAdd(y, src[1], V4Mul(z, src[2]))); + const BoolV cmp = V4IsGrtr(dot, max); + max = V4Max(dot, max); + maxIndex = V4Sel(cmp, index4, maxIndex); + index4 = V4Add(index4, delta4); + } + Vec4V horiMax = Vec4V_From_FloatV(V4ExtractMax(max)); + PxU32 mask = BGetBitMask(V4IsEq(horiMax, max)); + const PxU32 simdIndex = (0x12131210 >> (mask + mask)) & PxU32(3); + + /// NOTE! Could be load hit store + /// Would be better to have all simd. + PX_ALIGN(16, PxF32 f[4]); + V4StoreA(maxIndex, f); + PxU32 index = PxU32(PxI32(f[simdIndex])); + + const Vec4V* aovIndex = (mAovVertices + (index >> 2) * 3); + const PxF32* aovOffset = ((const PxF32*)aovIndex) + (index & 3); + + return Vec3V_From_Vec4V(V4LoadXYZW(aovOffset[0], aovOffset[4], aovOffset[8], 1.0f)); + } + + const Vec4V* mAovVertices; ///< Vertices storex x,x,x,x, y,y,y,y, z,z,z,z + PxU32 mNumAovVertices; ///< Number of groups of 4 of vertices +}; + +Status Collide(const Vec3V& initialDir, const ConvexV& convexA, const Mat34V& bToA, const ConvexV& convexB, Output& out) +{ + Vec3V Q[4]; + Vec3V A[4]; + Vec3V B[4]; + + Mat33V aToB = M34Trnsps33(bToA); + + PxU32 size = 0; + + const Vec3V zeroV = V3Zero(); + const BoolV bTrue = BTTTT(); + + //Vec3V v = V3UnitX(); + Vec3V v = V3Sel(FIsGrtr(V3Dot(initialDir, initialDir), FZero()), initialDir, V3UnitX()); + + //const FloatV minMargin = zero; + //const FloatV eps2 = FMul(minMargin, FLoad(0.01f)); + //FloatV eps2 = zero; + FloatV eps2 = FLoad(1e-6f); + const FloatV epsRel = FLoad(0.000225f); + + Vec3V closA(zeroV), closB(zeroV); + FloatV sDist = FMax(); + FloatV minDist = sDist; + Vec3V closAA = zeroV; + Vec3V closBB = zeroV; + + BoolV bNotTerminated = bTrue; + BoolV bCon = bTrue; + + do + { + minDist = sDist; + closAA = closA; + closBB = closB; + + PxU32 index = size++; + PX_ASSERT(index < 4); + + const Vec3V supportA = convexA.calcSupport(V3Neg(v)); + const Vec3V supportB = M34MulV3(bToA, convexB.calcSupport(M33MulV3(aToB, v))); + const Vec3V support = Vec3V_From_Vec4V(Vec4V_From_Vec3V(V3Sub(supportA, supportB))); + + A[index] = supportA; + B[index] = supportB; + Q[index] = support; + + const FloatV signDist = V3Dot(v, support); + const FloatV tmp0 = FSub(sDist, signDist); + if (FAllGrtr(FMul(epsRel, sDist), tmp0)) + { + out.mClosestA = closA; + out.mClosestB = closB; + out.mDistSq = sDist; + return STATUS_NON_INTERSECT; + } + + //calculate the closest point between two convex hull + v = doTesselation(Q, A, B, support, supportA, supportB, size, closA, closB); + sDist = V3Dot(v, v); + bCon = FIsGrtr(minDist, sDist); + + bNotTerminated = BAnd(FIsGrtr(sDist, eps2), bCon); + } while (BAllEq(bNotTerminated, bTrue)); + + out.mClosestA = V3Sel(bCon, closA, closAA); + out.mClosestB = V3Sel(bCon, closB, closBB); + out.mDistSq = FSel(bCon, sDist, minDist); + return Status(BAllEq(bCon, bTrue) == 1 ? STATUS_CONTACT : STATUS_DEGENERATE); +} + +static void _calcSeparation(const ConvexV& convexA, const physx::PxTransform& aToWorldIn, const Mat34V& bToA, ConvexV& convexB, Output& out, Separation& sep) +{ + + Mat33V aToB = M34Trnsps33(bToA); + Vec3V normalA = out.getNormal(); + + convexA.calcExtent(normalA, sep.min0, sep.max0); + Vec3V normalB = M33MulV3(aToB, normalA); + convexB.calcExtent(normalB, sep.min1, sep.max1); + + { + // Offset the min max taking into account transform + // Distance of origin from B's space in As space in direction of the normal in As space should fix it... + PxF32 fix; + FStore(V3Dot(bToA.col3, normalA), &fix); + sep.min1 += fix; + sep.max1 += fix; + } + + // Looks like it's the plane at the midpoint + Vec3V center = V3Scale(V3Add(out.mClosestA, out.mClosestB), FLoad(0.5f)); + // Transform to world space + Mat34V aToWorld; + *(PxMat44*)&aToWorld = aToWorldIn; + // Put the normal in world space + Vec3V worldCenter = M34MulV3(aToWorld, center); + Vec3V worldNormal = M34Mul33V3(aToWorld, normalA); + + FloatV dist = V3Dot(worldNormal, worldCenter); + V3StoreU(worldNormal, sep.plane.n); + FStore(dist, &sep.plane.d); + sep.plane.d = -sep.plane.d; +} + +static void _arrayVec3ToVec4(const PxVec3* src, Vec4V* dst, PxU32 num) +{ + const PxU32 num4 = num >> 2; + for (PxU32 i = 0; i < num4; i++, dst += 3, src += 4) + { + Vec3V v0 = V3LoadU(&src[0].x); + Vec3V v1 = V3LoadU(&src[1].x); + Vec3V v2 = V3LoadU(&src[2].x); + Vec3V v3 = V3LoadU(&src[3].x); + // Transpose + V4Transpose(v0, v1, v2, v3); + // Save + dst[0] = v0; + dst[1] = v1; + dst[2] = v2; + } + const PxU32 remain = num & 3; + if (remain) + { + Vec3V work[4]; + PxU32 i = 0; + for (; i < remain; i++) work[i] = V3LoadU(&src[i].x); + for (; i < 4; i++) work[i] = work[remain - 1]; + V4Transpose(work[0], work[1], work[2], work[3]); + dst[0] = work[0]; + dst[1] = work[1]; + dst[2] = work[2]; + } +} + + +static void _arrayVec3ToVec4(const PxVec3* src, const Vec3V& scale, Vec4V* dst, PxU32 num) +{ + // If no scale - use the faster version + if (V3AllEq(scale, V3One())) + { + return _arrayVec3ToVec4(src, dst, num); + } + + const PxU32 num4 = num >> 2; + for (PxU32 i = 0; i < num4; i++, dst += 3, src += 4) + { + Vec3V v0 = V3Mul(scale, V3LoadU(&src[0].x)); + Vec3V v1 = V3Mul(scale, V3LoadU(&src[1].x)); + Vec3V v2 = V3Mul(scale, V3LoadU(&src[2].x)); + Vec3V v3 = V3Mul(scale, V3LoadU(&src[3].x)); + // Transpose + V4Transpose(v0, v1, v2, v3); + // Save + dst[0] = v0; + dst[1] = v1; + dst[2] = v2; + } + const PxU32 remain = num & 3; + if (remain) + { + Vec3V work[4]; + PxU32 i = 0; + for (; i < remain; i++) work[i] = V3Mul(scale, V3LoadU(&src[i].x)); + for (; i < 4; i++) work[i] = work[remain - 1]; + V4Transpose(work[0], work[1], work[2], work[3]); + dst[0] = work[0]; + dst[1] = work[1]; + dst[2] = work[2]; + } +} + + +bool importerHullsInProximityApexFree(const std::vector<PxVec3>& hull0, PxBounds3& hull0Bounds, const physx::PxTransform& localToWorldRT0In, const physx::PxVec3& scale0In, + const std::vector<PxVec3>& hull1, PxBounds3& hull1Bounds, const physx::PxTransform& localToWorldRT1In, const physx::PxVec3& scale1In, + physx::PxF32 maxDistance, Separation* separation) +{ + + + const PxU32 numVerts0 = static_cast<PxU32>(hull0.size()); + const PxU32 numVerts1 = static_cast<PxU32>(hull1.size()); + const PxU32 numAov0 = (numVerts0 + 3) >> 2; + const PxU32 numAov1 = (numVerts1 + 3) >> 2; + Vec4V* verts0 = (Vec4V*)alloca((numAov0 + numAov1) * sizeof(Vec4V) * 3); + + // Make sure it's aligned + PX_ASSERT((size_t(verts0) & 0xf) == 0); + + Vec4V* verts1 = verts0 + (numAov0 * 3); + + const Vec3V scale0 = V3LoadU(&scale0In.x); + const Vec3V scale1 = V3LoadU(&scale1In.x); + std::vector<PxVec3> vert0(numVerts0); + for (uint32_t i = 0; i < numVerts0; ++i) + { + vert0[i] = hull0[i]; + } + std::vector<PxVec3> vert1(numVerts1); + for (uint32_t i = 0; i < numVerts1; ++i) + { + vert1[i] = hull1[i]; + } + + _arrayVec3ToVec4(&vert0[0], scale0, verts0, numVerts0); + _arrayVec3ToVec4(&vert1[0], scale1, verts1, numVerts1); + + const PxTransform trans1To0 = localToWorldRT0In.transformInv(localToWorldRT1In); + + // Load into simd mat + Mat34V bToA; + *(PxMat44*)&bToA = trans1To0; + (*(PxMat44*)&bToA).column3.w = 0.0f; // AOS wants the 4th component of Vec3V to be 0 to work properly + + ConvexV convexA; + ConvexV convexB; + + convexA.mNumAovVertices = numAov0; + convexA.mAovVertices = verts0; + + convexB.mNumAovVertices = numAov1; + convexB.mAovVertices = verts1; + + // Take the origin of B in As space as the inital direction as it is 'the difference in transform origins B-A in A's space' + // Should be a good first guess + const Vec3V initialDir = bToA.col3; + Output output; + Status status = Collide(initialDir, convexA, bToA, convexB, output); + + if (status == STATUS_DEGENERATE) + { + // Calculate the tolerance from the extents + const PxVec3 extents0 = hull0Bounds.getExtents(); + const PxVec3 extents1 = hull1Bounds.getExtents(); + + const FloatV tolerance0 = V3ExtractMin(V3Mul(V3LoadU(&extents0.x), scale0)); + const FloatV tolerance1 = V3ExtractMin(V3Mul(V3LoadU(&extents1.x), scale1)); + + const FloatV tolerance = FMul(FAdd(tolerance0, tolerance1), FLoad(0.01f)); + const FloatV sqTolerance = FMul(tolerance, tolerance); + + status = FAllGrtr(sqTolerance, output.mDistSq) ? STATUS_CONTACT : STATUS_NON_INTERSECT; + } + + switch (status) + { + case STATUS_CONTACT: + { + if (separation) + { + _calcSeparation(convexA, localToWorldRT0In, bToA, convexB, output, *separation); + } + return true; + } + default: + case STATUS_NON_INTERSECT: + { + if (separation) + { + _calcSeparation(convexA, localToWorldRT0In, bToA, convexB, output, *separation); + } + PxF32 val; + FStore(output.mDistSq, &val); + return val < (maxDistance * maxDistance); + } + } +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.h new file mode 100644 index 0000000..68e0412 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtApexSharedParts.h @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAPEXSHAREDPARTS_H +#define NVBLASTEXTAPEXSHAREDPARTS_H + +#include "NvBlast.h" +#include <vector> +#include <PxPlane.h> +namespace physx +{ + class PxVec3; + class PxTransform; + class PxBounds3; +} + +namespace Nv +{ +namespace Blast +{ + +struct Separation +{ + physx::PxPlane plane; + float min0, max0, min1, max1; + + float getDistance() + { + return physx::PxMax(min0 - max1, min1 - max0); + } +}; + +/** + Function to compute midplane between two convex hulls. Is copied from APEX. +*/ +bool importerHullsInProximityApexFree(const std::vector<physx::PxVec3>& hull0, physx::PxBounds3& hull0Bounds, const physx::PxTransform& localToWorldRT0In, const physx::PxVec3& scale0In, + const std::vector<physx::PxVec3>& hull1, physx::PxBounds3& hull1Bounds, const physx::PxTransform& localToWorldRT1In, const physx::PxVec3& scale1In, + physx::PxF32 maxDistance, Separation* separation); + +} // namespace Blast +} // namespace Nv + + +#endif // NVBLASTEXTAPEXSHAREDPARTS_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.cpp new file mode 100644 index 0000000..075bce9 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.cpp @@ -0,0 +1,629 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtAuthoringAccelerator.h" +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastExtAuthoringInternalCommon.h" + + +using namespace physx; + + +namespace Nv +{ +namespace Blast +{ + +DummyAccelerator::DummyAccelerator(int32_t count) :count(count) +{ + current = 0; +} +void DummyAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc) +{ + current = 0; + (void)pos; + (void)ed; + (void)fc; +} +void DummyAccelerator::setState(const physx::PxVec3& point) { + current = 0; + (void)point; +} +int32_t DummyAccelerator::getNextFacet() +{ + if (current < count) + { + ++current; + return current - 1; + } + else + return -1; +} + + + +BBoxBasedAccelerator::BBoxBasedAccelerator(Mesh* mesh, int32_t resolution) : mResolution(resolution), alreadyGotValue(1) +{ + mBounds = mesh->getBoundingBox(); + mSpatialMap.resize(resolution * resolution * resolution); + mCells.resize(resolution * resolution * resolution); + int32_t currentCell = 0; + PxVec3 incr = (mBounds.maximum - mBounds.minimum) * (1.0f / mResolution); + for (int32_t z = 0; z < resolution; ++z) + { + for (int32_t y = 0; y < resolution; ++y) + { + for (int32_t x = 0; x < resolution; ++x) + { + mCells[currentCell].minimum.x = mBounds.minimum.x + x * incr.x; + mCells[currentCell].minimum.y = mBounds.minimum.y + y * incr.y; + mCells[currentCell].minimum.z = mBounds.minimum.z + z * incr.z; + + mCells[currentCell].maximum.x = mBounds.minimum.x + (x + 1) * incr.x; + mCells[currentCell].maximum.y = mBounds.minimum.y + (y + 1) * incr.y; + mCells[currentCell].maximum.z = mBounds.minimum.z + (z + 1) * incr.z; + + ++currentCell; + } + } + } + + buildAccelStructure(mesh->getVertices(), mesh->getEdges(), mesh->getFacetsBuffer(), mesh->getFacetCount()); +} + + +BBoxBasedAccelerator::~BBoxBasedAccelerator() +{ + mResolution = 0; + mBounds.setEmpty(); + mSpatialMap.clear(); + mCells.clear(); +} + +int32_t BBoxBasedAccelerator::getNextFacet() +{ + int32_t facetId = -1; + + while (mIteratorCell != -1) + { + if (mIteratorFacet >= (int32_t)mSpatialMap[mIteratorCell].size()) + { + if (!cellList.empty()) + { + mIteratorCell = cellList.back(); + cellList.pop_back(); + mIteratorFacet = 0; + } + else + { + mIteratorCell = -1; + break; + } + } + if (alreadyGotFlag[mSpatialMap[mIteratorCell][mIteratorFacet]] != alreadyGotValue) + { + facetId = mSpatialMap[mIteratorCell][mIteratorFacet]; + mIteratorFacet++; + break; + } + else + { + mIteratorFacet++; + } + } + if (facetId != -1) + { + alreadyGotFlag[facetId] = alreadyGotValue; + } + return facetId; +} +void BBoxBasedAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc) +{ + alreadyGotValue++; + mIteratorCell = -1; + mIteratorFacet = -1; + cellList.clear(); + facetBox.setEmpty(); + Edge* edge = ed + fc.firstEdgeNumber; + uint32_t count = fc.edgesCount; + for (uint32_t ec = 0; ec < count; ++ec) + { + facetBox.include(pos[edge->s].p); + facetBox.include(pos[edge->e].p); + edge++; + } + for (uint32_t i = 0; i < mCells.size(); ++i) + { + if (testCellPolygonIntersection(i, facetBox)) + { + if (!mSpatialMap[i].empty()) + cellList.push_back(i); + } + } + if (!cellList.empty()) + { + mIteratorFacet = 0; + mIteratorCell = cellList.back(); + cellList.pop_back(); + } +} + + +void BBoxBasedAccelerator::setState(const PxVec3& p) +{ + alreadyGotValue++; + mIteratorCell = -1; + mIteratorFacet = -1; + cellList.clear(); + int32_t perSlice = mResolution * mResolution; + for (uint32_t i = 0; i < mCells.size(); ++i) + { + if (mCells[i].contains(p)) + { + int32_t xyCellId = i % perSlice; + for (int32_t zCell = 0; zCell < mResolution; ++zCell) + { + int32_t cell = zCell * perSlice + xyCellId; + if (!mSpatialMap[cell].empty()) + cellList.push_back(cell); + } + } + } + if (!cellList.empty()) + { + mIteratorFacet = 0; + mIteratorCell = cellList.back(); + cellList.pop_back(); + } +} + + +bool BBoxBasedAccelerator::testCellPolygonIntersection(int32_t cellId, PxBounds3& facetBB) +{ + if (weakBoundingBoxIntersection(mCells[cellId], facetBB)) + { + return true; + } + else + return false; +} + +void BBoxBasedAccelerator::buildAccelStructure(Vertex* pos, Edge* edges, Facet* fc, int32_t facetCount) +{ + for (int32_t facet = 0; facet < facetCount; ++facet) + { + PxBounds3 bBox; + bBox.setEmpty(); + Edge* edge = &edges[0] + fc->firstEdgeNumber; + int32_t count = fc->edgesCount; + for (int32_t ec = 0; ec < count; ++ec) + { + bBox.include(pos[edge->s].p); + bBox.include(pos[edge->e].p); + edge++; + } + + for (uint32_t i = 0; i < mCells.size(); ++i) + { + if (testCellPolygonIntersection(i, bBox)) + { + mSpatialMap[i].push_back(facet); + } + } + fc++; + } + alreadyGotFlag.resize(facetCount, 0); + cellList.resize(mCells.size()); +} + +int32_t testEdgeAgainstCube(PxVec3& p1, PxVec3& p2) +{ + PxVec3 vec = p2 - p1; + PxVec3 vecSigns; + for (int32_t i = 0; i < 3; ++i) + { + vecSigns[i] = (vec[i] < 0) ? -1 : 1; + } + for (int32_t i = 0; i < 3; ++i) + { + if (p1[i] * vecSigns[i] > 0.5f) return 0; + if (p2[i] * vecSigns[i] < -0.5f) return 0; + } + + for (int32_t i = 0; i < 3; ++i) + { + int32_t ip1 = (i + 1) % 3; + int32_t ip2 = (i + 2) % 3; + + float vl1 = vec[ip2] * p1[ip1] - vec[ip1] * p1[ip2]; + float vl2 = 0.5f * (vec[ip2] * vecSigns[ip1] + vec[ip1] * vecSigns[ip2]); + if (vl1 * vl1 > vl2 * vl2) + { + return 0; + } + } + return 1; +} + +NV_INLINE int32_t isInSegm(float a, float b, float c) +{ + return (b >= c) - (a >= c); +} + +NV_INLINE int32_t edgeIsAbovePoint(PxVec2& p1, PxVec2& p2, PxVec2& p) +{ + int32_t direction = isInSegm(p1.x, p2.x, p.x); + if (direction != 0) + { + if (isInSegm(p1.y, p2.y, p.y)) + { + if (direction * (p.x - p1.x) * (p2.y - p1.y) >= direction * (p.y - p1.y) * (p2.x - p1.x)) + { + return direction; + } + } + else + { + if (p1.y > p.y) + return direction; + } + } + return 0; +} + +int32_t pointInPolygon(PxVec3* vertices, PxVec3& diagPoint, int32_t edgeCount, PxVec3& normal) +{ + std::vector<PxVec2> projectedVertices(edgeCount * 2); + ProjectionDirections pDir = getProjectionDirection(normal); + PxVec2 projectedDiagPoint = getProjectedPoint(diagPoint, pDir); + PxVec2* saveVert = projectedVertices.data(); + PxVec3* p = vertices; + for (int32_t i = 0; i < edgeCount * 2; ++i) + { + *saveVert = getProjectedPoint(*p, pDir); + ++saveVert; + ++p; + } + int32_t counter = 0; + PxVec2* v = projectedVertices.data(); + for (int32_t i = 0; i < edgeCount; ++i) + { + PxVec2& p1 = *v; + PxVec2& p2 = *(v + 1); + counter += edgeIsAbovePoint(p1, p2, projectedDiagPoint); + v += 2; + } + return counter != 0; +} + + + +int32_t testFacetUnitCubeIntersectionInternal(PxVec3* vertices,PxVec3& facetNormal, int32_t edgeCount) +{ + PxVec3* pnt_p = vertices; + for (int32_t i = 0; i < edgeCount; ++i) + { + if (testEdgeAgainstCube(*pnt_p, *(pnt_p + 1)) == 1) + { + return 1; + } + pnt_p += 2; + } + + PxVec3 cubeDiag(0, 0, 0); + for (int32_t i = 0; i < 3; ++i) + cubeDiag[i] = (facetNormal[i] < 0) ? -1 : 1; + float t = vertices->dot(facetNormal) / (cubeDiag.dot(facetNormal)); + if (t > 0.5 || t < -0.5) + return 0; + + PxVec3 intersPoint = cubeDiag * t; + int trs = pointInPolygon(vertices, intersPoint, edgeCount, facetNormal); + return trs; +} + +enum TrivialFlags +{ + HAS_POINT_BELOW_HIGH_X = ~(1 << 0), + HAS_POINT_ABOVE_LOW_X = ~(1 << 1), + + HAS_POINT_BELOW_HIGH_Y = ~(1 << 2), + HAS_POINT_ABOVE_LOW_Y = ~(1 << 3), + + HAS_POINT_BELOW_HIGH_Z = ~(1 << 4), + HAS_POINT_ABOVE_LOW_Z = ~(1 << 5), + + + + ALL_ONE = (1 << 6) - 1 +}; + + + + + +int32_t testFacetUnitCubeIntersection(Vertex* vertices, Edge* edges, Facet& fc, PxBounds3 cube, float fattening) +{ + Edge* ed = edges + fc.firstEdgeNumber; + int32_t trivialFlags = ALL_ONE; + cube.fattenFast(fattening); + for (uint32_t i = 0; i < fc.edgesCount; ++i) + { + { + PxVec3& p = vertices[ed->s].p; + if (cube.contains(p)) + return 1; + if (p.x < cube.getCenter().x + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_X; + if (p.x > cube.getCenter().x - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_X; + + if (p.y < cube.getCenter().y + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_Y; + if (p.y > cube.getCenter().y - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_Y; + + if (p.z < cube.getCenter().z + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_Z; + if (p.z > cube.getCenter().z - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_Z; + } + { + PxVec3& p = vertices[ed->e].p; + if (cube.contains(p)) + return 1; + if (p.x < cube.getCenter().x + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_X; + if (p.x > cube.getCenter().x - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_X; + + if (p.y < cube.getCenter().y + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_Y; + if (p.y > cube.getCenter().y - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_Y; + + if (p.z < cube.getCenter().z + 0.5) + trivialFlags &= HAS_POINT_BELOW_HIGH_Z; + if (p.z > cube.getCenter().z - 0.5) + trivialFlags &= HAS_POINT_ABOVE_LOW_Z; + } + + ++ed; + } + if (trivialFlags != 0) + { + return 0; + } + std::vector<PxVec3> verticesRescaled(fc.edgesCount * 2); + + int32_t vrt = 0; + ed = edges + fc.firstEdgeNumber; + PxVec3 offset = cube.getCenter(); + PxVec3 normal(1, 1, 1); + + /** + Compute normal + */ + PxVec3& v1 = vertices[ed->s].p; + PxVec3* v2 = nullptr; + PxVec3* v3 = nullptr; + + for (uint32_t i = 0; i < fc.edgesCount; ++i) + { + if (v1 != vertices[ed->s].p) + { + v2 = &vertices[ed->s].p; + break; + } + if (v1 != vertices[ed->e].p) + { + v2 = &vertices[ed->e].p; + break; + } + ed++; + } + ed = edges + fc.firstEdgeNumber; + for (uint32_t i = 0; i < fc.edgesCount; ++i) + { + if (v1 != vertices[ed->s].p && *v2 != vertices[ed->s].p) + { + v3 = &vertices[ed->s].p; + break; + } + if (v1 != vertices[ed->e].p && *v2 != vertices[ed->e].p) + { + v3 = &vertices[ed->e].p; + break; + } + ed++; + } + ed = edges + fc.firstEdgeNumber; + if (v2 != nullptr && v3 != nullptr) + { + normal = (*v2 - v1).cross(*v3 - v1); + } + else + { + return true; // If cant find normal, assume it intersects box. + } + + + normal.normalize(); + + PxVec3 rescale(.5f / (cube.getExtents().x), .5f / (cube.getExtents().y), 0.5f / (cube.getExtents().z)); + for (uint32_t i = 0; i < fc.edgesCount; ++i) + { + verticesRescaled[vrt] = vertices[ed->s].p - offset; + verticesRescaled[vrt].x *= rescale.x; + verticesRescaled[vrt].y *= rescale.y; + verticesRescaled[vrt].z *= rescale.z; + ++vrt; + verticesRescaled[vrt] = vertices[ed->e].p - offset; + verticesRescaled[vrt].x *= rescale.x; + verticesRescaled[vrt].y *= rescale.y; + verticesRescaled[vrt].z *= rescale.z; + ++ed; + ++vrt; + } + return testFacetUnitCubeIntersectionInternal(verticesRescaled.data(), normal, fc.edgesCount); +} + + +IntersectionTestingAccelerator::IntersectionTestingAccelerator(Mesh* in, int32_t resolution) +{ + + + alreadyGotFlag.resize(in->getFacetCount(), 0); + alreadyGotValue = 0; + mResolution = resolution; + + float cubeSize = 1.0f / resolution; + PxVec3 cubeMinimal(-0.5, -0.5, -0.5); + PxVec3 extents(cubeSize, cubeSize, cubeSize); + mCubes.resize(mResolution * mResolution * mResolution); + mSpatialMap.resize(mCubes.size()); + int32_t cubeId = 0; + + // Build unit cube partition + for (int32_t i = 0; i < mResolution; ++i) + { + cubeMinimal.y = -0.5; + cubeMinimal.z = -0.5; + for (int32_t j = 0; j < mResolution; ++j) + { + cubeMinimal.z = -0.5; + for (int32_t k = 0; k < mResolution; ++k) + { + mCubes[cubeId].minimum = cubeMinimal; + mCubes[cubeId].maximum = cubeMinimal + extents; + cubeMinimal.z += cubeSize; + ++cubeId; + } + cubeMinimal.y += cubeSize; + } + cubeMinimal.x += cubeSize; + } + + + for (uint32_t i = 0; i < in->getFacetCount(); ++i) + { + for (uint32_t c = 0; c < mCubes.size(); ++c) + { + if (testFacetUnitCubeIntersection(in->getVertices(), in->getEdges(), *in->getFacet(i), mCubes[c], 0.001)) + { + mSpatialMap[c].push_back(i); + } + } + } +} + + +int32_t IntersectionTestingAccelerator::getNextFacet() +{ + int32_t facetId = -1; + + while (mIteratorCell != -1) + { + if (mIteratorFacet >= (int32_t)mSpatialMap[mIteratorCell].size()) + { + if (!cellList.empty()) + { + mIteratorCell = cellList.back(); + cellList.pop_back(); + mIteratorFacet = 0; + } + else + { + mIteratorCell = -1; + break; + } + } + if (alreadyGotFlag[mSpatialMap[mIteratorCell][mIteratorFacet]] != alreadyGotValue) + { + facetId = mSpatialMap[mIteratorCell][mIteratorFacet]; + mIteratorFacet++; + break; + } + else + { + mIteratorFacet++; + } + } + if (facetId != -1) + { + alreadyGotFlag[facetId] = alreadyGotValue; + } + return facetId; +} + +void IntersectionTestingAccelerator::setState(Vertex* pos, Edge* ed, Facet& fc) +{ + alreadyGotValue++; + mIteratorCell = -1; + mIteratorFacet = -1; + cellList.clear(); + PxBounds3 bigBox(PxVec3(-0.5, -0.5, -0.5), PxVec3(0.5, 0.5, 0.5)); + if (!testFacetUnitCubeIntersection(pos, ed, fc, bigBox, 0.001f)) + { + return; + } + for (uint32_t i = 0; i < mCubes.size(); ++i) + { + if (testFacetUnitCubeIntersection(pos, ed, fc, mCubes[i], 0.001f)) + { + if (!mSpatialMap[i].empty()) + cellList.push_back(i); + } + } + if (!cellList.empty()) + { + mIteratorFacet = 0; + mIteratorCell = cellList.back(); + cellList.pop_back(); + } +} + +void IntersectionTestingAccelerator::setState(const PxVec3& p) +{ + alreadyGotValue++; + mIteratorCell = -1; + mIteratorFacet = -1; + cellList.clear(); + + + for (uint32_t i = 0; i < mCubes.size(); ++i) + { + PxBounds3 tmp = mCubes[i]; + tmp.fattenFast(0.001); + if (tmp.contains(p)) + { + int32_t xyCellId = (((int)((float)i / mResolution)) * mResolution); + for (int32_t zCell = 0; zCell < mResolution; ++zCell) + { + int32_t cell = zCell + xyCellId; + if (!mSpatialMap[cell].empty()) + { + cellList.push_back(cell); + } + + } + } + } + if (!cellList.empty()) + { + mIteratorFacet = 0; + mIteratorCell = cellList.back(); + cellList.pop_back(); + } +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.h new file mode 100644 index 0000000..8284cd7 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringAccelerator.h @@ -0,0 +1,147 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGACCELERATOR_H +#define NVBLASTEXTAUTHORINGACCELERATOR_H + +#include <set> +#include <vector> +#include "NvBlastExtAuthoringTypes.h" + + +namespace Nv +{ +namespace Blast +{ + +class Mesh; + + +/** + Acceleration structure interface. +*/ +class SpatialAccelerator +{ +public: + /** + Set state of accelerator to return all facets which possibly can intersect given facet. + \param[in] pos Vertex buffer + \param[in] ed Edge buffer + \param[in] fc Facet which should be tested. + */ + virtual void setState(Vertex* pos, Edge* ed, Facet& fc) = 0; + /** + Set state of accelerator to return all facets which possibly can cover given point. Needed for testing whether point is inside mesh. + \param[in] point Point which should be tested. + */ + virtual void setState(const physx::PxVec3& point) = 0; + /** + Recieve next facet for setted state. + \return Next facet index, or -1 if no facets left. + */ + virtual int32_t getNextFacet() = 0; + + virtual ~SpatialAccelerator() {}; +}; + + +/** + Dummy accelerator iterates through all facets of mesh. +*/ +class DummyAccelerator : public SpatialAccelerator +{ +public: + /** + \param[in] count Mesh facets count for which accelerator should be built. + */ + DummyAccelerator(int32_t count); + virtual void setState(Vertex* pos, Edge* ed, Facet& fc); + virtual void setState(const physx::PxVec3& point); + virtual int32_t getNextFacet(); + +private: + int32_t count; + int32_t current; +}; + +/** + Accelerator which builds map from 3d grid to initial mesh facets. + To find all facets which possibly intersect given one, it return all facets which are pointed by grid cells, which intersects with bounding box of given facet. + To find all facets which possibly cover given point, all facets which are pointed by cells in column which contains given point are returned. +*/ +class BBoxBasedAccelerator : public SpatialAccelerator +{ +public: + /** + \param[in] mesh Mesh for which acceleration structure should be built. + \param[in] resolution Resolution on 3d grid. + */ + BBoxBasedAccelerator(Mesh* mesh, int32_t resolution); + virtual ~BBoxBasedAccelerator(); + int32_t getNextFacet(); + void setState(Vertex* pos, Edge* ed, Facet& fc); + void setState(const physx::PxVec3& p); +private: + + bool testCellPolygonIntersection(int32_t cellId, physx::PxBounds3& facetBB); + void buildAccelStructure(Vertex* pos, Edge* edges, Facet* fc, int32_t facetCount); + + int32_t mResolution; + physx::PxBounds3 mBounds; + physx::PxBounds3 facetBox; + std::vector< std::vector<int32_t> > mSpatialMap; + std::vector<physx::PxBounds3> mCells; + + + // Iterator data + std::vector<uint32_t> alreadyGotFlag; + uint32_t alreadyGotValue; + std::vector<int32_t> cellList; + int32_t mIteratorCell; + int32_t mIteratorFacet; +}; + + + +/** + Accelerator which builds map from 3d grid to initial mesh facets. + To find all facets which possibly intersect given one, it return all facets which are pointed by grid cells, which are intersected by given facet. + To find all facets which possibly cover given point, all facets which are pointed by cells in column which contains given point are returned. + + In difference with BBoxBasedAccelerator this accelerator computes actual intersection of cube with polygon. It is more precise and omits much more intersections but slower. +*/ + +class IntersectionTestingAccelerator : public SpatialAccelerator +{ +public: + IntersectionTestingAccelerator(Mesh* mesh, int32_t resolution); + int32_t getNextFacet(); + void setState(Vertex* pos, Edge* ed, Facet& fc); + void setState(const physx::PxVec3& p); + + +private: + std::vector< std::vector<int32_t> > mSpatialMap; + std::vector<physx::PxBounds3> mCubes; + int32_t mResolution; + + // Iterator data + std::vector<uint32_t> alreadyGotFlag; + uint32_t alreadyGotValue; + std::vector<int32_t> cellList; + int32_t mIteratorCell; + int32_t mIteratorFacet; +}; + +} // namespace Blast +} // namsepace Nv + + +#endif // ifndef NVBLASTEXTAUTHORINGACCELERATOR_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBondGenerator.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBondGenerator.cpp new file mode 100644 index 0000000..b2c3883 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBondGenerator.cpp @@ -0,0 +1,991 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +// This warning arises when using some stl containers with older versions of VC +// c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1826): warning C4702: unreachable code +#include "NvPreprocessor.h" +#if NV_VC && NV_VC < 14 +#pragma warning(disable : 4702) +#endif + +#include <NvBlastExtAuthoringBondGenerator.h> +#include <NvBlastTypes.h> +#include <NvBlast.h> +#include "NvBlastExtTriangleProcessor.h" +#include "NvBlastExtApexSharedParts.h" +#include "NvBlastExtAuthoringCollisionBuilder.h" +#include "NvBlastExtAuthoringInternalCommon.h" +#include <vector> +#include <map> +#include <PxPlane.h> +#include <algorithm> +#include <cmath> + +using physx::PxVec3; +using physx::PxBounds3; + +//#define DEBUG_OUTPUT +#ifdef DEBUG_OUTPUT + +void saveGeometryToObj(std::vector<PxVec3>& triangles, const char* filepath) +{ + + FILE* outStream = fopen(filepath, "w"); + + for (uint32_t i = 0; i < triangles.size(); ++i) + { + fprintf(outStream, "v %lf %lf %lf\n", triangles[i].x, triangles[i].y, triangles[i].z); + ++i; + fprintf(outStream, "v %lf %lf %lf\n", triangles[i].x, triangles[i].y, triangles[i].z); + ++i; + fprintf(outStream, "v %lf %lf %lf\n", triangles[i].x, triangles[i].y, triangles[i].z); + } + for (uint32_t i = 0; i < triangles.size() / 3; ++i) + { + PxVec3 normal = (triangles[3 * i + 2] - triangles[3 * i]).cross((triangles[3 * i + 1] - triangles[3 * i])).getNormalized(); + fprintf(outStream, "vn %lf %lf %lf\n", normal.x, normal.y, normal.z); + fprintf(outStream, "vn %lf %lf %lf\n", normal.x, normal.y, normal.z); + fprintf(outStream, "vn %lf %lf %lf\n", normal.x, normal.y, normal.z); + } + int indx = 1; + for (uint32_t i = 0; i < triangles.size() / 3; ++i) + { + fprintf(outStream, "f %d//%d ", indx, indx); + indx++; + fprintf(outStream, "%d//%d ", indx, indx); + indx++; + fprintf(outStream, "%d//%d \n", indx, indx); + indx++; + } + + fclose(outStream); + +} + + +std::vector<PxVec3> intersectionBuffer; +std::vector<PxVec3> meshBuffer; +#endif + +namespace Nv +{ + namespace Blast + { + + #define EPS_PLANE 0.0001f + + bool planeComparer(const PlaneChunkIndexer& as, const PlaneChunkIndexer& bs) + { + const PxPlane& a = as.plane; + const PxPlane& b = bs.plane; + + if (a.d + EPS_PLANE < b.d) return true; + if (a.d - EPS_PLANE > b.d) return false; + if (a.n.x + EPS_PLANE < b.n.x) return true; + if (a.n.x - EPS_PLANE > b.n.x) return false; + if (a.n.y + EPS_PLANE < b.n.y) return true; + if (a.n.y - EPS_PLANE > b.n.y) return false; + return a.n.z + EPS_PLANE < b.n.z; + } + + + struct Bond + { + int32_t m_chunkId; + int32_t m_planeIndex; + int32_t triangleIndex; + + bool operator<(const Bond& inp) const + { + if (abs(m_planeIndex) == abs(inp.m_planeIndex)) + { + return m_chunkId < inp.m_chunkId; + } + else + { + return abs(m_planeIndex) < abs(inp.m_planeIndex); + } + } + }; + + + struct BondInfo + { + float area; + physx::PxBounds3 m_bb; + physx::PxVec3 centroid; + physx::PxVec3 normal; + int32_t m_chunkId; + }; + + + float BlastBondGenerator::processWithMidplanes(TriangleProcessor* trProcessor, const std::vector<PxVec3>& chunk1Points, const std::vector<PxVec3>& chunk2Points, + const std::vector<PxVec3>& hull1p, const std::vector<PxVec3>& hull2p, PxVec3& normal, PxVec3& centroid) + { + PxBounds3 bounds; + PxBounds3 aBounds; + PxBounds3 bBounds; + bounds.setEmpty(); + aBounds.setEmpty(); + bBounds.setEmpty(); + + PxVec3 chunk1Centroid(0, 0, 0); + PxVec3 chunk2Centroid(0, 0, 0); + + /////////////////////////////////////////////////////////////////////////////////// + if (chunk1Points.size() < 4 || chunk2Points.size() < 4) + { + return 0.0; + } + + for (uint32_t i = 0; i < chunk1Points.size(); ++i) + { + chunk1Centroid += chunk1Points[i]; + bounds.include(chunk1Points[i]); + aBounds.include(chunk1Points[i]); + } + for (uint32_t i = 0; i < chunk2Points.size(); ++i) + { + chunk2Centroid += chunk2Points[i]; + bounds.include(chunk2Points[i]); + bBounds.include(chunk2Points[i]); + } + + + chunk1Centroid *= (1.0f / chunk1Points.size()); + chunk2Centroid *= (1.0f / chunk2Points.size()); + + Separation separation; + if (!importerHullsInProximityApexFree(hull1p, aBounds, PxTransform(PxIdentity), PxVec3(1, 1, 1), hull2p, bBounds, PxTransform(PxIdentity), PxVec3(1, 1, 1), 0.000, &separation)) + { + return 0.0; + } + + // Build first plane interface + PxPlane midplane = separation.plane; + if (!midplane.n.isFinite()) + { + return 0.0; + } + std::vector<PxVec3> interfacePoints; + + float firstCentroidSide = midplane.distance(chunk1Centroid); + float secondCentroidSide = midplane.distance(chunk2Centroid); + + for (uint32_t i = 0; i < chunk1Points.size(); ++i) + { + float dst = midplane.distance(chunk1Points[i]); + if (dst * firstCentroidSide < 0) + { + interfacePoints.push_back(chunk1Points[i]); + } + } + + for (uint32_t i = 0; i < chunk2Points.size(); ++i) + { + float dst = midplane.distance(chunk2Points[i]); + if (dst * secondCentroidSide < 0) + { + interfacePoints.push_back(chunk2Points[i]); + } + } + std::vector<PxVec3> convexHull; + trProcessor->buildConvexHull(interfacePoints, convexHull, midplane.n); + float area = 0; + PxVec3 centroidLocal(0, 0, 0); + if (convexHull.size() < 3) + { + return 0.0; + } + for (uint32_t i = 0; i < convexHull.size() - 1; ++i) + { + centroidLocal += convexHull[i]; + area += (convexHull[i] - convexHull[0]).cross((convexHull[i + 1] - convexHull[0])).magnitude(); + } + centroidLocal += convexHull.back(); + centroidLocal *= (1.0f / convexHull.size()); + float direction = midplane.n.dot(chunk2Centroid - chunk1Centroid); + if (direction < 0) + { + normal = -1.0f * normal; + } + normal = midplane.n; + centroid = centroidLocal; + return area * 0.5f; + } + + + int32_t BlastBondGenerator::bondsFromPrefractured(const std::vector<std::vector<Triangle>>& geometry, const std::vector<bool>& chunkIsSupport, std::vector<NvBlastBondDesc>& resultBondDescs, BondGenerationConfig conf) + { + int32_t ret_val = 0; + switch (conf.bondMode) + { + case BondGenerationConfig::AVERAGE: + ret_val = createFullBondListAveraged(geometry, chunkIsSupport, resultBondDescs, conf); + break; + case BondGenerationConfig::EXACT: + ret_val = createFullBondListExact(geometry, chunkIsSupport, resultBondDescs, conf); + break; + } + return ret_val; + } + + int32_t BlastBondGenerator::createFullBondListAveraged(const std::vector<std::vector<Triangle>>& chunksGeometry, const std::vector<bool>& supportFlags, std::vector<NvBlastBondDesc>& mResultBondDescs, BondGenerationConfig conf) + { + NV_UNUSED(conf); + + std::vector<std::vector<PxVec3> > chunksPoints(chunksGeometry.size()); + + for (uint32_t i = 0; i < chunksGeometry.size(); ++i) + { + if (!supportFlags[i]) + { + continue; + } + for (uint32_t j = 0; j < chunksGeometry[i].size(); ++j) + { + chunksPoints[i].push_back(chunksGeometry[i][j].a.p); + chunksPoints[i].push_back(chunksGeometry[i][j].b.p); + chunksPoints[i].push_back(chunksGeometry[i][j].c.p); + } + } + + Nv::Blast::ConvexMeshBuilder builder(mPxCooking, mPxInsertionCallback); + + std::vector<CollisionHull> cHulls(chunksGeometry.size()); + + for (uint32_t i = 0; i < chunksGeometry.size(); ++i) + { + if (!supportFlags[i]) + { + continue; + } + builder.buildCollisionGeometry(chunksPoints[i], cHulls[i]); + } + + std::vector<std::vector<PxVec3> > hullPoints(cHulls.size()); + + for (uint32_t chunk = 0; chunk < cHulls.size(); ++chunk) + { + if (!supportFlags[chunk]) + { + continue; + } + + hullPoints[chunk].resize(cHulls[chunk].points.size()); + for (uint32_t i = 0; i < cHulls[chunk].points.size(); ++i) + { + hullPoints[chunk][i].x = cHulls[chunk].points[i].x; + hullPoints[chunk][i].y = cHulls[chunk].points[i].y; + hullPoints[chunk][i].z = cHulls[chunk].points[i].z; + } + } + + TriangleProcessor trProcessor; + + for (uint32_t i = 0; i < chunksGeometry.size(); ++i) + { + if (!supportFlags[i]) + { + continue; + } + for (uint32_t j = i + 1; j < chunksGeometry.size(); ++j) + { + if (!supportFlags[i]) + { + continue; + } + PxVec3 normal; + PxVec3 centroid; + + float area = processWithMidplanes(&trProcessor, chunksPoints[i], chunksPoints[j], hullPoints[i], hullPoints[j], normal, centroid); + + if (area > 0) + { + NvBlastBondDesc bDesc; + bDesc.chunkIndices[0] = i; + bDesc.chunkIndices[1] = j; + bDesc.bond.area = area; + bDesc.bond.centroid[0] = centroid.x; + bDesc.bond.centroid[1] = centroid.y; + bDesc.bond.centroid[2] = centroid.z; + + bDesc.bond.normal[0] = normal.x; + bDesc.bond.normal[1] = normal.y; + bDesc.bond.normal[2] = normal.z; + + + mResultBondDescs.push_back(bDesc); + } + + } + } + + return 0; + } + + uint32_t isSamePlane(PxPlane& a, PxPlane& b) + { + if (PxAbs(a.d - b.d) > EPS_PLANE) return 0; + if (PxAbs(a.n.x - b.n.x) > EPS_PLANE) return 0; + if (PxAbs(a.n.y - b.n.y) > EPS_PLANE) return 0; + if (PxAbs(a.n.z - b.n.z) > EPS_PLANE) return 0; + return 1; + } + + int32_t BlastBondGenerator::createFullBondListExact(const std::vector<std::vector<Triangle>>& chunksGeometry, const std::vector<bool>& supportFlags, std::vector<NvBlastBondDesc>& mResultBondDescs, BondGenerationConfig conf) + { + std::vector < PlaneChunkIndexer > planeTriangleMapping; + NV_UNUSED(conf); + for (uint32_t i = 0; i < chunksGeometry.size(); ++i) + { + if (!supportFlags[i]) + { + continue; + } + for (uint32_t j = 0; j < chunksGeometry[i].size(); ++j) + { +#ifdef DEBUG_OUTPUT + meshBuffer.push_back(chunksGeometry[i][j].a.p ); + meshBuffer.push_back(chunksGeometry[i][j].b.p); + meshBuffer.push_back(chunksGeometry[i][j].c.p ); +#endif + + PxPlane nPlane = PxPlane(chunksGeometry[i][j].a.p, chunksGeometry[i][j].b.p, chunksGeometry[i][j].c.p); + planeTriangleMapping.push_back(PlaneChunkIndexer(i, j, nPlane)); + } + } + + std::sort(planeTriangleMapping.begin(), planeTriangleMapping.end(), planeComparer); + return createFullBondListExactInternal(chunksGeometry, planeTriangleMapping, mResultBondDescs); + } + + void BlastBondGenerator::buildGeometryCache(const std::vector<std::vector<Triangle> >& geometry) + { + mGeometryCache = geometry; + mHullsPointsCache.resize(geometry.size()); + mBoundsCache.resize(geometry.size()); + mCHullCache.resize(geometry.size()); + for (uint32_t i = 0; i < mGeometryCache.size(); ++i) + { + for (uint32_t j = 0; j < mGeometryCache[i].size(); ++j) + { + + PxPlane nPlane = PxPlane(mGeometryCache[i][j].a.p, mGeometryCache[i][j].b.p, mGeometryCache[i][j].c.p); + mPlaneCache.push_back(PlaneChunkIndexer(i, j, nPlane)); + } + } + + for (uint32_t ch = 0; ch < mGeometryCache.size(); ++ch) + { + std::vector<PxVec3> chunksPoints(mGeometryCache[ch].size() * 3); + + int32_t sp = 0; + for (uint32_t i = 0; i < mGeometryCache[ch].size(); ++i) + { + chunksPoints[sp++] = mGeometryCache[ch][i].a.p; + chunksPoints[sp++] = mGeometryCache[ch][i].b.p; + chunksPoints[sp++] = mGeometryCache[ch][i].c.p; + } + + Nv::Blast::ConvexMeshBuilder builder(mPxCooking, mPxInsertionCallback); + + CollisionHull& cHull = mCHullCache[ch]; + + builder.buildCollisionGeometry(chunksPoints, cHull); + + mHullsPointsCache[ch].resize(cHull.points.size()); + + mBoundsCache[ch].setEmpty(); + for (uint32_t i = 0; i < cHull.points.size(); ++i) + { + mHullsPointsCache[ch][i].x = cHull.points[i].x; + mHullsPointsCache[ch][i].y = cHull.points[i].y; + mHullsPointsCache[ch][i].z = cHull.points[i].z; + mBoundsCache[ch].include(mHullsPointsCache[ch][i]); + } + } + } + + void BlastBondGenerator::resetGeometryCache() + { + mGeometryCache.clear(); + mPlaneCache.clear(); + mHullsPointsCache.clear(); + mCHullCache.clear(); + mBoundsCache.clear(); + } + + int32_t BlastBondGenerator::createFullBondListExactInternal(const std::vector<std::vector<Triangle>>& chunksGeometry, std::vector < PlaneChunkIndexer >& planeTriangleMapping, std::vector<NvBlastBondDesc>& mResultBondDescs) + { + std::map<std::pair<int32_t, int32_t>, std::pair<NvBlastBondDesc, int32_t> > bonds; + + TriangleProcessor trPrc; + std::vector<PxVec3> intersectionBufferLocal; + + NvBlastBondDesc cleanBond; + memset(&cleanBond, 0, sizeof(NvBlastBondDesc)); + for (uint32_t tIndex = 0; tIndex < planeTriangleMapping.size(); ++tIndex) + { + + PlaneChunkIndexer opp = planeTriangleMapping[tIndex]; + + opp.plane.d *= -1; + opp.plane.n *= -1; + + uint32_t startIndex = (uint32_t)(std::lower_bound(planeTriangleMapping.begin(), planeTriangleMapping.end(), opp, planeComparer) - planeTriangleMapping.begin()); + uint32_t endIndex = (uint32_t)(std::upper_bound(planeTriangleMapping.begin(), planeTriangleMapping.end(), opp, planeComparer) - planeTriangleMapping.begin()); + // uint32_t startIndex = 0; + // uint32_t endIndex = (uint32_t)planeTriangleMapping.size(); + + PlaneChunkIndexer& mappedTr = planeTriangleMapping[tIndex]; + const Triangle& trl = chunksGeometry[mappedTr.chunkId][mappedTr.trId]; + PxPlane pln = mappedTr.plane; + TrPrcTriangle trp(trl.a.p, trl.b.p, trl.c.p); + PxVec3 trCentroid = (trl.a.p + trl.b.p + trl.c.p) * (1.0f / 3.0f); + trp.points[0] -= trCentroid; + trp.points[1] -= trCentroid; + trp.points[2] -= trCentroid; + ProjectionDirections pDir = getProjectionDirection(pln.n); + TrPrcTriangle2d trp2d; + trp2d.points[0] = getProjectedPointWithWinding(trp.points[0], pDir); + trp2d.points[1] = getProjectedPointWithWinding(trp.points[1], pDir); + trp2d.points[2] = getProjectedPointWithWinding(trp.points[2], pDir); + + for (uint32_t i = startIndex; i <= endIndex && i < planeTriangleMapping.size(); ++i) + { + PlaneChunkIndexer& mappedTr2 = planeTriangleMapping[i]; + if (mappedTr2.trId == opp.chunkId) + { + continue; + } + + if (!isSamePlane(opp.plane, mappedTr2.plane)) + { + continue; + } + + if (mappedTr.chunkId == mappedTr2.chunkId) + { + continue; + } + std::pair<int32_t, int32_t> bondEndPoints = std::make_pair(mappedTr.chunkId, mappedTr2.chunkId); + if (bondEndPoints.second < bondEndPoints.first) continue; + std::pair<int32_t, int32_t> bondEndPointsSwapped = std::make_pair(mappedTr2.chunkId, mappedTr.chunkId); + if (bonds.find(bondEndPoints) == bonds.end() && bonds.find(bondEndPointsSwapped) != bonds.end()) + { + continue; // We do not need account interface surface twice + } + if (bonds.find(bondEndPoints) == bonds.end()) + { + bonds[bondEndPoints].second = 0; + bonds[bondEndPoints].first = cleanBond; + bonds[bondEndPoints].first.chunkIndices[0] = bondEndPoints.first; + bonds[bondEndPoints].first.chunkIndices[1] = bondEndPoints.second; + bonds[bondEndPoints].first.bond.normal[0] = pln.n[0]; + bonds[bondEndPoints].first.bond.normal[1] = pln.n[1]; + bonds[bondEndPoints].first.bond.normal[2] = pln.n[2]; + } + + const Triangle& trl2 = chunksGeometry[mappedTr2.chunkId][mappedTr2.trId]; + + TrPrcTriangle trp2(trl2.a.p, trl2.b.p, trl2.c.p); + + intersectionBufferLocal.clear(); + intersectionBufferLocal.reserve(32); + trPrc.getTriangleIntersection(trp, trp2d, trp2, trCentroid, intersectionBufferLocal, pln.n); + PxVec3 centroidPoint(0, 0, 0); + int32_t collectedVerticesCount = 0; + float area = 0; + if (intersectionBufferLocal.size() >= 3) + { +#ifdef DEBUG_OUTPUT + for (uint32_t p = 1; p < intersectionBufferLocal.size() - 1; ++p) + { + intersectionBuffer.push_back(intersectionBufferLocal[0]); + intersectionBuffer.push_back(intersectionBufferLocal[p]); + intersectionBuffer.push_back(intersectionBufferLocal[p + 1]); + } +#endif + centroidPoint = intersectionBufferLocal[0] + intersectionBufferLocal.back(); + collectedVerticesCount = 2; + + for (uint32_t j = 1; j < intersectionBufferLocal.size() - 1; ++j) + { + ++collectedVerticesCount; + centroidPoint += intersectionBufferLocal[j]; + area += (intersectionBufferLocal[j + 1] - intersectionBufferLocal[0]).cross(intersectionBufferLocal[j] - intersectionBufferLocal[0]).magnitude(); + } + } + if (area > 0.00001f) + { + bonds[bondEndPoints].second += collectedVerticesCount; + + bonds[bondEndPoints].first.bond.area += area * 0.5f; + bonds[bondEndPoints].first.bond.centroid[0] += (centroidPoint.x); + bonds[bondEndPoints].first.bond.centroid[1] += (centroidPoint.y); + bonds[bondEndPoints].first.bond.centroid[2] += (centroidPoint.z); + } + } + } + + for (auto it : bonds) + { + if (it.second.first.bond.area > 0) + { + float mlt = 1.0f / (it.second.second); + it.second.first.bond.centroid[0] *= mlt; + it.second.first.bond.centroid[1] *= mlt; + it.second.first.bond.centroid[2] *= mlt; + + mResultBondDescs.push_back(it.second.first); + } + + } +#ifdef DEBUG_OUTPUT + saveGeometryToObj(meshBuffer, "Mesh.obj"); + saveGeometryToObj(intersectionBuffer, "inter.obj"); +#endif + return 0; + } + + int32_t BlastBondGenerator::createBondForcedInternal(const std::vector<PxVec3>& hull0, const std::vector<PxVec3>& hull1, + const CollisionHull& cHull0,const CollisionHull& cHull1, + PxBounds3 bound0, PxBounds3 bound1, NvBlastBond& resultBond, float overlapping) + { + + TriangleProcessor trProcessor; + Separation separation; + importerHullsInProximityApexFree(hull0, bound0, PxTransform(PxIdentity), PxVec3(1, 1, 1), hull1, bound1, PxTransform(PxIdentity), PxVec3(1, 1, 1), 0.000, &separation); + + if (std::isnan(separation.plane.d)) + { + importerHullsInProximityApexFree(hull0, bound0, PxTransform(PxVec3(0.000001f, 0.000001f, 0.000001f)), PxVec3(1, 1, 1), hull1, bound1, PxTransform(PxIdentity), PxVec3(1, 1, 1), 0.000, &separation); + if (std::isnan(separation.plane.d)) + { + return 1; + } + } + + PxPlane pl = separation.plane; + std::vector<PxVec3> ifsPoints[2]; + + float dst[2][2]; + + dst[0][0] = 0; + dst[0][1] = MAXIMUM_EXTENT; + for (uint32_t p = 0; p < cHull0.points.size(); ++p) + { + float d = pl.distance(PxVec3(cHull0.points[p].x, cHull0.points[p].y, cHull0.points[p].z)); + if (PxAbs(d) > PxAbs(dst[0][0])) + { + dst[0][0] = d; + } + if (PxAbs(d) < PxAbs(dst[0][1])) + { + dst[0][1] = d; + } + } + + dst[1][0] = 0; + dst[1][1] = MAXIMUM_EXTENT; + for (uint32_t p = 0; p < cHull1.points.size(); ++p) + { + float d = pl.distance(PxVec3(cHull1.points[p].x, cHull1.points[p].y, cHull1.points[p].z)); + if (PxAbs(d) > PxAbs(dst[1][0])) + { + dst[1][0] = d; + } + if (PxAbs(d) < PxAbs(dst[1][1])) + { + dst[1][1] = d; + } + } + + + float cvOffset[2] = { dst[0][1] + (dst[0][0] - dst[0][1]) * overlapping, dst[1][1] + (dst[1][0] - dst[1][1]) * overlapping }; + + for (uint32_t i = 0; i < cHull0.polygonData.size(); ++i) + { + uint32_t offset = cHull0.polygonData[i].mIndexBase; + PxVec3 result; + for (uint32_t j = 0; j < cHull0.polygonData[i].mNbVerts; ++j) + { + uint32_t nxj = (j + 1) % cHull0.polygonData[i].mNbVerts; + const uint32_t* ind = &cHull0.indices[0]; + PxVec3 a = hull0[ind[j + offset]] - pl.n * cvOffset[0]; + PxVec3 b = hull0[ind[nxj + offset]] - pl.n * cvOffset[0]; + + if (getPlaneSegmentIntersection(pl, a, b, result)) + { + ifsPoints[0].push_back(result); + } + } + } + + for (uint32_t i = 0; i < cHull1.polygonData.size(); ++i) + { + uint32_t offset = cHull1.polygonData[i].mIndexBase; + PxVec3 result; + for (uint32_t j = 0; j < cHull1.polygonData[i].mNbVerts; ++j) + { + uint32_t nxj = (j + 1) % cHull1.polygonData[i].mNbVerts; + const uint32_t* ind = &cHull1.indices[0]; + PxVec3 a = hull1[ind[j + offset]] - pl.n * cvOffset[1]; + PxVec3 b = hull1[ind[nxj + offset]] - pl.n * cvOffset[1]; + + if (getPlaneSegmentIntersection(pl, a, b, result)) + { + ifsPoints[1].push_back(result); + } + } + } + + + std::vector<PxVec3> convexes[2]; + + trProcessor.buildConvexHull(ifsPoints[0], convexes[0], pl.n); + trProcessor.buildConvexHull(ifsPoints[1], convexes[1], pl.n); + + float areas[2] = { 0, 0 }; + PxVec3 centroids[2] = { PxVec3(0, 0, 0), PxVec3(0, 0, 0) }; + + for (uint32_t cv = 0; cv < 2; ++cv) + { + if (convexes[cv].size() == 0) + { + continue; + } + centroids[cv] = convexes[cv][0] + convexes[cv].back(); + for (uint32_t i = 1; i < convexes[cv].size() - 1; ++i) + { + centroids[cv] += convexes[cv][i]; + areas[cv] += (convexes[cv][i + 1] - convexes[cv][0]).cross(convexes[cv][i] - convexes[cv][0]).magnitude(); +#ifdef DEBUG_OUTPUT + intersectionBuffer.push_back(convexes[cv][0]); + intersectionBuffer.push_back(convexes[cv][i]); + intersectionBuffer.push_back(convexes[cv][i + 1]); +#endif + + } + centroids[cv] *= (1.0f / convexes[cv].size()); + areas[cv] = PxAbs(areas[cv]); + } + + resultBond.area = (areas[0] + areas[1]) * 0.5f; + resultBond.centroid[0] = (centroids[0][0] + centroids[1][0]) * 0.5f; + resultBond.centroid[1] = (centroids[0][1] + centroids[1][1]) * 0.5f; + resultBond.centroid[2] = (centroids[0][2] + centroids[1][2]) * 0.5f; + resultBond.normal[0] = pl.n[0]; + resultBond.normal[1] = pl.n[1]; + resultBond.normal[2] = pl.n[2]; + +#ifdef DEBUG_OUTPUT + saveGeometryToObj(meshBuffer, "ArbitMeshes.obj"); + saveGeometryToObj(intersectionBuffer, "inter.obj"); +#endif + + + return 0; + } + + + int32_t BlastBondGenerator::buildDescFromInternalFracture(FractureTool* tool, const std::vector<bool>& chunkIsSupport, std::vector<NvBlastBondDesc>& mResultBondDescs, std::vector<NvBlastChunkDesc>& mResultChunkDescriptors) + { + const std::vector<ChunkInfo>& chunkData = tool->getChunkList(); + std::vector<std::vector<Triangle> > trianglesBuffer(chunkData.size()); + + for (uint32_t i = 0; i < trianglesBuffer.size(); ++i) + { + tool->getBaseMesh(i, trianglesBuffer[i]); + } + + if (chunkData.empty() || trianglesBuffer.empty()) + { + return 1; + } + mResultChunkDescriptors.resize(trianglesBuffer.size()); + std::vector<Bond> bondDescriptors; + mResultChunkDescriptors[0].parentChunkIndex = UINT32_MAX; + mResultChunkDescriptors[0].userData = 0; + + { + PxVec3 chunkCentroid(0, 0, 0); + for (uint32_t tr = 0; tr < trianglesBuffer[0].size(); ++tr) + { + chunkCentroid += trianglesBuffer[0][tr].a.p; + chunkCentroid += trianglesBuffer[0][tr].b.p; + chunkCentroid += trianglesBuffer[0][tr].c.p; + } + chunkCentroid *= (1.0f / (3 * trianglesBuffer[0].size())); + mResultChunkDescriptors[0].centroid[0] = chunkCentroid[0]; + mResultChunkDescriptors[0].centroid[1] = chunkCentroid[1]; + mResultChunkDescriptors[0].centroid[2] = chunkCentroid[2]; + } + + for (uint32_t i = 1; i < chunkData.size(); ++i) + { + + mResultChunkDescriptors[i].userData = i; + mResultChunkDescriptors[i].parentChunkIndex = tool->getChunkIndex(chunkData[i].parent); + if (chunkIsSupport[i]) + mResultChunkDescriptors[i].flags = NvBlastChunkDesc::SupportFlag; + PxVec3 chunkCentroid(0, 0, 0); + for (uint32_t tr = 0; tr < trianglesBuffer[i].size(); ++tr) + { + chunkCentroid += trianglesBuffer[i][tr].a.p; + chunkCentroid += trianglesBuffer[i][tr].b.p; + chunkCentroid += trianglesBuffer[i][tr].c.p; + + Triangle& trRef = trianglesBuffer[i][tr]; + int32_t id = trRef.userInfo; + if (id == 0) + continue; + bondDescriptors.push_back(Bond()); + Bond& bond = bondDescriptors.back(); + bond.m_chunkId = i; + bond.m_planeIndex = id; + bond.triangleIndex = tr; + } + chunkCentroid *= (1.0f / (3 * trianglesBuffer[i].size())); + mResultChunkDescriptors[i].centroid[0] = chunkCentroid[0]; + mResultChunkDescriptors[i].centroid[1] = chunkCentroid[1]; + mResultChunkDescriptors[i].centroid[2] = chunkCentroid[2]; + } + std::sort(bondDescriptors.begin(), bondDescriptors.end()); + if (bondDescriptors.empty()) + { + return 0; + } + int32_t chunkId, planeId; + chunkId = bondDescriptors[0].m_chunkId; + planeId = bondDescriptors[0].m_planeIndex; + std::vector<BondInfo> forwardChunks; + std::vector<BondInfo> backwardChunks; + + float area = 0; + PxVec3 normal(0, 0, 0); + PxVec3 centroid(0, 0, 0); + int32_t collected = 0; + PxBounds3 bb = PxBounds3::empty(); + + chunkId = -1; + planeId = bondDescriptors[0].m_planeIndex; + for (uint32_t i = 0; i <= bondDescriptors.size(); ++i) + { + if (i == bondDescriptors.size() || (chunkId != bondDescriptors[i].m_chunkId || abs(planeId) != abs(bondDescriptors[i].m_planeIndex))) + { + if (chunkId != -1) + { + if (bondDescriptors[i - 1].m_planeIndex > 0) { + forwardChunks.push_back(BondInfo()); + forwardChunks.back().area = area; + forwardChunks.back().normal = normal; + forwardChunks.back().centroid = centroid * (1.0f / 3.0f / collected); + forwardChunks.back().m_chunkId = chunkId; + forwardChunks.back().m_bb = bb; + + } + else + { + backwardChunks.push_back(BondInfo()); + backwardChunks.back().area = area; + backwardChunks.back().normal = normal; + backwardChunks.back().centroid = centroid * (1.0f / 3.0f / collected); + backwardChunks.back().m_chunkId = chunkId; + backwardChunks.back().m_bb = bb; + } + } + bb.setEmpty(); + collected = 0; + area = 0; + normal = PxVec3(0, 0, 0); + centroid = PxVec3(0, 0, 0); + if (i != bondDescriptors.size()) + chunkId = bondDescriptors[i].m_chunkId; + } + if (i == bondDescriptors.size() || abs(planeId) != abs(bondDescriptors[i].m_planeIndex)) + { + for (uint32_t fchunk = 0; fchunk < forwardChunks.size(); ++fchunk) + { + for (uint32_t bchunk = 0; bchunk < backwardChunks.size(); ++bchunk) + { + if (weakBoundingBoxIntersection(forwardChunks[fchunk].m_bb, backwardChunks[bchunk].m_bb) == 0) + { + continue; + } + if (chunkIsSupport[forwardChunks[fchunk].m_chunkId] == false || chunkIsSupport[backwardChunks[bchunk].m_chunkId] == false) + { + continue; + } + mResultBondDescs.push_back(NvBlastBondDesc()); + mResultBondDescs.back().bond.area = std::min(forwardChunks[fchunk].area, backwardChunks[bchunk].area); + mResultBondDescs.back().bond.normal[0] = forwardChunks[fchunk].normal.x; + mResultBondDescs.back().bond.normal[1] = forwardChunks[fchunk].normal.y; + mResultBondDescs.back().bond.normal[2] = forwardChunks[fchunk].normal.z; + + mResultBondDescs.back().bond.centroid[0] = (forwardChunks[fchunk].centroid.x + backwardChunks[bchunk].centroid.x ) * 0.5; + mResultBondDescs.back().bond.centroid[1] = (forwardChunks[fchunk].centroid.y + backwardChunks[bchunk].centroid.y) * 0.5; + mResultBondDescs.back().bond.centroid[2] = (forwardChunks[fchunk].centroid.z + backwardChunks[bchunk].centroid.z) * 0.5; + + + mResultBondDescs.back().chunkIndices[0] = forwardChunks[fchunk].m_chunkId; + mResultBondDescs.back().chunkIndices[1] = backwardChunks[bchunk].m_chunkId; + } + } + forwardChunks.clear(); + backwardChunks.clear(); + if (i != bondDescriptors.size()) + { + planeId = bondDescriptors[i].m_planeIndex; + } + else + { + break; + } + } + + collected++; + int32_t tr = bondDescriptors[i].triangleIndex; + PxVec3 n = trianglesBuffer[chunkId][tr].getNormal(); + area += n.magnitude(); + normal = n.getNormalized(); + centroid += trianglesBuffer[chunkId][tr].a.p; + centroid += trianglesBuffer[chunkId][tr].b.p; + centroid += trianglesBuffer[chunkId][tr].c.p; + + bb.include(trianglesBuffer[chunkId][tr].a.p); + bb.include(trianglesBuffer[chunkId][tr].b.p); + bb.include(trianglesBuffer[chunkId][tr].c.p); + } + + return 0; + } + + int32_t BlastBondGenerator::createBondBetweenMeshes(const std::vector<std::vector<Triangle> >& geometry, std::vector<NvBlastBondDesc>& resultBond,const std::vector<std::pair<uint32_t, uint32_t> >& overlaps, BondGenerationConfig cfg) + { + if (cfg.bondMode == BondGenerationConfig::AVERAGE) + { + resetGeometryCache(); + buildGeometryCache(geometry); + } + resultBond.clear(); + resultBond.resize(overlaps.size()); + + if (cfg.bondMode == BondGenerationConfig::EXACT) + { + for (uint32_t i = 0; i < overlaps.size(); ++i) + { + resultBond[i].chunkIndices[0] = overlaps[i].first; + resultBond[i].chunkIndices[1] = overlaps[i].second; + createBondBetweenMeshes(geometry[overlaps[i].first], geometry[overlaps[i].second], resultBond[i].bond, cfg); + } + } + else + { + for (uint32_t i = 0; i < overlaps.size(); ++i) + { + resultBond[i].chunkIndices[0] = overlaps[i].first; + resultBond[i].chunkIndices[1] = overlaps[i].second; + createBondForcedInternal(mHullsPointsCache[overlaps[i].first], mHullsPointsCache[overlaps[i].second], mCHullCache[overlaps[i].first], mCHullCache[overlaps[i].second], + mBoundsCache[overlaps[i].first], mBoundsCache[overlaps[i].second], resultBond[i].bond, 0.3f); + } + } + + return 0; + } + + + int32_t BlastBondGenerator::createBondBetweenMeshes(const std::vector<Triangle>& meshA, const std::vector<Triangle>& meshB, NvBlastBond& resultBond, BondGenerationConfig conf) + { + float overlapping = 0.3; + if (conf.bondMode == BondGenerationConfig::EXACT) + { + std::vector<std::vector<Triangle> > chunks; + chunks.push_back(meshA); + chunks.push_back(meshB); + std::vector<bool> isSupport(2, true); + std::vector<NvBlastBondDesc> desc; + createFullBondListExact(chunks, isSupport, desc, conf); + if (desc.size() > 0) + { + resultBond = desc.back().bond; + } + else + { + return 1; + } + return 0; + } + + std::vector<PxVec3> chunksPoints1(meshA.size() * 3); + std::vector<PxVec3> chunksPoints2(meshB.size() * 3); + + int32_t sp = 0; + for (uint32_t i = 0; i < meshA.size(); ++i) + { + chunksPoints1[sp++] = meshA[i].a.p; + chunksPoints1[sp++] = meshA[i].b.p; + chunksPoints1[sp++] = meshA[i].c.p; +#ifdef DEBUG_OUTPUT + meshBuffer.push_back(meshA[i].a.p); + meshBuffer.push_back(meshA[i].b.p); + meshBuffer.push_back(meshA[i].c.p); +#endif + + + } + sp = 0; + for (uint32_t i = 0; i < meshB.size(); ++i) + { + chunksPoints2[sp++] = meshB[i].a.p; + chunksPoints2[sp++] = meshB[i].b.p; + chunksPoints2[sp++] = meshB[i].c.p; +#ifdef DEBUG_OUTPUT + meshBuffer.push_back(meshB[i].a.p); + meshBuffer.push_back(meshB[i].b.p); + meshBuffer.push_back(meshB[i].c.p); +#endif + } + + + Nv::Blast::ConvexMeshBuilder builder(mPxCooking, mPxInsertionCallback); + + CollisionHull cHull[2]; + + builder.buildCollisionGeometry(chunksPoints1, cHull[0]); + builder.buildCollisionGeometry(chunksPoints2, cHull[1]); + + std::vector<PxVec3> hullPoints[2]; + hullPoints[0].resize(cHull[0].points.size()); + hullPoints[1].resize(cHull[1].points.size()); + + + PxBounds3 bb[2]; + bb[0].setEmpty(); + bb[1].setEmpty(); + + for (uint32_t cv = 0; cv < 2; ++cv) + { + for (uint32_t i = 0; i < cHull[cv].points.size(); ++i) + { + hullPoints[cv][i].x = cHull[cv].points[i].x; + hullPoints[cv][i].y = cHull[cv].points[i].y; + hullPoints[cv][i].z = cHull[cv].points[i].z; + bb[cv].include(hullPoints[cv][i]); + } + } + return createBondForcedInternal(hullPoints[0], hullPoints[1], cHull[0], cHull[1], bb[0], bb[1], resultBond, overlapping); + } + + + + } +} diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.cpp new file mode 100644 index 0000000..b5030d7 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.cpp @@ -0,0 +1,1351 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtAuthoringBooleanTool.h" +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastExtAuthoringAccelerator.h" + +#include <math.h> +#include <set> +#include <algorithm> + +using physx::PxVec3; +using physx::PxVec2; +using physx::PxBounds3; + + +namespace Nv +{ +namespace Blast +{ + +/* Linear interpolation of vectors */ + +NV_FORCE_INLINE void vec3Lerp(const PxVec3& a, const PxVec3& b, PxVec3& out, float t) +{ + out.x = (b.x - a.x) * t + a.x; + out.y = (b.y - a.y) * t + a.y; + out.z = (b.z - a.z) * t + a.z; +} + +NV_FORCE_INLINE void vec2Lerp(const PxVec2& a, const PxVec2& b, PxVec2& out, float t) +{ + out.x = (b.x - a.x) * t + a.x; + out.y = (b.y - a.y) * t + a.y; +} + + +NV_FORCE_INLINE int32_t BooleanEvaluator::addIfNotExist(Vertex& p) +{ + mVerticesAggregate.push_back(p); + return static_cast<int32_t>(mVerticesAggregate.size()) - 1; +} + +NV_FORCE_INLINE void BooleanEvaluator::addEdgeIfValid(EdgeWithParent& ed) +{ + mEdgeAggregate.push_back(ed); +} + +/** +Vertex level shadowing functions +*/ +NV_FORCE_INLINE int32_t vertexShadowing(const PxVec3& a, const PxVec3& b) +{ + return (b.x >= a.x) ? 1 : 0; +} +/** +Vertex-edge status functions +*/ +NV_FORCE_INLINE int32_t veStatus01(const PxVec3& sEdge, const PxVec3& eEdge, const PxVec3& p) +{ + return vertexShadowing(p, eEdge) - vertexShadowing(p, sEdge); +} + +NV_FORCE_INLINE int32_t veStatus10(const PxVec3& sEdge, const PxVec3& eEdge, const PxVec3& p) +{ + return -vertexShadowing(eEdge, p) + vertexShadowing(sEdge, p); +} + +/** +Vertex-edge shadowing functions +*/ +int32_t shadowing01(const Vertex& sEdge, const Vertex& eEdge, const PxVec3& p, Vertex& onEdgePoint, bool& hasOnEdge) +{ + int32_t winding = veStatus01(sEdge.p, eEdge.p, p); + if (winding != 0) + { + float t = (p.x - sEdge.p.x) / (eEdge.p.x - sEdge.p.x); + if (t >= 1) + { + onEdgePoint = eEdge; + } + else if (t <= 0) + { + onEdgePoint = sEdge; + } + else + { + vec3Lerp(sEdge.p, eEdge.p, onEdgePoint.p, t); + vec3Lerp(sEdge.n, eEdge.n, onEdgePoint.n, t); + vec2Lerp(sEdge.uv[0], eEdge.uv[0], onEdgePoint.uv[0], t); + } + hasOnEdge = true; + if (onEdgePoint.p.y >= p.y) + { + return winding; + } + } + else + { + hasOnEdge = false; + } + return 0; +} +int32_t shadowing10(const Vertex& sEdge, const Vertex& eEdge, const PxVec3& p, Vertex& onEdgePoint, bool& hasOnEdge) +{ + int32_t winding = veStatus10(sEdge.p, eEdge.p, p); + if (winding != 0) + { + float t = (p.x - sEdge.p.x) / (eEdge.p.x - sEdge.p.x); + if (t >= 1) + { + onEdgePoint = eEdge; + } + else if (t <= 0) + { + onEdgePoint = sEdge; + } + else + { + vec3Lerp(sEdge.p, eEdge.p, onEdgePoint.p, t); + vec3Lerp(sEdge.n, eEdge.n, onEdgePoint.n, t); + vec2Lerp(sEdge.uv[0], eEdge.uv[0], onEdgePoint.uv[0], t); + } + hasOnEdge = true; + if (onEdgePoint.p.y < p.y) + { + return winding; + } + } + else + { + hasOnEdge = false; + } + return 0; +} + +int32_t shadowing01(const PxVec3& sEdge, const PxVec3& eEdge, const PxVec3& p) +{ + int32_t winding = veStatus01(sEdge, eEdge, p); + if (winding != 0) + { + float t = ((p.x - sEdge.x) / (eEdge.x - sEdge.x)); + PxVec3 onEdgePoint; + if (t >= 1) + onEdgePoint = eEdge; + else if (t <= 0) + onEdgePoint = sEdge; + else + vec3Lerp(sEdge, eEdge, onEdgePoint, t); + if (onEdgePoint.y >= p.y) + { + return winding; + } + } + return 0; +} + +int32_t shadowing10(const PxVec3& sEdge, const PxVec3& eEdge, const PxVec3& p) +{ + int32_t winding = veStatus10(sEdge, eEdge, p); + if (winding != 0) + { + float t = ((p.x - sEdge.x) / (eEdge.x - sEdge.x)); + PxVec3 onEdgePoint; + if (t >= 1) + onEdgePoint = eEdge; + else if (t <= 0) + onEdgePoint = sEdge; + else + vec3Lerp(sEdge, eEdge, onEdgePoint, t); + if (onEdgePoint.y < p.y) + { + return winding; + } + } + return 0; +} + +/** +Vertex-facet shadowing functions +*/ + +int32_t vfStatus02(const PxVec3& p, const Vertex* points, const Edge* edges, int32_t edgesCount, Vertex& out1, Vertex& out2) +{ + int32_t val = 0; + Vertex pnt; + bool hasOnEdge = false; + for (int32_t i = 0; i < edgesCount; ++i) + { + val -= shadowing01(points[edges->s], points[edges->e], p, pnt, hasOnEdge); + if (hasOnEdge != 0) + { + out2 = out1; + out1 = pnt; + } + ++edges; + } + return val; +} + + +int32_t shadowing02(const PxVec3& p, const Vertex* points, const Edge* edges, int edgesCount, bool& hasOnFacetPoint, Vertex& onFacetPoint) +{ + Vertex p1, p2; + int32_t stat = vfStatus02(p, points, edges, edgesCount, p1, p2); + float z = 0; + hasOnFacetPoint = false; + if (stat != 0) + { + PxVec3 vc = p2.p - p1.p; + float t = 0; + t = (abs(vc.x) > abs(vc.y)) ? (p.x - p1.p.x) / vc.x : (p.y - p1.p.y) / vc.y; + t = (t < 0) ? 0 : t; + t = (t > 1) ? 1 : t; + z = t * vc.z + p1.p.z; + + hasOnFacetPoint = true; + onFacetPoint.p.x = p.x; + onFacetPoint.p.y = p.y; + onFacetPoint.p.z = z; + + vec2Lerp(p1.uv[0], p2.uv[0], onFacetPoint.uv[0], t); + vec3Lerp(p1.n, p2.n, onFacetPoint.n, t); + + if (z >= p.z) + { + return stat; + } + } + return 0; +} + +int32_t vfStatus20(const PxVec3& p, const Vertex* points, const Edge* edges, int32_t edgesCount, Vertex& out1, Vertex& out2) +{ + int32_t val = 0; + Vertex pnt; + bool hasOnEdge = false; + for (int32_t i = 0; i < edgesCount; ++i) + { + val += shadowing10(points[edges->s], points[edges->e], p, pnt, hasOnEdge); + if (hasOnEdge != 0) + { + out2 = out1; + out1 = pnt; + } + ++edges; + } + return val; +} + +int32_t shadowing20(const PxVec3& p, const Vertex* points, const Edge* edges, int edgesCount, bool& hasOnFacetPoint, Vertex& onFacetPoint) +{ + Vertex p1, p2; + int32_t stat = vfStatus20(p, points, edges, edgesCount, p1, p2); + hasOnFacetPoint = false; + if (stat != 0) + { + PxVec3 vc = p2.p - p1.p; + float t = 0; + t = (abs(vc.x) > abs(vc.y)) ? (p.x - p1.p.x) / vc.x : (p.y - p1.p.y) / vc.y; + t = (t < 0) ? 0 : t; + t = (t > 1) ? 1 : t; + + hasOnFacetPoint = true; + onFacetPoint.p.x = p.x; + onFacetPoint.p.y = p.y; + + onFacetPoint.p.z = t * vc.z + p1.p.z; + + vec2Lerp(p1.uv[0], p2.uv[0], onFacetPoint.uv[0], t); + vec3Lerp(p1.n, p2.n, onFacetPoint.n, t); + + if (onFacetPoint.p.z < p.z) + { + return stat; + } + } + return 0; +} + + +NV_FORCE_INLINE int32_t edgesCrossCheck(const PxVec3& eAs, const PxVec3& eAe, const PxVec3& eBs, const PxVec3& eBe) +{ + return shadowing01(eBs, eBe, eAe) - shadowing01(eBs, eBe, eAs) + shadowing10(eAs, eAe, eBe) - shadowing10(eAs, eAe, eBs); +} + +int32_t edgesIntersection(const Vertex& eAs, const Vertex& eAe, const Vertex& eBs, const Vertex& eBe, Vertex& intersectionA, Vertex& intersectionB, bool& hasPoints) +{ + int32_t status = edgesCrossCheck(eAs.p, eAe.p, eBs.p, eBe.p); + hasPoints = false; + if (status == 0) + return 0; + Vertex tempPoint; + + Vertex bShadowingPair[2]; + Vertex aShadowingPair[2]; + bool hasOnEdge = false; + int32_t shadowingType = shadowing10(eAs, eAe, eBs.p, tempPoint, hasOnEdge); + + bool aShadowing = false; + bool bShadowing = false; + + + if (shadowingType == 0 && hasOnEdge) + { + aShadowing = true; + aShadowingPair[0] = eBs; + aShadowingPair[1] = tempPoint; + } + else + { + if (shadowingType == 1 || shadowingType == -1) + { + bShadowing = true; + bShadowingPair[0] = eBs; + bShadowingPair[1] = tempPoint; + } + } + + shadowingType = shadowing10(eAs, eAe, eBe.p, tempPoint, hasOnEdge); + + if (shadowingType == 0 && !aShadowing && hasOnEdge) + { + aShadowing = true; + aShadowingPair[0] = eBe; + aShadowingPair[1] = tempPoint; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[0] = eBe; + bShadowingPair[1] = tempPoint; + } + } + shadowingType = shadowing01(eBs, eBe, eAe.p, tempPoint, hasOnEdge); + + if (shadowingType == 0 && !aShadowing && hasOnEdge) + { + aShadowing = true; + aShadowingPair[1] = eAe; + aShadowingPair[0] = tempPoint; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[1] = eAe; + bShadowingPair[0] = tempPoint; + } + } + + shadowingType = shadowing01(eBs, eBe, eAs.p, tempPoint, hasOnEdge); + + if (shadowingType == 0 && !aShadowing && hasOnEdge) + { + aShadowing = true; + aShadowingPair[1] = eAs; + aShadowingPair[0] = tempPoint; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[1] = eAs; + bShadowingPair[0] = tempPoint; + } + } + float deltaPlus = bShadowingPair[0].p.y - bShadowingPair[1].p.y; + float deltaMinus = aShadowingPair[0].p.y - aShadowingPair[1].p.y; + float div = 0; + if (deltaPlus > 0) + div = deltaPlus / (deltaPlus - deltaMinus); + else + div = 0; + + intersectionA.p = bShadowingPair[1].p - div * (bShadowingPair[1].p - aShadowingPair[1].p); + intersectionA.n = bShadowingPair[1].n - div * (bShadowingPair[1].n - aShadowingPair[1].n); + intersectionA.uv[0] = bShadowingPair[1].uv[0] - (bShadowingPair[1].uv[0] - aShadowingPair[1].uv[0]) * div; + intersectionB.p = intersectionA.p; + intersectionB.p.z = bShadowingPair[0].p.z - div * (bShadowingPair[0].p.z - aShadowingPair[0].p.z); + intersectionB.n = bShadowingPair[0].n - div * (bShadowingPair[0].n - aShadowingPair[0].n); + intersectionB.uv[0] = bShadowingPair[0].uv[0] - (bShadowingPair[0].uv[0] - aShadowingPair[0].uv[0]) * div; + + hasPoints = true; + return status; +} + +NV_FORCE_INLINE int32_t edgeEdgeShadowing(const Vertex& eAs, const Vertex& eAe, const Vertex& eBs, const Vertex& eBe, Vertex& intersectionA, Vertex& intersectionB, bool& hasPoints) +{ + int32_t status = edgesIntersection(eAs, eAe, eBs, eBe, intersectionA, intersectionB, hasPoints); + if (intersectionB.p.z >= intersectionA.p.z) + { + return status; + } + return 0; +} + +int32_t edgeFacetIntersection12(const Vertex& edSt, const Vertex& edEnd, const Vertex* points, const Edge* edges, int edgesCount, Vertex& intersectionA, Vertex& intersectionB) +{ + int32_t status = 0; + Vertex p1, p2; + Vertex bShadowingPair[2]; + Vertex aShadowingPair[2]; + bool hasPoint = false; + int32_t shadowingType = shadowing02(edEnd.p, points, edges, edgesCount, hasPoint, p1); + status -= shadowingType; + bool aShadowing = false; + bool bShadowing = false; + + if (shadowingType == 0 && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = p1; + aShadowingPair[1] = edEnd; + } + else + { + if (shadowingType == 1 || shadowingType == -1) + { + bShadowing = true; + bShadowingPair[0] = p1; + bShadowingPair[1] = edEnd; + } + } + + shadowingType = shadowing02(edSt.p, points, edges, edgesCount, hasPoint, p1); + status += shadowingType; + if (shadowingType == 0 && !aShadowing && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = p1; + aShadowingPair[1] = edSt; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[0] = p1; + bShadowingPair[1] = edSt; + } + } + + for (int32_t ed = 0; ed < edgesCount; ++ed) + { + shadowingType = edgeEdgeShadowing(edSt, edEnd, points[edges[ed].s], points[edges[ed].e], p1, p2, hasPoint); + status -= shadowingType; + if (shadowingType == 0 && !aShadowing && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = p2; + aShadowingPair[1] = p1; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[0] = p2; + bShadowingPair[1] = p1; + } + } + } + if (status == 0) + { + return 0; + } + if (!bShadowing || !aShadowing) + { + return 0; + } + float deltaPlus = bShadowingPair[0].p.z - bShadowingPair[1].p.z; + float div = 0; + if (deltaPlus != 0) + { + float deltaMinus = aShadowingPair[0].p.z - aShadowingPair[1].p.z; + div = deltaPlus / (deltaPlus - deltaMinus); + } + intersectionA.p = bShadowingPair[1].p - div * (bShadowingPair[1].p - aShadowingPair[1].p); + intersectionA.n = bShadowingPair[1].n - div * (bShadowingPair[1].n - aShadowingPair[1].n); + intersectionA.uv[0] = bShadowingPair[1].uv[0] - (bShadowingPair[1].uv[0] - aShadowingPair[1].uv[0]) * div; + + intersectionB.p = intersectionA.p; + intersectionB.n = bShadowingPair[0].n - div * (bShadowingPair[0].n - aShadowingPair[0].n); + intersectionB.uv[0] = bShadowingPair[0].uv[0] - (bShadowingPair[0].uv[0] - aShadowingPair[0].uv[0]) * div; + + + return status; +} + + +int32_t edgeFacetIntersection21(const Vertex& edSt, const Vertex& edEnd, const Vertex* points, const Edge* edges, int edgesCount, Vertex& intersectionA, Vertex& intersectionB) +{ + int32_t status = 0; + Vertex p1, p2; + + Vertex bShadowingPair[2]; + Vertex aShadowingPair[2]; + bool hasPoint = false; + int32_t shadowingType = shadowing20(edEnd.p, points, edges, edgesCount, hasPoint, p1); + status = shadowingType; + bool aShadowing = false; + bool bShadowing = false; + if (shadowingType == 0 && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = edEnd; + aShadowingPair[1] = p1; + } + else + { + if (shadowingType == 1 || shadowingType == -1) + { + bShadowing = true; + bShadowingPair[0] = edEnd; + bShadowingPair[1] = p1; + } + } + + shadowingType = shadowing20(edSt.p, points, edges, edgesCount, hasPoint, p1); + status -= shadowingType; + if (shadowingType == 0 && !aShadowing && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = edSt; + aShadowingPair[1] = p1; + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[0] = edSt; + bShadowingPair[1] = p1; + } + } + + for (int32_t ed = 0; ed < edgesCount; ++ed) + { + shadowingType = edgeEdgeShadowing(points[edges[ed].s], points[edges[ed].e], edSt, edEnd, p1, p2, hasPoint); + status -= shadowingType; + if (shadowingType == 0) + { + if (!aShadowing && hasPoint) + { + aShadowing = true; + aShadowingPair[0] = p2; + aShadowingPair[1] = p1; + } + } + else + { + if ((shadowingType == 1 || shadowingType == -1) && !bShadowing) + { + bShadowing = true; + bShadowingPair[0] = p2; + bShadowingPair[1] = p1; + } + } + } + if (status == 0) + { + return 0; + } + if (!bShadowing || !aShadowing) + { + return 0; + } + float deltaPlus = bShadowingPair[0].p.z - bShadowingPair[1].p.z; + float div = 0; + if (deltaPlus != 0) + { + float deltaMinus = aShadowingPair[0].p.z - aShadowingPair[1].p.z; + div = deltaPlus / (deltaPlus - deltaMinus); + } + intersectionA.p = bShadowingPair[1].p - div * (bShadowingPair[1].p - aShadowingPair[1].p); + intersectionA.n = bShadowingPair[1].n - div * (bShadowingPair[1].n - aShadowingPair[1].n); + intersectionA.uv[0] = bShadowingPair[1].uv[0] - (bShadowingPair[1].uv[0] - aShadowingPair[1].uv[0]) * div; + + intersectionB.p = intersectionA.p; + intersectionB.n = bShadowingPair[0].n - div * (bShadowingPair[0].n - aShadowingPair[0].n); + intersectionB.uv[0] = bShadowingPair[0].uv[0] - (bShadowingPair[0].uv[0] - aShadowingPair[0].uv[0]) * div; + + return status; +} + +int32_t BooleanEvaluator::vertexMeshStatus03(const PxVec3& p, Mesh* mesh) +{ + int32_t status = 0; + Vertex pnt; + bool hasPoint = false; + mAcceleratorB->setState(p); + int32_t facet = mAcceleratorB->getNextFacet(); + while (facet != -1) + { + Edge* ed = mesh->getEdges() + mesh->getFacet(facet)->firstEdgeNumber; + status += shadowing02(p, mesh->getVertices(), ed, mesh->getFacet(facet)->edgesCount, hasPoint, pnt); + facet = mAcceleratorB->getNextFacet(); + } + + //for (int32_t facet = 0; facet < mesh->getFacetCount(); ++facet) + //{ + // Edge* ed = mesh->getEdges() + mesh->getFacet(facet)->firstEdgeNumber; + // status += shadowing02(p, mesh->getVertices(), ed, mesh->getFacet(facet)->edgesCount, hasPoint, pnt); + //} + + return status; +} + +int32_t BooleanEvaluator::vertexMeshStatus30(const PxVec3& p, Mesh* mesh) +{ + int32_t status = 0; + bool hasPoints = false; + Vertex point; + mAcceleratorA->setState(p); + int32_t facet = mAcceleratorA->getNextFacet(); + while ( facet != -1) + { + Edge* ed = mesh->getEdges() + mesh->getFacet(facet)->firstEdgeNumber; + status -= shadowing20(p, mesh->getVertices(), ed, mesh->getFacet(facet)->edgesCount, hasPoints, point); + facet = mAcceleratorA->getNextFacet(); + } + + //for (int32_t facet = 0; facet < mesh->getFacetCount(); ++facet) + //{ + // Edge* ed = mesh->getEdges() + mesh->getFacet(facet)->firstEdgeNumber; + // status -= shadowing20(p, mesh->getVertices(), ed, mesh->getFacet(facet)->edgesCount, hasPoints, point); + //} + return status; +} + +NV_FORCE_INLINE int32_t inclusionValue03(BooleanConf& conf, int32_t xValue) +{ + return conf.ca + conf.ci * xValue; +} + +NV_FORCE_INLINE int32_t inclusionValueEdgeFace(BooleanConf& conf, int32_t xValue) +{ + return conf.ci * xValue; +} + +NV_FORCE_INLINE int32_t inclusionValue30(BooleanConf& conf, int32_t xValue) +{ + return conf.cb + conf.ci * xValue; +} + +struct VertexComparator +{ + VertexComparator(PxVec3 base = PxVec3()) : basePoint(base) {}; + PxVec3 basePoint; + bool operator()(const Vertex& a, const Vertex& b) + { + return (b.p - a.p).dot(basePoint) > 0.0; + } +}; + +struct VertexPairComparator +{ + VertexPairComparator(PxVec3 base = PxVec3()) : basePoint(base) {}; + PxVec3 basePoint; + bool operator()(const std::pair<Vertex, Vertex>& a, const std::pair<Vertex, Vertex>& b) + { + return (b.first.p - a.first.p).dot(basePoint) > 0.0; + } +}; + +int32_t BooleanEvaluator::isPointContainedInMesh(Mesh* msh, const PxVec3& point) +{ + if (msh == nullptr) + { + return 0; + } + DummyAccelerator dmAccel(msh->getFacetCount()); + mAcceleratorA = &dmAccel; + return vertexMeshStatus30(point, msh); + +} + +int32_t BooleanEvaluator::isPointContainedInMesh(Mesh* msh, SpatialAccelerator* spAccel, const PxVec3& point) +{ + if (msh == nullptr) + { + return 0; + } + mAcceleratorA = spAccel; + return vertexMeshStatus30(point, msh); +} + + +bool shouldSwap(const PxVec3& a, const PxVec3& b) +{ + if (a.x < b.x) return false; + if (a.x > b.x) return true; + + if (a.y < b.y) return false; + if (a.y > b.y) return true; + + if (a.z < b.z) return false; + if (a.z > b.z) return true; + + return false; +} + +void BooleanEvaluator::buildFaceFaceIntersections(BooleanConf mode) +{ + int32_t statusValue = 0; + int32_t inclusionValue = 0; + + std::vector<std::pair<Vertex, Vertex> > retainedStarts; + std::vector<std::pair<Vertex, Vertex>> retainedEnds; + VertexPairComparator comp; + + Vertex newPointA; + Vertex newPointB; + + Vertex* meshAPoints = mMeshA->getVertices(); + Vertex* meshBPoints = mMeshB->getVertices(); + EdgeWithParent newEdge; + mEdgeFacetIntersectionData12.clear(); + mEdgeFacetIntersectionData21.clear(); + + mEdgeFacetIntersectionData12.resize(mMeshA->getFacetCount()); + mEdgeFacetIntersectionData21.resize(mMeshB->getFacetCount()); + + for (uint32_t facetB = 0; facetB < mMeshB->getFacetCount(); ++facetB) + { + mAcceleratorA->setState(mMeshB->getVertices(), mMeshB->getEdges(), *mMeshB->getFacet(facetB)); + int32_t facetA = mAcceleratorA->getNextFacet(); + while (facetA != -1) + { + Edge* facetBEdges = mMeshB->getEdges() + mMeshB->getFacet(facetB)->firstEdgeNumber; + Edge* facetAEdges = mMeshA->getEdges() + mMeshA->getFacet(facetA)->firstEdgeNumber; + Edge* fbe = facetBEdges; + Edge* fae = facetAEdges; + retainedStarts.clear(); + retainedEnds.clear(); + PxVec3 compositeEndPoint(0, 0, 0); + PxVec3 compositeStartPoint(0, 0, 0); + uint32_t facetAEdgeCount = mMeshA->getFacet(facetA)->edgesCount; + uint32_t facetBEdgeCount = mMeshB->getFacet(facetB)->edgesCount; + int32_t ic = 0; + for (uint32_t i = 0; i < facetAEdgeCount; ++i) + { + if (shouldSwap(meshAPoints[fae->e].p, meshAPoints[fae->s].p)) + { + statusValue = -edgeFacetIntersection12(meshAPoints[fae->e], meshAPoints[fae->s], mMeshB->getVertices(), facetBEdges, facetBEdgeCount, newPointA, newPointB); + } + else + { + statusValue = edgeFacetIntersection12(meshAPoints[fae->s], meshAPoints[fae->e], mMeshB->getVertices(), facetBEdges, facetBEdgeCount, newPointA, newPointB); + } + inclusionValue = -inclusionValueEdgeFace(mode, statusValue); + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEnds.push_back(std::make_pair(newPointA, newPointB)); + compositeEndPoint += newPointA.p; + } + mEdgeFacetIntersectionData12[facetA].push_back(EdgeFacetIntersectionData(i, statusValue, newPointA)); + } + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStarts.push_back(std::make_pair(newPointA, newPointB)); + compositeStartPoint += newPointA.p; + } + mEdgeFacetIntersectionData12[facetA].push_back(EdgeFacetIntersectionData(i, statusValue, newPointA)); + } + fae++; + } + for (uint32_t i = 0; i < facetBEdgeCount; ++i) + { + if (shouldSwap(meshBPoints[fbe->e].p, meshBPoints[fbe->s].p)) + { + statusValue = -edgeFacetIntersection21(meshBPoints[(fbe)->e], meshBPoints[(fbe)->s], mMeshA->getVertices(), facetAEdges, facetAEdgeCount, newPointA, newPointB); + } + else + { + statusValue = edgeFacetIntersection21(meshBPoints[(fbe)->s], meshBPoints[(fbe)->e], mMeshA->getVertices(), facetAEdges, facetAEdgeCount, newPointA, newPointB); + } + inclusionValue = inclusionValueEdgeFace(mode, statusValue); + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEnds.push_back(std::make_pair(newPointA, newPointB)); + compositeEndPoint += newPointB.p; + } + mEdgeFacetIntersectionData21[facetB].push_back(EdgeFacetIntersectionData( i, statusValue, newPointB)); + } + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStarts.push_back(std::make_pair(newPointA, newPointB)); + compositeStartPoint += newPointB.p; + } + mEdgeFacetIntersectionData21[facetB].push_back(EdgeFacetIntersectionData(i, statusValue, newPointB)); + } + fbe++; + } + if (retainedStarts.size() != retainedEnds.size()) + { + NVBLAST_LOG_ERROR(mLoggingCallback, "Not equal number of starting and ending vertices! Probably input mesh has open edges."); + return; + } + if (retainedStarts.size() > 1) + { + comp.basePoint = compositeEndPoint - compositeStartPoint; + std::sort(retainedStarts.begin(), retainedStarts.end(), comp); + std::sort(retainedEnds.begin(), retainedEnds.end(), comp); + } + for (uint32_t rv = 0; rv < retainedStarts.size(); ++rv) + { + newEdge.s = addIfNotExist(retainedStarts[rv].first); + newEdge.e = addIfNotExist(retainedEnds[rv].first); + newEdge.parent = facetA; + addEdgeIfValid(newEdge); + newEdge.parent = facetB + mMeshA->getFacetCount(); + newEdge.e = addIfNotExist(retainedStarts[rv].second); + newEdge.s = addIfNotExist(retainedEnds[rv].second); + addEdgeIfValid(newEdge); + } + facetA = mAcceleratorA->getNextFacet(); + } // while (*iter != -1) + + } // for (uint32_t facetB = 0; facetB < mMeshB->getFacetCount(); ++facetB) + + + +} + + +void BooleanEvaluator::buildFastFaceFaceIntersection(BooleanConf mode) +{ + int32_t statusValue = 0; + int32_t inclusionValue = 0; + + std::vector<std::pair<Vertex, Vertex> > retainedStarts; + std::vector<std::pair<Vertex, Vertex>> retainedEnds; + VertexPairComparator comp; + + Vertex newPointA; + Vertex newPointB; + + Vertex* meshAPoints = mMeshA->getVertices(); + EdgeWithParent newEdge; + + mEdgeFacetIntersectionData12.clear(); + mEdgeFacetIntersectionData21.clear(); + + mEdgeFacetIntersectionData12.resize(mMeshA->getFacetCount()); + mEdgeFacetIntersectionData21.resize(mMeshB->getFacetCount()); + + for (uint32_t facetA = 0; facetA < mMeshA->getFacetCount(); ++facetA) + { + Edge* facetAEdges = mMeshA->getEdges() + mMeshA->getFacet(facetA)->firstEdgeNumber; + int32_t facetB = 0; + Edge* facetBEdges = mMeshB->getEdges() + mMeshB->getFacet(facetB)->firstEdgeNumber; + Edge* fae = facetAEdges; + retainedStarts.clear(); + retainedEnds.clear(); + PxVec3 compositeEndPoint(0, 0, 0); + PxVec3 compositeStartPoint(0, 0, 0); + uint32_t facetAEdgeCount = mMeshA->getFacet(facetA)->edgesCount; + uint32_t facetBEdgeCount = mMeshB->getFacet(facetB)->edgesCount; + int32_t ic = 0; + for (uint32_t i = 0; i < facetAEdgeCount; ++i) + { + if (shouldSwap(meshAPoints[fae->e].p, meshAPoints[fae->s].p)) + { + statusValue = -edgeFacetIntersection12(meshAPoints[fae->e], meshAPoints[fae->s], mMeshB->getVertices(), facetBEdges, facetBEdgeCount, newPointA, newPointB); + } + else + { + statusValue = edgeFacetIntersection12(meshAPoints[fae->s], meshAPoints[fae->e], mMeshB->getVertices(), facetBEdges, facetBEdgeCount, newPointA, newPointB); + } + inclusionValue = -inclusionValueEdgeFace(mode, statusValue); + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEnds.push_back(std::make_pair(newPointA, newPointB)); + compositeEndPoint += newPointA.p; + } + mEdgeFacetIntersectionData12[facetA].push_back(EdgeFacetIntersectionData(i, statusValue, newPointA)); + } + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStarts.push_back(std::make_pair(newPointA, newPointB)); + compositeStartPoint += newPointA.p; + } + mEdgeFacetIntersectionData12[facetA].push_back(EdgeFacetIntersectionData(i, statusValue, newPointA)); + } + fae++; + } + if (retainedStarts.size() != retainedEnds.size()) + { + NVBLAST_LOG_ERROR(mLoggingCallback, "Not equal number of starting and ending vertices! Probably input mesh has open edges."); + return; + } + if (retainedStarts.size() > 1) + { + comp.basePoint = compositeEndPoint - compositeStartPoint; + std::sort(retainedStarts.begin(), retainedStarts.end(), comp); + std::sort(retainedEnds.begin(), retainedEnds.end(), comp); + } + for (uint32_t rv = 0; rv < retainedStarts.size(); ++rv) + { + newEdge.s = addIfNotExist(retainedStarts[rv].first); + newEdge.e = addIfNotExist(retainedEnds[rv].first); + newEdge.parent = facetA; + addEdgeIfValid(newEdge); + newEdge.parent = facetB + mMeshA->getFacetCount(); + newEdge.e = addIfNotExist(retainedStarts[rv].second); + newEdge.s = addIfNotExist(retainedEnds[rv].second); + addEdgeIfValid(newEdge); + } + } + +} + + + +void BooleanEvaluator::collectRetainedPartsFromA(BooleanConf mode) +{ + + int32_t statusValue = 0; + int32_t inclusionValue = 0; + Vertex* vertices = mMeshA->getVertices(); + Vertex newPoint; + VertexComparator comp; + PxBounds3& bMeshBoudning = mMeshB->getBoundingBox(); + Edge* facetEdges = mMeshA->getEdges(); + std::vector<Vertex> retainedStartVertices; + std::vector<Vertex> retainedEndVertices; + retainedStartVertices.reserve(255); + retainedEndVertices.reserve(255); + int32_t ic = 0; + for (uint32_t facetId = 0; facetId < mMeshA->getFacetCount(); ++facetId) + { + retainedStartVertices.clear(); + retainedEndVertices.clear(); + for (uint32_t i = 0; i < mMeshA->getFacet(facetId)->edgesCount; ++i) + { + PxVec3 compositeEndPoint(0, 0, 0); + PxVec3 compositeStartPoint(0, 0, 0); + + int32_t lastPos = static_cast<int32_t>(retainedEndVertices.size()); + /* Test start and end point of edge against mesh */ + if (bMeshBoudning.contains(vertices[facetEdges->s].p)) + { + statusValue = vertexMeshStatus03(vertices[facetEdges->s].p, mMeshB); + } + else + { + statusValue = 0; + } + inclusionValue = -inclusionValue03(mode, statusValue); + + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(vertices[facetEdges->s]); + compositeEndPoint += vertices[facetEdges->s].p; + } + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(vertices[facetEdges->s]); + compositeStartPoint += vertices[facetEdges->s].p; + } + } + } + + if (bMeshBoudning.contains(vertices[facetEdges->e].p)) + { + statusValue = vertexMeshStatus03(vertices[facetEdges->e].p, mMeshB); + } + else + { + statusValue = 0; + } + inclusionValue = inclusionValue03(mode, statusValue); + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(vertices[facetEdges->e]); + compositeEndPoint += vertices[facetEdges->e].p; + } + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(vertices[facetEdges->e]); + compositeStartPoint += vertices[facetEdges->e].p; + } + } + } + /* Test edge intersection with mesh*/ + for (uint32_t intrs = 0; intrs < mEdgeFacetIntersectionData12[facetId].size(); ++intrs) + { + EdgeFacetIntersectionData& intr = mEdgeFacetIntersectionData12[facetId][intrs]; + if (intr.edId != (int32_t)i) + continue; + newPoint = intr.intersectionPoint; + inclusionValue = inclusionValueEdgeFace(mode, intr.intersectionType); + + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(newPoint); + compositeEndPoint += newPoint.p; + } + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(newPoint); + compositeStartPoint += newPoint.p; + } + } + } + } + facetEdges++; + if (retainedStartVertices.size() != retainedEndVertices.size()) + { + NVBLAST_LOG_ERROR(mLoggingCallback, "Not equal number of starting and ending vertices! Probably input mesh has open edges."); + return; + } + if (retainedEndVertices.size() > 1) + { + comp.basePoint = compositeEndPoint - compositeStartPoint; + std::sort(retainedStartVertices.begin() + lastPos, retainedStartVertices.end(), comp); + std::sort(retainedEndVertices.begin() + lastPos, retainedEndVertices.end(), comp); + } + } + + + EdgeWithParent newEdge; + for (uint32_t rv = 0; rv < retainedStartVertices.size(); ++rv) + { + newEdge.s = addIfNotExist(retainedStartVertices[rv]); + newEdge.e = addIfNotExist(retainedEndVertices[rv]); + newEdge.parent = facetId; + addEdgeIfValid(newEdge); + } + } + + return; +} + +void BooleanEvaluator::collectRetainedPartsFromB(BooleanConf mode) +{ + int32_t statusValue = 0; + int32_t inclusionValue = 0; + Vertex* vertices = mMeshB->getVertices(); + Vertex newPoint; + VertexComparator comp; + PxBounds3& aMeshBoudning = mMeshA->getBoundingBox(); + Edge* facetEdges = mMeshB->getEdges(); + std::vector<Vertex> retainedStartVertices; + std::vector<Vertex> retainedEndVertices; + retainedStartVertices.reserve(255); + retainedEndVertices.reserve(255); + int32_t ic = 0; + for (uint32_t facetId = 0; facetId < mMeshB->getFacetCount(); ++facetId) + { + retainedStartVertices.clear(); + retainedEndVertices.clear(); + for (uint32_t i = 0; i < mMeshB->getFacet(facetId)->edgesCount; ++i) + { + PxVec3 compositeEndPoint(0, 0, 0); + PxVec3 compositeStartPoint(0, 0, 0); + int32_t lastPos = static_cast<int32_t>(retainedEndVertices.size()); + if (aMeshBoudning.contains(vertices[facetEdges->s].p)) + { + statusValue = vertexMeshStatus30(vertices[facetEdges->s].p, mMeshA); + } + else + { + statusValue = 0; + } + inclusionValue = -inclusionValue30(mode, statusValue); + + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(vertices[facetEdges->s]); + compositeEndPoint += vertices[facetEdges->s].p; + } + + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(vertices[facetEdges->s]); + compositeStartPoint += vertices[facetEdges->s].p; + } + + } + } + + if (aMeshBoudning.contains(vertices[facetEdges->e].p)) + { + statusValue = vertexMeshStatus30(vertices[facetEdges->e].p, mMeshA); + } + else + { + statusValue = 0; + } + inclusionValue = inclusionValue30(mode, statusValue); + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(vertices[facetEdges->e]); + compositeEndPoint += vertices[facetEdges->e].p; + } + + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(vertices[facetEdges->e]); + compositeStartPoint += vertices[facetEdges->e].p; + } + + } + } + for (uint32_t intrs = 0; intrs < mEdgeFacetIntersectionData21[facetId].size(); ++intrs) + { + EdgeFacetIntersectionData& intr = mEdgeFacetIntersectionData21[facetId][intrs]; + if (intr.edId != (int32_t)i) + continue; + newPoint = intr.intersectionPoint; + inclusionValue = inclusionValueEdgeFace(mode, intr.intersectionType); + + if (inclusionValue > 0) + { + for (ic = 0; ic < inclusionValue; ++ic) + { + retainedEndVertices.push_back(newPoint); + compositeEndPoint += newPoint.p; + } + } + else + { + if (inclusionValue < 0) + { + for (ic = 0; ic < -inclusionValue; ++ic) + { + retainedStartVertices.push_back(newPoint); + compositeStartPoint += newPoint.p; + } + } + } + } + facetEdges++; + if (retainedStartVertices.size() != retainedEndVertices.size()) + { + NVBLAST_LOG_ERROR(mLoggingCallback, "Not equal number of starting and ending vertices! Probably input mesh has open edges."); + return; + } + if (retainedEndVertices.size() - lastPos > 1) + { + comp.basePoint = compositeEndPoint - compositeStartPoint; + std::sort(retainedStartVertices.begin() + lastPos, retainedStartVertices.end(), comp); + std::sort(retainedEndVertices.begin() + lastPos, retainedEndVertices.end(), comp); + } + } + EdgeWithParent newEdge; + for (uint32_t rv = 0; rv < retainedStartVertices.size(); ++rv) + { + newEdge.s = addIfNotExist(retainedStartVertices[rv]); + newEdge.e = addIfNotExist(retainedEndVertices[rv]); + newEdge.parent = facetId + mMeshA->getFacetCount(); + addEdgeIfValid(newEdge); + } + } + return; +} + +bool EdgeWithParentSortComp(const EdgeWithParent& a, const EdgeWithParent& b) +{ + return a.parent < b.parent; +} + + +void BooleanEvaluator::performBoolean(Mesh* meshA, Mesh* meshB, SpatialAccelerator* spAccelA, SpatialAccelerator* spAccelB, BooleanConf mode) +{ + reset(); + mMeshA = meshA; + mMeshB = meshB; + mAcceleratorA = spAccelA; + mAcceleratorB = spAccelB; + buildFaceFaceIntersections(mode); + collectRetainedPartsFromA(mode); + collectRetainedPartsFromB(mode); + mAcceleratorA = nullptr; + mAcceleratorB = nullptr; +} + +void BooleanEvaluator::performBoolean(Mesh* meshA, Mesh* meshB, BooleanConf mode) +{ + reset(); + mMeshA = meshA; + mMeshB = meshB; + DummyAccelerator ac = DummyAccelerator(mMeshA->getFacetCount()); + DummyAccelerator bc = DummyAccelerator(mMeshB->getFacetCount()); + performBoolean(meshA, meshB, &ac, &bc, mode); +} + + +void BooleanEvaluator::performFastCutting(Mesh* meshA, Mesh* meshB, SpatialAccelerator* spAccelA, SpatialAccelerator* spAccelB, BooleanConf mode) +{ + reset(); + mMeshA = meshA; + mMeshB = meshB; + mAcceleratorA = spAccelA; + mAcceleratorB = spAccelB; + buildFastFaceFaceIntersection(mode); + collectRetainedPartsFromA(mode); + mAcceleratorA = nullptr; + mAcceleratorB = nullptr; +} + +void BooleanEvaluator::performFastCutting(Mesh* meshA, Mesh* meshB, BooleanConf mode) +{ + reset(); + mMeshA = meshA; + mMeshB = meshB; + DummyAccelerator ac = DummyAccelerator(mMeshA->getFacetCount()); + DummyAccelerator bc = DummyAccelerator(mMeshB->getFacetCount()); + performFastCutting(meshA, meshB, &ac, &bc, mode); +} + + + + +BooleanEvaluator::BooleanEvaluator(NvBlastLog loggingCallback) +{ + mMeshA = nullptr; + mMeshB = nullptr; + mAcceleratorA = nullptr; + mAcceleratorB = nullptr; + mLoggingCallback = loggingCallback; +} +BooleanEvaluator::~BooleanEvaluator() +{ + reset(); +} + + + +Mesh* BooleanEvaluator::createNewMesh() +{ + if (mEdgeAggregate.size() == 0) + { + return nullptr; + } + std::sort(mEdgeAggregate.begin(), mEdgeAggregate.end(), EdgeWithParentSortComp); + std::vector<Facet> newFacets; + std::vector<Edge> newEdges(mEdgeAggregate.size()); + int32_t lastPos = 0; + int32_t lastParent = mEdgeAggregate[0].parent; + uint32_t collected = 0; + int32_t userData = 0; + for (uint32_t i = 0; i < mEdgeAggregate.size(); ++i) + { + if (mEdgeAggregate[i].parent != lastParent) + { + if (lastParent < (int32_t)mMeshA->getFacetCount()) + { + userData = mMeshA->getFacet(lastParent)->userData; + } + else + { + userData = mMeshB->getFacet(lastParent - mMeshA->getFacetCount())->userData; + } + newFacets.push_back(Facet(lastPos, collected, userData)); + lastPos = i; + lastParent = mEdgeAggregate[i].parent; + collected = 0; + } + collected++; + newEdges[i].s = mEdgeAggregate[i].s; + newEdges[i].e = mEdgeAggregate[i].e; + } + int32_t pr = lastParent - mMeshA->getFacetCount(); + if (lastParent < (int32_t)mMeshA->getFacetCount()) + { + userData = mMeshA->getFacet(lastParent)->userData; + } + else + { + userData = mMeshB->getFacet(pr)->userData; + } + newFacets.push_back(Facet(lastPos, collected, userData)); + return new Mesh(&mVerticesAggregate[0], &newEdges[0], &newFacets[0], static_cast<uint32_t>(mVerticesAggregate.size()), static_cast<uint32_t>(mEdgeAggregate.size()), static_cast<uint32_t>(newFacets.size())); +} + +void BooleanEvaluator::reset() +{ + mMeshA = nullptr; + mMeshB = nullptr; + mAcceleratorA = nullptr; + mAcceleratorB = nullptr; + mEdgeAggregate.clear(); + mVerticesAggregate.clear(); + mEdgeFacetIntersectionData12.clear(); + mEdgeFacetIntersectionData21.clear(); +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.h new file mode 100644 index 0000000..0b0b73a --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringBooleanTool.h @@ -0,0 +1,197 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGBOOLEANTOOL_H +#define NVBLASTEXTAUTHORINGBOOLEANTOOL_H + +#include "NvBlastExtAuthoringTypes.h" +#include "NvBlastExtAuthoringInternalCommon.h" +#include <vector> +#include <map> +#include "NvBlastTypes.h" + + +namespace Nv +{ +namespace Blast +{ + +class Mesh; + +/** + Boolean tool config, used to perform different operations: UNION, INTERSECTION, DIFFERENCE +*/ +struct BooleanConf +{ + int32_t ca, cb, ci; + BooleanConf(int32_t a, int32_t b, int32_t c) : ca(a), cb(b), ci(c) + { + } +}; + + +namespace BooleanConfigurations +{ + /** + Creates boolean tool configuration to perform intersection of meshes A and B. + */ +inline BooleanConf BOOLEAN_INTERSECION() +{ + return BooleanConf(0, 0, 1); +} + +/** + Creates boolean tool configuration to perform union of meshes A and B. +*/ +inline BooleanConf BOOLEAN_UNION() +{ + return BooleanConf(1, 1, -1); +} +/** + Creates boolean tool configuration to perform difference of meshes(A - B). +*/ +inline BooleanConf BOOLEAN_DIFFERENCE() +{ + return BooleanConf(1, 0, -1); +} +} + +/** + Structure which holds information about intersection facet with edge. +*/ +struct EdgeFacetIntersectionData +{ + int32_t edId; + int32_t intersectionType; + Vertex intersectionPoint; + EdgeFacetIntersectionData(int32_t edId, int32_t intersType, Vertex& inters) : edId(edId), intersectionType(intersType), intersectionPoint(inters) + { } + EdgeFacetIntersectionData(int32_t edId) : edId(edId) + { } + bool operator<(const EdgeFacetIntersectionData& b) const + { + return edId < b.edId; + } +}; + + +class SpatialAccelerator; + +/** + Tool for performing boolean operations on polygonal meshes. + Tool supports only closed meshes. Performing boolean on meshes with holes can lead to unexpected behavior, e.g. holes in result geometry. +*/ +class BooleanEvaluator +{ + +public: + BooleanEvaluator(NvBlastLog logCallback = nullptr); + ~BooleanEvaluator(); + + /** + Perform boolean operation on two polygonal meshes (A and B). + \param[in] meshA Mesh A + \param[in] meshB Mesh B + \param[in] spAccelA Acceleration structure for mesh A + \param[in] spAccelB Acceleration structure for mesh B + \param[in] mode Boolean operation type + */ + void performBoolean(Mesh* meshA, Mesh* meshB, SpatialAccelerator* spAccelA, SpatialAccelerator* spAccelB, BooleanConf mode); + + /** + Perform boolean operation on two polygonal meshes (A and B). + \param[in] meshA Mesh A + \param[in] meshB Mesh B + \param[in] mode Boolean operation type + */ + void performBoolean(Mesh* meshA, Mesh* meshB, BooleanConf mode); + + /** + Perform cutting of mesh with some large box, which represents cutting plane. This method skips part of intersetion computations, so + should be used ONLY with cutting box, received from getBigBox(...) method from NvBlastExtAuthoringMesh.h. For cutting use only BOOLEAN_INTERSECTION or BOOLEAN_DIFFERENCE mode. + \param[in] meshA Mesh A + \param[in] meshB Cutting box + \param[in] spAccelA Acceleration structure for mesh A + \param[in] spAccelB Acceleration structure for cutting box + \param[in] mode Boolean operation type + */ + void performFastCutting(Mesh* meshA, Mesh* meshB, SpatialAccelerator* spAccelA, SpatialAccelerator* spAccelB, BooleanConf mode); + + /** + Perform cutting of mesh with some large box, which represents cutting plane. This method skips part of intersetion computations, so + should be used ONLY with cutting box, received from getBigBox(...) method from NvBlastExtAuthoringMesh.h. For cutting use only BOOLEAN_INTERSECTION or BOOLEAN_DIFFERENCE mode. + \param[in] meshA Mesh A + \param[in] meshB Cutting box + \param[in] mode Boolean operation type + */ + void performFastCutting(Mesh* meshA, Mesh* meshB, BooleanConf mode); + + /** + Test whether point contained in mesh. + \param[in] mesh Mesh geometry + \param[in] point Point which should be tested + \return not 0 if point is inside of mesh + */ + int32_t isPointContainedInMesh(Mesh* mesh, const physx::PxVec3& point); + /** + Test whether point contained in mesh. + \param[in] mesh Mesh geometry + \param[in] spAccel Acceleration structure for mesh + \param[in] point Point which should be tested + \return not 0 if point is inside of mesh + */ + int32_t isPointContainedInMesh(Mesh* mesh, SpatialAccelerator* spAccel, const physx::PxVec3& point); + + + /** + Generates result polygon mesh after performing boolean operation. + \return If not nullptr - result mesh geometry. + */ + Mesh* createNewMesh(); + + /** + Reset tool state. + */ + void reset(); + +private: + + void buildFaceFaceIntersections(BooleanConf); + void buildFastFaceFaceIntersection(BooleanConf); + void collectRetainedPartsFromA(BooleanConf mode); + void collectRetainedPartsFromB(BooleanConf mode); + + int32_t addIfNotExist(Vertex& p); + void addEdgeIfValid(EdgeWithParent& ed); +private: + + int32_t vertexMeshStatus03(const physx::PxVec3& p, Mesh* mesh); + int32_t vertexMeshStatus30(const physx::PxVec3& p, Mesh* mesh); + + Mesh* mMeshA; + Mesh* mMeshB; + + SpatialAccelerator* mAcceleratorA; + SpatialAccelerator* mAcceleratorB; + + std::vector<EdgeWithParent> mEdgeAggregate; + std::vector<Vertex> mVerticesAggregate; + + std::vector<std::vector<EdgeFacetIntersectionData> > mEdgeFacetIntersectionData12; + std::vector<std::vector<EdgeFacetIntersectionData> > mEdgeFacetIntersectionData21; + + NvBlastLog mLoggingCallback; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTAUTHORINGBOOLEANTOOL_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringCollisionBuilder.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringCollisionBuilder.cpp new file mode 100644 index 0000000..becdce9 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringCollisionBuilder.cpp @@ -0,0 +1,279 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtAuthoringCollisionBuilder.h" +#include <PxConvexMesh.h> +#include <PxVec3.h> +#include <PxBounds3.h> +#include "PxPhysics.h" +#include "cooking/PxCooking.h" +#include <NvBlastExtApexSharedParts.h> +#include <NvBlastExtAuthoringInternalCommon.h> + +#include <NvBlastExtAuthoringBooleanTool.h> +#include <NvBlastExtAuthoringMesh.h> + +using namespace physx; + +namespace Nv +{ +namespace Blast +{ + +void ConvexMeshBuilder::buildCollisionGeometry(const std::vector<PxVec3>& vData, CollisionHull& output) +{ + std::vector<physx::PxVec3> vertexData = vData; + + PxConvexMeshDesc convexMeshDescr; + PxConvexMesh* resultConvexMesh; + PxBounds3 bounds; + // Scale chunk to unit cube size, to avoid numerical errors + bounds.setEmpty(); + for (uint32_t i = 0; i < vertexData.size(); ++i) + { + bounds.include(vertexData[i]); + } + PxVec3 bbCenter = bounds.getCenter(); + float scale = PxMax(PxAbs(bounds.getExtents(0)), PxMax(PxAbs(bounds.getExtents(1)), PxAbs(bounds.getExtents(2)))); + for (uint32_t i = 0; i < vertexData.size(); ++i) + { + vertexData[i] = vertexData[i] - bbCenter; + vertexData[i] *= (1.0f / scale); + } + bounds.setEmpty(); + for (uint32_t i = 0; i < vertexData.size(); ++i) + { + bounds.include(vertexData[i]); + } + convexMeshDescr.points.data = vertexData.data(); + convexMeshDescr.points.stride = sizeof(PxVec3); + convexMeshDescr.points.count = (uint32_t)vertexData.size(); + convexMeshDescr.flags = PxConvexFlag::eCOMPUTE_CONVEX; + resultConvexMesh = mCooking->createConvexMesh(convexMeshDescr, *mInsertionCallback); + if (!resultConvexMesh) + { + vertexData.clear(); + vertexData.push_back(bounds.minimum); + vertexData.push_back(PxVec3(bounds.minimum.x, bounds.maximum.y, bounds.minimum.z)); + vertexData.push_back(PxVec3(bounds.maximum.x, bounds.maximum.y, bounds.minimum.z)); + vertexData.push_back(PxVec3(bounds.maximum.x, bounds.minimum.y, bounds.minimum.z)); + vertexData.push_back(PxVec3(bounds.minimum.x, bounds.minimum.y, bounds.maximum.z)); + vertexData.push_back(PxVec3(bounds.minimum.x, bounds.maximum.y, bounds.maximum.z)); + vertexData.push_back(PxVec3(bounds.maximum.x, bounds.maximum.y, bounds.maximum.z)); + vertexData.push_back(PxVec3(bounds.maximum.x, bounds.minimum.y, bounds.maximum.z)); + convexMeshDescr.points.data = vertexData.data(); + convexMeshDescr.points.count = (uint32_t)vertexData.size(); + resultConvexMesh = mCooking->createConvexMesh(convexMeshDescr, *mInsertionCallback); + } + output.polygonData.resize(resultConvexMesh->getNbPolygons()); + output.points.resize(resultConvexMesh->getNbVertices()); + int32_t indicesCount = 0; + PxHullPolygon hPoly; + for (uint32_t i = 0; i < resultConvexMesh->getNbPolygons(); ++i) + { + CollisionHull::HullPolygon& pd = output.polygonData[i]; + resultConvexMesh->getPolygonData(i, hPoly); + pd.mIndexBase = hPoly.mIndexBase; + pd.mNbVerts = hPoly.mNbVerts; + pd.mPlane[0] = hPoly.mPlane[0]; + pd.mPlane[1] = hPoly.mPlane[1]; + pd.mPlane[2] = hPoly.mPlane[2]; + pd.mPlane[3] = hPoly.mPlane[3]; + + pd.mPlane[0] /= scale; + pd.mPlane[1] /= scale; + pd.mPlane[2] /= scale; + pd.mPlane[3] -= (pd.mPlane[0] * bbCenter.x + pd.mPlane[1] * bbCenter.y + pd.mPlane[2] * bbCenter.z); + float length = sqrt(pd.mPlane[0] * pd.mPlane[0] + pd.mPlane[1] * pd.mPlane[1] + pd.mPlane[2] * pd.mPlane[2]); + pd.mPlane[0] /= length; + pd.mPlane[1] /= length; + pd.mPlane[2] /= length; + pd.mPlane[3] /= length; + indicesCount = PxMax(indicesCount, pd.mIndexBase + pd.mNbVerts); + } + output.indices.resize(indicesCount); + for (uint32_t i = 0; i < resultConvexMesh->getNbVertices(); ++i) + { + PxVec3 p = resultConvexMesh->getVertices()[i] * scale + bbCenter; + output.points[i] = p; + } + for (int32_t i = 0; i < indicesCount; ++i) + { + output.indices[i] = resultConvexMesh->getIndexBuffer()[i]; + } + resultConvexMesh->release(); +} + +void ConvexMeshBuilder::trimCollisionGeometry(std::vector<CollisionHull>& in, const std::vector<uint32_t>& chunkDepth) +{ + std::vector<std::vector<PxPlane> > chunkMidplanes(in.size()); + std::vector<PxVec3> centers(in.size()); + std::vector<PxBounds3> hullsBounds(in.size()); + for (uint32_t i = 0; i < in.size(); ++i) + { + hullsBounds[i].setEmpty(); + centers[i] = PxVec3(0, 0, 0); + for (uint32_t p = 0; p < in[i].points.size(); ++p) + { + centers[i] += in[i].points[p]; + hullsBounds[i].include(in[i].points[p]); + } + centers[i] = hullsBounds[i].getCenter(); + } + + Separation params; + for (uint32_t hull = 0; hull < in.size(); ++hull) + { + for (uint32_t hull2 = hull + 1; hull2 < in.size(); ++hull2) + { + if (chunkDepth[hull] != chunkDepth[hull2]) + { + continue; + } + if (importerHullsInProximityApexFree(in[hull].points, hullsBounds[hull], PxTransform(PxIdentity), PxVec3(1, 1, 1), + in[hull2].points, hullsBounds[hull2], PxTransform(PxIdentity), PxVec3(1, 1, 1), 0.0, ¶ms) == false) + { + continue; + } + PxVec3 c1 = centers[hull]; + PxVec3 c2 = centers[hull2]; + float d = FLT_MAX; + PxVec3 n1; + PxVec3 n2; + for (uint32_t p = 0; p < in[hull].points.size(); ++p) + { + float ld = (in[hull].points[p] - c2).magnitude(); + if (ld < d) + { + n1 = in[hull].points[p]; + d = ld; + } + } + d = FLT_MAX; + for (uint32_t p = 0; p < in[hull2].points.size(); ++p) + { + float ld = (in[hull2].points[p] - c1).magnitude(); + if (ld < d) + { + n2 = in[hull2].points[p]; + d = ld; + } + } + + PxVec3 dir = c2 - c1; + + PxPlane pl = PxPlane((n1 + n2) * 0.5, dir.getNormalized()); + chunkMidplanes[hull].push_back(pl); + PxPlane pl2 = PxPlane((n1 + n2) * 0.5, -dir.getNormalized()); + chunkMidplanes[hull2].push_back(pl2); + } + } + std::vector<PxVec3> hPoints; + for (uint32_t i = 0; i < in.size(); ++i) + { + std::vector<Facet> facets; + std::vector<Vertex> vertices; + std::vector<Edge> edges; + for (uint32_t fc = 0; fc < in[i].polygonData.size(); ++fc) + { + Facet nFc; + nFc.firstEdgeNumber = edges.size(); + uint32_t n = in[i].polygonData[fc].mNbVerts; + for (uint32_t ed = 0; ed < n; ++ed) + { + uint32_t vr1 = in[i].indices[(ed) + in[i].polygonData[fc].mIndexBase]; + uint32_t vr2 = in[i].indices[(ed + 1) % n + in[i].polygonData[fc].mIndexBase]; + edges.push_back(Edge(vr1, vr2)); + } + nFc.edgesCount = n; + facets.push_back(nFc); + } + vertices.resize(in[i].points.size()); + for (uint32_t vr = 0; vr < in[i].points.size(); ++vr) + { + vertices[vr].p = in[i].points[vr]; + } + Mesh* hullMesh = new Mesh(vertices.data(), edges.data(), facets.data(), vertices.size(), edges.size(), facets.size()); + BooleanEvaluator evl; + Mesh* cuttingMesh = getCuttingBox(PxVec3(0, 0, 0), PxVec3(0, 0, 1), 40, 0); + for (uint32_t p = 0; p < chunkMidplanes[i].size(); ++p) + { + PxPlane& pl = chunkMidplanes[i][p]; + setCuttingBox(pl.pointInPlane(), pl.n.getNormalized(), cuttingMesh, 60, 0); + evl.performFastCutting(hullMesh, cuttingMesh, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + Mesh* result = evl.createNewMesh(); + if (result == nullptr) + { + break; + } + delete hullMesh; + hullMesh = result; + } + delete cuttingMesh; + if (hullMesh == nullptr) + { + continue; + } + hPoints.clear(); + hPoints.resize(hullMesh->getVerticesCount()); + for (uint32_t v = 0; v < hullMesh->getVerticesCount(); ++v) + { + hPoints[v] = hullMesh->getVertices()[v].p; + } + delete hullMesh; + buildCollisionGeometry(hPoints, in[i]); + } +} + + +PxConvexMesh* ConvexMeshBuilder::buildConvexMesh(std::vector<PxVec3>& vertexData) +{ + CollisionHull hull; + buildCollisionGeometry(vertexData, hull); + + PxConvexMeshDesc convexMeshDescr; + convexMeshDescr.indices.data = hull.indices.data(); + convexMeshDescr.indices.count = (uint32_t)hull.indices.size(); + convexMeshDescr.indices.stride = sizeof(uint32_t); + + convexMeshDescr.points.data = hull.points.data(); + convexMeshDescr.points.count = (uint32_t)hull.points.size(); + convexMeshDescr.points.stride = sizeof(PxVec3); + + convexMeshDescr.polygons.data = hull.polygonData.data(); + convexMeshDescr.polygons.count = (uint32_t)hull.polygonData.size(); + convexMeshDescr.polygons.stride = sizeof(PxHullPolygon); + + PxConvexMesh* convexMesh = mCooking->createConvexMesh(convexMeshDescr, *mInsertionCallback); + return convexMesh; +} + +PxConvexMesh* ConvexMeshBuilder::buildConvexMesh(CollisionHull& hull) +{ + PxConvexMeshDesc convexMeshDescr; + convexMeshDescr.indices.data = hull.indices.data(); + convexMeshDescr.indices.count = (uint32_t)hull.indices.size(); + convexMeshDescr.indices.stride = sizeof(uint32_t); + + convexMeshDescr.points.data = hull.points.data(); + convexMeshDescr.points.count = (uint32_t)hull.points.size(); + convexMeshDescr.points.stride = sizeof(PxVec3); + + convexMeshDescr.polygons.data = hull.polygonData.data(); + convexMeshDescr.polygons.count = (uint32_t)hull.polygonData.size(); + convexMeshDescr.polygons.stride = sizeof(PxHullPolygon); + + PxConvexMesh* convexMesh = mCooking->createConvexMesh(convexMeshDescr, *mInsertionCallback); + return convexMesh; +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringFractureTool.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringFractureTool.cpp new file mode 100644 index 0000000..48830fe --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringFractureTool.cpp @@ -0,0 +1,1510 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtAuthoringFractureTool.h" +// This warning arises when using some stl containers with older versions of VC +// c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1826): warning C4702: unreachable code +#if NV_VC && NV_VC < 14 +#pragma warning(disable : 4702) +#endif +#include <queue> +#include <vector> +#include "NvBlastExtAuthoringVSA.h" +#include <float.h> +#include "NvBlastExtAuthoringTriangulator.h" +#include "NvBlastExtAuthoringBooleanTool.h" +#include "NvBlastExtAuthoringAccelerator.h" +#include "NvBlast.h" +#include "NvBlastExtAuthoringPerlinNoise.h" +#include <NvBlastAssert.h> +using namespace physx; + +#define DEFAULT_BB_ACCELARATOR_RES 10 + +namespace Nv +{ +namespace Blast +{ + +struct Halfspace_partitioning : public VSA::VS3D_Halfspace_Set +{ + std::vector<physx::PxPlane> planes; + VSA::real farthest_halfspace(VSA::real plane[4], const VSA::real point[4]) + { + float biggest_d = -FLT_MAX; + for (uint32_t i = 0; i < planes.size(); ++i) + { + float d = planes[i].n.x * point[0] + planes[i].n.y * point[1] + planes[i].n.z * point[2] + planes[i].d * point[3]; + if (d > biggest_d) + { + biggest_d = d; + plane[0] = planes[i].n.x; + plane[1] = planes[i].n.y; + plane[2] = planes[i].n.z; + plane[3] = planes[i].d; + } + } + return biggest_d; + }; +}; + + +void findCellBasePlanes(const std::vector<PxVec3>& sites, std::vector<std::vector<int32_t> >& neighboors) +{ + Halfspace_partitioning prt; + std::vector<physx::PxPlane>& planes = prt.planes; + neighboors.resize(sites.size()); + for (uint32_t cellId = 0; cellId + 1 < sites.size(); ++cellId) + { + planes.clear(); + planes.resize(sites.size() - 1 - cellId); + std::vector<PxVec3> midpoints(sites.size() - 1); + int32_t collected = 0; + + for (uint32_t i = cellId + 1; i < sites.size(); ++i) + { + PxVec3 midpoint = 0.5 * (sites[i] + sites[cellId]); + PxVec3 direction = (sites[i] - sites[cellId]).getNormalized(); + planes[collected].n = direction; + planes[collected].d = -direction.dot(midpoint); + midpoints[collected] = midpoint; + ++collected; + } + for (uint32_t i = 0; i < planes.size(); ++i) + { + planes[i].n = -planes[i].n; + planes[i].d = -planes[i].d; + + if (VSA::vs3d_test(prt)) + { + neighboors[cellId].push_back(i + cellId + 1); + neighboors[i + cellId + 1].push_back(cellId); + }; + planes[i].n = -planes[i].n; + planes[i].d = -planes[i].d; + } + } +} + + +#define SITE_BOX_SIZE 4 +#define CUTTING_BOX_SIZE 40 + +Mesh* getCellMesh(BooleanEvaluator& eval, int32_t planeIndexerOffset, int32_t cellId, const std::vector<PxVec3>& sites, std::vector < std::vector<int32_t> >& neighboors) +{ + Mesh* cell = getBigBox(sites[cellId], SITE_BOX_SIZE); + Mesh* cuttingMesh = getCuttingBox(PxVec3(0, 0, 0), PxVec3(1, 1, 1), CUTTING_BOX_SIZE, 0); + + for (uint32_t i = 0; i < neighboors[cellId].size(); ++i) + { + int32_t nCell = neighboors[cellId][i]; + PxVec3 midpoint = 0.5 * (sites[nCell] + sites[cellId]); + PxVec3 direction = (sites[nCell] - sites[cellId]).getNormalized(); + int32_t planeIndex = static_cast<int32_t>(sites.size()) * std::min(cellId, nCell) + std::max(cellId, nCell) + planeIndexerOffset; + if (nCell < cellId) + planeIndex = -planeIndex; + setCuttingBox(midpoint, -direction, cuttingMesh, CUTTING_BOX_SIZE, planeIndex); + eval.performFastCutting(cell, cuttingMesh, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* newCell = eval.createNewMesh(); + delete cell; + cell = newCell; + if (cell == nullptr) + break; + } + return cell; +} + + +bool blastBondComparator(const NvBlastBondDesc& a, const NvBlastBondDesc& b) +{ + if (a.chunkIndices[0] == b.chunkIndices[0]) + return a.chunkIndices[1] < b.chunkIndices[1]; + else + return a.chunkIndices[0] < b.chunkIndices[0]; +} + + +#define MAX_VORONOI_ATTEMPT_NUMBER 450 + +VoronoiSitesGenerator::VoronoiSitesGenerator(Mesh* mesh, RandomGeneratorBase* rnd) +{ + mMesh = mesh; + mRnd = rnd; + mAccelerator = new BBoxBasedAccelerator(mMesh, DEFAULT_BB_ACCELARATOR_RES); + mStencil = nullptr; +} + +void VoronoiSitesGenerator::setBaseMesh(Mesh* m) +{ + mGeneratedSites.clear(); + delete mAccelerator; + mMesh = m; + mAccelerator = new BBoxBasedAccelerator(mMesh, DEFAULT_BB_ACCELARATOR_RES); +} + +VoronoiSitesGenerator::~VoronoiSitesGenerator() +{ + delete mAccelerator; + mAccelerator = nullptr; +} + + +void VoronoiSitesGenerator::setStencil(Mesh* stencil) +{ + mStencil = stencil; +} + + +void VoronoiSitesGenerator::clearStencil() +{ + mStencil = nullptr; +} + + +void VoronoiSitesGenerator::uniformlyGenerateSitesInMesh(const uint32_t sitesCount) +{ + BooleanEvaluator voronoiMeshEval(nullptr); + PxVec3 mn = mMesh->getBoundingBox().minimum; + PxVec3 mx = mMesh->getBoundingBox().maximum; + PxVec3 vc = mx - mn; + uint32_t attemptNumber = 0; + uint32_t generatedSites = 0; + while (generatedSites < sitesCount && attemptNumber < MAX_VORONOI_ATTEMPT_NUMBER) + { + float rn1 = mRnd->getRandomValue() * vc.x; + float rn2 = mRnd->getRandomValue() * vc.y; + float rn3 = mRnd->getRandomValue() * vc.z; + if (voronoiMeshEval.isPointContainedInMesh(mMesh, PxVec3(rn1, rn2, rn3) + mn) && (mStencil == nullptr + || voronoiMeshEval.isPointContainedInMesh(mStencil, PxVec3(rn1, rn2, rn3) + mn))) + { + generatedSites++; + mGeneratedSites.push_back(PxVec3(rn1, rn2, rn3) + mn); + attemptNumber = 0; + } + else + { + attemptNumber++; + if (attemptNumber > MAX_VORONOI_ATTEMPT_NUMBER) + break; + } + } +} + + +void VoronoiSitesGenerator::clusteredSitesGeneration(const uint32_t numberOfClusters, const uint32_t sitesPerCluster, float clusterRadius) +{ + BooleanEvaluator voronoiMeshEval(nullptr); + PxVec3 mn = mMesh->getBoundingBox().minimum; + PxVec3 mx = mMesh->getBoundingBox().maximum; + PxVec3 middle = (mx + mn) * 0.5; + PxVec3 vc = (mx - mn) * 0.5; + uint32_t attemptNumber = 0; + uint32_t generatedSites = 0; + std::vector<PxVec3> tempPoints; + while (generatedSites < numberOfClusters) + { + float rn1 = mRnd->getRandomValue() * 2 - 1; + float rn2 = mRnd->getRandomValue() * 2 - 1; + float rn3 = mRnd->getRandomValue() * 2 - 1; + PxVec3 p = PxVec3(middle.x + rn1 * vc.x, middle.y + rn2 * vc.y, middle.z + rn3 * vc.z); + + if (voronoiMeshEval.isPointContainedInMesh(mMesh, p) && (mStencil == nullptr + || voronoiMeshEval.isPointContainedInMesh(mStencil, p))) + { + generatedSites++; + tempPoints.push_back(p); + attemptNumber = 0; + } + else + { + attemptNumber++; + if (attemptNumber > MAX_VORONOI_ATTEMPT_NUMBER) + break; + } + } + int32_t totalCount = 0; + for (; tempPoints.size() > 0; tempPoints.pop_back()) + { + uint32_t unif = sitesPerCluster; + generatedSites = 0; + while (generatedSites < unif) + { + PxVec3 p = tempPoints.back() + PxVec3(mRnd->getRandomValue() * 2 - 1, mRnd->getRandomValue() * 2 - 1, mRnd->getRandomValue() * 2 - 1).getNormalized() * (mRnd->getRandomValue() + 0.001f) * clusterRadius; + if (voronoiMeshEval.isPointContainedInMesh(mMesh, p) && (mStencil == nullptr + || voronoiMeshEval.isPointContainedInMesh(mStencil, p))) + { + totalCount++; + generatedSites++; + mGeneratedSites.push_back(p); + attemptNumber = 0; + } + else + { + attemptNumber++; + if (attemptNumber > MAX_VORONOI_ATTEMPT_NUMBER) + break; + } + } + + } + +} + + +#define IN_SPHERE_ATTEMPT_NUMBER 20 + +void VoronoiSitesGenerator::addSite(const physx::PxVec3& site) +{ + mGeneratedSites.push_back(site); +} + + +void VoronoiSitesGenerator::generateInSphere(const uint32_t count, const float radius, const physx::PxVec3& center) +{ + BooleanEvaluator voronoiMeshEval(nullptr); + uint32_t attemptNumber = 0; + uint32_t generatedSites = 0; + std::vector<PxVec3> tempPoints; + + while (generatedSites < count && attemptNumber < MAX_VORONOI_ATTEMPT_NUMBER) + { + float rn1 = mRnd->getRandomValue() * radius; + float rn2 = mRnd->getRandomValue() * radius; + float rn3 = mRnd->getRandomValue() * radius; + if (voronoiMeshEval.isPointContainedInMesh(mMesh, PxVec3(rn1, rn2, rn3) + center) && (mStencil == nullptr + || voronoiMeshEval.isPointContainedInMesh(mStencil, PxVec3(rn1, rn2, rn3) + center))) + { + generatedSites++; + mGeneratedSites.push_back(PxVec3(rn1, rn2, rn3) + center); + attemptNumber = 0; + } + else + { + attemptNumber++; + if (attemptNumber > MAX_VORONOI_ATTEMPT_NUMBER) + break; + } + } +} + + +void VoronoiSitesGenerator::deleteInSphere(const float radius, const physx::PxVec3& center, float deleteProbability) +{ + float r2 = radius * radius; + for (uint32_t i = 0; i < mGeneratedSites.size(); ++i) + { + if ((mGeneratedSites[i] - center).magnitudeSquared() < r2 && mRnd->getRandomValue() <= deleteProbability) + { + std::swap(mGeneratedSites[i], mGeneratedSites.back()); + mGeneratedSites.pop_back(); + --i; + } + } +} + + +void VoronoiSitesGenerator::radialPattern(const physx::PxVec3& center, const physx::PxVec3& normal, float radius, int32_t angularSteps, int32_t radialSteps, float angleOffset, float variability) +{ +// mGeneratedSites.push_back(center); + physx::PxVec3 t1, t2; + if (abs(normal.z) < 0.9) + { + t1 = normal.cross(PxVec3(0, 0, 1)); + } + else + { + t1 = normal.cross(PxVec3(1, 0, 0)); + } + t2 = t1.cross(normal); + t1.normalize(); + t2.normalize(); + + float radStep = radius / radialSteps; + int32_t cCr = 0; + + float angleStep = PxPi * 2 / angularSteps; + for (float cRadius = radStep; cRadius < radius; cRadius += radStep) + { + float cAngle = angleOffset * cCr; + for (int32_t i = 0; i < angularSteps; ++i) + { + float angVars = mRnd->getRandomValue() * variability + (1.0f - 0.5f * variability); + float radVars = mRnd->getRandomValue() * variability + (1.0f - 0.5f * variability); + + PxVec3 nPos = (PxCos(cAngle * angVars) * t1 + PxSin(cAngle * angVars) * t2) * cRadius * radVars + center; + mGeneratedSites.push_back(nPos); + cAngle += angleStep; + } + ++cCr; + } +} + + +std::vector<PxVec3>& VoronoiSitesGenerator::getVoronoiSites() +{ + return mGeneratedSites; +} + + +int32_t FractureTool::voronoiFracturing(uint32_t chunkId, const std::vector<physx::PxVec3>& cellPointsIn, bool replaceChunk) +{ + if (chunkId == 0 && replaceChunk) + { + return 1; + } + + int32_t chunkIndex = getChunkIndex(chunkId); + if (chunkIndex == -1 || cellPointsIn.size() < 2) + { + return 1; + } + if (!mChunkData[chunkIndex].isLeaf) + { + deleteAllChildsOfChunk(chunkId); + } + chunkIndex = getChunkIndex(chunkId); + + Mesh* mesh = mChunkData[chunkIndex].meshData; + + std::vector<PxVec3> cellPoints(cellPointsIn.size()); + for (uint32_t i = 0; i < cellPointsIn.size(); ++i) + { + cellPoints[i] = (cellPointsIn[i] - mOffset) * (1.0f / mScaleFactor); + } + + /** + Prebuild accelerator structure + */ + BooleanEvaluator eval(mLoggingCallback); + BooleanEvaluator voronoiMeshEval(mLoggingCallback); + + BBoxBasedAccelerator spAccel = BBoxBasedAccelerator(mesh, DEFAULT_BB_ACCELARATOR_RES); + + std::vector<std::vector<int32_t> > neighboors; + findCellBasePlanes(cellPoints, neighboors); + + /** + Fracture + */ + int32_t parentChunk = replaceChunk ? mChunkData[chunkIndex].parent : chunkId; + std::vector<uint32_t> newlyCreatedChunksIds; + for (uint32_t i = 0; i < cellPoints.size(); ++i) + { + Mesh* cell = getCellMesh(eval, mPlaneIndexerOffset, i, cellPoints, neighboors); + + if (cell == nullptr) + { + continue; + } + DummyAccelerator dmAccel(cell->getFacetCount()); + voronoiMeshEval.performBoolean(mesh, cell, &spAccel, &dmAccel, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* resultMesh = voronoiMeshEval.createNewMesh(); + if (resultMesh) + { + mChunkData.push_back(ChunkInfo()); + mChunkData.back().isLeaf = true; + mChunkData.back().meshData = resultMesh; + mChunkData.back().parent = parentChunk; + mChunkData.back().chunkId = mChunkIdCounter++; + newlyCreatedChunksIds.push_back(mChunkData.back().chunkId); + } + eval.reset(); + delete cell; + } + mChunkData[chunkIndex].isLeaf = false; + if (replaceChunk) + { + eraseChunk(chunkId); + } + mPlaneIndexerOffset += static_cast<int32_t>(cellPoints.size() * cellPoints.size()); + + + if (mRemoveIslands) + { + for (auto chunkToCheck : newlyCreatedChunksIds) + { + islandDetectionAndRemoving(chunkToCheck); + } + } + return 0; +} + +Mesh FractureTool::getChunkMesh(int32_t chunkId) +{ + Mesh temp = *mChunkData[getChunkIndex(chunkId)].meshData; + for (uint32_t i = 0; i < temp.getVerticesCount(); ++i) + { + temp.getVertices()[i].p = temp.getVertices()[i].p * mScaleFactor + mOffset; + } + temp.recalculateBoundingBox(); + + return temp; +} + + +int32_t FractureTool::voronoiFracturing(uint32_t chunkId, const std::vector<physx::PxVec3>& cellPointsIn, const physx::PxVec3& scale, bool replaceChunk) +{ + if (chunkId == 0 && replaceChunk) + { + return 1; + } + + int32_t chunkIndex = getChunkIndex(chunkId); + if (chunkIndex == -1 || cellPointsIn.size() < 2) + { + return 1; + } + if (!mChunkData[chunkIndex].isLeaf) + { + deleteAllChildsOfChunk(chunkId); + } + chunkIndex = getChunkIndex(chunkId); + + Mesh* mesh = mChunkData[chunkIndex].meshData; + + std::vector<PxVec3> cellPoints(cellPointsIn.size()); + for (uint32_t i = 0; i < cellPointsIn.size(); ++i) + { + cellPoints[i] = (cellPointsIn[i] - mOffset) * (1.0f / mScaleFactor); + + cellPoints[i].x *= (1.0f / scale.x); + cellPoints[i].y *= (1.0f / scale.y); + cellPoints[i].z *= (1.0f / scale.z); + } + + /** + Prebuild accelerator structure + */ + BooleanEvaluator eval(mLoggingCallback); + BooleanEvaluator voronoiMeshEval(mLoggingCallback); + + BBoxBasedAccelerator spAccel = BBoxBasedAccelerator(mesh, DEFAULT_BB_ACCELARATOR_RES); + + std::vector<std::vector<int32_t> > neighboors; + findCellBasePlanes(cellPoints, neighboors); + + /** + Fracture + */ + int32_t parentChunk = replaceChunk ? mChunkData[chunkIndex].parent : chunkId; + std::vector<uint32_t> newlyCreatedChunksIds; + + for (uint32_t i = 0; i < cellPoints.size(); ++i) + { + Mesh* cell = getCellMesh(eval, mPlaneIndexerOffset, i, cellPoints, neighboors); + + if (cell == nullptr) + { + continue; + } + + for (uint32_t v = 0; v < cell->getVerticesCount(); ++v) + { + cell->getVertices()[v].p.x *= scale.x; + cell->getVertices()[v].p.y *= scale.y; + cell->getVertices()[v].p.z *= scale.z; + } + cell->recalculateBoundingBox(); + DummyAccelerator dmAccel(cell->getFacetCount()); + voronoiMeshEval.performBoolean(mesh, cell, &spAccel, &dmAccel, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* resultMesh = voronoiMeshEval.createNewMesh(); + if (resultMesh) + { + mChunkData.push_back(ChunkInfo()); + mChunkData.back().isLeaf = true; + mChunkData.back().meshData = resultMesh; + mChunkData.back().parent = parentChunk; + mChunkData.back().chunkId = mChunkIdCounter++; + newlyCreatedChunksIds.push_back(mChunkData.back().chunkId); + } + eval.reset(); + delete cell; + } + mChunkData[chunkIndex].isLeaf = false; + if (replaceChunk) + { + eraseChunk(chunkId); + } + mPlaneIndexerOffset += static_cast<int32_t>(cellPoints.size() * cellPoints.size()); + + if (mRemoveIslands) + { + for (auto chunkToCheck : newlyCreatedChunksIds) + { + islandDetectionAndRemoving(chunkToCheck); + } + } + + return 0; +} + + +int32_t FractureTool::slicing(uint32_t chunkId, SlicingConfiguration conf, bool replaceChunk, RandomGeneratorBase* rnd) +{ + if (conf.noiseAmplitude != 0) + { + return slicingNoisy(chunkId, conf, replaceChunk, rnd); + } + + if (replaceChunk && chunkId == 0) + { + return 1; + } + + int32_t chunkIndex = getChunkIndex(chunkId); + if (chunkIndex == -1) + { + return 1; + } + if (!mChunkData[chunkIndex].isLeaf) + { + deleteAllChildsOfChunk(chunkId); + } + chunkIndex = getChunkIndex(chunkId); + + + Mesh* mesh = new Mesh(*mChunkData[chunkIndex].meshData); + + BooleanEvaluator bTool(mLoggingCallback); + + int32_t x_slices = conf.x_slices; + int32_t y_slices = conf.y_slices; + int32_t z_slices = conf.z_slices; + + const PxBounds3 sourceBBox = mesh->getBoundingBox(); + + PxVec3 center = PxVec3(mesh->getBoundingBox().minimum.x, 0, 0); + + + float x_offset = (sourceBBox.maximum.x - sourceBBox.minimum.x) * (1.0f / (x_slices + 1)); + float y_offset = (sourceBBox.maximum.y - sourceBBox.minimum.y) * (1.0f / (y_slices + 1)); + float z_offset = (sourceBBox.maximum.z - sourceBBox.minimum.z) * (1.0f / (z_slices + 1)); + + center.x += x_offset; + + PxVec3 dir(1, 0, 0); + + Mesh* slBox = getCuttingBox(center, dir, 20, 0); + + ChunkInfo ch; + ch.isLeaf = true; + ch.parent = replaceChunk ? mChunkData[chunkIndex].parent : chunkId; + std::vector<ChunkInfo> xSlicedChunks; + std::vector<ChunkInfo> ySlicedChunks; + std::vector<uint32_t> newlyCreatedChunksIds; + /** + Slice along x direction + */ + for (int32_t slice = 0; slice < x_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + + setCuttingBox(center, lDir, slBox, 20, mPlaneIndexerOffset); + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + + if (ch.meshData != 0) + { + xSlicedChunks.push_back(ch); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete mesh; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.x += x_offset + (rnd->getRandomValue()) * conf.offset_variations * x_offset; + } + if (mesh != 0) + { + ch.meshData = mesh; + xSlicedChunks.push_back(ch); + } + + + for (uint32_t chunk = 0; chunk < xSlicedChunks.size(); ++chunk) + { + center = PxVec3(0, sourceBBox.minimum.y, 0); + center.y += y_offset; + dir = PxVec3(0, 1, 0); + mesh = xSlicedChunks[chunk].meshData; + + for (int32_t slice = 0; slice < y_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + + + setCuttingBox(center, lDir, slBox, 20, mPlaneIndexerOffset); + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + if (ch.meshData != 0) + { + ySlicedChunks.push_back(ch); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete mesh; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.y += y_offset + (rnd->getRandomValue()) * conf.offset_variations * y_offset; + } + if (mesh != 0) + { + ch.meshData = mesh; + ySlicedChunks.push_back(ch); + } + } + + + for (uint32_t chunk = 0; chunk < ySlicedChunks.size(); ++chunk) + { + center = PxVec3(0, 0, sourceBBox.minimum.z); + center.z += z_offset; + dir = PxVec3(0, 0, 1); + mesh = ySlicedChunks[chunk].meshData; + + for (int32_t slice = 0; slice < z_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + setCuttingBox(center, lDir, slBox, 20, mPlaneIndexerOffset); + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + if (ch.meshData != 0) + { + ch.chunkId = mChunkIdCounter++; + newlyCreatedChunksIds.push_back(ch.chunkId); + mChunkData.push_back(ch); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performFastCutting(mesh, slBox, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete mesh; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.z += z_offset + (rnd->getRandomValue()) * conf.offset_variations * z_offset; + } + if (mesh != 0) + { + ch.chunkId = mChunkIdCounter++; + ch.meshData = mesh; + mChunkData.push_back(ch); + newlyCreatedChunksIds.push_back(ch.chunkId); + } + } + + + delete slBox; + + mChunkData[chunkIndex].isLeaf = false; + if (replaceChunk) + { + eraseChunk(chunkId); + } + + if (mRemoveIslands) + { + for (auto chunkToCheck : newlyCreatedChunksIds) + { + islandDetectionAndRemoving(chunkToCheck); + } + } + + return 0; +} + +int32_t FractureTool::slicingNoisy(uint32_t chunkId, SlicingConfiguration conf, bool replaceChunk, RandomGeneratorBase* rnd) +{ + if (replaceChunk && chunkId == 0) + { + return 1; + } + + int32_t chunkIndex = getChunkIndex(chunkId); + if (chunkIndex == -1) + { + return 1; + } + if (!mChunkData[chunkIndex].isLeaf) + { + deleteAllChildsOfChunk(chunkId); + } + chunkIndex = getChunkIndex(chunkId); + + + Mesh* mesh = new Mesh(*mChunkData[chunkIndex].meshData); + + BooleanEvaluator bTool(mLoggingCallback); + + int32_t x_slices = conf.x_slices; + int32_t y_slices = conf.y_slices; + int32_t z_slices = conf.z_slices; + + const PxBounds3 sourceBBox = mesh->getBoundingBox(); + + PxVec3 center = PxVec3(mesh->getBoundingBox().minimum.x, 0, 0); + + + float x_offset = (sourceBBox.maximum.x - sourceBBox.minimum.x) * (1.0f / (x_slices + 1)); + float y_offset = (sourceBBox.maximum.y - sourceBBox.minimum.y) * (1.0f / (y_slices + 1)); + float z_offset = (sourceBBox.maximum.z - sourceBBox.minimum.z) * (1.0f / (z_slices + 1)); + + center.x += x_offset; + + PxVec3 dir(1, 0, 0); + + Mesh* slBox = nullptr; + + ChunkInfo ch; + ch.isLeaf = true; + ch.parent = replaceChunk ? mChunkData[chunkIndex].parent : chunkId; + std::vector<ChunkInfo> xSlicedChunks; + std::vector<ChunkInfo> ySlicedChunks; + std::vector<uint32_t> newlyCreatedChunksIds; + float noisyPartSize = 1.8f; + int32_t acceleratorRes = 5; + /** + Slice along x direction + */ + for (int32_t slice = 0; slice < x_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + slBox = getNoisyCuttingBoxPair(center, lDir, 40, noisyPartSize, conf.surfaceResolution, mPlaneIndexerOffset, conf.noiseAmplitude, conf.noiseFrequency, conf.noiseOctaveNumber, rnd->getRandomValue()); + // DummyAccelerator accel(mesh->getFacetCount()); + IntersectionTestingAccelerator accel(mesh, acceleratorRes); + DummyAccelerator dummy(slBox->getFacetCount()); + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + if (ch.meshData != 0) + { + xSlicedChunks.push_back(ch); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete slBox; + delete mesh; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.x += x_offset + (rnd->getRandomValue()) * conf.offset_variations * x_offset; + } + if (mesh != 0) + { + ch.meshData = mesh; + xSlicedChunks.push_back(ch); + } + slBox = getCuttingBox(center, dir, 20, 0); + uint32_t slicedChunkSize = xSlicedChunks.size(); + for (uint32_t chunk = 0; chunk < slicedChunkSize; ++chunk) + { + center = PxVec3(0, sourceBBox.minimum.y, 0); + center.y += y_offset; + dir = PxVec3(0, 1, 0); + mesh = xSlicedChunks[chunk].meshData; + + for (int32_t slice = 0; slice < y_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + + slBox = getNoisyCuttingBoxPair(center, lDir, 40, noisyPartSize, conf.surfaceResolution, mPlaneIndexerOffset, conf.noiseAmplitude, conf.noiseFrequency, conf.noiseOctaveNumber, rnd->getRandomValue()); + // DummyAccelerator accel(mesh->getFacetCount()); + IntersectionTestingAccelerator accel(mesh, acceleratorRes); + DummyAccelerator dummy(slBox->getFacetCount()); + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + if (ch.meshData != 0) + { + ySlicedChunks.push_back(ch); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete slBox; + delete mesh; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.y += y_offset + (rnd->getRandomValue()) * conf.offset_variations * y_offset; + } + if (mesh != 0) + { + ch.meshData = mesh; + ySlicedChunks.push_back(ch); + } + } + + for (uint32_t chunk = 0; chunk < ySlicedChunks.size(); ++chunk) + { + center = PxVec3(0, 0, sourceBBox.minimum.z); + center.z += z_offset; + dir = PxVec3(0, 0, 1); + mesh = ySlicedChunks[chunk].meshData; + + for (int32_t slice = 0; slice < z_slices; ++slice) + { + PxVec3 randVect = PxVec3(2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1, 2 * rnd->getRandomValue() - 1); + PxVec3 lDir = dir + randVect * conf.angle_variations; + slBox = getNoisyCuttingBoxPair(center, lDir, 40, noisyPartSize, conf.surfaceResolution, mPlaneIndexerOffset, conf.noiseAmplitude, conf.noiseFrequency, conf.noiseOctaveNumber, rnd->getRandomValue()); + // DummyAccelerator accel(mesh->getFacetCount()); + IntersectionTestingAccelerator accel(mesh, acceleratorRes); + DummyAccelerator dummy(slBox->getFacetCount()); + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_DIFFERENCE()); + ch.meshData = bTool.createNewMesh(); + if (ch.meshData != 0) + { + ch.chunkId = mChunkIdCounter++; + mChunkData.push_back(ch); + newlyCreatedChunksIds.push_back(ch.chunkId); + } + inverseNormalAndSetIndices(slBox, -mPlaneIndexerOffset); + ++mPlaneIndexerOffset; + bTool.performBoolean(mesh, slBox, &accel, &dummy, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + delete mesh; + delete slBox; + mesh = result; + if (mesh == nullptr) + { + break; + } + center.z += z_offset + (rnd->getRandomValue()) * conf.offset_variations * z_offset; + } + if (mesh != 0) + { + ch.chunkId = mChunkIdCounter++; + ch.meshData = mesh; + mChunkData.push_back(ch); + newlyCreatedChunksIds.push_back(ch.chunkId); + } + } + +// delete slBox; + + mChunkData[chunkIndex].isLeaf = false; + if (replaceChunk) + { + eraseChunk(chunkId); + } + + if (mRemoveIslands) + { + for (auto chunkToCheck : newlyCreatedChunksIds) + { + islandDetectionAndRemoving(chunkToCheck); + } + } + + return 0; +} + + + +int32_t FractureTool::getChunkIndex(int32_t chunkId) +{ + for (uint32_t i = 0; i < mChunkData.size(); ++i) + { + if (mChunkData[i].chunkId == chunkId) + { + return i; + } + } + return -1; +} + +int32_t FractureTool::getChunkDepth(int32_t chunkId) +{ + int32_t chunkIndex = getChunkIndex(chunkId); + if (chunkIndex == -1) + { + return -1; + } + + int32_t depth = 0; + + while (mChunkData[chunkIndex].parent != -1) + { + ++depth; + chunkIndex = getChunkIndex(mChunkData[chunkIndex].parent); + } + return depth; +} + +std::vector<int32_t> FractureTool::getChunksIdAtDepth(uint32_t depth) +{ + std::vector<int32_t> chunkIds; + + for (uint32_t i = 0; i < mChunkData.size(); ++i) + { + if (getChunkDepth(mChunkData[i].chunkId) == (int32_t)depth) + { + chunkIds.push_back(mChunkData[i].chunkId); + } + } + return chunkIds; +} + + +void FractureTool::getTransformation(PxVec3& offset, float& scale) +{ + offset = mOffset; + scale = mScaleFactor; +} + +void FractureTool::setSourceMesh(Mesh* mesh) +{ + if (mesh == nullptr) + { + return; + } + reset(); + + mChunkData.resize(1); + mChunkData[0].meshData = new Mesh(*mesh); + mChunkData[0].parent = -1; + mChunkData[0].isLeaf = true; + mChunkData[0].chunkId = mChunkIdCounter++; + mesh = mChunkData[0].meshData; + + /** + Move to origin and scale to unit cube + */ + + mOffset = (mesh->getBoundingBox().maximum + mesh->getBoundingBox().minimum) * 0.5f; + PxVec3 bbSizes = (mesh->getBoundingBox().maximum - mesh->getBoundingBox().minimum); + + mScaleFactor = std::max(bbSizes.x, std::max(bbSizes.y, bbSizes.z)); + + Vertex* verticesBuffer = mesh->getVertices(); + for (uint32_t i = 0; i < mesh->getVerticesCount(); ++i) + { + verticesBuffer[i].p = (verticesBuffer[i].p - mOffset) * (1.0f / mScaleFactor); + } + + mesh->getBoundingBox().minimum = (mesh->getBoundingBox().minimum - mOffset) * (1.0f / mScaleFactor); + mesh->getBoundingBox().maximum = (mesh->getBoundingBox().maximum - mOffset) * (1.0f / mScaleFactor); + + + for (uint32_t i = 0; i < mesh->getFacetCount(); ++i) + { + mesh->getFacet(i)->userData = 0; // Mark facet as initial boundary facet + } +} + + +void FractureTool::reset() +{ + mChunkPostprocessors.clear(); + for (uint32_t i = 0; i < mChunkData.size(); ++i) + { + delete mChunkData[i].meshData; + } + mChunkData.clear(); + mPlaneIndexerOffset = 1; + mChunkIdCounter = 0; +} + + + + +bool FractureTool::isAncestorForChunk(int32_t ancestorId, int32_t chunkId) +{ + if (ancestorId == chunkId) + { + return false; + } + while (chunkId != -1) + { + if (ancestorId == chunkId) + { + return true; + } + chunkId = getChunkIndex(chunkId); + if (chunkId == -1) + { + return false; + } + chunkId = mChunkData[chunkId].parent; + } + return false; +} + +void FractureTool::eraseChunk(int32_t chunkId) +{ + deleteAllChildsOfChunk(chunkId); + int32_t index = getChunkIndex(chunkId); + if (index != -1) + { + delete mChunkData[index].meshData; + std::swap(mChunkData.back(), mChunkData[index]); + mChunkData.pop_back(); + } +} + + +void FractureTool::deleteAllChildsOfChunk(int32_t chunkId) +{ + std::vector<int32_t> chunkToDelete; + for (uint32_t i = 0; i < mChunkData.size(); ++i) + { + if (isAncestorForChunk(chunkId, mChunkData[i].chunkId)) + { + chunkToDelete.push_back(i); + } + } + for (int32_t i = (int32_t)chunkToDelete.size() - 1; i >= 0; --i) + { + int32_t m = chunkToDelete[i]; + delete mChunkData[m].meshData; + std::swap(mChunkData.back(), mChunkData[m]); + mChunkData.pop_back(); + } +} + +void FractureTool::finalizeFracturing() +{ + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + delete mChunkPostprocessors[i]; + } + mChunkPostprocessors.resize(mChunkData.size()); + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + mChunkPostprocessors[i] = new ChunkPostProcessor(); + } + + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + mChunkPostprocessors[i]->triangulate(mChunkData[i].meshData); + } + std::vector<int32_t> badOnes; + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + if (mChunkPostprocessors[i]->getBaseMesh().empty()) + { + badOnes.push_back(i); + } + } + for (int32_t i = (int32_t)badOnes.size() - 1; i >= 0; --i) + { + int32_t chunkId = mChunkData[badOnes[i]].chunkId; + for (uint32_t j = 0; j < mChunkData.size(); ++j) + { + if (mChunkData[j].parent == chunkId) + mChunkData[j].parent = mChunkData[badOnes[i]].parent; + } + std::swap(mChunkPostprocessors[badOnes[i]], mChunkPostprocessors.back()); + mChunkPostprocessors.pop_back(); + std::swap(mChunkData[badOnes[i]], mChunkData.back()); + mChunkData.pop_back(); + } + +} + +const std::vector<ChunkInfo>& FractureTool::getChunkList() +{ + return mChunkData; +} + +void FractureTool::getBaseMesh(int32_t chunkIndex, std::vector<Triangle>& output) +{ + NVBLAST_ASSERT(mChunkPostprocessors.size() > 0); + if (mChunkPostprocessors.size() == 0) + { + return; // finalizeFracturing() should be called before getting mesh! + } + output = mChunkPostprocessors[chunkIndex]->getBaseMesh(); + + /* Scale mesh back */ + + for (uint32_t i = 0; i < output.size(); ++i) + { + output[i].a.p = output[i].a.p * mScaleFactor + mOffset; + output[i].b.p = output[i].b.p * mScaleFactor + mOffset; + output[i].c.p = output[i].c.p * mScaleFactor + mOffset; + } +} + +void FractureTool::getNoisedMesh(int32_t chunkIndex, std::vector<Triangle>& output) +{ + NVBLAST_ASSERT(mChunkPostprocessors.size() > 0); + if (mChunkPostprocessors.size() == 0) + { + return; // finalizeFracturing() should be called before getting mesh! + } + + if (mChunkData[chunkIndex].chunkId == 0) + { + output = mChunkPostprocessors[chunkIndex]->getBaseMesh(); + } + else + { + output = mChunkPostprocessors[chunkIndex]->getNoisyMesh(); + } + + for (uint32_t i = 0; i < output.size(); ++i) + { + output[i].a.p = output[i].a.p * mScaleFactor + mOffset; + output[i].b.p = output[i].b.p * mScaleFactor + mOffset; + output[i].c.p = output[i].c.p * mScaleFactor + mOffset; + } +} + +void FractureTool::tesselate(float averateEdgeLength) +{ + NVBLAST_ASSERT(mChunkPostprocessors.size() > 0); + if (mChunkPostprocessors.size() == 0) + { + return; // finalizeFracturing() should be called before tesselation! + } + + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + if (mChunkData[i].chunkId == 0) // skip source mesh + { + continue; + } + mChunkPostprocessors[i]->tesselateInternalSurface(averateEdgeLength / mScaleFactor); + } +} + +void FractureTool::applyNoise(float amplitude, float frequency, int32_t octaves, float falloff, int32_t relaxIterations, float relaxFactor, int32_t seed) +{ + octaves = octaves <= 0 ? 1 : octaves; + if (mChunkPostprocessors.empty()) + { + return; + } + SimplexNoise noise(amplitude / mScaleFactor, frequency * mScaleFactor, octaves, seed); + for (uint32_t i = 0; i < mChunkPostprocessors.size(); ++i) + { + if (mChunkData[i].chunkId == 0) // skip source mesh + { + continue; + } + mChunkPostprocessors[i]->applyNoise(noise, falloff, relaxIterations, relaxFactor); + } +} + +float getVolume(std::vector<Triangle>& triangles) +{ + float volume = 0.0f; + + for (uint32_t i = 0; i < triangles.size(); ++i) + { + PxVec3& a = triangles[i].a.p; + PxVec3& b = triangles[i].b.p; + PxVec3& c = triangles[i].c.p; + volume += (a.x * b.y * c.z - a.x * b.z * c.y - a.y * b.x * c.z + a.y * b.z * c.x + a.z * b.x * c.y - a.z * b.y * c.x); + } + return (1.0f / 6.0f) * PxAbs(volume); +} + +float FractureTool::getMeshOverlap(Mesh& meshA, Mesh& meshB) +{ + BooleanEvaluator bTool; + bTool.performBoolean(&meshA, &meshB, BooleanConfigurations::BOOLEAN_INTERSECION()); + Mesh* result = bTool.createNewMesh(); + if (result == nullptr) + { + return 0.0f; + } + + ChunkPostProcessor postProcessor; + postProcessor.triangulate(&meshA); + + float baseVolume = getVolume(postProcessor.getBaseMesh()); + if (baseVolume == 0) + { + return 0.0f; + } + postProcessor.triangulate(result); + float intrsVolume = getVolume(postProcessor.getBaseMesh()); + + delete result; + + return intrsVolume / baseVolume; +} + +void weldVertices(std::map<Vertex, uint32_t, VrtComp>& vertexMapping, std::vector<Vertex>& vertexBuffer, std::vector<uint32_t>& indexBuffer, std::vector<Triangle>& trb) +{ + for (uint32_t i = 0; i < trb.size(); ++i) + { + auto it = vertexMapping.find(trb[i].a); + if (it == vertexMapping.end()) + { + indexBuffer.push_back(static_cast<uint32_t>(vertexBuffer.size())); + vertexMapping[trb[i].a] = static_cast<uint32_t>(vertexBuffer.size()); + vertexBuffer.push_back(trb[i].a); + } + else + { + indexBuffer.push_back(it->second); + } + it = vertexMapping.find(trb[i].b); + if (it == vertexMapping.end()) + { + indexBuffer.push_back(static_cast<uint32_t>(vertexBuffer.size())); + vertexMapping[trb[i].b] = static_cast<uint32_t>(vertexBuffer.size()); + vertexBuffer.push_back(trb[i].b); + } + else + { + indexBuffer.push_back(it->second); + } + it = vertexMapping.find(trb[i].c); + if (it == vertexMapping.end()) + { + indexBuffer.push_back(static_cast<uint32_t>(vertexBuffer.size())); + vertexMapping[trb[i].c] = static_cast<uint32_t>(vertexBuffer.size()); + vertexBuffer.push_back(trb[i].c); + } + else + { + indexBuffer.push_back(it->second); + } + } + +} + +void FractureTool::setRemoveIslands(bool isRemoveIslands) +{ + mRemoveIslands = isRemoveIslands; +} + +int32_t FractureTool::islandDetectionAndRemoving(int32_t chunkId) +{ + if (chunkId == 0) + { + return 0; + } + int32_t chunkIndex = getChunkIndex(chunkId); + ChunkPostProcessor prc; + prc.triangulate(mChunkData[chunkIndex].meshData); + + Mesh* chunk = mChunkData[chunkIndex].meshData; + + std::vector<uint32_t>& mapping = prc.getBaseMapping(); + std::vector<TriangleIndexed>& trs = prc.getBaseMeshIndexed(); + + std::vector<std::vector<uint32_t> > graph(prc.getWeldedVerticesCount()); + std::vector<int32_t>& pm = prc.getPositionedMapping(); + if (pm.size() == 0) + { + return 0; + } + + /** + Chunk graph + */ + for (uint32_t i = 0; i < trs.size(); ++i) + { + graph[pm[trs[i].ea]].push_back(pm[trs[i].eb]); + graph[pm[trs[i].ea]].push_back(pm[trs[i].ec]); + + graph[pm[trs[i].ec]].push_back(pm[trs[i].eb]); + graph[pm[trs[i].ec]].push_back(pm[trs[i].ea]); + + graph[pm[trs[i].eb]].push_back(pm[trs[i].ea]); + graph[pm[trs[i].eb]].push_back(pm[trs[i].ec]); + } + for (uint32_t i = 0; i < chunk->getEdgesCount(); ++i) + { + int v1 = chunk->getEdges()[i].s; + int v2 = chunk->getEdges()[i].e; + + v1 = pm[mapping[v1]]; + v2 = pm[mapping[v2]]; + + graph[v1].push_back(v2); + graph[v2].push_back(v1); + + } + + + /** + Walk graph, mark components + */ + + std::vector<int32_t> comps(prc.getWeldedVerticesCount(), -1); + std::queue<uint32_t> que; + int32_t cComp = 0; + + for (uint32_t i = 0; i < prc.getWeldedVerticesCount(); ++i) + { + int32_t to = pm[i]; + if (comps[to] != -1) continue; + que.push(to); + comps[to] = cComp; + + while (!que.empty()) + { + int32_t c = que.front(); + que.pop(); + + for (uint32_t j = 0; j < graph[c].size(); ++j) + { + if (comps[graph[c][j]] == -1) + { + que.push(graph[c][j]); + comps[graph[c][j]] = cComp; + } + } + } + cComp++; + } + for (uint32_t i = 0; i < prc.getWeldedVerticesCount(); ++i) + { + int32_t to = pm[i]; + comps[i] = comps[to]; + } + std::vector<uint32_t> longComps(chunk->getVerticesCount()); + for (uint32_t i = 0; i < chunk->getVerticesCount(); ++i) + { + int32_t to = mapping[i]; + longComps[i] = comps[to]; + } + + if (cComp > 1) + { + std::vector<std::vector<Vertex> > compVertices(cComp); + std::vector<std::vector<Facet> > compFacets(cComp); + std::vector<std::vector<Edge> > compEdges(cComp); + + + std::vector<uint32_t> compVertexMapping(chunk->getVerticesCount(), 0); + Vertex* vrts = chunk->getVertices(); + for (uint32_t v = 0; v < chunk->getVerticesCount(); ++v) + { + int32_t vComp = comps[mapping[v]]; + compVertexMapping[v] = static_cast<uint32_t>(compVertices[vComp].size()); + compVertices[vComp].push_back(vrts[v]); + } + + Facet* fcb = chunk->getFacetsBuffer(); + Edge* edb = chunk->getEdges(); + + for (uint32_t fc = 0; fc < chunk->getFacetCount(); ++fc) + { + std::vector<uint32_t> edgesPerComp(cComp, 0); + for (uint32_t ep = fcb[fc].firstEdgeNumber; ep < fcb[fc].firstEdgeNumber + fcb[fc].edgesCount; ++ep) + { + int32_t vComp = comps[mapping[edb[ep].s]]; + edgesPerComp[vComp]++; + compEdges[vComp].push_back(Edge(compVertexMapping[edb[ep].s], compVertexMapping[edb[ep].e])); + } + for (int32_t c = 0; c < cComp; ++c) + { + if (edgesPerComp[c] == 0) + { + continue; + } + compFacets[c].push_back(*chunk->getFacet(fc)); + compFacets[c].back().edgesCount = edgesPerComp[c]; + compFacets[c].back().firstEdgeNumber = static_cast<int32_t>(compEdges[c].size()) - edgesPerComp[c]; + } + } + + delete mChunkData[chunkIndex].meshData; + mChunkData[chunkIndex].meshData = new Mesh(compVertices[0].data(), compEdges[0].data(), compFacets[0].data(), static_cast<uint32_t>(compVertices[0].size()), + static_cast<uint32_t>(compEdges[0].size()), static_cast<uint32_t>(compFacets[0].size()));; + for (int32_t i = 1; i < cComp; ++i) + { + mChunkData.push_back(ChunkInfo(mChunkData[chunkIndex])); + mChunkData.back().chunkId = mChunkIdCounter++; + mChunkData.back().meshData = new Mesh(compVertices[i].data(), compEdges[i].data(), compFacets[i].data(), static_cast<uint32_t>(compVertices[i].size()), + static_cast<uint32_t>(compEdges[i].size()), static_cast<uint32_t>(compFacets[i].size())); + } + + return cComp; + } + return 0; +} + +void FractureTool::getBufferedBaseMeshes(std::vector<Vertex>& vertexBuffer, std::vector<std::vector<uint32_t> >& indexBuffer) +{ + std::map<Vertex, uint32_t, VrtComp> vertexMapping; + vertexBuffer.clear(); + indexBuffer.clear(); + indexBuffer.resize(mChunkPostprocessors.size()); + + for (uint32_t ch = 0; ch < mChunkPostprocessors.size(); ++ch) + { + std::vector<Triangle>& trb = mChunkPostprocessors[ch]->getBaseMesh(); + weldVertices(vertexMapping, vertexBuffer, indexBuffer[ch], trb); + } + for (uint32_t i = 0; i < vertexBuffer.size(); ++i) + { + vertexBuffer[i].p = vertexBuffer[i].p * mScaleFactor + mOffset; + } +} + +int32_t FractureTool::getChunkId(int32_t chunkIndex) +{ + if (chunkIndex < 0 || static_cast<uint32_t>(chunkIndex) >= mChunkData.size()) + { + return -1; + } + return mChunkData[chunkIndex].chunkId; +} + +void FractureTool::getBufferedNoiseMeshes(std::vector<Vertex>& vertexBuffer, std::vector<std::vector<uint32_t> >& indexBuffer) +{ + std::map<Vertex, uint32_t, VrtComp> vertexMapping; + vertexBuffer.clear(); + indexBuffer.clear(); + indexBuffer.resize(mChunkPostprocessors.size()); + + for (uint32_t ch = 0; ch < mChunkPostprocessors.size(); ++ch) + { + if (ch == 0) + { + std::vector<Triangle>& trb = mChunkPostprocessors[ch]->getBaseMesh(); + weldVertices(vertexMapping, vertexBuffer, indexBuffer[ch], trb); + } + else + { + std::vector<Triangle>& trb = mChunkPostprocessors[ch]->getNoisyMesh(); + weldVertices(vertexMapping, vertexBuffer, indexBuffer[ch], trb); + } + } + for (uint32_t i = 0; i < vertexBuffer.size(); ++i) + { + vertexBuffer[i].p = vertexBuffer[i].p * mScaleFactor + mOffset; + } +} + + +} // namespace Blast +} // namespace Nv + diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringInternalCommon.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringInternalCommon.h new file mode 100644 index 0000000..b8fb20e --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringInternalCommon.h @@ -0,0 +1,193 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTINTERNALCOMMON_H +#define NVBLASTINTERNALCOMMON_H +#include "NvBlastExtAuthoringTypes.h" + +using namespace physx; + +namespace Nv +{ +namespace Blast +{ + +/** +Edge representation with index of parent facet +*/ +struct EdgeWithParent +{ + int32_t s, e; // Starting and ending vertices + int32_t parent; // Parent facet index + EdgeWithParent() : s(0), e(0), parent(0) {} + EdgeWithParent(int32_t s, int32_t e, int32_t p) : s(s), e(e), parent(p) {} +}; + + +/** +Comparator for sorting edges according to parent facet number. +*/ +struct EdgeComparator +{ + bool operator()(const EdgeWithParent& a, const EdgeWithParent& b) const + { + if (a.parent == b.parent) + { + if (a.s == b.s) + { + return a.e < b.e; + } + else + { + return a.s < b.s; + } + } + else + { + return a.parent < b.parent; + } + } +}; + + +/** +Vertex projection direction flag. +*/ +enum ProjectionDirections +{ + YZ_PLANE = 1 << 1, + XY_PLANE = 1 << 2, + ZX_PLANE = 1 << 3, + + OPPOSITE_WINDING = 1 << 4 +}; + +/** +Computes best direction to project points. +*/ +NV_FORCE_INLINE ProjectionDirections getProjectionDirection(const physx::PxVec3& normal) +{ + float maxv = std::max(abs(normal.x), std::max(abs(normal.y), abs(normal.z))); + ProjectionDirections retVal; + if (maxv == abs(normal.x)) + { + retVal = YZ_PLANE; + if (normal.x < 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING); + return retVal; + } + if (maxv == abs(normal.y)) + { + retVal = ZX_PLANE; + if (normal.y > 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING); + return retVal; + } + retVal = XY_PLANE; + if (normal.z < 0) retVal = (ProjectionDirections)((int)retVal | (int)OPPOSITE_WINDING); + return retVal; +} + + +/** +Computes point projected on given axis aligned plane. +*/ +NV_FORCE_INLINE physx::PxVec2 getProjectedPoint(const physx::PxVec3& point, ProjectionDirections dir) +{ + if (dir & YZ_PLANE) + { + return physx::PxVec2(point.y, point.z); + } + if (dir & ZX_PLANE) + { + return physx::PxVec2(point.x, point.z); + } + return physx::PxVec2(point.x, point.y); +} + +/** +Computes point projected on given axis aligned plane, this method is polygon-winding aware. +*/ +NV_FORCE_INLINE physx::PxVec2 getProjectedPointWithWinding(const physx::PxVec3& point, ProjectionDirections dir) +{ + if (dir & YZ_PLANE) + { + if (dir & OPPOSITE_WINDING) + { + return physx::PxVec2(point.z, point.y); + } + else + return physx::PxVec2(point.y, point.z); + } + if (dir & ZX_PLANE) + { + if (dir & OPPOSITE_WINDING) + { + return physx::PxVec2(point.z, point.x); + } + return physx::PxVec2(point.x, point.z); + } + if (dir & OPPOSITE_WINDING) + { + return physx::PxVec2(point.y, point.x); + } + return physx::PxVec2(point.x, point.y); +} + + + +#define MAXIMUM_EXTENT 1000 * 1000 * 1000 +#define BBOX_TEST_EPS 1e-5f + +/** +Test fattened bounding box intersetion. +*/ +NV_INLINE bool weakBoundingBoxIntersection(const physx::PxBounds3& aBox, const physx::PxBounds3& bBox) +{ + if (std::max(aBox.minimum.x, bBox.minimum.x) > std::min(aBox.maximum.x, bBox.maximum.x) + BBOX_TEST_EPS) + return false; + if (std::max(aBox.minimum.y, bBox.minimum.y) > std::min(aBox.maximum.y, bBox.maximum.y) + BBOX_TEST_EPS) + return false; + if (std::max(aBox.minimum.z, bBox.minimum.z) > std::min(aBox.maximum.z, bBox.maximum.z) + BBOX_TEST_EPS) + return false; + return true; +} + + + +/** +Test segment vs plane intersection. If segment intersects the plane true is returned. Point of intersection is written into 'result'. +*/ +NV_INLINE bool getPlaneSegmentIntersection(const PxPlane& pl, const PxVec3& a, const PxVec3& b, PxVec3& result) +{ + float div = (b - a).dot(pl.n); + if (PxAbs(div) < 0.0001f) + { + if (pl.contains(a)) + { + result = a; + return true; + } + else + { + return false; + } + } + float t = (-a.dot(pl.n) - pl.d) / div; + if (t < 0.0f || t > 1.0f) + { + return false; + } + result = (b - a) * t + a; + return true; +} + +} // namespace Blast +} // namespace Nv + +#endif
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringMesh.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringMesh.cpp new file mode 100644 index 0000000..a25d2fe --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringMesh.cpp @@ -0,0 +1,558 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#define _CRT_SECURE_NO_WARNINGS + +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastExtAuthoringTypes.h" +#include <string.h> +#include "NvBlastExtAuthoringPerlinNoise.h" + +using physx::PxVec2; +using physx::PxVec3; +using physx::PxBounds3; + +namespace Nv +{ +namespace Blast +{ + +Mesh::Mesh(PxVec3* position, PxVec3* normals, PxVec2* uv, uint32_t verticesCount, uint32_t* indices, uint32_t indicesCount) +{ + + mVertices.resize(verticesCount); + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mVertices[i].p = position[i]; + } + if (normals != 0) + { + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mVertices[i].n = normals[i]; + } + + } + else + { + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mVertices[i].n = PxVec3(0, 0, 0); + } + } + if (uv != 0) + { + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mVertices[i].uv[0] = uv[i]; + } + } + else + { + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mVertices[i].uv[0] = PxVec2(0, 0); + } + } + mEdges.resize(indicesCount); + mFacets.resize(indicesCount / 3); + mBounds.setEmpty(); + for (uint32_t i = 0; i < verticesCount; ++i) + { + mBounds.include(mVertices[i].p); + } + int32_t facetId = 0; + for (uint32_t i = 0; i < indicesCount; i += 3) + { + mEdges[i].s = indices[i]; + mEdges[i].e = indices[i + 1]; + + mEdges[i + 1].s = indices[i + 1]; + mEdges[i + 1].e = indices[i + 2]; + + mEdges[i + 2].s = indices[i + 2]; + mEdges[i + 2].e = indices[i]; + mFacets[facetId].firstEdgeNumber = i; + mFacets[facetId].edgesCount = 3; + facetId++; + } +} + +Mesh::Mesh(Vertex* vertices, Edge* edges, Facet* facets, uint32_t posCount, uint32_t edgesCount, uint32_t facetsCount) +{ + mVertices.resize(posCount); + mEdges.resize(edgesCount); + mFacets.resize(facetsCount); + + memcpy(mVertices.data(), vertices, sizeof(Vertex) * posCount); + memcpy(mEdges.data(), edges, sizeof(Edge) * edgesCount); + memcpy(mFacets.data(), facets, sizeof(Facet) * facetsCount); + mBounds.setEmpty(); + for (uint32_t i = 0; i < posCount; ++i) + { + mBounds.include(mVertices[i].p); + } +} + +float Mesh::getMeshVolume() +{ + /** + Check if mesh boundary consist only of triangles + */ + for (uint32_t i = 0; i < mFacets.size(); ++i) + { + if (mFacets[i].edgesCount != 3) + { + return 0.0f; + } + } + + float volume = 0; + for (uint32_t i = 0; i < mFacets.size(); ++i) + { + int32_t offset = mFacets[i].firstEdgeNumber; + PxVec3& a = mVertices[mEdges[offset].s].p; + PxVec3& b = mVertices[mEdges[offset + 1].s].p; + PxVec3& c = mVertices[mEdges[offset + 2].s].p; + + volume += (a.x * b.y * c.z - a.x * b.z * c.y - a.y * b.x * c.z + a.y * b.z * c.x + a.z * b.x * c.y - a.z * b.y * c.x); + } + return (1.0f / 6.0f) * abs(volume); +} + + +uint32_t Mesh::getFacetCount() +{ + return static_cast<uint32_t>(mFacets.size()); +} + +Vertex* Mesh::getVertices() +{ + return mVertices.data(); +} + +Edge* Mesh::getEdges() +{ + return mEdges.data(); +} + +uint32_t Mesh::getEdgesCount() +{ + return static_cast<uint32_t>(mEdges.size()); +} +uint32_t Mesh::getVerticesCount() +{ + return static_cast<uint32_t>(mVertices.size()); +} +Facet* Mesh::getFacetsBuffer() +{ + return mFacets.data(); +} +Facet* Mesh::getFacet(int32_t facet) +{ + return &mFacets[facet]; +} + + +Mesh::~Mesh() +{ +} + +PxBounds3& Mesh::getBoundingBox() +{ + return mBounds; +} + +void Mesh::recalculateBoundingBox() +{ + mBounds.setEmpty(); + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + mBounds.include(mVertices[i].p); + } +} + + + +void getTangents(PxVec3& normal, PxVec3& t1, PxVec3& t2) +{ + + if (abs(normal.z) < 0.9) + { + t1 = normal.cross(PxVec3(0, 0, 1)); + } + else + { + t1 = normal.cross(PxVec3(1, 0, 0)); + } + t2 = t1.cross(normal); +} + +Mesh* getCuttingBox(const PxVec3& point, const PxVec3& normal, float size, int32_t id) +{ + PxVec3 lNormal = normal.getNormalized(); + PxVec3 t1, t2; + getTangents(lNormal, t1, t2); + + std::vector<Vertex> positions(8); + positions[0].p = point + (t1 + t2) * size; + positions[1].p = point + (t2 - t1) * size; + + positions[2].p = point + (-t1 - t2) * size; + positions[3].p = point + (t1 - t2) * size; + + + positions[4].p = point + (t1 + t2 + lNormal) * size; + positions[5].p = point + (t2 - t1 + lNormal) * size; + + positions[6].p = point + (-t1 - t2 + lNormal) * size; + positions[7].p = point + (t1 - t2 + lNormal) * size; + + positions[0].n = -lNormal; + positions[1].n = -lNormal; + + positions[2].n = -lNormal; + positions[3].n = -lNormal; + + + positions[4].n = -lNormal; + positions[5].n = -lNormal; + + positions[6].n = -lNormal; + positions[7].n = -lNormal; + + positions[0].uv[0] = PxVec2(0, 0); + positions[1].uv[0] = PxVec2(10, 0); + + positions[2].uv[0] = PxVec2(10, 10); + positions[3].uv[0] = PxVec2(0, 10); + + + positions[4].uv[0] = PxVec2(0, 0); + positions[5].uv[0] = PxVec2(10, 0); + + positions[6].uv[0] = PxVec2(10, 10); + positions[7].uv[0] = PxVec2(0, 10); + + + std::vector<Edge> edges; + std::vector<Facet> facets; + + edges.push_back(Edge(0, 1)); + edges.push_back(Edge(1, 2)); + edges.push_back(Edge(2, 3)); + edges.push_back(Edge(3, 0)); + facets.push_back(Facet(0, 4, id)); + + + edges.push_back(Edge(0, 3)); + edges.push_back(Edge(3, 7)); + edges.push_back(Edge(7, 4)); + edges.push_back(Edge(4, 0)); + facets.push_back(Facet(4, 4, id)); + + edges.push_back(Edge(3, 2)); + edges.push_back(Edge(2, 6)); + edges.push_back(Edge(6, 7)); + edges.push_back(Edge(7, 3)); + facets.push_back(Facet(8, 4, id)); + + edges.push_back(Edge(5, 6)); + edges.push_back(Edge(6, 2)); + edges.push_back(Edge(2, 1)); + edges.push_back(Edge(1, 5)); + facets.push_back(Facet(12, 4, id)); + + edges.push_back(Edge(4, 5)); + edges.push_back(Edge(5, 1)); + edges.push_back(Edge(1, 0)); + edges.push_back(Edge(0, 4)); + facets.push_back(Facet(16, 4, id)); + + edges.push_back(Edge(4, 7)); + edges.push_back(Edge(7, 6)); + edges.push_back(Edge(6, 5)); + edges.push_back(Edge(5, 4)); + facets.push_back(Facet(20, 4, id)); + return new Mesh(positions.data(), edges.data(), facets.data(), static_cast<uint32_t>(positions.size()), static_cast<uint32_t>(edges.size()), static_cast<uint32_t>(facets.size())); +} + +void inverseNormalAndSetIndices(Mesh* mesh, int32_t id) +{ + for (uint32_t i = 0; i < mesh->getVerticesCount(); ++i) + { + mesh->getVertices()[i].n *= -1.0f; + } + for (uint32_t i = 0; i < mesh->getFacetCount(); ++i) + { + mesh->getFacet(i)->userData = id; + } + +} + +void setCuttingBox(const PxVec3& point, const PxVec3& normal, Mesh* mesh, float size, int32_t id) +{ + PxVec3 t1, t2; + PxVec3 lNormal = normal.getNormalized(); + getTangents(lNormal, t1, t2); + + Vertex* positions = mesh->getVertices(); + positions[0].p = point + (t1 + t2) * size; + positions[1].p = point + (t2 - t1) * size; + + positions[2].p = point + (-t1 - t2) * size; + positions[3].p = point + (t1 - t2) * size; + + + positions[4].p = point + (t1 + t2 + lNormal) * size; + positions[5].p = point + (t2 - t1 + lNormal) * size; + + positions[6].p = point + (-t1 - t2 + lNormal) * size; + positions[7].p = point + (t1 - t2 + lNormal) * size; + + positions[0].n = -lNormal; + positions[1].n = -lNormal; + + positions[2].n = -lNormal; + positions[3].n = -lNormal; + + + positions[4].n = -lNormal; + positions[5].n = -lNormal; + + positions[6].n = -lNormal; + positions[7].n = -lNormal; + + for (uint32_t i = 0; i < mesh->getFacetCount(); ++i) + { + mesh->getFacet(i)->userData = id; + } + mesh->recalculateBoundingBox(); +} + +bool Mesh::isValid() +{ + return mVertices.size() > 0 && mEdges.size() > 0 && mFacets.size() > 0; +} + +Mesh* getNoisyCuttingBoxPair(const physx::PxVec3& point, const physx::PxVec3& normal, float size, float jaggedPlaneSize, uint32_t resolution, int32_t id, float amplitude, float frequency, int32_t octaves, int32_t seed) +{ + SimplexNoise nEval(amplitude, frequency, octaves, seed); + PxVec3 t1, t2; + PxVec3 lNormal = normal.getNormalized(); + getTangents(lNormal, t1, t2); + + std::vector<Vertex> vertices ((resolution + 1) * (resolution + 1) + 12); + PxVec3 cPosit = point + (t1 + t2) * jaggedPlaneSize; + PxVec3 t1d = -t1 * 2.0f * jaggedPlaneSize / resolution; + PxVec3 t2d = -t2 * 2.0f * jaggedPlaneSize / resolution; + + int32_t vrtId = 0; + for (uint32_t i = 0; i < resolution + 1; ++i) + { + PxVec3 lcPosit = cPosit; + for (uint32_t j = 0; j < resolution + 1; ++j) + { + vertices[vrtId].p = lcPosit; + lcPosit += t1d; + vrtId++; + } + cPosit += t2d; + } + + + for (uint32_t i = 1; i < resolution; ++i) + { + for (uint32_t j = 1; j < resolution; ++j) + { + PxVec3& pnt = vertices[i * (resolution + 1) + j].p; + pnt += lNormal * nEval.sample(pnt); + } + } + + std::vector<Edge> edges; + std::vector<Facet> facets; + for (uint32_t i = 0; i < resolution; ++i) + { + for (uint32_t j = 0; j < resolution; ++j) + { + uint32_t start = edges.size(); + edges.push_back(Edge(i * (resolution + 1) + j, i * (resolution + 1) + j + 1)); + edges.push_back(Edge(i * (resolution + 1) + j + 1, (i + 1) * (resolution + 1) + j + 1)); + edges.push_back(Edge((i + 1) * (resolution + 1) + j + 1, (i + 1) * (resolution + 1) + j)); + edges.push_back(Edge((i + 1) * (resolution + 1) + j, i * (resolution + 1) + j)); + facets.push_back(Facet(start, 4, id)); + } + } + uint32_t offset = (resolution + 1) * (resolution + 1); + + vertices[0 + offset].p = point + (t1 + t2) * size; + vertices[1 + offset].p = point + (t2 - t1) * size; + + vertices[2 + offset].p = point + (-t1 - t2) * size; + vertices[3 + offset].p = point + (t1 - t2) * size; + + vertices[8 + offset].p = point + (t1 + t2) * jaggedPlaneSize; + vertices[9 + offset].p = point + (t2 - t1) * jaggedPlaneSize; + + vertices[10 + offset].p = point + (-t1 - t2) * jaggedPlaneSize; + vertices[11 + offset].p = point + (t1 - t2) * jaggedPlaneSize; + + + vertices[4 + offset].p = point + (t1 + t2 + lNormal) * size; + vertices[5 + offset].p = point + (t2 - t1 + lNormal) * size; + + vertices[6 + offset].p = point + (-t1 - t2 + lNormal) * size; + vertices[7 + offset].p = point + (t1 - t2 + lNormal) * size; + + for (uint32_t i = 1; i < resolution; ++i) + { + for (uint32_t j = 1; j < resolution; ++j) + { + PxVec3 v1 = vertices[(resolution + 1) * (i + 1) + j].p - vertices[(resolution + 1) * i + j].p; + PxVec3 v2 = vertices[(resolution + 1) * (i) + j + 1].p - vertices[(resolution + 1) * i + j].p; + PxVec3 v3 = vertices[(resolution + 1) * (i - 1) + j].p - vertices[(resolution + 1) * i + j].p; + PxVec3 v4 = vertices[(resolution + 1) * (i) + j - 1].p - vertices[(resolution + 1) * i + j].p; + + vertices[(resolution + 1) * i + j].n = v1.cross(v2) + v2.cross(v3) + v3.cross(v4) + v4.cross(v1); + vertices[(resolution + 1) * i + j].n.normalize(); + } + } + + int32_t edgeOffset = edges.size(); + edges.push_back(Edge(0 + offset, 1 + offset)); + edges.push_back(Edge(1 + offset, 2 + offset)); + edges.push_back(Edge(2 + offset, 3 + offset)); + edges.push_back(Edge(3 + offset, 0 + offset)); + + edges.push_back(Edge(11 + offset, 10 + offset)); + edges.push_back(Edge(10 + offset, 9 + offset)); + edges.push_back(Edge(9 + offset, 8 + offset)); + edges.push_back(Edge(8 + offset, 11 + offset)); + + facets.push_back(Facet(edgeOffset, 8, id)); + + + + edges.push_back(Edge(0 + offset, 3 + offset)); + edges.push_back(Edge(3 + offset, 7 + offset)); + edges.push_back(Edge(7 + offset, 4 + offset)); + edges.push_back(Edge(4 + offset, 0 + offset)); + facets.push_back(Facet(8 + edgeOffset, 4, id)); + + edges.push_back(Edge(3 + offset, 2 + offset)); + edges.push_back(Edge(2 + offset, 6 + offset)); + edges.push_back(Edge(6 + offset, 7 + offset)); + edges.push_back(Edge(7 + offset, 3 + offset)); + facets.push_back(Facet(12 + edgeOffset, 4, id)); + + edges.push_back(Edge(5 + offset, 6 + offset)); + edges.push_back(Edge(6 + offset, 2 + offset)); + edges.push_back(Edge(2 + offset, 1 + offset)); + edges.push_back(Edge(1 + offset, 5 + offset)); + facets.push_back(Facet(16 + edgeOffset, 4, id)); + + edges.push_back(Edge(4 + offset, 5 + offset)); + edges.push_back(Edge(5 + offset, 1 + offset)); + edges.push_back(Edge(1 + offset, 0 + offset)); + edges.push_back(Edge(0 + offset, 4 + offset)); + facets.push_back(Facet(20 + edgeOffset, 4, id)); + + edges.push_back(Edge(4 + offset, 7 + offset)); + edges.push_back(Edge(7 + offset, 6 + offset)); + edges.push_back(Edge(6 + offset, 5 + offset)); + edges.push_back(Edge(5 + offset, 4 + offset)); + facets.push_back(Facet(24 + edgeOffset, 4, id)); + + // + return new Mesh(vertices.data(), edges.data(), facets.data(), vertices.size(), edges.size(), facets.size()); +} + +Mesh* getBigBox(const PxVec3& point, float size) +{ + PxVec3 normal(0, 0, 1); + normal.normalize(); + PxVec3 t1, t2; + getTangents(normal, t1, t2); + + std::vector<Vertex> positions(8); + positions[0].p = point + (t1 + t2 - normal) * size; + positions[1].p = point + (t2 - t1 - normal) * size; + + positions[2].p = point + (-t1 - t2 - normal) * size; + positions[3].p = point + (t1 - t2 - normal) * size; + + + positions[4].p = point + (t1 + t2 + normal) * size; + positions[5].p = point + (t2 - t1 + normal) * size; + + positions[6].p = point + (-t1 - t2 + normal) * size; + positions[7].p = point + (t1 - t2 + normal) * size; + + positions[0].uv[0] = PxVec2(0, 0); + positions[1].uv[0] = PxVec2(10, 0); + + positions[2].uv[0] = PxVec2(10, 10); + positions[3].uv[0] = PxVec2(0, 10); + + + positions[4].uv[0] = PxVec2(0, 0); + positions[5].uv[0] = PxVec2(10, 0); + + positions[6].uv[0] = PxVec2(10, 10); + positions[7].uv[0] = PxVec2(0, 10); + + + std::vector<Edge> edges; + std::vector<Facet> facets; + + edges.push_back(Edge(0, 1)); + edges.push_back(Edge(1, 2)); + edges.push_back(Edge(2, 3)); + edges.push_back(Edge(3, 0)); + facets.push_back(Facet(0, 4)); + + + edges.push_back(Edge(0, 3)); + edges.push_back(Edge(3, 7)); + edges.push_back(Edge(7, 4)); + edges.push_back(Edge(4, 0)); + facets.push_back(Facet(4, 4)); + + edges.push_back(Edge(3, 2)); + edges.push_back(Edge(2, 6)); + edges.push_back(Edge(6, 7)); + edges.push_back(Edge(7, 3)); + facets.push_back(Facet(8, 4)); + + edges.push_back(Edge(5, 6)); + edges.push_back(Edge(6, 2)); + edges.push_back(Edge(2, 1)); + edges.push_back(Edge(1, 5)); + facets.push_back(Facet(12, 4)); + + edges.push_back(Edge(4, 5)); + edges.push_back(Edge(5, 1)); + edges.push_back(Edge(1, 0)); + edges.push_back(Edge(0, 4)); + facets.push_back(Facet(16, 4)); + + edges.push_back(Edge(4, 7)); + edges.push_back(Edge(7, 6)); + edges.push_back(Edge(6, 5)); + edges.push_back(Edge(5, 4)); + facets.push_back(Facet(20, 4)); + for (int i = 0; i < 8; ++i) + positions[i].n = PxVec3(0, 0, 0); + return new Mesh(positions.data(), edges.data(), facets.data(), static_cast<uint32_t>(positions.size()), static_cast<uint32_t>(edges.size()), static_cast<uint32_t>(facets.size())); +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringPerlinNoise.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringPerlinNoise.h new file mode 100644 index 0000000..95308c2 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringPerlinNoise.h @@ -0,0 +1,373 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGPERLINNOISE_H +#define NVBLASTEXTAUTHORINGPERLINNOISE_H + + +#include <NvBlastExtAuthoringTypes.h> + +#include <PxVec4.h> +#include <PxVec3.h> + +#define PERLIN_NOISE_SAMPLE_TABLE 512 +using physx::PxVec3; +namespace Nv +{ +namespace Blast +{ + +/*********** + Noise generation routines, copied from Apex. +*/ + + +NV_INLINE float at3(const float& rx, const float& ry, const float& rz, const PxVec3 q) +{ + return rx * q[0] + ry * q[1] + rz * q[2]; +} + +NV_INLINE float fade(float t) { return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); } + +NV_INLINE float lerp(float t, float a, float b) { return a + t * (b - a); } + +NV_INLINE void setup(int i, PxVec3 point, float& t, int& b0, int& b1, float& r0, float& r1) +{ + t = point[i] + (0x1000); + b0 = ((int)t) & (PERLIN_NOISE_SAMPLE_TABLE - 1); + b1 = (b0 + 1) & (PERLIN_NOISE_SAMPLE_TABLE - 1); + r0 = t - (int)t; + r1 = r0 - 1.0f; +} + + +NV_INLINE float noiseSample(PxVec3 point, int* p, PxVec3* g) +{ + int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; + float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v; + PxVec3 q; + int i, j; + + setup(0, point, t, bx0, bx1, rx0, rx1); + setup(1, point, t, by0, by1, ry0, ry1); + setup(2, point, t, bz0, bz1, rz0, rz1); + + i = p[bx0]; + j = p[bx1]; + + b00 = p[i + by0]; + b10 = p[j + by0]; + b01 = p[i + by1]; + b11 = p[j + by1]; + + t = fade(rx0); + sy = fade(ry0); + sz = fade(rz0); + + q = g[b00 + bz0]; u = at3(rx0, ry0, rz0, q); + q = g[b10 + bz0]; v = at3(rx1, ry0, rz0, q); + a = lerp(t, u, v); + + q = g[b01 + bz0]; u = at3(rx0, ry1, rz0, q); + q = g[b11 + bz0]; v = at3(rx1, ry1, rz0, q); + b = lerp(t, u, v); + + c = lerp(sy, a, b); + + q = g[b00 + bz1]; u = at3(rx0, ry0, rz1, q); + q = g[b10 + bz1]; v = at3(rx1, ry0, rz1, q); + a = lerp(t, u, v); + + q = g[b01 + bz1]; u = at3(rx0, ry1, rz1, q); + q = g[b11 + bz1]; v = at3(rx1, ry1, rz1, q); + b = lerp(t, u, v); + + d = lerp(sy, a, b); + + return lerp(sz, c, d); +} + +/** + Perlin Noise generation tool +*/ +class PerlinNoise +{ +public: + /** + \param[in] rnd Random value generator + \param[in] octaves Number of noise octaves + \param[in] frequency Frequency of noise + \param[in] amplitude Amplitude of noise + */ + PerlinNoise(Nv::Blast::RandomGeneratorBase* rnd, int octaves = 1, float frequency = 1., float amplitude = 1.) + : mRnd(rnd), + mOctaves(octaves), + mFrequency(frequency), + mAmplitude(amplitude), + mbInit(false) + { + + } + + /* + Reset state of noise generator + \param[in] octaves Number of noise octaves + \param[in] frequency Frequency of noise + \param[in] amplitude Amplitude of noise + */ + void reset(int octaves = 1, float frequency = 1.f, float amplitude = 1.f) + { + mOctaves = octaves; + mFrequency = frequency; + mAmplitude = amplitude; + init(); + } + + /** + Get Perlin Noise value at given point + */ + float sample(const physx::PxVec3& point) + { + return perlinNoise(point); + } + +private: + PerlinNoise& operator=(const PerlinNoise&); + + float perlinNoise(physx::PxVec3 point) + { + if (!mbInit) + init(); + + const int octaves = mOctaves; + const float frequency = mFrequency; + float amplitude = mAmplitude; + float result = 0.0f; + + point *= frequency; + + for (int i = 0; i < octaves; ++i) + { + PxVec3 lpnt; + lpnt[0] = point.x; + lpnt[1] = point.y; + lpnt[2] = point.z; + result += (noiseSample(lpnt, p, g)) * amplitude; + point *= 2.0f; + amplitude *= 0.5f; + } + return result; + } + + void init(void) + { + mbInit = true; + + unsigned i, j; + int k; + + for (i = 0; i < (unsigned)PERLIN_NOISE_SAMPLE_TABLE; i++) + { + p[i] = (int)i; + for (j = 0; j < 3; ++j) + g[i][j] = mRnd->getRandomValue(); + g[i].normalize(); + } + + while (--i) + { + k = p[i]; + j = static_cast<uint32_t>(mRnd->getRandomValue() * PERLIN_NOISE_SAMPLE_TABLE); + p[i] = p[j]; + p[j] = k; + } + + for (i = 0; i < PERLIN_NOISE_SAMPLE_TABLE + 2; ++i) + { + p[(unsigned)PERLIN_NOISE_SAMPLE_TABLE + i] = p[i]; + for (j = 0; j < 3; ++j) + g[(unsigned)PERLIN_NOISE_SAMPLE_TABLE + i][j] = g[i][j]; + } + + } + + Nv::Blast::RandomGeneratorBase* mRnd; + int mOctaves; + float mFrequency; + float mAmplitude; + + // Permutation vector + int p[(unsigned)(PERLIN_NOISE_SAMPLE_TABLE + PERLIN_NOISE_SAMPLE_TABLE + 2)]; + // Gradient vector + PxVec3 g[(unsigned)(PERLIN_NOISE_SAMPLE_TABLE + PERLIN_NOISE_SAMPLE_TABLE + 2)]; + + bool mbInit; +}; + + +/** + Simplex noise generation tool +*/ +class SimplexNoise +{ + + int32_t mOctaves; + float mAmplitude; + float mFrequency; + int32_t mSeed; + + static const int X_NOISE_GEN = 1619; + static const int Y_NOISE_GEN = 31337; + static const int Z_NOISE_GEN = 6971; + static const int W_NOISE_GEN = 1999; + static const int SEED_NOISE_GEN = 1013; + static const int SHIFT_NOISE_GEN = 8; + + NV_INLINE int fastfloor(float x) + { + return (x >= 0) ? (int)x : (int)(x - 1); + } + + SimplexNoise& operator=(const SimplexNoise&) + { + return *this; + } + +public: + /** + \param[in] ampl Amplitude of noise + \param[in] freq Frequency of noise + \param[in] octaves Number of noise octaves + \param[in] seed Random seed value + */ + SimplexNoise(float ampl, float freq, int32_t octaves, int32_t seed) : mOctaves(octaves), mAmplitude(ampl), mFrequency(freq), mSeed(seed) {}; + // 4D simplex noise + // returns: (x,y,z) = noise grad, w = noise value + + /** + Evaluate noise at given 4d-point + \param[in] x x coordinate of point + \param[in] y y coordinate of point + \param[in] z z coordinate of point + \param[in] w w coordinate of point + \param[in] seed Random seed value + \return Noise valued vector (x,y,z) and scalar (w) + */ + physx::PxVec4 eval4D(float x, float y, float z, float w, int seed) + { + // The skewing and unskewing factors are hairy again for the 4D case + const float F4 = (physx::PxSqrt(5.0f) - 1.0f) / 4.0f; + const float G4 = (5.0f - physx::PxSqrt(5.0f)) / 20.0f; + // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in + float s = (x + y + z + w) * F4; // Factor for 4D skewing + int ix = fastfloor(x + s); + int iy = fastfloor(y + s); + int iz = fastfloor(z + s); + int iw = fastfloor(w + s); + float tu = (ix + iy + iz + iw) * G4; // Factor for 4D unskewing + // Unskew the cell origin back to (x,y,z,w) space + float x0 = x - (ix - tu); // The x,y,z,w distances from the cell origin + float y0 = y - (iy - tu); + float z0 = z - (iz - tu); + float w0 = w - (iw - tu); + + int c = (x0 > y0) ? (1 << 0) : (1 << 2); + c += (x0 > z0) ? (1 << 0) : (1 << 4); + c += (x0 > w0) ? (1 << 0) : (1 << 6); + c += (y0 > z0) ? (1 << 2) : (1 << 4); + c += (y0 > w0) ? (1 << 2) : (1 << 6); + c += (z0 > w0) ? (1 << 4) : (1 << 6); + + physx::PxVec4 res; + res.setZero(); + + // Calculate the contribution from the five corners + for (int p = 4; p >= 0; --p) + { + int ixp = ((c >> 0) & 3) >= p ? 1 : 0; + int iyp = ((c >> 2) & 3) >= p ? 1 : 0; + int izp = ((c >> 4) & 3) >= p ? 1 : 0; + int iwp = ((c >> 6) & 3) >= p ? 1 : 0; + + float xp = x0 - ixp + (4 - p) * G4; + float yp = y0 - iyp + (4 - p) * G4; + float zp = z0 - izp + (4 - p) * G4; + float wp = w0 - iwp + (4 - p) * G4; + + float t = 0.6f - xp * xp - yp * yp - zp * zp - wp * wp; + if (t > 0) + { + //get index + int gradIndex = int(( + X_NOISE_GEN * (ix + ixp) + + Y_NOISE_GEN * (iy + iyp) + + Z_NOISE_GEN * (iz + izp) + + W_NOISE_GEN * (iw + iwp) + + SEED_NOISE_GEN * seed) + & 0xffffffff); + gradIndex ^= (gradIndex >> SHIFT_NOISE_GEN); + gradIndex &= 31; + + physx::PxVec4 g; + { + const int h = gradIndex; + const int hs = 2 - (h >> 4); + const int h1 = (h >> 3); + g.x = (h1 == 0) ? 0.0f : ((h & 4) ? -1.0f : 1.0f); + g.y = (h1 == 1) ? 0.0f : ((h & (hs << 1)) ? -1.0f : 1.0f); + g.z = (h1 == 2) ? 0.0f : ((h & hs) ? -1.0f : 1.0f); + g.w = (h1 == 3) ? 0.0f : ((h & 1) ? -1.0f : 1.0f); + } + float gdot = (g.x * xp + g.y * yp + g.z * zp + g.w * wp); + + float t2 = t * t; + float t3 = t2 * t; + float t4 = t3 * t; + + float dt4gdot = 8 * t3 * gdot; + + res.x += t4 * g.x - dt4gdot * xp; + res.y += t4 * g.y - dt4gdot * yp; + res.z += t4 * g.z - dt4gdot * zp; + res.w += t4 * gdot; + } + } + // scale the result to cover the range [-1,1] + res *= 27; + return res; + } + + /** + Evaluate noise at given 3d-point + \param[in] p Point in which noise will be evaluated + \return Noise value at given point + */ + float sample(physx::PxVec3 p) + { + p *= mFrequency; + float result = 0.0f; + float alpha = 1; + for (int32_t i = 1; i <= mOctaves; ++i) + { + result += eval4D(p.x * i, p.y * i, p.z * i, i * 5.0f, mSeed).w * alpha; + alpha *= 0.45; + } + return result * mAmplitude; + } + +}; + + + } // Blast namespace +} // Nv namespace + + + +#endif
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.cpp new file mode 100644 index 0000000..0b7187f --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.cpp @@ -0,0 +1,1439 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +// This warning arises when using some stl containers with older versions of VC +// c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1826): warning C4702: unreachable code +#include "NvPreprocessor.h" +#if NV_VC && NV_VC < 14 +#pragma warning(disable : 4702) +#endif +#include "NvBlastExtAuthoringTriangulator.h" +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastExtAuthoringTypes.h" +#include <math.h> +#include "NvPreprocessor.h" +#include <algorithm> +#include <vector> +#include <set> +#include "NvBlastExtAuthoringBooleanTool.h" +#include <queue> +#include "NvBlastExtAuthoringPerlinNoise.h" +#include <NvBlastAssert.h> + +using physx::PxVec3; +using physx::PxVec2; + +#define VEC_COMPARISON_OFFSET 1e-5f +#define TWO_VERTICES_THRESHOLD 1e-7 + +namespace Nv +{ +namespace Blast +{ + +bool VrtComp::operator()(const Vertex& a, const Vertex& b) const +{ + if (a.p.x + VEC_COMPARISON_OFFSET < b.p.x) return true; + if (a.p.x - VEC_COMPARISON_OFFSET > b.p.x) return false; + if (a.p.y + VEC_COMPARISON_OFFSET < b.p.y) return true; + if (a.p.y - VEC_COMPARISON_OFFSET > b.p.y) return false; + if (a.p.z + VEC_COMPARISON_OFFSET < b.p.z) return true; + if (a.p.z - VEC_COMPARISON_OFFSET > b.p.z) return false; + + if (a.n.x + 1e-3 < b.n.x) return true; + if (a.n.x - 1e-3 > b.n.x) return false; + if (a.n.y + 1e-3 < b.n.y) return true; + if (a.n.y - 1e-3 > b.n.y) return false; + if (a.n.z + 1e-3 < b.n.z) return true; + if (a.n.z - 1e-3 > b.n.z) return false; + + + if (a.uv[0].x + 1e-3 < b.uv[0].x) return true; + if (a.uv[0].x - 1e-3 > b.uv[0].x) return false; + if (a.uv[0].y + 1e-3 < b.uv[0].y) return true; + return false; +} + +bool VrtPositionComparator::operator()(const PxVec3& a, const PxVec3& b) const +{ + if (a.x + VEC_COMPARISON_OFFSET < b.x) return true; + if (a.x - VEC_COMPARISON_OFFSET > b.x) return false; + if (a.y + VEC_COMPARISON_OFFSET < b.y) return true; + if (a.y - VEC_COMPARISON_OFFSET > b.y) return false; + if (a.z + VEC_COMPARISON_OFFSET < b.z) return true; + if (a.z - VEC_COMPARISON_OFFSET > b.z) return false; + return false; +} + +NV_FORCE_INLINE bool compareTwoVertices(const PxVec3& a, const PxVec3& b) +{ + return std::abs(b.x - a.x) < TWO_VERTICES_THRESHOLD && std::abs(b.y - a.y) < TWO_VERTICES_THRESHOLD && std::abs(b.z - a.z) < TWO_VERTICES_THRESHOLD; +} +NV_FORCE_INLINE bool compareTwoVertices(const PxVec2& a, const PxVec2& b) +{ + return std::abs(b.x - a.x) < TWO_VERTICES_THRESHOLD && std::abs(b.y - a.y) < TWO_VERTICES_THRESHOLD; +} + +NV_FORCE_INLINE float getRotation(const PxVec2& a, const PxVec2& b) +{ + return a.x * b.y - a.y * b.x; +} + +inline bool pointInside(PxVec2 a, PxVec2 b, PxVec2 c, PxVec2 pnt) +{ + if (compareTwoVertices(a, pnt) || compareTwoVertices(b, pnt) || compareTwoVertices(c, pnt)) + { + return false; + } + float v1 = (getRotation((b - a), (pnt - a))); + float v2 = (getRotation((c - b), (pnt - b))); + float v3 = (getRotation((a - c), (pnt - c))); + + return (v1 >= 0.0f && v2 >= 0.0f && v3 >= 0.0f) || + (v1 <= 0.0f && v2 <= 0.0f && v3 <= 0.0f); + +} +void ChunkPostProcessor::triangulatePolygonWithEarClipping(std::vector<uint32_t>& inputPolygon, Vertex* vert, ProjectionDirections dir) +{ + // return; + //for (uint32_t i = 0; i < inputPolygon.size(); ++i) + //{ + // mBaseMeshTriangles.push_back(TriangleIndexed(inputPolygon[i], inputPolygon[i], inputPolygon[(i + 1) % inputPolygon.size()])); + //} + //return; + int32_t vCount = static_cast<int32_t>(inputPolygon.size()); + + if (vCount < 3) + { + return; + } + for (int32_t curr = 0; curr < vCount && vCount > 2; ++curr) + { + int32_t prev = (curr == 0) ? vCount - 1 : curr - 1; + int32_t next = (curr == vCount - 1) ? 0 : curr + 1; + + Vertex cV = vert[inputPolygon[curr]]; + Vertex nV = vert[inputPolygon[prev]]; + Vertex pV = vert[inputPolygon[next]]; + + PxVec2 cVp = getProjectedPoint(cV.p, dir); + PxVec2 nVp = getProjectedPoint(nV.p, dir); + PxVec2 pVp = getProjectedPoint(pV.p, dir); + + // Check wheather curr is ear-tip + float rot = getRotation((pVp - nVp).getNormalized(), (cVp - nVp).getNormalized()); + if (!(dir & OPPOSITE_WINDING)) rot = -rot; + if (rot > 0.0001) + { + bool good = true; + for (int vrt = 0; vrt < vCount; ++vrt) + { + if (vrt == curr || vrt == prev || vrt == next) continue; + if (pointInside(cVp, nVp, pVp, getProjectedPoint(vert[inputPolygon[vrt]].p, dir))) + { + good = false; + break; + } + } + if (good) + { + addEdgeTr(Edge(inputPolygon[curr], inputPolygon[prev])); + addEdgeTr(Edge(inputPolygon[next], inputPolygon[prev])); + addEdgeTr(Edge(inputPolygon[curr], inputPolygon[next])); + + mBaseMeshTriangles.push_back(TriangleIndexed(inputPolygon[curr], inputPolygon[prev], inputPolygon[next])); + vCount--; + inputPolygon.erase(inputPolygon.begin() + curr); + curr = -1; + } + } + } +} + + + +struct LoopInfo +{ + LoopInfo() + { + used = false; + } + PxVec3 normal; + float area; + int32_t index; + bool used; + bool operator<(const LoopInfo& b) const + { + return area < b.area; + } +}; + +int32_t unitePolygons(std::vector<uint32_t>& externalLoop, std::vector<uint32_t>& internalLoop, Vertex* vrx, ProjectionDirections dir) +{ + if (externalLoop.size() < 3 || internalLoop.size() < 3) + return 1; + /** + Find point with maximum x-coordinate + */ + float x_max = -MAXIMUM_EXTENT; + int32_t mIndex = -1; + for (uint32_t i = 0; i < internalLoop.size(); ++i) + { + float nx = getProjectedPoint(vrx[internalLoop[i]].p, dir).x; + if (nx > x_max) + { + mIndex = i; + x_max = nx; + } + } + if (mIndex == -1) + { + return 1; + } + + /** + Search for base point on external loop + */ + float minX = MAXIMUM_EXTENT; + int32_t vrtIndex = -1; + bool isFromBuffer = 0; + PxVec2 holePoint = getProjectedPoint(vrx[internalLoop[mIndex]].p, dir); + PxVec2 computedPoint; + for (uint32_t i = 0; i < externalLoop.size(); ++i) + { + int32_t nx = (i + 1) % externalLoop.size(); + PxVec2 pnt1 = getProjectedPoint(vrx[externalLoop[i]].p, dir); + PxVec2 pnt2 = getProjectedPoint(vrx[externalLoop[nx]].p, dir); + if (pnt1.x < x_max && pnt2.x < x_max) + { + continue; + } + PxVec2 vc = pnt2 - pnt1; + if (vc.y == 0 && pnt1.y == holePoint.y) + { + if (pnt1.x < minX && pnt1.x < pnt2.x && pnt1.x > x_max) + { + minX = pnt1.x; + vrtIndex = i; + isFromBuffer = true; + } + if (pnt2.x < minX && pnt2.x < pnt1.x && pnt2.x > x_max) + { + minX = pnt2.x; + vrtIndex = nx; + isFromBuffer = true; + } + } + else + { + float t = (holePoint.y - pnt1.y) / vc.y; + if (t <= 1 && t >= 0) + { + PxVec2 tempPoint = vc * t + pnt1; + if (tempPoint.x < minX && tempPoint.x > x_max) + { + minX = tempPoint.x; + vrtIndex = i; + isFromBuffer = false; + computedPoint = tempPoint; + } + } + } + } + if (vrtIndex == -1) + { + // std::cout << "Triangulation: base vertex for inner loop is not found..." << std::endl; + return 1; + } + int32_t bridgePoint = -1; + float bestAngle = 100; + if (!isFromBuffer) + { + PxVec2 ex1 = getProjectedPoint(vrx[externalLoop[vrtIndex]].p, dir); + PxVec2 ex2 = getProjectedPoint(vrx[externalLoop[(vrtIndex + 1) % externalLoop.size()]].p, dir); + + if (ex1.x > ex2.x) + { + vrtIndex = (vrtIndex + 1) % externalLoop.size(); + ex1 = ex2; + } + /* Check if some point is inside triangle */ + bool notFound = true; + for (int32_t i = 0; i < (int32_t)externalLoop.size(); ++i) + { + PxVec2 tempPoint = getProjectedPoint(vrx[externalLoop[i]].p, dir); + if (pointInside(holePoint, ex1, computedPoint, tempPoint)) + { + notFound = false; + PxVec2 cVp = getProjectedPoint(vrx[externalLoop[i]].p, dir); + PxVec2 pVp = getProjectedPoint(vrx[externalLoop[(i - 1 + externalLoop.size()) % externalLoop.size()]].p, dir); + PxVec2 nVp = getProjectedPoint(vrx[externalLoop[(i + 1) % externalLoop.size()]].p, dir); + float rt = getRotation((cVp - pVp).getNormalized(), (nVp - pVp).getNormalized()); + if ((dir & OPPOSITE_WINDING)) rt = -rt; + if (rt < 0.000001) + continue; + float tempAngle = PxVec2(1, 0).dot((tempPoint - holePoint).getNormalized()); + if (bestAngle < tempAngle) + { + bestAngle = tempAngle; + bridgePoint = i; + } + } + } + if (notFound) + { + bridgePoint = vrtIndex; + } + if (bridgePoint == -1) + { + // std::cout << "Triangulation: bridge vertex for inner loop is not found..." << std::endl; + return 1; + } + } + else + { + bridgePoint = vrtIndex; + } + std::vector<uint32_t> temporal; + + for (int32_t i = 0; i <= bridgePoint; ++i) + { + temporal.push_back(externalLoop[i]); + } + temporal.push_back(internalLoop[mIndex]); + for (int32_t i = (mIndex + 1) % internalLoop.size(); i != mIndex; i = (i + 1) % internalLoop.size()) + { + temporal.push_back(internalLoop[i]); + } + temporal.push_back(internalLoop[mIndex]); + for (uint32_t i = bridgePoint; i < externalLoop.size(); ++i) + { + temporal.push_back(externalLoop[i]); + } + externalLoop = temporal; + return 0; +} + +void ChunkPostProcessor::buildPolygonAndTriangulate(std::vector<Edge>& edges, Vertex* vertices, int32_t userData) +{ + std::vector<std::vector<uint32_t> > serializedLoops; + + std::set<int> visitedVertices; + std::vector<int> used(edges.size(), 0); + uint32_t collected = 0; + + std::vector<int> edgesIds; + /** + Add first edge to polygon + */ + edgesIds.push_back(0); + visitedVertices.insert(edges[0].s); + visitedVertices.insert(edges[0].e); + used[0] = true; + collected = 1; + uint32_t lastEdge = 0; + bool successfullPass = false; + for (; collected < edges.size();) + { + successfullPass = false; + for (uint32_t p = 0; p < edges.size(); ++p) + { + if (used[p] == 0 && edges[p].s == edges[lastEdge].e) + { + successfullPass = true; + collected++; + used[p] = true; + edgesIds.push_back(p); + lastEdge = p; + if (visitedVertices.find(edges[p].e) != visitedVertices.end()) // if we formed loop, detach it and triangulate + { + serializedLoops.push_back(std::vector<uint32_t>()); + std::vector<uint32_t>& serializedPositions = serializedLoops.back(); + while (edgesIds.size() > 0) + { + serializedPositions.push_back(edges[edgesIds.back()].s); + visitedVertices.erase(edges[edgesIds.back()].s); + if (edges[edgesIds.back()].s == edges[p].e) + { + edgesIds.pop_back(); + break; + } + edgesIds.pop_back(); + } + if (edgesIds.size() > 0) + { + lastEdge = edgesIds.back(); + } + else + { + for (uint32_t t = 0; t < edges.size(); ++t) + { + if (used[t] == 0) + { + edgesIds.push_back(t); + visitedVertices.insert(edges[t].s); + visitedVertices.insert(edges[t].e); + used[t] = true; + collected++; + lastEdge = t; + break; + } + } + } + } + else + { + visitedVertices.insert(edges[p].e); + } + } + } + if (!successfullPass) + { + break; + } + } + + std::vector<LoopInfo> loopsInfo(serializedLoops.size()); + // Compute normal to whole polygon, and areas of loops + PxVec3 wholeFacetNormal(0, 0, 0); + for (uint32_t loop = 0; loop < serializedLoops.size(); ++loop) + { + PxVec3 loopNormal(0, 0, 0); + std::vector<uint32_t>& pos = serializedLoops[loop]; + for (uint32_t vrt = 1; vrt + 1 < serializedLoops[loop].size(); ++vrt) + { + loopNormal += (vertices[pos[vrt]].p - vertices[pos[0]].p).cross(vertices[pos[vrt + 1]].p - vertices[pos[0]].p); + } + loopsInfo[loop].area = loopNormal.magnitude(); + loopsInfo[loop].normal = loopNormal; + loopsInfo[loop].index = loop; + wholeFacetNormal += loopNormal; + } + + // Change areas signs according to winding direction + for (uint32_t loop = 0; loop < serializedLoops.size(); ++loop) + { + if (wholeFacetNormal.dot(loopsInfo[loop].normal) < 0) + { + loopsInfo[loop].area = -loopsInfo[loop].area; + } + } + ProjectionDirections dir = getProjectionDirection(wholeFacetNormal); + std::sort(loopsInfo.begin(), loopsInfo.end()); + + std::vector<PxVec3> tempPositions; + int32_t oldSize = static_cast<int32_t>(mBaseMeshTriangles.size()); + for (uint32_t extPoly = 0; extPoly < loopsInfo.size(); ++extPoly) + { + if (loopsInfo[extPoly].area < 0) + { + continue; // Polygon with negative area is hole + } + int32_t baseLoop = loopsInfo[extPoly].index; + for (uint32_t intPoly = 0; intPoly < loopsInfo.size(); ++intPoly) + { + if (loopsInfo[intPoly].area > 0 || loopsInfo[intPoly].used || abs(loopsInfo[intPoly].area) > loopsInfo[extPoly].area) + { + continue; + } + int32_t holeLoop = loopsInfo[intPoly].index; + + if (!unitePolygons(serializedLoops[baseLoop], serializedLoops[holeLoop], vertices, dir)) + { + loopsInfo[intPoly].used = true; + }; + } + triangulatePolygonWithEarClipping(serializedLoops[baseLoop],vertices, dir); + } + for (uint32_t i = oldSize; i < mBaseMeshTriangles.size(); ++i) + { + mBaseMeshTriangles[i].userInfo = userData; + } +} + +NV_FORCE_INLINE int32_t ChunkPostProcessor::addVerticeIfNotExist(Vertex& p) +{ + auto it = mVertMap.find(p); + if (it == mVertMap.end()) + { + mVertMap[p] = static_cast<int32_t>(mVertices.size()); + mVertices.push_back(p); + return static_cast<int32_t>(mVertices.size()) - 1; + } + else + { + return it->second; + } +} + +NV_FORCE_INLINE void ChunkPostProcessor::addEdgeIfValid(EdgeWithParent& ed) +{ + if (ed.s == ed.e) + return; + EdgeWithParent opposite(ed.e, ed.s, ed.parent); + auto it = mEdgeMap.find(opposite); + if (it == mEdgeMap.end()) + { + mEdgeMap[ed] = static_cast<int32_t>(mBaseMeshEdges.size()); + mBaseMeshEdges.push_back(ed); + } + else + { + if (mBaseMeshEdges[it->second].s == NOT_VALID_VERTEX) + { + mBaseMeshEdges[it->second].s = ed.s; + mBaseMeshEdges[it->second].e = ed.e; + } + mBaseMeshEdges[it->second].s = NOT_VALID_VERTEX; + } +} + + + +void ChunkPostProcessor::prepare(Mesh* mesh) +{ + Edge* ed = mesh->getEdges(); + Vertex* vr = mesh->getVertices(); + mBaseMapping.resize(mesh->getVerticesCount()); + for (uint32_t i = 0; i < mesh->getFacetCount(); ++i) + { + Facet* fc = mesh->getFacet(i); + for (uint32_t j = fc->firstEdgeNumber; j < fc->firstEdgeNumber + fc->edgesCount; ++j) + { + int32_t a = addVerticeIfNotExist(vr[ed[j].s]); + int32_t b = addVerticeIfNotExist(vr[ed[j].e]); + mBaseMapping[ed[j].s] = a; + mBaseMapping[ed[j].e] = b; + EdgeWithParent e(a, b, i); + addEdgeIfValid(e); + } + } + std::vector<EdgeWithParent> temp; + temp.reserve(mBaseMeshEdges.size()); + for (uint32_t i = 0; i < mBaseMeshEdges.size(); ++i) + { + if (mBaseMeshEdges[i].s != NOT_VALID_VERTEX) + { + temp.push_back(mBaseMeshEdges[i]); + } + } + +} + +void ChunkPostProcessor::reset() +{ + isTesselated = false; + mVertices.clear(); + mBaseMeshEdges.clear(); + mVertMap.clear(); + mEdgeMap.clear(); + mTrMeshEdgeMap.clear(); + mTrMeshEdges.clear(); + mTrMeshEdToTr.clear(); + mBaseMeshTriangles.clear(); + mEdgeFlag.clear(); + mVertexValence.clear(); + mRestrictionFlag.clear(); + mVerticesDistances.clear(); + mVerticesNormalsSmoothed.clear(); + + mBaseMeshResultTriangles.clear(); + mTesselatedMeshResultTriangles.clear(); + mTesselatedMeshTriangles.clear(); +} + +void ChunkPostProcessor::triangulate(Mesh* mesh) +{ + reset(); + if (mesh == nullptr || !mesh->isValid()) + { + return; + } + prepare(mesh); + if (mBaseMeshEdges.empty()) + { + return; + } + std::vector<Edge> temp; + int32_t fP = mBaseMeshEdges[0].parent; + for (uint32_t i = 0; i < mBaseMeshEdges.size(); ++i) + { + if (fP != mBaseMeshEdges[i].parent) + { + if (temp.empty() == false) + { + buildPolygonAndTriangulate(temp, &mVertices[0], mesh->getFacet(fP)->userData); + } + temp.clear(); + fP = mBaseMeshEdges[i].parent; + } + temp.push_back(Edge(mBaseMeshEdges[i].s, mBaseMeshEdges[i].e)); + } + buildPolygonAndTriangulate(temp, &mVertices[0], mesh->getFacet(fP)->userData); + + /* Build final triangles */ + + mBaseMeshResultTriangles.clear(); + for (uint32_t i = 0; i < mBaseMeshTriangles.size(); ++i) + { + if (mBaseMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + mBaseMeshResultTriangles.push_back(Triangle(mVertices[mBaseMeshTriangles[i].ea], mVertices[mBaseMeshTriangles[i].eb], mVertices[mBaseMeshTriangles[i].ec])); + mBaseMeshResultTriangles.back().userInfo = mBaseMeshTriangles[i].userInfo; + } + + computePositionedMapping(); +} + +void ChunkPostProcessor::prebuildTesselatedTriangles() +{ + mTesselatedMeshResultTriangles.clear(); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + mTesselatedMeshResultTriangles.push_back(Triangle(mVertices[mTesselatedMeshTriangles[i].ea], mVertices[mTesselatedMeshTriangles[i].eb], mVertices[mTesselatedMeshTriangles[i].ec])); + mTesselatedMeshResultTriangles.back().userInfo = mTesselatedMeshTriangles[i].userInfo; + } + +} + + +int32_t ChunkPostProcessor::addEdgeTr(const Edge& e) +{ + Edge ed = e; + if (ed.e < ed.s) std::swap(ed.s, ed.e); + auto it = mTrMeshEdgeMap.find(ed); + if (it == mTrMeshEdgeMap.end()) + { + mTrMeshEdToTr.push_back(EdgeToTriangles()); + mTrMeshEdgeMap[ed] = (int)mTrMeshEdToTr.size() - 1; + mTrMeshEdges.push_back(ed); + mEdgeFlag.push_back(INTERNAL_EDGE); + return (int32_t)mTrMeshEdToTr.size() - 1; + } + else + { + return it->second; + } +} + +int32_t ChunkPostProcessor::findEdge(const Edge& e) +{ + Edge ed = e; + if (ed.e < ed.s) std::swap(ed.s, ed.e); + auto it = mTrMeshEdgeMap.find(ed); + if (it == mTrMeshEdgeMap.end()) + { + return -1; + } + return it->second; +} + +void ChunkPostProcessor::updateEdgeTriangleInfo() +{ + mTrMeshEdToTr.clear(); + mTrMeshEdToTr.resize(mTrMeshEdges.size()); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + TriangleIndexed& tr = mTesselatedMeshTriangles[i]; + if (tr.ea == NOT_VALID_VERTEX) + continue; + int32_t ed = addEdgeTr(Edge(tr.ea, tr.eb)); + mTrMeshEdToTr[ed].add(i); + ed = addEdgeTr(Edge(tr.ea, tr.ec)); + mTrMeshEdToTr[ed].add(i); + ed = addEdgeTr(Edge(tr.ec, tr.eb)); + mTrMeshEdToTr[ed].add(i); + } +} + +void ChunkPostProcessor::updateVertEdgeInfo() +{ + mVertexToTriangleMap.clear(); + mVertexToTriangleMap.resize(mVertices.size()); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + TriangleIndexed& tr = mTesselatedMeshTriangles[i]; + if (tr.ea == NOT_VALID_VERTEX) continue; + mVertexToTriangleMap[tr.ea].push_back(i); + mVertexToTriangleMap[tr.eb].push_back(i); + mVertexToTriangleMap[tr.ec].push_back(i); + } + mVertexValence.clear(); + mVertexValence.resize(mVertices.size(), 0); + + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + if (mTrMeshEdToTr[i].c != 0) + { + mVertexValence[mTrMeshEdges[i].s]++; + mVertexValence[mTrMeshEdges[i].e]++; + } + } +} + + +void ChunkPostProcessor::collapseEdge(int32_t id) +{ + Edge cEdge = mTrMeshEdges[id]; + uint32_t from = cEdge.s; + uint32_t to = cEdge.e; + + + if (mRestrictionFlag[from] && mRestrictionFlag[to]) + { + return; + } + + if (mVertexValence[from] > mVertexValence[to]) + { + std::swap(from, to); + } + + if (mRestrictionFlag[from]) + { + std::swap(from, to); + } + + std::set<int32_t> connectedToBegin; + std::set<int32_t> connectedToEnd; + std::set<int32_t> neighboorTriangles; + + int32_t trWithEdge[2] = {-1, -1}; + int32_t cntr = 0; + for (uint32_t i = 0; i < mVertexToTriangleMap[from].size(); ++i) + { + if (mTesselatedMeshTriangles[mVertexToTriangleMap[from][i]].ea == NOT_VALID_VERTEX) + continue; + if (neighboorTriangles.insert(mVertexToTriangleMap[from][i]).second && mTesselatedMeshTriangles[mVertexToTriangleMap[from][i]].isContainEdge(from, to)) + { + trWithEdge[cntr] = mVertexToTriangleMap[from][i]; + cntr++; + } + } + for (uint32_t i = 0; i < mVertexToTriangleMap[to].size(); ++i) + { + if (mTesselatedMeshTriangles[mVertexToTriangleMap[to][i]].ea == NOT_VALID_VERTEX) + continue; + if (neighboorTriangles.insert(mVertexToTriangleMap[to][i]).second && mTesselatedMeshTriangles[mVertexToTriangleMap[to][i]].isContainEdge(from, to)) + { + trWithEdge[cntr] = mVertexToTriangleMap[to][i]; + cntr++; + } + } + + if (cntr == 0) + { + return; + } + if (cntr > 2) + { + return; + } + + for (uint32_t i: neighboorTriangles) + { + if (mTesselatedMeshTriangles[i].ea == from || mTesselatedMeshTriangles[i].eb == from || mTesselatedMeshTriangles[i].ec == from) + { + if (mTesselatedMeshTriangles[i].ea != to && mTesselatedMeshTriangles[i].ea != from) + connectedToBegin.insert(mTesselatedMeshTriangles[i].ea); + if (mTesselatedMeshTriangles[i].eb != to && mTesselatedMeshTriangles[i].eb != from) + connectedToBegin.insert(mTesselatedMeshTriangles[i].eb); + if (mTesselatedMeshTriangles[i].ec != to && mTesselatedMeshTriangles[i].ec != from) + connectedToBegin.insert(mTesselatedMeshTriangles[i].ec); + } + + if (mTesselatedMeshTriangles[i].ea == to || mTesselatedMeshTriangles[i].eb == to || mTesselatedMeshTriangles[i].ec == to) + { + if (mTesselatedMeshTriangles[i].ea != to && mTesselatedMeshTriangles[i].ea != from) + connectedToEnd.insert(mTesselatedMeshTriangles[i].ea); + if (mTesselatedMeshTriangles[i].eb != to && mTesselatedMeshTriangles[i].eb != from) + connectedToEnd.insert(mTesselatedMeshTriangles[i].eb); + if (mTesselatedMeshTriangles[i].ec != to && mTesselatedMeshTriangles[i].ec != from) + connectedToEnd.insert(mTesselatedMeshTriangles[i].ec); + } + } + bool canBeCollapsed = true; + for (auto it = connectedToBegin.begin(); it != connectedToBegin.end(); ++it) + { + uint32_t currV = *it; + if (connectedToEnd.find(currV) == connectedToEnd.end()) + continue; + bool found = false; + for (int32_t tr : neighboorTriangles) + { + if ((mTesselatedMeshTriangles[tr].ea == from || mTesselatedMeshTriangles[tr].eb == from || mTesselatedMeshTriangles[tr].ec == from) && + (mTesselatedMeshTriangles[tr].ea == to || mTesselatedMeshTriangles[tr].eb == to || mTesselatedMeshTriangles[tr].ec == to) && + (mTesselatedMeshTriangles[tr].ea == currV || mTesselatedMeshTriangles[tr].eb == currV || mTesselatedMeshTriangles[tr].ec == currV)) + { + found = true; + break; + } + } + if (!found) + { + canBeCollapsed = false; + break; + } + } + + if (canBeCollapsed) + { + for (int32_t i : neighboorTriangles) + { + if (trWithEdge[0] == i) continue; + if (cntr == 2 && trWithEdge[1] == i) continue; + TriangleIndexed tr = mTesselatedMeshTriangles[i]; + PxVec3 oldNormal = (mVertices[tr.eb].p - mVertices[tr.ea].p).cross(mVertices[tr.ec].p - mVertices[tr.ea].p); + + if (tr.ea == from) + { + tr.ea = to; + } + else + if (tr.eb == from) + { + tr.eb = to; + } + else + if (tr.ec == from) + { + tr.ec = to; + } + PxVec3 newNormal = (mVertices[tr.eb].p - mVertices[tr.ea].p).cross(mVertices[tr.ec].p - mVertices[tr.ea].p); + if (newNormal.magnitude() < 1e-8f) + { + canBeCollapsed = false; + break; + } + if (oldNormal.dot(newNormal) < 0) + { + canBeCollapsed = false; + break; + } + } + } + + if (canBeCollapsed) + { + mTesselatedMeshTriangles[trWithEdge[0]].ea = NOT_VALID_VERTEX; + if (cntr == 2)mTesselatedMeshTriangles[trWithEdge[1]].ea = NOT_VALID_VERTEX; + + for (int32_t i : neighboorTriangles) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + continue; + if (mTesselatedMeshTriangles[i].ea == from) + { + mTesselatedMeshTriangles[i].ea = to; + mVertexToTriangleMap[from].clear(); + mVertexToTriangleMap[to].push_back(i); + } + else + if (mTesselatedMeshTriangles[i].eb == from) + { + mTesselatedMeshTriangles[i].eb = to; + mVertexToTriangleMap[from].clear(); + mVertexToTriangleMap[to].push_back(i); + } + else + if (mTesselatedMeshTriangles[i].ec == from) + { + mTesselatedMeshTriangles[i].ec = to; + mVertexToTriangleMap[from].clear(); + mVertexToTriangleMap[to].push_back(i); + } + } + } +} + + +void ChunkPostProcessor::divideEdge(int32_t id) +{ + + if (mTrMeshEdToTr[id].c == 0 ) + { + return; + } + + Edge cEdge = mTrMeshEdges[id]; + EdgeFlag snapRestriction = mEdgeFlag[id]; + Vertex middle; + uint32_t nv = NOT_VALID_VERTEX; + for (int32_t t = 0; t < mTrMeshEdToTr[id].c; ++t) + { + int32_t oldTriangleIndex = mTrMeshEdToTr[id].tr[t]; + TriangleIndexed tr = mTesselatedMeshTriangles[mTrMeshEdToTr[id].tr[t]]; + + if (tr.ea == NOT_VALID_VERTEX) + { + continue; + } + + uint32_t pbf[3]; + pbf[0] = tr.ea; + pbf[1] = tr.eb; + pbf[2] = tr.ec; + for (int32_t p = 0; p < 3; ++p) + { + int32_t pnx = (p + 1) % 3; + int32_t opp = (p + 2) % 3; + + if ((pbf[p] == cEdge.s && pbf[pnx] == cEdge.e) || (pbf[p] == cEdge.e && pbf[pnx] == cEdge.s)) + { + if (nv == NOT_VALID_VERTEX) + { + middle.p = (mVertices[pbf[p]].p + mVertices[pbf[pnx]].p) * 0.5f; + middle.n = (mVertices[pbf[p]].n + mVertices[pbf[pnx]].n) * 0.5f; + middle.uv[0] = (mVertices[pbf[p]].uv[0] + mVertices[pbf[pnx]].uv[0]) * 0.5f; + + nv = (uint32_t)mVertices.size(); + mVertices.push_back(middle); + } + if (nv < mRestrictionFlag.size()) + { + mRestrictionFlag[nv] = ((snapRestriction == EXTERNAL_BORDER_EDGE) || (snapRestriction == INTERNAL_BORDER_EDGE)); + } + else + { + mRestrictionFlag.push_back((snapRestriction == EXTERNAL_BORDER_EDGE) || (snapRestriction == INTERNAL_BORDER_EDGE)); + } + + uint32_t ind1 = addEdgeTr(Edge(pbf[p], nv)); + uint32_t ind2 = addEdgeTr(Edge(nv, pbf[pnx])); + uint32_t ind3 = addEdgeTr(Edge(nv, pbf[opp])); + + + mEdgeFlag[ind1] = snapRestriction; + mEdgeFlag[ind2] = snapRestriction; + mEdgeFlag[ind3] = INTERNAL_EDGE; + + mTrMeshEdToTr[ind1].add(mTrMeshEdToTr[id].tr[t]); + int32_t userInfo = mTesselatedMeshTriangles[mTrMeshEdToTr[id].tr[t]].userInfo; + mTesselatedMeshTriangles[mTrMeshEdToTr[id].tr[t]] = TriangleIndexed(pbf[p], nv, pbf[opp]); + mTesselatedMeshTriangles[mTrMeshEdToTr[id].tr[t]].userInfo = userInfo; + mTrMeshEdToTr[ind2].add((int32_t)mTesselatedMeshTriangles.size()); + mTrMeshEdToTr[ind3].add((int32_t)mTrMeshEdToTr[id].tr[t]); + mTrMeshEdToTr[ind3].add((int32_t)mTesselatedMeshTriangles.size()); + mTesselatedMeshTriangles.push_back(TriangleIndexed(nv,pbf[pnx], pbf[opp])); + mTesselatedMeshTriangles.back().userInfo = userInfo; + int32_t ed1 = findEdge(Edge(pbf[pnx], pbf[opp])); + mTrMeshEdToTr[ed1].replace(oldTriangleIndex, (int32_t)mTesselatedMeshTriangles.size() - 1); + break; + } + } + } +} + + +NV_FORCE_INLINE void markEdge(int32_t ui, int32_t ed, std::vector<ChunkPostProcessor::EdgeFlag>& shortMarkup, std::vector<int32_t>& lastOwner) +{ + if (shortMarkup[ed] == ChunkPostProcessor::NONE) + { + if (ui == 0) + { + shortMarkup[ed] = ChunkPostProcessor::EXTERNAL_EDGE; + } + else + { + shortMarkup[ed] = ChunkPostProcessor::INTERNAL_EDGE; + } + lastOwner[ed] = ui; + } + else + { + if (ui != 0) + { + if (shortMarkup[ed] == ChunkPostProcessor::EXTERNAL_EDGE) + { + shortMarkup[ed] = ChunkPostProcessor::EXTERNAL_BORDER_EDGE; + } + if ((shortMarkup[ed] == ChunkPostProcessor::INTERNAL_EDGE) && ui != lastOwner[ed]) + { + shortMarkup[ed] = ChunkPostProcessor::INTERNAL_BORDER_EDGE; + } + } + else + { + if (shortMarkup[ed] != ChunkPostProcessor::EXTERNAL_EDGE) + { + shortMarkup[ed] = ChunkPostProcessor::EXTERNAL_BORDER_EDGE; + } + } + } +} + +float falloffFunction(float x, float mx) +{ + float t = (x) / (mx + 1e-6f); + t = std::min(1.0f, t); + return t * t; +} + +void ChunkPostProcessor::recalcNoiseDirs() +{ + /** + Compute normals direction to apply noise + */ + mVerticesNormalsSmoothed.resize(mVertices.size(), PxVec3(0, 0, 0)); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + TriangleIndexed& tr = mTesselatedMeshTriangles[i]; + if (tr.userInfo == 0) continue; + + if (tr.userInfo < 0) + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ea]] += mVertices[tr.ea].n.getNormalized(); + else + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ea]] -= mVertices[tr.ea].n.getNormalized(); + + if (tr.userInfo < 0) + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.eb]] += mVertices[tr.eb].n.getNormalized(); + else + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.eb]] -= mVertices[tr.eb].n.getNormalized(); + + if (tr.userInfo < 0) + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ec]] += mVertices[tr.ec].n.getNormalized(); + else + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ec]] -= mVertices[tr.ec].n.getNormalized(); + + } + for (uint32_t i = 0; i < mVerticesNormalsSmoothed.size(); ++i) + { + + mVerticesNormalsSmoothed[i] = mVerticesNormalsSmoothed[mPositionMappedVrt[i]]; + mVerticesNormalsSmoothed[i].normalize(); + } +} + + + +void ChunkPostProcessor::applyNoise(SimplexNoise& noise, float falloff, int32_t relaxIterations, float relaxFactor) +{ + NVBLAST_ASSERT(isTesselated); + if (isTesselated == false) + { + return; + } + mRestrictionFlag.clear(); + mRestrictionFlag.resize(mVertices.size(), false); + + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + if (mTrMeshEdToTr[i].c != 0) + { + if (mEdgeFlag[i] == EXTERNAL_EDGE || mEdgeFlag[i] == EXTERNAL_BORDER_EDGE) + { + mRestrictionFlag[mTrMeshEdges[i].e] = true; + mRestrictionFlag[mTrMeshEdges[i].s] = true; + } + } + } + std::vector<Vertex> localVertices = mVertices; + + recalcNoiseDirs(); + + relax(relaxIterations, relaxFactor, localVertices); + + + /** + Apply noise + */ + for (uint32_t i = 0; i < localVertices.size(); ++i) + { + + if (!mRestrictionFlag[i]) + { + + float d = noise.sample(localVertices[i].p); + localVertices[i].p += (falloffFunction(mVerticesDistances[i], falloff)) * mVerticesNormalsSmoothed[i] * d; + } + } + + + /* Recalculate smoothed normals*/ + mVerticesNormalsSmoothed.assign(mVerticesNormalsSmoothed.size(), PxVec3(0, 0, 0)); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + TriangleIndexed& tr = mTesselatedMeshTriangles[i]; + if (tr.userInfo == 0) continue; + + Triangle pTr(localVertices[tr.ea], localVertices[tr.eb], localVertices[tr.ec]); + PxVec3 nrm = pTr.getNormal().getNormalized(); + + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ea]] += nrm; + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.eb]] += nrm; + mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ec]] += nrm; + } + for (uint32_t i = 0; i < mVerticesNormalsSmoothed.size(); ++i) + { + mVerticesNormalsSmoothed[i] = mVerticesNormalsSmoothed[mPositionMappedVrt[i]]; + mVerticesNormalsSmoothed[i].normalize(); + } + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + TriangleIndexed& tr = mTesselatedMeshTriangles[i]; + if (tr.userInfo == 0) continue; + + localVertices[tr.ea].n = mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ea]]; + localVertices[tr.eb].n = mVerticesNormalsSmoothed[mPositionMappedVrt[tr.eb]]; + localVertices[tr.ec].n = mVerticesNormalsSmoothed[mPositionMappedVrt[tr.ec]]; + } + + mTesselatedMeshResultTriangles.clear(); + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].ea == NOT_VALID_VERTEX) + { + continue; + } + mTesselatedMeshResultTriangles.push_back(Triangle(localVertices[mTesselatedMeshTriangles[i].ea], localVertices[mTesselatedMeshTriangles[i].eb], localVertices[mTesselatedMeshTriangles[i].ec])); + mTesselatedMeshResultTriangles.back().userInfo = mTesselatedMeshTriangles[i].userInfo; + } + + +} + + +void ChunkPostProcessor::computePositionedMapping() +{ + std::map<PxVec3, int32_t, VrtPositionComparator> mPosMap; + mPositionMappedVrt.clear(); + mPositionMappedVrt.resize(mVertices.size()); + + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + auto it = mPosMap.find(mVertices[i].p); + + if (it == mPosMap.end()) + { + mPosMap[mVertices[i].p] = i; + mPositionMappedVrt[i] = i; + } + else + { + mPositionMappedVrt[i] = it->second; + } + } +} + +void ChunkPostProcessor::computeFalloffAndNormals() +{ + // Map newly created vertices according to positions + + computePositionedMapping(); + + mGeometryGraph.resize(mVertices.size()); + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + if (mTrMeshEdToTr[i].c == 0) + { + continue; + } + int32_t v1 = mPositionMappedVrt[mTrMeshEdges[i].s]; + int32_t v2 = mPositionMappedVrt[mTrMeshEdges[i].e]; + + if (std::find(mGeometryGraph[v1].begin(), mGeometryGraph[v1].end(), v2) == mGeometryGraph[v1].end()) + mGeometryGraph[v1].push_back(v2); + if (std::find(mGeometryGraph[v2].begin(), mGeometryGraph[v2].end(), v1) == mGeometryGraph[v2].end()) + mGeometryGraph[v2].push_back(v1); + } + mVerticesDistances.clear(); + mVerticesDistances.resize(mVertices.size(), 10000.0f); + + std::queue<int32_t> que; + + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + if (mTrMeshEdToTr[i].c != 0 && (mEdgeFlag[i] == EXTERNAL_EDGE || mEdgeFlag[i] == EXTERNAL_BORDER_EDGE)) + { + int32_t v1 = mPositionMappedVrt[mTrMeshEdges[i].s]; + int32_t v2 = mPositionMappedVrt[mTrMeshEdges[i].e]; + mVerticesDistances[v1] = 0.0f; + mVerticesDistances[v2] = 0.0f; + que.push(v1); + que.push(v2); + } + } + while (!que.empty()) + { + int32_t curr = que.front(); + que.pop(); + + for (uint32_t i = 0; i < mGeometryGraph[curr].size(); ++i) + { + int32_t to = mGeometryGraph[curr][i]; + float d = mVerticesDistances[curr] + 0.1f;// (mVertices[to].p - mVertices[curr].p).magnitudeSquared(); + if (d < mVerticesDistances[to]) + { + mVerticesDistances[to] = d; + que.push(to); + } + } + } + + for (uint32_t i = 0; i < mVerticesDistances.size(); ++i) + { + int32_t from = mPositionMappedVrt[i]; + mVerticesDistances[i] = mVerticesDistances[from]; + } +} + +bool edgeOverlapTest(PxVec3& as, PxVec3& ae, PxVec3& bs, PxVec3& be) +{ + //return false; + if (std::max(std::min(as.x, ae.x), std::min(bs.x, be.x)) > std::min(std::max(as.x, ae.x), std::max(bs.x, be.x))) return false; + if (std::max(std::min(as.y, ae.y), std::min(bs.y, be.y)) > std::min(std::max(as.y, ae.y), std::max(bs.y, be.y))) return false; + if (std::max(std::min(as.z, ae.z), std::min(bs.z, be.z)) > std::min(std::max(as.z, ae.z), std::max(bs.z, be.z))) return false; + + return ((bs - as).cross(ae - as)).magnitudeSquared() < 1e-12f && ((be - as).cross(ae - as)).magnitudeSquared() < 1e-12f; +} + +void ChunkPostProcessor::relax(int32_t iteration, float factor, std::vector<Vertex>& vertices) +{ + std::vector<PxVec3> verticesTemp(vertices.size()); + std::vector<PxVec3> normalsTemp(vertices.size()); + for (int32_t iter = 0; iter < iteration; ++iter) + { + for (uint32_t i = 0; i < vertices.size(); ++i) + { + if (mRestrictionFlag[i]) + { + continue; + } + PxVec3 cps = vertices[i].p; + PxVec3 cns = mVerticesNormalsSmoothed[i]; + PxVec3 averaged(0, 0, 0); + PxVec3 averagedNormal(0, 0, 0); + + for (uint32_t p = 0; p < mGeometryGraph[mPositionMappedVrt[i]].size(); ++p) + { + int32_t to = mGeometryGraph[mPositionMappedVrt[i]][p]; + averaged += vertices[to].p; + averagedNormal += mVerticesNormalsSmoothed[to]; + + } + averaged *= (1.0f / mGeometryGraph[mPositionMappedVrt[i]].size()); + averagedNormal *= (1.0f / mGeometryGraph[mPositionMappedVrt[i]].size()); + verticesTemp[i] = cps + (averaged - cps) * factor; + normalsTemp[i] = cns * (1.0f - factor) + averagedNormal * factor; + } + for (uint32_t i = 0; i < vertices.size(); ++i) + { + if (mRestrictionFlag[i]) + { + continue; + } + vertices[i].p = verticesTemp[i]; + mVerticesNormalsSmoothed[i] = normalsTemp[i].getNormalized(); + + } + } + +} + +void ChunkPostProcessor::prebuildEdgeFlagArray() +{ + mRestrictionFlag.clear(); + mRestrictionFlag.resize(mVertices.size()); + mEdgeFlag.clear(); + mEdgeFlag.resize(mTrMeshEdges.size(), NONE); + + std::map<PxVec3, int32_t, VrtPositionComparator> mPosMap; + mPositionMappedVrt.clear(); + mPositionMappedVrt.resize(mVertices.size(), 0); + + for (uint32_t i = 0; i < mVertices.size(); ++i) + { + auto it = mPosMap.find(mVertices[i].p); + + if (it == mPosMap.end()) + { + mPosMap[mVertices[i].p] = i; + mPositionMappedVrt[i] = i; + } + else + { + mPositionMappedVrt[i] = it->second; + } + } + + std::map<Edge, int32_t> mPositionEdgeMap; + std::vector<int32_t> mPositionBasedEdges(mTrMeshEdges.size()); + + + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + Edge tmp = Edge(mPositionMappedVrt[mTrMeshEdges[i].s], mPositionMappedVrt[mTrMeshEdges[i].e]); + if (tmp.e < tmp.s) std::swap(tmp.e, tmp.s); + auto it = mPositionEdgeMap.find(tmp); + if (it == mPositionEdgeMap.end()) + { + mPositionEdgeMap[tmp] = i; + mPositionBasedEdges[i] = i; + } + else + { + mPositionBasedEdges[i] = it->second; + } + } + + std::vector<EdgeFlag> shortMarkup(mTrMeshEdges.size(), NONE); + std::vector<int32_t> lastOwner(mTrMeshEdges.size(), 0); + + std::vector<std::vector<int32_t> > edgeOverlap(mTrMeshEdges.size()); + for (auto it1 = mPositionEdgeMap.begin(); it1 != mPositionEdgeMap.end(); ++it1) + { + auto it2 = it1; + it2++; + for (; it2 != mPositionEdgeMap.end(); ++it2) + { + Edge& ed1 = mTrMeshEdges[it1->second]; + Edge& ed2 = mTrMeshEdges[it2->second]; + + if (edgeOverlapTest(mVertices[ed1.s].p, mVertices[ed1.e].p, mVertices[ed2.s].p, mVertices[ed2.e].p)) + { + edgeOverlap[it1->second].push_back(it2->second); + } + } + } + + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + int32_t ui = mTesselatedMeshTriangles[i].userInfo; + int32_t ed = mPositionBasedEdges[findEdge(Edge(mTesselatedMeshTriangles[i].ea, mTesselatedMeshTriangles[i].eb))]; + + + markEdge(ui, ed, shortMarkup, lastOwner); + for (uint32_t ov = 0; ov < edgeOverlap[ed].size(); ++ov) + { + markEdge(ui, edgeOverlap[ed][ov], shortMarkup, lastOwner); + } + + ed = mPositionBasedEdges[findEdge(Edge(mTesselatedMeshTriangles[i].ea, mTesselatedMeshTriangles[i].ec))]; + markEdge(ui, ed, shortMarkup, lastOwner); + for (uint32_t ov = 0; ov < edgeOverlap[ed].size(); ++ov) + { + markEdge(ui, edgeOverlap[ed][ov], shortMarkup, lastOwner); + } + + ed = mPositionBasedEdges[findEdge(Edge(mTesselatedMeshTriangles[i].eb, mTesselatedMeshTriangles[i].ec))]; + markEdge(ui, ed, shortMarkup, lastOwner); + for (uint32_t ov = 0; ov < edgeOverlap[ed].size(); ++ov) + { + markEdge(ui, edgeOverlap[ed][ov], shortMarkup, lastOwner); + } + + } + + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + mEdgeFlag[i] = shortMarkup[mPositionBasedEdges[i]]; + } + + for (uint32_t i = 0; i < mTesselatedMeshTriangles.size(); ++i) + { + if (mTesselatedMeshTriangles[i].userInfo != 0) continue; + + int32_t ed = findEdge(Edge(mTesselatedMeshTriangles[i].ea, mTesselatedMeshTriangles[i].eb)); + mEdgeFlag[ed] = EXTERNAL_EDGE; + ed = findEdge(Edge(mTesselatedMeshTriangles[i].ec, mTesselatedMeshTriangles[i].eb)); + mEdgeFlag[ed] = EXTERNAL_EDGE; + ed = findEdge(Edge(mTesselatedMeshTriangles[i].ea, mTesselatedMeshTriangles[i].ec)); + mEdgeFlag[ed] = EXTERNAL_EDGE; + } +} + + + +void ChunkPostProcessor::tesselateInternalSurface(float maxLenIn) +{ + mTesselatedMeshTriangles = mBaseMeshTriangles; + if (mTesselatedMeshTriangles.empty()) + { + return; + } + + updateEdgeTriangleInfo(); + prebuildEdgeFlagArray(); + mRestrictionFlag.resize(mVertices.size(), 0); + for (uint32_t i = 0; i < mTrMeshEdges.size(); ++i) + { + if (mEdgeFlag[i] == EXTERNAL_EDGE || mEdgeFlag[i] == EXTERNAL_BORDER_EDGE || mEdgeFlag[i] == INTERNAL_BORDER_EDGE) + { + mRestrictionFlag[mTrMeshEdges[i].s] = 1; + mRestrictionFlag[mTrMeshEdges[i].e] = 1; + } + } + + + float maxLen = std::max(0.1f, maxLenIn); + while (maxLen > maxLenIn) + { + float mlSq = maxLen * maxLen; + float minD = maxLen * 0.5f; + minD = minD * minD; + + for (int32_t iter = 0; iter < 15; ++iter) + { + updateVertEdgeInfo(); + uint32_t oldSize = (uint32_t)mTrMeshEdges.size(); + for (uint32_t i = 0; i < oldSize; ++i) + { + if (mEdgeFlag[i] == EXTERNAL_EDGE || mEdgeFlag[i] == INTERNAL_BORDER_EDGE) + { + continue; + } + if ((mVertices[mTrMeshEdges[i].s].p - mVertices[mTrMeshEdges[i].e].p).magnitudeSquared() < minD) + { + collapseEdge(i); + } + } + + oldSize = (uint32_t)mTrMeshEdges.size(); + updateEdgeTriangleInfo(); + for (uint32_t i = 0; i < oldSize; ++i) + { + if (mEdgeFlag[i] == EXTERNAL_EDGE) + { + continue; + } + if ((mVertices[mTrMeshEdges[i].s].p - mVertices[mTrMeshEdges[i].e].p).magnitudeSquared() > mlSq) + { + divideEdge(i); + } + } + } + maxLen *= 0.3; + maxLen = std::max(maxLen, maxLenIn); + } + computeFalloffAndNormals(); + prebuildTesselatedTriangles(); + isTesselated = true; +} + +} // namespace Blast +} // namespace Nv
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.h new file mode 100644 index 0000000..83942f4 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringTriangulator.h @@ -0,0 +1,261 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGTRIANGULATOR_H +#define NVBLASTEXTAUTHORINGTRIANGULATOR_H + + +#include <vector> +#include <map> +#include "NvBlastExtAuthoringTypes.h" +#include "NvBlastExtAuthoringMesh.h" +#include "NvBlastExtAuthoringInternalCommon.h" + +namespace Nv +{ +namespace Blast +{ +class SimplexNoise; + +/** + Vertex comparator for vertex welding. +*/ +struct VrtComp +{ + bool operator()(const Vertex& a, const Vertex& b) const; +}; + +/** + Vertex comparator for vertex welding (not accounts normal and uv parameters of vertice). +*/ +struct VrtPositionComparator +{ + bool operator()(const physx::PxVec3& a, const physx::PxVec3& b) const; +}; + +/** + Structure used on tesselation stage. Maps edge to two neighboor triangles +*/ +struct EdgeToTriangles +{ + int32_t tr[2]; + int32_t c; + EdgeToTriangles() + { + c = 0; + } + /** + Add triangle to edge. Should not be called more than twice for one edge!!!!. + */ + void add(int32_t t) + { + tr[c] = t; + ++c; + } + /** + Replaces mapping from one triangle to another. + */ + void replace(int32_t from, int32_t to) + { + if (tr[0] == from) + { + tr[0] = to; + } + else + { + if (c == 2 && tr[1] == from) + { + tr[1] = to; + } + } + } + /** + Get triangle which is mapped by this edge and which index is different than provided. + */ + int32_t getNot(int32_t id) + { + if (tr[0] != id) + { + return tr[0]; + } + if (c == 2 && tr[1] != id) + { + return tr[1]; + } + return -1; + } + +}; + + +/** + Tool for doing all post processing steps of authoring. +*/ +class ChunkPostProcessor +{ +public: + /** + Edge flags + */ + enum EdgeFlag{ INTERNAL_EDGE, EXTERNAL_BORDER_EDGE, INTERNAL_BORDER_EDGE, EXTERNAL_EDGE, NONE }; + + /** + Triangulates provided mesh and saves result internally. Uses Ear-clipping algorithm. + \param[in] mesh Mesh for triangulation + */ + void triangulate(Mesh* mesh); + + /** + \return Return array of triangles of base mesh. + */ + std::vector<Triangle>& getBaseMesh() + { + return mBaseMeshResultTriangles; + } + + /** + \return Return array of TriangleIndexed of base mesh. Each TriangleIndexed contains index of corresponding vertex in internal vertex buffer. + */ + std::vector<TriangleIndexed>& getBaseMeshIndexed() + { + return mBaseMeshTriangles; + } + + + /** + \return Return mapping from vertices of input Mesh to internal vertices buffer. Used for island detection. + */ + std::vector<uint32_t>& getBaseMapping() + { + return mBaseMapping; + }; + /** + \return Return mapping from vertices of input Mesh to internal vertices buffer, only positions are accounted. Used for island detection. + */ + std::vector<int32_t>& getPositionedMapping() + { + return mPositionMappedVrt; + }; + + /** + \return Return internal vertex buffer size. Vertices internally are welded with some threshold. + */ + uint32_t getWeldedVerticesCount() + { + return static_cast<uint32_t>(mVertices.size()); + } + /** + Tesselate internal surface. + \param[in] maxLen - maximal length of edge on internal surface. + */ + void tesselateInternalSurface(float maxLen); + + /** + Apply noise to internal surface. Must be called only after tesselation!!! + \param[in] noise - noise generator + \param[in] falloff - damping of noise around of external surface + \param[in] relaxIterations - number of smoothing iterations before applying noise + \param[in] relaxFactor - amount of smooting before applying noise. + */ + void applyNoise(SimplexNoise& noise, float falloff, int32_t relaxIterations, float relaxFactor); + + /** + \return Return array of noised mesh triangles. + */ + std::vector<Triangle>& getNoisyMesh() + { + return mTesselatedMeshResultTriangles; + }; + + /** + Removes all information about mesh triangulation, tesselation, etc. + */ + void reset(); + +private: + + + + void collapseEdge(int32_t id); + void divideEdge(int32_t id); + void updateVertEdgeInfo(); + void updateEdgeTriangleInfo(); + + int32_t addVerticeIfNotExist(Vertex& p); + void addEdgeIfValid(EdgeWithParent& ed); + + /* Data used before triangulation to build polygon loops*/ + + std::vector<Vertex> mVertices; + std::vector<EdgeWithParent> mBaseMeshEdges; + std::map<Vertex, int32_t, VrtComp> mVertMap; + std::map<EdgeWithParent, int32_t, EdgeComparator> mEdgeMap; + std::vector<uint32_t> mBaseMapping; + + + + /** + Unite all almost similar vertices, update edges according to this changes + */ + void prepare(Mesh* mesh); + + /* ------------------------------------------------------------ */ + + /* Triangulation and tesselation stage data */ + bool isTesselated; + + std::map<Edge, int32_t> mTrMeshEdgeMap; + std::vector<Edge> mTrMeshEdges; + std::vector<EdgeToTriangles> mTrMeshEdToTr; + std::vector<int32_t> mVertexValence; + std::vector<std::vector<int32_t> > mVertexToTriangleMap; + + std::vector<bool> mRestrictionFlag; + std::vector<EdgeFlag> mEdgeFlag; + + std::vector<float> mVerticesDistances; + std::vector<physx::PxVec3> mVerticesNormalsSmoothed; + std::vector<int32_t> mPositionMappedVrt; + std::vector<std::vector<int32_t> > mGeometryGraph; + + + int32_t addEdgeTr(const Edge& ed); + int32_t findEdge(const Edge& e); + + void prebuildEdgeFlagArray(); + void computePositionedMapping(); + + void computeFalloffAndNormals(); + + + + void triangulatePolygonWithEarClipping(std::vector<uint32_t>& inputPolygon, Vertex* vert, ProjectionDirections dir); + void buildPolygonAndTriangulate(std::vector<Edge>& edges, Vertex* vertices, int32_t userData); + + void relax(int32_t iterations, float factor, std::vector<Vertex>& vertices); + void recalcNoiseDirs(); + + std::vector<TriangleIndexed> mBaseMeshTriangles; + std::vector<TriangleIndexed> mTesselatedMeshTriangles; + + /** + Final triangles + */ + void prebuildTesselatedTriangles(); + + std::vector<Triangle> mBaseMeshResultTriangles; + std::vector<Triangle> mTesselatedMeshResultTriangles; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTAUTHORINGTRIANGULATOR_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringVSA.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringVSA.h new file mode 100644 index 0000000..fd0c9c9 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtAuthoringVSA.h @@ -0,0 +1,312 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAUTHORINGVSA_H +#define NVBLASTEXTAUTHORINGVSA_H + +namespace Nv +{ +namespace Blast +{ + +/* + This code copied from APEX GSA +*/ + +namespace VSA +{ +typedef float real; + +struct VS3D_Halfspace_Set +{ + virtual real farthest_halfspace(real plane[4], const real point[4]) = 0; +}; + + +// Simple types and operations for internal calculations +struct Vec3 { real x, y, z; }; // 3-vector +inline Vec3 vec3(real x, real y, real z) { Vec3 r; r.x = x; r.y = y; r.z = z; return r; } // vector builder +inline Vec3 operator + (const Vec3& a, const Vec3& b) { return vec3(a.x + b.x, a.y + b.y, a.z + b.z); } // vector addition +inline Vec3 operator * (real s, const Vec3& v) { return vec3(s*v.x, s*v.y, s*v.z); } // scalar multiplication +inline real operator | (const Vec3& a, const Vec3& b) { return a.x*b.x + a.y*b.y + a.z*b.z; } // dot product +inline Vec3 operator ^ (const Vec3& a, const Vec3& b) { return vec3(a.y*b.z - b.y*a.z, a.z*b.x - b.z*a.x, a.x*b.y - b.x*a.y); } // cross product + +struct Vec4 { Vec3 v; real w; }; // 4-vector split into 3-vector and scalar parts +inline Vec4 vec4(const Vec3& v, real w) { Vec4 r; r.v = v; r.w = w; return r; } // vector builder +inline real operator | (const Vec4& a, const Vec4& b) { return (a.v | b.v) + a.w*b.w; } // dot product + +// More accurate perpendicular +inline Vec3 perp(const Vec3& a, const Vec3& b) +{ + Vec3 c = a^b; // Cross-product gives perpendicular +#if VS3D_HIGH_ACCURACY || REAL_DOUBLE + const real c2 = c | c; + if (c2 != 0) c = c + (1 / c2)*((a | c)*(c^b) + (b | c)*(a^c)); // Improvement to (a b)^T(c) = (0) +#endif + return c; +} + +// Square +inline real sq(real x) { return x*x; } + +// Returns index of the extremal element in a three-element set {e0, e1, e2} based upon comparisons c_ij. The extremal index m is such that c_mn is true, or e_m == e_n, for all n. +inline int ext_index(int c_10, int c_21, int c_20) { return c_10 << c_21 | (c_21&c_20) << 1; } + +// Returns index (0, 1, or 2) of minimum argument +inline int index_of_min(real x0, real x1, real x2) { return ext_index((int)(x1 < x0), (int)(x2 < x1), (int)(x2 < x0)); } + +// Compare fractions with positive deominators. Returns a_num*sqrt(a_rden2) > b_num*sqrt(b_rden2) +inline bool frac_gt(real a_num, real a_rden2, real b_num, real b_rden2) +{ + const bool a_num_neg = a_num < 0; + const bool b_num_neg = b_num < 0; + return a_num_neg != b_num_neg ? b_num_neg : ((a_num*a_num*a_rden2 > b_num*b_num*b_rden2) != a_num_neg); +} + +// Returns index (0, 1, or 2) of maximum fraction with positive deominators +inline int index_of_max_frac(real x0_num, real x0_rden2, real x1_num, real x1_rden2, real x2_num, real x2_rden2) +{ + return ext_index((int)frac_gt(x1_num, x1_rden2, x0_num, x0_rden2), (int)frac_gt(x2_num, x2_rden2, x1_num, x1_rden2), (int)frac_gt(x2_num, x2_rden2, x0_num, x0_rden2)); +} + +// Compare values given their signs and squares. Returns a > b. a2 and b2 may have any constant offset applied to them. +inline bool sgn_sq_gt(real sgn_a, real a2, real sgn_b, real b2) { return sgn_a*sgn_b < 0 ? (sgn_b < 0) : ((a2 > b2) != (sgn_a < 0)); } + +// Returns index (0, 1, or 2) of maximum value given their signs and squares. sq_x0, sq_x1, and sq_x2 may have any constant offset applied to them. +inline int index_of_max_sgn_sq(real sgn_x0, real sq_x0, real sgn_x1, real sq_x1, real sgn_x2, real sq_x2) +{ + return ext_index((int)sgn_sq_gt(sgn_x1, sq_x1, sgn_x0, sq_x0), (int)sgn_sq_gt(sgn_x2, sq_x2, sgn_x1, sq_x1), (int)sgn_sq_gt(sgn_x2, sq_x2, sgn_x0, sq_x0)); +} + +// Project 2D (homogeneous) vector onto 2D half-space boundary +inline void project2D(Vec3& r, const Vec3& plane, real delta, real recip_n2, real eps2) +{ + r = r + (-delta*recip_n2)*vec3(plane.x, plane.y, 0); + r = r + (-(r | plane)*recip_n2)*vec3(plane.x, plane.y, 0); // Second projection for increased accuracy + if ((r | r) > eps2) return; + r = (-plane.z*recip_n2)*vec3(plane.x, plane.y, 0); + r.z = 1; +} + + +// Update function for vs3d_test +static bool vs3d_update(Vec4& p, Vec4 S[4], int& plane_count, const Vec4& q, real eps2) +{ + // h plane is the last plane + const Vec4& h = S[plane_count - 1]; + + // Handle plane_count == 1 specially (optimization; this could be commented out) + if (plane_count == 1) + { + // Solution is objective projected onto h plane + p = q; + p.v = p.v + -(p | h)*h.v; + if ((p | p) <= eps2) p = vec4(-h.w*h.v, 1); // If p == 0 then q is a direction vector, any point in h is a support point + return true; + } + + // Create basis in the h plane + const int min_i = index_of_min(h.v.x*h.v.x, h.v.y*h.v.y, h.v.z*h.v.z); + const Vec3 y = h.v^vec3((real)(min_i == 0), (real)(min_i == 1), (real)(min_i == 2)); + const Vec3 x = y^h.v; + + // Use reduced vector r instead of p + Vec3 r = { x | q.v, y | q.v, q.w*(y | y) }; // (x|x) = (y|y) = square of plane basis scale + + // If r == 0 (within epsilon), then it is a direction vector, and we have a bounded solution + if ((r | r) <= eps2) r.z = 1; + + // Create plane equations in the h plane. These will not be normalized in general. + int N = 0; // Plane count in h subspace + Vec3 R[3]; // Planes in h subspace + real recip_n2[3]; // Plane normal vector reciprocal lengths squared + real delta[3]; // Signed distance of objective to the planes + int index[3]; // Keep track of original plane indices + for (int i = 0; i < plane_count - 1; ++i) + { + const Vec3& vi = S[i].v; + const real cos_theta = h.v | vi; + R[N] = vec3(x | vi, y | vi, S[i].w - h.w*cos_theta); + index[N] = i; + const real n2 = R[N].x*R[N].x + R[N].y*R[N].y; + if (n2 >= eps2) + { + const real lin_norm = (real)1.5 - (real)0.5*n2; // 1st-order approximation to 1/sqrt(n2) expanded about n2 = 1 + R[N] = lin_norm*R[N]; // We don't need normalized plane equations, but rescaling (even with an approximate normalization) gives better numerical behavior + recip_n2[N] = 1 / (R[N].x*R[N].x + R[N].y*R[N].y); + delta[N] = r | R[N]; + ++N; // Keep this plane + } + else if (cos_theta < 0) return false; // Parallel cases are redundant and rejected, anti-parallel cases are 1D voids + } + + // Now work with the N-sized R array of half-spaces in the h plane + switch (N) + { + case 1: one_plane : + if (delta[0] < 0) N = 0; // S[0] is redundant, eliminate it + else project2D(r, R[0], delta[0], recip_n2[0], eps2); + break; + case 2: two_planes : + if (delta[0] < 0 && delta[1] < 0) N = 0; // S[0] and S[1] are redundant, eliminate them + else + { + const int max_d_index = (int)frac_gt(delta[1], recip_n2[1], delta[0], recip_n2[0]); + project2D(r, R[max_d_index], delta[max_d_index], recip_n2[max_d_index], eps2); + const int min_d_index = max_d_index ^ 1; + const real new_delta_min = r | R[min_d_index]; + if (new_delta_min < 0) + { + index[0] = index[max_d_index]; + N = 1; // S[min_d_index] is redundant, eliminate it + } + else + { + // Set r to the intersection of R[0] and R[1] and keep both + r = perp(R[0], R[1]); + if (r.z*r.z*recip_n2[0] * recip_n2[1] < eps2) + { + if (R[0].x*R[1].x + R[0].y*R[1].y < 0) return false; // 2D void found + goto one_plane; + } + r = (1 / r.z)*r; // We could just as well multiply r by sgn(r.z); we just need to ensure r.z > 0 + } + } + break; + case 3: + if (delta[0] < 0 && delta[1] < 0 && delta[2] < 0) N = 0; // S[0], S[1], and S[2] are redundant, eliminate them + else + { + const Vec3 row_x = { R[0].x, R[1].x, R[2].x }; + const Vec3 row_y = { R[0].y, R[1].y, R[2].y }; + const Vec3 row_w = { R[0].z, R[1].z, R[2].z }; + const Vec3 cof_w = perp(row_x, row_y); + const bool detR_pos = (row_w | cof_w) > 0; + const int nrw_sgn0 = cof_w.x*cof_w.x*recip_n2[1] * recip_n2[2] < eps2 ? 0 : (((int)((cof_w.x > 0) == detR_pos) << 1) - 1); + const int nrw_sgn1 = cof_w.y*cof_w.y*recip_n2[2] * recip_n2[0] < eps2 ? 0 : (((int)((cof_w.y > 0) == detR_pos) << 1) - 1); + const int nrw_sgn2 = cof_w.z*cof_w.z*recip_n2[0] * recip_n2[1] < eps2 ? 0 : (((int)((cof_w.z > 0) == detR_pos) << 1) - 1); + + if ((nrw_sgn0 | nrw_sgn1 | nrw_sgn2) >= 0) return false; // 3D void found + + const int positive_width_count = ((nrw_sgn0 >> 1) & 1) + ((nrw_sgn1 >> 1) & 1) + ((nrw_sgn2 >> 1) & 1); + if (positive_width_count == 1) + { + // A single positive width results from a redundant plane. Eliminate it and peform N = 2 calculation. + const int pos_width_index = ((nrw_sgn1 >> 1) & 1) | (nrw_sgn2 & 2); // Calculates which index corresponds to the positive-width side + R[pos_width_index] = R[2]; + recip_n2[pos_width_index] = recip_n2[2]; + delta[pos_width_index] = delta[2]; + index[pos_width_index] = index[2]; + N = 2; + goto two_planes; + } + + // Find the max dot product of r and R[i]/|R_normal[i]|. For numerical accuracy when the angle between r and the i^{th} plane normal is small, we take some care below: + const int max_d_index = r.z != 0 + ? index_of_max_frac(delta[0], recip_n2[0], delta[1], recip_n2[1], delta[2], recip_n2[2]) // displacement term resolves small-angle ambiguity, just use dot product + : index_of_max_sgn_sq(delta[0], -sq(r.x*R[0].y - r.y*R[0].x)*recip_n2[0], delta[1], -sq(r.x*R[1].y - r.y*R[1].x)*recip_n2[1], delta[2], -sq(r.x*R[2].y - r.y*R[2].x)*recip_n2[2]); // No displacement term. Use wedge product to find the sine of the angle. + + // Project r onto max-d plane + project2D(r, R[max_d_index], delta[max_d_index], recip_n2[max_d_index], eps2); + N = 1; // Unless we use a vertex in the loop below + const int index_max = index[max_d_index]; + + // The number of finite widths should be >= 2. If not, it should be 0, but in any case it implies three parallel lines in the plane, which we should not have here. + // If we do have three parallel lines (# of finite widths < 2), we've picked the line corresponding to the half-plane farthest from r, which is correct. + const int finite_width_count = (nrw_sgn0 & 1) + (nrw_sgn1 & 1) + (nrw_sgn2 & 1); + if (finite_width_count >= 2) + { + const int i_remaining[2] = { (1 << max_d_index) & 3, (3 >> max_d_index) ^ 1 }; // = {(max_d_index+1)%3, (max_d_index+2)%3} + const int i_select = (int)frac_gt(delta[i_remaining[1]], recip_n2[i_remaining[1]], delta[i_remaining[0]], recip_n2[i_remaining[0]]); // Select the greater of the remaining dot products + for (int i = 0; i < 2; ++i) + { + const int j = i_remaining[i_select^i]; // i = 0 => the next-greatest, i = 1 => the least + if ((r | R[j]) >= 0) + { + r = perp(R[max_d_index], R[j]); + r = (1 / r.z)*r; // We could just as well multiply r by sgn(r.z); we just need to ensure r.z > 0 + index[1] = index[j]; + N = 2; + break; + } + } + } + + index[0] = index_max; + } + break; + } + + // Transform r back to 3D space + p = vec4(r.x*x + r.y*y + (-r.z*h.w)*h.v, r.z); + + // Pack S array with kept planes + if (N < 2 || index[1] != 0) { for (int i = 0; i < N; ++i) S[i] = S[index[i]]; } // Safe to copy columns in order + else { const Vec4 temp = S[0]; S[0] = S[index[0]]; S[1] = temp; } // Otherwise use temp storage to avoid overwrite + S[N] = h; + plane_count = N + 1; + + return true; +} + + +// Performs the VS algorithm for D = 3 +inline int vs3d_test(VS3D_Halfspace_Set& halfspace_set, real* q = nullptr) +{ + // Objective = q if it is not NULL, otherwise it is the origin represented in homogeneous coordinates + const Vec4 objective = q ? (q[3] != 0 ? vec4((1 / q[3])*vec3(q[0], q[1], q[2]), 1) : *(Vec4*)q) : vec4(vec3(0, 0, 0), 1); + + // Tolerance for 3D void simplex algorithm + const real eps_f = (real)1 / (sizeof(real) == 4 ? (1L << 23) : (1LL << 52)); // Floating-point epsilon +#if VS3D_HIGH_ACCURACY || REAL_DOUBLE + const real eps = 8 * eps_f; +#else + const real eps = 80 * eps_f; +#endif + const real eps2 = eps*eps; // Using epsilon squared + + // Maximum allowed iterations of main loop. If exceeded, error code is returned + const int max_iteration_count = 50; + + // State + Vec4 S[4]; // Up to 4 planes + int plane_count = 0; // Number of valid planes + Vec4 p = objective; // Test point, initialized to objective + + // Default result, changed to valid result if found in loop below + int result = -1; + + // Iterate until a stopping condition is met or the maximum number of iterations is reached + for (int i = 0; result < 0 && i < max_iteration_count; ++i) + { + Vec4& plane = S[plane_count++]; + real delta = halfspace_set.farthest_halfspace(&plane.v.x, &p.v.x); +#if VS3D_UNNORMALIZED_PLANE_HANDLING != 0 + const real recip_norm = vs3d_recip_sqrt(plane.v | plane.v); + plane = vec4(recip_norm*plane.v, recip_norm*plane.w); + delta *= recip_norm; +#endif + if (delta <= 0 || delta*delta <= eps2*(p | p)) result = 1; // Intersection found + else if (!vs3d_update(p, S, plane_count, objective, eps2)) result = 0; // Void simplex found + } + + // If q is given, fill it with the solution (normalize p.w if it is not zero) + if (q) *(Vec4*)q = (p.w != 0) ? vec4((1 / p.w)*p.v, 1) : p; + + return result; +} + +} // namespace VSA + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTAUTHORINGVSA_H diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.cpp b/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.cpp new file mode 100644 index 0000000..3c3e540 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.cpp @@ -0,0 +1,355 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtTriangleProcessor.h" +#include "NvBlastExtAuthoringInternalCommon.h" +#define COLLIN_EPS 1e-4f +#define V_COMP_EPS 1e-5f + +using namespace physx; + +namespace Nv +{ +namespace Blast +{ +/** + Segments bounding box interseciton test +*/ +bool boundingRectangleIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2) +{ + // sl1/sl2 is always left bottom end of rectangle + // se1/el2 is always right top end of rectangle + + PxF32 sl1, sl2, el1, el2; + if (s1.x < e1.x) + { + sl1 = s1.x; + el1 = e1.x; + } + else + { + el1 = s1.x; + sl1 = e1.x; + } + + if (s2.x < e2.x) + { + sl2 = s2.x; + el2 = e2.x; + } + else + { + el2 = s2.x; + sl2 = e2.x; + } + if (PxMax(sl1, sl2) > PxMin(el1, el2)) + return false; + + if (s1.y < e1.y) + { + sl1 = s1.y; + el1 = e1.y; + } + else + { + el1 = s1.y; + sl1 = e1.y; + } + + if (s2.y < e2.y) + { + sl2 = s2.y; + el2 = e2.y; + } + else + { + el2 = s2.y; + sl2 = e2.y; + } + if (PxMax(sl1, sl2) > PxMin(el1, el2)) + return false; + + return true; +} + +inline PxF32 getRotation(PxVec2 a, PxVec2 b) +{ + return a.x * b.y - a.y * b.x; +} + +inline PxF32 getParameter(const PxVec2& a, const PxVec2& b, const PxVec2& point) +{ + return (point - a).magnitude() / (b - a).magnitude(); +} +inline PxVec3 lerp3D(const PxVec3& a, const PxVec3& b, const PxF32 t) +{ + return (b - a) * t + a; +} + + + +struct Line2D +{ + PxVec2 normal; + PxF32 c; + Line2D(PxVec2 vec, PxVec2 point) + { + normal.x = vec.y; + normal.y = -vec.x; + c = -normal.dot(point); + } +}; + + +uint32_t TriangleProcessor::getSegmentIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2, PxF32& t1) +{ + if (!boundingRectangleIntersection(s1, e1, s2, e2)) + return 0; + + PxVec2 vec1 = e1 - s1; + PxVec2 vec2 = e2 - s2; + PxF32 det1 = getRotation(vec1, vec2); + if (PxAbs(det1) < COLLIN_EPS) + { + return 0; + } + Line2D lineA(vec1, s1); + Line2D lineB(vec2, s2); + PxVec2 fInt; + + PxF32 detX = lineA.normal.y * lineB.c - lineA.c * lineB.normal.y; + PxF32 detY = lineA.c * lineB.normal.x - lineB.c * lineA.normal.x; + PxF32 x = detX / det1; + PxF32 y = detY / det1; + + if (x + V_COMP_EPS >= PxMax(PxMin(s1.x, e1.x), PxMin(s2.x, e2.x)) && + x - V_COMP_EPS <= PxMin(PxMax(s1.x, e1.x), PxMax(s2.x, e2.x)) && + y + V_COMP_EPS >= PxMax(PxMin(s1.y, e1.y), PxMin(s2.y, e2.y)) && + y - V_COMP_EPS <= PxMin(PxMax(s1.y, e1.y), PxMax(s2.y, e2.y))) + { + fInt.x = x; + fInt.y = y; + t1 = getParameter(s1, e1, fInt); + return 1; + } + + return 0; +} + +struct cwComparer +{ + PxVec3 basePoint; + PxVec3 normal; + cwComparer(PxVec3 basePointIn, PxVec3 norm) + { + basePoint = basePointIn; + normal = norm; + }; + bool operator()(const PxVec3& a, const PxVec3& b) + { + PxVec3 norm = (a - basePoint).cross(b - basePoint); + return normal.dot(norm) > 0; + } +}; + +bool vec3Comparer(const PxVec3& a, const PxVec3& b) +{ + if (a.x + V_COMP_EPS < b.x) return true; + if (a.x - V_COMP_EPS > b.x) return false; + if (a.y + V_COMP_EPS < b.y) return true; + if (a.y - V_COMP_EPS > b.y) return false; + if (a.z + V_COMP_EPS < b.z) return true; + return false; +} + +void TriangleProcessor::sortToCCW(std::vector<PxVec3>& points, PxVec3& normal) +{ + std::sort(points.begin(), points.end(), vec3Comparer); + int lastUnique = 0; + for (uint32_t i = 1; i < points.size(); ++i) + { + PxVec3 df = points[i] - points[lastUnique]; + if (df.x > V_COMP_EPS || df.y > V_COMP_EPS || df.z > V_COMP_EPS) + { + points[++lastUnique] = points[i]; + } + } + points.resize(lastUnique + 1); + if (points.size() > 2) + { + cwComparer compr(points[0], normal); + std::sort(points.begin() + 1, points.end(), compr); + } +} + + + +void TriangleProcessor::buildConvexHull(std::vector<PxVec3>& points, std::vector<PxVec3>& convexHull,const PxVec3& normal) +{ + + std::sort(points.begin(), points.end(), vec3Comparer); + int lastUnique = 0; + for (uint32_t i = 1; i < points.size(); ++i) + { + PxVec3 df = points[i] - points[lastUnique]; + if (df.x > V_COMP_EPS || df.y > V_COMP_EPS || df.z > V_COMP_EPS) + { + points[++lastUnique] = points[i]; + } + } + points.resize(lastUnique + 1); + if (points.size() > 2) + { + cwComparer compr(points[0], normal); + std::sort(points.begin() + 1, points.end(), compr); + } + if (points.size() < 3) + return; + convexHull.push_back(points[0]); + convexHull.push_back(points[1]); + ProjectionDirections projectionDirection = getProjectionDirection(normal); + for (uint32_t i = 2; i < points.size(); ++i) + { + PxVec2 pnt = getProjectedPointWithWinding(points[i], projectionDirection); + PxVec2 vec = pnt - getProjectedPointWithWinding(convexHull.back(), projectionDirection); + if (vec.x < V_COMP_EPS && vec.y < V_COMP_EPS) + { + continue; + } + if (getRotation(vec, getProjectedPointWithWinding(convexHull.back(), projectionDirection) - getProjectedPointWithWinding(convexHull[convexHull.size() - 2], projectionDirection)) < 0) + { + convexHull.push_back(points[i]); + } + else + { + while (convexHull.size() > 1 && getRotation(vec, getProjectedPointWithWinding(convexHull.back(), projectionDirection) - getProjectedPointWithWinding(convexHull[convexHull.size() - 2], projectionDirection)) > 0) + { + convexHull.pop_back(); + vec = pnt - getProjectedPointWithWinding(convexHull.back(), projectionDirection); + } + convexHull.push_back(points[i]); + } + } +} + + +uint32_t TriangleProcessor::getTriangleIntersection(TrPrcTriangle& a, TrPrcTriangle2d& aProjected, TrPrcTriangle &b, PxVec3& centroid, std::vector<PxVec3>& intersectionBuffer, PxVec3 normal) +{ + + b.points[0] -= centroid; + b.points[1] -= centroid; + b.points[2] -= centroid; + + ProjectionDirections prjDir = getProjectionDirection(normal); + + TrPrcTriangle2d bProjected; + bProjected.points[0] = getProjectedPointWithWinding(b.points[0], prjDir); + bProjected.points[1] = getProjectedPointWithWinding(b.points[1], prjDir); + bProjected.points[2] = getProjectedPointWithWinding(b.points[2], prjDir); + + + if (!triangleBoundingBoxIntersection(aProjected, bProjected)) return 0; + + //* Check triangle A against points of B *// + for (int i = 0; i < 3; ++i) + { + if (isPointInside(bProjected.points[i], aProjected)) + { + intersectionBuffer.push_back(b.points[i]); + } + } + //* Check triangle B against points of A *// + for (int i = 0; i < 3; ++i) + { + if (isPointInside(aProjected.points[i], bProjected)) + { + intersectionBuffer.push_back(a.points[i]); + } + } + + //* Check edges intersection *// + float param = 0; + for (int i = 0; i < 3; ++i) + { + for (int j = 0; j < 3; ++j) + { + if (getSegmentIntersection(aProjected.points[i], aProjected.points[(i + 1) % 3], bProjected.points[j], bProjected.points[(j + 1) % 3], param)) + { + intersectionBuffer.push_back(lerp3D(a.points[i], a.points[(i + 1) % 3], param)); + } + } + } + + if (intersectionBuffer.size() == 0) + return 0; + + // Intersection between two triangles is convex, but points should be reordered to construct right polygon // + std::vector<PxVec3> intrs; + buildConvexHull(intersectionBuffer, intrs, normal); + intersectionBuffer = intrs; + + // Return all points back from origin // + for (uint32_t i = 0; i < intersectionBuffer.size(); ++i) + { + intersectionBuffer[i] += centroid; + } + return 1; +} + + + +bool TriangleProcessor::triangleBoundingBoxIntersection(TrPrcTriangle2d& a, TrPrcTriangle2d& b) +{ + float fb = std::min(a.points[0].x, std::min(a.points[1].x, a.points[2].x)); + float fe = std::max(a.points[0].x, std::max(a.points[1].x, a.points[2].x)); + + float sb = std::min(b.points[0].x, std::min(b.points[1].x, b.points[2].x)); + float se = std::max(b.points[0].x, std::max(b.points[1].x, b.points[2].x)); + + if (std::min(fe, se) + V_COMP_EPS < std::max(fb, sb)) return 0; + + fb = std::min(a.points[0].y, std::min(a.points[1].y, a.points[2].y)); + fe = std::max(a.points[0].y, std::max(a.points[1].y, a.points[2].y)); + + sb = std::min(b.points[0].y, std::min(b.points[1].y, b.points[2].y)); + se = std::max(b.points[0].y, std::max(b.points[1].y, b.points[2].y)); + if (std::min(fe, se) + V_COMP_EPS < std::max(fb, sb)) return 0; + return 1; +} + + +uint32_t TriangleProcessor::isPointInside(const PxVec2& point, const TrPrcTriangle2d& triangle) +{ + PxF32 av = getRotation(point - triangle.points[0], triangle.points[1] - triangle.points[0]); + PxF32 bv = getRotation(point - triangle.points[1], triangle.points[2] - triangle.points[1]); + PxF32 cv = getRotation(point - triangle.points[2], triangle.points[0] - triangle.points[2]); + + + if (PxAbs(av) < COLLIN_EPS) av = 0; + if (PxAbs(bv) < COLLIN_EPS) bv = 0; + if (PxAbs(cv) < COLLIN_EPS) cv = 0; + + if (av >= 0 && bv >= 0 && cv >= 0) + { + if (av == 0 || bv == 0 || cv == 0) + return 2; + return 1; + } + if (av <= 0 && bv <= 0 && cv <= 0) + { + if (av == 0 || bv == 0 || cv == 0) + return 2; + return 1; + } + return 0; +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.h b/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.h new file mode 100644 index 0000000..db9f682 --- /dev/null +++ b/NvBlast/sdk/extensions/authoring/source/NvBlastExtTriangleProcessor.h @@ -0,0 +1,158 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTTRIANGLEPROCESSOR_H +#define NVBLASTEXTTRIANGLEPROCESSOR_H + +#include <PxPhysicsAPI.h> +#include <vector> +#include <algorithm> + +using namespace physx; + + +namespace Nv +{ +namespace Blast +{ + +/** + Triangle processor internal triangle representation. Contains only vertex positions. +*/ +struct TrPrcTriangle +{ + PxVec3 points[3]; + TrPrcTriangle(PxVec3 a = PxVec3(0.0f), PxVec3 b = PxVec3(0.0f), PxVec3 c = PxVec3(0.0f)) + { + points[0] = a; + points[1] = b; + points[2] = c; + } + + TrPrcTriangle& operator=(const TrPrcTriangle& b) + { + points[0] = b.points[0]; + points[1] = b.points[1]; + points[2] = b.points[2]; + return *this; + } + + TrPrcTriangle(const TrPrcTriangle& b) + { + points[0] = b.points[0]; + points[1] = b.points[1]; + points[2] = b.points[2]; + } + PxVec3 getNormal() const + { + return (points[1] - points[0]).cross(points[2] - points[0]); + } +}; + +/** + Triangle processor internal 2D triangle representation. Contains only vertex positions. +*/ +struct TrPrcTriangle2d +{ + PxVec2 points[3]; + TrPrcTriangle2d(PxVec2 a = PxVec2(0.0f), PxVec2 b = PxVec2(0.0f), PxVec2 c = PxVec2(0.0f)) + { + points[0] = a; + points[1] = b; + points[2] = c; + } + + TrPrcTriangle2d operator=(const TrPrcTriangle2d& b) + { + points[0] = b.points[0]; + points[1] = b.points[1]; + points[2] = b.points[2]; + return *this; + } + + TrPrcTriangle2d(const TrPrcTriangle2d& b) + { + points[0] = b.points[0]; + points[1] = b.points[1]; + points[2] = b.points[2]; + } +}; + +class TriangleProcessor +{ +public: + + + TriangleProcessor() + {}; + ~TriangleProcessor() + { + } + + + /** + Build intersection between two triangles + \param[in] a First triangle (A) + \param[in] aProjected Projected triangle A + \param[in] b Second triangle (B) + \param[in] centroid Centroid of first triangle (A) + \param[out] intersectionBuffer Result intersection polygon + \param[in] normal Normal vector to triangle (Common for both A and B). + \return 1 - if if intersection is found. + */ + uint32_t getTriangleIntersection(TrPrcTriangle& a, TrPrcTriangle2d& aProjected, TrPrcTriangle &b, PxVec3& centroid, std::vector<PxVec3>& intersectionBuffer, PxVec3 normal); + + /** + Test whether BB of triangles intersect. + \param[in] a First triangle (A) + \param[in] b Second triangle (B) + \return true - if intersect + */ + bool triangleBoundingBoxIntersection(TrPrcTriangle2d& a, TrPrcTriangle2d& b); + + + /** + Test whether point is inside of triangle. + \param[in] point Point coordinates in 2d space. + \param[in] triangle Triangle in 2d space. + \return 1 - if inside, 2 if on edge, 0 if neither inside nor edge. + */ + uint32_t isPointInside(const PxVec2& point, const TrPrcTriangle2d& triangle); + + /** + Segment intersection point + \param[in] s1 Segment-1 start point + \param[in] e1 Segment-1 end point + \param[in] s2 Segment-2 start point + \param[in] e2 Segment-2 end point + \param[out] t1 Intersection point parameter relatively to Segment-1, lies in [0.0, 1.0] range. + \return 0 if there is no intersections, 1 - if intersection is found. + */ + uint32_t getSegmentIntersection(const PxVec2& s1, const PxVec2& e1, const PxVec2& s2, const PxVec2& e2, PxF32& t1); + + /** + Sort vertices of polygon in CCW-order + */ + void sortToCCW(std::vector<PxVec3>& points, PxVec3& normal); + + /** + Builds convex polygon for given set of points. Points should be coplanar. + \param[in] points Input array of points + \param[out] convexHull Output polygon + \param[in] normal Normal vector to polygon. + */ + void buildConvexHull(std::vector<PxVec3>& points, std::vector<PxVec3>& convexHull, const PxVec3& normal); +}; + +} // namespace Blast +} // namespace Nv + + +#endif // NVBLASTEXTTRIANGLEPROCESSOR_H diff --git a/NvBlast/sdk/extensions/common/source/NvBlastExtAllocator.h b/NvBlast/sdk/extensions/common/source/NvBlastExtAllocator.h new file mode 100644 index 0000000..d917cbf --- /dev/null +++ b/NvBlast/sdk/extensions/common/source/NvBlastExtAllocator.h @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTALLOCATOR_H +#define NVBLASTEXTALLOCATOR_H + +#include "NvBlastTkFramework.h" +#include "PxAllocatorCallback.h" + + +namespace Nv +{ +namespace Blast +{ + +/** +ExtAllocator uses TkFramework allocator +*/ +class ExtAllocator +{ +public: + ExtAllocator(const char* = 0) + { + } + + void* allocate(size_t size, const char* filename, int line) + { + return NvBlastTkFrameworkGet()->getAllocatorCallback().allocate(size, nullptr, filename, line); + } + + void deallocate(void* ptr) + { + NvBlastTkFrameworkGet()->getAllocatorCallback().deallocate(ptr); + } + + + /** + Aligned allocation. + + Example using 16-byte alignment: + + // b will lie on a 16-byte boundary and point to 50 bytes of usable memory + void* b = alignedAlloc<16>(50); + */ + template<int A> + static void* alignedAlloc(size_t size, const char* filename, int line) + { + NV_COMPILE_TIME_ASSERT(A > 0 && A <= 256); + unsigned char* mem = (unsigned char*)ExtAllocator().allocate(size + A, filename, line); + const unsigned char offset = (unsigned char)((uintptr_t)A - (uintptr_t)mem % A - 1); + mem += offset; + *mem++ = offset; + return mem; + } + + template<int A> + static void* alignedAlloc(size_t size) + { + return alignedAlloc<A>(size, __FILE__, __LINE__); + } + + + /** + Version of alignedAlloc specialized 16-byte aligned allocation. + */ + static void* alignedAlloc16(size_t size) + { + return alignedAlloc<16>(size); + } + + + /** + Aligned deallocation. + + Memory freed using this function MUST have been allocated using alignedAlloc. + + Example using free: + + // Using the memory pointer b from the example above (for alignedAlloc) + alignedFree(b); + */ + static void alignedFree(void* block) + { + if (block != nullptr) + { + unsigned char* mem = (unsigned char*)block; + const unsigned char offset = *--mem; + ExtAllocator().deallocate(mem - offset); + } + }; +}; + + +/** +ExtAlignedAllocator uses ExtAllocator +*/ +template<int A> +class ExtAlignedAllocator +{ +public: + ExtAlignedAllocator(const char* = 0) + { + } + + void* allocate(size_t size, const char* filename, int line) + { + return ExtAllocator::alignedAlloc<A>(size, filename, line); + } + + void deallocate(void* ptr) + { + return ExtAllocator::alignedFree(ptr); + } +}; + +} // namespace Blast +} // namespace Nv + + +#endif // #ifndef NVBLASTEXTALLOCATOR_H diff --git a/NvBlast/sdk/extensions/common/source/NvBlastExtArray.h b/NvBlast/sdk/extensions/common/source/NvBlastExtArray.h new file mode 100644 index 0000000..9ea4777 --- /dev/null +++ b/NvBlast/sdk/extensions/common/source/NvBlastExtArray.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTARRAY_H +#define NVBLASTEXTARRAY_H + + +#include "NvBlastExtAllocator.h" +#include "PsInlineArray.h" + + +namespace Nv +{ +namespace Blast +{ + +template <class T> +struct ExtArray +{ + typedef physx::shdfnd::Array<T, ExtAllocator> type; +}; + + +template <class T, uint32_t N> +struct ExtInlineArray +{ + typedef physx::shdfnd::InlineArray<T, N, ExtAllocator> type; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // #ifndef NVBLASTEXTARRAY_H diff --git a/NvBlast/sdk/extensions/common/source/NvBlastExtDefs.h b/NvBlast/sdk/extensions/common/source/NvBlastExtDefs.h new file mode 100644 index 0000000..72b6c1d --- /dev/null +++ b/NvBlast/sdk/extensions/common/source/NvBlastExtDefs.h @@ -0,0 +1,64 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTDEFS_H +#define NVBLASTEXTDEFS_H + +#include "NvBlastTkFramework.h" +#include "PxAllocatorCallback.h" +#include <new> + + +//////// Log macros that use the ExtContext log function //////// + +#define NVBLASTEXT_LOG_ERROR(_msg) NVBLAST_LOG_ERROR(NvBlastTkFrameworkGet()->getLogFn(), _msg) +#define NVBLASTEXT_LOG_WARNING(_msg) NVBLAST_LOG_WARNING(NvBlastTkFrameworkGet()->getLogFn(), _msg) +#define NVBLASTEXT_LOG_INFO(_msg) NVBLAST_LOG_INFO(NvBlastTkFrameworkGet()->getLogFn(), _msg) +#define NVBLASTEXT_LOG_DEBUG(_msg) NVBLAST_LOG_DEBUG(NvBlastTkFrameworkGet()->getLogFn(), _msg) + +#define NVBLASTEXT_CHECK(_expr, _messageType, _msg, _onFail) \ + { \ + if(!(_expr)) \ + { \ + (*NvBlastTkFrameworkGet()->getLogFn())(_messageType, _msg, __FILE__, __LINE__); \ + { _onFail; }; \ + } \ + } + +#define NVBLASTEXT_CHECK_ERROR(_expr, _msg, _onFail) NVBLASTEXT_CHECK(_expr, NvBlastMessage::Error, _msg, _onFail) +#define NVBLASTEXT_CHECK_WARNING(_expr, _msg, _onFail) NVBLASTEXT_CHECK(_expr, NvBlastMessage::Warning, _msg, _onFail) +#define NVBLASTEXT_CHECK_INFO(_expr, _msg, _onFail) NVBLASTEXT_CHECK(_expr, NvBlastMessage::Info, _msg, _onFail) +#define NVBLASTEXT_CHECK_DEBUG(_expr, _msg, _onFail) NVBLASTEXT_CHECK(_expr, NvBlastMessage::Debug, _msg, _onFail) + + +//////// Allocator macros //////// + +/** +Placement new with ExtContext allocation. +Example: Foo* foo = NVBLASTEXT_NEW(Foo, context) (params); +*/ +#define NVBLASTEXT_NEW(T) new (NvBlastTkFrameworkGet()->getAllocatorCallback().allocate(sizeof(T), #T, __FILE__, __LINE__)) T + +/** +Respective delete to NVBLASTEXT_NEW +Example: NVBLASTEXT_DELETE(foo, Foo, context); +*/ +#define NVBLASTEXT_DELETE(obj, T) \ + (obj)->~T(); \ + NvBlastTkFrameworkGet()->getAllocatorCallback().deallocate(obj) + + +//////// Util macros //////// + +// Macro to load a uint32_t (or larger) with four characters +#define NVBLASTEXT_FOURCC(_a, _b, _c, _d) ( (uint32_t)(_a) | (uint32_t)(_b)<<8 | (uint32_t)(_c)<<16 | (uint32_t)(_d)<<24 ) + + +#endif // #ifndef NVBLASTEXTDEFS_H diff --git a/NvBlast/sdk/extensions/common/source/NvBlastExtHashMap.h b/NvBlast/sdk/extensions/common/source/NvBlastExtHashMap.h new file mode 100644 index 0000000..0fa094e --- /dev/null +++ b/NvBlast/sdk/extensions/common/source/NvBlastExtHashMap.h @@ -0,0 +1,34 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTHASHMAP_H +#define NVBLASTEXTHASHMAP_H + + +#include "NvBlastExtAllocator.h" +#include "PsHashMap.h" + + +namespace Nv +{ +namespace Blast +{ + +template <class Key, class Value, class HashFn = physx::shdfnd::Hash<Key>> +struct ExtHashMap +{ + typedef physx::shdfnd::HashMap<Key, Value, HashFn, ExtAllocator> type; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // #ifndef NVBLASTEXTHASHMAP_H diff --git a/NvBlast/sdk/extensions/common/source/NvBlastExtHashSet.h b/NvBlast/sdk/extensions/common/source/NvBlastExtHashSet.h new file mode 100644 index 0000000..97fc0a9 --- /dev/null +++ b/NvBlast/sdk/extensions/common/source/NvBlastExtHashSet.h @@ -0,0 +1,33 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTHASHSET_H +#define NVBLASTEXTHASHSET_H + + +#include "NvBlastExtAllocator.h" +#include "PsHashSet.h" + +namespace Nv +{ +namespace Blast +{ + +template <class Key, class HashFn = physx::shdfnd::Hash<Key>> +struct ExtHashSet +{ + typedef physx::shdfnd::HashSet<Key, HashFn, ExtAllocator> type; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // #ifndef NVBLASTEXTHASHSET_H diff --git a/NvBlast/sdk/extensions/converter/include/NvBlastExtDataConverter.h b/NvBlast/sdk/extensions/converter/include/NvBlastExtDataConverter.h new file mode 100644 index 0000000..b251518 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/include/NvBlastExtDataConverter.h @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTDATACONVERTER_H +#define NVBLASTEXTDATACONVERTER_H + + +#include "NvBlast.h" +#include <vector> + +namespace Nv +{ +namespace Blast +{ + /** + Generic version conversion function for Blast data blocks. + + Automatically determines block type (one of NvBlastDataBlock::Type) and uses appropriate converter. + + \param[out] outBlock User-supplied memory block to fill with new data. + \param[in] inBlock Data block to convert. + \param[in] outBlockVersion Version to convert too, pass 'nullptr' to convert to the latest version. + + \return true iff conversion was successful. + */ + NVBLAST_API bool convertDataBlock(std::vector<char>& outBlock, const std::vector<char>& inBlock, uint32_t* outBlockVersion = nullptr); + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTDATACONVERTER_H diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h new file mode 100644 index 0000000..44a0b54 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h @@ -0,0 +1,88 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H +#define NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H + + +#include "NvBlastExtBinaryBlockConverter.h" + + +namespace Nv +{ +namespace Blast +{ + +/* + WARNING: THIS CLASS IS AN EXAMPLE. + REPLACE WITH ACTUAL CONVERSION CODE ONCE NEEDED. +*/ +class NvBlastAssetBlockVersionConverter_v0_v1 : public BinaryBlockConverter::VersionConverter +{ +public: + virtual uint32_t getVersionFrom() const { return NvBlastAssetDataFormat::Initial; } + + virtual uint32_t getVersionTo() const { return 1/*NvBlastAssetDataFormat::BondCountSwap*/; } + + // remains the same + struct SupportGraph + { + uint32_t m_nodeCount; + uint32_t m_chunkIndicesOffset; + uint32_t m_adjacencyPartitionOffset; + uint32_t m_adjacentNodeIndicesOffset; + uint32_t m_adjacentBondIndicesOffset; + }; + + // prev version + struct AssetDataHeaderPrev + { + uint32_t m_formatVersion; + uint32_t m_size; + NvBlastID m_ID; + uint32_t m_totalChunkCount; + SupportGraph m_graph; + uint32_t m_leafChunkCount; + uint32_t m_firstSubsupportChunkIndex; // 0 + uint32_t m_bondCount; // 1 + }; + + // new version + struct AssetDataHeaderNew + { + uint32_t m_formatVersion; + uint32_t m_size; + NvBlastID m_ID; + uint32_t m_totalChunkCount; + SupportGraph m_graph; + uint32_t m_leafChunkCount; + uint32_t m_bondCount; // 1 + uint32_t m_firstSubsupportChunkIndex; // 0 + }; + + bool convert(const std::vector<char>& from, std::vector<char>& to) const + { + to = from; + + const AssetDataHeaderPrev* headerPrev = reinterpret_cast<const AssetDataHeaderPrev*>(from.data()); + AssetDataHeaderNew* headerNew = reinterpret_cast<AssetDataHeaderNew*>(to.data()); + headerNew->m_bondCount = headerPrev->m_bondCount; + headerNew->m_firstSubsupportChunkIndex = headerPrev->m_firstSubsupportChunkIndex; + headerNew->m_formatVersion = (uint32_t)getVersionTo(); + + return true; + } +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp new file mode 100644 index 0000000..a606b70 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp @@ -0,0 +1,152 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtBinaryBlockConverter.h" +#include <iostream> +#include <algorithm> +#include <queue> +#include <limits> + + +namespace Nv +{ +namespace Blast +{ + +bool BinaryBlockConverter::convertBinaryBlock(std::vector<char>& outBlock, const std::vector<VersionConverterPtr>& converters, const std::vector<char>& inBlock, uint32_t outBlockVersion, uint32_t inBlockVersion) +{ + if (inBlock.empty()) + { + std::cerr << "Conversion failed: empty input block." << std::endl; + return false; + } + + // starting version + uint32_t version; + version = inBlockVersion; + std::cout << "Input block version: " << version << std::endl; + + // target version + const uint32_t targetVersion = outBlockVersion; + std::cout << "Target version: " << targetVersion << std::endl; + + // search conversion path + std::vector<VersionConverterPtr> conversionPath; + if (!findShortestPath(conversionPath, converters, version, targetVersion)) + { + std::cerr << "Conversion failed: can't find conversion path." << std::endl; + return false; + } + + // starting convertion loop + std::vector<char> blockFrom(inBlock.begin(), inBlock.end()); + std::vector<char> blockTo(inBlock.size()); + for (const VersionConverterPtr converter : conversionPath) + { + // actual conversion call + std::cout << "Converting from version: " << converter->getVersionFrom() << " -> " << converter->getVersionTo() << " Result: "; + if (!converter->convert(blockFrom, blockTo)) + { + std::cout << "Fail." << std::endl; + std::cerr << "Conversion failed: inside converter for version: " << version << std::endl; + return false; + } + else + { + std::cout << "Success." << std::endl; + blockFrom.swap(blockTo); + version = converter->getVersionTo(); + } + } + + // copy result + outBlock = blockFrom; + + return true; +} + + +/** +Finds shortest path form versionFrom to verstionTo using breadth-first search with DP +*/ +bool BinaryBlockConverter::findShortestPath(std::vector<VersionConverterPtr>& conversionPath, const std::vector<VersionConverterPtr>& converters, uint32_t versionFrom, uint32_t versionTo) +{ + // find max version + uint32_t versionMax = 0; + for (VersionConverterPtr c : converters) + { + versionMax = std::max(versionMax, c->getVersionFrom()); + versionMax = std::max(versionMax, c->getVersionTo()); + } + + // dynamic programming data + struct Node + { + uint32_t distance; + VersionConverterPtr converter; + + Node() : distance(std::numeric_limits<uint32_t>::max()), converter(nullptr) {} + }; + std::vector<Node> nodes(versionMax + 1); + + // initial state (start from versionTo) + std::queue<uint32_t> q; + q.emplace(versionTo); + nodes[versionTo].distance = 0; + nodes[versionTo].converter = nullptr; + + // breadth-first search + bool found = false; + while (!q.empty() && !found) + { + uint32_t v0 = q.front(); + q.pop(); + + for (const VersionConverterPtr c : converters) + { + if (c->getVersionTo() == v0) + { + uint32_t v1 = c->getVersionFrom(); + if (nodes[v1].distance > nodes[v0].distance + 1) + { + nodes[v1].distance = nodes[v0].distance + 1; + nodes[v1].converter = c; + q.emplace(v1); + } + + if (c->getVersionFrom() == versionFrom) + { + found = true; + break; + } + } + } + } + + if (found) + { + // unfold found path to result conversionPath + uint32_t v = versionFrom; + conversionPath.clear(); + while (nodes[v].converter.get() != nullptr) + { + conversionPath.push_back(nodes[v].converter); + v = nodes[v].converter->getVersionTo(); + } + return true; + } + else + { + return false; + } +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h new file mode 100644 index 0000000..83eb6b3 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTBINARYBLOCKCONVERTER_H +#define NVBLASTEXTBINARYBLOCKCONVERTER_H + + +#include "NvBlast.h" +#include <vector> +#include <memory> + + +namespace Nv +{ +namespace Blast +{ + +/** +Generic binary block converter class. + +BinaryBlockConverter is an abstract class, as well as it's member class VersionConverter. In order to implement your own +binary converter - implement for every version conversion BinaryBlockConverter::VersionConverter. Then implement BinaryBlockConverter +where getVersionConverters() should return all your implemented BinaryBlockConverter::VersionConverter's. + +*/ +class BinaryBlockConverter +{ +public: + class VersionConverter + { + public: + virtual uint32_t getVersionFrom() const = 0; + virtual uint32_t getVersionTo() const = 0; + virtual bool convert(const std::vector<char>& from, std::vector<char>& to) const = 0; + }; + + typedef std::shared_ptr<VersionConverter> VersionConverterPtr; + + static bool convertBinaryBlock(std::vector<char>& outBlock, const std::vector<VersionConverterPtr>& converters, const std::vector<char>& inBlock, uint32_t outBlockVersion, uint32_t inBlockVersion); +protected: + +private: + static bool findShortestPath(std::vector<VersionConverterPtr>& conversionPath, const std::vector<VersionConverterPtr>& converters, uint32_t versionFrom, uint32_t versionTo); +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTBINARYBLOCKCONVERTER_H diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp new file mode 100644 index 0000000..fe8c745 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp @@ -0,0 +1,103 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtDataConverter.h" +#include "NvBlastExtBinaryBlockConverter.h" + +#include <iostream> + +// asset converters +#include "NvBlastExtAssetBlockVersionConverter_v0_v1.h" + + +namespace Nv +{ +namespace Blast +{ + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Asset Block Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +NV_INLINE std::vector<BinaryBlockConverter::VersionConverterPtr> getAssetConverters() +{ + /** + +==========================================+ + | HINT: ADD NEW VERSION CONVERTERS THERE | + +==========================================+ + */ + BinaryBlockConverter::VersionConverterPtr converters[] = + { + std::make_shared<NvBlastAssetBlockVersionConverter_v0_v1>() + }; + + return std::vector<BinaryBlockConverter::VersionConverterPtr>(converters, converters + sizeof(converters) / sizeof(converters[0])); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Family Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +NV_INLINE std::vector<BinaryBlockConverter::VersionConverterPtr> getFamilyConverters() +{ + /** + +==========================================+ + | HINT: ADD NEW VERSION CONVERTERS THERE | + +==========================================+ + */ + BinaryBlockConverter::VersionConverterPtr converters[] = + { + nullptr //std::make_shared<NvBlastFamilyVersionConverter_v0_v1>() + }; + + return std::vector<BinaryBlockConverter::VersionConverterPtr>(converters, converters + sizeof(converters) / sizeof(converters[0])); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Generic Block Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +bool convertDataBlock(std::vector<char>& outBlock, const std::vector<char>& inBlock, uint32_t* outBlockVersion) +{ + // Pick header to determine dataType and version + if (inBlock.size() < sizeof(NvBlastDataBlock)) + { + std::cerr << "Conversion failed: invalid block, passed block is too small." << std::endl; + return false; + } + const NvBlastDataBlock* dataBlock = reinterpret_cast<const NvBlastDataBlock*>(inBlock.data()); + + // Select appropriate converters and version based on dataType + std::vector<BinaryBlockConverter::VersionConverterPtr> converters; + uint32_t version; + switch (dataBlock->dataType) + { + case NvBlastDataBlock::AssetDataBlock: + std::cout << "Input block dataType: NvBlastDataBlock::Asset" << std::endl; + converters = getAssetConverters(); + version = (outBlockVersion == nullptr ? static_cast<uint32_t>(NvBlastAssetDataFormat::Current) : *outBlockVersion); + break; + case NvBlastDataBlock::FamilyDataBlock: + std::cout << "Input block dataType: NvBlastDataBlock::Family" << std::endl; + converters = getFamilyConverters(); + version = (outBlockVersion == nullptr ? static_cast<uint32_t>(NvBlastFamilyDataFormat::Current) : *outBlockVersion); + break; + default: + std::cerr << "Conversion failed: unsupported dataType: " << dataBlock->dataType << std::endl; + return false; + } + + return BinaryBlockConverter::convertBinaryBlock(outBlock, converters, inBlock, version, dataBlock->formatVersion); +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/import/include/NvBlastExtApexImportTool.h b/NvBlast/sdk/extensions/import/include/NvBlastExtApexImportTool.h new file mode 100644 index 0000000..bbfcfce --- /dev/null +++ b/NvBlast/sdk/extensions/import/include/NvBlastExtApexImportTool.h @@ -0,0 +1,199 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAPEXIMPORTTOOL_H +#define NVBLASTEXTAPEXIMPORTTOOL_H + +#include "NvBlast.h" +#include <vector> +#include <string> +#include "NvBlastExtPxAsset.h" + +namespace physx +{ +class PxErrorCallback; +class PxAllocatorCallback; +namespace general_PxIOStream2 +{ +class PxFileBuf; +} +} + +namespace nvidia +{ +namespace apex +{ +class ApexSDK; +class ModuleDestructible; +class DestructibleAsset; +} +using namespace physx::general_PxIOStream2; +} + + +namespace Nv +{ +namespace Blast +{ + +struct CollisionHull; +class TkFramework; + +namespace ApexImporter +{ + +struct ApexImporterConfig +{ + /** + Interface search mode: + + EXACT - - Importer tries to find triangles from two chunks which lay in common surface. + If such triangles are found, their intersections are considered as the interface. + + FORCED - Bond creation is forced no matter how far chunks from each other. + + */ + enum InterfaceSearchMode { EXACT, FORCED, MODE_COUNT }; + + ApexImporterConfig() + { + setDefaults(); + } + + void setDefaults() + { + infSearchMode = EXACT; + } + InterfaceSearchMode infSearchMode; +}; + + +class ApexDestruction; + + +/** + ApexImportTool provides routines to create NvBlastAssets from APEX assets. +*/ +class ApexImportTool +{ +public: + + /** + Constructor should be provided with user defined allocator and massage function: + \param[in] logFn User - supplied message function(see NvBlastLog definition).May be NULL. + */ + ApexImportTool(NvBlastLog logFn = NULL); + ~ApexImportTool(); + + ////////////////////////////////////////////////////////////////////////////// + + /** + Before using ApexImportTool should be initialized. ApexSDK and ModuleDestructible initialized internally. + \return If true, ApexImportTool initialized successfully. + */ + bool initialize(); + + /** + Before using ApexImportTool should be initialized. User can provide existing ApexSDK and ModuleDestructible objects + \param[in] apexSdk Pointer on ApexSDK object + \param[in] moduleDestructible Pointer on ModuleDestructible object + \return If true, ApexImportTool initialized successfully. + */ + bool initialize(nvidia::apex::ApexSDK* apexSdk, nvidia::apex::ModuleDestructible* moduleDestructible); + + /** + Checks whether ApexImportTool is initialized and can be used. + \return If true, ApexImportTool initialized successfully. + */ + bool isValid(); + + + /** + Method loads APEX Destruction asset from file + \param[in] stream Pointer on PxFileBuf stream with Apex Destruction asset + \return If not 0, pointer on DestructibleAsset object is returned. + */ + nvidia::apex::DestructibleAsset* loadAssetFromFile(nvidia::PxFileBuf* stream); + + + /** + Method builds NvBlastAsset form provided DestructibleAsset. DestructibleAsset must contain support graph! + \param[out] chunkReorderInvMap Chunk map from blast chunk to apex chunk to be filled. + \param[in] apexAsset Pointer on DestructibleAsset object which should be converted to NvBlastAsset + \param[out] chunkDescriptors Reference on chunk descriptors array to be filled. + \param[out] bondDescriptors Reference on bond descriptors array to be filled. + \param[out] flags Reference on chunk flags to be filled. + + \return If true, output arrays are filled. + */ + bool importApexAsset(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondDescriptors, std::vector<uint32_t>& flags); + + /** + Method builds NvBlastAsset form provided DestructibleAsset. DestructibleAsset must contain support graph! + Parameteres of conversion could be provided with ApexImporterConfig. + \param[out] chunkReorderInvMap Chunk map from blast chunk to apex chunk to be filled. + \param[in] apexAsset Pointer on DestructibleAsset object which should be converted to NvBlastAsset + \param[out] chunkDescriptors Reference on chunk descriptors array to be filled. + \param[out] bondDescriptors Reference on bond descriptors array to be filled. + \param[out] flags Reference on chunk flags to be filled. + \param[in] config ApexImporterConfig object with conversion parameters, see above. + \return If true, output arrays are filled. + */ + bool importApexAsset(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondDescriptors, std::vector<uint32_t>& flags, + const ApexImporterConfig& config); + + + /** + Method serializes user-supplied NvBlastAsset object to user-supplied PxFileBuf stream. + \param[in] asset Pointer on NvBlastAsset object which should be serialized + \param[in] stream Pointer on PxFileBuf object in which NvBlastAsset should be serialized. + \return If true, NvBlastAsset object serialized successfully. + */ + bool saveAsset(const NvBlastAsset* asset, nvidia::PxFileBuf* stream); + + /** + Method creates collision geometry from user-supplied APEX Destructible asset. + \param[in] apexAsset Pointer on DestructibleAsset object for which collision geometry should be created. + \param[in] chunkCount Blast asset chunk count, should be equal to number of blast chunk descriptors which are gathered at ApexImportTool::importApexAsset(...) + \param[in] chunkReorderInvMap Chunk map from blast chunk to apex chunk filled in ApexImportTool::importApexAsset(...) + \param[in] apexChunkFlags Chunk flags array + \param[out] physicsChunks Chunk physics info output array + \param[out] physicsSubchunks Chunk collision geometry and transformation data output array + \return If true - success, output arrays are filled. + */ + bool getCollisionGeometry(const nvidia::apex::DestructibleAsset* apexAsset, uint32_t chunkCount, std::vector<uint32_t>& chunkReorderInvMap, + const std::vector<uint32_t>& apexChunkFlags, std::vector<ExtPxAssetDesc::ChunkDesc>& physicsChunks, + std::vector<ExtPxAssetDesc::SubchunkDesc>& physicsSubchunks); + + ApexDestruction* m_apexDestruction; + ////////////////////////////////////////////////////////////////////////////// + +private: + bool importApexAssetInternal(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondDesc, std::vector<uint32_t>& flags, + const ApexImporterConfig& configDesc); + + +protected: + NvBlastLog m_log; + +protected: + ApexImportTool(const ApexImportTool&); + ApexImportTool& operator=(const ApexImportTool&); +}; + +} // namespace ApexImporter + +} // namespace Blast +} // namespace Nv + +#endif // NVBLASTEXTAPEXIMPORTTOOL_H diff --git a/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.cpp b/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.cpp new file mode 100644 index 0000000..889a8c8 --- /dev/null +++ b/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.cpp @@ -0,0 +1,220 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtApexDestruction.h" + + +#include "PxPhysicsAPI.h" +#include "Apex.h" +#include <ModuleDestructible.h> +#include <DestructibleAsset.h> +#include "NullRenderer.h" +#include "PsString.h" + +using namespace nvidia; +using namespace apex; + +////////////////////////////////////////////////////////////////////////////// + +PxDefaultAllocator gPxAllocator; +PxDefaultErrorCallback gErrorCallback; +NullRenderResourceManager gNullRenderer; + +///////////////////////////////////////////////////////////////////////////// + +namespace Nv +{ +namespace Blast +{ +using namespace ApexImporter; + +ApexDestruction::ApexDestruction(NvBlastLog log) +{ + m_log = log; + initialize(); +} + +ApexDestruction::ApexDestruction(ApexSDK* apexSdk, ModuleDestructible* moduleDestructible, NvBlastLog log) +{ + m_log = log; + + m_Foundation.reset(&apexSdk->getPhysXSDK()->getFoundation(), false); + m_PhysxSDK.reset(apexSdk->getPhysXSDK(), false); + m_Cooking.reset(apexSdk->getCookingInterface(), false); + m_ApexSDK.reset(apexSdk, false); + for (uint32_t i = 0; i < apexSdk->getNbModules(); ++i) + { + if (!physx::shdfnd::strcmp(apexSdk->getModules()[i]->getName(), "Legacy")) + { + hasLegacyModule = true; + } + } + m_DestructibleModule.reset(moduleDestructible, false); +} + + +bool ApexDestruction::isValid() +{ + return m_PhysxSDK && m_Cooking && m_ApexSDK && m_DestructibleModule && hasLegacyModule; +} + +bool ApexDestruction::initialize() +{ + if (isValid()) + { + return true; + } + + ////////////////////////////////////////////////////////////////////////////// + + m_Foundation.reset(PxCreateFoundation(PX_FOUNDATION_VERSION, gPxAllocator, gErrorCallback)); + if (!m_Foundation) + { + if (m_log) + { + m_log(NvBlastMessage::Error, "Error: failed to create Foundation\n", __FILE__, __LINE__); + } + return false; + } + physx::PxTolerancesScale scale; + m_PhysxSDK.reset(PxCreatePhysics(PX_PHYSICS_VERSION, *m_Foundation, scale, true)); + if (!m_PhysxSDK) + { + if (m_log) + { + m_log(NvBlastMessage::Error, "Error: failed to create PhysX\n", __FILE__, __LINE__); + } + return false; + } + +#if 0 + if (!PxInitExtensions(*mPhysxSDK, 0)) + { + if (m_log) + { + m_log(Error, "Error: failed to init PhysX extensions\n", __FILE__, __LINE__); + } + return false; + } +#endif + + physx::PxCookingParams cookingParams(scale); + cookingParams.buildGPUData = true; + m_Cooking.reset(PxCreateCooking(PX_PHYSICS_VERSION, m_PhysxSDK->getFoundation(), cookingParams)); + if (!m_Cooking) + { + m_log(NvBlastMessage::Error, "Error: failed to create PhysX Cooking\n", __FILE__, __LINE__); + return false; + } + + ////////////////////////////////////////////////////////////////////////////// + + ApexSDKDesc apexSDKDesc; + apexSDKDesc.physXSDK = m_PhysxSDK.get(); + apexSDKDesc.cooking = m_Cooking.get(); + apexSDKDesc.renderResourceManager = &gNullRenderer; + apexSDKDesc.resourceCallback = nullptr; + apexSDKDesc.foundation = &m_PhysxSDK->getFoundation(); + + m_ApexSDK.reset(CreateApexSDK(apexSDKDesc, nullptr, APEX_SDK_VERSION)); + if (!m_ApexSDK) + { + if (m_log) + { + m_log(NvBlastMessage::Error, "Error: failed to create APEX\n", __FILE__, __LINE__); + } + return false; + } + + ////////////////////////////////////////////////////////////////////////////// + + m_DestructibleModule.reset(static_cast<nvidia::apex::ModuleDestructible*>(m_ApexSDK->createModule("Destructible")), ApexReleaser(*m_ApexSDK)); + if (!m_DestructibleModule) + { + if (m_log) + { + m_log(NvBlastMessage::Error, "Error: failed to create ModuleDestructible\n", __FILE__, __LINE__); + } + return false; + } + + if (!m_ApexSDK->createModule("Legacy")) + { + if (m_log) + { + m_log(NvBlastMessage::Error, "Error: failed to create Legacy module\n", __FILE__, __LINE__); + } + return false; + }; + + + float massScaleExponenent = 1.f; + float massScale = 1.f; + + NvParameterized::Interface* params = m_DestructibleModule->getDefaultModuleDesc(); + NvParameterized::Handle paramsHandle(params); + paramsHandle.getParameter("scaledMassExponent"); + paramsHandle.setParamF32(massScaleExponenent); + paramsHandle.getParameter("massScale"); + paramsHandle.setParamF32(massScale); + m_DestructibleModule->init(*params); + + return true; +} + +DestructibleAsset* ApexDestruction::loadAsset(physx::PxFileBuf* stream) +{ + DestructibleAsset* asset = nullptr; + + if (stream && stream->isOpen()) + { + NvParameterized::Serializer::SerializeType serType = apexSDK()->getSerializeType(*stream); + NvParameterized::Serializer::ErrorType serError; + NvParameterized::Serializer* ser = apexSDK()->createSerializer(serType); + PX_ASSERT(ser); + + NvParameterized::Serializer::DeserializedData data; + serError = ser->deserialize(*stream, data); + + if (serError == NvParameterized::Serializer::ERROR_NONE && data.size() == 1) + { + NvParameterized::Interface* params = data[0]; + if (!physx::shdfnd::strcmp(params->className(), "DestructibleAssetParameters")) + { + asset = static_cast<DestructibleAsset*>(apexSDK()->createAsset(params, "")); + } + else + { + m_log(NvBlastMessage::Error, "Error: deserialized data is not an APEX Destructible\n", __FILE__, __LINE__); + } + } + else + { + m_log(NvBlastMessage::Error, "Error: failed to deserialize\n", __FILE__, __LINE__); + } + ser->release(); + } + + if (!asset) + { + char message[255] = { 0 }; + sprintf(message, "Error: failed to load asset...\n"); + m_log(NvBlastMessage::Error, message, __FILE__, __LINE__); + } + + return asset; +} + +ApexDestruction::~ApexDestruction() +{ +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.h b/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.h new file mode 100644 index 0000000..6560aed --- /dev/null +++ b/NvBlast/sdk/extensions/import/source/NvBlastExtApexDestruction.h @@ -0,0 +1,108 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTAPEXDESTRUCTION_H +#define NVBLASTEXTAPEXDESTRUCTION_H + +#include "ApexUsingNamespace.h" +#include "NvBlastExtScopedResource.h" +#include "PsUtilities.h" +#include <string> +#include <NvBlastTypes.h> +#include <PxFileBuf.h> + +namespace physx +{ + class PxFoundation; +} + + +namespace Nv +{ +namespace Blast +{ + +namespace ApexImporter +{ + /** + Class for working with APEX Destruction assets. + */ +class ApexDestruction +{ + PX_NOCOPY(ApexDestruction) + +public: + ApexDestruction(NvBlastLog log = NULL); + + ApexDestruction(nvidia::apex::ApexSDK* apexSdk, nvidia::apex::ModuleDestructible* moduleDestructible, NvBlastLog log = NULL); + ~ApexDestruction(); + + ////////////////////////////////////////////////////////////////////////////// + /** + ApexDestruction initialization. If APEX SDK and ModuleDestructible was provided to constructor, they will be used. + Otherwise, PhysXSDK and APEX SDK will be initialized. + */ + bool initialize(); + + /** + /return Return True if tool initialized sucessfully. + */ + bool isValid(); + + /** + Load Apex Destructible asset from PxFileBuf stream + \param[in] stream Apex asset stream + /return Return DestructibleAsset* if success, otherwise nullptr is returned. + */ + nvidia::apex::DestructibleAsset* loadAsset(physx::PxFileBuf* stream); + + /** + /return Return PxFoundation. + */ + physx::PxFoundation* foundation() { return m_Foundation.get(); } + /** + /return Return PxPhysics. + */ + physx::PxPhysics* physxSDK() { return m_PhysxSDK.get(); } + /** + /return Return PxCooking. + */ + physx::PxCooking* cooking() { return m_Cooking.get(); } + /** + /return Return ApexSDK. + */ + nvidia::apex::ApexSDK* apexSDK() { return m_ApexSDK.get(); } + + /** + /return Return ModuleDestructible. + */ + nvidia::apex::ModuleDestructible* destructibleModule() { return m_DestructibleModule.get(); } + +private: + bool hasLegacyModule; + NvBlastLog m_log; + ////////////////////////////////////////////////////////////////////////////// + +protected: + ScopedResource<physx::PxFoundation> m_Foundation; + ScopedResource<physx::PxPhysics> m_PhysxSDK; + ScopedResource<physx::PxCooking> m_Cooking; + ScopedResource<nvidia::apex::ApexSDK> m_ApexSDK; + ScopedResource<nvidia::apex::ModuleDestructible, ApexReleaser> m_DestructibleModule; + +}; + +} // namespace ApexImporter + +} // namespace Blast +} // namespace Nv + + +#endif // NVBLASTEXTAPEXDESTRUCTION_H diff --git a/NvBlast/sdk/extensions/import/source/NvBlastExtApexImportTool.cpp b/NvBlast/sdk/extensions/import/source/NvBlastExtApexImportTool.cpp new file mode 100644 index 0000000..d2def6f --- /dev/null +++ b/NvBlast/sdk/extensions/import/source/NvBlastExtApexImportTool.cpp @@ -0,0 +1,490 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtApexImportTool.h" + +#if NV_VC +#pragma warning(push) +#pragma warning(disable: 4996) // 'fopen' unsafe warning, from NxFileBuffer.h +#endif + +#include "PxFoundation.h" +#include "PxErrorCallback.h" +#include "PxAllocatorCallback.h" + +#include "NvBlastIndexFns.h" +#include "DestructibleAsset.h" +#include "NvBlastExtApexDestruction.h" +#include <PxConvexMesh.h> +#include "PxPhysics.h" +#include "NvBlastExtAuthoringCollisionBuilder.h" +#include "NvBlastExtPxAsset.h" +#include "NvBlastExtAuthoringTypes.h" +#include "NvBlastExtAuthoringBondGenerator.h" + +using namespace nvidia; +using namespace apex; + +namespace Nv +{ +namespace Blast +{ + +namespace ApexImporter +{ + /** + Should be consistent with IntPair in APEX + */ + struct IntPair + { + void set(int32_t _i0, int32_t _i1) + { + i0 = _i0; + i1 = _i1; + } + + int32_t i0, i1; + + static int compare(const void* a, const void* b) + { + const int32_t diff0 = ((IntPair*)a)->i0 - ((IntPair*)b)->i0; + return diff0 ? diff0 : (((IntPair*)a)->i1 - ((IntPair*)b)->i1); + } + }; + +ApexImportTool::ApexImportTool(NvBlastLog log) + : m_apexDestruction(NULL), m_log(log) +{ +} + + +bool ApexImportTool::isValid() +{ + return m_apexDestruction && m_apexDestruction->isValid(); +} + + +bool ApexImportTool::initialize() +{ + if (isValid()) + { + return true; + } + m_log(NvBlastMessage::Info, "APEX initialization \n", __FILE__, __LINE__); + m_apexDestruction = new ApexDestruction(m_log); + return isValid(); +} + + +bool ApexImportTool::initialize(nvidia::apex::ApexSDK* apexSdk, nvidia::apex::ModuleDestructible* moduleDestructible) +{ + if (isValid()) + { + return true; + } + m_log(NvBlastMessage::Info, "APEX initialization \n", __FILE__, __LINE__); + m_apexDestruction = new ApexDestruction(apexSdk, moduleDestructible, m_log); + return isValid(); +} + +DestructibleAsset* ApexImportTool::loadAssetFromFile(physx::PxFileBuf* stream) +{ + return m_apexDestruction->loadAsset(stream); +} + + +bool ApexImportTool::getCollisionGeometry(const nvidia::apex::DestructibleAsset* apexAsset, uint32_t chunkCount, std::vector<uint32_t>& chunkReorderInvMap, + const std::vector<uint32_t>& apexChunkFlags, std::vector<ExtPxAssetDesc::ChunkDesc>& physicsChunks, + std::vector<ExtPxAssetDesc::SubchunkDesc>& physicsSubchunks) +{ + physicsChunks.clear(); + physicsChunks.resize(chunkCount); + // prepare physics asset desc (convexes, transforms) + ConvexMeshBuilder collisionBuilder(m_apexDestruction->cooking(), &m_apexDestruction->apexSDK()->getPhysXSDK()->getPhysicsInsertionCallback()); + int32_t apexHullCount = 0; + const uint32_t apexChunkCount = apexAsset->getChunkCount(); + for (uint32_t chunkIndex = 0; chunkIndex < chunkCount; ++chunkIndex) + { + uint32_t apexChunkIndex = chunkReorderInvMap[chunkIndex]; + if (apexChunkIndex < apexChunkCount) + { + uint32_t partIndex = apexAsset->getPartIndex(apexChunkIndex); + apexHullCount += apexAsset->getPartConvexHullCount(partIndex); + } + } + physicsSubchunks.reserve(chunkCount); + { + for (uint32_t chunkIndex = 0; chunkIndex < chunkCount; ++chunkIndex) + { + uint32_t apexChunkIndex = chunkReorderInvMap[chunkIndex]; + if (apexChunkIndex < apexChunkCount) + { + uint32_t partIndex = apexAsset->getPartIndex(apexChunkIndex); + uint32_t partConvexHullCount = apexAsset->getPartConvexHullCount(partIndex); + NvParameterized::Interface** cxInterfaceArray = apexAsset->getPartConvexHullArray(partIndex); + physicsChunks[chunkIndex].subchunkCount = partConvexHullCount; + for (uint32_t hull = 0; hull < partConvexHullCount; ++hull) + { + NvParameterized::Handle paramHandle(cxInterfaceArray[hull]); + int32_t verticesCount = 0; + paramHandle.getParameter("vertices"); + paramHandle.getArraySize(verticesCount); + std::vector<PxVec3> vertexData(verticesCount); + paramHandle.getParamVec3Array(vertexData.data(), verticesCount); + + PxConvexMesh* convexMesh = collisionBuilder.buildConvexMesh(vertexData); + + const ExtPxAssetDesc::SubchunkDesc subchunk = + { + PxTransform(PxIdentity), + PxConvexMeshGeometry(convexMesh) + }; + physicsSubchunks.push_back(subchunk); + } + physicsChunks[chunkIndex].subchunks = partConvexHullCount ? (&physicsSubchunks.back() + 1 - partConvexHullCount) : nullptr; + + // static flag set + physicsChunks[chunkIndex].isStatic = (apexChunkFlags[apexChunkIndex] & (1 << 1)) != 0; + } + else + { + // this is earth chunk + physicsChunks[chunkIndex].isStatic = true; + } + } + } + + // check that vector didn't grow + if (static_cast<int32_t>(physicsSubchunks.size()) > apexHullCount) + { + m_log(NvBlastMessage::Error, "Error: sub chunk count seems to be wrong. \n", __FILE__, __LINE__); + return false; + } + return true; +} + +void gatherChunkHullPoints(const DestructibleAsset* apexAsset, std::vector<std::vector<PxVec3> >& hullPoints) +{ + hullPoints.resize(apexAsset->getChunkCount()); + for (uint32_t chunk = 0; chunk < apexAsset->getChunkCount(); ++chunk) + { + int32_t part = apexAsset->getPartIndex(chunk); + NvParameterized::Interface** cxInterfaceArray = apexAsset->getPartConvexHullArray(part); + for (uint32_t hull = 0; hull < apexAsset->getPartConvexHullCount(part); ++hull) + { + NvParameterized::Handle paramHandle(cxInterfaceArray[hull]); + int32_t verticesCount = 0; + paramHandle.getParameter("vertices"); + paramHandle.getArraySize(verticesCount); + uint32_t oldSize = (uint32_t)hullPoints[chunk].size(); + hullPoints[chunk].resize(hullPoints[chunk].size() + verticesCount); + paramHandle.getParamVec3Array(hullPoints[chunk].data() + oldSize, verticesCount); + } + } +} +PxBounds3 gatherChunkTriangles(const DestructibleAsset* apexAsset, std::vector<std::vector<Nv::Blast::Triangle> >& chunkTriangles, int32_t posBufferIndex, float scale, PxVec3 offset ) +{ + + PxBounds3 bnd; + bnd.setEmpty(); + chunkTriangles.clear(); + chunkTriangles.resize(apexAsset->getChunkCount()); + for (uint32_t chunkIndex = 0; chunkIndex < apexAsset->getChunkCount(); ++chunkIndex) + { + uint32_t part = apexAsset->getPartIndex(chunkIndex); + const RenderMeshAsset* rAsset = apexAsset->getRenderMeshAsset(); + uint32_t submeshCount = rAsset->getSubmeshCount(); + for (uint32_t submeshIndex = 0; submeshIndex < submeshCount; ++submeshIndex) + { + const RenderSubmesh& currentSubmesh = rAsset->getSubmesh(submeshIndex); + const uint32_t* indexArray = currentSubmesh.getIndexBuffer(part); + uint32_t indexCount = currentSubmesh.getIndexCount(part); + const PxVec3* positions = reinterpret_cast<const PxVec3*>(currentSubmesh.getVertexBuffer().getBuffer(posBufferIndex)); + + for (uint32_t i = 0; i < indexCount; i += 3) + { + Vertex a; + Vertex b; + Vertex c; + bnd.include(positions[indexArray[i]]); + bnd.include(positions[indexArray[i + 1]]); + bnd.include(positions[indexArray[i + 2]]); + + a.p = positions[indexArray[i]] - offset; + b.p = positions[indexArray[i + 1]] - offset; + c.p = positions[indexArray[i + 2]] - offset; + a.p *= scale; + b.p *= scale; + c.p *= scale; + chunkTriangles[chunkIndex].push_back(Nv::Blast::Triangle(a, b, c)); + } + } + } + return bnd; +} + +bool ApexImportTool::importApexAsset(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondDescriptors, std::vector<uint32_t>& apexChunkFlags) +{ + ApexImporterConfig configDesc; + configDesc.setDefaults(); + return importApexAsset(chunkReorderInvMap, apexAsset, chunkDescriptors, bondDescriptors, apexChunkFlags, configDesc); +} + + +bool ApexImportTool::importApexAsset(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondDescriptors, std::vector<uint32_t>& apexChunkFlags, const ApexImporterConfig& configDesc) +{ + return importApexAssetInternal(chunkReorderInvMap, apexAsset, chunkDescriptors, bondDescriptors, apexChunkFlags, configDesc); +} + + +bool ApexImportTool::importApexAssetInternal(std::vector<uint32_t>& chunkReorderInvMap, const nvidia::apex::DestructibleAsset* apexAsset, + std::vector<NvBlastChunkDesc>& chunkDescriptors, std::vector<NvBlastBondDesc>& bondsDescriptors, std::vector<uint32_t>& apexChunkFlags, const ApexImporterConfig& configDesc) +{ + + if (!apexAsset) + { + if (m_log != NULL) + { + m_log(NvBlastMessage::Error, "Error: attempting to import NULL Apex asset.\n", __FILE__, __LINE__); + } + return false; + } + + // Build chunk descriptors for asset // + const uint32_t apexChunkCount = apexAsset->getChunkCount(); + chunkDescriptors.clear(); + chunkDescriptors.resize(apexChunkCount); + uint32_t rootChunkIndex = 0; + + for (uint32_t i = 0; i < apexChunkCount; ++i) + { + // Use bounds center for centroid + const PxBounds3 bounds = apexAsset->getChunkActorLocalBounds(i); + const PxVec3 center = bounds.getCenter(); + memcpy(chunkDescriptors[i].centroid, ¢er.x, 3 * sizeof(float)); + + // Find chunk volume + uint32_t partIndex = apexAsset->getPartIndex(i); + uint32_t partConvexHullCount = apexAsset->getPartConvexHullCount(partIndex); + NvParameterized::Interface** cxInterfaceArray = apexAsset->getPartConvexHullArray(partIndex); + chunkDescriptors[i].volume = 0.0f; + for (uint32_t hull = 0; hull < partConvexHullCount; ++hull) + { + NvParameterized::Handle paramHandle(cxInterfaceArray[hull]); + float hullVolume; + paramHandle.getParameter("volume"); + paramHandle.getParamF32(hullVolume); + chunkDescriptors[i].volume += hullVolume; + } + + int32_t parent = apexAsset->getChunkParentIndex(i); + if (parent == -1) + { + rootChunkIndex = i; + chunkDescriptors[i].parentChunkIndex = UINT32_MAX; + } + else + { + chunkDescriptors[i].parentChunkIndex = parent; + } + + chunkDescriptors[i].flags = 0; + chunkDescriptors[i].userData = i; + } + // Get support graph data from Apex asset // + + const NvParameterized::Interface* assetParameterized = apexAsset->getAssetNvParameterized(); + uint32_t maximumSupportDepth = 0; + + NvParameterized::Handle parameterHandle(*assetParameterized); + parameterHandle.getParameter("supportDepth"); + parameterHandle.getParamU32(maximumSupportDepth); + std::vector<std::pair<uint32_t, uint32_t> > overlapsBuffer; + uint32_t overlapsCount = apexAsset->getCachedOverlapCountAtDepth(maximumSupportDepth); + if (overlapsCount != 0) + { + const IntPair* overlap = reinterpret_cast<const IntPair*>(apexAsset->getCachedOverlapsAtDepth(maximumSupportDepth)); + for (uint32_t i = 0; i < overlapsCount; ++i) + { + chunkDescriptors[overlap[i].i0].flags = NvBlastChunkDesc::SupportFlag; + chunkDescriptors[overlap[i].i1].flags = NvBlastChunkDesc::SupportFlag; + overlapsBuffer.push_back(std::make_pair(overlap[i].i0, overlap[i].i1)); + } + } + + // Format all connections as (chunk with lower index) -> (chunk with higher index) // + + for (uint32_t i = 0; i < overlapsBuffer.size(); ++i) + { + if (overlapsBuffer[i].first > overlapsBuffer[i].second) + { + std::swap(overlapsBuffer[i].first, overlapsBuffer[i].second); + } + } + + // Unique all connections // + std::sort(overlapsBuffer.begin(), overlapsBuffer.end()); + overlapsBuffer.resize(std::unique(overlapsBuffer.begin(), overlapsBuffer.end()) - overlapsBuffer.begin()); + + // Build bond descriptors (acquire area, normal, centroid) + bondsDescriptors.clear(); + bondsDescriptors.resize(overlapsBuffer.size()); + + Nv::Blast::BlastBondGenerator bondGenTool(GetApexSDK()->getCookingInterface(), &GetApexSDK()->getPhysXSDK()->getPhysicsInsertionCallback()); + std::vector<std::vector<Nv::Blast::Triangle> > chunkTriangles; + + PxBounds3 bnds = apexAsset->getRenderMeshAsset()->getBounds(); + PxVec3 offset = bnds.getCenter(); + float scale = 1.0f / PxMax(PxAbs(bnds.getExtents(0)), PxMax(PxAbs(bnds.getExtents(1)), PxAbs(bnds.getExtents(2)))); + + bnds = gatherChunkTriangles(apexAsset, chunkTriangles, 0, scale, offset); + + + BondGenerationConfig cf; + cf.bondMode = BondGenerationConfig::AVERAGE; + if (configDesc.infSearchMode == configDesc.EXACT) + { + cf.bondMode = BondGenerationConfig::EXACT; + } + + bondGenTool.createBondBetweenMeshes(chunkTriangles, bondsDescriptors, overlapsBuffer, cf); + + + + float inverScale = 1.0f / scale; + + for (uint32_t i = 0; i < bondsDescriptors.size(); ++i) + { + bondsDescriptors[i].bond.area *= inverScale * inverScale; + bondsDescriptors[i].bond.centroid[0] *= inverScale; + bondsDescriptors[i].bond.centroid[1] *= inverScale; + bondsDescriptors[i].bond.centroid[2] *= inverScale; + + bondsDescriptors[i].bond.centroid[0] += offset.x; + bondsDescriptors[i].bond.centroid[1] += offset.y; + bondsDescriptors[i].bond.centroid[2] += offset.z; + + } + + /// Delete all bonds with zero area /// + for (uint32_t i = 0; i < bondsDescriptors.size(); ++i) + { + if (bondsDescriptors[i].bond.area == 0) + { + bondsDescriptors[i].chunkIndices[0] = bondsDescriptors.back().chunkIndices[0]; + bondsDescriptors[i].chunkIndices[1] = bondsDescriptors.back().chunkIndices[1]; + bondsDescriptors[i].bond = bondsDescriptors.back().bond; + bondsDescriptors.pop_back(); + --i; + } + } + + + + apexChunkFlags.clear(); + apexChunkFlags.resize(chunkDescriptors.size()); + // special 'earth chunk' + { + uint32_t earthChunkIndex = (uint32_t)chunkDescriptors.size(); + NvBlastChunkDesc earthChunk; + memset(earthChunk.centroid, 0, 3 * sizeof(float)); + earthChunk.volume = 0.0f; + earthChunk.parentChunkIndex = rootChunkIndex; + earthChunk.flags = NvBlastChunkDesc::SupportFlag; + earthChunk.userData = earthChunkIndex; + uint32_t chunksConnectedToEarth = 0; + for (uint32_t i = 0; i < chunkDescriptors.size(); i++) + { + uint32_t chunkID = i; + const NvParameterized::Interface* assetInterface = apexAsset->getAssetNvParameterized(); + NvParameterized::Handle chunksHandle(*assetInterface, "chunks"); + chunksHandle.set(chunkID); + NvParameterized::Handle flagsHandle(*assetInterface); + chunksHandle.getChildHandle(assetInterface, "flags", flagsHandle); + uint32_t flags; + flagsHandle.getParamU32(flags); + + apexChunkFlags[chunkID] = flags; + + // world support flag + if (flags & (1 << 0)) + { + NvBlastBondDesc bond; + bond.chunkIndices[0] = i; + bond.chunkIndices[1] = earthChunkIndex; + bond.bond.area = 0.1f; // ??? + PxVec3 center = apexAsset->getChunkActorLocalBounds(chunkID).getCenter(); + memcpy(&bond.bond.centroid, ¢er.x, sizeof(PxVec3)); + PxVec3 normal = PxVec3(0, 0, 1); + memcpy(&bond.bond.normal, &normal.x, sizeof(PxVec3)); + bondsDescriptors.push_back(bond); + chunksConnectedToEarth++; + } + } + if (chunksConnectedToEarth > 0) + { + chunkDescriptors.push_back(earthChunk); + } + } + + const uint32_t chunkCount = static_cast<uint32_t>(chunkDescriptors.size()); + const uint32_t bondCount = static_cast<uint32_t>(bondsDescriptors.size()); + std::vector<uint32_t> chunkReorderMap(chunkCount); + std::vector<NvBlastChunkDesc> scratch(chunkCount); + NvBlastEnsureAssetExactSupportCoverage(chunkDescriptors.data(), chunkCount, scratch.data(), m_log); + NvBlastBuildAssetDescChunkReorderMap(chunkReorderMap.data(), chunkDescriptors.data(), chunkCount, scratch.data(), m_log); + NvBlastApplyAssetDescChunkReorderMapInplace(chunkDescriptors.data(), chunkCount, bondsDescriptors.data(), bondCount, chunkReorderMap.data(), scratch.data(), m_log); + chunkReorderInvMap.resize(chunkReorderMap.size()); + Nv::Blast::invertMap(chunkReorderInvMap.data(), chunkReorderMap.data(), static_cast<uint32_t>(chunkReorderMap.size())); + return true; +} + + +bool ApexImportTool::saveAsset(const NvBlastAsset* asset, PxFileBuf* stream) +{ + if (!asset) + { + if (m_log != NULL) + { + m_log(NvBlastMessage::Error, "Error: attempting to serialize NULL asset.\n", __FILE__, __LINE__); + } + return false; + } + if (!stream) + { + if (m_log != NULL) + { + m_log(NvBlastMessage::Error, "Error: bad output stream.\n", __FILE__, __LINE__); + } + return false; + } + const void* assetData = asset; + uint32_t assetDataSize = NvBlastAssetGetSize(asset, m_log); + stream->write(assetData, assetDataSize); + stream->close(); + if (m_log != NULL) + { + m_log(NvBlastMessage::Info, "Saving finished... \n", __FILE__, __LINE__); + } + return true; +} + + +ApexImportTool::~ApexImportTool() +{ + delete m_apexDestruction; +} + +} // namespace ApexImporter + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.cpp b/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.cpp new file mode 100644 index 0000000..7cf7492 --- /dev/null +++ b/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.cpp @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtScopedResource.h" + +#include <ApexSDK.h> +#include <RenderMeshAsset.h> +#include <DestructibleAsset.h> +#include <ModuleDestructible.h> + + +namespace Nv +{ +namespace Blast +{ + +namespace ApexImporter +{ + +void ApexReleaser::release(nvidia::apex::RenderMeshAssetAuthoring& a) +{ + if (mApex) + mApex->releaseAssetAuthoring(a); +} + + +void ApexReleaser::release(nvidia::apex::DestructibleAssetAuthoring& a) +{ + if (mApex) + mApex->releaseAssetAuthoring(a); +} + + +void ApexReleaser::release(nvidia::apex::ModuleDestructible& a) +{ + if (mApex) + mApex->releaseModule(&a); +} + +} // namespace ApexImporter + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.h b/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.h new file mode 100644 index 0000000..e0d35b7 --- /dev/null +++ b/NvBlast/sdk/extensions/import/source/NvBlastExtScopedResource.h @@ -0,0 +1,160 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTSCOPEDRESOURCE_H +#define NVBLASTEXTSCOPEDRESOURCE_H + +#include <PxAssert.h> + + +#pragma warning(push) +#pragma warning(disable:4512) + +namespace nvidia +{ +namespace apex +{ +class ApexSDK; +class Asset; +class DestructibleAsset; +class AssetAuthoring; +class ModuleDestructible; +class RenderMeshAssetAuthoring; +class DestructibleAssetAuthoring; +} +} + +namespace Nv +{ +namespace Blast +{ + +namespace ApexImporter +{ + +template <class T> +class DefaultReleaser +{ +public: + DefaultReleaser() { } + PX_INLINE void release(T& t) { t.release(); } +}; + +class ApexReleaser +{ +public: + ApexReleaser() : mApex(nullptr) { } + ApexReleaser(nvidia::apex::ApexSDK& apex) : mApex(&apex) { } + + void release(nvidia::apex::RenderMeshAssetAuthoring&); + void release(nvidia::apex::DestructibleAssetAuthoring&); + void release(nvidia::apex::ModuleDestructible&); + +protected: + bool mbValid; + nvidia::apex::ApexSDK* mApex; +}; + +template< class Releasable, class Releaser = DefaultReleaser<Releasable> > +class ScopedResource +{ +public: + ScopedResource() + : mpReleasable(nullptr), mIsReleasable(true){} + + ScopedResource(Releasable* pReleasable, const Releaser& releaser) + : mpReleasable(pReleasable), + mReleaser(releaser), mIsReleasable(true) { } + + ScopedResource(Releasable* pReleasable, bool isReleasable = true) + : mpReleasable(pReleasable), mIsReleasable(isReleasable) { } + + ~ScopedResource() + { + destroy(); + } + + PX_INLINE operator bool() const + { + return (nullptr != mpReleasable); + } + + PX_INLINE Releasable* get() const + { + return mpReleasable; + } + + PX_INLINE Releasable* release() + { + Releasable* pReleasable = mpReleasable; + mpReleasable = nullptr; + return pReleasable; + } + + PX_INLINE Releasable& operator* () const + { + PX_ASSERT(*this); + return *mpReleasable; + } + + PX_INLINE Releasable* operator-> () const + { + return mpReleasable; + } + + PX_INLINE bool operator==(const ScopedResource& b) const + { + return mpReleasable == b.mpReleasable; + } + + PX_INLINE bool operator!=(const ScopedResource& b) const + { + return !(*this == b); + } + + PX_INLINE void reset(Releasable* pReleasable, bool isReleasable = true) + { + if (mpReleasable == pReleasable) return; + destroy(); + mpReleasable = pReleasable; + mIsReleasable = isReleasable; + } + + PX_INLINE void reset(Releasable* pReleasable, const Releaser& releaser, bool isReleasable = true) + { + reset(pReleasable); + mReleaser = releaser; + mIsReleasable = isReleasable; + } + +private: + + void destroy() + { + if (*this && mIsReleasable) + mReleaser.release(*mpReleasable); + } + + ScopedResource(const ScopedResource&); + ScopedResource& operator=(const ScopedResource&); + + Releasable* mpReleasable; + Releaser mReleaser; + bool mIsReleasable; +}; + +} // namespace ApexImporter + +} // namespace Blast +} // namespace Nv + +#pragma warning(pop) + +#endif // NVBLASTEXTSCOPEDRESOURCE_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtImpactDamageManager.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtImpactDamageManager.h new file mode 100644 index 0000000..ac3576d --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtImpactDamageManager.h @@ -0,0 +1,142 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTIMPACTDAMAGEMANAGER_H +#define NVBLASTEXTIMPACTDAMAGEMANAGER_H + +#include "PxFiltering.h" +#include "NvPreprocessor.h" + +// Forward declarations +namespace physx +{ +struct PxContactPair; +struct PxContactPairHeader; +} + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxActor; +class ExtPxManager; + + +/** +Custom Damage Function +*/ +typedef bool(*ExtImpactDamageFunction)(void* data, ExtPxActor* actor, physx::PxShape* shape, physx::PxVec3 worldPos, physx::PxVec3 worldForce); + + +/** +Impact Damage Manager Settings. +*/ +struct ExtImpactSettings +{ + bool isSelfCollissionEnabled; //!< family's self collision enabled + float fragility; //!< global fragility factor + ExtImpactDamageFunction damageFunction; //!< custom damage function, can be nullptr, default internal one will be used in that case. + void* damageFunctionData; //!< data to be passed in custom damage function + + + ExtImpactSettings() : + isSelfCollissionEnabled(false), + fragility(1.0f), + damageFunction(nullptr) + {} +}; + + +/** +Impact Damage Manager. + +Requires ExtPxManager. +Call onContact from PxSimulationEventCallback onContact to accumulate damage. +Call applyDamage to apply accumulated damage. +*/ +class NV_DLL_EXPORT ExtImpactDamageManager +{ +public: + //////// manager creation //////// + + /** + Create a new ExtImpactDamageManager. + + \param[in] pxManager The ExtPxManager instance to be used by impact damage manager. + \param[in] settings The settings to be set on ExtImpactDamageManager. + + \return the new ExtImpactDamageManager if successful, NULL otherwise. + */ + static ExtImpactDamageManager* create(ExtPxManager* pxManager, ExtImpactSettings settings = ExtImpactSettings()); + + /** + Release this manager. + */ + virtual void release() = 0; + + + //////// interface //////// + + /** + Set ExtImpactDamageManager settings. + + \param[in] settings The settings to be set on ExtImpactDamageManager. + */ + virtual void setSettings(const ExtImpactSettings& settings) = 0; + + /** + This method is equal to PxSimulationEventCallback::onContact. + + User should implement own PxSimulationEventCallback onContact and call this method in order ExtImpactDamageManager to work correctly. + + Contacts will be processed and impact damage will be accumulated. + + \param[in] pairHeader Information on the two actors whose shapes triggered a contact report. + \param[in] pairs The contact pairs of two actors for which contact reports have been requested. @see PxContactPair. + \param[in] nbPairs The number of provided contact pairs. + + @see PxSimulationEventCallback + */ + virtual void onContact(const physx::PxContactPairHeader& pairHeader, const physx::PxContactPair* pairs, uint32_t nbPairs) = 0; + + + /** + Apply accumulated impact damage. + */ + virtual void applyDamage() = 0; + + + //////// filter shader //////// + + /** + Custom implementation of PxSimulationFilterShader, enables necessary information to be passed in onContact(). + Set it in your PxScene PxSceneDesc in order to impact damage to work correctly or implement your own. + + @see PxSimulationFilterShader + */ + static physx::PxFilterFlags FilterShader( + physx::PxFilterObjectAttributes attributes0, + physx::PxFilterData filterData0, + physx::PxFilterObjectAttributes attributes1, + physx::PxFilterData filterData1, + physx::PxPairFlags& pairFlags, + const void* constantBlock, + uint32_t constantBlockSize); + +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTIMPACTDAMAGEMANAGER_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPx.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPx.h new file mode 100644 index 0000000..b2d938b --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPx.h @@ -0,0 +1,29 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPX_H +#define NVBLASTEXTPX_H + + +/** +This is the main include header for the BlastExt Physics, for users who +want to use a single #include file. + +Alternatively, one can instead directly #include a subset of the below files. +*/ + +#include "NvBlastExtPxActor.h" +#include "NvBlastExtPxAsset.h" +#include "NvBlastExtPxFamily.h" +#include "NvBlastExtPxListener.h" +#include "NvBlastExtPxManager.h" + + +#endif // ifndef NVBLASTEXTPX_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPxActor.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxActor.h new file mode 100644 index 0000000..994ace7 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxActor.h @@ -0,0 +1,83 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXACTOR_H +#define NVBLASTEXTPXACTOR_H + +#include "NvBlastTypes.h" + + +// Forward declarations +namespace physx +{ + class PxRigidDynamic; +} + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxFamily; +class TkActor; + + +/** +Actor. + +Corresponds one to one to PxRigidDynamic and ExtActor. +*/ +class ExtPxActor +{ +public: + /** + Get the number of visible chunks for this actor. May be used in conjunction with getChunkIndices(). + + \return the number of visible chunk indices for the actor. + */ + virtual uint32_t getChunkCount() const = 0; + + /** + Access actor's array of chunk indices. Use getChunkCount() to get a size of this array. + + \return a pointer to an array of chunk indices of an actor. + */ + virtual const uint32_t* getChunkIndices() const = 0; + + /** + Every actor has corresponding PxActor. + + /return a pointer to PxRigidDynamic actor. + */ + virtual physx::PxRigidDynamic& getPhysXActor() const = 0; + + /** + Every actor has corresponding TkActor. + + /return a pointer to TkActor actor. + */ + virtual TkActor& getTkActor() const = 0; + + /** + Every actor has corresponding ExtPxFamily. + + /return a pointer to ExtPxFamily family. + */ + virtual ExtPxFamily& getFamily() const = 0; +}; + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXACTOR_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPxAsset.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxAsset.h new file mode 100644 index 0000000..a4dbe0e --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxAsset.h @@ -0,0 +1,201 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXASSET_H +#define NVBLASTEXTPXASSET_H + +#include "NvBlastTkFramework.h" +#include "PxConvexMeshGeometry.h" +#include "PxTransform.h" +#include "NvBlastPreprocessor.h" + + +// Forward declarations +namespace physx +{ +class PxCooking; + +namespace general_PxIOStream2 +{ +class PxFileBuf; +} +} + + +namespace Nv +{ +namespace Blast +{ + + +/** +Descriptor for PxAsset creation. + +PxAsset creates TkAsset internally, so TkAssetDesc must be filled. +In addition it needs physics chunks data. Every chunk can have any amount of Convexes (Subchunks). +*/ +struct ExtPxAssetDesc : public TkAssetDesc +{ + /** + Physics Subchunk. + + Represents convex and it's position. + */ + struct SubchunkDesc + { + physx::PxTransform transform; //!< convex local transform + physx::PxConvexMeshGeometry geometry; //!< convex geometry + }; + + /** + Physics Chunk. + + Contains any amount of subchunks. Empty subchunks array makes chunk invisible. + */ + struct ChunkDesc + { + SubchunkDesc* subchunks; //!< array of subchunks for chunk, can be empty + uint32_t subchunkCount; //!< size array of subchunks for chunk, can be 0 + bool isStatic; //!< is chunk static. Static chunk makes PxActor Kinematic. + }; + + ChunkDesc* pxChunks; //!< array of chunks in asset, should be of size chunkCount (@see NvBlastAssetDesc) +}; + + +/** +Physics Subchunk. + +Represents convex and it's local position. +*/ +struct ExtPxSubchunk +{ + physx::PxTransform transform; //!< convex local transform + physx::PxConvexMeshGeometry geometry; //!< convex geometry +}; + + +/** +Physics Chunk. + +Contains any amount of subchunks. +*/ +struct ExtPxChunk +{ + uint32_t firstSubchunkIndex; //!< first Subchunk index in Subchunk's array in ExtPhyicsAsset + uint32_t subchunkCount; //!< Subchunk count. Can be 0. + bool isStatic; //!< is chunk static (kinematic)?. +}; + + +/** +Asset. + +Keeps all the static data needed for physics. +*/ +class NV_DLL_EXPORT ExtPxAsset +{ +public: + + /** + Create a new ExtPxAsset. + + \param[in] desc The ExtPxAssetDesc descriptor to be used, @see ExtPxAssetDesc. + \param[in] framework The TkFramework instance to be used to create TkAsset. + + \return the new ExtPxAsset if successful, NULL otherwise. + */ + static ExtPxAsset* create(const ExtPxAssetDesc& desc, TkFramework& framework); + + + /* + Factory method for deserialization + + Doesn't specify chunks or subchunks as they'll be fed in during deserialization to avoid copying stuff around. + + */ + static ExtPxAsset* create(TkAsset* asset); + + + /** + Deserialize an ExtPxAsset object from the given stream. + + \param[in] stream User-defined stream object. + \param[in] framework The TkFramework instance to be used to deserialize TkAsset. + \param[in] physics The PxPhysics instance to be to deserialize PxConvexMesh(s). + + \return pointer the deserialized ExtPxAsset object if successful, or NULL if unsuccessful. + */ + static ExtPxAsset* deserialize(physx::general_PxIOStream2::PxFileBuf& stream, TkFramework& framework, physx::PxPhysics& physics); + + /** + Release this ExtPxAsset. + */ + virtual void release() = 0; + + /** + Write the asset's data to the user-defined PxFileBuf stream. Underlying TkAsset would be also serialized. + + \param[in] stream User-defined stream object. + \param[in] cooking The PxCooking instance to be used to serialize PxConvexMesh(s). + + \return true if serialization was successful, false otherwise. + */ + virtual bool serialize(physx::general_PxIOStream2::PxFileBuf& stream, physx::PxCooking& cooking) const = 0; + + /** + Every ExtPxAsset has corresponding TkAsset. + + /return a pointer to TkAsset actor. + */ + virtual const TkAsset& getTkAsset() const = 0; + + /** + Get the number of chunks for this asset. May be used in conjunction with getChunks(). + + \return the number of chunks for the asset. + */ + virtual uint32_t getChunkCount() const = 0; + + /** + Access asset's array of chunks. Use getChunkCount() to get the size of this array. + + \return a pointer to an array of chunk of an asset. + */ + virtual const ExtPxChunk* getChunks() const = 0; + + /** + Get the number of subchunks for this asset. May be used in conjunction with getSubchunks(). + Subchunk count is the maximum value of ExtPxChunk: (firstSubchunkIndex + subchunkCount). + + \return the number of subchunks for the asset. + */ + virtual uint32_t getSubchunkCount() const = 0; + + /** + Access asset's array of subchunks. Use getSubchunkCount() to get the size of this array. + + \return a pointer to an array of subchunks of an asset. + */ + virtual const ExtPxSubchunk* getSubchunks() const = 0; + + /** + Pointer field available to the user. + */ + void* userData; +}; + + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXASSET_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPxFamily.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxFamily.h new file mode 100644 index 0000000..7805c15 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxFamily.h @@ -0,0 +1,223 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXFAMILY_H +#define NVBLASTEXTPXFAMILY_H + +#include "PxFiltering.h" + + +// Forward declarations +namespace physx +{ +class PxRigidDynamic; +class PxMaterial; +class PxScene; +class PxTransform; +} + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxActor; +class ExtPxAsset; +class ExtPxListener; +class TkFamily; + + +/** +PxShape Desc. + +Used to set settings for newly created PxShapes. + +@see PxShape +*/ +struct ExtPxShapeDescTemplate +{ + uint8_t flags; //!< PxShapeFlags flags + physx::PxFilterData simulationFilterData; //!< user definable collision filter data + physx::PxFilterData queryFilterData; //!< user definable query filter data. + float contactOffset; //!< contact offset + float restOffset; //!< rest offset +}; + + +/** +PxActor Desc. + +Used to set settings for newly created PxActors. +*/ +struct ExtPxActorDescTemplate +{ + uint8_t flags; //!< actor flags +}; + + +/** +Physics Spawn Settings. + +This Struct unifies setting to be used when PhysX actors are created. +*/ +struct ExtPxSpawnSettings +{ + physx::PxScene* scene; //!< PxScene for PxActors to be spawned + physx::PxMaterial* material; //!< default PxMaterial + float density; //!< default density for PhysX +}; + + +/** +PxFamily. + +A collection of actors. Maps 1 to 1 with TkFamily. +*/ +class ExtPxFamily +{ +public: + /** + Spawn ExtPxFamily. Can be called only once. Actual PhysX actors will created and placed in PxScene + + \param[in] pose World transform. + \param[in] scale Scale applied to spawned actors. + \param[in] settings Spawn settings. + + \return true if spawn was successful, false otherwise. + */ + virtual bool spawn(const physx::PxTransform& pose, const physx::PxVec3& scale, const ExtPxSpawnSettings& settings) = 0; + + + /** + Despawn this ExtPxFamily. This removes the PhysX actors from PxScene and deletes them, as well as + deleting the created ExtPxActors + + This does not call release() on the family. + + \returns true if successful. + */ + virtual bool despawn() = 0; + + + /** + The number of actors currently in this family. + + \return the number of ExtPxActor that currently exist in this family. + */ + virtual uint32_t getActorCount() const = 0; + + /** + Retrieve an array of pointers (into the user-supplied buffer) to actors. + + \param[out] buffer A user-supplied array of ExtPxActor pointers. + \param[in] bufferSize The number of elements available to write into buffer. + + \return the number of ExtPxActor pointers written to the buffer. + */ + virtual uint32_t getActors(ExtPxActor** buffer, uint32_t bufferSize) const = 0; + + /** + Every family has corresponding TkFamily. + + /return a pointer to TkFamily actor. + */ + virtual TkFamily& getTkFamily() const = 0; + + /** + Access an array of shapes of subchunks. The size of array is equal getPxAsset()->getSubchunkCount(). + For every corresponding subchunk it contains pointer to created PxShape or nullptr. + + \return the pointer to subchunk shapes array. + */ + virtual const physx::PxShape* const* getSubchunkShapes() const = 0; + + /** + Every family has an associated asset. + + \return a pointer to the (const) ExtPxAsset object. + */ + virtual const ExtPxAsset& getPxAsset() const = 0; + + /** + Set the default material to be used for PxRigidDynamic creation. + + \param[in] material The material to be the new default. + */ + virtual void setMaterial(physx::PxMaterial& material) = 0; + + /* + Set ExtPxPxShapeDesc to be used on all newly created PxShapes. + + NOTE: Using it will override marking LEAF_CHUNK in simulationFilterData.word3 now. + + \param[in] pxShapeDesc The PxShape desc to be the new default. Can be nullptr. + */ + virtual void setPxShapeDescTemplate(const ExtPxShapeDescTemplate* pxShapeDesc) = 0; + + /** + Get the default ExtPxPxShapeDesc to be used on all newly created PxShapes. + + \return a pointer to the default PxShape desc. Can be nullptr. + */ + virtual const ExtPxShapeDescTemplate* getPxShapeDescTemplate() const = 0; + + /* + Set ExtPxPxActorDesc to be used on all newly created PxActors. + + \param[in] pxActorDesc The PxActor desc to be the new default. Can be nullptr. + */ + virtual void setPxActorDesc(const ExtPxActorDescTemplate* pxActorDesc) = 0; + + /** + Get the default ExtPxPxActorDesc to be used on all newly created PxActors. + + \return a pointer to the default PxActor desc. Can be nullptr. + */ + virtual const ExtPxActorDescTemplate* getPxActorDesc() const = 0; + + /** + Add a user implementation of ExtPxListener to this family's list of listeners. + + \param[in] listener The event listener to add. + */ + virtual void subscribe(ExtPxListener& listener) = 0; + + /** + Remove a user implementation of ExtPxListener from this family's list of listeners. + + \param[in] listener The event listener to remove. + */ + virtual void unsubscribe(ExtPxListener& listener) = 0; + + /** + Call after split. + */ + virtual void postSplitUpdate() = 0; + + /** + Release this family. + */ + virtual void release() = 0; + + /** + UserData pointer. Free to be used by user in any way. + */ + void* userData; +}; + + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXFAMILY_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPxListener.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxListener.h new file mode 100644 index 0000000..4c43283 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxListener.h @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXLISTENER_H +#define NVBLASTEXTPXLISTENER_H + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxFamily; +class ExtPxActor; + + +/** +Physics Listener Interface. + +Actor create/destroy events listener. +*/ +class ExtPxListener +{ +public: + /** + Interface to be implemented by the user. Will be called when ExtPxFamily creates new actor. + + \param[in] family Corresponding ExtPxFamily with new actor. + \param[in] actor The new actor. + */ + virtual void onActorCreated(ExtPxFamily& family, ExtPxActor& actor) = 0; + + /** + Interface to be implemented by the user. Will be called when ExtPxFamily destroy an actor. + + \param[in] family Corresponding ExtPxFamily. + \param[in] actor The actor to be destroyed. + */ + virtual void onActorDestroyed(ExtPxFamily& family, ExtPxActor& actor) = 0; +}; + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXLISTENER_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtPxManager.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxManager.h new file mode 100644 index 0000000..9d73898 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtPxManager.h @@ -0,0 +1,245 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXMANAGER_H +#define NVBLASTEXTPXMANAGER_H + +#include "NvBlastTypes.h" +#include "PxConvexMeshGeometry.h" +#include "PxTransform.h" +#include "NvPreprocessor.h" + + +// Forward declarations +namespace physx +{ +class PxPhysics; +class PxRigidDynamic; +class PxJoint; + +namespace general_PxIOStream2 +{ +class PxFileBuf; +} +} + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxActor; +class ExtPxAsset; +class ExtPxFamily; +class ExtPxListener; +class TkFamily; +class TkFramework; +class TkGroup; +class TkJoint; + + +/** +Family Desc. + +Used to create Physics Family. +*/ +struct ExtPxFamilyDesc +{ + const ExtPxAsset* pxAsset; //!< px asset to create from, pointer will be stored in family. + NvBlastActorDesc actorDesc; //!< actor descriptor to be used when creating TkActor. + TkGroup* group; //!< if not nullptr, created TkActor will be placed in group +}; + + +/** +Function pointer for PxJoint creation. + +It will be called when new joints are being created. It should return valid PxJoint pointer or nullptr. +*/ +typedef physx::PxJoint*(*ExtPxCreateJointFunction)(ExtPxActor* actor0, const physx::PxTransform& localFrame0, ExtPxActor* actor1, const physx::PxTransform& localFrame1, physx::PxPhysics& physics, TkJoint& joint); + + +/** +Physics Manager. + +Used to create and manage Physics Families. +*/ +class NV_DLL_EXPORT ExtPxManager +{ +public: + //////// manager creation //////// + + /** + Create a new ExtPxManager. + + \param[in] physics The PxPhysics instance to be used by ExtPxManager. + \param[in] framework The TkFramework instance to be used by ExtPxManager. + \param[in] createFn The function to be used when creating joints, can be nullptr. + \param[in] useUserData Flag if ExtPxManager is allowed to override PxActor's userData, it will store pointer to PxActor there. + It is recommended as fastest way. If set to 'false' HashMap will be used. + + \return the new ExtPxManager if successful, NULL otherwise. + */ + static ExtPxManager* create(physx::PxPhysics& physics, TkFramework& framework, ExtPxCreateJointFunction createFn = nullptr, bool useUserData = true); + + /** + Release this manager. + */ + virtual void release() = 0; + + + //////// impact //////// + + /** + Simulation Filter data to be set on leaf chunk actors + */ + enum FilterDataAttributes + { + LEAF_CHUNK = 1, + }; + + + //////// interface //////// + + /** + Create a px family from the given descriptor. + + \param[in] desc The family descriptor (see ExtPxFamilyDesc). + + \return the created family, if the descriptor was valid and memory was available for the operation. Otherwise, returns NULL. + */ + virtual ExtPxFamily* createFamily(const ExtPxFamilyDesc& desc) = 0; + + /** + Create a px joint associated with TkJoint. + + ExtPxCreateJointFunction will be called after this call. + ExtPxCreateJointFunction must be set, nothing will happen otherwise. + + \param[in] joint TkJoint to be used to create px joint. + + \return true iff Joint was created. + */ + virtual bool createJoint(TkJoint& joint) = 0; + + /** + Destroy a px joint associated with TkJoint. + + \param[in] joint TkJoint to be used to destroy px joint. + */ + virtual void destroyJoint(TkJoint& joint) = 0; + + /** + Set ExtPxCreateJointFunction to be used when new joints are being created.\ + + \param[in] createFn Create function pointer to set, can be nullptr. + */ + virtual void setCreateJointFunction(ExtPxCreateJointFunction createFn) = 0; + + /** + The number of families currently in this manager. + + \return the number of ExtPxFamily that currently exist in this manger. + */ + virtual uint32_t getFamilyCount() const = 0; + + /** + Retrieve an array of pointers (into the user-supplied buffer) to families. + + \param[out] buffer A user-supplied array of ExtPxFamily pointers. + \param[in] bufferSize The number of elements available to write into buffer. + + \return the number of ExtPxFamily pointers written to the buffer. + */ + virtual uint32_t getFamilies(ExtPxFamily** buffer, uint32_t bufferSize) const = 0; + + /** + Look up an associated ExtPxFamily by TkFamily pointer. + + \param[in] family The TkFamily pointer to look up. + + \return pointer to the ExtPxFamily object if it exists, NULL otherwise. + */ + virtual ExtPxFamily* getFamilyFromTkFamily(TkFamily& family) const = 0; + + /** + Look up an associated ExtPxActor by PxRigidDynamic pointer. + + \param[in] pxActor The PxRigidDynamic pointer to look up. + + \return pointer to the ExtPxActor object if it exists, NULL otherwise. + */ + virtual ExtPxActor* getActorFromPhysXActor(const physx::PxRigidDynamic& pxActor) const = 0; + + /** + Get a PxPhysics object pointer used upon manager creation. + + \return a pointer to the (const) PxPhysics object. + */ + virtual physx::PxPhysics& getPhysics() const = 0; + + /** + Get a TkFramework object pointer used upon manager creation. + + \return a pointer to the TkFramework object. + */ + virtual TkFramework& getFramework() const = 0; + + /** + Get if useUserData was set upon manager creation. + + \return true iff PxActor userData is used by manager. + */ + virtual bool isPxUserDataUsed() const = 0; + + /** + Limits the total number of actors that can exist at a given time. A value of zero disables this (gives no limit). + + \param[in] limit If not zero, the maximum number of actors that will be allowed to exist. + */ + virtual void setActorCountLimit(uint32_t limit) = 0; + + /** + Retrieve the limit to the total number of actors that can exist at a given time. A value of zero disables this (gives no limit). + + \return the limit to the total number of actors that can exist at a given time (or zero if there is no limit). + */ + virtual uint32_t getActorCountLimit() = 0; + + /** + The total number of PxActors generated by Blast. + + \return the total number of PxActors generated by Blast. + */ + virtual uint32_t getPxActorCount() const = 0; + + /** + Add a user implementation of ExtPxListener to this family's list of listeners. + + \param[in] listener The event listener to add. + */ + virtual void subscribe(ExtPxListener& listener) = 0; + + /** + Remove a user implementation of ExtPxListener from this family's list of listeners. + + \param[in] listener The event listener to remove. + */ + virtual void unsubscribe(ExtPxListener& listener) = 0; +}; + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXMANAGER_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtStressSolver.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtStressSolver.h new file mode 100644 index 0000000..2fd389d --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtStressSolver.h @@ -0,0 +1,209 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTSTRESSSOLVER_H +#define NVBLASTEXTSTRESSSOLVER_H + +#include "common/PxRenderBuffer.h" +#include <vector> +#include "NvPreprocessor.h" + + +namespace Nv +{ +namespace Blast +{ + +// forward declarations +class ExtPxFamily; +class ExtPxActor; + +/** +Stress Solver Settings + +Stress on every bond is calculated as +stress = bond.linearStress * stressLinearFactor + bond.angularStress * stressAngularFactor +where: +bond.linearStress - is linear stress force on particular bond +bond.angularStress - is angular stress force on particular bond +stressLinearFactor, stressAngularFactor - are a multiplier parameter set by this struct + +Support graph reduction: +2 ^ reduction level = max node count to be aggregated during graph reduction, so 0 is 2 % 0 = 1, basically use support graph. +So N nodes graph will be simplified to contain ~ N / (2 ^ reduction level) +*/ +struct ExtStressSolverSettings +{ + float stressLinearFactor; //!< linear stress on bond multiplier + float stressAngularFactor; //!< angular stress on bond multiplier + uint32_t bondIterationsPerFrame; //!< number of bond iterations to perform per frame, @see getIterationsPerFrame() below + uint32_t graphReductionLevel; //!< graph reduction level + + ExtStressSolverSettings() : + stressLinearFactor(0.00004f), + stressAngularFactor(0.00007f), + bondIterationsPerFrame(18000), + graphReductionLevel(3) + {} +}; + + +/** +Stress Solver. + +Uses ExtPxFamily, allocates and prepares it's graph once when it's created. Then it's being quickly updated on every +actor split. +Works on both dynamic and static actor's within family. +For static actors it applies gravity. +For dynamic actors it applies centrifugal force. +Additionally applyImpulse() method can be used to apply external impulse (like impact damage). +*/ +class NV_DLL_EXPORT ExtStressSolver +{ +public: + //////// creation //////// + + /** + Create a new ExtStressSolver. + + \param[in] family The ExtPxFamily instance to calculate stress on. + \param[in] settings The settings to be set on ExtStressSolver. + + \return the new ExtStressSolver if successful, NULL otherwise. + */ + static ExtStressSolver* create(ExtPxFamily& family, ExtStressSolverSettings settings = ExtStressSolverSettings()); + + + //////// interface //////// + + /** + Release this stress solver. + */ + virtual void release() = 0; + + /** + Set stress solver settings. + Changing graph reduction level will lead to graph being rebuilt (which is fast, but still not recommended). + All other settings are applied instantly and can be changed every frame. + + \param[in] settings The settings to be set on ExtStressSolver. + */ + virtual void setSettings(const ExtStressSolverSettings& settings) = 0; + + /** + Get stress solver settings. + + \return the pointer to stress solver settings currently set. + */ + virtual const ExtStressSolverSettings& getSettings() const = 0; + + /** + Apply external impulse on particular actor of family + + \param[in] actor The ExtPxActor to apply impulse on. + \param[in] position Local position in actor's coordinates to apply impulse on. + \param[in] force Impulse to apply (kg * m / s). + */ + virtual void applyImpulse(ExtPxActor& actor, physx::PxVec3 position, physx::PxVec3 force) = 0; + + /** + Update stress solver. + + Calculate stress and optionally apply damage. + + \param[in] doDamage If 'true' damage will be applied after stress solver. + */ + virtual void update(bool doDamage = true) = 0; + + /** + Reset stress solver. + + Stress solver uses warm start internally, calling this function will flush all previous data calculated and also zeros frame count. + This function is to be used for debug purposes. + */ + virtual void reset() = 0; + + /** + Debug Render Mode + */ + enum DebugRenderMode + { + STRESS_GRAPH = 0, //!< render only stress graph + STRESS_GRAPH_NODES_IMPULSES = 1, //!< render stress graph + nodes impulses after solving stress + STRESS_GRAPH_BONDS_IMPULSES = 2 //!< render stress graph + bonds impulses after solving stress + }; + + /** + Fill debug render for passed array of support graph nodes. + + \param[in] nodes Node indices of support graph to debug render for. + \param[out] lines Lines array to fill. + \param[in] mode Debug render mode. + \param[in] scale Scale to be applied on impulses. + */ + virtual void fillDebugRender(const std::vector<uint32_t>& nodes, std::vector<physx::PxDebugLine>& lines, DebugRenderMode mode, float scale = 1.0f) = 0; + + /** + Get stress solver linear error. + + \return the total linear error of stress calculation. + */ + virtual float getStressErrorLinear() const = 0; + + /** + Get stress solver angular error. + + \return the total angular error of stress calculation. + */ + virtual float getStressErrorAngular() const = 0; + + /** + Get stress solver total iterations count since it was created (or reset). + + \return the iterations count. + */ + virtual uint32_t getIterationCount() const = 0; + + /** + Get stress solver total frames count (update() calls) since it was created (or reset). + + \return the frames count. + */ + virtual uint32_t getFrameCount() const = 0; + + /** + Get stress solver bonds count, after graph reduction was applied. + + \return the bonds count. + */ + virtual uint32_t getBondCount() const = 0; + + + //////// helpers //////// + + /** + Get iteration per frame (update() call). + + Helper method to know how many solver iterations are made per frame. + + \return the iterations per frame count. + */ + uint32_t getIterationsPerFrame() const + { + uint32_t perFrame = getSettings().bondIterationsPerFrame / (getBondCount() + 1); + return perFrame > 0 ? perFrame : 1; + } +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTSTRESSSOLVER_H diff --git a/NvBlast/sdk/extensions/physx/include/NvBlastExtSync.h b/NvBlast/sdk/extensions/physx/include/NvBlastExtSync.h new file mode 100644 index 0000000..805378a --- /dev/null +++ b/NvBlast/sdk/extensions/physx/include/NvBlastExtSync.h @@ -0,0 +1,213 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTSYNC_H +#define NVBLASTEXTSYNC_H + +#include "NvBlastTk.h" +#include "foundation/PxTransform.h" +#include "foundation/PxAllocatorCallback.h" +#include "NvPreprocessor.h" + + +namespace Nv +{ +namespace Blast +{ + +class ExtPxFamily; +class ExtPxManager; + + +/** +Sync Event types +*/ +struct ExtSyncEventType +{ + enum Enum + { + Fracture = 0, //!< Contains Fracture commands + FamilySync, //!< Contains full family Family blob + Physics, //!< Contains actor's physical info, like transforms + + Count + }; +}; + + +/** +Generic Sync Event +*/ +struct NV_DLL_EXPORT ExtSyncEvent +{ + ExtSyncEvent(ExtSyncEventType::Enum t) : type(t) {} + virtual ~ExtSyncEvent() {} + + template<class T> + const T* getEvent() const { return reinterpret_cast<const T*>(this); } + + /** + Any Event can be copied (cloned). + + \return the pointer to the new copy of event. + */ + virtual ExtSyncEvent* clone() const = 0; + + void release(); + + ExtSyncEventType::Enum type; //!< Event type + uint64_t timestamp; //!< Event timestamp + NvBlastID familyID; //!< TkFamily ID +}; + + +/** +Generic CRTP for Sync Events +*/ +template <class T, ExtSyncEventType::Enum eventType> +struct ExtSyncEventInstance : public ExtSyncEvent +{ + ExtSyncEventInstance() : ExtSyncEvent(eventType) {} + + static const ExtSyncEventType::Enum EVENT_TYPE = eventType; + + ExtSyncEvent* clone() const override + { + return new (NvBlastTkFrameworkGet()->getAllocatorCallback().allocate(sizeof(T), nullptr, __FILE__, __LINE__)) T(*(T*)this); + } +}; + + +/** +Fracture Sync Event +*/ +struct ExtSyncEventFracture : public ExtSyncEventInstance<ExtSyncEventFracture, ExtSyncEventType::Fracture> +{ + std::vector<NvBlastBondFractureData> bondFractures; //!< bond fracture data + std::vector<NvBlastChunkFractureData> chunkFractures; //!< chunk fracture data +}; + + +/** +Family Sync Event +*/ +struct ExtSyncEventFamilySync : public ExtSyncEventInstance<ExtSyncEventFamilySync, ExtSyncEventType::FamilySync> +{ + std::vector<char> family; //!< family binary blob +}; + + +/** +Physics Sync Event +*/ +struct ExtSyncEventPhysicsSync : public ExtSyncEventInstance<ExtSyncEventPhysicsSync, ExtSyncEventType::Physics> +{ + struct ActorData + { + uint32_t actorIndex; //!< actor index in family + physx::PxTransform transform; //!< actor world transform + }; + + std::vector<ActorData> data; //!< actors data +}; + + +/** +Sync Manager. + +Implements TkEventListener interface. It can be directly subscribed to listen for family events. +*/ +class NV_DLL_EXPORT ExtSync : public TkEventListener +{ +public: + //////// creation //////// + + /** + Create a new ExtSync. + + \return the new ExtSync if successful, NULL otherwise. + */ + static ExtSync* create(); + + + //////// common interface //////// + + /** + Release Sync manager. + */ + virtual void release() = 0; + + + //////// server-side interface //////// + + /** + TkEventListener interface. + + \param[in] events The array of events being dispatched. + \param[in] eventCount The number of events in the array. + */ + virtual void receive(const TkEvent* events, uint32_t eventCount) = 0; + + /** + Sync family state. Writes to internal sync buffer. + + \param[in] family The TkFamily to sync + */ + virtual void syncFamily(const TkFamily& family) = 0; + + /** + Sync PxFamily state. Writes to internal sync buffer. + + \param[in] family The ExtPxFamily to sync + */ + virtual void syncFamily(const ExtPxFamily& family) = 0; + + /** + The size of internal sync buffer (events count). + + \return the number of events in internal sync buffer. + */ + virtual uint32_t getSyncBufferSize() const = 0; + + /** + Acquire internal sync buffer. + + \param[in] buffer Reference to sync event buffer pointer to be set. + \param[in] size Reference to the size of the buffer array to be set. + */ + virtual void acquireSyncBuffer(const ExtSyncEvent*const*& buffer, uint32_t& size) const = 0; + + /** + Clear internal sync buffer. + */ + virtual void releaseSyncBuffer() = 0; + + + //////// client-side interface //////// + + /** + Apply external sync buffer on TkFramework and possibly ExtPxManager. This function call will result in + respective families/actors changes in order to synchronize state. + + \param[in] framework The TkFramework instance to be used. + \param[in] buffer Sync buffer array pointer. + \param[in] size Sync buffer array size. + \param[in] groupForNewActors TkGroup to be used for newly created actors. Can be nullptr. + \param[in] manager The ExtPxManager instance to be used. Can be nullptr, physics sync events will be ignored in that case. + */ + virtual void applySyncBuffer(TkFramework& framework, const ExtSyncEvent** buffer, uint32_t size, TkGroup* groupForNewActors, ExtPxManager* manager = nullptr) = 0; + +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTSYNC_H diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpactDamageManager.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpactDamageManager.cpp new file mode 100644 index 0000000..54d2696 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpactDamageManager.cpp @@ -0,0 +1,448 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtImpactDamageManager.h" +#include "NvBlastExtPxManager.h" +#include "NvBlastExtPxFamily.h" +#include "NvBlastExtPxActor.h" +#include "NvBlastExtPxListener.h" + +#include "NvBlastAssert.h" + +#include "NvBlastExtDamageShaders.h" +#include "NvBlastExtArray.h" +#include "NvBlastExtDefs.h" + +#include "PxRigidDynamic.h" +#include "PxSimulationEventCallback.h" +#include "PxRigidBodyExt.h" + +#include "NvBlastTkFramework.h" +#include "NvBlastTkActor.h" +#include "NvBlastTkFamily.h" +#include "NvBlastTkAsset.h" + + +namespace Nv +{ +namespace Blast +{ + +using namespace physx; + +const float MIN_IMPACT_VELOCITY_SQUARED = 1.0f; + + +class ExtImpactDamageManagerImpl final : public ExtImpactDamageManager +{ +public: + ExtImpactDamageManagerImpl(ExtPxManager* pxManager, ExtImpactSettings settings) + : m_pxManager(pxManager), m_settings(settings), m_listener(this), m_usePxUserData(m_pxManager->isPxUserDataUsed()) + { + NVBLAST_ASSERT_WITH_MESSAGE(pxManager != nullptr, "ExtImpactDamageManager creation: input ExtPxManager is nullptr."); + m_pxManager->subscribe(m_listener); + + m_impactDamageBuffer.reserve(32); + } + + ~ExtImpactDamageManagerImpl() + { + m_pxManager->unsubscribe(m_listener); + } + + virtual void release() override + { + NVBLASTEXT_DELETE(this, ExtImpactDamageManagerImpl); + } + + + //////// interface //////// + + virtual void setSettings(const ExtImpactSettings& settings) override + { + m_settings = settings; + } + + virtual void onContact(const PxContactPairHeader& pairHeader, const PxContactPair* pairs, uint32_t nbPairs) override; + + virtual void applyDamage() override; + + + //////// public methods //////// + + void queueImpactDamage(ExtPxActor* actor, PxVec3 force, PxVec3 position, PxShape* shape) + { + ImpactDamageData data = { actor, force, position, shape }; + m_impactDamageBuffer.pushBack(data); + } + + +private: + //////// physics manager listener //////// + + class PxManagerListener : public ExtPxListener + { + public: + PxManagerListener(ExtImpactDamageManagerImpl* manager) : m_manager(manager) {} + + virtual void onActorCreated(ExtPxFamily&, ExtPxActor&) override {} + virtual void onActorDestroyed(ExtPxFamily& family, ExtPxActor& actor) override + { + NV_UNUSED(family); + + // filter out actor from queued buffer + auto& buffer = m_manager->m_impactDamageBuffer; + for (int32_t i = 0; i < (int32_t)buffer.size(); ++i) + { + if (buffer[i].actor == &actor) + { + buffer.replaceWithLast(i); + i--; + } + } + } + private: + ExtImpactDamageManagerImpl* m_manager; + }; + + + //////// private methods //////// + + void ensureBuffersSize(ExtPxActor* actor); + void damageActor(ExtPxActor* actor, PxShape* shape, PxVec3 position, PxVec3 force); + + + //////// data //////// + + ExtPxManager* m_pxManager; + ExtImpactSettings m_settings; + PxManagerListener m_listener; + ExtArray<PxContactPairPoint>::type m_pairPointBuffer; + bool m_usePxUserData; + + struct ImpactDamageData + { + ExtPxActor* actor; + PxVec3 force; + PxVec3 position; + PxShape* shape; + }; + + ExtArray<ImpactDamageData>::type m_impactDamageBuffer; + + NvBlastFractureBuffers m_fractureBuffers; + ExtArray<uint8_t>::type m_fractureData; +}; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtImpactDamageManagerImpl +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +ExtImpactDamageManager* ExtImpactDamageManager::create(ExtPxManager* pxManager, ExtImpactSettings settings) +{ + return NVBLASTEXT_NEW(ExtImpactDamageManagerImpl) (pxManager, settings); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// onContact callback call +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void ExtImpactDamageManagerImpl::onContact(const PxContactPairHeader& pairHeader, const PxContactPair* pairs, uint32_t nbPairs) +{ + if (pairHeader.flags & physx::PxContactPairHeaderFlag::eREMOVED_ACTOR_0 || + pairHeader.flags & physx::PxContactPairHeaderFlag::eREMOVED_ACTOR_1 || + pairHeader.actors[0] == nullptr || + pairHeader.actors[1] == nullptr) + { + return; + } + + PxRigidActor* rigidActor0 = pairHeader.actors[0]; + PxRigidActor* rigidActor1 = pairHeader.actors[1]; + + ExtPxActor* actors[2]; + + if (m_usePxUserData) + { + actors[0] = (ExtPxActor*)rigidActor0->userData; + actors[1] = (ExtPxActor*)rigidActor1->userData; + } + else + { + PxRigidDynamic* rigidDynamic0 = rigidActor0->is<PxRigidDynamic>(); + PxRigidDynamic* rigidDynamic1 = rigidActor1->is<PxRigidDynamic>(); + actors[0] = rigidDynamic0 ? m_pxManager->getActorFromPhysXActor(*rigidDynamic0) : nullptr; + actors[1] = rigidDynamic1 ? m_pxManager->getActorFromPhysXActor(*rigidDynamic1) : nullptr; + } + + + // check one of them is blast actor + if (actors[0] == nullptr && actors[1] == nullptr) + { + return; + } + + // self-collision check + if (actors[0] != nullptr && actors[1] != nullptr) + { + if (&actors[0]->getFamily() == &actors[1]->getFamily() && !m_settings.isSelfCollissionEnabled) + return; + } + + for (uint32_t pairIdx = 0; pairIdx < nbPairs; pairIdx++) + { + const PxContactPair& currentPair = pairs[pairIdx]; + + if (currentPair.flags & physx::PxContactPairFlag::eREMOVED_SHAPE_0 || + currentPair.flags & physx::PxContactPairFlag::eREMOVED_SHAPE_1 || + currentPair.shapes[0] == nullptr || + currentPair.shapes[1] == nullptr) + { + continue; + } + + float masses[2] = { 0, 0 }; + { + for (int i = 0; i < 2; ++i) + { + PxRigidDynamic* rigidDynamic = pairHeader.actors[i]->is<physx::PxRigidDynamic>(); + if (rigidDynamic) + { + if (!(rigidDynamic->getRigidBodyFlags() & physx::PxRigidBodyFlag::eKINEMATIC)) + { + masses[i] = rigidDynamic->getMass(); + } + } + } + }; + + float reducedMass; + if (masses[0] == 0.0f) + { + reducedMass = masses[1]; + } + else if (masses[1] == 0.0f) + { + reducedMass = masses[0]; + } + else + { + reducedMass = masses[0] * masses[1] / (masses[0] + masses[1]); + } + + + PxVec3 destructibleForces[2] = { PxVec3(0.0f), PxVec3(0.0f) }; + PxVec3 avgContactPosition = PxVec3(0.0f); + PxVec3 avgContactNormal = PxVec3(0.0f); + uint32_t numContacts = 0; + + m_pairPointBuffer.resize(currentPair.contactCount); + uint32_t numContactsInStream = currentPair.contactCount > 0 ? currentPair.extractContacts(m_pairPointBuffer.begin(), currentPair.contactCount) : 0; + + for (uint32_t contactIdx = 0; contactIdx < numContactsInStream; contactIdx++) + { + PxContactPairPoint& currentPoint = m_pairPointBuffer[contactIdx]; + + const PxVec3& patchNormal = currentPoint.normal; + const PxVec3& position = currentPoint.position; + PxVec3 velocities[2] = { PxVec3(0.0f), PxVec3(0.0f) }; + for (int i = 0; i < 2; ++i) + { + PxRigidBody* rigidBody = pairHeader.actors[i]->is<physx::PxRigidBody>(); + if (rigidBody) + { + velocities[i] = physx::PxRigidBodyExt::getVelocityAtPos(*rigidBody, position); + } + } + + const PxVec3 velocityDelta = velocities[0] - velocities[1]; + if (velocityDelta.magnitudeSquared() >= MIN_IMPACT_VELOCITY_SQUARED || reducedMass == 0.0f) // If reduced mass == 0, this is kineamtic vs. kinematic. Generate damage. + { + for (int i = 0; i < 2; ++i) + { + if (actors[i]) + { + // this is not really physically correct, but at least its deterministic... + destructibleForces[i] += (patchNormal * patchNormal.dot(velocityDelta)) * reducedMass * (i ? 1.0f : -1.0f); + } + } + avgContactPosition += position; + avgContactNormal += patchNormal; + numContacts++; + } + } + + if (numContacts) + { + avgContactPosition /= (float)numContacts; + avgContactNormal.normalize(); + for (uint32_t i = 0; i < 2; i++) + { + const PxVec3 force = destructibleForces[i] / (float)numContacts; + ExtPxActor* actor = actors[i]; + if (actor != nullptr) + { + if (!force.isZero()) + { + queueImpactDamage(actor, force, avgContactPosition, currentPair.shapes[i]); + } + else if (reducedMass == 0.0f) // Handle kinematic vs. kinematic + { + // holy molly + } + } + } + } + } +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtImpactDamageManager damage processing +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +float clampedLerp(float from, float to, float t) +{ + t = PxClamp(t, 0.0f, 1.0f); + return (1 - t) * from + to * t; +} + +void ExtImpactDamageManagerImpl::applyDamage() +{ + const auto damageFn = m_settings.damageFunction; + const auto damageFnData = m_settings.damageFunctionData; + + for (const ImpactDamageData& data : m_impactDamageBuffer) + { + float forceMag = data.force.magnitude(); + float acceleration = forceMag / data.actor->getPhysXActor().getMass(); + float factor = acceleration * m_settings.fragility * 0.001f; + if (factor > 0.05f) + { + PxTransform t(data.actor->getPhysXActor().getGlobalPose().getInverse()); + PxVec3 force = t.rotate(data.force); + PxVec3 position = t.transform(data.position); + + if (!damageFn || !damageFn(damageFnData, data.actor, data.shape, position, force)) + { + damageActor(data.actor, data.shape, position, force*.00001f); + } + } + } + m_impactDamageBuffer.clear(); +} + +void ExtImpactDamageManagerImpl::ensureBuffersSize(ExtPxActor* actor) +{ + const TkAsset* tkAsset = actor->getTkActor().getAsset(); + const uint32_t chunkCount = tkAsset->getChunkCount(); + const uint32_t bondCount = tkAsset->getBondCount(); + + m_fractureBuffers.bondFractureCount = bondCount; + m_fractureBuffers.chunkFractureCount = chunkCount; + m_fractureData.resize((uint32_t)(m_fractureBuffers.bondFractureCount*sizeof(NvBlastBondFractureData) + m_fractureBuffers.chunkFractureCount*sizeof(NvBlastChunkFractureData))); // chunk count + bond count + m_fractureBuffers.chunkFractures = reinterpret_cast<NvBlastChunkFractureData*>(m_fractureData.begin()); + m_fractureBuffers.bondFractures = reinterpret_cast<NvBlastBondFractureData*>(&m_fractureData.begin()[m_fractureBuffers.chunkFractureCount*sizeof(NvBlastChunkFractureData)]); +} + +void ExtImpactDamageManagerImpl::damageActor(ExtPxActor* actor, PxShape* /*shape*/, PxVec3 position, PxVec3 force) +{ + ensureBuffersSize(actor); + + NvBlastExtShearDamageDesc damage[] = { + { + { force[0], force[1], force[2] }, // shear + { position[0], position[1], position[2] } // position + } + }; + + const void* familyMaterial = actor->getTkActor().getFamily().getMaterial(); + + // default material params settings + const NvBlastExtMaterial defaultMaterial = { 3.0f, 0.1f, 0.2f, 1.5f + 1e-5f, 0.95f }; + + NvBlastProgramParams programParams; + programParams.damageDescCount = 1; + programParams.damageDescBuffer = &damage; + programParams.material = familyMaterial == nullptr ? &defaultMaterial : familyMaterial; + + NvBlastDamageProgram program = { + NvBlastExtShearGraphShader, + NvBlastExtShearSubgraphShader + }; + + NvBlastFractureBuffers fractureEvents = m_fractureBuffers; + + actor->getTkActor().generateFracture(&fractureEvents, program, &programParams); + actor->getTkActor().applyFracture(nullptr, &fractureEvents); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Filter Shader +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +PxFilterFlags ExtImpactDamageManager::FilterShader( + PxFilterObjectAttributes attributes0, + PxFilterData filterData0, + PxFilterObjectAttributes attributes1, + PxFilterData filterData1, + PxPairFlags& pairFlags, + const void* constantBlock, + uint32_t constantBlockSize) +{ + PX_UNUSED(constantBlock); + PX_UNUSED(constantBlockSize); + // let triggers through + if (PxFilterObjectIsTrigger(attributes0) || PxFilterObjectIsTrigger(attributes1)) + { + pairFlags = PxPairFlag::eTRIGGER_DEFAULT; + return PxFilterFlags(); + } + + if ((PxFilterObjectIsKinematic(attributes0) || PxFilterObjectIsKinematic(attributes1)) && + (PxGetFilterObjectType(attributes0) == PxFilterObjectType::eRIGID_STATIC || PxGetFilterObjectType(attributes1) == PxFilterObjectType::eRIGID_STATIC)) + { + return PxFilterFlag::eSUPPRESS; + } + + // use a group-based mechanism if the first two filter data words are not 0 + uint32_t f0 = filterData0.word0 | filterData0.word1; + uint32_t f1 = filterData1.word0 | filterData1.word1; + if (f0 && f1 && !(filterData0.word0&filterData1.word1 || filterData1.word0&filterData0.word1)) + return PxFilterFlag::eSUPPRESS; + + // determine if we should suppress notification + const bool suppressNotify = ((filterData0.word3 | filterData1.word3) & ExtPxManager::LEAF_CHUNK) != 0; + + pairFlags = PxPairFlag::eCONTACT_DEFAULT; + if (!suppressNotify) + { + pairFlags = pairFlags + | PxPairFlag::eNOTIFY_CONTACT_POINTS + | PxPairFlag::eNOTIFY_THRESHOLD_FORCE_PERSISTS + | PxPairFlag::eNOTIFY_THRESHOLD_FORCE_FOUND + | PxPairFlag::eNOTIFY_TOUCH_FOUND + | PxPairFlag::eNOTIFY_TOUCH_PERSISTS; + } + + // eSOLVE_CONTACT is invalid with kinematic pairs + if (PxFilterObjectIsKinematic(attributes0) && PxFilterObjectIsKinematic(attributes1)) + { + pairFlags &= ~PxPairFlag::eSOLVE_CONTACT; + } + + return PxFilterFlags(); +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.cpp new file mode 100644 index 0000000..8329de5 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.cpp @@ -0,0 +1,1312 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtImpulseStressSolver.h" +#include "NvBlastExtPxAsset.h" +#include "NvBlastExtPxFamily.h" +#include "NvBlastExtPxActor.h" +#include "NvBlastAssert.h" +#include "NvBlastIndexFns.h" +#include "NvBlastExtDefs.h" + +#include "NvBlastTkAsset.h" +#include "NvBlastTkActor.h" +#include "NvBlastTkFamily.h" + +#include "PxScene.h" +#include "PxRigidDynamic.h" + +#include <PsVecMath.h> +#include "PsFPU.h" + +#include <algorithm> +#include <set> + +#define USE_SCALAR_IMPL 0 +#define WARM_START 1 +#define USE_PHYSX_CONVEX_DATA 1 +#define GRAPH_INTERGRIRY_CHECK 0 + + +namespace Nv +{ +namespace Blast +{ + +using namespace physx; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Solver +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +class SequentialImpulseSolver +{ +public: + PX_ALIGN_PREFIX(16) + struct BondData + { + physx::PxVec3 impulseLinear; + uint32_t node0; + physx::PxVec3 impulseAngular; + uint32_t node1; + physx::PxVec3 offset0; + float invOffsetSqrLength; + + float getStressHealth(const ExtStressSolverSettings& settings) const + { + return (impulseLinear.magnitude() * settings.stressLinearFactor + impulseAngular.magnitude() * settings.stressAngularFactor); + } + } + PX_ALIGN_SUFFIX(16); + + PX_ALIGN_PREFIX(16) + struct NodeData + { + physx::PxVec3 velocityLinear; + float invI; + physx::PxVec3 velocityAngular; + float invMass; + } + PX_ALIGN_SUFFIX(16); + + SequentialImpulseSolver(uint32_t nodeCount, uint32_t maxBondCount) + { + m_nodesData.resize(nodeCount); + m_bondsData.reserve(maxBondCount); + } + + NV_INLINE const NodeData& getNodeData(uint32_t node) const + { + return m_nodesData[node]; + } + + NV_INLINE const BondData& getBondData(uint32_t bond) const + { + return m_bondsData[bond]; + } + + NV_INLINE uint32_t getBondCount() const + { + return m_bondsData.size(); + } + + NV_INLINE uint32_t getNodeCount() const + { + return m_nodesData.size();; + } + + NV_INLINE void setNodeMassInfo(uint32_t node, float invMass, float invI) + { + m_nodesData[node].invMass = invMass; + m_nodesData[node].invI = invI; + } + + NV_INLINE void initialize() + { + for (auto& node : m_nodesData) + { + node.velocityLinear = PxVec3(PxZero); + node.velocityAngular = PxVec3(PxZero); + } + } + + NV_INLINE void setNodeVelocities(uint32_t node, const PxVec3& velocityLinear, const PxVec3& velocityAngular) + { + m_nodesData[node].velocityLinear = velocityLinear; + m_nodesData[node].velocityAngular = velocityAngular; + } + + NV_INLINE uint32_t addBond(uint32_t node0, uint32_t node1, const PxVec3& offset) + { + const BondData data = { + PxVec3(PxZero), + node0, + PxVec3(PxZero), + node1, + offset, + 1.0f / offset.magnitudeSquared() + }; + m_bondsData.pushBack(data); + return m_bondsData.size() - 1; + } + + NV_INLINE void replaceWithLast(uint32_t bondIndex) + { + m_bondsData.replaceWithLast(bondIndex); + } + + NV_INLINE void reset(uint32_t nodeCount) + { + m_bondsData.clear(); + m_nodesData.resize(nodeCount); + } + + NV_INLINE void clearBonds() + { + m_bondsData.clear(); + } + + void solve(uint32_t iterationCount, bool warmStart = false) + { + solveInit(warmStart); + + for (uint32_t i = 0; i < iterationCount; ++i) + { + iterate(); + } + } + + void calcError(float& linear, float& angular) + { + linear = 0.0f; + angular = 0.0f; + for (BondData& bond : m_bondsData) + { + NodeData* node0 = &m_nodesData[bond.node0]; + NodeData* node1 = &m_nodesData[bond.node1]; + + const PxVec3 vA = node0->velocityLinear - node0->velocityAngular.cross(bond.offset0); + const PxVec3 vB = node1->velocityLinear + node1->velocityAngular.cross(bond.offset0); + + const PxVec3 vErrorLinear = vA - vB; + const PxVec3 vErrorAngular = node0->velocityAngular - node1->velocityAngular; + + linear += vErrorLinear.magnitude(); + angular += vErrorAngular.magnitude(); + } + } + +private: + void solveInit(bool warmStart = false) + { + if (warmStart) + { + for (BondData& bond : m_bondsData) + { + NodeData* node0 = &m_nodesData[bond.node0]; + NodeData* node1 = &m_nodesData[bond.node1]; + + const PxVec3 velocityLinearCorr0 = bond.impulseLinear * node0->invMass; + const PxVec3 velocityLinearCorr1 = bond.impulseLinear * node1->invMass; + + const PxVec3 velocityAngularCorr0 = bond.impulseAngular * node0->invI - bond.offset0.cross(velocityLinearCorr0) * bond.invOffsetSqrLength; + const PxVec3 velocityAngularCorr1 = bond.impulseAngular * node1->invI + bond.offset0.cross(velocityLinearCorr1) * bond.invOffsetSqrLength; + + node0->velocityLinear += velocityLinearCorr0; + node1->velocityLinear -= velocityLinearCorr1; + + node0->velocityAngular += velocityAngularCorr0; + node1->velocityAngular -= velocityAngularCorr1; + } + } + else + { + for (BondData& bond : m_bondsData) + { + bond.impulseLinear = PxVec3(PxZero); + bond.impulseAngular = PxVec3(PxZero); + } + } + } + + + NV_INLINE void iterate() + { + using namespace physx::shdfnd::aos; + + for (BondData& bond : m_bondsData) + { + NodeData* node0 = &m_nodesData[bond.node0]; + NodeData* node1 = &m_nodesData[bond.node1]; + +#if USE_SCALAR_IMPL + const PxVec3 vA = node0->velocityLinear - node0->velocityAngular.cross(bond.offset0); + const PxVec3 vB = node1->velocityLinear + node1->velocityAngular.cross(bond.offset0); + + const PxVec3 vErrorLinear = vA - vB; + const PxVec3 vErrorAngular = node0->velocityAngular - node1->velocityAngular; + + const float weightedMass = 1.0f / (node0->invMass + node1->invMass); + const float weightedInertia = 1.0f / (node0->invI + node1->invI); + + const PxVec3 outImpulseLinear = -vErrorLinear * weightedMass * 0.5f; + const PxVec3 outImpulseAngular = -vErrorAngular * weightedInertia * 0.5f; + + bond.impulseLinear += outImpulseLinear; + bond.impulseAngular += outImpulseAngular; + + const PxVec3 velocityLinearCorr0 = outImpulseLinear * node0->invMass; + const PxVec3 velocityLinearCorr1 = outImpulseLinear * node1->invMass; + + const PxVec3 velocityAngularCorr0 = outImpulseAngular * node0->invI - bond.offset0.cross(velocityLinearCorr0) * bond.invOffsetSqrLength; + const PxVec3 velocityAngularCorr1 = outImpulseAngular * node1->invI + bond.offset0.cross(velocityLinearCorr1) * bond.invOffsetSqrLength; + + node0->velocityLinear += velocityLinearCorr0; + node1->velocityLinear -= velocityLinearCorr1; + + node0->velocityAngular += velocityAngularCorr0; + node1->velocityAngular -= velocityAngularCorr1; +#else + const Vec3V velocityLinear0 = V3LoadUnsafeA(node0->velocityLinear); + const Vec3V velocityLinear1 = V3LoadUnsafeA(node1->velocityLinear); + const Vec3V velocityAngular0 = V3LoadUnsafeA(node0->velocityAngular); + const Vec3V velocityAngular1 = V3LoadUnsafeA(node1->velocityAngular); + + const Vec3V offset = V3LoadUnsafeA(bond.offset0); + const Vec3V vA = V3Add(velocityLinear0, V3Neg(V3Cross(velocityAngular0, offset))); + const Vec3V vB = V3Add(velocityLinear1, V3Cross(velocityAngular1, offset)); + + const Vec3V vErrorLinear = V3Sub(vA, vB); + const Vec3V vErrorAngular = V3Sub(velocityAngular0, velocityAngular1); + + const FloatV invM0 = FLoad(node0->invMass); + const FloatV invM1 = FLoad(node1->invMass); + const FloatV invI0 = FLoad(node0->invI); + const FloatV invI1 = FLoad(node1->invI); + const FloatV invOffsetSqrLength = FLoad(bond.invOffsetSqrLength); + + const FloatV weightedMass = FLoad(-0.5f / (node0->invMass + node1->invMass)); + const FloatV weightedInertia = FLoad(-0.5f / (node0->invI + node1->invI)); + + const Vec3V outImpulseLinear = V3Scale(vErrorLinear, weightedMass); + const Vec3V outImpulseAngular = V3Scale(vErrorAngular, weightedInertia); + + V3StoreA(V3Add(V3LoadUnsafeA(bond.impulseLinear), outImpulseLinear), bond.impulseLinear); + V3StoreA(V3Add(V3LoadUnsafeA(bond.impulseAngular), outImpulseAngular), bond.impulseAngular); + + const Vec3V velocityLinearCorr0 = V3Scale(outImpulseLinear, invM0); + const Vec3V velocityLinearCorr1 = V3Scale(outImpulseLinear, invM1); + + const Vec3V velocityAngularCorr0 = V3Sub(V3Scale(outImpulseAngular, invI0), V3Scale(V3Cross(offset, velocityLinearCorr0), invOffsetSqrLength)); + const Vec3V velocityAngularCorr1 = V3Add(V3Scale(outImpulseAngular, invI1), V3Scale(V3Cross(offset, velocityLinearCorr1), invOffsetSqrLength)); + + V3StoreA(V3Add(velocityLinear0, velocityLinearCorr0), node0->velocityLinear); + V3StoreA(V3Sub(velocityLinear1, velocityLinearCorr1), node1->velocityLinear); + + V3StoreA(V3Add(velocityAngular0, velocityAngularCorr0), node0->velocityAngular); + V3StoreA(V3Sub(velocityAngular1, velocityAngularCorr1), node1->velocityAngular); +#endif + } + } + + shdfnd::Array<BondData, ExtAlignedAllocator<16>> m_bondsData; + shdfnd::Array<NodeData, ExtAlignedAllocator<16>> m_nodesData; +}; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Graph Processor +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +#if GRAPH_INTERGRIRY_CHECK +#define CHECK_GRAPH_INTEGRITY checkGraphIntegrity() +#else +#define CHECK_GRAPH_INTEGRITY ((void)0) +#endif + +class SupportGraphProcessor +{ + +public: + struct BondData + { + uint32_t node0; + uint32_t node1; + uint32_t blastBondIndex; + }; + + struct NodeData + { + float mass; + float volume; + PxVec3 localPos; + bool isStatic; + uint32_t solverNode; + uint32_t neighborsCount; + PxVec3 impulse; + }; + + struct SolverNodeData + { + uint32_t supportNodesCount; + PxVec3 localPos; + union + { + float mass; + int32_t indexShift; + }; + float volume; + bool isStatic; + }; + + struct SolverBondData + { + ExtInlineArray<uint32_t, 8>::type blastBondIndices; + }; + + SupportGraphProcessor(uint32_t nodeCount, uint32_t maxBondCount) : m_solver(nodeCount, maxBondCount), m_nodesDirty(true) + { + m_nodesData.resize(nodeCount); + m_bondsData.reserve(maxBondCount); + + m_solverNodesData.resize(nodeCount); + m_solverBondsData.reserve(maxBondCount); + + m_solverBondsMap.reserve(maxBondCount); + + m_blastBondIndexMap.resize(maxBondCount); + memset(m_blastBondIndexMap.begin(), 0xFF, m_blastBondIndexMap.size() * sizeof(uint32_t)); + } + + NV_INLINE const NodeData& getNodeData(uint32_t node) const + { + return m_nodesData[node]; + } + + NV_INLINE const BondData& getBondData(uint32_t bond) const + { + return m_bondsData[bond]; + } + + NV_INLINE const SolverNodeData& getSolverNodeData(uint32_t node) const + { + return m_solverNodesData[node]; + } + + NV_INLINE const SolverBondData& getSolverBondData(uint32_t bond) const + { + return m_solverBondsData[bond]; + } + + NV_INLINE const SequentialImpulseSolver::BondData& getSolverInternalBondData(uint32_t bond) const + { + return m_solver.getBondData(bond); + } + + NV_INLINE const SequentialImpulseSolver::NodeData& getSolverInternalNodeData(uint32_t node) const + { + return m_solver.getNodeData(node); + } + + NV_INLINE uint32_t getBondCount() const + { + return m_bondsData.size(); + } + + NV_INLINE uint32_t getNodeCount() const + { + return m_nodesData.size();; + } + + NV_INLINE uint32_t getSolverBondCount() const + { + return m_solverBondsData.size(); + } + + NV_INLINE uint32_t getSolverNodeCount() const + { + return m_solverNodesData.size();; + } + + NV_INLINE void setNodeInfo(uint32_t node, float mass, float volume, PxVec3 localPos, bool isStatic) + { + m_nodesData[node].mass = mass; + m_nodesData[node].volume = volume; + m_nodesData[node].localPos = localPos; + m_nodesData[node].isStatic = isStatic; + } + + NV_INLINE void setNodeNeighborsCount(uint32_t node, uint32_t neighborsCount) + { + // neighbors count is expected to be the number of nodes on 1 island/actor. + m_nodesData[node].neighborsCount = neighborsCount; + + // check for too huge aggregates (happens after island's split) + if (!m_nodesDirty) + { + m_nodesDirty |= (m_solverNodesData[m_nodesData[node].solverNode].supportNodesCount > neighborsCount / 2); + } + } + + NV_INLINE void initialize() + { + sync(); + + m_solver.initialize(); + + for (auto& node : m_nodesData) + { + node.impulse = PxVec3(PxZero); + } + } + + NV_INLINE void addNodeImpulse(uint32_t node, const PxVec3& impulse) + { + m_nodesData[node].impulse += impulse; + } + + NV_INLINE void addNodeVelocity(uint32_t node, const PxVec3& velocity) + { + PxVec3 impulse = velocity * m_nodesData[node].mass; + addNodeImpulse(node, impulse); + } + + NV_INLINE void addBond(uint32_t node0, uint32_t node1, uint32_t blastBondIndex) + { + if (isInvalidIndex(m_blastBondIndexMap[blastBondIndex])) + { + const BondData data = { + node0, + node1, + blastBondIndex + }; + m_bondsData.pushBack(data); + m_blastBondIndexMap[blastBondIndex] = m_bondsData.size() - 1; + } + } + + NV_INLINE void removeBondIfExists(uint32_t blastBondIndex) + { + const uint32_t bondIndex = m_blastBondIndexMap[blastBondIndex]; + + if (!isInvalidIndex(bondIndex)) + { + const BondData& bond = m_bondsData[bondIndex]; + const uint32_t solverNode0 = m_nodesData[bond.node0].solverNode; + const uint32_t solverNode1 = m_nodesData[bond.node1].solverNode; + bool isBondInternal = (solverNode0 == solverNode1); + + if (isBondInternal) + { + // internal bond sadly requires graph resync (it never happens on reduction level '0') + m_nodesDirty = true; + } + else if (!m_nodesDirty) + { + // otherwise it's external bond, we can remove it manually and keep graph synced + // we don't need to spend time there if (m_nodesDirty == true), graph will be resynced anyways + + BondKey solverBondKey(solverNode0, solverNode1); + auto entry = m_solverBondsMap.find(solverBondKey); + if (entry) + { + const uint32_t solverBondIndex = entry->second; + auto& blastBondIndices = m_solverBondsData[solverBondIndex].blastBondIndices; + blastBondIndices.findAndReplaceWithLast(blastBondIndex); + if (blastBondIndices.empty()) + { + // all bonds associated with this solver bond were removed, so let's remove solver bond + + m_solverBondsData.replaceWithLast(solverBondIndex); + m_solver.replaceWithLast(solverBondIndex); + if (m_solver.getBondCount() > 0) + { + // update 'previously last' solver bond mapping + const auto& solverBond = m_solver.getBondData(solverBondIndex); + m_solverBondsMap[BondKey(solverBond.node0, solverBond.node1)] = solverBondIndex; + } + + m_solverBondsMap.erase(solverBondKey); + } + } + + CHECK_GRAPH_INTEGRITY; + } + + // remove bond from graph processor's list + m_blastBondIndexMap[blastBondIndex] = invalidIndex<uint32_t>(); + m_bondsData.replaceWithLast(bondIndex); + m_blastBondIndexMap[m_bondsData[bondIndex].blastBondIndex] = m_bondsData.size() > bondIndex ? bondIndex : invalidIndex<uint32_t>(); + } + } + + NV_INLINE void setGraphReductionLevel(uint32_t level) + { + m_graphReductionLevel = level; + m_nodesDirty = true; + } + + uint32_t getGraphReductionLevel() const + { + return m_graphReductionLevel; + } + + void solve(uint32_t iterationCount, bool warmStart = false) + { + CHECK_GRAPH_INTEGRITY; + + for (const NodeData& node : m_nodesData) + { + const SequentialImpulseSolver::NodeData& solverNode = m_solver.getNodeData(node.solverNode); + m_solver.setNodeVelocities(node.solverNode, solverNode.velocityLinear + node.impulse * solverNode.invMass, PxVec3(PxZero)); + } + + m_solver.solve(iterationCount, warmStart); + } + + void calcError(float& linear, float& angular) + { + m_solver.calcError(linear, angular); + } + + void generateFracture(ExtArray<NvBlastBondFractureData>::type& bondFractureBuffer, const ExtStressSolverSettings& settings, const float* blastBondHealths) + { + CHECK_GRAPH_INTEGRITY; + + for (uint32_t i = 0; i < m_solverBondsData.size(); ++i) + { + const SequentialImpulseSolver::BondData& solverInternalBond = m_solver.getBondData(i); + if (solverInternalBond.getStressHealth(settings) > 1.0f) + { + const auto& blastBondIndices = m_solverBondsData[i].blastBondIndices; + for (auto blastBondIndex : blastBondIndices) + { + const uint32_t bondIndex = m_blastBondIndexMap[blastBondIndex]; + if (!isInvalidIndex(bondIndex)) + { + const BondData& bond = m_bondsData[bondIndex]; + + NVBLAST_ASSERT(getNodeData(bond.node0).solverNode != getNodeData(bond.node1).solverNode); + NVBLAST_ASSERT(bond.blastBondIndex == blastBondIndex); + + NvBlastBondFractureData data; + data.health = blastBondHealths[blastBondIndex]; + data.nodeIndex0 = bond.node0; + data.nodeIndex1 = bond.node1; + bondFractureBuffer.pushBack(data); + } + } + } + } + } + +private: + + NV_INLINE void sync() + { + if (m_nodesDirty) + { + syncNodes(); + } + if (m_bondsDirty) + { + syncBonds(); + } + + CHECK_GRAPH_INTEGRITY; + } + + void syncNodes() + { + // init with 1<->1 blast nodes to solver nodes mapping + m_solverNodesData.resize(m_nodesData.size()); + for (uint32_t i = 0; i < m_nodesData.size(); ++i) + { + m_nodesData[i].solverNode = i; + m_solverNodesData[i].supportNodesCount = 1; + m_solverNodesData[i].indexShift = 0; + } + + // for static nodes aggregate size per graph reduction level is lower, it + // falls behind on few levels. (can be made as parameter) + const uint32_t STATIC_NODES_COUNT_PENALTY = 2 << 2; + + // reducing graph by aggregating nodes level by level + for (uint32_t k = 0; k < m_graphReductionLevel; k++) + { + const uint32_t maxAggregateSize = 1 << (k + 1); + + for (const BondData& bond : m_bondsData) + { + NodeData& node0 = m_nodesData[bond.node0]; + NodeData& node1 = m_nodesData[bond.node1]; + + if (node0.isStatic != node1.isStatic) + continue; + + if (node0.solverNode == node1.solverNode) + continue; + + SolverNodeData& solverNode0 = m_solverNodesData[node0.solverNode]; + SolverNodeData& solverNode1 = m_solverNodesData[node1.solverNode]; + + const int countPenalty = node0.isStatic ? STATIC_NODES_COUNT_PENALTY : 1; + const uint32_t aggregateSize = std::min<uint32_t>(maxAggregateSize, node0.neighborsCount / 2); + + if (solverNode0.supportNodesCount * countPenalty >= aggregateSize) + continue; + if (solverNode1.supportNodesCount * countPenalty >= aggregateSize) + continue; + + if (solverNode0.supportNodesCount >= solverNode1.supportNodesCount) + { + solverNode1.supportNodesCount--; + solverNode0.supportNodesCount++; + node1.solverNode = node0.solverNode; + } + else if (solverNode1.supportNodesCount >= solverNode0.supportNodesCount) + { + solverNode1.supportNodesCount++; + solverNode0.supportNodesCount--; + node0.solverNode = node1.solverNode; + } + } + } + + // Solver Nodes now sparse, a lot of empty ones. Rearrange them by moving all non-empty to the front + // 2 passes used for that + { + uint32_t currentNode = 0; + for (; currentNode < m_solverNodesData.size(); ++currentNode) + { + if (m_solverNodesData[currentNode].supportNodesCount > 0) + continue; + + // 'currentNode' is free + + // search next occupied node + uint32_t k = currentNode + 1; + for (; k < m_solverNodesData.size(); ++k) + { + if (m_solverNodesData[k].supportNodesCount > 0) + { + // replace currentNode and keep indexShift + m_solverNodesData[currentNode].supportNodesCount = m_solverNodesData[k].supportNodesCount; + m_solverNodesData[k].indexShift = k - currentNode; + m_solverNodesData[k].supportNodesCount = 0; + break; + } + } + + if (k == m_solverNodesData.size()) + { + break; + } + } + for (auto& node : m_nodesData) + { + node.solverNode -= m_solverNodesData[node.solverNode].indexShift; + } + + // now, we know total solver nodes count and which nodes are aggregated into them + m_solverNodesData.resize(currentNode); + } + + + // calculate all needed data + for (SolverNodeData& solverNode : m_solverNodesData) + { + solverNode.supportNodesCount = 0; + solverNode.localPos = PxVec3(PxZero); + solverNode.mass = 0.0f; + solverNode.volume = 0.0f; + solverNode.isStatic = false; + } + + for (NodeData& node : m_nodesData) + { + SolverNodeData& solverNode = m_solverNodesData[node.solverNode]; + solverNode.supportNodesCount++; + solverNode.localPos += node.localPos; + solverNode.mass += node.mass; + solverNode.volume += node.volume; + solverNode.isStatic |= node.isStatic; + } + + for (SolverNodeData& solverNode : m_solverNodesData) + { + solverNode.localPos /= (float)solverNode.supportNodesCount; + } + + m_solver.reset(m_solverNodesData.size()); + for (uint32_t nodeIndex = 0; nodeIndex < m_solverNodesData.size(); ++nodeIndex) + { + const SolverNodeData& solverNode = m_solverNodesData[nodeIndex]; + + const float invMass = solverNode.isStatic ? 0.0f : 1.0f / solverNode.mass; + const float R = PxPow(solverNode.volume * 3.0f * PxInvPi / 4.0f, 1.0f / 3.0f); // sphere volume approximation + const float invI = invMass / (R * R * 0.4f); // sphere inertia tensor approximation: I = 2/5 * M * R^2 ; invI = 1 / I; + m_solver.setNodeMassInfo(nodeIndex, invMass, invI); + } + + m_nodesDirty = false; + + syncBonds(); + } + + void syncBonds() + { + // traverse all blast bonds and aggregate + m_solver.clearBonds(); + m_solverBondsMap.clear(); + m_solverBondsData.clear(); + for (const BondData& bond : m_bondsData) + { + const NodeData& node0 = m_nodesData[bond.node0]; + const NodeData& node1 = m_nodesData[bond.node1]; + + if (node0.solverNode == node1.solverNode) + continue; // skip (internal) + + if (node0.isStatic && node1.isStatic) + continue; + + BondKey key(node0.solverNode, node1.solverNode); + auto entry = m_solverBondsMap.find(key); + SolverBondData* data; + if (!entry) + { + m_solverBondsData.pushBack(SolverBondData()); + data = &m_solverBondsData.back(); + m_solverBondsMap[key] = m_solverBondsData.size() - 1; + + SolverNodeData& solverNode0 = m_solverNodesData[node0.solverNode]; + SolverNodeData& solverNode1 = m_solverNodesData[node1.solverNode]; + m_solver.addBond(node0.solverNode, node1.solverNode, (solverNode1.localPos - solverNode0.localPos) * 0.5f); + } + else + { + data = &m_solverBondsData[entry->second]; + } + data->blastBondIndices.pushBack(bond.blastBondIndex); + } + + m_bondsDirty = false; + } + +#if GRAPH_INTERGRIRY_CHECK + void checkGraphIntegrity() + { + NVBLAST_ASSERT(m_solver.getBondCount() == m_solverBondsData.size()); + NVBLAST_ASSERT(m_solver.getNodeCount() == m_solverNodesData.size()); + + std::set<uint64_t> solverBonds; + for (uint32_t i = 0; i < m_solverBondsData.size(); ++i) + { + const auto& bondData = m_solver.getBondData(i); + BondKey key(bondData.node0, bondData.node1); + NVBLAST_ASSERT(solverBonds.find(key) == solverBonds.end()); + solverBonds.emplace(key); + auto entry = m_solverBondsMap.find(key); + NVBLAST_ASSERT(entry != nullptr); + const auto& solverBond = m_solverBondsData[entry->second]; + for (auto& blastBondIndex : solverBond.blastBondIndices) + { + if (!isInvalidIndex(m_blastBondIndexMap[blastBondIndex])) + { + auto& b = m_bondsData[m_blastBondIndexMap[blastBondIndex]]; + BondKey key2(m_nodesData[b.node0].solverNode, m_nodesData[b.node1].solverNode); + NVBLAST_ASSERT(key2 == key); + } + } + } + + for (auto& solverBond : m_solverBondsData) + { + for (auto& blastBondIndex : solverBond.blastBondIndices) + { + if (!isInvalidIndex(m_blastBondIndexMap[blastBondIndex])) + { + auto& b = m_bondsData[m_blastBondIndexMap[blastBondIndex]]; + NVBLAST_ASSERT(m_nodesData[b.node0].solverNode != m_nodesData[b.node1].solverNode); + } + } + } + uint32_t mappedBondCount = 0; + for (uint32_t i = 0; i < m_blastBondIndexMap.size(); i++) + { + const auto& bondIndex = m_blastBondIndexMap[i]; + if (!isInvalidIndex(bondIndex)) + { + mappedBondCount++; + NVBLAST_ASSERT(m_bondsData[bondIndex].blastBondIndex == i); + } + } + NVBLAST_ASSERT(m_bondsData.size() == mappedBondCount); + } +#endif + + struct BondKey + { + uint32_t node0; + uint32_t node1; + + BondKey(uint32_t n0, uint32_t n1) + { + node0 = n0 < n1 ? n0 : n1; + node1 = n0 < n1 ? n1 : n0; + } + + operator uint64_t() const + { + return static_cast<uint64_t>(node0) + (static_cast<uint64_t>(node1) << 32); + } + }; + + SequentialImpulseSolver m_solver; + ExtArray<SolverNodeData>::type m_solverNodesData; + ExtArray<SolverBondData>::type m_solverBondsData; + + uint32_t m_graphReductionLevel; + + bool m_nodesDirty; + bool m_bondsDirty; + + ExtHashMap<BondKey, uint32_t>::type m_solverBondsMap; + ExtArray<uint32_t>::type m_blastBondIndexMap; + + ExtArray<BondData>::type m_bondsData; + ExtArray<NodeData>::type m_nodesData; +}; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtImpulseStressSolver +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Creation +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +ExtImpulseStressSolver::ExtImpulseStressSolver(ExtPxFamily& family, ExtStressSolverSettings settings) + : m_family(family), m_settings(settings), m_isDirty(false), m_reset(false), + m_errorAngular(std::numeric_limits<float>::max()), m_errorLinear(std::numeric_limits<float>::max()), m_framesCount(0) +{ + + const TkAsset* tkAsset = m_family.getTkFamily().getAsset(); + const ExtPxAsset& asset = m_family.getPxAsset(); + const ExtPxChunk* chunks = asset.getChunks(); + const ExtPxSubchunk* subChunks = asset.getSubchunks(); + m_graph = tkAsset->getGraph(); + const uint32_t bondCount = tkAsset->getBondCount(); + + TkActor* tkActor; + m_family.getTkFamily().getActors(&tkActor, 1); + m_bondHealths = tkActor->getBondHealths(); + + m_graphProcessor = NVBLASTEXT_NEW(SupportGraphProcessor)(m_graph.nodeCount, bondCount); + + // traverse graph and fill node info + for (uint32_t i = 0; i < m_graph.nodeCount; ++i) + { + uint32_t node0 = i; + uint32_t chunkIndex0 = m_graph.chunkIndices[node0]; + const ExtPxChunk& chunk0 = chunks[chunkIndex0]; + + bool isChunkStatic = chunk0.isStatic; + + for (uint32_t adjacencyIndex = m_graph.adjacencyPartition[node0]; adjacencyIndex < m_graph.adjacencyPartition[node0 + 1]; adjacencyIndex++) + { + uint32_t bondIndex = m_graph.adjacentBondIndices[adjacencyIndex]; + if (m_bondHealths[bondIndex] <= 0.0f) + continue; + uint32_t node1 = m_graph.adjacentNodeIndices[adjacencyIndex]; + uint32_t chunkIndex1 = m_graph.chunkIndices[node1]; + const ExtPxChunk& chunk1 = chunks[chunkIndex1]; + + if (chunk1.subchunkCount == 0 || chunk1.isStatic) + { + isChunkStatic |= chunk1.isStatic; + continue; + } + } + + // fill node info + + float mass; + float volume; + PxVec3 localPos; + if (chunk0.subchunkCount > 0) + { +#if USE_PHYSX_CONVEX_DATA + const ExtPxSubchunk& subChunk = subChunks[chunk0.firstSubchunkIndex]; + PxVec3 localCenterOfMass; + PxMat33 intertia; + PxVec3 scale = subChunk.geometry.scale.scale; + subChunk.geometry.convexMesh->getMassInformation(mass, intertia, localCenterOfMass); + mass *= scale.x * scale.y * scale.z; + const PxTransform& chunk0LocalTransform = subChunk.transform; + localPos = chunk0LocalTransform.transform(localCenterOfMass); + volume = mass / 1.0f; // unit density +#else + volume = solverChunk0.volume; + mass = volume * 1.0f; // density + localPos = *reinterpret_cast<const PxVec3*>(solverChunk0.centroid); +#endif + } + else + { + mass = 0.0f; + volume = 0.0f; + localPos = PxVec3(PxZero); + isChunkStatic = true; + } + m_graphProcessor->setNodeInfo(node0, mass, volume, localPos, isChunkStatic); + } + + // traverse graph and fill bond info + for (uint32_t node0 = 0; node0 < m_graph.nodeCount; ++node0) + { + for (uint32_t adjacencyIndex = m_graph.adjacencyPartition[node0]; adjacencyIndex < m_graph.adjacencyPartition[node0 + 1]; adjacencyIndex++) + { + uint32_t bondIndex = m_graph.adjacentBondIndices[adjacencyIndex]; + if (m_bondHealths[bondIndex] <= 0.0f) + continue; + uint32_t node1 = m_graph.adjacentNodeIndices[adjacencyIndex]; + + if (node0 < node1) + { + m_graphProcessor->addBond(node0, node1, bondIndex); + } + } + } + + // fire initial actor's created + ExtInlineArray<ExtPxActor*, 4>::type actors;; + actors.resize((uint32_t)m_family.getActorCount()); + m_family.getActors(actors.begin(), actors.size()); + for (const auto actor : actors) + { + onActorCreated(m_family, *actor); + } + + m_family.subscribe(*this); +} + +ExtImpulseStressSolver::~ExtImpulseStressSolver() +{ + NVBLASTEXT_DELETE(m_graphProcessor, SupportGraphProcessor); + m_family.unsubscribe(*this); +} + +ExtStressSolver* ExtStressSolver::create(ExtPxFamily& family, ExtStressSolverSettings settings) +{ + return NVBLASTEXT_NEW(ExtImpulseStressSolver) (family, settings); +} + +void ExtImpulseStressSolver::release() +{ + NVBLASTEXT_DELETE(this, ExtImpulseStressSolver); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Actors +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void ExtImpulseStressSolver::onActorCreated(ExtPxFamily& /*family*/, ExtPxActor& actor) +{ + if (actor.getTkActor().getGraphNodeCount() > 1) + { + // update neighbors + { + const uint32_t graphNodeCount = actor.getTkActor().getGraphNodeCount(); + uint32_t* graphNodeIndices = getScratchArray<uint32_t>(graphNodeCount); + actor.getTkActor().getGraphNodeIndices(graphNodeIndices, graphNodeCount); + for (uint32_t i = 0; i < graphNodeCount; ++i) + { + m_graphProcessor->setNodeNeighborsCount(graphNodeIndices[i], graphNodeCount); + } + } + + m_actors.insert(&actor); + m_isDirty = true; + } +} + +void ExtImpulseStressSolver::onActorDestroyed(ExtPxFamily& /*family*/, ExtPxActor& actor) +{ + if (m_actors.erase(&actor)) + { + m_isDirty = true; + } +} + +void ExtImpulseStressSolver::syncSolver() +{ + // traverse graph and remove dead bonds + for (uint32_t node0 = 0; node0 < m_graph.nodeCount; ++node0) + { + for (uint32_t adjacencyIndex = m_graph.adjacencyPartition[node0]; adjacencyIndex < m_graph.adjacencyPartition[node0 + 1]; adjacencyIndex++) + { + uint32_t node1 = m_graph.adjacentNodeIndices[adjacencyIndex]; + if (node0 < node1) + { + uint32_t bondIndex = m_graph.adjacentBondIndices[adjacencyIndex]; + + if (m_bondHealths[bondIndex] <= 0.0f) + { + m_graphProcessor->removeBondIfExists(bondIndex); + } + } + } + } + + m_isDirty = false; +} + + +void ExtImpulseStressSolver::initialize() +{ + if (m_reset) + { + m_framesCount = 0; + } + + if (m_isDirty) + { + syncSolver(); + } + + if (m_settings.graphReductionLevel != m_graphProcessor->getGraphReductionLevel()) + { + m_graphProcessor->setGraphReductionLevel(m_settings.graphReductionLevel); + } + + m_graphProcessor->initialize(); + + for (auto it = m_actors.getIterator(); !it.done(); ++it) + { + const ExtPxActor* actor = *it; + const uint32_t graphNodeCount = actor->getTkActor().getGraphNodeCount(); + uint32_t* graphNodeIndices = getScratchArray<uint32_t>(graphNodeCount); + actor->getTkActor().getGraphNodeIndices(graphNodeIndices, graphNodeCount); + + PxRigidDynamic& rigidDynamic = actor->getPhysXActor(); + const bool isStatic = rigidDynamic.getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC; + if (isStatic) + { + PxVec3 gravity = rigidDynamic.getScene()->getGravity(); + gravity = rigidDynamic.getGlobalPose().rotateInv(gravity); + + for (uint32_t i = 0; i < graphNodeCount; ++i) + { + const uint32_t node = graphNodeIndices[i]; + m_graphProcessor->addNodeVelocity(node, gravity); + } + } + else + { + PxVec3 cMassPose = rigidDynamic.getCMassLocalPose().p; + + PxVec3 angularVelocity = rigidDynamic.getGlobalPose().rotateInv(rigidDynamic.getAngularVelocity()); + //PxVec3 linearVelocity = rigidDynamic.getGlobalPose().rotateInv(rigidDynamic.getLinearVelocity()); + + // Apply centrifugal force + for (uint32_t i = 0; i < graphNodeCount; ++i) + { + const uint32_t node = graphNodeIndices[i]; + const auto& localPos = m_graphProcessor->getNodeData(node).localPos; + // a = w x (w x r) + const PxVec3 centrifugalAcceleration = angularVelocity.cross(angularVelocity.cross(localPos - cMassPose)); + m_graphProcessor->addNodeVelocity(node, centrifugalAcceleration); + } + } + + const auto entry = m_impulseBuffer.find(actor); + if (entry) + { + for (const ImpulseData& data : entry->second) + { + float bestDist = FLT_MAX; + uint32_t bestNode = invalidIndex<uint32_t>(); + + for (uint32_t i = 0; i < graphNodeCount; ++i) + { + const uint32_t node = graphNodeIndices[i]; + const float sqrDist = (data.position - m_graphProcessor->getNodeData(node).localPos).magnitudeSquared(); + if (sqrDist < bestDist) + { + bestDist = sqrDist; + bestNode = node; + } + } + + if (!isInvalidIndex(bestNode)) + { + m_graphProcessor->addNodeImpulse(bestNode, data.impulse); + } + } + m_impulseBuffer[actor].clear(); + } + } +} + +void ExtImpulseStressSolver::applyImpulse(ExtPxActor& actor, physx::PxVec3 position, physx::PxVec3 force) +{ + ImpulseData data = { position, force }; + + m_impulseBuffer[&actor].pushBack(data); +} + +uint32_t ExtImpulseStressSolver::getBondCount() const +{ + return m_graphProcessor->getSolverBondCount(); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Update +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void ExtImpulseStressSolver::update(bool doDamage) +{ + initialize(); + + solve(); + + if (doDamage) + { + applyDamage(); + } + + m_framesCount++; +} + +void ExtImpulseStressSolver::solve() +{ + PX_SIMD_GUARD; + + const uint32_t iterations = getIterationsPerFrame(); + m_graphProcessor->solve(iterations, WARM_START && !m_reset); + m_reset = false; + + m_graphProcessor->calcError(m_errorLinear, m_errorAngular); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Damage +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void ExtImpulseStressSolver::applyDamage() +{ + m_bondFractureBuffer.clear(); + m_graphProcessor->generateFracture(m_bondFractureBuffer, m_settings, m_bondHealths); + + if (m_bondFractureBuffer.size() > 0) + { + NvBlastFractureBuffers fractureCommands; + fractureCommands.chunkFractureCount = 0; + fractureCommands.bondFractureCount = m_bondFractureBuffer.size(); + fractureCommands.bondFractures = m_bondFractureBuffer.begin(); + + m_family.getTkFamily().applyFracture(&fractureCommands); + } +} + +uint32_t ExtImpulseStressSolver::getIterationCount() const +{ + return getFrameCount() * getIterationsPerFrame(); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Debug Render +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +static PxU32 PxVec4ToU32Color(const PxVec4& color) +{ + PxU32 c = 0; + c |= (int)(color.w * 255); c <<= 8; + c |= (int)(color.z * 255); c <<= 8; + c |= (int)(color.y * 255); c <<= 8; + c |= (int)(color.x * 255); + return c; +} + +static PxVec4 PxVec4Lerp(const PxVec4 v0, const PxVec4 v1, float val) +{ + PxVec4 v( + v0.x * (1 - val) + v1.x * val, + v0.y * (1 - val) + v1.y * val, + v0.z * (1 - val) + v1.z * val, + v0.w * (1 - val) + v1.w * val + ); + return v; +} + +inline float clamp01(float v) +{ + return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v); +} + +inline PxVec4 bondHealthColor(float healthFraction) +{ + healthFraction = clamp01(healthFraction); + + const PxVec4 BOND_HEALTHY_COLOR(0.0f, 1.0f, 1.0f, 1.0f); + const PxVec4 BOND_MID_COLOR(1.0f, 1.0f, 0.0f, 1.0f); + const PxVec4 BOND_BROKEN_COLOR(1.0f, 0.0f, 0.0f, 1.0f); + + return healthFraction < 0.5 ? PxVec4Lerp(BOND_BROKEN_COLOR, BOND_MID_COLOR, 2.0f * healthFraction) : PxVec4Lerp(BOND_MID_COLOR, BOND_HEALTHY_COLOR, 2.0f * healthFraction - 1.0f); +} + +void ExtImpulseStressSolver::fillDebugRender(const std::vector<uint32_t>& nodes, std::vector<PxDebugLine>& lines, DebugRenderMode mode, float scale) +{ + const PxVec4 BOND_IMPULSE_LINEAR_COLOR(0.0f, 1.0f, 0.0f, 1.0f); + const PxVec4 BOND_IMPULSE_ANGULAR_COLOR(1.0f, 0.0f, 0.0f, 1.0f); + + if (m_isDirty) + return; + + ExtArray<uint8_t>::type& nodesSet = m_scratch; + + nodesSet.resize(m_graphProcessor->getSolverNodeCount()); + memset(nodesSet.begin(), 0, nodesSet.size() * sizeof(uint8_t)); + for (auto& nodeIndex : nodes) + { + nodesSet[m_graphProcessor->getNodeData(nodeIndex).solverNode] = 1; + } + + const uint32_t bondCount = m_graphProcessor->getSolverBondCount(); + for (uint32_t i = 0; i < bondCount; ++i) + { + const auto& solverInternalBondData = m_graphProcessor->getSolverInternalBondData(i); + if (nodesSet[solverInternalBondData.node0] != 0) + { + NVBLAST_ASSERT(nodesSet[solverInternalBondData.node1] != 0); + + const auto& solverInternalNode0 = m_graphProcessor->getSolverInternalNodeData(solverInternalBondData.node0); + const auto& solverInternalNode1 = m_graphProcessor->getSolverInternalNodeData(solverInternalBondData.node1); + const auto& solverNode0 = m_graphProcessor->getSolverNodeData(solverInternalBondData.node0); + const auto& solverNode1 = m_graphProcessor->getSolverNodeData(solverInternalBondData.node1); + + PxVec3 p0 = solverNode0.localPos; + PxVec3 p1 = solverNode1.localPos; + PxVec3 center = (p0 + p1) * 0.5f; + + const float stress = std::min<float>(solverInternalBondData.getStressHealth(m_settings), 1.0f); + PxVec4 color = bondHealthColor(1.0f - stress); + + lines.push_back(PxDebugLine(p0, p1, PxVec4ToU32Color(color))); + + float impulseScale = scale; + + if (mode == DebugRenderMode::STRESS_GRAPH_NODES_IMPULSES) + { + lines.push_back(PxDebugLine(p0, p0 + solverInternalNode0.velocityLinear * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_LINEAR_COLOR))); + lines.push_back(PxDebugLine(p0, p0 + solverInternalNode0.velocityAngular * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_ANGULAR_COLOR))); + lines.push_back(PxDebugLine(p1, p1 + solverInternalNode1.velocityLinear * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_LINEAR_COLOR))); + lines.push_back(PxDebugLine(p1, p1 + solverInternalNode1.velocityAngular * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_ANGULAR_COLOR))); + } + else if (mode == DebugRenderMode::STRESS_GRAPH_BONDS_IMPULSES) + { + lines.push_back(PxDebugLine(center, center + solverInternalBondData.impulseLinear * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_LINEAR_COLOR))); + lines.push_back(PxDebugLine(center, center + solverInternalBondData.impulseAngular * impulseScale, PxVec4ToU32Color(BOND_IMPULSE_ANGULAR_COLOR))); + } + } + } +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.h b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.h new file mode 100644 index 0000000..d274789 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtImpulseStressSolver.h @@ -0,0 +1,164 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTIMPULSESTRESSSOLVER_H +#define NVBLASTEXTIMPULSESTRESSSOLVER_H + +#include "NvBlastExtStressSolver.h" +#include "NvBlastExtPxManager.h" +#include "NvBlastExtPxListener.h" +#include "NvBlastTypes.h" +#include <NvBlastExtArray.h> +#include <NvBlastExtHashSet.h> +#include <NvBlastExtHashMap.h> + +namespace Nv +{ +namespace Blast +{ + + +struct ExtStressNodeCachedData +{ + physx::PxVec3 localPos; + bool isStatic; +}; + + +struct ExtStressBondCachedData +{ + uint32_t bondIndex; +}; + +class SupportGraphProcessor; + +/** +*/ +class ExtImpulseStressSolver : public ExtStressSolver, ExtPxListener +{ + NV_NOCOPY(ExtImpulseStressSolver) + +public: + ExtImpulseStressSolver(ExtPxFamily& family, ExtStressSolverSettings settings); + virtual void release() override; + + + //////// ExtStressSolver interface //////// + + virtual void setSettings(const ExtStressSolverSettings& settings) override + { + m_settings = settings; + } + + virtual const ExtStressSolverSettings& getSettings() const override + { + return m_settings; + } + + virtual void applyImpulse(ExtPxActor& actor, physx::PxVec3 position, physx::PxVec3 force) override; + + virtual void update(bool doDamage) override; + + void reset() override + { + m_reset = true; + } + + virtual float getStressErrorLinear() const override + { + return m_errorLinear; + } + + virtual float getStressErrorAngular() const override + { + return m_errorAngular; + } + + virtual uint32_t getIterationCount() const override; + + virtual uint32_t getFrameCount() const override + { + return m_framesCount; + } + + virtual uint32_t getBondCount() const override; + + virtual void fillDebugRender(const std::vector<uint32_t>& nodes, std::vector<physx::PxDebugLine>& lines, DebugRenderMode mode, float scale) override; + + + //////// ExtPxListener interface //////// + + virtual void onActorCreated(ExtPxFamily& family, ExtPxActor& actor) final; + + virtual void onActorDestroyed(ExtPxFamily& family, ExtPxActor& actor) final; + + +private: + ~ExtImpulseStressSolver(); + + + //////// private methods //////// + + void solve(); + + void applyDamage(); + + void initialize(); + + NV_INLINE void iterate(); + + void syncSolver(); + + template<class T> + NV_INLINE T* getScratchArray(uint32_t size); + + + //////// data //////// + + struct ImpulseData + { + physx::PxVec3 position; + physx::PxVec3 impulse; + }; + + ExtPxFamily& m_family; + ExtHashSet<ExtPxActor*>::type m_actors; + ExtStressSolverSettings m_settings; + NvBlastSupportGraph m_graph; + bool m_isDirty; + bool m_reset; + const float* m_bondHealths; + SupportGraphProcessor* m_graphProcessor; + float m_errorAngular; + float m_errorLinear; + uint32_t m_framesCount; + ExtArray<NvBlastBondFractureData>::type m_bondFractureBuffer; + ExtHashMap<const ExtPxActor*, ExtArray<ImpulseData>::type>::type m_impulseBuffer; + ExtArray<uint8_t>::type m_scratch; +}; + + +template<class T> +NV_INLINE T* ExtImpulseStressSolver::getScratchArray(uint32_t size) +{ + const uint32_t scratchSize = sizeof(T) * size; + if (m_scratch.size() < scratchSize) + { + m_scratch.resize(scratchSize); + } + return reinterpret_cast<T*>(m_scratch.begin()); +} + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTIMPULSESTRESSSOLVER_H diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.cpp new file mode 100644 index 0000000..7732d18 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.cpp @@ -0,0 +1,180 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtPxActorImpl.h" +#include "NvBlastExtPxAsset.h" +#include "NvBlastExtPxManagerImpl.h" +#include "NvBlastExtPxFamilyImpl.h" + +#include "PxRigidDynamic.h" +#include "PxPhysics.h" + +#include "NvBlastTkActor.h" +#include "NvBlastTkAsset.h" + +#include "PxRigidBodyExt.h" + + +namespace Nv +{ +namespace Blast +{ + + +ExtPxActorImpl::ExtPxActorImpl(ExtPxFamilyImpl* family, TkActor* tkActor, const PxActorCreateInfo& pxActorInfo) + : m_family(family), m_tkActor(tkActor) +{ + const ExtPxChunk* pxChunks = m_family->m_pxAsset.getChunks(); + const ExtPxSubchunk* pxSubchunks = m_family->m_pxAsset.getSubchunks(); + const NvBlastChunk* chunks = m_tkActor->getAsset()->getChunks(); + uint32_t nodeCount = m_tkActor->getGraphNodeCount(); + + PxFilterData simulationFilterData; // Default constructor = {0,0,0,0} + + // get visible chunk indices list + { + auto& chunkIndices = m_family->m_indicesScratch; + chunkIndices.resize(m_tkActor->getVisibleChunkCount()); + m_tkActor->getVisibleChunkIndices(chunkIndices.begin(), static_cast<uint32_t>(chunkIndices.size())); + + // fill visible chunk indices list with mapped to our asset indices + m_chunkIndices.reserve(chunkIndices.size()); + for (const uint32_t chunkIndex : chunkIndices) + { + const ExtPxChunk& chunk = pxChunks[chunkIndex]; + if (chunk.subchunkCount == 0) + continue; + m_chunkIndices.pushBack(chunkIndex); + } + + // Single lower-support chunk actors might be leaf actors, check for this and disable contact callbacks if so + if (nodeCount <= 1) + { + PX_ASSERT(chunkIndices.size() == 1); + if (chunkIndices.size() > 0) + { + const NvBlastChunk& chunk = chunks[chunkIndices[0]]; + if (chunk.firstChildIndex == chunk.childIndexStop) + { + simulationFilterData.word3 = ExtPxManager::LEAF_CHUNK; // mark as leaf chunk if chunk has no children + } + } + } + } + + // create rigidDynamic and setup + PxPhysics& physics = m_family->m_manager.m_physics; + m_rigidDynamic = physics.createRigidDynamic(pxActorInfo.m_transform); + if (m_family->m_pxActorDescTemplate != nullptr) + { + m_rigidDynamic->setActorFlags(static_cast<physx::PxActorFlags>(m_family->m_pxActorDescTemplate->flags)); + } + + // fill rigidDynamic with shapes + PxMaterial* material = m_family->m_spawnSettings.material; + for (uint32_t i = 0; i < m_chunkIndices.size(); ++i) + { + uint32_t chunkID = m_chunkIndices[i]; + const ExtPxChunk& chunk = pxChunks[chunkID]; + for (uint32_t c = 0; c < chunk.subchunkCount; c++) + { + const uint32_t subchunkIndex = chunk.firstSubchunkIndex + c; + auto& subchunk = pxSubchunks[subchunkIndex]; + PxShape* shape = physics.createShape(subchunk.geometry, *material); + shape->setLocalPose(subchunk.transform); + + const ExtPxShapeDescTemplate* pxShapeDesc = m_family->m_pxShapeDescTemplate; + if (pxShapeDesc != nullptr) + { + shape->setFlags(static_cast<PxShapeFlags>(pxShapeDesc->flags)); + shape->setSimulationFilterData(pxShapeDesc->simulationFilterData); + shape->setQueryFilterData(pxShapeDesc->queryFilterData); + shape->setContactOffset(pxShapeDesc->contactOffset); + shape->setRestOffset(pxShapeDesc->restOffset); + } + else + { + shape->setSimulationFilterData(simulationFilterData); + } + + m_rigidDynamic->attachShape(*shape); + + PX_ASSERT_WITH_MESSAGE(m_family->m_subchunkShapes[subchunkIndex] == nullptr, "Chunk has some shapes(live)."); + m_family->m_subchunkShapes[subchunkIndex] = shape; + } + } + + // search for static chunk in actor's graph (make actor static if it contains static chunk) + bool staticFound = false; + if (nodeCount > 0) + { + auto& graphChunkIndices = m_family->m_indicesScratch; + graphChunkIndices.resize(nodeCount); + m_tkActor->getGraphNodeIndices(graphChunkIndices.begin(), static_cast<uint32_t>(graphChunkIndices.size())); + const NvBlastSupportGraph graph = m_tkActor->getAsset()->getGraph(); + + for (uint32_t i = 0; !staticFound && i < graphChunkIndices.size(); ++i) + { + uint32_t chunkIndex = graph.chunkIndices[graphChunkIndices[i]]; + const ExtPxChunk& chunk = pxChunks[chunkIndex]; + staticFound = chunk.isStatic; + } + } + m_rigidDynamic->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, staticFound); + + // store pointer to actor in px userData + m_family->m_manager.registerActor(m_rigidDynamic, this); + + // store pointer to actor in blast userData + m_tkActor->userData = this; + + // update mass properties + PxRigidBodyExt::updateMassAndInertia(*m_rigidDynamic, m_family->m_spawnSettings.density); + + // set initial velocities + if (!(m_rigidDynamic->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC)) + { + m_rigidDynamic->setLinearVelocity(pxActorInfo.m_linearVelocity); + m_rigidDynamic->setAngularVelocity(pxActorInfo.m_angularVelocity); + } +} + +void ExtPxActorImpl::release() +{ + if (m_rigidDynamic != nullptr) + { + m_family->m_manager.unregisterActor(m_rigidDynamic); + m_rigidDynamic->release(); + m_rigidDynamic = nullptr; + } + + const ExtPxChunk* pxChunks = m_family->m_pxAsset.getChunks(); + for (uint32_t chunkID : m_chunkIndices) + { + const ExtPxChunk& chunk = pxChunks[chunkID]; + for (uint32_t c = 0; c < chunk.subchunkCount; c++) + { + const uint32_t subchunkIndex = chunk.firstSubchunkIndex + c; + m_family->m_subchunkShapes[subchunkIndex] = nullptr; + } + } + m_chunkIndices.clear(); + + m_tkActor->userData = nullptr; +} + +ExtPxFamily& ExtPxActorImpl::getFamily() const +{ + return *m_family; +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.h b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.h new file mode 100644 index 0000000..a592293 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxActorImpl.h @@ -0,0 +1,94 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXACTORIMPL_H +#define NVBLASTEXTPXACTORIMPL_H + +#include "NvBlastExtPxActor.h" +#include "NvBlastExtArray.h" +#include "PxTransform.h" + + +using namespace physx; + +namespace Nv +{ +namespace Blast +{ + + +// Forward declarations +class ExtPxFamilyImpl; + +struct PxActorCreateInfo +{ + PxTransform m_transform; + PxVec3 m_scale; + PxVec3 m_linearVelocity; + PxVec3 m_angularVelocity; +}; + + +class ExtPxActorImpl final : public ExtPxActor +{ +public: + //////// ctor //////// + + ExtPxActorImpl(ExtPxFamilyImpl* family, TkActor* tkActor, const PxActorCreateInfo& pxActorInfo); + + ~ExtPxActorImpl() + { + release(); + } + + void release(); + + + //////// interface //////// + + virtual uint32_t getChunkCount() const override + { + return static_cast<uint32_t>(m_chunkIndices.size()); + } + + virtual const uint32_t* getChunkIndices() const override + { + return m_chunkIndices.begin(); + } + + virtual PxRigidDynamic& getPhysXActor() const override + { + return *m_rigidDynamic; + } + + virtual TkActor& getTkActor() const override + { + return *m_tkActor; + } + + virtual ExtPxFamily& getFamily() const override; + + +private: + //////// data //////// + + ExtPxFamilyImpl* m_family; + TkActor* m_tkActor; + PxRigidDynamic* m_rigidDynamic; + ExtInlineArray<uint32_t, 4>::type m_chunkIndices; +}; + + + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXACTORIMPL_H diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.cpp new file mode 100644 index 0000000..a0f75fc --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.cpp @@ -0,0 +1,315 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtPxAssetImpl.h" +#include "NvBlastExtHashMap.h" + +#include "NvBlastAssert.h" +#include "NvBlastIndexFns.h" + +#include "NvBlastTkAsset.h" + +#include "PxIO.h" +#include "PxPhysics.h" +#include "PxFileBuf.h" +#include "cooking/PxCooking.h" + +#include <algorithm> + +namespace Nv +{ +namespace Blast +{ + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Helpers/Wrappers +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +class FileBufToPxInputStream final : public PxInputStream +{ +public: + FileBufToPxInputStream(PxFileBuf& filebuf) : m_filebuf(filebuf) {} + + virtual uint32_t read(void* dest, uint32_t count) + { + return m_filebuf.read(dest, count); + } + +private: + FileBufToPxInputStream& operator=(const FileBufToPxInputStream&); + + PxFileBuf& m_filebuf; +}; + + +class FileBufToPxOutputStream final : public PxOutputStream +{ +public: + FileBufToPxOutputStream(PxFileBuf& filebuf) : m_filebuf(filebuf) {} + + virtual uint32_t write(const void* src, uint32_t count) override + { + return m_filebuf.write(src, count); + } + +private: + FileBufToPxOutputStream& operator=(const FileBufToPxOutputStream&); + + PxFileBuf& m_filebuf; +}; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtPxAssetImpl Implementation +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +ExtPxAssetImpl::ExtPxAssetImpl(const ExtPxAssetDesc& desc, TkFramework& framework) +{ + m_tkAsset = framework.createAsset(desc); + + // count subchunks and reserve memory + uint32_t subchunkCount = 0; + for (uint32_t i = 0; i < desc.chunkCount; ++i) + { + const auto& chunk = desc.pxChunks[i]; + subchunkCount += static_cast<uint32_t>(chunk.subchunkCount); + } + m_subchunks.reserve(subchunkCount); + + // fill chunks and subchunks + m_chunks.resize(desc.chunkCount); + for (uint32_t i = 0; i < desc.chunkCount; ++i) + { + const auto& chunk = desc.pxChunks[i]; + m_chunks[i].isStatic = chunk.isStatic; + m_chunks[i].firstSubchunkIndex = m_subchunks.size(); + m_chunks[i].subchunkCount = chunk.subchunkCount; + for (uint32_t k = 0; k < chunk.subchunkCount; ++k) + { + ExtPxSubchunk subchunk = + { + chunk.subchunks[k].transform, + chunk.subchunks[k].geometry + }; + m_subchunks.pushBack(subchunk); + } + } +} + +ExtPxAssetImpl::ExtPxAssetImpl(TkAsset* tkAsset): + m_tkAsset(tkAsset) +{ + +} + +ExtPxAssetImpl::~ExtPxAssetImpl() +{ + if (m_tkAsset) + { + m_tkAsset->release(); + } +} + +void ExtPxAssetImpl::release() +{ + NVBLASTEXT_DELETE(this, ExtPxAssetImpl); +} + +NV_INLINE bool serializeConvexMesh(const PxConvexMesh& convexMesh, PxCooking& cooking, ExtArray<uint32_t>::type& indicesScratch, + ExtArray<PxHullPolygon>::type hullPolygonsScratch, PxOutputStream& stream) +{ + PxConvexMeshDesc desc; + desc.points.data = convexMesh.getVertices(); + desc.points.count = convexMesh.getNbVertices(); + desc.points.stride = sizeof(PxVec3); + + hullPolygonsScratch.resize(convexMesh.getNbPolygons()); + + uint32_t indexCount = 0; + for (uint32_t i = 0; i < convexMesh.getNbPolygons(); i++) + { + PxHullPolygon polygon; + convexMesh.getPolygonData(i, polygon); + if (polygon.mNbVerts) + { + indexCount = std::max<uint32_t>(indexCount, polygon.mIndexBase + polygon.mNbVerts); + } + } + indicesScratch.resize(indexCount); + + for (uint32_t i = 0; i < convexMesh.getNbPolygons(); i++) + { + PxHullPolygon polygon; + convexMesh.getPolygonData(i, polygon); + for (uint32_t j = 0; j < polygon.mNbVerts; j++) + { + indicesScratch[polygon.mIndexBase + j] = convexMesh.getIndexBuffer()[polygon.mIndexBase + j]; + } + + hullPolygonsScratch[i] = polygon; + } + + desc.indices.count = indexCount; + desc.indices.data = indicesScratch.begin(); + desc.indices.stride = sizeof(uint32_t); + + desc.polygons.count = convexMesh.getNbPolygons(); + desc.polygons.data = hullPolygonsScratch.begin(); + desc.polygons.stride = sizeof(PxHullPolygon); + + return cooking.cookConvexMesh(desc, stream); +} + +bool ExtPxAssetImpl::serialize(PxFileBuf& stream, PxCooking& cooking) const +{ + // Header data + stream.storeDword(ClassID); + stream.storeDword(Version::Current); + + m_tkAsset->serialize(stream); + + // Chunks + const uint32_t chunkCount = m_tkAsset->getChunkCount(); + for (uint32_t i = 0; i < chunkCount; ++i) + { + const ExtPxChunk& chunk = m_chunks[i]; + stream.storeDword(chunk.firstSubchunkIndex); + stream.storeDword(chunk.subchunkCount); + stream.storeDword(chunk.isStatic ? 1 : 0); + } + + stream.storeDword(m_subchunks.size()); + + ExtArray<uint32_t>::type indicesScratch(512); + ExtArray<PxHullPolygon>::type hullPolygonsScratch(512); + ExtHashMap<PxConvexMesh*, uint32_t>::type convexReuseMap; + + FileBufToPxOutputStream outputStream(stream); + for (uint32_t i = 0; i < m_subchunks.size(); ++i) + { + auto& subchunk = m_subchunks[i]; + + // Subchunk transform + stream.storeFloat(subchunk.transform.q.x); stream.storeFloat(subchunk.transform.q.y); stream.storeFloat(subchunk.transform.q.z); stream.storeFloat(subchunk.transform.q.w); + stream.storeFloat(subchunk.transform.p.x); stream.storeFloat(subchunk.transform.p.y); stream.storeFloat(subchunk.transform.p.z); + + // Subchunk scale + stream.storeFloat(subchunk.geometry.scale.scale.x); stream.storeFloat(subchunk.geometry.scale.scale.y); stream.storeFloat(subchunk.geometry.scale.scale.z); + stream.storeFloat(subchunk.geometry.scale.rotation.x); stream.storeFloat(subchunk.geometry.scale.rotation.y); stream.storeFloat(subchunk.geometry.scale.rotation.z); stream.storeFloat(subchunk.geometry.scale.rotation.w); + + auto convexMesh = subchunk.geometry.convexMesh; + NVBLASTEXT_CHECK_ERROR(convexMesh != nullptr, "ExtPxAssetImpl::serialize: subchunk convexMesh is nullptr.", return false); + + auto entry = convexReuseMap.find(convexMesh); + if (entry) + { + stream.storeDword(entry->second); + } + else + { + stream.storeDword(invalidIndex<uint32_t>()); + if (!serializeConvexMesh(*convexMesh, cooking, indicesScratch, hullPolygonsScratch, outputStream)) + { + NVBLASTEXT_LOG_ERROR("ExtPxAssetImpl::serialize: subchunk convexMesh cooking/serialization failed."); + return false; + } + convexReuseMap[convexMesh] = i; + } + } + + return true; +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtPxAsset Static +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +ExtPxAsset* ExtPxAsset::create(const ExtPxAssetDesc& desc, TkFramework& framework) +{ + ExtPxAssetImpl* asset = NVBLASTEXT_NEW(ExtPxAssetImpl)(desc, framework); + return asset; +} + + +Nv::Blast::ExtPxAsset* ExtPxAsset::create(TkAsset* tkAsset) +{ + ExtPxAssetImpl* asset = NVBLASTEXT_NEW(ExtPxAssetImpl)(tkAsset); + + // Don't populate the chunks or subchunks! + + return asset; +} + +ExtPxAsset* ExtPxAsset::deserialize(PxFileBuf& stream, TkFramework& framework, PxPhysics& physics) +{ + ExtPxAssetImpl::DataHeader header; + header.dataType = stream.readDword(); + header.version = stream.readDword(); + NVBLASTEXT_CHECK_ERROR(header.dataType == ExtPxAssetImpl::ClassID, "ExtPxAsset::deserialize: wrong data type in filebuf stream.", return nullptr); + NVBLASTEXT_CHECK_ERROR(header.version == ExtPxAssetImpl::Version::Current, "ExtPxAsset::deserialize: wrong data version in filebuf stream.", return nullptr); + + TkAsset* tkAsset = static_cast<TkAsset*>(framework.deserialize(stream)); + NVBLASTEXT_CHECK_ERROR(tkAsset != nullptr, "ExtPxAsset::deserialize: failed to deserialize TkAsset.", return nullptr); + + ExtPxAssetImpl* asset = NVBLASTEXT_NEW(ExtPxAssetImpl)(tkAsset); + + asset->m_chunks.resize(asset->m_tkAsset->getChunkCount()); + + const uint32_t chunkCount = asset->m_chunks.size(); + for (uint32_t i = 0; i < chunkCount; ++i) + { + ExtPxChunk& chunk = asset->m_chunks[i]; + chunk.firstSubchunkIndex = stream.readDword(); + chunk.subchunkCount = stream.readDword(); + chunk.isStatic = 0 != stream.readDword(); + } + + const uint32_t subchunkCount = stream.readDword(); + asset->m_subchunks.resize(subchunkCount); + + FileBufToPxInputStream inputStream(stream); + for (uint32_t i = 0; i < asset->m_subchunks.size(); ++i) + { + ExtPxSubchunk& subChunk = asset->m_subchunks[i]; + + // Subchunk transform + subChunk.transform.q.x = stream.readFloat(); subChunk.transform.q.y = stream.readFloat(); subChunk.transform.q.z = stream.readFloat(); subChunk.transform.q.w = stream.readFloat(); + subChunk.transform.p.x = stream.readFloat(); subChunk.transform.p.y = stream.readFloat(); subChunk.transform.p.z = stream.readFloat(); + + // Subchunk scale + subChunk.geometry.scale.scale.x = stream.readFloat(); subChunk.geometry.scale.scale.y = stream.readFloat(); subChunk.geometry.scale.scale.z = stream.readFloat(); + subChunk.geometry.scale.rotation.x = stream.readFloat(); subChunk.geometry.scale.rotation.y = stream.readFloat(); subChunk.geometry.scale.rotation.z = stream.readFloat(); subChunk.geometry.scale.rotation.w = stream.readFloat(); + + const uint32_t convexReuseIndex = stream.readDword(); + if (isInvalidIndex(convexReuseIndex)) + { + subChunk.geometry.convexMesh = physics.createConvexMesh(inputStream); + } + else + { + NVBLAST_ASSERT_WITH_MESSAGE(convexReuseIndex < i, "ExtPxAsset::deserialize: wrong convexReuseIndex."); + subChunk.geometry.convexMesh = asset->m_subchunks[convexReuseIndex].geometry.convexMesh; + } + if (!subChunk.geometry.convexMesh) + { + NVBLASTEXT_LOG_ERROR("ExtPxAsset::deserialize: failed to deserialize convex mesh."); + asset->release(); + return nullptr; + } + } + + return asset; +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.h b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.h new file mode 100644 index 0000000..fd95293 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxAssetImpl.h @@ -0,0 +1,126 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXASSETIMPL_H +#define NVBLASTEXTPXASSETIMPL_H + +#include "NvBlastExtPxAsset.h" +#include "NvBlastExtArray.h" +#include "NvBlastExtDefs.h" + + +namespace Nv +{ +namespace Blast +{ + + +using namespace physx; +using namespace general_PxIOStream2; + + +class ExtPxAssetImpl final : public ExtPxAsset +{ + NV_NOCOPY(ExtPxAssetImpl) + +public: + friend class ExtPxAsset; + + /** + Enum which keeps track of the serialized data format. + */ + enum Version + { + /** Initial version */ + Initial, + + // New formats must come before Count. They should be given descriptive names with more information in comments. + + /** The number of serialized formats. */ + Count, + + /** The current version. This should always be Count-1 */ + Current = Count - 1 + }; + + //////// ctor //////// + + ExtPxAssetImpl(const ExtPxAssetDesc& desc, TkFramework& framework); + ExtPxAssetImpl(TkAsset* tkAsset); + + ~ExtPxAssetImpl(); + + + //////// interface //////// + + virtual void release() override; + + virtual const TkAsset& getTkAsset() const override + { + return *m_tkAsset; + } + + virtual uint32_t getChunkCount() const override + { + return m_chunks.size(); + } + + virtual const ExtPxChunk* getChunks() const override + { + return m_chunks.begin(); + } + + virtual uint32_t getSubchunkCount() const override + { + return m_subchunks.size(); + } + + virtual const ExtPxSubchunk* getSubchunks() const override + { + return m_subchunks.begin(); + } + + virtual bool serialize(PxFileBuf& stream, PxCooking& cooking) const override; + + + /* + Get the underlying array for the chunks. Used for serialization. + */ + ExtArray<ExtPxChunk>::type& getChunksArray() { return m_chunks; } + + /* + Get the underlying array for the subchunks. Used for serialization. + */ + ExtArray<ExtPxSubchunk>::type& getSubchunksArray() { return m_subchunks; } + +private: + //////// serialization data //////// + + struct DataHeader + { + uint32_t dataType; + uint32_t version; + }; + + enum { ClassID = NVBLASTEXT_FOURCC('B', 'P', 'X', 'A') }; // Blast PhysX Asset + + + //////// data //////// + + TkAsset* m_tkAsset; + ExtArray<ExtPxChunk>::type m_chunks; + ExtArray<ExtPxSubchunk>::type m_subchunks; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXASSETIMPL_H diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.cpp new file mode 100644 index 0000000..b2d3a47 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.cpp @@ -0,0 +1,294 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtPxFamilyImpl.h" +#include "NvBlastExtPxActorImpl.h" +#include "NvBlastExtPxAssetImpl.h" +#include "NvBlastExtPxListener.h" +#include "NvBlastExtPxManagerImpl.h" + +#include "NvBlastTkFamily.h" +#include "NvBlastTkActor.h" +#include "NvBlastTkJoint.h" + +#include "NvBlastAssert.h" + +#include "PxRigidDynamic.h" +#include "PxScene.h" + +#include <algorithm> + + +namespace Nv +{ +namespace Blast +{ + + +ExtPxFamilyImpl::ExtPxFamilyImpl(ExtPxManagerImpl& manager, TkFamily& tkFamily, const ExtPxAsset& pxAsset) + : m_manager(manager), m_tkFamily(tkFamily), m_pxAsset(pxAsset), m_pxShapeDescTemplate(nullptr), m_pxActorDescTemplate(nullptr), m_isSpawned(false) +{ + m_subchunkShapes.resize(static_cast<uint32_t>(m_pxAsset.getSubchunkCount())); + + userData = nullptr; + + m_manager.registerFamily(*this); +} + +ExtPxFamilyImpl::~ExtPxFamilyImpl() +{ + m_manager.unregisterFamily(*this); + + if (m_isSpawned) + { + m_tkFamily.removeListener(*this); + + auto& actors = m_actorsBuffer; + actors.resize(m_actors.size()); + uint32_t i = 0; + for (auto it = m_actors.getIterator(); !it.done(); ++it) + { + actors[i++] = *it; + } + destroyActors(actors.begin(), actors.size()); + } + + m_tkFamily.release(); +} + +void ExtPxFamilyImpl::release() +{ + NVBLASTEXT_DELETE(this, ExtPxFamilyImpl); +} + +bool ExtPxFamilyImpl::spawn(const physx::PxTransform& pose, const physx::PxVec3& scale, const ExtPxSpawnSettings& settings) +{ + NVBLASTEXT_CHECK_ERROR(!m_isSpawned, "Family spawn: family already spawned. Was spawn() called twice?", return false); + NVBLASTEXT_CHECK_ERROR(settings.scene != nullptr, "Family creation: desc.scene is nullptr", return false); + NVBLASTEXT_CHECK_ERROR(settings.material != nullptr, "Family creation: desc.material is nullptr", return false); + + m_initialTransform = pose; + m_spawnSettings = settings; + + // get current tkActors (usually it's only 1, but it can be already in split state) + const uint32_t actorCount = (uint32_t)m_tkFamily.getActorCount(); + m_newActorsBuffer.resize(actorCount); + m_tkFamily.getActors(m_newActorsBuffer.begin(), actorCount); + + // calc max split count + uint32_t splitMaxActorCount = 0; + for (TkActor* actor : m_newActorsBuffer) + { + splitMaxActorCount = std::max<uint32_t>(splitMaxActorCount, actor->getSplitMaxActorCount()); + } + + // preallocate memory + m_newActorsBuffer.resize(splitMaxActorCount); + m_newActorCreateInfo.resize(splitMaxActorCount); + m_physXActorsBuffer.resize(splitMaxActorCount); + m_physXActorsBuffer.resize(splitMaxActorCount); + m_indicesScratch.reserve(splitMaxActorCount); + + // fill initial actor create info + for (uint32_t i = 0; i < actorCount; ++i) + { + PxActorCreateInfo& pxActorInfo = m_newActorCreateInfo[i]; + pxActorInfo.m_angularVelocity = PxVec3(PxZero); + pxActorInfo.m_linearVelocity = PxVec3(PxZero); + pxActorInfo.m_transform = pose; + pxActorInfo.m_scale = scale; + } + + // create first actors in family + createActors(m_newActorsBuffer.begin(), m_newActorCreateInfo.begin(), actorCount); + + // listen family for new actors + m_tkFamily.addListener(*this); + + m_isSpawned = true; + + return true; +} + +bool ExtPxFamilyImpl::despawn() +{ + NVBLASTEXT_CHECK_ERROR(m_spawnSettings.scene != nullptr, "Family despawn: desc.scene is nullptr", return false); + + auto& actors = m_actorsBuffer; + actors.resize(m_actors.size()); + uint32_t i = 0; + for (auto it = m_actors.getIterator(); !it.done(); ++it) + { + actors[i++] = *it; + } + destroyActors(actors.begin(), actors.size()); + + return true; +} + +void ExtPxFamilyImpl::receive(const TkEvent* events, uint32_t eventCount) +{ + auto& actorsToDelete = m_actorsBuffer; + actorsToDelete.clear(); + uint32_t totalNewActorsCount = 0; + + for (uint32_t i = 0; i < eventCount; ++i) + { + const TkEvent& e = events[i]; + if (e.type == TkEvent::Split) + { + const TkSplitEvent* splitEvent = e.getPayload<TkSplitEvent>(); + + uint32_t newActorsCount = splitEvent->numChildren; + + ExtPxActorImpl* parentActor = nullptr; + PxRigidDynamic* parentPxActor = nullptr; + if (splitEvent->parentData.userData) + { + parentActor = reinterpret_cast<ExtPxActorImpl*>(splitEvent->parentData.userData); + parentPxActor = &parentActor->getPhysXActor(); + } + + for (uint32_t j = totalNewActorsCount; j < totalNewActorsCount + newActorsCount; ++j) + { + m_newActorCreateInfo[j].m_transform = parentPxActor ? parentPxActor->getGlobalPose() : m_initialTransform; + + //TODO: Get the current scale of the actor! + m_newActorCreateInfo[j].m_scale = m_initialScale; + + m_newActorCreateInfo[j].m_linearVelocity = parentPxActor ? parentPxActor->getLinearVelocity() : PxVec3(PxZero); + m_newActorCreateInfo[j].m_angularVelocity = parentPxActor ? parentPxActor->getAngularVelocity() : PxVec3(PxZero); + + m_newActorsBuffer[j] = splitEvent->children[j - totalNewActorsCount]; + } + + totalNewActorsCount += newActorsCount; + + if (parentActor) + { + actorsToDelete.pushBack(parentActor); + } + } + } + + destroyActors(actorsToDelete.begin(), actorsToDelete.size()); + if (totalNewActorsCount > 0) + { + uint32_t cappedNewActorsCount = totalNewActorsCount; + const uint32_t actorCountLimit = m_manager.getActorCountLimit(); + const uint32_t totalActorCount = m_manager.getPxActorCount(); + if (actorCountLimit > 0 && cappedNewActorsCount + totalActorCount > actorCountLimit) + { + cappedNewActorsCount = actorCountLimit > totalActorCount ? actorCountLimit - totalActorCount : 0; + } + createActors(m_newActorsBuffer.begin(), m_newActorCreateInfo.begin(), cappedNewActorsCount); + m_culledActors.reserve(m_culledActors.size() + totalNewActorsCount - cappedNewActorsCount); + for (uint32_t i = cappedNewActorsCount; i < totalNewActorsCount; ++i) + { + m_culledActors.pushBack(m_newActorsBuffer[i]); + } + totalNewActorsCount = cappedNewActorsCount; // In case it's used below + } + + for (uint32_t i = 0; i < eventCount; ++i) + { + const TkEvent& e = events[i]; + if (e.type == TkEvent::JointUpdate) + { + const TkJointUpdateEvent* jointEvent = e.getPayload<TkJointUpdateEvent>(); + NVBLAST_ASSERT(jointEvent->joint); + TkJoint& joint = *jointEvent->joint; + + switch (jointEvent->subtype) + { + case TkJointUpdateEvent::External: + m_manager.createJoint(joint); + break; + case TkJointUpdateEvent::Changed: + m_manager.updateJoint(joint); + break; + case TkJointUpdateEvent::Unreferenced: + m_manager.destroyJoint(joint); + joint.release(); + break; + } + } + } +} + +void ExtPxFamilyImpl::createActors(TkActor** tkActors, const PxActorCreateInfo* pxActorInfos, uint32_t count) +{ + auto actorsToAdd = m_physXActorsBuffer.begin(); + for (uint32_t i = 0; i < count; ++i) + { + ExtPxActorImpl* actor = NVBLASTEXT_NEW(ExtPxActorImpl)(this, tkActors[i], pxActorInfos[i]); + m_actors.insert(actor); + actorsToAdd[i] = &actor->getPhysXActor(); + dispatchActorCreated(*actor); + + // Handle incomplete joints + auto e = m_manager.m_incompleteJointMultiMap.find(tkActors[i]); + if (e != nullptr) + { + ExtArray<TkJoint*>::type joints = e->second; // Copying the array + m_manager.m_incompleteJointMultiMap.erase(tkActors[i]); + for (uint32_t j = 0; j < joints.size(); ++j) + { + m_manager.updateJoint(*joints[j]); + } + } + } + m_spawnSettings.scene->addActors(actorsToAdd, static_cast<uint32_t>(count)); +} + +void ExtPxFamilyImpl::destroyActors(ExtPxActor** actors, uint32_t count) +{ + auto pxActorsToRemove = m_physXActorsBuffer.begin(); + for (uint32_t i = 0; i < count; ++i) + { + pxActorsToRemove[i] = &actors[i]->getPhysXActor(); + } + m_spawnSettings.scene->removeActors(pxActorsToRemove, static_cast<uint32_t>(count)); + + for (uint32_t i = 0; i < count; ++i) + { + ExtPxActorImpl* actor = (ExtPxActorImpl*)actors[i]; + m_actors.erase(actor); + dispatchActorDestroyed(*actor); + NVBLASTEXT_DELETE(actor, ExtPxActorImpl); + } +} + +void ExtPxFamilyImpl::dispatchActorCreated(ExtPxActor& actor) +{ + for (ExtPxListener* listener : m_listeners) + listener->onActorCreated(*this, actor); + m_manager.dispatchActorCreated(*this, actor); +} + +void ExtPxFamilyImpl::dispatchActorDestroyed(ExtPxActor& actor) +{ + for (ExtPxListener* listener : m_listeners) + listener->onActorDestroyed(*this, actor); + m_manager.dispatchActorDestroyed(*this, actor); +} + +void ExtPxFamilyImpl::postSplitUpdate() +{ + for (auto actor : m_culledActors) + { + actor->release(); + } + m_culledActors.resize(0); +} + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.h b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.h new file mode 100644 index 0000000..5c90346 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxFamilyImpl.h @@ -0,0 +1,168 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXFAMILYIMPL_H +#define NVBLASTEXTPXFAMILYIMPL_H + +#include "NvBlastExtPxFamily.h" +#include "NvBlastExtArray.h" +#include "NvBlastExtHashSet.h" +#include "PxTransform.h" +#include "NvBlastTkEvent.h" + + +using namespace physx; + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class ExtPxManagerImpl; +class ExtPxActorImpl; +struct PxActorCreateInfo; + + +class ExtPxFamilyImpl final : public ExtPxFamily, TkEventListener +{ + NV_NOCOPY(ExtPxFamilyImpl) + +public: + friend ExtPxActorImpl; + friend ExtPxManagerImpl; + + //////// ctor //////// + + ExtPxFamilyImpl(ExtPxManagerImpl& manager, TkFamily& tkFamily, const ExtPxAsset& pxAsset); + ~ExtPxFamilyImpl(); + + virtual void release() override; + + + //////// ExtPxFamily interface //////// + +// virtual bool spawn(const PxTransform& pose, const ExtPxSpawnSettings& settings) override; + virtual bool spawn(const physx::PxTransform& pose, const physx::PxVec3& scale, const ExtPxSpawnSettings& settings) override; + virtual bool despawn() override; + + + virtual uint32_t getActorCount() const override + { + return m_actors.size(); + } + + virtual uint32_t getActors(ExtPxActor** buffer, uint32_t bufferSize) const override + { + uint32_t index = 0; + for (auto it = const_cast<ExtPxFamilyImpl*>(this)->m_actors.getIterator(); !it.done() && index < bufferSize; ++it) + { + buffer[index++] = *it; + } + return index; + } + + virtual TkFamily& getTkFamily() const override + { + return m_tkFamily; + } + + virtual const physx::PxShape* const* getSubchunkShapes() const override + { + return m_subchunkShapes.begin(); + } + + virtual const ExtPxAsset& getPxAsset() const override + { + return m_pxAsset; + } + + virtual void setMaterial(PxMaterial& material) override + { + m_spawnSettings.material = &material; + } + + virtual void setPxShapeDescTemplate(const ExtPxShapeDescTemplate* pxShapeDesc) override + { + m_pxShapeDescTemplate = pxShapeDesc; + } + + virtual const ExtPxShapeDescTemplate* getPxShapeDescTemplate() const override + { + return m_pxShapeDescTemplate; + } + + virtual void setPxActorDesc(const ExtPxActorDescTemplate* pxActorDesc) override + { + m_pxActorDescTemplate = pxActorDesc; + } + + virtual const ExtPxActorDescTemplate* getPxActorDesc() const override + { + return m_pxActorDescTemplate; + } + + virtual void subscribe(ExtPxListener& listener) override + { + m_listeners.pushBack(&listener); + } + + virtual void unsubscribe(ExtPxListener& listener) override + { + m_listeners.findAndReplaceWithLast(&listener); + } + + virtual void postSplitUpdate() override; + + //////// TkEventListener interface //////// + + virtual void receive(const TkEvent* events, uint32_t eventCount) override; + + + //////// events dispatch //////// + + void dispatchActorCreated(ExtPxActor& actor); + void dispatchActorDestroyed(ExtPxActor& actor); + + +private: + //////// private methods //////// + + void createActors(TkActor** tkActors, const PxActorCreateInfo* pxActorInfos, uint32_t count); + void destroyActors(ExtPxActor** actors, uint32_t count); + + //////// data //////// + + ExtPxManagerImpl& m_manager; + TkFamily& m_tkFamily; + const ExtPxAsset& m_pxAsset; + ExtPxSpawnSettings m_spawnSettings; + const ExtPxShapeDescTemplate* m_pxShapeDescTemplate; + const ExtPxActorDescTemplate* m_pxActorDescTemplate; + bool m_isSpawned; + PxTransform m_initialTransform; + PxVec3 m_initialScale; + ExtHashSet<ExtPxActor*>::type m_actors; + ExtArray<TkActor*>::type m_culledActors; + ExtInlineArray<ExtPxListener*, 4>::type m_listeners; + ExtArray<PxShape*>::type m_subchunkShapes; + ExtArray<TkActor*>::type m_newActorsBuffer; + ExtArray<PxActorCreateInfo>::type m_newActorCreateInfo; + ExtArray<PxActor*>::type m_physXActorsBuffer; + ExtArray<ExtPxActor*>::type m_actorsBuffer; + ExtArray<uint32_t>::type m_indicesScratch; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXFAMILYIMPL_H diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.cpp b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.cpp new file mode 100644 index 0000000..42266ee --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.cpp @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtPxManagerImpl.h" +#include "NvBlastExtPxAssetImpl.h" +#include "NvBlastExtPxActorImpl.h" +#include "NvBlastExtPxFamilyImpl.h" + +#include "NvBlastAssert.h" + +#include "NvBlastTkActor.h" +#include "NvBlastTkFamily.h" +#include "NvBlastTkGroup.h" +#include "NvBlastTkJoint.h" + +#include "PxRigidDynamic.h" +#include "PxJoint.h" + + +namespace Nv +{ +namespace Blast +{ + + +ExtPxManager* ExtPxManager::create(PxPhysics& physics, TkFramework& framework, ExtPxCreateJointFunction createFn, bool useUserData) +{ + return NVBLASTEXT_NEW(ExtPxManagerImpl)(physics, framework, createFn, useUserData); +} + +void ExtPxManagerImpl::release() +{ + NVBLASTEXT_DELETE(this, ExtPxManagerImpl); +} + +ExtPxFamily* ExtPxManagerImpl::createFamily(const ExtPxFamilyDesc& desc) +{ + NVBLASTEXT_CHECK_ERROR(desc.pxAsset != nullptr, "Family creation: pxAsset is nullptr.", return nullptr); + + // create tk family + TkActorDesc tkActorDesc; + (&tkActorDesc)->NvBlastActorDesc::operator=(desc.actorDesc); + tkActorDesc.asset = &desc.pxAsset->getTkAsset(); + TkActor* actor = m_framework.createActor(tkActorDesc); + NVBLASTEXT_CHECK_ERROR(actor != nullptr, "Family creation: tk actor creation failed.", return nullptr); + + ExtPxFamilyImpl* family = NVBLASTEXT_NEW(ExtPxFamilyImpl)(*this, actor->getFamily(), *desc.pxAsset); + + if (desc.group) + { + desc.group->addActor(*actor); + } + + return family; +} + +bool ExtPxManagerImpl::createJoint(TkJoint& joint) +{ + if (!joint.userData && m_createJointFn) + { + const TkJointData data = joint.getData(); + ExtPxActorImpl* pxActor0 = data.actors[0] != nullptr ? reinterpret_cast<ExtPxActorImpl*>(data.actors[0]->userData) : nullptr; + ExtPxActorImpl* pxActor1 = data.actors[1] != nullptr ? reinterpret_cast<ExtPxActorImpl*>(data.actors[1]->userData) : nullptr; + NVBLAST_ASSERT(pxActor0 || pxActor1); + PxTransform lf0(data.attachPositions[0]); + PxTransform lf1(data.attachPositions[1]); + PxJoint* pxJoint = m_createJointFn(pxActor0, lf0, pxActor1, lf1, m_physics, joint); + if (pxJoint) + { + joint.userData = pxJoint; + return true; + } + } + return false; +} + +void ExtPxManagerImpl::updateJoint(TkJoint& joint) +{ + if (joint.userData) + { + const TkJointData& data = joint.getData(); + ExtPxActorImpl* pxActors[2]; + for (int i = 0; i < 2; ++i) + { + if (data.actors[i] != nullptr) + { + pxActors[i] = reinterpret_cast<ExtPxActorImpl*>(data.actors[i]->userData); + if (pxActors[i] == nullptr) + { + ExtArray<TkJoint*>::type& joints = m_incompleteJointMultiMap[data.actors[i]]; + NVBLAST_ASSERT(joints.find(&joint) == joints.end()); + joints.pushBack(&joint); + return; // Wait until the TkActor is received to create this joint + } + } + else + { + pxActors[i] = nullptr; + } + } + NVBLAST_ASSERT(pxActors[0] || pxActors[1]); + PxJoint* pxJoint = reinterpret_cast<PxJoint*>(joint.userData); + pxJoint->setActors(pxActors[0] ? &pxActors[0]->getPhysXActor() : nullptr, pxActors[1] ? &pxActors[1]->getPhysXActor() : nullptr); + } +} + +void ExtPxManagerImpl::destroyJoint(TkJoint& joint) +{ + if (joint.userData) + { + PxJoint* pxJoint = reinterpret_cast<PxJoint*>(joint.userData); + pxJoint->release(); + joint.userData = nullptr; + } +} + + + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.h b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.h new file mode 100644 index 0000000..1f5e510 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/physics/NvBlastExtPxManagerImpl.h @@ -0,0 +1,202 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTPXMANAGERIMPL_H +#define NVBLASTEXTPXMANAGERIMPL_H + +#include "NvBlastExtPxManager.h" +#include "NvBlastExtArray.h" +#include "NvBlastExtHashMap.h" +#include "NvBlastExtPxListener.h" +#include "NvBlastExtPxFamily.h" + +#include "PxRigidDynamic.h" + + +using namespace physx; + + +namespace Nv +{ +namespace Blast +{ + +// Forward declarations +class TkActor; + +class ExtPxManagerImpl final : public ExtPxManager +{ + NV_NOCOPY(ExtPxManagerImpl) + +public: + friend class ExtPxActorImpl; + friend class ExtPxFamilyImpl; + + ExtPxManagerImpl(PxPhysics& physics, TkFramework&framework, ExtPxCreateJointFunction createFn, bool usePxUserData) + : m_physics(physics), m_framework(framework), m_createJointFn(createFn), m_usePxUserData(usePxUserData), m_actorCountLimit(0) + { + } + + ~ExtPxManagerImpl() + { + } + + virtual void release() override; + + + //////// interface //////// + + virtual ExtPxFamily* createFamily(const ExtPxFamilyDesc& desc) override; + + virtual bool createJoint(TkJoint& joint) override; + + virtual void destroyJoint(TkJoint& joint) override; + + virtual void setCreateJointFunction(ExtPxCreateJointFunction createFn) override + { + m_createJointFn = createFn; + } + + virtual uint32_t getFamilyCount() const override + { + return m_tkFamiliesMap.size(); + } + + virtual uint32_t getFamilies(ExtPxFamily** buffer, uint32_t bufferSize) const override + { + uint32_t index = 0; + for (auto it = const_cast<ExtPxManagerImpl*>(this)->m_tkFamiliesMap.getIterator(); !it.done() && index < bufferSize; ++it) + { + buffer[index++] = it->second; + } + return index; + } + + virtual ExtPxFamily* getFamilyFromTkFamily(TkFamily& family) const override + { + auto entry = m_tkFamiliesMap.find(&family); + return entry != nullptr ? entry->second : nullptr; + } + + virtual ExtPxActor* getActorFromPhysXActor(const PxRigidDynamic& pxActor) const override + { + auto it = m_physXActorsMap.find(&pxActor); + return it != nullptr ? it->second : nullptr; + } + + virtual PxPhysics& getPhysics() const override + { + return m_physics; + } + + virtual TkFramework& getFramework() const override + { + return m_framework; + } + + virtual bool isPxUserDataUsed() const override + { + return m_usePxUserData; + } + + virtual void subscribe(ExtPxListener& listener) override + { + m_listeners.pushBack(&listener); + } + + virtual void unsubscribe(ExtPxListener& listener) override + { + m_listeners.findAndReplaceWithLast(&listener); + } + + virtual void setActorCountLimit(uint32_t limit) override + { + m_actorCountLimit = limit; + } + + virtual uint32_t getActorCountLimit() override + { + return m_actorCountLimit; + } + + virtual uint32_t getPxActorCount() const override + { + return m_physXActorsMap.size(); + } + + + //////// internal public methods //////// + + void registerActor(PxRigidDynamic* pxActor, ExtPxActor* actor) + { + if (m_usePxUserData) + { + pxActor->userData = actor; + } + m_physXActorsMap[pxActor] = actor; + } + + void unregisterActor(PxRigidDynamic* pxActor) + { + if (m_usePxUserData) + { + pxActor->userData = nullptr; + } + m_physXActorsMap.erase(pxActor); + } + + void registerFamily(ExtPxFamily& family) + { + m_tkFamiliesMap[&family.getTkFamily()] = &family; + } + + void unregisterFamily(ExtPxFamily& family) + { + m_tkFamiliesMap.erase(&family.getTkFamily()); + } + + void updateJoint(TkJoint& joint); + + + //////// events dispatch //////// + + void dispatchActorCreated(ExtPxFamily& family, ExtPxActor& actor) + { + for (ExtPxListener* listener : m_listeners) + listener->onActorCreated(family, actor); + } + + void dispatchActorDestroyed(ExtPxFamily& family, ExtPxActor&actor) + { + for (ExtPxListener* listener : m_listeners) + listener->onActorDestroyed(family, actor); + } + + +private: + + //////// data //////// + + PxPhysics& m_physics; + TkFramework& m_framework; + ExtPxCreateJointFunction m_createJointFn; + bool m_usePxUserData; + ExtInlineArray<ExtPxListener*, 8>::type m_listeners; + ExtHashMap<const PxRigidDynamic*, ExtPxActor*>::type m_physXActorsMap; + ExtHashMap<TkFamily*, ExtPxFamily*>::type m_tkFamiliesMap; + ExtHashMap<TkActor*, ExtArray<TkJoint*>::type >::type m_incompleteJointMultiMap; + uint32_t m_actorCountLimit; +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTPXMANAGERIMPL_H diff --git a/NvBlast/sdk/extensions/physx/source/sync/NvBlastExtSync.cpp b/NvBlast/sdk/extensions/physx/source/sync/NvBlastExtSync.cpp new file mode 100644 index 0000000..5f018d9 --- /dev/null +++ b/NvBlast/sdk/extensions/physx/source/sync/NvBlastExtSync.cpp @@ -0,0 +1,235 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + + +#include "NvBlastExtSync.h" +#include "NvBlastAssert.h" +#include "NvBlast.h" +#include "NvBlastExtDefs.h" +#include "NvBlastExtPxManager.h" +#include "NvBlastExtPxFamily.h" +#include "NvBlastExtPxActor.h" +#include "PxRigidDynamic.h" + +#include <chrono> +using namespace std::chrono; + +namespace Nv +{ +namespace Blast +{ + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtSyncImpl Definition +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +class ExtSyncImpl : public ExtSync +{ + NV_NOCOPY(ExtSyncImpl) + +public: + //////// ctor //////// + + ExtSyncImpl(); + + ~ExtSyncImpl(); + + + //////// TkEventListener interface //////// + + virtual void receive(const TkEvent* events, uint32_t eventCount) override; + + + //////// ExtSync interface //////// + + virtual void release() override; + + virtual void syncFamily(const TkFamily& family) override; + virtual void syncFamily(const ExtPxFamily& family) override; + + virtual uint32_t getSyncBufferSize() const override; + virtual void acquireSyncBuffer(const ExtSyncEvent*const*& buffer, uint32_t& size) const override; + virtual void releaseSyncBuffer() override; + + virtual void applySyncBuffer(TkFramework& framework, const ExtSyncEvent** buffer, uint32_t size, TkGroup* groupForNewActors, ExtPxManager* manager) override; + + +private: + //////// data //////// + + std::vector<ExtSyncEvent*> m_syncEvents; +}; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtSyncEvent Implementation +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void ExtSyncEvent::release() +{ + NVBLASTEXT_DELETE(this, ExtSyncEvent); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// ExtSyncImpl Implementation +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +ExtSync* ExtSync::create() +{ + return NVBLASTEXT_NEW(ExtSyncImpl) (); +} + +void ExtSyncImpl::release() +{ + NVBLASTEXT_DELETE(this, ExtSyncImpl); +} + +ExtSyncImpl::ExtSyncImpl() +{ +} + +ExtSyncImpl::~ExtSyncImpl() +{ + releaseSyncBuffer(); +} + +void ExtSyncImpl::receive(const TkEvent* events, uint32_t eventCount) +{ + for (uint32_t i = 0; i < eventCount; ++i) + { + const TkEvent& tkEvent = events[i]; + if (tkEvent.type == TkEvent::FractureCommand) + { + const TkFractureCommands* fracEvent = tkEvent.getPayload<TkFractureCommands>(); + ExtSyncEventFracture* e = NVBLASTEXT_NEW(ExtSyncEventFracture) (); + e->timestamp = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); + e->familyID = fracEvent->tkActorData.family->getID(); + e->bondFractures.resize(fracEvent->buffers.bondFractureCount); + e->chunkFractures.resize(fracEvent->buffers.chunkFractureCount); + memcpy(e->bondFractures.data(), fracEvent->buffers.bondFractures, e->bondFractures.size() * sizeof(NvBlastBondFractureData)); + memcpy(e->chunkFractures.data(), fracEvent->buffers.chunkFractures, e->chunkFractures.size() * sizeof(NvBlastChunkFractureData)); + m_syncEvents.push_back(e); + } + } +} + +void ExtSyncImpl::syncFamily(const TkFamily& family) +{ + ExtSyncEventFamilySync* e = NVBLASTEXT_NEW(ExtSyncEventFamilySync) (); + e->timestamp = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); + e->familyID = family.getID(); + const NvBlastFamily* familyLL = family.getFamilyLL(); + const uint32_t size = NvBlastFamilyGetSize(familyLL, NvBlastTkFrameworkGet()->getLogFn()); + e->family = std::vector<char>((char*)familyLL, (char*)familyLL + size); + m_syncEvents.push_back(e); +} + +void ExtSyncImpl::syncFamily(const ExtPxFamily& family) +{ + const TkFamily& tkFamily = family.getTkFamily(); + + syncFamily(tkFamily); + + ExtSyncEventPhysicsSync* e = NVBLASTEXT_NEW(ExtSyncEventPhysicsSync) (); + e->timestamp = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); + e->familyID = tkFamily.getID(); + std::vector<ExtPxActor*> actors(family.getActorCount()); + family.getActors(actors.data(), static_cast<uint32_t>(actors.size())); + e->data.reserve(actors.size()); + for (ExtPxActor* actor : actors) + { + ExtSyncEventPhysicsSync::ActorData data; + data.transform = actor->getPhysXActor().getGlobalPose(); + data.actorIndex = actor->getTkActor().getIndex(); + e->data.push_back(data); + } + + m_syncEvents.push_back(e); +} + +uint32_t ExtSyncImpl::getSyncBufferSize() const +{ + return static_cast<uint32_t>(m_syncEvents.size()); +} + +void ExtSyncImpl::acquireSyncBuffer(const ExtSyncEvent* const*& buffer, uint32_t& size) const +{ + buffer = m_syncEvents.data(); + size = static_cast<uint32_t>(m_syncEvents.size()); +} + +void ExtSyncImpl::releaseSyncBuffer() +{ + for (uint32_t i = 0; i < m_syncEvents.size(); ++i) + { + NVBLASTEXT_DELETE(m_syncEvents[i], ExtSyncEvent); + } + m_syncEvents.clear(); +} + +void ExtSyncImpl::applySyncBuffer(TkFramework& framework, const ExtSyncEvent** buffer, uint32_t size, TkGroup* groupForNewActors, ExtPxManager* manager) +{ + const TkType* familyType = framework.getType(TkTypeIndex::Family); + NVBLAST_ASSERT(familyType); + + for (uint32_t i = 0; i < size; ++i) + { + const ExtSyncEvent* e = buffer[i]; + const NvBlastID& id = e->familyID; + TkIdentifiable* object = framework.findObjectByID(id); + if (object && object->getType() == *familyType) + { + TkFamily* family = static_cast<TkFamily*>(object); + + if (e->type == ExtSyncEventFracture::EVENT_TYPE) + { + const ExtSyncEventFracture* fractureEvent = e->getEvent<ExtSyncEventFracture>(); + const NvBlastFractureBuffers commands = + { + static_cast<uint32_t>(fractureEvent->bondFractures.size()), + static_cast<uint32_t>(fractureEvent->chunkFractures.size()), + const_cast<NvBlastBondFractureData*>(fractureEvent->bondFractures.data()), + const_cast<NvBlastChunkFractureData*>(fractureEvent->chunkFractures.data()) + }; + family->applyFracture(&commands); + } + else if (e->type == ExtSyncEventFamilySync::EVENT_TYPE) + { + const ExtSyncEventFamilySync* familyEvent = e->getEvent<ExtSyncEventFamilySync>(); + family->reinitialize((NvBlastFamily*)familyEvent->family.data(), groupForNewActors); + } + else if (e->type == ExtSyncEventPhysicsSync::EVENT_TYPE && manager) + { + const ExtSyncEventPhysicsSync* physicsEvent = e->getEvent<ExtSyncEventPhysicsSync>(); + ExtPxFamily* pxFamily = manager->getFamilyFromTkFamily(*family); + if (pxFamily) + { + std::vector<ExtPxActor*> actors(pxFamily->getActorCount()); + pxFamily->getActors(actors.data(), static_cast<uint32_t>(actors.size())); + + for (auto data : physicsEvent->data) + { + for (ExtPxActor* physicsaActor : actors) + { + if (data.actorIndex == physicsaActor->getTkActor().getIndex()) + { + physicsaActor->getPhysXActor().setGlobalPose(data.transform); + } + } + } + } + } + } + } +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationInterface.h b/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationInterface.h new file mode 100644 index 0000000..e4f27e0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationInterface.h @@ -0,0 +1,38 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +/* + Include this file to get the C interface to serialization for all asset types (LL, Tk and Ext) +*/ +#pragma once +#include <ostream> +#include "NvBlastTkAsset.h" +#include "NvBlastExtPxAsset.h" + +#include "NvBlastExtSerializationLLInterface.h" + +namespace physx +{ + class PxPhysics; +} + +NVBLAST_API void setPhysXSDK(physx::PxPhysics* physXSDK); + +NVBLAST_API Nv::Blast::TkAsset* deserializeTkAsset(const unsigned char* input, uint32_t size); +NVBLAST_API Nv::Blast::TkAsset* deserializeTkAssetFromStream(std::istream &inputStream); +NVBLAST_API bool serializeTkAssetIntoStream(const Nv::Blast::TkAsset *asset, std::ostream &outputStream); +NVBLAST_API bool serializeTkAssetIntoNewBuffer(const Nv::Blast::TkAsset *asset, unsigned char **outBuffer, uint32_t &outSize); +NVBLAST_API bool serializeTkAssetIntoExistingBuffer(const Nv::Blast::TkAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize); + +NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAsset(const unsigned char* input, uint32_t size); +NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAssetFromStream(std::istream &inputStream); +NVBLAST_API bool serializeExtPxAssetIntoStream(const Nv::Blast::ExtPxAsset *asset, std::ostream &outputStream); +NVBLAST_API bool serializeExtPxAssetIntoNewBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char **outBuffer, uint32_t &outSize); +NVBLAST_API bool serializeExtPxAssetIntoExistingBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize); diff --git a/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationLLInterface.h b/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationLLInterface.h new file mode 100644 index 0000000..d21c7ca --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/include/NvBlastExtSerializationLLInterface.h @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +/* +Include this file to access the C API for serialization, for Low Level assets only. + +For Serialization of Tk and Ext assets, include only NvBlastExtSerializationInterface.h, which will include this file as well. + +*/ +#pragma once +#include <ostream> +#include "NvBlastPreprocessor.h" +#include "NvBlastTypes.h" + +#include "NvBlastExtGlobals.h" + +/* + Set a global NvBlastAlloc signature allocation function that the deserialization will use when required. + + NOTE: This will NOT be used when using the combined serialization library, as it will use the TkFramework's allocation and logging +*/ +NVBLAST_API void setAllocator(NvBlastExtAlloc alloc); + +/* + Set a global NvBlastLog signature allocation function that the library will use when required. + + NOTE: This will NOT be used when using the combined serialization library, as it will use the TkFramework's allocation and logging +*/ +NVBLAST_API void setLog(NvBlastLog log); + + +NVBLAST_API NvBlastAsset* deserializeAsset(const unsigned char* input, uint32_t size); +NVBLAST_API NvBlastAsset* deserializeAssetFromStream(std::istream &inputStream); +NVBLAST_API bool serializeAssetIntoStream(const NvBlastAsset *asset, std::ostream &outputStream); +NVBLAST_API bool serializeAssetIntoNewBuffer(const NvBlastAsset *asset, unsigned char **outBuffer, uint32_t &outSize); +NVBLAST_API bool serializeAssetIntoExistingBuffer(const NvBlastAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize); diff --git a/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn b/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn new file mode 100644 index 0000000..49b1dbb --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn @@ -0,0 +1,162 @@ +@0xaffe4498f275ee58; + +using Cxx = import "/capnp/c++.capnp"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct Asset +{ + header @0 :NvBlastDataBlock; + + iD @1 :UUID; + + chunkCount @2 :UInt32; + + graph @3 :NvBlastSupportGraph; + + leafChunkCount @4 :UInt32; + + firstSubsupportChunkIndex @5 :UInt32; + + bondCount @6 :UInt32; + + chunks @7: List(NvBlastChunk); + + bonds @8: List(NvBlastBond); + + subtreeLeafChunkCounts @9: List(UInt32); + + chunkToGraphNodeMap @10: List(UInt32); +} + +struct TkAsset +{ + assetLL @0 :Asset; + + jointDescs @1 :List(TkAssetJointDesc); + +} + +struct ExtPxAsset +{ + asset @0 :TkAsset; + chunks @1 :List(ExtPxChunk); + subchunks @2 :List(ExtPxSubchunk); +} + +struct ExtPxChunk +{ + firstSubchunkIndex @0 :UInt32; + subchunkCount @1 :UInt32; + isStatic @2 :Bool; +} + +struct ExtPxSubchunk +{ + transform @0 :PxTransform; + geometry @1 :PxConvexMeshGeometry; +} + +struct PxConvexMeshGeometry +{ + scale @0 :PxMeshScale; + convexMesh @1 :Data; + meshFlags @2 :UInt8; + + enum Type + { + eSPHERE @0; + ePLANE @1; + eCAPSULE @2; + eBOX @3; + eCONVEXMESH @4; + eTRIANGLEMESH @5; + eHEIGHTFIELD @6; + } + + type @3 :Type; +} + +struct NvBlastDataBlock +{ + enum Type + { + assetDataBlock @0; + instanceDataBlock @1; + } + + dataType @0 :Type; + + formatVersion @1 :UInt32; + + size @2 :UInt32; +} + +struct NvBlastChunk +{ + centroid @0 :List(Float32); + + volume @1 :Float32; + + parentChunkIndex @2 :UInt32; + firstChildIndex @3 :UInt32; + childIndexStop @4 :UInt32; + userData @5 :UInt32; +} + +struct NvBlastBond +{ + normal @0 :List(Float32); + area @1 :Float32; + centroid @2 :List(Float32); + userData @3 :UInt32; +} + +struct TkAssetJointDesc +{ + nodeIndices @0 :List(UInt32); + attachPositions @1 :List(PxVec3); +} + +struct PxVec3 +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; +} + +struct PxQuat +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; + w @3 :Float32; +} + +struct PxMeshScale +{ + scale @0 :PxVec3; + rotation @1 :PxQuat; +} + +struct PxTransform +{ + q @0 :PxQuat; + p @1 :PxVec3; +} + +struct NvBlastSupportGraph +{ + nodeCount @0 : UInt32; + + chunkIndices @1 : List(UInt32); + adjacencyPartition @2 : List(UInt32); + adjacentNodeIndices @3 : List(UInt32); + adjacentBondIndices @4 : List(UInt32); +} + +struct UUID +{ + value @0 : Data; +} + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp new file mode 100644 index 0000000..8d035fc --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp @@ -0,0 +1,187 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "AssetDTO.h" +#include "NvBlastIDDTO.h" +#include "NvBlastChunkDTO.h" +#include "NvBlastBondDTO.h" +#include "NvBlastAsset.h" +#include "NvBlastExtSerializationLLImpl.h" +#include "NvBlastExtGlobals.h" + +#if !defined(BLAST_LL_ALLOC) +#include "NvBlastExtAllocator.h" +#endif + +namespace Nv +{ + namespace Blast + { + bool AssetDTO::serialize(Nv::Blast::Serialization::Asset::Builder builder, const Nv::Blast::Asset * poco) + { + NvBlastIDDTO::serialize(builder.initID(), &poco->m_ID); + + builder.setLeafChunkCount(poco->m_leafChunkCount); + + builder.setFirstSubsupportChunkIndex(poco->m_firstSubsupportChunkIndex); + + capnp::List<Nv::Blast::Serialization::NvBlastChunk>::Builder chunks = builder.initChunks(poco->m_chunkCount); + + builder.setChunkCount(poco->m_chunkCount); + + NVBLAST_ASSERT_WITH_MESSAGE(builder.getChunkCount() == poco->m_chunkCount, "WTF"); + + for (uint32_t i = 0; i < poco->m_chunkCount; i++) + { + NvBlastChunk& chunk = poco->getChunks()[i]; + + NvBlastChunkDTO::serialize(chunks[i], &chunk); + } + + NVBLAST_ASSERT_WITH_MESSAGE(builder.getChunkCount() == poco->m_chunkCount, "WTF"); + + capnp::List<Nv::Blast::Serialization::NvBlastBond>::Builder bonds = builder.initBonds(poco->m_bondCount); + + builder.setBondCount(poco->m_bondCount); + + for (uint32_t i = 0; i < poco->m_bondCount; i++) + { + NvBlastBond& bond = poco->getBonds()[i]; + + NvBlastBondDTO::serialize(bonds[i], &bond); + } + + kj::ArrayPtr<uint32_t> stlcArray(poco->getSubtreeLeafChunkCounts(), poco->m_chunkCount); + builder.initSubtreeLeafChunkCounts(poco->m_chunkCount); + builder.setSubtreeLeafChunkCounts(stlcArray); + + kj::ArrayPtr<uint32_t> ctgnArray(poco->getChunkToGraphNodeMap(), poco->m_chunkCount); + builder.setChunkToGraphNodeMap(ctgnArray); + + Nv::Blast::Serialization::NvBlastSupportGraph::Builder graphBulder = builder.initGraph(); + + graphBulder.setNodeCount(poco->m_graph.m_nodeCount); + + uint32_t* ciPtr = poco->m_graph.getChunkIndices(); + + kj::ArrayPtr<const uint32_t> ciArray(ciPtr, poco->m_graph.m_nodeCount); + graphBulder.setChunkIndices(ciArray); + + kj::ArrayPtr<const uint32_t> adjPart(poco->m_graph.getAdjacencyPartition(), poco->m_graph.m_nodeCount + 1); + graphBulder.setAdjacencyPartition(adjPart); + + NVBLAST_ASSERT(graphBulder.getAdjacencyPartition().size() == poco->m_graph.m_nodeCount + 1); + + kj::ArrayPtr<const uint32_t> nodeIndices(poco->m_graph.getAdjacentNodeIndices(), poco->m_bondCount * 2); + graphBulder.setAdjacentNodeIndices(nodeIndices); + + NVBLAST_ASSERT(graphBulder.getAdjacentNodeIndices().size() == poco->m_bondCount * 2); + + kj::ArrayPtr<const uint32_t> bondIndices(poco->m_graph.getAdjacentBondIndices(), poco->m_bondCount * 2); + graphBulder.setAdjacentBondIndices(bondIndices); + + return true; + } + + Nv::Blast::Asset* AssetDTO::deserialize(Nv::Blast::Serialization::Asset::Reader reader) + { + NvBlastID EmptyId = {}; + + NvBlastExtAlloc allocFn = gAlloc; + NvBlastLog logFn = gLog; + +#if !defined(BLAST_LL_ALLOC) + allocFn = ExtAllocator::alignedAlloc16; + logFn = NvBlastTkFrameworkGet()->getLogFn(); +#endif + + void* mem = allocFn(reader.totalSize().wordCount * sizeof(uint64_t)); + + auto asset = Nv::Blast::initializeAsset(mem, EmptyId, reader.getChunkCount(), reader.getGraph().getNodeCount(), reader.getLeafChunkCount(), reader.getFirstSubsupportChunkIndex(), reader.getBondCount(), + logFn); + + bool result = deserializeInto(reader, asset); + + return result ? asset : nullptr; + } + + bool AssetDTO::deserializeInto(Nv::Blast::Serialization::Asset::Reader reader, Nv::Blast::Asset * poco) + { + NvBlastIDDTO::deserializeInto(reader.getID(), &poco->m_ID); + + NvBlastBond* bonds = poco->getBonds(); + + uint32_t bondCount = reader.getBondCount(); + for (uint32_t i = 0; i < bondCount; i++) + { + auto bondReader = reader.getBonds()[i]; + + NvBlastBondDTO::deserializeInto(bondReader, &bonds[i]); + } + + NvBlastChunk* chunks = poco->getChunks(); + + uint32_t chunkCount = reader.getChunkCount(); + for (uint32_t i = 0; i < chunkCount; i++) + { + auto chunkReader = reader.getChunks()[i]; + + NvBlastChunkDTO::deserializeInto(chunkReader, &chunks[i]); + } + + poco->m_graph.m_nodeCount = reader.getGraph().getNodeCount(); + + NVBLAST_ASSERT(reader.getSubtreeLeafChunkCounts().size() == poco->m_chunkCount); + for (uint32_t i = 0; i < poco->m_chunkCount; i++) + { + poco->getSubtreeLeafChunkCounts()[i] = reader.getSubtreeLeafChunkCounts()[i]; + } + + for (uint32_t i = 0; i < chunkCount; i++) + { + poco->getChunkToGraphNodeMap()[i] = reader.getChunkToGraphNodeMap()[i]; + } + + uint32_t* ciPtr = poco->m_graph.getChunkIndices(); + + NVBLAST_ASSERT(reader.getGraph().getChunkIndices().size() == poco->m_graph.m_nodeCount); + for (uint32_t i = 0; i < poco->m_graph.m_nodeCount; i++) + { + ciPtr[i] = reader.getGraph().getChunkIndices()[i]; + } + + uint32_t* adjPartition = poco->m_graph.getAdjacencyPartition(); + uint32_t idx = 0; + + for (uint32_t adjPartIndex : reader.getGraph().getAdjacencyPartition()) + { + adjPartition[idx++] = adjPartIndex; + } + + uint32_t* adjNodes = poco->m_graph.getAdjacentNodeIndices(); + idx = 0; + + for (uint32_t adjNodeIndex : reader.getGraph().getAdjacentNodeIndices()) + { + adjNodes[idx++] = adjNodeIndex; + } + + uint32_t* adjBonds = poco->m_graph.getAdjacentBondIndices(); + idx = 0; + + for (uint32_t adjBondIndex : reader.getGraph().getAdjacentBondIndices()) + { + adjBonds[idx++] = adjBondIndex; + } + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h new file mode 100644 index 0000000..c090b5f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastAsset.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + +DTO_CLASS_LL(Asset, Nv::Blast::Asset, Nv::Blast::Serialization::Asset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h b/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h new file mode 100644 index 0000000..a234aec --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +#define DTO_CLASS(_NAME, _POCO, _SERIALIZER) \ +namespace Nv { \ +namespace Blast { \ +class _NAME ## DTO \ +{ \ +public: \ + static class physx::PxCooking* Cooking; \ + static class physx::PxPhysics* Physics; \ + \ + static bool serialize(_SERIALIZER::Builder builder, const _POCO * poco); \ + static _POCO* deserialize(_SERIALIZER::Reader reader); \ + static bool deserializeInto(_SERIALIZER::Reader reader, _POCO * poco); \ +}; \ +} \ +} \ + \ + +#define DTO_CLASS_LL(_NAME, _POCO, _SERIALIZER) \ +namespace Nv { \ +namespace Blast { \ +class _NAME ## DTO \ +{ \ +public: \ + \ + static bool serialize(_SERIALIZER::Builder builder, const _POCO * poco); \ + static _POCO* deserialize(_SERIALIZER::Reader reader); \ + static bool deserializeInto(_SERIALIZER::Reader reader, _POCO * poco); \ +}; \ +} \ +} \ + \ + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp new file mode 100644 index 0000000..cf4cadc --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp @@ -0,0 +1,78 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "ExtPxAssetDTO.h" +#include "TkAssetDTO.h" +#include "ExtPxChunkDTO.h" +#include "ExtPxSubchunkDTO.h" +#include "physics/NvBlastExtPxAssetImpl.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxAssetDTO::serialize(Nv::Blast::Serialization::ExtPxAsset::Builder builder, const Nv::Blast::ExtPxAsset * poco) + { + TkAssetDTO::serialize(builder.getAsset(), &poco->getTkAsset()); + + auto chunks = builder.initChunks(poco->getChunkCount()); + + for (uint32_t i = 0; i <poco->getChunkCount(); i++) + { + ExtPxChunkDTO::serialize(chunks[i], &poco->getChunks()[i]); + } + + auto subchunks = builder.initSubchunks(poco->getSubchunkCount()); + + for (uint32_t i = 0; i < poco->getSubchunkCount(); i++) + { + ExtPxSubchunkDTO::serialize(subchunks[i], &poco->getSubchunks()[i]); + } + + return true; + } + + Nv::Blast::ExtPxAsset* ExtPxAssetDTO::deserialize(Nv::Blast::Serialization::ExtPxAsset::Reader reader) + { + auto tkAsset = TkAssetDTO::deserialize(reader.getAsset()); + + Nv::Blast::ExtPxAssetImpl* asset = reinterpret_cast<Nv::Blast::ExtPxAssetImpl*>(Nv::Blast::ExtPxAsset::create(tkAsset)); + + NVBLAST_ASSERT(asset != nullptr); + + auto chunks = asset->getChunksArray(); + + chunks.resize(reader.getChunks().size()); + for (uint32_t i = 0; i < reader.getChunks().size(); i++) + { + ExtPxChunkDTO::deserializeInto(reader.getChunks()[i], &chunks[i]); + } + + auto subchunks = asset->getSubchunksArray(); + + subchunks.resize(reader.getSubchunks().size()); + for (uint32_t i = 0; i < reader.getSubchunks().size(); i++) + { + ExtPxSubchunkDTO::deserializeInto(reader.getSubchunks()[i], &subchunks[i]); + } + + return asset; + } + + bool ExtPxAssetDTO::deserializeInto(Nv::Blast::Serialization::ExtPxAsset::Reader reader, Nv::Blast::ExtPxAsset * poco) + { + reader = reader; + poco = nullptr; + //NOTE: Because of the way this is structured, can't do this. + return false; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h new file mode 100644 index 0000000..a35d38a --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "NvBlastBondDTO.h" +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" + +DTO_CLASS(ExtPxAsset, Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp new file mode 100644 index 0000000..e096bc1 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "ExtPxChunkDTO.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxChunkDTO::serialize(Nv::Blast::Serialization::ExtPxChunk::Builder builder, const Nv::Blast::ExtPxChunk * poco) + { + builder.setFirstSubchunkIndex(poco->firstSubchunkIndex); + builder.setSubchunkCount(poco->subchunkCount); + builder.setIsStatic(poco->isStatic); + + return true; + } + + Nv::Blast::ExtPxChunk* ExtPxChunkDTO::deserialize(Nv::Blast::Serialization::ExtPxChunk::Reader reader) + { + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool ExtPxChunkDTO::deserializeInto(Nv::Blast::Serialization::ExtPxChunk::Reader reader, Nv::Blast::ExtPxChunk * poco) + { + poco->firstSubchunkIndex = reader.getFirstSubchunkIndex(); + poco->subchunkCount = reader.getSubchunkCount(); + poco->isStatic = reader.getIsStatic(); + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h new file mode 100644 index 0000000..1ff36df --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" + +DTO_CLASS(ExtPxChunk, Nv::Blast::ExtPxChunk, Nv::Blast::Serialization::ExtPxChunk) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp new file mode 100644 index 0000000..cc2be96 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "ExtPxSubchunkDTO.h" +#include "PxTransformDTO.h" +#include "PxConvexMeshGeometryDTO.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxSubchunkDTO::serialize(Nv::Blast::Serialization::ExtPxSubchunk::Builder builder, const Nv::Blast::ExtPxSubchunk * poco) + { + PxTransformDTO::serialize(builder.getTransform(), &poco->transform); + PxConvexMeshGeometryDTO::serialize(builder.getGeometry(), &poco->geometry); + + return true; + } + + Nv::Blast::ExtPxSubchunk* ExtPxSubchunkDTO::deserialize(Nv::Blast::Serialization::ExtPxSubchunk::Reader reader) + { + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool ExtPxSubchunkDTO::deserializeInto(Nv::Blast::Serialization::ExtPxSubchunk::Reader reader, Nv::Blast::ExtPxSubchunk * poco) + { + PxTransformDTO::deserializeInto(reader.getTransform(), &poco->transform); + + return true; + } + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h new file mode 100644 index 0000000..91f78e0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "DTOMacros.h" + +DTO_CLASS(ExtPxSubchunk, Nv::Blast::ExtPxSubchunk, Nv::Blast::Serialization::ExtPxSubchunk) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp new file mode 100644 index 0000000..27cbb11 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp @@ -0,0 +1,63 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastBondDTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + + bool NvBlastBondDTO::serialize(Nv::Blast::Serialization::NvBlastBond::Builder builder, const NvBlastBond * poco) + { + NVBLAST_ASSERT(poco != nullptr); + + kj::ArrayPtr<const float> normArray(poco->normal, 3); + + builder.setNormal(normArray); + + builder.setArea(poco->area); + + kj::ArrayPtr<const float> centArray(poco->centroid, 3); + + builder.setCentroid(centArray); + + builder.setUserData(poco->userData); + + return true; + } + + NvBlastBond* NvBlastBondDTO::deserialize(Nv::Blast::Serialization::NvBlastBond::Reader reader) + { + //FIXME + reader = reader; + //TODO: Allocate with ExtContext and return + return nullptr; + } + + bool NvBlastBondDTO::deserializeInto(Nv::Blast::Serialization::NvBlastBond::Reader reader, NvBlastBond * poco) + { + poco->area = reader.getArea(); + + poco->centroid[0] = reader.getCentroid()[0]; + poco->centroid[1] = reader.getCentroid()[1]; + poco->centroid[2] = reader.getCentroid()[2]; + + poco->normal[0] = reader.getNormal()[0]; + poco->normal[1] = reader.getNormal()[1]; + poco->normal[2] = reader.getNormal()[2]; + + poco->userData = reader.getUserData(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h new file mode 100644 index 0000000..8b67bd7 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + +DTO_CLASS_LL(NvBlastBond, NvBlastBond, Nv::Blast::Serialization::NvBlastBond) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp new file mode 100644 index 0000000..38814ed --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastChunkDTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool NvBlastChunkDTO::serialize(Nv::Blast::Serialization::NvBlastChunk::Builder builder, const NvBlastChunk* poco) + { + NVBLAST_ASSERT(poco != nullptr); + + kj::ArrayPtr<const float> centArray(poco->centroid, 3); + builder.setCentroid(centArray); + + builder.setVolume(poco->volume); + + builder.setParentChunkIndex(poco->parentChunkIndex); + builder.setFirstChildIndex(poco->firstChildIndex); + builder.setChildIndexStop(poco->childIndexStop); + builder.setUserData(poco->userData); + + return true; + } + + NvBlastChunk* NvBlastChunkDTO::deserialize(Nv::Blast::Serialization::NvBlastChunk::Reader reader) + { + //FIXME + reader = reader; + + return nullptr; + } + + bool NvBlastChunkDTO::deserializeInto(Nv::Blast::Serialization::NvBlastChunk::Reader reader, NvBlastChunk* target) + { + NVBLAST_ASSERT(target != nullptr); + + target->centroid[0] = reader.getCentroid()[0]; + target->centroid[1] = reader.getCentroid()[1]; + target->centroid[2] = reader.getCentroid()[2]; + + target->childIndexStop = reader.getChildIndexStop(); + target->firstChildIndex = reader.getFirstChildIndex(); + target->parentChunkIndex = reader.getParentChunkIndex(); + target->userData = reader.getUserData(); + target->volume = reader.getVolume(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h new file mode 100644 index 0000000..5fec498 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h @@ -0,0 +1,18 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + + +DTO_CLASS_LL(NvBlastChunk, NvBlastChunk, Nv::Blast::Serialization::NvBlastChunk) + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp new file mode 100644 index 0000000..e540cd8 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastIDDTO.h" +#include "NvBlastTypes.h" +#include "NvBlastAssert.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + + +namespace Nv +{ + namespace Blast + { + + bool NvBlastIDDTO::serialize(Nv::Blast::Serialization::UUID::Builder builder, const NvBlastID * poco) + { + capnp::Data::Reader idArrayReader((unsigned char *)poco->data, 16); + builder.setValue(idArrayReader); + + return true; + } + + NvBlastID* NvBlastIDDTO::deserialize(Nv::Blast::Serialization::UUID::Reader reader) + { + //FIXME + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool NvBlastIDDTO::deserializeInto(Nv::Blast::Serialization::UUID::Reader reader, NvBlastID * poco) + { + NVBLAST_ASSERT_WITH_MESSAGE(reader.getValue().size() == 16, "BlastID must be 16 bytes"); + + memcpy(poco, reader.getValue().begin(), 16); + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h new file mode 100644 index 0000000..afe6cf0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" +#include "DTOMacros.h" + +DTO_CLASS_LL(NvBlastID, NvBlastID, ::Nv::Blast::Serialization::UUID) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp new file mode 100644 index 0000000..1c46f9e --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp @@ -0,0 +1,127 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PxConvexMeshGeometryDTO.h" +#include "PxMeshScaleDTO.h" +#include "NvBlastAssert.h" +#include "NvBlastExtKJPxInputStream.h" +#include "NvBlastExtKJPxOutputStream.h" +#include "PxConvexMeshDesc.h" +#include "NvBlastExtSerialization.h" +#include "PxVec3.h" +#include <algorithm> +#include "PxPhysics.h" + + +namespace Nv +{ + namespace Blast + { + physx::PxCooking* PxConvexMeshGeometryDTO::Cooking = nullptr; + physx::PxPhysics* PxConvexMeshGeometryDTO::Physics = nullptr; + + bool PxConvexMeshGeometryDTO::serialize(Nv::Blast::Serialization::PxConvexMeshGeometry::Builder builder, const physx::PxConvexMeshGeometry * poco) + { + PxMeshScaleDTO::serialize(builder.getScale(), &poco->scale); + + //TODO: Use cooking.cookConvexMesh to cook the mesh to a stream - then get that backing buffer and put it into the Data field + + physx::PxConvexMeshDesc desc; + desc.points.data = poco->convexMesh->getVertices(); + desc.points.count = poco->convexMesh->getNbVertices(); + desc.points.stride = sizeof(physx::PxVec3); + + std::vector<uint32_t> indicesScratch; + std::vector<physx::PxHullPolygon> hullPolygonsScratch; + + hullPolygonsScratch.resize(poco->convexMesh->getNbPolygons()); + + uint32_t indexCount = 0; + for (uint32_t i = 0; i < hullPolygonsScratch.size(); i++) + { + physx::PxHullPolygon polygon; + poco->convexMesh->getPolygonData(i, polygon); + if (polygon.mNbVerts) + { + indexCount = std::max<uint32_t>(indexCount, polygon.mIndexBase + polygon.mNbVerts); + } + } + indicesScratch.resize(indexCount); + + for (uint32_t i = 0; i < hullPolygonsScratch.size(); i++) + { + physx::PxHullPolygon polygon; + poco->convexMesh->getPolygonData(i, polygon); + for (uint32_t j = 0; j < polygon.mNbVerts; j++) + { + indicesScratch[polygon.mIndexBase + j] = poco->convexMesh->getIndexBuffer()[polygon.mIndexBase + j]; + } + + hullPolygonsScratch[i] = polygon; + } + + desc.indices.count = indexCount; + desc.indices.data = indicesScratch.data(); + desc.indices.stride = sizeof(uint32_t); + + desc.polygons.count = poco->convexMesh->getNbPolygons(); + desc.polygons.data = hullPolygonsScratch.data(); + desc.polygons.stride = sizeof(physx::PxHullPolygon); + + + std::vector<unsigned char> buffer; + buffer.resize(16 * 1024 * 1024); // No idea how much memory is needed! Allocate 16MB + kj::ArrayPtr<unsigned char> bufferArray(buffer.data(), buffer.size()); + + Nv::Blast::ExtKJPxOutputStream outputStream(bufferArray); + + bool cookResult = Cooking->cookConvexMesh(desc, outputStream); + + if (!cookResult) + { + return false; + } + + kj::ArrayPtr<unsigned char> cookedBuffer(outputStream.getBuffer().begin(), outputStream.getWrittenBytes()); + + builder.setConvexMesh(cookedBuffer); + + // builder.getConvexMesh(). + + return true; + } + + physx::PxConvexMeshGeometry* PxConvexMeshGeometryDTO::deserialize(Nv::Blast::Serialization::PxConvexMeshGeometry::Reader reader) + { + NVBLAST_ASSERT(PxConvexMeshGeometryDTO::Cooking != nullptr); + + reader = reader; + + return nullptr; + } + + bool PxConvexMeshGeometryDTO::deserializeInto(Nv::Blast::Serialization::PxConvexMeshGeometry::Reader reader, physx::PxConvexMeshGeometry * poco) + { + NVBLAST_ASSERT(PxConvexMeshGeometryDTO::Cooking != nullptr); + + PxMeshScaleDTO::deserializeInto(reader.getScale(), &poco->scale); + + Nv::Blast::ExtKJPxInputStream inputStream(reader.getConvexMesh()); + + //NOTE: Naive approach, no shared convex hulls + poco->convexMesh = Physics->createConvexMesh(inputStream); + + return false; + } + + + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h new file mode 100644 index 0000000..27b3754 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxConvexMeshGeometry.h" +#include "PxCooking.h" + +DTO_CLASS(PxConvexMeshGeometry, physx::PxConvexMeshGeometry, Nv::Blast::Serialization::PxConvexMeshGeometry) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp new file mode 100644 index 0000000..8fee6ad --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PxMeshScaleDTO.h" +#include "PxVec3DTO.h" +#include "PxQuatDTO.h" + +namespace Nv +{ + namespace Blast + { + bool PxMeshScaleDTO::serialize(Nv::Blast::Serialization::PxMeshScale::Builder builder, const physx::PxMeshScale * poco) + { + PxVec3DTO::serialize(builder.getScale(), &poco->scale); + PxQuatDTO::serialize(builder.getRotation(), &poco->rotation); + + return true; + } + + physx::PxMeshScale* PxMeshScaleDTO::deserialize(Nv::Blast::Serialization::PxMeshScale::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxMeshScaleDTO::deserializeInto(Nv::Blast::Serialization::PxMeshScale::Reader reader, physx::PxMeshScale * poco) + { + PxVec3DTO::deserializeInto(reader.getScale(), &poco->scale); + PxQuatDTO::deserializeInto(reader.getRotation(), &poco->rotation); + + return true; + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h new file mode 100644 index 0000000..7b758c8 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxMeshScale.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxMeshScale, physx::PxMeshScale, Nv::Blast::Serialization::PxMeshScale) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp new file mode 100644 index 0000000..8faeaa6 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PxQuatDTO.h" + +namespace Nv +{ + namespace Blast + { + + bool PxQuatDTO::serialize(Nv::Blast::Serialization::PxQuat::Builder builder, const physx::PxQuat * poco) + { + builder.setX(poco->x); + builder.setY(poco->y); + builder.setZ(poco->z); + builder.setW(poco->w); + + return true; + } + + physx::PxQuat* PxQuatDTO::deserialize(Nv::Blast::Serialization::PxQuat::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxQuatDTO::deserializeInto(Nv::Blast::Serialization::PxQuat::Reader reader, physx::PxQuat * poco) + { + poco->x = reader.getX(); + poco->y = reader.getY(); + poco->z = reader.getZ(); + poco->w = reader.getW(); + + return true; + } + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h new file mode 100644 index 0000000..460d6c5 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxQuat.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxQuat, physx::PxQuat, Nv::Blast::Serialization::PxQuat) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp new file mode 100644 index 0000000..20a7cbb --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp @@ -0,0 +1,42 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PxTransformDTO.h" +#include "PxQuatDTO.h" +#include "PxVec3DTO.h" + +namespace Nv +{ + namespace Blast + { + + bool PxTransformDTO::serialize(Nv::Blast::Serialization::PxTransform::Builder builder, const physx::PxTransform * poco) + { + PxQuatDTO::serialize(builder.getQ(), &poco->q); + PxVec3DTO::serialize(builder.getP(), &poco->p); + + return true; + } + + physx::PxTransform* PxTransformDTO::deserialize(Nv::Blast::Serialization::PxTransform::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxTransformDTO::deserializeInto(Nv::Blast::Serialization::PxTransform::Reader reader, physx::PxTransform * poco) + { + PxQuatDTO::deserializeInto(reader.getQ(), &poco->q); + PxVec3DTO::deserializeInto(reader.getP(), &poco->p); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h new file mode 100644 index 0000000..49a6b73 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxTransform.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxTransform, physx::PxTransform, Nv::Blast::Serialization::PxTransform) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp new file mode 100644 index 0000000..9827cd0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "PxVec3DTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool PxVec3DTO::serialize(Nv::Blast::Serialization::PxVec3::Builder builder, const physx::PxVec3 * poco) + { + NVBLAST_ASSERT(poco != nullptr); + + builder.setX(poco->x); + builder.setY(poco->y); + builder.setZ(poco->z); + + return true; + } + + physx::PxVec3* PxVec3DTO::deserialize(Nv::Blast::Serialization::PxVec3::Reader reader) + { + //TODO: Allocate using ExtContext and return + reader = reader; + return nullptr; + } + + bool PxVec3DTO::deserializeInto(Nv::Blast::Serialization::PxVec3::Reader reader, physx::PxVec3* target) + { + target->x = reader.getX(); + target->y = reader.getY(); + target->z = reader.getZ(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h new file mode 100644 index 0000000..8a04c8b --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxVec3.h" +#include "PxCooking.h" + +DTO_CLASS(PxVec3, physx::PxVec3, Nv::Blast::Serialization::PxVec3) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp new file mode 100644 index 0000000..acc55ba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp @@ -0,0 +1,67 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "TkAssetDTO.h" +#include "AssetDTO.h" +#include "TkAssetJointDescDTO.h" +#include <vector> +#include "NvBlastTkFramework.h" + + + +namespace Nv +{ + namespace Blast + { + bool TkAssetDTO::serialize(Nv::Blast::Serialization::TkAsset::Builder builder, const Nv::Blast::TkAsset * poco) + { + const Asset* assetLL = reinterpret_cast<const Nv::Blast::Asset*>(poco->getAssetLL()); + + Nv::Blast::AssetDTO::serialize(builder.getAssetLL(), assetLL); + + uint32_t jointDescCount = poco->getJointDescCount(); + + capnp::List<Nv::Blast::Serialization::TkAssetJointDesc>::Builder jointDescs = builder.initJointDescs(jointDescCount); + + for (uint32_t i = 0; i < jointDescCount; i++) + { + TkAssetJointDescDTO::serialize(jointDescs[i], &poco->getJointDescs()[i]); + } + + return true; + } + + Nv::Blast::TkAsset* TkAssetDTO::deserialize(Nv::Blast::Serialization::TkAsset::Reader reader) + { + const NvBlastAsset* assetLL = reinterpret_cast<const NvBlastAsset*>(AssetDTO::deserialize(reader.getAssetLL())); + + std::vector<Nv::Blast::TkAssetJointDesc> jointDescs; + jointDescs.resize(reader.getJointDescs().size()); + + for (uint32_t i = 0; i < jointDescs.size(); i++) + { + TkAssetJointDescDTO::deserializeInto(reader.getJointDescs()[i], &jointDescs[i]); + } + + // Make sure to set ownsAsset to true - this is serialization and no one else owns it. + Nv::Blast::TkAsset* asset = NvBlastTkFrameworkGet()->createAsset(assetLL, jointDescs.data(), jointDescs.size(), true); + + return asset; + } + + bool TkAssetDTO::deserializeInto(Nv::Blast::Serialization::TkAsset::Reader reader, Nv::Blast::TkAsset * poco) + { + reader = reader; + poco = nullptr; + // NOTE: Because of the way TkAsset is currently structured, this won't work. + return false; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h new file mode 100644 index 0000000..1b21eba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTkAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(TkAsset, Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp new file mode 100644 index 0000000..9118d19 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp @@ -0,0 +1,53 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "TkAssetJointDescDTO.h" +#include "PxVec3DTO.h" + + +namespace Nv +{ + namespace Blast + { + + bool TkAssetJointDescDTO::serialize(Nv::Blast::Serialization::TkAssetJointDesc::Builder builder, const Nv::Blast::TkAssetJointDesc * poco) + { + kj::ArrayPtr<const uint32_t> nodeIndices(poco->nodeIndices, 2); + builder.setNodeIndices(nodeIndices); + + for (int i = 0; i < 2; i++) + { + PxVec3DTO::serialize(builder.getAttachPositions()[i], &poco->attachPositions[i]); + } + + return true; + } + + Nv::Blast::TkAssetJointDesc* TkAssetJointDescDTO::deserialize(Nv::Blast::Serialization::TkAssetJointDesc::Reader reader) + { + //TODO: Allocate with ExtContent and return + + reader = reader; + + return nullptr; + } + + bool TkAssetJointDescDTO::deserializeInto(Nv::Blast::Serialization::TkAssetJointDesc::Reader reader, Nv::Blast::TkAssetJointDesc * poco) + { + PxVec3DTO::deserializeInto(reader.getAttachPositions()[0], &poco->attachPositions[0]); + PxVec3DTO::deserializeInto(reader.getAttachPositions()[1], &poco->attachPositions[1]); + + poco->nodeIndices[0] = reader.getNodeIndices()[0]; + poco->nodeIndices[1] = reader.getNodeIndices()[1]; + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h new file mode 100644 index 0000000..88364bd --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTkAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(TkAssetJointDesc, Nv::Blast::TkAssetJointDesc, Nv::Blast::Serialization::TkAssetJointDesc) diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h new file mode 100644 index 0000000..dd50afd --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h @@ -0,0 +1,10 @@ +#pragma once + +/** +Function pointer type for allocation - has same signature as stdlib malloc. +*/ +typedef void* (*NvBlastExtAlloc)(size_t size); + +extern NvBlastExtAlloc gAlloc; +extern NvBlastLog gLog; + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp new file mode 100644 index 0000000..7ef9b62 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp @@ -0,0 +1,38 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtInputStream.h" + + +Nv::Blast::ExtInputStream::ExtInputStream(std::istream &inputStream): + m_inputStream(inputStream) +{ + +} + + +size_t Nv::Blast::ExtInputStream::tryRead(void* buffer, size_t /*minBytes*/, size_t maxBytes) +{ + m_inputStream.read((char *) buffer, maxBytes); + + if (m_inputStream.fail()) + { + // Throw exception, log error +// NVBLASTEXT_LOG_ERROR("Failure when reading from stream"); + } + + // Since we're using a blocking read above, if we don't have maxBytes we're probably done + if ((size_t) m_inputStream.gcount() < maxBytes) + { +// NVBLASTEXT_LOG_ERROR("Failed to read requested number of bytes during blocking read."); + } + + return m_inputStream.gcount(); +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h new file mode 100644 index 0000000..9b19d9c --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "kj/io.h" +#include <istream> + +namespace Nv +{ + namespace Blast + { + class ExtInputStream : public kj::InputStream + { + public: + ExtInputStream() = delete; + ExtInputStream(std::istream &inputStream); + + // Returns a read of maxBytes. This is supposed to be happy doing partial reads, but currently isn't. + virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; + + private: + std::istream &m_inputStream; + }; + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp new file mode 100644 index 0000000..9af13a9 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp @@ -0,0 +1,32 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtKJPxInputStream.h" + +namespace Nv +{ + namespace Blast + { + ExtKJPxInputStream::ExtKJPxInputStream(capnp::Data::Reader inReader) : + dataReader(inReader), + inputStream(nullptr) + { + kj::ArrayPtr<const unsigned char> buffer(inReader.begin(), inReader.size()); + + inputStream = std::make_shared<kj::ArrayInputStream>(buffer); + } + + uint32_t ExtKJPxInputStream::read(void* dest, uint32_t count) + { + return inputStream->tryRead(dest, count, count); + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h new file mode 100644 index 0000000..452892d --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "PxIO.h" +#include "capnp/common.h" +#include "kj/io.h" +#include <memory> +#include "generated/NvBlastExtSerialization.capn.h" + +namespace Nv +{ + namespace Blast + { + /* + A wrapper around a Capn Proto Data reader. + + Since it needs to behave like a stream, it's internally wrapped in a stream. + + */ + class ExtKJPxInputStream : public physx::PxInputStream + { + public: + ExtKJPxInputStream(capnp::Data::Reader inReader); + ~ExtKJPxInputStream() = default; + + virtual uint32_t read(void* dest, uint32_t count) override; + + private: + capnp::Data::Reader dataReader; + std::shared_ptr<kj::ArrayInputStream> inputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp new file mode 100644 index 0000000..0f17a01 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp @@ -0,0 +1,35 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtKJPxOutputStream.h" + +namespace Nv +{ + namespace Blast + { + ExtKJPxOutputStream::ExtKJPxOutputStream(kj::ArrayPtr<unsigned char> inBuffer) : + writtenBytes(0), + Buffer(inBuffer), + outputStream(nullptr) + { + outputStream = std::make_shared<kj::ArrayOutputStream>(inBuffer); + } + + uint32_t ExtKJPxOutputStream::write(const void* src, uint32_t count) + { + outputStream->write(src, count); + + writtenBytes += count; + + return count; + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h new file mode 100644 index 0000000..0ed563f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h @@ -0,0 +1,40 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "PxIO.h" +#include "kj/common.h" +#include <memory> +#include "kj/io.h" + +namespace Nv +{ + namespace Blast + { + class ExtKJPxOutputStream : public physx::PxOutputStream + { + public: + ExtKJPxOutputStream(kj::ArrayPtr<unsigned char> inBuffer); + ~ExtKJPxOutputStream() = default; + + virtual uint32_t write(const void* src, uint32_t count) override; + + uint32_t getWrittenBytes() { return writtenBytes; } + + kj::ArrayPtr<unsigned char> getBuffer() { return Buffer; } + + private: + uint32_t writtenBytes; + + kj::ArrayPtr<unsigned char> Buffer; + std::shared_ptr<kj::ArrayOutputStream> outputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp new file mode 100644 index 0000000..cf6e31f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp @@ -0,0 +1,24 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtOutputStream.h" + + + +Nv::Blast::ExtOutputStream::ExtOutputStream(std::ostream &outputStream): + m_outputStream(outputStream) +{ + +} + +void Nv::Blast::ExtOutputStream::write(const void* buffer, size_t size) +{ + m_outputStream.write((char *) buffer, size); +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h new file mode 100644 index 0000000..ba0044e --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "kj/io.h" +#include <ostream> + +namespace Nv +{ + namespace Blast + { + class ExtOutputStream : public kj::OutputStream + { + + public: + ExtOutputStream() = delete; + ExtOutputStream(std::ostream &outputStream); + + virtual void write(const void* buffer, size_t size) override; + private: + std::ostream &m_outputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn new file mode 100644 index 0000000..ddc439a --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn @@ -0,0 +1,95 @@ +# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +@0xaffe4498f275ee58; + +using Cxx = import "/capnp/c++.capnp"; + +using LL = import "NvBlastExtSerializationLL.capn"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct TkAsset +{ + assetLL @0 :LL.Asset; + + jointDescs @1 :List(TkAssetJointDesc); +} + +struct ExtPxAsset +{ + asset @0 :TkAsset; + chunks @1 :List(ExtPxChunk); + subchunks @2 :List(ExtPxSubchunk); +} + +struct ExtPxChunk +{ + firstSubchunkIndex @0 :UInt32; + subchunkCount @1 :UInt32; + isStatic @2 :Bool; +} + +struct ExtPxSubchunk +{ + transform @0 :PxTransform; + geometry @1 :PxConvexMeshGeometry; +} + +struct PxConvexMeshGeometry +{ + scale @0 :PxMeshScale; + convexMesh @1 :Data; + meshFlags @2 :UInt8; + + enum Type + { + eSPHERE @0; + ePLANE @1; + eCAPSULE @2; + eBOX @3; + eCONVEXMESH @4; + eTRIANGLEMESH @5; + eHEIGHTFIELD @6; + } + + type @3 :Type; +} + +struct TkAssetJointDesc +{ + nodeIndices @0 :List(UInt32); + attachPositions @1 :List(PxVec3); +} + +struct PxVec3 +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; +} + +struct PxQuat +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; + w @3 :Float32; +} + +struct PxMeshScale +{ + scale @0 :PxVec3; + rotation @1 :PxQuat; +} + +struct PxTransform +{ + q @0 :PxQuat; + p @1 :PxVec3; +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp new file mode 100644 index 0000000..28a1553 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp @@ -0,0 +1,146 @@ +#include "NvBlastExtSerialization.h" +#include "BlastSerialization.h" +#include <memory> +#include "PxPhysicsVersion.h" +#include "PxConvexMeshGeometryDTO.h" +#include "NvBlastExtDefs.h" + + +// This is terrible. +physx::PxPhysics* g_Physics = nullptr; + + +std::shared_ptr<physx::PxCooking> getCooking() +{ + physx::PxCookingParams cookingParams(g_Physics->getTolerancesScale()); + cookingParams.buildGPUData = true; + + std::shared_ptr<physx::PxCooking> m_Cooking = std::shared_ptr<physx::PxCooking>(PxCreateCooking(PX_PHYSICS_VERSION, g_Physics->getFoundation(), cookingParams), [=](physx::PxCooking* cooking) + { + cooking->release(); + }); + + NVBLASTEXT_CHECK_ERROR(m_Cooking, "Error: failed to create PhysX Cooking\n", return nullptr); + + return m_Cooking; +} + + +extern "C" +{ + NVBLAST_API void setPhysXSDK(physx::PxPhysics* physXSDK) + { + g_Physics = physXSDK; + } + + NVBLAST_API NvBlastAsset* deserializeAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserialize(input, size); + } + + NVBLAST_API NvBlastAsset* deserializeAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeAssetIntoStream(const NvBlastAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::Asset *>(asset), outputStream); + } + + NVBLAST_API bool serializeAssetIntoNewBuffer(const NvBlastAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeAssetIntoExistingBuffer(const NvBlastAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // TkAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeTkAssetIntoStream(const Nv::Blast::TkAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeTkAssetIntoNewBuffer(const Nv::Blast::TkAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeTkAssetIntoExistingBuffer(const Nv::Blast::TkAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // ExtPxAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAsset(const unsigned char* input, uint32_t size) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAssetFromStream(std::istream &inputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoStream(const Nv::Blast::ExtPxAsset *asset, std::ostream &outputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoNewBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeExtPxAssetIntoExistingBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), buffer, maxSize, usedSize); + } + + +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h new file mode 100644 index 0000000..5d44554 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h @@ -0,0 +1,172 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once +#include "kj/io.h" +#include "capnp/serialize.h" +#include "generated/NvBlastExtSerializationLL.capn.h" +#include <vector> +#include "NvBlastExtInputStream.h" +#include "NvBlastExtOutputStream.h" + +#if !defined(BLAST_LL_ALLOC) +#include "NvBlastExtAllocator.h" +#endif +#include "NvBlastExtGlobals.h" + +namespace Nv +{ + namespace Blast + { + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + class ExtSerialization + { + public: + static TAsset* deserialize(const unsigned char* input, uint32_t size); + static TAsset* deserializeFromStream(std::istream &inputStream); + + static bool serializeIntoExistingBuffer(const TAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize); + static bool serializeIntoNewBuffer(const TAsset *asset, unsigned char **outBuffer, uint32_t &outSize); + static bool serializeIntoStream(const TAsset *asset, std::ostream &outputStream); + + private: + + static void serializeMessageIntoNewBuffer(capnp::MallocMessageBuilder& message, unsigned char ** outBuffer, uint32_t &outSize); + + // Specialized + static bool serializeIntoBuilder(TSerializationBuilder& assetBuilder, const TAsset* asset); + static bool serializeIntoMessage(capnp::MallocMessageBuilder& message, const TAsset* asset); + static TAsset* deserializeFromStreamReader(capnp::InputStreamMessageReader &message); + }; + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + TAsset* ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::deserialize(const unsigned char* input, uint32_t size) + { + kj::ArrayPtr<const unsigned char> source(input, size); + + kj::ArrayInputStream inputStream(source); + + std::vector<uint64_t> scratch; + scratch.resize(size); + kj::ArrayPtr<capnp::word> scratchArray((capnp::word*) scratch.data(), size); + + capnp::InputStreamMessageReader message(inputStream, capnp::ReaderOptions(), scratchArray); + + return deserializeFromStreamReader(message); + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + TAsset* ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::deserializeFromStream(std::istream &inputStream) + { + Nv::Blast::ExtInputStream readStream(inputStream); + + capnp::InputStreamMessageReader message(readStream); + + return deserializeFromStreamReader(message); + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoExistingBuffer(const TAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + usedSize = 0; + return false; + } + + uint32_t messageSize = computeSerializedSizeInWords(message) * sizeof(uint64_t); + + if (maxSize < messageSize) + { + NvBlastLog logFn = gLog; + +#if !defined(BLAST_LL_ALLOC) + logFn = NvBlastTkFrameworkGet()->getLogFn(); +#endif + + NVBLAST_LOG_ERROR(logFn, "When attempting to serialize into an existing buffer, the provided buffer was too small."); + usedSize = 0; + return false; + } + + kj::ArrayPtr<unsigned char> outputBuffer(buffer, maxSize); + kj::ArrayOutputStream outputStream(outputBuffer); + + capnp::writeMessage(outputStream, message); + + usedSize = messageSize;; + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoNewBuffer(const TAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + *outBuffer = nullptr; + outSize = 0; + return false; + } + + serializeMessageIntoNewBuffer(message, outBuffer, outSize); + + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoStream(const TAsset *asset, std::ostream &outputStream) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + return false; + } + + Nv::Blast::ExtOutputStream blastOutputStream(outputStream); + + writeMessage(blastOutputStream, message); + + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + void ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeMessageIntoNewBuffer(capnp::MallocMessageBuilder& message, unsigned char ** outBuffer, uint32_t &outSize) + { + uint32_t messageSize = computeSerializedSizeInWords(message) * sizeof(uint64_t); + + NvBlastExtAlloc allocFn = gAlloc; + +#if !defined(BLAST_LL_ALLOC) + allocFn = ExtAllocator::alignedAlloc16; +#endif + + unsigned char* buffer = static_cast<unsigned char *>(allocFn(messageSize)); + + kj::ArrayPtr<unsigned char> outputBuffer(buffer, messageSize); + kj::ArrayOutputStream outputStream(outputBuffer); + + capnp::writeMessage(outputStream, message); + + *outBuffer = buffer; + outSize = messageSize; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h new file mode 100644 index 0000000..7f9fbc9 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h @@ -0,0 +1,75 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +#include "NvBlastExtSerialization.h" +#include "NvBlastTkAsset.h" +#include "NvBlastExtPxAsset.h" +#include "TkAssetDTO.h" +#include "ExtPxAssetDTO.h" + +namespace Nv +{ + namespace Blast + { + /* + Specializations here - one set for each top level asset. (TkAsset, ExtPxAsset) + */ + + + // TkAsset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::TkAsset::Builder& assetBuilder, const Nv::Blast::TkAsset* asset) + { + return TkAssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::TkAsset* ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::TkAsset::Reader reader = message.getRoot<Nv::Blast::Serialization::TkAsset>(); + + return TkAssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::TkAsset* asset) + { + Nv::Blast::Serialization::TkAsset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::TkAsset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + + + //ExtPxAsset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::ExtPxAsset::Builder& assetBuilder, const Nv::Blast::ExtPxAsset* asset) + { + return ExtPxAssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::ExtPxAsset* ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::ExtPxAsset::Reader reader = message.getRoot<Nv::Blast::Serialization::ExtPxAsset>(); + + return ExtPxAssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::ExtPxAsset* asset) + { + Nv::Blast::Serialization::ExtPxAsset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::ExtPxAsset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp new file mode 100644 index 0000000..bebee5b --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp @@ -0,0 +1,133 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtSerializationImpl.h" +#include <memory> +#include "PxPhysicsVersion.h" +#include "PxConvexMeshGeometryDTO.h" +#include "NvBlastExtDefs.h" +#include "PxPhysics.h" +#include "NvBlastAssert.h" + + +// This is terrible. +physx::PxPhysics* g_Physics = nullptr; + +using namespace Nv::Blast; + +std::shared_ptr<physx::PxCooking> getCooking() +{ + physx::PxCookingParams cookingParams(g_Physics->getTolerancesScale()); + cookingParams.buildGPUData = true; + + std::shared_ptr<physx::PxCooking> m_Cooking = std::shared_ptr<physx::PxCooking>(PxCreateCooking(PX_PHYSICS_VERSION, g_Physics->getFoundation(), cookingParams), [=](physx::PxCooking* cooking) + { + cooking->release(); + }); + + NVBLASTEXT_CHECK_ERROR(m_Cooking, "Error: failed to create PhysX Cooking\n", return nullptr); + + return m_Cooking; +} + + +extern "C" +{ + NVBLAST_API void setPhysXSDK(physx::PxPhysics* physXSDK) + { + g_Physics = physXSDK; + } + + ////////////////////////////////////////////////////////////////////////// + // TkAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeTkAssetIntoStream(const Nv::Blast::TkAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeTkAssetIntoNewBuffer(const Nv::Blast::TkAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeTkAssetIntoExistingBuffer(const Nv::Blast::TkAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // ExtPxAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAsset(const unsigned char* input, uint32_t size) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAssetFromStream(std::istream &inputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoStream(const Nv::Blast::ExtPxAsset *asset, std::ostream &outputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoNewBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeExtPxAssetIntoExistingBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), buffer, maxSize, usedSize); + } + + +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn new file mode 100644 index 0000000..026056f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn @@ -0,0 +1,89 @@ +# Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +# +# NVIDIA CORPORATION and its licensors retain all intellectual property +# and proprietary rights in and to this software, related documentation +# and any modifications thereto. Any use, reproduction, disclosure or +# distribution of this software and related documentation without an express +# license agreement from NVIDIA CORPORATION is strictly prohibited. + +@0x9a4a58fac38375e0; + +using Cxx = import "/capnp/c++.capnp"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct Asset +{ + header @0 :NvBlastDataBlock; + + iD @1 :UUID; + + chunkCount @2 :UInt32; + + graph @3 :NvBlastSupportGraph; + + leafChunkCount @4 :UInt32; + + firstSubsupportChunkIndex @5 :UInt32; + + bondCount @6 :UInt32; + + chunks @7: List(NvBlastChunk); + + bonds @8: List(NvBlastBond); + + subtreeLeafChunkCounts @9: List(UInt32); + + chunkToGraphNodeMap @10: List(UInt32); +} + +struct NvBlastDataBlock +{ + enum Type + { + assetDataBlock @0; + instanceDataBlock @1; + } + + dataType @0 :Type; + + formatVersion @1 :UInt32; + + size @2 :UInt32; +} + +struct NvBlastChunk +{ + centroid @0 :List(Float32); + + volume @1 :Float32; + + parentChunkIndex @2 :UInt32; + firstChildIndex @3 :UInt32; + childIndexStop @4 :UInt32; + userData @5 :UInt32; +} + +struct NvBlastBond +{ + normal @0 :List(Float32); + area @1 :Float32; + centroid @2 :List(Float32); + userData @3 :UInt32; +} + +struct NvBlastSupportGraph +{ + nodeCount @0 : UInt32; + + chunkIndices @1 : List(UInt32); + adjacencyPartition @2 : List(UInt32); + adjacentNodeIndices @3 : List(UInt32); + adjacentBondIndices @4 : List(UInt32); +} + +struct UUID +{ + value @0 : Data; +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h new file mode 100644 index 0000000..d7595ba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#pragma once + +#include "NvBlastExtSerialization.h" +#include "NvBlastAsset.h" +#include "AssetDTO.h" + +namespace Nv +{ + namespace Blast + { + /* + Specializations here - LL asset only + */ + + // Asset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::Asset::Builder& assetBuilder, const Nv::Blast::Asset* asset) + { + return AssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::Asset* ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::Asset::Reader reader = message.getRoot<Nv::Blast::Serialization::Asset>(); + + return AssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::Asset* asset) + { + Nv::Blast::Serialization::Asset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::Asset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp new file mode 100644 index 0000000..26d8667 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp @@ -0,0 +1,101 @@ +/* +* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtSerializationLLInterface.h" +#include "NvBlastExtSerializationLLImpl.h" +#include <memory> +#include "NvBlastExtSerialization.h" +#include "NvBlastAsset.h" +#include <iostream> +#include "NvBlastExtGlobals.h" + + +NvBlastExtAlloc gAlloc = nullptr; +NvBlastLog gLog = nullptr; + +extern "C" +{ + NVBLAST_API void setAllocator(NvBlastExtAlloc alloc) + { + gAlloc = alloc; + } + + NVBLAST_API void setLog(NvBlastLog log) + { + gLog = log; + } + + NVBLAST_API NvBlastAsset* deserializeAsset(const unsigned char* input, uint32_t size) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return nullptr; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserialize(input, size); + } + + NVBLAST_API NvBlastAsset* deserializeAssetFromStream(std::istream &inputStream) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return nullptr; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeAssetIntoStream(const NvBlastAsset *asset, std::ostream &outputStream) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::Asset *>(asset), outputStream); + } + + NVBLAST_API bool serializeAssetIntoNewBuffer(const NvBlastAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeAssetIntoExistingBuffer(const NvBlastAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), buffer, maxSize, usedSize); + } + +} + diff --git a/NvBlast/sdk/extensions/shaders/include/NvBlastExtDamageShaders.h b/NvBlast/sdk/extensions/shaders/include/NvBlastExtDamageShaders.h new file mode 100644 index 0000000..385bf52 --- /dev/null +++ b/NvBlast/sdk/extensions/shaders/include/NvBlastExtDamageShaders.h @@ -0,0 +1,111 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTDAMAGESHADERS_H +#define NVBLASTEXTDAMAGESHADERS_H + +#include "NvBlastTypes.h" +#include "NvBlastPreprocessor.h" + +/** +A few example damage shader implementations. +*/ + + +/////////////////////////////////////////////////////////////////////////////// +// Common Material +/////////////////////////////////////////////////////////////////////////////// + +/** +Specific parameters for the material functions here present. + +Material function implementers may choose their own set. +*/ +struct NvBlastExtMaterial +{ + float singleChunkThreshold; //!< subsupport chunks only take damage surpassing this value + float graphChunkThreshold; //!< support chunks only take damage surpassing this value + float bondTangentialThreshold; //!< bond only take damage surpassing this value + float bondNormalThreshold; //!< currently unused - forward damage propagation + float damageAttenuation; //!< factor of damage attenuation while forwarding +}; + + +/////////////////////////////////////////////////////////////////////////////// +// Radial Damage +/////////////////////////////////////////////////////////////////////////////// + +/** +Radial Damage Desc +*/ +struct NvBlastExtRadialDamageDesc +{ + float compressive; //!< compressive (radial) damage component + float position[3]; //!< origin of damage action + float minRadius; //!< inner radius of damage action + float maxRadius; //!< outer radius of damage action +}; + +/** +Radial Falloff and Radial Cutter damage for both graph and subgraph shaders. + +NOTE: The signature of shader functions are equal to NvBlastGraphShaderFunction and NvBlastSubgraphShaderFunction respectively. +They are not expected to be called directly. +@see NvBlastGraphShaderFunction, NvBlastSubgraphShaderFunction +*/ +NVBLAST_API void NvBlastExtFalloffGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params); +NVBLAST_API void NvBlastExtFalloffSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params); +NVBLAST_API void NvBlastExtCutterGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params); +NVBLAST_API void NvBlastExtCutterSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params); + + +/** +Helper Radial Falloff Damage function. + +Basically it calls NvBlastActorGenerateFracture and then NvBlastActorApplyFracture with Radial Falloff shader. + +\param[in,out] actor The NvBlastActor to apply fracture to. +\param[in,out] buffers Target buffers to hold applied command events. +\param[in] damageDescBuffer Damage descriptors array. +\param[in] damageDescCount Size of damage descriptors array. +\param[in] material Material to use. +\param[in] logFn User-supplied message function (see NvBlastLog definition). May be NULL. +\param[in,out] timers If non-NULL this struct will be filled out with profiling information for the step, in profile build configurations. + +\return true iff any fracture was applied. +*/ +NVBLAST_API bool NvBlastExtDamageActorRadialFalloff(NvBlastActor* actor, NvBlastFractureBuffers* buffers, const NvBlastExtRadialDamageDesc* damageDescBuffer, uint32_t damageDescCount, const NvBlastExtMaterial* material, NvBlastLog logFn, NvBlastTimers* timers); + + +/////////////////////////////////////////////////////////////////////////////// +// Shear Damage +/////////////////////////////////////////////////////////////////////////////// + +/** +Shear Damage Desc +*/ +struct NvBlastExtShearDamageDesc +{ + float shear[3]; //!< directional damage component + float position[3]; //!< origin of damage action +}; + +/** +Shear Damage Shaders + +NOTE: The signature of shader functions are equal to NvBlastGraphShaderFunction and NvBlastSubgraphShaderFunction respectively. +They are not expected to be called directly. +@see NvBlastGraphShaderFunction, NvBlastSubgraphShaderFunction +*/ +NVBLAST_API void NvBlastExtShearGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params); +NVBLAST_API void NvBlastExtShearSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params); + + +#endif // NVBLASTEXTDAMAGESHADERS_H diff --git a/NvBlast/sdk/extensions/shaders/source/NvBlastExtRadialShaders.cpp b/NvBlast/sdk/extensions/shaders/source/NvBlastExtRadialShaders.cpp new file mode 100644 index 0000000..00d1010 --- /dev/null +++ b/NvBlast/sdk/extensions/shaders/source/NvBlastExtRadialShaders.cpp @@ -0,0 +1,205 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtDamageShaders.h" +#include "NvBlastIndexFns.h" +#include "NvBlastMath.h" +#include "NvBlastGeometry.h" +#include "NvBlastAssert.h" +#include "NvBlast.h" +#include "stdlib.h" // for abs() on linux + +using namespace Nv::Blast; +using namespace Nv::Blast::VecMath; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Profiles +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +typedef float(*ProfileFunction)(float, float, float, float); + +float falloffProfile(float min, float max, float x, float f = 1.0f) +{ + if (x > max) return 0.0f; + if (x < min) return f; + + float y = 1.0f - (x - min) / (max - min); + return y * f; +} + +float cutterProfile(float min, float max, float x, float f = 1.0f) +{ + if (x > max || x < min) return 0.0f; + + return f; +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Radial Graph Shader Template +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +template <ProfileFunction profile> +void RadialProfileGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params) +{ + const uint32_t* graphNodeIndexLinks = actor->graphNodeIndexLinks; + const uint32_t firstGraphNodeIndex = actor->firstGraphNodeIndex; + const uint32_t* adjacencyPartition = actor->adjacencyPartition; + const uint32_t* adjacentNodeIndices = actor->adjacentNodeIndices; + const uint32_t* adjacentBondIndices = actor->adjacentBondIndices; + const NvBlastBond* assetBonds = actor->assetBonds; + const float* familyBondHealths = actor->familyBondHealths; + + const NvBlastExtRadialDamageDesc* damageData = reinterpret_cast<const NvBlastExtRadialDamageDesc*>(params->damageDescBuffer); + const uint32_t damageCount = params->damageDescCount; + + uint32_t outCount = 0; + + uint32_t currentNodeIndex = firstGraphNodeIndex; + while (!Nv::Blast::isInvalidIndex(currentNodeIndex)) + { + for (uint32_t adj = adjacencyPartition[currentNodeIndex]; adj < adjacencyPartition[currentNodeIndex + 1]; adj++) + { + uint32_t adjacentNodeIndex = adjacentNodeIndices[adj]; + if (currentNodeIndex < adjacentNodeIndex) + { + uint32_t bondIndex = adjacentBondIndices[adj]; + + // skip bonds that are already broken or were visited already + // TODO: investigate why testing against health > -1.0f seems slower + // could reuse the island edge bitmap instead + if ((familyBondHealths[bondIndex] > 0.0f)) + { + + const NvBlastBond& bond = assetBonds[bondIndex]; + + float totalBondDamage = 0.0f; + + for (uint32_t damageIndex = 0; damageIndex < damageCount; damageIndex++) + { + const NvBlastExtRadialDamageDesc& damage = damageData[damageIndex]; + + float relativePosition[3]; + sub(damage.position, bond.centroid, relativePosition); + float distance = sqrtf(dot(relativePosition, relativePosition)); + + float dir[3]; + normal(relativePosition, dir); + + totalBondDamage += profile(damage.minRadius, damage.maxRadius, distance, damage.compressive); + } + + if (totalBondDamage > 0.0f) + { + NvBlastBondFractureData& outCommand = commandBuffers->bondFractures[outCount++]; + outCommand.nodeIndex0 = currentNodeIndex; + outCommand.nodeIndex1 = adjacentNodeIndex; + outCommand.health = totalBondDamage; + } + } + } + } + currentNodeIndex = graphNodeIndexLinks[currentNodeIndex]; + } + + commandBuffers->bondFractureCount = outCount; + commandBuffers->chunkFractureCount = 0; +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Radial Single Shader Template +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +template <ProfileFunction profile> +void RadialProfileSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params) +{ + uint32_t chunkFractureCount = 0; + const uint32_t chunkIndex = actor->chunkIndex; + const NvBlastChunk* assetChunks = actor->assetChunks; + const NvBlastChunk& chunk = assetChunks[chunkIndex]; + + float totalDamage = 0.0f; + for (uint32_t i = 0; i < params->damageDescCount; ++i) + { + const NvBlastExtRadialDamageDesc& damage = reinterpret_cast<const NvBlastExtRadialDamageDesc*>(params->damageDescBuffer)[i]; + + float relativePosition[3]; + sub(damage.position, chunk.centroid, relativePosition); + float distance = sqrtf(dot(relativePosition, relativePosition)); + + totalDamage += profile(damage.minRadius, damage.maxRadius, distance, damage.compressive); + } + + if (totalDamage > 0.0f) + { + NvBlastChunkFractureData& frac = commandBuffers->chunkFractures[chunkFractureCount++]; + frac.chunkIndex = chunkIndex; + frac.health = totalDamage; + } + + commandBuffers->bondFractureCount = 0; + commandBuffers->chunkFractureCount = chunkFractureCount; +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Shader Instantiation +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void NvBlastExtFalloffGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params) +{ + RadialProfileGraphShader<falloffProfile>(commandBuffers, actor, params); +} + +void NvBlastExtFalloffSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params) +{ + RadialProfileSubgraphShader<falloffProfile>(commandBuffers, actor, params); +} + +void NvBlastExtCutterGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params) +{ + RadialProfileGraphShader<cutterProfile>(commandBuffers, actor, params); +} + +void NvBlastExtCutterSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params) +{ + RadialProfileSubgraphShader<cutterProfile>(commandBuffers, actor, params); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Helper Functions +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +bool NvBlastExtDamageActorRadialFalloff(NvBlastActor* actor, NvBlastFractureBuffers* buffers, const NvBlastExtRadialDamageDesc* damageDescBuffer, uint32_t damageDescCount, const NvBlastExtMaterial* material, NvBlastLog logFn, NvBlastTimers* timers) +{ + NvBlastDamageProgram program = + { + NvBlastExtFalloffGraphShader, + NvBlastExtFalloffSubgraphShader + }; + + NvBlastProgramParams params = + { + damageDescBuffer, + damageDescCount, + material + }; + + NvBlastActorGenerateFracture(buffers, actor, program, ¶ms, logFn, timers); + if (buffers->bondFractureCount > 0 || buffers->chunkFractureCount > 0) + { + NvBlastActorApplyFracture(nullptr, actor, buffers, logFn, timers); + return true; + } + + return false; +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/shaders/source/NvBlastExtShearShaders.cpp b/NvBlast/sdk/extensions/shaders/source/NvBlastExtShearShaders.cpp new file mode 100644 index 0000000..26707e1 --- /dev/null +++ b/NvBlast/sdk/extensions/shaders/source/NvBlastExtShearShaders.cpp @@ -0,0 +1,149 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtDamageShaders.h" +#include "NvBlastIndexFns.h" +#include "NvBlastMath.h" +#include "NvBlastGeometry.h" +#include "NvBlastAssert.h" +#include "stdlib.h" // for abs() on linux + +using namespace Nv::Blast; +using namespace Nv::Blast::VecMath; + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Graph Shader +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void NvBlastExtShearGraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastGraphShaderActor* actor, const NvBlastProgramParams* params) +{ + const NvBlastExtMaterial* materialProperties = reinterpret_cast<const NvBlastExtMaterial*>(params->material); + const float graphChunkThreshold = materialProperties->graphChunkThreshold; + const float bondTangentialThreshold = materialProperties->bondTangentialThreshold; + const float damageAttenuation = 1.0f - materialProperties->damageAttenuation; + + uint32_t chunkFractureCount = 0; + uint32_t chunkFractureCountMax = commandBuffers->chunkFractureCount; + uint32_t bondFractureCount = 0; + uint32_t bondFractureCountMax = commandBuffers->bondFractureCount; + + for (uint32_t i = 0; i < params->damageDescCount; ++i) + { + const NvBlastExtShearDamageDesc& damage = reinterpret_cast<const NvBlastExtShearDamageDesc*>(params->damageDescBuffer)[i]; + + const uint32_t* graphNodeIndexLinks = actor->graphNodeIndexLinks; + const uint32_t firstGraphNodeIndex = actor->firstGraphNodeIndex; + const uint32_t* chunkIndices = actor->chunkIndices; + const uint32_t* adjacencyPartition = actor->adjacencyPartition; + const uint32_t* adjacentNodeIndices = actor->adjacentNodeIndices; + const uint32_t* adjacentBondIndices = actor->adjacentBondIndices; + const NvBlastBond* assetBonds = actor->assetBonds; + const float* familyBondHealths = actor->familyBondHealths; + + uint32_t closestNode = findNodeByPositionLinked(damage.position, firstGraphNodeIndex, graphNodeIndexLinks, adjacencyPartition, adjacentNodeIndices, adjacentBondIndices, assetBonds, familyBondHealths); + NVBLAST_ASSERT(!isInvalidIndex(closestNode)); + + float damageDir[3]; + float damageMag = VecMath::normal(damage.shear, damageDir); + + uint32_t nodeIndex = closestNode; + float maxDist = 0.0f; + uint32_t nextNode = invalidIndex<uint32_t>(); + + if (damageMag > graphChunkThreshold && chunkFractureCount < chunkFractureCountMax) + { + NvBlastChunkFractureData& frac = commandBuffers->chunkFractures[chunkFractureCount++]; + frac.chunkIndex = chunkIndices[nodeIndex]; + frac.health = damageMag * 2; + } + + do { + const uint32_t startIndex = adjacencyPartition[nodeIndex]; + const uint32_t stopIndex = adjacencyPartition[nodeIndex + 1]; + + + for (uint32_t adjacentNodeIndex = startIndex; adjacentNodeIndex < stopIndex; adjacentNodeIndex++) + { + const uint32_t neighbourIndex = adjacentNodeIndices[adjacentNodeIndex]; + const uint32_t bondIndex = adjacentBondIndices[adjacentNodeIndex]; + const NvBlastBond& bond = assetBonds[bondIndex]; + + if (!(familyBondHealths[bondIndex] > 0.0f)) + continue; + + float shear = 1 * abs(1 - abs(VecMath::dot(damage.shear, bond.normal))); + + float d[3]; VecMath::sub(bond.centroid, damage.position, d); + float ahead = VecMath::dot(d, damage.shear); + if (ahead > maxDist) + { + maxDist = ahead; + nextNode = neighbourIndex; + } + + if (shear > bondTangentialThreshold && bondFractureCount < bondFractureCountMax) + { + NvBlastBondFractureData& frac = commandBuffers->bondFractures[bondFractureCount++]; + frac.userdata = bond.userData; + frac.nodeIndex0 = nodeIndex; + frac.nodeIndex1 = neighbourIndex; + frac.health = shear; + } + } + + if (nodeIndex == nextNode) + break; + + nodeIndex = nextNode; + + damageMag *= damageAttenuation; + } while (!isInvalidIndex(nextNode)); + } + + commandBuffers->bondFractureCount = bondFractureCount; + commandBuffers->chunkFractureCount = chunkFractureCount; +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Single Shader +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void NvBlastExtShearSubgraphShader(NvBlastFractureBuffers* commandBuffers, const NvBlastSubgraphShaderActor* actor, const NvBlastProgramParams* params) +{ + const NvBlastExtMaterial* materialProperties = reinterpret_cast<const NvBlastExtMaterial*>(params->material); + + uint32_t chunkFractureCount = 0; + + float totalDamage = 0.0f; + for (uint32_t i = 0; i < params->damageDescCount; ++i) + { + const NvBlastExtShearDamageDesc& damage = reinterpret_cast<const NvBlastExtShearDamageDesc*>(params->damageDescBuffer)[i]; + + float damageDir[3]; + float damageMag = VecMath::normal(damage.shear, damageDir); + + if (damageMag > materialProperties->singleChunkThreshold) + { + totalDamage += damageMag * 2; + } + } + + if (totalDamage > 0.0f) + { + NvBlastChunkFractureData& frac = commandBuffers->chunkFractures[chunkFractureCount++]; + frac.chunkIndex = actor->chunkIndex; + frac.health = totalDamage; + } + + commandBuffers->bondFractureCount = 0; + commandBuffers->chunkFractureCount = chunkFractureCount; +} |