Common.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. public partial class Channel
  31. {
  32. public int GetWidth()
  33. {
  34. return w;
  35. }
  36. public int GetHeight()
  37. {
  38. return h;
  39. }
  40. public Channel Copy()
  41. {
  42. Channel x = new Channel(w, h);
  43. x.map = (double[,]) map.Clone();
  44. return x;
  45. }
  46. public void SetDiff()
  47. {
  48. SetDiff(1);
  49. }
  50. public void SetDiff(int val)
  51. {
  52. for (int x = 0; x < w/16; x++)
  53. {
  54. for (int y = 0; y < h/16; y++)
  55. {
  56. diff[x, y] = val;
  57. }
  58. }
  59. }
  60. public void SetDiff(int x, int y)
  61. {
  62. diff[x/16, y/16]++;
  63. }
  64. public void Set(int x, int y, double val)
  65. {
  66. if (x >= w)
  67. throw new Exception("Bounds error while setting pixel (width)");
  68. if (y >= h)
  69. throw new Exception("Bounds error while setting pixel (height)");
  70. if (x < 0)
  71. throw new Exception("Bounds error while setting pixel (width)");
  72. if (y < 0)
  73. throw new Exception("Bounds error while setting pixel (height)");
  74. if (map[x, y] != val)
  75. {
  76. SetDiff(x, y);
  77. map[x, y] = val;
  78. }
  79. }
  80. public void SetClip(int x, int y, double val)
  81. {
  82. SetDiff(x, y);
  83. if (x >= w)
  84. throw new Exception("Bounds error while setting pixel (width)");
  85. if (y >= h)
  86. throw new Exception("Bounds error while setting pixel (height)");
  87. if (x < 0)
  88. throw new Exception("Bounds error while setting pixel (width)");
  89. if (y < 0)
  90. throw new Exception("Bounds error while setting pixel (height)");
  91. if (val > 1.0)
  92. val = 1.0;
  93. if (val < 0.0)
  94. val = 0.0;
  95. map[x, y] = val;
  96. }
  97. private double GetBilinearInterpolate(double x, double y)
  98. {
  99. if (x > w - 2.0)
  100. x = w - 2.0;
  101. if (y > h - 2.0)
  102. y = h - 2.0;
  103. if (x < 0.0)
  104. x = 0.0;
  105. if (y < 0.0)
  106. y = 0.0;
  107. int stepSize = 1;
  108. double h00 = Get((int) x, (int) y);
  109. double h10 = Get((int) x + stepSize, (int) y);
  110. double h01 = Get((int) x, (int) y + stepSize);
  111. double h11 = Get((int) x + stepSize, (int) y + stepSize);
  112. double h1 = h00;
  113. double h2 = h10;
  114. double h3 = h01;
  115. double h4 = h11;
  116. double a00 = h1;
  117. double a10 = h2 - h1;
  118. double a01 = h3 - h1;
  119. double a11 = h1 - h2 - h3 + h4;
  120. double partialx = x - (int) x;
  121. double partialz = y - (int) y;
  122. double hi = a00 + (a10*partialx) + (a01*partialz) + (a11*partialx*partialz);
  123. return hi;
  124. }
  125. public double Get(int x, int y)
  126. {
  127. try
  128. {
  129. return map[x, y];
  130. }
  131. catch (IndexOutOfRangeException)
  132. {
  133. if (x >= w)
  134. x = w - 1;
  135. if (y >= h)
  136. y = h - 1;
  137. if (x < 0)
  138. x = 0;
  139. if (y < 0)
  140. y = 0;
  141. return map[x, y];
  142. }
  143. }
  144. public void SetWrap(int x, int y, double val)
  145. {
  146. SetDiff(x, y);
  147. map[x%w, y%h] = val;
  148. }
  149. public void SetWrapClip(int x, int y, double val)
  150. {
  151. SetDiff(x, y);
  152. if (val > 1.0)
  153. val = 1.0;
  154. if (val < 0.0)
  155. val = 0.0;
  156. map[x%w, y%h] = val;
  157. }
  158. public void Fill(double val)
  159. {
  160. SetDiff();
  161. int x, y;
  162. for (x = 0; x < w; x++)
  163. {
  164. for (y = 0; y < h; y++)
  165. {
  166. map[x, y] = val;
  167. }
  168. }
  169. }
  170. public void Fill(double min, double max, double val)
  171. {
  172. SetDiff();
  173. int x, y;
  174. for (x = 0; x < w; x++)
  175. {
  176. for (y = 0; y < h; y++)
  177. {
  178. if (map[x, y] >= min && map[x, y] <= max)
  179. map[x, y] = val;
  180. }
  181. }
  182. }
  183. public double FindMax()
  184. {
  185. int x, y;
  186. double max = double.MinValue;
  187. for (x = 0; x < w; x++)
  188. {
  189. for (y = 0; y < h; y++)
  190. {
  191. if (map[x, y] > max)
  192. max = map[x, y];
  193. }
  194. }
  195. return max;
  196. }
  197. public double FindMin()
  198. {
  199. int x, y;
  200. double min = double.MaxValue;
  201. for (x = 0; x < w; x++)
  202. {
  203. for (y = 0; y < h; y++)
  204. {
  205. if (map[x, y] < min)
  206. min = map[x, y];
  207. }
  208. }
  209. return min;
  210. }
  211. public double Sum()
  212. {
  213. int x, y;
  214. double sum = 0.0;
  215. for (x = 0; x < w; x++)
  216. {
  217. for (y = 0; y < h; y++)
  218. {
  219. sum += map[x, y];
  220. }
  221. }
  222. return sum;
  223. }
  224. public double Avg()
  225. {
  226. return Sum()/(w*h);
  227. }
  228. public bool ContainsNaN()
  229. {
  230. int x, y;
  231. for (x = 0; x < w; x++)
  232. {
  233. for (y = 0; y < h; y++)
  234. {
  235. double elm = map[x, y];
  236. if (Double.IsNaN(elm))
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. }
  243. }