TerrainModifier.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 OpenSimulator 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. using System.Reflection;
  29. using log4net;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Interfaces;
  32. namespace OpenSim.Region.CoreModules.World.Terrain
  33. {
  34. public abstract class TerrainModifier : ITerrainModifier
  35. {
  36. protected ITerrainModule m_module;
  37. protected static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. protected TerrainModifier(ITerrainModule module)
  39. {
  40. m_module = module;
  41. }
  42. public abstract string ModifyTerrain(ITerrainChannel map, string[] args);
  43. public abstract string GetUsage();
  44. public abstract float operate(float[,] map, TerrainModifierData data, int x, int y);
  45. protected String parseParameters(string[] args, out TerrainModifierData data)
  46. {
  47. string val;
  48. string arg;
  49. string result;
  50. data = new TerrainModifierData();
  51. data.shape = String.Empty;
  52. data.bevel = String.Empty;
  53. data.dx = 0;
  54. data.dy = 0;
  55. if (args.Length < 4)
  56. {
  57. result = "Usage: " + GetUsage();
  58. }
  59. else
  60. {
  61. result = this.parseFloat(args[3], out data.elevation);
  62. }
  63. if (result.Length == 0)
  64. {
  65. int index = 3;
  66. while(++index < args.Length && result.Length == 0)
  67. {
  68. arg = args[index];
  69. // check for shape
  70. if (arg.StartsWith("-rec=") || arg.StartsWith("-ell="))
  71. {
  72. if (data.shape != String.Empty)
  73. {
  74. result = "Only 1 '-rec' or '-ell' parameter is permitted.";
  75. }
  76. else
  77. {
  78. data.shape = arg.StartsWith("-ell=") ? "ellipse" : "rectangle";
  79. val = arg.Substring(arg.IndexOf("=") + 1);
  80. string[] coords = val.Split(Util.SplitCommaArray);
  81. if ((coords.Length < 3) || (coords.Length > 4))
  82. {
  83. result = String.Format("Bad format for shape parameter {0}", arg);
  84. }
  85. else
  86. {
  87. result = this.parseInt(coords[0], out data.x0);
  88. if (result.Length == 0)
  89. {
  90. result = this.parseInt(coords[1], out data.y0);
  91. }
  92. if (result.Length == 0)
  93. {
  94. result = this.parseInt(coords[2], out data.dx);
  95. }
  96. if (result.Length == 0)
  97. {
  98. if (coords.Length == 4)
  99. {
  100. result = this.parseInt(coords[3], out data.dy);
  101. }
  102. else
  103. {
  104. data.dy = data.dx;
  105. }
  106. }
  107. if (result.Length == 0)
  108. {
  109. if ((data.dx <= 0) || (data.dy <= 0))
  110. {
  111. result = "Shape sizes must be positive integers";
  112. }
  113. }
  114. else
  115. {
  116. result = String.Format("Bad value in shape parameters {0}", arg);
  117. }
  118. }
  119. }
  120. }
  121. else if (arg.StartsWith("-taper="))
  122. {
  123. if (data.bevel != String.Empty)
  124. {
  125. result = "Only 1 '-taper' parameter is permitted.";
  126. }
  127. else
  128. {
  129. data.bevel = "taper";
  130. val = arg.Substring(arg.IndexOf("=") + 1);
  131. result = this.parseFloat(val, out data.bevelevation);
  132. if (result != String.Empty)
  133. {
  134. result = String.Format("Bad format for taper parameter {0}", arg);
  135. }
  136. }
  137. }
  138. else
  139. {
  140. result = String.Format("Unrecognized parameter {0}", arg);
  141. }
  142. }
  143. }
  144. return result;
  145. }
  146. protected string parseFloat(String s, out float f)
  147. {
  148. if (float.TryParse(s, out f))
  149. return string.Empty;
  150. f = -1.0f;
  151. return string.Format("{0} is invalid", s);
  152. }
  153. protected string parseInt(String s, out int i)
  154. {
  155. if (Int32.TryParse(s, out i))
  156. return string.Empty;
  157. return string.Format("{0} is invalid", s);
  158. }
  159. protected void applyModification(ITerrainChannel map, TerrainModifierData data)
  160. {
  161. bool[,] mask;
  162. int xMax;
  163. int yMax;
  164. int xMid;
  165. int yMid;
  166. if (data.shape == "ellipse")
  167. {
  168. mask = this.ellipticalMask(data.dx, data.dy);
  169. xMax = mask.GetLength(0);
  170. yMax = mask.GetLength(1);
  171. xMid = xMax / 2 + xMax % 2;
  172. yMid = yMax / 2 + yMax % 2;
  173. }
  174. else
  175. {
  176. mask = this.rectangularMask(data.dx, data.dy);
  177. xMax = mask.GetLength(0);
  178. yMax = mask.GetLength(1);
  179. xMid = 0;
  180. yMid = 0;
  181. }
  182. // m_log.DebugFormat("Apply {0} mask {1}x{2} @ {3},{4}", data.shape, xMax, yMax, xMid, yMid);
  183. float[,] buffer = new float[map.Width, map.Height];
  184. for (int x = data.x0; x < xMax; ++x)
  185. for (int y = data.y0; y < yMax; ++y)
  186. buffer[x,y] = map[x,y];
  187. int yDim = yMax;
  188. while(--yDim >= 0)
  189. {
  190. int yPos = data.y0 + yDim - yMid;
  191. if ((yPos >= 0) && (yPos < map.Height))
  192. {
  193. int xDim = xMax;
  194. while(--xDim >= 0)
  195. {
  196. int xPos = data.x0 + xDim - xMid;
  197. if ((xPos >= 0) && (xPos < map.Width) && (mask[xDim, yDim]))
  198. {
  199. double endElevation = this.operate(buffer, data, xPos, yPos);
  200. map[xPos, yPos] = (float)endElevation;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. protected float computeBevel(TerrainModifierData data, int x, int y)
  207. {
  208. int deltaX;
  209. int deltaY;
  210. int xMax;
  211. int yMax;
  212. float factor;
  213. if (data.bevel == "taper")
  214. {
  215. if (data.shape == "ellipse")
  216. {
  217. deltaX = x - data.x0;
  218. deltaY = y - data.y0;
  219. xMax = data.dx;
  220. yMax = data.dy;
  221. factor = ((deltaX * deltaX) + (deltaY * deltaY));
  222. factor /= ((xMax * xMax) + (yMax * yMax));
  223. }
  224. else
  225. {
  226. // pyramid
  227. xMax = data.dx / 2 + data.dx % 2;
  228. yMax = data.dy / 2 + data.dy % 2;
  229. deltaX = Math.Abs(data.x0 + xMax - x);
  230. deltaY = Math.Abs(data.y0 + yMax - y);
  231. factor = Math.Max(((float)(deltaY) / yMax), ((float)(deltaX) / xMax));
  232. }
  233. }
  234. else
  235. {
  236. factor = 0.0f;
  237. }
  238. return factor;
  239. }
  240. private bool[,] rectangularMask(int xSize, int ySize)
  241. {
  242. bool[,] mask = new bool[xSize, ySize];
  243. int yPos = ySize;
  244. while(--yPos >= 0)
  245. {
  246. int xPos = xSize;
  247. while(--xPos >= 0)
  248. {
  249. mask[xPos, yPos] = true;
  250. }
  251. }
  252. return mask;
  253. }
  254. /*
  255. * Fast ellipse-based derivative of Bresenham algorithm.
  256. * https://web.archive.org/web/20120225095359/http://homepage.smc.edu/kennedy_john/belipse.pdf
  257. */
  258. private bool[,] ellipticalMask(int xRadius, int yRadius)
  259. {
  260. long twoASquared = 2L * xRadius * xRadius;
  261. long twoBSquared = 2L * yRadius * yRadius;
  262. bool[,] mask = new bool[2 * xRadius + 1, 2 * yRadius + 1];
  263. long ellipseError = 0L;
  264. long stoppingX = twoBSquared * xRadius;
  265. long stoppingY = 0L;
  266. long xChange = yRadius * yRadius * (1L - 2L * xRadius);
  267. long yChange = xRadius * xRadius;
  268. int xPos = xRadius;
  269. int yPos = 0;
  270. // first set of points
  271. while(stoppingX >= stoppingY)
  272. {
  273. int yUpper = yRadius + yPos;
  274. int yLower = yRadius - yPos;
  275. // fill in the mask
  276. int xNow = xPos;
  277. while(xNow >= 0)
  278. {
  279. mask[xRadius + xNow, yUpper] = true;
  280. mask[xRadius - xNow, yUpper] = true;
  281. mask[xRadius + xNow, yLower] = true;
  282. mask[xRadius - xNow, yLower] = true;
  283. --xNow;
  284. }
  285. yPos++;
  286. stoppingY += twoASquared;
  287. ellipseError += yChange;
  288. yChange += twoASquared;
  289. if ((2L * ellipseError + xChange) > 0L)
  290. {
  291. xPos--;
  292. stoppingX -= twoBSquared;
  293. ellipseError += xChange;
  294. xChange += twoBSquared;
  295. }
  296. }
  297. // second set of points
  298. xPos = 0;
  299. yPos = yRadius;
  300. xChange = yRadius * yRadius;
  301. yChange = xRadius * xRadius * (1L - 2L * yRadius);
  302. ellipseError = 0L;
  303. stoppingX = 0L;
  304. stoppingY = twoASquared * yRadius;
  305. while(stoppingX <= stoppingY)
  306. {
  307. int xUpper = xRadius + xPos;
  308. int xLower = xRadius - xPos;
  309. // fill in the mask
  310. int yNow = yPos;
  311. while(yNow >= 0)
  312. {
  313. mask[xUpper, yRadius + yNow] = true;
  314. mask[xUpper, yRadius - yNow] = true;
  315. mask[xLower, yRadius + yNow] = true;
  316. mask[xLower, yRadius - yNow] = true;
  317. --yNow;
  318. }
  319. xPos++;
  320. stoppingX += twoBSquared;
  321. ellipseError += xChange;
  322. xChange += twoBSquared;
  323. if ((2L * ellipseError + yChange) > 0L)
  324. {
  325. yPos--;
  326. stoppingY -= twoASquared;
  327. ellipseError += yChange;
  328. yChange += twoASquared;
  329. }
  330. }
  331. return mask;
  332. }
  333. }
  334. }