| 1 | // |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions |
| 4 | // are met: |
| 5 | // * Redistributions of source code must retain the above copyright |
| 6 | // notice, this list of conditions and the following disclaimer. |
| 7 | // * Redistributions in binary form must reproduce the above copyright |
| 8 | // notice, this list of conditions and the following disclaimer in the |
| 9 | // documentation and/or other materials provided with the distribution. |
| 10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
| 11 | // contributors may be used to endorse or promote products derived |
| 12 | // from this software without specific prior written permission. |
| 13 | // |
| 14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
| 15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
| 27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
| 28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
| 29 | |
| 30 | |
| 31 | #ifndef PX_PHYSICS_GEOMUTILS_PX_TRIANGLE |
| 32 | #define PX_PHYSICS_GEOMUTILS_PX_TRIANGLE |
| 33 | /** \addtogroup geomutils |
| 34 | @{ |
| 35 | */ |
| 36 | |
| 37 | #include "common/PxPhysXCommonConfig.h" |
| 38 | #include "foundation/PxVec3.h" |
| 39 | |
| 40 | #if !PX_DOXYGEN |
| 41 | namespace physx |
| 42 | { |
| 43 | #endif |
| 44 | |
| 45 | /** |
| 46 | \brief Triangle class. |
| 47 | */ |
| 48 | class PxTriangle |
| 49 | { |
| 50 | public: |
| 51 | /** |
| 52 | \brief Constructor |
| 53 | */ |
| 54 | PX_FORCE_INLINE PxTriangle() {} |
| 55 | |
| 56 | /** |
| 57 | \brief Constructor |
| 58 | |
| 59 | \param[in] p0 Point 0 |
| 60 | \param[in] p1 Point 1 |
| 61 | \param[in] p2 Point 2 |
| 62 | */ |
| 63 | PX_FORCE_INLINE PxTriangle(const PxVec3& p0, const PxVec3& p1, const PxVec3& p2) |
| 64 | { |
| 65 | verts[0] = p0; |
| 66 | verts[1] = p1; |
| 67 | verts[2] = p2; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | \brief Copy constructor |
| 72 | |
| 73 | \param[in] triangle Tri to copy |
| 74 | */ |
| 75 | PX_FORCE_INLINE PxTriangle(const PxTriangle& triangle) |
| 76 | { |
| 77 | verts[0] = triangle.verts[0]; |
| 78 | verts[1] = triangle.verts[1]; |
| 79 | verts[2] = triangle.verts[2]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | \brief Destructor |
| 84 | */ |
| 85 | PX_FORCE_INLINE ~PxTriangle() {} |
| 86 | |
| 87 | /** |
| 88 | \brief Assignment operator |
| 89 | */ |
| 90 | PX_FORCE_INLINE void operator=(const PxTriangle& triangle) |
| 91 | { |
| 92 | verts[0] = triangle.verts[0]; |
| 93 | verts[1] = triangle.verts[1]; |
| 94 | verts[2] = triangle.verts[2]; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | \brief Compute the normal of the Triangle. |
| 99 | |
| 100 | \param[out] _normal Triangle normal. |
| 101 | */ |
| 102 | PX_FORCE_INLINE void normal(PxVec3& _normal) const |
| 103 | { |
| 104 | _normal = (verts[1]-verts[0]).cross(v: verts[2]-verts[0]); |
| 105 | _normal.normalize(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | \brief Compute the unnormalized normal of the triangle. |
| 110 | |
| 111 | \param[out] _normal Triangle normal (not normalized). |
| 112 | */ |
| 113 | PX_FORCE_INLINE void denormalizedNormal(PxVec3& _normal) const |
| 114 | { |
| 115 | _normal = (verts[1]-verts[0]).cross(v: verts[2]-verts[0]); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | \brief Compute the area of the triangle. |
| 120 | |
| 121 | \return Area of the triangle. |
| 122 | */ |
| 123 | PX_FORCE_INLINE PxReal area() const |
| 124 | { |
| 125 | const PxVec3& p0 = verts[0]; |
| 126 | const PxVec3& p1 = verts[1]; |
| 127 | const PxVec3& p2 = verts[2]; |
| 128 | return ((p0 - p1).cross(v: p0 - p2)).magnitude() * 0.5f; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | \return Computes a point on the triangle from u and v barycentric coordinates. |
| 133 | */ |
| 134 | PxVec3 pointFromUV(PxReal u, PxReal v) const { return (1.0f-u-v)*verts[0] + u*verts[1] + v*verts[2]; } |
| 135 | |
| 136 | /** |
| 137 | \brief Array of Vertices. |
| 138 | */ |
| 139 | PxVec3 verts[3]; |
| 140 | |
| 141 | }; |
| 142 | |
| 143 | |
| 144 | #if !PX_DOXYGEN |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | /** @} */ |
| 149 | #endif |
| 150 | |