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 669 -
(hide annotations)
(download)
(as text)
Mon Mar 21 19:25:46 2011 UTC (14 years ago) by amb
File MIME type: text/x-csrc
File size: 4873 byte(s)
Mon Mar 21 19:25:46 2011 UTC (14 years ago) by amb
File MIME type: text/x-csrc
File size: 4873 byte(s)
Include math.h for files that use math functions.
1 | amb | 2 | /*************************************** |
2 | Segment data type functions. | ||
3 | amb | 151 | |
4 | Part of the Routino routing software. | ||
5 | amb | 2 | ******************/ /****************** |
6 | amb | 669 | This file Copyright 2008-2011 Andrew M. Bishop |
7 | amb | 2 | |
8 | amb | 151 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by | ||
10 | the Free Software Foundation, either version 3 of the License, or | ||
11 | (at your option) any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU Affero General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Affero General Public License | ||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | amb | 2 | ***************************************/ |
21 | |||
22 | |||
23 | amb | 214 | #include <sys/types.h> |
24 | amb | 2 | #include <stdlib.h> |
25 | amb | 669 | #include <math.h> |
26 | amb | 2 | |
27 | amb | 109 | #include "types.h" |
28 | amb | 39 | #include "nodes.h" |
29 | amb | 26 | #include "segments.h" |
30 | amb | 109 | #include "ways.h" |
31 | amb | 449 | |
32 | #include "files.h" | ||
33 | amb | 109 | #include "profiles.h" |
34 | amb | 2 | |
35 | |||
36 | amb | 23 | /*++++++++++++++++++++++++++++++++++++++ |
37 | amb | 2 | Load in a segment list from a file. |
38 | |||
39 | amb | 228 | Segments* LoadSegmentList Returns the segment list that has just been loaded. |
40 | amb | 23 | |
41 | amb | 2 | const char *filename The name of the file to load. |
42 | ++++++++++++++++++++++++++++++++++++++*/ | ||
43 | |||
44 | amb | 23 | Segments *LoadSegmentList(const char *filename) |
45 | amb | 2 | { |
46 | amb | 88 | Segments *segments; |
47 | |||
48 | segments=(Segments*)malloc(sizeof(Segments)); | ||
49 | |||
50 | amb | 459 | #if !SLIM |
51 | amb | 88 | |
52 | amb | 459 | segments->data=MapFile(filename); |
53 | amb | 88 | |
54 | amb | 459 | /* Copy the SegmentsFile structure from the loaded data */ |
55 | amb | 88 | |
56 | amb | 459 | segments->file=*((SegmentsFile*)segments->data); |
57 | amb | 88 | |
58 | amb | 459 | /* Set the pointers in the Segments structure. */ |
59 | amb | 88 | |
60 | amb | 459 | segments->segments=(Segment*)(segments->data+sizeof(SegmentsFile)); |
61 | |||
62 | #else | ||
63 | |||
64 | segments->fd=ReOpenFile(filename); | ||
65 | |||
66 | /* Copy the SegmentsFile header structure from the loaded data */ | ||
67 | |||
68 | ReadFile(segments->fd,&segments->file,sizeof(SegmentsFile)); | ||
69 | |||
70 | segments->incache[0]=NO_SEGMENT; | ||
71 | segments->incache[1]=NO_SEGMENT; | ||
72 | segments->incache[2]=NO_SEGMENT; | ||
73 | |||
74 | #endif | ||
75 | |||
76 | amb | 88 | return(segments); |
77 | amb | 2 | } |
78 | |||
79 | |||
80 | /*++++++++++++++++++++++++++++++++++++++ | ||
81 | Find the next segment with a particular starting node. | ||
82 | |||
83 | amb | 88 | Segment *NextSegment Returns a pointer to the next segment with the same id. |
84 | amb | 2 | |
85 | amb | 23 | Segments* segments The set of segments to process. |
86 | |||
87 | amb | 2 | Segment *segment The current segment. |
88 | amb | 228 | |
89 | index_t node The current node. | ||
90 | amb | 2 | ++++++++++++++++++++++++++++++++++++++*/ |
91 | |||
92 | amb | 104 | Segment *NextSegment(Segments* segments,Segment *segment,index_t node) |
93 | amb | 2 | { |
94 | amb | 208 | if(segment->node1==node) |
95 | amb | 128 | { |
96 | amb | 459 | #if SLIM |
97 | index_t index=IndexSegment(segments,segment); | ||
98 | index++; | ||
99 | |||
100 | if(index>=segments->file.number) | ||
101 | return(NULL); | ||
102 | segment=LookupSegment(segments,index,1); | ||
103 | if(segment->node1!=node) | ||
104 | return(NULL); | ||
105 | else | ||
106 | return(segment); | ||
107 | #else | ||
108 | amb | 128 | segment++; |
109 | amb | 459 | if(IndexSegment(segments,segment)>=segments->file.number || segment->node1!=node) |
110 | amb | 128 | return(NULL); |
111 | else | ||
112 | return(segment); | ||
113 | amb | 459 | #endif |
114 | amb | 128 | } |
115 | amb | 104 | else |
116 | amb | 128 | { |
117 | amb | 176 | if(segment->next2==NO_NODE) |
118 | amb | 128 | return(NULL); |
119 | else | ||
120 | amb | 459 | return(LookupSegment(segments,segment->next2,1)); |
121 | amb | 128 | } |
122 | amb | 89 | } |
123 | amb | 88 | |
124 | |||
125 | amb | 2 | /*++++++++++++++++++++++++++++++++++++++ |
126 | amb | 99 | Calculate the distance between two locations. |
127 | |||
128 | amb | 114 | distance_t Distance Returns the distance between the locations. |
129 | amb | 99 | |
130 | amb | 219 | double lat1 The latitude of the first location. |
131 | amb | 99 | |
132 | amb | 219 | double lon1 The longitude of the first location. |
133 | amb | 99 | |
134 | amb | 219 | double lat2 The latitude of the second location. |
135 | amb | 99 | |
136 | amb | 219 | double lon2 The longitude of the second location. |
137 | amb | 99 | ++++++++++++++++++++++++++++++++++++++*/ |
138 | |||
139 | amb | 219 | distance_t Distance(double lat1,double lon1,double lat2,double lon2) |
140 | amb | 99 | { |
141 | amb | 219 | double dlon = lon1 - lon2; |
142 | double dlat = lat1 - lat2; | ||
143 | amb | 99 | |
144 | amb | 219 | double a1,a2,a,sa,c,d; |
145 | amb | 99 | |
146 | if(dlon==0 && dlat==0) | ||
147 | return 0; | ||
148 | |||
149 | amb | 219 | a1 = sin (dlat / 2); |
150 | a2 = sin (dlon / 2); | ||
151 | a = (a1 * a1) + cos (lat1) * cos (lat2) * a2 * a2; | ||
152 | sa = sqrt (a); | ||
153 | amb | 99 | if (sa <= 1.0) |
154 | amb | 219 | {c = 2 * asin (sa);} |
155 | amb | 99 | else |
156 | amb | 219 | {c = 2 * asin (1.0);} |
157 | amb | 99 | d = 6378.137 * c; |
158 | |||
159 | amb | 114 | return km_to_distance(d); |
160 | amb | 99 | } |
161 | |||
162 | |||
163 | /*++++++++++++++++++++++++++++++++++++++ | ||
164 | amb | 63 | Calculate the duration of segment. |
165 | |||
166 | duration_t Duration Returns the duration of travel between the nodes. | ||
167 | |||
168 | Segment *segment The segment to traverse. | ||
169 | |||
170 | Way *way The way that the segment belongs to. | ||
171 | |||
172 | amb | 82 | Profile *profile The profile of the transport being used. |
173 | amb | 63 | ++++++++++++++++++++++++++++++++++++++*/ |
174 | |||
175 | amb | 82 | duration_t Duration(Segment *segment,Way *way,Profile *profile) |
176 | amb | 63 | { |
177 | amb | 137 | speed_t speed1=way->speed; |
178 | speed_t speed2=profile->speed[HIGHWAY(way->type)]; | ||
179 | amb | 90 | distance_t distance=DISTANCE(segment->distance); |
180 | amb | 63 | |
181 | amb | 137 | if(speed1==0) |
182 | { | ||
183 | if(speed2==0) | ||
184 | return(hours_to_duration(10)); | ||
185 | else | ||
186 | return distance_speed_to_duration(distance,speed2); | ||
187 | } | ||
188 | else /* if(speed1!=0) */ | ||
189 | { | ||
190 | if(speed2==0) | ||
191 | return distance_speed_to_duration(distance,speed1); | ||
192 | else if(speed1<=speed2) | ||
193 | return distance_speed_to_duration(distance,speed1); | ||
194 | else | ||
195 | return distance_speed_to_duration(distance,speed2); | ||
196 | } | ||
197 | amb | 63 | } |
Properties
Name | Value |
---|---|
cvs:description | Segment data type. |