Search in sources :

Example 1 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class WFFuncPresets method parseParam.

protected double parseParam(String preset, String token) {
    String value = parseToken(preset, token);
    if (value == null) {
        return 0.0;
    }
    JEPWrapper parser = new JEPWrapper();
    Node node = parser.parse(value);
    return (Double) parser.evaluate(node);
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Example 2 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class ZPlot2DTransformer method transformMesh.

@Override
protected void transformMesh(Mesh3D pMesh3D, int pImageWidth, int pImageHeight) {
    int pCount = pMesh3D.getPCount();
    int width = pImageWidth;
    int height = pImageHeight;
    double[] x = pMesh3D.getX();
    double[] y = pMesh3D.getY();
    double[] z = pMesh3D.getZ();
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("x", 0.0);
    Node node = parser.parse(formula);
    // Don't calculate the actual bounding box because this may cause unexpected results if the object was deformed by another tansformer before
    double objXMin = -(double) width / 2.0;
    // double objXMax = (double) width / 2.0;
    double objYMin = -(double) height / 2.0;
    // double objYMax = (double) height / 2.0;
    double objXSize = (double) width;
    double objYSize = (double) height;
    double dx = this.xMax - this.xMin;
    if (this.axis == Axis.X) {
        if (!this.damp) {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
                parser.setVarValue("x", xx);
                double amp = (Double) parser.evaluate(node);
                z[i] -= amp;
            }
        } else {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
                parser.setVarValue("x", xx);
                double amp = (Double) parser.evaluate(node);
                double dxx = ((x[i] - objXMin) * dx) / objXSize + xMin - originX;
                double drr = Math.abs(dxx);
                double dmp = drr * damping;
                amp *= Math.exp(dmp);
                z[i] -= amp;
            }
        }
    } else if (this.axis == Axis.Y) {
        if (!this.damp) {
            for (int i = 0; i < pCount; i++) {
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin;
                parser.setVarValue("x", yy);
                double amp = (Double) parser.evaluate(node);
                z[i] -= amp;
            }
        } else {
            for (int i = 0; i < pCount; i++) {
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin;
                parser.setVarValue("x", yy);
                double amp = (Double) parser.evaluate(node);
                double dyy = ((y[i] - objYMin) * dx) / objYSize + xMin - originY;
                double drr = Math.abs(dyy);
                double dmp = drr * damping;
                amp *= Math.exp(dmp);
                z[i] -= amp;
            }
        }
    } else if (this.axis == Axis.XY) {
        if (!this.damp) {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin;
                double rr = Math.sqrt(xx * xx + yy * yy);
                parser.setVarValue("x", rr);
                double amp = (Double) parser.evaluate(node);
                z[i] -= amp;
            }
        } else {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin;
                double rr = Math.sqrt(xx * xx + yy * yy);
                parser.setVarValue("x", rr);
                double amp = (Double) parser.evaluate(node);
                double dxx = ((x[i] - objXMin) * dx) / objXSize + xMin - originX;
                double dyy = ((y[i] - objYMin) * dx) / objYSize + xMin - originY;
                double drr = Math.sqrt(dxx * dxx + dyy * dyy);
                double dmp = drr * damping;
                amp *= Math.exp(dmp);
                z[i] -= amp;
            }
        }
    } else {
        /* radial */
        if (!this.damp) {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin - originX;
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin - originY;
                double zz = z[i] - originZ;
                double rr = Math.sqrt(xx * xx + yy * yy + zz * zz);
                parser.setVarValue("x", rr);
                double amp = (Double) parser.evaluate(node);
                double vx, vy, vz;
                if (rr > 0.00001) {
                    vx = xx / rr;
                    vy = yy / rr;
                    vz = zz / rr;
                } else {
                    vx = vy = 0.0;
                    vz = 1.0;
                }
                x[i] += vx * amp;
                y[i] += vy * amp;
                z[i] += vz * amp;
            }
        } else {
            for (int i = 0; i < pCount; i++) {
                double xx = ((x[i] - objXMin) * dx) / objXSize + xMin - originX;
                double yy = ((y[i] - objYMin) * dx) / objYSize + xMin - originY;
                double zz = z[i] - originZ;
                double rr = Math.sqrt(xx * xx + yy * yy + zz * zz);
                parser.setVarValue("x", rr);
                double amp = (Double) parser.evaluate(node);
                double drr = rr;
                double dmp = drr * damping;
                amp *= Math.exp(dmp);
                double vx, vy, vz;
                if (rr > 0.00001) {
                    vx = xx / rr;
                    vy = yy / rr;
                    vz = zz / rr;
                } else {
                    vx = vy = 0.0;
                    vz = 1.0;
                }
                x[i] += vx * amp;
                y[i] += vy * amp;
                z[i] += vz * amp;
            }
        }
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Example 3 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class ZPlot3DTransformer method transformMesh.

@Override
protected void transformMesh(Mesh3D pMesh3D, int pImageWidth, int pImageHeight) {
    initPreset();
    int pCount = pMesh3D.getPCount();
    int width = pImageWidth;
    int height = pImageHeight;
    double[] x = pMesh3D.getX();
    double[] y = pMesh3D.getY();
    double[] z = pMesh3D.getZ();
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("x", 0.0);
    parser.addVariable("y", 0.0);
    Node node = parser.parse(formula);
    // Don't calculate the actual bounding box because this may cause unexpected results if the object was deformed by another tansformer before
    double objXMin = -(double) width / 2.0;
    // double objXMax = (double) width / 2.0;
    double objYMin = -(double) height / 2.0;
    // double objYMax = (double) height / 2.0;
    double objXSize = (double) width;
    double objYSize = (double) height;
    double dx = this.xMax - this.xMin;
    double dy = this.yMax - this.yMin;
    if (!this.damp) {
        for (int i = 0; i < pCount; i++) {
            double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
            double yy = ((y[i] - objYMin) * dy) / objYSize + xMin;
            parser.setVarValue("x", xx);
            parser.setVarValue("y", yy);
            double amp = (Double) parser.evaluate(node);
            z[i] -= amp;
        }
    } else {
        for (int i = 0; i < pCount; i++) {
            double xx = ((x[i] - objXMin) * dx) / objXSize + xMin;
            double yy = ((y[i] - objYMin) * dy) / objYSize + xMin;
            parser.setVarValue("x", xx);
            parser.setVarValue("y", yy);
            double amp = (Double) parser.evaluate(node);
            double dxx = ((x[i] - objXMin) * dx) / objXSize + xMin - originX;
            double dyy = ((y[i] - objYMin) * dy) / objYSize + xMin - originY;
            double drr = Math.sqrt(dxx * dxx + dyy * dyy);
            double dmp = drr * damping;
            amp *= Math.exp(dmp);
            z[i] -= amp;
        }
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Example 4 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class FormulaColorTransformer method performPixelTransformation.

@Override
protected void performPixelTransformation(WFImage pImg) {
    SimpleImage img = (SimpleImage) pImg;
    int width = pImg.getImageWidth();
    int height = pImg.getImageHeight();
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("r", 0.0);
    parser.addVariable("g", 0.0);
    parser.addVariable("b", 0.0);
    parser.addVariable("x", 0.0);
    parser.addVariable("y", 0.0);
    parser.addVariable("width", (double) width);
    parser.addVariable("height", (double) height);
    Node redNode = parser.parse(formula1Red);
    Node greenNode = parser.parse(formula2Green);
    Node blueNode = parser.parse(formula3Blue);
    Pixel pixel = new Pixel();
    for (int i = 0; i < height; i++) {
        parser.setVarValue("y", i);
        for (int j = 0; j < width; j++) {
            parser.setVarValue("x", j);
            pixel.setARGBValue(srcImg.getARGBValue(j, i));
            if (useOriginalRGBValues) {
                parser.setVarValue("r", (double) pixel.r);
                parser.setVarValue("g", (double) pixel.g);
                parser.setVarValue("b", (double) pixel.b);
                pixel.r = Tools.roundColor((Double) parser.evaluate(redNode));
                pixel.g = Tools.roundColor((Double) parser.evaluate(greenNode));
                pixel.b = Tools.roundColor((Double) parser.evaluate(blueNode));
            } else {
                parser.setVarValue("r", (double) pixel.r / 255.0);
                parser.setVarValue("g", (double) pixel.g / 255.0);
                parser.setVarValue("b", (double) pixel.b / 255.0);
                pixel.r = Tools.roundColor((Double) parser.evaluate(redNode) * 255.0);
                pixel.g = Tools.roundColor((Double) parser.evaluate(greenNode) * 255.0);
                pixel.b = Tools.roundColor((Double) parser.evaluate(blueNode) * 255.0);
            }
            img.setRGB(j, i, pixel);
        }
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) SimpleImage(org.jwildfire.image.SimpleImage) Node(org.nfunk.jep.Node) Pixel(org.jwildfire.image.Pixel)

Example 5 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class ParPlot3DTransformer method transformMesh.

@Override
protected void transformMesh(Mesh3D pMesh3D, int pImageWidth, int pImageHeight) {
    initPreset();
    int pCount = pMesh3D.getPCount();
    int width = pImageWidth;
    int height = pImageHeight;
    double[] x = pMesh3D.getX();
    double[] y = pMesh3D.getY();
    double[] z = pMesh3D.getZ();
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("u", 0.0);
    parser.addVariable("v", 0.0);
    Node xNode = parser.parse(xFormula);
    Node yNode = parser.parse(yFormula);
    Node zNode = parser.parse(zFormula);
    // Don't calculate the actual bounding box because this may cause unexpected results if the object was deformed by another tansformer before
    double objUMin = -(double) width / 2.0;
    // double objXMax = (double) width / 2.0;
    double objVMin = -(double) height / 2.0;
    // double objYMax = (double) height / 2.0;
    double objUSize = (double) width;
    double objVSize = (double) height;
    double du = this.uMax - this.uMin;
    double dv = this.vMax - this.vMin;
    double xMin = 0.0, yMin = 0.0, zMin = 0.0;
    double xMax = 0.0, yMax = 0.0, zMax = 0.0;
    double oriZMin = 0.0, oriZMax = 0.0;
    for (int i = 0; i < pCount; i++) {
        if (z[i] < oriZMin)
            oriZMin = z[i];
        else if (z[i] > oriZMax)
            oriZMax = z[i];
    }
    double oriZSize = oriZMax - oriZMin;
    double oriZScale = oriZSize / Math.sqrt(width * width + height * height);
    for (int i = 0; i < pCount; i++) {
        double zz = oriZSize > MathLib.EPSILON ? z[i] / oriZSize * oriZScale : 0.0;
        double uu = ((x[i] - objUMin) * du) / objUSize + uMin;
        double vv = ((y[i] - objVMin) * dv) / objVSize + vMin;
        parser.setVarValue("u", uu);
        parser.setVarValue("v", vv);
        x[i] = (Double) parser.evaluate(xNode) * (1 + zz * this.zScale);
        if (x[i] < xMin)
            xMin = x[i];
        else if (x[i] > xMax)
            xMax = x[i];
        y[i] = (Double) parser.evaluate(yNode) * (1 + zz * this.zScale);
        if (y[i] < yMin)
            yMin = y[i];
        else if (y[i] > yMax)
            yMax = y[i];
        z[i] = (Double) parser.evaluate(zNode) * (1 + zz * this.zScale);
        if (z[i] < zMin)
            zMin = z[i];
        else if (z[i] > zMax)
            zMax = z[i];
    }
    double xSize = xMax - xMin;
    if (xSize < MathLib.EPSILON)
        xSize = MathLib.EPSILON;
    double ySize = yMax - yMin;
    if (ySize < MathLib.EPSILON)
        ySize = MathLib.EPSILON;
    double zSize = zMax - zMin;
    if (zSize < MathLib.EPSILON)
        zSize = MathLib.EPSILON;
    double xScl = (double) width / xSize;
    double yScl = (double) height / ySize;
    double zScl = Math.sqrt(width * width + height * height) / zSize;
    for (int i = 0; i < pCount; i++) {
        x[i] *= xScl;
        y[i] *= yScl;
        z[i] *= zScl;
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Aggregations

JEPWrapper (org.jwildfire.base.mathparser.JEPWrapper)8 Node (org.nfunk.jep.Node)8 Pixel (org.jwildfire.image.Pixel)2 SimpleImage (org.jwildfire.image.SimpleImage)2