forked from dkogan/horizonator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoints_of_interest.c
85 lines (65 loc) · 1.83 KB
/
points_of_interest.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "points_of_interest.h"
static struct poi_t pois[] = {
#include "features_generated.h"
};
#define MAX_MARKER_DIST 35000.0
#define MIN_MARKER_DIST 50.0
const float Rearth = 6371000.0;
static int N_active_pois;
static int* active_pois = NULL;
static float lat0, lon0, cos_lat0_sq;
static bool within( int ipoi )
{
float arclen( float lat1, float lon1 )
{
// On the surface of the earth the arclen is dtheta*Rearth
//
// Given v0,v1, |dth| ~ |sin(dth)| = | v0 x v1 |
//
// v = [ cos(lat) cos(lon)
// cos(lat) sin(lon)
// sin(lat) ]
//
// |v0 x v1| ~ sqrt( cos(lat0)^2 cos(lat1)^2 dlon^2 + dlat^2 )
float cos_lat1_sq = cosf(lat1);
cos_lat1_sq *= cos_lat1_sq;
float dlat = lat1 - lat0;
float dlon = lon1 - lon0;
return Rearth * sqrtf( dlon*dlon * cos_lat0_sq * cos_lat1_sq +
dlat*dlat );
}
float len = arclen( pois[ipoi].lat, pois[ipoi].lon );
return MIN_MARKER_DIST < len && len < MAX_MARKER_DIST;
}
static void addToActive( int ipoi )
{
active_pois[N_active_pois++] = ipoi;
}
void initPOIs( float lat, float lon )
{
// alloc an upper-bound amount of memory. It's probably too much, but I know
// we'll never run out
int Npois = sizeof(pois) / sizeof(pois[0]);
if( active_pois == NULL )
active_pois = malloc(Npois * sizeof(active_pois[0]) );
lat0 = lat;
lon0 = lon;
cos_lat0_sq = cosf( lon );
cos_lat0_sq *= cos_lat0_sq;
N_active_pois = 0;
for( int i=0; i<Npois; i++ )
if( within( i ) )
addToActive( i );
}
// caller may modify the indices and the poi internals
void getPOIs( int** _indices, int* _N,
struct poi_t** _pois)
{
*_indices = active_pois;
*_N = N_active_pois;
*_pois = pois;
}