Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Transforms/CoordinateTransformExtensions.cs @ 12586

Last change on this file since 12586 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 13.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows;
6using System.Diagnostics.CodeAnalysis;
7using Microsoft.Research.DynamicDataDisplay.Common;
8using Microsoft.Research.DynamicDataDisplay.Filters;
9
10namespace Microsoft.Research.DynamicDataDisplay
11{
12  /// <summary>
13  /// Contains extension methods that perform coordinate space transformations.
14  /// </summary>
15  public static class CoordinateTransformExtensions
16  {
17    #region Points
18
19    /// <summary>
20    /// Transforms point in data coordinates to screen coordinates.
21    /// </summary>
22    /// <param name="dataPoint">Point in data coordinates</param>
23    /// <param name="transform">CoordinateTransform used to perform transformation</param>
24    /// <returns>Point in screen coordinates</returns>
25    public static Point DataToScreen(this Point dataPoint, CoordinateTransform transform)
26    {
27      return transform.DataToScreen(dataPoint);
28    }
29
30    /// <summary>
31    /// Transforms point in screen coordinates to data coordinates.
32    /// </summary>
33    /// <param name="screenPoint">Point in screen coordinates</param>
34    /// <param name="transform">CoordinateTransform used to perform transformation</param>
35    /// <returns>Point in data coordinates</returns>
36    public static Point ScreenToData(this Point screenPoint, CoordinateTransform transform)
37    {
38      return transform.ScreenToData(screenPoint);
39    }
40
41    /// <summary>
42    /// Transforms point in screen coordinates to viewport coordinates.
43    /// </summary>
44    /// <param name="screenPoint">Point in screen coordinates</param>
45    /// <param name="transform">CoordinateTransform used to perform transformation</param>
46    /// <returns>Point in viewport coordinates</returns>
47    public static Point ScreenToViewport(this Point screenPoint, CoordinateTransform transform)
48    {
49      return transform.ScreenToViewport(screenPoint);
50    }
51
52    /// <summary>
53    /// Transforms point in viewport coordinates to screen coordinates.
54    /// </summary>
55    /// <param name="viewportPoint">Point in viewport coordinates</param>
56    /// <param name="transform">CoordinateTransform used to perform transformation</param>
57    /// <returns>Point in screen coordinates</returns>
58    public static Point ViewportToScreen(this Point viewportPoint, CoordinateTransform transform)
59    {
60      return transform.ViewportToScreen(viewportPoint);
61    }
62
63    /// <summary>
64    /// Transforms point in viewport coordinates to data coordinates.
65    /// </summary>
66    /// <param name="viewportPoint">Point in viewport coordinates</param>
67    /// <param name="transform">CoordinateTransform used to perform transformation</param>
68    /// <returns>Point in data coordinates</returns>
69    public static Point ViewportToData(this Point viewportPoint, CoordinateTransform transform)
70    {
71      return transform.DataTransform.ViewportToData(viewportPoint);
72    }
73
74    /// <summary>
75    /// Transforms point in data coordinates to viewport coordinates.
76    /// </summary>
77    /// <param name="dataPoint">Point in data coordinates</param>
78    /// <param name="transform">CoordinateTransform used to perform transformation</param>
79    /// <returns>Point in viewport coordinates</returns>
80    public static Point DataToViewport(this Point dataPoint, CoordinateTransform transform)
81    {
82      return transform.DataTransform.DataToViewport(dataPoint);
83    }
84
85    /// <summary>
86    /// Transforms point in data coordinates to viewport coordinates.
87    /// </summary>
88    /// <param name="dataPoint">Point in data coordinates</param>
89    /// <param name="transform">CoordinateTransform used to perform transformation</param>
90    /// <returns>Point in viewport coordinates</returns>
91    public static Point DataToViewport(this Point dataPoint, DataTransform transform)
92    {
93      return transform.DataToViewport(dataPoint);
94    }
95
96    #endregion
97
98    #region Rects
99
100    /// <summary>
101    /// Transforms rectangle from screen coordinates to data coordinates.
102    /// </summary>
103    /// <param name="screenRect">Rectangle in screen coordinates</param>
104    /// <param name="transform">CoordinateTransform used to perform transformation</param>
105    /// <returns>Rectangle in data coordinates</returns>
106    public static Rect ScreenToData(this Rect screenRect, CoordinateTransform transform)
107    {
108      Point p1 = screenRect.BottomLeft.ScreenToData(transform);
109      Point p2 = screenRect.TopRight.ScreenToData(transform);
110
111      return new Rect(p1, p2);
112    }
113
114    /// <summary>
115    /// Transforms rectangle from data coordinates to screen coordinates.
116    /// </summary>
117    /// <param name="dataRect">Rectangle in data coordinates</param>
118    /// <param name="transform">CoordinateTransform used to perform transformation</param>
119    /// <returns>Rectangle in screen coordinates</returns>
120    public static Rect DataToScreen(this DataRect dataRect, CoordinateTransform transform)
121    {
122      Point p1 = dataRect.XMaxYMax.DataToScreen(transform);
123      Point p2 = dataRect.XMinYMin.DataToScreen(transform);
124
125      return new Rect(p1, p2);
126    }
127
128    /// <summary>
129    /// Transforms rectangle from screen coordinates to viewport coordinates.
130    /// </summary>
131    /// <param name="screenRect">Rectangle in screen coordinates</param>
132    /// <param name="transform">CoordinateTransform used to perform transformation</param>
133    /// <returns>Rectangle in viewport coordinates</returns>
134    public static DataRect ScreenToViewport(this Rect screenRect, CoordinateTransform transform)
135    {
136      Point p1 = screenRect.BottomLeft.ScreenToViewport(transform);
137      Point p2 = screenRect.TopRight.ScreenToViewport(transform);
138
139      return new DataRect(p1, p2);
140    }
141
142    /// <summary>
143    /// Transforms rectangle from viewport coordinates to screen coordinates.
144    /// </summary>
145    /// <param name="viewportRect">Rectangle in viewport coordinates</param>
146    /// <param name="transform">CoordinateTransform used to perform transformation</param>
147    /// <returns>Rectangle in screen coordinates</returns>
148    public static Rect ViewportToScreen(this DataRect viewportRect, CoordinateTransform transform)
149    {
150      Point p1 = viewportRect.XMaxYMax.ViewportToScreen(transform);
151      Point p2 = viewportRect.XMinYMin.ViewportToScreen(transform);
152
153      return new Rect(p1, p2);
154    }
155
156    /// <summary>
157    /// Transforms rectangle from viewport coordinates to data coordinates.
158    /// </summary>
159    /// <param name="viewportRect">Rectangle in viewport coordinates</param>
160    /// <param name="transform">CoordinateTransform used to perform transformation</param>
161    /// <returns>Rectangle in data coordinates</returns>
162    public static DataRect ViewportToData(this DataRect viewportRect, CoordinateTransform transform)
163    {
164      Point p1 = viewportRect.XMaxYMax.ViewportToData(transform);
165      Point p2 = viewportRect.XMinYMin.ViewportToData(transform);
166
167      return new DataRect(p1, p2);
168    }
169
170    /// <summary>
171    /// Transforms rectangle from data coordinates to viewport coordinates.
172    /// </summary>
173    /// <param name="dataRect">Rectangle in data coordinates</param>
174    /// <param name="transform">CoordinateTransform used to perform transformation</param>
175    /// <returns>Rectangle in viewport coordinates</returns>
176    public static DataRect DataToViewport(this DataRect dataRect, CoordinateTransform transform)
177    {
178      Point p1 = dataRect.XMaxYMax.DataToViewport(transform);
179      Point p2 = dataRect.XMinYMin.DataToViewport(transform);
180
181      return new DataRect(p1, p2);
182    }
183
184    /// <summary>
185    /// Transforms rectangle from viewport coordinates to data coordinates.
186    /// </summary>
187    /// <param name="viewportRect">Rectangle in viewport coordinates</param>
188    /// <param name="transform">CoordinateTransform used to perform transformation</param>
189    /// <returns>Rectangle in data coordinates</returns>
190    public static DataRect ViewportToData(this DataRect viewportRect, DataTransform transform)
191    {
192      Point p1 = transform.ViewportToData(viewportRect.XMaxYMax);
193      Point p2 = transform.ViewportToData(viewportRect.XMinYMin);
194
195      return new DataRect(p1, p2);
196    }
197
198    /// <summary>
199    /// Transforms rectangle from data coordinates to viewport coordinates.
200    /// </summary>
201    /// <param name="dataRect">Rectangle in data coordinates</param>
202    /// <param name="transform">CoordinateTransform used to perform transformation</param>
203    /// <returns>Rectangle in viewport coordinates</returns>
204    public static DataRect DataToViewport(this DataRect dataRect, DataTransform transform)
205    {
206      Point p1 = transform.DataToViewport(dataRect.XMinYMin);
207      Point p2 = transform.DataToViewport(dataRect.XMaxYMax);
208
209      return new DataRect(p1, p2);
210    }
211
212    #endregion
213
214    #region Collections
215
216    /// <summary>
217    /// Transforms index-wrapped points from viewport to screen coordinate systems.
218    /// </summary>
219    /// <param name="viewportPoints">The viewport points.</param>
220    /// <param name="transform">The transform.</param>
221    /// <returns></returns>
222    public static IEnumerable<IndexWrapper<Point>> ViewportToScreen(this IEnumerable<IndexWrapper<Point>> viewportPoints, CoordinateTransform transform)
223    {
224      foreach (var index in viewportPoints)
225      {
226        Point pt = index.Data.ViewportToScreen(transform);
227
228        var copy = index;
229        copy.Data = pt;
230
231        yield return copy;
232      }
233    }
234
235    public static IEnumerable<Point> ViewportToScreen(this IEnumerable<Point> viewportPoints, CoordinateTransform transform)
236    {
237      foreach (var point in viewportPoints)
238      {
239        yield return point.ViewportToScreen(transform);
240      }
241    }
242
243    public static IEnumerable<Point> DataToScreen(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
244    {
245      foreach (var point in dataPoints)
246      {
247        yield return point.DataToScreen(transform);
248      }
249    }
250
251    public static IEnumerable<IndexWrapper<Point>> DataToScreen(this IEnumerable<IndexWrapper<Point>> viewportPoints, CoordinateTransform transform)
252    {
253      foreach (var index in viewportPoints)
254      {
255        Point pt = index.Data.DataToScreen(transform);
256
257        var copy = index;
258        copy.Data = pt;
259
260        yield return copy;
261      }
262    }
263
264    /// <summary>
265    /// Transforms list of points from data coordinates to screen coordinates.
266    /// </summary>
267    /// <param name="dataPoints">Points in data coordinates</param>
268    /// <param name="transform">CoordinateTransform used to perform transformation</param>
269    /// <returns>Points in screen coordinates</returns>
270    public static List<Point> DataToScreenAsList(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
271    {
272      ICollection<Point> iCollection = dataPoints as ICollection<Point>;
273      List<Point> res;
274
275      if (iCollection != null)
276      {
277        res = new List<Point>(iCollection.Count);
278      }
279      else
280      {
281        res = new List<Point>();
282      }
283
284      foreach (var point in dataPoints)
285      {
286        res.Add(transform.DataToScreen(point));
287      }
288
289      return res;
290    }
291
292    /// <summary>
293    /// Transforms list of points from data coordinates to screen coordinates.
294    /// </summary>
295    /// <param name="transform">Coordinate transform used to perform transformation</param>
296    /// <param name="dataPoints">Points in data coordinates</param>
297    /// <returns>List of points in screen coordinates</returns>
298    [SuppressMessage("Microsoft.Design", "CA1002:DoNotExposeGenericLists")]
299    public static List<Point> DataToScreenAsList(this CoordinateTransform transform, IEnumerable<Point> dataPoints)
300    {
301      return dataPoints.DataToScreenAsList(transform);
302    }
303
304    /// <summary>
305    /// Transforms list of points from data coordinates to viewport coordinates.
306    /// </summary>
307    /// <param name="dataPoints">Points in data coordinates</param>
308    /// <param name="transform">Data transform used to perform transformation</param>
309    /// <returns>List of points in viewport coordinates</returns>
310    public static IEnumerable<Point> DataToViewport(this IEnumerable<Point> dataPoints, DataTransform transform)
311    {
312      foreach (Point pt in dataPoints)
313      {
314        yield return pt.DataToViewport(transform);
315      }
316    }
317
318    public static IEnumerable<IndexWrapper<Point>> DataToViewport(this IEnumerable<IndexWrapper<Point>> viewportPoints, CoordinateTransform transform)
319    {
320      foreach (var index in viewportPoints)
321      {
322        Point pt = index.Data.DataToViewport(transform);
323
324        var copy = index;
325        copy.Data = pt;
326
327        yield return copy;
328      }
329    }
330
331    public static IEnumerable<Point> DataToViewport(this IEnumerable<Point> dataPoints, CoordinateTransform transform)
332    {
333      return dataPoints.DataToViewport(transform.DataTransform);
334    }
335
336    public static IEnumerable<Point> ScreenToViewport(this IEnumerable<Point> screenPoints, CoordinateTransform transform)
337    {
338      foreach (Point pt in screenPoints)
339      {
340        yield return pt.ScreenToViewport(transform);
341      }
342    }
343
344    public static IEnumerable<IndexWrapper<Point>> ScreenToViewport(this IEnumerable<IndexWrapper<Point>> viewportPoints, CoordinateTransform transform)
345    {
346      foreach (var index in viewportPoints)
347      {
348        Point pt = index.Data.ScreenToViewport(transform);
349
350        var copy = index;
351        copy.Data = pt;
352
353        yield return copy;
354      }
355    }
356
357    public static IEnumerable<Point> ScreenToData(this IEnumerable<Point> screenPoints, CoordinateTransform transform)
358    {
359      foreach (Point pt in screenPoints)
360      {
361        yield return pt.ScreenToData(transform);
362      }
363    }
364
365    public static IEnumerable<IndexWrapper<Point>> ScreenToData(this IEnumerable<IndexWrapper<Point>> viewportPoints, CoordinateTransform transform)
366    {
367      foreach (var index in viewportPoints)
368      {
369        Point pt = index.Data.ScreenToData(transform);
370
371        var copy = index;
372        copy.Data = pt;
373
374        yield return copy;
375      }
376    }
377
378    #endregion
379  }
380}
Note: See TracBrowser for help on using the repository browser.