1
0

Common.cs 7.5 KB

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