OlsenSphere.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.PaintBrushes
  30. {
  31. /// <summary>
  32. /// Speed-Optimised Hybrid Erosion Brush
  33. ///
  34. /// As per Jacob Olsen's Paper
  35. /// http://www.oddlabs.com/download/terrain_generation.pdf
  36. /// </summary>
  37. public class OlsenSphere : ITerrainPaintableEffect
  38. {
  39. private double nConst = 1024.0;
  40. private NeighbourSystem type = NeighbourSystem.Moore; // Parameter
  41. #region Supporting Functions
  42. private int[] Neighbours(NeighbourSystem type, int index)
  43. {
  44. int[] coord = new int[2];
  45. index++;
  46. switch (type)
  47. {
  48. case NeighbourSystem.Moore:
  49. switch (index)
  50. {
  51. case 1:
  52. coord[0] = -1;
  53. coord[1] = -1;
  54. break;
  55. case 2:
  56. coord[0] = -0;
  57. coord[1] = -1;
  58. break;
  59. case 3:
  60. coord[0] = +1;
  61. coord[1] = -1;
  62. break;
  63. case 4:
  64. coord[0] = -1;
  65. coord[1] = -0;
  66. break;
  67. case 5:
  68. coord[0] = -0;
  69. coord[1] = -0;
  70. break;
  71. case 6:
  72. coord[0] = +1;
  73. coord[1] = -0;
  74. break;
  75. case 7:
  76. coord[0] = -1;
  77. coord[1] = +1;
  78. break;
  79. case 8:
  80. coord[0] = -0;
  81. coord[1] = +1;
  82. break;
  83. case 9:
  84. coord[0] = +1;
  85. coord[1] = +1;
  86. break;
  87. default:
  88. break;
  89. }
  90. break;
  91. case NeighbourSystem.VonNeumann:
  92. switch (index)
  93. {
  94. case 1:
  95. coord[0] = 0;
  96. coord[1] = -1;
  97. break;
  98. case 2:
  99. coord[0] = -1;
  100. coord[1] = 0;
  101. break;
  102. case 3:
  103. coord[0] = +1;
  104. coord[1] = 0;
  105. break;
  106. case 4:
  107. coord[0] = 0;
  108. coord[1] = +1;
  109. break;
  110. case 5:
  111. coord[0] = -0;
  112. coord[1] = -0;
  113. break;
  114. default:
  115. break;
  116. }
  117. break;
  118. }
  119. return coord;
  120. }
  121. private double SphericalFactor(double x, double y, double rx, double ry, double size)
  122. {
  123. double z = size * size - ((x - rx) * (x - rx) + (y - ry) * (y - ry));
  124. return z;
  125. }
  126. private enum NeighbourSystem
  127. {
  128. Moore,
  129. VonNeumann
  130. } ;
  131. #endregion
  132. #region ITerrainPaintableEffect Members
  133. public void PaintEffect(ITerrainChannel map, double rx, double ry, double strength, double duration)
  134. {
  135. strength = TerrainUtil.MetersToSphericalStrength(strength);
  136. int x, y;
  137. for (x = 0; x < map.Width; x++)
  138. {
  139. for (y = 0; y < map.Height; y++)
  140. {
  141. double z = SphericalFactor(x, y, rx, ry, strength);
  142. if (z > 0) // add in non-zero amount
  143. {
  144. int NEIGHBOUR_ME = 4;
  145. int NEIGHBOUR_MAX = type == NeighbourSystem.Moore ? 9 : 5;
  146. double max = Double.MinValue;
  147. int loc = 0;
  148. double cellmax = 0;
  149. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  150. {
  151. if (j != NEIGHBOUR_ME)
  152. {
  153. int[] coords = Neighbours(type, j);
  154. coords[0] += x;
  155. coords[1] += y;
  156. if (coords[0] > map.Width - 1)
  157. continue;
  158. if (coords[1] > map.Height - 1)
  159. continue;
  160. if (coords[0] < 0)
  161. continue;
  162. if (coords[1] < 0)
  163. continue;
  164. cellmax = map[x, y] - map[coords[0], coords[1]];
  165. if (cellmax > max)
  166. {
  167. max = cellmax;
  168. loc = j;
  169. }
  170. }
  171. }
  172. double T = nConst / ((map.Width + map.Height) / 2);
  173. // Apply results
  174. if (0 < max && max <= T)
  175. {
  176. int[] maxCoords = Neighbours(type, loc);
  177. double heightDelta = 0.5 * max * z * duration;
  178. map[x, y] -= heightDelta;
  179. map[x + maxCoords[0], y + maxCoords[1]] += heightDelta;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. #endregion
  186. }
  187. }