aboutsummaryrefslogtreecommitdiff
path: root/src/modules/navigator/geofence.cpp
diff options
context:
space:
mode:
authorThomas Gubler <thomasgubler@gmail.com>2014-01-04 22:01:31 +0100
committerThomas Gubler <thomasgubler@gmail.com>2014-01-04 22:01:31 +0100
commit819822e1722dc31c0f97b8494c96a6c292b07185 (patch)
treedc98dacd4a2b7de9a01b40e13c89a6440530cb6f /src/modules/navigator/geofence.cpp
parent70d4ef480ac5461ef54ac72a54bd335007e233cc (diff)
downloadpx4-firmware-819822e1722dc31c0f97b8494c96a6c292b07185.tar.gz
px4-firmware-819822e1722dc31c0f97b8494c96a6c292b07185.tar.bz2
px4-firmware-819822e1722dc31c0f97b8494c96a6c292b07185.zip
navigator/geofence: add isEmpty() function and checks
Diffstat (limited to 'src/modules/navigator/geofence.cpp')
-rw-r--r--src/modules/navigator/geofence.cpp66
1 files changed, 36 insertions, 30 deletions
diff --git a/src/modules/navigator/geofence.cpp b/src/modules/navigator/geofence.cpp
index 1063fd2e2..a49a71e1b 100644
--- a/src/modules/navigator/geofence.cpp
+++ b/src/modules/navigator/geofence.cpp
@@ -81,41 +81,48 @@ bool Geofence::inside(const struct vehicle_global_position_s *vehicle)
bool Geofence::inside(double lat, double lon, float altitude)
{
if (valid()) {
- /* Vertical check */
- if (altitude > _altitude_max || altitude < _altitude_min)
- return false;
- /*Horizontal check */
- /* Adaptation of algorithm originally presented as
- * PNPOLY - Point Inclusion in Polygon Test
- * W. Randolph Franklin (WRF) */
+ if (!isEmpty()) {
+ /* Vertical check */
+ if (altitude > _altitude_max || altitude < _altitude_min)
+ return false;
+
+ /*Horizontal check */
+ /* Adaptation of algorithm originally presented as
+ * PNPOLY - Point Inclusion in Polygon Test
+ * W. Randolph Franklin (WRF) */
+
+ bool c = false;
+
+ struct fence_vertex_s temp_vertex_i;
+ struct fence_vertex_s temp_vertex_j;
+
+ /* Red until fence is finished */
+ for (unsigned i = 0, j = _verticesCount - 1; i < _verticesCount; j = i++) {
+ if (dm_read(DM_KEY_FENCE_POINTS, i, &temp_vertex_i, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
+ break;
+ }
+ if (dm_read(DM_KEY_FENCE_POINTS, j, &temp_vertex_j, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
+ break;
+ }
+
+ // skip vertex 0 (return point)
+ if (((temp_vertex_i.lon) >= lon != (temp_vertex_j.lon >= lon)) &&
+ (lat <= (temp_vertex_j.lat - temp_vertex_i.lat) * (lon - temp_vertex_i.lon) /
+ (temp_vertex_j.lon - temp_vertex_i.lon) + temp_vertex_i.lat)) {
+ c = !c;
+ }
- bool c = false;
-
- struct fence_vertex_s temp_vertex_i;
- struct fence_vertex_s temp_vertex_j;
-
- /* Red until fence is finished */
- for (int i = 0, j = _verticesCount - 1; i < _verticesCount; j = i++) {
- if (dm_read(DM_KEY_FENCE_POINTS, i, &temp_vertex_i, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
- break;
- }
- if (dm_read(DM_KEY_FENCE_POINTS, j, &temp_vertex_j, sizeof(struct fence_vertex_s)) != sizeof(struct fence_vertex_s)) {
- break;
- }
-
- // skip vertex 0 (return point)
- if (((temp_vertex_i.lon) >= lon != (temp_vertex_j.lon >= lon)) &&
- (lat <= (temp_vertex_j.lat - temp_vertex_i.lat) * (lon - temp_vertex_i.lon) /
- (temp_vertex_j.lon - temp_vertex_i.lon) + temp_vertex_i.lat)) {
- c = !c;
}
+ return c;
+ } else {
+ /* Empty fence --> accept all points */
+ return true;
}
- return c;
-
} else {
+ /* Invalid fence --> accept all points */
return true;
}
}
@@ -124,9 +131,8 @@ bool
Geofence::valid()
{
// NULL fence is valid
- if (_verticesCount == 0) {
+ if (isEmpty())
return true;
- }
// Otherwise
if ((_verticesCount < 4) || (_verticesCount > GEOFENCE_MAX_VERTICES)) {