RavEngine
Loading...
Searching...
No Matches
MeshGenerator.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef PHYSX_MESHGENERATOR_H
30#define PHYSX_MESHGENERATOR_H
31
32#include "PxPhysicsAPI.h"
33#include "extensions/PxRemeshingExt.h"
34
35namespace meshgenerator
36{
37using namespace physx;
38
39void createCube(PxArray<PxVec3>& triVerts, PxArray<PxU32>& triIndices, const PxVec3& pos, PxReal scaling)
40{
41 triVerts.clear();
42 triIndices.clear();
43 triVerts.pushBack(scaling * PxVec3(0.5f, -0.5f, -0.5f) + pos);
44 triVerts.pushBack(scaling * PxVec3(0.5f, -0.5f, 0.5f) + pos);
45 triVerts.pushBack(scaling * PxVec3(-0.5f, -0.5f, 0.5f) + pos);
46 triVerts.pushBack(scaling * PxVec3(-0.5f, -0.5f, -0.5f) + pos);
47 triVerts.pushBack(scaling * PxVec3(0.5f, 0.5f, -0.5f) + pos);
48 triVerts.pushBack(scaling * PxVec3(0.5f, 0.5f, 0.5f) + pos);
49 triVerts.pushBack(scaling * PxVec3(-0.5f, 0.5f, 0.5f) + pos);
50 triVerts.pushBack(scaling * PxVec3(-0.5f, 0.5f, -0.5f) + pos);
51
52 triIndices.pushBack(1); triIndices.pushBack(2); triIndices.pushBack(3);
53 triIndices.pushBack(7); triIndices.pushBack(6); triIndices.pushBack(5);
54 triIndices.pushBack(4); triIndices.pushBack(5); triIndices.pushBack(1);
55 triIndices.pushBack(5); triIndices.pushBack(6); triIndices.pushBack(2);
56
57 triIndices.pushBack(2); triIndices.pushBack(6); triIndices.pushBack(7);
58 triIndices.pushBack(0); triIndices.pushBack(3); triIndices.pushBack(7);
59 triIndices.pushBack(0); triIndices.pushBack(1); triIndices.pushBack(3);
60 triIndices.pushBack(4); triIndices.pushBack(7); triIndices.pushBack(5);
61
62 triIndices.pushBack(0); triIndices.pushBack(4); triIndices.pushBack(1);
63 triIndices.pushBack(1); triIndices.pushBack(5); triIndices.pushBack(2);
64 triIndices.pushBack(3); triIndices.pushBack(2); triIndices.pushBack(7);
65 triIndices.pushBack(4); triIndices.pushBack(0); triIndices.pushBack(7);
66}
67
68void createConeY(PxArray<PxVec3>& triVerts, PxArray<PxU32>& triIndices, const PxVec3& center, PxReal radius, PxReal height, PxU32 numPointsOnRing = 32)
69{
70 triVerts.clear();
71 triIndices.clear();
72 for (PxU32 i = 0; i < numPointsOnRing; ++i)
73 {
74 PxReal angle = i * 2.0f * 3.1415926535898f / numPointsOnRing;
75 triVerts.pushBack(center + radius * PxVec3(PxSin(angle), 0, PxCos(angle)));
76 }
77
78 triVerts.pushBack(center);
79 triVerts.pushBack(center + PxVec3(0, height, 0));
80 for (PxU32 i = 0; i < numPointsOnRing; ++i)
81 {
82 triIndices.pushBack(numPointsOnRing); triIndices.pushBack(i); triIndices.pushBack((i + 1) % numPointsOnRing);
83 triIndices.pushBack(numPointsOnRing + 1); triIndices.pushBack((i + 1) % numPointsOnRing); triIndices.pushBack(i);
84 }
85}
86
87void projectPointsOntoSphere(PxArray<PxVec3>& triVerts, const PxVec3& center, PxReal radius)
88{
89 for (PxU32 i = 0; i < triVerts.size(); ++i)
90 {
91 PxVec3 dir = triVerts[i] - center;
92 dir.normalize();
93 triVerts[i] = center + radius * dir;
94 }
95}
96
97void createSphere(PxArray<PxVec3>& triVerts, PxArray<PxU32>& triIndices, const PxVec3& center, PxReal radius, const PxReal maxEdgeLength)
98{
99 triVerts.clear();
100 triIndices.clear();
101 createCube(triVerts, triIndices, center, radius);
102 projectPointsOntoSphere(triVerts, center, radius);
103 while (PxRemeshingExt::limitMaxEdgeLength(triIndices, triVerts, maxEdgeLength, 1))
104 projectPointsOntoSphere(triVerts, center, radius);
105}
106}
107
108#endif
Definition PxArray.h:53
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_INLINE void clear()
Definition PxArray.h:250
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
static bool limitMaxEdgeLength(PxArray< PxU32 > &triangles, PxArray< PxVec3 > &points, PxReal maxEdgeLength, PxU32 maxIterations=100, PxArray< PxU32 > *triangleMap=NULL, PxU32 triangleCountThreshold=0xFFFFFFFF)
Processes a triangle mesh and makes sure that no triangle edge is longer than the maximal edge length...
Definition ExtRemeshingExt.cpp:151
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float normalize()
normalizes the vector in place
Definition PxVec3.h:300
GLM_FUNC_DECL T angle(qua< T, Q > const &x)
Definition quaternion_trigonometric.inl:6
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxCos(float a)
Cosine of an angle (Unit: Radians)
Definition PxMath.h:190
PX_CUDA_CALLABLE PX_FORCE_INLINE float PxSin(float a)
trigonometry – all angles are in radians.
Definition PxMath.h:178