opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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 | |||
28 | using System.Collections.Generic; |
||
29 | using NUnit.Framework; |
||
30 | using OpenSim.Framework; |
||
31 | using OpenSim.Tests.Common; |
||
32 | using OpenSim.Region.ScriptEngine.Shared; |
||
33 | using OpenSim.Region.Framework.Scenes; |
||
34 | using Nini.Config; |
||
35 | using OpenSim.Region.ScriptEngine.Shared.Api; |
||
36 | using OpenSim.Region.ScriptEngine.Shared.Instance; |
||
37 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; |
||
38 | using OpenMetaverse; |
||
39 | using System; |
||
40 | using OpenSim.Tests.Common.Mock; |
||
41 | |||
42 | namespace OpenSim.Region.ScriptEngine.Shared.Tests |
||
43 | { |
||
44 | /// <summary> |
||
45 | /// Tests for LSL_Api |
||
46 | /// </summary> |
||
47 | [TestFixture, LongRunning] |
||
48 | public class LSL_ApiTest |
||
49 | { |
||
50 | private const double VECTOR_COMPONENT_ACCURACY = 0.0000005d; |
||
51 | private const double ANGLE_ACCURACY_IN_RADIANS = 1E-6; |
||
52 | private LSL_Api m_lslApi; |
||
53 | |||
54 | [SetUp] |
||
55 | public void SetUp() |
||
56 | { |
||
57 | IConfigSource initConfigSource = new IniConfigSource(); |
||
58 | IConfig config = initConfigSource.AddConfig("XEngine"); |
||
59 | config.Set("Enabled", "true"); |
||
60 | |||
61 | Scene scene = new SceneHelpers().SetupScene(); |
||
62 | SceneObjectPart part = SceneHelpers.AddSceneObject(scene).RootPart; |
||
63 | |||
64 | XEngine.XEngine engine = new XEngine.XEngine(); |
||
65 | engine.Initialise(initConfigSource); |
||
66 | engine.AddRegion(scene); |
||
67 | |||
68 | m_lslApi = new LSL_Api(); |
||
69 | m_lslApi.Initialize(engine, part, null, null); |
||
70 | } |
||
71 | |||
72 | [Test] |
||
73 | public void TestllAngleBetween() |
||
74 | { |
||
75 | TestHelpers.InMethod(); |
||
76 | |||
77 | CheckllAngleBetween(new Vector3(1, 0, 0), 0, 1, 1); |
||
78 | CheckllAngleBetween(new Vector3(1, 0, 0), 90, 1, 1); |
||
79 | CheckllAngleBetween(new Vector3(1, 0, 0), 180, 1, 1); |
||
80 | |||
81 | CheckllAngleBetween(new Vector3(0, 1, 0), 0, 1, 1); |
||
82 | CheckllAngleBetween(new Vector3(0, 1, 0), 90, 1, 1); |
||
83 | CheckllAngleBetween(new Vector3(0, 1, 0), 180, 1, 1); |
||
84 | |||
85 | CheckllAngleBetween(new Vector3(0, 0, 1), 0, 1, 1); |
||
86 | CheckllAngleBetween(new Vector3(0, 0, 1), 90, 1, 1); |
||
87 | CheckllAngleBetween(new Vector3(0, 0, 1), 180, 1, 1); |
||
88 | |||
89 | CheckllAngleBetween(new Vector3(1, 1, 1), 0, 1, 1); |
||
90 | CheckllAngleBetween(new Vector3(1, 1, 1), 90, 1, 1); |
||
91 | CheckllAngleBetween(new Vector3(1, 1, 1), 180, 1, 1); |
||
92 | |||
93 | CheckllAngleBetween(new Vector3(1, 0, 0), 0, 1.6f, 1.8f); |
||
94 | CheckllAngleBetween(new Vector3(1, 0, 0), 90, 0.3f, 3.9f); |
||
95 | CheckllAngleBetween(new Vector3(1, 0, 0), 180, 8.8f, 7.4f); |
||
96 | |||
97 | CheckllAngleBetween(new Vector3(0, 1, 0), 0, 9.8f, -9.4f); |
||
98 | CheckllAngleBetween(new Vector3(0, 1, 0), 90, 8.4f, -8.2f); |
||
99 | CheckllAngleBetween(new Vector3(0, 1, 0), 180, 0.4f, -5.8f); |
||
100 | |||
101 | CheckllAngleBetween(new Vector3(0, 0, 1), 0, -6.8f, 3.4f); |
||
102 | CheckllAngleBetween(new Vector3(0, 0, 1), 90, -3.6f, 5.6f); |
||
103 | CheckllAngleBetween(new Vector3(0, 0, 1), 180, -3.8f, 1.1f); |
||
104 | |||
105 | CheckllAngleBetween(new Vector3(1, 1, 1), 0, -7.7f, -2.0f); |
||
106 | CheckllAngleBetween(new Vector3(1, 1, 1), 90, -3.0f, -9.1f); |
||
107 | CheckllAngleBetween(new Vector3(1, 1, 1), 180, -7.9f, -8.0f); |
||
108 | } |
||
109 | |||
110 | private void CheckllAngleBetween(Vector3 axis,float originalAngle, float denorm1, float denorm2) |
||
111 | { |
||
112 | Quaternion rotation1 = Quaternion.CreateFromAxisAngle(axis, 0); |
||
113 | Quaternion rotation2 = Quaternion.CreateFromAxisAngle(axis, ToRadians(originalAngle)); |
||
114 | rotation1 *= denorm1; |
||
115 | rotation2 *= denorm2; |
||
116 | |||
117 | double deducedAngle = FromLslFloat(m_lslApi.llAngleBetween(ToLslQuaternion(rotation2), ToLslQuaternion(rotation1))); |
||
118 | |||
119 | Assert.That(deducedAngle, Is.EqualTo(ToRadians(originalAngle)).Within(ANGLE_ACCURACY_IN_RADIANS), "TestllAngleBetween check fail"); |
||
120 | } |
||
121 | |||
122 | #region Conversions to and from LSL_Types |
||
123 | |||
124 | private float ToRadians(double degrees) |
||
125 | { |
||
126 | return (float)(Math.PI * degrees / 180); |
||
127 | } |
||
128 | |||
129 | // private double FromRadians(float radians) |
||
130 | // { |
||
131 | // return radians * 180 / Math.PI; |
||
132 | // } |
||
133 | |||
134 | private double FromLslFloat(LSL_Types.LSLFloat lslFloat) |
||
135 | { |
||
136 | return lslFloat.value; |
||
137 | } |
||
138 | |||
139 | // private LSL_Types.LSLFloat ToLslFloat(double value) |
||
140 | // { |
||
141 | // return new LSL_Types.LSLFloat(value); |
||
142 | // } |
||
143 | |||
144 | // private Quaternion FromLslQuaternion(LSL_Types.Quaternion lslQuaternion) |
||
145 | // { |
||
146 | // return new Quaternion((float)lslQuaternion.x, (float)lslQuaternion.y, (float)lslQuaternion.z, (float)lslQuaternion.s); |
||
147 | // } |
||
148 | |||
149 | private LSL_Types.Quaternion ToLslQuaternion(Quaternion quaternion) |
||
150 | { |
||
151 | return new LSL_Types.Quaternion(quaternion.X, quaternion.Y, quaternion.Z, quaternion.W); |
||
152 | } |
||
153 | |||
154 | #endregion |
||
155 | |||
156 | [Test] |
||
157 | // llRot2Euler test. |
||
158 | public void TestllRot2Euler() |
||
159 | { |
||
160 | TestHelpers.InMethod(); |
||
161 | |||
162 | // 180, 90 and zero degree rotations. |
||
163 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 0.0f, 1.0f)); |
||
164 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 0.707107f, 0.707107f)); |
||
165 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 1.0f, 0.0f)); |
||
166 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, 0.0f, 0.707107f, -0.707107f)); |
||
167 | CheckllRot2Euler(new LSL_Types.Quaternion(0.707107f, 0.0f, 0.0f, 0.707107f)); |
||
168 | CheckllRot2Euler(new LSL_Types.Quaternion(0.5f, -0.5f, 0.5f, 0.5f)); |
||
169 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, -0.707107f, 0.707107f, 0.0f)); |
||
170 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, -0.5f, 0.5f, -0.5f)); |
||
171 | CheckllRot2Euler(new LSL_Types.Quaternion(1.0f, 0.0f, 0.0f, 0.0f)); |
||
172 | CheckllRot2Euler(new LSL_Types.Quaternion(0.707107f, -0.707107f, 0.0f, 0.0f)); |
||
173 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, -1.0f, 0.0f, 0.0f)); |
||
174 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.707107f, -0.707107f, 0.0f, 0.0f)); |
||
175 | CheckllRot2Euler(new LSL_Types.Quaternion(0.707107f, 0.0f, 0.0f, -0.707107f)); |
||
176 | CheckllRot2Euler(new LSL_Types.Quaternion(0.5f, -0.5f, -0.5f, -0.5f)); |
||
177 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, -0.707107f, -0.707107f, 0.0f)); |
||
178 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, -0.5f, -0.5f, 0.5f)); |
||
179 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, -0.707107f, 0.0f, 0.707107f)); |
||
180 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, -0.5f, 0.5f, 0.5f)); |
||
181 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.707107f, 0.0f, 0.707107f, 0.0f)); |
||
182 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, 0.5f, 0.5f, -0.5f)); |
||
183 | CheckllRot2Euler(new LSL_Types.Quaternion(0.0f, -0.707107f, 0.0f, -0.707107f)); |
||
184 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, -0.5f, -0.5f, -0.5f)); |
||
185 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.707107f, 0.0f, -0.707107f, 0.0f)); |
||
186 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.5f, 0.5f, -0.5f, 0.5f)); |
||
187 | |||
188 | // A couple of messy rotations. |
||
189 | CheckllRot2Euler(new LSL_Types.Quaternion(1.0f, 5.651f, -3.1f, 67.023f)); |
||
190 | CheckllRot2Euler(new LSL_Types.Quaternion(0.719188f, -0.408934f, -0.363998f, -0.427841f)); |
||
191 | |||
192 | // Some deliberately malicious rotations (intended on provoking singularity errors) |
||
193 | // The "f" suffexes are deliberately omitted. |
||
194 | CheckllRot2Euler(new LSL_Types.Quaternion(0.50001f, 0.50001f, 0.50001f, 0.50001f)); |
||
195 | // More malice. The "f" suffixes are deliberately omitted. |
||
196 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.701055, 0.092296, 0.701055, -0.092296)); |
||
197 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.183005, -0.683010, 0.183005, 0.683010)); |
||
198 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.430460, -0.560982, 0.430460, 0.560982)); |
||
199 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.701066, 0.092301, -0.701066, 0.092301)); |
||
200 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.183013, -0.683010, 0.183013, 0.683010)); |
||
201 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.183005, -0.683014, -0.183005, -0.683014)); |
||
202 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.353556, 0.612375, 0.353556, -0.612375)); |
||
203 | CheckllRot2Euler(new LSL_Types.Quaternion(0.353554, -0.612385, -0.353554, 0.612385)); |
||
204 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.560989, 0.430450, 0.560989, -0.430450)); |
||
205 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.183013, 0.683009, -0.183013, 0.683009)); |
||
206 | CheckllRot2Euler(new LSL_Types.Quaternion(0.430457, -0.560985, -0.430457, 0.560985)); |
||
207 | CheckllRot2Euler(new LSL_Types.Quaternion(0.353552, 0.612360, -0.353552, -0.612360)); |
||
208 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.499991, 0.500003, 0.499991, -0.500003)); |
||
209 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.353555, -0.612385, -0.353555, -0.612385)); |
||
210 | CheckllRot2Euler(new LSL_Types.Quaternion(0.701066, -0.092301, -0.701066, 0.092301)); |
||
211 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.499991, 0.500007, 0.499991, -0.500007)); |
||
212 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.683002, 0.183016, -0.683002, 0.183016)); |
||
213 | CheckllRot2Euler(new LSL_Types.Quaternion(0.430458, 0.560982, 0.430458, 0.560982)); |
||
214 | CheckllRot2Euler(new LSL_Types.Quaternion(0.499991, -0.500003, -0.499991, 0.500003)); |
||
215 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.183009, 0.683011, -0.183009, 0.683011)); |
||
216 | CheckllRot2Euler(new LSL_Types.Quaternion(0.560975, -0.430457, 0.560975, -0.430457)); |
||
217 | CheckllRot2Euler(new LSL_Types.Quaternion(0.701055, 0.092300, 0.701055, 0.092300)); |
||
218 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.560990, 0.430459, -0.560990, 0.430459)); |
||
219 | CheckllRot2Euler(new LSL_Types.Quaternion(-0.092302, -0.701059, -0.092302, -0.701059)); |
||
220 | } |
||
221 | |||
222 | /// <summary> |
||
223 | /// Check an llRot2Euler conversion. |
||
224 | /// </summary> |
||
225 | /// <remarks> |
||
226 | /// Testing Rot2Euler this way instead of comparing against expected angles because |
||
227 | /// 1. There are several ways to get to the original Quaternion. For example a rotation |
||
228 | /// of PI and -PI will give the same result. But PI and -PI aren't equal. |
||
229 | /// 2. This method checks to see if the calculated angles from a quaternion can be used |
||
230 | /// to create a new quaternion to produce the same rotation. |
||
231 | /// However, can't compare the newly calculated quaternion against the original because |
||
232 | /// once again, there are multiple quaternions that give the same result. For instance |
||
233 | /// <X, Y, Z, S> == <-X, -Y, -Z, -S>. Additionally, the magnitude of S can be changed |
||
234 | /// and will still result in the same rotation if the values for X, Y, Z are also changed |
||
235 | /// to compensate. |
||
236 | /// However, if two quaternions represent the same rotation, then multiplying the first |
||
237 | /// quaternion by the conjugate of the second, will give a third quaternion representing |
||
238 | /// a zero rotation. This can be tested for by looking at the X, Y, Z values which should |
||
239 | /// be zero. |
||
240 | /// </remarks> |
||
241 | /// <param name="rot"></param> |
||
242 | private void CheckllRot2Euler(LSL_Types.Quaternion rot) |
||
243 | { |
||
244 | // Call LSL function to convert quaternion rotaion to euler radians. |
||
245 | LSL_Types.Vector3 eulerCalc = m_lslApi.llRot2Euler(rot); |
||
246 | // Now use the euler radians to recalculate a new quaternion rotation |
||
247 | LSL_Types.Quaternion newRot = m_lslApi.llEuler2Rot(eulerCalc); |
||
248 | // Multiple original quaternion by conjugate of quaternion calculated with angles. |
||
249 | LSL_Types.Quaternion check = rot * new LSL_Types.Quaternion(-newRot.x, -newRot.y, -newRot.z, newRot.s); |
||
250 | |||
251 | Assert.AreEqual(0.0, check.x, VECTOR_COMPONENT_ACCURACY, "TestllRot2Euler X bounds check fail"); |
||
252 | Assert.AreEqual(0.0, check.y, VECTOR_COMPONENT_ACCURACY, "TestllRot2Euler Y bounds check fail"); |
||
253 | Assert.AreEqual(0.0, check.z, VECTOR_COMPONENT_ACCURACY, "TestllRot2Euler Z bounds check fail"); |
||
254 | } |
||
255 | |||
256 | [Test] |
||
257 | public void TestllVecNorm() |
||
258 | { |
||
259 | TestHelpers.InMethod(); |
||
260 | |||
261 | // Check special case for normalizing zero vector. |
||
262 | CheckllVecNorm(new LSL_Types.Vector3(0.0d, 0.0d, 0.0d), new LSL_Types.Vector3(0.0d, 0.0d, 0.0d)); |
||
263 | // Check various vectors. |
||
264 | CheckllVecNorm(new LSL_Types.Vector3(10.0d, 25.0d, 0.0d), new LSL_Types.Vector3(0.371391d, 0.928477d, 0.0d)); |
||
265 | CheckllVecNorm(new LSL_Types.Vector3(1.0d, 0.0d, 0.0d), new LSL_Types.Vector3(1.0d, 0.0d, 0.0d)); |
||
266 | CheckllVecNorm(new LSL_Types.Vector3(-90.0d, 55.0d, 2.0d), new LSL_Types.Vector3(-0.853128d, 0.521356d, 0.018958d)); |
||
267 | CheckllVecNorm(new LSL_Types.Vector3(255.0d, 255.0d, 255.0d), new LSL_Types.Vector3(0.577350d, 0.577350d, 0.577350d)); |
||
268 | } |
||
269 | |||
270 | public void CheckllVecNorm(LSL_Types.Vector3 vec, LSL_Types.Vector3 vecNormCheck) |
||
271 | { |
||
272 | // Call LSL function to normalize the vector. |
||
273 | LSL_Types.Vector3 vecNorm = m_lslApi.llVecNorm(vec); |
||
274 | // Check each vector component against expected result. |
||
275 | Assert.AreEqual(vecNorm.x, vecNormCheck.x, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on x component"); |
||
276 | Assert.AreEqual(vecNorm.y, vecNormCheck.y, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on y component"); |
||
277 | Assert.AreEqual(vecNorm.z, vecNormCheck.z, VECTOR_COMPONENT_ACCURACY, "TestllVecNorm vector check fail on z component"); |
||
278 | } |
||
279 | } |
||
280 | } |