SceneObjectLinkingTests.cs 18 KB

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