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