LSL_ApiTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Collections.Generic;
  28. using NUnit.Framework;
  29. using OpenSim.Tests.Common;
  30. using OpenSim.Region.ScriptEngine.Shared;
  31. using OpenSim.Tests.Common.Setup;
  32. using OpenSim.Region.Framework.Scenes;
  33. using Nini.Config;
  34. using OpenSim.Region.ScriptEngine.Shared.Api;
  35. using OpenMetaverse;
  36. using System;
  37. using OpenSim.Tests.Common.Mock;
  38. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  39. {
  40. /// <summary>
  41. /// Tests for LSL_Api
  42. /// </summary>
  43. [TestFixture, LongRunning]
  44. public class LSL_ApiTest
  45. {
  46. private const double ANGLE_ACCURACY_IN_RADIANS = 1E-6;
  47. private LSL_Api m_lslApi;
  48. [SetUp]
  49. public void SetUp()
  50. {
  51. IConfigSource initConfigSource = new IniConfigSource();
  52. IConfig config = initConfigSource.AddConfig("XEngine");
  53. config.Set("Enabled", "true");
  54. Scene scene = SceneSetupHelpers.SetupScene();
  55. SceneObjectPart part = SceneSetupHelpers.AddSceneObject(scene);
  56. XEngine.XEngine engine = new XEngine.XEngine();
  57. engine.Initialise(initConfigSource);
  58. engine.AddRegion(scene);
  59. m_lslApi = new LSL_Api();
  60. m_lslApi.Initialize(engine, part, part.LocalId, part.UUID);
  61. }
  62. [Test]
  63. public void TestllAngleBetween()
  64. {
  65. CheckllAngleBetween(new Vector3(1, 0, 0), 0);
  66. CheckllAngleBetween(new Vector3(1, 0, 0), 90);
  67. CheckllAngleBetween(new Vector3(1, 0, 0), 180);
  68. CheckllAngleBetween(new Vector3(0, 1, 0), 0);
  69. CheckllAngleBetween(new Vector3(0, 1, 0), 90);
  70. CheckllAngleBetween(new Vector3(0, 1, 0), 180);
  71. CheckllAngleBetween(new Vector3(0, 0, 1), 0);
  72. CheckllAngleBetween(new Vector3(0, 0, 1), 90);
  73. CheckllAngleBetween(new Vector3(0, 0, 1), 180);
  74. CheckllAngleBetween(new Vector3(1, 1, 1), 0);
  75. CheckllAngleBetween(new Vector3(1, 1, 1), 90);
  76. CheckllAngleBetween(new Vector3(1, 1, 1), 180);
  77. }
  78. private void CheckllAngleBetween(Vector3 axis,float originalAngle)
  79. {
  80. Quaternion rotation1 = Quaternion.CreateFromAxisAngle(axis, 0);
  81. Quaternion rotation2 = Quaternion.CreateFromAxisAngle(axis, ToRadians(originalAngle));
  82. double deducedAngle = FromLslFloat(m_lslApi.llAngleBetween(ToLslQuaternion(rotation2), ToLslQuaternion(rotation1)));
  83. Assert.Greater(deducedAngle, ToRadians(originalAngle) - ANGLE_ACCURACY_IN_RADIANS);
  84. Assert.Less(deducedAngle, ToRadians(originalAngle) + ANGLE_ACCURACY_IN_RADIANS);
  85. }
  86. #region Conversions to and from LSL_Types
  87. private float ToRadians(double degrees)
  88. {
  89. return (float)(Math.PI * degrees / 180);
  90. }
  91. // private double FromRadians(float radians)
  92. // {
  93. // return radians * 180 / Math.PI;
  94. // }
  95. private double FromLslFloat(LSL_Types.LSLFloat lslFloat)
  96. {
  97. return lslFloat.value;
  98. }
  99. // private LSL_Types.LSLFloat ToLslFloat(double value)
  100. // {
  101. // return new LSL_Types.LSLFloat(value);
  102. // }
  103. // private Quaternion FromLslQuaternion(LSL_Types.Quaternion lslQuaternion)
  104. // {
  105. // return new Quaternion((float)lslQuaternion.x, (float)lslQuaternion.y, (float)lslQuaternion.z, (float)lslQuaternion.s);
  106. // }
  107. private LSL_Types.Quaternion ToLslQuaternion(Quaternion quaternion)
  108. {
  109. return new LSL_Types.Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W);
  110. }
  111. #endregion
  112. }
  113. }