using System.Diagnostics;
namespace LibNoise.Operator
{
///
/// Provides a noise module that clamps the output value from a source module to a
/// range of values. [OPERATOR]
///
public class Clamp : ModuleBase
{
#region Fields
private double _min = -1.0;
private double _max = 1.0;
#endregion
#region Constructors
///
/// Initializes a new instance of Clamp.
///
public Clamp()
: base(1)
{
}
///
/// Initializes a new instance of Clamp.
///
/// The input module.
public Clamp(ModuleBase input)
: base(1)
{
Modules[0] = input;
}
///
/// Initializes a new instance of Clamp.
///
/// The input module.
/// The minimum value.
/// The maximum value.
public Clamp(double min, double max, ModuleBase input)
: base(1)
{
Minimum = min;
Maximum = max;
Modules[0] = input;
}
#endregion
#region Properties
///
/// Gets or sets the maximum to clamp to.
///
public double Maximum
{
get { return _max; }
set { _max = value; }
}
///
/// Gets or sets the minimum to clamp to.
///
public double Minimum
{
get { return _min; }
set { _min = value; }
}
#endregion
#region Methods
///
/// Sets the bounds.
///
/// The minimum value.
/// The maximum value.
public void SetBounds(double min, double max)
{
Debug.Assert(min < max);
_min = min;
_max = max;
}
#endregion
#region ModuleBase Members
///
/// 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 (_min > _max)
{
var t = _min;
_min = _max;
_max = t;
}
var v = Modules[0].GetValue(x, y, z);
if (v < _min)
{
return _min;
}
if (v > _max)
{
return _max;
}
return v;
}
#endregion
}
}