?? ?
2010-08-27 1c3b0bcfb1df3558d72f2dc4d190a4952d3d3119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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;
 
public class EllipseElement extends Element implements GeometryConverter {
    private static final Logger logger = Logger.getLogger(EllipseElement.class);
 
    EllipseElement(byte[] raw) {
        super(raw);
    }
 
    public double getStartAngle() {
        return 0.0;
    }
 
    public void setStartAngle(double value) {
    }
 
    public double getSweepAngle() {
        return 360.0;
    }
 
    public void setSweepAngle(double value) {
    }
 
    public double getPrimary() {
        rawBuffer.position(18 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
        byte[] primary = new byte[8];
        rawBuffer.get(primary);
        rawBuffer.order(bo);
        return DgnUtility.convertDGNToIEEEDouble(primary) / 1000.0;
    }
 
    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() {
        rawBuffer.position(22 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
        byte[] secondary = new byte[8];
        rawBuffer.get(secondary);
        rawBuffer.order(bo);
        return DgnUtility.convertDGNToIEEEDouble(secondary) / 1000.0;
    }
 
    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() {
        int rotation = (raw[26] << 16 & 0xffff0000);
        rotation |= raw[27] & 0x0000ffff;
 
        return DgnUtility.converIntToRotation(rotation);
    }
 
    public void setRotationAngle(double value) {
        int angle = DgnUtility.converRotatioToInt(value);
 
        raw[26] = (short) (angle >> 16 & 0x0000ffff);
        raw[27] = (short) (angle & 0x0000ffff);
    }
 
    public Coordinate getOrigin() {
        rawBuffer.position(28 * 2);
        ByteOrder bo = rawBuffer.order();
        rawBuffer.order(ByteOrder.BIG_ENDIAN);
        byte[] rawValue = new byte[8];
 
        rawBuffer.get(rawValue); // x
        double dx = DgnUtility.converUnitToCoord(DgnUtility.convertDGNToIEEEDouble(rawValue));
 
        rawBuffer.get(rawValue); // y
        double dy = DgnUtility.converUnitToCoord(DgnUtility.convertDGNToIEEEDouble(rawValue));
 
        rawBuffer.order(bo);
 
        return new Coordinate(dx, dy);
    }
 
    public void setOrigin(Coordinate value) {
        double temp = DgnUtility.converCoordToUnit(value.x);
        short[] x = DgnUtility.convertIEEEDoubleToDGN(temp);
 
        System.arraycopy(x, 0, raw, 28, 4);
        temp = DgnUtility.converCoordToUnit(value.y);
 
        short[] y = DgnUtility.convertIEEEDoubleToDGN(temp);
 
        System.arraycopy(y, 0, raw, 32, 4);
    }
 
    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(convertToPolygon(pts)), null);
    }
 
    private Coordinate[] convertToPolygon(int pts) {
        ArrayList<Coordinate> result = new ArrayList<Coordinate>();
        double beta = DgnUtility.converRotationToRadian(-getRotationAngle());
        double startAngle = getStartAngle();
        double sweepAngle = getSweepAngle();
        double endAngle = startAngle + sweepAngle;
        double steps = sweepAngle / pts;
        double current;
        //sweepAngle always 360 degree
        for (current = startAngle; current < endAngle; current += steps) {
            Coordinate pt = computePointOnEllipseByAngle(beta, current);
            result.add(pt);
        }
 
        Coordinate pt = computePointOnEllipseByAngle(beta, endAngle);
        result.add(pt);
 
        if (!result.get(0).equals(result.get(result.size() - 1))) {
            result.add(result.get(0));
        }
 
        return result.toArray(new Coordinate[result.size()]);
    }
 
 
    private Coordinate computePointOnEllipseByAngle(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 {
        private static ElementHandler instance = null;
 
        public ElementHandler() {
            super(ElementType.ELLIPSE);
        }
 
        public static IElementHandler getInstance() {
            if (instance == null) {
                instance = new ElementHandler();
            }
 
            return instance;
        }
 
        protected Element createElement(byte[] raw) {
            return new EllipseElement(raw);
        }
    }
}