Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PlaneT.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // CLASS PlaneT
56 //
57 //=============================================================================
58 
59 
60 #ifndef ACG_PLANE_HH
61 #define ACG_PLANE_HH
62 
63 
64 //== INCLUDES =================================================================
65 
66 
67 #include "../../Math/VectorT.hh"
68 #include "../../Math/Matrix4x4T.hh"
69 
70 
71 //== NAMESPACES ===============================================================
72 
73 
74 namespace ACG {
75 namespace Geometry {
76 
77 
78 //== CLASS DEFINITION =========================================================
79 
80 
87 template <typename Scalar>
88 class PlaneT
89 {
90 public:
91 
94  typedef VectorT<Scalar, 4> Vec4;
95  typedef Matrix4x4T<Scalar> Mat4x4;
96 
97 
99  PlaneT( Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0 )
100  : coeffs_(_a, _b, _c, _d)
101  { HNF(); }
102 
103 
105  PlaneT( const Vec3& _o, const Vec3& _n )
106  : coeffs_(_n[0], _n[1], _n[2], -(_n|_o))
107  { HNF(); }
108 
109 
111  PlaneT( const Vec3& _v0, const Vec3& _v1, const Vec3& _v2 )
112  {
113  Vec3 n = (_v1-_v0) % (_v2-_v0);
114  coeffs_ = Vec4(n[0], n[1], n[2], -(n|_v0));
115  HNF();
116  }
117 
118 
120  Vec3 normal() const { return Vec3(coeffs_[0], coeffs_[1], coeffs_[2]); }
121 
122 
124  const Vec4& coeffs() const { return coeffs_; }
125 
126 
128  Scalar distance( const Vec3& _v ) const
129  {
130  return ( _v[0]*coeffs_[0] +
131  _v[1]*coeffs_[1] +
132  _v[2]*coeffs_[2] +
133  coeffs_[3] );
134  }
135 
136 
138  bool operator() ( const Vec3& _v ) const { return distance(_v) > 0; }
139 
140 
141  // INTERSECTION
142  enum IntersectionTarget { Line, LineSegment, Ray };
143 
144  // intersect with (infinite) line
145  bool intersect_line( const Vec3& _v0, const Vec3& _v1,
146  Vec3& _v, Scalar& _t ) const
147  { return intersect(_v0, _v1, _v, _t, Line); }
148 
149  // intersect with ray
150  bool intersect_ray( const Vec3& _v0, const Vec3& _v1,
151  Vec3& _v, Scalar& _t ) const
152  { return intersect(_v0, _v1, _v, _t, Ray); }
153 
154  // intersect with line segment
155  bool intersect_linesegment( const Vec3& _v0, const Vec3& _v1,
156  Vec3& _v, Scalar& _t ) const
157  { return intersect(_v0, _v1, _v, _t, LineSegment); }
158 
170  bool intersect( const Vec3& _v0,
171  const Vec3& _v1,
172  Vec3& _v,
173  Scalar& _t,
174  IntersectionTarget _target ) const
175  {
176 #define SGN(d) ((d>0.0) ? 1 : -1)
177 
178  Scalar d0(distance(_v0)), d1(distance(_v1));
179  Scalar a0(fabs(d0)), a1(fabs(d1));
180 
181  // endpoint on plane
182  if (a0 < FLT_MIN) { _v = _v0; _t = 0.0; return true; }
183  if (a1 < FLT_MIN) { _v = _v1; _t = 1.0; return true; }
184 
185  // triv accept
186  if (SGN(d0) != SGN(d1))
187  {
188  _t = (a0/(a0+a1));
189  _v = _v0*(1.0-_t) + _v1*_t;
190  return true;
191  }
192 
193  // depends on target
194  else
195  {
196  if (_target == LineSegment) return false;
197 
198  if (fabs(d0-d1) < FLT_MIN) return false; // line parallel to plane
199  else _t = d0/(d0-d1);
200 
201  if (_target == Ray && _t < 0.0) return false;
202 
203  _v = _v0*(1.0-_t) + _v1*_t;
204  return true;
205  }
206 #undef SGN
207  }
208 
209 
211  bool affine_transformation( const Mat4x4& _M )
212  {
213  Mat4x4 M(_M);
214  if (!M.invert()) return false;
215  M.transpone();
216  affineTransformation_precomp(M);
217  return true;
218  }
219 
220 
222  void affine_transformation_precomp( const Mat4x4& _M_inverseTransposed )
223  {
224  coeffs_ = _M_inverseTransposed*coeffs_;
225  HNF();
226  }
227 
228 
229 private:
230 
231  void HNF() {
232  Scalar n = normal().norm();
233  if (n != 0.0) coeffs_ /= n;
234  }
235 
236  Vec4 coeffs_;
237 };
238 
239 
240 
241 typedef PlaneT<float> Planef;
242 typedef PlaneT<double> Planed;
243 
244 
245 //=============================================================================
246 } // namespace Geometry
247 } // namespace ACG
248 //=============================================================================
249 #endif // ACG_PLANE_HH defined
250 //=============================================================================
251 
bool operator()(const Vec3 &_v) const
predicate: above plane
Definition: PlaneT.hh:138
Vec3 normal() const
normal vector
Definition: PlaneT.hh:120
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
PlaneT(Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0)
constructor: coefficients
Definition: PlaneT.hh:99
PlaneT(const Vec3 &_v0, const Vec3 &_v1, const Vec3 &_v2)
constructor: 3 points
Definition: PlaneT.hh:111
VectorT< Scalar, 3 > Vec3
typedefs
Definition: PlaneT.hh:93
void affine_transformation_precomp(const Mat4x4 &_M_inverseTransposed)
affine transformation of the plane (M^{-T} precomputed)
Definition: PlaneT.hh:222
bool affine_transformation(const Mat4x4 &_M)
affine transformation of the plane
Definition: PlaneT.hh:211
bool intersect(const Vec3 &_v0, const Vec3 &_v1, Vec3 &_v, Scalar &_t, IntersectionTarget _target) const
General intersection function.
Definition: PlaneT.hh:170
Scalar distance(const Vec3 &_v) const
signed distance point-plane
Definition: PlaneT.hh:128
const Vec4 & coeffs() const
coeffitients
Definition: PlaneT.hh:124
PlaneT(const Vec3 &_o, const Vec3 &_n)
constructor: origin and normal
Definition: PlaneT.hh:105
bool invert()
matrix inversion (returns true on success)
Definition: Matrix4x4T.cc:297