TerrainUtil.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using OpenSim.Region.Environment.Interfaces;
  29. namespace OpenSim.Region.Environment.Modules.World.Terrain
  30. {
  31. public static class TerrainUtil
  32. {
  33. public static double MetersToSphericalStrength(double size)
  34. {
  35. return Math.Pow(2, size);
  36. }
  37. public static double SphericalFactor(double x, double y, double rx, double ry, double size)
  38. {
  39. return size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry));
  40. }
  41. public static double GetBilinearInterpolate(double x, double y, ITerrainChannel map)
  42. {
  43. int w = map.Width;
  44. int h = map.Height;
  45. if (x > w - 2.0)
  46. x = w - 2.0;
  47. if (y > h - 2.0)
  48. y = h - 2.0;
  49. if (x < 0.0)
  50. x = 0.0;
  51. if (y < 0.0)
  52. y = 0.0;
  53. int stepSize = 1;
  54. double h00 = map[(int) x, (int) y];
  55. double h10 = map[(int) x + stepSize, (int) y];
  56. double h01 = map[(int) x, (int) y + stepSize];
  57. double h11 = map[(int) x + stepSize, (int) y + stepSize];
  58. double h1 = h00;
  59. double h2 = h10;
  60. double h3 = h01;
  61. double h4 = h11;
  62. double a00 = h1;
  63. double a10 = h2 - h1;
  64. double a01 = h3 - h1;
  65. double a11 = h1 - h2 - h3 + h4;
  66. double partialx = x - (int) x;
  67. double partialz = y - (int) y;
  68. double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz);
  69. return hi;
  70. }
  71. private static double Noise(double x, double y)
  72. {
  73. int n = (int) x + (int) (y * 749);
  74. n = (n << 13) ^ n;
  75. return (1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);
  76. }
  77. private static double SmoothedNoise1(double x, double y)
  78. {
  79. double corners = (Noise(x - 1, y - 1) + Noise(x + 1, y - 1) + Noise(x - 1, y + 1) + Noise(x + 1, y + 1)) / 16;
  80. double sides = (Noise(x - 1, y) + Noise(x + 1, y) + Noise(x, y - 1) + Noise(x, y + 1)) / 8;
  81. double center = Noise(x, y) / 4;
  82. return corners + sides + center;
  83. }
  84. private static double Interpolate(double x, double y, double z)
  85. {
  86. return (x * (1.0 - z)) + (y * z);
  87. }
  88. private static double InterpolatedNoise(double x, double y)
  89. {
  90. int integer_X = (int) (x);
  91. double fractional_X = x - integer_X;
  92. int integer_Y = (int) y;
  93. double fractional_Y = y - integer_Y;
  94. double v1 = SmoothedNoise1(integer_X, integer_Y);
  95. double v2 = SmoothedNoise1(integer_X + 1, integer_Y);
  96. double v3 = SmoothedNoise1(integer_X, integer_Y + 1);
  97. double v4 = SmoothedNoise1(integer_X + 1, integer_Y + 1);
  98. double i1 = Interpolate(v1, v2, fractional_X);
  99. double i2 = Interpolate(v3, v4, fractional_X);
  100. return Interpolate(i1, i2, fractional_Y);
  101. }
  102. public static double PerlinNoise2D(double x, double y, int octaves, double persistence)
  103. {
  104. double frequency = 0.0;
  105. double amplitude = 0.0;
  106. double total = 0.0;
  107. for (int i = 0; i < octaves; i++)
  108. {
  109. frequency = Math.Pow(2, i);
  110. amplitude = Math.Pow(persistence, i);
  111. total += InterpolatedNoise(x * frequency, y * frequency) * amplitude;
  112. }
  113. return total;
  114. }
  115. }
  116. }