Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/src/segments.c
Parent Directory
|
Revision Log
Revision 151 -
(hide annotations)
(download)
(as text)
Wed Apr 8 16:54:34 2009 UTC (15 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 4399 byte(s)
Wed Apr 8 16:54:34 2009 UTC (15 years, 11 months ago) by amb
File MIME type: text/x-csrc
File size: 4399 byte(s)
Changed the license to Affero GPLv3.
1 | amb | 2 | /*************************************** |
2 | amb | 151 | $Header: /home/amb/CVS/routino/src/segments.c,v 1.37 2009-04-08 16:54:34 amb Exp $ |
3 | amb | 2 | |
4 | Segment data type functions. | ||
5 | amb | 151 | |
6 | Part of the Routino routing software. | ||
7 | amb | 2 | ******************/ /****************** |
8 | amb | 151 | This file Copyright 2008,2009 Andrew M. Bishop |
9 | amb | 2 | |
10 | amb | 151 | This program is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU Affero General Public License as published by | ||
12 | the Free Software Foundation, either version 3 of the License, or | ||
13 | (at your option) any later version. | ||
14 | |||
15 | This program is distributed in the hope that it will be useful, | ||
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
18 | GNU Affero General Public License for more details. | ||
19 | |||
20 | You should have received a copy of the GNU Affero General Public License | ||
21 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
22 | amb | 2 | ***************************************/ |
23 | |||
24 | |||
25 | amb | 8 | #include <assert.h> |
26 | amb | 2 | #include <math.h> |
27 | amb | 104 | #include <string.h> |
28 | amb | 2 | #include <stdlib.h> |
29 | |||
30 | amb | 109 | #include "types.h" |
31 | #include "functions.h" | ||
32 | amb | 39 | #include "nodes.h" |
33 | amb | 26 | #include "segments.h" |
34 | amb | 109 | #include "ways.h" |
35 | #include "profiles.h" | ||
36 | amb | 2 | |
37 | |||
38 | amb | 23 | /*++++++++++++++++++++++++++++++++++++++ |
39 | amb | 2 | Load in a segment list from a file. |
40 | |||
41 | amb | 23 | Segments* SaveSegmentList Returns the segment list that has just been loaded. |
42 | |||
43 | amb | 2 | const char *filename The name of the file to load. |
44 | ++++++++++++++++++++++++++++++++++++++*/ | ||
45 | |||
46 | amb | 23 | Segments *LoadSegmentList(const char *filename) |
47 | amb | 2 | { |
48 | amb | 88 | void *data; |
49 | Segments *segments; | ||
50 | |||
51 | segments=(Segments*)malloc(sizeof(Segments)); | ||
52 | |||
53 | data=MapFile(filename); | ||
54 | |||
55 | amb | 100 | if(!data) |
56 | return(NULL); | ||
57 | |||
58 | amb | 88 | /* Copy the Segments structure from the loaded data */ |
59 | |||
60 | *segments=*((Segments*)data); | ||
61 | |||
62 | /* Adjust the pointers in the Segments structure. */ | ||
63 | |||
64 | amb | 104 | segments->data=data; |
65 | amb | 88 | segments->segments=(Segment*)(data+(off_t)segments->segments); |
66 | |||
67 | return(segments); | ||
68 | amb | 2 | } |
69 | |||
70 | |||
71 | /*++++++++++++++++++++++++++++++++++++++ | ||
72 | Find the next segment with a particular starting node. | ||
73 | |||
74 | amb | 88 | Segment *NextSegment Returns a pointer to the next segment with the same id. |
75 | amb | 2 | |
76 | amb | 23 | Segments* segments The set of segments to process. |
77 | |||
78 | amb | 2 | Segment *segment The current segment. |
79 | ++++++++++++++++++++++++++++++++++++++*/ | ||
80 | |||
81 | amb | 104 | Segment *NextSegment(Segments* segments,Segment *segment,index_t node) |
82 | amb | 2 | { |
83 | amb | 104 | if(NODE(segment->node1)==node) |
84 | amb | 128 | { |
85 | segment++; | ||
86 | if(NODE(segment->node1)!=node || (segment-segments->segments)>=segments->number) | ||
87 | return(NULL); | ||
88 | else | ||
89 | return(segment); | ||
90 | } | ||
91 | amb | 104 | else |
92 | amb | 128 | { |
93 | if(segment->next2==~0) | ||
94 | return(NULL); | ||
95 | else | ||
96 | return(LookupSegment(segments,segment->next2)); | ||
97 | } | ||
98 | amb | 89 | } |
99 | amb | 88 | |
100 | |||
101 | amb | 2 | /*++++++++++++++++++++++++++++++++++++++ |
102 | amb | 99 | Calculate the distance between two locations. |
103 | |||
104 | amb | 114 | distance_t Distance Returns the distance between the locations. |
105 | amb | 99 | |
106 | float lat1 The latitude of the first location. | ||
107 | |||
108 | float lon1 The longitude of the first location. | ||
109 | |||
110 | float lat2 The latitude of the second location. | ||
111 | |||
112 | float lon2 The longitude of the second location. | ||
113 | ++++++++++++++++++++++++++++++++++++++*/ | ||
114 | |||
115 | amb | 114 | distance_t Distance(float lat1,float lon1,float lat2,float lon2) |
116 | amb | 99 | { |
117 | amb | 121 | float dlon = lon1 - lon2; |
118 | float dlat = lat1 - lat2; | ||
119 | amb | 99 | |
120 | amb | 120 | float a1,a2,a,sa,c,d; |
121 | amb | 99 | |
122 | if(dlon==0 && dlat==0) | ||
123 | return 0; | ||
124 | |||
125 | amb | 120 | a1 = sinf (dlat / 2); |
126 | a2 = sinf (dlon / 2); | ||
127 | amb | 121 | a = (a1 * a1) + cosf (lat1) * cosf (lat2) * a2 * a2; |
128 | amb | 120 | sa = sqrtf (a); |
129 | amb | 99 | if (sa <= 1.0) |
130 | amb | 120 | {c = 2 * asinf (sa);} |
131 | amb | 99 | else |
132 | amb | 120 | {c = 2 * asinf (1.0);} |
133 | amb | 99 | d = 6378.137 * c; |
134 | |||
135 | amb | 114 | return km_to_distance(d); |
136 | amb | 99 | } |
137 | |||
138 | |||
139 | /*++++++++++++++++++++++++++++++++++++++ | ||
140 | amb | 63 | Calculate the duration of segment. |
141 | |||
142 | duration_t Duration Returns the duration of travel between the nodes. | ||
143 | |||
144 | Segment *segment The segment to traverse. | ||
145 | |||
146 | Way *way The way that the segment belongs to. | ||
147 | |||
148 | amb | 82 | Profile *profile The profile of the transport being used. |
149 | amb | 63 | ++++++++++++++++++++++++++++++++++++++*/ |
150 | |||
151 | amb | 82 | duration_t Duration(Segment *segment,Way *way,Profile *profile) |
152 | amb | 63 | { |
153 | amb | 137 | speed_t speed1=way->speed; |
154 | speed_t speed2=profile->speed[HIGHWAY(way->type)]; | ||
155 | amb | 90 | distance_t distance=DISTANCE(segment->distance); |
156 | amb | 63 | |
157 | amb | 137 | if(speed1==0) |
158 | { | ||
159 | if(speed2==0) | ||
160 | return(hours_to_duration(10)); | ||
161 | else | ||
162 | return distance_speed_to_duration(distance,speed2); | ||
163 | } | ||
164 | else /* if(speed1!=0) */ | ||
165 | { | ||
166 | if(speed2==0) | ||
167 | return distance_speed_to_duration(distance,speed1); | ||
168 | else if(speed1<=speed2) | ||
169 | return distance_speed_to_duration(distance,speed1); | ||
170 | else | ||
171 | return distance_speed_to_duration(distance,speed2); | ||
172 | } | ||
173 | amb | 63 | } |
Properties
Name | Value |
---|---|
cvs:description | Segment data type. |