Overpass API regression suite coverage (Nova Scotia) / overpass_api/core/geometry.h
overpass_api/core/geometry.h
159 of 275 instrumented lines run by the suite (57.8%) at 8edf11b0419c (this file on GitHub).
runnever runcolumns: line, executions, queries (click for the queries that run the line)
1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.2 *3 * This file is part of Overpass_API.4 *5 * Overpass_API is free software: you can redistribute it and/or modify6 * it under the terms of the GNU Affero General Public License as7 * published by the Free Software Foundation, either version 3 of the8 * License, or (at your option) any later version.9 *10 * Overpass_API is distributed in the hope that it will be useful,11 * but WITHOUT ANY WARRANTY; without even the implied warranty of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU Affero General Public License16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.17 */19#ifndef DE__OSM3S___OVERPASS_API__CORE__GEOMETRY_H20#define DE__OSM3S___OVERPASS_API__CORE__GEOMETRY_H23#include <cmath>24#include <vector>27struct Point_Double28{29public:32 double lat;33 double lon;35208765220 q bool operator==(const Point_Double& rhs) const { return lat == rhs.lat && lon == rhs.lon; }37 bool operator<(const Point_Double& rhs) const40 bool epsilon_equal(const Point_Double& rhs) const42};45struct Bbox_Double46{47public:48 Bbox_Double(double south_, double west_, double north_, double east_)51 bool valid() const57 double center_lat() const;58 double center_lon() const;60 double south, west, north, east;62 bool contains(const Point_Double& point) const;63 bool intersects(const Point_Double& from, const Point_Double& to) const;65 const static Bbox_Double invalid;66};69// All coordinates are always in latitude and longitude70class Opaque_Geometry71{72public:74 virtual Opaque_Geometry* clone() const = 0;76 virtual bool has_center() const = 0;77 virtual double center_lat() const = 0;78 virtual double center_lon() const = 0;80 // We require for a bounding box the following:81 // Usually, west is smaller than east.82 // If the object passes through all lines of longitude then the bounding box is west -180.0, east 180.0.83 // If the object crosses the date line but doesn't pass all lines of longitude then east is smaller than west.84 // For a single point, south and north are equal, and so are west and east.85 virtual bool has_bbox() const = 0;86 virtual double south() const = 0;87 virtual double north() const = 0;88 virtual double west() const = 0;89 virtual double east() const = 0;91 virtual bool has_line_geometry() const = 0;94 virtual bool has_multiline_geometry() const = 0;950 virtual const std::vector< std::vector< Point_Double > >* get_multiline_geometry() const { return 0; }97 virtual bool has_components() const = 0;101 virtual unsigned int way_size() const = 0;102 virtual bool has_faithful_way_geometry() const = 0;103 virtual bool way_pos_is_valid(unsigned int pos) const = 0;104 virtual double way_pos_lat(unsigned int pos) const = 0;105 virtual double way_pos_lon(unsigned int pos) const = 0;107 virtual bool has_faithful_relation_geometry() const = 0;108 virtual bool relation_pos_is_valid(unsigned int member_pos) const = 0;109 virtual double relation_pos_lat(unsigned int member_pos) const = 0;110 virtual double relation_pos_lon(unsigned int member_pos) const = 0;111 virtual unsigned int relation_way_size(unsigned int member_pos) const = 0;112 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const = 0;113 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const = 0;114 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const = 0;116 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const = 0;117};120class Null_Geometry : public Opaque_Geometry121{122public:1510 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }1520 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }1530 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }156};159class Point_Geometry : public Opaque_Geometry160{161public:1900 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }1910 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }1920 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }194 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;196private:197 Point_Double pt;198};201class Bbox_Geometry : public Opaque_Geometry202{203public:204329611 q Bbox_Geometry(double south, double west, double north, double east) : bbox(south, west, north, east) {}2330 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }2340 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }2350 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }239private:240 Bbox_Double bbox;241};244class Linestring_Geometry : public Opaque_Geometry245{246public:247124214173 q Linestring_Geometry(const std::vector< Point_Double >& points_) : points(points_), bounds(0) {}252 virtual double center_lat() const;253 virtual double center_lon() const;256 virtual double south() const;257 virtual double north() const;258 virtual double west() const;259 virtual double east() const;262230147529 q virtual const std::vector< Point_Double >* get_line_geometry() const { return &points; }269250382018 q virtual bool way_pos_is_valid(unsigned int pos) const { return pos < points.size(); }2780 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }2790 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }2800 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }282 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;284private:285 std::vector< Point_Double > points;286 mutable Bbox_Double* bounds;287};290class Partial_Way_Geometry : public Opaque_Geometry291{292public:294 Partial_Way_Geometry(const std::vector< Point_Double >& points_);299 virtual double center_lat() const;300 virtual double center_lon() const;303 virtual double south() const;304 virtual double north() const;305 virtual double west() const;306 virtual double east() const;309 virtual const std::vector< Point_Double >* get_line_geometry() const313 virtual const std::vector< std::vector< Point_Double > >* get_multiline_geometry() const3201709119 q virtual bool way_pos_is_valid(unsigned int pos) const { return pos < points.size() && points[pos].lat < 100.; }324 void add_point(const Point_Double& point);3310 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }3320 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }3330 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }335 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;337private:338 std::vector< Point_Double > points;339 std::vector< std::vector< Point_Double > > valid_segments;340 mutable Bbox_Double* bounds;341 bool has_coords;342};345class Free_Polygon_Geometry : public Opaque_Geometry346{347public:349 Free_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_);354 virtual double center_lat() const;355 virtual double center_lon() const;358 virtual double south() const;359 virtual double north() const;360 virtual double west() const;361 virtual double east() const;3652281211 q virtual const std::vector< std::vector< Point_Double > >* get_multiline_geometry() const { return &linestrings; }3790 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }3800 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }3810 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }383 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;385 void add_linestring(const std::vector< Point_Double >& linestring);387private:388 std::vector< std::vector< Point_Double > > linestrings;389 mutable Bbox_Double* bounds;390};393class RHR_Polygon_Geometry : public Opaque_Geometry394{395public:396 RHR_Polygon_Geometry(const Free_Polygon_Geometry& rhs);3982944358 q virtual Opaque_Geometry* clone() const { return new RHR_Polygon_Geometry(linestrings); }401 virtual double center_lat() const;402 virtual double center_lon() const;405 virtual double south() const;406 virtual double north() const;407 virtual double west() const;408 virtual double east() const;412175901 q virtual const std::vector< std::vector< Point_Double > >* get_multiline_geometry() const { return &linestrings; }4260 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const { return false; }4270 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const { return 0; }4280 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const { return 0; }430 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;432 void add_linestring(const std::vector< Point_Double >& linestring);434private:435 RHR_Polygon_Geometry(const std::vector< std::vector< Point_Double > >& linestrings_)438 std::vector< std::vector< Point_Double > > linestrings;439 mutable Bbox_Double* bounds;440};443class Compound_Geometry : public Opaque_Geometry444{445public:4472716320 q Compound_Geometry(const std::vector< Opaque_Geometry* >& components_) : components(components_), bounds(0) {}448 virtual ~Compound_Geometry()45162666940 q for (std::vector< Opaque_Geometry* >::iterator it = components.begin(); it != components.end(); ++it)454 virtual Opaque_Geometry* clone() const;456 virtual bool has_center() const;457 virtual double center_lat() const;458 virtual double center_lon() const;460 virtual bool has_bbox() const;461 virtual double south() const;462 virtual double north() const;463 virtual double west() const;464 virtual double east() const;471110414 q virtual const std::vector< Opaque_Geometry* >* get_components() const { return &components; }474 void add_component(Opaque_Geometry* component);483 virtual bool relation_pos_is_valid(unsigned int member_pos) const;484 virtual double relation_pos_lat(unsigned int member_pos) const;485 virtual double relation_pos_lon(unsigned int member_pos) const;486 virtual unsigned int relation_way_size(unsigned int member_pos) const;487 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const;488 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const;489 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const;491 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;493private:494 std::vector< Opaque_Geometry* > components;495 mutable Bbox_Double* bounds;496};499class Partial_Relation_Geometry : public Opaque_Geometry500{501public:503 Partial_Relation_Geometry(const std::vector< Opaque_Geometry* >& components_)516 virtual ~Partial_Relation_Geometry()51926229019 q for (std::vector< Opaque_Geometry* >::iterator it = components.begin(); it != components.end(); ++it)522 virtual Opaque_Geometry* clone() const;524 virtual bool has_center() const;525 virtual double center_lat() const;526 virtual double center_lon() const;528 virtual bool has_bbox() const;529 virtual double south() const;530 virtual double north() const;531 virtual double west() const;532 virtual double east() const;539675 q virtual const std::vector< Opaque_Geometry* >* get_components() const { return &components; }542 void add_placeholder();543 void add_point(const Point_Double& point);544 void start_way();545 void add_way_point(const Point_Double& point);546 void add_way_placeholder();555 virtual bool relation_pos_is_valid(unsigned int member_pos) const;556 virtual double relation_pos_lat(unsigned int member_pos) const;557 virtual double relation_pos_lon(unsigned int member_pos) const;558 virtual unsigned int relation_way_size(unsigned int member_pos) const;559 virtual bool relation_pos_is_valid(unsigned int member_pos, unsigned int nd_pos) const;560 virtual double relation_pos_lat(unsigned int member_pos, unsigned int nd_pos) const;561 virtual double relation_pos_lon(unsigned int member_pos, unsigned int nd_pos) const;563 virtual bool relevant_to_bbox(const Bbox_Double& bbox) const;565private:566 std::vector< Opaque_Geometry* > components;567 mutable Bbox_Double* bounds;568 bool has_coords;569};572struct Cartesian573{576 Cartesian(double lat, double lon)587public:588 double x;589 double y;590 double z;591};594double length(const Opaque_Geometry& geometry);596Opaque_Geometry* make_trace(const Opaque_Geometry& geometry);598Opaque_Geometry* make_hull(const Opaque_Geometry& geometry);600double great_circle_dist(double lat1, double lon1, double lat2, double lon2);603class Great_Circle604{605public:606 Great_Circle(const Point_Double& lhs, const Point_Double& rhs)608// std::cout<<"gc "<<lhs.lat<<' '<<lhs.lon<<' '<<rhs.lat<<' '<<rhs.lon609// <<' '<<(lhs.lon < rhs.lon)<<(rhs.lon - lhs.lon > 180.)<<610// (rhs.lon < lhs.lon)<<(lhs.lon - rhs.lon < 180.)<<'\n';639// std::cout<<ortho_s<<' '<<ortho_cs<<' '<<ortho_cc<<'\n'640// <<(ortho_s*ortho_s + ortho_cs*ortho_cs + ortho_cc*ortho_cc)<<'\n'641// <<asin(ortho_s)/M_PI*180.<<' '<<asin(ortho_cs/sqrt(1 - ortho_s*ortho_s))/M_PI*180.<<'\n';644 double lat_of(double lon)646 //rotate ortho such that the longitude to use for cartesian computation is always zero654private:655 double ortho_s;656 double ortho_cs;657 double ortho_cc;658};661#endif