Ticket #14485: styled-map-renderer-precompute-order.patch

File styled-map-renderer-precompute-order.patch, 3.7 KB (added by michael2402, 8 years ago)
  • src/org/openstreetmap/josm/data/osm/visitor/paint/StyledMapRenderer.java

     
    251251        private final StyleElement style;
    252252        private final OsmPrimitive osm;
    253253        private final int flags;
     254        private final long order;
    254255
    255256        StyleRecord(StyleElement style, OsmPrimitive osm, int flags) {
    256257            this.style = style;
    257258            this.osm = osm;
    258259            this.flags = flags;
    259         }
    260260
    261         @Override
    262         public int compareTo(StyleRecord other) {
    263             if ((this.flags & FLAG_DISABLED) != 0 && (other.flags & FLAG_DISABLED) == 0)
    264                 return -1;
    265             if ((this.flags & FLAG_DISABLED) == 0 && (other.flags & FLAG_DISABLED) != 0)
    266                 return 1;
     261            long order = 0;
     262            if ((this.flags & FLAG_DISABLED) != 0) {
     263                order |= 1;
     264            }
    267265
    268             int d0 = Float.compare(this.style.majorZIndex, other.style.majorZIndex);
    269             if (d0 != 0)
    270                 return d0;
     266            order <<= 24;
     267            order |= floatToFixed(this.style.majorZIndex, 24, 8);
    271268
    272269            // selected on top of member of selected on top of unselected
    273             // FLAG_DISABLED bit is the same at this point
    274             if (this.flags > other.flags)
    275                 return 1;
    276             if (this.flags < other.flags)
    277                 return -1;
     270            // FLAG_DISABLED bit is the same at this point, but we simply ignore it
     271            order <<= 4;
     272            order |= this.flags & 0xf;
    278273
    279             int dz = Float.compare(this.style.zIndex, other.style.zIndex);
    280             if (dz != 0)
    281                 return dz;
     274            order <<= 24;
     275            order |= floatToFixed(this.style.zIndex, 24, 8);
    282276
     277            order <<= 1;
    283278            // simple node on top of icons and shapes
    284             if (NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && !NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
    285                 return 1;
    286             if (!NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style) && NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(other.style))
    287                 return -1;
     279            if (NodeElement.SIMPLE_NODE_ELEMSTYLE.equals(this.style)) {
     280                order |= 1;
     281            }
    288282
     283            this.order = order;
     284        }
     285
     286        /**
     287         * Converts a float to a fixed pointdecimal so that the order stays the same.
     288         *
     289         * @param number The float to convert
     290         * @param totalBits
     291         *            Total number of bits. 1 sign bit, then the bits before the
     292         *            decimal point, then those after.
     293         * @param afterDecimalBits
     294         *            Number of fixed bits after the decimal point.
     295         * @return The float converted to an integer.
     296         */
     297        private static long floatToFixed(double number, int totalBits, int afterDecimalBits) {
     298            long value = (long) (number * (1l << afterDecimalBits));
     299            long highestBitMask = 1l << totalBits - 1;
     300            long valueMask = highestBitMask - 1;
     301            long signBit = number < 0 ? 0 : highestBitMask;
     302            return signBit | value & valueMask;
     303        }
     304
     305        @Override
     306        public int compareTo(StyleRecord other) {
     307            int d = Long.compare(order, other.order);
     308            if (d != 0) {
     309                return d;
     310            }
     311
    289312            // newer primitives to the front
    290313            long id = this.osm.getUniqueId() - other.osm.getUniqueId();
    291314            if (id > 0)