~ubuntu-branches/ubuntu/vivid/mkgmap/vivid

« back to all changes in this revision

Viewing changes to src/uk/me/parabola/imgfmt/Utils.java

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2014-08-13 22:13:41 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140813221341-i9dzzjuto2o7hfh6
Tags: 0.0.0+svn3333-1
* New upstream version
  Closes: #745097
* add debian/classpath (thanks for the patch to Manfred Stock
  <manfred.stock+debian@gmail.com>)
  Closes: #741596
* d/copyright: DEP5

Show diffs side-by-side

added added

removed removed

Lines of Context:
228
228
        
229
229
        /**
230
230
         * Calculates the angle between the two segments (c1,c2),(c2,c3).
 
231
         * It is assumed that the segments are rhumb lines, not great circle paths.
231
232
         * @param c1 first point
232
233
         * @param c2 second point
233
234
         * @param c3 third point
244
245
                
245
246
                return angle;
246
247
        }
 
248
        
 
249
        /**
 
250
         * Calculates the angle between the two segments (c1,c2),(c2,c3)
 
251
         * using the coords in map units.
 
252
         * @param c1 first point
 
253
         * @param c2 second point
 
254
         * @param c3 third point
 
255
         * @return angle between the two segments in degree [-180;180]
 
256
         */
 
257
        public static double getDisplayedAngle(Coord c1, Coord c2, Coord c3) {
 
258
                return getAngle(c1.getDisplayedCoord(), c2.getDisplayedCoord(), c3.getDisplayedCoord());
 
259
        }
 
260
 
247
261
        public final static int NOT_STRAIGHT = 0;
248
262
        public final static int STRAIGHT_SPIKE = 1;
249
263
        public final static int STRICTLY_STRAIGHT = 2;
282
296
                return NOT_STRAIGHT;
283
297
                
284
298
        }
 
299
 
 
300
        /**
 
301
         * Checks if the two segments (c1,c2),(c2,c3) form a straight line
 
302
         * using high precision coordinates.
 
303
         * @param c1 first point
 
304
         * @param c2 second point
 
305
         * @param c3 third point
 
306
         * @return NOT_STRAIGHT, STRAIGHT_SPIKE or STRICTLY_STRAIGHT 
 
307
         */
 
308
        public static int isHighPrecStraight(Coord c1, Coord c2, Coord c3) {
 
309
                if (c1.highPrecEquals(c3))
 
310
                        return STRAIGHT_SPIKE;
 
311
                long area;
 
312
                long c1Lat = c1.getHighPrecLat();
 
313
                long c2Lat = c2.getHighPrecLat();
 
314
                long c3Lat = c3.getHighPrecLat();
 
315
                long c1Lon = c1.getHighPrecLon();
 
316
                long c2Lon = c2.getHighPrecLon();
 
317
                long c3Lon = c3.getHighPrecLon();
 
318
                // calculate the area that is enclosed by the three points
 
319
                // (as if a closing line is drawn from c3 back to c1)
 
320
                area = c1Lon * c2Lat - c2Lon * c1Lat;
 
321
                area += c2Lon * c3Lat - c3Lon * c2Lat;
 
322
                area += c3Lon * c1Lat - c1Lon * c3Lat;
 
323
                if (area == 0){
 
324
                        // area is empty-> points lie on a straight line
 
325
                        long delta1 = c1Lat - c2Lat;
 
326
                        long delta2 = c2Lat - c3Lat;
 
327
                        if (delta1 < 0 && delta2 > 0 || delta1 > 0 && delta2 < 0)
 
328
                                return STRAIGHT_SPIKE;
 
329
                        delta1 = c1Lon - c2Lon;
 
330
                        delta2 = c2Lon - c3Lon;
 
331
                        if (delta1 < 0 && delta2 > 0 || delta1 > 0 && delta2 < 0)
 
332
                                return STRAIGHT_SPIKE;
 
333
                        return STRICTLY_STRAIGHT;
 
334
                }
 
335
                // line is not straight
 
336
                return NOT_STRAIGHT;
 
337
                
 
338
        }
 
339
 
 
340
        /**
 
341
         * approximate atan2, much faster than Math.atan2()
 
342
         * Based on a 50-year old arctan approximation due to Hastings
 
343
         */
 
344
        private final static double PI_BY_2 = Math.PI / 2;
 
345
        // |error| < 0.005
 
346
        public static double atan2_approximation( double y, double x )
 
347
        {
 
348
                if ( x == 0.0f )
 
349
                {
 
350
                        if ( y > 0.0f ) return PI_BY_2 ;
 
351
                        if ( y == 0.0f ) return 0.0f;
 
352
                        return -PI_BY_2 ;
 
353
                }
 
354
                double atan;
 
355
                double z = y/x;
 
356
                if ( Math.abs( z ) < 1.0f )
 
357
                {
 
358
                        atan = z/(1.0f + 0.28f*z*z);
 
359
                        if ( x < 0.0f )
 
360
                        {
 
361
                                if ( y < 0.0f ) return atan - Math.PI;
 
362
                                return atan + Math.PI;
 
363
                        }
 
364
                }
 
365
                else
 
366
                {
 
367
                        atan = PI_BY_2  - z/(z*z + 0.28f);
 
368
                        if ( y < 0.0f ) return atan - Math.PI;
 
369
                }
 
370
                return atan;
 
371
        }
 
372
        
 
373
        /**
 
374
         * calculate a long value for the latitude and longitude of a coord
 
375
         * in high precision. 
 
376
         * @param co
 
377
         * @return a long that can be used as a key in HashMaps 
 
378
         */
 
379
        public static long coord2Long(Coord co){
 
380
                int lat30 = co.getHighPrecLat();
 
381
                int lon30 = co.getHighPrecLon();
 
382
                
 
383
                return (long)(lat30 & 0xffffffffL) << 32 | (lon30 & 0xffffffffL);
 
384
        }
 
385
        
285
386
}
286
387