SceneObjectLinkingTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.Reflection;
  29. using NUnit.Framework;
  30. using NUnit.Framework.SyntaxHelpers;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Region.Communications.Local;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Tests.Common;
  38. using OpenSim.Tests.Common.Mock;
  39. using OpenSim.Tests.Common.Setup;
  40. using log4net;
  41. namespace OpenSim.Region.Framework.Scenes.Tests
  42. {
  43. /// <summary>
  44. /// Linking tests
  45. /// </summary>
  46. [TestFixture]
  47. public class SceneObjectLinkingTests
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. [Test]
  51. public void TestLinkDelink2SceneObjects()
  52. {
  53. TestHelper.InMethod();
  54. bool debugtest = false;
  55. Scene scene = SceneSetupHelpers.SetupScene();
  56. SceneObjectPart part1 = SceneSetupHelpers.AddSceneObject(scene);
  57. SceneObjectGroup grp1 = part1.ParentGroup;
  58. SceneObjectPart part2 = SceneSetupHelpers.AddSceneObject(scene);
  59. SceneObjectGroup grp2 = part2.ParentGroup;
  60. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  61. grp2.AbsolutePosition = Vector3.Zero;
  62. // <90,0,0>
  63. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  64. // <180,0,0>
  65. grp2.UpdateGroupRotationR(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  66. // Required for linking
  67. grp1.RootPart.UpdateFlag = 0;
  68. grp2.RootPart.UpdateFlag = 0;
  69. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  70. grp1.LinkToGroup(grp2);
  71. // FIXME: Can't do this test yet since group 2 still has its root part! We can't yet null this since
  72. // it might cause SOG.ProcessBackup() to fail due to the race condition. This really needs to be fixed.
  73. Assert.That(grp2.IsDeleted, "SOG 2 was not registered as deleted after link.");
  74. Assert.That(grp2.Children.Count, Is.EqualTo(0), "Group 2 still contained children after delink.");
  75. Assert.That(grp1.Children.Count == 2);
  76. if (debugtest)
  77. {
  78. m_log.Debug("parts: " + grp1.Children.Count);
  79. m_log.Debug("Group1: Pos:"+grp1.AbsolutePosition+", Rot:"+grp1.Rotation);
  80. m_log.Debug("Group1: Prim1: OffsetPosition:"+ part1.OffsetPosition+", OffsetRotation:"+part1.RotationOffset);
  81. m_log.Debug("Group1: Prim2: OffsetPosition:"+part2.OffsetPosition+", OffsetRotation:"+part2.RotationOffset);
  82. }
  83. // root part should have no offset position or rotation
  84. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity,
  85. "root part should have no offset position or rotation");
  86. // offset position should be root part position - part2.absolute position.
  87. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10),
  88. "offset position should be root part position - part2.absolute position.");
  89. float roll = 0;
  90. float pitch = 0;
  91. float yaw = 0;
  92. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  93. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  94. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  95. if (debugtest)
  96. m_log.Debug(rotEuler1);
  97. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  98. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  99. if (debugtest)
  100. m_log.Debug(rotEuler2);
  101. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f),
  102. "Not exactly sure what this is asserting...");
  103. // Delink part 2
  104. grp1.DelinkFromGroup(part2.LocalId);
  105. if (debugtest)
  106. m_log.Debug("Group2: Prim2: OffsetPosition:" + part2.AbsolutePosition + ", OffsetRotation:" + part2.RotationOffset);
  107. Assert.That(grp1.Children.Count, Is.EqualTo(1), "Group 1 still contained part2 after delink.");
  108. Assert.That(part2.AbsolutePosition == Vector3.Zero, "The absolute position should be zero");
  109. }
  110. [Test]
  111. public void TestLinkDelink2groups4SceneObjects()
  112. {
  113. TestHelper.InMethod();
  114. bool debugtest = false;
  115. Scene scene = SceneSetupHelpers.SetupScene();
  116. SceneObjectPart part1 = SceneSetupHelpers.AddSceneObject(scene);
  117. SceneObjectGroup grp1 = part1.ParentGroup;
  118. SceneObjectPart part2 = SceneSetupHelpers.AddSceneObject(scene);
  119. SceneObjectGroup grp2 = part2.ParentGroup;
  120. SceneObjectPart part3 = SceneSetupHelpers.AddSceneObject(scene);
  121. SceneObjectGroup grp3 = part3.ParentGroup;
  122. SceneObjectPart part4 = SceneSetupHelpers.AddSceneObject(scene);
  123. SceneObjectGroup grp4 = part4.ParentGroup;
  124. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  125. grp2.AbsolutePosition = Vector3.Zero;
  126. grp3.AbsolutePosition = new Vector3(20, 20, 20);
  127. grp4.AbsolutePosition = new Vector3(40, 40, 40);
  128. // <90,0,0>
  129. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  130. // <180,0,0>
  131. grp2.UpdateGroupRotationR(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  132. // <270,0,0>
  133. grp3.Rotation = (Quaternion.CreateFromEulers(270 * Utils.DEG_TO_RAD, 0, 0));
  134. // <0,90,0>
  135. grp4.UpdateGroupRotationR(Quaternion.CreateFromEulers(0, 90 * Utils.DEG_TO_RAD, 0));
  136. // Required for linking
  137. grp1.RootPart.UpdateFlag = 0;
  138. grp2.RootPart.UpdateFlag = 0;
  139. grp3.RootPart.UpdateFlag = 0;
  140. grp4.RootPart.UpdateFlag = 0;
  141. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  142. grp1.LinkToGroup(grp2);
  143. // Link grp4 to grp3.
  144. grp3.LinkToGroup(grp4);
  145. // At this point we should have 4 parts total in two groups.
  146. Assert.That(grp1.Children.Count == 2, "Group1 children count should be 2");
  147. Assert.That(grp2.IsDeleted, "Group 2 was not registered as deleted after link.");
  148. Assert.That(grp2.Children.Count, Is.EqualTo(0), "Group 2 still contained parts after delink.");
  149. Assert.That(grp3.Children.Count == 2, "Group3 children count should be 2");
  150. Assert.That(grp4.IsDeleted, "Group 4 was not registered as deleted after link.");
  151. Assert.That(grp4.Children.Count, Is.EqualTo(0), "Group 4 still contained parts after delink.");
  152. if (debugtest)
  153. {
  154. m_log.Debug("--------After Link-------");
  155. m_log.Debug("Group1: parts:" + grp1.Children.Count);
  156. m_log.Debug("Group1: Pos:"+grp1.AbsolutePosition+", Rot:"+grp1.Rotation);
  157. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  158. m_log.Debug("Group1: Prim2: OffsetPosition:"+part2.OffsetPosition+", OffsetRotation:"+ part2.RotationOffset);
  159. m_log.Debug("Group3: parts:"+grp3.Children.Count);
  160. m_log.Debug("Group3: Pos:"+grp3.AbsolutePosition+", Rot:"+grp3.Rotation);
  161. m_log.Debug("Group3: Prim1: OffsetPosition:"+part3.OffsetPosition+", OffsetRotation:"+part3.RotationOffset);
  162. m_log.Debug("Group3: Prim2: OffsetPosition:"+part4.OffsetPosition+", OffsetRotation:"+part4.RotationOffset);
  163. }
  164. // Required for linking
  165. grp1.RootPart.UpdateFlag = 0;
  166. grp3.RootPart.UpdateFlag = 0;
  167. // root part should have no offset position or rotation
  168. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity,
  169. "root part should have no offset position or rotation (again)");
  170. // offset position should be root part position - part2.absolute position.
  171. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10),
  172. "offset position should be root part position - part2.absolute position (again)");
  173. float roll = 0;
  174. float pitch = 0;
  175. float yaw = 0;
  176. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  177. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  178. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  179. if (debugtest)
  180. m_log.Debug(rotEuler1);
  181. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  182. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  183. if (debugtest)
  184. m_log.Debug(rotEuler2);
  185. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f),
  186. "Not sure what this assertion is all about...");
  187. // Now we're linking the first group to the third group. This will make the first group child parts of the third one.
  188. grp3.LinkToGroup(grp1);
  189. // Delink parts 2 and 3
  190. grp3.DelinkFromGroup(part2.LocalId);
  191. grp3.DelinkFromGroup(part3.LocalId);
  192. if (debugtest)
  193. {
  194. m_log.Debug("--------After De-Link-------");
  195. m_log.Debug("Group1: parts:" + grp1.Children.Count);
  196. m_log.Debug("Group1: Pos:" + grp1.AbsolutePosition + ", Rot:" + grp1.Rotation);
  197. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  198. m_log.Debug("Group1: Prim2: OffsetPosition:" + part2.OffsetPosition + ", OffsetRotation:" + part2.RotationOffset);
  199. m_log.Debug("Group3: parts:" + grp3.Children.Count);
  200. m_log.Debug("Group3: Pos:" + grp3.AbsolutePosition + ", Rot:" + grp3.Rotation);
  201. m_log.Debug("Group3: Prim1: OffsetPosition:" + part3.OffsetPosition + ", OffsetRotation:" + part3.RotationOffset);
  202. m_log.Debug("Group3: Prim2: OffsetPosition:" + part4.OffsetPosition + ", OffsetRotation:" + part4.RotationOffset);
  203. }
  204. Assert.That(part2.AbsolutePosition == Vector3.Zero, "Badness 1");
  205. Assert.That(part4.OffsetPosition == new Vector3(20, 20, 20), "Badness 2");
  206. Quaternion compareQuaternion = new Quaternion(0, 0.7071068f, 0, 0.7071068f);
  207. Assert.That((part4.RotationOffset.X - compareQuaternion.X < 0.00003)
  208. && (part4.RotationOffset.Y - compareQuaternion.Y < 0.00003)
  209. && (part4.RotationOffset.Z - compareQuaternion.Z < 0.00003)
  210. && (part4.RotationOffset.W - compareQuaternion.W < 0.00003),
  211. "Badness 3");
  212. }
  213. }
  214. }