1/*
2 * Copyright (c) 2008-2015, NVIDIA CORPORATION. All rights reserved.
3 *
4 * NVIDIA CORPORATION and its licensors retain all intellectual property
5 * and proprietary rights in and to this software, related documentation
6 * and any modifications thereto. Any use, reproduction, disclosure or
7 * distribution of this software and related documentation without an express
8 * license agreement from NVIDIA CORPORATION is strictly prohibited.
9 */
10// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
11// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
12
13
14#ifndef PX_FOUNDATION_PX_PLANE_H
15#define PX_FOUNDATION_PX_PLANE_H
16
17/** \addtogroup foundation
18@{
19*/
20
21#include "foundation/PxMath.h"
22#include "foundation/PxVec3.h"
23
24#ifndef PX_DOXYGEN
25namespace physx
26{
27#endif
28
29/**
30\brief Representation of a plane.
31
32 Plane equation used: n.dot(v) + d = 0
33*/
34class PxPlane
35{
36public:
37 /**
38 \brief Constructor
39 */
40 PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane()
41 {
42 }
43
44 /**
45 \brief Constructor from a normal and a distance
46 */
47 PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane(PxReal nx, PxReal ny, PxReal nz, PxReal distance)
48 : n(nx, ny, nz)
49 , d(distance)
50 {
51 }
52
53 /**
54 \brief Constructor from a normal and a distance
55 */
56 PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane(const PxVec3& normal, PxReal distance)
57 : n(normal)
58 , d(distance)
59 {
60 }
61
62
63 /**
64 \brief Constructor from a point on the plane and a normal
65 */
66 PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane(const PxVec3& point, const PxVec3& normal)
67 : n(normal)
68 , d(-point.dot(n)) // p satisfies normal.dot(p) + d = 0
69 {
70 }
71
72 /**
73 \brief Constructor from three points
74 */
75 PX_CUDA_CALLABLE PX_FORCE_INLINE PxPlane(const PxVec3& p0, const PxVec3& p1, const PxVec3& p2)
76 {
77 n = (p1 - p0).cross(p2 - p0).getNormalized();
78 d = -p0.dot(n);
79 }
80
81 /**
82 \brief returns true if the two planes are exactly equal
83 */
84 PX_CUDA_CALLABLE PX_INLINE bool operator==(const PxPlane& p) const { return n == p.n && d == p.d; }
85
86 PX_CUDA_CALLABLE PX_FORCE_INLINE PxReal distance(const PxVec3& p) const
87 {
88 return p.dot(n) + d;
89 }
90
91 PX_CUDA_CALLABLE PX_FORCE_INLINE bool contains(const PxVec3& p) const
92 {
93 return PxAbs(distance(p)) < (1.0e-7f);
94 }
95
96 /**
97 \brief projects p into the plane
98 */
99 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 project(const PxVec3 & p) const
100 {
101 return p - n * distance(p);
102 }
103
104 /**
105 \brief find an arbitrary point in the plane
106 */
107 PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 pointInPlane() const
108 {
109 return -n*d;
110 }
111
112 /**
113 \brief equivalent plane with unit normal
114 */
115
116 PX_CUDA_CALLABLE PX_FORCE_INLINE void normalize()
117 {
118 PxReal denom = 1.0f / n.magnitude();
119 n *= denom;
120 d *= denom;
121 }
122
123
124 PxVec3 n; //!< The normal to the plane
125 PxReal d; //!< The distance from the origin
126};
127
128#ifndef PX_DOXYGEN
129} // namespace physx
130#endif
131
132/** @} */
133#endif
134
135