package com.ximple.io.dgn7;
|
|
import com.vividsolutions.jts.geom.Coordinate;
|
import com.vividsolutions.jts.geom.Geometry;
|
import com.vividsolutions.jts.geom.GeometryFactory;
|
|
public class EllipseElement extends Element implements GeometryConverter
|
{
|
public EllipseElement(short[] 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()
|
{
|
short[] primary = new short[4];
|
|
System.arraycopy(raw, 18, primary, 0, 4);
|
|
return Utility.DGNToIEEEDouble(primary) / 1000.0;
|
}
|
|
public void setPrimary(double value)
|
{
|
double temp = value * 1000.0;
|
short[] primary = Utility.IEEEDoubleToDGN(temp);
|
|
System.arraycopy(primary, 0, raw, 18, 4);
|
}
|
|
public double getSecondary()
|
{
|
short[] secondary = new short[4];
|
|
System.arraycopy(raw, 22, secondary, 0, 4);
|
|
return Utility.DGNToIEEEDouble(secondary) / 1000.0;
|
}
|
|
public void setSecondary(double value)
|
{
|
double temp = value * 1000.0;
|
short[] secondary = Utility.IEEEDoubleToDGN(temp);
|
|
System.arraycopy(secondary, 0, raw, 22, 4);
|
}
|
|
public double getRotationAngle()
|
{
|
int rotation = (raw[26] << 16 & 0xffff0000);
|
|
rotation += raw[27] & 0x0000ffff;
|
|
return Utility.ConverIntToRotation(rotation);
|
}
|
|
public void setRotationAngle(double value)
|
{
|
int angle = Utility.ConverRotatioToInt(value);
|
|
raw[26] = (short) (angle >> 16 & 0x0000ffff);
|
raw[27] = (short) (angle & 0x0000ffff);
|
}
|
|
public Coordinate getOrigin()
|
{
|
short[] x = new short[4];
|
|
System.arraycopy(raw, 28, x, 0, 4);
|
|
double dx = Utility.ConverUnitToCoord((int) Utility.DGNToIEEEDouble(x));
|
short[] y = new short[4];
|
|
System.arraycopy(raw, 32, y, 0, 4);
|
|
double dy = Utility.ConverUnitToCoord((int) Utility.DGNToIEEEDouble(y));
|
|
return new Coordinate(dx, dy);
|
}
|
|
public void setOrigin(Coordinate value)
|
{
|
double temp = Utility.ConverCoordToUnit(value.x);
|
short[] x = Utility.IEEEDoubleToDGN(temp);
|
|
System.arraycopy(x, 0, raw, 28, 4);
|
temp = Utility.ConverCoordToUnit(value.y);
|
|
short[] y = Utility.IEEEDoubleToDGN(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(convertToLineString(pts)), null);
|
}
|
|
private Coordinate[] convertToLineString(int pts)
|
{
|
Coordinate[] result = new Coordinate[pts];
|
double beta = -getRotationAngle() / 180 * Math.PI;
|
double sinbeta = Math.sin(beta);
|
double cosbeta = Math.cos(beta);
|
double startAngle = getStartAngle();
|
double endAngle = getSweepAngle();
|
double steps = Math.abs(startAngle - endAngle) / pts;
|
int i = 0;
|
for (double current = startAngle; current < endAngle; current += steps)
|
{
|
if (i < pts)
|
{
|
Coordinate pt = new Coordinate();
|
double alpha = current / 180 * Math.PI;
|
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++;
|
}
|
}
|
return result;
|
}
|
|
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(short[] raw)
|
{
|
return new EllipseElement(raw);
|
}
|
}
|
}
|