Grid.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. public Channel Normalise()
  34. {
  35. SetDiff();
  36. double max = FindMax();
  37. double min = FindMin();
  38. int x, y;
  39. if (max != min)
  40. {
  41. for (x = 0; x < w; x++)
  42. {
  43. for (y = 0; y < h; y++)
  44. {
  45. map[x, y] = (map[x, y] - min)*(1.0/(max - min));
  46. }
  47. }
  48. }
  49. else
  50. {
  51. Fill(0.5);
  52. }
  53. return this;
  54. }
  55. public Channel Normalise(double minv, double maxv)
  56. {
  57. SetDiff();
  58. if (minv == maxv)
  59. {
  60. Fill(minv);
  61. return this;
  62. }
  63. double max = FindMax();
  64. double min = FindMin();
  65. int x, y;
  66. for (x = 0; x < w; x++)
  67. {
  68. for (y = 0; y < h; y++)
  69. {
  70. if (min != max)
  71. {
  72. double val = (map[x, y] - min)*(1.0/(max - min));
  73. val *= maxv - minv;
  74. val += minv;
  75. map[x, y] = val;
  76. }
  77. else
  78. {
  79. map[x, y] = 0.5;
  80. }
  81. }
  82. }
  83. return this;
  84. }
  85. public Channel Elevate(double meters)
  86. {
  87. SetDiff();
  88. int x, y;
  89. for (x = 0; x < w; x++)
  90. {
  91. for (y = 0; y < h; y++)
  92. {
  93. map[x, y] += meters;
  94. }
  95. }
  96. return this;
  97. }
  98. public Channel Clip()
  99. {
  100. int x, y;
  101. for (x = 0; x < w; x++)
  102. {
  103. for (y = 0; y < h; y++)
  104. {
  105. SetClip(x, y, map[x, y]);
  106. }
  107. }
  108. return this;
  109. }
  110. public Channel Clip(double min, double max)
  111. {
  112. int x, y;
  113. for (x = 0; x < w; x++)
  114. {
  115. for (y = 0; y < h; y++)
  116. {
  117. double val = map[x, y];
  118. if (val > max) val = max;
  119. if (val < min) val = min;
  120. Set(x, y, val);
  121. }
  122. }
  123. return this;
  124. }
  125. public Channel Crop(int x1, int y1, int x2, int y2)
  126. {
  127. int width = x1 - x2 + 1;
  128. int height = y1 - y2 + 1;
  129. Channel chan = new Channel(width, height);
  130. int x, y;
  131. int nx, ny;
  132. nx = 0;
  133. for (x = x1; x < x2; x++)
  134. {
  135. ny = 0;
  136. for (y = y1; y < y2; y++)
  137. {
  138. chan.map[nx, ny] = map[x, y];
  139. ny++;
  140. }
  141. nx++;
  142. }
  143. return this;
  144. }
  145. public Channel AddClip(Channel other)
  146. {
  147. SetDiff();
  148. int x, y;
  149. for (x = 0; x < w; x++)
  150. {
  151. for (y = 0; y < h; y++)
  152. {
  153. map[x, y] = other.map[x, y];
  154. if (map[x, y] > 1)
  155. map[x, y] = 1;
  156. if (map[x, y] < 0)
  157. map[x, y] = 0;
  158. }
  159. }
  160. return this;
  161. }
  162. public void Smooth(double amount)
  163. {
  164. SetDiff();
  165. double area = amount;
  166. double step = amount/4.0;
  167. double[,] manipulate = new double[w,h];
  168. int x, y;
  169. double n, l;
  170. for (x = 0; x < w; x++)
  171. {
  172. for (y = 0; y < h; y++)
  173. {
  174. double average = 0.0;
  175. int avgsteps = 0;
  176. for (n = 0.0 - area; n < area; n += step)
  177. {
  178. for (l = 0.0 - area; l < area; l += step)
  179. {
  180. avgsteps++;
  181. average += GetBilinearInterpolate(x + n, y + l);
  182. }
  183. }
  184. manipulate[x, y] = average/avgsteps;
  185. }
  186. }
  187. map = manipulate;
  188. }
  189. public void Pertubation(double amount)
  190. {
  191. SetDiff();
  192. // Simple pertubation filter
  193. double[,] manipulated = new double[w,h];
  194. Random generator = new Random(seed); // Seeds FTW!
  195. //double amount = 8.0;
  196. int x, y;
  197. for (x = 0; x < w; x++)
  198. {
  199. for (y = 0; y < h; y++)
  200. {
  201. double offset_x = (double) x + (generator.NextDouble()*amount) - (amount/2.0);
  202. double offset_y = (double) y + (generator.NextDouble()*amount) - (amount/2.0);
  203. double p = GetBilinearInterpolate(offset_x, offset_y);
  204. manipulated[x, y] = p;
  205. }
  206. }
  207. map = manipulated;
  208. }
  209. public void PertubationMask(Channel mask)
  210. {
  211. // Simple pertubation filter
  212. double[,] manipulated = new double[w,h];
  213. Random generator = new Random(seed); // Seeds FTW!
  214. //double amount = 8.0;
  215. double amount;
  216. int x, y;
  217. for (x = 0; x < w; x++)
  218. {
  219. for (y = 0; y < h; y++)
  220. {
  221. amount = mask.map[x, y];
  222. double offset_x = (double) x + (generator.NextDouble()*amount) - (amount/2.0);
  223. double offset_y = (double) y + (generator.NextDouble()*amount) - (amount/2.0);
  224. if (offset_x > w)
  225. offset_x = w - 1;
  226. if (offset_y > h)
  227. offset_y = h - 1;
  228. if (offset_y < 0)
  229. offset_y = 0;
  230. if (offset_x < 0)
  231. offset_x = 0;
  232. double p = GetBilinearInterpolate(offset_x, offset_y);
  233. manipulated[x, y] = p;
  234. SetDiff(x, y);
  235. }
  236. }
  237. map = manipulated;
  238. }
  239. public void Distort(Channel mask, double str)
  240. {
  241. // Simple pertubation filter
  242. double[,] manipulated = new double[w,h];
  243. double amount;
  244. int x, y;
  245. for (x = 0; x < w; x++)
  246. {
  247. for (y = 0; y < h; y++)
  248. {
  249. amount = mask.map[x, y];
  250. double offset_x = (double) x + (amount*str) - (0.5*str);
  251. double offset_y = (double) y + (amount*str) - (0.5*str);
  252. if (offset_x > w)
  253. offset_x = w - 1;
  254. if (offset_y > h)
  255. offset_y = h - 1;
  256. if (offset_y < 0)
  257. offset_y = 0;
  258. if (offset_x < 0)
  259. offset_x = 0;
  260. double p = GetBilinearInterpolate(offset_x, offset_y);
  261. manipulated[x, y] = p;
  262. SetDiff(x, y);
  263. }
  264. }
  265. map = manipulated;
  266. }
  267. public void Distort(Channel mask, Channel mask2, double str)
  268. {
  269. // Simple pertubation filter
  270. double[,] manipulated = new double[w,h];
  271. double amountX;
  272. double amountY;
  273. int x, y;
  274. for (x = 0; x < w; x++)
  275. {
  276. for (y = 0; y < h; y++)
  277. {
  278. amountX = mask.map[x, y];
  279. amountY = mask2.map[x, y];
  280. double offset_x = (double) x + (amountX*str) - (0.5*str);
  281. double offset_y = (double) y + (amountY*str) - (0.5*str);
  282. if (offset_x > w)
  283. offset_x = w - 1;
  284. if (offset_y > h)
  285. offset_y = h - 1;
  286. if (offset_y < 0)
  287. offset_y = 0;
  288. if (offset_x < 0)
  289. offset_x = 0;
  290. double p = GetBilinearInterpolate(offset_x, offset_y);
  291. manipulated[x, y] = p;
  292. SetDiff(x, y);
  293. }
  294. }
  295. map = manipulated;
  296. }
  297. public Channel Blend(Channel other, double amount)
  298. {
  299. int x, y;
  300. for (x = 0; x < w; x++)
  301. {
  302. for (y = 0; y < h; y++)
  303. {
  304. Set(x, y, Tools.LinearInterpolate(map[x, y], other.map[x, y], amount));
  305. }
  306. }
  307. return this;
  308. }
  309. public Channel Blend(Channel other, Channel amount)
  310. {
  311. int x, y;
  312. for (x = 0; x < w; x++)
  313. {
  314. for (y = 0; y < h; y++)
  315. {
  316. Set(x, y, Tools.LinearInterpolate(map[x, y], other.map[x, y], amount.map[x, y]));
  317. }
  318. }
  319. return this;
  320. }
  321. }
  322. }