Path Tracer
Ref.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_REF_H
11 #define EIGEN_REF_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename _PlainObjectType, int _Options, typename _StrideType>
18 struct traits<Ref<_PlainObjectType, _Options, _StrideType> >
19  : public traits<Map<_PlainObjectType, _Options, _StrideType> >
20 {
21  typedef _PlainObjectType PlainObjectType;
22  typedef _StrideType StrideType;
23  enum {
24  Options = _Options,
25  Flags = traits<Map<_PlainObjectType, _Options, _StrideType> >::Flags | NestByRefBit,
27  };
28 
29  template<typename Derived> struct match {
30  enum {
31  IsVectorAtCompileTime = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime,
33  StorageOrderMatch = IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
34  InnerStrideMatch = int(StrideType::InnerStrideAtCompileTime)==int(Dynamic)
35  || int(StrideType::InnerStrideAtCompileTime)==int(Derived::InnerStrideAtCompileTime)
36  || (int(StrideType::InnerStrideAtCompileTime)==0 && int(Derived::InnerStrideAtCompileTime)==1),
37  OuterStrideMatch = IsVectorAtCompileTime
38  || int(StrideType::OuterStrideAtCompileTime)==int(Dynamic) || int(StrideType::OuterStrideAtCompileTime)==int(Derived::OuterStrideAtCompileTime),
39  // NOTE, this indirection of evaluator<Derived>::Alignment is needed
40  // to workaround a very strange bug in MSVC related to the instantiation
41  // of has_*ary_operator in evaluator<CwiseNullaryOp>.
42  // This line is surprisingly very sensitive. For instance, simply adding parenthesis
43  // as "DerivedAlignment = (int(evaluator<Derived>::Alignment))," will make MSVC fail...
44  DerivedAlignment = int(evaluator<Derived>::Alignment),
45  AlignmentMatch = (int(traits<PlainObjectType>::Alignment)==int(Unaligned)) || (DerivedAlignment >= int(Alignment)), // FIXME the first condition is not very clear, it should be replaced by the required alignment
47  MatchAtCompileTime = HasDirectAccess && StorageOrderMatch && InnerStrideMatch && OuterStrideMatch && AlignmentMatch && ScalarTypeMatch
48  };
50  };
51 
52 };
53 
54 template<typename Derived>
55 struct traits<RefBase<Derived> > : public traits<Derived> {};
56 
57 }
58 
59 template<typename Derived> class RefBase
60  : public MapBase<Derived>
61 {
62  typedef typename internal::traits<Derived>::PlainObjectType PlainObjectType;
63  typedef typename internal::traits<Derived>::StrideType StrideType;
64 
65 public:
66 
67  typedef MapBase<Derived> Base;
68  EIGEN_DENSE_PUBLIC_INTERFACE(RefBase)
69 
70  EIGEN_DEVICE_FUNC inline Index innerStride() const
71  {
72  return StrideType::InnerStrideAtCompileTime != 0 ? m_stride.inner() : 1;
73  }
74 
75  EIGEN_DEVICE_FUNC inline Index outerStride() const
76  {
77  return StrideType::OuterStrideAtCompileTime != 0 ? m_stride.outer()
78  : IsVectorAtCompileTime ? this->size()
79  : int(Flags)&RowMajorBit ? this->cols()
80  : this->rows();
81  }
82 
83  EIGEN_DEVICE_FUNC RefBase()
84  : Base(0,RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime),
85  // Stride<> does not allow default ctor for Dynamic strides, so let' initialize it with dummy values:
86  m_stride(StrideType::OuterStrideAtCompileTime==Dynamic?0:StrideType::OuterStrideAtCompileTime,
87  StrideType::InnerStrideAtCompileTime==Dynamic?0:StrideType::InnerStrideAtCompileTime)
88  {}
89 
90  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(RefBase)
91 
92 protected:
93 
95 
96  template<typename Expression>
97  EIGEN_DEVICE_FUNC void construct(Expression& expr)
98  {
99  EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(PlainObjectType,Expression);
100 
101  if(PlainObjectType::RowsAtCompileTime==1)
102  {
103  eigen_assert(expr.rows()==1 || expr.cols()==1);
104  ::new (static_cast<Base*>(this)) Base(expr.data(), 1, expr.size());
105  }
106  else if(PlainObjectType::ColsAtCompileTime==1)
107  {
108  eigen_assert(expr.rows()==1 || expr.cols()==1);
109  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.size(), 1);
110  }
111  else
112  ::new (static_cast<Base*>(this)) Base(expr.data(), expr.rows(), expr.cols());
113 
114  if(Expression::IsVectorAtCompileTime && (!PlainObjectType::IsVectorAtCompileTime) && ((Expression::Flags&RowMajorBit)!=(PlainObjectType::Flags&RowMajorBit)))
115  ::new (&m_stride) StrideBase(expr.innerStride(), StrideType::InnerStrideAtCompileTime==0?0:1);
116  else
117  ::new (&m_stride) StrideBase(StrideType::OuterStrideAtCompileTime==0?0:expr.outerStride(),
118  StrideType::InnerStrideAtCompileTime==0?0:expr.innerStride());
119  }
120 
121  StrideBase m_stride;
122 };
123 
195 template<typename PlainObjectType, int Options, typename StrideType> class Ref
196  : public RefBase<Ref<PlainObjectType, Options, StrideType> >
197 {
198  private:
200  template<typename Derived>
201  EIGEN_DEVICE_FUNC inline Ref(const PlainObjectBase<Derived>& expr,
202  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0);
203  public:
204 
205  typedef RefBase<Ref> Base;
206  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
207 
208 
209  #ifndef EIGEN_PARSED_BY_DOXYGEN
210  template<typename Derived>
211  EIGEN_DEVICE_FUNC inline Ref(PlainObjectBase<Derived>& expr,
212  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
213  {
214  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
215  Base::construct(expr.derived());
216  }
217  template<typename Derived>
218  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
219  typename internal::enable_if<bool(Traits::template match<Derived>::MatchAtCompileTime),Derived>::type* = 0)
220  #else
221 
222  template<typename Derived>
223  inline Ref(DenseBase<Derived>& expr)
224  #endif
225  {
226  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
227  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
228  EIGEN_STATIC_ASSERT(!Derived::IsPlainObjectBase,THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
229  Base::construct(expr.const_cast_derived());
230  }
231 
232  EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Ref)
233 
234 };
235 
236 // this is the const ref version
237 template<typename TPlainObjectType, int Options, typename StrideType> class Ref<const TPlainObjectType, Options, StrideType>
238  : public RefBase<Ref<const TPlainObjectType, Options, StrideType> >
239 {
241  public:
242 
243  typedef RefBase<Ref> Base;
244  EIGEN_DENSE_PUBLIC_INTERFACE(Ref)
245 
246  template<typename Derived>
247  EIGEN_DEVICE_FUNC inline Ref(const DenseBase<Derived>& expr,
248  typename internal::enable_if<bool(Traits::template match<Derived>::ScalarTypeMatch),Derived>::type* = 0)
249  {
250 // std::cout << match_helper<Derived>::HasDirectAccess << "," << match_helper<Derived>::OuterStrideMatch << "," << match_helper<Derived>::InnerStrideMatch << "\n";
251 // std::cout << int(StrideType::OuterStrideAtCompileTime) << " - " << int(Derived::OuterStrideAtCompileTime) << "\n";
252 // std::cout << int(StrideType::InnerStrideAtCompileTime) << " - " << int(Derived::InnerStrideAtCompileTime) << "\n";
253  construct(expr.derived(), typename Traits::template match<Derived>::type());
254  }
255 
256  EIGEN_DEVICE_FUNC inline Ref(const Ref& other) : Base(other) {
257  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
258  }
259 
260  template<typename OtherRef>
261  EIGEN_DEVICE_FUNC inline Ref(const RefBase<OtherRef>& other) {
262  construct(other.derived(), typename Traits::template match<OtherRef>::type());
263  }
264 
265  protected:
266 
267  template<typename Expression>
268  EIGEN_DEVICE_FUNC void construct(const Expression& expr,internal::true_type)
269  {
270  Base::construct(expr);
271  }
272 
273  template<typename Expression>
274  EIGEN_DEVICE_FUNC void construct(const Expression& expr, internal::false_type)
275  {
276  internal::call_assignment_no_alias(m_object,expr,internal::assign_op<Scalar,Scalar>());
277  Base::construct(m_object);
278  }
279 
280  protected:
281  TPlainObjectType m_object;
282 };
283 
284 } // end namespace Eigen
285 
286 #endif // EIGEN_REF_H
Eigen::Stride::outer
EIGEN_DEVICE_FUNC Index outer() const
Definition: Stride.h:77
Eigen
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
Eigen::Stride< StrideType::OuterStrideAtCompileTime, StrideType::InnerStrideAtCompileTime >
Eigen::Unaligned
@ Unaligned
Definition: Constants.h:232
Eigen::internal::is_lvalue
Definition: XprHelper.h:668
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:65
Eigen::internal::false_type
Definition: Meta.h:64
Eigen::internal::true_type
Definition: Meta.h:63
Eigen::RefBase
Definition: Ref.h:61
Eigen::MapBase
Definition: ForwardDeclarations.h:112
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:21
Eigen::PlainObjectBase
Definition: PlainObjectBase.h:100
Eigen::internal::evaluator
Definition: CoreEvaluators.h:91
Eigen::internal::assign_op
Definition: AssignmentFunctors.h:21
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:197
Eigen::internal::traits
Definition: ForwardDeclarations.h:17
Eigen::DenseBase
Base class for all dense matrices, vectors, and arrays.
Definition: DenseBase.h:47
Eigen::internal::is_same
Definition: Meta.h:115
Eigen::internal::enable_if
Definition: Meta.h:234
Eigen::Stride::inner
EIGEN_DEVICE_FUNC Index inner() const
Definition: Stride.h:80
Eigen::internal::has_direct_access
Definition: ForwardDeclarations.h:26
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:42