using System;
using Azimuth;
namespace Prism.Glass
{
///
/// The colors a function plot is drawn in.
///
public struct PlotColors
{
///
/// The color behind the plot.
///
public Color Background;
///
/// The color of the lines that mark where the function is 0 and where it is 1.
///
public Color Guide;
///
/// The color of the function's own line.
///
public Color Line;
}
///
/// Draws a function from 0 to 1 along the width of a pixel buffer, with the values it takes running up the height.
///
public static class FunctionPlotter
{
private static void CheckWidth(int width)
{
if (width < 2)
{
throw new ArgumentOutOfRangeException(nameof(width), width, "A plot needs at least two columns.");
}
}
// One value for each column, from 0 at the first to 1 at the last.
private static DenseVector Sample(IScalarFunction function, int width)
{
var returnMe = new DenseVector(width);
for (int column = 0; column < width; column++)
{
returnMe[column] = column / (double)(width - 1);
}
function.Evaluate(returnMe);
return returnMe;
}
///
/// Returns the range a plot of this function spans up its height: 0 to 1 at least, grown to hold whatever
/// finite values the function takes at the plot's columns.
///
/// The function is null.
/// The width is under 2.
public static Interval RangeOf(IScalarFunction function, int width)
{
if (function == null)
{
throw new ArgumentNullException(nameof(function));
}
CheckWidth(width);
DenseVector values = Sample(function, width);
double low = 0;
double high = 1;
for (int column = 0; column < width; column++)
{
double value = values[column].Value;
if (!double.IsNaN(value) && !double.IsInfinity(value))
{
low = Math.Min(low, value);
high = Math.Max(high, value);
}
}
return new Interval(low, high);
}
///
/// Returns the row a value is drawn in, counting from the top, which is the range's highest value. A value
/// past the range is held to the top or bottom row.
///
/// The range is empty or not finite.
/// The height is under 1.
public static int RowOf(Scalar value, Interval range, int height)
{
CheckRange(range);
if (height < 1)
{
throw new ArgumentOutOfRangeException(nameof(height), height, "A plot needs at least one row.");
}
double high = range.Max.Value;
double row = Math.Floor((high - value.Value) / (high - range.Min.Value) * (height - 1) + 0.5);
if (row < 0)
{
return 0;
}
return row > height - 1 ? height - 1 : (int)row;
}
private static void CheckRange(Interval range)
{
bool finite = !Scalar.IsInfinity(range.Min) && !Scalar.IsInfinity(range.Max);
if (!finite || !(range.Max > range.Min))
{
throw new ArgumentException("The range must be finite and have some height.", nameof(range));
}
}
private static void FillRow(byte[] pixels, int width, int row, Color color)
{
for (int column = 0; column < width; column++)
{
Rgba.Write(pixels, row * width + column, color);
}
}
///
/// Draws the function into the pixels, with running up the height. A value that isn't
/// finite leaves a gap, and a steep run is filled so the line has none.
///
/// The function or the buffer is null.
/// The width is under 2, or the height is under 1.
/// The buffer isn't the plot's size, or the range is empty.
public static void Draw(
byte[] pixels, int width, int height, IScalarFunction function, Interval range, PlotColors colors)
{
if (function == null)
{
throw new ArgumentNullException(nameof(function));
}
CheckWidth(width);
Rgba.CheckBuffer(pixels, width, height);
CheckRange(range);
DenseVector values = Sample(function, width);
for (int i = 0; i < width * height; i++)
{
Rgba.Write(pixels, i, colors.Background);
}
FillRow(pixels, width, RowOf(0, range, height), colors.Guide);
FillRow(pixels, width, RowOf(1, range, height), colors.Guide);
int previousRow = -1;
for (int column = 0; column < width; column++)
{
double value = values[column].Value;
if (double.IsNaN(value) || double.IsInfinity(value))
{
previousRow = -1;
continue;
}
int row = RowOf(value, range, height);
// A steep run fills the rows between this column and the last, so the line has no gaps.
int first = row;
if (previousRow >= 0 && previousRow != row)
{
first = previousRow + Math.Sign(row - previousRow);
}
for (int fill = Math.Min(first, row); fill <= Math.Max(first, row); fill++)
{
Rgba.Write(pixels, fill * width + column, colors.Line);
}
previousRow = row;
}
}
}
}