BorderTests.cs 12 KB

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