SceneObjectLinkingTests.cs 18 KB

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