Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/segments.c
Parent Directory
|
Revision Log
Revision 214 -
(show annotations)
(download)
(as text)
Thu Jul 2 17:49:16 2009 UTC (15 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 4357 byte(s)
Thu Jul 2 17:49:16 2009 UTC (15 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 4357 byte(s)
Fix some gcc pedantic warnings.
1 | /*************************************** |
2 | $Header: /home/amb/CVS/routino/src/segments.c,v 1.41 2009-07-02 17:49:16 amb Exp $ |
3 | |
4 | Segment data type functions. |
5 | |
6 | Part of the Routino routing software. |
7 | ******************/ /****************** |
8 | This file Copyright 2008,2009 Andrew M. Bishop |
9 | |
10 | 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 | ***************************************/ |
23 | |
24 | |
25 | #include <sys/types.h> |
26 | #include <stdlib.h> |
27 | |
28 | #include "types.h" |
29 | #include "functions.h" |
30 | #include "nodes.h" |
31 | #include "segments.h" |
32 | #include "ways.h" |
33 | #include "profiles.h" |
34 | |
35 | |
36 | /*++++++++++++++++++++++++++++++++++++++ |
37 | Load in a segment list from a file. |
38 | |
39 | Segments* SaveSegmentList Returns the segment list that has just been loaded. |
40 | |
41 | const char *filename The name of the file to load. |
42 | ++++++++++++++++++++++++++++++++++++++*/ |
43 | |
44 | Segments *LoadSegmentList(const char *filename) |
45 | { |
46 | void *data; |
47 | Segments *segments; |
48 | |
49 | segments=(Segments*)malloc(sizeof(Segments)); |
50 | |
51 | data=MapFile(filename); |
52 | |
53 | if(!data) |
54 | return(NULL); |
55 | |
56 | /* Copy the Segments structure from the loaded data */ |
57 | |
58 | *segments=*((Segments*)data); |
59 | |
60 | /* Adjust the pointers in the Segments structure. */ |
61 | |
62 | segments->data=data; |
63 | segments->segments=(Segment*)(data+(off_t)segments->segments); |
64 | |
65 | return(segments); |
66 | } |
67 | |
68 | |
69 | /*++++++++++++++++++++++++++++++++++++++ |
70 | Find the next segment with a particular starting node. |
71 | |
72 | Segment *NextSegment Returns a pointer to the next segment with the same id. |
73 | |
74 | Segments* segments The set of segments to process. |
75 | |
76 | Segment *segment The current segment. |
77 | ++++++++++++++++++++++++++++++++++++++*/ |
78 | |
79 | Segment *NextSegment(Segments* segments,Segment *segment,index_t node) |
80 | { |
81 | if(segment->node1==node) |
82 | { |
83 | segment++; |
84 | if((segment-segments->segments)>=segments->number || segment->node1!=node) |
85 | return(NULL); |
86 | else |
87 | return(segment); |
88 | } |
89 | else |
90 | { |
91 | if(segment->next2==NO_NODE) |
92 | return(NULL); |
93 | else |
94 | return(LookupSegment(segments,segment->next2)); |
95 | } |
96 | } |
97 | |
98 | |
99 | /*++++++++++++++++++++++++++++++++++++++ |
100 | Calculate the distance between two locations. |
101 | |
102 | distance_t Distance Returns the distance between the locations. |
103 | |
104 | float lat1 The latitude of the first location. |
105 | |
106 | float lon1 The longitude of the first location. |
107 | |
108 | float lat2 The latitude of the second location. |
109 | |
110 | float lon2 The longitude of the second location. |
111 | ++++++++++++++++++++++++++++++++++++++*/ |
112 | |
113 | distance_t Distance(float lat1,float lon1,float lat2,float lon2) |
114 | { |
115 | float dlon = lon1 - lon2; |
116 | float dlat = lat1 - lat2; |
117 | |
118 | float a1,a2,a,sa,c,d; |
119 | |
120 | if(dlon==0 && dlat==0) |
121 | return 0; |
122 | |
123 | a1 = sinf (dlat / 2); |
124 | a2 = sinf (dlon / 2); |
125 | a = (a1 * a1) + cosf (lat1) * cosf (lat2) * a2 * a2; |
126 | sa = sqrtf (a); |
127 | if (sa <= 1.0) |
128 | {c = 2 * asinf (sa);} |
129 | else |
130 | {c = 2 * asinf (1.0);} |
131 | d = 6378.137 * c; |
132 | |
133 | return km_to_distance(d); |
134 | } |
135 | |
136 | |
137 | /*++++++++++++++++++++++++++++++++++++++ |
138 | Calculate the duration of segment. |
139 | |
140 | duration_t Duration Returns the duration of travel between the nodes. |
141 | |
142 | Segment *segment The segment to traverse. |
143 | |
144 | Way *way The way that the segment belongs to. |
145 | |
146 | Profile *profile The profile of the transport being used. |
147 | ++++++++++++++++++++++++++++++++++++++*/ |
148 | |
149 | duration_t Duration(Segment *segment,Way *way,Profile *profile) |
150 | { |
151 | speed_t speed1=way->speed; |
152 | speed_t speed2=profile->speed[HIGHWAY(way->type)]; |
153 | distance_t distance=DISTANCE(segment->distance); |
154 | |
155 | if(speed1==0) |
156 | { |
157 | if(speed2==0) |
158 | return(hours_to_duration(10)); |
159 | else |
160 | return distance_speed_to_duration(distance,speed2); |
161 | } |
162 | else /* if(speed1!=0) */ |
163 | { |
164 | if(speed2==0) |
165 | return distance_speed_to_duration(distance,speed1); |
166 | else if(speed1<=speed2) |
167 | return distance_speed_to_duration(distance,speed1); |
168 | else |
169 | return distance_speed_to_duration(distance,speed2); |
170 | } |
171 | } |
Properties
Name | Value |
---|---|
cvs:description | Segment data type. |