Search in sources :

Example 6 with NFastDoubleArrayJSO

use of com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO in project lienzo-core by ahome-it.

the class SVGPath method parse.

public static final void parse(final PathPartList partlist, String path) {
    partlist.clear();
    path = path.replaceAll("\\s+", " ").trim();
    if (Path2D.isSupported()) {
        partlist.setPath2D(new Path2D(path));
    }
    for (final String element : COMMANDS) {
        path = path.replaceAll(element + " ", element);
    }
    path = path.replaceAll(" ", ",");
    for (final String element : COMMANDS) {
        path = path.replaceAll(element, "#" + element);
    }
    final String[] list = path.split("#");
    double cpx = 0;
    double cpy = 0;
    for (int n = 1, l = list.length; n < l; n++) {
        String str = list[n];
        char chr = str.charAt(0);
        str = str.substring(1).replaceAll(",-", "-").replaceAll("-", ",-").replaceAll("e,-", "e-");
        final String[] pts = str.split(",");
        int beg = 0;
        if ((pts.length > 0) && (pts[0].isEmpty())) {
            beg = 1;
        }
        final NFastDoubleArrayJSO source = NFastDoubleArrayJSO.make();
        for (int i = beg, z = pts.length; i < z; i++) {
            source.push(Double.valueOf(pts[i]).doubleValue());
        }
        PathPartEntryJSO prev;
        double ctx, cty;
        while (source.size() > 0) {
            int cmd = PathPartEntryJSO.UNDEFINED_PATH_PART;
            final NFastDoubleArrayJSO points = NFastDoubleArrayJSO.make();
            switch(chr) {
                case 'l':
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'L':
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'm':
                    final double dx = source.shift();
                    final double dy = source.shift();
                    cpx += dx;
                    cpy += dy;
                    final int size = partlist.size();
                    if ((size > 2) && (partlist.get(size - 1).getCommand() == PathPartEntryJSO.CLOSE_PATH_PART)) {
                        for (int idx = size - 2; idx >= 0; idx--) {
                            prev = partlist.get(idx);
                            if (prev.getCommand() == PathPartEntryJSO.MOVETO_ABSOLUTE) {
                                cpx = prev.getPoints().get(0) + dx;
                                cpy = prev.getPoints().get(1) + dy;
                                break;
                            }
                        }
                    }
                    points.push(cpx);
                    points.push(cpy);
                    chr = 'l';
                    cmd = PathPartEntryJSO.MOVETO_ABSOLUTE;
                    break;
                case 'M':
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    chr = 'L';
                    cmd = PathPartEntryJSO.MOVETO_ABSOLUTE;
                    break;
                case 'h':
                    cpx += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'H':
                    cpx = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'v':
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'V':
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'C':
                    points.push(source.shift());
                    points.push(source.shift());
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'c':
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'S':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(2));
                        cty = cpy + (cpy - prev.getPoints().get(3));
                    }
                    points.push(ctx);
                    points.push(cty);
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 's':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(2));
                        cty = cpy + (cpy - prev.getPoints().get(3));
                    }
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'Q':
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'q':
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'T':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(0));
                        cty = cpy + (cpy - prev.getPoints().get(1));
                    }
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 't':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(0));
                        cty = cpy + (cpy - prev.getPoints().get(1));
                    }
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'A':
                    {
                        final double rx = source.shift();
                        final double ry = source.shift();
                        final double ps = source.shift();
                        final double fa = source.shift();
                        final double fs = source.shift();
                        final double x1 = cpx;
                        final double y1 = cpy;
                        cpx = source.shift();
                        cpy = source.shift();
                        PathPartList.convertEndpointToCenterParameterization(points, x1, y1, cpx, cpy, fa, fs, rx, ry, ps);
                        points.push(cpx);
                        points.push(cpy);
                        cmd = PathPartEntryJSO.ARCTO_ABSOLUTE;
                        break;
                    }
                case 'a':
                    {
                        final double rx = source.shift();
                        final double ry = source.shift();
                        final double ps = source.shift();
                        final double fa = source.shift();
                        final double fs = source.shift();
                        final double x1 = cpx;
                        final double y1 = cpy;
                        cpx += source.shift();
                        cpy += source.shift();
                        PathPartList.convertEndpointToCenterParameterization(points, x1, y1, cpx, cpy, fa, fs, rx, ry, ps);
                        points.push(cpx);
                        points.push(cpy);
                        cmd = PathPartEntryJSO.ARCTO_ABSOLUTE;
                        break;
                    }
            }
            if (cmd != PathPartEntryJSO.UNDEFINED_PATH_PART) {
                partlist.push(PathPartEntryJSO.make(cmd, points));
            }
        }
        if ((chr == 'z') || (chr == 'Z')) {
            partlist.Z();
        }
    }
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO) Path2D(com.ait.lienzo.client.core.Path2D) PathPartEntryJSO(com.ait.lienzo.client.core.types.PathPartEntryJSO)

Example 7 with NFastDoubleArrayJSO

use of com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO in project lienzo-core by ahome-it.

the class Geometry method getBoundingBoxOfCurve.

public static final BoundingBox getBoundingBoxOfCurve(final double computedLocationOffsetX, final double computedLocationOffsetY, final Point2DArray points) {
    if (null == points) {
        return null;
    }
    int size = points.size();
    if (size < 3) {
        return null;
    }
    double minx = Double.MAX_VALUE;
    double miny = Double.MAX_VALUE;
    double maxx = -Double.MAX_VALUE;
    double maxy = -Double.MAX_VALUE;
    final NFastDoubleArrayJSO xval = NFastDoubleArrayJSO.make();
    final NFastDoubleArrayJSO yval = NFastDoubleArrayJSO.make();
    for (int i = 0; i < size; i++) {
        final Point2D p = points.get(i);
        xval.push(p.getX());
        yval.push(p.getY());
    }
    final NFastDoubleArrayJSO inflections = getInflections(points, xval, yval);
    size = inflections.size();
    for (int i = 0; i < size; i++) {
        final double t = inflections.get(i);
        final double x = getValue(t, xval);
        final double y = getValue(t, yval);
        minx = Math.min(x, minx);
        maxx = Math.max(x, maxx);
        miny = Math.min(y, miny);
        maxy = Math.max(y, maxy);
    }
    return new BoundingBox(computedLocationOffsetX + minx, computedLocationOffsetY + miny, computedLocationOffsetX + maxx, computedLocationOffsetY + maxy);
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO) Point2D(com.ait.lienzo.client.core.types.Point2D) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox)

Example 8 with NFastDoubleArrayJSO

use of com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO in project lienzo-core by ahome-it.

the class Geometry method getInflections.

private static final NFastDoubleArrayJSO getInflections(final Point2DArray points, final NFastDoubleArrayJSO xval, final NFastDoubleArrayJSO yval) {
    int size = points.size();
    final int ordr = size - 1;
    NFastDoubleArrayJSO root;
    final NFastDoubleArrayJSO tval = NFastDoubleArrayJSO.make();
    tval.push(0.0);
    tval.push(1.0);
    root = findAllRoots(1, xval);
    size = root.size();
    for (int i = 0; i < size; i++) {
        final double t = root.get(i);
        if ((0 < t) && (t < 1)) {
            tval.push(t);
        }
    }
    root = findAllRoots(1, yval);
    size = root.size();
    for (int i = 0; i < size; i++) {
        final double t = root.get(i);
        if ((0 < t) && (t < 1)) {
            tval.push(t);
        }
    }
    if (ordr > 2) {
        root = findAllRoots(2, xval);
        size = root.size();
        for (int i = 0; i < size; i++) {
            final double t = root.get(i);
            if ((0 < t) && (t < 1)) {
                tval.push(t);
            }
        }
        root = findAllRoots(2, yval);
        size = root.size();
        for (int i = 0; i < size; i++) {
            final double t = root.get(i);
            if ((0 < t) && (t < 1)) {
                tval.push(t);
            }
        }
    }
    return tval.uniq();
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)

Example 9 with NFastDoubleArrayJSO

use of com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO in project lienzo-core by ahome-it.

the class Geometry method findAllRoots.

private static final NFastDoubleArrayJSO findAllRoots(final int derivative, final NFastDoubleArrayJSO values) {
    final NFastDoubleArrayJSO root = NFastDoubleArrayJSO.make();
    if (areLinear(values) || ((derivative == 1) && (values.size() == 3))) {
        if (derivative > 1) {
            return root;
        }
        final double beg = getDerivative(1, 0, values);
        final double end = getDerivative(1, 1, values);
        if ((beg > 0) && (end > 0)) {
            return root;
        }
        if ((beg < 0) && (end < 0)) {
            return root;
        }
        root.push(map(0, beg, end, 0, 1));
        return root;
    }
    for (double t = 0; t <= 1.0; t += 0.01) {
        final double r = Math.round(findRoots(derivative, t, values) / NRRF_PRECISION) * NRRF_PRECISION;
        if (root.contains(r)) {
            continue;
        }
        root.push(r);
    }
    return root;
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)

Example 10 with NFastDoubleArrayJSO

use of com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO in project lienzo-core by ahome-it.

the class PathPartEntryJSO method copy.

public final PathPartEntryJSO copy() {
    final int command = getCommand();
    final NFastDoubleArrayJSO points = getPoints().copy();
    return make(command, points);
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)

Aggregations

NFastDoubleArrayJSO (com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)16 Point2D (com.ait.lienzo.client.core.types.Point2D)6 PathPartEntryJSO (com.ait.lienzo.client.core.types.PathPartEntryJSO)4 BoundingBox (com.ait.lienzo.client.core.types.BoundingBox)3 PathPartList (com.ait.lienzo.client.core.types.PathPartList)3 Point2DArray (com.ait.lienzo.client.core.types.Point2DArray)2 Direction (com.ait.lienzo.shared.core.types.Direction)2 Context2D (com.ait.lienzo.client.core.Context2D)1 Path2D (com.ait.lienzo.client.core.Path2D)1 ImageData (com.ait.lienzo.client.core.types.ImageData)1 ScratchPad (com.ait.lienzo.client.core.util.ScratchPad)1 NFastStringMap (com.ait.tooling.nativetools.client.collection.NFastStringMap)1