BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Kernel_d/Aff_transformationHd.h
Go to the documentation of this file.
00001 // Copyright (c) 2000,2001  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Kernel_d/include/CGAL/Kernel_d/Aff_transformationHd.h $
00019 // $Id: Aff_transformationHd.h 42940 2008-04-17 13:32:52Z spion $
00020 // 
00021 //
00022 // Author(s)     : Michael Seel
00023 
00024 #ifndef CGAL_AFF_TRANSFORMATIONHD_H
00025 #define CGAL_AFF_TRANSFORMATIONHD_H
00026 
00027 #include <CGAL/basic.h>
00028 #include <CGAL/aff_transformation_tags.h>
00029 #include <CGAL/rational_rotation.h>
00030 #include <CGAL/Handle_for.h>
00031 
00032 CGAL_BEGIN_NAMESPACE
00033 
00034 template <class RT, class LA > class Aff_transformationHd;
00035 template <class RT, class LA > class Aff_transformationHd_rep;
00036 
00037 template <class RT, class LA>
00038 class Aff_transformationHd_rep 
00039 {
00040   friend class Aff_transformationHd<RT,LA>;
00041   typedef typename LA::Matrix Matrix;
00042   Matrix M_;
00043 public:
00044   Aff_transformationHd_rep(int d) : M_(d+1) {}
00045   Aff_transformationHd_rep(const Matrix& M_init) : M_(M_init) {}
00046   ~Aff_transformationHd_rep() {}
00047 }; 
00048 
00049 
00050 /*{\Moptions outfile=Aff_transformation_d.man}*/ 
00051 /*{\Manpage{Aff_transformation_d}{R}{Affine Transformations}{t}}*/
00052 /*{\Msubst 
00053 Hd<RT,LA>#_d<R>
00054 Aff_transformationHd#Aff_transformation_d
00055 Quotient<RT>#FT
00056 }*/
00057 
00058 template <class _RT, class _LA>
00059 class Aff_transformationHd : 
00060   public Handle_for< Aff_transformationHd_rep<_RT,_LA> > { 
00061 
00062   typedef Aff_transformationHd_rep<_RT,_LA> Rep;
00063   typedef Handle_for<Rep> Base;
00064   typedef Aff_transformationHd<_RT,_LA> Self;
00065 
00066   using Base::ptr;
00067 
00068 /*{\Mdefinition 
00069 An instance of the data type |\Mname| is an affine transformation of
00070 $d$-dimensional space. It is specified by a square matrix
00071 $M$ of dimension $d + 1$. All entries in the last row of |M| except
00072 the diagonal entry must be zero; the diagonal entry must be non-zero.
00073 A point $p$ with homogeneous coordinates $(p[0], \ldots, p[d])$ can be
00074 transformed into the point |p.transform(A)|, where |A| is an affine
00075 transformation created from |M| by the constructors below. }*/
00076 
00077 public: 
00078 /*{\Mtypes 4}*/
00079 
00080 typedef _RT RT;
00081 /*{\Mtypemember the ring type.}*/
00082 typedef Quotient<_RT> FT;
00083 /*{\Mtypemember the field type.}*/
00084 typedef _LA LA;
00085 /*{\Mtypemember the linear algebra layer.}*/
00086 typedef typename _LA::Matrix Matrix;
00087 /*{\Mtypemember the matrix type.}*/
00088 typedef typename _LA::Vector Vector;
00089 
00090 /*{\Mcreation 3}*/
00091 
00092 Aff_transformationHd(int d = 0) : Base( Rep(d) ) {}
00093 /*{\Mcreate introduces a transformation in $d$-dimensional space.}*/
00094 
00095 Aff_transformationHd(int d, Identity_transformation) : Base( Rep(d) )
00096 /*{\Mcreate introduces the identity transformation in $d$-dimensional 
00097     space.}*/
00098 { for (int i = 0; i <= d; ++i) ptr()->M_(i,i) = RT(1); }
00099 
00100 Aff_transformationHd(const Matrix& M) : Base( Rep(M) )
00101 /*{\Mcreate introduces the transformation of $d$ - space specified by
00102 matrix $M$. \precond |M| is a square matrix of dimension $d + 1$. }*/
00103 { CGAL_assertion_msg((M.row_dimension()==M.column_dimension()),
00104     "Aff_transformationHd::\
00105      construction: initialization matrix is not quadratic.");
00106 }
00107 
00108 template <typename Forward_iterator>
00109 Aff_transformationHd(Scaling, Forward_iterator start, Forward_iterator end) :
00110   Base( Rep(std::distance(start,end)-1) )
00111 /*{\Mcreate introduces the transformation of $d$-space specified by a
00112 diagonal matrix with entries |set [start,end)| on the diagonal 
00113 (a scaling of the space). \precond |set [start,end)| is a vector of 
00114 dimension $d+1$.}*/
00115 { int i=0; while (start != end) { ptr()->M_(i,i) = *start++;++i; } }
00116 
00117 Aff_transformationHd(Translation, const VectorHd<RT,LA>& v) :
00118   Base( Rep(v.dimension()) )
00119 /*{\Mcreate introduces the translation by vector $v$.}*/ 
00120 { int d = v.dimension();
00121   for (int i = 0; i < d; ++i) {
00122     ptr()->M_(i,i) = v.homogeneous(d);
00123     ptr()->M_(i,d) = v.homogeneous(i);
00124   }
00125   ptr()->M_(d,d) = v.homogeneous(d);
00126 }
00127 
00128 Aff_transformationHd(int d, Scaling, const RT& num, const RT& den) 
00129   : Base( Rep(d) ) 
00130 /*{\Mcreate returns a scaling by a scale factor |num/den|.}*/
00131 { Matrix& M = ptr()->M_;
00132   for (int i = 0; i < d; ++i) M(i,i) = num;
00133   M(d,d) = den;
00134 }
00135 
00136 Aff_transformationHd(int d, Rotation,  
00137   const RT& sin_num, const RT& cos_num, const RT& den, 
00138   int e1 = 0, int e2 = 1) : Base( Rep(d) ) 
00139 /*{\Mcreate returns a planar rotation with sine and cosine values
00140 |sin_num/den| and |cos_num/den| in the plane spanned by
00141 the base vectors $b_{e1}$ and $b_{e2}$ in $d$-space. Thus
00142 the default use delivers a planar rotation in the $x$-$y$
00143 plane. \precond $|sin_num|^2 + |cos_num|^2 = |den|^2$
00144 and $0 \leq e_1 < e_2 < d$}*/
00145 {
00146   CGAL_assertion_msg((sin_num*sin_num + cos_num*cos_num == den*den),
00147     "planar_rotation: rotation parameters disobey precondition.");
00148   CGAL_assertion_msg((0<=e1 && e1<=e2 && e2<d),
00149     "planar_rotation: base vector indices wrong.");
00150   Matrix& M = ptr()->M_;
00151   for (int i=0; i<d; i++) M(i,i) = 1;
00152   M(e1,e1) = cos_num; M(e1,e2) = -sin_num;
00153   M(e2,e1) = sin_num; M(e2,e2) = cos_num;
00154   M(d,d) = den;
00155 }
00156 
00157 Aff_transformationHd(int d, Rotation, const DirectionHd<RT,LA>& dir, 
00158   const RT& eps_num, const RT& eps_den, int e1 = 0, int e2 = 1)
00159 /*{\Mcreate returns a planar rotation within the plane spanned by
00160 the base vectors $b_{e1}$ and $b_{e2}$ in $d$-space.  The rotation
00161 parameters are given by the $2$-dimensional direction |dir|, such that
00162 the difference between the sines and cosines of the rotation given by
00163 |dir| and the approximated rotation are at most |num/den| each.\\
00164 \precond |dir.dimension()==2|, |!dir.is_degenerate()| and |num < den|
00165 is positive and $0 \leq e_1 < e_2 < d$ }*/
00166   : Base( Rep(d) )  
00167 {
00168   CGAL_assertion(dir.dimension()==2);
00169   Matrix& M = ptr()->M_;
00170   for (int i=0; i<d; i++) M(i,i) = RT(1);
00171   RT sin_num, cos_num, denom;
00172   rational_rotation_approximation(dir.dx(), dir.dy(),
00173                                   sin_num, cos_num, denom,
00174                                   eps_num, eps_den);
00175 
00176   M(e1,e1) = cos_num; M(e1,e2) = -sin_num;
00177   M(e2,e1) = sin_num; M(e2,e2) = cos_num;
00178   M(d,d) = denom;
00179 }
00180 
00181 /*{\Moperations 5 3}*/
00182 
00183 int dimension() const 
00184 { return ptr()->M_.row_dimension()-1; }
00185 /*{\Mop the dimension of the underlying space }*/
00186 
00187 const Matrix& matrix() const { return ptr()->M_; }
00188 /*{\Mop returns the transformation matrix }*/
00189 
00190 Vector operator()(const Vector& iv) const
00191 // transforms the ivector by a matrix multiplication
00192 { return matrix()*iv; }
00193 
00194 bool is_odd() const
00195 /*{\Mop returns true iff |\Mvar| is odd.}*/
00196 { return LA::sign_of_determinant(matrix())<0; }
00197 
00198 Aff_transformationHd<RT,LA> inverse() const
00199 /*{\Mop returns the inverse transformation.
00200 \precond |\Mvar.matrix()| is invertible.}*/
00201 { Aff_transformationHd<RT,LA> Inv; RT D; 
00202   Vector dummy;
00203   if ( !LA::inverse(matrix(),Inv.ptr()->M_,D,dummy) ) 
00204   CGAL_error_msg("Aff_transformationHd::inverse: not invertible.");
00205   if ( D < 0 ) Inv.ptr()->M_ = -Inv.ptr()->M_;
00206   return Inv;
00207 }
00208   
00209 Aff_transformationHd<RT,LA>  
00210 operator*(const Aff_transformationHd<RT,LA>& s) const
00211 /*{\Mbinop composition of transformations. Note that transformations
00212 are not necessarily commutative. |t*s| is the transformation
00213 which transforms first by |t| and then by |s|.}*/
00214 { CGAL_assertion_msg((dimension()==s.dimension()),
00215   "Aff_transformationHd::operator*: dimensions disagree.");
00216   return Aff_transformationHd<RT,LA>(matrix()*s.matrix()); 
00217 }
00218 
00219 bool operator==(const Aff_transformationHd<RT,LA>& a1) const
00220 { if ( this->identical(a1) ) return true;
00221   return ( matrix() == a1.matrix() );
00222 }
00223 bool operator!=(const Aff_transformationHd<RT,LA>& a1) const
00224 { return !operator==(a1); }
00225 
00226 }; // Aff_transformationHd
00227 
00228 template <class RT, class LA>
00229 std::ostream& operator<<(
00230   std::ostream& os, const Aff_transformationHd<RT,LA>& t) 
00231 { os << t.matrix(); return os; }
00232 
00233 template <class RT, class LA>
00234 std::istream& operator>>(
00235   std::istream& is, Aff_transformationHd<RT,LA>& t)
00236 { typename LA::Matrix M(t.dimension());
00237   is >> M; t = Aff_transformationHd<RT,LA>(M); 
00238   return is;
00239 }
00240 
00241 /*{\Mimplementation 
00242 Affine Transformations are implemented by matrices of integers as an
00243 item type.  All operations like creation, initialization, input and
00244 output on a transformation $t$ take time $O(|t.dimension()|^2)$. |dimension()|
00245 takes constant time.  The operations for inversion and composition
00246 have the cubic costs of the used matrix operations. The space
00247 requirement is $O(|t.dimension()|^2)$. }*/
00248 
00249 // ----------------------------- end of file ----------------------------
00250 
00251 
00252 CGAL_END_NAMESPACE
00253 #endif // CGAL_AFF_TRANSFORMATIONHD_H
00254 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines