summaryrefslogtreecommitdiff
path: root/include/gudhi_patches/CGAL/NewKernel_d/Cartesian_LA_functors.h
blob: 871c463ac77f586787f1d007fafdedd960fcad4c (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// 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_CARTESIAN_LA_FUNCTORS_H
#define CGAL_CARTESIAN_LA_FUNCTORS_H

#include <CGAL/NewKernel_d/utils.h>
#include <CGAL/is_iterator.h>
#include <CGAL/argument_swaps.h>
#include <CGAL/Kernel/Return_base_tag.h>
#include <CGAL/transforming_iterator.h>
#include <CGAL/NewKernel_d/store_kernel.h>
#include <CGAL/Dimension.h>

namespace CGAL {
namespace CartesianDVectorBase {
#ifndef CGAL_CXX11
namespace internal {
template<class R_,class Dim_> struct Construct_LA_vector_ {
	struct Never_use {};
	void operator()(Never_use)const;
};
#define CGAL_CODE(Z,N,_) template<class R> struct Construct_LA_vector_<R,Dimension_tag<N> > { \
	typedef typename R::Constructor Constructor; \
	typedef typename Get_type<R, RT_tag>::type RT; \
	typedef typename R::Vector_ result_type; \
	result_type operator() \
	(BOOST_PP_ENUM_PARAMS(N,RT const& t)) const { \
	return typename Constructor::Values()(BOOST_PP_ENUM_PARAMS(N,t)); \
	} \
	result_type operator() \
	(BOOST_PP_ENUM_PARAMS(BOOST_PP_INC(N),RT const& t)) const { \
	return typename Constructor::Values_divide()(t##N,BOOST_PP_ENUM_PARAMS(N,t)); \
	} \
	};
BOOST_PP_REPEAT_FROM_TO(2, 11, CGAL_CODE, _ )
#undef CGAL_CODE
}
#endif

template<class R_,class Zero_> struct Construct_LA_vector
: private Store_kernel<R_>
#ifndef CGAL_CXX11
, public internal::Construct_LA_vector_<R_,typename R_::Default_ambient_dimension>
#endif
{
	//CGAL_FUNCTOR_INIT_IGNORE(Construct_LA_vector)
	CGAL_FUNCTOR_INIT_STORE(Construct_LA_vector)
	typedef R_ R;
	typedef typename R::Constructor Constructor;
	typedef typename Get_type<R, RT_tag>::type RT;
	typedef typename Get_type<R, FT_tag>::type FT;
	typedef typename R::Vector_ result_type;
	typedef typename R_::Default_ambient_dimension Dimension;
	result_type operator()(int d)const{
		CGAL_assertion(check_dimension_eq(d,this->kernel().dimension()));
		return typename Constructor::Dimension()(d);
	}
	result_type operator()()const{
		return typename Constructor::Dimension()((std::max)(0,this->kernel().dimension()));
	}
	result_type operator()(int d, Zero_ const&)const{
		CGAL_assertion(check_dimension_eq(d,this->kernel().dimension()));
		return typename Constructor::Dimension()(d);
	}
	result_type operator()(Zero_ const&)const{
	  // Makes no sense for an unknown dimension.
		return typename Constructor::Dimension()(this->kernel().dimension());
	}
	result_type operator()(result_type const& v)const{
		return v;
	}
#ifdef CGAL_CXX11
	result_type operator()(result_type&& v)const{
		return std::move(v);
	}
#endif
#ifdef CGAL_CXX11
	template<class...U>
	typename std::enable_if<Constructible_from_each<RT,U...>::value &&
		boost::is_same<Dimension_tag<sizeof...(U)>, Dimension>::value,
	  result_type>::type
	operator()(U&&...u)const{
		return typename Constructor::Values()(std::forward<U>(u)...);
	}
	//template<class...U,class=typename std::enable_if<Constructible_from_each<RT,U...>::value>::type,class=typename std::enable_if<(sizeof...(U)==static_dim+1)>::type,class=void>
	template<class...U>
	typename std::enable_if<Constructible_from_each<RT,U...>::value &&
		boost::is_same<Dimension_tag<sizeof...(U)-1>, Dimension>::value,
	  result_type>::type
	operator()(U&&...u)const{
		return Apply_to_last_then_rest()(typename Constructor::Values_divide(),std::forward<U>(u)...);
	}
#else
	using internal::Construct_LA_vector_<R_,typename R::Default_ambient_dimension>::operator();
#endif
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(Iter f,Iter g,Cartesian_tag t)const
	{
		return this->operator()((int)std::distance(f,g),f,g,t);
	}
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(int d,Iter f,Iter g,Cartesian_tag)const
	{
		CGAL_assertion(d==std::distance(f,g));
		CGAL_assertion(check_dimension_eq(d,this->kernel().dimension()));
		return typename Constructor::Iterator()(d,f,g);
	}
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::bidirectional_iterator_tag>,result_type>::type operator()
		(Iter f,Iter g,Homogeneous_tag)const
	{
		--g;
		return this->operator()((int)std::distance(f,g),f,g,*g);
	}
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::bidirectional_iterator_tag>,result_type>::type operator()
		(int d,Iter f,Iter g,Homogeneous_tag)const
	{
		--g;
		return this->operator()(d,f,g,*g);
	}
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(Iter f,Iter g)const
	{
	  // Shouldn't it try comparing dist(f,g) to the dimension if it is known?
		return this->operator()(f,g,typename R::Rep_tag());
	}
	template<class Iter> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(int d,Iter f,Iter g)const
	{
		return this->operator()(d,f,g,typename R::Rep_tag());
	}

	// Last homogeneous coordinate given separately
	template<class Iter,class NT> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(int d,Iter f,Iter g,NT const&l)const
	{
		CGAL_assertion(d==std::distance(f,g));
		CGAL_assertion(check_dimension_eq(d,this->kernel().dimension()));
		// RT? better be safe for now
		return typename Constructor::Iterator()(d,CGAL::make_transforming_iterator(f,Divide<FT,NT>(l)),CGAL::make_transforming_iterator(g,Divide<FT,NT>(l)));
	}
	template<class Iter,class NT> inline
	  typename boost::enable_if<is_iterator_type<Iter,std::forward_iterator_tag>,result_type>::type operator()
		(Iter f,Iter g,NT const&l)const
	{
		return this->operator()((int)std::distance(f,g),f,g,l);
	}
};

template<class R_> struct Compute_cartesian_coordinate {
	CGAL_FUNCTOR_INIT_IGNORE(Compute_cartesian_coordinate)
	typedef R_ R;
	typedef typename Get_type<R, RT_tag>::type RT;
	typedef typename R::Vector_ first_argument_type;
	typedef int second_argument_type;
	typedef Tag_true Is_exact;
#ifdef CGAL_CXX11
	typedef decltype(std::declval<const first_argument_type>()[0]) result_type;
#else
	typedef RT const& result_type;
	// RT const& doesn't work with some LA (Eigen2 for instance) so we
	// should use plain RT or find a way to detect this.
#endif

	result_type operator()(first_argument_type const& v,int i)const{
		return v[i];
	}
};

template<class R_> struct Construct_cartesian_const_iterator {
	CGAL_FUNCTOR_INIT_IGNORE(Construct_cartesian_const_iterator)
	typedef R_ R;
	typedef typename R::Vector_ argument_type;
	typedef typename R::LA_vector S_;
	typedef typename R::Point_cartesian_const_iterator result_type;
	// same as Vector
	typedef Tag_true Is_exact;

	result_type operator()(argument_type const& v,Begin_tag)const{
		return S_::vector_begin(v);
	}
	result_type operator()(argument_type const& v,End_tag)const{
		return S_::vector_end(v);
	}
};

template<class R_> struct Midpoint {
	CGAL_FUNCTOR_INIT_IGNORE(Midpoint)
	typedef R_ R;
	typedef typename Get_type<R, Point_tag>::type first_argument_type;
	typedef typename Get_type<R, Point_tag>::type second_argument_type;
	typedef typename Get_type<R, Point_tag>::type result_type;

	result_type operator()(result_type const& a, result_type const& b)const{
		return (a+b)/2;
	}
};

template<class R_> struct Sum_of_vectors {
	CGAL_FUNCTOR_INIT_IGNORE(Sum_of_vectors)
	typedef R_ R;
	typedef typename Get_type<R, Vector_tag>::type first_argument_type;
	typedef typename Get_type<R, Vector_tag>::type second_argument_type;
	typedef typename Get_type<R, Vector_tag>::type result_type;

	result_type operator()(result_type const& a, result_type const& b)const{
		return a+b;
	}
};

template<class R_> struct Difference_of_vectors {
	CGAL_FUNCTOR_INIT_IGNORE(Difference_of_vectors)
	typedef R_ R;
	typedef typename Get_type<R, Vector_tag>::type first_argument_type;
	typedef typename Get_type<R, Vector_tag>::type second_argument_type;
	typedef typename Get_type<R, Vector_tag>::type result_type;

	result_type operator()(result_type const& a, result_type const& b)const{
		return a-b;
	}
};

template<class R_> struct Opposite_vector {
	CGAL_FUNCTOR_INIT_IGNORE(Opposite_vector)
	typedef R_ R;
	typedef typename Get_type<R, Vector_tag>::type result_type;
	typedef typename Get_type<R, Vector_tag>::type argument_type;

	result_type operator()(result_type const& v)const{
		return -v;
	}
};

template<class R_> struct Scalar_product {
	CGAL_FUNCTOR_INIT_IGNORE(Scalar_product)
	typedef R_ R;
	typedef typename R::LA_vector LA;
	typedef typename Get_type<R, RT_tag>::type result_type;
	typedef typename Get_type<R, Vector_tag>::type first_argument_type;
	typedef typename Get_type<R, Vector_tag>::type second_argument_type;

	result_type operator()(first_argument_type const& a, second_argument_type const& b)const{
		return LA::dot_product(a,b);
	}
};

template<class R_> struct Squared_distance_to_origin_stored {
	CGAL_FUNCTOR_INIT_IGNORE(Squared_distance_to_origin_stored)
	typedef R_ R;
	typedef typename R::LA_vector LA;
	typedef typename Get_type<R, RT_tag>::type result_type;
	typedef typename Get_type<R, Point_tag>::type argument_type;

	result_type operator()(argument_type const& a)const{
		return LA::squared_norm(a);
	}
};

template<class R_> struct Squared_distance_to_origin_via_dotprod {
	CGAL_FUNCTOR_INIT_IGNORE(Squared_distance_to_origin_via_dotprod)
	typedef R_ R;
	typedef typename R::LA_vector LA;
	typedef typename Get_type<R, RT_tag>::type result_type;
	typedef typename Get_type<R, Point_tag>::type argument_type;

	result_type operator()(argument_type const& a)const{
		return LA::dot_product(a,a);
	}
};

template<class R_> struct Orientation_of_vectors {
	CGAL_FUNCTOR_INIT_IGNORE(Orientation_of_vectors)
	typedef R_ R;
	typedef typename R::Vector_cartesian_const_iterator first_argument_type;
	typedef typename R::Vector_cartesian_const_iterator second_argument_type;
	typedef typename Get_type<R, Orientation_tag>::type result_type;
	typedef typename R::LA_vector LA;

	template<class Iter>
	result_type operator()(Iter const& f, Iter const& e) const {
		return LA::determinant_of_iterators_to_vectors(f,e);
	}
};

template<class R_> struct Orientation_of_points {
	CGAL_FUNCTOR_INIT_IGNORE(Orientation_of_points)
	typedef R_ R;
	typedef typename R::Point_cartesian_const_iterator first_argument_type;
	typedef typename R::Point_cartesian_const_iterator second_argument_type;
	typedef typename Get_type<R, Orientation_tag>::type result_type;
	typedef typename R::LA_vector LA;

	template<class Iter>
	result_type operator()(Iter const& f, Iter const& e) const {
		return LA::determinant_of_iterators_to_points(f,e);
	}
};

template<class R_> struct PV_dimension {
	CGAL_FUNCTOR_INIT_IGNORE(PV_dimension)
	typedef R_ R;
	typedef typename R::Vector_ argument_type;
	typedef int result_type;
	typedef typename R::LA_vector LA;
	typedef Tag_true Is_exact;

	template<class T>
	result_type operator()(T const& v) const {
		return LA::size_of_vector(v);
	}
};

template<class R_> struct Identity_functor {
  CGAL_FUNCTOR_INIT_IGNORE(Identity_functor)
  template<class T>
  T const& operator()(T const&t) const { return t; }
};

}
} // namespace CGAL
#endif // CGAL_CARTESIAN_LA_FUNCTORS_H