File: polyhedral_to_labeled_function_wrapper.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 (342 lines) | stat: -rw-r--r-- 9,848 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
// Copyright (c) 2009 INRIA Sophia-Antipolis (France).
// 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/Mesh_3/include/CGAL/Mesh_3/polyhedral_to_labeled_function_wrapper.h $
// $Id: polyhedral_to_labeled_function_wrapper.h 67117 2012-01-13 18:14:48Z lrineau $
//
//
// Author(s)     : Stéphane Tayeb
//
//******************************************************************************
// File Description :
//
//******************************************************************************

#ifndef CGAL_MESH_3_POLYHEDRAL_TO_LABELED_FUNCTION_WRAPPER_H
#define CGAL_MESH_3_POLYHEDRAL_TO_LABELED_FUNCTION_WRAPPER_H


#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_polyhedron_triangle_primitive.h>
#include <CGAL/AABB_polyhedron_segment_primitive.h>

namespace CGAL {

namespace Mesh_3 {


namespace details {

inline
double
max_length(const Bbox_3& b)
{
  return (std::max)(b.xmax()-b.xmin(),
                    (std::max)(b.ymax()-b.ymin(),b.zmax()-b.zmin()) );
}

template <class K>
inline
typename K::Sphere_3
bounding_sphere(const Bbox_3& bbox)
{
  const double radius = max_length(bbox)*2;

  typename K::Point_3 center( (bbox.xmin()+bbox.xmax())/2.,
                              (bbox.ymin()+bbox.ymax())/2.,
                              (bbox.zmin()+bbox.zmax())/2. );

  return typename K::Sphere_3(center, radius * radius + 1.);
}


} // end namespace details

template<class Polyhedron_, class BGT>
class Polyhedral_to_labeled_function_wrapper
{
public:
  // AABB_tree Types
  typedef class AABB_const_polyhedron_triangle_primitive<BGT, Polyhedron_>
                                                    AABB_primitive;
  typedef class AABB_traits<BGT,AABB_primitive>     AABB_traits;
  typedef class AABB_tree<AABB_traits>              AABB_tree;
  typedef typename AABB_traits::Bounding_box        Bounding_box;

  // Geometric traits types
  typedef typename BGT::Point_3     Point_3;
  typedef typename BGT::Sphere_3    Sphere_3;

  // Return type
  typedef int return_type;

  /// Constructor
  Polyhedral_to_labeled_function_wrapper(const Polyhedron_& p,
                                         const int nb_step,
                                         const double step_size,
                                         const double first_level)
    : tree_(new AABB_tree(p.facets_begin(), p.facets_end()))
    , hint_(p.facets_begin()->halfedge()->vertex()->point())
    , nb_step_(nb_step)
    , step_size_(step_size)
    , first_level_(first_level)
  {
    tree_->accelerate_distance_queries();
  }

  // Default copy constructor and assignment operator are ok

  /// Destructor
  ~Polyhedral_to_labeled_function_wrapper() {}

  /// Operator ()
  return_type operator()(const Point_3& p, bool use_cache=false) const
  {
    const double sq_distance = sq_sign_dist(p,use_cache);

    double lvl = first_level_;
    double sq_lvl = CGAL::sign(lvl)*lvl*lvl;
    for ( int i = 1 ; i <= nb_step_ ; ++i )
    {
      if ( sq_distance < sq_lvl ) return i;
      lvl += step_size_;
      sq_lvl = CGAL::sign(lvl)*lvl*lvl;
    }
    return 0;
  }

  Sphere_3 bounding_sphere() const
  {
    return details::bounding_sphere<BGT>(tree_->bbox());
  }

private:
  double sq_sign_dist(const Point_3& p, bool use_cache) const
  {
    const Bounding_box bbox = tree_->bbox();
    const double diameter = details::max_length(bbox) * 2;

    typedef typename BGT::RT RT;
    typedef typename BGT::FT FT;
    typedef typename BGT::Segment_3 Segment_3;

    Random_points_on_sphere_3<Point_3> random_point(1.);

    typename BGT::Construct_vector_3 vector =
      BGT().construct_vector_3_object();
    typename BGT::Construct_segment_3 segment =
      BGT().construct_segment_3_object();
    typename BGT::Construct_translated_point_3 translate =
      BGT().construct_translated_point_3_object();
    typename BGT::Construct_scaled_vector_3 scale =
      BGT().construct_scaled_vector_3_object();

    Segment_3 seg =
      segment(p,
              translate(p, scale(vector(ORIGIN,*random_point), diameter)) );

    double sign = 1.;
    if (   p.x() >= bbox.xmin() && p.x() <= bbox.xmax()
        && p.y() >= bbox.ymin() && p.y() <= bbox.ymax()
        && p.z() >= bbox.zmin() && p.z() <= bbox.zmax()
        && ((tree_->number_of_intersected_primitives(seg) % 2) == 1) )
    {
        // Point is inside domain
        sign = -1;
    }

    Point_3 proj;
    if ( use_cache )
      proj = tree_->closest_point(p,hint_);
    else
      proj = tree_->closest_point(p);

    // update cache
    hint_ = proj;

    // Return sign squared distance
    typename BGT::Compute_squared_distance_3 distance =
      BGT().compute_squared_distance_3_object();

    return (sign*CGAL::to_double(distance(p,proj)));
  }

private:
  /// Functions to wrap
  AABB_tree* tree_;
  mutable Point_3 hint_;
  const int nb_step_;
  const double step_size_;
  const double first_level_;

};  // end class Polyhedral_to_labeled_function_wrapper



template<class Polyhedron_, class BGT>
class Polyhedral_tolerance_to_labeled_function_wrapper
{
public:
  // AABB_tree Types
  typedef class AABB_const_polyhedron_triangle_primitive<BGT, Polyhedron_>
                                                    AABB_primitive;
  typedef class AABB_traits<BGT,AABB_primitive>     AABB_traits;
  typedef class AABB_tree<AABB_traits>              AABB_tree;
  typedef typename AABB_traits::Bounding_box        Bounding_box;

  // Geometric traits types
  typedef typename BGT::Point_3     Point_3;
  typedef typename BGT::Sphere_3    Sphere_3;

  // Return type
  typedef int return_type;

  /// Constructor
  Polyhedral_tolerance_to_labeled_function_wrapper(const Polyhedron_& p,
                                                   const double tolerance_size)
    : tree_(new AABB_tree(p.facets_begin(), p.facets_end()))
    , sq_tolerance_size_(tolerance_size*tolerance_size)
    , hint_(p.facets_begin()->halfedge()->vertex()->point())
  {
    tree_->accelerate_distance_queries();
  }

  // Default copy constructor and assignment operator are ok

  /// Destructor
  ~Polyhedral_tolerance_to_labeled_function_wrapper() {}

  /// Operator ()
  return_type operator()(const Point_3& p, bool use_cache=false) const
  {
    if ( sq_dist(p,use_cache) < sq_tolerance_size_ )
      return 1;
    else
      return 0;
  }

  Sphere_3 bounding_sphere() const
  {
    return details::bounding_sphere<BGT>(tree_->bbox());
  }

private:
  double sq_dist(const Point_3& p, bool use_cache) const
  {
    Point_3 proj;
    if ( use_cache )
      proj = tree_->closest_point(p,hint_);
    else
      proj = tree_->closest_point(p);

    // update cache
    hint_ = proj;

    // Return squared distance
    typename BGT::Compute_squared_distance_3 distance =
      BGT().compute_squared_distance_3_object();

    return (CGAL::to_double(distance(p,proj)));
  }

private:
  /// Functions to wrap
  AABB_tree* tree_;
  const double sq_tolerance_size_;
  mutable Point_3 hint_;

};  // end class Polyhedral_tolerance_to_labeled_function_wrapper



template<class Polyhedron_, class BGT>
class Polyhedral_edge_tolerance_to_labeled_function_wrapper
{
public:
  // AABB_tree Types
  typedef class AABB_const_polyhedron_edge_primitive<BGT, Polyhedron_>
                                                    AABB_primitive;
  typedef class AABB_traits<BGT,AABB_primitive>     AABB_traits;
  typedef class AABB_tree<AABB_traits>              AABB_tree;
  typedef typename AABB_traits::Bounding_box        Bounding_box;

  // Geometric traits types
  typedef typename BGT::Point_3     Point_3;
  typedef typename BGT::Sphere_3    Sphere_3;

  // Return type
  typedef int return_type;

  /// Constructor
  Polyhedral_edge_tolerance_to_labeled_function_wrapper(const Polyhedron_& p,
                                                        const double tolerance_size)
    : tree_(new AABB_tree(p.edges_begin(), p.edges_end()))
    , sq_tolerance_size_(tolerance_size*tolerance_size)
    , hint_(p.facets_begin()->halfedge()->vertex()->point())
  {
    tree_->accelerate_distance_queries();
  }

  // Default copy constructor and assignment operator are ok

  /// Destructor
  ~Polyhedral_edge_tolerance_to_labeled_function_wrapper() {}

  /// Operator ()
  return_type operator()(const Point_3& p, bool use_cache=false) const
  {
    if ( sq_dist(p,use_cache) < sq_tolerance_size_ )
      return 1;
    else
      return 0;
  }

  Sphere_3 bounding_sphere() const
  {
    return details::bounding_sphere<BGT>(tree_->bbox());
  }

private:
  double sq_dist(const Point_3& p, bool use_cache) const
  {
    Point_3 proj;
    if ( use_cache )
      proj = tree_->closest_point(p,hint_);
    else
      proj = tree_->closest_point(p);

    // update cache
    hint_ = proj;

    // Return squared distance
    typename BGT::Compute_squared_distance_3 distance =
      BGT().compute_squared_distance_3_object();

    return (CGAL::to_double(distance(p,proj)));
  }

private:
  /// Functions to wrap
  AABB_tree* tree_;
  const double sq_tolerance_size_;
  mutable Point_3 hint_;

};  // end class Polyhedral_edge_tolerance_to_labeled_function_wrapper

}  // end namespace Mesh_3

}  // end namespace CGAL

#endif // CGAL_MESH_3_POLYHEDRAL_TO_LABELED_FUNCTION_WRAPPER_H