Voronoi.cs 7.5 KB

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