mappix/geo-tools.h

133 lines
4.4 KiB
C

/*
* geo-tools.h
*
* Copyright (C) 2006,2007 Nils Faerber <nils.faerber@kernelconcepts.de>
*
* parts taken from Maemo-Mapper (GPL)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef _GEO_TOOLS_H
#define _GEO_TOOLS_H
/* from GNU glibc math.h - since we do not need __USE_GNU else
* we copy it here
*/
#ifndef M_PIl
#define M_PIl 3.1415926535897932384626433832795029L
#endif
/*
* the following unit-latlpon conversions are
* shamelessly stolen^Wborrowed from Maemo-Mapper ;)
* ...and will be returned in time
* Some minor modifications have been done...
*/
#define pixel2unit(pixel) ((pixel) << _zoom)
#define unit2pixel(pixel) ((pixel) >> _zoom)
/* use 31 bits for best accuracy */
#define WORLD_SIZE_UNITS (1 << 30 /*26*/)
#define MERCATOR_SPAN (-6.28318377773622f)
#define MERCATOR_TOP (3.14159188886811f)
#define latlon2unit(lat, lon, unitx, unity) { \
gdouble tmp; \
unitx = (lon + 180.f) * (WORLD_SIZE_UNITS / 360.f) + 0.5f; \
tmp = sin(lat * (M_PIl / 180.f)); \
unity = 0.5f + (WORLD_SIZE_UNITS / MERCATOR_SPAN) \
* (log((1.f + tmp) / (1.f - tmp)) * 0.5f - MERCATOR_TOP); \
}
#define unit2latlon(unitx, unity, lat, lon) { \
(lon) = ((unitx) * (360.f / WORLD_SIZE_UNITS)) - 180.f; \
(lat) = (360.f * (atan(exp(((unity) \
* (MERCATOR_SPAN / WORLD_SIZE_UNITS)) \
+ MERCATOR_TOP)))) * (1.f / M_PIl) - 90.f; \
}
#define MACRO_RECALC_OFFSET() { \
_offsetx = grid2pixel( \
unit2grid(_center.unitx) \
- _screen_grids_halfwidth \
- tile2grid(_base_tilex)); \
_offsety = grid2pixel( \
unit2grid(_center.unity) \
- _screen_grids_halfheight \
- tile2grid(_base_tiley)); \
}
#define MACRO_RECALC_FOCUS_BASE() { \
_focus.unitx = x2unit(_screen_width_pixels * _center_ratio / 20); \
_focus.unity = y2unit(_screen_height_pixels * _center_ratio / 20); \
}
#define MACRO_RECALC_FOCUS_SIZE() { \
_focus_unitwidth = pixel2unit( \
(10 - _center_ratio) * _screen_width_pixels / 10); \
_focus_unitheight = pixel2unit( \
(10 - _center_ratio) * _screen_height_pixels / 10); \
}
#define MACRO_RECALC_CENTER_BOUNDS() { \
_min_center.unitx = pixel2unit(grid2pixel(_screen_grids_halfwidth)); \
_min_center.unity = pixel2unit(grid2pixel(_screen_grids_halfheight)); \
_max_center.unitx = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfwidth) - 1; \
_max_center.unity = WORLD_SIZE_UNITS-grid2unit(_screen_grids_halfheight)- 1; \
}
#define MACRO_INIT_TRACK(track) { \
(track).head = (track).tail = g_new(TrackPoint, ARRAY_CHUNK_SIZE); \
(track).tail->unitx = (track).tail->unity = 0; \
(track).cap = (track).head + ARRAY_CHUNK_SIZE; \
(track).whead = g_new(WayPoint, ARRAY_CHUNK_SIZE); \
(track).wtail = (track).whead - 1; \
(track).wcap = (track).whead + ARRAY_CHUNK_SIZE; \
}
#define MACRO_CLEAR_TRACK(track) if((track).head) { \
WayPoint *curr; \
g_free((track).head); \
(track).head = (track).tail = (track).cap = NULL; \
for(curr = (track).whead - 1; curr++ != (track).wtail; ) \
g_free(curr->desc); \
g_free((track).whead); \
(track).whead = (track).wtail = (track).wcap = NULL; \
}
#define DISTANCE_ROUGH(point) \
(abs((gint)((point).unitx) - _pos.unitx) \
+ abs((gint)((point).unity) - _pos.unity))
#define MACRO_QUEUE_DRAW_AREA() \
gtk_widget_queue_draw_area( \
_map_widget, \
0, 0, \
_screen_width_pixels, \
_screen_height_pixels)
#define KEEP_DISPLAY_ON() { \
/* Note that the flag means keep on ONLY when fullscreen. */ \
if(_always_keep_on || _fullscreen) \
{ \
osso_display_state_on(_osso); \
osso_display_blanking_pause(_osso); \
} \
}
#endif