Fracture.cs 5.2 KB

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