LSL_ApiTest.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.Region.Framework.Scenes;
  32. using Nini.Config;
  33. using OpenSim.Region.ScriptEngine.Shared.Api;
  34. using OpenMetaverse;
  35. using System;
  36. using OpenSim.Tests.Common.Mock;
  37. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  38. {
  39. /// <summary>
  40. /// Tests for LSL_Api
  41. /// </summary>
  42. [TestFixture, LongRunning]
  43. public class LSL_ApiTest
  44. {
  45. private const double ANGLE_ACCURACY_IN_RADIANS = 1E-6;
  46. private const double VECTOR_COMPONENT_ACCURACY = 0.0000005d;
  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. [Test]
  113. // llRot2Euler test.
  114. public void TestllRot2Euler()
  115. {
  116. // 180, 90 and zero degree rotations.
  117. CheckllRot2Euler(new LSL_Types.Quaternion(1.0f, 0.0f, 0.0f, 0.0f), new LSL_Types.Vector3(Math.PI, 0.0f, 0.0f));
  118. CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 1.0f, 0.0f, 0.0f), new LSL_Types.Vector3(Math.PI, 0.0f, Math.PI));
  119. CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 1.0f, 0.0f), new LSL_Types.Vector3(0.0f, 0.0f, Math.PI));
  120. CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 0.0f, 1.0f), new LSL_Types.Vector3(0.0f, 0.0f, 0.0f));
  121. CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, -0.5f, 0.5f, 0.5f), new LSL_Types.Vector3(0, -Math.PI / 2.0f, Math.PI / 2.0f));
  122. CheckllRot2Euler(new LSL_Types.Quaternion(-0.707107f, 0.0f, 0.0f, -0.707107f), new LSL_Types.Vector3(Math.PI / 2.0f, 0.0f, 0.0f));
  123. // A couple of messy rotations.
  124. CheckllRot2Euler(new LSL_Types.Quaternion(1.0f, 5.651f, -3.1f, 67.023f), new LSL_Types.Vector3(0.037818f, 0.166447f, -0.095595f));
  125. CheckllRot2Euler(new LSL_Types.Quaternion(0.719188f, -0.408934f, -0.363998f, -0.427841f), new LSL_Types.Vector3(-1.954769f, -0.174533f, 1.151917f));
  126. }
  127. private void CheckllRot2Euler(LSL_Types.Quaternion rot, LSL_Types.Vector3 eulerCheck)
  128. {
  129. // Call LSL function to convert quaternion rotaion to euler radians.
  130. LSL_Types.Vector3 eulerCalc = m_lslApi.llRot2Euler(rot);
  131. // Check upper and lower bounds of x, y and z.
  132. // This type of check is performed as opposed to comparing for equal numbers, in order to allow slight
  133. // differences in accuracy.
  134. Assert.Greater(eulerCalc.x, eulerCheck.x - ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler X lower bounds check fail");
  135. Assert.Less(eulerCalc.x, eulerCheck.x + ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler X upper bounds check fail");
  136. Assert.Greater(eulerCalc.y, eulerCheck.y - ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler Y lower bounds check fail");
  137. Assert.Less(eulerCalc.y, eulerCheck.y + ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler Y upper bounds check fail");
  138. Assert.Greater(eulerCalc.z, eulerCheck.z - ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler Z lower bounds check fail");
  139. Assert.Less(eulerCalc.z, eulerCheck.z + ANGLE_ACCURACY_IN_RADIANS, "TestllRot2Euler Z upper bounds check fail");
  140. }
  141. [Test]
  142. // llVecNorm test.
  143. public void TestllVecNorm()
  144. {
  145. // Check special case for normalizing zero vector.
  146. CheckllVecNorm(new LSL_Types.Vector3(0.0d, 0.0d, 0.0d), new LSL_Types.Vector3(0.0d, 0.0d, 0.0d));
  147. // Check various vectors.
  148. CheckllVecNorm(new LSL_Types.Vector3(10.0d, 25.0d, 0.0d), new LSL_Types.Vector3(0.371391d, 0.928477d, 0.0d));
  149. CheckllVecNorm(new LSL_Types.Vector3(1.0d, 0.0d, 0.0d), new LSL_Types.Vector3(1.0d, 0.0d, 0.0d));
  150. CheckllVecNorm(new LSL_Types.Vector3(-90.0d, 55.0d, 2.0d), new LSL_Types.Vector3(-0.853128d, 0.521356d, 0.018958d));
  151. CheckllVecNorm(new LSL_Types.Vector3(255.0d, 255.0d, 255.0d), new LSL_Types.Vector3(0.577350d, 0.577350d, 0.577350d));
  152. }
  153. public void CheckllVecNorm(LSL_Types.Vector3 vec, LSL_Types.Vector3 vecNormCheck)
  154. {
  155. // Call LSL function to normalize the vector.
  156. LSL_Types.Vector3 vecNorm = m_lslApi.llVecNorm(vec);
  157. // Check each vector component against expected result.
  158. Assert.AreEqual(vecNorm.x, vecNormCheck.x, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on x component");
  159. Assert.AreEqual(vecNorm.y, vecNormCheck.y, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on y component");
  160. Assert.AreEqual(vecNorm.z, vecNormCheck.z, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on z component");
  161. }
  162. }
  163. }