Skip to content

Commit d4be2d5

Browse files
committed
Added optimization
1 parent d2654a9 commit d4be2d5

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int main() {
6565

6666
### PolyUtil class
6767

68-
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic)`](#containsLocation)
68+
* [`containsLocation(LatLng point, LatLngList polygon, bool geodesic)`](#containsLocation)
6969
* [`isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance, bool geodesic)`](#isLocationOnEdge)
7070
* [`isLocationOnPath(LatLng point, LatLngList polyline, double tolerance, bool geodesic)`](#isLocationOnPath)
7171
* [`distanceToLine(LatLng point, LatLng start, LatLng end)`](#distanceToLine)
@@ -113,13 +113,13 @@ std::array<LatLng, 1U> northPole = { {90, 0} };
113113
### PolyUtil functions
114114

115115
<a name="containsLocation"></a>
116-
**`PolyUtil::containsLocation(LatLng point, LatLngList polygon, bool geodesic = false)`** - Computes whether the given point lies inside the specified polygon
116+
**`PolyUtil::containsLocation(const LatLng& point, const LatLngList& polygon, bool geodesic = false)`** - Computes whether the given point lies inside the specified polygon
117117

118118
* `point` - a point in geographical coordinates: latitude and longitude
119119
* `polygon` - a series of connected coordinates in an ordered sequence
120120
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
121121

122-
Return value: `boolean` - whether the given point lies inside the specified polygon
122+
Return value: `bool` - whether the given point lies inside the specified polygon
123123

124124
```c++
125125
// Around the north pole.
@@ -132,14 +132,14 @@ std::cout << PolyUtil::containsLocation(LatLng(-90, 0), aroundNorthPole); // fal
132132
---
133133
134134
<a name="isLocationOnEdge"></a>
135-
**`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.
135+
**`PolyUtil::isLocationOnEdge(const LatLng& point, const 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.
136136
137137
* `point` - a point in geographical coordinates: latitude and longitude
138138
* `polygon` - a series of connected coordinates in an ordered sequence
139139
* `tolerance` - tolerance value in meters
140140
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
141141
142-
Return value: `boolean` - whether the given point lies on or near the edge of a polygon
142+
Return value: `bool` - whether the given point lies on or near the edge of a polygon
143143
144144
```c++
145145
// On equator.
@@ -155,14 +155,14 @@ std::cout << PolyUtil::isLocationOnEdge(LatLng(0, 90 - big), equator); // fals
155155
---
156156

157157
<a name="isLocationOnPath"></a>
158-
**`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.
158+
**`PolyUtil::isLocationOnPath(const LatLng& point, const 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.
159159

160160
* `point` - a point in geographical coordinates: latitude and longitude
161161
* `polygon` - a series of connected coordinates in an ordered sequence
162162
* `tolerance` - tolerance value in meters
163163
* `geodesic` - the polyline is composed of great circle segments if geodesic is true, and of Rhumb segments otherwise
164164

165-
Return value: `boolean` - whether the point lies on or near a polyline
165+
Return value: `bool` - whether the point lies on or near a polyline
166166

167167
```c++
168168
// On equator.
@@ -178,7 +178,7 @@ std::cout << PolyUtil::isLocationOnPath(LatLng(0, 90 - big), equator); // fals
178178
---
179179
180180
<a name="distanceToLine"></a>
181-
**`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.
181+
**`PolyUtil::distanceToLine(const LatLng& p, const LatLng& start, const LatLng& end)`** - Computes the distance on the sphere between the point p and the line segment start to end.
182182
183183
* `point` - the point to be measured
184184
* `start` - the beginning of the line segment
@@ -197,7 +197,7 @@ std::cout << PolyUtil::distanceToLine(point, startLine, endLine); // 37.947946
197197
### SphericalUtil functions
198198

199199
<a name="computeHeading"></a>
200-
**`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).
200+
**`SphericalUtil::computeHeading(const LatLng& from, const LatLng& to)`** - Returns the heading from one LatLng to another LatLng. Headings are expressed in degrees clockwise from North within the range [-180,180).
201201

202202
* `from` - a point in geographical coordinates: latitude and longitude
203203
* `to` - a point in geographical coordinates: latitude and longitude
@@ -215,7 +215,7 @@ std::cout << SphericalUtil::computeHeading(front, right); // +90
215215
---
216216
217217
<a name="computeOffset"></a>
218-
**`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).
218+
**`SphericalUtil::computeOffset(const 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).
219219
220220
* `from` - the LatLng from which to start.
221221
* `distance` - the distance to travel.
@@ -236,7 +236,7 @@ auto back = SphericalUtil::computeOffset(front, M_PI * MathUtil::EARTH_RADIUS,
236236
---
237237

238238
<a name="computeOffsetOrigin"></a>
239-
**`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.
239+
**`SphericalUtil::computeOffsetOrigin(const 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.
240240

241241
* `from` - the destination LatLng
242242
* `distance` - the distance travelled, in meters.
@@ -258,7 +258,7 @@ assert(front == SphericalUtil::computeOffsetOrigin(LatLng(-45, 0), M_PI * Math
258258
---
259259
260260
<a name="interpolate"></a>
261-
**`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.
261+
**`SphericalUtil::interpolate(const LatLng& from, const LatLng& to, double fraction)`** - Returns the LatLng which lies the given fraction of the way between the origin LatLng and the destination LatLng.
262262
263263
* `from` - the LatLng from which to start.
264264
* `to` - the LatLng toward which to travel.
@@ -280,7 +280,7 @@ assert(LatLng(89, 0) == SphericalUtil::interpolate(up, front, 1 / 90.0));
280280
---
281281

282282
<a name="computeDistanceBetween"></a>
283-
**`computeDistanceBetween(LatLng from, LatLng to)`** - Returns the distance, in meters, between two LatLngs.
283+
**`SphericalUtil::computeDistanceBetween(const LatLng& from, const LatLng& to)`** - Returns the distance, in meters, between two LatLngs.
284284

285285
* `from` - the first point
286286
* `to` - the second point
@@ -297,7 +297,7 @@ std:cout << SphericalUtil::computeDistanceBetween(up, down); // MathUtil::EARTH_
297297
---
298298
299299
<a name="computeLength"></a>
300-
**`computeLength(LatLngList path)`** - Returns the length of the given path, in meters, on Earth
300+
**`SphericalUtil::computeLength(const LatLngList& path)`** - Returns the length of the given path, in meters, on Earth
301301
302302
* `path` - a series of connected coordinates in an ordered sequence. Any iterable containers.
303303
@@ -313,7 +313,7 @@ std::cout << SphericalUtil::computeLength(latLngs2); // M_PI * MathUtil::EARTH_R
313313
---
314314

315315
<a name="computeArea"></a>
316-
**`computeArea(LatLngList path)`** - Returns the area of a closed path on Earth.
316+
**`SphericalUtil::computeArea(const LatLngList& path)`** - Returns the area of a closed path on Earth.
317317

318318
* `path` - a closed path. Any iterable containers.
319319

@@ -333,7 +333,7 @@ std::cout << SphericalUtil::computeArea(second); // M_PI * MathUtil::EARTH_RADIU
333333
---
334334
335335
<a name="computeSignedArea"></a>
336-
**`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.
336+
**`SphericalUtil::computeSignedArea(const 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.
337337
338338
* `path` - a closed path. Any iterable containers.
339339

include/LatLng.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class LatLng {
3434

3535
LatLng& operator=(const LatLng & other) = default;
3636

37-
bool operator==(const LatLng & other) {
37+
bool operator==(const LatLng & other) const {
3838
return isCoordinateEqual(lat, other.lat) &&
3939
isCoordinateEqual(lng, other.lng);
4040
}
4141

4242

4343
private:
44-
bool isCoordinateEqual(double first, double second) {
44+
bool isCoordinateEqual(double first, double second) const {
4545
return std::fabs(first - second) < 1e-12;
4646
}
4747
};

include/PolyUtil.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PolyUtil {
3232
* (loxodromic) segments otherwise.
3333
*/
3434
template <typename LatLngList>
35-
static inline bool containsLocation(LatLng point, LatLngList polygon, bool geodesic = false) {
35+
static inline bool containsLocation(const LatLng& point, const LatLngList& polygon, bool geodesic = false) {
3636
size_t size = polygon.size();
3737

3838
if (size == 0) {
@@ -74,7 +74,7 @@ class PolyUtil {
7474
* closing segment between the first point and the last point is included.
7575
*/
7676
template <typename LatLngList>
77-
static inline bool isLocationOnEdge(LatLng point, LatLngList polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) {
77+
static inline bool isLocationOnEdge(const LatLng& point, const LatLngList& polygon, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) {
7878
return PolyUtil::isLocationOnEdgeOrPath(point, polygon, true, geodesic, tolerance);
7979
}
8080

@@ -86,7 +86,7 @@ class PolyUtil {
8686
* segment between the first point and the last point is not included.
8787
*/
8888
template <typename LatLngList>
89-
static inline bool isLocationOnPath(LatLng point, LatLngList polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) {
89+
static inline bool isLocationOnPath(const LatLng& point, const LatLngList& polyline, double tolerance = PolyUtil::DEFAULT_TOLERANCE, bool geodesic = true) {
9090
return PolyUtil::isLocationOnEdgeOrPath(point, polyline, false, geodesic, tolerance);
9191
}
9292

@@ -107,7 +107,7 @@ class PolyUtil {
107107
* poly.size()-2 if between poly[poly.size() - 2] and poly[poly.size() - 1]
108108
*/
109109
template <typename LatLngList>
110-
static inline bool isLocationOnEdgeOrPath(LatLng point, LatLngList poly, bool closed, bool geodesic, double toleranceEarth) {
110+
static inline bool isLocationOnEdgeOrPath(const LatLng& point, const LatLngList& poly, bool closed, bool geodesic, double toleranceEarth) {
111111
size_t size = poly.size();
112112

113113
if (size == 0U) {
@@ -185,7 +185,7 @@ class PolyUtil {
185185
* @param end the end of the line segment
186186
* @return the distance in meters (assuming spherical earth)
187187
*/
188-
static inline double distanceToLine(LatLng p, LatLng start, LatLng end) {
188+
static inline double distanceToLine(const LatLng& p, const LatLng& start, const LatLng& end) {
189189
if (start == end) {
190190
return SphericalUtil::computeDistanceBetween(end, p);
191191
}

include/SphericalUtil.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SphericalUtil {
2626
*
2727
* @return The heading in degrees clockwise from north.
2828
*/
29-
inline static double computeHeading(LatLng from, LatLng to) {
29+
inline static double computeHeading(const LatLng& from, const LatLng& to) {
3030
// http://williams.best.vwh.net/avform.htm#Crs
3131
double fromLat = deg2rad(from.lat);
3232
double fromLng = deg2rad(from.lng);
@@ -49,7 +49,7 @@ class SphericalUtil {
4949
* @param distance The distance to travel.
5050
* @param heading The heading in degrees clockwise from north.
5151
*/
52-
inline static LatLng computeOffset(LatLng from, double distance, double heading) {
52+
inline static LatLng computeOffset(const LatLng& from, double distance, double heading) {
5353
distance /= MathUtil::EARTH_RADIUS;
5454
heading = deg2rad(heading);
5555
// http://williams.best.vwh.net/avform.htm#LL
@@ -79,7 +79,7 @@ class SphericalUtil {
7979
* @param distance The distance travelled, in meters.
8080
* @param heading The heading in degrees clockwise from north.
8181
*/
82-
inline static LatLng computeOffsetOrigin(LatLng to, double distance, double heading) {
82+
inline static LatLng computeOffsetOrigin(const LatLng& to, double distance, double heading) {
8383
heading = deg2rad(heading);
8484
distance /= MathUtil::EARTH_RADIUS;
8585
// http://lists.maptools.org/pipermail/proj/2008-October/003939.html
@@ -124,7 +124,7 @@ class SphericalUtil {
124124
* @param fraction A fraction of the distance to travel.
125125
* @return The interpolated LatLng.
126126
*/
127-
inline static LatLng interpolate(LatLng from, LatLng to, double fraction) {
127+
inline static LatLng interpolate(const LatLng& from, const LatLng& to, double fraction) {
128128
// http://en.wikipedia.org/wiki/Slerp
129129
double fromLat = deg2rad(from.lat);
130130
double fromLng = deg2rad(from.lng);
@@ -154,22 +154,22 @@ class SphericalUtil {
154154
* Returns the angle between two LatLngs, in radians. This is the same as the distance
155155
* on the unit sphere.
156156
*/
157-
inline static double computeAngleBetween(LatLng from, LatLng to) {
157+
inline static double computeAngleBetween(const LatLng& from, const LatLng& to) {
158158
return SphericalUtil::distanceRadians(deg2rad(from.lat), deg2rad(from.lng), deg2rad(to.lat), deg2rad(to.lng));
159159
}
160160

161161
/**
162162
* Returns the distance between two LatLngs, in meters.
163163
*/
164-
inline static double computeDistanceBetween(LatLng from, LatLng to) {
164+
inline static double computeDistanceBetween(const LatLng& from, const LatLng& to) {
165165
return SphericalUtil::computeAngleBetween(from, to) * MathUtil::EARTH_RADIUS;
166166
}
167167

168168
/**
169169
* Returns the length of the given path, in meters, on Earth.
170170
*/
171171
template <typename LatLngList>
172-
inline static double computeLength(LatLngList path) {
172+
inline static double computeLength(const LatLngList& path) {
173173
if (path.size() < 2U) {
174174
return 0;
175175
}
@@ -194,7 +194,7 @@ class SphericalUtil {
194194
* @return The path's area in square meters.
195195
*/
196196
template <typename LatLngList>
197-
inline static double computeArea(LatLngList path) {
197+
inline static double computeArea(const LatLngList& path) {
198198
return abs(SphericalUtil::computeSignedArea(path));
199199
}
200200

@@ -207,7 +207,7 @@ class SphericalUtil {
207207
* @return The loop's area in square meters.
208208
*/
209209
template <typename LatLngList>
210-
inline static double computeSignedArea(LatLngList path) {
210+
inline static double computeSignedArea(const LatLngList& path) {
211211
return SphericalUtil::computeSignedAreaP(path, MathUtil::EARTH_RADIUS);
212212
}
213213

@@ -226,7 +226,7 @@ class SphericalUtil {
226226
* Used by SphericalUtilTest.
227227
*/
228228
template <typename LatLngList>
229-
inline static double computeSignedAreaP(LatLngList path, double radius) {
229+
inline static double computeSignedAreaP(const LatLngList& path, double radius) {
230230
size_t size = path.size();
231231
if (size < 3U) { return 0; }
232232
double total = 0;

0 commit comments

Comments
 (0)