HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plane.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_GF_PLANE_H
8 #define PXR_BASE_GF_PLANE_H
9 
10 /// \file gf/plane.h
11 /// \ingroup group_gf_BasicGeometry
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/gf/vec3d.h"
15 #include "pxr/base/gf/api.h"
16 
17 #include <iosfwd>
18 #include <vector>
19 
21 
22 class GfRange3d;
23 class GfMatrix4d;
24 class GfVec4d;
25 
26 /// \class GfPlane
27 /// \ingroup group_gf_BasicGeometry
28 ///
29 /// Basic type: 3-dimensional plane
30 ///
31 /// This class represents a three-dimensional plane as a normal vector
32 /// and the distance of the plane from the origin, measured along the
33 /// normal. The plane can also be used to represent a half-space: the
34 /// side of the plane in the direction of the normal.
35 ///
36 class GfPlane
37 {
38  public:
39 
40  /// The default constructor leaves the plane parameters undefined.
41  GfPlane() {
42  }
43 
44  /// This constructor sets this to the plane perpendicular to \p normal and
45  /// at \p distance units from the origin. The passed-in normal is
46  /// normalized to unit length first.
47  GfPlane(const GfVec3d &normal, double distanceToOrigin) {
48  Set(normal, distanceToOrigin);
49  }
50 
51  /// This constructor sets this to the plane perpendicular to \p normal and
52  /// that passes through \p point. The passed-in normal is normalized to
53  /// unit length first.
54  GfPlane(const GfVec3d &normal, const GfVec3d &point) {
55  Set(normal, point);
56  }
57 
58  /// This constructor sets this to the plane that contains the three given
59  /// points. The normal is constructed from the cross product of (\p p1 -
60  /// \p p0) (\p p2 - \p p0). Results are undefined if the points are
61  /// collinear.
62  GfPlane(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2) {
63  Set(p0, p1, p2);
64  }
65 
66  /// This constructor creates a plane given by the equation
67  /// \p eqn[0] * x + \p eqn[1] * y + \p eqn[2] * z + \p eqn[3] = 0.
68  GfPlane(const GfVec4d &eqn) {
69  Set(eqn);
70  }
71 
72  /// Sets this to the plane perpendicular to \p normal and at \p distance
73  /// units from the origin. The passed-in normal is normalized to unit
74  /// length first.
75  void Set(const GfVec3d &normal, double distanceToOrigin) {
76  _normal = normal.GetNormalized();
77  _distance = distanceToOrigin;
78  }
79 
80  /// This constructor sets this to the plane perpendicular to \p normal and
81  /// that passes through \p point. The passed-in normal is normalized to
82  /// unit length first.
83  GF_API
84  void Set(const GfVec3d &normal, const GfVec3d &point);
85 
86  /// This constructor sets this to the plane that contains the three given
87  /// points. The normal is constructed from the cross product of (\p p1 -
88  /// \p p0) (\p p2 - \p p0). Results are undefined if the points are
89  /// collinear.
90  GF_API
91  void Set(const GfVec3d &p0,
92  const GfVec3d &p1,
93  const GfVec3d &p2);
94 
95  /// This method sets this to the plane given by the equation
96  /// \p eqn[0] * x + \p eqn[1] * y + \p eqn[2] * z + \p eqn[3] = 0.
97  GF_API
98  void Set(const GfVec4d &eqn);
99 
100  /// Returns the unit-length normal vector of the plane.
101  const GfVec3d & GetNormal() const {
102  return _normal;
103  }
104 
105  /// Returns the distance of the plane from the origin.
106  double GetDistanceFromOrigin() const {
107  return _distance;
108  }
109 
110  /// Give the coefficients of the equation of the plane. Suitable
111  /// to OpenGL calls to set the clipping plane.
112  GF_API
113  GfVec4d GetEquation() const;
114 
115  /// Component-wise equality test. The normals and distances must match
116  /// exactly for planes to be considered equal.
117  bool operator ==(const GfPlane &p) const {
118  return (_normal == p._normal &&
119  _distance == p._distance);
120  }
121 
122  /// Component-wise inequality test. The normals and distances must match
123  /// exactly for planes to be considered equal.
124  bool operator !=(const GfPlane &p) const {
125  return ! (*this == p);
126  }
127 
128  /// Returns the distance of point \p from the plane. This distance will be
129  /// positive if the point is on the side of the plane containing the
130  /// normal.
131  double GetDistance(const GfVec3d &p) const {
132  return p * _normal - _distance;
133  }
134 
135  /// Return the projection of \p p onto the plane.
136  GfVec3d Project(const GfVec3d& p) const {
137  return p - GetDistance(p) * _normal;
138  }
139 
140  /// Transforms the plane by the given matrix.
141  GF_API
142  GfPlane & Transform(const GfMatrix4d &matrix);
143 
144  /// Flip the plane normal (if necessary) so that \p p is in the positive
145  /// halfspace.
146  void Reorient(const GfVec3d& p) {
147  if (GetDistance(p) < 0) {
148  _normal = -_normal;
149  _distance = -_distance;
150  }
151  }
152 
153  /// Returns \c true if the given aligned bounding box is at least
154  /// partially on the positive side (the one the normal points into) of the
155  /// plane.
156  GF_API
157  bool IntersectsPositiveHalfSpace(const GfRange3d &box) const;
158 
159  /// Returns true if the given point is on the plane or within its positive
160  /// half space.
161  bool IntersectsPositiveHalfSpace(const GfVec3d &pt) const {
162  return GetDistance(pt) >= 0.0;
163  }
164 
165  private:
166  /// The normal to the plane. Points in direction of half-space.
167  GfVec3d _normal;
168 
169  /// Distance from the plane to the origin.
170  double _distance;
171 };
172 
173 /// Fits a plane to the given \p points. There must be at least three points in
174 /// order to fit the plane; if the size of \p points is less than three, this
175 /// issues a coding error.
176 ///
177 /// If the \p points are all collinear, then no plane can be determined, and
178 /// this function returns \c false. Otherwise, if the fitting is successful,
179 /// it returns \c true and sets \p *fitPlane to the fitted plane. If \p points
180 /// contains exactly three points, then the resulting plane is the exact plane
181 /// defined by the three points. If \p points contains more than three points,
182 /// then this function determines the best-fitting plane for the given points.
183 /// The orientation of the plane normal is arbitrary with regards to the
184 /// plane's positive and negative half-spaces; you can use GfPlane::Reorient()
185 /// to flip the plane if necessary.
186 ///
187 /// The current implementation uses linear least squares and thus defines
188 /// "best-fitting" as minimizing the sum of the squares of the vertical
189 /// distances between points and the plane surface.
190 GF_API
191 bool GfFitPlaneToPoints(const std::vector<GfVec3d>& points, GfPlane* fitPlane);
192 
193 /// Output a GfPlane using the format [(nx ny nz) distance].
194 /// \ingroup group_gf_DebuggingOutput
195 GF_API std::ostream& operator<<(std::ostream&, const GfPlane&);
196 
198 
199 #endif // PXR_BASE_GF_PLANE_H
GF_API GfPlane & Transform(const GfMatrix4d &matrix)
Transforms the plane by the given matrix.
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glad.h:2676
GfPlane(const GfVec3d &normal, const GfVec3d &point)
Definition: plane.h:54
bool operator==(const GfPlane &p) const
Definition: plane.h:117
bool IntersectsPositiveHalfSpace(const GfVec3d &pt) const
Definition: plane.h:161
bool operator!=(const GfPlane &p) const
Definition: plane.h:124
void Set(const GfVec3d &normal, double distanceToOrigin)
Definition: plane.h:75
GfPlane(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2)
Definition: plane.h:62
Definition: vec4d.h:45
GF_API bool IntersectsPositiveHalfSpace(const GfRange3d &box) const
Definition: plane.h:36
void Reorient(const GfVec3d &p)
Definition: plane.h:146
const GfVec3d & GetNormal() const
Returns the unit-length normal vector of the plane.
Definition: plane.h:101
GF_API bool GfFitPlaneToPoints(const std::vector< GfVec3d > &points, GfPlane *fitPlane)
double GetDistance(const GfVec3d &p) const
Definition: plane.h:131
double GetDistanceFromOrigin() const
Returns the distance of the plane from the origin.
Definition: plane.h:106
GfVec3d Project(const GfVec3d &p) const
Return the projection of p onto the plane.
Definition: plane.h:136
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
GfPlane()
The default constructor leaves the plane parameters undefined.
Definition: plane.h:41
Definition: vec3d.h:45
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GfPlane(const GfVec4d &eqn)
Definition: plane.h:68
GF_API std::ostream & operator<<(std::ostream &, const GfPlane &)
GfPlane(const GfVec3d &normal, double distanceToOrigin)
Definition: plane.h:47
GF_API GfVec4d GetEquation() const
#define GF_API
Definition: api.h:23
GfVec3d GetNormalized(double eps=GF_MIN_VECTOR_LENGTH) const
Definition: vec3d.h:260