RavEngine
Loading...
Searching...
No Matches
GuTriangle.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 GU_TRIANGLE_H
30#define GU_TRIANGLE_H
31
32#include "foundation/PxVec3.h"
33#include "foundation/PxUtilities.h"
34#include "foundation/PxUserAllocated.h"
35
36namespace physx
37{
38namespace Gu
39{
40 // PT: I'm taking back control of these files and re-introducing the "ICE" naming conventions:
41 // - "Triangle" is for actual triangles (like the PxTriangle class)
42 // - If it contains vertex indices, it's "IndexedTriangle".
43 // - "v" is too ambiguous (it could be either an actual vertex or a vertex reference) so use "ref" instead.
44 // Plus we sometimes reference edges, not vertices, so "v" is too restrictive.
45
46 template <class T>
48 {
50 PX_INLINE IndexedTriangleT (T a, T b, T c) { mRef[0] = a; mRef[1] = b; mRef[2] = c; }
51 template <class TX>
52 PX_INLINE IndexedTriangleT (const IndexedTriangleT <TX>& other) { mRef[0] = other[0]; mRef[1] = other[1]; mRef[2] = other[2]; }
53
54 PX_INLINE T& operator[](T i) { return mRef[i]; }
55 PX_INLINE const T& operator[](T i) const { return mRef[i]; }
56
57 template<class TX>//any type of IndexedTriangleT <>, possibly with different T
58 PX_INLINE IndexedTriangleT <T>& operator=(const IndexedTriangleT <TX>& i) { mRef[0]=i[0]; mRef[1]=i[1]; mRef[2]=i[2]; return *this; }
59
60 void flip()
61 {
62 PxSwap(mRef[1], mRef[2]);
63 }
64
65 PX_INLINE bool contains(T id) const
66 {
67 return mRef[0] == id || mRef[1] == id || mRef[2] == id;
68 }
69
70 PX_INLINE void center(const PxVec3* verts, PxVec3& center) const
71 {
72 const PxVec3& p0 = verts[mRef[0]];
73 const PxVec3& p1 = verts[mRef[1]];
74 const PxVec3& p2 = verts[mRef[2]];
75 center = (p0+p1+p2)*0.33333333333333333333f;
76 }
77
78 float area(const PxVec3* verts) const
79 {
80 const PxVec3& p0 = verts[mRef[0]];
81 const PxVec3& p1 = verts[mRef[1]];
82 const PxVec3& p2 = verts[mRef[2]];
83 return ((p0-p1).cross(p0-p2)).magnitude() * 0.5f;
84 }
85
86 PxU8 findEdge(T vref0, T vref1) const
87 {
88 if(mRef[0]==vref0 && mRef[1]==vref1) return 0;
89 else if(mRef[0]==vref1 && mRef[1]==vref0) return 0;
90 else if(mRef[0]==vref0 && mRef[2]==vref1) return 1;
91 else if(mRef[0]==vref1 && mRef[2]==vref0) return 1;
92 else if(mRef[1]==vref0 && mRef[2]==vref1) return 2;
93 else if(mRef[1]==vref1 && mRef[2]==vref0) return 2;
94 return 0xff;
95 }
96
97 // counter clock wise order
98 PxU8 findEdgeCCW(T vref0, T vref1) const
99 {
100 if(mRef[0]==vref0 && mRef[1]==vref1) return 0;
101 else if(mRef[0]==vref1 && mRef[1]==vref0) return 0;
102 else if(mRef[0]==vref0 && mRef[2]==vref1) return 2;
103 else if(mRef[0]==vref1 && mRef[2]==vref0) return 2;
104 else if(mRef[1]==vref0 && mRef[2]==vref1) return 1;
105 else if(mRef[1]==vref1 && mRef[2]==vref0) return 1;
106 return 0xff;
107 }
108
109 bool replaceVertex(T oldref, T newref)
110 {
111 if(mRef[0]==oldref) { mRef[0] = newref; return true; }
112 else if(mRef[1]==oldref) { mRef[1] = newref; return true; }
113 else if(mRef[2]==oldref) { mRef[2] = newref; return true; }
114 return false;
115 }
116
117 bool isDegenerate() const
118 {
119 if(mRef[0]==mRef[1]) return true;
120 if(mRef[1]==mRef[2]) return true;
121 if(mRef[2]==mRef[0]) return true;
122 return false;
123 }
124
125 PX_INLINE void denormalizedNormal(const PxVec3* verts, PxVec3& normal) const
126 {
127 const PxVec3& p0 = verts[mRef[0]];
128 const PxVec3& p1 = verts[mRef[1]];
129 const PxVec3& p2 = verts[mRef[2]];
130 normal = ((p2 - p1).cross(p0 - p1));
131 }
132
133 T mRef[3]; //vertex indices
134 };
135
138
141}
142}
143
144#endif
Definition PxUserAllocated.h:43
3 Element vector class.
Definition PxVec3.h:50
PX_CUDA_CALLABLE PX_FORCE_INLINE float magnitude() const
returns the magnitude
Definition PxVec3.h:183
#define PX_COMPILE_TIME_ASSERT(exp)
Definition PxPreprocessor.h:428
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition GuTriangle.h:48