Voronoi.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 System.Collections.Generic;
  29. namespace libTerrain
  30. {
  31. partial class Channel
  32. {
  33. /// <summary>
  34. /// Generates a Voronoi diagram (sort of a stained glass effect) which will fill the entire channel
  35. /// </summary>
  36. /// <remarks>3-Clause BSD Licensed</remarks>
  37. /// <param name="pointsPerBlock">The number of generator points in each block</param>
  38. /// <param name="blockSize">A multiple of the channel width and height which will have voronoi points generated in it.
  39. /// <para>This is to ensure a more even distribution of the points than pure random allocation.</para></param>
  40. /// <param name="c">The Voronoi diagram type. Usually an array with values consisting of [-1,1]. Experiment with the chain, you can have as many values as you like.</param>
  41. public void VoronoiDiagram(int pointsPerBlock, int blockSize, double[] c)
  42. {
  43. SetDiff();
  44. List<Point2D> points = new List<Point2D>();
  45. Random generator = new Random(seed);
  46. // Generate the emitter points
  47. int x, y, i;
  48. for (x = -blockSize; x < w + blockSize; x += blockSize)
  49. {
  50. for (y = -blockSize; y < h + blockSize; y += blockSize)
  51. {
  52. for (i = 0; i < pointsPerBlock; i++)
  53. {
  54. double pX = x + (generator.NextDouble()*(double) blockSize);
  55. double pY = y + (generator.NextDouble()*(double) blockSize);
  56. points.Add(new Point2D(pX, pY));
  57. }
  58. }
  59. }
  60. double[] distances = new double[points.Count];
  61. // Calculate the distance each pixel is from an emitter
  62. for (x = 0; x < w; x++)
  63. {
  64. for (y = 0; y < h; y++)
  65. {
  66. for (i = 0; i < points.Count; i++)
  67. {
  68. double dx, dy;
  69. dx = Math.Abs((double) x - points[i].x);
  70. dy = Math.Abs((double) y - points[i].y);
  71. distances[i] = (dx*dx + dy*dy);
  72. }
  73. Array.Sort(distances);
  74. double f = 0.0;
  75. // Multiply the distances with their 'c' counterpart
  76. // ordering the distances descending
  77. for (i = 0; i < c.Length; i++)
  78. {
  79. if (i >= points.Count)
  80. break;
  81. f += c[i]*distances[i];
  82. }
  83. map[x, y] = f;
  84. }
  85. }
  86. // Normalise the result
  87. Normalise();
  88. }
  89. public void VoronoiDiagram(List<Point2D> points, double[] c)
  90. {
  91. SetDiff();
  92. int x, y, i;
  93. double[] distances = new double[points.Count];
  94. // Calculate the distance each pixel is from an emitter
  95. for (x = 0; x < w; x++)
  96. {
  97. for (y = 0; y < h; y++)
  98. {
  99. for (i = 0; i < points.Count; i++)
  100. {
  101. double dx, dy;
  102. dx = Math.Abs((double) x - points[i].x);
  103. dy = Math.Abs((double) y - points[i].y);
  104. distances[i] = (dx*dx + dy*dy);
  105. }
  106. Array.Sort(distances);
  107. double f = 0.0;
  108. // Multiply the distances with their 'c' counterpart
  109. // ordering the distances descending
  110. for (i = 0; i < c.Length; i++)
  111. {
  112. if (i >= points.Count)
  113. break;
  114. f += c[i]*distances[i];
  115. }
  116. map[x, y] = f;
  117. }
  118. }
  119. // Normalise the result
  120. Normalise();
  121. }
  122. public void VoroflatDiagram(int pointsPerBlock, int blockSize)
  123. {
  124. SetDiff();
  125. List<Point2D> points = new List<Point2D>();
  126. Random generator = new Random(seed);
  127. // Generate the emitter points
  128. int x, y, i;
  129. for (x = -blockSize; x < w + blockSize; x += blockSize)
  130. {
  131. for (y = -blockSize; y < h + blockSize; y += blockSize)
  132. {
  133. for (i = 0; i < pointsPerBlock; i++)
  134. {
  135. double pX = x + (generator.NextDouble()*(double) blockSize);
  136. double pY = y + (generator.NextDouble()*(double) blockSize);
  137. points.Add(new Point2D(pX, pY));
  138. }
  139. }
  140. }
  141. double[] distances = new double[points.Count];
  142. // Calculate the distance each pixel is from an emitter
  143. for (x = 0; x < w; x++)
  144. {
  145. for (y = 0; y < h; y++)
  146. {
  147. for (i = 0; i < points.Count; i++)
  148. {
  149. double dx, dy;
  150. dx = Math.Abs((double) x - points[i].x);
  151. dy = Math.Abs((double) y - points[i].y);
  152. distances[i] = (dx*dx + dy*dy);
  153. }
  154. //Array.Sort(distances);
  155. double f = 0.0;
  156. double min = double.MaxValue;
  157. for (int j = 0; j < distances.Length; j++)
  158. {
  159. if (distances[j] < min)
  160. {
  161. min = distances[j];
  162. f = j;
  163. }
  164. }
  165. // Multiply the distances with their 'c' counterpart
  166. // ordering the distances descending
  167. map[x, y] = f;
  168. }
  169. }
  170. // Normalise the result
  171. Normalise();
  172. }
  173. }
  174. }