Developer Documentation
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 //
45 // CLASS PlaneT
46 //
47 //=============================================================================
48 
49 
50 #ifndef ACG_PLANE_HH
51 #define ACG_PLANE_HH
52 
53 
54 //== INCLUDES =================================================================
55 
56 
57 #include "../../Math/VectorT.hh"
58 #include "../../Math/Matrix4x4T.hh"
59 
60 
61 //== NAMESPACES ===============================================================
62 
63 
64 namespace ACG {
65 namespace Geometry {
66 
67 
68 //== CLASS DEFINITION =========================================================
69 
70 
77 template <typename Scalar>
78 class PlaneT
79 {
80 public:
81 
84  typedef VectorT<Scalar, 4> Vec4;
85  typedef Matrix4x4T<Scalar> Mat4x4;
86 
87 
89  PlaneT( Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0 )
90  : coeffs_(_a, _b, _c, _d)
91  { HNF(); }
92 
93 
95  PlaneT( const Vec3& _o, const Vec3& _n )
96  : coeffs_(_n[0], _n[1], _n[2], -(_n|_o))
97  { HNF(); }
98 
99 
101  PlaneT( const Vec3& _v0, const Vec3& _v1, const Vec3& _v2 )
102  {
103  Vec3 n = (_v1-_v0) % (_v2-_v0);
104  coeffs_ = Vec4(n[0], n[1], n[2], -(n|_v0));
105  HNF();
106  }
107 
108 
110  Vec3 normal() const { return Vec3(coeffs_[0], coeffs_[1], coeffs_[2]); }
111 
112 
114  const Vec4& coeffs() const { return coeffs_; }
115 
116 
118  Scalar distance( const Vec3& _v ) const
119  {
120  return ( _v[0]*coeffs_[0] +
121  _v[1]*coeffs_[1] +
122  _v[2]*coeffs_[2] +
123  coeffs_[3] );
124  }
125 
126 
128  bool operator() ( const Vec3& _v ) const { return distance(_v) > 0; }
129 
130 
131  // INTERSECTION
132  enum IntersectionTarget { Line, LineSegment, Ray };
133 
134  // intersect with (infinite) line
135  bool intersect_line( const Vec3& _v0, const Vec3& _v1,
136  Vec3& _v, Scalar& _t ) const
137  { return intersect(_v0, _v1, _v, _t, Line); }
138 
139  // intersect with ray
140  bool intersect_ray( const Vec3& _v0, const Vec3& _v1,
141  Vec3& _v, Scalar& _t ) const
142  { return intersect(_v0, _v1, _v, _t, Ray); }
143 
144  // intersect with line segment
145  bool intersect_linesegment( const Vec3& _v0, const Vec3& _v1,
146  Vec3& _v, Scalar& _t ) const
147  { return intersect(_v0, _v1, _v, _t, LineSegment); }
148 
160  bool intersect( const Vec3& _v0,
161  const Vec3& _v1,
162  Vec3& _v,
163  Scalar& _t,
164  IntersectionTarget _target ) const
165  {
166 #define SGN(d) ((d>0.0) ? 1 : -1)
167 
168  Scalar d0(distance(_v0)), d1(distance(_v1));
169  Scalar a0(fabs(d0)), a1(fabs(d1));
170 
171  // endpoint on plane
172  if (a0 < FLT_MIN) { _v = _v0; _t = 0.0; return true; }
173  if (a1 < FLT_MIN) { _v = _v1; _t = 1.0; return true; }
174 
175  // triv accept
176  if (SGN(d0) != SGN(d1))
177  {
178  _t = (a0/(a0+a1));
179  _v = _v0*(1.0-_t) + _v1*_t;
180  return true;
181  }
182 
183  // depends on target
184  else
185  {
186  if (_target == LineSegment) return false;
187 
188  if (fabs(d0-d1) < FLT_MIN) return false; // line parallel to plane
189  else _t = d0/(d0-d1);
190 
191  if (_target == Ray && _t < 0.0) return false;
192 
193  _v = _v0*(1.0-_t) + _v1*_t;
194  return true;
195  }
196 #undef SGN
197  }
198 
199 
201  bool affine_transformation( const Mat4x4& _M )
202  {
203  Mat4x4 M(_M);
204  if (!M.invert()) return false;
205  M.transpone();
206  affineTransformation_precomp(M);
207  return true;
208  }
209 
210 
212  void affine_transformation_precomp( const Mat4x4& _M_inverseTransposed )
213  {
214  coeffs_ = _M_inverseTransposed*coeffs_;
215  HNF();
216  }
217 
218 
219 private:
220 
221  void HNF() {
222  Scalar n = normal().norm();
223  if (n != 0.0) coeffs_ /= n;
224  }
225 
226  Vec4 coeffs_;
227 };
228 
229 
230 
231 typedef PlaneT<float> Planef;
232 typedef PlaneT<double> Planed;
233 
234 
235 //=============================================================================
236 } // namespace Geometry
237 } // namespace ACG
238 //=============================================================================
239 #endif // ACG_PLANE_HH defined
240 //=============================================================================
241 
VectorT< Scalar, 3 > Vec3
typedefs
Definition: PlaneT.hh:83
void affine_transformation_precomp(const Mat4x4 &_M_inverseTransposed)
affine transformation of the plane (M^{-T} precomputed)
Definition: PlaneT.hh:212
Vec3 normal() const
normal vector
Definition: PlaneT.hh:110
Namespace providing different geometric functions concerning angles.
PlaneT(const Vec3 &_v0, const Vec3 &_v1, const Vec3 &_v2)
constructor: 3 points
Definition: PlaneT.hh:101
bool affine_transformation(const Mat4x4 &_M)
affine transformation of the plane
Definition: PlaneT.hh:201
Scalar distance(const Vec3 &_v) const
signed distance point-plane
Definition: PlaneT.hh:118
PlaneT(const Vec3 &_o, const Vec3 &_n)
constructor: origin and normal
Definition: PlaneT.hh:95
const Vec4 & coeffs() const
coeffitients
Definition: PlaneT.hh:114
bool invert()
matrix inversion (returns true on success)
auto norm() const -> decltype(std::sqrt(std::declval< VectorT< S, DIM >>().sqrnorm()))
compute euclidean norm
Definition: Vector11T.hh:433
PlaneT(Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0)
constructor: coefficients
Definition: PlaneT.hh:89
bool intersect(const Vec3 &_v0, const Vec3 &_v1, Vec3 &_v, Scalar &_t, IntersectionTarget _target) const
General intersection function.
Definition: PlaneT.hh:160
bool operator()(const Vec3 &_v) const
predicate: above plane
Definition: PlaneT.hh:128