|
| 1 | +using System.Drawing; |
| 2 | +using GeoAPI.Geometries; |
| 3 | + |
| 4 | +namespace SharpMap.Utilities |
| 5 | +{ |
| 6 | + public static class RectangleUtility |
| 7 | + { |
| 8 | + public static RectangleF ToRectangleF(this Envelope e) |
| 9 | + { |
| 10 | + //new RectangleF((float)envelope.MinX, (float)envelope.MinY, (float)envelope.Width, (float)envelope.Height); |
| 11 | + return RectangleF.FromLTRB(ToLower(e.MinX), ToLower(e.MinY), ToUpper(e.MaxX), ToUpper(e.MaxY)); |
| 12 | + } |
| 13 | + |
| 14 | + private static float ToLower(double d) |
| 15 | + { |
| 16 | + float f = (float) d; |
| 17 | + if (f <= d) return f; |
| 18 | + return nextafter(f, float.MinValue); |
| 19 | + } |
| 20 | + private static float ToUpper(double d) |
| 21 | + { |
| 22 | + float f = (float)d; |
| 23 | + if (f >= d) return f; |
| 24 | + return nextafter(f, float.MaxValue); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Gets the floating-point number that is next after <paramref name="fromNumber"/> in the direction of <paramref name="towardNumber"/>. |
| 29 | + /// </summary> |
| 30 | + /// <param name="fromNumber">A floating-point number.</param> |
| 31 | + /// <param name="towardNumber">A floating-point number.</param> |
| 32 | + /// <returns>The floating-point number that is next after <paramref name="fromNumber"/> in the direction of <paramref name="towardNumber"/>.</returns> |
| 33 | + /// <remarks> |
| 34 | + /// <para> |
| 35 | + /// IEC 60559 recommends that <paramref name="fromNumber"/> be returned whenever <c><paramref name="fromNumber"/> == <paramref name="towardNumber"/></c>. |
| 36 | + /// These functions return <paramref name="towardNumber"/> instead, which makes the behavior around zero consistent: <c><see cref="math.nextafter(float, float)">nextafter</see>(-0.0, +0.0)</c> |
| 37 | + /// returns <c>+0.0</c> and <c><see cref="math.nextafter(float, float)">nextafter</see>(+0.0, -0.0)</c> returns <c>–0.0</c>. |
| 38 | + /// </para> |
| 39 | + /// <para> |
| 40 | + /// See <a href="http://en.cppreference.com/w/c/numeric/math/nextafter">nextafter</a> in the C standard documentation. |
| 41 | + /// </para> |
| 42 | + /// </remarks> |
| 43 | + /// <example> |
| 44 | + /// <code language="C#"> |
| 45 | + /// Assert.IsTrue(math.nextafter(0F, 0F) == 0F); |
| 46 | + /// Assert.IsTrue(math.nextafter(-0F, 0F) == 0F; |
| 47 | + /// Assert.IsTrue(math.nextafter(0F, -0F) == -0F); |
| 48 | + /// |
| 49 | + /// Assert.IsTrue(math.nextafter(math.FLT_MIN, 0D) == math.FLT_DENORM_MAX); |
| 50 | + /// Assert.IsTrue(math.nextafter(math.FLT_DENORM_MIN, 0F) == 0F); |
| 51 | + /// Assert.IsTrue(math.nextafter(math.FLT_MIN, -0F) == math.FLT_DENORM_MAX); |
| 52 | + /// Assert.IsTrue(math.nextafter(math.FLT_DENORM_MIN, -0F) == 0F); |
| 53 | + /// |
| 54 | + /// Assert.IsTrue(math.nextafter(0F, System.Single.PositiveInfinity) == math.FLT_DENORM_MIN); |
| 55 | + /// Assert.IsTrue(math.nextafter(-0F, System.Single.NegativeInfinity) == -math.FLT_DENORM_MIN); |
| 56 | + /// </code> |
| 57 | + /// <code language="VB.NET"> |
| 58 | + /// Assert.IsTrue(math.nextafter(0F, 0F) = 0F); |
| 59 | + /// Assert.IsTrue(math.nextafter(-0F, 0F) = 0F); |
| 60 | + /// Assert.IsTrue(math.nextafter(0F, -0F) = -0F); |
| 61 | + /// |
| 62 | + /// Assert.IsTrue(math.nextafter(math.FLT_MIN, 0F) = math.FLT_DENORM_MAX); |
| 63 | + /// Assert.IsTrue(math.nextafter(math.FLT_DENORM_MIN, 0F) = 0F); |
| 64 | + /// Assert.IsTrue(math.nextafter(math.FLT_MIN, -0F) = math.FLT_DENORM_MAX); |
| 65 | + /// Assert.IsTrue(math.nextafter(math.FLT_DENORM_MIN, -0F) = 0F); |
| 66 | + /// |
| 67 | + /// Assert.IsTrue(math.nextafter(0F, System.Single.PositiveInfinity) = math.FLT_DENORM_MIN); |
| 68 | + /// Assert.IsTrue(math.nextafter(-0F, System.Single.NegativeInfinity) = -math.FLT_DENORM_MIN); |
| 69 | + /// </code> |
| 70 | + /// </example> |
| 71 | + private static float nextafter(float fromNumber, float towardNumber) |
| 72 | + { |
| 73 | + // If either fromNumber or towardNumber is NaN, return NaN. |
| 74 | + if (System.Single.IsNaN(towardNumber) || System.Single.IsNaN(fromNumber)) |
| 75 | + { |
| 76 | + return System.Single.NaN; |
| 77 | + } |
| 78 | + // If no direction or if fromNumber is infinity or is not a number, return fromNumber. |
| 79 | + if (fromNumber == towardNumber) |
| 80 | + { |
| 81 | + return towardNumber; |
| 82 | + } |
| 83 | + // If fromNumber is zero, return smallest subnormal. |
| 84 | + if (fromNumber == 0) |
| 85 | + { |
| 86 | + return (towardNumber > 0) ? System.Single.Epsilon : -System.Single.Epsilon; |
| 87 | + } |
| 88 | + // All other cases are handled by incrementing or decrementing the bits value. |
| 89 | + // Transitions to infinity, to subnormal, and to zero are all taken care of this way. |
| 90 | + int bits = SingleToInt32Bits(fromNumber); |
| 91 | + // A xor here avoids nesting conditionals. We have to increment if fromValue lies between 0 and toValue. |
| 92 | + if ((fromNumber > 0) ^ (fromNumber > towardNumber)) |
| 93 | + { |
| 94 | + bits += 1; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + bits -= 1; |
| 99 | + } |
| 100 | + return Int32BitsToSingle(bits); |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Converts the specified single-precision floating point number to a 32-bit signed integer. |
| 105 | + /// </summary> |
| 106 | + /// <param name="value">The number to convert.</param> |
| 107 | + /// <returns>A 32-bit signed integer whose value is equivalent to <paramref name="value"/>.</returns> |
| 108 | + private static unsafe int SingleToInt32Bits(float value) |
| 109 | + { |
| 110 | + return *((int*)&value); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Converts the specified 32-bit signed integer to a single-precision floating point number. |
| 115 | + /// </summary> |
| 116 | + /// <param name="value">The number to convert.</param> |
| 117 | + /// <returns>A double-precision floating point number whose value is equivalent to <paramref name="value"/>.</returns> |
| 118 | + private static unsafe float Int32BitsToSingle(int value) |
| 119 | + { |
| 120 | + return *((float*)&value); |
| 121 | + } |
| 122 | + |
| 123 | + } |
| 124 | +} |
0 commit comments