CARLA
Loading...
Searching...
No Matches
osm2odr/Geometry.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <cmath>
10#include <optional>
11#include <vector>
12
13#include <boost/geometry.hpp>
14#include <boost/geometry/geometries/point_xy.hpp>
15#include <boost/geometry/geometries/linestring.hpp>
16#include <boost/geometry/geometries/polygon.hpp>
17#include <boost/geometry/geometries/multi_polygon.hpp>
18#include <boost/geometry/index/rtree.hpp>
19
20namespace twinmodel {
21namespace geom {
22
23 namespace bg = boost::geometry;
24 namespace bgi = boost::geometry::index;
25
26 using Point = bg::model::d2::point_xy<double>;
27 using LineString = bg::model::linestring<Point>;
28 using Ring = bg::model::ring<Point, true, true>;
29 using Polygon = bg::model::polygon<Point, true, true>;
30 using MultiPolygon = bg::model::multi_polygon<Polygon>;
31 using Box = bg::model::box<Point>;
32
33 // A vertex carrying the elevation the reference line was sampled at; the
34 // planar algorithms ignore z, the OpenDRIVE elevation profile reads it back.
35 struct Point3 {
36 double x = 0.0;
37 double y = 0.0;
38 double z = 0.0;
39 };
40
41 using Line3 = std::vector<Point3>;
42
43 inline Point To2D(const Point3 &p) {
44 return Point(p.x, p.y);
45 }
46
47 inline LineString To2D(const Line3 &line) {
48 LineString out;
49 out.reserve(line.size());
50 for (const auto &p : line) {
51 bg::append(out, To2D(p));
52 }
53 return out;
54 }
55
56 inline double Length(const LineString &line) {
57 return bg::length(line);
58 }
59
60 /// Distance along @a line of the projection of @a p, shapely's LineString.project.
61 double Project(const LineString &line, const Point &p);
62
63 /// Point at distance @a s along @a line, shapely's LineString.interpolate.
64 Point Interpolate(const LineString &line, double s);
65
66 /// Heading of @a line at distance @a s, in radians.
67 double HeadingAt(const LineString &line, double s);
68
69 /// The piece of @a line between distances @a from and @a to, shapely's
70 /// substring: endpoints are inserted so the cut is exact.
71 LineString Substring(const LineString &line, double from, double to);
72
73 /// Offset @a line sideways by @a offset metres, positive to the left.
74 LineString OffsetLine(const LineString &line, double offset);
75
76 /// Build polygons from a set of noded edges, shapely's polygonize. Used for
77 /// junction outlines, where the input is the union of the incoming kerbs.
78 std::vector<Polygon> Polygonize(const std::vector<LineString> &edges);
79
80 MultiPolygon UnaryUnion(const std::vector<Polygon> &polygons);
81
82 /// bg::is_valid plus a correction pass, shapely's make_valid.
83 Polygon MakeValid(const Polygon &polygon);
84
85 inline Point Centroid(const Polygon &polygon) {
86 Point c(0.0, 0.0);
87 bg::centroid(polygon, c);
88 return c;
89 }
90
91 inline double Hypot(const Point &a, const Point &b) {
92 return std::hypot(bg::get<0>(b) - bg::get<0>(a), bg::get<1>(b) - bg::get<1>(a));
93 }
94
95 /// Signed angle in (-pi, pi] that turns heading @a from into heading @a to.
96 inline double NormalizeAngle(double angle) {
97 while (angle <= -M_PI) angle += 2.0 * M_PI;
98 while (angle > M_PI) angle -= 2.0 * M_PI;
99 return angle;
100 }
101
102} // namespace geom
103} // namespace twinmodel
LineString Substring(const LineString &line, double from, double to)
The piece of line between distances from and to, shapely's substring: endpoints are inserted so the c...
double Length(const LineString &line)
LineString OffsetLine(const LineString &line, double offset)
Offset line sideways by offset metres, positive to the left.
bg::model::ring< Point, true, true > Ring
Point Centroid(const Polygon &polygon)
Point Interpolate(const LineString &line, double s)
Point at distance s along line, shapely's LineString.interpolate.
std::vector< Polygon > Polygonize(const std::vector< LineString > &edges)
Build polygons from a set of noded edges, shapely's polygonize.
double HeadingAt(const LineString &line, double s)
Heading of line at distance s, in radians.
std::vector< Point3 > Line3
bg::model::linestring< Point > LineString
MultiPolygon UnaryUnion(const std::vector< Polygon > &polygons)
double NormalizeAngle(double angle)
Signed angle in (-pi, pi] that turns heading from into heading to.
double Hypot(const Point &a, const Point &b)
bg::model::multi_polygon< Polygon > MultiPolygon
bg::model::d2::point_xy< double > Point
bg::model::polygon< Point, true, true > Polygon
bg::model::box< Point > Box
Point To2D(const Point3 &p)
double Project(const LineString &line, const Point &p)
Distance along line of the projection of p, shapely's LineString.project.
Polygon MakeValid(const Polygon &polygon)
bg::is_valid plus a correction pass, shapely's make_valid.