summaryrefslogtreecommitdiff
path: root/include/gudhi_patches/CGAL/NewKernel_d/LA_eigen/constructors.h
blob: 3636996f30b7c95af361b7d7c97e8ea799351626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2014
// INRIA Saclay-Ile de France (France)
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
// Author(s)     : Marc Glisse

#ifndef CGAL_LA_EIGEN_CONSTRUCTORS_H
#define CGAL_LA_EIGEN_CONSTRUCTORS_H
#include <CGAL/config.h>

#if defined(BOOST_MSVC)
#  pragma warning(push)
#  pragma warning(disable:4003) // not enough actual parameters for macro 'BOOST_PP_EXPAND_I'
                                // http://lists.boost.org/boost-users/2014/11/83291.php
#endif 

#ifndef CGAL_EIGEN3_ENABLED
#error Requires Eigen
#endif
#include <boost/type_traits/is_arithmetic.hpp>
#include <boost/utility/enable_if.hpp>
#include <CGAL/Dimension.h>
#include <Eigen/Dense>
#include <CGAL/iterator_from_indices.h>
#include <CGAL/NewKernel_d/utils.h>
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>

namespace CGAL {
  template <class Vector_> struct Construct_eigen {
    typedef Vector_ result_type;
    typedef typename Vector_::Scalar NT;

    private:
    static void check_dim(int CGAL_assertion_code(d)){
      CGAL_assertion_code(int m = result_type::MaxSizeAtCompileTime;)
      CGAL_assertion((m == Eigen::Dynamic) || (d <= m));
    }
    public:

    struct Dimension {
      // Initialize with NaN if possible?
      result_type operator()(int d) const {
	check_dim(d);
	return result_type(d);
      }
    };

    struct Iterator {
      template<typename Iter>
	result_type operator()(int d,Iter const& f,Iter const& e) const {
	  check_dim(d);
	  CGAL_assertion(d==std::distance(f,e));
	  result_type a(d);
	  // TODO: check the right way to do this
	  std::copy(f,e,&a[0]);
	  return a;
	}
    };

#if 0
    struct Iterator_add_one {
      template<typename Iter>
	result_type operator()(int d,Iter const& f,Iter const& e) const {
	  check_dim(d);
	  CGAL_assertion(d==std::distance(f,e)+1);
	  result_type a(d);
	  std::copy(f,e,&a[0]);
	  a[d-1]=1;
	  return a;
	}
    };
#endif

    struct Iterator_and_last {
      template<typename Iter,typename T>
	result_type operator()(int d,Iter const& f,Iter const& e,CGAL_FORWARDABLE(T) t) const {
	  check_dim(d);
	  CGAL_assertion(d==std::distance(f,e)+1);
	  result_type a(d);
	  std::copy(f,e,&a[0]);
	  a[d-1]=CGAL_FORWARD(T,t);
	  return a;
	}
    };

#ifdef CGAL_CXX11
    struct Initializer_list {
      // Fix T==NT?
      template<class T>
	result_type operator()(std::initializer_list<T> l) const {
	  return Iterator()(l.size(),l.begin(),l.end());
	}
    };
#endif

    struct Values {
#ifdef CGAL_CXX11
      // TODO avoid going through Initializer_list which may cause extra copies. Possibly use forward_as_tuple.
      template<class...U>
	result_type operator()(U&&...u) const {
	  check_dim(sizeof...(U)); // TODO: use static_assert
	  return Initializer_list()({forward_safe<NT,U>(u)...});
	}
#else

#define CGAL_CODE(Z,N,_) result_type operator()(BOOST_PP_ENUM_PARAMS(N,NT const& t)) const { \
  check_dim(N); \
  result_type a(N); \
  a << BOOST_PP_ENUM_PARAMS(N,t); \
  return a; \
}
BOOST_PP_REPEAT_FROM_TO(1, 11, CGAL_CODE, _ )
#undef CGAL_CODE

#endif
    };

    struct Values_divide {
#ifdef CGAL_CXX11
      template<class H,class...U>
	result_type operator()(H const&h,U&&...u) const {
	  check_dim(sizeof...(U)); // TODO: use static_assert
	  return Initializer_list()({Rational_traits<NT>().make_rational(std::forward<U>(u),h)...});
	}
#else

#define CGAL_VAR(Z,N,_) ( Rational_traits<NT>().make_rational( t##N ,h) )
#define CGAL_CODE(Z,N,_) template <class H> result_type \
  operator()(H const&h, BOOST_PP_ENUM_PARAMS(N,NT const& t)) const { \
    check_dim(N); \
    result_type a(N); \
    a << BOOST_PP_ENUM(N,CGAL_VAR,); \
    return a; \
  }
  BOOST_PP_REPEAT_FROM_TO(1, 11, CGAL_CODE, _ )
#undef CGAL_CODE
#undef CGAL_VAR

#endif
    };
  };
}
#if defined(BOOST_MSVC)
#  pragma warning(pop)
#endif

#endif