SceneObjectLinkingTests.cs 13 KB

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