CUGL
Cornell University Game Library
CUPlane.h
1 //
2 // CUPlane.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a 3d plane.
6 //
7 // Because math objects are intended to be on the stack, we do not provide
8 // any shared pointer support in this class.
9 //
10 // CUGL zlib License:
11 // This software is provided 'as-is', without any express or implied
12 // warranty. In no event will the authors be held liable for any damages
13 // arising from the use of this software.
14 //
15 // Permission is granted to anyone to use this software for any purpose,
16 // including commercial applications, and to alter it and redistribute it
17 // freely, subject to the following restrictions:
18 //
19 // 1. The origin of this software must not be misrepresented; you must not
20 // claim that you wrote the original software. If you use this software
21 // in a product, an acknowledgment in the product documentation would be
22 // appreciated but is not required.
23 //
24 // 2. Altered source versions must be plainly marked as such, and must not
25 // be misrepresented as being the original software.
26 //
27 // 3. This notice may not be removed or altered from any source distribution.
28 //
29 // Author: Walker White
30 // Version: 6/20/16
31 
32 #ifndef __CU_PLANE_H__
33 #define __CU_PLANE_H__
34 
35 #include "CUVec3.h"
36 #include "CUMat4.h"
37 #include "CURay.h"
38 
39 namespace cugl {
40 
47 class Plane {
48 #pragma mark Values
49 public:
53  enum class Side {
55  INCIDENT,
57  BACK,
59  FRONT
60  };
61 
65  float offset;
66 
68  static const Plane XY;
70  static const Plane XZ;
72  static const Plane YZ;
73 
74 #pragma mark -
75 #pragma mark Constructors
76 
79  Plane () : normal(Vec3::UNIT_Z), offset(0) {}
80 
87  Plane (const Vec3& normal, float dist=0) {
88  set(normal,dist);
89  }
90 
97  Plane (const Vec3& normal, const Vec3& point) {
98  set(normal,point);
99  }
100 
111  Plane(const Vec3& point1, const Vec3& point2, const Vec3& point3) {
112  set(point1, point2, point3);
113  }
114 
125  Plane(float a, float b, float c, float d) {
126  set(a,b,c,d);
127  }
128 
134  Plane(const Plane& plane) {
135  set(plane);
136  }
137 
141  ~Plane() {}
142 
143 #pragma mark -
144 #pragma mark Setters
145 
152  Plane& operator=(const Plane& plane) {
153  return set(plane);
154  }
155 
165  Plane& operator=(const Vec3& normal) {
166  return set(normal,0);
167  }
168 
177  Plane& set(const Vec3& normal, float dist=0);
178 
187  Plane& set(const Vec3& normal, const Vec3& point);
188 
201  Plane& set(const Vec3& point1, const Vec3& point2, const Vec3& point3);
202 
213  Plane& set(float a, float b, float c, float d);
214 
222  Plane& set(const Plane& plane) {
223  normal = plane.normal;
224  offset = plane.offset;
225  return *this;
226  }
227 
228 #pragma mark -
229 #pragma mark Static Arithmetic
230 
245  static Plane* multiply(const Plane& plane, const Mat4& mat, Plane* dst);
246 
258  static float intersection(const Plane& plane, const Ray& ray);
259 
260 #pragma mark -
261 #pragma mark Arithmetic
262 
275  Plane& multiply(const Mat4& mat) {
276  return *(multiply(*this,mat,this));
277  }
278 
292  Plane& operator*=(const Mat4& mat) {
293  return *(multiply(*this,mat,this));
294  }
295 
309  const Plane operator*(const Mat4& mat) const {
310  Plane result;
311  return *(multiply(*this,mat,&result));
312  }
313 
314 #pragma mark -
315 #pragma mark Plane Methods
316 
325  float distance(const Vec3& point) const {
326  return normal.dot(point) + offset;
327  }
328 
339  float getIntersection(const Ray& ray) const {
340  return intersection(*this,ray);
341  }
342 
352  bool contains(const Vec3& point, float variance=CU_MATH_EPSILON) const {
353  return fabsf(distance(point)) <= variance;
354  }
355 
365  Side sideOf(const Vec3& point) const {
366  float dist = normal.dot(point) + offset;
367  return (dist == 0 ? Side::INCIDENT : (dist < 0 ? Side::BACK : Side::FRONT));
368  }
369 
381  Side sideOf(float x, float y, float z) const {
382  return sideOf(Vec3(x,y,z));
383  }
384 
385 
396  bool isFrontFacing(const Vec3& direction) const {
397  float dot = normal.dot(direction);
398  return dot <= 0;
399  }
400 
401 #pragma mark -
402 #pragma mark Comparisons
403 
413  bool operator==(const Plane& p) const {
414  return normal == p.normal && offset == p.offset;
415  }
416 
427  bool operator!=(const Plane& p) const {
428  return normal != p.normal || offset != p.offset;
429  }
430 
441  bool equals(const Plane& p, float variance=CU_MATH_EPSILON) const {
442  return (normal.equals(p.normal,variance) &&
443  CU_MATH_APPROX(offset,p.offset,variance));
444  }
445 
446 #pragma mark -
447 #pragma mark Conversion Methods
448 
460  std::string toString(bool verbose = false) const;
461 
463  operator std::string() const { return toString(); }};
464 }
465 
466 #endif /* __CU_PLANE_H__ */
float dot(const Vec3 &v) const
Definition: CUVec3.h:1016
bool isFrontFacing(const Vec3 &direction) const
Definition: CUPlane.h:396
Plane & set(const Vec3 &normal, float dist=0)
Side
Definition: CUPlane.h:53
bool equals(const Plane &p, float variance=CU_MATH_EPSILON) const
Definition: CUPlane.h:441
Side sideOf(float x, float y, float z) const
Definition: CUPlane.h:381
static const Plane YZ
Definition: CUPlane.h:72
Plane(const Vec3 &normal, const Vec3 &point)
Definition: CUPlane.h:97
Plane & operator=(const Plane &plane)
Definition: CUPlane.h:152
Plane()
Definition: CUPlane.h:79
bool contains(const Vec3 &point, float variance=CU_MATH_EPSILON) const
Definition: CUPlane.h:352
float distance(const Vec3 &point) const
Definition: CUPlane.h:325
float offset
Definition: CUPlane.h:65
Vec3 normal
Definition: CUPlane.h:63
Side sideOf(const Vec3 &point) const
Definition: CUPlane.h:365
Plane & operator=(const Vec3 &normal)
Definition: CUPlane.h:165
float getIntersection(const Ray &ray) const
Definition: CUPlane.h:339
Definition: CUPlane.h:47
~Plane()
Definition: CUPlane.h:141
Plane & operator*=(const Mat4 &mat)
Definition: CUPlane.h:292
Plane & set(const Plane &plane)
Definition: CUPlane.h:222
static const Plane XY
Definition: CUPlane.h:68
Plane(const Vec3 &normal, float dist=0)
Definition: CUPlane.h:87
Plane(const Vec3 &point1, const Vec3 &point2, const Vec3 &point3)
Definition: CUPlane.h:111
static Plane * multiply(const Plane &plane, const Mat4 &mat, Plane *dst)
std::string toString(bool verbose=false) const
static const Plane XZ
Definition: CUPlane.h:70
Definition: CURay.h:45
const Plane operator*(const Mat4 &mat) const
Definition: CUPlane.h:309
Plane(float a, float b, float c, float d)
Definition: CUPlane.h:125
Definition: CUVec3.h:61
Plane & multiply(const Mat4 &mat)
Definition: CUPlane.h:275
static float intersection(const Plane &plane, const Ray &ray)
Plane(const Plane &plane)
Definition: CUPlane.h:134
Definition: CUAnimationNode.h:52
Definition: CUMat4.h:92
bool operator!=(const Plane &p) const
Definition: CUPlane.h:427
bool operator==(const Plane &p) const
Definition: CUPlane.h:413
bool equals(const Vec3 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:877