Developer Documentation
QuadricT.hh
Go to the documentation of this file.
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
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 
50 //=============================================================================
51 //
52 // CLASS QuadricT
53 //
54 //=============================================================================
55 
56 #ifndef OPENMESH_GEOMETRY_QUADRIC_HH
57 #define OPENMESH_GEOMETRY_QUADRIC_HH
58 
59 
60 //== INCLUDES =================================================================
61 
62 #include "Config.hh"
63 #include <OpenMesh/Core/Geometry/VectorT.hh>
64 #include <OpenMesh/Core/Utils/GenProg.hh>
65 
66 //== NAMESPACE ================================================================
67 
68 namespace OpenMesh { //BEGIN_NS_OPENMESH
69 namespace Geometry { //BEGIN_NS_GEOMETRY
70 
71 
72 //== CLASS DEFINITION =========================================================
73 
74 
81 template <class Scalar>
82 class QuadricT
83 {
84 public:
85  typedef Scalar value_type;
86  typedef QuadricT<Scalar> type;
87  typedef QuadricT<Scalar> Self;
88  // typedef VectorInterface<Scalar, VecStorage3<Scalar> > Vec3;
89  // typedef VectorInterface<Scalar, VecStorage4<Scalar> > Vec4;
90  //typedef Vector3Elem Vec3;
91  //typedef Vector4Elem Vec4;
92 
94  QuadricT(Scalar _a, Scalar _b, Scalar _c, Scalar _d,
95  Scalar _e, Scalar _f, Scalar _g,
96  Scalar _h, Scalar _i,
97  Scalar _j)
98  : a_(_a), b_(_b), c_(_c), d_(_d),
99  e_(_e), f_(_f), g_(_g),
100  h_(_h), i_(_i),
101  j_(_j)
102  {
103  }
104 
105 
107  QuadricT( Scalar _a=0.0, Scalar _b=0.0, Scalar _c=0.0, Scalar _d=0.0 )
108  : a_(_a*_a), b_(_a*_b), c_(_a*_c), d_(_a*_d),
109  e_(_b*_b), f_(_b*_c), g_(_b*_d),
110  h_(_c*_c), i_(_c*_d),
111  j_(_d*_d)
112  {}
113 
114  template <class _Point>
115  explicit QuadricT(const _Point& _pt)
116  {
117  set_distance_to_point(_pt);
118  }
119 
120  template <class _Normal, class _Point>
121  QuadricT(const _Normal& _n, const _Point& _p)
122  {
123  set_distance_to_plane(_n,_p);
124  }
125 
126  //set operator
127  void set(Scalar _a, Scalar _b, Scalar _c, Scalar _d,
128  Scalar _e, Scalar _f, Scalar _g,
129  Scalar _h, Scalar _i,
130  Scalar _j)
131  {
132  a_ = _a; b_ = _b; c_ = _c; d_ = _d;
133  e_ = _e; f_ = _f; g_ = _g;
134  h_ = _h; i_ = _i;
135  j_ = _j;
136  }
137 
138  //sets the quadric representing the squared distance to _pt
139  template <class _Point>
140  void set_distance_to_point(const _Point& _pt)
141  {
142  set(1, 0, 0, -_pt[0],
143  1, 0, -_pt[1],
144  1, -_pt[2],
145  dot(_pt,_pt));
146  }
147 
148  //sets the quadric representing the squared distance to the plane [_a,_b,_c,_d]
149  void set_distance_to_plane(Scalar _a, Scalar _b, Scalar _c, Scalar _d)
150  {
151  a_ = _a*_a; b_ = _a*_b; c_ = _a*_c; d_ = _a*_d;
152  e_ = _b*_b; f_ = _b*_c; g_ = _b*_d;
153  h_ = _c*_c; i_ = _c*_d;
154  j_ = _d*_d;
155  }
156 
157  //sets the quadric representing the squared distance to the plane
158  //determined by the normal _n and the point _p
159  template <class _Normal, class _Point>
160  void set_distance_to_plane(const _Normal& _n, const _Point& _p)
161  {
162  set_distance_to_plane(_n[0], _n[1], _n[2], -dot(_n,_p));
163  }
164 
166  void clear() { a_ = b_ = c_ = d_ = e_ = f_ = g_ = h_ = i_ = j_ = 0.0; }
167 
170  {
171  a_ += _q.a_; b_ += _q.b_; c_ += _q.c_; d_ += _q.d_;
172  e_ += _q.e_; f_ += _q.f_; g_ += _q.g_;
173  h_ += _q.h_; i_ += _q.i_;
174  j_ += _q.j_;
175  return *this;
176  }
177 
178  QuadricT<Scalar> operator+(const QuadricT<Scalar>& _other ) const
179  {
180  QuadricT<Scalar> result = *this;
181  return result += _other;
182  }
183 
184 
187  {
188  a_ *= _s; b_ *= _s; c_ *= _s; d_ *= _s;
189  e_ *= _s; f_ *= _s; g_ *= _s;
190  h_ *= _s; i_ *= _s;
191  j_ *= _s;
192  return *this;
193  }
194 
195  QuadricT<Scalar> operator*(Scalar _s) const
196  {
197  QuadricT<Scalar> result = *this;
198  return result *= _s;
199  }
200 
202  template <class _Vec4>
203  _Vec4 operator*(const _Vec4& _v) const
204  {
205  Scalar x(_v[0]), y(_v[1]), z(_v[2]), w(_v[3]);
206  return _Vec4(x*a_ + y*b_ + z*c_ + w*d_,
207  x*b_ + y*e_ + z*f_ + w*g_,
208  x*c_ + y*f_ + z*h_ + w*i_,
209  x*d_ + y*g_ + z*i_ + w*j_);
210  }
211 
213  template <class _Vec>
214  Scalar operator()(const _Vec& _v) const
215  {
216  return evaluate(_v, GenProg::Int2Type<vector_traits<_Vec>::size_>());
217  }
218 
219  Scalar a() const { return a_; }
220  Scalar b() const { return b_; }
221  Scalar c() const { return c_; }
222  Scalar d() const { return d_; }
223  Scalar e() const { return e_; }
224  Scalar f() const { return f_; }
225  Scalar g() const { return g_; }
226  Scalar h() const { return h_; }
227  Scalar i() const { return i_; }
228  Scalar j() const { return j_; }
229 
230  Scalar xx() const { return a_; }
231  Scalar xy() const { return b_; }
232  Scalar xz() const { return c_; }
233  Scalar xw() const { return d_; }
234  Scalar yy() const { return e_; }
235  Scalar yz() const { return f_; }
236  Scalar yw() const { return g_; }
237  Scalar zz() const { return h_; }
238  Scalar zw() const { return i_; }
239  Scalar ww() const { return j_; }
240 
241 protected:
242 
244  template <class _Vec3>
245  Scalar evaluate(const _Vec3& _v, GenProg::Int2Type<3>/*_dimension*/) const
246  {
247  Scalar x(_v[0]), y(_v[1]), z(_v[2]);
248  return a_*x*x + 2.0*b_*x*y + 2.0*c_*x*z + 2.0*d_*x
249  + e_*y*y + 2.0*f_*y*z + 2.0*g_*y
250  + h_*z*z + 2.0*i_*z
251  + j_;
252  }
253 
255  template <class _Vec4>
256  Scalar evaluate(const _Vec4& _v, GenProg::Int2Type<4>/*_dimension*/) const
257  {
258  Scalar x(_v[0]), y(_v[1]), z(_v[2]), w(_v[3]);
259  return a_*x*x + 2.0*b_*x*y + 2.0*c_*x*z + 2.0*d_*x*w
260  + e_*y*y + 2.0*f_*y*z + 2.0*g_*y*w
261  + h_*z*z + 2.0*i_*z*w
262  + j_*w*w;
263  }
264 
265 private:
266 
267  Scalar a_, b_, c_, d_,
268  e_, f_, g_,
269  h_, i_,
270  j_;
271 };
272 
273 
276 
279 
280 
281 //=============================================================================
282 } // END_NS_GEOMETRY
283 } // END_NS_OPENMESH
284 //============================================================================
285 #endif // OPENMESH_GEOMETRY_HH defined
286 //=============================================================================
Scalar evaluate(const _Vec3 &_v, GenProg::Int2Type< 3 >) const
evaluate quadric Q at 3D vector v: v*Q*v
Definition: QuadricT.hh:245
Scalar evaluate(const _Vec4 &_v, GenProg::Int2Type< 4 >) const
evaluate quadric Q at 4D vector v: v*Q*v
Definition: QuadricT.hh:256
Scalar operator()(const _Vec &_v) const
evaluate quadric Q at (3D or 4D) vector v: v*Q*v
Definition: QuadricT.hh:214
QuadricT(Scalar _a, Scalar _b, Scalar _c, Scalar _d, Scalar _e, Scalar _f, Scalar _g, Scalar _h, Scalar _i, Scalar _j)
construct with upper triangle of symmetrix 4x4 matrix
Definition: QuadricT.hh:94
QuadricT< Scalar > & operator+=(const QuadricT< Scalar > &_q)
add quadrics
Definition: QuadricT.hh:169
QuadricT(Scalar _a=0.0, Scalar _b=0.0, Scalar _c=0.0, Scalar _d=0.0)
constructor from given plane equation: ax+by+cz+d_=0
Definition: QuadricT.hh:107
_Vec4 operator*(const _Vec4 &_v) const
multiply 4D vector from right: Q*v
Definition: QuadricT.hh:203
void clear()
set all entries to zero
Definition: QuadricT.hh:166
QuadricT< Scalar > & operator*=(Scalar _s)
multiply by scalar
Definition: QuadricT.hh:186
osg::Vec3f::ValueType dot(const osg::Vec3f &_v1, const osg::Vec3f &_v2)
Adapter for osg vector member computing a scalar product.