Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/src/segments.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (hide annotations) (download) (as text)
Sun Feb 15 14:30:11 2009 UTC (16 years, 1 month ago) by amb
File MIME type: text/x-csrc
File size: 3404 byte(s)
Store radians rather than degrees.

1 amb 2 /***************************************
2 amb 121 $Header: /home/amb/CVS/routino/src/segments.c,v 1.34 2009-02-15 14:30:11 amb Exp $
3 amb 2
4     Segment data type functions.
5     ******************/ /******************
6     Written by Andrew M. Bishop
7    
8 amb 4 This file Copyright 2008,2009 Andrew M. Bishop
9 amb 2 It may be distributed under the GNU Public License, version 2, or
10     any higher version. See section COPYING of the GNU Public license
11     for conditions under which this file may be redistributed.
12     ***************************************/
13    
14    
15 amb 8 #include <assert.h>
16 amb 2 #include <math.h>
17 amb 104 #include <string.h>
18 amb 2 #include <stdlib.h>
19    
20 amb 109 #include "types.h"
21     #include "functions.h"
22 amb 39 #include "nodes.h"
23 amb 26 #include "segments.h"
24 amb 109 #include "ways.h"
25     #include "profiles.h"
26 amb 2
27    
28 amb 23 /*++++++++++++++++++++++++++++++++++++++
29 amb 2 Load in a segment list from a file.
30    
31 amb 23 Segments* SaveSegmentList Returns the segment list that has just been loaded.
32    
33 amb 2 const char *filename The name of the file to load.
34     ++++++++++++++++++++++++++++++++++++++*/
35    
36 amb 23 Segments *LoadSegmentList(const char *filename)
37 amb 2 {
38 amb 88 void *data;
39     Segments *segments;
40    
41     segments=(Segments*)malloc(sizeof(Segments));
42    
43     data=MapFile(filename);
44    
45 amb 100 if(!data)
46     return(NULL);
47    
48 amb 88 /* Copy the Segments structure from the loaded data */
49    
50     *segments=*((Segments*)data);
51    
52     /* Adjust the pointers in the Segments structure. */
53    
54 amb 104 segments->data=data;
55 amb 88 segments->segments=(Segment*)(data+(off_t)segments->segments);
56    
57     return(segments);
58 amb 2 }
59    
60    
61     /*++++++++++++++++++++++++++++++++++++++
62     Find the next segment with a particular starting node.
63    
64 amb 88 Segment *NextSegment Returns a pointer to the next segment with the same id.
65 amb 2
66 amb 23 Segments* segments The set of segments to process.
67    
68 amb 2 Segment *segment The current segment.
69     ++++++++++++++++++++++++++++++++++++++*/
70    
71 amb 104 Segment *NextSegment(Segments* segments,Segment *segment,index_t node)
72 amb 2 {
73 amb 104 index_t next;
74 amb 2
75 amb 104 if(NODE(segment->node1)==node)
76     next=segment->next1;
77     else
78     next=segment->next2;
79    
80     if(next==~0)
81 amb 23 return(NULL);
82 amb 104 else
83     return(LookupSegment(segments,next));
84 amb 89 }
85 amb 88
86    
87 amb 2 /*++++++++++++++++++++++++++++++++++++++
88 amb 99 Calculate the distance between two locations.
89    
90 amb 114 distance_t Distance Returns the distance between the locations.
91 amb 99
92     float lat1 The latitude of the first location.
93    
94     float lon1 The longitude of the first location.
95    
96     float lat2 The latitude of the second location.
97    
98     float lon2 The longitude of the second location.
99     ++++++++++++++++++++++++++++++++++++++*/
100    
101 amb 114 distance_t Distance(float lat1,float lon1,float lat2,float lon2)
102 amb 99 {
103 amb 121 float dlon = lon1 - lon2;
104     float dlat = lat1 - lat2;
105 amb 99
106 amb 120 float a1,a2,a,sa,c,d;
107 amb 99
108     if(dlon==0 && dlat==0)
109     return 0;
110    
111 amb 120 a1 = sinf (dlat / 2);
112     a2 = sinf (dlon / 2);
113 amb 121 a = (a1 * a1) + cosf (lat1) * cosf (lat2) * a2 * a2;
114 amb 120 sa = sqrtf (a);
115 amb 99 if (sa <= 1.0)
116 amb 120 {c = 2 * asinf (sa);}
117 amb 99 else
118 amb 120 {c = 2 * asinf (1.0);}
119 amb 99 d = 6378.137 * c;
120    
121 amb 114 return km_to_distance(d);
122 amb 99 }
123    
124    
125     /*++++++++++++++++++++++++++++++++++++++
126 amb 63 Calculate the duration of segment.
127    
128     duration_t Duration Returns the duration of travel between the nodes.
129    
130     Segment *segment The segment to traverse.
131    
132     Way *way The way that the segment belongs to.
133    
134 amb 82 Profile *profile The profile of the transport being used.
135 amb 63 ++++++++++++++++++++++++++++++++++++++*/
136    
137 amb 82 duration_t Duration(Segment *segment,Way *way,Profile *profile)
138 amb 63 {
139 amb 82 speed_t speed=profile->speed[HIGHWAY(way->type)];
140 amb 90 distance_t distance=DISTANCE(segment->distance);
141 amb 63
142 amb 86 return distance_speed_to_duration(distance,speed);
143 amb 63 }

Properties

Name Value
cvs:description Segment data type.