Goosefoot Mesher - CGAL
mesher_cgal.h
1 
20 #ifndef MESHER_CGAL_H
21 #define MESHER_CGAL_H
22 
23 #include "cgalsettings.pb.h"
24 
25 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
26 #include <CGAL/Mesh_triangulation_3.h>
27 #include <CGAL/Mesh_complex_3_in_triangulation_3.h>
28 #include <CGAL/Mesh_criteria_3.h>
29 
30 #include <CGAL/Nef_polyhedron_3.h>
31 #include <CGAL/IO/Nef_polyhedron_iostream_3.h>
32 
33 #include <CGAL/Polyhedral_mesh_domain_3.h>
34 #include <CGAL/make_mesh_3.h>
35 #include <CGAL/refine_mesh_3.h>
36 
37 // IO
38 #include <CGAL/IO/Polyhedron_iostream.h>
39 
40 // EPECK kernel
41 #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
42 
43 // surface mesh
44 #include <CGAL/Polyhedron_3.h>
45 
46 // nef
47 #include <CGAL/Nef_polyhedron_3.h>
48 #include <CGAL/Nef_3/SNC_indexed_items.h>
49 
50 #include <CGAL/bounding_box.h>
51 
52 // tree
53 #include <CGAL/AABB_tree.h>
54 #include <CGAL/AABB_traits.h>
55 #include <CGAL/AABB_face_graph_triangle_primitive.h>
56 
57 #include <CGAL/Side_of_triangle_mesh.h>
58 
59 #include <vtkSmartPointer.h>
60 #include <vtkUnstructuredGrid.h>
61 #include <vtkPolyData.h>
62 #include <vtkSelectEnclosedPoints.h>
63 
64 // Domain
65 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
66 typedef CGAL::Polyhedron_3<K> Polyhedron;
67 typedef CGAL::Polyhedral_mesh_domain_3<Polyhedron, K> Mesh_domain;
68 
69 typedef CGAL::Exact_predicates_exact_constructions_kernel Exact_Kernel;
70 typedef CGAL::Polyhedron_3<Exact_Kernel> Exact_polyhedron;
71 typedef CGAL::Cartesian_converter<Exact_Kernel,K> EK_to_K;
72 typedef CGAL::Cartesian_converter<K,Exact_Kernel> K_to_EK;
73 typedef CGAL::Polyhedral_mesh_domain_3<Exact_polyhedron, Exact_Kernel> Exact_Mesh_domain;
74 typedef CGAL::Nef_polyhedron_3<Exact_Kernel,
75  CGAL::SNC_indexed_items,
76  bool> Nef_polyhedron;
77 
78 typedef K::Point_3 Point;
79 typedef Exact_Kernel::Point_3 Exact_Point;
80 typedef K::FT FT;
81 typedef FT (Function)(const Point&);
82 
83 typedef Exact_polyhedron::HalfedgeDS Exact_HalfedgeDS;
84 
85 // Triangulation
86 typedef CGAL::Mesh_triangulation_3<Mesh_domain>::type Tr;
87 typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
88 
89 typedef CGAL::AABB_face_graph_triangle_primitive<Polyhedron> Primitive;
90 typedef CGAL::AABB_traits<K, Primitive> Traits;
91 typedef CGAL::AABB_tree<Traits> Tree;
92 
93 typedef CGAL::Side_of_triangle_mesh<Polyhedron,K> Side_of_triangle_mesh;
94 
95 namespace mesherCGAL {
96 
97  //int visualization_set_allocation_order(int ix, int order, double cl, double needle_dist, double x, double y, double z);
98  //int visualization_create_structured_grid(double x0, double y0, double z0, int nx, int ny, int nz, double dx);
99  //int visualization_save(std::string& filename);
100  typedef std::map< int, std::shared_ptr<Polyhedron> > region_ip_map;
101  typedef std::map< int, std::shared_ptr<Exact_polyhedron> > region_ep_map;
102 
103  int simplify(Polyhedron& boundary);
104 
106  K::Point_3* centre;
107  float r;
108  int i;
109  };
110 
111  class Zone {
112  private:
113  /* If this ever ends up in a scenario where zone is destroyed, switch to unique_ptr and
114  * check performance impact */
116 
117  protected:
118  int _id;
119  float _cl;
120  float _priority;
121 
122  public:
123  Zone(int id, float cl, float priority) :
124  activity_sphere(NULL), _id(id), _cl(cl), _priority(priority) {}
125  virtual ~Zone() {}
126  bool set_activity_sphere(float x, float y, float z, float r, int i);
127  void print_activity_sphere();
128  int get_id() const { return _id; }
129  float get_priority() const { return _priority; }
130  float get_cl() const { return _cl; }
131  virtual bool is_container() const = 0;
132  virtual std::shared_ptr<Exact_polyhedron> exact_polyhedron() = 0;
133 
134  int contains(const K::Point_3& p, bool check_activity_sphere=true) const {
135  if (!is_container())
136  return 0;
137 
138  int id = contains_all(p) ? _id : 0;
139 
140  if (id && check_activity_sphere) {
141  int inactivity_index;
142  if ((inactivity_index = outside_activity_sphere(p)) != 0)
143  return inactivity_index;
144  }
145 
146  return id;
147  }
148  virtual bool has_tree() const = 0;
149  virtual float squared_distance(const K::Point_3& p) const = 0;
150  virtual bool add_tree() = 0;
151  virtual bool load(std::string filename) = 0;
152  virtual bool is_valid() = 0;
153 
154  protected:
155  /* Ignores inactivity */
156  virtual bool contains_all(const K::Point_3& p) const = 0;
157  int outside_activity_sphere(const K::Point_3& p) const {
158  if (activity_sphere) {
159  Point* centre = activity_sphere->centre;
160  float radius = activity_sphere->r;
161  if (CGAL::squared_distance(*centre, p) > radius * radius)
162  {
163  return -activity_sphere->i;
164  }
165  }
166 
167  return 0;
168  }
169  };
170 
171  class PolyhedralZone : public Zone {
172  Tree* _tree;
173  std::shared_ptr<Side_of_triangle_mesh> _pip;
174  std::shared_ptr<Polyhedron> _ip;
175  std::shared_ptr<Exact_polyhedron> _ep;
176 
177  public:
178  PolyhedralZone(int id, float cl, float priority) : Zone(id, cl, priority),
179  _tree(NULL), _ip(NULL), _ep(NULL) {}
180  bool is_container() const { return (bool)_pip; }
181  std::shared_ptr<Exact_polyhedron> exact_polyhedron() { return _ep; }
182 
183  bool has_tree() const { return _tree; }
184  float squared_distance(const K::Point_3& p) const {
185  if (!has_tree())
186  return -1.0;
187  return _tree->squared_distance(p);
188  }
189  bool add_tree() {
190  if (!_ip || has_tree())
191  return false;
192 
193  _tree = new Tree();
194  _tree->insert(_ip->facets_begin(), _ip->facets_end(), *_ip);
195 
196  return true;
197  }
198  bool is_valid() { return _ep && _ep->is_valid(); }
199  bool load(std::string filename);
200 
201  protected:
202  bool contains_all(const K::Point_3& p) const {
203  return (*_pip)(p) != CGAL::ON_UNBOUNDED_SIDE;
204  }
205 
206  private:
207  bool create_polyhedra(std::shared_ptr<Exact_polyhedron> ep);
208  };
209 
210  class VtkUnstructuredGridZone : public Zone {
211  vtkSmartPointer<vtkUnstructuredGrid> _grid;
212 
213  public:
214  VtkUnstructuredGridZone(int id, float cl, float priority) : Zone(id, cl, priority) {}
215  bool is_container() const { return true; };
216  std::shared_ptr<Exact_polyhedron> exact_polyhedron() { return NULL; };
217 
218  bool has_tree() const { return false; };
219  float squared_distance(const K::Point_3&) const { return 0; };
220  bool add_tree() { return false; };
221  bool is_valid() { return true; };
222  bool load(std::string filename);
223 
224  protected:
225  bool contains_all(const K::Point_3& p) const;
226  };
227 
228  class VtkPolyhedronZone : public Zone {
229  vtkSmartPointer<vtkPolyData> _poly;
230  vtkSmartPointer<vtkSelectEnclosedPoints> _select_enclosed;
231 
232  public:
233  VtkPolyhedronZone(int id, float cl, float priority) : Zone(id, cl, priority) {}
234  bool is_container() const { return true; };
235  std::shared_ptr<Exact_polyhedron> exact_polyhedron() { return NULL; };
236 
237  bool has_tree() const { return false; };
238  float squared_distance(const K::Point_3&) const { return 0; };
239  bool add_tree() { return false; };
240  bool load(std::string filename);
241  bool is_valid() { return true; };
242 
243  protected:
244  bool contains_all(const K::Point_3& p) const;
245  };
246 }
247 
248 #endif
Definition: mesher_cgal.h:105
Definition: mesher_cgal.h:210
Definition: implicit_zone_function.h:17
Definition: mesher_cgal.h:228
Definition: mesher_cgal.h:111
Definition: mesher_cgal.h:171