Fracture.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. */
  28. using System;
  29. namespace libTerrain
  30. {
  31. partial class Channel
  32. {
  33. /// <summary>
  34. /// Produces a set of coordinates defined by an edge point. Eg - 0 = 0,0. 256 = 0,256. 512 = 256,256
  35. /// Assumes a 256^2 heightmap. This needs fixing for input values of w,h
  36. /// </summary>
  37. /// <param name="val"></param>
  38. /// <param name="w"></param>
  39. /// <param name="h"></param>
  40. /// <returns></returns>
  41. private int[] RadialEdge256(int val)
  42. {
  43. // Four cases:
  44. // 1. 000..255 return 0,val
  45. // 2. 256..511 return val - 256,255
  46. // 3. 512..767 return 255, val - 511
  47. // 4. 768..1023 return val - 768,0
  48. int[] ret = new int[2];
  49. if (val < 256)
  50. {
  51. ret[0] = 0;
  52. ret[1] = val;
  53. return ret;
  54. }
  55. if (val < 512)
  56. {
  57. ret[0] = (val%256);
  58. ret[1] = 255;
  59. return ret;
  60. }
  61. if (val < 768)
  62. {
  63. ret[0] = 255;
  64. ret[1] = 255 - (val%256);
  65. return ret;
  66. }
  67. if (val < 1024)
  68. {
  69. ret[0] = 255 - (val%256);
  70. ret[1] = 255;
  71. return ret;
  72. }
  73. throw new Exception("Out of bounds parameter (val)");
  74. }
  75. public void Fracture(int number, double scalemin, double scalemax)
  76. {
  77. SetDiff();
  78. Random rand = new Random(seed);
  79. for (int i = 0; i < number; i++)
  80. {
  81. int[] a, b;
  82. a = RadialEdge256(rand.Next(1023)); // TODO: Broken
  83. b = RadialEdge256(rand.Next(1023)); // TODO: Broken
  84. double z = rand.NextDouble();
  85. double u = rand.NextDouble();
  86. double v = rand.NextDouble();
  87. for (int x = 0; x < w; x++)
  88. {
  89. for (int y = 0; y < h; y++)
  90. {
  91. double miny = Tools.LinearInterpolate(a[1], b[1], (double) x/(double) w);
  92. if (v >= 0.5)
  93. {
  94. if (u >= 0.5)
  95. {
  96. if (y > miny)
  97. {
  98. map[x, y] += Tools.LinearInterpolate(scalemin, scalemax, z);
  99. }
  100. }
  101. else
  102. {
  103. if (y < miny)
  104. {
  105. map[x, y] += Tools.LinearInterpolate(scalemin, scalemax, z);
  106. }
  107. }
  108. }
  109. else
  110. {
  111. if (u >= 0.5)
  112. {
  113. if (x > miny)
  114. {
  115. map[x, y] += Tools.LinearInterpolate(scalemin, scalemax, z);
  116. }
  117. }
  118. else
  119. {
  120. if (x < miny)
  121. {
  122. map[x, y] += Tools.LinearInterpolate(scalemin, scalemax, z);
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. Normalise();
  130. }
  131. }
  132. }