plane.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_GF_PLANE_H
25 #define PXR_BASE_GF_PLANE_H
26 
29 
30 #include "pxr/pxr.h"
31 #include "pxr/base/gf/vec3d.h"
32 #include "pxr/base/gf/api.h"
33 
34 #include <iosfwd>
35 #include <vector>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
39 class GfRange3d;
40 class GfMatrix4d;
41 class GfVec4d;
42 
53 class GfPlane
54 {
55  public:
56 
58  GfPlane() {
59  }
60 
64  GfPlane(const GfVec3d &normal, double distanceToOrigin) {
65  Set(normal, distanceToOrigin);
66  }
67 
71  GfPlane(const GfVec3d &normal, const GfVec3d &point) {
72  Set(normal, point);
73  }
74 
79  GfPlane(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2) {
80  Set(p0, p1, p2);
81  }
82 
85  GfPlane(const GfVec4d &eqn) {
86  Set(eqn);
87  }
88 
92  void Set(const GfVec3d &normal, double distanceToOrigin) {
93  _normal = normal.GetNormalized();
94  _distance = distanceToOrigin;
95  }
96 
100  GF_API
101  void Set(const GfVec3d &normal, const GfVec3d &point);
102 
107  GF_API
108  void Set(const GfVec3d &p0,
109  const GfVec3d &p1,
110  const GfVec3d &p2);
111 
114  GF_API
115  void Set(const GfVec4d &eqn);
116 
118  const GfVec3d & GetNormal() const {
119  return _normal;
120  }
121 
123  double GetDistanceFromOrigin() const {
124  return _distance;
125  }
126 
129  GF_API
130  GfVec4d GetEquation() const;
131 
134  bool operator ==(const GfPlane &p) const {
135  return (_normal == p._normal &&
136  _distance == p._distance);
137  }
138 
141  bool operator !=(const GfPlane &p) const {
142  return ! (*this == p);
143  }
144 
148  double GetDistance(const GfVec3d &p) const {
149  return p * _normal - _distance;
150  }
151 
153  GfVec3d Project(const GfVec3d& p) const {
154  return p - GetDistance(p) * _normal;
155  }
156 
158  GF_API
159  GfPlane & Transform(const GfMatrix4d &matrix);
160 
163  void Reorient(const GfVec3d& p) {
164  if (GetDistance(p) < 0) {
165  _normal = -_normal;
166  _distance = -_distance;
167  }
168  }
169 
173  GF_API
174  bool IntersectsPositiveHalfSpace(const GfRange3d &box) const;
175 
178  bool IntersectsPositiveHalfSpace(const GfVec3d &pt) const {
179  return GetDistance(pt) >= 0.0;
180  }
181 
182  private:
184  GfVec3d _normal;
185 
187  double _distance;
188 };
189 
207 GF_API
208 bool GfFitPlaneToPoints(const std::vector<GfVec3d>& points, GfPlane* fitPlane);
209 
212 GF_API std::ostream& operator<<(std::ostream&, const GfPlane&);
213 
214 PXR_NAMESPACE_CLOSE_SCOPE
215 
216 #endif // PXR_BASE_GF_PLANE_H
GfPlane(const GfVec3d &normal, const GfVec3d &point)
This constructor sets this to the plane perpendicular to normal and that passes through point.
Definition: plane.h:71
bool operator !=(const GfPlane &p) const
Component-wise inequality test.
Definition: plane.h:141
Basic type: 3-dimensional floating point range.
Definition: range3d.h:64
GfPlane(const GfVec3d &normal, double distanceToOrigin)
This constructor sets this to the plane perpendicular to normal and at distance units from the origin...
Definition: plane.h:64
double GetDistanceFromOrigin() const
Returns the distance of the plane from the origin.
Definition: plane.h:123
GfPlane(const GfVec4d &eqn)
This constructor creates a plane given by the equation eqn[0] * x + eqn[1] * y + eqn[2] * z + eqn[3] ...
Definition: plane.h:85
GF_API std::ostream & operator<<(std::ostream &, const GfPlane &)
Output a GfPlane using the format [(nx ny nz) distance].
Basic type for a vector of 4 double components.
Definition: vec4d.h:63
Basic type: 3-dimensional plane.
Definition: plane.h:53
Stores a 4x4 matrix of double elements.
Definition: matrix4d.h:88
void Reorient(const GfVec3d &p)
Flip the plane normal (if necessary) so that p is in the positive halfspace.
Definition: plane.h:163
GF_API bool GfFitPlaneToPoints(const std::vector< GfVec3d > &points, GfPlane *fitPlane)
Fits a plane to the given points.
GF_API bool IntersectsPositiveHalfSpace(const GfRange3d &box) const
Returns true if the given aligned bounding box is at least partially on the positive side (the one th...
double GetDistance(const GfVec3d &p) const
Returns the distance of point from the plane.
Definition: plane.h:148
GfPlane(const GfVec3d &p0, const GfVec3d &p1, const GfVec3d &p2)
This constructor sets this to the plane that contains the three given points.
Definition: plane.h:79
GfVec3d Project(const GfVec3d &p) const
Return the projection of p onto the plane.
Definition: plane.h:153
GF_API GfPlane & Transform(const GfMatrix4d &matrix)
Transforms the plane by the given matrix.
Basic type for a vector of 3 double components.
Definition: vec3d.h:63
bool operator==(const GfPlane &p) const
Component-wise equality test.
Definition: plane.h:134
GfPlane()
The default constructor leaves the plane parameters undefined.
Definition: plane.h:58
GF_API GfVec4d GetEquation() const
Give the coefficients of the equation of the plane.
const GfVec3d & GetNormal() const
Returns the unit-length normal vector of the plane.
Definition: plane.h:118
bool IntersectsPositiveHalfSpace(const GfVec3d &pt) const
Returns true if the given point is on the plane or within its positive half space.
Definition: plane.h:178
void Set(const GfVec3d &normal, double distanceToOrigin)
Sets this to the plane perpendicular to normal and at distance units from the origin.
Definition: plane.h:92