1
0

SceneObjectLinkingTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.Reflection;
  30. using NUnit.Framework;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Tests.Common;
  36. using OpenSim.Tests.Common.Mock;
  37. using log4net;
  38. namespace OpenSim.Region.Framework.Scenes.Tests
  39. {
  40. /// <summary>
  41. /// Linking tests
  42. /// </summary>
  43. [TestFixture]
  44. public class SceneObjectLinkingTests
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. [Test]
  48. public void TestLinkDelink2SceneObjects()
  49. {
  50. TestHelpers.InMethod();
  51. bool debugtest = false;
  52. Scene scene = SceneHelpers.SetupScene();
  53. SceneObjectPart part1 = SceneHelpers.AddSceneObject(scene);
  54. SceneObjectGroup grp1 = part1.ParentGroup;
  55. SceneObjectPart part2 = SceneHelpers.AddSceneObject(scene);
  56. SceneObjectGroup grp2 = part2.ParentGroup;
  57. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  58. grp2.AbsolutePosition = Vector3.Zero;
  59. // <90,0,0>
  60. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  61. // <180,0,0>
  62. grp2.UpdateGroupRotationR(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  63. // Required for linking
  64. grp1.RootPart.UpdateFlag = 0;
  65. grp2.RootPart.UpdateFlag = 0;
  66. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  67. grp1.LinkToGroup(grp2);
  68. // FIXME: Can't do this test yet since group 2 still has its root part! We can't yet null this since
  69. // it might cause SOG.ProcessBackup() to fail due to the race condition. This really needs to be fixed.
  70. Assert.That(grp2.IsDeleted, "SOG 2 was not registered as deleted after link.");
  71. Assert.That(grp2.Parts.Length, Is.EqualTo(0), "Group 2 still contained children after delink.");
  72. Assert.That(grp1.Parts.Length == 2);
  73. if (debugtest)
  74. {
  75. m_log.Debug("parts: " + grp1.Parts.Length);
  76. m_log.Debug("Group1: Pos:"+grp1.AbsolutePosition+", Rot:"+grp1.Rotation);
  77. m_log.Debug("Group1: Prim1: OffsetPosition:"+ part1.OffsetPosition+", OffsetRotation:"+part1.RotationOffset);
  78. m_log.Debug("Group1: Prim2: OffsetPosition:"+part2.OffsetPosition+", OffsetRotation:"+part2.RotationOffset);
  79. }
  80. // root part should have no offset position or rotation
  81. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity,
  82. "root part should have no offset position or rotation");
  83. // offset position should be root part position - part2.absolute position.
  84. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10),
  85. "offset position should be root part position - part2.absolute position.");
  86. float roll = 0;
  87. float pitch = 0;
  88. float yaw = 0;
  89. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  90. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  91. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  92. if (debugtest)
  93. m_log.Debug(rotEuler1);
  94. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  95. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  96. if (debugtest)
  97. m_log.Debug(rotEuler2);
  98. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f),
  99. "Not exactly sure what this is asserting...");
  100. // Delink part 2
  101. SceneObjectGroup grp3 = grp1.DelinkFromGroup(part2.LocalId);
  102. if (debugtest)
  103. m_log.Debug("Group2: Prim2: OffsetPosition:" + part2.AbsolutePosition + ", OffsetRotation:" + part2.RotationOffset);
  104. Assert.That(grp1.Parts.Length, Is.EqualTo(1), "Group 1 still contained part2 after delink.");
  105. Assert.That(part2.AbsolutePosition == Vector3.Zero, "The absolute position should be zero");
  106. Assert.That(grp3.HasGroupChangedDueToDelink, Is.True);
  107. }
  108. [Test]
  109. public void TestLinkDelink2groups4SceneObjects()
  110. {
  111. TestHelpers.InMethod();
  112. bool debugtest = false;
  113. Scene scene = SceneHelpers.SetupScene();
  114. SceneObjectPart part1 = SceneHelpers.AddSceneObject(scene);
  115. SceneObjectGroup grp1 = part1.ParentGroup;
  116. SceneObjectPart part2 = SceneHelpers.AddSceneObject(scene);
  117. SceneObjectGroup grp2 = part2.ParentGroup;
  118. SceneObjectPart part3 = SceneHelpers.AddSceneObject(scene);
  119. SceneObjectGroup grp3 = part3.ParentGroup;
  120. SceneObjectPart part4 = SceneHelpers.AddSceneObject(scene);
  121. SceneObjectGroup grp4 = part4.ParentGroup;
  122. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  123. grp2.AbsolutePosition = Vector3.Zero;
  124. grp3.AbsolutePosition = new Vector3(20, 20, 20);
  125. grp4.AbsolutePosition = new Vector3(40, 40, 40);
  126. // <90,0,0>
  127. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  128. // <180,0,0>
  129. grp2.UpdateGroupRotationR(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  130. // <270,0,0>
  131. grp3.Rotation = (Quaternion.CreateFromEulers(270 * Utils.DEG_TO_RAD, 0, 0));
  132. // <0,90,0>
  133. grp4.UpdateGroupRotationR(Quaternion.CreateFromEulers(0, 90 * Utils.DEG_TO_RAD, 0));
  134. // Required for linking
  135. grp1.RootPart.UpdateFlag = 0;
  136. grp2.RootPart.UpdateFlag = 0;
  137. grp3.RootPart.UpdateFlag = 0;
  138. grp4.RootPart.UpdateFlag = 0;
  139. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  140. grp1.LinkToGroup(grp2);
  141. // Link grp4 to grp3.
  142. grp3.LinkToGroup(grp4);
  143. // At this point we should have 4 parts total in two groups.
  144. Assert.That(grp1.Parts.Length == 2, "Group1 children count should be 2");
  145. Assert.That(grp2.IsDeleted, "Group 2 was not registered as deleted after link.");
  146. Assert.That(grp2.Parts.Length, Is.EqualTo(0), "Group 2 still contained parts after delink.");
  147. Assert.That(grp3.Parts.Length == 2, "Group3 children count should be 2");
  148. Assert.That(grp4.IsDeleted, "Group 4 was not registered as deleted after link.");
  149. Assert.That(grp4.Parts.Length, Is.EqualTo(0), "Group 4 still contained parts after delink.");
  150. if (debugtest)
  151. {
  152. m_log.Debug("--------After Link-------");
  153. m_log.Debug("Group1: parts:" + grp1.Parts.Length);
  154. m_log.Debug("Group1: Pos:"+grp1.AbsolutePosition+", Rot:"+grp1.Rotation);
  155. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  156. m_log.Debug("Group1: Prim2: OffsetPosition:"+part2.OffsetPosition+", OffsetRotation:"+ part2.RotationOffset);
  157. m_log.Debug("Group3: parts:" + grp3.Parts.Length);
  158. m_log.Debug("Group3: Pos:"+grp3.AbsolutePosition+", Rot:"+grp3.Rotation);
  159. m_log.Debug("Group3: Prim1: OffsetPosition:"+part3.OffsetPosition+", OffsetRotation:"+part3.RotationOffset);
  160. m_log.Debug("Group3: Prim2: OffsetPosition:"+part4.OffsetPosition+", OffsetRotation:"+part4.RotationOffset);
  161. }
  162. // Required for linking
  163. grp1.RootPart.UpdateFlag = 0;
  164. grp3.RootPart.UpdateFlag = 0;
  165. // root part should have no offset position or rotation
  166. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity,
  167. "root part should have no offset position or rotation (again)");
  168. // offset position should be root part position - part2.absolute position.
  169. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10),
  170. "offset position should be root part position - part2.absolute position (again)");
  171. float roll = 0;
  172. float pitch = 0;
  173. float yaw = 0;
  174. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  175. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  176. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  177. if (debugtest)
  178. m_log.Debug(rotEuler1);
  179. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  180. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  181. if (debugtest)
  182. m_log.Debug(rotEuler2);
  183. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f),
  184. "Not sure what this assertion is all about...");
  185. // Now we're linking the first group to the third group. This will make the first group child parts of the third one.
  186. grp3.LinkToGroup(grp1);
  187. // Delink parts 2 and 3
  188. grp3.DelinkFromGroup(part2.LocalId);
  189. grp3.DelinkFromGroup(part3.LocalId);
  190. if (debugtest)
  191. {
  192. m_log.Debug("--------After De-Link-------");
  193. m_log.Debug("Group1: parts:" + grp1.Parts.Length);
  194. m_log.Debug("Group1: Pos:" + grp1.AbsolutePosition + ", Rot:" + grp1.Rotation);
  195. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  196. m_log.Debug("Group1: Prim2: OffsetPosition:" + part2.OffsetPosition + ", OffsetRotation:" + part2.RotationOffset);
  197. m_log.Debug("Group3: parts:" + grp3.Parts.Length);
  198. m_log.Debug("Group3: Pos:" + grp3.AbsolutePosition + ", Rot:" + grp3.Rotation);
  199. m_log.Debug("Group3: Prim1: OffsetPosition:" + part3.OffsetPosition + ", OffsetRotation:" + part3.RotationOffset);
  200. m_log.Debug("Group3: Prim2: OffsetPosition:" + part4.OffsetPosition + ", OffsetRotation:" + part4.RotationOffset);
  201. }
  202. Assert.That(part2.AbsolutePosition == Vector3.Zero, "Badness 1");
  203. Assert.That(part4.OffsetPosition == new Vector3(20, 20, 20), "Badness 2");
  204. Quaternion compareQuaternion = new Quaternion(0, 0.7071068f, 0, 0.7071068f);
  205. Assert.That((part4.RotationOffset.X - compareQuaternion.X < 0.00003)
  206. && (part4.RotationOffset.Y - compareQuaternion.Y < 0.00003)
  207. && (part4.RotationOffset.Z - compareQuaternion.Z < 0.00003)
  208. && (part4.RotationOffset.W - compareQuaternion.W < 0.00003),
  209. "Badness 3");
  210. }
  211. /// <summary>
  212. /// Test that a new scene object which is already linked is correctly persisted to the persistence layer.
  213. /// </summary>
  214. [Test]
  215. public void TestNewSceneObjectLinkPersistence()
  216. {
  217. TestHelpers.InMethod();
  218. //log4net.Config.XmlConfigurator.Configure();
  219. TestScene scene = SceneHelpers.SetupScene();
  220. string rootPartName = "rootpart";
  221. UUID rootPartUuid = new UUID("00000000-0000-0000-0000-000000000001");
  222. string linkPartName = "linkpart";
  223. UUID linkPartUuid = new UUID("00000000-0000-0000-0001-000000000000");
  224. SceneObjectPart rootPart
  225. = new SceneObjectPart(UUID.Zero, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero)
  226. { Name = rootPartName, UUID = rootPartUuid };
  227. SceneObjectPart linkPart
  228. = new SceneObjectPart(UUID.Zero, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero)
  229. { Name = linkPartName, UUID = linkPartUuid };
  230. SceneObjectGroup sog = new SceneObjectGroup(rootPart);
  231. sog.AddPart(linkPart);
  232. scene.AddNewSceneObject(sog, true);
  233. // In a test, we have to crank the backup handle manually. Normally this would be done by the timer invoked
  234. // scene backup thread.
  235. scene.Backup(true);
  236. List<SceneObjectGroup> storedObjects = scene.SimulationDataService.LoadObjects(scene.RegionInfo.RegionID);
  237. Assert.That(storedObjects.Count, Is.EqualTo(1));
  238. Assert.That(storedObjects[0].Parts.Length, Is.EqualTo(2));
  239. Assert.That(storedObjects[0].ContainsPart(rootPartUuid));
  240. Assert.That(storedObjects[0].ContainsPart(linkPartUuid));
  241. }
  242. /// <summary>
  243. /// Test that a delink of a previously linked object is correctly persisted to the database
  244. /// </summary>
  245. [Test]
  246. public void TestDelinkPersistence()
  247. {
  248. TestHelpers.InMethod();
  249. //log4net.Config.XmlConfigurator.Configure();
  250. TestScene scene = SceneHelpers.SetupScene();
  251. string rootPartName = "rootpart";
  252. UUID rootPartUuid = new UUID("00000000-0000-0000-0000-000000000001");
  253. string linkPartName = "linkpart";
  254. UUID linkPartUuid = new UUID("00000000-0000-0000-0001-000000000000");
  255. SceneObjectPart rootPart
  256. = new SceneObjectPart(UUID.Zero, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero)
  257. { Name = rootPartName, UUID = rootPartUuid };
  258. SceneObjectPart linkPart
  259. = new SceneObjectPart(UUID.Zero, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero)
  260. { Name = linkPartName, UUID = linkPartUuid };
  261. SceneObjectGroup sog = new SceneObjectGroup(rootPart);
  262. sog.AddPart(linkPart);
  263. scene.AddNewSceneObject(sog, true);
  264. // In a test, we have to crank the backup handle manually. Normally this would be done by the timer invoked
  265. // scene backup thread.
  266. scene.Backup(true);
  267. // These changes should occur immediately without waiting for a backup pass
  268. SceneObjectGroup groupToDelete = sog.DelinkFromGroup(linkPart, false);
  269. Assert.That(groupToDelete.HasGroupChangedDueToDelink, Is.True);
  270. scene.DeleteSceneObject(groupToDelete, false);
  271. Assert.That(groupToDelete.HasGroupChangedDueToDelink, Is.False);
  272. List<SceneObjectGroup> storedObjects = scene.SimulationDataService.LoadObjects(scene.RegionInfo.RegionID);
  273. Assert.That(storedObjects.Count, Is.EqualTo(1));
  274. Assert.That(storedObjects[0].Parts.Length, Is.EqualTo(1));
  275. Assert.That(storedObjects[0].ContainsPart(rootPartUuid));
  276. }
  277. }
  278. }