From 8e27656fcc0331c8521e4ef8e8ece4495f1f32ae Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Wed, 12 Mar 2008 11:02:30 +0000 Subject: [PATCH] * Refactored some terrain brushes to move out some common functions into TerrainUtil class. More needs doing. * Adjusted strength of brushes to Math.Pow(2,size), this should in theory work closer to how it was before. --- .../Terrain/PaintBrushes/ErodeSphere.cs | 43 ++-------------- .../Terrain/PaintBrushes/FlattenSphere.cs | 2 + .../Terrain/PaintBrushes/LowerSphere.cs | 2 + .../Terrain/PaintBrushes/NoiseSphere.cs | 2 + .../Terrain/PaintBrushes/OlsenSphere.cs | 2 + .../Terrain/PaintBrushes/RaiseSphere.cs | 2 + .../Terrain/PaintBrushes/RevertSphere.cs | 7 +++ .../Terrain/PaintBrushes/SmoothSphere.cs | 47 ++--------------- .../Terrain/PaintBrushes/WeatherSphere.cs | 43 ++-------------- .../Modules/Terrain/TerrainUtil.cs | 51 +++++++++++++++++++ 10 files changed, 79 insertions(+), 122 deletions(-) create mode 100644 OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs index 2d46fd96aa..e67918957a 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs @@ -146,51 +146,14 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes return coord; } - private double SphericalFactor(double x, double y, double rx, double ry, double size) - { - double z = size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry)); - return z; - } - - private double GetBilinearInterpolate(double x, double y, ITerrainChannel map) - { - int w = map.Width; - int h = map.Height; - - if (x > w - 2.0) - x = w - 2.0; - if (y > h - 2.0) - y = h - 2.0; - if (x < 0.0) - x = 0.0; - if (y < 0.0) - y = 0.0; - - int stepSize = 1; - double h00 = map[(int)x, (int)y]; - double h10 = map[(int)x + stepSize, (int)y]; - double h01 = map[(int)x, (int)y + stepSize]; - double h11 = map[(int)x + stepSize, (int)y + stepSize]; - double h1 = h00; - double h2 = h10; - double h3 = h01; - double h4 = h11; - double a00 = h1; - double a10 = h2 - h1; - double a01 = h3 - h1; - double a11 = h1 - h2 - h3 + h4; - double partialx = x - (int)x; - double partialz = y - (int)y; - double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); - return hi; - } - #endregion #region ITerrainPaintableEffect Members public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; // Using one 'rain' round for this, so skipping a useless loop // Will need to adapt back in for the Flood brush @@ -201,7 +164,7 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes // Fill with rain for (x = 0; x < water.Width; x++) for (y = 0; y < water.Height; y++) - water[x, y] = Math.Max(0.0, SphericalFactor(x, y, rx, ry, strength) * rainHeight * duration); + water[x, y] = Math.Max(0.0, TerrainUtil.SphericalFactor(x, y, rx, ry, strength) * rainHeight * duration); for (int i = 0; i < rounds; i++) { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/FlattenSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/FlattenSphere.cs index 0e98111d29..180f37ec93 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/FlattenSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/FlattenSphere.cs @@ -74,6 +74,8 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; double[,] tweak = new double[map.Width, map.Height]; diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/LowerSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/LowerSphere.cs index 2201584f11..3478927491 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/LowerSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/LowerSphere.cs @@ -36,6 +36,8 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; for (x = 0; x < map.Width; x++) { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/NoiseSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/NoiseSphere.cs index 776e31f110..4a03a17211 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/NoiseSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/NoiseSphere.cs @@ -37,6 +37,8 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; for (x = 0; x < map.Width; x++) { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/OlsenSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/OlsenSphere.cs index 8855ce7d9f..65c9b12954 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/OlsenSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/OlsenSphere.cs @@ -159,6 +159,8 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; for (x = 0; x < map.Width; x++) diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RaiseSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RaiseSphere.cs index 5b9f41013e..909184603c 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RaiseSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RaiseSphere.cs @@ -36,6 +36,8 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; for (x = 0; x < map.Width; x++) { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RevertSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RevertSphere.cs index 9d9c462b97..bc98b07c4d 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RevertSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/RevertSphere.cs @@ -43,6 +43,13 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + if (duration > 1.0) + duration = 1.0; + if (duration < 0) + return; + int x, y; for (x = 0; x < map.Width; x++) { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/SmoothSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/SmoothSphere.cs index 90bbafc052..537f961e56 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/SmoothSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/SmoothSphere.cs @@ -31,49 +31,12 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes { public class SmoothSphere : ITerrainPaintableEffect { - private double SphericalFactor(double x, double y, double rx, double ry, double size) - { - double z = size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry)); - return z; - } - - private double GetBilinearInterpolate(double x, double y, ITerrainChannel map) - { - int w = map.Width; - int h = map.Height; - - if (x > w - 2.0) - x = w - 2.0; - if (y > h - 2.0) - y = h - 2.0; - if (x < 0.0) - x = 0.0; - if (y < 0.0) - y = 0.0; - - int stepSize = 1; - double h00 = map[(int)x, (int)y]; - double h10 = map[(int)x + stepSize, (int)y]; - double h01 = map[(int)x, (int)y + stepSize]; - double h11 = map[(int)x + stepSize, (int)y + stepSize]; - double h1 = h00; - double h2 = h10; - double h3 = h01; - double h4 = h11; - double a00 = h1; - double a10 = h2 - h1; - double a01 = h3 - h1; - double a11 = h1 - h2 - h3 + h4; - double partialx = x - (int)x; - double partialz = y - (int)y; - double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); - return hi; - } - #region ITerrainPaintableEffect Members public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; double[,] tweak = new double[map.Width, map.Height]; @@ -86,7 +49,7 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes { for (y = 0; y < map.Height; y++) { - double z = SphericalFactor(x, y, rx, ry, strength); + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); if (z > 0) // add in non-zero amount { @@ -98,7 +61,7 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes for (l = 0.0 - area; l < area; l += step) { avgsteps++; - average += GetBilinearInterpolate(x + n, y + l, map); + average += TerrainUtil.GetBilinearInterpolate(x + n, y + l, map); } } tweak[x, y] = average / avgsteps; @@ -110,7 +73,7 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes { for (y = 0; y < map.Height; y++) { - double z = SphericalFactor(x, y, rx, ry, strength); + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); if (z > 0) // add in non-zero amount { diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/WeatherSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/WeatherSphere.cs index cac3d35c2f..ef0f9fd7e1 100644 --- a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/WeatherSphere.cs +++ b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/WeatherSphere.cs @@ -142,58 +142,21 @@ namespace OpenSim.Region.Environment.Modules.Terrain.PaintBrushes return coord; } - private double SphericalFactor(double x, double y, double rx, double ry, double size) - { - double z = size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry)); - return z; - } - - private double GetBilinearInterpolate(double x, double y, ITerrainChannel map) - { - int w = map.Width; - int h = map.Height; - - if (x > w - 2.0) - x = w - 2.0; - if (y > h - 2.0) - y = h - 2.0; - if (x < 0.0) - x = 0.0; - if (y < 0.0) - y = 0.0; - - int stepSize = 1; - double h00 = map[(int)x, (int)y]; - double h10 = map[(int)x + stepSize, (int)y]; - double h01 = map[(int)x, (int)y + stepSize]; - double h11 = map[(int)x + stepSize, (int)y + stepSize]; - double h1 = h00; - double h2 = h10; - double h3 = h01; - double h4 = h11; - double a00 = h1; - double a10 = h2 - h1; - double a01 = h3 - h1; - double a11 = h1 - h2 - h3 + h4; - double partialx = x - (int)x; - double partialz = y - (int)y; - double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); - return hi; - } - #endregion #region ITerrainPaintableEffect Members public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration) { + strength = TerrainUtil.MetersToSphericalStrength(strength); + int x, y; for (x = 0; x < map.Width; x++) { for (y = 0; y < map.Height; y++) { - double z = SphericalFactor(x, y, rx, ry, strength); + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); if (z > 0) // add in non-zero amount { diff --git a/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs b/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs new file mode 100644 index 0000000000..c52d2c820c --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs @@ -0,0 +1,51 @@ +using System; +using OpenSim.Region.Environment.Interfaces; + +namespace OpenSim.Region.Environment.Modules.Terrain +{ + public static class TerrainUtil + { + public static double MetersToSphericalStrength(double size) + { + return Math.Pow(2, size); + } + + public static double SphericalFactor(double x, double y, double rx, double ry, double size) + { + return size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry)); + } + + public static double GetBilinearInterpolate(double x, double y, ITerrainChannel map) + { + int w = map.Width; + int h = map.Height; + + if (x > w - 2.0) + x = w - 2.0; + if (y > h - 2.0) + y = h - 2.0; + if (x < 0.0) + x = 0.0; + if (y < 0.0) + y = 0.0; + + int stepSize = 1; + double h00 = map[(int)x, (int)y]; + double h10 = map[(int)x + stepSize, (int)y]; + double h01 = map[(int)x, (int)y + stepSize]; + double h11 = map[(int)x + stepSize, (int)y + stepSize]; + double h1 = h00; + double h2 = h10; + double h3 = h01; + double h4 = h11; + double a00 = h1; + double a10 = h2 - h1; + double a01 = h3 - h1; + double a11 = h1 - h2 - h3 + h4; + double partialx = x - (int)x; + double partialz = y - (int)y; + double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); + return hi; + } + } +}