PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/Main/src/DynamicDataDisplay/Transforms/CoordinateTransform.cs

#
C# | 199 lines | 116 code | 29 blank | 54 comment | 2 complexity | bc98ea6916df9074ac6c9ffc9c4c668b MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Windows;
  6using Microsoft.Research.DynamicDataDisplay.Common;
  7using System.Diagnostics;
  8using System.Windows.Media;
  9
 10namespace Microsoft.Research.DynamicDataDisplay
 11{
 12	/// <summary>
 13	/// A central class in 2d coordinate transformation in DynamicDataDisplay.
 14	/// Provides methods to transform point from one coordinate system to another.
 15	/// Should be immutable.
 16	/// </summary>
 17	public sealed class CoordinateTransform
 18	{
 19		private CoordinateTransform(DataRect visibleRect, Rect screenRect)
 20		{
 21			this.visibleRect = visibleRect;
 22			this.screenRect = screenRect;
 23
 24			rxToScreen = screenRect.Width / visibleRect.Width;
 25			ryToScreen = screenRect.Height / visibleRect.Height;
 26			cxToScreen = visibleRect.XMin * rxToScreen - screenRect.Left;
 27			cyToScreen = screenRect.Height + screenRect.Top + visibleRect.YMin * ryToScreen;
 28
 29			rxToData = visibleRect.Width / screenRect.Width;
 30			ryToData = visibleRect.Height / screenRect.Height;
 31			cxToData = screenRect.Left * rxToData - visibleRect.XMin;
 32			cyToData = visibleRect.Height + visibleRect.YMin + screenRect.Top * ryToData;
 33		}
 34
 35		#region Coeffs
 36		double rxToScreen;
 37		double ryToScreen;
 38		double cxToScreen;
 39		double cyToScreen;
 40
 41		double rxToData;
 42		double ryToData;
 43		double cxToData;
 44		double cyToData;
 45		#endregion
 46
 47		#region Creation methods
 48
 49		/// <summary>
 50		/// Creates CoordinateTransform fro specified rectangles and with Identity transform.
 51		/// </summary>
 52		/// <param name="visibleRect">The visible rect.</param>
 53		/// <param name="screenRect">The screen rect.</param>
 54		/// <returns></returns>
 55		public static CoordinateTransform FromRects(DataRect visibleRect, Rect screenRect)
 56		{
 57			CoordinateTransform result = new CoordinateTransform(visibleRect, screenRect);
 58			return result;
 59		}
 60
 61		/// <summary>
 62		/// Closnes transform with current dataTransform and specified rectangles.
 63		/// </summary>
 64		/// <param name="visibleRect">The visible rect.</param>
 65		/// <param name="screenRect">The screen rect.</param>
 66		/// <returns></returns>
 67		public CoordinateTransform WithRects(DataRect visibleRect, Rect screenRect)
 68		{
 69			CoordinateTransform copy = new CoordinateTransform(visibleRect, screenRect);
 70			copy.dataTransform = dataTransform;
 71			return copy;
 72		}
 73
 74		/// <summary>
 75		/// Creates a new instance of CoordinateTransform with the given data transform.
 76		/// </summary>
 77		/// <param name="dataTransform">The data transform.</param>
 78		/// <returns></returns>
 79		public CoordinateTransform WithDataTransform(DataTransform dataTransform)
 80		{
 81			if (dataTransform == null)
 82				throw new ArgumentNullException("dataTransform");
 83
 84			CoordinateTransform copy = new CoordinateTransform(visibleRect, screenRect);
 85			copy.dataTransform = dataTransform;
 86			return copy;
 87		}
 88
 89		internal CoordinateTransform WithScreenOffset(double x, double y)
 90		{
 91			Rect screenCopy = screenRect;
 92			screenCopy.Offset(x, y);
 93			CoordinateTransform copy = new CoordinateTransform(visibleRect, screenCopy);
 94			return copy;
 95		}
 96
 97		internal static CoordinateTransform CreateDefault()
 98		{
 99			CoordinateTransform transform = new CoordinateTransform(new Rect(0, 0, 1, 1), new Rect(0, 0, 1, 1));
100
101			return transform;
102		}
103
104		#endregion
105
106		#region Transform methods
107
108		/// <summary>
109		/// Transforms point from data coordinates to screen.
110		/// </summary>
111		/// <param name="dataPoint">The point in data coordinates.</param>
112		/// <returns></returns>
113		public Point DataToScreen(Point dataPoint)
114		{
115			Point viewportPoint = dataTransform.DataToViewport(dataPoint);
116
117			Point screenPoint = new Point(viewportPoint.X * rxToScreen - cxToScreen,
118				cyToScreen - viewportPoint.Y * ryToScreen);
119
120			return screenPoint;
121		}
122
123		/// <summary>
124		/// Transforms point from screen coordinates to data coordinates.
125		/// </summary>
126		/// <param name="screenPoint">The point in screen coordinates.</param>
127		/// <returns></returns>
128		public Point ScreenToData(Point screenPoint)
129		{
130			Point viewportPoint = new Point(screenPoint.X * rxToData - cxToData,
131				cyToData - screenPoint.Y * ryToData);
132
133			Point dataPoint = dataTransform.ViewportToData(viewportPoint);
134
135			return dataPoint;
136		}
137
138		/// <summary>
139		/// Transforms point from viewport coordinates to screen coordinates.
140		/// </summary>
141		/// <param name="viewportPoint">The point in viewport coordinates.</param>
142		/// <returns></returns>
143		public Point ViewportToScreen(Point viewportPoint)
144		{
145			Point screenPoint = new Point(viewportPoint.X * rxToScreen - cxToScreen,
146				cyToScreen - viewportPoint.Y * ryToScreen);
147
148			return screenPoint;
149		}
150
151		/// <summary>
152		/// Transforms point from screen coordinates to viewport coordinates.
153		/// </summary>
154		/// <param name="screenPoint">The point in screen coordinates.</param>
155		/// <returns></returns>
156		public Point ScreenToViewport(Point screenPoint)
157		{
158			Point viewportPoint = new Point(screenPoint.X * rxToData - cxToData,
159				cyToData - screenPoint.Y * ryToData);
160
161			return viewportPoint;
162		}
163
164		#endregion
165
166		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
167		private DataRect visibleRect;
168		/// <summary>
169		/// Gets the viewport rectangle.
170		/// </summary>
171		/// <value>The viewport rect.</value>
172		public DataRect ViewportRect
173		{
174			get { return visibleRect; }
175		}
176
177		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
178		private Rect screenRect;
179		/// <summary>
180		/// Gets the screen rectangle.
181		/// </summary>
182		/// <value>The screen rect.</value>
183		public Rect ScreenRect
184		{
185			get { return screenRect; }
186		}
187
188		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
189		private DataTransform dataTransform = DataTransforms.Identity;
190		/// <summary>
191		/// Gets the data transform.
192		/// </summary>
193		/// <value>The data transform.</value>
194		public DataTransform DataTransform
195		{
196			get { return dataTransform; }
197		}
198	}
199}