Operators.cs 6.5 KB

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