Changeset 10915 in josm


Ignore:
Timestamp:
2016-08-29T18:21:41+02:00 (8 years ago)
Author:
michael2402
Message:

Trim interpolate in EastNorth/LatLon for performance and add comment explaining it. Added unit tests.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/coor/EastNorth.java

    r10378 r10915  
    8282     */
    8383    public EastNorth interpolate(EastNorth en2, double proportion) {
    84         return new EastNorth(this.x + proportion * (en2.x - this.x),
    85                 this.y + proportion * (en2.y - this.y));
     84        // this is an alternate form of this.x + proportion * (en2.x - this.x) that is slightly faster
     85        return new EastNorth((1 - proportion) * this.x + proportion * en2.x,
     86                (1 - proportion) * this.y + proportion * en2.y);
    8687    }
    8788
     
    9293     */
    9394    public EastNorth getCenter(EastNorth en2) {
    94         return new EastNorth((this.x + en2.x)/2.0, (this.y + en2.y)/2.0);
     95        // The JIT will inline this for us, it is as fast as the normal /2 approach
     96        return interpolate(en2, .5);
    9597    }
    9698
  • trunk/src/org/openstreetmap/josm/data/coor/LatLon.java

    r10914 r10915  
    427427     */
    428428    public LatLon interpolate(LatLon ll2, double proportion) {
    429         return new LatLon(this.lat() + proportion * (ll2.lat() - this.lat()),
    430                 this.lon() + proportion * (ll2.lon() - this.lon()));
     429        // this is an alternate form of this.lat() + proportion * (ll2.lat() - this.lat()) that is slightly faster
     430        return new LatLon((1 - proportion) * this.lat() + proportion * ll2.lat(),
     431                (1 - proportion) * this.lon() + proportion * ll2.lon());
    431432    }
    432433
     
    437438     */
    438439    public LatLon getCenter(LatLon ll2) {
     440        // The JIT will inline this for us, it is as fast as the normal /2 approach
    439441        return interpolate(ll2, .5);
    440442    }
  • trunk/test/unit/org/openstreetmap/josm/data/coor/LatLonTest.java

    r10758 r10915  
    165165        assertEquals("2115070.3250722", c.lonToString(CoordinateFormat.EAST_NORTH));
    166166    }
     167
     168    /**
     169     * Test {@link LatLon#interpolate(LatLon, double)}
     170     * @since 10915
     171     */
     172    @Test
     173    public void testInterpolate() {
     174        LatLon ll1 = new LatLon(0, 0);
     175        LatLon ll2 = new LatLon(30, 60);
     176        LatLon ll3 = new LatLon(-70, -40);
     177        // lat:
     178        assertEquals(15, ll1.interpolate(ll2, 0.5).lat(), 1e-10);
     179        assertEquals(0, ll1.interpolate(ll2, 0).lat(), 1e-10);
     180        assertEquals(30, ll1.interpolate(ll2, 1).lat(), 1e-10);
     181        assertEquals(0, ll3.interpolate(ll2, .7).lat(), 1e-10);
     182        // lon
     183        assertEquals(30, ll1.interpolate(ll2, 0.5).lon(), 1e-10);
     184        assertEquals(0, ll1.interpolate(ll2, 0).lon(), 1e-10);
     185        assertEquals(60, ll1.interpolate(ll2, 1).lon(), 1e-10);
     186        assertEquals(0, ll3.interpolate(ll2, .4).lon(), 1e-10);
     187    }
     188
     189    /**
     190     * Test {@link LatLon#getCenter(LatLon)}
     191     * @since 10915
     192     */
     193    @Test
     194    public void testGetCenter() {
     195        LatLon ll1 = new LatLon(0, 0);
     196        LatLon ll2 = new LatLon(30, 60);
     197        LatLon ll3 = new LatLon(-70, -40);
     198
     199        assertEquals(15, ll1.getCenter(ll2).lat(), 1e-10);
     200        assertEquals(15, ll2.getCenter(ll1).lat(), 1e-10);
     201        assertEquals(-20, ll3.getCenter(ll2).lat(), 1e-10);
     202
     203        assertEquals(30, ll1.getCenter(ll2).lon(), 1e-10);
     204        assertEquals(30, ll2.getCenter(ll1).lon(), 1e-10);
     205        assertEquals(10, ll3.getCenter(ll2).lon(), 1e-10);
     206    }
    167207}
Note: See TracChangeset for help on using the changeset viewer.