• Views
  • Iteration Report
  • My Iteration Report
  •  
OMERO.server
  • Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Changeset
  • Next Changeset →

Changeset 1582

Show
Ignore:
Timestamp:
06/03/07 13:29:14 (18 months ago)
Author:
callan
Message:

Optimizations to avoid #725:

  • Removal of "approximation" code that just delegates to Math, the extra method call overhead is a bit ridiculous
  • Millions of small PlanePoint allocations have been avoided in the StatsFactory by using non-object oriented double precision arithmatic
Location:
trunk/components/rendering
Files:
2 removed
5 modified

  • src/ome/util/math/Approximation.java (deleted)
  • src/ome/util/math/geom2D/PlanePoint.java (modified) (2 diffs)
  • src/ome/util/math/geom2D/Segment.java (modified) (6 diffs)
  • src/ome/util/math/package.html (deleted)
  • src/omeis/providers/re/metadata/StatsFactory.java (modified) (1 diff)
  • src/omeis/providers/re/quantum/Quantization_8_16_bit.java (modified) (2 diffs)
  • test/ome/util/math/geom2D/TestSegment.java (modified) (11 diffs)

Legend:

Unmodified
Added
Removed
  • trunk/components/rendering/src/ome/util/math/geom2D/PlanePoint.java

    r1167 r1582  
    141141        return new PlanePoint(p.x1 - x1, p.x2 - x2); 
    142142    } 
     143     
     144    /** 
     145     * Calculates the vector associated to this point and the specified 
     146     * argument. This is the map that makes the set A = <b>R</b><sup>2</sup> 
     147     * an affine space over the vector space V = <b>R</b><sup>2</sup> and is 
     148     * defined by: 
     149     * <p> 
     150     * <nobr><i> f: AxA ---&gt; V <br> 
     151     * f(a, b) = b - a = (b<sub>1</sub> - a<sub>1</sub>, b<sub>2</sub> - a<sub>2</sub>) 
     152     * </i></nobr> 
     153     * </p> 
     154     * This method returns <nobr><i>f(t, p)</i></nobr>, where <i>t</i> is 
     155     * this point and <i>p</i> is the specified argument. 
     156     *  
     157     * @param p 
     158     *            The other point. Mustn't be a <code>null</code> reference. 
     159     * @return The vector associated to this point and <i>p</i>. 
     160     */ 
     161    public PlanePoint vec(double x1, double x2) { 
     162        return new PlanePoint(x1 - this.x1, x2 - this.x2); 
     163    } 
    143164 
    144165    /** 
    … …  
    234255        return isEqual; 
    235256    } 
     257     
     258    /** 
     259     * Check to see if the point values are equal. 
     260     *  
     261     * @param x1 The comparison point's first element. 
     262     * @param x2 The comparison point's second element. 
     263     * @return <code>true</code> if the points are equal, <code>false</code> 
     264     * otherwise. 
     265     */ 
     266    public boolean equals(double x1, double x2) { 
     267        return this.x1 == x1 && this.x2 == x2; 
     268    } 
    236269 
    237270    /** 
  • trunk/components/rendering/src/ome/util/math/geom2D/Segment.java

    r1167 r1582  
    2828public class Segment { 
    2929 
    30     /** The origin point of the segment. */ 
    31     public final PlanePoint origin; 
     30        /** The origin of the segment's first element. */ 
     31        public final double originX1; 
     32         
     33        /** The origin of the segment's first element. */ 
     34        public final double originX2; 
    3235 
    33     /** The end point of the segment. */ 
    34     public final PlanePoint direction; 
     36    /** The end point of the segment's first element. */ 
     37    public final double directionX1; 
     38     
     39    /** The end point of the segment's second element. */ 
     40    public final double directionX2; 
    3541 
    3642    /** 
    3743     * Creates a new instance. 
    3844     *  
    39      * @param o 
    40      *            The origin point of the segment. 
    41      * @param e 
    42      *            The end point of the segment. 
     45         * @param originX1 The origin of the segment's first element. 
     46         * @param originX2 The origin of the segment's first element. 
     47     * @param o The origin point of the segment. 
     48     * @param endX1 The end point's first element. 
     49     * @param endX2 The end point's second element. 
    4350     */ 
    44     public Segment(PlanePoint o, PlanePoint e) { 
    45         if (o == null) { 
    46             throw new NullPointerException("No origin."); 
    47         } 
    48         if (e == null) { 
    49             throw new NullPointerException("No end p."); 
    50         } 
    51         if (o.equals(e)) { 
     51    public Segment(double originX1, double originX2, double endX1, double endX2) 
     52    { 
     53        if (originX1 == endX1 && originX2 == endX2) 
     54        { 
    5255            throw new IllegalArgumentException("Need two different points."); 
    5356        } 
    54         origin = o; 
    55         direction = origin.vec(e); 
     57        this.originX1 = originX1; 
     58        this.originX2 = originX2; 
     59 
     60        /* 
     61         * Calculate the vector associated to the origin and the destination end  
     62         * point of this segment. This is the map that makes the set  
     63         * A = <b>R</b><sup>2</sup> an affine space over the vector space  
     64         * V = <b>R</b><sup>2</sup> and is defined by: 
     65         * <p> 
     66         * <nobr><i> f: AxA ---&gt; V <br> 
     67         * f(a, b) = b - a = (b<sub>1</sub> - a<sub>1</sub>, b<sub>2</sub> - a<sub>2</sub>) 
     68         * </i></nobr> 
     69         * </p> 
     70         */ 
     71        directionX1 = endX1 - originX1; 
     72        directionX2 = endX2 - originX2; 
    5673    } 
    57  
     74     
    5875    /** 
    5976     * Returns the point of this line defined by <code>k</code>. More 
    … …  
    7188                    + "range [0, 1]."); 
    7289        } 
    73         return new PlanePoint(origin.x1 + k * direction.x1, origin.x2 + k 
    74                 * direction.x2); 
     90        return new PlanePoint(originX1 + k * directionX1, originX2 + k 
     91                * directionX2); 
    7592    } 
    7693 
    7794    /** 
    78      * Tells whether the specified point lies on this line. 
     95     * Tells whether a specified point lies on this line. 
    7996     *  
    8097     * @param p 
    … …  
    83100     *         <code>false</code> otherwise. 
    84101     */ 
    85     public boolean lies(PlanePoint p) { 
    86         if (p == null) { 
    87             throw new NullPointerException("No point."); 
    88         } 
     102    public boolean lies(double x1, double x2) { 
    89103        boolean result = false; 
    90104        double k1, k2; 
    91         if (direction.x1 == 0 && direction.x2 != 0) { 
    92             k2 = (p.x2 - origin.x2) / direction.x2; 
     105        if (directionX1 == 0 && directionX2 != 0) { 
     106            k2 = (x2 - originX2) / directionX2; 
    93107            if (k2 < 0 || k2 > 1) { 
    94108                result = false; 
    95109            } else { 
    96                 result = p.x1 == origin.x1; 
     110                result = x1 == originX1; 
    97111            } 
    98         } else if (direction.x1 != 0 && direction.x2 == 0) { 
    99             k1 = (p.x1 - origin.x1) / direction.x1; 
     112        } else if (directionX1 != 0 && directionX2 == 0) { 
     113            k1 = (x1 - originX1) / directionX1; 
    100114            if (k1 < 0 || k1 > 1) { 
    101115                result = false; 
    102116            } else { 
    103                 result = p.x2 == origin.x2; 
     117                result = x2 == originX2; 
    104118            } 
    105         } else if (direction.x1 != 0 && direction.x2 != 0) { 
    106             k1 = (p.x1 - origin.x1) / direction.x1; 
    107             k2 = (p.x2 - origin.x2) / direction.x2; 
     119        } else if (directionX1 != 0 && directionX2 != 0) { 
     120            k1 = (x1 - originX1) / directionX1; 
     121            k2 = (x2 - originX2) / directionX2; 
    108122            if (k1 == k2) { 
    109123                if (k1 < 0 || k1 > 1) { 
    … …  
    115129        } 
    116130        return result; 
     131    } 
     132     
     133    /** 
     134     * Performs an equality test based on a point on this line defined 
     135     * by <code>k</code> as in {@link getPoint()} and another given point. 
     136     * @param k The coefficient to select the point. Must be in the range 
     137     * <code>[0, 1]</code>. 
     138     * @param x1 The point to test's first element. 
     139     * @param x2 The point to test's second element. 
     140     * @return <code>true</code> if the points are geometrically equal,  
     141     * <code>false</code> otherwise. 
     142     */ 
     143    public boolean equals(double k, double x1, double x2) 
     144    { 
     145        if (k < 0 || k > 1) { 
     146            throw new IllegalArgumentException("Coefficient must be in the " 
     147                    + "range [0, 1]."); 
     148        } 
     149        double kPointX1 = originX1 + k * directionX1; 
     150        double kPointX2 = originX2 + k * directionX2; 
     151        return kPointX1 == x1 && kPointX2 == x2; 
     152 
    117153    } 
    118154 
    … …  
    128164        if (o != null && o instanceof Line) { 
    129165            Line other = (Line) o; 
    130             isEqual = origin == other.origin && direction == other.direction; 
     166            isEqual =  
     167                other.origin.x1 == originX1 
     168                && other.origin.x2 == originX2 
     169                && other.direction.x1 == directionX1 
     170                && other.direction.x2 == directionX2; 
    131171        } 
    132172        return isEqual; 
    … …  
    141181    @Override 
    142182    public int hashCode() { 
    143         return origin.hashCode(); 
     183        long bits = Double.doubleToLongBits(originX1); 
     184        bits ^= Double.doubleToLongBits(originX2) * 31; 
     185        return (int) bits ^ (int) (bits >> 32); 
    144186    } 
    145187 
  • trunk/components/rendering/src/omeis/providers/re/metadata/StatsFactory.java

    r1167 r1582  
    8282        int[] totals = new int[NB_BIN]; 
    8383        locationStats = new double[NB_BIN]; 
    84         PlanePoint o, e; 
    8584        Segment[] segments = new Segment[NB_BIN]; 
    8685        for (int i = 0; i < NB_BIN; i++) { 
    87             o = new PlanePoint(gMin + i * sizeBin, 0); 
    88             e = new PlanePoint(gMin + (i + 1) * sizeBin, 0); 
    89             segments[i] = new Segment(o, e); 
    90         } 
    91         PlanePoint point; 
    92         // check segment [o,e[ 
     86            segments[i] = new Segment( 
     87                        gMin + i * sizeBin, 0, 
     88                        gMin + (i + 1) * sizeBin, 0); 
     89        } 
     90 
     91        // check segment [o,e] 
     92        double pointX1; 
     93        double pointX2 = 0; 
    9394        for (int x2 = 0; x2 < sizeX2; ++x2) { 
    9495            for (int x1 = 0; x1 < sizeX1; ++x1) { 
    95                 point = new PlanePoint(p2D.getPixelValue(x1, x2), 0); 
    9696                for (int i = 0; i < segments.length; i++) { 
    97                     if (segments[i].lies(point) 
    98                             && !segments[i].getPoint(1).equals(point)) { 
     97                        pointX1 = p2D.getPixelValue(x1, x2); 
     98                    if (!segments[i].equals(1, pointX1, pointX2) 
     99                        && segments[i].lies(pointX1, pointX2)) 
     100                    { 
    99101                        totals[i]++; 
    100102                        break; 
  • trunk/components/rendering/src/omeis/providers/re/quantum/Quantization_8_16_bit.java

    r1574 r1582  
    1515 
    1616// Application-internal dependencies 
    17 import ome.api.IPixels; 
    1817import ome.model.display.QuantumDef; 
    1918import ome.model.enums.PixelsType; 
    20 import ome.util.math.Approximation; 
    21 import omeis.providers.re.Renderer; 
    2219 
    2320/** 
    … …  
    197194            } 
    198195            v = aNormalized * (valueMapper.transform(v, k) - ysNormalized); 
    199             v = Approximation.nearestInteger(v); 
    200             v = Approximation.nearestInteger(a1 * v + cdStart); 
     196            v = Math.round(v); 
     197            v = Math.round(a1 * v + cdStart); 
    201198            LUT[x - min] = (byte) v; 
    202199        } 
  • trunk/components/rendering/test/ome/util/math/geom2D/TestSegment.java

    r1167 r1582  
    3737    public void testSegmentBadArgs() { 
    3838        try { 
    39             new Segment(null, null); 
    40             fail("Shouldn't allow nulls."); 
    41         } catch (NullPointerException npe) { 
    42             // Ok, expected. 
    43         } 
    44         try { 
    45             new Segment(null, new PlanePoint(0, 0)); 
    46             fail("Shouldn't allow null origin."); 
    47         } catch (NullPointerException npe) { 
    48             // Ok, expected. 
    49         } 
    50         try { 
    51             new Segment(new PlanePoint(0, 0), null); 
    52             fail("Shouldn't allow null head."); 
    53         } catch (NullPointerException npe) { 
    54             // Ok, expected. 
    55         } 
    56         try { 
    57             new Segment(new PlanePoint(1, 1), new PlanePoint(1, 1)); 
     39            new Segment(1, 1, 1, 1); 
    5840            fail("Shouldn't allow same points."); 
    5941        } catch (IllegalArgumentException iae) { 
    … …  
    6446    @Test 
    6547    public void testSegment() { 
    66         PlanePoint o = new PlanePoint(0, 0), p = new PlanePoint(1, 1); 
    67         Segment r = new Segment(o, p); 
    68         assertEquals("Shouldn't change the origin.", o, r.origin); 
     48        Segment r = new Segment(0, 0, 1, 1); 
     49        assertEquals("Shouldn't change the originX1.", 0.0, r.originX1); 
     50        assertEquals("Shouldn't change the originX2.", 0.0, r.originX2); 
    6951    } 
    7052 
    7153    @Test 
    7254    public void testGetPointXAxis() { 
    73         PlanePoint o = new PlanePoint(0, 0), p = new PlanePoint(1, 0); 
    74         Segment r = new Segment(o, p); 
     55        PlanePoint p; 
     56        Segment r = new Segment(0, 0, 1, 0); 
    7557        double d; 
    7658        for (int i = 0; i < INTERVAL; ++i) { 
    … …  
    8365    @Test 
    8466    public void testGetPointYAxis() { 
    85         PlanePoint o = new PlanePoint(0, 0), p = new PlanePoint(0, 1); 
    86         Segment r = new Segment(o, p); 
     67        PlanePoint p; 
     68        Segment r = new Segment(0, 0, 0, 1); 
    8769        double d; 
    8870        for (int i = 0; i < INTERVAL; ++i) { 
    … …  
    9577    @Test 
    9678    public void testGetPointParallelXAxis() { 
    97         PlanePoint o = new PlanePoint(0, 1), p = new PlanePoint(1, 1); 
    98         Segment r = new Segment(o, p); 
     79        PlanePoint p; 
     80        Segment r = new Segment(0, 1, 1, 1); 
    9981        double d; 
    10082        for (int i = 0; i < INTERVAL; ++i) { 
    … …  
    10789    @Test 
    10890    public void testGetPointParallelYAxis() { 
    109         PlanePoint o = new PlanePoint(1, 0), p = new PlanePoint(1, 1); 
    110         Segment r = new Segment(o, p); 
     91        PlanePoint p; 
     92        Segment r = new Segment(1, 0, 1, 1); 
    11193        double d; 
    11294        for (int i = 0; i < INTERVAL; ++i) { 
    … …  
    118100 
    119101    @Test 
    120     public void testLiesNull() { 
    121         PlanePoint o = new PlanePoint(0, 1), p = new PlanePoint(1, 1); 
    122         Segment r = new Segment(o, p); 
    123         try { 
    124             r.lies(null); 
    125             fail("Souldn't accept null."); 
    126         } catch (NullPointerException npe) { 
    127             // Ok, expected. 
    128         } 
    129     } 
    130  
    131     @Test 
    132102    public void testLies1() { 
    133         PlanePoint o = new PlanePoint(0, 1), p = new PlanePoint(1, 1); 
    134         Segment r = new Segment(o, p); 
     103        Segment r = new Segment(0, 1, 1, 1); 
    135104        double d; 
    136105        for (int i = 0; i < INTERVAL; ++i) { 
    137106            d = (double) i / INTERVAL; 
    138             p = new PlanePoint(d, 1); 
    139             assertTrue("Actually lies on r [i = " + i + "].", r.lies(p)); 
    140             p = new PlanePoint(d, 0); 
    141             assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(p)); 
     107            assertTrue("Actually lies on r [i = " + i + "].", r.lies(d, 1)); 
     108            assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(d, 0)); 
    142109        } 
    143110    } 
    … …  
    145112    @Test 
    146113    public void testLies2() { 
    147         PlanePoint o = new PlanePoint(1, 0), p = new PlanePoint(1, 1); 
    148         Segment r = new Segment(o, p); 
     114        Segment r = new Segment(1, 0, 1, 1); 
    149115        double d; 
    150116        for (int i = 0; i < INTERVAL; ++i) { 
    151117            d = (double) i / INTERVAL; 
    152             p = new PlanePoint(1, d); 
    153             assertTrue("Actually lies on r [i = " + i + "].", r.lies(p)); 
    154             p = new PlanePoint(d, 0); 
    155             assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(p)); 
     118            assertTrue("Actually lies on r [i = " + i + "].", r.lies(1, d)); 
     119            assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(d, 0)); 
    156120        } 
    157121    } 
    … …  
    159123    @Test 
    160124    public void testLies3() { 
    161         PlanePoint o = new PlanePoint(0, 0), p = new PlanePoint(1, 1); 
    162         Segment r = new Segment(o, p); 
     125        Segment r = new Segment(0, 0, 1, 1); 
    163126        double d; 
    164127        for (int i = 1; i < INTERVAL; ++i) { 
    165128            d = (double) i / INTERVAL; 
    166             p = new PlanePoint(d, d); 
    167             assertTrue("Actually lies on r [i = " + i + "].", r.lies(p)); 
    168             p = new PlanePoint(d, 0); 
    169             assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(p)); 
     129            assertTrue("Actually lies on r [i = " + i + "].", r.lies(d, d)); 
     130            assertFalse("Doesn't lie on r [i = " + i + "].", r.lies(d, 0)); 
    170131        } 
    171132    } 
    … …  
    174135    public void testEquals() { 
    175136        PlanePoint o = new PlanePoint(0, 0), p = new PlanePoint(1, 1); 
    176         Segment r = new Segment(o, p); 
     137        Segment r = new Segment(0, 0, 1, 1); 
    177138        assertFalse("Should never be equal to null.", r.equals(null)); 
    178139        assertFalse("Should never be equal to a different type.", r 
    … …  
    186147    @Test 
    187148    public void testHashCodeDiffCalls() { 
    188         PlanePoint p = new PlanePoint(500, -30000), q = new PlanePoint(0, 0); 
    189         Segment r = new Segment(p, q); 
     149        Segment r = new Segment(500, -30000, 0, 0); 
    190150        int h = r.hashCode(); 
    191151        for (int i = 0; i < MAX_ITER; ++i) { 
    … …  
    197157    @Test 
    198158    public void testHashCodeObjectEquality() { 
    199         PlanePoint p, q; 
    200159        Segment r, s; 
    201160        for (int i = -MAX_ITER / 2; i < MAX_ITER / 2; ++i) { 
    202             p = new PlanePoint(i, -i); 
    203             q = new PlanePoint(i + 1, -i + 1); 
    204             r = new Segment(p, q); 
    205             s = new Segment(p, q); 
     161            r = new Segment(i, -i, i + 1, -i + 1); 
     162            s = new Segment(i, -i, i + 1, -i + 1); 
    206163            assertEquals("Should return same value for equal objects [i = " + i 
    207164                    + "].", r.hashCode(), s.hashCode()); 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/