BorderTests.cs 11 KB

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