File: 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 (236 lines) | stat: -rw-r--r-- 7,322 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
// Copyright (c) 2004  Max-Planck-Institute Saarbruecken (Germany).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you may redistribute it under
// the terms of the Q Public License version 1.0.
// See the file LICENSE.QPL 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/Box_intersection_d/include/CGAL/Box_intersection_d/Box_d.h $
// $Id: Box_d.h 33370 2006-08-17 09:05:15Z afabri $
// 
//
// Author(s)     : Lutz Kettner  <kettner@mpi-sb.mpg.de>
//                 Andreas Meyer <ameyer@mpi-sb.mpg.de>

#ifndef CGAL_BOX_INTERSECTION_D_BOX_D_H
#define CGAL_BOX_INTERSECTION_D_BOX_D_H

#include <CGAL/basic.h>
#include <CGAL/Bbox_2.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/Box_intersection_d/box_limits.h>
#include <algorithm>

CGAL_BEGIN_NAMESPACE

namespace Box_intersection_d {

// Pseudo template class to skip the need for a C file for the static counter
template <class Dummy>
struct Unique_numbers {
    typedef std::size_t ID;
    Unique_numbers() : i(n++) {}
    std::size_t id() const { return i; }
private:
    static std::size_t n;
    std::size_t i;
};
template <class Dummy>
std::size_t Unique_numbers<Dummy>::n = 0;


// Policies for id-number of boxes
struct ID_NONE {};
struct ID_EXPLICIT {};
struct ID_FROM_BOX_ADDRESS {};
struct ID_FROM_HANDLE {};

// Generic template signature of boxes, specialized for policies
template<class NT_, int N, class IdPolicy = ID_EXPLICIT> 
class Box_d;

// ID_NONE is used as base class and cannot be used directly in the algorithms
template<class NT_, int N>
class Box_d< NT_, N, ID_NONE> {
protected:
    NT_ lo[N];
    NT_ hi[N];
public:
    typedef NT_         NT;
    typedef std::size_t ID;

    Box_d() {}
    Box_d(bool complete) { init(complete); }
    Box_d(NT l[N], NT h[N]) {
        std::copy( l, l + N, lo );
        std::copy( h, h + N, hi );
    }
    void init (bool complete = false) {
        NT inf = box_limits<NT>::inf();
        NT sup = box_limits<NT>::sup();
        if(!complete)
            std::swap(inf,sup);
        std::fill( lo, lo+N, inf );
        std::fill( hi, hi+N, sup );
    }
    void extend(NT p[N]) {
        for( int dim = 0; dim < N; ++dim ) {
            lo[dim] = (std::min)( lo[dim], p[dim] );
            hi[dim] = (std::max)( hi[dim], p[dim] );
        }
    }
    void extend(std::pair<NT,NT> p[N]) {
        for( int dim = 0; dim < N; ++dim ) {
            lo[dim] = (std::min)( lo[dim], p[dim].first );
            hi[dim] = (std::max)( hi[dim], p[dim].second );
        }
    }
    static int dimension() { return N; }
    NT min_coord(int dim) const { return lo[dim]; }
    NT max_coord(int dim) const { return hi[dim]; }
};

// Specialization for Bbox_2, i.e. double and dim 2.
template<>
class Box_d< double, 2, ID_NONE> {
protected:
    Bbox_2 bbx;
public:
    typedef double      NT;
    typedef std::size_t ID;

    Box_d() {}
    Box_d(bool complete) { init(complete); }
    Box_d(NT l[2], NT h[2]) : bbx(l[0], l[1], h[0], h[1]) {}
    Box_d( const Bbox_2& b) : bbx( b) {}
    const Bbox_2& bbox() const { return bbx; }
    void init () {
        NT inf = box_limits<NT>::inf();
        NT sup = box_limits<NT>::sup();
        bbx = Bbox_2( sup, sup, inf, inf);
    }
    void init (bool complete) {
        NT inf = box_limits<NT>::inf();
        NT sup = box_limits<NT>::sup();
        if ( complete)
            bbx = Bbox_2( inf, inf, sup, sup);
        else
            bbx = Bbox_2( sup, sup, inf, inf);
    }
    void extend(NT p[2]) {
        bbx = Bbox_2(
            (std::min)( bbx.xmin(), p[0]),
            (std::min)( bbx.ymin(), p[1]),
            (std::max)( bbx.xmax(), p[0]),
            (std::max)( bbx.ymax(), p[1]));
    }
    void extend(std::pair<NT,NT> p[2]) {
        bbx = Bbox_2(
	    (std::min)( bbx.xmin(), p[0].first),
	    (std::min)( bbx.ymin(), p[1].first),
	    (std::max)( bbx.xmax(), p[0].second),
	    (std::max)( bbx.ymax(), p[1].second));
    }
    static int dimension() { return 2; }
    NT min_coord(int dim) const { return (dim==0) ? bbx.xmin() : bbx.ymin();}
    NT max_coord(int dim) const { return (dim==0) ? bbx.xmax() : bbx.ymax();}
};

// Specialization for Bbox_3, i.e. double and dim 3.
template<>
class Box_d< double, 3, ID_NONE> {
protected:
    Bbox_3 bbx;
public:
    typedef double      NT;
    typedef std::size_t ID;

    Box_d() {}
    Box_d(bool complete) { init(complete); }
    Box_d(NT l[3], NT h[3]) : bbx(l[0], l[1], l[2], h[0], h[1], h[2]) {}
    Box_d( const Bbox_3& b) : bbx( b) {}
    const Bbox_3& bbox() const { return bbx; }
    void init () {
        NT inf = box_limits<NT>::inf();
        NT sup = box_limits<NT>::sup();
        bbx = Bbox_3( sup, sup, sup, inf, inf, inf);
    }
    void init (bool complete) {
        NT inf = box_limits<NT>::inf();
        NT sup = box_limits<NT>::sup();
        if ( complete)
            bbx = Bbox_3( inf, inf, inf, sup, sup, sup);
        else
            bbx = Bbox_3( sup, sup, sup, inf, inf, inf);
    }
    void extend(NT p[3]) {
        bbx = Bbox_3(
            (std::min)( bbx.xmin(), p[0]),
            (std::min)( bbx.ymin(), p[1]),
            (std::min)( bbx.zmin(), p[2]),
            (std::max)( bbx.xmax(), p[0]),
            (std::max)( bbx.ymax(), p[1]),
            (std::max)( bbx.zmax(), p[2]));
    }
    void extend(std::pair<NT,NT> p[3]) {
        bbx = Bbox_3(
	    (std::min)( bbx.xmin(), p[0].first),
	    (std::min)( bbx.ymin(), p[1].first),
	    (std::min)( bbx.zmin(), p[2].first),
	    (std::max)( bbx.xmax(), p[0].second),
	    (std::max)( bbx.ymax(), p[1].second),
	    (std::max)( bbx.zmax(), p[2].second));
    }
    static int dimension() { return 3; }
    NT min_coord(int dim) const { 
        return (dim==0) ? bbx.xmin() : ((dim==1) ? bbx.ymin() : bbx.zmin());
    }
    NT max_coord(int dim) const {
        return (dim==0) ? bbx.xmax() : ((dim==1) ? bbx.ymax() : bbx.zmax());
    }
};

// ID_EXPLICIT
template<class NT_, int N>
class Box_d< NT_, N, ID_EXPLICIT> : public Box_d< NT_, N, ID_NONE>,
                                    public Unique_numbers<int> {
public:
    typedef Box_d< NT_, N, ID_NONE> Base;
    typedef NT_                     NT;
    typedef typename Unique_numbers<int>::ID ID;
    Box_d() {}
    Box_d(bool complete) : Base(complete) {}
    Box_d(NT l[N], NT h[N]) : Base( l, h) {}
    Box_d( const Bbox_2& b) : Base( b) {}
    Box_d( const Bbox_3& b) : Base( b) {}
};

// ID_FROM_BOX_ADDRESS
template<class NT_, int N>
class Box_d< NT_, N, ID_FROM_BOX_ADDRESS> : public Box_d< NT_, N, ID_NONE> {
public:
    typedef Box_d< NT_, N, ID_NONE> Base;
    typedef NT_                     NT;
    typedef std::size_t             ID;

    Box_d() {}
    Box_d(bool complete) : Base(complete) {}
    Box_d(NT l[N], NT h[N]) : Base( l, h) {}
    Box_d( const Bbox_2& b) : Base( b) {}
    Box_d( const Bbox_3& b) : Base( b) {}
    ID  id() const { return reinterpret_cast<ID>(this); }
};


} // end namespace Box_intersection_d


CGAL_END_NAMESPACE

#endif