TerrainModifier.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 float operate(float[,] 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. float d;
  149. if (float.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. float[,] buffer = new float[map.Width, map.Height];
  207. for (int x = data.x0; x < xMax; ++x)
  208. for (int y = data.y0; y < yMax; ++y)
  209. buffer[x,y] = map[x,y];
  210. int yDim = yMax;
  211. while(--yDim >= 0)
  212. {
  213. int yPos = data.y0 + yDim - yMid;
  214. if ((yPos >= 0) && (yPos < map.Height))
  215. {
  216. int xDim = xMax;
  217. while(--xDim >= 0)
  218. {
  219. int xPos = data.x0 + xDim - xMid;
  220. if ((xPos >= 0) && (xPos < map.Width) && (mask[xDim, yDim]))
  221. {
  222. double endElevation = this.operate(buffer, data, xPos, yPos);
  223. map[xPos, yPos] = (float)endElevation;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. protected float computeBevel(TerrainModifierData data, int x, int y)
  230. {
  231. int deltaX;
  232. int deltaY;
  233. int xMax;
  234. int yMax;
  235. float factor;
  236. if (data.bevel == "taper")
  237. {
  238. if (data.shape == "ellipse")
  239. {
  240. deltaX = x - data.x0;
  241. deltaY = y - data.y0;
  242. xMax = data.dx;
  243. yMax = data.dy;
  244. factor = ((deltaX * deltaX) + (deltaY * deltaY));
  245. factor /= ((xMax * xMax) + (yMax * yMax));
  246. }
  247. else
  248. {
  249. // pyramid
  250. xMax = data.dx / 2 + data.dx % 2;
  251. yMax = data.dy / 2 + data.dy % 2;
  252. deltaX = Math.Abs(data.x0 + xMax - x);
  253. deltaY = Math.Abs(data.y0 + yMax - y);
  254. factor = Math.Max(((float)(deltaY) / yMax), ((float)(deltaX) / xMax));
  255. }
  256. }
  257. else
  258. {
  259. factor = 0.0f;
  260. }
  261. return factor;
  262. }
  263. private bool[,] rectangularMask(int xSize, int ySize)
  264. {
  265. bool[,] mask = new bool[xSize, ySize];
  266. int yPos = ySize;
  267. while(--yPos >= 0)
  268. {
  269. int xPos = xSize;
  270. while(--xPos >= 0)
  271. {
  272. mask[xPos, yPos] = true;
  273. }
  274. }
  275. return mask;
  276. }
  277. /*
  278. * Fast ellipse-based derivative of Bresenham algorithm.
  279. * https://web.archive.org/web/20120225095359/http://homepage.smc.edu/kennedy_john/belipse.pdf
  280. */
  281. private bool[,] ellipticalMask(int xRadius, int yRadius)
  282. {
  283. long twoASquared = 2L * xRadius * xRadius;
  284. long twoBSquared = 2L * yRadius * yRadius;
  285. bool[,] mask = new bool[2 * xRadius + 1, 2 * yRadius + 1];
  286. long ellipseError = 0L;
  287. long stoppingX = twoBSquared * xRadius;
  288. long stoppingY = 0L;
  289. long xChange = yRadius * yRadius * (1L - 2L * xRadius);
  290. long yChange = xRadius * xRadius;
  291. int xPos = xRadius;
  292. int yPos = 0;
  293. // first set of points
  294. while(stoppingX >= stoppingY)
  295. {
  296. int yUpper = yRadius + yPos;
  297. int yLower = yRadius - yPos;
  298. // fill in the mask
  299. int xNow = xPos;
  300. while(xNow >= 0)
  301. {
  302. mask[xRadius + xNow, yUpper] = true;
  303. mask[xRadius - xNow, yUpper] = true;
  304. mask[xRadius + xNow, yLower] = true;
  305. mask[xRadius - xNow, yLower] = true;
  306. --xNow;
  307. }
  308. yPos++;
  309. stoppingY += twoASquared;
  310. ellipseError += yChange;
  311. yChange += twoASquared;
  312. if ((2L * ellipseError + xChange) > 0L)
  313. {
  314. xPos--;
  315. stoppingX -= twoBSquared;
  316. ellipseError += xChange;
  317. xChange += twoBSquared;
  318. }
  319. }
  320. // second set of points
  321. xPos = 0;
  322. yPos = yRadius;
  323. xChange = yRadius * yRadius;
  324. yChange = xRadius * xRadius * (1L - 2L * yRadius);
  325. ellipseError = 0L;
  326. stoppingX = 0L;
  327. stoppingY = twoASquared * yRadius;
  328. while(stoppingX <= stoppingY)
  329. {
  330. int xUpper = xRadius + xPos;
  331. int xLower = xRadius - xPos;
  332. // fill in the mask
  333. int yNow = yPos;
  334. while(yNow >= 0)
  335. {
  336. mask[xUpper, yRadius + yNow] = true;
  337. mask[xUpper, yRadius - yNow] = true;
  338. mask[xLower, yRadius + yNow] = true;
  339. mask[xLower, yRadius - yNow] = true;
  340. --yNow;
  341. }
  342. xPos++;
  343. stoppingX += twoBSquared;
  344. ellipseError += xChange;
  345. xChange += twoBSquared;
  346. if ((2L * ellipseError + yChange) > 0L)
  347. {
  348. yPos--;
  349. stoppingY -= twoASquared;
  350. ellipseError += yChange;
  351. yChange += twoASquared;
  352. }
  353. }
  354. return mask;
  355. }
  356. }
  357. }