Class TurbulencePatternRed

  • All Implemented Interfaces:
    java.awt.image.RenderedImage, CachableRed

    public final class TurbulencePatternRed
    extends AbstractRed
    This class creates a RenderedImage in conformance to the one defined for the feTurbulence filter of the SVG specification. What follows is my high-level description of how the noise is generated. This is not contained in the SVG spec, just the algorithm for doing it. This is provided in the hope that someone will figure out a clever way to accelerate parts of the function. gradient contains a long list of random unit vectors. For each point we are to generate noise for we do two things. first we use the latticeSelector to 'co-mingle' the integer portions of x and y (this allows us to have a one-dimensional array of gradients that appears 2 dimensional, by using the co-mingled index). We do this for [x,y], [x+1,y], [x,y+1], and [x+1, y+1], this gives us the four gradient vectors that surround the point (b00, b10, ...) Next we construct the four vectors from the grid points (where the gradient vectors are defined) [these are rx0, rx1, ry0, ry1]. We then take the dot product between the gradient vectors and the grid point vectors (this gives the portion of the grid point vector that projects along the gradient vector for each grid point). These four dot projects are then combined with linear interpolation. The weight factor for the linear combination is the result of applying the 's' curve function to the fractional part of x and y (rx0, ry0). The S curve function get's it's name because it looks a bit like as 'S' from 0->1.
    Version:
    $Id: TurbulencePatternRed.java 1808001 2017-09-11 09:51:29Z ssteiner $
    • Field Detail

      • IDENTITY

        private static final java.awt.geom.AffineTransform IDENTITY
        Identity transform, default used when null input in the constructor.
      • baseFrequencyX

        private double baseFrequencyX
        x-axis base frequency for the noise function along the x-axis
      • baseFrequencyY

        private double baseFrequencyY
        y-axis base frequency for the noise function along the y-axis
      • numOctaves

        private int numOctaves
        Number of octaves in the noise function
      • seed

        private int seed
        Starting number for the pseudo random number generator
      • tile

        private java.awt.geom.Rectangle2D tile
        Defines the tile for the turbulence function, if non-null turns on stitching, so frequencies are adjusted to avoid discontinuities in case frequencies do not match tile boundaries.
      • txf

        private java.awt.geom.AffineTransform txf
        Defines the tile for the turbulence function
      • isFractalNoise

        private boolean isFractalNoise
        Defines whether the filter performs a fractal noise or a turbulence function
      • channels

        private int[] channels
        List of channels that the generator produces.
      • tx

        double[] tx
      • ty

        double[] ty
      • RAND_m

        private static final int RAND_m
        Produces results in the range [1, 2**31 - 2]. Algorithm is: r = (a * r) mod m where a = 16807 and m = 2**31 - 1 = 2147483647 See [Park & Miller], CACM vol. 31 no. 10 p. 1195, Oct. 1988 To test: the algorithm should produce the result 1043618065 as the 10,000th generated number if the original seed is 1.
        See Also:
        Constant Field Values
      • latticeSelector

        private final int[] latticeSelector
      • gradient

        private final double[] gradient
    • Constructor Detail

      • TurbulencePatternRed

        public TurbulencePatternRed​(double baseFrequencyX,
                                    double baseFrequencyY,
                                    int numOctaves,
                                    int seed,
                                    boolean isFractalNoise,
                                    java.awt.geom.Rectangle2D tile,
                                    java.awt.geom.AffineTransform txf,
                                    java.awt.Rectangle devRect,
                                    java.awt.color.ColorSpace cs,
                                    boolean alpha)
        Parameters:
        baseFrequencyX - x-axis base frequency for the noise function along the x-axis
        baseFrequencyY - y-axis base frequency for the noise function along the x-axis
        numOctaves - number of octaves in the noise function. Positive integral value.
        seed - starting number for the pseudo random number generator
        isFractalNoise - defines whether the filter performs a fractal noise or a turbulence function.
        tile - defines the tile size. May be null if stitchTiles is false. Otherwise, should not be null.
        txf - The affine transform from device to user space.
        cs - The Colorspace to output.
        alpha - True if the data should have an alpha channel.
    • Method Detail

      • getBaseFrequencyX

        public double getBaseFrequencyX()
      • getBaseFrequencyY

        public double getBaseFrequencyY()
      • getNumOctaves

        public int getNumOctaves()
      • getSeed

        public int getSeed()
      • getTile

        public java.awt.geom.Rectangle2D getTile()
      • isFractalNoise

        public boolean isFractalNoise()
      • getChannels

        public boolean[] getChannels()
      • setupSeed

        public final int setupSeed​(int seed)
      • random

        public final int random​(int seed)
      • initLattice

        private void initLattice​(int seed)
      • s_curve

        private static final double s_curve​(double t)
      • lerp

        private static final double lerp​(double t,
                                         double a,
                                         double b)
      • noise2

        private final void noise2​(double[] noise,
                                  double vec0,
                                  double vec1)
        Generate a pixel of noise corresponding to the point vec0,vec1. See class description for a high level discussion of method. This handles cases where channels <= 4.
        Parameters:
        noise - The place to put the generated noise.
        vec0 - The X coordiate to generate noise for
        vec1 - The Y coordiate to generate noise for
      • noise2Stitch

        private final void noise2Stitch​(double[] noise,
                                        double vec0,
                                        double vec1,
                                        TurbulencePatternRed.StitchInfo stitchInfo)
        This version of the noise function implements stitching. If any of the lattice is on the right or bottom edge, the function uses the the latice on the other side of the tile, i.e., the left or right edge.
        Parameters:
        noise - The place to put the generated noise.
        vec0 - The X coordiate to generate noise for
        vec1 - The Y coordiate to generate noise for
        stitchInfo - The stitching information for the noise function.
      • turbulence_4

        private final int turbulence_4​(double pointX,
                                       double pointY,
                                       double[] fSum)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec. This is special case for 4 bands of output.
        Parameters:
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        Returns:
        The ARGB pixel value.
      • turbulence

        private final void turbulence​(int[] rgb,
                                      double pointX,
                                      double pointY,
                                      double[] fSum,
                                      double[] noise)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.
        Parameters:
        rgb - array for the four color components
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        noise - array used to avoid reallocating double array for each pixel
      • turbulenceStitch

        private final void turbulenceStitch​(int[] rgb,
                                            double pointX,
                                            double pointY,
                                            double[] fSum,
                                            double[] noise,
                                            TurbulencePatternRed.StitchInfo stitchInfo)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.
        Parameters:
        rgb - array for the four color components
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        noise - array used to avoid reallocating double array for each pixel
        stitchInfo - The stitching information for the noise function
      • turbulenceFractal_4

        private final int turbulenceFractal_4​(double pointX,
                                              double pointY,
                                              double[] fSum)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec. This handles the case where we are generating 4 channels of noise.
        Parameters:
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        Returns:
        The ARGB pixel
      • turbulenceFractal

        private final void turbulenceFractal​(int[] rgb,
                                             double pointX,
                                             double pointY,
                                             double[] fSum,
                                             double[] noise)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.
        Parameters:
        rgb - array for the four color components
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        noise - array used to avoid reallocating double array for each pixel
      • turbulenceFractalStitch

        private final void turbulenceFractalStitch​(int[] rgb,
                                                   double pointX,
                                                   double pointY,
                                                   double[] fSum,
                                                   double[] noise,
                                                   TurbulencePatternRed.StitchInfo stitchInfo)
        This is the heart of the turbulence calculation. It returns 'turbFunctionResult', as defined in the spec.
        Parameters:
        rgb - array for the four color components
        pointX - x coordinate of the point to process.
        pointY - y coordinate of the point to process.
        fSum - array used to avoid reallocating double array for each pixel
        noise - array used to avoid reallocating double array for each pixel
        stitchInfo - The stitching information for the noise function
      • copyData

        public java.awt.image.WritableRaster copyData​(java.awt.image.WritableRaster dest)
        Generates a Perlin noise pattern into dest Raster.
        Parameters:
        dest - Raster to fill with the pattern.