Class ValueLayerDiffuser

  • public class ValueLayerDiffuser
    extends Object
    This implements a diffusion algorithm on a grid value layer. By setting the diffusion constant and the evaporation constant you can control the way the diffusion occurs. Also, this allows you to specify whether the diffusion should act as though the space is toroidal (meaning the edges are connected to each other), or not. If the space is not toroidal then the edge of the space is considered a "value sink", meaning any values that hit it are evacuated out of the space (ie, if this space is diffusing heat, this acts as though there is a perfect heat sink around the space).

    A diffusion constant of 1.0 means that all the values at a specific spot are diffused away, so if the space looks like [0, 10, 0] diffusing (assuming no evaporation) gives [5, 0, 5].

    An evaporation is used as a multiplier against the values in the space, this means that a rate of 1.0 means no evaporation, and a rate of 0.0 means instantaneous evaporation. So, going off the previous example, with a diffusion constant of 1.0 and an evaporation rate of 0.0, [0, 10, 0] diffuses to be [2.5, 0, 2.5].

    Jerry Vos
    • Field Detail

      • maxValue

        protected double maxValue
      • minValue

        protected double minValue
      • evaporationConst

        protected double evaporationConst
      • diffusionConst

        protected double diffusionConst
      • toroidal

        protected boolean toroidal
      • computedVals

        protected transient Object computedVals
    • Constructor Detail

      • ValueLayerDiffuser

        public ValueLayerDiffuser()
        Constructs this with the default evaporation and diffusion constants. Before this diffuser can be used, a value layer must be set through the #setValueLayer(GridValueLayer) method.

        This is the same as new ValueLayer(null, DEFAULT_EVAP_CONST, DEFAULT_DIFF_CONST)

        See Also:
      • ValueLayerDiffuser

        public ValueLayerDiffuser​(IGridValueLayer valueLayer,
                                  double evaporationConst,
                                  double diffusionConst)
        Constructs this with the specified evaporation and diffusion constants. This also has the diffusion acting in a toroidal manner, so values from the edges will be diffused to the other side of the space.

        This is the same as new ValueLayer(valueLayer, evaporationConst, diffusionConstant, true)

        valueLayer - the layer this will be diffusing values on
        evaporationConst - the constant used for evaporating values off the layer
        diffusionConst - the constant used for diffusing values on the layer
      • ValueLayerDiffuser

        public ValueLayerDiffuser​(IGridValueLayer valueLayer,
                                  double evaporationConst,
                                  double diffusionConst,
                                  boolean toroidal)
        Constructs this with the specified evaporation constant, diffusion constant, and toroidal'ness.
        valueLayer - the layer this will be diffusing values on
        evaporationConst - the constant used for evaporating values off the layer
        diffusionConst - the constant used for diffusing values on the layer
        toroidal - if this should act as though the edges of the layer are connected
    • Method Detail

      • getValue

        protected double getValue​(double... coords)
        Retrieves a value from the layer, taking into account the toroidal'ness.
        coords - the coordinates
        the value at the specified coordinate
      • inBounds

        protected double inBounds​(double... coords)
        Returns 1 if the coordinates are in the layer's bounds, otherwise returns 0.0.
        coords - the coordinates to check
        1 or 0.0
      • constrainByMinMax

        protected double constrainByMinMax​(double val)
        Massages the value into the range specified by [minValue, maxValue].
        val - the value to bring into range (if necessary)
        the value
      • computeVals

        protected void computeVals()
        Computes all the values for the space.
      • diffuse

        public void diffuse()
        Runs the diffusion with the current rates and values. Following the Swarm class, it is roughly newValue = evap(ownValue + diffusionConstant * (nghAvg - ownValue)) where nghAvg is the weighted average of a cells neighbors, and ownValue is the current value for the current cell.

        Values from the value layer are used to calculate diffusion. This value is then written to a buffer. When this has been done for every cell in the grid, the buffer is copied to the value layer.

      • getDiffusionConst

        public double getDiffusionConst()
      • setDiffusionConst

        public void setDiffusionConst​(double diffusionConst)
      • getEvaporationConst

        public double getEvaporationConst()
      • setEvaporationConst

        public void setEvaporationConst​(double evapRate)
      • getMaxValue

        public double getMaxValue()
      • setMaxValue

        public void setMaxValue​(double max)
      • getMinValue

        public double getMinValue()
      • setMinValue

        public void setMinValue​(double min)
      • isToroidal

        public boolean isToroidal()
      • setToroidal

        public void setToroidal​(boolean toroidal)
      • getValueLayer

        public ValueLayer getValueLayer()
      • setValueLayer

        public void setValueLayer​(IGridValueLayer valueLayer)