mirror of
https://github.com/bulletphysics/bullet3
synced 2024-12-14 13:50:04 +00:00
Merge remote-tracking branch 'bp/master'
This commit is contained in:
commit
d7cbe8dd26
@ -1385,6 +1385,15 @@ const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus() {
|
||||
|
||||
if (serverCmd.m_type == CMD_SYNC_USER_DATA_COMPLETED) {
|
||||
B3_PROFILE("CMD_SYNC_USER_DATA_COMPLETED");
|
||||
// Remove all cached user data entries.
|
||||
for(int i=0; i<m_data->m_bodyJointMap.size(); i++)
|
||||
{
|
||||
BodyJointInfoCache** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
|
||||
if (bodyJointsPtr && *bodyJointsPtr)
|
||||
{
|
||||
(*bodyJointsPtr)->m_jointToUserDataMap.clear();
|
||||
}
|
||||
}
|
||||
const int numIdentifiers = serverCmd.m_syncUserDataArgs.m_numUserDataIdentifiers;
|
||||
if (numIdentifiers > 0) {
|
||||
m_data->m_tempBackupServerStatus = m_data->m_lastServerStatus;
|
||||
|
@ -695,6 +695,7 @@ void PhysicsDirect::processAddUserData(const struct SharedMemoryStatus& serverCm
|
||||
(userDataCachePtr)->m_userDataMap.insert(userDataGlobalId.m_userDataId, SharedMemoryUserData(serverCmd.m_userDataResponseArgs.m_key));
|
||||
userDataPtr = (userDataCachePtr)->m_userDataMap[userDataGlobalId.m_userDataId];
|
||||
userDataPtr->replaceValue(dataStream,serverCmd.m_userDataResponseArgs.m_valueLength,userDataValue.m_type);
|
||||
(userDataCachePtr)->m_keyToUserDataIdMap.insert(serverCmd.m_userDataResponseArgs.m_key, userDataGlobalId.m_userDataId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1127,6 +1128,15 @@ void PhysicsDirect::postProcessStatus(const struct SharedMemoryStatus& serverCmd
|
||||
case CMD_SYNC_USER_DATA_COMPLETED:
|
||||
{
|
||||
B3_PROFILE("CMD_SYNC_USER_DATA_COMPLETED");
|
||||
// Remove all cached user data entries.
|
||||
for(int i=0; i<m_data->m_bodyJointMap.size(); i++)
|
||||
{
|
||||
BodyJointInfoCache2** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
|
||||
if (bodyJointsPtr && *bodyJointsPtr)
|
||||
{
|
||||
(*bodyJointsPtr)->m_jointToUserDataMap.clear();
|
||||
}
|
||||
}
|
||||
const int numIdentifiers = serverCmd.m_syncUserDataArgs.m_numUserDataIdentifiers;
|
||||
b3UserDataGlobalIdentifier *identifiers = new b3UserDataGlobalIdentifier[numIdentifiers];
|
||||
memcpy(identifiers, &m_data->m_bulletStreamDataServerToClient[0], numIdentifiers * sizeof(b3UserDataGlobalIdentifier));
|
||||
|
239
examples/pybullet/unittests/userDataTest.py
Normal file
239
examples/pybullet/unittests/userDataTest.py
Normal file
@ -0,0 +1,239 @@
|
||||
import unittest
|
||||
import pybullet
|
||||
|
||||
from pybullet_utils import bullet_client
|
||||
|
||||
PLANE_PATH = "plane.urdf"
|
||||
ROBOT_PATH = "r2d2.urdf"
|
||||
|
||||
|
||||
class TestUserDataMethods(unittest.TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
TestUserDataMethods.server = bullet_client.BulletClient(connection_mode=pybullet.SHARED_MEMORY_SERVER)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
del TestUserDataMethods.server
|
||||
|
||||
def setUp(self):
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
def tearDown(self):
|
||||
self.client.resetSimulation()
|
||||
del self.client
|
||||
|
||||
def testAddUserData(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
# Retrieve user data and make sure it's correct.
|
||||
self.assertEqual(b"MyValue1", self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(b"MyValue2", self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(b"MyValue3", self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(b"MyValue4", self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
# Disconnect/reconnect and make sure that the user data is synced back.
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(b"MyValue1", self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(b"MyValue2", self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(b"MyValue3", self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(b"MyValue4", self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
self.client.resetSimulation()
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
|
||||
def testGetNumUserData(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
self.assertEqual(4, self.client.getNumUserData(plane_id, 0))
|
||||
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(4, self.client.getNumUserData(plane_id, 0))
|
||||
|
||||
|
||||
def testReplaceUserData(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid = self.client.addUserData(plane_id, 0, "MyKey", "MyValue")
|
||||
|
||||
self.assertEqual(b"MyValue", self.client.getUserData(plane_id, 0, uid))
|
||||
|
||||
new_uid = self.client.addUserData(plane_id, 0, "MyKey", "MyNewValue")
|
||||
self.assertEqual(uid, new_uid)
|
||||
self.assertEqual(b"MyNewValue", self.client.getUserData(plane_id, 0, uid))
|
||||
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(b"MyNewValue", self.client.getUserData(plane_id, 0, uid))
|
||||
|
||||
def testGetUserDataId(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
self.assertEqual(uid1, self.client.getUserDataId(plane_id, 0, "MyKey1"))
|
||||
self.assertEqual(uid2, self.client.getUserDataId(plane_id, 0, "MyKey2"))
|
||||
self.assertEqual(uid3, self.client.getUserDataId(plane_id, 0, "MyKey3"))
|
||||
self.assertEqual(uid4, self.client.getUserDataId(plane_id, 0, "MyKey4"))
|
||||
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(uid1, self.client.getUserDataId(plane_id, 0, "MyKey1"))
|
||||
self.assertEqual(uid2, self.client.getUserDataId(plane_id, 0, "MyKey2"))
|
||||
self.assertEqual(uid3, self.client.getUserDataId(plane_id, 0, "MyKey3"))
|
||||
self.assertEqual(uid4, self.client.getUserDataId(plane_id, 0, "MyKey4"))
|
||||
|
||||
|
||||
def testRemoveUserData(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
self.client.removeUserData(plane_id, 0, uid2)
|
||||
|
||||
self.assertEqual(3, self.client.getNumUserData(plane_id, 0))
|
||||
self.assertEqual(-1, self.client.getUserDataId(plane_id, 0, "MyKey2"))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(b"MyValue1", self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(b"MyValue3", self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(b"MyValue4", self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(3, self.client.getNumUserData(plane_id, 0))
|
||||
self.assertEqual(-1, self.client.getUserDataId(plane_id, 0, "MyKey2"))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(b"MyValue1", self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(b"MyValue3", self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(b"MyValue4", self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
|
||||
def testIterateAllUserData(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
entries = set()
|
||||
for i in range(self.client.getNumUserData(plane_id, 0)):
|
||||
userDataId, key = self.client.getUserDataInfo(plane_id, 0, i)
|
||||
value = self.client.getUserData(plane_id, 0, userDataId);
|
||||
entries.add((userDataId, key, value))
|
||||
|
||||
self.assertTrue((uid1, b"MyKey1", b"MyValue1") in entries)
|
||||
self.assertTrue((uid2, b"MyKey2", b"MyValue2") in entries)
|
||||
self.assertTrue((uid3, b"MyKey3", b"MyValue3") in entries)
|
||||
self.assertTrue((uid4, b"MyKey4", b"MyValue4") in entries)
|
||||
self.assertEqual(4, len(entries))
|
||||
|
||||
|
||||
def testRemoveBody(self):
|
||||
plane_id = self.client.loadURDF(PLANE_PATH)
|
||||
uid1 = self.client.addUserData(plane_id, 0, "MyKey1", "MyValue1")
|
||||
uid2 = self.client.addUserData(plane_id, 0, "MyKey2", "MyValue2")
|
||||
uid3 = self.client.addUserData(plane_id, 0, "MyKey3", "MyValue3")
|
||||
uid4 = self.client.addUserData(plane_id, 0, "MyKey4", "MyValue4")
|
||||
|
||||
self.client.removeBody(plane_id)
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
del self.client
|
||||
self.client = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid1))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid2))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid3))
|
||||
self.assertEqual(None, self.client.getUserData(plane_id, 0, uid4))
|
||||
|
||||
def testMultipleBodies(self):
|
||||
plane1 = self.client.loadURDF(PLANE_PATH)
|
||||
plane2 = self.client.loadURDF(PLANE_PATH)
|
||||
|
||||
uid1 = self.client.addUserData(plane1, 0, "MyKey1", "This is plane 1 - 1")
|
||||
uid2 = self.client.addUserData(plane1, 0, "MyKey2", "This is plane 1 - 2")
|
||||
|
||||
uid3 = self.client.addUserData(plane2, 0, "MyKey1", "This is plane 2 - 1")
|
||||
uid4 = self.client.addUserData(plane2, 0, "MyKey2", "This is plane 2 - 2")
|
||||
uid5 = self.client.addUserData(plane2, 0, "MyKey3", "This is plane 2 - 3")
|
||||
|
||||
self.assertEqual(b"This is plane 1 - 1", self.client.getUserData(plane1, 0, self.client.getUserDataId(plane1, 0, "MyKey1")))
|
||||
self.assertEqual(b"This is plane 1 - 2", self.client.getUserData(plane1, 0, self.client.getUserDataId(plane1, 0, "MyKey2")))
|
||||
|
||||
self.assertEqual(b"This is plane 2 - 1", self.client.getUserData(plane2, 0, self.client.getUserDataId(plane2, 0, "MyKey1")))
|
||||
self.assertEqual(b"This is plane 2 - 2", self.client.getUserData(plane2, 0, self.client.getUserDataId(plane2, 0, "MyKey2")))
|
||||
self.assertEqual(b"This is plane 2 - 3", self.client.getUserData(plane2, 0, self.client.getUserDataId(plane2, 0, "MyKey3")))
|
||||
|
||||
|
||||
def testMultipleLinks(self):
|
||||
body_id = self.client.loadURDF(ROBOT_PATH)
|
||||
num_links = self.client.getNumJoints(body_id)
|
||||
|
||||
self.assertTrue(num_links > 1)
|
||||
|
||||
for link_index in range(num_links):
|
||||
uid1 = self.client.addUserData(body_id, link_index, "MyKey1", "Value1 for link %s" % link_index)
|
||||
uid2 = self.client.addUserData(body_id, link_index, "MyKey2", "Value2 for link %s" % link_index)
|
||||
|
||||
for link_index in range(num_links):
|
||||
uid1 = self.client.getUserDataId(body_id, link_index, "MyKey1")
|
||||
uid2 = self.client.getUserDataId(body_id, link_index, "MyKey2")
|
||||
self.assertEqual(("Value1 for link %s" % link_index).encode(), self.client.getUserData(body_id, link_index, uid1))
|
||||
self.assertEqual(("Value2 for link %s" % link_index).encode(), self.client.getUserData(body_id, link_index, uid2))
|
||||
|
||||
def testMultipleClients(self):
|
||||
client1 = self.client
|
||||
client2 = bullet_client.BulletClient(pybullet.SHARED_MEMORY)
|
||||
|
||||
plane_id = client1.loadURDF(PLANE_PATH)
|
||||
client2.syncBodyInfo()
|
||||
|
||||
# Add user data on client 1, check on client 1
|
||||
uid = client1.addUserData(plane_id, 0, "MyKey", "MyValue")
|
||||
self.assertEqual(None, client2.getUserData(plane_id, 0, uid))
|
||||
client2.syncUserData()
|
||||
self.assertEqual(b"MyValue", client2.getUserData(plane_id, 0, uid))
|
||||
|
||||
# Overwrite the value on client 2, check on client 1
|
||||
client2.addUserData(plane_id, 0, "MyKey", "MyNewValue")
|
||||
self.assertEqual(b"MyValue", client1.getUserData(plane_id, 0, uid))
|
||||
client1.syncUserData()
|
||||
self.assertEqual(b"MyNewValue", client1.getUserData(plane_id, 0, uid))
|
||||
|
||||
# Remove user data on client 1, check on client 2
|
||||
client1.removeUserData(plane_id, 0, uid)
|
||||
self.assertEqual(b"MyNewValue", client2.getUserData(plane_id, 0, uid))
|
||||
client2.syncUserData()
|
||||
self.assertEqual(None, client2.getUserData(plane_id, 0, uid))
|
||||
|
||||
del client2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
@ -17,12 +17,13 @@ subject to the following restrictions:
|
||||
#ifndef BT_HASH_MAP_H
|
||||
#define BT_HASH_MAP_H
|
||||
|
||||
#include <string>
|
||||
#include "btAlignedObjectArray.h"
|
||||
|
||||
///very basic hashable string implementation, compatible with btHashMap
|
||||
struct btHashString
|
||||
{
|
||||
const char* m_string;
|
||||
std::string m_string;
|
||||
unsigned int m_hash;
|
||||
|
||||
SIMD_FORCE_INLINE unsigned int getHash()const
|
||||
@ -71,7 +72,7 @@ struct btHashString
|
||||
bool equals(const btHashString& other) const
|
||||
{
|
||||
return (m_string == other.m_string) ||
|
||||
(0==portableStringCompare(m_string,other.m_string));
|
||||
(0==portableStringCompare(m_string.c_str(),other.m_string.c_str()));
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user