Skip to content

Commit d1f75d3

Browse files
authored
Merge pull request #5 from gistrec/docs
Added README.md
2 parents 388126d + 32ff82e commit d1f75d3

File tree

1 file changed

+365
-2
lines changed

1 file changed

+365
-2
lines changed

Diff for: README.md

+365-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,365 @@
1-
# cpp-geometry-library
2-
CPP Geometry Library
1+
# Geometry Library
2+
3+
<p align="left">
4+
<a href="https://circleci.com/gh/gistrec/cpp-geometry-library/tree/master">
5+
<img src="https://img.shields.io/circleci/build/github/gistrec/cpp-geometry-library/master" alt="Build status">
6+
</a>
7+
<a href="#">
8+
<img src="https://img.shields.io/codacy/grade/bcff544711544d5fb7da95b68abf566d" alt="Code quality">
9+
</a>
10+
<a href="https://github.com/gistrec/cpp-geometry-library/releases">
11+
<img src="https://img.shields.io/github/v/release/gistrec/cpp-geometry-library" alt="Release">
12+
</a>
13+
<a href="#">
14+
<img src="https://img.shields.io/badge/platform-windows%20%7C%20linux%20%7C%20osx-brightgreen" alt="Supported platforms">
15+
</a>
16+
<a href="https://github.com/gistrec/cpp-geometry-library/blob/master/LICENSE">
17+
<img src="https://img.shields.io/github/license/gistrec/cpp-geometry-library?color=brightgreen" alt="License">
18+
</a>
19+
</p>
20+
21+
C++ Geometry Library provides utility functions for the computation of geometric data on the surface of the Earth. Code ported from Google [Maps Android API](https://github.com/googlemaps/android-maps-utils/).
22+
23+
## Features
24+
25+
* [Spherical](https://developers.google.com/maps/documentation/javascript/reference#spherical) contains spherical geometry utilities allowing you to compute angles, distances and areas from latitudes and longitudes.
26+
* [Poly](https://developers.google.com/maps/documentation/javascript/reference#poly) utility functions for computations involving polygons and polylines.
27+
* [Encoding](https://developers.google.com/maps/documentation/javascript/reference#encoding) utilities for polyline encoding and decoding.
28+
29+
## Usage
30+
31+
You just need to include `SphericalUtil.hpp` or `PolyUtil.hpp`
32+
33+
Here is an example of using this library:
34+
35+
```c++
36+
#include <iostream>
37+
#include <vector>
38+
39+
#include "SphericalUtil.hpp"
40+
41+
int main() {
42+
LatLng up = { 90.0, 0.0 };
43+
LatLng down = {-90.0, 0.0 };
44+
LatLng front = { 0.0, 0.0 };
45+
LatLng right = { 0.0, 90.0 };
46+
47+
auto angle = SphericalUtil::computeAngleBetween(up, right); // 90
48+
std::cout << "The angle between up and right is " << rad2deg(angle) << " degrees" << std::endl;
49+
50+
auto distance = SphericalUtil::computeDistanceBetween(up, down); // 2.00151e+07
51+
std::cout << "The distance between up and down is " << distance << " meters" << std::endl;
52+
53+
std::vector<LatLng> points = { front, up, right };
54+
55+
auto length = SphericalUtil::computeLength(points); // 2.00151e+07
56+
std::cout << "The length between front, up and right is " << length << " meters" << std::endl;
57+
58+
auto area = SphericalUtil::computeArea(points); // 6.37582e+13
59+
std::cout << "The area between front, up and right is " << area << " square meters" << std::endl;
60+
61+
return 0;
62+
}
63+
```
64+
65+
## Available methods
66+
67+
### PolyUtil class
68+
69+
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic = false)`](#containsLocation)
70+
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnEdge)
71+
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`](#isLocationOnPath)
72+
* [`distanceToLine(LatLng point, LatLng start, LatLng end)`](#distanceToLine)
73+
74+
### SphericalUtil class
75+
76+
* [`computeHeading(LatLng from, LatLng to)`](#computeHeading)
77+
* [`computeOffset(LatLng from, double distance, double heading)`](#computeOffset)
78+
* [`computeOffsetOrigin(LatLng to, double distance, double heading)`](#computeOffsetOrigin)
79+
* [`interpolate(LatLng from, LatLng to, double fraction)`](#interpolate)
80+
* [`computeDistanceBetween(LatLng from, LatLng to)`](#computeDistanceBetween)
81+
* [`computeLength(LatLngList path)`](#computeLength)
82+
* [`computeArea(LatLngList path)`](#computeArea)
83+
* [`computeSignedArea(LatLngList path)`](#computeSignedArea)
84+
85+
## Classes description
86+
87+
`LatLng` - a point in geographical coordinates: latitude and longitude.
88+
89+
* Latitude ranges between `-90` and `90` degrees, inclusive
90+
* Longitude ranges between `-180` and `180` degrees, inclusive
91+
92+
Usage example:
93+
94+
```c++
95+
LatLng northPole = {90, 0};
96+
97+
LatLng otherPoint = northPole;
98+
```
99+
100+
---
101+
102+
`LatLngList` - a series of connected coordinates in an ordered sequence. Any iterable containers.
103+
104+
Usage example:
105+
106+
```c++
107+
std::vector<LatLng> aroundNorthPole = { {89, 0}, {89, 120}, {89, -120} };
108+
109+
std::array<LatLng, 1U> northPole = { {90, 0} };
110+
```
111+
112+
## Functions description
113+
114+
### PolyUtil functions
115+
116+
<a name="containsLocation"></a>
117+
**`PolyUtil::containsLocation(LatLng point, LatLngList polygon, bool geodesic = false)`** - Computes whether the given point lies inside the specified polygon
118+
119+
* `point` - a point in geographical coordinates: latitude and longitude
120+
* `polygon` - a series of connected coordinates in an ordered sequence
121+
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
122+
123+
Return value: `boolean` - whether the given point lies inside the specified polygon
124+
125+
```c++
126+
// Around the north pole.
127+
std::vector<LatLng> aroundNorthPole = { {89, 0}, {89, 120}, {89, -120} };
128+
129+
std::cout << PolyUtil::containsLocation(LatLng(90, 0), aroundNorthPole); // true
130+
std::cout << PolyUtil::containsLocation(LatLng(-90, 0), aroundNorthPole); // false
131+
```
132+
133+
---
134+
135+
<a name="isLocationOnEdge"></a>
136+
**`PolyUtil::isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`** - Computes whether the given point lies on or near to a polyline, or the edge of a polygon, within a specified tolerance. Returns true when the difference between the latitude and longitude of the supplied point, and the closest point on the edge, is less than the tolerance. The tolerance defaults to `0.1` meters.
137+
138+
* `point` - a point in geographical coordinates: latitude and longitude
139+
* `polygon` - a series of connected coordinates in an ordered sequence
140+
* `tolerance` - tolerance value in meters
141+
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
142+
143+
Return value: `boolean` - whether the given point lies on or near the edge of a polygon
144+
145+
```c++
146+
// On equator.
147+
std::vector<LatLng> equator = { {0, 90}, {0, 180} };
148+
149+
double small = 5e-7; // Half the default tolerance.
150+
double big = 2e-6; // Double the default tolerance.
151+
152+
std::cout << PolyUtil::isLocationOnEdge(LatLng(0, 90 - small), equator); // true
153+
std::cout << PolyUtil::isLocationOnEdge(LatLng(0, 90 - big), equator); // false
154+
```
155+
156+
---
157+
158+
<a name="isLocationOnPath"></a>
159+
**`PolyUtil::isLocationOnPath(LatLng point, LatLngList polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true)`** - Computes whether the given point lies on or near a polyline, within a specified tolerance in meters. The polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing segment between the first point and the last point is not included.
160+
161+
* `point` - a point in geographical coordinates: latitude and longitude
162+
* `polygon` - a series of connected coordinates in an ordered sequence
163+
* `tolerance` - tolerance value in meters
164+
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
165+
166+
Return value: `boolean` - whether the point lies on or near a polyline
167+
168+
```c++
169+
// On equator.
170+
std::vector<LatLng> equator = { {0, 90}, {0, 180} };
171+
172+
double small = 5e-7; // Half the default tolerance.
173+
double big = 2e-6; // Double the default tolerance.
174+
175+
std::cout << PolyUtil::isLocationOnPath(LatLng(0, 90 - small), equator); // true
176+
std::cout << PolyUtil::isLocationOnPath(LatLng(0, 90 - big), equator); // false
177+
```
178+
179+
---
180+
181+
<a name="distanceToLine"></a>
182+
**`PolyUtil::distanceToLine(LatLng p, LatLng start, LatLng end)`** - Computes the distance on the sphere between the point p and the line segment start to end.
183+
184+
* `point` - the point to be measured
185+
* `start` - the beginning of the line segment
186+
* `end` - the end of the line segment
187+
188+
Return value: `double` - the distance in meters (assuming spherical earth)
189+
190+
```c++
191+
LatLng startLine(28.05359, -82.41632);
192+
LatLng endLine(28.05310, -82.41634);
193+
LatLng point(28.05342, -82.41594);
194+
195+
std::cout << PolyUtil::distanceToLine(point, startLine, endLine); // 37.947946
196+
```
197+
198+
### SphericalUtil functions
199+
200+
<a name="computeHeading"></a>
201+
**`SphericalUtil::computeHeading(LatLng from, LatLng to)`** - Returns the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).
202+
203+
* `from` - a point in geographical coordinates: latitude and longitude
204+
* `to` - a point in geographical coordinates: latitude and longitude
205+
206+
Return value: `double` - the heading in degrees clockwise from north
207+
208+
```c++
209+
LatLng front(0, 0);
210+
LatLng right(0, 90);
211+
212+
std::cout << SphericalUtil::computeHeading(right, front); // -90
213+
std::cout << SphericalUtil::computeHeading(front, right); // +90
214+
```
215+
216+
---
217+
218+
<a name="computeOffset"></a>
219+
**`computeOffset(LatLng from, double distance, double heading)`** - Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).
220+
221+
* `from` - the LatLng from which to start.
222+
* `distance` - the distance to travel.
223+
* `heading` - the heading in degrees clockwise from north.
224+
225+
Return value: `LatLng` - resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north)
226+
227+
```c++
228+
LatLng front(0, 0);
229+
230+
auto up = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 0); // LatLng( 90, 0)
231+
auto down = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 180); // LatLng(-90, 0)
232+
auto left = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, -90); // LatLng( 0, -90)
233+
auto right = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS / 2, 90); // LatLng( 0, 90)
234+
auto back = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS, 90); // LatLng( 0, -180)
235+
```
236+
237+
---
238+
239+
<a name="computeOffsetOrigin"></a>
240+
**`computeOffsetOrigin(LatLng to, double distance, double heading)`** - Returns the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North.
241+
242+
* `from` - the destination LatLng
243+
* `distance` - the distance travelled, in meters.
244+
* `heading` - the heading in degrees clockwise from north
245+
246+
Return value: `LatLng` - the location of origin when provided with a LatLng destination, meters travelled and original heading. Headings are expressed in degrees clockwise from North
247+
248+
```c++
249+
LatLng front(0, 0);
250+
251+
assert(front == SphericalUtil::computeOffsetOrigin(front, 0, 0));
252+
253+
assert(front == SphericalUtil::computeOffsetOrigin(LatLng( 0, 45), M_PI * MathUtil::EARTH_RADIUS / 4, 90));
254+
assert(front == SphericalUtil::computeOffsetOrigin(LatLng( 0, -45), M_PI * MathUtil::EARTH_RADIUS / 4, -90));
255+
assert(front == SphericalUtil::computeOffsetOrigin(LatLng( 45, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 0));
256+
assert(front == SphericalUtil::computeOffsetOrigin(LatLng(-45, 0), M_PI * MathUtil::EARTH_RADIUS / 4, 190));
257+
```
258+
259+
---
260+
261+
<a name="interpolate"></a>
262+
**`interpolate(LatLng from, LatLng to, double fraction)`** - Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.
263+
264+
* `from` - the LatLng from which to start.
265+
* `to` - the LatLng toward which to travel.
266+
* `fraction` - a fraction of the distance to travel.
267+
268+
Return value: `LatLng` - point which lies the given fraction of the way between the origin LatLng and the destination LatLng
269+
270+
```c++
271+
LatLng up(90, 0);
272+
LatLng front(0, 0);
273+
274+
assert(LatLng(1, 0) == SphericalUtil::interpolate(front, up, 1 / 90.0));
275+
assert(LatLng(1, 0) == SphericalUtil::interpolate(up, front, 89 / 90.0));
276+
assert(LatLng(89, 0) == SphericalUtil::interpolate(front, up, 89 / 90.0));
277+
assert(LatLng(89, 0) == SphericalUtil::interpolate(up, front, 1 / 90.0));
278+
279+
```
280+
281+
---
282+
283+
<a name="computeDistanceBetween"></a>
284+
**`computeDistanceBetween(LatLng from, LatLng to)`** - Returns the distance, in meters, between two LatLngs.
285+
286+
* `from` - the first point
287+
* `to` - the second point
288+
289+
Return value: `double` - the distance, in meters, between two LatLngs
290+
291+
```c++
292+
LatLng up(90, 0);
293+
LatLng down(-90, 0);
294+
295+
std:cout << SphericalUtil::computeDistanceBetween(up, down); // MathUtil::EARTH_RADIUS
296+
```
297+
298+
---
299+
300+
<a name="computeLength"></a>
301+
**`computeLength(LatLngList path)`** - Returns the length of the given path, in meters, on Earth
302+
303+
* `path` - a series of connected coordinates in an ordered sequence. Any iterable containers.
304+
305+
Return valuse: `double` - the length of the given path, in meters, on Earth
306+
307+
```c++
308+
// List with three points
309+
std::vector<LatLng> latLngs2 = { {0, 0}, {90, 0}, {0, 90} };
310+
311+
std::cout << SphericalUtil::computeLength(latLngs2); // M_PI * MathUtil::EARTH_RADIUS
312+
```
313+
314+
---
315+
316+
<a name="computeArea"></a>
317+
**`computeArea(LatLngList path)`** - Returns the area of a closed path on Earth.
318+
319+
* `path` - a closed path. Any iterable containers.
320+
321+
Return value: `double` - the area of a closed path on Earth
322+
323+
```c++
324+
LatLng up = { 90.0, 0.0 };
325+
LatLng down = {-90.0, 0.0 };
326+
LatLng front = { 0.0, 0.0 };
327+
LatLng right = { 0.0, 90.0 };
328+
329+
std::vector<LatLng> path = { right, down, front, up, right };
330+
331+
std::cout << SphericalUtil::computeArea(second); // M_PI * MathUtil::EARTH_RADIUS * MathUtil::EARTH_RADIUS
332+
```
333+
334+
---
335+
336+
<a name="computeSignedArea"></a>
337+
**`computeSignedArea(LatLngList path)`** - Returns the signed area of a closed path on Earth. The sign of the area may be used to determine the orientation of the path. "inside" is the surface that does not contain the South Pole.
338+
339+
* `path` - a closed path. Any iterable containers.
340+
341+
Return value: `double` - the loop's area in square meters
342+
343+
```c++
344+
LatLng up = { 90.0, 0.0 };
345+
LatLng down = {-90.0, 0.0 };
346+
LatLng front = { 0.0, 0.0 };
347+
LatLng right = { 0.0, 90.0 };
348+
349+
std::vector<LatLng> path = { right, up, front, down, right };
350+
std::vector<LatLng> pathReversed = { right, down, front, up, right };
351+
352+
assert(SphericalUtil::computeSignedArea(path) == -SphericalUtil::computeSignedArea(pathReversed));
353+
```
354+
355+
---
356+
357+
## Support
358+
359+
[Please open an issue on GitHub](https://github.com/gistrec/cpp-geometry-library/issues)
360+
361+
## License
362+
363+
Geometry Library Google Maps API V3 is released under the MIT License. See the bundled
364+
[LICENSE](https://github.com/alexpechkarev/geometry-library/blob/master/LICENSE)
365+
file for details.

0 commit comments

Comments
 (0)