toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN sl.h test/sl.cc


From: Gerhard Reitmayr
Subject: [Toon-members] TooN sl.h test/sl.cc
Date: Tue, 14 Apr 2009 09:11:14 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Gerhard Reitmayr <gerhard>      09/04/14 09:11:14

Modified files:
        .              : sl.h 
        test           : sl.cc 

Log message:
        aligned coerce with other groups, matrix multiplication, better doc

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/sl.h?cvsroot=toon&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/TooN/test/sl.cc?cvsroot=toon&r1=1.1&r2=1.2

Patches:
Index: sl.h
===================================================================
RCS file: /cvsroot/toon/TooN/sl.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- sl.h        10 Apr 2009 06:15:25 -0000      1.2
+++ sl.h        14 Apr 2009 09:11:14 -0000      1.3
@@ -40,60 +40,69 @@
 template <int N, typename P> class SL;
 template <int N, typename P> std::istream & operator>>(std::istream &, SL<N, 
P> &);
 
-/// represents an element from the group SL(n), the nxn matrices M with det(M) 
= 1.
+/// represents an element from the group SL(n), the NxN matrices M with det(M) 
= 1.
 /// This can be used to conveniently estimate homographies on n-1 dimentional 
spaces.
 /// The implementation uses the matrix exponential function @ref exp for
 /// exponentiation from an element in the Lie algebra and LU to compute an 
inverse.
 /// 
-/// The Lie algebra are the nxn matrices M with trace(M) = 0. The generators 
used
+/// The Lie algebra are the NxN matrices M with trace(M) = 0. The N*N-1 
generators used
 /// to represent this vector space are the following:
-/// @item n-1 diag(...,1,-1,...), along the diagonal
-/// @item symmetric generators for every pair of off-diagonal elements
-/// @item anti-symmetric generators for every pair of off-diagonal elements
+/// - diag(...,1,-1,...), n-1 along the diagonal
+/// - symmetric generators for every pair of off-diagonal elements
+/// - anti-symmetric generators for every pair of off-diagonal elements
 /// This choice represents the fact that SL(n) can be interpreted as the 
product
 /// of all symmetric matrices with det() = 1 times SO(n).
+/// @ingroup gTransforms
 template <int N, typename Precision = double>
 class SL {
        friend std::istream & operator>> <N,Precision>(std::istream &, SL &);
 public:
-       static const int size = N;
-       static const int dim = N*N - 1;
+       static const int size = N;          ///< size of the matrices 
represented by SL<N>
+       static const int dim = N*N - 1;     ///< dimension of the vector space 
represented by SL<N>
 
+       /// default constructor, creates identity element
        SL() : my_matrix(Identity) {}
+
+       /// exp constructor, creates element through exponentiation of Lie 
algebra vector. see @ref SL::exp.
        template <int S, typename P, typename B>
        SL( const Vector<S,P,B> & v ) { *this = exp(v); }
 
+       /// copy constructor from a matrix, coerces matrix to be of determinant 
= 1
        template <int R, int C, typename P, typename A>
-       SL(Matrix<R,C,P,A>& M) : my_matrix(M) {
-               coerce(M);
-       }
+       SL(Matrix<R,C,P,A>& M) : my_matrix(M) { coerce(); }
 
+       /// returns the represented matrix
        const Matrix<N,N,Precision> & get_matrix() const { return my_matrix; }
+       /// returns the inverse using LU
        SL inverse() const { return SL(*this, Invert()); }
 
+       /// multiplies to SLs together by multiplying the underlying matrices
        SL operator*( const SL & rhs) const { return SL(*this, rhs); }
+       /// right multiplies this SL with another one
        SL operator*=( const SL & rhs) { *this = *this*rhs; return *this; }
 
+       /// exponentiates a vector in the Lie algebra to compute the 
corresponding element
+       /// @arg v a vector of dimension SL::dim
        template <int S, typename P, typename B>
        static inline SL exp( const Vector<S,P,B> &);
 
+       /// returns one generator of the group. see SL for a detailed 
description of 
+       /// the generators used.
+       /// @arg i number of the generator between 0 and SL::dim -1 inclusive
        static inline Matrix<N,N,Precision> generator(int);
 
-       template <int R, int C, typename P, typename A>
-       static void coerce(Matrix<R,C,P,A>& M){
-               using std::abs;
-               SizeMismatch<N,R>::test(N, M.num_rows());
-               SizeMismatch<N,C>::test(N, M.num_cols());
-               P det = LU<N>(M).determinant();
-               assert(abs(det) > 0);
-               M /= det;
-       }
-
 private:
        struct Invert {};
        SL( const SL & from, struct Invert ) : 
my_matrix(LU<N>(from.get_matrix()).get_inverse()) {}
        SL( const SL & a, const SL & b) : my_matrix(a.get_matrix() * 
b.get_matrix()) {}
 
+       void coerce(){
+               using std::abs;
+               Precision det = LU<N>(my_matrix).determinant();
+               assert(abs(det) > 0);
+               my_matrix /= det;
+       }
+
        /// these constants indicate which parts of the parameter vector 
        /// map to which generators
        ///{
@@ -148,12 +157,22 @@
 }
 
 template <int S, typename PV, typename B, int N, typename P>
-Vector<N, P> operator*( const SL<N, P> & lhs, const Vector<S,PV,B> & rhs ){
+Vector<N, typename Internal::MultiplyType<P, PV>::type> operator*( const SL<N, 
P> & lhs, const Vector<S,PV,B> & rhs ){
        return lhs.get_matrix() * rhs;
 }
 
 template <int S, typename PV, typename B, int N, typename P>
-Vector<N, P> operator*( const Vector<S,PV,B> & lhs, const SL<N,P> & rhs ){
+Vector<N, typename Internal::MultiplyType<PV, P>::type> operator*( const 
Vector<S,PV,B> & lhs, const SL<N,P> & rhs ){
+       return lhs * rhs.get_matrix();
+}
+
+template<int R, int C, typename PM, typename A, int N, typename P> inline
+Matrix<N, C, typename Internal::MultiplyType<P, PM>::type> operator*(const 
SL<N,P>& lhs, const Matrix<R, C, PM, A>& rhs){
+       return lhs.get_matrix() * rhs;
+}
+
+template<int R, int C, typename PM, typename A, int N, typename P> inline
+Matrix<R, N, typename Internal::MultiplyType<PM, P>::type> operator*(const 
Matrix<R, C, PM, A>& lhs, const SL<N,P>& rhs){
        return lhs * rhs.get_matrix();
 }
 
@@ -166,7 +185,7 @@
 template <int N, typename P>
 std::istream & operator>>(std::istream & in, SL<N, P> & h){
        in >> h.my_matrix;
-       SL<N,P>::coerce(h.my_matrix);
+       h.coerce();
        return in;
 }
 

Index: test/sl.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/sl.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- test/sl.cc  3 Apr 2009 14:30:34 -0000       1.1
+++ test/sl.cc  14 Apr 2009 09:11:14 -0000      1.2
@@ -13,14 +13,35 @@
        cout << h.inverse() << endl;
        cout << SL<3>::exp(makeVector(-1,0,1,0,0,0,0,0)) << endl;
        cout << h * h.inverse() << endl;
+       h *= h.inverse();
+       cout << h << endl;
        
        for(int i = 0; i < SL<3>::dim; ++i)
                cout << "generator " << i << "\n" << SL<3>::generator(i) << 
endl;
 
-       for(int i = 0; i < SL<5>::dim; ++i)
-               cout << "generator " << i << "\n" << SL<5>::generator(i) << 
endl;
+       for(int i = 0; i < SL<2>::dim; ++i)
+               cout << "generator " << i << "\n" << SL<2>::generator(i) << 
endl;
        
        cout << SL<2>::exp(makeVector(1,2,3)) << endl;
        
+       h = SL<3>::exp(makeVector(1,0,-1,0,0,0,1,0));
+       
+       cout << h << "\n";
+       Vector<3> t = makeVector(0,1,2);
+       cout << "with vector " << t << "\n";
+       cout << h * t << "\n";
+       cout << t * h << "\n";
+       
+       Matrix<3,5> m = Zero;
+       m[0] = makeVector(0,1,2,3,4);
+       m[1] = makeVector(1,2,3,4,-5);
+       m[2] = makeVector(2,3,4,5,8);
+       
+       cout << "with matrix " << m << "\n";
+       cout << h * m << "\n";
+       cout << m.T() * h << "\n";
+       
+       cout << endl;
+       
        return 0;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]