]> err.no Git - mapper/blob - src/path.h
Path: misc adjustments
[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 #include <libgnomevfs/gnome-vfs.h>
28
29 G_BEGIN_DECLS
30
31 #define PATH_TYPE                               (path_get_type ())
32 #define PATH(obj)                               (G_TYPE_CHECK_INSTANCE_CAST ((obj), PATH_TYPE, Path))
33 #define PATH_CLASS(klass)               (G_TYPE_CHECK_CLASS_CAST ((klass), PATH_TYPE, PathClass))
34 #define IS_PATH(obj)                    (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PATH_TYPE))
35 #define IS_PATH_CLASS(klass)    (G_TYPE_CHECK_CLASS_TYPE ((klass), PATH_TYPE))
36 #define PATH_GET_CLASS(obj)             (G_TYPE_INSTANCE_GET_CLASS ((obj), PATH_TYPE, PathClass))
37
38 typedef enum {
39         PATH_TYPE_TRACK=0,
40         PATH_TYPE_ROUTE,
41         PATH_TYPE_FRIEND,
42 } PathType;
43
44 typedef enum {
45         GPX_PATH_PREPEND=-1,
46         GPX_PATH_NEW=0,
47         GPX_PATH_APPEND=1,
48 } gpx_path_policy;
49
50 /* Path store items */
51 typedef enum {
52     PATH_LATLON,
53     PATH_DISTANCE,
54     PATH_WAYPOINT,
55     PATH_LAT,
56     PATH_LON,
57     PATH_NUM_COLUMNS
58 } PathStoreList;
59
60
61 /* Fixed ID's */
62 #define PATH_ID_MY_TRACK        (1)
63 #define PATH_ID_MY_ROUTE        (2)
64
65 /** 
66  * A general definition of a point in the Map unit system. 
67  */
68 typedef struct _Point Point;
69 struct _Point {
70         guint unitx;
71         guint unity;
72         time_t time;
73         gfloat altitude;
74 };
75
76 #define DISTANCE_SQUARED(a, b) \
77         ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx)) \
78         + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
79
80 /**
81  * A latitude/longitue pair
82  */
83 typedef struct _LatLon LatLon;
84 struct _LatLon {
85         gdouble lat;
86         gdouble lon;
87 };
88
89 /** 
90  * A WayPoint, which is a Point with a description. 
91  */
92 typedef struct _WayPoint WayPoint;
93 struct _WayPoint {
94         Point *point;
95         gchar *desc;
96 };
97
98 /** 
99  * Path GObject
100  */
101 typedef struct _Path Path;
102 struct _Path {
103         GObject parent;
104
105         PathType type;
106         guint id;
107         gint sensitivity;
108
109         /* Path points */
110         Point *head;            /* points to first element in array; NULL if empty. */
111         Point *tail;            /* points to last element in array. */
112         Point *cap;                     /* points after last slot in array. */
113
114         /* Path bounding box */
115         LatLon min;
116         LatLon max;
117
118         /* Path waypoints */
119         WayPoint *whead;        /* points to first element in array; NULL if empty. */
120         WayPoint *wtail;        /* points to last element in array. */
121         WayPoint *wcap;         /* points after last slot in array. */
122
123         /* Internal waypoint data */
124         Point *near_point;
125         guint64 near_point_dist_squared;
126
127         /* next_way is what we currently interpret to be the next waypoint. */
128         WayPoint *next_way;
129         guint64 next_way_dist_squared;
130
131         /* next_wpt is the route point immediately following next_way. */
132         Point *next_wpt;
133         guint64 next_wpt_dist_squared;
134
135         /* The waypoint we last announced */
136         WayPoint *announced_waypoint;
137         gint announce_notice_ratio;
138
139         /* Path statistics */
140         guint32 points;
141         guint32 wpcnt;          /* Auto waypoint number counter */
142         gdouble length;
143         gdouble tspeed;
144         gdouble avgspeed;
145         gfloat maxspeed;
146
147         /* GPX metadata fields */
148         gchar *name;
149         gchar *desc;
150         gchar *author;
151         gchar *keywords;
152         gchar *copyright;
153         gchar *src;
154         time_t time;
155 };
156
157 typedef struct _PathClass PathClass;
158 struct _PathClass {
159         GObjectClass parent;
160
161         void (*new_point) (Path *path);
162         void (*new_break) (Path *path);
163         void (*new_waypoint) (Path *path);
164 };
165
166 /* Null point */
167 Point _point_null;
168
169 Path *path_new(PathType type, guint id);
170 void path_free(Path *p);
171 void path_clear(Path *p);
172
173 gboolean path_gpx_read(Path *path, GnomeVFSHandle *handle);
174 gboolean path_gpx_write(Path *path, GnomeVFSHandle *handle, GError **error);
175 gboolean path_gpx_parse(Path *to_replace, gchar *buffer, gint size, gpx_path_policy policy);
176
177 Point *path_find_last_point(Path *path);
178
179 gboolean path_resize(Path *path, guint size);
180 gboolean path_wresize(Path *path, guint wsize);
181
182 gdouble path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon);
183
184 void path_find_nearest_point(Path *path, gdouble lat, gdouble lon);
185
186 gboolean path_add_latlon(Path *path, gdouble lat, gdouble lon, time_t ptime, gfloat speed, gfloat altitude);
187
188 gboolean path_has_points(Path *path);
189 gboolean path_has_waypoints(Path *path);
190
191 gboolean path_add_break(Path *path);
192 void path_insert_mark_text(Path *path, gchar *text);
193 void path_insert_mark_autonumber(Path *path);
194 void path_insert_mark_audio(Path *path, gchar *audio);
195
196 gboolean path_get_waypoint_latlon(WayPoint *way, gdouble *lat, gdouble *lon);
197
198 GtkListStore *path_get_waypoints_store(Path *path);
199
200 #define MACRO_PATH_INIT(path) { \
201         (path).head = (path).tail = g_new0(Point, ARRAY_CHUNK_SIZE); \
202         *((path).tail) = _point_null; \
203         (path).cap = (path).head + ARRAY_CHUNK_SIZE; \
204         (path).whead = g_new0(WayPoint, ARRAY_CHUNK_SIZE); \
205         (path).wtail = (path).whead; \
206         (path).wcap = (path).whead + ARRAY_CHUNK_SIZE; \
207 }
208
209 #define MACRO_PATH_FREE(path) if((path).head) { \
210         WayPoint *curr; \
211         g_free((path).head); \
212         (path).head = (path).tail = (path).cap = NULL; \
213         for(curr = (path).whead; curr++ != (path).wtail; ) \
214                 g_free(curr->desc); \
215         g_free((path).whead); \
216         (path).whead = (path).wtail = (path).wcap = NULL; \
217 }
218
219 #define MACRO_PATH_INCREMENT_TAIL(route) { \
220         if(++(route).tail == (route).cap) \
221                 path_resize(&(route), (route).cap - (route).head + ARRAY_CHUNK_SIZE);\
222 }
223
224 #define MACRO_PATH_INCREMENT_WTAIL(route) { \
225         if(++(route).wtail == (route).wcap) \
226                 path_wresize(&(route), (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \
227 }
228
229 G_END_DECLS
230
231 #endif