Operators.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. partial class Channel
  31. {
  32. /* Operator combination of channel datatypes */
  33. public static Channel operator +(Channel A, Channel B)
  34. {
  35. if (A.h != B.h)
  36. throw new Exception("Cannot add heightmaps, of different height.");
  37. if (A.w != B.w)
  38. throw new Exception("Cannot add heightmaps, of different width.");
  39. int x, y;
  40. for (x = 0; x < A.w; x++)
  41. {
  42. for (y = 0; y < A.h; y++)
  43. {
  44. if (B.map[x, y] != 0)
  45. A.SetDiff(x, y);
  46. A.map[x, y] += B.map[x, y];
  47. }
  48. }
  49. return A;
  50. }
  51. public static Channel operator *(Channel A, Channel B)
  52. {
  53. if (A.h != B.h)
  54. throw new Exception("Cannot multiply heightmaps, of different height.");
  55. if (A.w != B.w)
  56. throw new Exception("Cannot multiply heightmaps, of different width.");
  57. int x, y;
  58. for (x = 0; x < A.w; x++)
  59. {
  60. for (y = 0; y < A.h; y++)
  61. {
  62. A.map[x, y] *= B.map[x, y];
  63. }
  64. }
  65. A.SetDiff();
  66. return A;
  67. }
  68. public static Channel operator -(Channel A, Channel B)
  69. {
  70. if (A.h != B.h)
  71. throw new Exception("Cannot subtract heightmaps, of different height.");
  72. if (A.w != B.w)
  73. throw new Exception("Cannot subtract heightmaps, of different width.");
  74. int x, y;
  75. for (x = 0; x < A.w; x++)
  76. {
  77. for (y = 0; y < A.h; y++)
  78. {
  79. if (B.map[x, y] != 0)
  80. A.SetDiff(x, y);
  81. A.map[x, y] -= B.map[x, y];
  82. }
  83. }
  84. return A;
  85. }
  86. public static Channel operator /(Channel A, Channel B)
  87. {
  88. if (A.h != B.h)
  89. throw new Exception("Cannot divide heightmaps, of different height.");
  90. if (A.w != B.w)
  91. throw new Exception("Cannot divide heightmaps, of different width.");
  92. int x, y;
  93. for (x = 0; x < A.w; x++)
  94. {
  95. for (y = 0; y < A.h; y++)
  96. {
  97. A.map[x, y] /= B.map[x, y];
  98. }
  99. }
  100. A.SetDiff();
  101. return A;
  102. }
  103. public static Channel operator ^(Channel A, Channel B)
  104. {
  105. if (A.h != B.h)
  106. throw new Exception("Cannot divide heightmaps, of different height.");
  107. if (A.w != B.w)
  108. throw new Exception("Cannot divide heightmaps, of different width.");
  109. int x, y;
  110. for (x = 0; x < A.w; x++)
  111. {
  112. for (y = 0; y < A.h; y++)
  113. {
  114. A.map[x, y] = Math.Pow(A.map[x, y], B.map[x, y]);
  115. }
  116. }
  117. A.SetDiff();
  118. return A;
  119. }
  120. /* Operator combination of channel and double datatypes */
  121. public static Channel operator +(Channel A, double B)
  122. {
  123. int x, y;
  124. for (x = 0; x < A.w; x++)
  125. {
  126. for (y = 0; y < A.h; y++)
  127. {
  128. A.map[x, y] += B;
  129. }
  130. }
  131. if (B != 0)
  132. A.SetDiff();
  133. return A;
  134. }
  135. public static Channel operator -(Channel A, double B)
  136. {
  137. int x, y;
  138. for (x = 0; x < A.w; x++)
  139. {
  140. for (y = 0; y < A.h; y++)
  141. {
  142. A.map[x, y] -= B;
  143. }
  144. }
  145. if (B != 0)
  146. A.SetDiff();
  147. return A;
  148. }
  149. public static Channel operator *(Channel A, double B)
  150. {
  151. int x, y;
  152. for (x = 0; x < A.w; x++)
  153. {
  154. for (y = 0; y < A.h; y++)
  155. {
  156. A.map[x, y] *= B;
  157. }
  158. }
  159. if (B != 1)
  160. A.SetDiff();
  161. return A;
  162. }
  163. public static Channel operator /(Channel A, double B)
  164. {
  165. int x, y;
  166. for (x = 0; x < A.w; x++)
  167. {
  168. for (y = 0; y < A.h; y++)
  169. {
  170. A.map[x, y] /= B;
  171. }
  172. }
  173. if (B != 1)
  174. A.SetDiff();
  175. return A;
  176. }
  177. public static Channel operator ^(Channel A, double B)
  178. {
  179. int x, y;
  180. for (x = 0; x < A.w; x++)
  181. {
  182. for (y = 0; y < A.h; y++)
  183. {
  184. A.map[x, y] = Math.Pow(A.map[x, y], B);
  185. }
  186. }
  187. A.SetDiff();
  188. return A;
  189. }
  190. }
  191. }