From 0acf7a49b2da4dd1873d64012ba360eaf51754f4 Mon Sep 17 00:00:00 2001
From: ?? ? <ulysseskao@ximple.com.tw>
Date: Thu, 15 May 2008 10:42:55 +0800
Subject: [PATCH] update for EOFM-83

---
 xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/ArcElement.java |   98 +++++++++++++++++++++++++++++-------------------
 1 files changed, 59 insertions(+), 39 deletions(-)

diff --git a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/ArcElement.java b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/ArcElement.java
index e54f755..bd452df 100644
--- a/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/ArcElement.java
+++ b/xdgnjobs/ximple-dgnio/src/main/java/com/ximple/io/dgn7/ArcElement.java
@@ -1,12 +1,15 @@
 package com.ximple.io.dgn7;
 
 import java.nio.ByteOrder;
+import java.util.ArrayList;
 
 import org.apache.log4j.Logger;
 
 import com.vividsolutions.jts.geom.Coordinate;
 import com.vividsolutions.jts.geom.Geometry;
 import com.vividsolutions.jts.geom.GeometryFactory;
+
+import com.ximple.util.DgnUtility;
 
 /**
  * ArcElement
@@ -27,12 +30,12 @@
     public double getStartAngle()
     {
         int angle = (raw[18] & 0x0000ffff) << 16 | (raw[19] & 0x0000ffff);
-        return Utility.converIntToRotation(angle);
+        return DgnUtility.converIntToRotation(angle);
     }
 
     public void setStartAngle(double value)
     {
-        int angle = Utility.converRotatioToInt(value);
+        int angle = DgnUtility.converRotatioToInt(value);
 
         raw[18] = (short) (angle >>> 16 & 0x0000ffff);
         raw[19] = (short) (angle & 0x0000ffff);
@@ -44,12 +47,12 @@
         if (angle < 0)
             angle = -1 * (angle & 0x7fffffff);
 
-        return Utility.converIntToRotation(angle);
+        return DgnUtility.converIntToRotation(angle);
     }
 
     public void setSweepAngle(double value)
     {
-        int angle = Utility.converRotatioToInt(value);
+        int angle = DgnUtility.converRotatioToInt(value);
         if (angle < 0)
         {
             angle &= 0x7fffffff;
@@ -68,13 +71,13 @@
         byte[] primary = new byte[8];
         rawBuffer.get(primary);
         rawBuffer.order(bo);
-        return Utility.convertDGNToIEEEDouble(primary) / 1000.0;
+        return DgnUtility.convertDGNToIEEEDouble(primary) / 1000.0;
     }
 
     public void setPrimary(double value)
     {
         double temp = value * 1000.0;
-        short[] primary = Utility.convertIEEEDoubleToDGN(temp);
+        short[] primary = DgnUtility.convertIEEEDoubleToDGN(temp);
 
         System.arraycopy(primary, 0, raw, 22, 4);
     }
@@ -87,13 +90,13 @@
         byte[] secondary = new byte[8];
         rawBuffer.get(secondary);
         rawBuffer.order(bo);
-        return Utility.convertDGNToIEEEDouble(secondary) / 1000.0;
+        return DgnUtility.convertDGNToIEEEDouble(secondary) / 1000.0;
     }
 
     public void setSecondary(double value)
     {
         double temp = value * 1000.0;
-        short[] secondary = Utility.convertIEEEDoubleToDGN(temp);
+        short[] secondary = DgnUtility.convertIEEEDoubleToDGN(temp);
 
         System.arraycopy(secondary, 0, raw, 26, 4);
     }
@@ -103,12 +106,12 @@
         int rotation = (raw[30] << 16 & 0xffff0000);
         rotation |= raw[31] & 0x0000ffff;
 
-        return Utility.converIntToRotation(rotation);
+        return DgnUtility.converIntToRotation(rotation);
     }
 
     public void setRotationAngle(double value)
     {
-        int angle = Utility.converRotatioToInt(value);
+        int angle = DgnUtility.converRotatioToInt(value);
 
         raw[30] = (short) (angle >> 16 & 0x0000ffff);
         raw[31] = (short) (angle & 0x0000ffff);
@@ -122,10 +125,10 @@
         byte[] rawValue = new byte[8];
 
         rawBuffer.get(rawValue); // x
-        double dx = Utility.converUnitToCoord(Utility.convertDGNToIEEEDouble(rawValue));
+        double dx = DgnUtility.converUnitToCoord(DgnUtility.convertDGNToIEEEDouble(rawValue));
 
         rawBuffer.get(rawValue); // y
-        double dy = Utility.converUnitToCoord(Utility.convertDGNToIEEEDouble(rawValue));
+        double dy = DgnUtility.converUnitToCoord(DgnUtility.convertDGNToIEEEDouble(rawValue));
 
         rawBuffer.order(bo);
 
@@ -134,22 +137,21 @@
 
     public void setOrigin(Coordinate value)
     {
-        double temp = Utility.converCoordToUnit(value.x);
-        short[] x = Utility.convertIEEEDoubleToDGN(temp);
+        double temp = DgnUtility.converCoordToUnit(value.x);
+        short[] x = DgnUtility.convertIEEEDoubleToDGN(temp);
 
         System.arraycopy(x, 0, raw, 32, 4);
-        temp = Utility.converCoordToUnit(value.y);
+        temp = DgnUtility.converCoordToUnit(value.y);
 
-        short[] y = Utility.convertIEEEDoubleToDGN(temp);
+        short[] y = DgnUtility.convertIEEEDoubleToDGN(temp);
 
         System.arraycopy(y, 0, raw, 36, 4);
     }
 
     public Geometry toGeometry(GeometryFactory factory)
     {
-        double start = getStartAngle();
-        double end = getSweepAngle();
-        double temp = Math.abs(start - end);
+        double sweep = getSweepAngle();
+        double temp = Math.abs(sweep);
         temp /= 4;
         int pts = (temp < 3) ? 3 : (int) temp;
         return factory.createLineString(convertToLineString(pts));
@@ -157,31 +159,49 @@
 
     private Coordinate[] convertToLineString(int pts)
     {
-        Coordinate[] result = new Coordinate[pts];
-        double beta = Utility.converRotationToRadian(-getRotationAngle());
-        double sinbeta = Math.sin(beta);
-        double cosbeta = Math.cos(beta);
+        ArrayList<Coordinate> result = new ArrayList<Coordinate>();
+        double beta = DgnUtility.converRotationToRadian(-getRotationAngle());
         double startAngle = getStartAngle();
-        double endAngle = getSweepAngle();
-        double steps = Math.abs(startAngle - endAngle) / pts;
-        int i = 0;
-        for (double current = startAngle; current < endAngle; current += steps)
+        double sweepAngle = getSweepAngle();
+        double endAngle = startAngle + sweepAngle;
+        double steps = sweepAngle / pts;
+        double current;
+        if (sweepAngle < 0)
         {
-            if (i < pts)
+            for (current = startAngle; current > endAngle; current += steps)
             {
-                Coordinate pt = new Coordinate();
-                double alpha = Utility.converRotationToRadian(current);
-                double sinalpha = Math.sin(alpha);
-                double cosalpha = Math.cos(alpha);
-                pt.x = getOrigin().x + (getPrimary() * cosalpha * cosbeta -
-                        getSecondary() * sinalpha * sinbeta);
-                pt.y = getOrigin().y + (getPrimary() * cosalpha * sinbeta +
-                        getSecondary() * sinalpha * cosbeta);
-                result[i] = pt;
-                i++;
+                Coordinate pt = computePointOnArcByAngle(beta, current);
+                result.add(pt);
+            }
+
+        } else
+        {
+            for (current = startAngle; current < endAngle; current += steps)
+            {
+                Coordinate pt = computePointOnArcByAngle(beta, current);
+                result.add(pt);
             }
         }
-        return result;
+
+        Coordinate pt = computePointOnArcByAngle(beta, endAngle);
+        result.add(pt);
+
+        return result.toArray(new Coordinate[result.size()]);
+    }
+
+    private Coordinate computePointOnArcByAngle(double beta, double current)
+    {
+        double sinbeta = Math.sin(beta);
+        double cosbeta = Math.cos(beta);
+        Coordinate pt = new Coordinate();
+        double alpha = DgnUtility.converRotationToRadian(current);
+        double sinalpha = Math.sin(alpha);
+        double cosalpha = Math.cos(alpha);
+        pt.x = getOrigin().x + (getPrimary() * cosalpha * cosbeta -
+                getSecondary() * sinalpha * sinbeta);
+        pt.y = getOrigin().y + (getPrimary() * cosalpha * sinbeta +
+                getSecondary() * sinalpha * cosbeta);
+        return pt;
     }
 
     public static class ElementHandler extends Element.ElementHandler

--
Gitblit v0.0.0-SNAPSHOT