package com.ximple.util; import java.nio.ByteBuffer; import java.nio.ByteOrder; import org.apache.log4j.Logger; import com.vividsolutions.jts.geom.Envelope; /** * Utility * * @author Ulysses * @version 0.1 * @since 2006/5/18 下午 01:33:00 */ public final class DgnUtility { private static final Logger logger = Logger.getLogger(DgnUtility.class); 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 double converUnitToCoord(double 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 long convertDGNToRAWIEEEDouble(byte[] org) { ByteBuffer buf = ByteBuffer.allocate(8); buf.order(ByteOrder.LITTLE_ENDIAN); buf.mark(); buf.put(org[2]); buf.put(org[3]); buf.put(org[0]); buf.put(org[1]); buf.put(org[6]); buf.put(org[7]); buf.put(org[4]); buf.put(org[5]); buf.position(0); int[] tmp = new int[2]; tmp[0] = buf.getInt(); tmp[1] = buf.getInt(); int exponent; int sign = (tmp[0] & 0x80000000); exponent = (tmp[0] >>> 23) & 0x000000ff; if (exponent != 0) { exponent = exponent - 129 + 1023; } int 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; buf.position(0); buf.order(ByteOrder.BIG_ENDIAN); buf.putInt(tmp[0]); buf.putInt(tmp[1]); buf.position(0); byte[] tmpRaw = new byte[8]; buf.get(tmpRaw); buf.position(0); buf.order(ByteOrder.LITTLE_ENDIAN); for (int i = tmpRaw.length; i > 0; i--) { buf.put(tmpRaw[i - 1]); } buf.position(0); long result = buf.getLong(); return result; } public static double convertDGNToIEEEDouble(byte[] src) { return Double.longBitsToDouble(convertDGNToRAWIEEEDouble(src)); } 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)); } }