public void LinearInterpTest(double alpha) { const double N0 = 0.0; const double N1 = 1.0; Assert.Equal(alpha, NoiseMath.Linear(N0, N1, alpha)); }
/// <summary> /// See the documentation on the base class. /// <seealso cref="Module"/> /// </summary> /// <param name="x">X coordinate</param> /// <param name="y">Y coordinate</param> /// <param name="z">Z coordinate</param> /// <returns>Returns the computed value</returns> public override double GetValue(double x, double y, double z) { var v0 = SourceModules[0].GetValue(x, y, z); var v1 = SourceModules[1].GetValue(x, y, z); var alpha = (SourceModules[2].GetValue(x, y, z) + 1) / 2; return(NoiseMath.Linear(v0, v1, alpha)); }
/// <summary> /// See the documentation on the base class. /// <seealso cref="Module"/> /// </summary> /// <param name="x">X coordinate</param> /// <param name="y">Y coordinate</param> /// <param name="z">Z coordinate</param> /// <returns>Returns the computed value</returns> public override double GetValue(double x, double y, double z) { double v0 = this.SourceModules[0].GetValue(x, y, z); double v1 = this.SourceModules[1].GetValue(x, y, z); double alpha = (this.SourceModules[2].GetValue(x, y, z) + 1) / 2; return(NoiseMath.Linear(v0, v1, alpha)); }
public void Math_Interp_Linear_Right_Test() { var value1 = 0D; var value2 = 5D; var alpha = 1D; var expected = 5D; var actual = NoiseMath.Linear(value1, value2, alpha); Assert.AreEqual(expected, actual); }
/// <summary> /// See the documentation on the base class. /// <seealso cref="Module"/> /// </summary> /// <param name="x">X coordinate</param> /// <param name="y">Y coordinate</param> /// <param name="z">Z coordinate</param> /// <returns>Returns the computed value</returns> public override double GetValue(double x, double y, double z) { // 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 < ControlPoints.Count; indexPos++) { if (sourceModuleValue < ControlPoints[indexPos].x) { break; } } // Find the two nearest control points so that we can map their values // onto a quadratic curve. var index0 = NoiseMath.Clamp(indexPos - 1, 0, ControlPoints.Count - 1); var index1 = NoiseMath.Clamp(indexPos, 0, ControlPoints.Count - 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].x); } // Compute the alpha value used for linear interpolation. var value0 = ControlPoints[index0].x; var value1 = ControlPoints[index1].x; var alpha = (sourceModuleValue - value0) / (value1 - value0); if (InvertTerraces) { alpha = 1.0 - alpha; NoiseMath.Swap(ref value0, ref value1); } // Squaring the alpha produces the terrace effect. alpha *= alpha; // Now perform the linear interpolation given the alpha value. return(NoiseMath.Linear(value0, value1, alpha)); }
/// <summary> /// Calculates the destination color. /// </summary> /// <param name="sourceColor">The source color generated from the color /// gradient.</param> /// <param name="backgroundColor">The color from the background image at the /// corresponding position.</param> /// <param name="lightValue">The intensity of the light at that position.</param> /// <returns>The destination color.</returns> Color CalcDestinationColor(Color sourceColor, Color backgroundColor, double lightValue) { var sourceRed = (double)sourceColor.Red / 255.0; var sourceGreen = (double)sourceColor.Green / 255.0; var sourceBlue = (double)sourceColor.Blue / 255.0; var sourceAlpha = (double)sourceColor.Alpha / 255.0; var backgroundRed = (double)backgroundColor.Red / 255.0; var backgroundGreen = (double)backgroundColor.Green / 255.0; var backgroundBlue = (double)backgroundColor.Blue / 255.0; // First, blend the source color to the background color using the alpha // of the source color. var red = NoiseMath.Linear(backgroundRed, sourceRed, sourceAlpha); var green = NoiseMath.Linear(backgroundGreen, sourceGreen, sourceAlpha); var blue = NoiseMath.Linear(backgroundBlue, sourceBlue, sourceAlpha); if (EnableLight) { // Now calculate the light color. var lightRed = lightValue * (double)LightColor.Red / 255.0; var lightGreen = lightValue * (double)LightColor.Green / 255.0; var lightBlue = lightValue * (double)LightColor.Blue / 255.0; // Apply the light color to the new color. red *= lightRed; green *= lightGreen; blue *= lightBlue; } // Clamp the color channels to the (0..1) range. red = NoiseMath.Clamp(red, 0, 1); green = NoiseMath.Clamp(green, 0, 1); blue = NoiseMath.Clamp(blue, 0, 1); // Rescale the color channels to the (0..255) range and return // the new color. Color newColor = new Color( (byte)((int)(red * 255) & 0xff), (byte)((int)(green * 255) & 0xff), (byte)((int)(blue * 255) & 0xff), Math.Max(sourceColor.Alpha, backgroundColor.Alpha)); return(newColor); }
protected override void BuildImpl(CancellationToken cancellationToken) { Plane planeModel = new Plane(SourceModule); var xExtent = UpperXBound - LowerXBound; var zExtent = UpperZBound - LowerZBound; var xDelta = xExtent / destWidth; var zDelta = zExtent / destHeight; var po = new ParallelOptions() { CancellationToken = cancellationToken, }; Parallel.For(0, destHeight, po, z => { double zCur = LowerZBound + z * zDelta; int x; double xCur; for (x = 0, xCur = LowerXBound; x < destWidth; x++, xCur += xDelta) { float finalValue; if (!EnableSeamless) { finalValue = (float)planeModel.GetValue(xCur, zCur); } else { var swValue = planeModel.GetValue(xCur, zCur); var seValue = planeModel.GetValue(xCur + xExtent, zCur); var nwValue = planeModel.GetValue(xCur, zCur + zExtent); var neValue = planeModel.GetValue(xCur + xExtent, zCur + zExtent); var xBlend = 1.0 - ((xCur - LowerXBound) / xExtent); var zBlend = 1.0 - ((zCur - LowerZBound) / zExtent); var z0 = NoiseMath.Linear(swValue, seValue, xBlend); var z1 = NoiseMath.Linear(nwValue, neValue, xBlend); finalValue = (float)NoiseMath.Linear(z0, z1, zBlend); } DestNoiseMap[x, z] = finalValue; } }); }
protected override void BuildImpl(CancellationToken cancellationToken) { Plane planeModel = new Plane(this.SourceModule); double xExtent = this.UpperXBound - this.LowerXBound; double zExtent = this.UpperZBound - this.LowerZBound; double xDelta = xExtent / this.destWidth; double zDelta = zExtent / this.destHeight; ParallelOptions po = new ParallelOptions() { CancellationToken = cancellationToken, }; Parallel.For(0, this.destHeight, po, z => { double zCur = this.LowerZBound + z * zDelta; int x; double xCur; for (x = 0, xCur = this.LowerXBound; x < this.destWidth; x++, xCur += xDelta) { float finalValue; if (!this.EnableSeamless) { finalValue = (float)planeModel.GetValue(xCur, zCur); } else { double swValue = planeModel.GetValue(xCur, zCur); double seValue = planeModel.GetValue(xCur + xExtent, zCur); double nwValue = planeModel.GetValue(xCur, zCur + zExtent); double neValue = planeModel.GetValue(xCur + xExtent, zCur + zExtent); double xBlend = 1.0 - ((xCur - this.LowerXBound) / xExtent); double zBlend = 1.0 - ((zCur - this.LowerZBound) / zExtent); double z0 = NoiseMath.Linear(swValue, seValue, xBlend); double z1 = NoiseMath.Linear(nwValue, neValue, xBlend); finalValue = (float)NoiseMath.Linear(z0, z1, zBlend); } this.DestNoiseMap[x, z] = finalValue; } }); }
/// <summary> /// See the documentation on the base class. /// <seealso cref="Module"/> /// </summary> /// <param name="x">X coordinate</param> /// <param name="y">Y coordinate</param> /// <param name="z">Z coordinate</param> /// <returns>Returns the computed value</returns> public override double GetValue(double x, double y, double z) { var controlValue = SourceModules[2].GetValue(x, y, z); double alpha; if (EdgeFalloff > 0.0) { if (controlValue < (LowerBound - EdgeFalloff)) { // The output value from the control module is below the selector // threshold; return the output value from the first source module. return(SourceModules[0].GetValue(x, y, z)); } else if (controlValue < (LowerBound + EdgeFalloff)) { // The output value from the control module is near the lower end of the // selector threshold and within the smooth curve. Interpolate between // the output values from the first and second source modules. double lowerCurve = (LowerBound - EdgeFalloff); double upperCurve = (LowerBound + EdgeFalloff); alpha = NoiseMath.SCurve3((controlValue - lowerCurve) / (upperCurve - lowerCurve)); return(NoiseMath.Linear(SourceModules[0].GetValue(x, y, z), SourceModules[1].GetValue(x, y, z), alpha)); } else if (controlValue < (UpperBound - EdgeFalloff)) { // The output value from the control module is within the selector // threshold; return the output value from the second source module. return(SourceModules[1].GetValue(x, y, z)); } else if (controlValue < (UpperBound + EdgeFalloff)) { // The output value from the control module is near the upper end of the // selector threshold and within the smooth curve. Interpolate between // the output values from the first and second source modules. double lowerCurve = (UpperBound - EdgeFalloff); double upperCurve = (UpperBound + EdgeFalloff); alpha = NoiseMath.SCurve3((controlValue - lowerCurve) / (upperCurve - lowerCurve)); return(NoiseMath.Linear(SourceModules[1].GetValue(x, y, z), SourceModules[0].GetValue(x, y, z), alpha)); } else { // Output value from the control module is above the selector threshold; // return the output value from the first source module. return(SourceModules[0].GetValue(x, y, z)); } } else { if (controlValue < LowerBound || controlValue > UpperBound) { return(SourceModules[0].GetValue(x, y, z)); } else { return(SourceModules[1].GetValue(x, y, z)); } } }