Contents of /trunk/src/geomath.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 226 - (hide annotations)
Wed Dec 2 20:05:52 2009 UTC (14 years, 6 months ago) by harbaum
File MIME type: text/plain
File size: 9868 byte(s)
Map nav source selection
1 harbaum 1 /*
2     * Copyright (C) 2008 Till Harbaum <till@harbaum.org>.
3     *
4     * This file is part of GPXView.
5     *
6     * GPXView is free software: you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * GPXView is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
18     */
19    
20     #include "gpxview.h"
21     #include <math.h>
22    
23 harbaum 198 #if defined(USE_MAEMO) && (MAEMO_VERSION_MAJOR >= 5)
24     #include <hildon/hildon-entry.h>
25     #endif
26    
27 harbaum 1 #define STR_NAN "----"
28    
29     typedef struct {
30     appdata_t *appdata;
31    
32     GtkWidget *lon1, *lat1, *dist1, *dir1;
33     GtkWidget *lon2, *lat2;
34    
35     GtkWidget *distance, *proj_lat, *proj_lon;
36     } math_dialog_state_t;
37    
38     static gboolean mark(GtkWidget *widget, gboolean valid) {
39     gtk_widget_set_state(widget, valid?GTK_STATE_NORMAL:TAG_STATE);
40     return valid;
41     }
42    
43     /* a label that is colored red when being "active" */
44     static GtkWidget *gtk_red_label_new(char *str) {
45     GdkColor color;
46 harbaum 198 GtkWidget *widget = left_label_new(str);
47 harbaum 1 gdk_color_parse("#ff0000", &color);
48     gtk_widget_modify_fg(widget, TAG_STATE, &color);
49     return widget;
50     }
51    
52     static void on_calc_clicked(GtkButton *button, gpointer user_data) {
53     math_dialog_state_t *state = (math_dialog_state_t*)user_data;
54     pos_t pos1, pos2;
55     gboolean pos1_ok = FALSE, pos2_ok = FALSE;
56     float dist1;
57     gboolean dist1_ok = FALSE;
58     float dir1;
59     gboolean dir1_ok = FALSE;
60    
61     /* parse input */
62 harbaum 221 pos1.lat = lat_entry_get(state->lat1);
63     pos1.lon = lon_entry_get(state->lon1);
64 harbaum 1 if(!isnan(pos1.lat) && !isnan(pos1.lon)) pos1_ok = TRUE;
65    
66 harbaum 221 pos2.lat = lat_entry_get(state->lat2);
67     pos2.lon = lon_entry_get(state->lon2);
68 harbaum 1 if(!isnan(pos2.lat) && !isnan(pos2.lon)) pos2_ok = TRUE;
69    
70 harbaum 223 dist1 = dist_entry_get(state->dist1, state->appdata->imperial);
71 harbaum 1 if(!isnan(dist1)) dist1_ok = TRUE;
72    
73 harbaum 223 dir1 = angle_entry_get(state->dir1);
74 harbaum 1 if(!isnan(dir1)) dir1_ok = TRUE;
75    
76     /* ------------------- do all calculations ------------------- */
77    
78    
79     /* ------------------- distance of coo1 and coo2 ------------------- */
80     if(mark(state->distance, pos1_ok && pos2_ok)) {
81     char str[32];
82     float dist = gpx_pos_get_distance(pos1, pos2, state->appdata->imperial);
83     distance_str(str, sizeof(str), dist, state->appdata->imperial);
84    
85     gtk_label_set_text(GTK_LABEL(state->distance), str);
86     } else
87     gtk_label_set_text(GTK_LABEL(state->distance), STR_NAN);
88    
89     // N 53° 09.033' W 001° 50.666' 100km / 30° = N 53° 55.616, W001° 04.850
90     /* ------------------- coordinate projection ---------------- */
91     mark(state->proj_lat, pos1_ok && dist1_ok && dir1_ok);
92     if(mark(state->proj_lon, pos1_ok && dist1_ok && dir1_ok)) {
93     pos_t pro;
94    
95     /* get great circle radius in miles/kilometers */
96     float gcrad = state->appdata->imperial?3959.0:6371.0;
97    
98     // from: http://www.movable-type.co.uk/scripts/latlong.html
99     pro.lat = asin(sin(pos1.lat/180*M_PI) * cos(dist1/gcrad) +
100     cos(pos1.lat/180*M_PI) * sin(dist1/gcrad) *
101     cos(dir1/180*M_PI) )/M_PI*180;
102     pro.lon = pos1.lon + atan2(sin(dir1/180*M_PI)*sin(dist1/gcrad)*
103     cos(pos1.lat/180*M_PI),
104     cos(dist1/gcrad)-sin(pos1.lat/180*M_PI)*
105     sin(pro.lat/180*M_PI))/M_PI*180;
106     pro.lon = fmodf(pro.lon+180,360) - 180; // normalise to -180...+180
107    
108 harbaum 221 lat_label_set(state->proj_lat, pro.lat);
109     lon_label_set(state->proj_lon, pro.lon);
110 harbaum 1
111     if(!isnan(pro.lat) && !isnan(pro.lon))
112     state->appdata->geomath = pro;
113     } else {
114     gtk_label_set_text(GTK_LABEL(state->proj_lat), STR_NAN);
115     gtk_label_set_text(GTK_LABEL(state->proj_lon), STR_NAN);
116     }
117     }
118    
119 harbaum 226 static void table_attach_button(GtkWidget *table, GtkWidget *button, int x, int y) {
120     #ifndef FREMANTLE
121     gtk_table_attach_defaults(GTK_TABLE(table), button, x, x+1, y, y+1);
122     #else
123     gtk_table_attach(GTK_TABLE(table), button, x, x+1, y, y+1, GTK_EXPAND | GTK_FILL, 0, 0, 0);
124     #endif
125     }
126    
127 harbaum 1 void geomath_dialog(appdata_t *appdata) {
128     static pos_t pos1 = { 0.0, 0.0 }, pos2 = { 0.0, 0.0 };
129     static float dist1 = 0.0;
130     static float dir1 = 0.0;
131     static gboolean is_imperial = FALSE;
132    
133     math_dialog_state_t state;
134    
135     /* this is quite ugly. It would be nice to run the entire system on */
136     /* one specific system (e.g. metric) and only convert for in- and output */
137     if(!appdata->imperial && is_imperial)
138     dist1 *= 6371.0/3959.0; /* we just switched to metric */
139     if(appdata->imperial && !is_imperial)
140     dist1 *= 3959.0/6371.0; /* we just switched to imperial */
141     is_imperial = appdata->imperial;
142    
143     state.appdata = appdata;
144    
145     #ifdef USE_MAEMO
146     if(appdata->cur_cache)
147     printf("current cache is %s\n", appdata->cur_cache->id);
148     else
149     printf("no current cache\n");
150     #endif
151    
152     GtkWidget *dialog = gtk_dialog_new_with_buttons(_("Geomath"),
153     GTK_WINDOW(appdata->window),
154     // GTK_DIALOG_NO_SEPARATOR |
155     GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
156     GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
157     NULL);
158    
159     #if defined(USE_MAEMO) && defined(HILDON_HELP)
160     hildon_help_dialog_help_enable(GTK_DIALOG(dialog), HELP_ID_GEOMATH,
161     appdata->osso_context);
162     #endif
163    
164     gtk_window_set_default_size(GTK_WINDOW(dialog), DIALOG_WIDTH, DIALOG_HEIGHT);
165    
166     /* do geomath dialog */
167     GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
168    
169     /* ------------------------- input area ------------------------- */
170 harbaum 214 GtkWidget *table = gtk_table_new(5, 5, FALSE);
171 harbaum 226 gtk_table_set_col_spacing(GTK_TABLE(table), 0, 20);
172 harbaum 214 gtk_table_set_col_spacing(GTK_TABLE(table), 2, 20);
173 harbaum 1
174 harbaum 214 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Latitude:")), 0, 1, 1, 2);
175     gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Longitude:")), 0, 1, 2, 3);
176 harbaum 1
177 harbaum 226 #ifndef FREMANTLE
178     gtk_table_attach_defaults(GTK_TABLE(table), gtk_label_new(_("Coordinate 1")), 1, 3, 0, 1);
179     #endif
180     table_attach_button(table, state.lat1 = lat_entry_new(pos1.lat), 1, 1);
181     table_attach_button(table, state.lon1 = lon_entry_new(pos1.lon), 1, 2);
182     table_attach_button(table, preset_coordinate_picker(appdata, state.lat1, state.lon1), 2,1);
183 harbaum 1
184 harbaum 226 #ifndef FREMANTLE
185     gtk_table_attach_defaults(GTK_TABLE(table), gtk_label_new(_("Coordinate 2")), 3, 5, 0, 1);
186     #endif
187     table_attach_button(table, state.lat2 = lat_entry_new(pos2.lat), 3, 1);
188     table_attach_button(table, state.lon2 = lon_entry_new(pos2.lon), 3, 2);
189     table_attach_button(table, preset_coordinate_picker(appdata, state.lat2, state.lon2), 4, 1);
190 harbaum 1
191 harbaum 198 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Distance:")), 0, 1, 3, 4);
192 harbaum 226 table_attach_button(table, state.dist1 = dist_entry_new(dist1, appdata->imperial), 1,3);
193 harbaum 214
194 harbaum 198 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Direction:")), 0, 1, 4, 5);
195 harbaum 226 table_attach_button(table, state.dir1 = angle_entry_new(dir1), 1,4);
196 harbaum 1
197     gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
198    
199     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0);
200    
201     /* ------------------------- do-it-button ------------------------- */
202    
203     GtkWidget *button = gtk_button_new_with_label(_("Calculate!"));
204 harbaum 198 g_signal_connect(button, "clicked", (GCallback)on_calc_clicked, &state);
205    
206 harbaum 223 #ifdef FREMANTLE
207 harbaum 133 hildon_gtk_widget_set_theme_size(button,
208     (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH));
209 harbaum 214 gtk_table_attach(GTK_TABLE(table), button, 3,5,3,5, GTK_EXPAND, GTK_EXPAND, 0, 0);
210 harbaum 198 #else
211     /* in non-maemo5 the button has its own row */
212     hbox = gtk_hbox_new(FALSE, 0);
213 harbaum 1 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
214     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, FALSE, 0);
215 harbaum 198 #endif
216 harbaum 1
217     /* ------------------------- output area ------------------------- */
218    
219     table = gtk_table_new(3, 3, FALSE);
220    
221 harbaum 198 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Distance = ")),
222 harbaum 1 0, 1, 0, 1);
223     gtk_table_attach_defaults(GTK_TABLE(table), state.distance = gtk_red_label_new(STR_NAN),
224     1, 3, 0, 1);
225    
226 harbaum 198 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Projection = ")),
227 harbaum 1 0, 1, 1, 2);
228     gtk_table_attach_defaults(GTK_TABLE(table), state.proj_lat = gtk_red_label_new(STR_NAN),
229     1, 2, 1, 2);
230     gtk_table_attach_defaults(GTK_TABLE(table), state.proj_lon = gtk_red_label_new(STR_NAN),
231     2, 3, 1, 2);
232     #if 0
233 harbaum 198 gtk_table_attach_defaults(GTK_TABLE(table), left_label_new(_("Middle = ")),
234 harbaum 1 0, 1, 2, 3);
235     gtk_table_attach_defaults(GTK_TABLE(table), state.middle_lat = gtk_red_label_new(STR_NAN),
236     1, 2, 2, 3);
237     gtk_table_attach_defaults(GTK_TABLE(table), state.middle_lon = gtk_red_label_new(STR_NAN),
238     2, 3, 2, 3);
239     #endif
240    
241     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), table, TRUE, TRUE, 0);
242    
243    
244     gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CLOSE);
245    
246     gtk_widget_show_all(dialog);
247     gtk_dialog_run(GTK_DIALOG(dialog));
248    
249     /* copy values back to local static variables so they re-appear if */
250     /* the dialog is re-opened, convert illegal values (NAN) to 0 */
251    
252 harbaum 221 pos1.lat = lat_entry_get(state.lat1); if(isnan(pos1.lat)) pos1.lat=0;
253     pos1.lon = lon_entry_get(state.lon1); if(isnan(pos1.lon)) pos1.lon=0;
254     pos2.lat = lat_entry_get(state.lat2); if(isnan(pos2.lat)) pos2.lat=0;
255     pos2.lon = lon_entry_get(state.lon2); if(isnan(pos2.lon)) pos2.lon=0;
256 harbaum 223 dist1 = dist_entry_get(state.dist1, state.appdata->imperial);
257     if(isnan(dist1)) dist1=0;
258     dir1 = angle_entry_get(state.dir1); if(isnan(dir1)) dir1=0;
259 harbaum 1
260     gtk_widget_destroy(dialog);
261     }