]> err.no Git - mapper/blob - src/path.h
More map widget integration changes
[mapper] / src / path.h
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2008 Kaj-Michael Lang
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifndef _PATH_H
22 #define _PATH_H
23
24 #include <glib.h>
25 #include <glib-object.h>
26 #include <gtk/gtk.h>
27
28 G_BEGIN_DECLS
29
30 #define PATH_TYPE                               (path_get_type ())
31 #define PATH(obj)                               (G_TYPE_CHECK_INSTANCE_CAST ((obj), PATH_TYPE, Path))
32 #define PATH_CLASS(klass)               (G_TYPE_CHECK_CLASS_CAST ((klass), PATH_TYPE, PathClass))
33 #define IS_PATH(obj)                    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PATH_TYPE))
34 #define IS_PATH_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE ((klass), PATH_TYPE))
35 #define PATH_GET_CLASS(obj)             (G_TYPE_INSTANCE_GET_CLASS ((obj), PATH_TYPE, PathClass))
36
37 typedef enum {
38         PATH_TYPE_TRACK=0,
39         PATH_TYPE_ROUTE,
40         PATH_TYPE_FRIEND,
41 } PathType;
42
43 /* Path store items */
44 typedef enum {
45     PATH_LATLON,
46     PATH_DISTANCE,
47     PATH_WAYPOINT,
48     PATH_LAT,
49     PATH_LON,
50     PATH_NUM_COLUMNS
51 } PathStoreList;
52
53
54 /* Fixed ID's */
55 #define PATH_ID_MY_TRACK        (1)
56 #define PATH_ID_MY_ROUTE        (2)
57
58 /** 
59  * A general definition of a point in the Map unit system. 
60  */
61 typedef struct _Point Point;
62 struct _Point {
63         guint unitx;
64         guint unity;
65         time_t time;
66         gfloat altitude;
67 };
68
69 #define DISTANCE_SQUARED(a, b) \
70         ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx)) \
71         + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
72
73 /**
74  * A latitude/longitue pair
75  */
76 typedef struct _LatLon LatLon;
77 struct _LatLon {
78         gdouble lat;
79         gdouble lon;
80 };
81
82 /** 
83  * A WayPoint, which is a Point with a description. 
84  */
85 typedef struct _WayPoint WayPoint;
86 struct _WayPoint {
87         Point *point;
88         gchar *desc;
89 };
90
91 /** 
92  * Path GObject
93  */
94 typedef struct _Path Path;
95 struct _Path {
96         GObject parent;
97
98         PathType type;
99         guint id;
100         gint sensitivity;
101
102         /* Path points */
103         Point *head;            /* points to first element in array; NULL if empty. */
104         Point *tail;            /* points to last element in array. */
105         Point *cap;                     /* points after last slot in array. */
106
107         /* Path bounding box */
108         LatLon min;
109         LatLon max;
110
111         /* Path waypoints */
112         WayPoint *whead;        /* points to first element in array; NULL if empty. */
113         WayPoint *wtail;        /* points to last element in array. */
114         WayPoint *wcap;         /* points after last slot in array. */
115
116         /* Internal waypoint data */
117         Point *near_point;
118         guint64 near_point_dist_squared;
119
120         /* next_way is what we currently interpret to be the next waypoint. */
121         WayPoint *next_way;
122         guint64 next_way_dist_squared;
123
124         /* next_wpt is the route point immediately following next_way. */
125         Point *next_wpt;
126         guint64 next_wpt_dist_squared;
127
128         /* Path statistics */
129         guint32 points;
130         guint32 wpcnt;          /* Auto waypoint number counter */
131         gdouble length;
132         gdouble tspeed;
133         gdouble avgspeed;
134         gfloat maxspeed;
135
136         /* GPX metadata fields */
137         gchar *name;
138         gchar *desc;
139         gchar *author;
140         gchar *keywords;
141         gchar *copyright;
142         gchar *src;
143         time_t time;
144 };
145
146 typedef struct _PathClass PathClass;
147 struct _PathClass {
148         GObjectClass parent;
149
150         void (*new_point) (Path *path);
151         void (*new_break) (Path *path);
152         void (*new_waypoint) (Path *path);
153 };
154
155 /* Null point */
156 Point _point_null;
157
158 Path *path_new(PathType type, guint id);
159 void path_free(Path *p);
160 void path_clear(Path *p);
161
162 Point *path_find_last_point(Path *path);
163
164 gboolean path_resize(Path *path, guint size);
165 gboolean path_wresize(Path *path, guint wsize);
166
167 gdouble path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon);
168
169 void path_find_nearest_point(Path *path);
170 gboolean path_update_nears(Path *route, Point *point, gboolean quick);
171
172 gboolean path_add_break(Path *path);
173 void path_insert_mark_text(Path *path, gchar *text);
174 void path_insert_mark_autonumber(Path *path);
175 void path_insert_mark_audio(Path *path, gchar *audio);
176
177 gboolean path_get_waypoint_latlon(WayPoint *way, gdouble *lat, gdouble *lon);
178
179 GtkListStore *path_get_waypoints_store(Path *path);
180
181 #define MACRO_PATH_INIT(path) { \
182         (path).head = (path).tail = g_new0(Point, ARRAY_CHUNK_SIZE); \
183         *((path).tail) = _point_null; \
184         (path).cap = (path).head + ARRAY_CHUNK_SIZE; \
185         (path).whead = g_new0(WayPoint, ARRAY_CHUNK_SIZE); \
186         (path).wtail = (path).whead; \
187         (path).wcap = (path).whead + ARRAY_CHUNK_SIZE; \
188 }
189
190 #define MACRO_PATH_FREE(path) if((path).head) { \
191         WayPoint *curr; \
192         g_free((path).head); \
193         (path).head = (path).tail = (path).cap = NULL; \
194         for(curr = (path).whead; curr++ != (path).wtail; ) \
195                 g_free(curr->desc); \
196         g_free((path).whead); \
197         (path).whead = (path).wtail = (path).wcap = NULL; \
198 }
199
200 #define MACRO_PATH_INCREMENT_TAIL(route) { \
201         if(++(route).tail == (route).cap) \
202                 path_resize(&(route), (route).cap - (route).head + ARRAY_CHUNK_SIZE);\
203 }
204
205 #define MACRO_PATH_INCREMENT_WTAIL(route) { \
206         if(++(route).wtail == (route).wcap) \
207                 path_wresize(&(route), (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \
208 }
209
210 G_END_DECLS
211
212 #endif