SceneObjectLinkingTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.UpdateGroupRotation(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. // offset position should be root part position - part2.absolute position.
  86. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  87. float roll = 0;
  88. float pitch = 0;
  89. float yaw = 0;
  90. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  91. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  92. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  93. if (debugtest)
  94. m_log.Debug(rotEuler1);
  95. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  96. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  97. if (debugtest)
  98. m_log.Debug(rotEuler2);
  99. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  100. // Delink part 2
  101. grp1.DelinkFromGroup(part2.LocalId);
  102. if (debugtest)
  103. m_log.Debug("Group2: Prim2: OffsetPosition:" + part2.AbsolutePosition + ", OffsetRotation:" + part2.RotationOffset);
  104. Assert.That(grp1.Children.Count, Is.EqualTo(1), "Group 1 still contained part2 after delink.");
  105. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  106. }
  107. [Test]
  108. public void TestLinkDelink2groups4SceneObjects()
  109. {
  110. TestHelper.InMethod();
  111. bool debugtest = false;
  112. Scene scene = SceneSetupHelpers.SetupScene();
  113. SceneObjectPart part1 = SceneSetupHelpers.AddSceneObject(scene);
  114. SceneObjectGroup grp1 = part1.ParentGroup;
  115. SceneObjectPart part2 = SceneSetupHelpers.AddSceneObject(scene);
  116. SceneObjectGroup grp2 = part2.ParentGroup;
  117. SceneObjectPart part3 = SceneSetupHelpers.AddSceneObject(scene);
  118. SceneObjectGroup grp3 = part3.ParentGroup;
  119. SceneObjectPart part4 = SceneSetupHelpers.AddSceneObject(scene);
  120. SceneObjectGroup grp4 = part4.ParentGroup;
  121. grp1.AbsolutePosition = new Vector3(10, 10, 10);
  122. grp2.AbsolutePosition = Vector3.Zero;
  123. grp3.AbsolutePosition = new Vector3(20, 20, 20);
  124. grp4.AbsolutePosition = new Vector3(40, 40, 40);
  125. // <90,0,0>
  126. grp1.Rotation = (Quaternion.CreateFromEulers(90 * Utils.DEG_TO_RAD, 0, 0));
  127. // <180,0,0>
  128. grp2.UpdateGroupRotation(Quaternion.CreateFromEulers(180 * Utils.DEG_TO_RAD, 0, 0));
  129. // <270,0,0>
  130. grp3.Rotation = (Quaternion.CreateFromEulers(270 * Utils.DEG_TO_RAD, 0, 0));
  131. // <0,90,0>
  132. grp4.UpdateGroupRotation(Quaternion.CreateFromEulers(0, 90 * Utils.DEG_TO_RAD, 0));
  133. // Required for linking
  134. grp1.RootPart.UpdateFlag = 0;
  135. grp2.RootPart.UpdateFlag = 0;
  136. grp3.RootPart.UpdateFlag = 0;
  137. grp4.RootPart.UpdateFlag = 0;
  138. // Link grp2 to grp1. part2 becomes child prim to grp1. grp2 is eliminated.
  139. grp1.LinkToGroup(grp2);
  140. // Link grp4 to grp3.
  141. grp3.LinkToGroup(grp4);
  142. // At this point we should have 4 parts total in two groups.
  143. Assert.That(grp1.Children.Count == 2);
  144. Assert.That(grp2.IsDeleted, "Group 2 was not registered as deleted after link.");
  145. Assert.That(grp2.Children.Count, Is.EqualTo(0), "Group 2 still contained parts after delink.");
  146. Assert.That(grp3.Children.Count == 2);
  147. Assert.That(grp4.IsDeleted, "Group 4 was not registered as deleted after link.");
  148. Assert.That(grp4.Children.Count, Is.EqualTo(0), "Group 4 still contained parts after delink.");
  149. if (debugtest)
  150. {
  151. m_log.Debug("--------After Link-------");
  152. m_log.Debug("Group1: parts:" + grp1.Children.Count);
  153. m_log.Debug("Group1: Pos:"+grp1.AbsolutePosition+", Rot:"+grp1.Rotation);
  154. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  155. m_log.Debug("Group1: Prim2: OffsetPosition:"+part2.OffsetPosition+", OffsetRotation:"+ part2.RotationOffset);
  156. m_log.Debug("Group3: parts:"+grp3.Children.Count);
  157. m_log.Debug("Group3: Pos:"+grp3.AbsolutePosition+", Rot:"+grp3.Rotation);
  158. m_log.Debug("Group3: Prim1: OffsetPosition:"+part3.OffsetPosition+", OffsetRotation:"+part3.RotationOffset);
  159. m_log.Debug("Group3: Prim2: OffsetPosition:"+part4.OffsetPosition+", OffsetRotation:"+part4.RotationOffset);
  160. }
  161. // Required for linking
  162. grp1.RootPart.UpdateFlag = 0;
  163. grp3.RootPart.UpdateFlag = 0;
  164. // root part should have no offset position or rotation
  165. Assert.That(part1.OffsetPosition == Vector3.Zero && part1.RotationOffset == Quaternion.Identity);
  166. // offset position should be root part position - part2.absolute position.
  167. Assert.That(part2.OffsetPosition == new Vector3(-10, -10, -10));
  168. float roll = 0;
  169. float pitch = 0;
  170. float yaw = 0;
  171. // There's a euler anomoly at 180, 0, 0 so expect 180 to turn into -180.
  172. part1.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  173. Vector3 rotEuler1 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  174. if (debugtest)
  175. m_log.Debug(rotEuler1);
  176. part2.RotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  177. Vector3 rotEuler2 = new Vector3(roll * Utils.RAD_TO_DEG, pitch * Utils.RAD_TO_DEG, yaw * Utils.RAD_TO_DEG);
  178. if (debugtest)
  179. m_log.Debug(rotEuler2);
  180. Assert.That(rotEuler2.ApproxEquals(new Vector3(-180, 0, 0), 0.001f) || rotEuler2.ApproxEquals(new Vector3(180, 0, 0), 0.001f));
  181. // Now we're linking the first group to the third group. This will make the first group child parts of the third one.
  182. grp3.LinkToGroup(grp1);
  183. // Delink parts 2 and 3
  184. grp3.DelinkFromGroup(part2.LocalId);
  185. grp3.DelinkFromGroup(part3.LocalId);
  186. if (debugtest)
  187. {
  188. m_log.Debug("--------After De-Link-------");
  189. m_log.Debug("Group1: parts:" + grp1.Children.Count);
  190. m_log.Debug("Group1: Pos:" + grp1.AbsolutePosition + ", Rot:" + grp1.Rotation);
  191. m_log.Debug("Group1: Prim1: OffsetPosition:" + part1.OffsetPosition + ", OffsetRotation:" + part1.RotationOffset);
  192. m_log.Debug("Group1: Prim2: OffsetPosition:" + part2.OffsetPosition + ", OffsetRotation:" + part2.RotationOffset);
  193. m_log.Debug("Group3: parts:" + grp3.Children.Count);
  194. m_log.Debug("Group3: Pos:" + grp3.AbsolutePosition + ", Rot:" + grp3.Rotation);
  195. m_log.Debug("Group3: Prim1: OffsetPosition:" + part3.OffsetPosition + ", OffsetRotation:" + part3.RotationOffset);
  196. m_log.Debug("Group3: Prim2: OffsetPosition:" + part4.OffsetPosition + ", OffsetRotation:" + part4.RotationOffset);
  197. }
  198. Assert.That(part2.AbsolutePosition == Vector3.Zero);
  199. Assert.That(part4.OffsetPosition == new Vector3(20, 20, 20));
  200. Quaternion compareQuaternion = new Quaternion(0, 0.7071068f, 0, 0.7071068f);
  201. Assert.That((part4.RotationOffset.X - compareQuaternion.X < 0.00003)
  202. && (part4.RotationOffset.Y - compareQuaternion.Y < 0.00003)
  203. && (part4.RotationOffset.Z - compareQuaternion.Z < 0.00003)
  204. && (part4.RotationOffset.W - compareQuaternion.W < 0.00003));
  205. }
  206. }
  207. }