From 8e27656fcc0331c8521e4ef8e8ece4495f1f32ae Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Wed, 12 Mar 2008 11:02:30 +0000 Subject: * 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. --- .../Modules/Terrain/PaintBrushes/ErodeSphere.cs | 43 ++---------------- .../Modules/Terrain/PaintBrushes/FlattenSphere.cs | 2 + .../Modules/Terrain/PaintBrushes/LowerSphere.cs | 2 + .../Modules/Terrain/PaintBrushes/NoiseSphere.cs | 2 + .../Modules/Terrain/PaintBrushes/OlsenSphere.cs | 2 + .../Modules/Terrain/PaintBrushes/RaiseSphere.cs | 2 + .../Modules/Terrain/PaintBrushes/RevertSphere.cs | 7 +++ .../Modules/Terrain/PaintBrushes/SmoothSphere.cs | 47 +++----------------- .../Modules/Terrain/PaintBrushes/WeatherSphere.cs | 43 ++---------------- .../Environment/Modules/Terrain/TerrainUtil.cs | 51 ++++++++++++++++++++++ 10 files changed, 79 insertions(+), 122 deletions(-) create mode 100644 OpenSim/Region/Environment/Modules/Terrain/TerrainUtil.cs (limited to 'OpenSim/Region') diff --git a/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs b/OpenSim/Region/Environment/Modules/Terrain/PaintBrushes/ErodeSphere.cs index 2d46fd9..e679189 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 0e98111..180f37e 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 2201584..3478927 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 776e31f..4a03a17 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 8855ce7..65c9b12 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 5b9f410..9091846 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 9d9c462..bc98b07 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 90bbafc..537f961 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 cac3d35..ef0f9fd 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 0000000..c52d2c8 --- /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; + } + } +} -- cgit v1.1