From 5cd77c50627873fcfb4ce4328adb188659860503 Mon Sep 17 00:00:00 2001 From: AndresTraks Date: Fri, 14 Jul 2017 22:22:57 +0300 Subject: [PATCH] Implement AlignedCollisionObjectArray.IndexOf and Contains --- .../LinearMath/AlignedCollisionObjectArray.cs | 5 +- BulletSharpPInvoke/UnsafeNativeMethods.cs | 2 + .../test/AlignedCollisionObjectArrayTests.cs | 84 +++++++++++++++++++ .../test/BulletSharpTest.csproj | 1 + libbulletc/src/btAlignedObjectArray_wrap.cpp | 5 ++ libbulletc/src/btAlignedObjectArray_wrap.h | 1 + 6 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 BulletSharpPInvoke/test/AlignedCollisionObjectArrayTests.cs diff --git a/BulletSharpPInvoke/LinearMath/AlignedCollisionObjectArray.cs b/BulletSharpPInvoke/LinearMath/AlignedCollisionObjectArray.cs index 414f056d..4b19df59 100644 --- a/BulletSharpPInvoke/LinearMath/AlignedCollisionObjectArray.cs +++ b/BulletSharpPInvoke/LinearMath/AlignedCollisionObjectArray.cs @@ -85,7 +85,7 @@ namespace BulletSharp public int IndexOf(CollisionObject item) { - throw new NotImplementedException(); + return btAlignedObjectArray_btCollisionObjectPtr_findLinearSearch2(_native, item.Native); } public void Insert(int index, CollisionObject item) @@ -180,8 +180,7 @@ namespace BulletSharp public bool Contains(CollisionObject item) { - //return btAlignedObjectArray_btCollisionObjectPtr_findLinearSearch(_native, item._native) != Count; - throw new NotImplementedException(); + return IndexOf(item) != -1; } public void CopyTo(CollisionObject[] array, int arrayIndex) diff --git a/BulletSharpPInvoke/UnsafeNativeMethods.cs b/BulletSharpPInvoke/UnsafeNativeMethods.cs index aca1ecc7..1fa90b65 100644 --- a/BulletSharpPInvoke/UnsafeNativeMethods.cs +++ b/BulletSharpPInvoke/UnsafeNativeMethods.cs @@ -161,6 +161,8 @@ namespace BulletSharp [DllImport(Native.Dll, CallingConvention = Native.Conv)] public static extern IntPtr btAlignedObjectArray_btCollisionObjectPtr_at(IntPtr obj, int n); [DllImport(Native.Dll, CallingConvention = Native.Conv)] + public static extern int btAlignedObjectArray_btCollisionObjectPtr_findLinearSearch2(IntPtr obj, IntPtr key); + [DllImport(Native.Dll, CallingConvention = Native.Conv)] public static extern void btAlignedObjectArray_btCollisionObjectPtr_push_back(IntPtr obj, IntPtr val); [DllImport(Native.Dll, CallingConvention = Native.Conv)] public static extern void btAlignedObjectArray_btCollisionObjectPtr_resizeNoInitialize(IntPtr obj, int newSize); diff --git a/BulletSharpPInvoke/test/AlignedCollisionObjectArrayTests.cs b/BulletSharpPInvoke/test/AlignedCollisionObjectArrayTests.cs new file mode 100644 index 00000000..0693ed09 --- /dev/null +++ b/BulletSharpPInvoke/test/AlignedCollisionObjectArrayTests.cs @@ -0,0 +1,84 @@ +using BulletSharp; +using NUnit.Framework; + +namespace BulletSharpTest +{ + [TestFixture] + [Category("AlignedObjectArray")] + class AlignedCollisionObjectArrayTests + { + private DefaultCollisionConfiguration _conf; + private CollisionDispatcher _dispatcher; + private DbvtBroadphase _broadphase; + private SequentialImpulseConstraintSolver _solver; + private DiscreteDynamicsWorld _world; + private CollisionShape _shape; + private RigidBody _body1; + private RigidBody _body2; + + [Test] + public void AlignedCollisionObjectArrayTest() + { + var worldArray = _world.CollisionObjectArray; + Assert.IsEmpty(worldArray); + + _world.AddRigidBody(_body1); + _world.AddRigidBody(_body2); + Assert.That(worldArray, Has.Count.EqualTo(2)); + Assert.AreEqual(2, _world.NumCollisionObjects); + + Assert.True(worldArray.Contains(_body1)); + Assert.True(worldArray.Contains(_body2)); + + Assert.AreEqual(0, worldArray.IndexOf(_body1)); + Assert.AreEqual(1, worldArray.IndexOf(_body2)); + + _world.RemoveRigidBody(_body1); + Assert.That(worldArray, Has.Count.EqualTo(1)); + Assert.AreEqual(1, _world.NumCollisionObjects); + + _world.RemoveRigidBody(_body1); + Assert.That(worldArray, Has.Count.EqualTo(1)); + Assert.AreEqual(1, _world.NumCollisionObjects); + + worldArray.Remove(_body2); + Assert.AreEqual(0, _world.NumCollisionObjects); + + _world.AddRigidBody(_body1); + _world.AddRigidBody(_body2); + worldArray.Clear(); + Assert.AreEqual(0, _world.NumCollisionObjects); + } + + [OneTimeSetUp] + public void SetUp() + { + _conf = new DefaultCollisionConfiguration(); + _dispatcher = new CollisionDispatcher(_conf); + _broadphase = new DbvtBroadphase(); + _solver = new SequentialImpulseConstraintSolver(); + _world = new DiscreteDynamicsWorld(_dispatcher, _broadphase, _solver, _conf); + + _shape = new BoxShape(1); + using (var bodyInfo = new RigidBodyConstructionInfo(1, null, _shape)) + { + _body1 = new RigidBody(bodyInfo); + _body2 = new RigidBody(bodyInfo); + } + } + + [OneTimeTearDown] + public void TearDown() + { + _world.Dispose(); + _solver.Dispose(); + _broadphase.Dispose(); + _dispatcher.Dispose(); + _conf.Dispose(); + + _shape.Dispose(); + _body1.Dispose(); + _body2.Dispose(); + } + } +} diff --git a/BulletSharpPInvoke/test/BulletSharpTest.csproj b/BulletSharpPInvoke/test/BulletSharpTest.csproj index 534bb523..334a8811 100644 --- a/BulletSharpPInvoke/test/BulletSharpTest.csproj +++ b/BulletSharpPInvoke/test/BulletSharpTest.csproj @@ -59,6 +59,7 @@ + diff --git a/libbulletc/src/btAlignedObjectArray_wrap.cpp b/libbulletc/src/btAlignedObjectArray_wrap.cpp index f45a16fc..c0cd81e3 100644 --- a/libbulletc/src/btAlignedObjectArray_wrap.cpp +++ b/libbulletc/src/btAlignedObjectArray_wrap.cpp @@ -32,6 +32,11 @@ btCollisionObject* btAlignedObjectArray_btCollisionObjectPtr_at(btAlignedObjectA return obj->at(n); } +int btAlignedObjectArray_btCollisionObjectPtr_findLinearSearch2(btAlignedObjectArray_btCollisionObjectPtr* obj, btCollisionObject* key) +{ + return obj->findLinearSearch2(key); +} + void btAlignedObjectArray_btCollisionObjectPtr_push_back(btAlignedObjectArray_btCollisionObjectPtr* obj, btCollisionObject* val) { obj->push_back(val); diff --git a/libbulletc/src/btAlignedObjectArray_wrap.h b/libbulletc/src/btAlignedObjectArray_wrap.h index cb5de3c6..5c2efa8f 100644 --- a/libbulletc/src/btAlignedObjectArray_wrap.h +++ b/libbulletc/src/btAlignedObjectArray_wrap.h @@ -9,6 +9,7 @@ extern "C" { EXPORT int btAlignedObjectArray_btBroadphasePair_size(btAlignedObjectArray_btBroadphasePair* obj); EXPORT btCollisionObject* btAlignedObjectArray_btCollisionObjectPtr_at(btAlignedObjectArray_btCollisionObjectPtr* obj, int n); + EXPORT int btAlignedObjectArray_btCollisionObjectPtr_findLinearSearch2(btAlignedObjectArray_btCollisionObjectPtr* obj, btCollisionObject* key); EXPORT void btAlignedObjectArray_btCollisionObjectPtr_push_back(btAlignedObjectArray_btCollisionObjectPtr* obj, btCollisionObject* val); EXPORT void btAlignedObjectArray_btCollisionObjectPtr_resizeNoInitialize(btAlignedObjectArray_btCollisionObjectPtr* obj, int newSize); EXPORT int btAlignedObjectArray_btCollisionObjectPtr_size(btAlignedObjectArray_btCollisionObjectPtr* obj);