From 47e485e19bd474405c017a558e314bdf3b8848d1 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Thu, 20 Oct 2022 01:35:29 +0100 Subject: [PATCH] a few changtes to ubode --- .../PhysicsModules/ubOde/ODEDynamics.cs | 53 +++----- .../PhysicsModules/ubOde/ODEMeshWorker.cs | 125 +++++++----------- .../Region/PhysicsModules/ubOde/ODEPrim.cs | 11 +- 3 files changed, 72 insertions(+), 117 deletions(-) diff --git a/OpenSim/Region/PhysicsModules/ubOde/ODEDynamics.cs b/OpenSim/Region/PhysicsModules/ubOde/ODEDynamics.cs index f2ad0fa523..7ee0b9722e 100644 --- a/OpenSim/Region/PhysicsModules/ubOde/ODEDynamics.cs +++ b/OpenSim/Region/PhysicsModules/ubOde/ODEDynamics.cs @@ -666,7 +666,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde m_ffactor = 1f; } - public static Vector3 Xrot(Quaternion rot) + public static Vector3 Xrot(in Quaternion rot) { Vector3 vec; rot.Normalize(); // just in case @@ -676,7 +676,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde return vec; } - public static Vector3 Zrot(Quaternion rot) + public static Vector3 Zrot(in Quaternion rot) { Vector3 vec; rot.Normalize(); // just in case @@ -690,7 +690,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde private const float pi = MathF.PI; private const float halfpi = 0.5f * MathF.PI; - public static Vector3 ubRot2Euler(Quaternion rot) + public static Vector3 ubRot2Euler(in Quaternion rot) { // returns roll in X // pitch in Y @@ -732,7 +732,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde return vec; } - public static void GetRollPitch(Quaternion rot, out float roll, out float pitch) + public static void GetRollPitch(in Quaternion rot, out float roll, out float pitch) { // assuming rot is normalised // rot.Normalize(); @@ -766,30 +766,20 @@ namespace OpenSim.Region.PhysicsModule.ubOde SafeNativeMethods.BodyGetMass(Body, out SafeNativeMethods.Mass dmass); - SafeNativeMethods.Quaternion rot = SafeNativeMethods.BodyGetQuaternion(Body); - Quaternion objrotq = new(rot.X, rot.Y, rot.Z, rot.W); // rotq = rotation of object + Quaternion objrotq = SafeNativeMethods.BodyGetQuaternionOMV(Body); Quaternion rotq = objrotq; // rotq = rotation of object rotq *= m_referenceFrame; // rotq is now rotation in vehicle reference frame Quaternion irotq = Quaternion.Inverse(rotq); - SafeNativeMethods.Vector3 dvtmp; Vector3 tmpV; - Vector3 curVel; // velocity in world - Vector3 curAngVel; // angular velocity in world Vector3 force = Vector3.Zero; // actually linear aceleration until mult by mass in world frame Vector3 torque = Vector3.Zero;// actually angular aceleration until mult by Inertia in vehicle frame SafeNativeMethods.Vector3 dtorque = new(); - dvtmp = SafeNativeMethods.BodyGetLinearVel(Body); - curVel.X = dvtmp.X; - curVel.Y = dvtmp.Y; - curVel.Z = dvtmp.Z; + Vector3 curVel = SafeNativeMethods.BodyGetLinearVelOMV(Body); // velocity in world Vector3 curLocalVel = curVel * irotq; // current velocity in local - dvtmp = SafeNativeMethods.BodyGetAngularVel(Body); - curAngVel.X = dvtmp.X; - curAngVel.Y = dvtmp.Y; - curAngVel.Z = dvtmp.Z; + Vector3 curAngVel = SafeNativeMethods.BodyGetAngularVelOMV(Body); // angular velocity in world Vector3 curLocalAngVel = curAngVel * irotq; // current angular velocity in local float ldampZ = 0; @@ -852,7 +842,6 @@ namespace OpenSim.Region.PhysicsModule.ubOde // hover if (m_VhoverTimescale < 300 && rootPrim.m_prim_geom != IntPtr.Zero) { - // d.Vector3 pos = d.BodyGetPosition(Body); SafeNativeMethods.Vector3 pos = SafeNativeMethods.GeomGetPosition(rootPrim.m_prim_geom); pos.Z -= 0.21f; // minor offset that seems to be always there in sl @@ -908,7 +897,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde if (m_linearDeflectionEfficiency > 0) { float len = curVel.Length(); - if (len > 0.01) // if moving + if (len > 0.01f) // if moving { Vector3 atAxis; atAxis = Xrot(rotq); // where are we pointing to @@ -921,7 +910,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde tmpV -= curVel; // error to oposite float lens = tmpV.LengthSquared(); - if (len > 0.01 || lens > 0.01) // do nothing if close enougth + if (len > 0.01f || lens > 0.01f) // do nothing if close enougth { if (len < lens) tmpV = atAxis; @@ -1001,9 +990,9 @@ namespace OpenSim.Region.PhysicsModule.ubOde if (vfact > 1.0f) vfact = 1.0f; if (curLocalVel.X >= 0) - broll *= (1 + (vfact - 1) * m_bankingMix); + broll *= (1f + (vfact - 1f) * m_bankingMix); else - broll *= -(1 + (vfact - 1) * m_bankingMix); + broll *= -(1f + (vfact - 1f) * m_bankingMix); } // make z rot be in world Z not local as seems to be in sl @@ -1023,16 +1012,16 @@ namespace OpenSim.Region.PhysicsModule.ubOde } else { - m_amdampZ = 1 / m_angularFrictionTimescale.Z; + m_amdampZ = 1f / m_angularFrictionTimescale.Z; m_amdampY = m_amdampX; } } else { m_ampwr = 1.0f; - m_amdampX = 1 / m_angularFrictionTimescale.X; - m_amdampY = 1 / m_angularFrictionTimescale.Y; - m_amdampZ = 1 / m_angularFrictionTimescale.Z; + m_amdampX = 1f / m_angularFrictionTimescale.X; + m_amdampY = 1f / m_angularFrictionTimescale.Y; + m_amdampZ = 1f / m_angularFrictionTimescale.Z; } if(mousemode) @@ -1107,7 +1096,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde else { // angular motor - if (m_amEfect > 0.01 && m_angularMotorTimescale < 1000) + if (m_amEfect > 0.01 && m_angularMotorTimescale < 1000f) { tmpV = m_angularMotorDirection - curLocalAngVel; // velocity error tmpV *= m_amEfect / m_angularMotorTimescale; // error to correct in this timestep @@ -1165,26 +1154,26 @@ namespace OpenSim.Region.PhysicsModule.ubOde force += rootPrim.m_forceacc; rootPrim.m_forceacc = Vector3.Zero; - if (force.X != 0 || force.Y != 0 || force.Z != 0) + if (force.X != 0f || force.Y != 0f || force.Z != 0f) { SafeNativeMethods.BodyAddForce(Body, force.X, force.Y, force.Z); } - if (torque.X != 0 || torque.Y != 0 || torque.Z != 0) + if (torque.X != 0f || torque.Y != 0f || torque.Z != 0f) { torque *= m_referenceFrame; // to object frame - dtorque.X = torque.X ; + dtorque.X = torque.X; dtorque.Y = torque.Y; dtorque.Z = torque.Z; - SafeNativeMethods.MultiplyM3V3(out dvtmp, ref dmass.I, ref dtorque); + SafeNativeMethods.MultiplyM3V3(out SafeNativeMethods.Vector3 dvtmp, ref dmass.I, ref dtorque); SafeNativeMethods.BodyAddRelTorque(Body, dvtmp.X, dvtmp.Y, dvtmp.Z); // add torque in object frame } torque = rootPrim.m_torque; torque += rootPrim.m_angularForceacc; rootPrim.m_angularForceacc = Vector3.Zero; - if (torque.X != 0 || torque.Y != 0 || torque.Z != 0) + if (torque.X != 0f || torque.Y != 0f || torque.Z != 0f) SafeNativeMethods.BodyAddTorque(Body,torque.X, torque.Y, torque.Z); } } diff --git a/OpenSim/Region/PhysicsModules/ubOde/ODEMeshWorker.cs b/OpenSim/Region/PhysicsModules/ubOde/ODEMeshWorker.cs index 4ed862ae6d..e2dde5c678 100644 --- a/OpenSim/Region/PhysicsModules/ubOde/ODEMeshWorker.cs +++ b/OpenSim/Region/PhysicsModules/ubOde/ODEMeshWorker.cs @@ -3,7 +3,6 @@ */ using System; -using System.Collections.Concurrent; using OpenSim.Framework; using OpenSim.Region.PhysicsModules.SharedBase; using log4net; @@ -77,7 +76,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde private ObjectJobEngine workQueue; private bool m_running; - private readonly object m_threadLock = new object(); + private readonly object m_threadLock = new(); public ODEMeshWorker(ODEScene pScene, ILog pLog, IMesher pMesher, IConfig pConfig) { @@ -85,7 +84,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde m_log = pLog; m_mesher = pMesher; - if (pConfig != null) + if (pConfig is not null) { meshSculptedPrim = pConfig.GetBoolean("mesh_sculpted_prim", meshSculptedPrim); meshSculptLOD = pConfig.GetFloat("mesh_lod", meshSculptLOD); @@ -96,8 +95,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde Util.FireAndForget(DoCacheExpire, null, "OdeCacheExpire", false); lock(m_threadLock) { - if(workQueue == null) - workQueue = new ObjectJobEngine(DoWork, "OdeMeshWorker"); + workQueue ??= new ObjectJobEngine(DoWork, "OdeMeshWorker"); } } @@ -113,8 +111,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde private void DoWork(object rep) { - ODEPhysRepData nextRep = rep as ODEPhysRepData; - if (m_running && nextRep != null && m_scene.haveActor(nextRep.actor)) + if (m_running && rep is ODEPhysRepData nextRep && m_scene.haveActor(nextRep.actor)) { switch (nextRep.comand) { @@ -148,11 +145,13 @@ namespace OpenSim.Region.PhysicsModule.ubOde public void ChangeActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs, Vector3 size, byte shapetype) { - ODEPhysRepData repData = new ODEPhysRepData(); - repData.actor = actor; - repData.pbs = pbs; - repData.size = size; - repData.shapetype = shapetype; + ODEPhysRepData repData = new() + { + actor = actor, + pbs = pbs, + size = size, + shapetype = shapetype + }; CheckMesh(repData); CalcVolumeData(repData); @@ -163,11 +162,13 @@ namespace OpenSim.Region.PhysicsModule.ubOde public ODEPhysRepData NewActorPhysRep(PhysicsActor actor, PrimitiveBaseShape pbs, Vector3 size, byte shapetype) { - ODEPhysRepData repData = new ODEPhysRepData(); - repData.actor = actor; - repData.pbs = pbs; - repData.size = size; - repData.shapetype = shapetype; + ODEPhysRepData repData = new() + { + actor = actor, + pbs = pbs, + size = size, + shapetype = shapetype + }; CheckMesh(repData); CalcVolumeData(repData); @@ -204,14 +205,10 @@ namespace OpenSim.Region.PhysicsModule.ubOde { IMesh mesh = repData.mesh; - if (mesh != null) + if (mesh is not null) { - IntPtr vertices, indices; - int vertexCount, indexCount; - int vertexStride, triStride; - - mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount); - mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount); + mesh.getVertexListAsPtrToFloatArray(out _, out _, out int vertexCount); + mesh.getIndexListAsPtrToIntArray(out _, out _, out int indexCount); if (vertexCount == 0 || indexCount == 0) { @@ -245,7 +242,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde } } else - repData.pbs.SculptData = Utils.EmptyBytes; + repData.pbs.SculptData = Array.Empty(); } public void DoRepDataGetMesh(ODEPhysRepData repData) @@ -256,7 +253,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde if (repData.meshState != MeshState.loadingAsset) return; - if (repData.assetID == null || repData.assetID.Value.IsZero()) + if (repData.assetID is null || repData.assetID.Value.IsZero()) return; if (repData.assetID != repData.pbs.SculptTexture) @@ -272,9 +269,9 @@ namespace OpenSim.Region.PhysicsModule.ubOde } RequestAssetDelegate assetProvider = m_scene.RequestAssetMethod; - if (assetProvider == null) + if (assetProvider is null) return; - ODEAssetRequest asr = new ODEAssetRequest(this, assetProvider, repData, m_log); + ODEAssetRequest asr = new(this, assetProvider, repData, m_log); } @@ -426,17 +423,14 @@ namespace OpenSim.Region.PhysicsModule.ubOde return; } - IMesh mesh = null; - Vector3 size = repData.size; - int clod = (int)LevelOfDetail.High; byte shapetype = repData.shapetype; bool convex = shapetype == 2; - mesh = m_mesher.GetMesh(actor.Name, pbs, size, clod, true, convex); + IMesh mesh = m_mesher.GetMesh(actor.Name, pbs, size, clod, true, convex); - if (mesh == null) + if (mesh is null) { if (pbs.SculptEntry) { @@ -454,7 +448,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde { repData.meshState = MeshState.needMesh; mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true); - if (mesh == null) + if (mesh is null) { repData.meshState = MeshState.MeshFailed; return; @@ -473,7 +467,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde repData.assetID = pbs.SculptTexture; } - pbs.SculptData = Utils.EmptyBytes; + pbs.SculptData = Array.Empty(); return ; } @@ -506,7 +500,6 @@ namespace OpenSim.Region.PhysicsModule.ubOde repData.meshState = MeshState.noNeed; - IMesh mesh = null; Vector3 size = repData.size; byte shapetype = repData.shapetype; @@ -521,9 +514,9 @@ namespace OpenSim.Region.PhysicsModule.ubOde clod = (int)LevelOfDetail.Low; } - mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true); + IMesh mesh = m_mesher.CreateMesh(actor.Name, pbs, size, clod, true, convex, true); - if (mesh == null) + if (mesh is null) { if (pbs.SculptEntry) { @@ -532,7 +525,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde repData.assetID = pbs.SculptTexture; - if (pbs.SculptData == null || pbs.SculptData.Length == 0) + if (pbs.SculptData is null || pbs.SculptData.Length == 0) { repData.meshState = MeshState.needAsset; return; @@ -543,7 +536,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde repData.mesh = mesh; repData.pbs.SculptData = Utils.EmptyBytes; - if (mesh == null) + if (mesh is null) { if (pbs.SculptEntry) repData.meshState = MeshState.AssetFailed; @@ -558,7 +551,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde return; } - private void CalculateBasicPrimVolume(ODEPhysRepData repData) + private static void CalculateBasicPrimVolume(ODEPhysRepData repData) { Vector3 _size = repData.size; @@ -572,7 +565,7 @@ namespace OpenSim.Region.PhysicsModule.ubOde PrimitiveBaseShape _pbs = repData.pbs; float tmp; - float hollowAmount = (float)_pbs.ProfileHollow * 2.0e-5f; + float hollowAmount = ((float)_pbs.ProfileHollow) * 2.0e-5f; float hollowVolume = hollowAmount * hollowAmount; switch (_pbs.ProfileShape) @@ -728,9 +721,9 @@ namespace OpenSim.Region.PhysicsModule.ubOde hollowVolume *= 0.909f; break; - // case HollowShape.Triangle: - // hollowVolume *= .827f; - // break; + //case HollowShape.Triangle: + // hollowVolume *= .827f; + // break; default: hollowVolume = 0; break; @@ -865,20 +858,11 @@ namespace OpenSim.Region.PhysicsModule.ubOde repData.volume = volume; } - private void CalcVolumeData(ODEPhysRepData repData) + private static void CalcVolumeData(ODEPhysRepData repData) { - if (repData.hasOBB) + if (!repData.hasOBB) { - Vector3 OBB = repData.OBB; - } - else - { - Vector3 OBB = repData.size; - OBB.X *= 0.5f; - OBB.Y *= 0.5f; - OBB.Z *= 0.5f; - - repData.OBB = OBB; + repData.OBB = repData.size * 0.5f; repData.OBBOffset = Vector3.Zero; } @@ -888,9 +872,9 @@ namespace OpenSim.Region.PhysicsModule.ubOde public class ODEAssetRequest { - ODEMeshWorker m_worker; - private ILog m_log; - ODEPhysRepData repData; + private readonly ODEMeshWorker m_worker; + private readonly ILog m_log; + private readonly ODEPhysRepData repData; public ODEAssetRequest(ODEMeshWorker pWorker, RequestAssetDelegate provider, ODEPhysRepData pRepData, ILog plog) @@ -899,27 +883,16 @@ namespace OpenSim.Region.PhysicsModule.ubOde m_log = plog; repData = pRepData; - repData.meshState = MeshState.AssetFailed; - if (provider == null) - return; - - if (repData.assetID == null) - return; - - UUID assetID = (UUID) repData.assetID; - if (assetID.IsZero()) - return; - repData.meshState = MeshState.loadingAsset; - provider(assetID, ODEassetReceived); + provider((UUID)repData.assetID, ODEassetReceived); } void ODEassetReceived(AssetBase asset) { repData.meshState = MeshState.AssetFailed; - if (asset != null) + if (asset is not null) { - if (asset.Data != null && asset.Data.Length > 0) + if (asset.Data is not null && asset.Data.Length > 0) { repData.meshState = MeshState.noNeed; @@ -928,8 +901,8 @@ namespace OpenSim.Region.PhysicsModule.ubOde if (repData.pbs.SculptTexture != repData.assetID) return; -// repData.pbs.SculptData = new byte[asset.Data.Length]; -// asset.Data.CopyTo(repData.pbs.SculptData,0); + //repData.pbs.SculptData = new byte[asset.Data.Length]; + //asset.Data.CopyTo(repData.pbs.SculptData,0); repData.pbs.SculptData = asset.Data; repData.meshState = MeshState.AssetOK; m_worker.AssetLoaded(repData); diff --git a/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs b/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs index bc62d2f90b..52a1d75f8e 100644 --- a/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs +++ b/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs @@ -1037,15 +1037,8 @@ namespace OpenSim.Region.PhysicsModule.ubOde SafeNativeMethods.AllocateODEDataForThread(0); if(Body != IntPtr.Zero) { - SafeNativeMethods.Vector3 dtmp = SafeNativeMethods.BodyGetAngularVel(Body); - m_rotationalVelocity.X = dtmp.X; - m_rotationalVelocity.Y = dtmp.Y; - m_rotationalVelocity.Z = dtmp.Z; - - dtmp = SafeNativeMethods.BodyGetLinearVel(Body); - _velocity.X = dtmp.X; - _velocity.Y = dtmp.Y; - _velocity.Z = dtmp.Z; + m_rotationalVelocity = SafeNativeMethods.BodyGetAngularVelOMV(Body); + _velocity = SafeNativeMethods.BodyGetLinearVelOMV(Body); SafeNativeMethods.BodySetLinearVel(Body, 0, 0, 0); // stop it SafeNativeMethods.BodySetAngularVel(Body, 0, 0, 0);