Rheolef  7.2
an efficient C++ finite element environment
cgal_kernel.h
Go to the documentation of this file.
1 #ifndef _RHEO_CGAL_KERNEL_H
2 #define _RHEO_CGAL_KERNEL_H
23 //
24 // defines a cutsom CGAL kernel by using rheolef::point_basic<T>
25 // => avoid copy of coordinates
26 //
27 // Pierre.Saramito@imag.fr
28 //
29 // 12 march 2012
30 //
31 // References:
32 // https://lists-sop.inria.fr/sympa/arc/cgal-discuss/2010-08/msg00205.html
33 // examples/Kernel_23/MyKernel.h
34 //
35 // https://doc.cgal.org/latest/Kernel_23/index.html#Section_11.5
36 
37 // cicumvents debian bug #683975 in boost-1.49 (fixed in boost-1.50 or 1.53)
38 #ifndef CGAL_HAS_NO_THREADS
39 #define CGAL_HAS_NO_THREADS
40 #endif
41 
42 #pragma GCC diagnostic push
43 #pragma GCC diagnostic ignored "-Weffc++"
44 #pragma GCC diagnostic ignored "-Wignored-attributes"
45 #include <CGAL/Cartesian.h>
46 #pragma GCC diagnostic pop
47 
48 #include "rheolef/point.h"
49 
50 namespace rheolef { namespace custom_cgal {
51 
52 // -------------------------------------------------------------------------
53 // 1) segment
54 // -------------------------------------------------------------------------
55 
56 template <class R_>
58 {
59  typedef typename R_::FT FT;
60  typedef typename R_::Point_2 Point_2;
61  typedef typename R_::Vector_2 Vector_2;
62  typedef typename R_::Direction_2 Direction_2;
63  typedef typename R_::Line_2 Line_2;
64  typedef typename R_::Segment_2 Segment_2;
65  typedef typename R_::Aff_transformation_2 Aff_transformation_2;
66 
67  Point_2 sp_, tp_;
68 public:
69  typedef R_ R;
70 
72 
73  MySegmentC2(const Point_2 &sp, const Point_2 &tp)
74  : sp_(sp), tp_(tp) {}
75 
76  bool is_horizontal() const;
77  bool is_vertical() const;
78  bool has_on(const Point_2 &p) const;
79  bool collinear_has_on(const Point_2 &p) const;
80 
81  bool operator==(const MySegmentC2 &s) const;
82  bool operator!=(const MySegmentC2 &s) const;
83 
84  const Point_2 & source() const
85  {
86  return sp_;
87  }
88 
89  const Point_2 & target() const
90  {
91  return tp_;
92  }
93  const Point_2 & start() const;
94  const Point_2 & end() const;
95 
96  const Point_2 & min () const;
97  const Point_2 & max () const;
98  const Point_2 & vertex(int i) const;
99  const Point_2 & point(int i) const;
100  const Point_2 & operator[](int i) const;
101 
102  FT squared_length() const;
103 
104  Direction_2 direction() const;
105  Vector_2 to_vector() const;
106  Line_2 supporting_line() const;
107  Segment_2 opposite() const;
108 
109  Segment_2 transform(const Aff_transformation_2 &t) const
110  {
111  return Segment_2(t.transform(source()), t.transform(target()));
112  }
113 
114  bool is_degenerate() const;
115  CGAL::Bbox_2 bbox() const;
116 };
117 template < class R >
118 inline
119 bool
121 {
122  return source() == s.source() && target() == s.target();
123 }
124 template < class R >
125 inline
126 bool
128 {
129  return !(*this == s);
130 }
131 template < class R >
132 inline
133 const typename MySegmentC2<R>::Point_2 &
135 {
136  typename R::Less_xy_2 less_xy;
137  return less_xy(source(),target()) ? source() : target();
138 }
139 template < class R >
140 inline
141 const typename MySegmentC2<R>::Point_2 &
143 {
144  typename R::Less_xy_2 less_xy;
145  return less_xy(source(),target()) ? target() : source();
146 }
147 template < class R >
148 inline
149 const typename MySegmentC2<R>::Point_2 &
151 {
152  return (i%2 == 0) ? source() : target();
153 }
154 template < class R >
155 inline
156 const typename MySegmentC2<R>::Point_2 &
158 {
159  return (i%2 == 0) ? source() : target();
160 }
161 template < class R >
162 inline
163 const typename MySegmentC2<R>::Point_2 &
165 {
166  return vertex(i);
167 }
168 template < class R >
169 inline
170 typename MySegmentC2<R>::FT
172 {
173  typename R::Compute_squared_distance_2 squared_distance;
174  return squared_distance(source(), target());
175 }
176 template < class R >
177 inline
178 typename MySegmentC2<R>::Direction_2
180 {
181  typename R::Construct_vector_2 construct_vector;
182  return Direction_2( construct_vector( source(), target()));
183 }
184 template < class R >
185 inline
186 typename MySegmentC2<R>::Vector_2
188 {
189  typename R::Construct_vector_2 construct_vector;
190  return construct_vector( source(), target());
191 }
192 template < class R >
193 inline
194 typename MySegmentC2<R>::Line_2
196 {
197  typename R::Construct_line_2 construct_line;
198 
199  return construct_line(*this);
200 }
201 template < class R >
202 inline
203 typename MySegmentC2<R>::Segment_2
205 {
206  return MySegmentC2<R>(target(), source());
207 }
208 template < class R >
209 inline
210 CGAL::Bbox_2
212 {
213  return source().bbox() + target().bbox();
214 }
215 template < class R >
216 inline
217 bool
219 {
220  return R().equal_y_2_object()(source(), target());
221 }
222 template < class R >
223 inline
224 bool
226 {
227  return R().equal_y_2_object()(source(), target());
228 }
229 template < class R >
230 inline
231 bool
233 {
234  return R().equal_x_2_object()(source(), target());
235 }
236 template < class R >
237 inline
238 bool
240 has_on(const typename MySegmentC2<R>::Point_2 &p) const
241 {
242  return R().collinear_are_ordered_along_line_2_object()(source(), p, target());
243 }
244 template < class R >
245 inline
246 bool
248 collinear_has_on(const typename MySegmentC2<R>::Point_2 &p) const
249 {
250  return R().collinear_has_on_2_object()(*this, p);
251 }
252 template < class R >
253 std::ostream &
254 operator<<(std::ostream &os, const MySegmentC2<R> &s)
255 {
256 #if CGAL_VERSION_NR >= 1041101000
257  switch(CGAL::get_mode(os)) {
258 #elif CGAL_VERSION_NR >= 1040801000
259  switch(os.iword(CGAL::IO::get_static_mode())) {
260 #else
261  switch(os.iword(CGAL::IO::mode)) {
262 #endif
263  case CGAL::IO::ASCII :
264  return os << s.source() << ' ' << s.target();
265  case CGAL::IO::BINARY :
266  return os << s.source() << s.target();
267  default:
268  return os << "MySegmentC2(" << s.source() << ", " << s.target() << ")";
269  }
270 }
271 template < class R >
272 std::istream &
273 operator>>(std::istream &is, MySegmentC2<R> &s)
274 {
275  typename R::Point_2 p, q;
276 
277  is >> p >> q;
278 
279  if (is)
280  s = MySegmentC2<R>(p, q);
281  return is;
282 }
283 // -------------------------------------------------------------------------
284 // 2) bbox
285 // -------------------------------------------------------------------------
286 template <class ConstructBbox_2>
287 class MyConstruct_bbox_2 : public ConstructBbox_2 {
288 public:
289  using ConstructBbox_2::operator();
290  CGAL::Bbox_2 operator()(const point_basic<Float>& p) const {
291  return CGAL::Bbox_2(p.x(), p.y(), p.x(), p.y());
292  }
293 };
294 // -------------------------------------------------------------------------
295 // 3) coordinate iterator
296 // -------------------------------------------------------------------------
297 template <class T>
299 public:
300  const T* operator() (const point_basic<T>& p) { return &p.x(); }
301  const T* operator() (const point_basic<T>& p, int) {
302  const T* pyptr = &p.y();
303  pyptr++;
304  return pyptr;
305  }
306 };
307 // -------------------------------------------------------------------------
308 // 4) construct point
309 // -------------------------------------------------------------------------
310 template <typename K, typename OldK>
312 {
313  typedef typename K::RT RT;
314  typedef typename K::Point_2 Point_2;
315  typedef typename K::Line_2 Line_2;
316  typedef typename Point_2::Rep Rep;
317 public:
318  typedef Point_2 result_type;
319 
320  // Note : the CGAL::Return_base_tag is really internal CGAL stuff.
321  // Unfortunately it is needed for optimizing away copy-constructions,
322  // due to current lack of delegating constructors in the C++ standard.
323  Rep // Point_2
324  operator() (CGAL::Return_base_tag, CGAL::Origin o) const
325  { return Rep(o); }
326 
327  Rep // Point_2
328  operator() (CGAL::Return_base_tag, const RT& x, const RT& y) const
329  { return Rep(x, y); }
330 
331 #ifdef TO_CLEAN
332  Rep // Point_2
333  operator() (CGAL::Return_base_tag, const RT& x, const RT& y, const RT& w) const
334  { return Rep(x, y); }
335 #endif // TO_CLEAN
336 
337  Point_2
338  operator()(const CGAL::Origin& o) const
339  { return point_basic<RT>(0, 0); }
340 
341  Point_2
342  operator()(const RT& x, const RT& y) const
343  {
344  return point_basic<RT>(x, y);
345  }
346 
347  const Point_2&
348  operator()(const Point_2 & p) const
349  {
350  return p;
351  }
352 
353  Point_2
354  operator()(const Line_2& l) const
355  {
356  typename OldK::Construct_point_2 base_operator;
357  Point_2 p = base_operator(l);
358  return p;
359  }
360 
361  Point_2
362  operator()(const Line_2& l, int i) const
363  {
364  typename OldK::Construct_point_2 base_operator;
365  return base_operator(l, i);
366  }
367 
368  // We need this one, as such a functor is in the Filtered_kernel
369  Point_2
370  operator() (const RT& x, const RT& y, const RT& w) const
371  {
372  if(w != 1){
373  return point_basic<RT>(x/w, y/w);
374  } else {
375  return point_basic<RT>(x,y);
376  }
377  }
378 };
379 // -------------------------------------------------------------------------
380 // cartesian kernel
381 // -------------------------------------------------------------------------
382 template <typename NewKernel, typename BaseKernel>
383 class my_cartesian2d_base : public BaseKernel::template Base<NewKernel>::Type {
384  typedef typename BaseKernel::template Base<NewKernel>::Type OldKernel;
385 public:
386  typedef typename BaseKernel::FT FT;
387  typedef NewKernel Kernel;
389 #ifdef TODO
390  typedef point_basic<FT> Point_1;
391  typedef point_basic<FT> Point_3;
392 #endif // TODO
398 
403 
404  template <typename Kernel2>
405  struct Base {
407  };
408 };
409 template <typename FT_>
410 struct kernel_2d
411  : public CGAL::Type_equality_wrapper<
412  my_cartesian2d_base<kernel_2d<FT_>, CGAL::Cartesian<FT_> >,
413  kernel_2d<FT_> >
414 {};
415 template <typename NewKernel, typename BaseKernel>
416 class my_cartesian3d_base : public BaseKernel::template Base<NewKernel>::Type {
417  typedef typename BaseKernel::template Base<NewKernel>::Type OldKernel;
418 public:
419  typedef typename BaseKernel::FT FT;
420  typedef NewKernel Kernel;
421 #ifdef TODO
422  typedef point_basic<FT> Point_2;
423  typedef point_basic<FT> Point_1;
424 #endif // TODO
426 #ifdef TODO
427  typedef MySegmentC2<Kernel> Segment_2;
429  typedef MyConstruct_coord_iterator<FT> Construct_cartesian_const_iterator_2;
430  typedef const FT* Cartesian_const_iterator_2;
431  typedef MyConstruct_point_2<Kernel, OldKernel> Construct_point_2;
432 
433  Construct_point_2 construct_point_2_object() const { return Construct_point_2(); }
434  Construct_bbox_2 construct_bbox_2_object() const { return Construct_bbox_2(); }
435  Construct_cartesian_const_iterator_2 construct_cartesian_const_iterator_2_object() const
436  { return Construct_cartesian_const_iterator_2(); }
437 #endif // TODO
438 
439  template <typename Kernel2>
440  struct Base {
442  };
443 };
444 template <typename FT_>
445 struct kernel_3d
446  : public CGAL::Type_equality_wrapper<
447  my_cartesian3d_base<kernel_3d<FT_>, CGAL::Cartesian<FT_> >,
448  kernel_3d<FT_> >
449 {};
450 
451 }} // namespace rheolef::custom_cgal
452 #endif // _RHEO_CGAL_KERNEL_H
CGAL::Bbox_2 operator()(const point_basic< Float > &p) const
Definition: cgal_kernel.h:290
const T * operator()(const point_basic< T > &p)
Definition: cgal_kernel.h:300
Point_2 operator()(const CGAL::Origin &o) const
Definition: cgal_kernel.h:338
const Point_2 & operator()(const Point_2 &p) const
Definition: cgal_kernel.h:348
Point_2 operator()(const Line_2 &l, int i) const
Definition: cgal_kernel.h:362
Point_2 operator()(const RT &x, const RT &y) const
Definition: cgal_kernel.h:342
Point_2 operator()(const Line_2 &l) const
Definition: cgal_kernel.h:354
Rep operator()(CGAL::Return_base_tag, CGAL::Origin o) const
Definition: cgal_kernel.h:324
const Point_2 & end() const
bool has_on(const Point_2 &p) const
Definition: cgal_kernel.h:240
Segment_2 transform(const Aff_transformation_2 &t) const
Definition: cgal_kernel.h:109
bool operator!=(const MySegmentC2 &s) const
Definition: cgal_kernel.h:127
MySegmentC2(const Point_2 &sp, const Point_2 &tp)
Definition: cgal_kernel.h:73
const Point_2 & max() const
Definition: cgal_kernel.h:142
const Point_2 & target() const
Definition: cgal_kernel.h:89
bool collinear_has_on(const Point_2 &p) const
Definition: cgal_kernel.h:248
const Point_2 & source() const
Definition: cgal_kernel.h:84
const Point_2 & start() const
const Point_2 & min() const
Definition: cgal_kernel.h:134
const Point_2 & point(int i) const
Definition: cgal_kernel.h:157
Direction_2 direction() const
Definition: cgal_kernel.h:179
const Point_2 & operator[](int i) const
Definition: cgal_kernel.h:164
bool operator==(const MySegmentC2 &s) const
Definition: cgal_kernel.h:120
const Point_2 & vertex(int i) const
Definition: cgal_kernel.h:150
MyConstruct_bbox_2< typename OldKernel::Construct_bbox_2 > Construct_bbox_2
Definition: cgal_kernel.h:394
MyConstruct_point_2< Kernel, OldKernel > Construct_point_2
Definition: cgal_kernel.h:397
Construct_bbox_2 construct_bbox_2_object() const
Definition: cgal_kernel.h:400
Construct_cartesian_const_iterator_2 construct_cartesian_const_iterator_2_object() const
Definition: cgal_kernel.h:401
MyConstruct_coord_iterator< FT > Construct_cartesian_const_iterator_2
Definition: cgal_kernel.h:395
Construct_point_2 construct_point_2_object() const
Definition: cgal_kernel.h:399
rheolef::space_base_rep< T, M > t
const point vertex[n_vertex]
Definition: edge.icc:67
Expr1::float_type T
Definition: field_expr.h:230
std::istream & operator>>(std::istream &is, MySegmentC2< R > &s)
Definition: cgal_kernel.h:273
std::ostream & operator<<(std::ostream &os, const MySegmentC2< R > &s)
Definition: cgal_kernel.h:254
This file is part of Rheolef.
Definition: sphere.icc:25
my_cartesian2d_base< Kernel2, BaseKernel > Type
Definition: cgal_kernel.h:406
my_cartesian3d_base< Kernel2, BaseKernel > Type
Definition: cgal_kernel.h:441