Initial Commit
This commit is contained in:
187
Transforms/CoordinateTransform.cs
Normal file
187
Transforms/CoordinateTransform.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using Microsoft.Research.DynamicDataDisplay.Common;
|
||||
using System.Diagnostics;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
/// <summary>
|
||||
/// A central class in 2d coordinate transformation in DynamicDataDisplay.
|
||||
/// Provides methods to transform point from one coordinate system to another.
|
||||
/// Should be immutable.
|
||||
/// </summary>
|
||||
public sealed class CoordinateTransform
|
||||
{
|
||||
private CoordinateTransform(DataRect visibleRect, Rect screenRect)
|
||||
{
|
||||
this.visibleRect = visibleRect;
|
||||
this.screenRect = screenRect;
|
||||
|
||||
rxToScreen = screenRect.Width / visibleRect.Width;
|
||||
ryToScreen = screenRect.Height / visibleRect.Height;
|
||||
cxToScreen = visibleRect.XMin * rxToScreen - screenRect.Left;
|
||||
cyToScreen = screenRect.Height + screenRect.Top + visibleRect.YMin * ryToScreen;
|
||||
|
||||
rxToData = visibleRect.Width / screenRect.Width;
|
||||
ryToData = visibleRect.Height / screenRect.Height;
|
||||
cxToData = screenRect.Left * rxToData - visibleRect.XMin;
|
||||
cyToData = visibleRect.Height + visibleRect.YMin + screenRect.Top * ryToData;
|
||||
}
|
||||
|
||||
#region Coeffs
|
||||
double rxToScreen;
|
||||
double ryToScreen;
|
||||
double cxToScreen;
|
||||
double cyToScreen;
|
||||
|
||||
double rxToData;
|
||||
double ryToData;
|
||||
double cxToData;
|
||||
double cyToData;
|
||||
#endregion
|
||||
|
||||
#region Creation methods
|
||||
|
||||
internal static CoordinateTransform FromRects(DataRect visibleRect, Rect screenRect)
|
||||
{
|
||||
CoordinateTransform result = new CoordinateTransform(visibleRect, screenRect);
|
||||
return result;
|
||||
}
|
||||
|
||||
internal CoordinateTransform WithRects(DataRect visibleRect, Rect screenRect)
|
||||
{
|
||||
CoordinateTransform copy = new CoordinateTransform(visibleRect, screenRect);
|
||||
copy.dataTransform = dataTransform;
|
||||
return copy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of CoordinateTransform with the given data transform.
|
||||
/// </summary>
|
||||
/// <param name="dataTransform">The data transform.</param>
|
||||
/// <returns></returns>
|
||||
public CoordinateTransform WithDataTransform(DataTransform dataTransform)
|
||||
{
|
||||
if (dataTransform == null)
|
||||
throw new ArgumentNullException("dataTransform");
|
||||
|
||||
CoordinateTransform copy = new CoordinateTransform(visibleRect, screenRect);
|
||||
copy.dataTransform = dataTransform;
|
||||
return copy;
|
||||
}
|
||||
|
||||
internal CoordinateTransform WithScreenOffset(double x, double y)
|
||||
{
|
||||
Rect screenCopy = screenRect;
|
||||
screenCopy.Offset(x, y);
|
||||
CoordinateTransform copy = new CoordinateTransform(visibleRect, screenCopy);
|
||||
return copy;
|
||||
}
|
||||
|
||||
internal static CoordinateTransform CreateDefault()
|
||||
{
|
||||
CoordinateTransform transform = new CoordinateTransform(new Rect(0, 0, 1, 1), new Rect(0, 0, 1, 1));
|
||||
|
||||
return transform;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Transform methods
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point from data coordinates to screen.
|
||||
/// </summary>
|
||||
/// <param name="dataPoint">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public Point DataToScreen(Point dataPoint)
|
||||
{
|
||||
Point viewportPoint = dataTransform.DataToViewport(dataPoint);
|
||||
|
||||
Point screenPoint = new Point(viewportPoint.X * rxToScreen - cxToScreen,
|
||||
cyToScreen - viewportPoint.Y * ryToScreen);
|
||||
|
||||
return screenPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point from screen coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenPoint">The point in screen coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public Point ScreenToData(Point screenPoint)
|
||||
{
|
||||
Point viewportPoint = new Point(screenPoint.X * rxToData - cxToData,
|
||||
cyToData - screenPoint.Y * ryToData);
|
||||
|
||||
Point dataPoint = dataTransform.ViewportToData(viewportPoint);
|
||||
|
||||
return dataPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point from viewport coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportPoint">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public Point ViewportToScreen(Point viewportPoint)
|
||||
{
|
||||
Point screenPoint = new Point(viewportPoint.X * rxToScreen - cxToScreen,
|
||||
cyToScreen - viewportPoint.Y * ryToScreen);
|
||||
|
||||
return screenPoint;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point from screen coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenPoint">The point in screen coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public Point ScreenToViewport(Point screenPoint)
|
||||
{
|
||||
Point viewportPoint = new Point(screenPoint.X * rxToData - cxToData,
|
||||
cyToData - screenPoint.Y * ryToData);
|
||||
|
||||
return viewportPoint;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||
private DataRect visibleRect;
|
||||
/// <summary>
|
||||
/// Gets the viewport rectangle.
|
||||
/// </summary>
|
||||
/// <value>The viewport rect.</value>
|
||||
public DataRect ViewportRect
|
||||
{
|
||||
get { return visibleRect; }
|
||||
}
|
||||
|
||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||
private Rect screenRect;
|
||||
/// <summary>
|
||||
/// Gets the screen rectangle.
|
||||
/// </summary>
|
||||
/// <value>The screen rect.</value>
|
||||
public Rect ScreenRect
|
||||
{
|
||||
get { return screenRect; }
|
||||
}
|
||||
|
||||
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
|
||||
private DataTransform dataTransform = DataTransforms.Identity;
|
||||
/// <summary>
|
||||
/// Gets the data transform.
|
||||
/// </summary>
|
||||
/// <value>The data transform.</value>
|
||||
public DataTransform DataTransform
|
||||
{
|
||||
get { return dataTransform; }
|
||||
}
|
||||
}
|
||||
}
|
||||
305
Transforms/CoordinateTransformExtensions.cs
Normal file
305
Transforms/CoordinateTransformExtensions.cs
Normal file
@@ -0,0 +1,305 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Microsoft.Research.DynamicDataDisplay.Common;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
public static class CoordinateTransformExtensions
|
||||
{
|
||||
#region Points
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in data coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataPoint">Point in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in screen coordinates</returns>
|
||||
public static Point DataToScreen(this Point dataPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.DataToScreen(dataPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in screen coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenPoint">Point in screen coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in data coordinates</returns>
|
||||
public static Point ScreenToData(this Point screenPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.ScreenToData(screenPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in screen coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenPoint">Point in screen coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in viewport coordinates</returns>
|
||||
public static Point ScreenToViewport(this Point screenPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.ScreenToViewport(screenPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in viewport coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportPoint">Point in viewport coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in screen coordinates</returns>
|
||||
public static Point ViewportToScreen(this Point viewportPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.ViewportToScreen(viewportPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportPoint">Point in viewport coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in data coordinates</returns>
|
||||
public static Point ViewportToData(this Point viewportPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.DataTransform.ViewportToData(viewportPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataPoint">Point in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in viewport coordinates</returns>
|
||||
public static Point DataToViewport(this Point dataPoint, CoordinateTransform transform)
|
||||
{
|
||||
return transform.DataTransform.DataToViewport(dataPoint);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataPoint">Point in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Point in viewport coordinates</returns>
|
||||
public static Point DataToViewport(this Point dataPoint, DataTransform transform)
|
||||
{
|
||||
return transform.DataToViewport(dataPoint);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Rects
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from screen coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenRect">Rectangle in screen coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in data coordinates</returns>
|
||||
public static Rect ScreenToData(this Rect screenRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = screenRect.BottomLeft.ScreenToData(transform);
|
||||
Point p2 = screenRect.TopRight.ScreenToData(transform);
|
||||
|
||||
return new Rect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from data coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataRect">Rectangle in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in screen coordinates</returns>
|
||||
public static Rect DataToScreen(this DataRect dataRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = dataRect.XMaxYMax.DataToScreen(transform);
|
||||
Point p2 = dataRect.XMinYMin.DataToScreen(transform);
|
||||
|
||||
return new Rect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from screen coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="screenRect">Rectangle in screen coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in viewport coordinates</returns>
|
||||
public static DataRect ScreenToViewport(this Rect screenRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = screenRect.BottomLeft.ScreenToViewport(transform);
|
||||
Point p2 = screenRect.TopRight.ScreenToViewport(transform);
|
||||
|
||||
return new DataRect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from viewport coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportRect">Rectangle in viewport coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in screen coordinates</returns>
|
||||
public static Rect ViewportToScreen(this DataRect viewportRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = viewportRect.XMaxYMax.ViewportToScreen(transform);
|
||||
Point p2 = viewportRect.XMinYMin.ViewportToScreen(transform);
|
||||
|
||||
return new Rect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportRect">Rectangle in viewport coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in data coordinates</returns>
|
||||
public static DataRect ViewportToData(this DataRect viewportRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = viewportRect.XMaxYMax.ViewportToData(transform);
|
||||
Point p2 = viewportRect.XMinYMin.ViewportToData(transform);
|
||||
|
||||
return new DataRect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataRect">Rectangle in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in viewport coordinates</returns>
|
||||
public static DataRect DataToViewport(this DataRect dataRect, CoordinateTransform transform)
|
||||
{
|
||||
Point p1 = dataRect.XMaxYMax.DataToViewport(transform);
|
||||
Point p2 = dataRect.XMinYMin.DataToViewport(transform);
|
||||
|
||||
return new DataRect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="viewportRect">Rectangle in viewport coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in data coordinates</returns>
|
||||
public static DataRect ViewportToData(this DataRect viewportRect, DataTransform transform)
|
||||
{
|
||||
Point p1 = transform.ViewportToData(viewportRect.XMaxYMax);
|
||||
Point p2 = transform.ViewportToData(viewportRect.XMinYMin);
|
||||
|
||||
return new DataRect(p1, p2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms rectangle from data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataRect">Rectangle in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Rectangle in viewport coordinates</returns>
|
||||
public static DataRect DataToViewport(this DataRect dataRect, DataTransform transform)
|
||||
{
|
||||
Point p1 = transform.DataToViewport(dataRect.XMinYMin);
|
||||
Point p2 = transform.DataToViewport(dataRect.XMaxYMax);
|
||||
|
||||
return new DataRect(p1, p2);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Collections
|
||||
|
||||
public static IEnumerable<Point> ViewportToScreen(this IEnumerable<Point> viewportPoints, CoordinateTransform transform)
|
||||
{
|
||||
foreach (var point in viewportPoints)
|
||||
{
|
||||
yield return point.ViewportToScreen(transform);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Point> DataToScreen(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
|
||||
{
|
||||
foreach (var point in dataPoints)
|
||||
{
|
||||
yield return point.DataToScreen(transform);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms list of points from data coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataPoints">Points in data coordinates</param>
|
||||
/// <param name="transform">CoordinateTransform used to perform transformation</param>
|
||||
/// <returns>Points in screen coordinates</returns>
|
||||
public static List<Point> DataToScreenAsList(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
|
||||
{
|
||||
ICollection<Point> iCollection = dataPoints as ICollection<Point>;
|
||||
List<Point> res;
|
||||
|
||||
if (iCollection != null)
|
||||
{
|
||||
res = new List<Point>(iCollection.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = new List<Point>();
|
||||
}
|
||||
|
||||
foreach (var point in dataPoints)
|
||||
{
|
||||
res.Add(transform.DataToScreen(point));
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms list of points from data coordinates to screen coordinates.
|
||||
/// </summary>
|
||||
/// <param name="transform">Coordinate transform used to perform transformation</param>
|
||||
/// <param name="dataPoints">Points in data coordinates</param>
|
||||
/// <returns>List of points in screen coordinates</returns>
|
||||
[SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
|
||||
public static List<Point> DataToScreenAsList(this CoordinateTransform transform, IEnumerable<Point> dataPoints)
|
||||
{
|
||||
return dataPoints.DataToScreenAsList(transform);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms list of points from data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="dataPoints">Points in data coordinates</param>
|
||||
/// <param name="transform">Data transform used to perform transformation</param>
|
||||
/// <returns>List of points in viewport coordinates</returns>
|
||||
public static IEnumerable<Point> DataToViewport(this IEnumerable<Point> dataPoints, DataTransform transform)
|
||||
{
|
||||
foreach (Point pt in dataPoints)
|
||||
{
|
||||
yield return pt.DataToViewport(transform);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Point> DataToViewport(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
|
||||
{
|
||||
return dataPoints.DataToViewport(transform.DataTransform);
|
||||
}
|
||||
|
||||
public static IEnumerable<Point> ScreenToViewport(this IEnumerable<Point> screenPoints, CoordinateTransform transform)
|
||||
{
|
||||
foreach (Point pt in screenPoints)
|
||||
{
|
||||
yield return pt.ScreenToViewport(transform);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Point> ScreenToData(this IEnumerable<Point> screenPoints, CoordinateTransform transform)
|
||||
{
|
||||
foreach (Point pt in screenPoints)
|
||||
{
|
||||
yield return pt.ScreenToData(transform);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
28
Transforms/DataDomains.cs
Normal file
28
Transforms/DataDomains.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
public static class DataDomains
|
||||
{
|
||||
private static readonly DataRect xPositive = DataRect.FromPoints(Double.Epsilon, Double.MinValue / 2, Double.MaxValue, Double.MaxValue / 2);
|
||||
public static DataRect XPositive
|
||||
{
|
||||
get { return xPositive; }
|
||||
}
|
||||
|
||||
private static readonly DataRect yPositive = DataRect.FromPoints(Double.MinValue / 2, Double.Epsilon, Double.MaxValue / 2, Double.MaxValue);
|
||||
public static DataRect YPositive
|
||||
{
|
||||
get { return yPositive; }
|
||||
}
|
||||
|
||||
private static readonly DataRect xyPositive = DataRect.FromPoints(Double.Epsilon, Double.Epsilon, Double.MaxValue, Double.MaxValue);
|
||||
public static DataRect XYPositive
|
||||
{
|
||||
get { return xyPositive; }
|
||||
}
|
||||
}
|
||||
}
|
||||
564
Transforms/DataTransforms.cs
Normal file
564
Transforms/DataTransforms.cs
Normal file
@@ -0,0 +1,564 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for all data transforms.
|
||||
/// Defines methods to transform point from data coordinate system to viewport coordinates and vice versa.
|
||||
/// Derived class should be immutable; to perform any changes a new new instance with different parameters should be created.
|
||||
/// </summary>
|
||||
public abstract class DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns>Transformed point in viewport coordinates.</returns>
|
||||
public abstract Point DataToViewport(Point pt);
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns>Transformed point in data coordinates.</returns>
|
||||
public abstract Point ViewportToData(Point pt);
|
||||
|
||||
private static readonly DataRect defaultDomain = DataRect.Empty;
|
||||
/// <summary>
|
||||
/// Gets the data domain of this dataTransform.
|
||||
/// </summary>
|
||||
/// <value>The data domain of this dataTransform.</value>
|
||||
public virtual DataRect DataDomain { get { return defaultDomain; } }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents identity data transform, which applies no transformation.
|
||||
/// is by default in CoordinateTransform.
|
||||
/// </summary>
|
||||
public sealed class IdentityTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="IdentityTransform"/> class.
|
||||
/// </summary>
|
||||
public IdentityTransform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
return pt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return pt;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a logarithmic transform of y-values of points.
|
||||
/// </summary>
|
||||
public sealed class Log10YTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Log10YTransform"/> class.
|
||||
/// </summary>
|
||||
public Log10YTransform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
double y = pt.Y;
|
||||
|
||||
if (y < 0)
|
||||
y = Double.MinValue;
|
||||
else
|
||||
y = Math.Log10(y);
|
||||
|
||||
return new Point(pt.X, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return new Point(pt.X, Math.Pow(10, pt.Y));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data domain of this dataTransform.
|
||||
/// </summary>
|
||||
/// <value>The data domain of this dataTransform.</value>
|
||||
public override DataRect DataDomain
|
||||
{
|
||||
get { return DataDomains.YPositive; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a logarithmic transform of x-values of points.
|
||||
/// </summary>
|
||||
public sealed class Log10XTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Log10XTransform"/> class.
|
||||
/// </summary>
|
||||
public Log10XTransform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
double x = pt.X;
|
||||
|
||||
if (x < 0)
|
||||
x = Double.MinValue;
|
||||
else
|
||||
x = Math.Log10(x);
|
||||
|
||||
return new Point(x, pt.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return new Point(Math.Pow(10, pt.X), pt.Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data domain.
|
||||
/// </summary>
|
||||
/// <value>The data domain.</value>
|
||||
public override DataRect DataDomain
|
||||
{
|
||||
get { return DataDomains.XPositive; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a mercator transform, used in maps.
|
||||
/// Transforms y coordinates.
|
||||
/// </summary>
|
||||
public sealed class MercatorTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MercatorTransform"/> class.
|
||||
/// </summary>
|
||||
public MercatorTransform()
|
||||
{
|
||||
CalcScale(maxLatitude);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MercatorTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="maxLatitude">The maximal latitude.</param>
|
||||
public MercatorTransform(double maxLatitude)
|
||||
{
|
||||
this.maxLatitude = maxLatitude;
|
||||
CalcScale(maxLatitude);
|
||||
}
|
||||
|
||||
private void CalcScale(double maxLatitude)
|
||||
{
|
||||
double maxLatDeg = maxLatitude;
|
||||
double maxLatRad = maxLatDeg * Math.PI / 180;
|
||||
scale = maxLatDeg / Math.Log(Math.Tan(maxLatRad / 2 + Math.PI / 4));
|
||||
}
|
||||
|
||||
private double scale;
|
||||
/// <summary>
|
||||
/// Gets the scale.
|
||||
/// </summary>
|
||||
/// <value>The scale.</value>
|
||||
public double Scale
|
||||
{
|
||||
get { return scale; }
|
||||
}
|
||||
|
||||
private double maxLatitude = 85;
|
||||
/// <summary>
|
||||
/// Gets the maximal latitude.
|
||||
/// </summary>
|
||||
/// <value>The max latitude.</value>
|
||||
public double MaxLatitude
|
||||
{
|
||||
get { return maxLatitude; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public sealed override Point DataToViewport(Point pt)
|
||||
{
|
||||
double y = pt.Y;
|
||||
if (-maxLatitude <= y && y <= maxLatitude)
|
||||
{
|
||||
y = scale * Math.Log(Math.Tan(Math.PI * (pt.Y + 90) / 360));
|
||||
}
|
||||
|
||||
return new Point(pt.X, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public sealed override Point ViewportToData(Point pt)
|
||||
{
|
||||
double y = pt.Y;
|
||||
if (-maxLatitude <= y && y <= maxLatitude)
|
||||
{
|
||||
double e = Math.Exp(y / scale);
|
||||
y = 360 * Math.Atan(e) / Math.PI - 90;
|
||||
}
|
||||
|
||||
return new Point(pt.X, y);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents transform from polar coordinate system to rectangular coordinate system.
|
||||
/// </summary>
|
||||
public sealed class PolarToRectTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PolarToRectTransform"/> class.
|
||||
/// </summary>
|
||||
public PolarToRectTransform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
double r = pt.X;
|
||||
double phi = pt.Y;
|
||||
|
||||
double x = r * Math.Cos(phi);
|
||||
double y = r * Math.Sin(phi);
|
||||
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
double x = pt.X;
|
||||
double y = pt.Y;
|
||||
double r = Math.Sqrt(x * x + y * y);
|
||||
double phi = Math.Atan2(y, x);
|
||||
|
||||
return new Point(r, phi);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a data transform which applies rotation around specified center at specified angle.
|
||||
/// </summary>
|
||||
public sealed class RotateDataTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RotateDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="angleInRadians">The angle in radians.</param>
|
||||
public RotateDataTransform(double angleInRadians)
|
||||
{
|
||||
this.center = new Point();
|
||||
this.angle = angleInRadians;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RotateDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="angleInRadians">The angle in radians.</param>
|
||||
/// <param name="center">The center of rotation.</param>
|
||||
public RotateDataTransform(double angleInRadians, Point center)
|
||||
{
|
||||
this.center = center;
|
||||
this.angle = angleInRadians;
|
||||
}
|
||||
|
||||
private readonly Point center;
|
||||
/// <summary>
|
||||
/// Gets the center of rotation.
|
||||
/// </summary>
|
||||
/// <value>The center.</value>
|
||||
public Point Center
|
||||
{
|
||||
get { return center; }
|
||||
}
|
||||
|
||||
private readonly double angle;
|
||||
/// <summary>
|
||||
/// Gets the rotation angle.
|
||||
/// </summary>
|
||||
/// <value>The angle.</value>
|
||||
public double Angle
|
||||
{
|
||||
get { return angle; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
return Transform(pt, angle);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return Transform(pt, -angle);
|
||||
}
|
||||
|
||||
private Point Transform(Point pt, double angle)
|
||||
{
|
||||
Vector vec = pt - center;
|
||||
double currAngle = Math.Atan2(vec.Y, vec.X);
|
||||
currAngle += angle;
|
||||
|
||||
Vector rotatedVec = new Vector(Math.Cos(currAngle), Math.Sin(currAngle)) * vec.Length;
|
||||
|
||||
return center + rotatedVec;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents data transform performed by multiplication on given matrix.
|
||||
/// </summary>
|
||||
public sealed class MatrixDataTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MatrixDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="matrix">The transform matrix.</param>
|
||||
public MatrixDataTransform(Matrix matrix)
|
||||
{
|
||||
this.matrix = matrix;
|
||||
this.invertedMatrix = matrix;
|
||||
invertedMatrix.Invert();
|
||||
}
|
||||
|
||||
private readonly Matrix matrix;
|
||||
private readonly Matrix invertedMatrix;
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
return matrix.Transform(pt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return invertedMatrix.Transform(pt);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a chain of transforms which are being applied consequently.
|
||||
/// </summary>
|
||||
public sealed class CompositeDataTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompositeDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="transforms">The transforms.</param>
|
||||
public CompositeDataTransform(params DataTransform[] transforms)
|
||||
{
|
||||
if (transforms == null)
|
||||
throw new ArgumentNullException("transforms");
|
||||
foreach (var transform in transforms)
|
||||
{
|
||||
if (transform == null)
|
||||
throw new ArgumentNullException("transforms", Strings.Exceptions.EachTransformShouldNotBeNull);
|
||||
}
|
||||
|
||||
this.transforms = transforms;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompositeDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="transforms">The transforms.</param>
|
||||
public CompositeDataTransform(IEnumerable<DataTransform> transforms)
|
||||
{
|
||||
if (transforms == null)
|
||||
throw new ArgumentNullException("transforms");
|
||||
|
||||
this.transforms = transforms;
|
||||
}
|
||||
|
||||
private readonly IEnumerable<DataTransform> transforms;
|
||||
/// <summary>
|
||||
/// Gets the transforms.
|
||||
/// </summary>
|
||||
/// <value>The transforms.</value>
|
||||
public IEnumerable<DataTransform> Transforms
|
||||
{
|
||||
get { return transforms; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
foreach (var transform in transforms)
|
||||
{
|
||||
pt = transform.DataToViewport(pt);
|
||||
}
|
||||
|
||||
return pt;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
foreach (var transform in transforms.Reverse())
|
||||
{
|
||||
pt = transform.ViewportToData(pt);
|
||||
}
|
||||
|
||||
return pt;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a data transform, performed by given lambda function.
|
||||
/// </summary>
|
||||
public sealed class LambdaDataTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DelegateDataTransform"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dataToViewport">The data to viewport transform delegate.</param>
|
||||
/// <param name="viewportToData">The viewport to data transform delegate.</param>
|
||||
public LambdaDataTransform(Func<Point, Point> dataToViewport, Func<Point, Point> viewportToData)
|
||||
{
|
||||
if (dataToViewport == null)
|
||||
throw new ArgumentNullException("dataToViewport");
|
||||
if (viewportToData == null)
|
||||
throw new ArgumentNullException("viewportToData");
|
||||
|
||||
this.dataToViewport = dataToViewport;
|
||||
this.viewportToData = viewportToData;
|
||||
}
|
||||
|
||||
private readonly Func<Point, Point> dataToViewport;
|
||||
/// <summary>
|
||||
/// Gets the data to viewport transform delegate.
|
||||
/// </summary>
|
||||
/// <value>The data to viewport func.</value>
|
||||
public Func<Point, Point> DataToViewportFunc
|
||||
{
|
||||
get { return dataToViewport; }
|
||||
}
|
||||
|
||||
private readonly Func<Point, Point> viewportToData;
|
||||
/// <summary>
|
||||
/// Gets the viewport to data transform delegate.
|
||||
/// </summary>
|
||||
/// <value>The viewport to data func.</value>
|
||||
public Func<Point, Point> ViewportToDataFunc
|
||||
{
|
||||
get { return viewportToData; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
return dataToViewport(pt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns></returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return viewportToData(pt);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Contains default data transforms.
|
||||
/// </summary>
|
||||
public static class DataTransforms
|
||||
{
|
||||
private static readonly IdentityTransform identity = new IdentityTransform();
|
||||
/// <summary>
|
||||
/// Gets the default identity data transform.
|
||||
/// </summary>
|
||||
/// <value>The identity data transform.</value>
|
||||
public static IdentityTransform Identity
|
||||
{
|
||||
get
|
||||
{
|
||||
return identity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Transforms/Log10Transform.cs
Normal file
56
Transforms/Log10Transform.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a logarithmic transform of both x- and y-values.
|
||||
/// </summary>
|
||||
public sealed class Log10Transform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Log10Transform"/> class.
|
||||
/// </summary>
|
||||
public Log10Transform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns>
|
||||
/// Transformed point in viewport coordinates.
|
||||
/// </returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
double x = pt.X;
|
||||
double y = pt.Y;
|
||||
|
||||
x = x < 0 ? Double.MinValue : Math.Log10(x);
|
||||
y = y < 0 ? Double.MinValue : Math.Log10(y);
|
||||
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns>Transformed point in data coordinates.</returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return new Point(Math.Pow(10, pt.X), Math.Pow(10, pt.Y));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data domain of this dataTransform.
|
||||
/// </summary>
|
||||
/// <value>The data domain of this dataTransform.</value>
|
||||
public override DataRect DataDomain
|
||||
{
|
||||
get { return DataDomains.XYPositive; }
|
||||
}
|
||||
}
|
||||
}
|
||||
41
Transforms/SwapTransform.cs
Normal file
41
Transforms/SwapTransform.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a DataTransform that simply swaps points' coefficitiens from x to y and vice verca.
|
||||
/// </summary>
|
||||
public sealed class SwapTransform : DataTransform
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SwapTransform"/> class.
|
||||
/// </summary>
|
||||
public SwapTransform() { }
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in data coordinates to viewport coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in data coordinates.</param>
|
||||
/// <returns>
|
||||
/// Transformed point in viewport coordinates.
|
||||
/// </returns>
|
||||
public override Point DataToViewport(Point pt)
|
||||
{
|
||||
return new Point(pt.Y, pt.X);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Transforms the point in viewport coordinates to data coordinates.
|
||||
/// </summary>
|
||||
/// <param name="pt">The point in viewport coordinates.</param>
|
||||
/// <returns>Transformed point in data coordinates.</returns>
|
||||
public override Point ViewportToData(Point pt)
|
||||
{
|
||||
return new Point(pt.Y, pt.X);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user