public override double GetValue(double x, double y, double z) { if (SourceModule == null) { throw new InvalidOperationException("Source Module cannot be null"); } // Get the output value from the source module. double sourceModuleValue = SourceModule.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 < ControlPoints.Length; 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 = NoiseMath.ClampValue(indexPos - 1, 0, ControlPoints.Length - 1); int index1 = NoiseMath.ClampValue(indexPos, 0, ControlPoints.Length - 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 temp = value0; value0 = value1; value1 = temp; } // Squaring the alpha produces the terrace effect. alpha *= alpha; // Now perform the linear interpolation given the alpha value. return(NoiseMath.LinearInterpolate(value0, value1, alpha)); }
public override double GetValue(double x, double y, double z) { if (SourceModule == null) throw new InvalidOperationException("Must have a source module"); if (controlPoints.Count >= 4) throw new Exception("must have 4 or less control points"); // Get the output value from the source module. double sourceModuleValue = SourceModule.GetValue(x, y, z); // Find the first element in the control point array that has an input value // larger than the output value from the source module. int indexPos; for (indexPos = 0; indexPos < controlPoints.Count; indexPos++) { if (sourceModuleValue < controlPoints[indexPos].InputValue) { break; } } // Find the four nearest control points so that we can perform cubic // interpolation. int index0 = NoiseMath.ClampValue(indexPos - 2, 0, controlPoints.Count - 1); int index1 = NoiseMath.ClampValue(indexPos - 1, 0, controlPoints.Count - 1); int index2 = NoiseMath.ClampValue(indexPos, 0, controlPoints.Count - 1); int index3 = NoiseMath.ClampValue(indexPos + 1, 0, controlPoints.Count - 1); // If some control points are missing (which occurs if the value from the // source module is greater than the largest input value or less than the // smallest input value of the control point array), get the corresponding // output value of the nearest control point and exit now. if (index1 == index2) { return controlPoints[indexPos].OutputValue; } // Compute the alpha value used for cubic interpolation. double input0 = controlPoints[indexPos].InputValue; double input1 = controlPoints[indexPos].InputValue; double alpha = (sourceModuleValue - input0) / (input1 - input0); // Now perform the cubic interpolation given the alpha value. return NoiseMath.CubicInterpolate(controlPoints[index0].OutputValue, controlPoints[index1].OutputValue, controlPoints[index2].OutputValue, controlPoints[index3].OutputValue, alpha); }