Allolib  1.0
C++ Components For Interactive Multimedia
al_Plane.hpp
1 #ifndef INCLUDE_AL_PLANE_HPP
2 #define INCLUDE_AL_PLANE_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  A 3-dimensional plane encoded as a normal and scalar component
40 
41  File author(s):
42  Lance Putnam, 2011, putnam.lance@gmail.com
43 */
44 
45 #include "al/math/al_Vec.hpp"
46 
47 namespace al {
48 
52 template <class T>
53 class Plane {
54  public:
55  typedef al::Vec<3, T> Vec3;
56 
57  Plane() : mNormal(1, 0, 0), mD(0) {}
58  Plane(const Vec3& v1, const Vec3& v2, const Vec3& v3);
59 
61  const Vec3& normal() const { return mNormal; }
62 
64  T d() const { return mD; }
65 
67  T distance(const Vec3& p) const { return (mD + mNormal.dot(p)); }
68 
70  bool inNegativeSpace(const Vec3& p) const { return mNormal.dot(p) < -d(); }
71 
73  bool inPositiveSpace(const Vec3& p) const { return mNormal.dot(p) >= -d(); }
74 
76 
79  Plane& from3Points(const Vec3& v1, const Vec3& v2, const Vec3& v3);
80 
82  Plane& fromNormalAndPoint(const Vec3& normal, const Vec3& point);
83 
85  Plane& fromCoefficients(T a, T b, T c, T d);
86 
87  protected:
88  Vec3 mNormal; // plane orientation as perp. unit vector
89  T mD; // plane position as translation factor along normal
90 };
91 
92 template <class T>
93 Plane<T>::Plane(const Vec3& v1, const Vec3& v2, const Vec3& v3) {
94  from3Points(v1, v2, v3);
95 }
96 
97 template <class T>
98 Plane<T>& Plane<T>::from3Points(const Vec3& v1, const Vec3& v2,
99  const Vec3& v3) {
100  // return fromNormalAndPoint(cross(v1-v2, v3-v2).normalize(), v2); //
101  // left-handed
102  return fromNormalAndPoint(cross(v3 - v2, v1 - v2).normalize(),
103  v2); // right-handed
104 }
105 
106 template <class T>
107 Plane<T>& Plane<T>::fromNormalAndPoint(const Vec3& nrm, const Vec3& point) {
108  mNormal = nrm;
109  mD = -(mNormal.dot(point));
110  return *this;
111 }
112 
113 template <class T>
114 Plane<T>& Plane<T>::fromCoefficients(T a, T b, T c, T d) {
115  mNormal(a, b, c);
116  T l = mNormal.mag();
117  mNormal(a / l, b / l, c / l);
118  mD = d / l;
119  return *this;
120 }
121 
122 } // namespace al
123 
124 #endif
T distance(const Vec3 &p) const
Returns distance from plane to point (measured relative to plane normal)
Definition: al_Plane.hpp:67
const Vec3 & normal() const
Get normal perpendicular to plane (a, b, and c components)
Definition: al_Plane.hpp:61
Plane & fromCoefficients(T a, T b, T c, T d)
Set plane from coefficients.
Definition: al_Plane.hpp:114
Plane & from3Points(const Vec3 &v1, const Vec3 &v2, const Vec3 &v3)
Set from three points lying on the plane.
Definition: al_Plane.hpp:98
T d() const
Get scalar component of plane equation.
Definition: al_Plane.hpp:64
bool inPositiveSpace(const Vec3 &p) const
Returns whether a point is in the positive half-space of the plane.
Definition: al_Plane.hpp:73
Plane & fromNormalAndPoint(const Vec3 &normal, const Vec3 &point)
Set plane from a unit length normal and point lying on the plane.
Definition: al_Plane.hpp:107
bool inNegativeSpace(const Vec3 &p) const
Returns whether a point is in the negative half-space of the plane.
Definition: al_Plane.hpp:70
T dot(const Vec< N, U > &v) const
Returns dot (inner) product between vectors.
Definition: al_Vec.hpp:406
Definition: al_App.hpp:23
void cross(Vec< 3, T > &r, const Vec< 3, T > &a, const Vec< 3, T > &b)
Sets r to cross product, a x b.
Definition: al_Vec.hpp:574