-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoordinates.cpp
211 lines (184 loc) · 5.13 KB
/
Coordinates.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "Coordinates.hpp"
#include <iostream>
#include <stdlib.h>
#include <math.h>
uint32_t Coordinates::toNormalizedLon(const std::string& coord)
{
size_t pos = coord.find(".");
int signe = 1;
long long signedInt;
if(pos != std::string::npos) signedInt = atoll(coord.substr(0,pos).c_str());
else signedInt = atoll(coord.c_str());
if (coord.at(0) == '-') signe = -1;
uint32_t integerPart = 180 + signedInt;
std::string sDecimalPart;
if(pos == std::string::npos) sDecimalPart = "";
else sDecimalPart = coord.substr(pos+1);
while(sDecimalPart.size() < 7) sDecimalPart += '0';
uint32_t decimalPart = atol(sDecimalPart.c_str());
uint64_t intCoord;
if(signe == -1)
intCoord = (integerPart) * 10000000 - decimalPart;
else
intCoord = ( integerPart) * 10000000 + decimalPart;
uint32_t normalized = 0;
uint32_t toCompare = 3600000000ULL;
normalized = 1;
for (int i = 0; i < 32; i++ )
{
normalized <<= 1;
if (intCoord > toCompare)
{
normalized |= 1;
intCoord = intCoord - toCompare;
}
toCompare >>= 1;
}
return normalized;
}
uint32_t Coordinates::toNormalizedLat(const std::string& coord)
{
size_t pos = coord.find(".");
int signe = 1;
long long signedInt;
if(pos != std::string::npos) signedInt = atoll(coord.substr(0,pos).c_str());
else signedInt = atoll(coord.c_str());
if (coord.at(0) == '-') signe = -1;
uint32_t integerPart = 90 - signedInt;
std::string sDecimalPart;
if(pos == std::string::npos) sDecimalPart = "";
else sDecimalPart = coord.substr(pos+1);
while(sDecimalPart.size() < 7) sDecimalPart += '0';
uint32_t decimalPart = atol(sDecimalPart.c_str());
uint32_t intCoord;
if(signe == 1)
intCoord = (integerPart) * 10000000 - decimalPart;
else
intCoord = (integerPart)* 10000000 + decimalPart;
uint32_t normalized = 0;
uint32_t toCompare = 1800000000ULL;
normalized = 1;
for (int i = 0; i < 32; i++ )
{
normalized <<= 1;
if (intCoord > toCompare)
{
normalized |= 1;
intCoord = intCoord - toCompare;
}
toCompare >>= 1;
}
return normalized;
}
double Coordinates::fromNormalizedLon(const uint32_t coord)
{
double res = ((double) coord * 360.0 /(double) 0b10000000000000000000000000000000) - 180.0;
return res;
}
double Coordinates::fromNormalizedLat(const uint32_t coord)
{
double res = 90.0 - ((double) coord * 180.0 /(double) 0b10000000000000000000000000000000);
return res;
}
/*uint32_t Coordinates::toNormalizedLat(const std::string& coord)
{
double angle = atof(coord.c_str());
double angleRadians = angle * M_PI / 180.0;
bool signe = false;
if(angleRadians < 0) {
angleRadians = -1.0 * angleRadians;
signe = true;
}
double dist = log(tan((angleRadians/2.0 + M_PI/4.0)));
uint32_t normalized = 0;
if(signe) normalized = 0b1000000000000000000000000000000 * (1 + dist/(2.5 * M_PI));
else normalized = 0b1000000000000000000000000000000 * (1 - dist/(2.5 * M_PI));
return normalized;
}
double Coordinates::fromNormalizedLat(uint32_t coord)
{
double dist;
double res;
int signe;
if(coord < 0b1000000000000000000000000000000)
{
signe = 1;
dist =( 0b1000000000000000000000000000000 - coord) *(2.5 * M_PI) / (double)0b1000000000000000000000000000000;
}
else
{
signe = -1;
dist =( coord - 0b1000000000000000000000000000000) *(2.5 * M_PI) / (double)0b1000000000000000000000000000000 ;
}
res = signe * (atan(exp(dist))*2 - M_PI/2.0) * 180.0 / M_PI;
return res;
}*/
std::string Coordinates::toHex(const uint32_t number)
{
static const char* digits = "0123456789ABCDEF";
std::string rc(8,'0');
for (size_t i=0, j=(7)*4 ; i<8; ++i,j-=4)
rc[i] = digits[(number>>j) & 0x0f];
return rc;
}
uint32_t Coordinates::fromHex(const std::string& s)
{
int len = s.size();
const char* b = s.c_str();
uint32_t result = 0;
for(int i = 0; i < len ; i++ )
{
result <<= 4;
switch(b[i])
{
case '0':
break;
case '1':
result |= 1;
break;
case '2':
result |= 2;
break;
case '3':
result |= 3;
break;
case '4':
result |= 4;
break;
case '5':
result |= 5;
break;
case '6':
result |= 6;
break;
case '7':
result |= 7;
break;
case '8':
result |= 8;
break;
case '9':
result |= 9;
break;
case 'A':
result |= 10;
break;
case 'B':
result |= 11;
break;
case 'C':
result |= 12;
break;
case 'D':
result |= 13;
break;
case 'E':
result |= 14;
break;
case 'F':
result |= 15;
break;
}
}
return result;
}