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