using System.Diagnostics;
namespace LibNoise.Operator
{
///
/// Provides a noise module that caches the last output value generated by a source
/// module. [OPERATOR]
///
public class Cache : ModuleBase
{
#region Fields
private double _value;
private bool _cached;
private double _x;
private double _y;
private double _z;
#endregion
#region Constructors
///
/// Initializes a new instance of Cache.
///
public Cache()
: base(1)
{
}
///
/// Initializes a new instance of Cache.
///
/// The input module.
public Cache(ModuleBase input)
: base(1)
{
Modules[0] = input;
}
#endregion
#region ModuleBase Members
///
/// Gets or sets a source module by index.
///
/// The index of the source module to aquire.
/// The requested source module.
public override ModuleBase this[int index]
{
get { return base[index]; }
set
{
base[index] = value;
_cached = false;
}
}
///
/// Returns the output value for the given input coordinates.
///
/// The input coordinate on the x-axis.
/// The input coordinate on the y-axis.
/// The input coordinate on the z-axis.
/// The resulting output value.
public override double GetValue(double x, double y, double z)
{
Debug.Assert(Modules[0] != null);
if (!(_cached && _x == x && _y == y && _z == z))
{
_value = Modules[0].GetValue(x, y, z);
_x = x;
_y = y;
_z = z;
}
_cached = true;
return _value;
}
#endregion
}
}