1 /***************************************
2 $Header: /home/amb/routino/src/RCS/segments.c,v 1.47 2010/07/23 14:35:27 amb Exp $
4 Segment data type functions.
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008-2010 Andrew M. Bishop
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.
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.
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 ***************************************/
25 #include <sys/types.h>
37 /*++++++++++++++++++++++++++++++++++++++
38 Load in a segment list from a file.
40 Segments* LoadSegmentList Returns the segment list that has just been loaded.
42 const char *filename The name of the file to load.
43 ++++++++++++++++++++++++++++++++++++++*/
45 Segments *LoadSegmentList(const char *filename)
49 segments=(Segments*)malloc(sizeof(Segments));
53 segments->data=MapFile(filename);
55 /* Copy the SegmentsFile structure from the loaded data */
57 segments->file=*((SegmentsFile*)segments->data);
59 /* Set the pointers in the Segments structure. */
61 segments->segments=(Segment*)(segments->data+sizeof(SegmentsFile));
65 segments->fd=ReOpenFile(filename);
67 /* Copy the SegmentsFile header structure from the loaded data */
69 ReadFile(segments->fd,&segments->file,sizeof(SegmentsFile));
71 segments->incache[0]=NO_SEGMENT;
72 segments->incache[1]=NO_SEGMENT;
73 segments->incache[2]=NO_SEGMENT;
81 /*++++++++++++++++++++++++++++++++++++++
82 Find the next segment with a particular starting node.
84 Segment *NextSegment Returns a pointer to the next segment with the same id.
86 Segments* segments The set of segments to process.
88 Segment *segment The current segment.
90 index_t node The current node.
91 ++++++++++++++++++++++++++++++++++++++*/
93 Segment *NextSegment(Segments* segments,Segment *segment,index_t node)
95 if(segment->node1==node)
98 index_t index=IndexSegment(segments,segment);
101 if(index>=segments->file.number)
103 segment=LookupSegment(segments,index,1);
104 if(segment->node1!=node)
110 if(IndexSegment(segments,segment)>=segments->file.number || segment->node1!=node)
118 if(segment->next2==NO_NODE)
121 return(LookupSegment(segments,segment->next2,1));
126 /*++++++++++++++++++++++++++++++++++++++
127 Calculate the distance between two locations.
129 distance_t Distance Returns the distance between the locations.
131 double lat1 The latitude of the first location.
133 double lon1 The longitude of the first location.
135 double lat2 The latitude of the second location.
137 double lon2 The longitude of the second location.
138 ++++++++++++++++++++++++++++++++++++++*/
140 distance_t Distance(double lat1,double lon1,double lat2,double lon2)
142 double dlon = lon1 - lon2;
143 double dlat = lat1 - lat2;
145 double a1,a2,a,sa,c,d;
147 if(dlon==0 && dlat==0)
152 a = (a1 * a1) + cos (lat1) * cos (lat2) * a2 * a2;
157 {c = 2 * asin (1.0);}
160 return km_to_distance(d);
164 /*++++++++++++++++++++++++++++++++++++++
165 Calculate the duration of segment.
167 duration_t Duration Returns the duration of travel between the nodes.
169 Segment *segment The segment to traverse.
171 Way *way The way that the segment belongs to.
173 Profile *profile The profile of the transport being used.
174 ++++++++++++++++++++++++++++++++++++++*/
176 duration_t Duration(Segment *segment,Way *way,Profile *profile)
178 speed_t speed1=way->speed;
179 speed_t speed2=profile->speed[HIGHWAY(way->type)];
180 distance_t distance=DISTANCE(segment->distance);
185 return(hours_to_duration(10));
187 return distance_speed_to_duration(distance,speed2);
189 else /* if(speed1!=0) */
192 return distance_speed_to_duration(distance,speed1);
193 else if(speed1<=speed2)
194 return distance_speed_to_duration(distance,speed1);
196 return distance_speed_to_duration(distance,speed2);