]> err.no Git - mapper/blob - src/path.h
More map widget integration work
[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 /* Fixed ID's */
44 #define PATH_ID_MY_TRACK        (1)
45 #define PATH_ID_MY_ROUTE        (2)
46
47 /** 
48  * A general definition of a point in the Map unit system. 
49  */
50 typedef struct _Point Point;
51 struct _Point {
52         guint unitx;
53         guint unity;
54         time_t time;
55         gfloat altitude;
56 };
57
58 #define DISTANCE_SQUARED(a, b) \
59         ((guint64)((((gint64)(b).unitx)-(a).unitx)*(((gint64)(b).unitx)-(a).unitx)) \
60         + (guint64)((((gint64)(b).unity)-(a).unity)*(((gint64)(b).unity)-(a).unity)))
61
62 /**
63  * A latitude/longitue pair
64  */
65 typedef struct _LatLon LatLon;
66 struct _LatLon {
67         gdouble lat;
68         gdouble lon;
69 };
70
71 /** 
72  * A WayPoint, which is a Point with a description. 
73  */
74 typedef struct _WayPoint WayPoint;
75 struct _WayPoint {
76         Point *point;
77         gchar *desc;
78 };
79
80 /** 
81  * Path GObject
82  */
83 typedef struct _Path Path;
84 struct _Path {
85         GObject parent;
86
87         PathType type;
88         guint id;
89         gint sensitivity;
90
91         /* Path points */
92         Point *head;            /* points to first element in array; NULL if empty. */
93         Point *tail;            /* points to last element in array. */
94         Point *cap;                     /* points after last slot in array. */
95
96         /* Path bounding box */
97         LatLon min;
98         LatLon max;
99
100         /* Path waypoints */
101         WayPoint *whead;        /* points to first element in array; NULL if empty. */
102         WayPoint *wtail;        /* points to last element in array. */
103         WayPoint *wcap;         /* points after last slot in array. */
104
105         /* Internal waypoint data */
106         Point *near_point;
107         guint64 near_point_dist_squared;
108
109         /* next_way is what we currently interpret to be the next waypoint. */
110         WayPoint *next_way;
111         guint64 next_way_dist_squared;
112
113         /* next_wpt is the route point immediately following next_way. */
114         Point *next_wpt;
115         guint64 next_wpt_dist_squared;
116
117         /* Path statistics */
118         guint32 points;
119         guint32 wpcnt;          /* Auto waypoint number counter */
120         gdouble length;
121         gdouble tspeed;
122         gdouble avgspeed;
123         gfloat maxspeed;
124
125         /* GPX metadata fields */
126         gchar *name;
127         gchar *desc;
128         gchar *author;
129         gchar *keywords;
130         gchar *copyright;
131         gchar *src;
132         time_t time;
133 };
134
135 typedef struct _PathClass PathClass;
136 struct _PathClass {
137         GObjectClass parent;
138
139         void (*new_point) (Path *path);
140         void (*new_break) (Path *path);
141         void (*new_waypoint) (Path *path);
142 };
143
144 /* Null point */
145 Point _point_null;
146
147 Path *path_new(PathType type, guint id);
148 void path_free(Path *p);
149 void path_clear(Path *p);
150
151 Point *path_find_last_point(Path *path);
152
153 gboolean path_resize(Path *path, guint size);
154 gboolean path_wresize(Path *path, guint wsize);
155
156 gdouble path_get_distance_to(Path *path, Point *point, gdouble lat, gdouble lon);
157
158 gboolean path_update_nears(Path *route, Point *point, gboolean quick);
159
160 gboolean path_insert_break(Path *path);
161 void path_insert_mark_text(Path *path, gchar *text);
162 void path_insert_mark_autonumber(Path *path);
163 void path_insert_mark_audio(Path *path, gchar *audio);
164
165 gboolean path_get_waypoint_latlon(WayPoint *way, gdouble *lat, gdouble *lon);
166
167 GtkListStore *path_get_waypoints_store(Path *path);
168
169 #define MACRO_PATH_INIT(path) { \
170         (path).head = (path).tail = g_new0(Point, ARRAY_CHUNK_SIZE); \
171         *((path).tail) = _point_null; \
172         (path).cap = (path).head + ARRAY_CHUNK_SIZE; \
173         (path).whead = g_new0(WayPoint, ARRAY_CHUNK_SIZE); \
174         (path).wtail = (path).whead; \
175         (path).wcap = (path).whead + ARRAY_CHUNK_SIZE; \
176 }
177
178 #define MACRO_PATH_FREE(path) if((path).head) { \
179         WayPoint *curr; \
180         g_free((path).head); \
181         (path).head = (path).tail = (path).cap = NULL; \
182         for(curr = (path).whead; curr++ != (path).wtail; ) \
183                 g_free(curr->desc); \
184         g_free((path).whead); \
185         (path).whead = (path).wtail = (path).wcap = NULL; \
186 }
187
188 #define MACRO_PATH_INCREMENT_TAIL(route) { \
189         if(++(route).tail == (route).cap) \
190                 path_resize(&(route), (route).cap - (route).head + ARRAY_CHUNK_SIZE);\
191 }
192
193 #define MACRO_PATH_INCREMENT_WTAIL(route) { \
194         if(++(route).wtail == (route).wcap) \
195                 path_wresize(&(route), (route).wcap - (route).whead + ARRAY_CHUNK_SIZE); \
196 }
197
198 G_END_DECLS
199
200 #endif