PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/Auxiliary/IDataSource2DExtensions.cs

#
C# | 160 lines | 135 code | 25 blank | 0 comment | 26 complexity | e6b59a7f951a2fedfe80ea323568f072 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Research.DynamicDataDisplay.Charts;
  6. using Microsoft.Research.DynamicDataDisplay.DataSources;
  7. using System.Windows;
  8. using System.Diagnostics.CodeAnalysis;
  9. namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
  10. {
  11. public static class IDataSource2DExtensions
  12. {
  13. public static Range<double> GetMinMax(this double[,] data)
  14. {
  15. data.VerifyNotNull("data");
  16. int width = data.GetLength(0);
  17. int height = data.GetLength(1);
  18. Verify.IsTrueWithMessage(width > 0, Strings.Exceptions.ArrayWidthShouldBePositive);
  19. Verify.IsTrueWithMessage(height > 0, Strings.Exceptions.ArrayHeightShouldBePositive);
  20. double min = data[0, 0];
  21. double max = data[0, 0];
  22. for (int x = 0; x < width; x++)
  23. {
  24. for (int y = 0; y < height; y++)
  25. {
  26. if (data[x, y] < min)
  27. min = data[x, y];
  28. if (data[x, y] > max)
  29. max = data[x, y];
  30. }
  31. }
  32. Range<double> res = new Range<double>(min, max);
  33. return res;
  34. }
  35. public static Range<double> GetMinMax(this double[,] data, double missingValue)
  36. {
  37. data.VerifyNotNull("data");
  38. int width = data.GetLength(0);
  39. int height = data.GetLength(1);
  40. Verify.IsTrueWithMessage(width > 0, Strings.Exceptions.ArrayWidthShouldBePositive);
  41. Verify.IsTrueWithMessage(height > 0, Strings.Exceptions.ArrayHeightShouldBePositive);
  42. double min = Double.MaxValue;
  43. double max = Double.MinValue;
  44. for (int x = 0; x < width; x++)
  45. {
  46. for (int y = 0; y < height; y++)
  47. {
  48. if (data[x, y] != missingValue && data[x, y] < min)
  49. min = data[x, y];
  50. if (data[x, y] != missingValue && data[x, y] > max)
  51. max = data[x, y];
  52. }
  53. }
  54. Range<double> res = new Range<double>(min, max);
  55. return res;
  56. }
  57. public static Range<double> GetMinMax(this IDataSource2D<double> dataSource)
  58. {
  59. dataSource.VerifyNotNull("dataSource");
  60. return GetMinMax(dataSource.Data);
  61. }
  62. public static Range<double> GetMinMax(this IDataSource2D<double> dataSource, double missingValue)
  63. {
  64. dataSource.VerifyNotNull("dataSource");
  65. return GetMinMax(dataSource.Data, missingValue);
  66. }
  67. public static Range<double> GetMinMax(this IDataSource2D<double> dataSource, DataRect area)
  68. {
  69. if (dataSource == null)
  70. throw new ArgumentNullException("dataSource");
  71. double min = Double.PositiveInfinity;
  72. double max = Double.NegativeInfinity;
  73. int width = dataSource.Width;
  74. int height = dataSource.Height;
  75. var grid = dataSource.Grid;
  76. var data = dataSource.Data;
  77. for (int ix = 0; ix < width; ix++)
  78. {
  79. for (int iy = 0; iy < height; iy++)
  80. {
  81. if (area.Contains(grid[ix, iy]))
  82. {
  83. var value = data[ix, iy];
  84. if (value < min)
  85. min = value;
  86. if (value > max)
  87. max = value;
  88. }
  89. }
  90. }
  91. if (min < max)
  92. return new Range<double>(min, max);
  93. else
  94. return new Range<double>();
  95. }
  96. public static DataRect GetGridBounds(this Point[,] grid)
  97. {
  98. double minX = grid[0, 0].X;
  99. double maxX = minX;
  100. double minY = grid[0, 0].Y;
  101. double maxY = minY;
  102. int width = grid.GetLength(0);
  103. int height = grid.GetLength(1);
  104. for (int ix = 0; ix < width; ix++)
  105. {
  106. for (int iy = 0; iy < height; iy++)
  107. {
  108. Point pt = grid[ix, iy];
  109. double x = pt.X;
  110. double y = pt.Y;
  111. if (x < minX) minX = x;
  112. if (x > maxX) maxX = x;
  113. if (y < minY) minY = y;
  114. if (y > maxY) maxY = y;
  115. }
  116. }
  117. return new DataRect(new Point(minX, minY), new Point(maxX, maxY));
  118. }
  119. public static DataRect GetGridBounds<T>(this IDataSource2D<T> dataSource) where T : struct
  120. {
  121. return dataSource.Grid.GetGridBounds();
  122. }
  123. public static DataRect GetGridBounds<T>(this INonUniformDataSource2D<T> dataSource) where T : struct
  124. {
  125. var xCoordinates = dataSource.XCoordinates;
  126. var yCoordinates = dataSource.YCoordinates;
  127. var xMin = xCoordinates[0];
  128. var xMax = xCoordinates[xCoordinates.Length - 1];
  129. var yMin = yCoordinates[0];
  130. var yMax = yCoordinates[yCoordinates.Length - 1];
  131. var contentBounds = DataRect.FromPoints(xMin, yMin, xMax, yMax);
  132. return contentBounds;
  133. }
  134. }
  135. }