Homogeneous.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_HOMOGENEOUS_H
26 #define EIGEN_HOMOGENEOUS_H
27 
28 namespace Eigen {
29 
45 namespace internal {
46 
47 template<typename MatrixType,int Direction>
48 struct traits<Homogeneous<MatrixType,Direction> >
49  : traits<MatrixType>
50 {
51  typedef typename traits<MatrixType>::StorageKind StorageKind;
52  typedef typename nested<MatrixType>::type MatrixTypeNested;
53  typedef typename remove_reference<MatrixTypeNested>::type _MatrixTypeNested;
54  enum {
55  RowsPlusOne = (MatrixType::RowsAtCompileTime != Dynamic) ?
56  int(MatrixType::RowsAtCompileTime) + 1 : Dynamic,
57  ColsPlusOne = (MatrixType::ColsAtCompileTime != Dynamic) ?
58  int(MatrixType::ColsAtCompileTime) + 1 : Dynamic,
59  RowsAtCompileTime = Direction==Vertical ? RowsPlusOne : MatrixType::RowsAtCompileTime,
60  ColsAtCompileTime = Direction==Horizontal ? ColsPlusOne : MatrixType::ColsAtCompileTime,
61  MaxRowsAtCompileTime = RowsAtCompileTime,
62  MaxColsAtCompileTime = ColsAtCompileTime,
63  TmpFlags = _MatrixTypeNested::Flags & HereditaryBits,
64  Flags = ColsAtCompileTime==1 ? (TmpFlags & ~RowMajorBit)
65  : RowsAtCompileTime==1 ? (TmpFlags | RowMajorBit)
66  : TmpFlags,
67  CoeffReadCost = _MatrixTypeNested::CoeffReadCost
68  };
69 };
70 
71 template<typename MatrixType,typename Lhs> struct homogeneous_left_product_impl;
72 template<typename MatrixType,typename Rhs> struct homogeneous_right_product_impl;
73 
74 } // end namespace internal
75 
76 template<typename MatrixType,int _Direction> class Homogeneous
77  : public MatrixBase<Homogeneous<MatrixType,_Direction> >
78 {
79  public:
80 
81  enum { Direction = _Direction };
82 
85 
86  inline Homogeneous(const MatrixType& matrix)
87  : m_matrix(matrix)
88  {}
89 
90  inline Index rows() const { return m_matrix.rows() + (int(Direction)==Vertical ? 1 : 0); }
91  inline Index cols() const { return m_matrix.cols() + (int(Direction)==Horizontal ? 1 : 0); }
92 
93  inline Scalar coeff(Index row, Index col) const
94  {
95  if( (int(Direction)==Vertical && row==m_matrix.rows())
96  || (int(Direction)==Horizontal && col==m_matrix.cols()))
97  return 1;
98  return m_matrix.coeff(row, col);
99  }
100 
101  template<typename Rhs>
102  inline const internal::homogeneous_right_product_impl<Homogeneous,Rhs>
103  operator* (const MatrixBase<Rhs>& rhs) const
104  {
106  return internal::homogeneous_right_product_impl<Homogeneous,Rhs>(m_matrix,rhs.derived());
107  }
108 
109  template<typename Lhs> friend
110  inline const internal::homogeneous_left_product_impl<Homogeneous,Lhs>
111  operator* (const MatrixBase<Lhs>& lhs, const Homogeneous& rhs)
112  {
114  return internal::homogeneous_left_product_impl<Homogeneous,Lhs>(lhs.derived(),rhs.m_matrix);
115  }
116 
117  template<typename Scalar, int Dim, int Mode, int Options> friend
118  inline const internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >
120  {
122  return internal::homogeneous_left_product_impl<Homogeneous,Transform<Scalar,Dim,Mode,Options> >(lhs,rhs.m_matrix);
123  }
124 
125  protected:
126  typename MatrixType::Nested m_matrix;
127 };
128 
140 template<typename Derived>
143 {
145  return derived();
146 }
147 
156 template<typename ExpressionType, int Direction>
159 {
160  return _expression();
161 }
162 
171 template<typename Derived>
172 inline const typename MatrixBase<Derived>::HNormalizedReturnType
174 {
176  return ConstStartMinusOne(derived(),0,0,
177  ColsAtCompileTime==1?size()-1:1,
178  ColsAtCompileTime==1?1:size()-1) / coeff(size()-1);
179 }
180 
189 template<typename ExpressionType, int Direction>
192 {
193  return HNormalized_Block(_expression(),0,0,
194  Direction==Vertical ? _expression().rows()-1 : _expression().rows(),
195  Direction==Horizontal ? _expression().cols()-1 : _expression().cols()).cwiseQuotient(
197  Direction==Vertical ? HNormalized_SizeMinusOne : 1,
198  Direction==Horizontal ? HNormalized_SizeMinusOne : 1>
199  (HNormalized_Factors(_expression(),
200  Direction==Vertical ? _expression().rows()-1:0,
201  Direction==Horizontal ? _expression().cols()-1:0,
202  Direction==Vertical ? 1 : _expression().rows(),
203  Direction==Horizontal ? 1 : _expression().cols()),
204  Direction==Vertical ? _expression().rows()-1 : 1,
205  Direction==Horizontal ? _expression().cols()-1 : 1));
206 }
207 
208 namespace internal {
209 
210 template<typename MatrixOrTransformType>
211 struct take_matrix_for_product
212 {
213  typedef MatrixOrTransformType type;
214  static const type& run(const type &x) { return x; }
215 };
216 
217 template<typename Scalar, int Dim, int Mode,int Options>
218 struct take_matrix_for_product<Transform<Scalar, Dim, Mode, Options> >
219 {
220  typedef Transform<Scalar, Dim, Mode, Options> TransformType;
221  typedef typename internal::add_const<typename TransformType::ConstAffinePart>::type type;
222  static type run (const TransformType& x) { return x.affine(); }
223 };
224 
225 template<typename Scalar, int Dim, int Options>
226 struct take_matrix_for_product<Transform<Scalar, Dim, Projective, Options> >
227 {
228  typedef Transform<Scalar, Dim, Projective, Options> TransformType;
229  typedef typename TransformType::MatrixType type;
230  static const type& run (const TransformType& x) { return x.matrix(); }
231 };
232 
233 template<typename MatrixType,typename Lhs>
234 struct traits<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
235 {
236  typedef typename take_matrix_for_product<Lhs>::type LhsMatrixType;
237  typedef typename remove_all<MatrixType>::type MatrixTypeCleaned;
238  typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
239  typedef typename make_proper_matrix_type<
241  LhsMatrixTypeCleaned::RowsAtCompileTime,
242  MatrixTypeCleaned::ColsAtCompileTime,
243  MatrixTypeCleaned::PlainObject::Options,
244  LhsMatrixTypeCleaned::MaxRowsAtCompileTime,
245  MatrixTypeCleaned::MaxColsAtCompileTime>::type ReturnType;
246 };
247 
248 template<typename MatrixType,typename Lhs>
249 struct homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs>
250  : public ReturnByValue<homogeneous_left_product_impl<Homogeneous<MatrixType,Vertical>,Lhs> >
251 {
252  typedef typename traits<homogeneous_left_product_impl>::LhsMatrixType LhsMatrixType;
253  typedef typename remove_all<LhsMatrixType>::type LhsMatrixTypeCleaned;
254  typedef typename remove_all<typename LhsMatrixTypeCleaned::Nested>::type LhsMatrixTypeNested;
255  typedef typename MatrixType::Index Index;
256  homogeneous_left_product_impl(const Lhs& lhs, const MatrixType& rhs)
257  : m_lhs(take_matrix_for_product<Lhs>::run(lhs)),
258  m_rhs(rhs)
259  {}
260 
261  inline Index rows() const { return m_lhs.rows(); }
262  inline Index cols() const { return m_rhs.cols(); }
263 
264  template<typename Dest> void evalTo(Dest& dst) const
265  {
266  // FIXME investigate how to allow lazy evaluation of this product when possible
267  dst = Block<const LhsMatrixTypeNested,
268  LhsMatrixTypeNested::RowsAtCompileTime,
269  LhsMatrixTypeNested::ColsAtCompileTime==Dynamic?Dynamic:LhsMatrixTypeNested::ColsAtCompileTime-1>
270  (m_lhs,0,0,m_lhs.rows(),m_lhs.cols()-1) * m_rhs;
271  dst += m_lhs.col(m_lhs.cols()-1).rowwise()
272  .template replicate<MatrixType::ColsAtCompileTime>(m_rhs.cols());
273  }
274 
275  typename LhsMatrixTypeCleaned::Nested m_lhs;
276  typename MatrixType::Nested m_rhs;
277 };
278 
279 template<typename MatrixType,typename Rhs>
280 struct traits<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
281 {
282  typedef typename make_proper_matrix_type<typename traits<MatrixType>::Scalar,
283  MatrixType::RowsAtCompileTime,
284  Rhs::ColsAtCompileTime,
285  MatrixType::PlainObject::Options,
286  MatrixType::MaxRowsAtCompileTime,
287  Rhs::MaxColsAtCompileTime>::type ReturnType;
288 };
289 
290 template<typename MatrixType,typename Rhs>
291 struct homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs>
292  : public ReturnByValue<homogeneous_right_product_impl<Homogeneous<MatrixType,Horizontal>,Rhs> >
293 {
294  typedef typename remove_all<typename Rhs::Nested>::type RhsNested;
295  typedef typename MatrixType::Index Index;
296  homogeneous_right_product_impl(const MatrixType& lhs, const Rhs& rhs)
297  : m_lhs(lhs), m_rhs(rhs)
298  {}
299 
300  inline Index rows() const { return m_lhs.rows(); }
301  inline Index cols() const { return m_rhs.cols(); }
302 
303  template<typename Dest> void evalTo(Dest& dst) const
304  {
305  // FIXME investigate how to allow lazy evaluation of this product when possible
306  dst = m_lhs * Block<const RhsNested,
307  RhsNested::RowsAtCompileTime==Dynamic?Dynamic:RhsNested::RowsAtCompileTime-1,
308  RhsNested::ColsAtCompileTime>
309  (m_rhs,0,0,m_rhs.rows()-1,m_rhs.cols());
310  dst += m_rhs.row(m_rhs.rows()-1).colwise()
311  .template replicate<MatrixType::RowsAtCompileTime>(m_lhs.rows());
312  }
313 
314  typename MatrixType::Nested m_lhs;
315  typename Rhs::Nested m_rhs;
316 };
317 
318 } // end namespace internal
319 
320 } // end namespace Eigen
321 
322 #endif // EIGEN_HOMOGENEOUS_H