-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinate.cpp
More file actions
144 lines (116 loc) · 4.09 KB
/
coordinate.cpp
File metadata and controls
144 lines (116 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***
* File: coordinate.cpp
* Author: Nikolai
*
* Created on 1. januar 2013, 17:07
*/
#include "coordinate.h"
using namespace std;
/**
* @function init()
*
* initializes various required functionality for the class and also calls
* read file function within fileRead class and returns the vectors containing
* the data from the csv file.
*
* then it places the data from the csv file into a vector of waypoint structs
* accordingly
*
* @return - returns true to allow checking that the initialization
* was successful.
*/
bool coordinate::init(){
fRead.readFile("data.csv");
int s = fRead.getVectLen();
tempLat.reserve(s);
tempLon.reserve(s);
fRead.getVector(tempLat, tempLon);
for (unsigned int i = 0; i < s; i++){
wpt.push_back({tempLat.at(i), tempLon.at(i)});
}
tempLat.clear();
tempLon.clear();
//TODO: sort out full c++0x support and implement .shrink_to_fit()
initialised = true;
return initialised;
}
/**
* @function: private arcInRadians()
*
* Uses the law of haversine to calculate the great circle distance between
* coordinates
*
* @param latFrom - latitude of coordinate from
* @param lonFrom - longitude of coordinate from
* @param latTo - latitude of coordinate to
* @param lonTo - longitude of coordinate to
* @return - the arc in radians
*/
double coordinate::arcInRadians(double latFrom, double lonFrom, double latTo, double lonTo) {
double latitudeArc = (latFrom - latTo) * DEG_TO_RAD;
double longitudeArc = (lonFrom - lonTo) * DEG_TO_RAD;
double latitudeH = sin(latitudeArc * 0.5);
latitudeH *= latitudeH;
double lontitudeH = sin(longitudeArc * 0.5);
lontitudeH *= lontitudeH;
double tmp = cos(latFrom*DEG_TO_RAD) * cos(latTo*DEG_TO_RAD);
return 2.0 * asin(sqrt(latitudeH + tmp*lontitudeH));
}
/**
* @function: public distanceInMeters()
*
* takes in parameters mentioned below, multiplies the earth radius in meters
* with the returned value from @function arcInRadians() and returns it as a
* distance in meters
*
* @param latFrom - latitude of coordinate from
* @param lonFrom - longitude of coordinate from
* @param latTo - latitude of coordinate to
* @param lonTo - longitude of coordinate to
* @return - returns the distance between the coordiantes in meters
*/
double coordinate::distanceInMeters(double latFrom, double lonFrom, double latTo, double lonTo) {
return EARTH_RADIUS_IN_METERS*arcInRadians(latFrom, lonFrom, latTo, lonTo);
}
/**
* @function: public nearestWaypt()
*
* takes in current waypoint, calculates distance to all other waypoints in
* vector of waypoints and returns the id of the nearest waypoint
*
* @param wptCur - current waypoint
* @return - returns the ID of the nearest waypoint
*
* @todo: figure out if it is needed to store the distance to nearest waypoint.
*/
int coordinate::nearestWaypt(int wptCur) {
double latCur, lonCur, curShortest, d;
curShortest = 40000000; //40000000 is roughly the circumference of the earth
int id = 0;
coordinate::getPos(wptCur, latCur, lonCur);
for(int i = 0; i < wpt.size(); i++) {
d = coordinate::distanceInMeters(latCur, lonCur, wpt.at(i).lat, wpt.at(i).lon);
cout << "dist to: [" << i << "] is: " << d << endl;
if (i != wptCur) {
if (d < curShortest) {
curShortest = d;
id = i;
}
}
}
return id;
}
void coordinate::add(double flat, double flon){
tempLat.push_back(flat);
tempLon.push_back(flon);
}
void coordinate::getPos(int i, double& lat, double& lon) {
lat = wpt.at(i).lat;
lon = wpt.at(i).lon;
return;
}
void coordinate::printVectors() {
for(unsigned int i = 0; i < wpt.size(); i++){
cout << "lat: " << wpt.at(i).lat << " lon: " << wpt.at(i).lon << endl;
}
}