forked from geodmms/xdgnjobs

Dennis Kao
2014-01-15 94ae08701bbd7585a0b7e5a92d1975965a503c03
xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/EllipseElement.java
@@ -11,35 +11,28 @@
import com.ximple.util.DgnUtility;
public class EllipseElement extends Element implements GeometryConverter
{
public class EllipseElement extends Element implements GeometryConverter {
    private static final Logger logger = Logger.getLogger(EllipseElement.class);
    public EllipseElement(byte[] raw)
    {
    EllipseElement(byte[] raw) {
        super(raw);
    }
    public double getStartAngle()
    {
    public double getStartAngle() {
        return 0.0;
    }
    public void setStartAngle(double value)
    {
    public void setStartAngle(double value) {
    }
    public double getSweepAngle()
    {
    public double getSweepAngle() {
        return 360.0;
    }
    public void setSweepAngle(double value)
    {
    public void setSweepAngle(double value) {
    }
    public double getPrimary()
    {
    public double getPrimary() {
        rawBuffer.position(18 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
@@ -49,16 +42,14 @@
        return DgnUtility.convertDGNToIEEEDouble(primary) / 1000.0;
    }
    public void setPrimary(double value)
    {
    public void setPrimary(double value) {
        double temp = value * 1000.0;
        short[] primary = DgnUtility.convertIEEEDoubleToDGN(temp);
        System.arraycopy(primary, 0, raw, 18, 4);
    }
    public double getSecondary()
    {
    public double getSecondary() {
        rawBuffer.position(22 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
@@ -68,32 +59,28 @@
        return DgnUtility.convertDGNToIEEEDouble(secondary) / 1000.0;
    }
    public void setSecondary(double value)
    {
    public void setSecondary(double value) {
        double temp = value * 1000.0;
        short[] secondary = DgnUtility.convertIEEEDoubleToDGN(temp);
        System.arraycopy(secondary, 0, raw, 22, 4);
    }
    public double getRotationAngle()
    {
    public double getRotationAngle() {
        int rotation = (raw[26] << 16 & 0xffff0000);
        rotation |= raw[27] & 0x0000ffff;
        return DgnUtility.converIntToRotation(rotation);
    }
    public void setRotationAngle(double value)
    {
    public void setRotationAngle(double value) {
        int angle = DgnUtility.converRotatioToInt(value);
        raw[26] = (short) (angle >> 16 & 0x0000ffff);
        raw[27] = (short) (angle & 0x0000ffff);
    }
    public Coordinate getOrigin()
    {
    public Coordinate getOrigin() {
        rawBuffer.position(28 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
@@ -110,8 +97,7 @@
        return new Coordinate(dx, dy);
    }
    public void setOrigin(Coordinate value)
    {
    public void setOrigin(Coordinate value) {
        double temp = DgnUtility.converCoordToUnit(value.x);
        short[] x = DgnUtility.convertIEEEDoubleToDGN(temp);
@@ -123,16 +109,14 @@
        System.arraycopy(y, 0, raw, 32, 4);
    }
    public Geometry toGeometry(GeometryFactory factory)
    {
    public Geometry toGeometry(GeometryFactory factory) {
        double temp = Math.abs(getStartAngle() - getSweepAngle());
        temp /= 4;
        int pts = (temp < 3) ? 3 : (int) temp;
        return factory.createPolygon(factory.createLinearRing(convertToLineString(pts)), null);
        return factory.createPolygon(factory.createLinearRing(convertToPolygon(pts)), null);
    }
    private Coordinate[] convertToLineString(int pts)
    {
    private Coordinate[] convertToPolygon(int pts) {
        ArrayList<Coordinate> result = new ArrayList<Coordinate>();
        double beta = DgnUtility.converRotationToRadian(-getRotationAngle());
        double startAngle = getStartAngle();
@@ -140,28 +124,16 @@
        double endAngle = startAngle + sweepAngle;
        double steps = sweepAngle / pts;
        double current;
        if (sweepAngle < 0)
        {
            for (current = startAngle; current > endAngle; current += steps)
            {
                Coordinate pt = computePointOnArcByAngle(beta, current);
                result.add(pt);
            }
        } else
        {
            for (current = startAngle; current < endAngle; current += steps)
            {
                Coordinate pt = computePointOnArcByAngle(beta, current);
                result.add(pt);
            }
        //sweepAngle always 360 degree
        for (current = startAngle; current < endAngle; current += steps) {
            Coordinate pt = computePointOnEllipseByAngle(beta, current);
            result.add(pt);
        }
        Coordinate pt = computePointOnArcByAngle(beta, endAngle);
        Coordinate pt = computePointOnEllipseByAngle(beta, endAngle);
        result.add(pt);
        if (!result.get(0).equals(result.get(result.size() - 1)))
        {
        if (!result.get(0).equals(result.get(result.size() - 1))) {
            result.add(result.get(0));
        }
@@ -169,8 +141,7 @@
    }
    private Coordinate computePointOnArcByAngle(double beta, double current)
    {
    private Coordinate computePointOnEllipseByAngle(double beta, double current) {
        double sinbeta = Math.sin(beta);
        double cosbeta = Math.cos(beta);
        Coordinate pt = new Coordinate();
@@ -178,33 +149,28 @@
        double sinalpha = Math.sin(alpha);
        double cosalpha = Math.cos(alpha);
        pt.x = getOrigin().x + (getPrimary() * cosalpha * cosbeta -
                getSecondary() * sinalpha * sinbeta);
            getSecondary() * sinalpha * sinbeta);
        pt.y = getOrigin().y + (getPrimary() * cosalpha * sinbeta +
                getSecondary() * sinalpha * cosbeta);
            getSecondary() * sinalpha * cosbeta);
        return pt;
    }
    public static class ElementHandler extends Element.ElementHandler
    {
    public static class ElementHandler extends Element.ElementHandler {
        private static ElementHandler instance = null;
        public ElementHandler()
        {
        public ElementHandler() {
            super(ElementType.ELLIPSE);
        }
        public static IElementHandler getInstance()
        {
            if (instance == null)
            {
        public static IElementHandler getInstance() {
            if (instance == null) {
                instance = new ElementHandler();
            }
            return instance;
        }
        protected Element createElement(byte[] raw)
        {
        protected Element createElement(byte[] raw) {
            return new EllipseElement(raw);
        }
    }