SceneObjectLinkingTests.cs 18 KB

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