EMODnet Quantized Mesh Generator for Cesium
cgal_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Coronis Computing S.L. (Spain)
2 // All rights reserved.
3 //
4 // This file is part of EMODnet Quantized Mesh Generator for Cesium.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 //
19 // Author: Ricard Campos (ricardcd@gmail.com)
20 
21 #ifndef EMODNET_QMGC_CGAL_UTILS_H
22 #define EMODNET_QMGC_CGAL_UTILS_H
23 
24 #include "tin_creation/tin_creation_cgal_types.h"
25 #include <fstream>
26 #include <CGAL/Polyhedron_incremental_builder_3.h>
27 #include <CGAL/Polyhedron_3.h>
28 #include <CGAL/Delaunay_triangulation_2.h>
29 #include <CGAL/Triangulation_2.h>
30 
40 template<class Polyhedron>
41 bool isTileCorner( typename Polyhedron::Halfedge_const_handle e ) {
42  typedef typename Polyhedron::Point_3 Point_3 ;
43 
44  if (e->is_border()) {
45  Point_3 p0 = e->vertex()->point() ; // The vertex the halfedge is incident to
46  Point_3 p1 = e->prev()->vertex()->point() ; // This is the previous vertex, with which p0 forms an edge
47 
48  // Differences between the points in the edge
49  double diffX = fabs( p1.x() - p0.x() ) ;
50  double diffY = fabs( p1.y() - p0.y() ) ;
51 
52  // Next edge on the border (since we are in a border halfedge, the next operator points to the next halfedge around the "hole"
53  Point_3 p2 = e->next()->vertex()->point() ;
54 
55  // Differences between the points in the next edge
56  double diffXNext = fabs( p2.x() - p0.x() ) ;
57  double diffYNext = fabs( p2.y() - p0.y() ) ;
58 
59  // Compare slopes to see if e is incident to a corner vertex (i.e., if p0 is a corner vertex)
60  return ( ( diffX < diffY ) && ( diffXNext > diffYNext ) ) ||
61  ( ( diffX > diffY ) && ( diffXNext < diffYNext ) ) ;
62  }
63  else {
64  return false ;
65  }
66 }
67 
68 
69 
71 template <class Delaunay>
72 void delaunayToOFF( const std::string &outFilePath, const Delaunay &dt )
73 {
74  std::ofstream ofs( outFilePath.c_str() );
75  ofs << "OFF\n" << dt.number_of_vertices()
76  << " " << dt.number_of_faces() << " 0" << std::endl;
77 
78  std::map<typename Delaunay::Vertex_handle,int> indices;
79  int counter = 0;
80 
81  for(typename Delaunay::Finite_vertices_iterator it = dt.finite_vertices_begin(); it != dt.finite_vertices_end(); ++it)
82  {
83  ofs << it->point() << std::endl;
84  indices.insert(std::pair<typename Delaunay::Vertex_handle,int>(it, counter++));
85  }
86 
87  for(typename Delaunay::Finite_faces_iterator it = dt.finite_faces_begin(); it != dt.finite_faces_end(); ++it)
88  {
89  ofs << "3 " << indices[it->vertex(0)]
90  << " " << indices[it->vertex(1)]
91  << " " << indices[it->vertex(2)] << std::endl;
92  }
93 
94  ofs.close() ;
95 }
96 
97 
98 
100 template <class Polyhedron>
101 bool isBorder(typename Polyhedron::Vertex_handle& v)
102 {
103  typename Polyhedron::Halfedge_around_vertex_const_circulator hv = v->vertex_begin();
104  //move around the vertex and check if there is a halfedge which
105  //is on border
106  do {
107  if(hv->is_border())
108  return true;
109  }while (++hv != v->vertex_begin());
110  return false;
111 }
112 
113 
115 template <class Polyhedron>
116 bool isBorder(typename Polyhedron::Vertex_const_handle& v)
117 {
118  typename Polyhedron::Halfedge_around_vertex_const_circulator hv = v->vertex_begin();
119  //move around the vertex and check if there is a halfedge which
120  //is on border
121  do {
122  if(hv->is_border())
123  return true;
124  }while (++hv != v->vertex_begin());
125  return false;
126 }
127 
128 
129 
131 template <class K>
132 bool isPointInArc( const typename K::Point_2& query,
133  const typename K::Point_2& center,
134  const typename K::Point_2& p0,
135  const typename K::Point_2& p1 )
136 {
137  typedef typename K::Vector_2 Vector_2;
138 
139  Vector_2 a = p0 - center ;
140  Vector_2 b = query - center ;
141  Vector_2 c = p1 - center ;
142 
143  double AxB = a.x()*b.y() - a.y()*b.x() ;
144  double AxC = a.x()*c.y() - a.y()*c.x() ;
145  double CxB = c.x()*b.y() - c.y()*b.x() ;
146  double CxA = c.x()*a.y() - c.y()*a.x() ;
147 
148  return ( AxB*AxC >= 0. && CxB*CxA >= 0. ) ;
149 }
150 
151 #endif //EMODNET_QMGC_CGAL_UTILS_H
bool isPointInArc(const typename K::Point_2 &query, const typename K::Point_2 &center, const typename K::Point_2 &p0, const typename K::Point_2 &p1)
Computes if a point query falls within the arc defined by the vectors p0 - center and p1 - center...
Definition: cgal_utils.h:132
void delaunayToOFF(const std::string &outFilePath, const Delaunay &dt)
Exports a 2D Delaunay on a 3D point set (i.e., using the Projection_traits_xy_3) to an OFF file...
Definition: cgal_utils.h:72
bool isBorder(typename Polyhedron::Vertex_handle &v)
Checks if a vertex in the polyhedron is in the border.
Definition: cgal_utils.h:101
bool isTileCorner(typename Polyhedron::Halfedge_const_handle e)
Definition: cgal_utils.h:41