]> Pileus Git - grits/blob - src/gis-world.c
Reorganize BMNG and SRTM into plugins
[grits] / src / gis-world.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <glib.h>
19 #include <math.h>
20
21 #include "gis-marshal.h"
22 #include "gis-world.h"
23
24 /* Constants */
25 double WGS884_SEMI_MAJOR = 6378137.0;      // a
26 double WGS884_SEMI_MINOR = 6356752.314245; // b
27 double WGS884_INV_FLAT   = 298.257223563;  // 1/f
28
29 enum {
30         SIG_REFRESH,
31         SIG_OFFLINE,
32         NUM_SIGNALS,
33 };
34 static guint signals[NUM_SIGNALS];
35
36
37 /* Signal helpers */
38 static void _gis_world_emit_refresh(GisWorld *world)
39 {
40         g_signal_emit(world, signals[SIG_REFRESH], 0);
41 }
42 static void _gis_world_emit_offline(GisWorld *world)
43 {
44         g_signal_emit(world, signals[SIG_OFFLINE], 0,
45                         world->offline);
46 }
47
48 /***********
49  * Methods *
50  ***********/
51 GisWorld *gis_world_new()
52 {
53         g_debug("GisWorld: new");
54         return g_object_new(GIS_TYPE_WORLD, NULL);
55 }
56
57 void gis_world_refresh(GisWorld *world)
58 {
59         g_debug("GisWorld: refresh");
60         _gis_world_emit_refresh(world);
61 }
62
63 void gis_world_set_offline(GisWorld *world, gboolean offline)
64 {
65         g_assert(GIS_IS_WORLD(world));
66         g_debug("GisWorld: set_offline - %d", offline);
67         world->offline = offline;
68         _gis_world_emit_offline(world);
69 }
70
71 gboolean gis_world_get_offline(GisWorld *world)
72 {
73         g_assert(GIS_IS_WORLD(world));
74         g_debug("GisWorld: get_offline - %d", world->offline);
75         return world->offline;
76 }
77
78
79 /****************
80  * GObject code *
81  ****************/
82 G_DEFINE_TYPE(GisWorld, gis_world, G_TYPE_OBJECT);
83 static void gis_world_init(GisWorld *self)
84 {
85         g_debug("GisWorld: init");
86         /* Default values */
87         self->offline = FALSE;
88 }
89 static void gis_world_dispose(GObject *gobject)
90 {
91         g_debug("GisWorld: dispose");
92         /* Drop references to other GObjects */
93         G_OBJECT_CLASS(gis_world_parent_class)->dispose(gobject);
94 }
95 static void gis_world_finalize(GObject *gobject)
96 {
97         g_debug("GisWorld: finalize");
98         GisWorld *self = GIS_WORLD(gobject);
99         G_OBJECT_CLASS(gis_world_parent_class)->finalize(gobject);
100 }
101 static void gis_world_class_init(GisWorldClass *klass)
102 {
103         g_debug("GisWorld: class_init");
104         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
105         gobject_class->dispose      = gis_world_dispose;
106         gobject_class->finalize     = gis_world_finalize;
107         signals[SIG_REFRESH] = g_signal_new(
108                         "refresh",
109                         G_TYPE_FROM_CLASS(gobject_class),
110                         G_SIGNAL_RUN_LAST,
111                         0,
112                         NULL,
113                         NULL,
114                         g_cclosure_marshal_VOID__VOID,
115                         G_TYPE_NONE,
116                         0);
117         signals[SIG_OFFLINE] = g_signal_new(
118                         "offline",
119                         G_TYPE_FROM_CLASS(gobject_class),
120                         G_SIGNAL_RUN_LAST,
121                         0,
122                         NULL,
123                         NULL,
124                         g_cclosure_marshal_VOID__BOOLEAN,
125                         G_TYPE_NONE,
126                         1,
127                         G_TYPE_BOOLEAN);
128 }
129
130
131 /******************
132  * Global helpers *
133  ******************/
134 void lle2xyz(gdouble lat, gdouble lon, gdouble elev,
135                 gdouble *x, gdouble *y, gdouble *z)
136 {
137         gdouble rad  = elev2rad(elev);
138         gdouble azim = lon2azim(lon);
139         gdouble incl = lat2incl(lat);
140         *z = rad * cos(azim) * sin(incl);
141         *x = rad * sin(azim) * sin(incl);
142         *y = rad * cos(incl);
143 }
144
145 void xyz2lle(gdouble x, gdouble y, gdouble z,
146                 gdouble *lat, gdouble *lon, gdouble *elev)
147 {
148         gdouble rad = sqrt(x*x + y*y + z*z);
149         *lat  = incl2lat(acos(y / rad));
150         *lon  = azim2lon(atan2(x,z));
151         *elev = rad2elev(rad);
152 }
153
154
155 gdouble ll2m(gdouble lon_dist, gdouble lat)
156 {
157         gdouble azim = (-lat+90)/180*M_PI;
158         gdouble rad  = sin(azim) * EARTH_R;
159         gdouble circ = 2 * M_PI * rad;
160         return lon_dist/360 * circ;
161 }
162
163 gdouble distd(gdouble *a, gdouble *b)
164 {
165         return sqrt((a[0]-b[0])*(a[0]-b[0]) +
166                     (a[1]-b[1])*(a[1]-b[1]) +
167                     (a[2]-b[2])*(a[2]-b[2]));
168 }