EMODnet Quantized Mesh Generator for Cesium
tin_creation_greedy_insertion_strategy.h
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_TIN_CREATION_GREEDY_INSERTION_H
22 #define EMODNET_QMGC_TIN_CREATION_GREEDY_INSERTION_H
23 
24 #include "tin_creator.h"
25 #include "tin_creation_cgal_types.h"
26 #include <CGAL/Triangulation_face_base_2.h>
27 #include <CGAL/Triangulation_face_base_with_info_2.h>
28 #include <memory>
29 #include <boost/heap/fibonacci_heap.hpp>
30 #include "tin_creation_utils.h"
31 
32 namespace TinCreation {
33 
38 struct GIHeapNode {
39  FT error ;
40  Point_3 candidate ;
41 
42  GIHeapNode() : error(0.0), candidate(Point_3(.0,.0,.0)) {}
43  GIHeapNode( const FT& e, const Point_3& c ) : error(e), candidate(c) {}
44 };
45 
51 {
52  bool operator()(const GIHeapNode& n1, const GIHeapNode& n2) const
53  {
54  return n1.error < n2.error;
55  }
56 };
57 
58 typedef boost::heap::fibonacci_heap<GIHeapNode, boost::heap::compare<CompareGIHeapNodes>> GIHeap ;
59 typedef typename GIHeap::handle_type GIHeapNodeHandle ;
60 
65 class GIFaceInfo {
66 public:
67  GIFaceInfo() : m_pointsInFacePtrs(), m_heapNodeHandle(), m_hasHeapNodeHandle(false) {}
68  ~GIFaceInfo() {}
69 
70  void setHeapNodeHandle( GIHeapNodeHandle h ) { m_heapNodeHandle = h; m_hasHeapNodeHandle = true ; }
71  void addPointPtr(std::shared_ptr<Point_3> pp) {m_pointsInFacePtrs.push_back(pp);}
72  std::vector<std::shared_ptr<Point_3>> getPtsSharedPtrs() { return m_pointsInFacePtrs; } ;
73  size_t getNumPtsInFace() { return m_pointsInFacePtrs.size(); }
74  bool hasHeapNodeHandle() {return m_hasHeapNodeHandle; }
75  GIHeapNodeHandle getHeapNodeHandle() { return m_heapNodeHandle ; }
76 
77  // Some face handles may not be erased after insertion, apply this to all the faces on the conflict zone prior to insert a point to ensure that all the faces' info are empty
78  void clearInfo() {
79  m_pointsInFacePtrs.clear();
80  m_heapNodeHandle = GIHeapNodeHandle();
81  m_hasHeapNodeHandle = false;
82  };
83 private:
84  std::vector<std::shared_ptr<Point_3>> m_pointsInFacePtrs;
85  GIHeapNodeHandle m_heapNodeHandle ;
86  bool m_hasHeapNodeHandle ;
87 };
88 
98 {
99 public:
100  // --- Typedefs ---
101  typedef CGAL::Triangulation_vertex_base_2<Gt> Vb;
102  typedef CGAL::Triangulation_face_base_with_info_2<GIFaceInfo, Gt> Fb;
103  typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
104  typedef CGAL::Delaunay_triangulation_2<Gt, Tds> DT;
105  typedef DT::Face_handle FaceHandle;
106  typedef DT::Vertex_handle VertexHandle;
107  typedef DT::Face_circulator FaceCirculator;
108  typedef Gt::Rp::Triangle_3 Triangle_3;
109  typedef Gt::Rp::Vector_3 Vector_3;
110  typedef Gt::Rp::Segment_3 Segment_3;
111  typedef Gt::Rp::Line_3 Line_3;
112  typedef Gt::FT FT;
113  typedef Gt::Rp::Intersect_3 Intersect_3;
114 
115 public:
116  enum ErrorType { ErrorHeight=0, Error3D } ;
117 
118  // --- Public Methods ---
119 
126  TinCreationGreedyInsertionStrategy(double rootApproxTolerance,
127  int initGridSamples = -1,
128  int errorType = ErrorHeight)
129  : m_initGridSamples(initGridSamples)
130  , m_errorType(errorType)
131  {
132  m_approxTolPerZoom = std::vector<FT>{rootApproxTolerance};
133  setParamsForZoom(0);
134  }
135 
142  TinCreationGreedyInsertionStrategy(const std::vector<FT>& approxTolPerZoom,
143  int initGridSamples = -1,
144  int errorType = ErrorHeight)
145  : m_initGridSamples(initGridSamples)
146  , m_errorType(errorType)
147  , m_approxTolPerZoom(approxTolPerZoom)
148  {
149  setParamsForZoom(0);
150  }
151 
152  void setParamsForZoom(const unsigned int& zoom)
153  {
154  m_approxTol = standardHandlingOfThresholdPerZoom(m_approxTolPerZoom, zoom);
155  }
156 
157  Polyhedron create(const std::vector<Point_3>& dataPts,
158  const bool& constrainEasternVertices,
159  const bool& constrainWesternVertices,
160  const bool& constrainNorthernVertices,
161  const bool& constrainSouthernVertices);
162 private:
163  // --- Attributes ---
164  FT m_approxTol ;
165  FT m_scaledSqApproxTol ;
166  std::vector<Point_3> m_dataPts ;
167  DT m_dt ;
168  GIHeap m_heap;
169  int m_errorType;
170  int m_initGridSamples;
171  std::vector<FT> m_approxTolPerZoom; // in metric, not squared!
172 
173  // --- Private Methods ---
175  void initialize(const bool& constrainEasternVertices,
176  const bool& constrainWesternVertices,
177  const bool& constrainNorthernVertices,
178  const bool& constrainSouthernVertices);
179 
182  FT error(const Point_3& p, const Triangle_3&t) const;
183 
186  FT errorHeight(const Point_3& p, const Triangle_3&t) const;
187 
190  FT error3D(const Point_3& p, const Triangle_3&t) const;
191 
194  FT eval(const Point_3& p, const Triangle_3&t) const;
195 
197  void insert(const Point_3& p);
198 
204  void computeErrorAndUpdateHeap(FaceHandle fh);
205 };
206 
207 } // End namespace TinCreation
208 
209 #endif //EMODNET_QMGC_TIN_CREATION_GREEDY_INSERTION_H
T standardHandlingOfThresholdPerZoom(const std::vector< T > &thresholdsPerZoom, const unsigned int &zoom, const bool &downScale=true)
Definition: tin_creation_utils.h:43
This namespace contains all the types/classes/functions required to create a TIN out of a regularly g...
Comparison operator (less than) for GIHeapNodes.
Definition: tin_creation_greedy_insertion_strategy.h:50
Creates a TIN using the Greedy Insertion method.
Definition: tin_creation_greedy_insertion_strategy.h:97
TinCreationGreedyInsertionStrategy(const std::vector< FT > &approxTolPerZoom, int initGridSamples=-1, int errorType=ErrorHeight)
Definition: tin_creation_greedy_insertion_strategy.h:142
TinCreationGreedyInsertionStrategy(double rootApproxTolerance, int initGridSamples=-1, int errorType=ErrorHeight)
Types of errors to use.
Definition: tin_creation_greedy_insertion_strategy.h:126
The information to be maintained in the heap structure.
Definition: tin_creation_greedy_insertion_strategy.h:38
Defines the interphase of a TIN creation algorithm.
Definition: tin_creator.h:37
Additional information associated to a face in a Delaunay triangulation required by the Greedy Insert...
Definition: tin_creation_greedy_insertion_strategy.h:65
void setParamsForZoom(const unsigned int &zoom)
Adapts the parameters of the algorithm for the desired zoom level.
Definition: tin_creation_greedy_insertion_strategy.h:152