File: Gps_simplifier_traits.h

package info (click to toggle)
cgal 4.0-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 65,068 kB
  • sloc: cpp: 500,870; ansic: 102,544; sh: 321; python: 92; makefile: 75; xml: 2
file content (478 lines) | stat: -rw-r--r-- 13,413 bytes parent folder | download
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright (c) 1997  Tel-Aviv University (Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// 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: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Boolean_set_operations_2/include/CGAL/Boolean_set_operations_2/Gps_simplifier_traits.h $
// $Id: Gps_simplifier_traits.h 67117 2012-01-13 18:14:48Z lrineau $
// 
//
// Author(s)     : Baruch Zukerman <baruchzu@post.tau.ac.il>

#ifndef CGAL_GPS_SIMPLIFIER_TRAITS_H
#define CGAL_GPS_SIMPLIFIER_TRAITS_H

#include <CGAL/Boolean_set_operations_2/Gps_traits_decorator.h>

namespace CGAL {

class Gps_simplifier_curve_data
{
protected:
  unsigned int m_bc;
  unsigned int m_twin_bc;
  unsigned int m_index;

public:
  Gps_simplifier_curve_data()
  {}

  Gps_simplifier_curve_data(unsigned int bc,
                            unsigned int twin_bc,
                            unsigned int index):
    m_bc(bc),
    m_twin_bc(twin_bc),
    m_index(index)
  {}

  unsigned int bc() const
  {
    return m_bc;
  }

  unsigned int twin_bc() const
  {
    return m_twin_bc;
  }

  unsigned int index() const
  {
    return m_index;
  }

  unsigned int& index() 
  {
    return m_index;
  }

  unsigned int& twin_bc() 
  {
    return m_twin_bc;
  }

  void set_bc(unsigned int bc)
  {
    m_bc = bc;
  }

  void set_twin_bc(unsigned int twin_bc)
  {
    m_twin_bc = twin_bc;
  }

  void set_index(unsigned int index)
  {
    m_index = index;
  }
};

struct Gps_simplifier_point_data
{
protected:
  unsigned int m_index;

public:
  Gps_simplifier_point_data()
  {}

  Gps_simplifier_point_data(unsigned int index) : m_index(index)
  {}

  unsigned int index() const
  {
    return m_index;
  }
  
  void set_index(unsigned int index)
  {
    m_index = index;
  }
};

template <class Traits_>
class Gps_simplifier_traits :
  public Gps_traits_decorator<Traits_,
                              Gps_simplifier_curve_data,
                              Gps_simplifier_point_data>
{
public:
  typedef Traits_    Traits;
  typedef Gps_traits_decorator<Traits_,
                               Gps_simplifier_curve_data,
                               Gps_simplifier_point_data>    Base;
  typedef Gps_simplifier_traits<Traits>                      Self;
  typedef typename Traits::X_monotone_curve_2     Base_X_monotone_curve_2;
  typedef typename Traits::Point_2                Base_Point_2;
  typedef typename Traits::Construct_min_vertex_2 Base_Construct_min_vertex_2;
  typedef typename Traits::Construct_max_vertex_2 Base_Construct_max_vertex_2;
  typedef typename Traits::Compare_endpoints_xy_2 Base_Compare_endpoints_xy_2;
  typedef typename Traits::Compare_xy_2           Base_Compare_xy_2;
  typedef typename Traits::Compare_y_at_x_right_2 Base_Compare_y_at_x_right_2;
  typedef typename Traits::Compare_y_at_x_2       Base_Compare_y_at_x_2;
  typedef typename Traits::Intersect_2            Base_Intersect_2;
  typedef typename Traits::Split_2                Base_Split_2;

protected:
  unsigned int m_pgn_size;

  
public:

  typedef typename Base::X_monotone_curve_2       X_monotone_curve_2;
  typedef typename Base::Point_2                  Point_2;
  typedef typename Base::Multiplicity             Multiplicity; 

  typedef typename Base::Curve_data               Curve_data;
  typedef typename Base::Point_data               Point_data;

  Gps_simplifier_traits()
  {}

  Gps_simplifier_traits(const Traits & tr) : Base(tr)
  {}

  unsigned int polygon_size() const
  {
    return m_pgn_size;
  }

  void set_polygon_size(unsigned int pgn_size)
  {
    m_pgn_size = pgn_size;
  }

  bool is_valid_index(unsigned int index) const
  {
    return (index < m_pgn_size);
  }

  unsigned int invalid_index() const
  {
    return (m_pgn_size);
  }


  class Intersect_2
  {
  private:

    Base_Intersect_2             m_base;
    Base_Compare_endpoints_xy_2  m_base_cmp_endpoints;
    Base_Compare_xy_2            m_base_cmp_xy;
    Base_Construct_min_vertex_2  m_ctr_min_v;
    const Self * m_self_tr;

  public:
   
    /*! Constructor. */
    Intersect_2 (const Base_Intersect_2& base,
                 const Base_Compare_endpoints_xy_2& base_cmp_endpoints,
                 const Base_Compare_xy_2& base_cmp_xy,
                 const Base_Construct_min_vertex_2& ,
                 const Self*  tr) : 
      m_base(base),
      m_base_cmp_endpoints(base_cmp_endpoints),
      m_base_cmp_xy(base_cmp_xy),
      m_self_tr(tr)
    {}

    template<class OutputIterator>
    OutputIterator operator() (const X_monotone_curve_2& cv1,
                               const X_monotone_curve_2& cv2,
                               OutputIterator oi) const
    {
      //// if the two curves are incident, do not intersect them
      //if(m_self_tr->is_valid_index(cv1.data().index()) && 
      //   m_self_tr->is_valid_index(cv2.data().index()))
      //{
      //  unsigned int index_diff = 
      //    (cv1.data().index() > cv2.data().index()) ? 
      //    (cv1.data().index() - cv2.data().index()):
      //    (cv2.data().index() - cv1.data().index());

      //  if(index_diff == 1 ||index_diff == m_self_tr->polygon_size() -1)
      //  {
      //    return (oi);
      //  }
      //}
      const std::pair<Base_Point_2, Multiplicity>   *base_pt;
      const Base_X_monotone_curve_2                 *overlap_cv;
      OutputIterator oi_end;
      if(m_base_cmp_xy(m_ctr_min_v(cv1.base()),
                       m_ctr_min_v(cv2.base())) == LARGER)
        oi_end = m_base(cv1.base(), cv2.base(), oi);
      else
        oi_end = m_base(cv2.base(), cv1.base(), oi);

      // convert objects that are associated with Base_X_monotone_curve_2 to
      // the extenede X_monotone_curve_2 
      for(; oi != oi_end; ++oi)
      {
        base_pt = object_cast<std::pair<Base_Point_2, Multiplicity> >(&(*oi));

        if (base_pt != NULL)
        {
          Point_data pt_data(m_self_tr->invalid_index());
          Point_2 point_plus (base_pt->first, pt_data); // the extended point
          *oi = CGAL::make_object(std::make_pair(point_plus, 
                                                 base_pt->second));
        }
        else
        {
          overlap_cv = object_cast<Base_X_monotone_curve_2> (&(*oi));

          if (overlap_cv != NULL)
          {
            unsigned int ov_bc;
            unsigned int ov_twin_bc;
            if(m_base_cmp_endpoints(cv1) == m_base_cmp_endpoints(cv2))
            {
              // cv1 and cv2 have the same directions
              ov_bc = cv1.data().bc() + cv2.data().bc();
              ov_twin_bc = cv1.data().twin_bc() + cv2.data().twin_bc();
            }
            else
            {
              // cv1 and cv2 have opposite directions
              ov_bc = cv1.data().bc() + cv2.data().twin_bc();
              ov_twin_bc = cv1.data().twin_bc() + cv2.data().bc();
            }

            if(m_base_cmp_endpoints(*overlap_cv) != m_base_cmp_endpoints(cv1))
            {
              // overlap_cv, cv1 have opposite directions
              std::swap(ov_bc, ov_twin_bc);
            }

            Curve_data cv_data(ov_bc, ov_twin_bc, m_self_tr->invalid_index());
            *oi = CGAL::make_object (X_monotone_curve_2 (*overlap_cv, cv_data));
          }
        }
      }
      //return past-end iterator
      return oi_end;
    }
  };

   /*! Get an Intersect_2 functor object. */
  Intersect_2 intersect_2_object () const
  {
    return Intersect_2(this->m_base_tr->intersect_2_object(),
                       this->m_base_tr->compare_endpoints_xy_2_object(),
                       this->m_base_tr->compare_xy_2_object(),
                       this->m_base_tr->construct_min_vertex_2_object(),
                       this);
  }

  class Split_2
  {
  private:
    Base_Split_2      m_base_split;
    const Self * m_self_tr;

  public:

    /*! Constructor. */
    Split_2 (const Base_Split_2& base, const Self* tr) :
      m_base_split(base),
      m_self_tr(tr)
    {}

    void operator() (const X_monotone_curve_2& cv, const Point_2 & p,
                     X_monotone_curve_2& c1, X_monotone_curve_2& c2) const
    {
      m_base_split(cv.base(),
                   p.base(),
                   c1.base(),
                   c2.base());
      const Curve_data& cv_data = cv.data();
      c1.set_data(Curve_data(cv_data.bc(),
                             cv_data.twin_bc(),
                             m_self_tr->invalid_index()));
      
      c2.set_data(Curve_data(cv_data.bc(),
                             cv_data.twin_bc(),
                             m_self_tr->invalid_index()));
    }
  };

  /*! Get a Split_2 functor object. */
  Split_2 split_2_object () const
  {
    return Split_2(this->m_base_tr->split_2_object(), this);
  }

  class Construct_min_vertex_2
  {
  private:
    Base_Construct_min_vertex_2 m_base;
    Base_Compare_endpoints_xy_2 m_base_cmp_endpoints;
    const Self * m_self_tr;

  public:

    Construct_min_vertex_2(const Base_Construct_min_vertex_2& base,
                          const Base_Compare_endpoints_xy_2& base_cmp_endpoints,
                          const Self * tr):
      m_base(base),
      m_base_cmp_endpoints(base_cmp_endpoints),
      m_self_tr(tr)
    {}

    /*!
      * Get the left endpoint of the x-monotone curve (segment).
      * \param cv The curve.
      * \return The left endpoint.
      */
    Point_2 operator() (const X_monotone_curve_2 & cv) const
    {
      if(!m_self_tr->is_valid_index(cv.data().index()))
      {
        return Point_2 (m_base(cv.base()), m_self_tr->invalid_index());
      }
      
      Comparison_result res = m_base_cmp_endpoints(cv);
      Point_data pt_data;
      if(res == SMALLER)
      {
        // min vertex is the source
        pt_data.set_index(cv.data().index());
      }
      else
      {
        // min vertex is the target
        pt_data.set_index((cv.data().index() + 1) % m_self_tr->polygon_size());
      }
      return Point_2 (m_base(cv.base()), pt_data);
    }
  };

  /*! Get a Construct_min_vertex_2 functor object. */
  Construct_min_vertex_2 construct_min_vertex_2_object () const
  {
    return Construct_min_vertex_2
      (this->m_base_tr->construct_min_vertex_2_object(),
       this->m_base_tr->compare_endpoints_xy_2_object(),
       this);
  }


  class Construct_max_vertex_2
  {
  private:
    Base_Construct_max_vertex_2      m_base;
    Base_Compare_endpoints_xy_2      m_base_cmp_endpoints;
    const Self * m_self_tr;

  public:

    Construct_max_vertex_2(const Base_Construct_max_vertex_2& base,
                          const Base_Compare_endpoints_xy_2& base_cmp_endpoints,
                          const Self * tr):
      m_base(base),
      m_base_cmp_endpoints(base_cmp_endpoints),
      m_self_tr(tr)
    {}

    /*!
      * Get the right endpoint of the x-monotone curve (segment).
      * \param cv The curve.
      * \return The left endpoint.
      */
    Point_2 operator() (const X_monotone_curve_2 & cv) const
    {
      if(!m_self_tr->is_valid_index(cv.data().index()))
      {
        return Point_2 (m_base(cv.base()), m_self_tr->invalid_index());
      }
      Comparison_result res = m_base_cmp_endpoints(cv);
      Point_data pt_data;
      if(res == SMALLER)
      {
        // min vertex is the target
        pt_data.set_index((cv.data().index() + 1) % m_self_tr->polygon_size());
      }
      else
      {
        // min vertex is the source
        pt_data.set_index(cv.data().index());
      }
      return Point_2 (m_base(cv.base()), pt_data);
    }
  };

  /*! Get a Construct_min_vertex_2 functor object. */
  Construct_max_vertex_2 construct_max_vertex_2_object () const
  {
    return Construct_max_vertex_2
      (this->m_base_tr->construct_max_vertex_2_object(),
       this->m_base_tr->compare_endpoints_xy_2_object(),
       this);
  }

  class Compare_xy_2
  {
  private:
    Base_Compare_xy_2       m_base;
    const Self * m_self_tr;

  public:
    Compare_xy_2(const Base_Compare_xy_2& base,
                 const Self * tr):
      m_base(base),
      m_self_tr(tr)
    {}


    /*!
      * Get the left endpoint of the x-monotone curve (segment).
      * \param cv The curve.
      * \return The left endpoint.
      */
    Comparison_result operator() (const Point_2& p1, const Point_2& p2) const
    {
      //if one of the indexes is invalid, compare p1 and p2
      if(! m_self_tr->is_valid_index(p1.data().index()) ||
        ! m_self_tr->is_valid_index(p2.data().index()))
        return (m_base(p1.base(), p2.base()));

      // if the two point has the same index, return EQUAL
      if(p1.data().index() == p2.data().index())
      {
        return EQUAL;
      }

      return (m_base(p1.base(), p2.base()));
    }
  };


  /*! Get a Construct_min_vertex_2 functor object. */
  Compare_xy_2 compare_xy_2_object () const
  {
    return Compare_xy_2(this->m_base_tr->compare_xy_2_object(), this);
  }
};

} //namespace CGAL

#endif