use of com.graphbuilder.curve.ShapeMultiPath in project poi by apache.
the class SplineCollector method addToPath.
public void addToPath(java.awt.geom.Path2D.Double path, XDGFShape parent) {
// ok, we have the start, and all knots... do something with this
Point2D last = path.getCurrentPoint();
// create a control path and knots
ControlPath controlPath = new ControlPath();
ValueVector knots = new ValueVector(_knots.size() + 3);
double firstKnot = _start.getB();
double lastKnot = _start.getC();
int degree = _start.getD();
// first/second knot
knots.add(firstKnot);
knots.add(_start.getA());
// first/second control point
controlPath.addPoint(PointFactory.create(last.getX(), last.getY()));
controlPath.addPoint(PointFactory.create(_start.getX(), _start.getY()));
// middle knots/control points
for (SplineKnot knot : _knots) {
knots.add(knot.getA());
controlPath.addPoint(PointFactory.create(knot.getX(), knot.getY()));
}
// last knot
knots.add(lastKnot);
ShapeMultiPath shape = SplineRenderer.createNurbsSpline(controlPath, knots, null, degree);
path.append(shape, true);
}
use of com.graphbuilder.curve.ShapeMultiPath in project poi by apache.
the class NURBSTo method addToPath.
@Override
public void addToPath(java.awt.geom.Path2D.Double path, XDGFShape parent) {
if (getDel())
return;
Point2D last = path.getCurrentPoint();
// A NURBS formula: knotLast, degree, xType, yType, x1, y1, knot1,
// weight1, ..
String formula = getE().trim();
if (!formula.startsWith("NURBS(") || !formula.endsWith(")"))
throw new POIXMLException("Invalid NURBS formula: " + formula);
String[] components = formula.substring(6, formula.length() - 1).split(",");
if (components.length < 8)
throw new POIXMLException("Invalid NURBS formula (not enough arguments)");
if ((components.length - 4) % 4 != 0)
throw new POIXMLException("Invalid NURBS formula -- need 4 + n*4 arguments, got " + components.length);
double lastControlX = getX();
double lastControlY = getY();
double secondToLastKnot = getA();
double lastWeight = getB();
double firstKnot = getC();
double firstWeight = getD();
double lastKnot = Double.parseDouble(components[0].trim());
int degree = Integer.parseInt(components[1].trim());
int xType = Integer.parseInt(components[2].trim());
int yType = Integer.parseInt(components[3].trim());
double xScale = 1;
double yScale = 1;
if (xType == 0)
xScale = parent.getWidth();
if (yType == 0)
yScale = parent.getHeight();
// setup first knots/weights/control point
ControlPath controlPath = new ControlPath();
ValueVector knots = new ValueVector();
ValueVector weights = new ValueVector();
knots.add(firstKnot);
weights.add(firstWeight);
controlPath.addPoint(PointFactory.create(last.getX(), last.getY()));
// iterate get knots/weights
int sets = (components.length - 4) / 4;
for (int i = 0; i < sets; i++) {
double x1 = Double.parseDouble(components[4 + i * 4 + 0].trim());
double y1 = Double.parseDouble(components[4 + i * 4 + 1].trim());
double k = Double.parseDouble(components[4 + i * 4 + 2].trim());
double w = Double.parseDouble(components[4 + i * 4 + 3].trim());
controlPath.addPoint(PointFactory.create(x1 * xScale, y1 * yScale));
knots.add(k);
weights.add(w);
}
// last knots/weights/control point
knots.add(secondToLastKnot);
knots.add(lastKnot);
weights.add(lastWeight);
controlPath.addPoint(PointFactory.create(lastControlX, lastControlY));
ShapeMultiPath shape = SplineRenderer.createNurbsSpline(controlPath, knots, weights, degree);
path.append(shape, true);
}
use of com.graphbuilder.curve.ShapeMultiPath in project poi by apache.
the class SplineRenderer method createNurbsSpline.
public static ShapeMultiPath createNurbsSpline(ControlPath controlPoints, ValueVector knots, ValueVector weights, int degree) {
double firstKnot = knots.get(0);
final int count = knots.size();
double lastKnot = knots.get(count - 1);
// scale knots to [0, 1] based on first/last knots
for (int i = 0; i < count; i++) {
knots.set((knots.get(i) - firstKnot) / lastKnot, i);
}
// if we don't have enough knots, duplicate the last knot until we do
final int knotsToAdd = controlPoints.numPoints() + degree + 1;
for (int i = count; i < knotsToAdd; i++) {
knots.add(1);
}
GroupIterator gi = new GroupIterator("0:n-1", controlPoints.numPoints());
NURBSpline spline = new NURBSpline(controlPoints, gi);
spline.setDegree(degree);
spline.setKnotVectorType(NURBSpline.NON_UNIFORM);
spline.setKnotVector(knots);
if (weights == null) {
spline.setUseWeightVector(false);
} else {
spline.setWeightVector(weights);
}
// now that this is done, add it to the path
ShapeMultiPath shape = new ShapeMultiPath();
shape.setFlatness(0.01);
spline.appendTo(shape);
return shape;
}
Aggregations