package com.ximple.io.dgn7;
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
import com.vividsolutions.jts.geom.Coordinate;
|
import com.vividsolutions.jts.geom.Geometry;
|
import com.vividsolutions.jts.geom.GeometryFactory;
|
|
/**
|
* LineStringElement
|
*
|
* @author Ulysses
|
* @version 0.1
|
* @since 2006/5/18 下午 02:48:58
|
*/
|
public class LineStringElement extends Element implements GeometryConverter
|
{
|
public LineStringElement(short[] raw)
|
{
|
super(raw);
|
}
|
|
public Coordinate getCentroid(double dTolerance)
|
{
|
return null;
|
}
|
|
public Coordinate getEndPoint()
|
{
|
return new Coordinate(getX(getVerticeSize() - 1), getY(getVerticeSize() - 1));
|
}
|
|
public Coordinate getNormal()
|
{
|
return null;
|
}
|
|
public Coordinate getOrigin()
|
{
|
return null;
|
}
|
|
public Coordinate getStartPoint()
|
{
|
return new Coordinate(getX(0), getY(0));
|
}
|
|
public Coordinate getVertex(int index)
|
{
|
return (index == 0)
|
? getStartPoint()
|
: getEndPoint();
|
}
|
|
public int getVerticeSize()
|
{
|
return raw[18] & 0x0000ffff;
|
}
|
|
public double getLength()
|
{
|
double result = 0.0;
|
Coordinate[] vset = getVertices();
|
|
for (int i = 1; i < getVerticeSize(); i++)
|
{
|
Coordinate p1 = vset[i - 1];
|
Coordinate p2 = vset[i];
|
|
result += Utility.getLength(p1.x, p1.y, p2.x, p2.y);
|
}
|
|
return result;
|
}
|
|
public Coordinate pointAtDistance(double dDistance, double dTolerance)
|
{
|
return null;
|
}
|
|
public Coordinate[] getVertices()
|
{
|
Coordinate[] result = new Coordinate[getVerticeSize()];
|
|
for (int i = 0; i < getVerticeSize(); i++)
|
{
|
result[i] = new Coordinate(getX(i), getY(i));
|
}
|
|
return result;
|
}
|
|
public Geometry toGeometry(GeometryFactory factory)
|
{
|
return factory.createLineString(getVertices());
|
}
|
|
protected double getX(int index)
|
{
|
if ((index < 0) || (index > getVerticeSize()))
|
{
|
return -1;
|
}
|
|
int x = (int) ((raw[19 + (4 * index)] << 16) & 0xffff0000);
|
|
x += (int) (raw[20 + (4 * index)] & 0x0000ffff);
|
|
return Utility.converUnitToCoord(x);
|
}
|
|
protected void setX(int index, double dx)
|
{
|
int newVal = Utility.converCoordToUnit(dx);
|
|
raw[19 + (4 * index)] = (short) (newVal >> 16 & 0x0000ffff);
|
raw[20 + (4 * index)] = (short) (newVal & 0x0000ffff);
|
}
|
|
protected double getY(int index)
|
{
|
if ((index < 0) || (index > getVerticeSize()))
|
{
|
return -1;
|
}
|
|
int y = (int) ((raw[21 + (4 * index)] << 16) & 0xffff0000);
|
|
y = y + (int) (raw[22 + (4 * index)] & 0x0000ffff);
|
|
return Utility.converUnitToCoord(y);
|
}
|
|
protected void setY(int index, double dy)
|
{
|
int newVal = Utility.converCoordToUnit(dy);
|
|
raw[21 + (4 * index)] = (short) ((newVal >> 16) & 0x0000ffff);
|
raw[22 + (4 * index)] = (short) (newVal & 0x0000ffff);
|
}
|
|
public static class ElementHandler extends Element.ElementHandler
|
{
|
private static ElementHandler instance = null;
|
|
public ElementHandler()
|
{
|
super(ElementType.LINESTRING);
|
}
|
|
public static IElementHandler getInstance()
|
{
|
if (instance == null)
|
{
|
instance = new ElementHandler();
|
}
|
|
return instance;
|
}
|
|
protected Element createElement(short[] raw)
|
{
|
return new LineStringElement(raw);
|
}
|
}
|
}
|