File: Iso_box_d.h

package info (click to toggle)
cgal 3.6.1-2
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 62,184 kB
  • ctags: 95,782
  • sloc: cpp: 453,758; ansic: 96,821; sh: 226; makefile: 120; xml: 2
file content (410 lines) | stat: -rw-r--r-- 12,802 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
// Copyright (c) 2002  Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and 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 Lesser General Public License as
// published by the Free Software Foundation; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// 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/CGAL-3.5-branch/Kernel_d/include/CGAL/Kernel_d/Iso_box_d.h $
// $Id: Iso_box_d.h 46239 2008-10-13 13:18:11Z afabri $
// 
//
// Authors       : Hans Tangelder <hanst@cs.uu.nl>, Michael Hoffmann

#ifndef CGAL_ISO_BOX_D_H
#define CGAL_ISO_BOX_D_H

#include <CGAL/basic.h>
#include <CGAL/Handle_for.h>
#include <CGAL/representation_tags.h> 
#include <CGAL/Dimension.h>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cstddef>

namespace CGAL {
  
  namespace Kernel_d {
    struct Begin {};
    struct End {};
    struct Cartesian_end {};
  } // namespace Kernel_d
  
  template < typename Point_, typename Functor_ >
  struct Cartesian_iterator {
    typedef Point_                                    Point;
    typedef Functor_                                  Functor;
    typedef typename Point::Cartesian_const_iterator  Iterator;
    typedef Cartesian_iterator<Point,Functor>         Self;

    typedef typename Functor::result_type  value_type;
    typedef value_type&                    reference;
    typedef value_type*                    pointer;
    typedef std::ptrdiff_t                 difference_type;
    typedef std::input_iterator_tag        iterator_category;
      
  protected:
      
    Iterator pb, qb;
    Functor f;
      
  public:
      
    Cartesian_iterator(const Point& p, const Point& q, Kernel_d::Begin)
      : pb(p.cartesian_begin()), qb(q.cartesian_begin())
    {}
    
    Cartesian_iterator(const Point& p, const Point& q, Kernel_d::End)
      : pb(p.cartesian_end()), qb(q.cartesian_end())
    {}
    
    Cartesian_iterator(const Point& p, const Point& q, Kernel_d::Cartesian_end)
      : pb(p.cartesian_end()), qb(q.cartesian_end())
    {}
    
    Cartesian_iterator(const Point& p, const Point& q, const Functor& f_,
		       Kernel_d::Begin) 
      : pb(p.cartesian_begin()), qb(q.cartesian_begin()), f(f_) 
    {}
      
    Cartesian_iterator(const Point& p, const Point& q, const Functor& f_,
		       Kernel_d::End) 
      : pb(p.cartesian_end()), qb(q.cartesian_end()), f(f_) 
    {}
      
    Cartesian_iterator(const Point& p, const Point& q, const Functor& f_,
		       Kernel_d::Cartesian_end) 
      : pb(p.cartesian_end()), qb(q.cartesian_end()), f(f_) 
    {}
      
    Self& operator++() { ++pb; ++qb; return *this; }
      
    Self operator++(int) {
      Self tmp(*this);
      ++(*this);
      return tmp;
    }
    
    value_type operator*()  const { return f(*pb, *qb); }
    pointer    operator->() const { return &(**this); }
      
    const Functor& functor() const { return f; }
    Iterator base_p() const { return pb; }
    Iterator base_q() const { return qb; }
  };
  
  template < typename Iterator, typename Functor > inline
  bool operator==(const Cartesian_iterator<Iterator,Functor>& x, 
		  const Cartesian_iterator<Iterator,Functor>& y)
  {
    return x.base_p() == y.base_p() && x.base_q() == y.base_q(); 
  }
  
  template < typename Iterator, typename Functor > inline
  bool operator!=(const Cartesian_iterator<Iterator,Functor>& x, 
		  const Cartesian_iterator<Iterator,Functor>& y)
  {
    return ! (x == y);
  }
  
  template < typename Point_, typename Functor_ >
  struct Homogeneous_iterator {
    typedef Point_                                      Point;
    typedef Functor_                                    Functor;
    typedef typename Point::Homogeneous_const_iterator  Iterator;
    typedef Homogeneous_iterator<Point,Functor>         Self;

    typedef typename Functor::result_type  value_type;
    typedef value_type&                    reference;
    typedef value_type*                    pointer;
    typedef std::ptrdiff_t                 difference_type;
    typedef std::input_iterator_tag        iterator_category;
      
  protected:
      
    Iterator pb, qb;
    Functor f;
    typedef typename Kernel_traits<Point>::Kernel::RT RT;
    RT hp, hq; // homogenizing coordinates
    
  public:
      
    Homogeneous_iterator(const Point& p, const Point& q, Kernel_d::Begin)
      : pb(p.homogeneous_begin()), qb(q.homogeneous_begin()),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {}
    
    Homogeneous_iterator(const Point& p, const Point& q, Kernel_d::End)
      : pb(p.homogeneous_end()), qb(q.homogeneous_end()),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {}
    
    Homogeneous_iterator(const Point& p, const Point& q, 
			 Kernel_d::Cartesian_end)
      : pb(p.homogeneous_end()), qb(q.homogeneous_end()),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {
      --pb; --qb;
    }
    
    Homogeneous_iterator(const Point& p, const Point& q, const Functor& f_,
			 Kernel_d::Begin) 
      : pb(p.homogeneous_begin()), qb(q.homogeneous_begin()), f(f_),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {}
    
    Homogeneous_iterator(const Point& p, const Point& q, const Functor& f_,
			 Kernel_d::End) 
      : pb(p.homogeneous_end()), qb(q.homogeneous_end()), f(f_),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {}
      
    Homogeneous_iterator(const Point& p, const Point& q, const Functor& f_,
			 Kernel_d::Cartesian_end) 
      : pb(p.homogeneous_end()), qb(q.homogeneous_end()), f(f_),
	hp(p.homogeneous(p.dimension())), hq(q.homogeneous(q.dimension()))
    {
      --pb; --qb;
    }
      
    Self& operator++() { ++pb; ++qb; return *this; }
      
    Self operator++(int) {
      Self tmp(*this);
      ++(*this);
      return tmp;
    }
    
    value_type operator*()  const { return f(*pb * hq, *qb * hp); }
    pointer    operator->() const { return &(**this); }
      
    const Functor& functor() const { return f; }
    Iterator base_p() const { return pb; }
    Iterator base_q() const { return qb; }
  };
  
  template < typename Iterator, typename Functor > inline
  bool operator==(const Homogeneous_iterator<Iterator,Functor>& x, 
		  const Homogeneous_iterator<Iterator,Functor>& y)
  {
    return x.base_p() == y.base_p() && x.base_q() == y.base_q(); 
  }
  
  template < typename Iterator, typename Functor > inline
  bool operator!=(const Homogeneous_iterator<Iterator,Functor>& x, 
		  const Homogeneous_iterator<Iterator,Functor>& y)
  {
    return ! (x == y);
  }
  
  template < typename Kernel_ > class Iso_box_d;
  
  namespace Kernel_d {

    template < typename RepTag > struct Coordinate_iterator;
    
    template <> struct Coordinate_iterator<Cartesian_tag> {
      template < typename Point, typename Functor > 
      struct Iterator {
	typedef Cartesian_iterator<Point,Functor>  type;
      };
    };
    
    template <> struct Coordinate_iterator<Homogeneous_tag> {
      template < typename Point, typename Functor > 
      struct Iterator {
	typedef Homogeneous_iterator<Point,Functor>  type;
      };
    };

    template < typename Kernel_ > 
    struct Iso_box_d_rep {
      typedef Kernel_                   Kernel;
      friend class Iso_box_d<Kernel>;
      
    protected:

      typedef typename Kernel::Point_d  Point_d;
      Point_d lower, upper;

    public:

      Iso_box_d_rep() {}
      
      template < typename InputIteratorI, typename InputIteratorII >
      Iso_box_d_rep(int dim,
		    InputIteratorI b1, InputIteratorI e1,
		    InputIteratorII b2, InputIteratorII e2)
	: lower(dim, b1, e1), upper(dim, b2, e2)
      {}
      
    };

  } // namespace Kernel_d

  template < typename Kernel_ > 
  class Iso_box_d : public Handle_for< Kernel_d::Iso_box_d_rep<Kernel_> > 
  { 

  public:
    typedef Kernel_                   Kernel;
    typedef Kernel_                   R;
    
  protected:

    typedef Kernel_d::Iso_box_d_rep<Kernel>  Rep;
    typedef Handle_for<Rep>                  Base;
    typedef Iso_box_d<Kernel>                Self;

    using Base::ptr;

    typedef typename Kernel::RT       RT;
    typedef typename Kernel::FT       FT;
    typedef typename Kernel::Point_d  Point_d;
    typedef typename Kernel::Rep_tag  Rep_tag;
   
    typedef CGAL::Kernel_d::Coordinate_iterator<Rep_tag>           CIRT;
    typedef typename CIRT::template Iterator<Point_d,Min<RT> >::type  MinIter;
    typedef typename CIRT::template Iterator<Point_d,Max<RT> >::type  MaxIter;

    typedef Kernel_d::Begin            Begin; 
    typedef Kernel_d::End              End; 
    typedef Kernel_d::Cartesian_end    Cartesian_end; 

    RT volume_nominator() const
    {
      typedef typename CIRT::template Iterator<Point_d,std::minus<RT> >::type
	      Iter;
      Iter b(ptr()->upper, ptr()->lower, Begin());
      Iter e(ptr()->upper, ptr()->lower, Cartesian_end());
      return std::accumulate(b, e, RT(1), std::multiplies<RT>());
    }
    
    RT volume_denominator() const
    {
      RT den = 
	ptr()->lower.homogeneous(dimension()) * 
	ptr()->upper.homogeneous(dimension());
      RT prod = den;
      for (int i = 1; i < dimension(); ++i)
	prod *= den;
      return prod;
    }
    
    FT volume(Homogeneous_tag) const
    { 
      return FT(volume_nominator(), volume_denominator());
    }
    
    FT volume(Cartesian_tag) const
    { 
      return volume_nominator();
    }
    
public:

    typedef CGAL::Dynamic_dimension_tag Ambient_dimension;
    typedef CGAL::Dynamic_dimension_tag Feature_dimension;

    Iso_box_d() {}
    
    Iso_box_d(const Point_d& p, const Point_d& q)
      : Base(Rep(p.dimension(), 
		 MinIter(p, q, Begin()), MinIter(p, q, End()),
		 MaxIter(p, q, Begin()), MaxIter(p, q, End())))
    { 
      CGAL_precondition(p.dimension() == q.dimension());
    }
    
    Bounded_side bounded_side(const Point_d& p) const
    { 
      CGAL_precondition(p.dimension() == dimension());
      typedef typename CIRT::template Iterator<Point_d,Compare<RT> >::type
	      Iter;
      
      Iter il(p, ptr()->lower, Begin());
      Iter ilend(p, ptr()->lower, Cartesian_end());
      Iter iu(p, ptr()->upper, Begin());
      CGAL_assertion_code(Iter iuend(p, ptr()->upper, Cartesian_end()));
      
      for (; il != ilend; ++il, ++iu) {
	CGAL_assertion(iu != iuend);
	Comparison_result low = *il;
	Comparison_result upp = *iu;
	if (low == LARGER && upp == SMALLER) continue;
	if (low == SMALLER || upp == LARGER) return ON_UNBOUNDED_SIDE;
	return ON_BOUNDARY;
      }
      return ON_BOUNDED_SIDE;
    }

    bool has_on_bounded_side(const Point_d& p) const
    { 
      return (bounded_side(p)==ON_BOUNDED_SIDE);
    } 

    bool has_on_unbounded_side(const Point_d& p) const
    {
      return (bounded_side(p)==ON_UNBOUNDED_SIDE); 
    } 

    bool has_on_boundary(const Point_d& p) const
    {
      return (bounded_side(p)==ON_BOUNDARY); 
    } 

    int dimension() const { return ptr()->lower.dimension();}
    
    // FIXME!
    FT min_coord(int i) const { return ptr()->lower[i]; }

    FT max_coord(int i) const { return ptr()->upper[i]; }

    const Point_d& min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return ptr()->lower; }

    const Point_d& max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return ptr()->upper; }

    FT volume() const { return volume(Rep_tag()); }
    
    bool is_degenerate() const
    {
      typedef typename CIRT::
	      template Iterator<Point_d,std::equal_to<RT> >::type Iter;
      // omit homogenizing coordinates
      Iter e(ptr()->lower, ptr()->upper, Cartesian_end());
      return 
	e != std::find(Iter(ptr()->lower, ptr()->upper, Begin()), e, true);
    }

}; // end of class

template < typename Kernel >
inline bool
operator==(const Iso_box_d<Kernel>& b1, Iso_box_d<Kernel>& b2)
{
  CGAL_precondition(b1.dimension() == b2.dimension());
  return (b1.min)() == (b2.min)() && (b1.max)() == (b2.max)();
}

template < typename Kernel >
inline bool
operator!=(const Iso_box_d<Kernel>& b1, Iso_box_d<Kernel>& b2)
{
  return ! (b1 == b2); 
}

} // namespace CGAL

#endif // CGAL_ISO_BOX_D_H