Rev 135 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
135 | ligi | 1 | /********************************************* |
2 | * |
||
3 | * class representing the DebugData Structure |
||
4 | * |
||
5 | * Author: Marcus -LiGi- Bueschleb |
||
6 | * |
||
7 | * see README for further Infos |
||
8 | * |
||
149 | ligi | 9 | * Some code taken from here: |
10 | * http://www.koders.com/java/fidFC75A641A87B51BB757E9CD3136C7886C491487F.aspx |
||
11 | * |
||
12 | * and |
||
13 | * http://www.movable-type.co.uk/scripts/latlong.html |
||
14 | * |
||
15 | * thanx a lot for sharing! |
||
16 | * |
||
135 | ligi | 17 | ********************************************/ |
18 | |||
149 | ligi | 19 | |
20 | |||
21 | import java.lang.Math; |
||
135 | ligi | 22 | public class MKGPSPosition |
23 | { |
||
149 | ligi | 24 | public final byte GPS_FORMAT_DECIMAL=0; |
25 | public final byte GPS_FORMAT_MINSEC=1; |
||
26 | public final byte GPS_FORMAT_COUNT=2; |
||
27 | byte act_gps_format=GPS_FORMAT_DECIMAL; |
||
135 | ligi | 28 | |
29 | public final static int MAX_WAYPOINTS=100; |
||
30 | |||
31 | int[] LongWP; |
||
32 | int[] LatWP; |
||
149 | ligi | 33 | String[] NameWP; |
34 | |||
135 | ligi | 35 | int last_wp=0; |
36 | |||
37 | |||
38 | int Longitude; |
||
39 | int Latitude; |
||
40 | |||
41 | int TargetLongitude; |
||
42 | int TargetLatitude; |
||
43 | |||
44 | int Distance2Target; |
||
45 | int Angle2Target; |
||
46 | |||
47 | byte Used_Sat; |
||
149 | ligi | 48 | /** |
49 | * Holds value Math.PI. |
||
50 | */ |
||
51 | public static final double PI = Math.PI; |
||
52 | /** |
||
53 | * Holds value PI / 2.0. |
||
54 | */ |
||
55 | public static final double PI_div2 = PI / 2.0; |
||
56 | /** |
||
57 | * Holds value PI / 4.0. |
||
58 | */ |
||
59 | public static final double PI_div4 = PI / 4.0; |
||
135 | ligi | 60 | |
61 | |||
149 | ligi | 62 | public static final double RADIANS = PI / 180.0; |
63 | public static final double DEGREES = 180.0 / PI; |
||
64 | |||
65 | |||
66 | private static final double p4 = 0.161536412982230228262e2; |
||
67 | /** |
||
68 | * Holds value 0.26842548195503973794141e3. |
||
69 | */ |
||
70 | private static final double p3 = 0.26842548195503973794141e3; |
||
71 | /** |
||
72 | * Holds value 0.11530293515404850115428136e4. |
||
73 | */ |
||
74 | private static final double p2 = 0.11530293515404850115428136e4; |
||
75 | /** |
||
76 | * Holds value 0.178040631643319697105464587e4. |
||
77 | */ |
||
78 | private static final double p1 = 0.178040631643319697105464587e4; |
||
79 | /** |
||
80 | * Holds value 0.89678597403663861959987488e3. |
||
81 | */ |
||
82 | private static final double p0 = 0.89678597403663861959987488e3; |
||
83 | /** |
||
84 | * Holds value 0.5895697050844462222791e2. |
||
85 | */ |
||
86 | private static final double q4 = 0.5895697050844462222791e2; |
||
87 | /** |
||
88 | * Holds value 0.536265374031215315104235e3. |
||
89 | */ |
||
90 | private static final double q3 = 0.536265374031215315104235e3; |
||
91 | /** |
||
92 | * Holds value 0.16667838148816337184521798e4. |
||
93 | */ |
||
94 | private static final double q2 = 0.16667838148816337184521798e4; |
||
95 | /** |
||
96 | * Holds value 0.207933497444540981287275926e4. |
||
97 | */ |
||
98 | private static final double q1 = 0.207933497444540981287275926e4; |
||
99 | /** |
||
100 | * Holds value 0.89678597403663861962481162e3. |
||
101 | */ |
||
102 | private static final double q0 = 0.89678597403663861962481162e3; |
||
103 | |||
104 | |||
105 | |||
106 | |||
107 | |||
108 | private static double _ATAN(double X) { |
||
109 | if (X < 0.414213562373095048802) { /* tan(PI/8) */ |
||
110 | return _ATANX(X); |
||
135 | ligi | 111 | } |
149 | ligi | 112 | else if (X > 2.414213562373095048802) { /* tan(3*PI/8) */ |
113 | return PI_div2 - _ATANX(1.0 / X); |
||
114 | } |
||
115 | else { |
||
116 | return PI_div4 + _ATANX((X - 1.0) / (X + 1.0)); |
||
117 | } |
||
118 | } |
||
135 | ligi | 119 | |
120 | |||
149 | ligi | 121 | private static double _ATANX(double X) { |
122 | double XX = X * X; |
||
123 | return X * ((((p4 * XX + p3) * XX + p2) * XX + p1) * XX + p0) |
||
124 | / (((((XX + q4) * XX + q3) * XX + q2) * XX + q1) * XX + q0); |
||
125 | } |
||
126 | |||
127 | |||
128 | |||
129 | public double aTan2(double Y, double X) { |
||
130 | |||
131 | //private static double ATAN2(double Y, double X) { |
||
132 | // return Math.atan2(Y,X); // not in CLDC 1.1 |
||
133 | |||
134 | // X=0 |
||
135 | if (X == 0.0) { |
||
136 | if (Y > 0.0) { |
||
137 | // mid Q1/Q2 |
||
138 | return PI_div2; |
||
139 | } |
||
140 | else if (Y < 0.0) { |
||
141 | // mid Q3/Q4 |
||
142 | return -PI_div2; |
||
143 | } |
||
144 | else { |
||
145 | // undefined |
||
146 | return 0.0; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | // X<0 |
||
151 | if (X < 0.0) { |
||
152 | if (Y >= 0.0) { |
||
153 | // Q2 |
||
154 | return (PI - _ATAN(Y / -X)); // Y>=0,X<0 |Y/X| |
||
155 | } |
||
156 | else { |
||
157 | // Q3 |
||
158 | return -(PI - _ATAN(Y / X)); // Y<0,X<0 |Y/X| |
||
159 | } |
||
160 | } |
||
161 | |||
162 | // X>0 |
||
163 | if (X > 0.0) { |
||
164 | // Q1/A4 |
||
165 | //return ATAN( Y / X); |
||
166 | if (Y > 0.0) { |
||
167 | // Q1 |
||
168 | return _ATAN(Y / X); |
||
169 | } |
||
170 | else { |
||
171 | // Q4 |
||
172 | return -_ATAN(-Y / X); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /* will never reach here */ |
||
177 | return 0.0; |
||
178 | |||
179 | } |
||
180 | /* |
||
181 | |||
182 | |||
183 | |||
184 | double coeff_1 = Math.PI / 4d; |
||
185 | double coeff_2 = 3d * coeff_1; |
||
186 | double abs_y = Math.abs(y); |
||
187 | double angle; |
||
188 | if (x >= 0d) { |
||
189 | double r = (x - abs_y) / (x + abs_y); |
||
190 | angle = coeff_1 - coeff_1 * r; |
||
191 | } else { |
||
192 | double r = (x + abs_y) / (abs_y - x); |
||
193 | angle = coeff_2 - coeff_1 * r; |
||
194 | } |
||
195 | return y < 0d ? -angle : angle; |
||
196 | } |
||
197 | |||
198 | */ |
||
199 | public int distance2wp(int id) |
||
135 | ligi | 200 | { |
149 | ligi | 201 | double lat1=(Latitude/10000000.0)*RADIANS; |
202 | double long1=(Longitude/10000000.0)*RADIANS; |
||
203 | |||
204 | double lat2=(LatWP[id]/10000000.0)*RADIANS; |
||
205 | double long2=(LongWP[id]/10000000.0)*RADIANS; |
||
206 | |||
207 | |||
208 | double dLat= (lat2-lat1); |
||
209 | double dLon= (long2-long1); |
||
210 | |||
211 | double a = Math.sin(dLat/2.0) * Math.sin(dLat/2.0) + |
||
212 | Math.cos(lat1) * Math.cos(lat2) * |
||
213 | Math.sin(dLon/2.0) * Math.sin(dLon/2.0); |
||
214 | |||
215 | return (int)(( 2.0 * aTan2(Math.sqrt(a), Math.sqrt(1.0-a)) )*6371008.8); |
||
135 | ligi | 216 | } |
217 | |||
218 | |||
149 | ligi | 219 | |
220 | |||
221 | public int angle2wp(int id) |
||
135 | ligi | 222 | { |
149 | ligi | 223 | // TODO reuse from distance |
224 | double lat1=(Latitude/10000000.0)*RADIANS; |
||
225 | double long1=(Longitude/10000000.0)*RADIANS; |
||
226 | |||
227 | double lat2=(LatWP[id]/10000000.0)*RADIANS; |
||
228 | double long2=(LongWP[id]/10000000.0)*RADIANS; |
||
229 | |||
230 | |||
231 | double dLat= (lat2-lat1); |
||
232 | double dLon= (long2-long1); |
||
233 | |||
135 | ligi | 234 | |
149 | ligi | 235 | |
236 | double y = Math.sin(dLon) * Math.cos(lat2); |
||
237 | double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); |
||
238 | return ((int)(aTan2(y, x)*DEGREES)+360)%360; |
||
239 | |||
135 | ligi | 240 | } |
241 | |||
149 | ligi | 242 | |
243 | |||
244 | public void push_wp() |
||
135 | ligi | 245 | { |
149 | ligi | 246 | LongWP[last_wp]=Longitude; |
247 | LatWP[last_wp]=Latitude; |
||
135 | ligi | 248 | |
149 | ligi | 249 | last_wp++; |
135 | ligi | 250 | } |
251 | |||
149 | ligi | 252 | public void next_gps_format() |
253 | { |
||
254 | act_gps_format=(byte)((act_gps_format+1)%GPS_FORMAT_COUNT); |
||
255 | } |
||
135 | ligi | 256 | |
149 | ligi | 257 | public String act_gps_format_str(int val) |
258 | { |
||
259 | switch(act_gps_format) |
||
260 | { |
||
261 | case GPS_FORMAT_DECIMAL: |
||
262 | return "" + val/10000000 + "." +val%10000000 ; |
||
263 | case GPS_FORMAT_MINSEC: |
||
264 | return "" + val/10000000 + "^" + ((val%10000000)*60)/10000000 + "'" + ((((val%10000000)*60)%10000000)*60)/10000000 + "." + ((((val%10000000)*60)%10000000)*60)%10000000; |
||
265 | default: |
||
266 | return "invalid format"; |
||
267 | } |
||
268 | } |
||
135 | ligi | 269 | |
149 | ligi | 270 | public String WP_Latitude_str(int id) |
135 | ligi | 271 | { |
272 | |||
149 | ligi | 273 | return "" + act_gps_format_str(LatWP[id]); //+ "''N" ; |
135 | ligi | 274 | } |
275 | |||
149 | ligi | 276 | public String WP_Longitude_str(int id) |
135 | ligi | 277 | { |
149 | ligi | 278 | return "" + act_gps_format_str(LongWP[id]); //+ "''E" ; |
135 | ligi | 279 | |
280 | } |
||
281 | |||
282 | public String Latitude_str() |
||
283 | { |
||
149 | ligi | 284 | |
285 | return "" + act_gps_format_str(Latitude) ; |
||
135 | ligi | 286 | } |
287 | |||
288 | public String Longitude_str() |
||
289 | { |
||
149 | ligi | 290 | return "" + act_gps_format_str(Longitude) ; |
291 | |||
135 | ligi | 292 | } |
293 | |||
149 | ligi | 294 | |
295 | // Constructor |
||
135 | ligi | 296 | public MKGPSPosition() |
297 | { |
||
298 | |||
299 | LongWP=new int[MAX_WAYPOINTS]; |
||
300 | LatWP=new int[MAX_WAYPOINTS]; |
||
149 | ligi | 301 | |
135 | ligi | 302 | |
149 | ligi | 303 | NameWP=new String[MAX_WAYPOINTS]; |
304 | // predefined waypoints |
||
305 | |||
306 | |||
307 | LongWP[0]=123230170; |
||
308 | LatWP[0]= 513600170 ; |
||
309 | NameWP[0]="Sicherer PC1"; |
||
310 | |||
311 | LongWP[1]=123269000; |
||
312 | LatWP[1]= 513662670; |
||
313 | NameWP[1]="Sicherer PC2"; |
||
314 | |||
315 | LongWP[2]=123475570; |
||
316 | LatWP[2]= 513569750 ; |
||
317 | NameWP[2]="Treffpunkt Seba"; |
||
318 | |||
319 | |||
320 | last_wp=2; |
||
135 | ligi | 321 | } |
322 | |||
323 | private int parse_arr(int offset,int[] in_arr) |
||
324 | { |
||
325 | return ((in_arr[offset+3]<<24) | |
||
326 | (in_arr[offset+2]<<16) | |
||
327 | (in_arr[offset+1]<<8) | |
||
328 | (in_arr[offset+0])); |
||
329 | } |
||
330 | |||
331 | public void set_by_mk_data(int[] in_arr,MKVersion version) |
||
332 | { |
||
333 | Longitude=parse_arr(0,in_arr); |
||
334 | Latitude=parse_arr(4,in_arr); |
||
335 | TargetLongitude=parse_arr(8,in_arr); |
||
336 | TargetLatitude=parse_arr(12,in_arr); |
||
337 | Distance2Target=parse_arr(16,in_arr); |
||
338 | Angle2Target=parse_arr(20,in_arr); |
||
339 | Used_Sat=(byte)in_arr[24]; |
||
340 | |||
341 | } |
||
342 | } |