1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "four_field_index.h" 21#include "index_computations.h" 28const Bbox_Double Bbox_Double::invalid(100.0, 200.0, 100.0, 200.0); 31double Bbox_Double::center_lat() const 33701723 q return (south + north) / 2; 37double Bbox_Double::center_lon() const 402859623 q return (west + east) / 2; 410 else if ((west + east) / 2 <= 0) 420 return (west + east) / 2 + 180.0; 440 return (west + east) / 2 - 180.0; 48bool Bbox_Double::contains(const Point_Double& point) const 5029228 q if (point.lat + 5e-8 < south || point.lat - 5e-8 > north) 5416826 q return point.lon + 5e-8 >= west && point.lon - 5e-8 <= east; 560 return point.lon + 5e-8 >= west || point.lon - 5e-8 <= east; 59bool Bbox_Double::intersects(const Point_Double& from, const Point_Double& to) const 614672105135 q double from_lon = from.lon; 624672105135 q double to_lon = to.lon; 634672105135 q if (from.lon < 0. && to.lon - from.lon > 180.) 654672105135 q if (to.lon < 0. && from.lon - to.lon > 180.) 684672105135 q double delta_from = 0; 694672105135 q if (from.lat + 5e-8 < south) 712415859129 q if (to.lat + 5e-8 < south) 733449125 q if (to.lat - 5e-8 > from.lat) 743449125 q delta_from = (to_lon - from_lon)*(south - from.lat)/(to.lat - from.lat); 75 // Asserted: (south, from_lon + delta_from) is a point on the segment 772256246132 q else if (from.lat - 5e-8 > north) 791317511130 q if (to.lat - 5e-8 > north) 813528126 q if (to.lat + 5e-8 < from.lat) 823528126 q delta_from = (to_lon - from_lon)*(north - from.lat)/(to.lat - from.lat); 83 // Asserted: (north, from_lon + delta_from) is a point on the segment 86945712129 q if (to.lat + 5e-8 < south && from.lat - 5e-8 > to.lat) 87 // Adjust to.lat and to.lon 882134121 q to_lon += (from_lon - to_lon)*(south - to.lat)/(from.lat - to.lat); 89 // Asserted: (south, to_lon) is a point on the segment 90943578129 q else if (to.lat - 5e-8 > north && from.lat + 5e-8 < to.lat) 91 // Adjust to.lat and to.lon 922484122 q to_lon += (from_lon - to_lon)*(north - to.lat)/(from.lat - to.lat); 93 // Asserted: (north, to_lon) is a point on the segment 94945712129 q from_lon += delta_from; 96 // Asserted: (std::min(north, std::max(south, from.lat)), from_lon) is on the segment 97 // Asserted: (std::min(north, std::max(south, to.lat)), to_lon) is on the segment 99 // Now we know that both latitudes are between south and north. 100 // Thus we only need to check whether the segment touches the bbox in its east-west-extent. 101 // Note that the lons have now values between -180.0 and 360.0. 102945712129 q double min_lon = std::min(from_lon, to_lon); 103945712129 q double max_lon = std::max(from_lon, to_lon); 107945712129 q return min_lon - 5e-8 <= east && max_lon + 5e-8 >= west; 1080 else if (min_lon > 180.) 1090 return min_lon - 5e-8 - 360. <= east && max_lon + 5e-8 - 360. >= west; 1110 return min_lon - 5e-8 <= east && max_lon + 5e-8 - 360. >= west; 1150 return min_lon - 5e-8 <= east || max_lon + 5e-8 >= west; 1160 else if (min_lon > 180.) 1170 return min_lon - 5e-8 - 360. <= east || max_lon + 5e-8 - 360. >= west; 123bool Point_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 1250 return bbox.contains(pt); 129Bbox_Double* calc_bounds(const std::vector< Point_Double >& points) 13132000438 q double south = 100.0; 13332000438 q double north = -100.0; 13432000438 q double east = -200.0; 136933177238 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 140901170338 q south = std::min(south, it->lat); 141901170338 q west = std::min(west, it->lon); 142901170338 q north = std::max(north, it->lat); 143901170338 q east = std::max(east, it->lon); 1480 return new Bbox_Double(Bbox_Double::invalid); 14932000438 q else if (east - west > 180.0) 150 // In this special case we should check whether the bounding box should rather cross the date line 1520 double wrapped_west = 180.0; 1530 double wrapped_east = -180.0; 1550 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 1600 wrapped_west = std::min(wrapped_west, it->lon); 1620 wrapped_east = std::max(wrapped_east, it->lon); 1660 if (wrapped_west - wrapped_east > 180.0) 1670 return new Bbox_Double(south, wrapped_west, north, wrapped_east); 169 // The points go around the world, hence a bounding box limit doesn't make sense. 1700 return new Bbox_Double(south, -180.0, north, 180.0); 17332000438 q return new Bbox_Double(south, west, north, east); 177double Linestring_Geometry::center_lat() const 180105810 q bounds = calc_bounds(points); 182114810 q return bounds->center_lat(); 186double Linestring_Geometry::center_lon() const 18912336 q bounds = calc_bounds(points); 191196510 q return bounds->center_lon(); 195double Linestring_Geometry::south() const 19829957127 q bounds = calc_bounds(points); 20030042028 q return bounds->south; 204double Linestring_Geometry::north() const 2070 bounds = calc_bounds(points); 20930013728 q return bounds->north; 213double Linestring_Geometry::west() const 2162831 q bounds = calc_bounds(points); 222double Linestring_Geometry::east() const 2250 bounds = calc_bounds(points); 231bool Linestring_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 2330 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 2350 if (bbox.contains(*it)) 2390 for (uint i = 1; i < points.size(); ++i) 2410 if (bbox.intersects(points[i-1], points[i])) 249double Partial_Way_Geometry::center_lat() const 2550 bounds = calc_bounds(points); 2570 return bounds->center_lat(); 261double Partial_Way_Geometry::center_lon() const 2670 bounds = calc_bounds(points); 2690 return bounds->center_lon(); 273double Partial_Way_Geometry::south() const 2791785913 q bounds = calc_bounds(points); 285double Partial_Way_Geometry::north() const 2910 bounds = calc_bounds(points); 297double Partial_Way_Geometry::west() const 3030 bounds = calc_bounds(points); 309double Partial_Way_Geometry::east() const 3150 bounds = calc_bounds(points); 321Partial_Way_Geometry::Partial_Way_Geometry(const std::vector< Point_Double >& points_) 32220164 q : points(points_), bounds(0), has_coords(false) 32440324 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end() && !has_coords; ++it) 32520164 q has_coords |= (it->lat < 100.); 32919984 q valid_segments.push_back(std::vector< Point_Double >()); 33019984 q if (!points.empty() && points.front().lat < 100.) 33119984 q valid_segments.back().push_back(points.front()); 332381384 q for (unsigned int i = 1; i < points.size(); ++i) 334361404 q if (points[i].lat < 100.) 336361404 q if (points[i-1].lat >= 100.) 3370 valid_segments.push_back(std::vector< Point_Double >()); 338361404 q valid_segments.back().push_back(points[i]); 344void Partial_Way_Geometry::add_point(const Point_Double& point) 34868371121 q if (point.lat < 100.) 35062938717 q if (points.empty() || (points.back().lat >= 100.)) 3512907617 q valid_segments.push_back(std::vector< Point_Double >()); 35262938717 q valid_segments.back().push_back(point); 35568371121 q points.push_back(point); 359bool Partial_Way_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 3610 for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 3630 if (it->lat < 100. && bbox.contains(*it)) 3670 for (uint i = 1; i < points.size(); ++i) 3690 if (points[i-1].lat < 100. && points[i].lat < 100. && bbox.intersects(points[i-1], points[i])) 377Bbox_Double* calc_bounds(const std::vector< std::vector< Point_Double > >& linestrings) 37946083 q double south = 100.0; 38046083 q double west = 200.0; 38146083 q double north = -100.0; 38246083 q double east = -200.0; 38446083 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 38592163 q iti != linestrings.end(); ++iti) 387604783 q for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 389558703 q if (it->lat < 100.) 391558703 q south = std::min(south, it->lat); 392558703 q west = std::min(west, it->lon); 393558703 q north = std::max(north, it->lat); 394558703 q east = std::max(east, it->lon); 39946083 q if (north == -100.0) 4000 return new Bbox_Double(Bbox_Double::invalid); 40146083 q else if (east - west > 180.0) 402 // In this special case we should check whether the bounding box should rather cross the date line 4040 double wrapped_west = 180.0; 4050 double wrapped_east = -180.0; 4070 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 4080 iti != linestrings.end(); ++iti) 4100 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 4150 wrapped_west = std::min(wrapped_west, it->lon); 4170 wrapped_east = std::max(wrapped_east, it->lon); 4220 if (wrapped_west - wrapped_east > 180.0) 4230 return new Bbox_Double(south, wrapped_west, north, wrapped_east); 425 // The points go around the world, hence a bounding box limit doesn't make sense. 4260 return new Bbox_Double(south, -180.0, north, 180.0); 42946083 q return new Bbox_Double(south, west, north, east); 43346227 qFree_Polygon_Geometry::Free_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_) : linestrings(linestrings_), bounds(0) 43592447 q for (std::vector< std::vector< Point_Double > >::iterator it = linestrings.begin(); it != linestrings.end(); 43846227 q if (it->front() != it->back()) 43946227 q it->push_back(it->front()); 444double Free_Polygon_Geometry::center_lat() const 4470 bounds = calc_bounds(linestrings); 4490 return bounds->center_lat(); 453double Free_Polygon_Geometry::center_lon() const 4560 bounds = calc_bounds(linestrings); 4580 return bounds->center_lon(); 462double Free_Polygon_Geometry::south() const 4650 bounds = calc_bounds(linestrings); 4670 return bounds->south; 471double Free_Polygon_Geometry::north() const 4740 bounds = calc_bounds(linestrings); 4760 return bounds->north; 480double Free_Polygon_Geometry::west() const 4830 bounds = calc_bounds(linestrings); 4850 return bounds->west; 489double Free_Polygon_Geometry::east() const 4920 bounds = calc_bounds(linestrings); 4940 return bounds->east; 498void Free_Polygon_Geometry::add_linestring(const std::vector< Point_Double >& linestring) 500181904 q if (linestring.size() < 2) 505181904 q linestrings.push_back(linestring); 506181904 q if (linestrings.back().front() != linestrings.back().back()) 507181852 q linestrings.back().push_back(linestrings.back().front()); 511void toggle_if_inside(bool& is_inside, bool& on_vertex, bool& on_segment, double border_lat, double border_lon, 512 Point_Double pt, Point_Double lhs, Point_Double rhs) 514839899 q if (pt.lon < -179.9) 5160 lhs.lon -= lhs.lon > 0 ? 360. : 0.; 5170 rhs.lon -= rhs.lon > 0 ? 360. : 0.; 519839899 q else if (pt.lon > 179.9) 5210 lhs.lon += lhs.lon < 0 ? 360. : 0.; 5220 rhs.lon += rhs.lon < 0 ? 360. : 0.; 525839899 q if (lhs.lat == rhs.lat) 527176253 q if (lhs.lat < pt.lat) 528101 q is_inside ^= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0); 529176153 q else if (lhs.lat == pt.lat) 531176013 q if (pt.lon == lhs.lon || pt.lon == rhs.lon) 5340 on_segment |= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0); 536 // no else required -- such a segment does not play a role 538663649 q else if (lhs.lon == rhs.lon) 540176133 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 542176102 q else if (pt.lon == lhs.lon && (pt.lat - lhs.lat)*(pt.lat - rhs.lat) < 0) 544176102 q else if (lhs.lon < pt.lon && (lhs.lat < border_lat || rhs.lat < border_lat)) 5450 is_inside = !is_inside; 546 // no else required -- such a segment does not play a role 548487519 q else if ((lhs.lat - rhs.lat)*(lhs.lon - rhs.lon) < 0) 549 // The segment runs from nw to se or se to nw 551163367 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 553110347 q else if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0) 55517023 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon); 55617023 q if (isect_lat < pt.lat) 55712153 q is_inside = !is_inside; 5584871 q else if (isect_lat == pt.lat) 56193327 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon) 5630 if (lhs.lat < pt.lat) 5640 is_inside = !is_inside; 56693327 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon) 56911 q is_inside = !is_inside; 57193317 q else if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0) 5737163 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat); 5747163 q if (border_lon < isect_lon && isect_lon < pt.lon) 5751131 q is_inside = !is_inside; 579 // The segment runs from sw to ne or ne to sw 581324158 q if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0) 58310294 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat); 58410294 q if ((lhs.lon <= pt.lon || rhs.lon <= pt.lon) && (border_lon < isect_lon)) 5857524 q is_inside = !is_inside; 587324158 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 593108936 q if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0) 59518325 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon); 59618325 q if (isect_lat < pt.lat) 59713315 q is_inside = !is_inside; 5985012 q else if (isect_lat == pt.lat) 60190616 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon) 6030 if (lhs.lat < pt.lat) 6040 is_inside = !is_inside; 60690616 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon) 60921 q is_inside = !is_inside; 615bool Free_Polygon_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 6170 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 6180 iti != linestrings.end(); ++iti) 6200 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 6220 if (bbox.contains(*it)) 6270 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 6280 iti != linestrings.end(); ++iti) 6300 for (uint i = 1; i < iti->size(); ++i) 6320 if (bbox.intersects((*iti)[i-1], (*iti)[i])) 641Point_Double interpolation_point( 642 double orth_x, double orth_y, double orth_z, 643 double lhs_gc_x, double lhs_gc_z, double factor, double lhs_lon) 645106236 q static const double deg_to_arc = acos(0)/90.; 647106236 q double sin_factor = sin(factor); 648106236 q double cos_factor = cos(factor); 649106236 q double new_x = sin_factor * orth_x + cos_factor * lhs_gc_x; 650106236 q double new_y = sin_factor * orth_y; 651106236 q double new_z = sin_factor * orth_z + cos_factor * lhs_gc_z; 653106236 q double new_lat = asin(new_x)/deg_to_arc; 654106236 q double new_lon = atan2(new_y, new_z)/deg_to_arc + lhs_lon; 655106236 q if (new_lon < -180.) 657106236 q else if (new_lon > 180.) 660106236 q return Point_Double(new_lat, new_lon); 666 struct Interpolation_Collector 668 Interpolation_Collector(double orth_x, double orth_y, double orth_z, 669 double lhs_gc_x, double lhs_gc_z, double dist, double lhs_lon, std::vector< Point_Double >& target); 6710 void collect_single_point(const Point_Double& pt) { target->push_back(pt); } 672 void collect_single_point(double dist); 673 void collect_sequence(double from, double to, double max_step); 674 void collect_center(double from, double to, int divisor); 685 double acceptable_max_length; 686 std::vector< Point_Double >* target; 690 Interpolation_Collector::Interpolation_Collector(double orth_x_, double orth_y_, double orth_z_, 691 double lhs_gc_x_, double lhs_gc_z_, double dist, double lhs_lon_, std::vector< Point_Double >& target_) 69227926 q : orth_x(orth_x_), orth_y(orth_y_), orth_z(orth_z_), 69327926 q lhs_gc_x(lhs_gc_x_), lhs_gc_z(lhs_gc_z_), lhs_lon(lhs_lon_), 69427926 q center(lhs_gc_x == 0 ? dist : atan(orth_x/lhs_gc_x)), 69527926 q ex_pt(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, 69627926 q std::max(std::min(center, dist), 0.), lhs_lon)), 69727926 q ex_gc_pt_lat(ex_pt.lat), 70027926 q static const double deg_to_arc = acos(0)/90.; 70127926 q acceptable_max_length = .0065536*cos(ex_pt.lat * deg_to_arc); 70327926 q double bounded_center = std::max(std::min(center, dist), 0.); 70427926 q if (acceptable_max_length < .0016384 && center != bounded_center) 7060 Point_Double ex_gc_pt = interpolation_point(orth_x, orth_y, orth_z, 7070 lhs_gc_x, lhs_gc_z, center, lhs_lon); 7080 ex_gc_pt_lat = ex_gc_pt.lat; 71027926 q center = bounded_center; 714 void Interpolation_Collector::collect_single_point(double dist) 71642 q target->push_back(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon)); 720 void Interpolation_Collector::collect_sequence(double from, double to, double max_step) 72255846 q int num_sections = (int)((to - from)/max_step)+1; 723134116 q for (int j = 1; j < num_sections; ++j) 72478276 q target->push_back(interpolation_point(orth_x, orth_y, orth_z, 72578276 q lhs_gc_x, lhs_gc_z, from + (to - from)*j/num_sections, lhs_lon)); 729 void Interpolation_Collector::collect_center(double from, double to, int divisor) 73127926 q static const double deg_to_arc = acos(0)/90.; 73327926 q double max_length_threshold = .0065536/divisor; 73427926 q if (acceptable_max_length < max_length_threshold && divisor < 65536) 7360 double dist_ex_s = acos(sqrt(1-1./divisor/divisor)/sin(ex_gc_pt_lat * deg_to_arc)); 7370 double bound_l = std::max(std::min(center - dist_ex_s, to), from); 7380 double bound_r = std::max(std::min(center + dist_ex_s, to), from); 7400 collect_sequence(from, bound_l, max_length_threshold*deg_to_arc); 7410 if (bound_l > 0 && bound_l < to) 7420 collect_single_point(bound_l); 7440 collect_center(bound_l, bound_r, divisor*4); 7460 if (bound_r > 0 && bound_r < to) 7470 collect_single_point(bound_r); 7480 collect_sequence(bound_r, to, max_length_threshold*deg_to_arc); 75227926 q if (acceptable_max_length < 1e-9) 7530 acceptable_max_length = 1e-9; 75427926 q acceptable_max_length *= deg_to_arc; 75627926 q collect_sequence(from, center, acceptable_max_length); 75727926 q if (center > from && center < to) 75842 q collect_single_point(center); 75927926 q collect_sequence(center, to, acceptable_max_length); 765void interpolate_segment(double lhs_lat, double lhs_lon, double rhs_lat, double rhs_lon, 766 std::vector< Point_Double >& target) 7689967411 q static const double deg_to_arc = acos(0)/90.; 7709967411 q if (fabs(rhs_lat - lhs_lat) < .0065536 && fabs(rhs_lon - lhs_lon) < .0065536) 7719688211 q target.push_back(Point_Double(rhs_lat, rhs_lon)); 77427926 q if (fabs(rhs_lon - lhs_lon) > 180.) 7750 rhs_lon = (lhs_lon < 0 ? rhs_lon - 360. : rhs_lon + 360.); 77727926 q if (fabs(rhs_lon - lhs_lon) > 179.999999) 7790 double pole_lat = (lhs_lat + rhs_lat >= 0 ? 90. : -90.); 7810 int num_sections = (int)(fabs(pole_lat - lhs_lat)/.0065536)+1; 7820 for (int j = 1; j < num_sections; ++j) 7830 target.push_back(Point_Double(lhs_lat + (pole_lat - lhs_lat)*j/num_sections, lhs_lon)); 7840 target.push_back(Point_Double(pole_lat, lhs_lon)); 7860 num_sections = (int)(fabs(rhs_lon - lhs_lon)/.0065536)+1; 7870 for (int j = 1; j < num_sections; ++j) 7880 target.push_back(Point_Double(pole_lat, lhs_lon + (rhs_lon - lhs_lon)*j/num_sections)); 7890 target.push_back(Point_Double(pole_lat, rhs_lon)); 7910 num_sections = (int)(fabs(rhs_lat - pole_lat)/.0065536)+1; 7920 for (int j = 1; j < num_sections; ++j) 7930 target.push_back(Point_Double(pole_lat + (rhs_lat - pole_lat)*j/num_sections, rhs_lon)); 79727926 q double lhs_cos = cos(lhs_lat * deg_to_arc); 79827926 q double lhs_gc_x = sin(lhs_lat * deg_to_arc); 79927926 q double lhs_gc_z = lhs_cos; 80127926 q double rhs_cos = cos(rhs_lat * deg_to_arc); 80227926 q double rhs_gc_x = sin(rhs_lat * deg_to_arc); 80327926 q double rhs_gc_y = sin((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos; 80427926 q double rhs_gc_z = cos((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos; 80627926 q double prod = lhs_gc_x * rhs_gc_x + lhs_gc_z * rhs_gc_z; 80727926 q double dist = acos(prod); 80927926 q double orth_x = rhs_gc_x - prod * lhs_gc_x; 81027926 q double orth_y = rhs_gc_y; 81127926 q double orth_z = rhs_gc_z - prod * lhs_gc_z; 81227926 q double lg_orth = sqrt(orth_x * orth_x + orth_y * orth_y + orth_z * orth_z); 81727926 q Interpolation_Collector collector(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon, target); 81827926 q collector.collect_center(0, dist, 4); 82127926 q if (rhs_lon < -180.) 82327926 q else if (rhs_lon > 180.) 82527926 q target.push_back(Point_Double(rhs_lat, rhs_lon)); 830void add_segment(std::map< uint32, std::vector< unsigned int > >& segments_per_idx, 831 const Point_Double& from, const Point_Double& to, unsigned int pos) 83310750511 q uint32 lhs_ilat = ::ilat(from.lat); 83410750511 q int32 lhs_ilon = ::ilon(from.lon); 83510750511 q uint32 rhs_ilat = ::ilat(to.lat); 83610750511 q int32 rhs_ilon = ::ilon(to.lon); 83810750511 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos); 83910750511 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 84099446 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos); 84110750511 q if ((lhs_ilat & 0xffff0000) != (rhs_ilat & 0xffff0000)) 84375806 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos); 84475806 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 84528506 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos); 850void replace_segment(std::vector< unsigned int >& segments, unsigned int old_pos, unsigned int new_pos) 8520 for (std::vector< unsigned int >::iterator it = segments.begin(); it != segments.end(); ++it) 860void replace_segment(std::map< uint32, std::vector< unsigned int > >& segments_per_idx, 861 const Point_Double& from, const Point_Double& via, const Point_Double& to, 862 unsigned int old_pos, unsigned int new_pos) 8640 uint32 lhs_ilat = ::ilat(from.lat) & 0xffff0000; 8650 int32 lhs_ilon = ::ilon(from.lon) & 0xffff0000; 8660 uint32 rhs_ilat = ::ilat(to.lat) & 0xffff0000; 8670 int32 rhs_ilon = ::ilon(to.lon) & 0xffff0000; 8680 uint32 via_ilat = ::ilat(via.lat) & 0xffff0000; 8690 int32 via_ilon = ::ilon(via.lon) & 0xffff0000; 871 // The upper part is the one that possibly crosses the index boundary 8720 if (via_ilat == lhs_ilat && via_ilon == lhs_ilon) 8750 replace_segment(segments_per_idx[lhs_ilat | (uint32(lhs_ilon)>>16)], old_pos, new_pos); 8760 if (lhs_ilon != rhs_ilon) 8770 replace_segment(segments_per_idx[lhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos); 8780 if (lhs_ilat != rhs_ilat) 8800 replace_segment(segments_per_idx[rhs_ilat | (uint32(lhs_ilon)>>16)], old_pos, new_pos); 8810 if (lhs_ilon != rhs_ilon) 8830 if ((via_ilat == lhs_ilat && via_ilon == lhs_ilon) || (via_ilat == rhs_ilat && via_ilon == rhs_ilon)) 8840 replace_segment(segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos); 8870 segments_per_idx[lhs_ilat | (uint32(rhs_ilon)>>16)].push_back(new_pos+1); 8880 segments_per_idx[rhs_ilat | (uint32(lhs_ilon)>>16)].push_back(new_pos+1); 8890 replace_segment(segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)], old_pos, new_pos+1); 8940 if (via_ilat == lhs_ilat && via_ilon == lhs_ilon) 8950 segments_per_idx[lhs_ilat | (uint32(lhs_ilon)>>16)].push_back(new_pos-1); 8960 else if (via_ilat == rhs_ilat && via_ilon == rhs_ilon) 8970 segments_per_idx[rhs_ilat | (uint32(rhs_ilon)>>16)].push_back(new_pos+1); 901bool try_intersect(const Point_Double& lhs_from, const Point_Double& lhs_to, 902 const Point_Double& rhs_from, const Point_Double& rhs_to, Point_Double& isect) 90428974710 q double rfmlt_lat = rhs_from.lat - lhs_to.lat; 90528974710 q double rfmlt_lon = rhs_from.lon - lhs_to.lon; 907 //The two segments are connected by a vertex 90828974710 q if (!rfmlt_lat && !rfmlt_lon) 9102036369 q if (lhs_from == rhs_to || lhs_from == rhs_from || lhs_to == rhs_to) 9131811837 q double lfmlt_lat = lhs_from.lat - lhs_to.lat; 9141811837 q double lfmlt_lon = lhs_from.lon - lhs_to.lon; 9151811837 q double rfmrt_lat = rhs_from.lat - rhs_to.lat; 9161811837 q double rfmrt_lon = rhs_from.lon - rhs_to.lon; 9171811837 q double det = lfmlt_lat * rfmrt_lon - rfmrt_lat * lfmlt_lon; 921 // Segments on parallel but distinct beams 922642 q if (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat != 0) 925642 q if (fabs(rfmlt_lat) > fabs(rfmlt_lon)) 92741 q if (lhs_from.lat < lhs_to.lat) 92941 q if (rhs_from.lat < rhs_to.lat) 931 // Segments are non-overlapping 93241 q if (lhs_to.lat < rhs_from.lat || rhs_to.lat < lhs_from.lat) 9350 if (lhs_from.lat < rhs_from.lat) 942 // Segments are non-overlapping 9430 if (lhs_to.lat < rhs_to.lat || rhs_from.lat < lhs_from.lat) 9460 if (lhs_from.lat < rhs_to.lat) 9540 if (rhs_from.lat < rhs_to.lat) 956 // Segments are non-overlapping 9570 if (lhs_from.lat < rhs_from.lat || rhs_to.lat < lhs_to.lat) 9600 if (lhs_to.lat < rhs_from.lat) 967 // Segments are non-overlapping 9680 if (lhs_from.lat < rhs_to.lat || rhs_from.lat < lhs_to.lat) 9710 if (lhs_to.lat < rhs_to.lat) 980601 q if (lhs_from.lon < lhs_to.lon) 982601 q if (rhs_from.lon < rhs_to.lon) 984 // Segments are non-overlapping 985601 q if (lhs_to.lon < rhs_from.lon || rhs_to.lon < lhs_from.lon) 9880 if (lhs_from.lon < rhs_from.lon) 995 // Segments are non-overlapping 9960 if (lhs_to.lon < rhs_to.lon || rhs_from.lon < lhs_from.lon) 9990 if (lhs_from.lon < rhs_to.lon) 10070 if (rhs_from.lon < rhs_to.lon) 1009 // Segments are non-overlapping 10100 if (lhs_from.lon < rhs_from.lon || rhs_to.lon < lhs_to.lon) 10130 if (lhs_to.lon < rhs_from.lon) 1020 // Segments are non-overlapping 10210 if (lhs_from.lon < rhs_to.lon || rhs_from.lon < lhs_to.lon) 10240 if (lhs_to.lon < rhs_to.lon) 10341811197 q double x = (rfmrt_lon * rfmlt_lat - rfmrt_lat * rfmlt_lon)/det; 10351811197 q double y = (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat)/det; 10371811197 q if (x <= -1e-7 || x >= 1+1e-7 || y <= -1e-7 || y >= 1+1e-7) 10400 isect.lat = lhs_to.lat + x * lfmlt_lat; 10410 isect.lon = lhs_to.lon + x * lfmlt_lon; 1046struct Idx_Per_Point_Double 1048 Idx_Per_Point_Double(const Point_Double& pt_, unsigned int idx_, unsigned int remote_idx_) 104921501011 q : pt(pt_), idx(idx_), remote_idx(remote_idx_) {} 1053 unsigned int remote_idx; 1055 bool operator<(const Idx_Per_Point_Double& rhs) const 105762407411 q if (pt.lat != rhs.pt.lat) 105838137510 q return pt.lat < rhs.pt.lat; 105924269911 q if (pt.lon != rhs.pt.lon) 1060882844 q return pt.lon < rhs.pt.lon; 1067struct Line_Divertion 106930625811 q Line_Divertion() : to_idx(std::numeric_limits< uint >::max()), 107030625811 q to_remote_idx(std::numeric_limits< uint >::max()) {} 107121501011 q Line_Divertion(const Idx_Per_Point_Double& rhs) : to_idx(rhs.idx), to_remote_idx(rhs.remote_idx) {} 1073 unsigned int to_idx; 1074 unsigned int to_remote_idx; 1079 std::vector< Point_Double >& all_segments, 1080 std::vector< unsigned int >& gap_positions, 1081 std::map< uint32, std::vector< unsigned int > >& segments_per_idx) 10832281211 q for (std::map< uint32, std::vector< unsigned int > >::iterator idx_it = segments_per_idx.begin(); 10845903111 q idx_it != segments_per_idx.end(); ++idx_it) 10863621911 q Point_Double isect(100, 0); 108712787911 q for (unsigned int i = 1; i < idx_it->second.size(); ++i) 108938140710 q for (unsigned int j = 0; j < i; ++j) 109128974710 q if (try_intersect(all_segments[idx_it->second[j]], all_segments[idx_it->second[j]+1], 109228974710 q all_segments[idx_it->second[i]], all_segments[idx_it->second[i]+1], isect)) 10940 uint32 lhs_ilat = ::ilat(isect.lat); 10950 int32 lhs_ilon = ::ilon(isect.lon); 10970 if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx_it->first) 1098 // Ensure that the same intersection is processed only in one index 1100 // Avoid rounding artifacts 11010 for (unsigned int k = 0; k < idx_it->second.size(); ++k) 11030 if (isect.epsilon_equal(all_segments[idx_it->second[k]])) 11050 isect.lat = all_segments[idx_it->second[k]].lat; 11060 isect.lon = all_segments[idx_it->second[k]].lon; 11080 if (isect.epsilon_equal(all_segments[idx_it->second[k]+1])) 11100 isect.lat = all_segments[idx_it->second[k]+1].lat; 11110 isect.lon = all_segments[idx_it->second[k]+1].lon; 11150 if (isect != all_segments[idx_it->second[j]] && isect != all_segments[idx_it->second[j]+1]) 11170 all_segments.push_back(all_segments[idx_it->second[j]]); 11180 all_segments.push_back(isect); 11190 all_segments.push_back(all_segments[idx_it->second[j]+1]); 11200 gap_positions.insert(std::lower_bound( 11210 gap_positions.begin(), gap_positions.end(), idx_it->second[j]+1), idx_it->second[j]+1); 11220 gap_positions.push_back(all_segments.size()); 11240 replace_segment(segments_per_idx, all_segments[idx_it->second[j]], isect, 11250 all_segments[idx_it->second[j]+1], idx_it->second[j], all_segments.size()-3); 11280 if (isect != all_segments[idx_it->second[i]] && isect != all_segments[idx_it->second[i]+1]) 11300 all_segments.push_back(all_segments[idx_it->second[i]]); 11310 all_segments.push_back(isect); 11320 all_segments.push_back(all_segments[idx_it->second[i]+1]); 11330 gap_positions.insert(std::lower_bound( 11340 gap_positions.begin(), gap_positions.end(), idx_it->second[i]+1), idx_it->second[i]+1); 11350 gap_positions.push_back(all_segments.size()); 11370 replace_segment(segments_per_idx, all_segments[idx_it->second[i]], isect, 11380 all_segments[idx_it->second[i]+1], idx_it->second[i], all_segments.size()-3); 1142 //TODO: check and track common segments 1150unsigned int line_divertion_idx(const Idx_Per_Point_Double& rhs) 115221501011 q return rhs.idx*2 + ((int)rhs.remote_idx - (int)rhs.idx - 1)/2; 1156void collect_divertions(const std::vector< Point_Double >& all_segments, 1157 uint32 idx, const std::vector< unsigned int >& segments, 1158 std::vector< Line_Divertion >& divertions) 11603621911 q std::vector< Idx_Per_Point_Double > pos_per_pt; 11623621911 q for (std::vector< unsigned int >::const_iterator seg_it = segments.begin(); 116316409811 q seg_it != segments.end(); ++seg_it) 116512787911 q uint32 lhs_ilat = ::ilat(all_segments[*seg_it].lat); 116612787911 q int32 lhs_ilon = ::ilon(all_segments[*seg_it].lon); 116712787911 q if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx) 116810750511 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it], *seg_it, *seg_it+1)); 117012787911 q uint32 rhs_ilat = ::ilat(all_segments[*seg_it+1].lat); 117112787911 q int32 rhs_ilon = ::ilon(all_segments[*seg_it+1].lon); 117212787911 q if (((rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)) == idx) 117310750511 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it+1], *seg_it+1, *seg_it)); 11793278611 q std::sort(pos_per_pt.begin(), pos_per_pt.end()); 11813278611 q unsigned int same_since = 0; 118328058211 q while (i <= pos_per_pt.size()) 118524779611 q if (i == pos_per_pt.size() || pos_per_pt[i].pt != pos_per_pt[same_since].pt) 118910750511 q divertions[line_divertion_idx(pos_per_pt[same_since])] = Line_Divertion(pos_per_pt[same_since+1]); 119010750511 q divertions[line_divertion_idx(pos_per_pt[same_since+1])] = Line_Divertion(pos_per_pt[same_since]); 11940 std::vector< std::pair< double, unsigned int > > line_per_gradient; 11950 for (unsigned int j = same_since; j < i; ++j) 11960 line_per_gradient.push_back(std::make_pair( 11970 atan2(all_segments[pos_per_pt[j].remote_idx].lon - all_segments[pos_per_pt[j].idx].lon, 11980 all_segments[pos_per_pt[j].remote_idx].lat - all_segments[pos_per_pt[j].idx].lat), 12010 std::sort(line_per_gradient.begin(), line_per_gradient.end()); 12030 for (unsigned int j = 0; j < line_per_gradient.size(); j += 2) 12050 if (j+3 < line_per_gradient.size() 12060 && line_per_gradient[j+1].first == line_per_gradient[j+2].first) 12080 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j].second])] 12090 = Line_Divertion(pos_per_pt[line_per_gradient[j+3].second]); 12100 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+1].second])] 12110 = Line_Divertion(pos_per_pt[line_per_gradient[j+2].second]); 12120 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+2].second])] 12130 = Line_Divertion(pos_per_pt[line_per_gradient[j+1].second]); 12140 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+3].second])] 12150 = Line_Divertion(pos_per_pt[line_per_gradient[j].second]); 12200 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j].second])] 12210 = Line_Divertion(pos_per_pt[line_per_gradient[j+1].second]); 12220 divertions[line_divertion_idx(pos_per_pt[line_per_gradient[j+1].second])] 12230 = Line_Divertion(pos_per_pt[line_per_gradient[j].second]); 1235void assemble_linestrings(const std::vector< Point_Double >& all_segments, 1236 std::vector< Line_Divertion >& divertions, std::vector< std::vector< Point_Double > >& linestrings) 1238 // the function changes divertions because divertions is no longer needed after the function 124215312911 q for (unsigned int i = 0; i < divertions.size(); i += 2) 124313031711 q count += (divertions[i+1].to_idx == std::numeric_limits< uint >::max()); 1245 //Invariant: count is always equal to the number of i with divertions[2*i+1] == uint::max() 12464562411 q while (count < all_segments.size()) 12482281211 q while (divertions[2*pos].to_idx == std::numeric_limits< uint >::max()) 12510 if (pos == all_segments.size()-1) 12552281211 q linestrings.push_back(std::vector< Point_Double >()); 12562281211 q std::vector< Point_Double >& cur_target = linestrings.back(); 125913031711 q while (divertions[2*pos + dir].to_idx != std::numeric_limits< uint >::max()) 126110750511 q if (cur_target.size() >= 2 && all_segments[pos].epsilon_equal(cur_target[cur_target.size()-2])) 1262 // Remove pairs of equal segments 12630 cur_target.pop_back(); 126410750511 q else if (cur_target.empty() || !all_segments[pos].epsilon_equal(cur_target.back())) 126510750511 q cur_target.push_back(all_segments[pos]); 126710750511 q Line_Divertion& old_div = divertions[2*(int)pos + dir]; 126810750511 q Line_Divertion& divertion = divertions[2*(int)pos + (3*dir-1)/2]; 126910750511 q dir = divertion.to_remote_idx - divertion.to_idx; 127310750511 q old_div.to_idx = std::numeric_limits< uint >::max(); 12772281211 q if (pos == all_segments.size()-1) 12802281211 q if (cur_target.size() <= 2) 1283222149 q cur_target.push_back(cur_target.front()); 1288struct RHR_Polygon_Area_Oracle : Area_Oracle 1290 RHR_Polygon_Area_Oracle( 1291 const std::vector< Point_Double >& all_segments_, 1292 const std::map< uint32, std::vector< unsigned int > >& segments_per_idx_) 12932281211 q : all_segments(&all_segments_), segments_per_idx(&segments_per_idx_) {} 1295 virtual void build_area(bool sw_corner_inside, int32 value, bool* se_corner_inside, bool* nw_corner_inside); 1296 virtual Area_Oracle::point_status get_point_status(int32 value, double lat, double lon); 1299 const std::vector< Point_Double >* all_segments; 1300 const std::map< uint32, std::vector< unsigned int > >* segments_per_idx; 1301 std::set< uint32 > inside_corners; 1305void RHR_Polygon_Area_Oracle::build_area( 1306 bool sw_corner_inside, int32 value, bool* se_corner_inside, bool* nw_corner_inside) 13083621911 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value); 13093621911 q if (spi_it == segments_per_idx->end()) 131331326 q inside_corners.insert(value); 13153621911 q if (::lon(uint32(value)<<16) > -179.99) 131962439 q *nw_corner_inside = sw_corner_inside; 132162439 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 1322297229 q seg_it != spi_it->second.end(); ++seg_it) 1324234799 q int32 lhs_ilon = ::ilon((*all_segments)[*seg_it].lon) & 0xffff0000; 1325234799 q int32 rhs_ilon = ::ilon((*all_segments)[*seg_it+1].lon) & 0xffff0000; 1327234799 q if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16)) 1328234799 q || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16))) 133042606 q double isect_lat = (*all_segments)[*seg_it].lat 133142606 q + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat) 133242606 q *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon) 133342606 q /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon); 133442606 q if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000)) 133533246 q *nw_corner_inside = !*nw_corner_inside; 1342120089 q *se_corner_inside = sw_corner_inside; 1344120089 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 1345588069 q seg_it != spi_it->second.end(); ++seg_it) 1347467989 q uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000; 1348467989 q uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000; 1350467989 q if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000)) 1351467989 q || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000))) 135369266 q double isect_lon = (*all_segments)[*seg_it].lon 135469266 q + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon) 135569266 q *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat) 135669266 q /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat); 135769266 q if ((::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16)) 135850526 q *se_corner_inside = !*se_corner_inside; 13650 if (nw_corner_inside) 13670 *nw_corner_inside = sw_corner_inside; 13690 for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 13700 seg_it != spi_it->second.end(); ++seg_it) 13720 double lhs_lon = (*all_segments)[*seg_it].lon; 13730 lhs_lon -= lhs_lon > 0 ? 360. : 0.; 13740 double rhs_lon = (*all_segments)[*seg_it+1].lon; 13750 rhs_lon -= rhs_lon > 0 ? 360. : 0.; 13770 if (::lon(uint32(value)<<16) > -180.) 13790 int32 lhs_ilon = ::ilon(lhs_lon) & 0xffff0000; 13800 int32 rhs_ilon = ::ilon(rhs_lon) & 0xffff0000; 13820 if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16)) 13830 || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16))) 13850 double isect_lat = (*all_segments)[*seg_it].lat 13860 + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat) 13870 *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon) 13880 /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon); 13890 if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000)) 13900 *nw_corner_inside = !*nw_corner_inside; 13950 if ((lhs_lon <= -180. && rhs_lon > -180.) || (rhs_lon <= -180. && lhs_lon > -180.)) 13970 double isect_lat = (*all_segments)[*seg_it].lat 13980 + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat) 13990 *(-180. - (*all_segments)[*seg_it].lon) 14000 /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon); 14010 if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000)) 14020 *nw_corner_inside = !*nw_corner_inside; 14080 if (se_corner_inside) 14100 *se_corner_inside = sw_corner_inside; 14120 for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 14130 seg_it != spi_it->second.end(); ++seg_it) 14150 uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000; 14160 uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000; 14180 if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000)) 14190 || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000))) 14210 double lhs_lon = (*all_segments)[*seg_it].lon; 14220 lhs_lon -= lhs_lon > 0 ? 360. : 0.; 14230 double rhs_lon = (*all_segments)[*seg_it+1].lon; 14240 rhs_lon -= rhs_lon > 0 ? 360. : 0.; 14260 double isect_lon = (*all_segments)[*seg_it].lon 14270 + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon) 14280 *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat) 14290 /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat); 14300 if (isect_lon >= -180. && (::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16)) 14310 *se_corner_inside = !*se_corner_inside; 1439Area_Oracle::point_status RHR_Polygon_Area_Oracle::get_point_status(int32 value, double lat, double lon) 1444222149 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value); 1445222149 q if (spi_it == segments_per_idx->end()) 1448222149 q double border_lon = std::max(::lon(value<<16), -180.); 1449222149 q double border_lat = ::lat(value & 0xffff0000); 1451222149 q bool on_vertex = false; 1452222149 q bool on_segment = false; 1453222149 q bool is_inside = (inside_corners.find(value) != inside_corners.end()); 1454 // is_inside is now true iff the sw corner is inside the area 1456222149 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 14571062039 q seg_it != spi_it->second.end(); ++seg_it) 1458839899 q toggle_if_inside(is_inside, on_vertex, on_segment, border_lat, border_lon, Point_Double(lat, lon), 1459839899 q (*all_segments)[*seg_it], (*all_segments)[*seg_it+1]); 1462222149 q return 0x20 + 2*is_inside; 14640 return 0x10 + 2*is_inside; 14660 return 2*is_inside; 1470bool strictly_west_of(double lhs, double rhs) 14730 return lhs - 360. < rhs; 1479bool weakly_west_of(double lhs, double rhs) 14820 return lhs - 360. <= rhs; 14882281211 qRHR_Polygon_Geometry::RHR_Polygon_Geometry(const Free_Polygon_Geometry& rhs) : bounds(0) 14902281211 q std::vector< std::vector< Point_Double > > input(*rhs.get_multiline_geometry()); 14922281211 q std::vector< Point_Double > all_segments; 14932281211 q std::vector< unsigned int > gap_positions; 14952281211 q gap_positions.push_back(0); 14964562411 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = input.begin(); iti != input.end(); ++iti) 14982281211 q all_segments.push_back((*iti)[0]); 149912248611 q for (unsigned int i = 1; i < iti->size(); ++i) 15009967411 q interpolate_segment((*iti)[i-1].lat, (*iti)[i-1].lon, (*iti)[i].lat, (*iti)[i].lon, all_segments); 15012281211 q gap_positions.push_back(all_segments.size()); 15042281211 q std::map< uint32, std::vector< unsigned int > > segments_per_idx; 15052281211 q std::vector< unsigned int >::const_iterator gap_it = gap_positions.begin(); 150615312911 q for (unsigned int i = 0; i < all_segments.size(); ++i) 151110750511 q add_segment(segments_per_idx, all_segments[i-1], all_segments[i], i-1); 15132281211 q split_segments(all_segments, gap_positions, segments_per_idx); 1515 // divertions[2*i - 2] tells which segment follows the segment from all_segments[i+1] to all_segments[i] 1516 // divertions[2*i - 1] tells which segment follows the segment from all_segments[i] to all_segments[i+1] 15172281211 q std::vector< Line_Divertion > divertions(2*all_segments.size()); 1519 // fill the gaps with invalid following segments 15204562411 q for (unsigned int i = 1; i < gap_positions.size(); ++i) 15222281211 q divertions[gap_positions[i]*2 - 2] = Line_Divertion(); 15232281211 q divertions[gap_positions[i]*2 - 1] = Line_Divertion(); 1526 // compute the real segment connections 15272281211 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin(); 15285903111 q idx_it != segments_per_idx.end(); ++idx_it) 15293621911 q collect_divertions(all_segments, idx_it->first, idx_it->second, divertions); 15312281211 q assemble_linestrings(all_segments, divertions, linestrings); 15332281211 q RHR_Polygon_Area_Oracle area_oracle(all_segments, segments_per_idx); 15342281211 q Four_Field_Index four_field_idx(&area_oracle); 15362281211 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin(); 15375903111 q idx_it != segments_per_idx.end(); ++idx_it) 15383621911 q four_field_idx.add_point(::lat(idx_it->first | 0x8000), ::lon(idx_it->first<<16 | 0x8000), idx_it->first); 15402281211 q four_field_idx.compute_inside_parts(); 15422281211 q for (std::vector< std::vector< Point_Double > >::iterator lstr_it = linestrings.begin(); 15434502611 q lstr_it != linestrings.end(); ++lstr_it) 1545222149 q if (lstr_it->size() > 2) 1547222149 q if (strictly_west_of((*lstr_it)[0].lon, (*lstr_it)[1].lon)) 1549185943 q if (weakly_west_of((*lstr_it)[1].lon, (*lstr_it)[2].lon)) 155110042 q if (four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 15520 std::reverse(lstr_it->begin(), lstr_it->end()); 1556175901 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 1557175901 q ^ (((*lstr_it)[1].lat - (*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon) 1558175901 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon))) 1559175901 q std::reverse(lstr_it->begin(), lstr_it->end()); 156236208 q else if (strictly_west_of((*lstr_it)[2].lon, (*lstr_it)[1].lon)) 156429237 q if (!(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)) 156591 q std::reverse(lstr_it->begin(), lstr_it->end()); 15676972 q else if ((*lstr_it)[0].lon == (*lstr_it)[1].lon) 156921 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 157021 q ^ ((*lstr_it)[0].lat < (*lstr_it)[1].lat)) 157121 q std::reverse(lstr_it->begin(), lstr_it->end()); 15736951 q else if ((*lstr_it)[2].lon == (*lstr_it)[1].lon) 157511 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 157611 q ^ ((*lstr_it)[1].lat < (*lstr_it)[2].lat)) 157711 q std::reverse(lstr_it->begin(), lstr_it->end()); 15816941 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 15826941 q ^ (((*lstr_it)[1].lat -(*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon) 15836941 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon))) 15840 std::reverse(lstr_it->begin(), lstr_it->end()); 1591double RHR_Polygon_Geometry::center_lat() const 159446083 q bounds = calc_bounds(linestrings); 159646083 q return bounds->center_lat(); 1600double RHR_Polygon_Geometry::center_lon() const 16030 bounds = calc_bounds(linestrings); 160546083 q return bounds->center_lon(); 1609double RHR_Polygon_Geometry::south() const 16120 bounds = calc_bounds(linestrings); 16140 return bounds->south; 1618double RHR_Polygon_Geometry::north() const 16210 bounds = calc_bounds(linestrings); 16230 return bounds->north; 1627double RHR_Polygon_Geometry::west() const 16300 bounds = calc_bounds(linestrings); 16320 return bounds->west; 1636double RHR_Polygon_Geometry::east() const 16390 bounds = calc_bounds(linestrings); 16410 return bounds->east; 1645bool RHR_Polygon_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 16470 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 16480 iti != linestrings.end(); ++iti) 16500 for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 16520 if (bbox.contains(*it)) 16570 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 16580 iti != linestrings.end(); ++iti) 16600 for (uint i = 1; i < iti->size(); ++i) 16620 if (bbox.intersects((*iti)[i-1], (*iti)[i])) 16670 bool is_inside = false; 16680 bool on_vertex = false; 16690 bool on_segment = false; 16700 Point_Double bbox_center(bbox.center_lat(), bbox.center_lon()); 16720 for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 16730 iti != linestrings.end(); ++iti) 16750 for (uint i = 1; i < iti->size(); ++i) 16760 toggle_if_inside(is_inside, on_vertex, on_segment, -90., -180., 16770 bbox_center, (*iti)[i-1], (*iti)[i]); 16790 return (is_inside || on_vertex || on_segment); 1683Opaque_Geometry* Compound_Geometry::clone() const 16852716320 q std::vector< Opaque_Geometry* > cloned; 168613652520 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 168710936219 q cloned.push_back((*it)->clone()); 16882716320 q return new Compound_Geometry(cloned); 1692Bbox_Double* calc_bounds(const std::vector< Opaque_Geometry* >& components) 170045457526 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 170433872825 q south = std::min(south, (*it)->south()); 170533872825 q north = std::max(north, (*it)->north()); 170633872825 q west = std::min(west, (*it)->west()); 170733872825 q east = std::max(east, (*it)->east()); 170933872825 q wrapped |= ((*it)->east() < (*it)->west()); 171432 q return new Bbox_Double(Bbox_Double::invalid); 17152677425 q if (!wrapped && east - west <= 180.) 17162677425 q return new Bbox_Double(south, west, north, east); 17200 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 17220 if (!(*it)->has_bbox()) 17250 if ((*it)->east() <= 0) 17270 east = std::max(east, (*it)->east()); 17280 if ((*it)->west() >= 0) 17290 west = std::min(west, (*it)->west()); 17310 else if ((*it)->west() >= 0) 17320 west = std::min(west, (*it)->west()); 1734 // The components are too wildly distributed 17350 return new Bbox_Double(south, -180.0, north, 180.0); 17380 if (west - east > 180.) 17390 return new Bbox_Double(south, west, north, east); 17410 return new Bbox_Double(south, -180.0, north, 180.0); 1745bool Compound_Geometry::has_center() const 17482110618 q bounds = calc_bounds(components); 1754double Compound_Geometry::center_lat() const 17570 bounds = calc_bounds(components); 175974515 q return bounds->center_lat(); 1763double Compound_Geometry::center_lon() const 17660 bounds = calc_bounds(components); 17682104711 q return bounds->center_lon(); 1772bool Compound_Geometry::has_bbox() const 177549799 q bounds = calc_bounds(components); 177749799 q return bounds->valid(); 1781double Compound_Geometry::south() const 17840 bounds = calc_bounds(components); 1790double Compound_Geometry::north() const 17930 bounds = calc_bounds(components); 1799double Compound_Geometry::west() const 18020 bounds = calc_bounds(components); 1808double Compound_Geometry::east() const 18110 bounds = calc_bounds(components); 1817bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos) const 181943936 q return (member_pos < components.size() && components[member_pos] 182043936 q && components[member_pos]->has_center()); 1824double Compound_Geometry::relation_pos_lat(unsigned int member_pos) const 182643936 q if (member_pos < components.size() && components[member_pos]) 182743936 q return components[member_pos]->center_lat(); 1832double Compound_Geometry::relation_pos_lon(unsigned int member_pos) const 183443936 q if (member_pos < components.size() && components[member_pos]) 183543936 q return components[member_pos]->center_lon(); 1840unsigned int Compound_Geometry::relation_way_size(unsigned int member_pos) const 184228977238 q if (member_pos < components.size() && components[member_pos]) 184328977238 q return components[member_pos]->way_size(); 1848bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const 185020941358 q return (member_pos < components.size() && components[member_pos] 185120941358 q && components[member_pos]->way_pos_is_valid(nd_pos)); 1855double Compound_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const 185714255528 q if (member_pos < components.size() && components[member_pos]) 185814255528 q return components[member_pos]->way_pos_lat(nd_pos); 1863double Compound_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const 186514255528 q if (member_pos < components.size() && components[member_pos]) 186614255528 q return components[member_pos]->way_pos_lon(nd_pos); 1871void Compound_Geometry::add_component(Opaque_Geometry* component) 187545788340 q components.push_back(component); 1879bool Compound_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 18810 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 18830 if ((*it)->relevant_to_bbox(bbox)) 1891Opaque_Geometry* Partial_Relation_Geometry::clone() const 1893504 q std::vector< Opaque_Geometry* > cloned; 189471324 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 189570824 q cloned.push_back((*it)->clone()); 1896504 q return new Partial_Relation_Geometry(cloned); 1900bool Partial_Relation_Geometry::has_center() const 19064806 q bounds = calc_bounds(components); 1912double Partial_Relation_Geometry::center_lat() const 19180 bounds = calc_bounds(components); 1920165 q return bounds->center_lat(); 1924double Partial_Relation_Geometry::center_lon() const 19300 bounds = calc_bounds(components); 19324764 q return bounds->center_lon(); 1936bool Partial_Relation_Geometry::has_bbox() const 19422126 q bounds = calc_bounds(components); 1948double Partial_Relation_Geometry::south() const 19540 bounds = calc_bounds(components); 1960double Partial_Relation_Geometry::north() const 19660 bounds = calc_bounds(components); 1972double Partial_Relation_Geometry::west() const 19780 bounds = calc_bounds(components); 1984double Partial_Relation_Geometry::east() const 19900 bounds = calc_bounds(components); 1996bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos) const 19987306 q return (member_pos < components.size() && components[member_pos] 19997306 q && components[member_pos]->has_center()); 2003double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos) const 20051653 q if (member_pos < components.size() && components[member_pos]) 20061653 q return components[member_pos]->center_lat(); 2011double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos) const 20131653 q if (member_pos < components.size() && components[member_pos]) 20141653 q return components[member_pos]->center_lon(); 2019unsigned int Partial_Relation_Geometry::relation_way_size(unsigned int member_pos) const 20212208118 q if (member_pos < components.size() && components[member_pos]) 20222208118 q return components[member_pos]->way_size(); 2027bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const 20291283966 q return (member_pos < components.size() && components[member_pos] 20301283966 q && components[member_pos]->way_pos_is_valid(nd_pos)); 2034double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const 20361045746 q if (member_pos < components.size() && components[member_pos]) 20371045746 q return components[member_pos]->way_pos_lat(nd_pos); 2042double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const 20441045746 q if (member_pos < components.size() && components[member_pos]) 20451045746 q return components[member_pos]->way_pos_lon(nd_pos); 2050void Partial_Relation_Geometry::add_placeholder() 205221259119 q components.push_back(new Null_Geometry()); 2056void Partial_Relation_Geometry::add_point(const Point_Double& point) 206353812 q components.push_back(new Point_Geometry(point.lat, point.lon)); 20660 components.push_back(new Null_Geometry()); 2070void Partial_Relation_Geometry::start_way() 20724056418 q components.push_back(new Partial_Way_Geometry()); 2076void Partial_Relation_Geometry::add_way_point(const Point_Double& point) 208162910116 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back()); 2090void Partial_Relation_Geometry::add_way_placeholder() 20921150310 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back()); 20941150310 q geom->add_point(Point_Double(100., 200.)); 2098bool Partial_Relation_Geometry::relevant_to_bbox(const Bbox_Double& bbox) const 21000 for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 21020 if ((*it)->relevant_to_bbox(bbox)) 2110double great_circle_dist(double lat1, double lon1, double lat2, double lon2) 21121400417673 q if (lat1 == lat2 && lon1 == lon2) 21141400414973 q static const double deg_to_arc = acos(0)/90.; 21151400414973 q double scalar_prod = cos((lat2-lat1)*deg_to_arc) 21161400414973 q + cos(lat1*deg_to_arc)*cos(lat2*deg_to_arc)*(cos((lon2-lon1)*deg_to_arc) - 1); 21191400414973 q static const double arc_to_meter = 10*1000*1000/acos(0); 21201400414973 q return acos(scalar_prod)*arc_to_meter; 2124double length(const Opaque_Geometry& geometry) 21285711113 q if (geometry.has_components()) 213010906 q const std::vector< Opaque_Geometry* >* components = geometry.get_components(); 2131535456 q for (std::vector< Opaque_Geometry* >::const_iterator it = components->begin(); it != components->end(); ++it) 2132524556 q result += (*it ? length(**it) : 0); 21345602113 q else if (geometry.has_line_geometry()) 21362906013 q const std::vector< Point_Double >* line_geometry = geometry.get_line_geometry(); 213782226613 q for (unsigned int i = 1; i < line_geometry->size(); ++i) 213879320613 q result += great_circle_dist((*line_geometry)[i-1].lat, (*line_geometry)[i-1].lon, 213979320613 q (*line_geometry)[i].lat, (*line_geometry)[i].lon); 2141269615 q else if (geometry.has_faithful_way_geometry()) 2143731 q for (unsigned int i = 1; i < geometry.way_size(); ++i) 21440 result += (geometry.way_pos_is_valid(i-1) && geometry.way_pos_is_valid(i) ? 21450 great_circle_dist(geometry.way_pos_lat(i-1), geometry.way_pos_lon(i-1), 21460 geometry.way_pos_lat(i), geometry.way_pos_lon(i)) : 0); 2153void collect_components( 2154 std::vector< Point_Double >& nodes, std::vector< std::vector< Point_Double > >& linestrings, 2155 const Opaque_Geometry& geometry) 2157433938 q if (geometry.has_components()) 2159454 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components(); 2160407994 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 2163407544 q collect_components(nodes, linestrings, **it); 2166433488 q else if (geometry.has_line_geometry()) 2167227175 q linestrings.push_back(*geometry.get_line_geometry()); 2168206315 q else if (geometry.has_multiline_geometry()) 21700 const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry(); 21710 for (std::vector< std::vector< Point_Double > >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it) 21720 linestrings.push_back(*it); 2174206315 q else if (geometry.has_center()) 2175206074 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon())); 2179struct Linestring_Geometry_Ptr 2181692035 q Linestring_Geometry_Ptr(Linestring_Geometry* ptr_) : ptr(ptr_) {} 2182 bool operator<(Linestring_Geometry_Ptr rhs) const; 2185 Linestring_Geometry* ptr; 2189bool Linestring_Geometry_Ptr::operator<(Linestring_Geometry_Ptr rhs_) const 219611151184 q const std::vector< Point_Double >& lhs = *ptr->get_line_geometry(); 219711151184 q const std::vector< Point_Double >& rhs = *rhs_.ptr->get_line_geometry(); 219911151184 q if (lhs.size() != rhs.size()) 2200852824 q return lhs.size() < rhs.size(); 220410298363 q if (lhs.back() < lhs.front()) 22064312552 q l_start = lhs.size()-1; 221210298363 q if (rhs.back() < rhs.front()) 22145359612 q r_start = rhs.size()-1; 221811083703 q for (int i = 0; i < (int)lhs.size(); ++i) 222010749443 q if (lhs[l_start + l_factor*i] != rhs[r_start + r_factor*i]) 22219964103 q return lhs[l_start + l_factor*i] < rhs[r_start + r_factor*i]; 2227Opaque_Geometry* make_trace(const Opaque_Geometry& geometry) 222926398 q std::vector< Point_Double > nodes; 223026398 q std::vector< std::vector< Point_Double > > linestrings; 223126398 q collect_components(nodes, linestrings, geometry); 223326398 q std::sort(nodes.begin(), nodes.end()); 223426398 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end()); 223626398 q std::map< Point_Double, uint > coord_count; 2237232168 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 224026398 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin(); 2241253568 q lit != linestrings.end(); ++lit) 2245227175 q coord_count[lit->front()] += 2; 2246227175 q coord_count[lit->back()] += 2; 22472515265 q for (uint i = 1; i < lit->size() - 1; ++i) 22482288095 q ++coord_count[(*lit)[i]]; 225126398 q Compound_Geometry* result = new Compound_Geometry(); 2253232168 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 2254205774 q result->add_component(new Point_Geometry(it->lat, it->lon)); 225626398 q std::set< Linestring_Geometry_Ptr > lstrs; 225726398 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin(); 2258253568 q lit != linestrings.end(); ++lit) 2263227175 q std::vector< Point_Double > points; 2264227175 q points.push_back(lit->front()); 22652515265 q for (uint i = 1; i < lit->size() - 1; ++i) 22672288095 q if (coord_count[(*lit)[i]] > 1) 2269464863 q points.push_back((*lit)[i]); 2271464863 q Linestring_Geometry* lstr = new Linestring_Geometry(points); 2272464863 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second) 2273309323 q result->add_component(lstr); 22792288095 q points.push_back((*lit)[i]); 2281227175 q points.push_back(lit->back()); 2282227175 q Linestring_Geometry* lstr = new Linestring_Geometry(points); 2283227175 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second) 2284215585 q result->add_component(lstr); 2293void collect_components(std::vector< Point_Double >& nodes, const Opaque_Geometry& geometry) 229567309 q if (geometry.has_components()) 2297364 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components(); 229814474 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 230114114 q collect_components(nodes, **it); 230466949 q else if (geometry.has_line_geometry()) 230659007 q const std::vector< Point_Double >& lstrs = *geometry.get_line_geometry(); 23071870647 q for (std::vector< Point_Double >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it) 23081811647 q nodes.push_back(*it); 23107944 q else if (geometry.has_multiline_geometry()) 231291 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry(); 231391 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = lstrs.begin(); lit != lstrs.end(); ++lit) 23150 for (std::vector< Point_Double >::const_iterator it = lit->begin(); it != lit->end(); ++it) 23160 nodes.push_back(*it); 23197854 q else if (geometry.has_center()) 23201383 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon())); 2324struct Point_Double_By_Lon 2326 bool operator()(const Point_Double& lhs, const Point_Double& rhs) 232812758547 q if (lhs.lon != rhs.lon) 232912723347 q return lhs.lon < rhs.lon; 233035207 q return lhs.lat < rhs.lat; 2335struct Spherical_Vector 2337963957 q Spherical_Vector() : x(0), y(0), z(0) {} 23391882567 q Spherical_Vector(const Point_Double& pt) : x(sin(pt.lat*deg_to_arc())) 23411882567 q double cos_lat = cos(pt.lat*deg_to_arc()); 23421882567 q y = cos_lat*sin(pt.lon*deg_to_arc()); 23431882567 q z = cos_lat*cos(pt.lon*deg_to_arc()); 2346 Spherical_Vector(const Spherical_Vector& lhs, const Spherical_Vector& rhs) 23471832467 q : x(lhs.y*rhs.z - lhs.z*rhs.y), y(lhs.z*rhs.x - lhs.x*rhs.z), z(lhs.x*rhs.y - lhs.y*rhs.x) 23491832467 q double length = sqrt(x*x + y*y + z*z); 23590 double operator==(const Spherical_Vector& rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z; } 236021868267 q double operator*(const Spherical_Vector& rhs) const { return x*rhs.x + y*rhs.y + z*rhs.z; } 2362 static double deg_to_arc() 23647530247 q static const double result = acos(0)/90.; 237495447 q Hull_Segment(const Point_Double& pt) : ll_pt(pt), s_pt(pt) {} 2375868517 q Hull_Segment(const Point_Double& pt, const Spherical_Vector& s) : ll_pt(pt), s_pt(s) {} 2378 Spherical_Vector s_pt; 2379 Spherical_Vector edge; 2382 Proto_Hull(const Point_Double& s, const Point_Double& w, const Point_Double& n, const Point_Double& e); 2383 void enhance(const Point_Double& rhs); 2384 std::vector< Point_Double > get_line_geometry() const; 2387 std::vector< Hull_Segment > segments; 2391Proto_Hull::Proto_Hull(const Point_Double& s, const Point_Double& w, const Point_Double& n, const Point_Double& e) 239547727 q segments.push_back(Hull_Segment(w)); 239647727 q segments.push_back(Hull_Segment(e)); 23980 else if (n.lat != s.lat) 24000 segments.push_back(Hull_Segment(s)); 24010 segments.push_back(Hull_Segment(n)); 240647727 q segments[1].edge = Spherical_Vector(segments[0].s_pt, segments[1].s_pt); 240747727 q segments[0].edge = Spherical_Vector(segments[1].s_pt, segments[0].s_pt); 2412void Proto_Hull::enhance(const Point_Double& rhs) 24141787127 q Spherical_Vector s_pt(rhs); 24161787127 q std::vector< Hull_Segment >::iterator from_it = segments.begin(); 241717985787 q while (from_it != segments.end() && from_it->edge*s_pt < 1e-8) 24201787127 q if (from_it != segments.end() && from_it->edge*s_pt >= 1e-8) 2422868517 q if (segments.begin()->edge*s_pt >= 0) 2424422137 q from_it = segments.end(); 2426422137 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0) 2429422137 q segments.erase(from_it, segments.end()); 2431422137 q std::vector< Hull_Segment >::iterator to_it = segments.begin(); 24321081647 q while (to_it != segments.end() && to_it->edge*s_pt >= 0) 2435422137 q from_it = segments.begin(); 2437422137 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt); 2439217817 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt)); 2443204327 q from_it = segments.erase(from_it, to_it); 2445204327 q *from_it = Hull_Segment(rhs, s_pt); 2450446387 q std::vector< Hull_Segment >::iterator to_it = from_it; 24511141277 q while (to_it != segments.end() && to_it->edge*s_pt >= 0) 2454910777 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0) 2458446387 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt); 2460219027 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt)); 2464227366 q from_it = segments.erase(from_it, to_it); 2466227366 q *from_it = Hull_Segment(rhs, s_pt); 2470868517 q if (from_it == segments.begin()) 2471422137 q from_it->edge = Spherical_Vector(segments.back().s_pt, s_pt); 2473446387 q from_it->edge = Spherical_Vector((from_it-1)->s_pt, s_pt); 2478std::vector< Point_Double > Proto_Hull::get_line_geometry() const 248047727 q std::vector< Point_Double > result; 248147727 q result.reserve(segments.size()); 2482507777 q for (std::vector< Hull_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it) 2483460057 q result.push_back(it->ll_pt); 2488Opaque_Geometry* make_hull(const Opaque_Geometry& geometry) 249053199 q std::vector< Point_Double > nodes; 249153199 q collect_components(nodes, geometry); 249353199 q std::sort(nodes.begin(), nodes.end(), Point_Double_By_Lon()); 249453199 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end()); 249711 q return new Null_Geometry(); 2499241 q return new Point_Geometry(nodes.front().lat, nodes.front().lon); 25025221 q nodes.push_back(nodes.front()); 25035221 q return new Linestring_Geometry(nodes); 250647727 q Point_Double min_lat(100., 0.); 250747727 q Point_Double max_lat(-100., 0.); 25081834847 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 25101787127 q if (it->lat < min_lat.lat) 25121787127 q if (it->lat > max_lat.lat) 251647727 q double max_lon_delta = nodes.front().lon + 360. - nodes.back().lon; 251747727 q Point_Double west_end = nodes.front(); 251847727 q Point_Double east_end = nodes.back(); 25191787127 q for (uint i = 1; i < nodes.size(); ++i) 25211739407 q if (max_lon_delta < nodes[i].lon - nodes[i-1].lon) 25230 max_lon_delta = nodes[i].lon - nodes[i-1].lon; 25240 west_end = nodes[i]; 25250 east_end = nodes[i-1]; 252947727 q Proto_Hull proto_hull(min_lat, west_end, max_lat, east_end); 25301834847 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 25311787127 q proto_hull.enhance(*it); 253347727 q std::vector< Point_Double > line_geom = proto_hull.get_line_geometry(); 253447727 q if (line_geom.size() < 3) 25351501 q return new Linestring_Geometry(line_geom); 253746227 q return new RHR_Polygon_Geometry(Free_Polygon_Geometry( 253846227 q std::vector< std::vector< Point_Double > >(1, line_geom)));