package com.ximple.io.dgn7;
|
|
import java.nio.ByteOrder;
|
|
import org.apache.log4j.Logger;
|
|
import com.vividsolutions.jts.geom.Coordinate;
|
import com.vividsolutions.jts.geom.Geometry;
|
import com.vividsolutions.jts.geom.GeometryFactory;
|
|
/**
|
* ArcElement
|
*
|
* @author Ulysses
|
* @version 0.1
|
* @since 2006/5/26 下午 06:41:45
|
*/
|
public class ArcElement extends Element implements GeometryConverter
|
{
|
private static final Logger logger = Logger.getLogger(ArcElement.class);
|
|
public ArcElement(byte[] raw)
|
{
|
super(raw);
|
}
|
|
public double getStartAngle()
|
{
|
int angle = (raw[18] & 0x0000ffff) << 16 | (raw[19] & 0x0000ffff);
|
return Utility.converIntToRotation(angle);
|
}
|
|
public void setStartAngle(double value)
|
{
|
int angle = Utility.converRotatioToInt(value);
|
|
raw[18] = (short) (angle >>> 16 & 0x0000ffff);
|
raw[19] = (short) (angle & 0x0000ffff);
|
}
|
|
public double getSweepAngle()
|
{
|
int angle = (raw[20] & 0x0000ffff) << 16 | (raw[21] & 0x0000ffff);
|
if (angle < 0)
|
angle = -1 * (angle & 0x7fffffff);
|
|
return Utility.converIntToRotation(angle);
|
}
|
|
public void setSweepAngle(double value)
|
{
|
int angle = Utility.converRotatioToInt(value);
|
if (angle < 0)
|
{
|
angle &= 0x7fffffff;
|
angle |= 0x80000000;
|
}
|
|
raw[20] = (short) (angle >> 16 & 0x0000ffff);
|
raw[21] = (short) (angle & 0x0000ffff);
|
}
|
|
public double getPrimary()
|
{
|
rawBuffer.position(22 * 2);
|
ByteOrder bo = rawBuffer.order();
|
rawBuffer.order(ByteOrder.BIG_ENDIAN);
|
byte[] primary = new byte[8];
|
rawBuffer.get(primary);
|
rawBuffer.order(bo);
|
return Utility.convertDGNToIEEEDouble(primary) / 1000.0;
|
}
|
|
public void setPrimary(double value)
|
{
|
double temp = value * 1000.0;
|
short[] primary = Utility.convertIEEEDoubleToDGN(temp);
|
|
System.arraycopy(primary, 0, raw, 22, 4);
|
}
|
|
public double getSecondary()
|
{
|
rawBuffer.position(26 * 2);
|
ByteOrder bo = rawBuffer.order();
|
rawBuffer.order(ByteOrder.BIG_ENDIAN);
|
byte[] secondary = new byte[8];
|
rawBuffer.get(secondary);
|
rawBuffer.order(bo);
|
return Utility.convertDGNToIEEEDouble(secondary) / 1000.0;
|
}
|
|
public void setSecondary(double value)
|
{
|
double temp = value * 1000.0;
|
short[] secondary = Utility.convertIEEEDoubleToDGN(temp);
|
|
System.arraycopy(secondary, 0, raw, 26, 4);
|
}
|
|
public double getRotationAngle()
|
{
|
int rotation = (raw[30] << 16 & 0xffff0000);
|
rotation |= raw[31] & 0x0000ffff;
|
|
return Utility.converIntToRotation(rotation);
|
}
|
|
public void setRotationAngle(double value)
|
{
|
int angle = Utility.converRotatioToInt(value);
|
|
raw[30] = (short) (angle >> 16 & 0x0000ffff);
|
raw[31] = (short) (angle & 0x0000ffff);
|
}
|
|
public Coordinate getOrigin()
|
{
|
rawBuffer.position(32 * 2);
|
ByteOrder bo = rawBuffer.order();
|
rawBuffer.order(ByteOrder.BIG_ENDIAN);
|
byte[] rawValue = new byte[8];
|
|
rawBuffer.get(rawValue); // x
|
double dx = Utility.converUnitToCoord(Utility.convertDGNToIEEEDouble(rawValue));
|
|
rawBuffer.get(rawValue); // y
|
double dy = Utility.converUnitToCoord(Utility.convertDGNToIEEEDouble(rawValue));
|
|
rawBuffer.order(bo);
|
|
return new Coordinate(dx, dy);
|
}
|
|
public void setOrigin(Coordinate value)
|
{
|
double temp = Utility.converCoordToUnit(value.x);
|
short[] x = Utility.convertIEEEDoubleToDGN(temp);
|
|
System.arraycopy(x, 0, raw, 32, 4);
|
temp = Utility.converCoordToUnit(value.y);
|
|
short[] y = Utility.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);
|
temp /= 4;
|
int pts = (temp < 3) ? 3 : (int) temp;
|
return factory.createLineString(convertToLineString(pts));
|
}
|
|
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);
|
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 = 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++;
|
}
|
}
|
return result;
|
}
|
|
public static class ElementHandler extends Element.ElementHandler
|
{
|
private static ElementHandler instance = null;
|
|
public ElementHandler()
|
{
|
super(ElementType.ARC);
|
}
|
|
public static IElementHandler getInstance()
|
{
|
if (instance == null)
|
{
|
instance = new ElementHandler();
|
}
|
|
return instance;
|
}
|
|
protected Element createElement(byte[] raw)
|
{
|
return new ArcElement(raw);
|
}
|
}
|
}
|