From c0e389cfff1b43bce8fb8e706b6617bed95381a9 Mon Sep 17 00:00:00 2001 From: Charles Krinke Date: Fri, 18 Jul 2008 19:09:51 +0000 Subject: [PATCH] Mantis#1778. Thank you kindly, Junta_Kohime for a patch that: llRot2Left and llRot2Up functions modified, using fast algebric calculations instead of vectors and quaternions products. The accuracy is the same. Normalization is now implemented. --- .../Common/LSL_BuiltIn_Commands.cs | 33 +++++++++++++++++-- .../Shared/Api/Implementation/LSL_Api.cs | 32 ++++++++++++++++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs b/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs index 42c5abc0b3..cbd4db2eea 100644 --- a/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs +++ b/OpenSim/Region/ScriptEngine/Common/LSL_BuiltIn_Commands.cs @@ -450,14 +450,43 @@ namespace OpenSim.Region.ScriptEngine.Common public LSL_Types.Vector3 llRot2Left(LSL_Types.Quaternion r) { m_host.AddScriptLPS(1); - return (new LSL_Types.Vector3(0, 1, 0) * r); + double x,y,z,m; + m = Math.Sqrt(r.x*r.x+r.y*r.y+r.z*r.z+r.s*r.s); + // m is always greater than zero + if (m!=1) // if m is not equal to 1 then Rotation needs to be normalized + { + r.x/=m; + r.y/=m; + r.z/=m; + r.s/=m; + } + // Fast Algebric Calculations instead of Vectors & Quaternions Product + x = 2*(r.x*r.y-r.z*r.s); + y = -r.x*r.x+r.y*r.y-r.z*r.z+r.s*r.s; + z = 2*(r.x*r.s+r.y*r.z); + return (new LSL_Types.Vector3(x,y,z)); } public LSL_Types.Vector3 llRot2Up(LSL_Types.Quaternion r) { m_host.AddScriptLPS(1); - return (new LSL_Types.Vector3(0, 0, 1) * r); + double x,y,z,m; + m = Math.Sqrt(r.x*r.x+r.y*r.y+r.z*r.z+r.s*r.s); + // m is always greater than zero + if (m!=1) // if m is not equal to 1 then Rotation needs to be normalized + { + r.x/=m; + r.y/=m; + r.z/=m; + r.s/=m; + } + // Fast Algebric Calculations instead of Vectors & Quaternions Product + x = 2*(r.x*r.z+r.y*r.s); + y = 2*(-r.x*r.s+r.y*r.z); + z = -r.x*r.x-r.y*r.y+r.z*r.z+r.s*r.s; + return (new LSL_Types.Vector3(x,y,z)); } + public LSL_Types.Quaternion llRotBetween(LSL_Types.Vector3 a, LSL_Types.Vector3 b) { //A and B should both be normalized diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs index e396bb4d35..decd2d0197 100644 --- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs @@ -437,13 +437,41 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api public LSL_Types.Vector3 llRot2Left(LSL_Types.Quaternion r) { m_host.AddScriptLPS(1); - return (new LSL_Types.Vector3(0, 1, 0) * r); + double x,y,z,m; + m = Math.Sqrt(r.x*r.x+r.y*r.y+r.z*r.z+r.s*r.s); + // m is always greater than zero + if (m!=1) // if m is not equal to 1 then Rotation needs to be normalized + { + r.x/=m; + r.y/=m; + r.z/=m; + r.s/=m; + } + // Fast Algebric Calculations instead of Vectors & Quaternions Product + x = 2*(r.x*r.y-r.z*r.s); + y = -r.x*r.x+r.y*r.y-r.z*r.z+r.s*r.s; + z = 2*(r.x*r.s+r.y*r.z); + return (new LSL_Types.Vector3(x,y,z)); } public LSL_Types.Vector3 llRot2Up(LSL_Types.Quaternion r) { m_host.AddScriptLPS(1); - return (new LSL_Types.Vector3(0, 0, 1) * r); + double x,y,z,m; + m = Math.Sqrt(r.x*r.x+r.y*r.y+r.z*r.z+r.s*r.s); + // m is always greater than zero + if (m!=1) // if m is not equal to 1 then Rotation needs to be normalized + { + r.x/=m; + r.y/=m; + r.z/=m; + r.s/=m; + } + // Fast Algebric Calculations instead of Vectors & Quaternions Product + x = 2*(r.x*r.z+r.y*r.s); + y = 2*(-r.x*r.s+r.y*r.z); + z = -r.x*r.x-r.y*r.y+r.z*r.z+r.s*r.s; + return (new LSL_Types.Vector3(x,y,z)); } public LSL_Types.Quaternion llRotBetween(LSL_Types.Vector3 a, LSL_Types.Vector3 b)