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 338985523 q return (south + north) / 2; 37double Bbox_Double::center_lon() const 409254424 q return (west + east) / 2; 4121 q else if ((west + east) / 2 <= 0) 4221 q return (west + east) / 2 + 180.0; 440 return (west + east) / 2 - 180.0; 48bool Bbox_Double::contains(const Point_Double& point) const 501392152 q if (point.lat + 5e-8 < south || point.lat - 5e-8 > north) 541143552 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 617041842143 q double from_lon = from.lon; 627041842143 q double to_lon = to.lon; 637041842143 q if (from.lon < 0. && to.lon - from.lon > 180.) 657041842143 q if (to.lon < 0. && from.lon - to.lon > 180.) 687041842143 q double delta_from = 0; 697041842143 q if (from.lat + 5e-8 < south) 712876626128 q if (to.lat + 5e-8 < south) 736957118 q if (to.lat - 5e-8 > from.lat) 746957118 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 774165216142 q else if (from.lat - 5e-8 > north) 791210628129 q if (to.lat - 5e-8 > north) 816309120 q if (to.lat + 5e-8 < from.lat) 826309120 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 862967854139 q if (to.lat + 5e-8 < south && from.lat - 5e-8 > to.lat) 87 // Adjust to.lat and to.lon 883386116 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 902964468139 q else if (to.lat - 5e-8 > north && from.lat + 5e-8 < to.lat) 91 // Adjust to.lat and to.lon 922955114 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 942967854139 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. 1022967854139 q double min_lon = std::min(from_lon, to_lon); 1032967854139 q double max_lon = std::max(from_lon, to_lon); 1072967854139 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) 13158764536 q double south = 100.0; 13358764536 q double north = -100.0; 13458764536 q double east = -200.0; 136541922036 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end(); ++it) 140482758736 q south = std::min(south, it->lat); 141482758736 q west = std::min(west, it->lon); 142482758736 q north = std::max(north, it->lat); 143482758736 q east = std::max(east, it->lon); 1480 return new Bbox_Double(Bbox_Double::invalid); 14958764536 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); 17358764536 q return new Bbox_Double(south, west, north, east); 177double Linestring_Geometry::center_lat() const 1808266511 q bounds = calc_bounds(points); 1828374811 q return bounds->center_lat(); 186double Linestring_Geometry::center_lon() const 189776675 q bounds = calc_bounds(points); 1918610312 q return bounds->center_lon(); 195double Linestring_Geometry::south() const 19834242024 q bounds = calc_bounds(points); 20034405525 q return bounds->south; 204double Linestring_Geometry::north() const 2070 bounds = calc_bounds(points); 20934351025 q return bounds->north; 213double Linestring_Geometry::west() const 2165451 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 2798434813 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_) 3221812944 q : points(points_), bounds(0), has_coords(false) 3243625884 q for (std::vector< Point_Double >::const_iterator it = points.begin(); it != points.end() && !has_coords; ++it) 3251812944 q has_coords |= (it->lat < 100.); 3291798544 q valid_segments.push_back(std::vector< Point_Double >()); 3301798544 q if (!points.empty() && points.front().lat < 100.) 3311798544 q valid_segments.back().push_back(points.front()); 33210750474 q for (unsigned int i = 1; i < points.size(); ++i) 3348951934 q if (points[i].lat < 100.) 3368951934 q if (points[i-1].lat >= 100.) 3370 valid_segments.push_back(std::vector< Point_Double >()); 3388951934 q valid_segments.back().push_back(points[i]); 344void Partial_Way_Geometry::add_point(const Point_Double& point) 348228632521 q if (point.lat < 100.) 350219014218 q if (points.empty() || (points.back().lat >= 100.)) 35135196018 q valid_segments.push_back(std::vector< Point_Double >()); 352219014218 q valid_segments.back().push_back(point); 355228632521 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) 3791272 q double south = 100.0; 3801272 q double west = 200.0; 3811272 q double north = -100.0; 3821272 q double east = -200.0; 3841272 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = linestrings.begin(); 3852542 q iti != linestrings.end(); ++iti) 38715152 q for (std::vector< Point_Double >::const_iterator it = iti->begin(); it != iti->end(); ++it) 38913882 q if (it->lat < 100.) 39113882 q south = std::min(south, it->lat); 39213882 q west = std::min(west, it->lon); 39313882 q north = std::max(north, it->lat); 39413882 q east = std::max(east, it->lon); 3991272 q if (north == -100.0) 4000 return new Bbox_Double(Bbox_Double::invalid); 4011272 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); 4291272 q return new Bbox_Double(south, west, north, east); 4333925 qFree_Polygon_Geometry::Free_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_) : linestrings(linestrings_), bounds(0) 4357845 q for (std::vector< std::vector< Point_Double > >::iterator it = linestrings.begin(); it != linestrings.end(); 4383925 q if (it->front() != it->back()) 4393925 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) 500602183 q if (linestring.size() < 2) 505601632 q linestrings.push_back(linestring); 506601632 q if (linestrings.back().front() != linestrings.back().back()) 507601211 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) 5141825877 q if (pt.lon < -179.9) 5160 lhs.lon -= lhs.lon > 0 ? 360. : 0.; 5170 rhs.lon -= rhs.lon > 0 ? 360. : 0.; 5191825877 q else if (pt.lon > 179.9) 5210 lhs.lon += lhs.lon < 0 ? 360. : 0.; 5220 rhs.lon += rhs.lon < 0 ? 360. : 0.; 5251825877 q if (lhs.lat == rhs.lat) 527601643 q if (lhs.lat < pt.lat) 5280 is_inside ^= ((lhs.lon - pt.lon)*(rhs.lon - pt.lon) < 0); 529601643 q else if (lhs.lat == pt.lat) 531601643 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 5381224237 q else if (lhs.lon == rhs.lon) 540601632 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 542601211 q else if (pt.lon == lhs.lon && (pt.lat - lhs.lat)*(pt.lat - rhs.lat) < 0) 544601211 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 548622607 q else if ((lhs.lat - rhs.lat)*(lhs.lon - rhs.lon) < 0) 549 // The segment runs from nw to se or se to nw 55111505 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 5537285 q else if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0) 5551384 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon); 5561384 q if (isect_lat < pt.lat) 557964 q is_inside = !is_inside; 558423 q else if (isect_lat == pt.lat) 5615905 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon) 5630 if (lhs.lat < pt.lat) 5640 is_inside = !is_inside; 5665905 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon) 5680 if (rhs.lat < pt.lat) 5690 is_inside = !is_inside; 5715905 q else if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0) 573213 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat); 574213 q if (border_lon < isect_lon && isect_lon < pt.lon) 57532 q is_inside = !is_inside; 579 // The segment runs from sw to ne or ne to sw 581611107 q if ((border_lat - lhs.lat)*(border_lat - rhs.lat) < 0) 583304 q double isect_lon = lhs.lon + (rhs.lon - lhs.lon)*(border_lat - lhs.lat)/(rhs.lat - lhs.lat); 584304 q if ((lhs.lon <= pt.lon || rhs.lon <= pt.lon) && (border_lon < isect_lon)) 585194 q is_inside = !is_inside; 587611107 q if ((pt.lat == lhs.lat && pt.lon == lhs.lon) || (pt.lat == rhs.lat && pt.lon == rhs.lon)) 5936286 q if ((pt.lon - lhs.lon)*(pt.lon - rhs.lon) < 0) 595894 q double isect_lat = lhs.lat + (rhs.lat - lhs.lat)*(pt.lon - lhs.lon)/(rhs.lon - lhs.lon); 596894 q if (isect_lat < pt.lat) 597504 q is_inside = !is_inside; 598392 q else if (isect_lat == pt.lat) 6015396 q else if (pt.lon == lhs.lon && rhs.lon < pt.lon) 6030 if (lhs.lat < pt.lat) 6040 is_inside = !is_inside; 6065396 q else if (pt.lon == rhs.lon && lhs.lon < pt.lon) 6080 if (rhs.lat < pt.lat) 6090 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) 6454984 q static const double deg_to_arc = acos(0)/90.; 6474984 q double sin_factor = sin(factor); 6484984 q double cos_factor = cos(factor); 6494984 q double new_x = sin_factor * orth_x + cos_factor * lhs_gc_x; 6504984 q double new_y = sin_factor * orth_y; 6514984 q double new_z = sin_factor * orth_z + cos_factor * lhs_gc_z; 6534984 q double new_lat = asin(new_x)/deg_to_arc; 6544984 q double new_lon = atan2(new_y, new_z)/deg_to_arc + lhs_lon; 6554984 q if (new_lon < -180.) 6574984 q else if (new_lon > 180.) 6604984 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_) 692534 q : orth_x(orth_x_), orth_y(orth_y_), orth_z(orth_z_), 693534 q lhs_gc_x(lhs_gc_x_), lhs_gc_z(lhs_gc_z_), lhs_lon(lhs_lon_), 694534 q center(lhs_gc_x == 0 ? dist : atan(orth_x/lhs_gc_x)), 695534 q ex_pt(interpolation_point(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, 696534 q std::max(std::min(center, dist), 0.), lhs_lon)), 697534 q ex_gc_pt_lat(ex_pt.lat), 700534 q static const double deg_to_arc = acos(0)/90.; 701534 q acceptable_max_length = .0065536*cos(ex_pt.lat * deg_to_arc); 703534 q double bounded_center = std::max(std::min(center, dist), 0.); 704534 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; 710534 q center = bounded_center; 714 void Interpolation_Collector::collect_single_point(double dist) 7160 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) 7221064 q int num_sections = (int)((to - from)/max_step)+1; 7235514 q for (int j = 1; j < num_sections; ++j) 7244454 q target->push_back(interpolation_point(orth_x, orth_y, orth_z, 7254454 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) 731534 q static const double deg_to_arc = acos(0)/90.; 733534 q double max_length_threshold = .0065536/divisor; 734534 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); 752534 q if (acceptable_max_length < 1e-9) 7530 acceptable_max_length = 1e-9; 754534 q acceptable_max_length *= deg_to_arc; 756534 q collect_sequence(from, center, acceptable_max_length); 757534 q if (center > from && center < to) 7580 collect_single_point(center); 759534 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) 7681829217 q static const double deg_to_arc = acos(0)/90.; 7701829217 q if (fabs(rhs_lat - lhs_lat) < .0065536 && fabs(rhs_lon - lhs_lon) < .0065536) 7711828687 q target.push_back(Point_Double(rhs_lat, rhs_lon)); 774534 q if (fabs(rhs_lon - lhs_lon) > 180.) 7750 rhs_lon = (lhs_lon < 0 ? rhs_lon - 360. : rhs_lon + 360.); 777534 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)); 797534 q double lhs_cos = cos(lhs_lat * deg_to_arc); 798534 q double lhs_gc_x = sin(lhs_lat * deg_to_arc); 799534 q double lhs_gc_z = lhs_cos; 801534 q double rhs_cos = cos(rhs_lat * deg_to_arc); 802534 q double rhs_gc_x = sin(rhs_lat * deg_to_arc); 803534 q double rhs_gc_y = sin((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos; 804534 q double rhs_gc_z = cos((rhs_lon - lhs_lon) * deg_to_arc) * rhs_cos; 806534 q double prod = lhs_gc_x * rhs_gc_x + lhs_gc_z * rhs_gc_z; 807534 q double dist = acos(prod); 809534 q double orth_x = rhs_gc_x - prod * lhs_gc_x; 810534 q double orth_y = rhs_gc_y; 811534 q double orth_z = rhs_gc_z - prod * lhs_gc_z; 812534 q double lg_orth = sqrt(orth_x * orth_x + orth_y * orth_y + orth_z * orth_z); 817534 q Interpolation_Collector collector(orth_x, orth_y, orth_z, lhs_gc_x, lhs_gc_z, dist, lhs_lon, target); 818534 q collector.collect_center(0, dist, 4); 823534 q else if (rhs_lon > 180.) 825534 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) 8331833667 q uint32 lhs_ilat = ::ilat(from.lat); 8341833667 q int32 lhs_ilon = ::ilon(from.lon); 8351833667 q uint32 rhs_ilat = ::ilat(to.lat); 8361833667 q int32 rhs_ilon = ::ilon(to.lon); 8381833667 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos); 8391833667 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 8405504 q segments_per_idx[(lhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)].push_back(pos); 8411833667 q if ((lhs_ilat & 0xffff0000) != (rhs_ilat & 0xffff0000)) 8432524 q segments_per_idx[(rhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)].push_back(pos); 8442524 q if ((lhs_ilon & 0xffff0000) != (rhs_ilon & 0xffff0000)) 8451384 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) 9041889457 q double rfmlt_lat = rhs_from.lat - lhs_to.lat; 9051889457 q double rfmlt_lon = rhs_from.lon - lhs_to.lon; 907 //The two segments are connected by a vertex 9081889457 q if (!rfmlt_lat && !rfmlt_lon) 910660967 q if (lhs_from == rhs_to || lhs_from == rhs_from || lhs_to == rhs_to) 91355355 q double lfmlt_lat = lhs_from.lat - lhs_to.lat; 91455355 q double lfmlt_lon = lhs_from.lon - lhs_to.lon; 91555355 q double rfmrt_lat = rhs_from.lat - rhs_to.lat; 91655355 q double rfmrt_lon = rhs_from.lon - rhs_to.lon; 91755355 q double det = lfmlt_lat * rfmrt_lon - rfmrt_lat * lfmlt_lon; 921 // Segments on parallel but distinct beams 922242 q if (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat != 0) 9250 if (fabs(rfmlt_lat) > fabs(rfmlt_lon)) 9270 if (lhs_from.lat < lhs_to.lat) 9290 if (rhs_from.lat < rhs_to.lat) 931 // Segments are non-overlapping 9320 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) 9800 if (lhs_from.lon < lhs_to.lon) 9820 if (rhs_from.lon < rhs_to.lon) 984 // Segments are non-overlapping 9850 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) 103455115 q double x = (rfmrt_lon * rfmlt_lat - rfmrt_lat * rfmlt_lon)/det; 103555115 q double y = (lfmlt_lat * rfmlt_lon - lfmlt_lon * rfmlt_lat)/det; 103755115 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_) 10493667327 q : pt(pt_), idx(idx_), remote_idx(remote_idx_) {} 1053 unsigned int remote_idx; 1055 bool operator<(const Idx_Per_Point_Double& rhs) const 10578598837 q if (pt.lat != rhs.pt.lat) 10583141367 q return pt.lat < rhs.pt.lat; 10595457477 q if (pt.lon != rhs.pt.lon) 10603006503 q return pt.lon < rhs.pt.lon; 10622450977 q return idx < rhs.idx; 1067struct Line_Divertion 10696089527 q Line_Divertion() : to_idx(std::numeric_limits< uint >::max()), 10706089527 q to_remote_idx(std::numeric_limits< uint >::max()) {} 10713667327 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) 1083606108 q for (std::map< uint32, std::vector< unsigned int > >::iterator idx_it = segments_per_idx.begin(); 10841219488 q idx_it != segments_per_idx.end(); ++idx_it) 1086613387 q Point_Double isect(100, 0); 10871843067 q for (unsigned int i = 1; i < idx_it->second.size(); ++i) 10893119137 q for (unsigned int j = 0; j < i; ++j) 10911889457 q if (try_intersect(all_segments[idx_it->second[j]], all_segments[idx_it->second[j]+1], 10921889457 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) 11523667327 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) 1160613387 q std::vector< Idx_Per_Point_Double > pos_per_pt; 1162613387 q for (std::vector< unsigned int >::const_iterator seg_it = segments.begin(); 11632456447 q seg_it != segments.end(); ++seg_it) 11651843067 q uint32 lhs_ilat = ::ilat(all_segments[*seg_it].lat); 11661843067 q int32 lhs_ilon = ::ilon(all_segments[*seg_it].lon); 11671843067 q if (((lhs_ilat & 0xffff0000) | (uint32(lhs_ilon)>>16)) == idx) 11681833667 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it], *seg_it, *seg_it+1)); 11701843067 q uint32 rhs_ilat = ::ilat(all_segments[*seg_it+1].lat); 11711843067 q int32 rhs_ilon = ::ilon(all_segments[*seg_it+1].lon); 11721843067 q if (((rhs_ilat & 0xffff0000) | (uint32(rhs_ilon)>>16)) == idx) 11731833667 q pos_per_pt.push_back(Idx_Per_Point_Double(all_segments[*seg_it+1], *seg_it+1, *seg_it)); 1176613387 q if (pos_per_pt.empty()) 1179610947 q std::sort(pos_per_pt.begin(), pos_per_pt.end()); 1181610947 q unsigned int same_since = 0; 11834889207 q while (i <= pos_per_pt.size()) 11854278267 q if (i == pos_per_pt.size() || pos_per_pt[i].pt != pos_per_pt[same_since].pt) 11871833667 q if (i - same_since == 2) 11891833667 q divertions[line_divertion_idx(pos_per_pt[same_since])] = Line_Divertion(pos_per_pt[same_since+1]); 11901833667 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 1241606108 q unsigned int count = 0; 12423045318 q for (unsigned int i = 0; i < divertions.size(); i += 2) 12432439217 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() 12461211658 q while (count < all_segments.size()) 1248605557 q while (divertions[2*pos].to_idx == std::numeric_limits< uint >::max()) 12510 if (pos == all_segments.size()-1) 1255605557 q linestrings.push_back(std::vector< Point_Double >()); 1256605557 q std::vector< Point_Double >& cur_target = linestrings.back(); 12592439217 q while (divertions[2*pos + dir].to_idx != std::numeric_limits< uint >::max()) 12611833667 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(); 12641833667 q else if (cur_target.empty() || !all_segments[pos].epsilon_equal(cur_target.back())) 12651833667 q cur_target.push_back(all_segments[pos]); 12671833667 q Line_Divertion& old_div = divertions[2*(int)pos + dir]; 12681833667 q Line_Divertion& divertion = divertions[2*(int)pos + (3*dir-1)/2]; 12691833667 q dir = divertion.to_remote_idx - divertion.to_idx; 12701833667 q pos = divertion.to_idx; 12731833667 q old_div.to_idx = std::numeric_limits< uint >::max(); 1277605557 q if (pos == all_segments.size()-1) 1280605557 q if (cur_target.size() <= 2) 12810 linestrings.pop_back(); 1283605557 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_) 1293606108 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) 1308613387 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value); 1309613387 q if (spi_it == segments_per_idx->end()) 13133174 q inside_corners.insert(value); 1315613387 q if (::lon(uint32(value)<<16) > -179.99) 13194796 q *nw_corner_inside = sw_corner_inside; 13214796 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 132220546 q seg_it != spi_it->second.end(); ++seg_it) 132415756 q int32 lhs_ilon = ::ilon((*all_segments)[*seg_it].lon) & 0xffff0000; 132515756 q int32 rhs_ilon = ::ilon((*all_segments)[*seg_it+1].lon) & 0xffff0000; 132715756 q if ((lhs_ilon < (value<<16) && rhs_ilon == (value<<16)) 132815756 q || (lhs_ilon == (value<<16) && rhs_ilon < (value<<16))) 13302354 q double isect_lat = (*all_segments)[*seg_it].lat 13312354 q + ((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat) 13322354 q *(::lon(uint32(value)<<16) - (*all_segments)[*seg_it].lon) 13332354 q /((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon); 13342354 q if ((::ilat(isect_lat) & 0xffff0000) == (value & 0xffff0000)) 13351934 q *nw_corner_inside = !*nw_corner_inside; 13428236 q *se_corner_inside = sw_corner_inside; 13448236 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 134534656 q seg_it != spi_it->second.end(); ++seg_it) 134726426 q uint32 lhs_ilat = ::ilat((*all_segments)[*seg_it].lat) & 0xffff0000; 134826426 q uint32 rhs_ilat = ::ilat((*all_segments)[*seg_it+1].lat) & 0xffff0000; 135026426 q if ((lhs_ilat < (value & 0xffff0000) && rhs_ilat == (value & 0xffff0000)) 135126426 q || (lhs_ilat == (value & 0xffff0000) && rhs_ilat < (value & 0xffff0000))) 13532634 q double isect_lon = (*all_segments)[*seg_it].lon 13542634 q + ((*all_segments)[*seg_it+1].lon - (*all_segments)[*seg_it].lon) 13552634 q *(::lat(value & 0xffff0000) - (*all_segments)[*seg_it].lat) 13562634 q /((*all_segments)[*seg_it+1].lat - (*all_segments)[*seg_it].lat); 13572634 q if ((::ilon(isect_lon) & 0xffff0000) == (uint32(value)<<16)) 13581744 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) 1444605557 q std::map< uint32, std::vector< unsigned int > >::const_iterator spi_it = segments_per_idx->find(value); 1445605557 q if (spi_it == segments_per_idx->end()) 1448605557 q double border_lon = std::max(::lon(value<<16), -180.); 1449605557 q double border_lat = ::lat(value & 0xffff0000); 1451605557 q bool on_vertex = false; 1452605557 q bool on_segment = false; 1453605557 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 1456605557 q for (std::vector< unsigned int >::const_iterator seg_it = spi_it->second.begin(); 14572431427 q seg_it != spi_it->second.end(); ++seg_it) 14581825877 q toggle_if_inside(is_inside, on_vertex, on_segment, border_lat, border_lon, Point_Double(lat, lon), 14591825877 q (*all_segments)[*seg_it], (*all_segments)[*seg_it+1]); 1462605557 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; 1488606108 qRHR_Polygon_Geometry::RHR_Polygon_Geometry(const Free_Polygon_Geometry& rhs) : bounds(0) 1490606108 q std::vector< std::vector< Point_Double > > input(*rhs.get_multiline_geometry()); 1492606108 q std::vector< Point_Double > all_segments; 1493606108 q std::vector< unsigned int > gap_positions; 1495606108 q gap_positions.push_back(0); 14961211658 q for (std::vector< std::vector< Point_Double > >::const_iterator iti = input.begin(); iti != input.end(); ++iti) 1498605557 q all_segments.push_back((*iti)[0]); 14992434767 q for (unsigned int i = 1; i < iti->size(); ++i) 15001829217 q interpolate_segment((*iti)[i-1].lat, (*iti)[i-1].lon, (*iti)[i].lat, (*iti)[i].lon, all_segments); 1501605557 q gap_positions.push_back(all_segments.size()); 1504606108 q std::map< uint32, std::vector< unsigned int > > segments_per_idx; 1505606108 q std::vector< unsigned int >::const_iterator gap_it = gap_positions.begin(); 15063045318 q for (unsigned int i = 0; i < all_segments.size(); ++i) 15111833667 q add_segment(segments_per_idx, all_segments[i-1], all_segments[i], i-1); 1513606108 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] 1517606108 q std::vector< Line_Divertion > divertions(2*all_segments.size()); 1519 // fill the gaps with invalid following segments 15201211658 q for (unsigned int i = 1; i < gap_positions.size(); ++i) 1522605557 q divertions[gap_positions[i]*2 - 2] = Line_Divertion(); 1523605557 q divertions[gap_positions[i]*2 - 1] = Line_Divertion(); 1526 // compute the real segment connections 1527606108 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin(); 15281219488 q idx_it != segments_per_idx.end(); ++idx_it) 1529613387 q collect_divertions(all_segments, idx_it->first, idx_it->second, divertions); 1531606108 q assemble_linestrings(all_segments, divertions, linestrings); 1533606108 q RHR_Polygon_Area_Oracle area_oracle(all_segments, segments_per_idx); 1534606108 q Four_Field_Index four_field_idx(&area_oracle); 1536606108 q for (std::map< uint32, std::vector< unsigned int > >::const_iterator idx_it = segments_per_idx.begin(); 15371219488 q idx_it != segments_per_idx.end(); ++idx_it) 1538613387 q four_field_idx.add_point(::lat(idx_it->first | 0x8000), ::lon(idx_it->first<<16 | 0x8000), idx_it->first); 1540606108 q four_field_idx.compute_inside_parts(); 1542606108 q for (std::vector< std::vector< Point_Double > >::iterator lstr_it = linestrings.begin(); 15431211658 q lstr_it != linestrings.end(); ++lstr_it) 1545605557 q if (lstr_it->size() > 2) 1547605557 q if (strictly_west_of((*lstr_it)[0].lon, (*lstr_it)[1].lon)) 1549602024 q if (weakly_west_of((*lstr_it)[1].lon, (*lstr_it)[2].lon)) 1551813 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()); 1556601211 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 1557601211 q ^ (((*lstr_it)[1].lat - (*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon) 1558601211 q < ((*lstr_it)[2].lat -(*lstr_it)[1].lat)/((*lstr_it)[2].lon -(*lstr_it)[1].lon))) 1559601211 q std::reverse(lstr_it->begin(), lstr_it->end()); 15623536 q else if (strictly_west_of((*lstr_it)[2].lon, (*lstr_it)[1].lon)) 15641605 q if (!(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3)) 156511 q std::reverse(lstr_it->begin(), lstr_it->end()); 15671934 q else if ((*lstr_it)[0].lon == (*lstr_it)[1].lon) 1569421 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 1570421 q ^ ((*lstr_it)[0].lat < (*lstr_it)[1].lat)) 1571421 q std::reverse(lstr_it->begin(), lstr_it->end()); 15731513 q else if ((*lstr_it)[2].lon == (*lstr_it)[1].lon) 15750 if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 15760 ^ ((*lstr_it)[1].lat < (*lstr_it)[2].lat)) 15770 std::reverse(lstr_it->begin(), lstr_it->end()); 15811513 q if (bool(four_field_idx.get_point_status((*lstr_it)[1].lat, (*lstr_it)[1].lon) & 0x3) 15821513 q ^ (((*lstr_it)[1].lat -(*lstr_it)[0].lat)/((*lstr_it)[1].lon -(*lstr_it)[0].lon) 15831513 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 15941272 q bounds = calc_bounds(linestrings); 15961272 q return bounds->center_lat(); 1600double RHR_Polygon_Geometry::center_lon() const 16030 bounds = calc_bounds(linestrings); 16051272 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 16857925619 q std::vector< Opaque_Geometry* > cloned; 168678803519 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 168770877918 q cloned.push_back((*it)->clone()); 16887925619 q return new Compound_Geometry(cloned); 1692Bbox_Double* calc_bounds(const std::vector< Opaque_Geometry* >& components) 1700104344627 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 170452103324 q south = std::min(south, (*it)->south()); 170552103324 q north = std::max(north, (*it)->north()); 170652103324 q west = std::min(west, (*it)->west()); 170752103324 q east = std::max(east, (*it)->east()); 170952103324 q wrapped |= ((*it)->east() < (*it)->west()); 171463 q return new Bbox_Double(Bbox_Double::invalid); 1715865224 q if (!wrapped && east - west <= 180.) 1716865224 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 1748729218 q bounds = calc_bounds(components); 1754double Compound_Geometry::center_lat() const 17570 bounds = calc_bounds(components); 1759582213 q return bounds->center_lat(); 1763double Compound_Geometry::center_lon() const 17660 bounds = calc_bounds(components); 1768614414 q return bounds->center_lon(); 1772bool Compound_Geometry::has_bbox() const 177510857 q bounds = calc_bounds(components); 177710857 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 181916704 q return (member_pos < components.size() && components[member_pos] 182016704 q && components[member_pos]->has_center()); 1824double Compound_Geometry::relation_pos_lat(unsigned int member_pos) const 182616704 q if (member_pos < components.size() && components[member_pos]) 182716704 q return components[member_pos]->center_lat(); 1832double Compound_Geometry::relation_pos_lon(unsigned int member_pos) const 183416704 q if (member_pos < components.size() && components[member_pos]) 183516704 q return components[member_pos]->center_lon(); 1840unsigned int Compound_Geometry::relation_way_size(unsigned int member_pos) const 18422930626 q if (member_pos < components.size() && components[member_pos]) 18432930626 q return components[member_pos]->way_size(); 1848bool Compound_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const 18501416796 q return (member_pos < components.size() && components[member_pos] 18511416796 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 18571408616 q if (member_pos < components.size() && components[member_pos]) 18581408616 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 18651408616 q if (member_pos < components.size() && components[member_pos]) 18661408616 q return components[member_pos]->way_pos_lon(nd_pos); 1871void Compound_Geometry::add_component(Opaque_Geometry* component) 1875117716236 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 18932614 q std::vector< Opaque_Geometry* > cloned; 18945634924 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 18955632314 q cloned.push_back((*it)->clone()); 18962614 q return new Partial_Relation_Geometry(cloned); 1900bool Partial_Relation_Geometry::has_center() const 19062107 q bounds = calc_bounds(components); 1912double Partial_Relation_Geometry::center_lat() const 19180 bounds = calc_bounds(components); 19201516 q return bounds->center_lat(); 1924double Partial_Relation_Geometry::center_lon() const 19300 bounds = calc_bounds(components); 19321656 q return bounds->center_lon(); 1936bool Partial_Relation_Geometry::has_bbox() const 1942715 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 1998477956 q return (member_pos < components.size() && components[member_pos] 1999477956 q && components[member_pos]->has_center()); 2003double Partial_Relation_Geometry::relation_pos_lat(unsigned int member_pos) const 20054454 q if (member_pos < components.size() && components[member_pos]) 20064454 q return components[member_pos]->center_lat(); 2011double Partial_Relation_Geometry::relation_pos_lon(unsigned int member_pos) const 20134454 q if (member_pos < components.size() && components[member_pos]) 20144454 q return components[member_pos]->center_lon(); 2019unsigned int Partial_Relation_Geometry::relation_way_size(unsigned int member_pos) const 20212825557 q if (member_pos < components.size() && components[member_pos]) 20222825557 q return components[member_pos]->way_size(); 2027bool Partial_Relation_Geometry::relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const 2029646095 q return (member_pos < components.size() && components[member_pos] 2030646095 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 2036540965 q if (member_pos < components.size() && components[member_pos]) 2037540965 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 2044540965 q if (member_pos < components.size() && components[member_pos]) 2045540965 q return components[member_pos]->way_pos_lon(nd_pos); 2050void Partial_Relation_Geometry::add_placeholder() 205288873320 q components.push_back(new Null_Geometry()); 2056void Partial_Relation_Geometry::add_point(const Point_Double& point) 2063531115 q components.push_back(new Point_Geometry(point.lat, point.lon)); 20660 components.push_back(new Null_Geometry()); 2070void Partial_Relation_Geometry::start_way() 207240275619 q components.push_back(new Partial_Way_Geometry()); 2076void Partial_Relation_Geometry::add_way_point(const Point_Double& point) 2081218897217 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back()); 2090void Partial_Relation_Geometry::add_way_placeholder() 20925152914 q Partial_Way_Geometry* geom = dynamic_cast< Partial_Way_Geometry* >(components.back()); 20945152914 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) 21121393273095 q if (lat1 == lat2 && lon1 == lon2) 21141393268895 q static const double deg_to_arc = acos(0)/90.; 21151393268895 q double scalar_prod = cos((lat2-lat1)*deg_to_arc) 21161393268895 q + cos(lat1*deg_to_arc)*cos(lat2*deg_to_arc)*(cos((lon2-lon1)*deg_to_arc) - 1); 21191393268895 q static const double arc_to_meter = 10*1000*1000/acos(0); 21201393268895 q return acos(scalar_prod)*arc_to_meter; 2124double length(const Opaque_Geometry& geometry) 21281622711 q if (geometry.has_components()) 21301706 q const std::vector< Opaque_Geometry* >* components = geometry.get_components(); 2131139506 q for (std::vector< Opaque_Geometry* >::const_iterator it = components->begin(); it != components->end(); ++it) 2132137806 q result += (*it ? length(**it) : 0); 21341605711 q else if (geometry.has_line_geometry()) 21361349111 q const std::vector< Point_Double >* line_geometry = geometry.get_line_geometry(); 213728984611 q for (unsigned int i = 1; i < line_geometry->size(); ++i) 213827635511 q result += great_circle_dist((*line_geometry)[i-1].lat, (*line_geometry)[i-1].lon, 213927635511 q (*line_geometry)[i].lat, (*line_geometry)[i].lon); 214125666 q else if (geometry.has_faithful_way_geometry()) 2143191 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) 21571003928 q if (geometry.has_components()) 21591583 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components(); 2160927103 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 2163925523 q collect_components(nodes, linestrings, **it); 21661002348 q else if (geometry.has_line_geometry()) 2167296624 q linestrings.push_back(*geometry.get_line_geometry()); 2168705726 q else if (geometry.has_multiline_geometry()) 2170931 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry(); 2171931 q for (std::vector< std::vector< Point_Double > >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it) 21720 linestrings.push_back(*it); 2174704796 q else if (geometry.has_center()) 2175417955 q nodes.push_back(Point_Double(geometry.center_lat(), geometry.center_lon())); 2179struct Linestring_Geometry_Ptr 21811437724 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 219625417152 q const std::vector< Point_Double >& lhs = *ptr->get_line_geometry(); 219725417152 q const std::vector< Point_Double >& rhs = *rhs_.ptr->get_line_geometry(); 219925417152 q if (lhs.size() != rhs.size()) 2200431952 q return lhs.size() < rhs.size(); 220424985202 q if (lhs.back() < lhs.front()) 220613149692 q l_start = lhs.size()-1; 221224985202 q if (rhs.back() < rhs.front()) 221415754112 q r_start = rhs.size()-1; 221828255122 q for (int i = 0; i < (int)lhs.size(); ++i) 222026742622 q if (lhs[l_start + l_factor*i] != rhs[r_start + r_factor*i]) 222123472702 q return lhs[l_start + l_factor*i] < rhs[r_start + r_factor*i]; 2227Opaque_Geometry* make_trace(const Opaque_Geometry& geometry) 222978408 q std::vector< Point_Double > nodes; 223078408 q std::vector< std::vector< Point_Double > > linestrings; 223178408 q collect_components(nodes, linestrings, geometry); 223378408 q std::sort(nodes.begin(), nodes.end()); 223478408 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end()); 223678408 q std::map< Point_Double, uint > coord_count; 2237483768 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 224078408 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin(); 2241375028 q lit != linestrings.end(); ++lit) 2245296624 q coord_count[lit->front()] += 2; 2246296624 q coord_count[lit->back()] += 2; 22471930574 q for (uint i = 1; i < lit->size() - 1; ++i) 22481633954 q ++coord_count[(*lit)[i]]; 225178408 q Compound_Geometry* result = new Compound_Geometry(); 2253483768 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 2254405365 q result->add_component(new Point_Geometry(it->lat, it->lon)); 225678408 q std::set< Linestring_Geometry_Ptr > lstrs; 225778408 q for (std::vector< std::vector< Point_Double > >::const_iterator lit = linestrings.begin(); 2258375028 q lit != linestrings.end(); ++lit) 2263296624 q std::vector< Point_Double > points; 2264296624 q points.push_back(lit->front()); 22651930574 q for (uint i = 1; i < lit->size() - 1; ++i) 22671633954 q if (coord_count[(*lit)[i]] > 1) 22691141101 q points.push_back((*lit)[i]); 22711141101 q Linestring_Geometry* lstr = new Linestring_Geometry(points); 22721141101 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second) 2273521841 q result->add_component(lstr); 22791633954 q points.push_back((*lit)[i]); 2281296624 q points.push_back(lit->back()); 2282296624 q Linestring_Geometry* lstr = new Linestring_Geometry(points); 2283296624 q if (lstrs.insert(Linestring_Geometry_Ptr(lstr)).second) 2284159634 q result->add_component(lstr); 2293void collect_components(std::vector< Point_Double >& nodes, const Opaque_Geometry& geometry) 229510988 q if (geometry.has_components()) 229752 q const std::vector< Opaque_Geometry* >& components = *geometry.get_components(); 22985692 q for (std::vector< Opaque_Geometry* >::const_iterator it = components.begin(); it != components.end(); ++it) 23015642 q collect_components(nodes, **it); 230410938 q else if (geometry.has_line_geometry()) 23067015 q const std::vector< Point_Double >& lstrs = *geometry.get_line_geometry(); 230784745 q for (std::vector< Point_Double >::const_iterator it = lstrs.begin(); it != lstrs.end(); ++it) 23103925 q else if (geometry.has_multiline_geometry()) 2312441 q const std::vector< std::vector< Point_Double > >& lstrs = *geometry.get_multiline_geometry(); 2313441 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); 23193485 q else if (geometry.has_center()) 23201223 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) 2328681695 q if (lhs.lon != rhs.lon) 2329675175 q return lhs.lon < rhs.lon; 23306525 q return lhs.lat < rhs.lat; 2335struct Spherical_Vector 233735735 q Spherical_Vector() : x(0), y(0), z(0) {} 233981785 q Spherical_Vector(const Point_Double& pt) : x(sin(pt.lat*deg_to_arc())) 234181785 q double cos_lat = cos(pt.lat*deg_to_arc()); 234281785 q y = cos_lat*sin(pt.lon*deg_to_arc()); 234381785 q z = cos_lat*cos(pt.lon*deg_to_arc()); 2346 Spherical_Vector(const Spherical_Vector& lhs, const Spherical_Vector& rhs) 234762785 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) 234962785 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; } 2360625215 q double operator*(const Spherical_Vector& rhs) const { return x*rhs.x + y*rhs.y + z*rhs.z; } 2362 static double deg_to_arc() 2364327125 q static const double result = acos(0)/90.; 23748685 q Hull_Segment(const Point_Double& pt) : ll_pt(pt), s_pt(pt) {} 237527055 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) 23954345 q segments.push_back(Hull_Segment(w)); 23964345 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)); 24064345 q segments[1].edge = Spherical_Vector(segments[0].s_pt, segments[1].s_pt); 24074345 q segments[0].edge = Spherical_Vector(segments[1].s_pt, segments[0].s_pt); 2412void Proto_Hull::enhance(const Point_Double& rhs) 241473105 q Spherical_Vector s_pt(rhs); 241673105 q std::vector< Hull_Segment >::iterator from_it = segments.begin(); 2417527515 q while (from_it != segments.end() && from_it->edge*s_pt < 1e-8) 242073105 q if (from_it != segments.end() && from_it->edge*s_pt >= 1e-8) 242227055 q if (segments.begin()->edge*s_pt >= 0) 242414775 q from_it = segments.end(); 242614775 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0) 242914775 q segments.erase(from_it, segments.end()); 243114775 q std::vector< Hull_Segment >::iterator to_it = segments.begin(); 243235545 q while (to_it != segments.end() && to_it->edge*s_pt >= 0) 243514775 q from_it = segments.begin(); 243714775 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt); 24399365 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt)); 24435414 q from_it = segments.erase(from_it, to_it); 24455414 q *from_it = Hull_Segment(rhs, s_pt); 245012285 q std::vector< Hull_Segment >::iterator to_it = from_it; 245128795 q while (to_it != segments.end() && to_it->edge*s_pt >= 0) 245424905 q while (from_it != segments.begin() && from_it->edge*s_pt >= 0) 245812285 q to_it->edge = Spherical_Vector(s_pt, to_it->s_pt); 24608375 q from_it = segments.insert(from_it, Hull_Segment(rhs, s_pt)); 24643915 q from_it = segments.erase(from_it, to_it); 24663915 q *from_it = Hull_Segment(rhs, s_pt); 247027055 q if (from_it == segments.begin()) 247114775 q from_it->edge = Spherical_Vector(segments.back().s_pt, s_pt); 247312285 q from_it->edge = Spherical_Vector((from_it-1)->s_pt, s_pt); 2478std::vector< Point_Double > Proto_Hull::get_line_geometry() const 24804345 q std::vector< Point_Double > result; 24814345 q result.reserve(segments.size()); 248229505 q for (std::vector< Hull_Segment >::const_iterator it = segments.begin(); it != segments.end(); ++it) 248325165 q result.push_back(it->ll_pt); 2488Opaque_Geometry* make_hull(const Opaque_Geometry& geometry) 24905348 q std::vector< Point_Double > nodes; 24915348 q collect_components(nodes, geometry); 24935348 q std::sort(nodes.begin(), nodes.end(), Point_Double_By_Lon()); 24945348 q nodes.erase(std::unique(nodes.begin(), nodes.end()), nodes.end()); 249742 q return new Null_Geometry(); 2499241 q return new Point_Geometry(nodes.front().lat, nodes.front().lon); 2502723 q nodes.push_back(nodes.front()); 2503723 q return new Linestring_Geometry(nodes); 25064345 q Point_Double min_lat(100., 0.); 25074345 q Point_Double max_lat(-100., 0.); 250877445 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 251073105 q if (it->lat < min_lat.lat) 251273105 q if (it->lat > max_lat.lat) 25164345 q double max_lon_delta = nodes.front().lon + 360. - nodes.back().lon; 25174345 q Point_Double west_end = nodes.front(); 25184345 q Point_Double east_end = nodes.back(); 251973105 q for (uint i = 1; i < nodes.size(); ++i) 252168765 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]; 25294345 q Proto_Hull proto_hull(min_lat, west_end, max_lat, east_end); 253077445 q for (std::vector< Point_Double >::const_iterator it = nodes.begin(); it != nodes.end(); ++it) 253173105 q proto_hull.enhance(*it); 25334345 q std::vector< Point_Double > line_geom = proto_hull.get_line_geometry(); 25344345 q if (line_geom.size() < 3) 2535423 q return new Linestring_Geometry(line_geom); 25373925 q return new RHR_Polygon_Geometry(Free_Polygon_Geometry( 25383925 q std::vector< std::vector< Point_Double > >(1, line_geom)));