예제 #1
0
파일: Terrace.java 프로젝트: Rsgm/space-rpg
  public double getValue(double x, double y, double z) {
    assert (sourceModules[0] != null);
    assert (controlPointCount >= 2);

    // Get the output value from the source module.
    double sourceModuleValue = sourceModules[0].getValue(x, y, z);

    // Find the first element in the control point array that has a value
    // larger than the output value from the source module.
    int indexPos;
    for (indexPos = 0; indexPos < controlPointCount; indexPos++) {
      if (sourceModuleValue < controlPoints[indexPos]) break;
    }

    // Find the two nearest control points so that we can map their values
    // onto a quadratic curve.
    int index0 = Misc.ClampValue(indexPos - 1, 0, controlPointCount - 1);
    int index1 = Misc.ClampValue(indexPos, 0, controlPointCount - 1);

    // If some control points are missing (which occurs if the output value from
    // the source module is greater than the largest value or less than the
    // smallest value of the control point array), get the value of the nearest
    // control point and exit now.
    if (index0 == index1) return controlPoints[index1];

    // Compute the alpha value used for linear interpolation.
    double value0 = controlPoints[index0];
    double value1 = controlPoints[index1];
    double alpha = (sourceModuleValue - value0) / (value1 - value0);
    if (invertTerraces) {
      alpha = 1.0 - alpha;
      double tempValue = value0;
      value0 = value1;
      value1 = tempValue;
    }

    // Squaring the alpha produces the terrace effect.
    alpha *= alpha;

    // Now perform the linear interpolation given the alpha value.
    return Interp.linearInterp(value0, value1, alpha);
  }