BorderTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Region.Framework.Scenes;
  34. using OpenSim.Tests.Common;
  35. namespace OpenSim.Region.Framework.Scenes.Tests
  36. {
  37. [TestFixture]
  38. public class BorderTests : OpenSimTestCase
  39. {
  40. [Test]
  41. public void TestCross()
  42. {
  43. TestHelpers.InMethod();
  44. List<Border> testborders = new List<Border>();
  45. Border NorthBorder = new Border();
  46. NorthBorder.BorderLine = new Vector3(0, 256, 256); //<---
  47. NorthBorder.CrossDirection = Cardinals.N;
  48. testborders.Add(NorthBorder);
  49. Border SouthBorder = new Border();
  50. SouthBorder.BorderLine = new Vector3(0, 256, 0); //--->
  51. SouthBorder.CrossDirection = Cardinals.S;
  52. testborders.Add(SouthBorder);
  53. Border EastBorder = new Border();
  54. EastBorder.BorderLine = new Vector3(0, 256, 256); //<---
  55. EastBorder.CrossDirection = Cardinals.E;
  56. testborders.Add(EastBorder);
  57. Border WestBorder = new Border();
  58. WestBorder.BorderLine = new Vector3(0, 256, 0); //--->
  59. WestBorder.CrossDirection = Cardinals.W;
  60. testborders.Add(WestBorder);
  61. Vector3 position = new Vector3(200,200,21);
  62. foreach (Border b in testborders)
  63. Assert.That(!b.TestCross(position));
  64. position = new Vector3(200,280,21);
  65. Assert.That(NorthBorder.TestCross(position));
  66. // Test automatic border crossing
  67. // by setting the border crossing aabb to be the whole region
  68. position = new Vector3(25,25,21); // safely within one 256m region
  69. // The Z value of the BorderLine is reversed, making all positions within the region
  70. // trigger bordercross
  71. SouthBorder.BorderLine = new Vector3(0,256,256); // automatic border cross in the region
  72. Assert.That(SouthBorder.TestCross(position));
  73. NorthBorder.BorderLine = new Vector3(0, 256, 0); // automatic border cross in the region
  74. Assert.That(NorthBorder.TestCross(position));
  75. EastBorder.BorderLine = new Vector3(0, 256, 0); // automatic border cross in the region
  76. Assert.That(EastBorder.TestCross(position));
  77. WestBorder.BorderLine = new Vector3(0, 256, 255); // automatic border cross in the region
  78. Assert.That(WestBorder.TestCross(position));
  79. }
  80. [Test]
  81. public void TestCrossSquare512()
  82. {
  83. TestHelpers.InMethod();
  84. List<Border> testborders = new List<Border>();
  85. Border NorthBorder = new Border();
  86. NorthBorder.BorderLine = new Vector3(0, 512, 512);
  87. NorthBorder.CrossDirection = Cardinals.N;
  88. testborders.Add(NorthBorder);
  89. Border SouthBorder = new Border();
  90. SouthBorder.BorderLine = new Vector3(0, 512, 0);
  91. SouthBorder.CrossDirection = Cardinals.S;
  92. testborders.Add(SouthBorder);
  93. Border EastBorder = new Border();
  94. EastBorder.BorderLine = new Vector3(0, 512, 512);
  95. EastBorder.CrossDirection = Cardinals.E;
  96. testborders.Add(EastBorder);
  97. Border WestBorder = new Border();
  98. WestBorder.BorderLine = new Vector3(0, 512, 0);
  99. WestBorder.CrossDirection = Cardinals.W;
  100. testborders.Add(WestBorder);
  101. Vector3 position = new Vector3(450,220,21);
  102. foreach (Border b in testborders)
  103. {
  104. Assert.That(!b.TestCross(position));
  105. }
  106. //Trigger east border
  107. position = new Vector3(513,220,21);
  108. foreach (Border b in testborders)
  109. {
  110. if (b.CrossDirection == Cardinals.E)
  111. Assert.That(b.TestCross(position));
  112. else
  113. Assert.That(!b.TestCross(position));
  114. }
  115. //Trigger west border
  116. position = new Vector3(-1, 220, 21);
  117. foreach (Border b in testborders)
  118. {
  119. if (b.CrossDirection == Cardinals.W)
  120. Assert.That(b.TestCross(position));
  121. else
  122. Assert.That(!b.TestCross(position));
  123. }
  124. //Trigger north border
  125. position = new Vector3(220, 513, 21);
  126. foreach (Border b in testborders)
  127. {
  128. if (b.CrossDirection == Cardinals.N)
  129. Assert.That(b.TestCross(position));
  130. else
  131. Assert.That(!b.TestCross(position));
  132. }
  133. //Trigger south border
  134. position = new Vector3(220, -1, 21);
  135. foreach (Border b in testborders)
  136. {
  137. if (b.CrossDirection == Cardinals.S)
  138. Assert.That(b.TestCross(position));
  139. else
  140. Assert.That(!b.TestCross(position));
  141. }
  142. }
  143. [Test]
  144. public void TestCrossRectangle512x256()
  145. {
  146. TestHelpers.InMethod();
  147. List<Border> testborders = new List<Border>();
  148. Border NorthBorder = new Border();
  149. NorthBorder.BorderLine = new Vector3(0, 512, 256);
  150. NorthBorder.CrossDirection = Cardinals.N;
  151. testborders.Add(NorthBorder);
  152. Border SouthBorder = new Border();
  153. SouthBorder.BorderLine = new Vector3(0, 512, 0);
  154. SouthBorder.CrossDirection = Cardinals.S;
  155. testborders.Add(SouthBorder);
  156. Border EastBorder = new Border();
  157. EastBorder.BorderLine = new Vector3(0, 256, 512);
  158. EastBorder.CrossDirection = Cardinals.E;
  159. testborders.Add(EastBorder);
  160. Border WestBorder = new Border();
  161. WestBorder.BorderLine = new Vector3(0, 256, 0);
  162. WestBorder.CrossDirection = Cardinals.W;
  163. testborders.Add(WestBorder);
  164. Vector3 position = new Vector3(450, 220, 21);
  165. foreach (Border b in testborders)
  166. {
  167. Assert.That(!b.TestCross(position));
  168. }
  169. //Trigger east border
  170. position = new Vector3(513, 220, 21);
  171. foreach (Border b in testborders)
  172. {
  173. if (b.CrossDirection == Cardinals.E)
  174. Assert.That(b.TestCross(position));
  175. else
  176. Assert.That(!b.TestCross(position));
  177. }
  178. //Trigger west border
  179. position = new Vector3(-1, 220, 21);
  180. foreach (Border b in testborders)
  181. {
  182. if (b.CrossDirection == Cardinals.W)
  183. Assert.That(b.TestCross(position));
  184. else
  185. Assert.That(!b.TestCross(position));
  186. }
  187. //Trigger north border
  188. position = new Vector3(220, 257, 21);
  189. foreach (Border b in testborders)
  190. {
  191. if (b.CrossDirection == Cardinals.N)
  192. Assert.That(b.TestCross(position));
  193. else
  194. Assert.That(!b.TestCross(position));
  195. }
  196. //Trigger south border
  197. position = new Vector3(220, -1, 21);
  198. foreach (Border b in testborders)
  199. {
  200. if (b.CrossDirection == Cardinals.S)
  201. Assert.That(b.TestCross(position));
  202. else
  203. Assert.That(!b.TestCross(position));
  204. }
  205. }
  206. [Test]
  207. public void TestCrossOdd512x512w256hole()
  208. {
  209. TestHelpers.InMethod();
  210. List<Border> testborders = new List<Border>();
  211. // 512____
  212. // | |
  213. // 256__| |___
  214. // | |
  215. // |______|
  216. // 0 | 512
  217. // 256
  218. // Compound North border since the hole is at the top
  219. Border NorthBorder1 = new Border();
  220. NorthBorder1.BorderLine = new Vector3(0, 256, 512);
  221. NorthBorder1.CrossDirection = Cardinals.N;
  222. testborders.Add(NorthBorder1);
  223. Border NorthBorder2 = new Border();
  224. NorthBorder2.BorderLine = new Vector3(256, 512, 256);
  225. NorthBorder2.CrossDirection = Cardinals.N;
  226. testborders.Add(NorthBorder2);
  227. Border SouthBorder = new Border();
  228. SouthBorder.BorderLine = new Vector3(0, 512, 0);
  229. SouthBorder.CrossDirection = Cardinals.S;
  230. testborders.Add(SouthBorder);
  231. //Compound East border
  232. Border EastBorder1 = new Border();
  233. EastBorder1.BorderLine = new Vector3(0, 256, 512);
  234. EastBorder1.CrossDirection = Cardinals.E;
  235. testborders.Add(EastBorder1);
  236. Border EastBorder2 = new Border();
  237. EastBorder2.BorderLine = new Vector3(257, 512, 256);
  238. EastBorder2.CrossDirection = Cardinals.E;
  239. testborders.Add(EastBorder2);
  240. Border WestBorder = new Border();
  241. WestBorder.BorderLine = new Vector3(0, 512, 0);
  242. WestBorder.CrossDirection = Cardinals.W;
  243. testborders.Add(WestBorder);
  244. Vector3 position = new Vector3(450, 220, 21);
  245. foreach (Border b in testborders)
  246. {
  247. Assert.That(!b.TestCross(position));
  248. }
  249. position = new Vector3(220, 450, 21);
  250. foreach (Border b in testborders)
  251. {
  252. Assert.That(!b.TestCross(position));
  253. }
  254. bool result = false;
  255. int bordersTriggered = 0;
  256. position = new Vector3(450, 450, 21);
  257. foreach (Border b in testborders)
  258. {
  259. if (b.TestCross(position))
  260. {
  261. bordersTriggered++;
  262. result = true;
  263. }
  264. }
  265. Assert.That(result);
  266. Assert.That(bordersTriggered == 2);
  267. }
  268. }
  269. }