smoothMedian static method

void smoothMedian(
  1. Float32List g,
  2. TerrainOptions options
)

Smooth the terrain by setting each point to the median of its neighborhood.

Float32List g The geometry's z-positions to modify with heightmap data. TerrainOptions options A map of settings that control how the terrain is constructed and displayed. Valid values are the same as those for the options parameter of {@link THREE.Terrain}().

Implementation

static void smoothMedian(Float32List g, TerrainOptions options) {
  final heightmap = Float32List(g.length);
  List<int> neighborValues = [],
    neighborKeys = [];
  int sortByValue(a, b) {
    return (neighborValues.get(a)??0) - (neighborValues.get(b) ?? 0);
  }
  for (int i = 0, xl = options.xSegments + 1, yl = options.ySegments + 1; i < xl; i++) {
    for (int j = 0; j < yl; j++) {
      neighborValues.length = 0;
      neighborKeys.length = 0;
      for (int n = -1; n <= 1; n++) {
        for (int m = -1; m <= 1; m++) {
          final key = (j+n)*xl + i + m;
          if (i+m >= 0 && j+n >= 0 && i+m < xl && j+n < yl) {//typeof g[key] != 'undefined' &&
            neighborValues.add(g[key].toInt());
            neighborKeys.add(key);
          }
        }
      }
      neighborKeys.sort(sortByValue);
      final halfKey = (neighborKeys.length*0.5).floor();
      double median;
      if (neighborKeys.length % 2 == 1) {
        median = g[neighborKeys[halfKey]];
      }
      else {
        median = (g[neighborKeys[halfKey-1]] + g[neighborKeys[halfKey]])*0.5;
      }
      heightmap[j*xl + i] = median;
    }
  }
  for (int k = 0, l = g.length; k < l; k++) {
    g[k] = heightmap[k];
  }
}