// Exactly same method as above but using a SortedDictionary WeightedIndex[] LerpSorted(WeightedIndex[] left, WeightedIndex[] right, float lerp) { SortedDictionary <int, WeightedIndex> indices = new SortedDictionary <int, WeightedIndex>(); for (int i = 0; i < left.Length; i++) { int index = left[i].Index; WeightedIndex windex; indices.TryGetValue(index, out windex); indices[index] = new WeightedIndex() { Index = index, Weight = windex.Weight + (1 - lerp) * left[i].Weight }; } for (int i = 0; i < right.Length; i++) { int index = right[i].Index; WeightedIndex windex; indices.TryGetValue(index, out windex); indices[index] = new WeightedIndex() { Index = index, Weight = windex.Weight + lerp * right[i].Weight }; } return(indices.Values.ToArray()); }
public LerpedWeightedIndex2DMap(int[] rawScalarValues, int sizeX, int boxBlurRadius, int dataTopLeftPadding, int dataBotRightPadding) { this.sizeX = sizeX; this.topleftPadding = dataTopLeftPadding; this.botRightPadding = dataBotRightPadding; groups = new WeightedIndex[rawScalarValues.Length][]; Dictionary <int, float> indices = new Dictionary <int, float>(); for (int x = 0; x < sizeX; x++) { for (int z = 0; z < sizeX; z++) { int minx = Math.Max(0, x - boxBlurRadius); int minz = Math.Max(0, z - boxBlurRadius); int maxx = Math.Min(sizeX - 1, x + boxBlurRadius); int maxz = Math.Min(sizeX - 1, z + boxBlurRadius); indices.Clear(); float weightFrac = 1f / ((maxx - minx + 1) * (maxz - minz + 1)); // Box Blur for (int bx = minx; bx <= maxx; bx++) { for (int bz = minz; bz <= maxz; bz++) { int index = rawScalarValues[bz * sizeX + bx]; if (indices.ContainsKey(index)) { indices[index] += weightFrac; } else { indices[index] = weightFrac; } } } // Write blurred entries for this coordinate groups[z * sizeX + x] = new WeightedIndex[indices.Count]; int i = 0; foreach (var val in indices) { groups[z * sizeX + x][i++] = new WeightedIndex() { Index = val.Key, Weight = val.Value }; } } } }
public LerpedWeightedIndex2DMap(int[] rawScalarValues, int size, int boxBlurRadius) { this.sizeX = size; groups = new WeightedIndex[rawScalarValues.Length][]; Dictionary <int, float> indices = new Dictionary <int, float>(); for (int x = 0; x < size; x++) { // TODO: Use fast blur for better performance (see Cairo::SurfaceTransformBlur or http://blog.ivank.net/fastest-gaussian-blur.html) for (int z = 0; z < size; z++) { int minx = Math.Max(0, x - boxBlurRadius); int minz = Math.Max(0, z - boxBlurRadius); int maxx = Math.Min(size - 1, x + boxBlurRadius); int maxz = Math.Min(size - 1, z + boxBlurRadius); indices.Clear(); float weightFrac = 1f / ((maxx - minx + 1) * (maxz - minz + 1)); // Box Blur for (int bx = minx; bx <= maxx; bx++) { for (int bz = minz; bz <= maxz; bz++) { int index = rawScalarValues[bz * size + bx]; if (indices.ContainsKey(index)) { indices[index] += weightFrac; } else { indices[index] = weightFrac; } } } // Write blurred entries for this coordinate groups[z * size + x] = new WeightedIndex[indices.Count]; int i = 0; foreach (var val in indices) { groups[z * size + x][i++] = new WeightedIndex() { index = val.Key, weight = val.Value }; } } } }
public LerpedWeightedIndex2DMap(int[] discreteValues2d, int sizeX) { this.sizeX = sizeX; groups = new WeightedIndex[discreteValues2d.Length][]; for (int i = 0; i < discreteValues2d.Length; i++) { groups[i] = new WeightedIndex[] { new WeightedIndex() { Index = discreteValues2d[i], Weight = 1 } }; } }
// Exactly same method as above but using a SortedDictionary WeightedIndex[] LerpSorted(WeightedIndex[] left, WeightedIndex[] right, float lerp) { SortedDictionary <int, WeightedIndex> indices = new SortedDictionary <int, WeightedIndex>(); for (int i = 0; i < left.Length; i++) { int index = left[i].index; if (indices.ContainsKey(index)) { indices[index].weight += (1 - lerp) * left[i].weight; } else { indices[index] = new WeightedIndex() { index = index, weight = (1 - lerp) * left[i].weight }; } } for (int i = 0; i < right.Length; i++) { int index = right[i].index; if (indices.ContainsKey(index)) { indices[index].weight += lerp * right[i].weight; } else { indices[index] = new WeightedIndex() { index = index, weight = lerp * right[i].weight }; } } return(indices.Values.ToArray()); }