HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Plane.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Plane.h (UT Library, C++)
7  *
8  * COMMENTS:
9  * A 3D plane.
10  *
11  */
12 
13 #ifndef __UT_Plane_h__
14 #define __UT_Plane_h__
15 
16 #include "UT_API.h"
17 #include "UT_Assert.h"
18 #include "UT_Axis.h"
19 #include "UT_Vector3.h"
20 #include "UT_VectorTypes.h"
21 #include <SYS/SYS_Deprecated.h>
22 #include <SYS/SYS_Types.h>
23 
24 // The 3 classic types of planes. Evidently, a UT_PlaneT object can be
25 // an arbitrary plane, but it helps to have some defaults.
27 {
31 };
32 
33 template <typename T>
34 class UT_PlaneT
35 {
36 public:
37 
38  // Several constructors that define a plane as (X - point)*normal = 0
39  UT_PlaneT(
40  UT_PlaneType plane_type = UT_PLANE_XY);
41  UT_PlaneT(
42  const UT_Vector3T<T> &pt,
43  UT_PlaneType plane_type = UT_PLANE_XY);
44  UT_PlaneT(
45  const UT_Vector3T<T> &apoint,
46  const UT_Vector3T<T> &norm_dir,
47  bool norm = true);
48  UT_PlaneT(
49  const UT_Vector3T<T> &p0,
50  const UT_Vector3T<T> &p1,
51  const UT_Vector3T<T> &p2);
52  UT_PlaneT(
53  const UT_Vector4T<T> &p);
54 
55  ~UT_PlaneT();
56 
57  // Intersect a line defined parametrically as 'p + t*v' with this plane.
58  // If the line runs parallel to the plane, both methods return "max"
59  // as the intersection point. The second method also returns -1; if not
60  // parallel, it returns 0.
62  const UT_Vector3T<T> &p,
63  const UT_Vector3T<T> &v) const;
64  int intersectLine(
65  const UT_Vector3T<T> &p,
66  const UT_Vector3T<T> &v,
67  UT_Vector3T<T> &hit) const;
68  int intersectRay(
69  const UT_Vector3T<T> &p,
70  const UT_Vector3T<T> &v,
71  UT_Vector3T<T> &hit) const;
72 
73  // We occasionally need to find the intersection of a line with this
74  // plane shifted to another position (ie. myPoint changes but myNormal
75  // doesn't). offset_pt is a point on the shifted plane.
76  int intersectLine(
77  const UT_Vector3T<T> &offset_pt,
78  const UT_Vector3T<T> &p,
79  const UT_Vector3T<T> &v,
80  UT_Vector3T<T> &hit) const;
81  int intersectRay(
82  const UT_Vector3T<T> &offset_pt,
83  const UT_Vector3T<T> &p,
84  const UT_Vector3T<T> &v,
85  UT_Vector3T<T> &hit) const;
86 
87  // Find the projection of point p on the plane. The second method
88  // returns the signed distance to the plane.
89  UT_Vector3T<T> project(const UT_Vector3T<T> &p) const;
90  T project(
91  const UT_Vector3T<T> &p,
92  UT_Vector3T<T> &projection) const;
93 
94  // Find the point symmetric to p and store it in p:
95  void symmetry(UT_Vector3T<T> &p) const;
96 
97  // Find out if the plane contains the point p and return true or false.
98  bool contains(const UT_Vector3T<T> &p) const;
99 
100  // Finds if the given point is on one side or the other. The side
101  // in direction of the normal is 1, the other is 0.
102  int side(const UT_Vector3T<T> &p) const;
103 
104  /// Returns the signed distance to the plane. Negative is inside,
105  /// positive is outside. The normal points outwards.
106  T distance(const UT_Vector3T<T> &p) const;
107 
108  // Query the point and the normal;
109  const UT_Vector3T<T> & point() const
110  { return myPoint; }
111  void setPoint(const UT_Vector3T<T> &p)
112  { myPoint = p; }
113  const UT_Vector3T<T> & normal() const
114  { return myNormal; }
116  {
117  myNormal = n;
118  UT_ASSERT(SYSisEqual(myNormal.length2(), 1));
119  }
121  {
122  myNormal = n;
123  myNormal.normalize();
124  }
125  void setNormal(UT_PlaneType plane_type)
126  {
127  switch (plane_type)
128  {
129  case UT_PLANE_XY:
130  myNormal.assign(0.0, 0.0, 1.0);
131  break;
132  case UT_PLANE_YZ:
133  myNormal.assign(1.0, 0.0, 0.0);
134  break;
135  default:
136  UT_ASSERT(!"Invalid plane type");
138  case UT_PLANE_XZ:
139  myNormal.assign(0.0, 1.0, 0.0);
140  break;
141  }
142  }
143 
144  // Flip by 180 degrees:
145  void negate();
146 
147  // Rotate by a given angle, expressed in radians:
148  void rotate(
149  UT_Vector3T<T> &axis,
150  T theta,
151  bool norm = true);
152  void rotate(UT_Axis3::axis a, T theta);
153 
154  // Only rotate the point or normal
155  void rotatePoint(const UT_Matrix3T<T> &rot_matx)
156  {
157  myPoint *= rot_matx;
158  }
159  void rotateNormal(const UT_Matrix3T<T> &rot_matx,
160  bool norm = true)
161  {
162  myNormal *= rot_matx;
163  if (norm)
164  myNormal.normalize();
165  }
166 
167  // Apply an invertible transformation to the plane.
168  void transform(const UT_Matrix4F &matx);
169  void transform(const UT_Matrix4D &matx);
170 
171  // Apply an invertible transformation to the plane and return the inverted
172  // and transposed matrix in the non-const argument.
175 
176  // Translate the plane along the normal by the given factor.
177  void shiftOffset(T by_factor)
178  {
179  myPoint += (myNormal * by_factor);
180  }
181 
183  {
184  myPoint += offset;
185  }
186 
187 private:
188  static const UT_Vector3T<T> theInvalidHit;
189 
190  // The defining point and normal:
191  UT_Vector3T<T> myPoint;
192  UT_Vector3T<T> myNormal;
193 };
194 
198 typedef UT_PlaneT<float> UT_Plane; // deprecated
199 
200 template <typename T>
201 UT_API size_t format(char *buffer, size_t bufsize, const UT_PlaneT<T> &p);
202 
203 #include "UT_PlaneImpl.h"
204 
205 #endif // __UT_Plane_h__
UT_Vector3T< T > project(const UT_Vector3T< T > &p) const
Definition: UT_PlaneImpl.h:224
UT_PlaneT< fpreal32 > UT_PlaneF
Definition: UT_Plane.h:196
void setVectorAsNormal(const UT_Vector3T< T > &n)
Definition: UT_Plane.h:120
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
UT_PlaneT< fpreal > UT_PlaneR
Definition: UT_Plane.h:195
const GLdouble * v
Definition: glcorearb.h:837
void rotatePoint(const UT_Matrix3T< T > &rot_matx)
Definition: UT_Plane.h:155
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define UT_API
Definition: UT_API.h:14
void setNormal(UT_PlaneType plane_type)
Definition: UT_Plane.h:125
3D Vector class.
4D Vector class.
Definition: UT_Vector4.h:174
void transform(const UT_Matrix4F &matx)
Definition: UT_PlaneImpl.h:265
const UT_Vector3T< T > & normal() const
Definition: UT_Plane.h:113
UT_Vector3T< T > intersect(const UT_Vector3T< T > &p, const UT_Vector3T< T > &v) const
Definition: UT_PlaneImpl.h:92
void setPoint(const UT_Vector3T< T > &p)
Definition: UT_Plane.h:111
GLdouble n
Definition: glcorearb.h:2008
UT_PlaneT< float > UT_Plane
Definition: UT_Plane.h:198
GLintptr offset
Definition: glcorearb.h:665
Definition: core.h:760
UT_API size_t format(char *buffer, size_t bufsize, const UT_PlaneT< T > &p)
bool contains(const UT_Vector3T< T > &p) const
Definition: UT_PlaneImpl.h:307
void setNormal(const UT_Vector3T< T > &n)
Definition: UT_Plane.h:115
#define SYS_FALLTHROUGH
Definition: SYS_Compiler.h:68
void rotateNormal(const UT_Matrix3T< T > &rot_matx, bool norm=true)
Definition: UT_Plane.h:159
int intersectLine(const UT_Vector3T< T > &p, const UT_Vector3T< T > &v, UT_Vector3T< T > &hit) const
Definition: UT_PlaneImpl.h:109
void rotate(UT_Vector3T< T > &axis, T theta, bool norm=true)
Definition: UT_PlaneImpl.h:241
void transformAndReturnNormalXform(UT_Matrix4F &matx)
Definition: UT_PlaneImpl.h:276
void translate(const UT_Vector3T< T > &offset)
Definition: UT_Plane.h:182
int side(const UT_Vector3T< T > &p) const
Definition: UT_PlaneImpl.h:321
int intersectRay(const UT_Vector3T< T > &p, const UT_Vector3T< T > &v, UT_Vector3T< T > &hit) const
Definition: UT_PlaneImpl.h:134
void symmetry(UT_Vector3T< T > &p) const
Definition: UT_PlaneImpl.h:216
void negate()
Definition: UT_PlaneImpl.h:209
UT_PlaneType
Definition: UT_Plane.h:26
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
const UT_Vector3T< T > & point() const
Definition: UT_Plane.h:109
UT_PlaneT(UT_PlaneType plane_type=UT_PLANE_XY)
Definition: UT_PlaneImpl.h:32
void shiftOffset(T by_factor)
Definition: UT_Plane.h:177
bool SYSisEqual(const UT_Vector2T< T > &a, const UT_Vector2T< T > &b, S tol=SYS_FTOLERANCE)
Componentwise equality.
Definition: UT_Vector2.h:674
T distance(const UT_Vector3T< T > &p) const
Definition: UT_PlaneImpl.h:314
UT_PlaneT< fpreal64 > UT_PlaneD
Definition: UT_Plane.h:197