package com.ximple.io.dgn7; import com.vividsolutions.jts.geom.Envelope; /** * Utility * * @author Ulysses * @version 0.1 * @since 2006/5/18 下午 01:33:00 */ public final class Utility { public static double converIntToDouble(int src) { return (double) ((long) ((src * 6) / 1000.0 + 0.5)) / 1000.0; } public static int converDoubleToInt(double src) { return (int) (src / 6 * 1000000.0); } public static int convertFromDGN(int aValue) { int newVal; newVal = (((aValue ^ 0x00008000) << 16) & 0xffff0000); newVal |= (aValue >>> 16) & 0x0000ffff; return newVal; } public static int converToDGN(int aValue) { int newVal; newVal = (aValue << 16 & 0xffff0000); newVal |= (((aValue ^ 0x80000000) >>> 16) & 0x0000ffff); return newVal; } public static double converIntToRotation(int aValue) { return aValue / 360000.0; } public static int converRotatioToInt(double aValue) { return (int) (aValue * 360000.0); } public static double converRotationToRadian(double aValue) { return aValue * Math.PI / 180; } public static double converUnitToCoord(int aValue) { double newVal; newVal = aValue / 1000.0; newVal += 2147483.648; // 2147483.648 = 2 ^ 31 return newVal; } public static int converCoordToUnit(double aValue) { double newVal = aValue; newVal -= 2147483.648; newVal = newVal * 1000.0; return (int) newVal; } public static Envelope converUnitToCoord(Envelope range) { if (range == null) { return null; } return new Envelope(converUnitToCoord((int) range.getMinX()), converUnitToCoord((int) range.getMaxX()), converUnitToCoord((int) range.getMinY()), converUnitToCoord((int) range.getMaxY())); } public static Envelope converCoordToUnit(Envelope range) { if (range == null) { return null; } return new Envelope(converCoordToUnit(range.getMinX()), converCoordToUnit(range.getMaxX()), converCoordToUnit(range.getMinY()), converCoordToUnit(range.getMaxY())); } public static double convertDGNToIEEEDouble(short[] src) { int[] tmp = new int[2]; long des; int sign; int exponent; int rndbits; if (src == null) { throw new RuntimeException("Source short array is null"); } tmp[0] = ((src[0] << 16) & 0xffff0000) | (src[1] & 0x0000ffff); tmp[1] = ((src[2] << 16) & 0xffff0000) | (src[3] & 0x0000ffff); sign = (tmp[0] & 0x80000000); exponent = (tmp[0] >>> 23) & 0x000000ff; if (exponent != 0) { exponent = exponent - 129 + 1023; } rndbits = tmp[1] & 0x00000007; tmp[1] = tmp[1] >>> 3; tmp[1] = (tmp[1] & 0x1fffffff) | (tmp[0] << 29); if (rndbits != 0) { tmp[1] = tmp[1] | 0x00000001; } tmp[0] = (tmp[0] >>> 3) & 0x000fffff; tmp[0] = tmp[0] | (exponent << 20) | sign; des = (((long) tmp[0] << 32)); des = des | (long) tmp[1]; return Double.longBitsToDouble(des); } public static short[] convertIEEEDoubleToDGN(double src) { long newVal = Double.doubleToLongBits(src); // uint[] tmp = new int[ 2 ]; // ushort[] des = new short[ 4 ]; int[] tmp = new int[2]; short[] des = new short[4]; int sign; int exponent; tmp[0] = (int) ((newVal >>> 32)); tmp[1] = (int) (newVal); // sign = ( int ) ( ( uint ) tmp[ 0 ] & 0x80000000 ); sign = tmp[0] & 0x80000000; exponent = (tmp[0] >>> 20) & 0x07ff; if (exponent != 0) { exponent = exponent - 1023 + 129; } if (exponent > 255) { if (sign != 0) { des[0] = -1; } else { des[0] = 0x7fff; } des[1] = -1; des[2] = -1; des[3] = -1; return des; } else if ((exponent < 0) || ((exponent == 0) && (sign == 0))) { des[0] = 0x0; des[1] = 0x0; des[2] = 0x0; des[3] = 0x0; return des; } else { tmp[0] = (tmp[0] << 3) | (tmp[1] >> 29); tmp[0] = tmp[0] & 0x007fffff; tmp[0] = tmp[0] | (exponent << 23) | sign; // changed by phil 07/05/2004 // tmp[ 1 ] = tmp[ 1 ] >> 3; tmp[1] = tmp[1] << 3; } des[0] = (short) ((tmp[0] >>> 16) & 0x0000ffff); des[1] = (short) (tmp[0] & 0x0000ffff); des[2] = (short) ((tmp[1] >>> 16) & 0x0000ffff); des[3] = (short) (tmp[1] & 0x0000ffff); return des; } public static double getLength(double x1, double y1, double x2, double y2) { double dx = x1 - x2; double dy = y1 - y2; return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); } }