TerrainModifier.cs 14 KB

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