rheolef  6.3
cgal_kernel.h
Go to the documentation of this file.
1 #ifndef _RHEO_CGAL_KERNEL_H
2 #define _RHEO_CGAL_KERNEL_H
3 // defines a cutsom CGAL kernel by using rheolef::point_basic<T>
4 // => avoid copy of coordinates
5 // https://lists-sop.inria.fr/sympa/arc/cgal-discuss/2010-08/msg00205.html
6 // examples/Kernel_23/MyKernel.h
7 #include <CGAL/Cartesian.h>
8 #include "rheolef/point.h"
9 
10 namespace rheolef { namespace custom_cgal {
11 
12 
13 template <class R_>
15 {
16  typedef typename R_::FT FT;
17  typedef typename R_::Point_2 Point_2;
18  typedef typename R_::Vector_2 Vector_2;
19  typedef typename R_::Direction_2 Direction_2;
20  typedef typename R_::Line_2 Line_2;
21  typedef typename R_::Segment_2 Segment_2;
22  typedef typename R_::Aff_transformation_2 Aff_transformation_2;
23 
25 public:
26  typedef R_ R;
27 
29 
30  MySegmentC2(const Point_2 &sp, const Point_2 &tp)
31  : sp_(sp), tp_(tp) {}
32 
33  bool is_horizontal() const;
34  bool is_vertical() const;
35  bool has_on(const Point_2 &p) const;
36  bool collinear_has_on(const Point_2 &p) const;
37 
38  bool operator==(const MySegmentC2 &s) const;
39  bool operator!=(const MySegmentC2 &s) const;
40 
41  const Point_2 & source() const
42  {
43  return sp_;
44  }
45 
46  const Point_2 & target() const
47  {
48  return tp_;
49  }
50  const Point_2 & start() const;
51  const Point_2 & end() const;
52 
53  const Point_2 & min () const;
54  const Point_2 & max () const;
55  const Point_2 & vertex(int i) const;
56  const Point_2 & point(int i) const;
57  const Point_2 & operator[](int i) const;
58 
59  FT squared_length() const;
60 
61  Direction_2 direction() const;
62  Vector_2 to_vector() const;
63  Line_2 supporting_line() const;
64  Segment_2 opposite() const;
65 
67  {
68  return Segment_2(t.transform(source()), t.transform(target()));
69  }
70 
71  bool is_degenerate() const;
72  CGAL::Bbox_2 bbox() const;
73 };
74 template < class R >
75 inline
76 bool
78 {
79  return source() == s.source() && target() == s.target();
80 }
81 template < class R >
82 inline
83 bool
85 {
86  return !(*this == s);
87 }
88 template < class R >
89 inline
90 const typename MySegmentC2<R>::Point_2 &
92 {
93  typename R::Less_xy_2 less_xy;
94  return less_xy(source(),target()) ? source() : target();
95 }
96 template < class R >
97 inline
98 const typename MySegmentC2<R>::Point_2 &
100 {
101  typename R::Less_xy_2 less_xy;
102  return less_xy(source(),target()) ? target() : source();
103 }
104 template < class R >
105 inline
106 const typename MySegmentC2<R>::Point_2 &
108 {
109  return (i%2 == 0) ? source() : target();
110 }
111 template < class R >
112 inline
113 const typename MySegmentC2<R>::Point_2 &
115 {
116  return (i%2 == 0) ? source() : target();
117 }
118 template < class R >
119 inline
120 const typename MySegmentC2<R>::Point_2 &
122 {
123  return vertex(i);
124 }
125 template < class R >
126 inline
127 typename MySegmentC2<R>::FT
129 {
130  typename R::Compute_squared_distance_2 squared_distance;
131  return squared_distance(source(), target());
132 }
133 template < class R >
134 inline
137 {
138  typename R::Construct_vector_2 construct_vector;
139  return Direction_2( construct_vector( source(), target()));
140 }
141 template < class R >
142 inline
145 {
146  typename R::Construct_vector_2 construct_vector;
147  return construct_vector( source(), target());
148 }
149 template < class R >
150 inline
151 typename MySegmentC2<R>::Line_2
153 {
154  typename R::Construct_line_2 construct_line;
155 
156  return construct_line(*this);
157 }
158 template < class R >
159 inline
162 {
163  return MySegmentC2<R>(target(), source());
164 }
165 template < class R >
166 inline
167 CGAL::Bbox_2
169 {
170  return source().bbox() + target().bbox();
171 }
172 template < class R >
173 inline
174 bool
176 {
177  return R().equal_y_2_object()(source(), target());
178 }
179 template < class R >
180 inline
181 bool
183 {
184  return R().equal_y_2_object()(source(), target());
185 }
186 template < class R >
187 inline
188 bool
190 {
191  return R().equal_x_2_object()(source(), target());
192 }
193 template < class R >
194 inline
195 bool
197 has_on(const typename MySegmentC2<R>::Point_2 &p) const
198 {
199  return R().collinear_are_ordered_along_line_2_object()(source(), p, target());
200 }
201 template < class R >
202 inline
203 bool
206 {
207  return R().collinear_has_on_2_object()(*this, p);
208 }
209 template < class R >
210 std::ostream &
211 operator<<(std::ostream &os, const MySegmentC2<R> &s)
212 {
213  switch(os.iword(CGAL::IO::mode)) {
214  case CGAL::IO::ASCII :
215  return os << s.source() << ' ' << s.target();
216  case CGAL::IO::BINARY :
217  return os << s.source() << s.target();
218  default:
219  return os << "MySegmentC2(" << s.source() << ", " << s.target() << ")";
220  }
221 }
222 template < class R >
223 std::istream &
224 operator>>(std::istream &is, MySegmentC2<R> &s)
225 {
226  typename R::Point_2 p, q;
227 
228  is >> p >> q;
229 
230  if (is)
231  s = MySegmentC2<R>(p, q);
232  return is;
233 }
234 template <class ConstructBbox_2>
235 class MyConstruct_bbox_2 : public ConstructBbox_2 {
236 public:
237  using ConstructBbox_2::operator();
238  CGAL::Bbox_2 operator()(const point_basic<double>& p) const {
239  return CGAL::Bbox_2(p.x(), p.y(), p.x(), p.y());
240  }
241 };
242 template <class T>
244 public:
245  const T* operator() (const point_basic<T>& p) { return &p.x(); }
246  const T* operator() (const point_basic<T>& p, int) {
247  const T* pyptr = &p.y();
248  pyptr++;
249  return pyptr;
250  }
251 };
252 template <typename K, typename OldK>
254 {
255  typedef typename K::RT RT;
256  typedef typename K::Point_2 Point_2;
257  typedef typename K::Line_2 Line_2;
258  typedef typename Point_2::Rep Rep;
259 public:
261 
262  Rep // Point_2
263  operator() (CGAL::Return_base_tag, CGAL::Origin o) const
264  { return Rep(o); }
265 
266  Rep // Point_2
267  operator() (CGAL::Return_base_tag, const RT& x, const RT& y) const
268  { return Rep(x, y); }
269 
270 #ifdef TO_CLEAN
271  Rep // Point_2
272  operator() (CGAL::Return_base_tag, const RT& x, const RT& y, const RT& w) const
273  { return Rep(x, y); }
274 #endif // TO_CLEAN
275 
276  Point_2
277  operator()(CGAL::Origin o) const
278  { return point_basic<RT>(0, 0); }
279 
280  Point_2
281  operator()(const RT& x, const RT& y) const
282  {
283  return point_basic<RT>(x, y);
284  }
285 
286  Point_2
287  operator()(const Line_2& l) const
288  {
289  typename OldK::Construct_point_2 base_operator;
290  Point_2 p = base_operator(l);
291  return p;
292  }
293 
294  Point_2
295  operator()(const Line_2& l, int i) const
296  {
297  typename OldK::Construct_point_2 base_operator;
298  return base_operator(l, i);
299  }
300 
301  Point_2
302  operator() (const RT& x, const RT& y, const RT& w) const
303  {
304  if(w != 1){
305  return point_basic<RT>(x/w, y/w);
306  } else {
307  return point_basic<RT>(x,y);
308  }
309  }
310 };
311 template <typename NewKernel, typename BaseKernel>
312 class my_cartesian2d_base : public BaseKernel::template Base<NewKernel>::Type {
313  typedef typename BaseKernel::template Base<NewKernel>::Type OldKernel;
314 public:
315  typedef typename BaseKernel::FT FT;
316  typedef NewKernel Kernel;
318 #ifdef TODO
319  typedef point_basic<FT> Point_1;
320  typedef point_basic<FT> Point_3;
321 #endif // TODO
327 
332 
333  template <typename Kernel2>
334  struct Base {
336  };
337 };
338 template <typename FT_>
339 struct kernel_2d
340  : public CGAL::Type_equality_wrapper<
341  my_cartesian2d_base<kernel_2d<FT_>, CGAL::Cartesian<FT_> >,
342  kernel_2d<FT_> >
343 {};
344 template <typename NewKernel, typename BaseKernel>
345 class my_cartesian3d_base : public BaseKernel::template Base<NewKernel>::Type {
346  typedef typename BaseKernel::template Base<NewKernel>::Type OldKernel;
347 public:
348  typedef typename BaseKernel::FT FT;
349  typedef NewKernel Kernel;
350 #ifdef TODO
351  typedef point_basic<FT> Point_2;
352  typedef point_basic<FT> Point_1;
353 #endif // TODO
355 #ifdef TODO
356  typedef MySegmentC2<Kernel> Segment_2;
358  typedef MyConstruct_coord_iterator<FT> Construct_cartesian_const_iterator_2;
359  typedef const FT* Cartesian_const_iterator_2;
360  typedef MyConstruct_point_2<Kernel, OldKernel> Construct_point_2;
361 
362  Construct_point_2 construct_point_2_object() const { return Construct_point_2(); }
363  Construct_bbox_2 construct_bbox_2_object() const { return Construct_bbox_2(); }
364  Construct_cartesian_const_iterator_2 construct_cartesian_const_iterator_2_object() const
365  { return Construct_cartesian_const_iterator_2(); }
366 #endif // TODO
367 
368  template <typename Kernel2>
369  struct Base {
371  };
372 };
373 template <typename FT_>
374 struct kernel_3d
375  : public CGAL::Type_equality_wrapper<
376  my_cartesian3d_base<kernel_3d<FT_>, CGAL::Cartesian<FT_> >,
377  kernel_3d<FT_> >
378 {};
379 
380 }} // namespace rheolef::custom_cgal
381 #endif // _RHEO_CGAL_KERNEL_H
382