Raise.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. namespace libTerrain
  29. {
  30. partial class Channel
  31. {
  32. /// <summary>
  33. /// Raises land around the selection
  34. /// </summary>
  35. /// <param name="rx">The center the X coordinate of where you wish to raise the land</param>
  36. /// <param name="ry">The center the Y coordinate of where you wish to raise the land</param>
  37. /// <param name="size">The radius of the dimple</param>
  38. /// <param name="amount">How much impact to add to the terrain (0..2 usually)</param>
  39. public void Raise(double rx, double ry, double size, double amount)
  40. {
  41. RaiseSphere(rx, ry, size, amount);
  42. }
  43. /// <summary>
  44. /// Raises land in a sphere around the selection
  45. /// </summary>
  46. /// <param name="rx">The center the X coordinate of where you wish to raise the land</param>
  47. /// <param name="ry">The center the Y coordinate of where you wish to raise the land</param>
  48. /// <param name="size">The radius of the sphere dimple</param>
  49. /// <param name="amount">How much impact to add to the terrain (0..2 usually)</param>
  50. public void RaiseSphere(double rx, double ry, double size, double amount)
  51. {
  52. int x, y;
  53. for (x = 0; x < w; x++)
  54. {
  55. for (y = 0; y < h; y++)
  56. {
  57. double z = size;
  58. z *= z;
  59. z -= ((x - rx)*(x - rx)) + ((y - ry)*(y - ry));
  60. if (z > 0.0)
  61. Set(x, y, map[x, y] + (z * amount));
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Raises land in a cone around the selection
  67. /// </summary>
  68. /// <param name="rx">The center the X coordinate of where you wish to raise the land</param>
  69. /// <param name="ry">The center the Y coordinate of where you wish to raise the land</param>
  70. /// <param name="size">The radius of the cone</param>
  71. /// <param name="amount">How much impact to add to the terrain (0..2 usually)</param>
  72. public void RaiseCone(double rx, double ry, double size, double amount)
  73. {
  74. int x, y;
  75. for (x = 0; x < w; x++)
  76. {
  77. for (y = 0; y < h; y++)
  78. {
  79. double z = size;
  80. z -= Math.Sqrt(((x - rx)*(x - rx)) + ((y - ry)*(y - ry)));
  81. if (z > 0.0)
  82. Set(x, y, map[x, y] + (z * amount));
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// Lowers land in a sphere around the selection
  88. /// </summary>
  89. /// <param name="rx">The center the X coordinate of where you wish to lower the land</param>
  90. /// <param name="ry">The center the Y coordinate of where you wish to lower the land</param>
  91. /// <param name="size">The radius of the sphere dimple</param>
  92. /// <param name="amount">How much impact to remove from the terrain (0..2 usually)</param>
  93. public void Lower(double rx, double ry, double size, double amount)
  94. {
  95. LowerSphere(rx, ry, size, amount);
  96. }
  97. /// <summary>
  98. /// Lowers land in a sphere around the selection
  99. /// </summary>
  100. /// <param name="rx">The center the X coordinate of where you wish to lower the land</param>
  101. /// <param name="ry">The center the Y coordinate of where you wish to lower the land</param>
  102. /// <param name="size">The radius of the sphere dimple</param>
  103. /// <param name="amount">How much impact to remove from the terrain (0..2 usually)</param>
  104. public void LowerSphere(double rx, double ry, double size, double amount)
  105. {
  106. int x, y;
  107. for (x = 0; x < w; x++)
  108. {
  109. for (y = 0; y < h; y++)
  110. {
  111. double z = size;
  112. z *= z;
  113. z -= ((x - rx)*(x - rx)) + ((y - ry)*(y - ry));
  114. if (z > 0.0)
  115. Set(x, y, map[x, y] - (z * amount));
  116. }
  117. }
  118. }
  119. public double SphericalFactor(double x, double y, double rx, double ry, double size)
  120. {
  121. double z = size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry));
  122. return z;
  123. }
  124. public void SmoothRegion(double rx, double ry, double size, double amount)
  125. {
  126. int x, y;
  127. double[,] tweak = new double[w, h];
  128. double n, l;
  129. double area = size;
  130. double step = size / 4.0;
  131. // compute delta map
  132. for (x = 0; x < w; x++)
  133. {
  134. for (y = 0; y < h; y++)
  135. {
  136. double z = SphericalFactor(x, y, rx, ry, size);
  137. if (z > 0) // add in non-zero amount
  138. {
  139. double average = 0.0;
  140. int avgsteps = 0;
  141. for (n = 0.0 - area; n < area; n += step)
  142. {
  143. for (l = 0.0 - area; l < area; l += step)
  144. {
  145. avgsteps++;
  146. average += GetBilinearInterpolate(x + n, y + l);
  147. }
  148. }
  149. tweak[x, y] = average / avgsteps;
  150. //if (x == rx && y == ry)
  151. // Console.WriteLine("tweak[ " + x + " , " + y + " ] = " + tweak[x, y]);
  152. }
  153. }
  154. }
  155. // blend in map
  156. for (x = 0; x < w; x++)
  157. {
  158. for (y = 0; y < h; y++)
  159. {
  160. double z = SphericalFactor(x, y, rx, ry, size);
  161. if (z > 0) // add in non-zero amount
  162. {
  163. double da = z * amount;
  164. double a = (map[x, y] - tweak[x, y]) * da;
  165. double newz = map[x, y] - a;
  166. //if (rx == x || ry == y)
  167. // Console.WriteLine("map[ " + x + " , " + y + " ] = " + map[x, y] + " tweak, a , da, z, size, amount = " + tweak[x, y] + " " + a + " " + da + " " + z + " " + size + " " + amount);
  168. if (newz > 0.0)
  169. Set(x, y, newz);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }