navit  0.5.3-trunk
transform.c File Reference
#include <assert.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <glib.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"
#include "coord.h"
#include "debug.h"
#include "item.h"
#include "map.h"
#include "transform.h"
#include "projection.h"
#include "point.h"

Data Structures

struct  transformation
 The parameters needed to transform a map for display. More...
 
struct  coord_3d
 
struct  z_clip_result
 

Macros

#define _USE_MATH_DEFINES   1
 
#define POST_SHIFT   5
 Bitshift to apply during coordinate transformation. More...
 
#define HOG(t)   0
 
#define TWOPI   (M_PI*2)
 
#define GC2RAD(c)   ((c) * TWOPI/(1<<24))
 
#define minf(a, b)   ((a) < (b) ? (a) : (b))
 
#define GMETER   2.3887499999999999
 

Functions

static void transform_set_screen_dist (struct transformation *t, int dist)
 
static void transform_setup_matrix (struct transformation *t)
 
struct transformationtransform_new (struct pcoord *center, int scale, int yaw)
 
int transform_get_hog (struct transformation *this_)
 
void transform_set_hog (struct transformation *this_, int hog)
 
int transform_get_attr (struct transformation *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
 
int transform_set_attr (struct transformation *this_, struct attr *attr)
 
int transformation_get_order_base (struct transformation *this_)
 
void transform_set_order_base (struct transformation *this_, int order_base)
 
struct transformationtransform_dup (struct transformation *t)
 
void transform_to_geo (enum projection pro, const struct coord *c, struct coord_geo *g)
 Transform the coordinates of a geographical point from a coord representation to a geographical (lat, long) representation. More...
 
void transform_from_geo (enum projection pro, const struct coord_geo *g, struct coord *c)
 Transform the coordinates of a geographical point from a geographical (lat, long) representation to a coord representation. More...
 
void transform_from_to_count (struct coord *cfrom, enum projection from, struct coord *cto, enum projection to, int count)
 
void transform_from_to (struct coord *cfrom, enum projection from, struct coord *cto, enum projection to)
 
void transform_geo_to_cart (struct coord_geo *geo, navit_float a, navit_float b, struct coord_geo_cart *cart)
 Transforms geodetic to Cartesian coordinates. More...
 
void transform_cart_to_geo (struct coord_geo_cart *cart, navit_float a, navit_float b, struct coord_geo *geo)
 Transforms Cartesian to geodetic coordinates. More...
 
void transform_utm_to_geo (const double UTMEasting, const double UTMNorthing, int ZoneNumber, int NorthernHemisphere, struct coord_geo *geo)
 Converts UTM coords to lat/long. More...
 
static struct coord transform_correct_projection (struct transformation *t, enum projection required_projection, struct coord c)
 
static struct coord transform_shift_by_center_and_scale (struct transformation *t, struct coord c)
 
static struct coord_3d transform_rotate (struct transformation *t, struct coord c)
 
static struct coord_3d transform_z_clip (struct coord_3d c, struct coord_3d c_old, int zlimit)
 
static struct point transform_project_onto_view_plane (struct transformation *t, struct coord_3d c)
 
static int transform_points_too_close (struct point screen_point, struct point screen_point_old, int mindist)
 
static struct z_clip_result transform_z_clip_if_necessary (struct coord_3d coord, int zlimit, struct z_clip_result clip_result_old)
 
int transform_point (struct transformation *t, enum projection required_projection, struct coord *input, struct point *result)
 
int transform_point_buf (struct transformation *t, enum projection required_projection, struct coord *input, struct point *result, long result_size, int count, int mindist, int width, int *width_result)
 
static void transform_apply_inverse_matrix (struct transformation *t, struct coord_geo_cart *in, struct coord_geo_cart *out)
 
static int transform_zplane_intersection (struct coord_geo_cart *p1, struct coord_geo_cart *p2, navit_float z, struct coord_geo_cart *result)
 
static void transform_screen_to_3d (struct transformation *t, struct point *p, navit_float z, struct coord_geo_cart *cg)
 
static int transform_reverse_near_far (struct transformation *t, struct point *p, struct coord *c, int near, int far)
 
int transform_reverse (struct transformation *t, struct point *p, struct coord *c)
 
double transform_pixels_to_map_distance (struct transformation *transformation, int pixels)
 
enum projection transform_get_projection (struct transformation *this_)
 
void transform_set_projection (struct transformation *this_, enum projection pro)
 
static int min4 (int v1, int v2, int v3, int v4)
 
static int max4 (int v1, int v2, int v3, int v4)
 
struct map_selectiontransform_get_selection (struct transformation *this_, enum projection pro, int order)
 
struct coordtransform_center (struct transformation *this_)
 
struct coordtransform_get_center (struct transformation *this_)
 
void transform_set_center (struct transformation *this_, struct coord *c)
 
void transform_set_yaw (struct transformation *t, int yaw)
 
int transform_get_yaw (struct transformation *this_)
 
void transform_set_pitch (struct transformation *this_, int pitch)
 
int transform_get_pitch (struct transformation *this_)
 
void transform_set_roll (struct transformation *this_, int roll)
 
int transform_get_roll (struct transformation *this_)
 
void transform_set_distance (struct transformation *this_, int distance)
 
int transform_get_distance (struct transformation *this_)
 
void transform_set_scales (struct transformation *this_, int xscale, int yscale, int wscale)
 
void transform_set_screen_selection (struct transformation *t, struct map_selection *sel)
 
void transform_set_screen_center (struct transformation *t, struct point *p)
 
void transform_get_size (struct transformation *t, int *width, int *height)
 
void transform_setup_source_rect (struct transformation *t)
 
long transform_get_scale (struct transformation *t)
 
void transform_set_scale (struct transformation *t, long scale)
 
int transform_get_order (struct transformation *t)
 
static double transform_distance_garmin (struct coord *c1, struct coord *c2)
 
double transform_scale (int y)
 
double transform_distance (enum projection pro, struct coord *c1, struct coord *c2)
 Calculates the distance between two points. More...
 
void transform_project (enum projection pro, struct coord *c, int distance, int angle, struct coord *res)
 
double transform_polyline_length (enum projection pro, struct coord *c, int count)
 
static int transform_overflow_possible_if_squared (int count,...)
 
int transform_distance_sq (struct coord *c1, struct coord *c2)
 Determines the squared Mercator distance between two points. More...
 
navit_float transform_distance_sq_float (struct coord *c1, struct coord *c2)
 
int transform_distance_sq_pc (struct pcoord *c1, struct pcoord *c2)
 
int transform_distance_line_sq (struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
 Determines the point on a line segment that is closest to a reference point, and its distance from the reference point. More...
 
navit_float transform_distance_line_sq_float (struct coord *l0, struct coord *l1, struct coord *ref, struct coord *lpnt)
 
int transform_distance_polyline_sq (struct coord *c, int count, struct coord *ref, struct coord *lpnt, int *pos)
 Determines the point on a polyline that is closest to a reference point, and its distance from the reference point. More...
 
int transform_douglas_peucker (struct coord *in, int count, int dist_sq, struct coord *out)
 
int transform_douglas_peucker_float (struct coord *in, int count, navit_float dist_sq, struct coord *out)
 
void transform_print_deg (double deg)
 
int transform_get_angle_delta (struct coord *c1, struct coord *c2, int dir)
 Gets the bearing from one point to another. More...
 
int transform_within_border (struct transformation *this_, struct point *p, int border)
 
int transform_within_dist_point (struct coord *ref, struct coord *c, int dist)
 
int transform_within_dist_line (struct coord *ref, struct coord *c0, struct coord *c1, int dist)
 
int transform_within_dist_polyline (struct coord *ref, struct coord *c, int count, int close, int dist)
 
int transform_within_dist_polygon (struct coord *ref, struct coord *c, int count, int dist)
 
int transform_within_dist_item (struct coord *ref, enum item_type type, struct coord *c, int count, int dist)
 
void transform_copy (struct transformation *src, struct transformation *dst)
 
void transform_destroy (struct transformation *t)
 

Variables

static const navit_float gar2geo_units = 360.0/(1<<24)
 
static const navit_float geo2gar_units = 1/(360.0/(1<<24))
 

Detailed Description

Coordinate transformations and projections.

Macro Definition Documentation

◆ _USE_MATH_DEFINES

#define _USE_MATH_DEFINES   1

Navit, a modular navigation system. Copyright (C) 2005-2008 Navit Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

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 Street, Fifth Floor, Boston, MA 02110-1301, USA.

◆ GC2RAD

#define GC2RAD (   c)    ((c) * TWOPI/(1<<24))

◆ GMETER

#define GMETER   2.3887499999999999

◆ HOG

#define HOG (   t)    0

◆ minf

#define minf (   a,
 
)    ((a) < (b) ? (a) : (b))

◆ POST_SHIFT

#define POST_SHIFT   5

Bitshift to apply during coordinate transformation.

This bitshift is applied (left shift) during coordinate transformation (and later reversed). The transformation is performed with integer arithmetic, and this shift reduces rounding errors when converting floating point numbers to integers, particularly because some input values are fairly small (for example, the entries in the transformation matrix, struct transformation).

This works because the transformations involve only multiplications, so the shift can be applied to one factor and removed from the result.

The value is a compromise; if it is too small, rounding errors increase, if it is too large, signed integer calculations will overflow at high zoom levels (which is undefined behavior).

See also
transformation

◆ TWOPI

#define TWOPI   (M_PI*2)

Function Documentation

◆ max4()

static int max4 ( int  v1,
int  v2,
int  v3,
int  v4 
)
static

◆ min4()

static int min4 ( int  v1,
int  v2,
int  v3,
int  v4 
)
static

◆ transform_apply_inverse_matrix()

◆ transform_cart_to_geo()

void transform_cart_to_geo ( struct coord_geo_cart cart,
navit_float  a,
navit_float  b,
struct coord_geo geo 
)

Transforms Cartesian to geodetic coordinates.

This transforms ECEF Cartesian coordinates into geodetic coordinates, consisting of latitude and longitude.

Parameters
cartThe Cartesian coordinates
aSemi-minor axis of the ellipsoid
bSemi-minor axis of the ellipsoid
geoPoints to a structure that will receive the geodetic coordinates

References coord_geo::lat, lat, coord_geo::lng, M_PI, navit_atan, navit_sin, navit_sqrt, navit_tan, coord_geo_cart::x, coord_geo_cart::y, and coord_geo_cart::z.

◆ transform_center()

◆ transform_copy()

void transform_copy ( struct transformation src,
struct transformation dst 
)

◆ transform_correct_projection()

static struct coord transform_correct_projection ( struct transformation t,
enum projection  required_projection,
struct coord  c 
)
static

References D, e1, coord_geo::lat, coord_geo::lng, M, and M_PI.

Referenced by transform_point_buf().

◆ transform_destroy()

◆ transform_distance()

◆ transform_distance_garmin()

static double transform_distance_garmin ( struct coord c1,
struct coord c2 
)
static

◆ transform_distance_line_sq()

int transform_distance_line_sq ( struct coord l0,
struct coord l1,
struct coord ref,
struct coord lpnt 
)

Determines the point on a line segment that is closest to a reference point, and its distance from the reference point.

Parameters
l0The first coordinate of the line segment
l1The second coordinate of the line segment
refThe reference point
lpntReceives the coordinates of the point on the line segment that is closest to ref, can be NULL
Returns
The square of the Mercator distance between ref and lpnt, or INT_MAX if an overflow occurred

References c1, c2, transform_distance_sq(), transform_overflow_possible_if_squared(), coord::x, and coord::y.

Referenced by osd_route_guard_draw(), tracking_update(), tracking_value(), transform_distance_polyline_sq(), and transform_douglas_peucker().

◆ transform_distance_line_sq_float()

navit_float transform_distance_line_sq_float ( struct coord l0,
struct coord l1,
struct coord ref,
struct coord lpnt 
)

◆ transform_distance_polyline_sq()

int transform_distance_polyline_sq ( struct coord c,
int  count,
struct coord ref,
struct coord lpnt,
int *  pos 
)

Determines the point on a polyline that is closest to a reference point, and its distance from the reference point.

Parameters
cAn array containing the coordinates of the polyline
countNumber of elements in c
refThe reference point
lpntReceives the coordinates of the point on the polyline that is closest to ref, can be NULL
posReceives the index of the line segment containing lpnt, can be NULL
Returns
The square of the Mercator distance between ref and lpnt, or INT_MAX if an overflow occurred

References c, startup::pos, and transform_distance_line_sq().

Referenced by route_find_nearest_street(), and route_path_get_distances().

◆ transform_distance_sq()

int transform_distance_sq ( struct coord c1,
struct coord c2 
)

Determines the squared Mercator distance between two points.

Parameters
c0The first coordinate
c1The second coordinate
Returns
The squared distance between c1 and c2, or INT_MAX if an overflow occurs.

References c1, c2, and transform_overflow_possible_if_squared().

Referenced by osd_speed_cam_draw(), search_house_number_coordinate(), transform_distance_line_sq(), and transform_distance_sq_pc().

◆ transform_distance_sq_float()

navit_float transform_distance_sq_float ( struct coord c1,
struct coord c2 
)

References c1, and c2.

Referenced by transform_distance_line_sq_float().

◆ transform_distance_sq_pc()

int transform_distance_sq_pc ( struct pcoord c1,
struct pcoord c2 
)

◆ transform_douglas_peucker()

int transform_douglas_peucker ( struct coord in,
int  count,
int  dist_sq,
struct coord out 
)

◆ transform_douglas_peucker_float()

int transform_douglas_peucker_float ( struct coord in,
int  count,
navit_float  dist_sq,
struct coord out 
)

◆ transform_dup()

◆ transform_from_geo()

◆ transform_from_to()

void transform_from_to ( struct coord cfrom,
enum projection  from,
struct coord cto,
enum projection  to 
)

◆ transform_from_to_count()

void transform_from_to_count ( struct coord cfrom,
enum projection  from,
struct coord cto,
enum projection  to,
int  count 
)

References transform_from_geo(), and transform_to_geo().

Referenced by do_draw().

◆ transform_geo_to_cart()

void transform_geo_to_cart ( struct coord_geo geo,
navit_float  a,
navit_float  b,
struct coord_geo_cart cart 
)

Transforms geodetic to Cartesian coordinates.

This transforms geodetic coordinates (consisting of latitude and longitude) into ECEF Cartesian coordinates.

Parameters
geoThe WGS84 coordinates to convert
aSemi-minor axis of the ellipsoid
bSemi-minor axis of the ellipsoid
cartPoints to a structure that will receive the Cartesian coordinates

References coord_geo::lat, coord_geo::lng, navit_cos, navit_sin, coord_geo_cart::x, coord_geo_cart::y, and coord_geo_cart::z.

◆ transform_get_angle_delta()

int transform_get_angle_delta ( struct coord c1,
struct coord c2,
int  dir 
)

Gets the bearing from one point to another.

Parameters
c1The first coordinate
c2The second coordinate
dirThe direction: if it is -1, the bearing from c2 to c1 is returned, else the bearing from c1 to c2
Returns
The bearing in degrees,
0 <= result < 360
Definition: command.c:34
.

References c1, c2, and M_PI.

Referenced by NGQPoint::getPOI(), gui_internal_cmd_pois_item(), model_poi(), pedestrian_draw_arrows(), road_angle(), tracking_get_angles(), and vehicle_demo_timer().

◆ transform_get_attr()

int transform_get_attr ( struct transformation this_,
enum attr_type  type,
struct attr attr,
struct attr_iter iter 
)

References attr::num, attr::type, type, and attr::u.

◆ transform_get_center()

struct coord* transform_get_center ( struct transformation this_)

◆ transform_get_distance()

int transform_get_distance ( struct transformation this_)

References transformation::screen_dist.

Referenced by keypress().

◆ transform_get_hog()

int transform_get_hog ( struct transformation this_)

References HOG.

Referenced by keypress().

◆ transform_get_order()

int transform_get_order ( struct transformation t)

◆ transform_get_pitch()

int transform_get_pitch ( struct transformation this_)

References transformation::pitch.

Referenced by keypress(), and navit_get_attr().

◆ transform_get_projection()

◆ transform_get_roll()

int transform_get_roll ( struct transformation this_)

◆ transform_get_scale()

◆ transform_get_selection()

◆ transform_get_size()

◆ transform_get_yaw()

int transform_get_yaw ( struct transformation this_)

◆ transform_new()

◆ transform_overflow_possible_if_squared()

static int transform_overflow_possible_if_squared ( int  count,
  ... 
)
static

◆ transform_pixels_to_map_distance()

double transform_pixels_to_map_distance ( struct transformation transformation,
int  pixels 
)

Referenced by osd_scale_draw().

◆ transform_point()

int transform_point ( struct transformation t,
enum projection  required_projection,
struct coord input,
struct point result 
)

◆ transform_point_buf()

◆ transform_points_too_close()

static int transform_points_too_close ( struct point  screen_point,
struct point  screen_point_old,
int  mindist 
)
static

References point::x, and point::y.

Referenced by transform_point_buf().

◆ transform_polyline_length()

double transform_polyline_length ( enum projection  pro,
struct coord c,
int  count 
)

References c, and transform_distance().

Referenced by route_info_distances().

◆ transform_print_deg()

void transform_print_deg ( double  deg)

◆ transform_project()

void transform_project ( enum projection  pro,
struct coord c,
int  distance,
int  angle,
struct coord res 
)

◆ transform_project_onto_view_plane()

static struct point transform_project_onto_view_plane ( struct transformation t,
struct coord_3d  c 
)
static

Referenced by transform_point_buf().

◆ transform_reverse()

◆ transform_reverse_near_far()

◆ transform_rotate()

static struct coord_3d transform_rotate ( struct transformation t,
struct coord  c 
)
static

Referenced by transform_point_buf().

◆ transform_scale()

◆ transform_screen_to_3d()

◆ transform_set_attr()

int transform_set_attr ( struct transformation this_,
struct attr attr 
)

References attr::num, attr::type, and attr::u.

◆ transform_set_center()

void transform_set_center ( struct transformation this_,
struct coord c 
)

◆ transform_set_distance()

void transform_set_distance ( struct transformation this_,
int  distance 
)

◆ transform_set_hog()

void transform_set_hog ( struct transformation this_,
int  hog 
)

◆ transform_set_order_base()

void transform_set_order_base ( struct transformation this_,
int  order_base 
)

◆ transform_set_pitch()

void transform_set_pitch ( struct transformation this_,
int  pitch 
)

◆ transform_set_projection()

void transform_set_projection ( struct transformation this_,
enum projection  pro 
)

◆ transform_set_roll()

void transform_set_roll ( struct transformation this_,
int  roll 
)

◆ transform_set_scale()

◆ transform_set_scales()

void transform_set_scales ( struct transformation this_,
int  xscale,
int  yscale,
int  wscale 
)

◆ transform_set_screen_center()

void transform_set_screen_center ( struct transformation t,
struct point p 
)

References p, and transformation::screen_center.

Referenced by vehicle_draw().

◆ transform_set_screen_dist()

static void transform_set_screen_dist ( struct transformation t,
int  dist 
)
static

◆ transform_set_screen_selection()

◆ transform_set_yaw()

◆ transform_setup_matrix()

◆ transform_setup_source_rect()

◆ transform_shift_by_center_and_scale()

static struct coord transform_shift_by_center_and_scale ( struct transformation t,
struct coord  c 
)
static

Referenced by transform_point_buf().

◆ transform_to_geo()

◆ transform_utm_to_geo()

void transform_utm_to_geo ( const double  UTMEasting,
const double  UTMNorthing,
int  ZoneNumber,
int  NorthernHemisphere,
struct coord_geo geo 
)

Converts UTM coords to lat/long.

Equations from USGS Bulletin 1532.

Author
Chuck Gantz- chuck.nosp@m..gan.nosp@m.tz@gl.nosp@m.obal.nosp@m.star..nosp@m.com

Referenced by transform_to_geo().

◆ transform_within_border()

int transform_within_border ( struct transformation this_,
struct point p,
int  border 
)

◆ transform_within_dist_item()

int transform_within_dist_item ( struct coord ref,
enum item_type  type,
struct coord c,
int  count,
int  dist 
)

◆ transform_within_dist_line()

int transform_within_dist_line ( struct coord ref,
struct coord c0,
struct coord c1,
int  dist 
)

◆ transform_within_dist_point()

int transform_within_dist_point ( struct coord ref,
struct coord c,
int  dist 
)

◆ transform_within_dist_polygon()

int transform_within_dist_polygon ( struct coord ref,
struct coord c,
int  count,
int  dist 
)

◆ transform_within_dist_polyline()

int transform_within_dist_polyline ( struct coord ref,
struct coord c,
int  count,
int  close,
int  dist 
)

◆ transform_z_clip()

static struct coord_3d transform_z_clip ( struct coord_3d  c,
struct coord_3d  c_old,
int  zlimit 
)
static

◆ transform_z_clip_if_necessary()

static struct z_clip_result transform_z_clip_if_necessary ( struct coord_3d  coord,
int  zlimit,
struct z_clip_result  clip_result_old 
)
static

Referenced by transform_point_buf().

◆ transform_zplane_intersection()

static int transform_zplane_intersection ( struct coord_geo_cart p1,
struct coord_geo_cart p2,
navit_float  z,
struct coord_geo_cart result 
)
static

◆ transformation_get_order_base()

int transformation_get_order_base ( struct transformation this_)

Variable Documentation

◆ gar2geo_units

const navit_float gar2geo_units = 360.0/(1<<24)
static

Referenced by transform_to_geo().

◆ geo2gar_units

const navit_float geo2gar_units = 1/(360.0/(1<<24))
static

Referenced by transform_from_geo().