EMODnet Quantized Mesh Generator for Cesium
point_set_features_simplification_cost.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_POINT_SET_FEATURES_SIMPLIFICATION_COST_H
22 #define EMODNET_QMGC_POINT_SET_FEATURES_SIMPLIFICATION_COST_H
23 
24 #include <CGAL/algorithm.h>
25 #include <CGAL/intersections.h>
26 
27 namespace CGAL {
28 
29 template < class Tr >
31 
32 namespace Polyline_simplification_2
33 {
34 
44 {
45 
46 public:
47 
49  PointSetFeaturesSimplificationCost(const double& maxLength) : m_maxSqLength(maxLength*maxLength) {}
50 
56  template<class CDT>
57  boost::optional<typename CDT::Geom_traits::FT>
60  {
61  // Typedefs for projected calculations
62  typedef typename Constrained_triangulation_plus_2<CDT>::Points_in_constraint_iterator Points_in_constraint_iterator;
63  typedef typename Constrained_triangulation_plus_2<CDT>::Geom_traits Geom_traits ;
64  typedef typename Geom_traits::FT FT;
65  typedef typename Geom_traits::Compute_squared_distance_3 Compute_squared_distance;
66  typedef typename Geom_traits::Construct_segment_3 Construct_segment;
67  typedef typename Geom_traits::Segment_3 Segment;
68  typedef typename Geom_traits::Point_3 Point;
69 
70  // Typedefs for unprojected calculations
71  typedef typename Geom_traits::Rp::Vector_3 Vector_3;
72  typedef typename Geom_traits::Rp::Plane_3 Plane_3;
73  typedef typename Geom_traits::Rp::Line_3 Line_3;
74  typedef typename Geom_traits::Rp::Segment_3 Segment_3;
75  typedef typename Geom_traits::Rp::Intersect_3 Intersect_3;
76 
77  Compute_squared_distance compute_squared_distance = pct.geom_traits().compute_squared_distance_3_object();
78  Construct_segment construct_segment = pct.geom_traits().construct_segment_3_object();
79  typedef typename Constrained_triangulation_plus_2<CDT>::Vertices_in_constraint_iterator Vertices_in_constraint_iterator;
80 
81  // Get the previous and next vertices in the current simplified polyline
82  Vertices_in_constraint_iterator vicp = boost::prior(vicq);
83  Vertices_in_constraint_iterator vicr = boost::next(vicq);
84 
85  Point const& lP = (*vicp)->point();
86  Point const& lR = (*vicr)->point();
87  Point const& Q = (*vicq)->point();
88 
89  // Check if the segments (P,Q) or (Q,R) are too large when projected to the XY plane
90  Point const& lP2 = Point(lP.x(), lP.y(), FT(0.0));
91  Point const& lR2 = Point(lR.x(), lR.y(), FT(0.0));
92  Point const& Q2 = Point(Q.x(), Q.y(), FT(0.0));
93  if ( compute_squared_distance(lP2, Q2) > m_maxSqLength || compute_squared_distance(lR2, Q2) > m_maxSqLength ) {
94  return std::numeric_limits<double>::infinity();
95  }
96 
97  // Otherwise, check the cost
98  Segment lP_R = construct_segment(lP, lR) ;
99 
100  // Using 3D distance
101  // FT d1 = 0.0;
102  // Points_in_constraint_iterator pp(vicp), rr(vicr);
103  // ++pp;
104  //
105  // for ( ;pp != rr; ++pp )
106  // d1 = (std::max)(d1, compute_squared_distance( lP_R, *pp ) ) ;
107 
108  // Using distance in height
109  Vector_3 cpLineAndZAxis = CGAL::cross_product<typename Geom_traits::Rp>(lP_R.to_vector(), Vector_3(0,0,1));
110  Vector_3 planeNml = CGAL::cross_product<typename Geom_traits::Rp>(lP_R.to_vector(), cpLineAndZAxis);
111  Plane_3 lineAsAPlane = Plane_3(lP, planeNml);
112 
113  FT d1 = 0.0;
114  Points_in_constraint_iterator pp(vicp), rr(vicr);
115  ++pp;
116  for ( ;pp != rr; ++pp ) {
117  // Construct a line in the Z direction
118  Line_3 l(*pp, Vector_3(0, 0, 1));
119 
120  // Compute their intersection
121  typedef typename CGAL::cpp11::result_of<typename Geom_traits::Rp::Intersect_3(Line_3, Plane_3)>::type IntersectionResult;
122  IntersectionResult intersect = CGAL::intersection(l, lineAsAPlane);
123 
124  // If everything goes as expected, the intersection should exist and should be a point
125  if (!intersect) {
126  std::cerr << "Error! Empty intersection" << std::endl;
127  return std::numeric_limits<double>::infinity();
128  }
129  if (const Line_3 *s = boost::get<Line_3>(&*intersect)) {
130  std::cerr << "Error! Line intersection" << std::endl;
131  return std::numeric_limits<double>::infinity();
132  }
133 
134  // Get the intersection point
135  const Point *ip = boost::get<Point>(&*intersect);
136 
137  // Finally, compute the squared distance between the query point and the intersection
138  FT sqDist = CGAL::squared_distance<typename Geom_traits::Rp>(*pp, *ip);
139  d1 = (std::max)(d1, sqDist) ;
140  }
141 
142  return d1 ;
143  }
144 
145 private:
146  // --- Attributes ---
147  double m_maxSqLength;
148 
149 };
150 
151 } // namespace Polyline_simplification_2
152 
153 } //namespace CGAL
154 
155 #endif //EMODNET_QMGC_POINT_SET_FEATURES_SIMPLIFICATION_COST_H
Definition: point_set_features_simplification_cost.h:30
Extension of CGAL&#39;s Constrained_placement class.
Definition: further_constrained_placement.h:38
Polyline simplification cost function used by point set simplification methods.
Definition: point_set_features_simplification_cost.h:43
PointSetFeaturesSimplificationCost(const double &maxLength)
Initializes the cost function.
Definition: point_set_features_simplification_cost.h:49
boost::optional< typename CDT::Geom_traits::FT > operator()(const Constrained_triangulation_plus_2< CDT > &pct, typename Constrained_triangulation_plus_2< CDT >::Vertices_in_constraint_iterator vicq) const
Definition: point_set_features_simplification_cost.h:58