PhysicsClientSharedMemory.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #include "PhysicsClientSharedMemory.h"
  2. #include "PosixSharedMemory.h"
  3. #include "Win32SharedMemory.h"
  4. #include "LinearMath/btAlignedObjectArray.h"
  5. #include "LinearMath/btVector3.h"
  6. #include <string.h>
  7. #include "Bullet3Common/b3Logging.h"
  8. #include "../Utils/b3ResourcePath.h"
  9. #include "../../Extras/Serialize/BulletFileLoader/btBulletFile.h"
  10. #include "../../Extras/Serialize/BulletFileLoader/autogenerated/bullet.h"
  11. #include "SharedMemoryBlock.h"
  12. #include "BodyJointInfoUtility.h"
  13. struct BodyJointInfoCache
  14. {
  15. std::string m_baseName;
  16. btAlignedObjectArray<b3JointInfo> m_jointInfo;
  17. };
  18. struct PhysicsClientSharedMemoryInternalData {
  19. SharedMemoryInterface* m_sharedMemory;
  20. bool m_ownsSharedMemory;
  21. SharedMemoryBlock* m_testBlock1;
  22. btHashMap<btHashInt,BodyJointInfoCache*> m_bodyJointMap;
  23. btAlignedObjectArray<TmpFloat3> m_debugLinesFrom;
  24. btAlignedObjectArray<TmpFloat3> m_debugLinesTo;
  25. btAlignedObjectArray<TmpFloat3> m_debugLinesColor;
  26. int m_cachedCameraPixelsWidth;
  27. int m_cachedCameraPixelsHeight;
  28. btAlignedObjectArray<unsigned char> m_cachedCameraPixelsRGBA;
  29. btAlignedObjectArray<float> m_cachedCameraDepthBuffer;
  30. btAlignedObjectArray<int> m_cachedSegmentationMaskBuffer;
  31. btAlignedObjectArray<b3ContactPointData> m_cachedContactPoints;
  32. btAlignedObjectArray<int> m_bodyIdsRequestInfo;
  33. SharedMemoryStatus m_tempBackupServerStatus;
  34. SharedMemoryStatus m_lastServerStatus;
  35. int m_counter;
  36. bool m_isConnected;
  37. bool m_waitingForServer;
  38. bool m_hasLastServerStatus;
  39. int m_sharedMemoryKey;
  40. bool m_verboseOutput;
  41. PhysicsClientSharedMemoryInternalData()
  42. : m_sharedMemory(0),
  43. m_ownsSharedMemory(false),
  44. m_testBlock1(0),
  45. m_counter(0),
  46. m_cachedCameraPixelsWidth(0),
  47. m_cachedCameraPixelsHeight(0),
  48. m_isConnected(false),
  49. m_waitingForServer(false),
  50. m_hasLastServerStatus(false),
  51. m_sharedMemoryKey(SHARED_MEMORY_KEY),
  52. m_verboseOutput(false) {}
  53. void processServerStatus();
  54. bool canSubmitCommand() const;
  55. };
  56. int PhysicsClientSharedMemory::getNumBodies() const
  57. {
  58. return m_data->m_bodyJointMap.size();
  59. }
  60. int PhysicsClientSharedMemory::getBodyUniqueId(int serialIndex) const
  61. {
  62. if ((serialIndex >= 0) && (serialIndex < getNumBodies()))
  63. {
  64. return m_data->m_bodyJointMap.getKeyAtIndex(serialIndex).getUid1();
  65. }
  66. return -1;
  67. }
  68. bool PhysicsClientSharedMemory::getBodyInfo(int bodyUniqueId, struct b3BodyInfo& info) const
  69. {
  70. BodyJointInfoCache** bodyJointsPtr = m_data->m_bodyJointMap[bodyUniqueId];
  71. if (bodyJointsPtr && *bodyJointsPtr)
  72. {
  73. BodyJointInfoCache* bodyJoints = *bodyJointsPtr;
  74. info.m_baseName = bodyJoints->m_baseName.c_str();
  75. return true;
  76. }
  77. return false;
  78. }
  79. int PhysicsClientSharedMemory::getNumJoints(int bodyUniqueId) const
  80. {
  81. BodyJointInfoCache** bodyJointsPtr = m_data->m_bodyJointMap[bodyUniqueId];
  82. if (bodyJointsPtr && *bodyJointsPtr)
  83. {
  84. BodyJointInfoCache* bodyJoints = *bodyJointsPtr;
  85. return bodyJoints->m_jointInfo.size();
  86. }
  87. return 0;
  88. }
  89. bool PhysicsClientSharedMemory::getJointInfo(int bodyUniqueId, int jointIndex, b3JointInfo& info) const
  90. {
  91. BodyJointInfoCache** bodyJointsPtr = m_data->m_bodyJointMap[bodyUniqueId];
  92. if (bodyJointsPtr && *bodyJointsPtr)
  93. {
  94. BodyJointInfoCache* bodyJoints = *bodyJointsPtr;
  95. info = bodyJoints->m_jointInfo[jointIndex];
  96. return true;
  97. }
  98. return false;
  99. }
  100. PhysicsClientSharedMemory::PhysicsClientSharedMemory()
  101. {
  102. m_data = new PhysicsClientSharedMemoryInternalData;
  103. #ifdef _WIN32
  104. m_data->m_sharedMemory = new Win32SharedMemoryClient();
  105. #else
  106. m_data->m_sharedMemory = new PosixSharedMemory();
  107. #endif
  108. m_data->m_ownsSharedMemory = true;
  109. }
  110. PhysicsClientSharedMemory::~PhysicsClientSharedMemory() {
  111. if (m_data->m_isConnected) {
  112. disconnectSharedMemory();
  113. }
  114. if (m_data->m_ownsSharedMemory)
  115. {
  116. delete m_data->m_sharedMemory;
  117. }
  118. delete m_data;
  119. }
  120. void PhysicsClientSharedMemory::setSharedMemoryKey(int key) { m_data->m_sharedMemoryKey = key; }
  121. void PhysicsClientSharedMemory::setSharedMemoryInterface(class SharedMemoryInterface* sharedMem)
  122. {
  123. if (m_data->m_sharedMemory && m_data->m_ownsSharedMemory)
  124. {
  125. delete m_data->m_sharedMemory;
  126. }
  127. m_data->m_ownsSharedMemory = false;
  128. m_data->m_sharedMemory = sharedMem;
  129. }
  130. void PhysicsClientSharedMemory::disconnectSharedMemory() {
  131. if (m_data->m_isConnected && m_data->m_sharedMemory) {
  132. m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE);
  133. }
  134. m_data->m_isConnected = false;
  135. }
  136. bool PhysicsClientSharedMemory::isConnected() const { return m_data->m_isConnected; }
  137. bool PhysicsClientSharedMemory::connect() {
  138. /// server always has to create and initialize shared memory
  139. bool allowCreation = false;
  140. m_data->m_testBlock1 = (SharedMemoryBlock*)m_data->m_sharedMemory->allocateSharedMemory(
  141. m_data->m_sharedMemoryKey, SHARED_MEMORY_SIZE, allowCreation);
  142. if (m_data->m_testBlock1) {
  143. if (m_data->m_testBlock1->m_magicId != SHARED_MEMORY_MAGIC_NUMBER) {
  144. b3Error("Error: please start server before client\n");
  145. m_data->m_sharedMemory->releaseSharedMemory(m_data->m_sharedMemoryKey,
  146. SHARED_MEMORY_SIZE);
  147. m_data->m_testBlock1 = 0;
  148. return false;
  149. } else {
  150. if (m_data->m_verboseOutput) {
  151. b3Printf("Connected to existing shared memory, status OK.\n");
  152. }
  153. m_data->m_isConnected = true;
  154. }
  155. } else {
  156. b3Error("Cannot connect to shared memory");
  157. return false;
  158. }
  159. return true;
  160. }
  161. ///todo(erwincoumans) refactor this: merge with PhysicsDirect::processBodyJointInfo
  162. void PhysicsClientSharedMemory::processBodyJointInfo(int bodyUniqueId, const SharedMemoryStatus& serverCmd)
  163. {
  164. bParse::btBulletFile bf(
  165. &this->m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0],
  166. serverCmd.m_dataStreamArguments.m_streamChunkLength);
  167. bf.setFileDNAisMemoryDNA();
  168. bf.parse(false);
  169. BodyJointInfoCache* bodyJoints = new BodyJointInfoCache;
  170. m_data->m_bodyJointMap.insert(bodyUniqueId,bodyJoints);
  171. for (int i = 0; i < bf.m_multiBodies.size(); i++)
  172. {
  173. int flag = bf.getFlags();
  174. if ((flag & bParse::FD_DOUBLE_PRECISION) != 0)
  175. {
  176. Bullet::btMultiBodyDoubleData* mb =
  177. (Bullet::btMultiBodyDoubleData*)bf.m_multiBodies[i];
  178. bodyJoints->m_baseName = mb->m_baseName;
  179. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  180. } else
  181. {
  182. Bullet::btMultiBodyFloatData* mb =
  183. (Bullet::btMultiBodyFloatData*)bf.m_multiBodies[i];
  184. bodyJoints->m_baseName = mb->m_baseName;
  185. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  186. }
  187. }
  188. if (bf.ok()) {
  189. if (m_data->m_verboseOutput)
  190. {
  191. b3Printf("Received robot description ok!\n");
  192. }
  193. } else
  194. {
  195. b3Warning("Robot description not received");
  196. }
  197. }
  198. const SharedMemoryStatus* PhysicsClientSharedMemory::processServerStatus() {
  199. SharedMemoryStatus* stat = 0;
  200. if (!m_data->m_testBlock1) {
  201. m_data->m_lastServerStatus.m_type = CMD_SHARED_MEMORY_NOT_INITIALIZED;
  202. return &m_data->m_lastServerStatus;
  203. }
  204. if (!m_data->m_waitingForServer) {
  205. return 0;
  206. }
  207. if (m_data->m_testBlock1->m_magicId != SHARED_MEMORY_MAGIC_NUMBER)
  208. {
  209. m_data->m_lastServerStatus.m_type = CMD_SHARED_MEMORY_NOT_INITIALIZED;
  210. return &m_data->m_lastServerStatus;
  211. }
  212. if (m_data->m_testBlock1->m_numServerCommands >
  213. m_data->m_testBlock1->m_numProcessedServerCommands) {
  214. btAssert(m_data->m_testBlock1->m_numServerCommands ==
  215. m_data->m_testBlock1->m_numProcessedServerCommands + 1);
  216. const SharedMemoryStatus& serverCmd = m_data->m_testBlock1->m_serverCommands[0];
  217. m_data->m_lastServerStatus = serverCmd;
  218. EnumSharedMemoryServerStatus s = (EnumSharedMemoryServerStatus)serverCmd.m_type;
  219. // consume the command
  220. switch (serverCmd.m_type) {
  221. case CMD_CLIENT_COMMAND_COMPLETED: {
  222. if (m_data->m_verboseOutput) {
  223. b3Printf("Server completed command");
  224. }
  225. break;
  226. }
  227. case CMD_SDF_LOADING_COMPLETED: {
  228. if (m_data->m_verboseOutput) {
  229. b3Printf("Server loading the SDF OK\n");
  230. }
  231. break;
  232. }
  233. case CMD_URDF_LOADING_COMPLETED: {
  234. if (m_data->m_verboseOutput) {
  235. b3Printf("Server loading the URDF OK\n");
  236. }
  237. if (serverCmd.m_dataStreamArguments.m_streamChunkLength > 0) {
  238. bParse::btBulletFile bf(
  239. this->m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor,
  240. serverCmd.m_dataStreamArguments.m_streamChunkLength);
  241. bf.setFileDNAisMemoryDNA();
  242. bf.parse(false);
  243. int bodyUniqueId = serverCmd.m_dataStreamArguments.m_bodyUniqueId;
  244. BodyJointInfoCache* bodyJoints = new BodyJointInfoCache;
  245. m_data->m_bodyJointMap.insert(bodyUniqueId,bodyJoints);
  246. for (int i = 0; i < bf.m_multiBodies.size(); i++) {
  247. int flag = bf.getFlags();
  248. if ((flag & bParse::FD_DOUBLE_PRECISION) != 0) {
  249. Bullet::btMultiBodyDoubleData* mb =
  250. (Bullet::btMultiBodyDoubleData*)bf.m_multiBodies[i];
  251. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  252. } else
  253. {
  254. Bullet::btMultiBodyFloatData* mb =
  255. (Bullet::btMultiBodyFloatData*)bf.m_multiBodies[i];
  256. addJointInfoFromMultiBodyData(mb,bodyJoints, m_data->m_verboseOutput);
  257. }
  258. }
  259. if (bf.ok()) {
  260. if (m_data->m_verboseOutput) {
  261. b3Printf("Received robot description ok!\n");
  262. }
  263. } else {
  264. b3Warning("Robot description not received");
  265. }
  266. }
  267. break;
  268. }
  269. case CMD_DESIRED_STATE_RECEIVED_COMPLETED: {
  270. if (m_data->m_verboseOutput) {
  271. b3Printf("Server received desired state");
  272. }
  273. break;
  274. }
  275. case CMD_STEP_FORWARD_SIMULATION_COMPLETED: {
  276. if (m_data->m_verboseOutput) {
  277. b3Printf("Server completed step simulation");
  278. }
  279. break;
  280. }
  281. case CMD_URDF_LOADING_FAILED: {
  282. if (m_data->m_verboseOutput) {
  283. b3Printf("Server failed loading the URDF...\n");
  284. }
  285. break;
  286. }
  287. case CMD_BODY_INFO_COMPLETED:
  288. {
  289. if (m_data->m_verboseOutput) {
  290. b3Printf("Received body info\n");
  291. }
  292. int bodyUniqueId = serverCmd.m_dataStreamArguments.m_bodyUniqueId;
  293. processBodyJointInfo(bodyUniqueId, serverCmd);
  294. break;
  295. }
  296. case CMD_SDF_LOADING_FAILED: {
  297. if (m_data->m_verboseOutput) {
  298. b3Printf("Server failed loading the SDF...\n");
  299. }
  300. break;
  301. }
  302. case CMD_BULLET_DATA_STREAM_RECEIVED_COMPLETED: {
  303. if (m_data->m_verboseOutput) {
  304. b3Printf("Server received bullet data stream OK\n");
  305. }
  306. break;
  307. }
  308. case CMD_BULLET_DATA_STREAM_RECEIVED_FAILED: {
  309. if (m_data->m_verboseOutput) {
  310. b3Printf("Server failed receiving bullet data stream\n");
  311. }
  312. break;
  313. }
  314. case CMD_ACTUAL_STATE_UPDATE_COMPLETED: {
  315. if (m_data->m_verboseOutput) {
  316. b3Printf("Received actual state\n");
  317. }
  318. SharedMemoryStatus& command = m_data->m_testBlock1->m_serverCommands[0];
  319. int numQ = command.m_sendActualStateArgs.m_numDegreeOfFreedomQ;
  320. int numU = command.m_sendActualStateArgs.m_numDegreeOfFreedomU;
  321. if (m_data->m_verboseOutput) {
  322. b3Printf("size Q = %d, size U = %d\n", numQ, numU);
  323. }
  324. char msg[1024];
  325. {
  326. sprintf(msg, "Q=[");
  327. for (int i = 0; i < numQ; i++) {
  328. if (i < numQ - 1) {
  329. sprintf(msg, "%s%f,", msg,
  330. command.m_sendActualStateArgs.m_actualStateQ[i]);
  331. } else {
  332. sprintf(msg, "%s%f", msg,
  333. command.m_sendActualStateArgs.m_actualStateQ[i]);
  334. }
  335. }
  336. sprintf(msg, "%s]", msg);
  337. }
  338. if (m_data->m_verboseOutput) {
  339. b3Printf(msg);
  340. }
  341. {
  342. sprintf(msg, "U=[");
  343. for (int i = 0; i < numU; i++) {
  344. if (i < numU - 1) {
  345. sprintf(msg, "%s%f,", msg,
  346. command.m_sendActualStateArgs.m_actualStateQdot[i]);
  347. } else {
  348. sprintf(msg, "%s%f", msg,
  349. command.m_sendActualStateArgs.m_actualStateQdot[i]);
  350. }
  351. }
  352. sprintf(msg, "%s]", msg);
  353. }
  354. if (m_data->m_verboseOutput) {
  355. b3Printf(msg);
  356. }
  357. if (m_data->m_verboseOutput) {
  358. b3Printf("\n");
  359. }
  360. break;
  361. }
  362. case CMD_RESET_SIMULATION_COMPLETED: {
  363. if (m_data->m_verboseOutput) {
  364. b3Printf("CMD_RESET_SIMULATION_COMPLETED clean data\n");
  365. }
  366. m_data->m_debugLinesFrom.clear();
  367. m_data->m_debugLinesTo.clear();
  368. m_data->m_debugLinesColor.clear();
  369. for (int i=0;i<m_data->m_bodyJointMap.size();i++)
  370. {
  371. BodyJointInfoCache** bodyJointsPtr = m_data->m_bodyJointMap.getAtIndex(i);
  372. if (bodyJointsPtr && *bodyJointsPtr)
  373. {
  374. BodyJointInfoCache* bodyJoints = *bodyJointsPtr;
  375. for (int j=0;j<bodyJoints->m_jointInfo.size();j++) {
  376. if (bodyJoints->m_jointInfo[j].m_jointName)
  377. {
  378. free(bodyJoints->m_jointInfo[j].m_jointName);
  379. }
  380. if (bodyJoints->m_jointInfo[j].m_linkName)
  381. {
  382. free(bodyJoints->m_jointInfo[j].m_linkName);
  383. }
  384. }
  385. delete (*bodyJointsPtr);
  386. }
  387. }
  388. m_data->m_bodyJointMap.clear();
  389. break;
  390. }
  391. case CMD_DEBUG_LINES_COMPLETED: {
  392. if (m_data->m_verboseOutput) {
  393. b3Printf("Success receiving %d debug lines",
  394. serverCmd.m_sendDebugLinesArgs.m_numDebugLines);
  395. }
  396. int numLines = serverCmd.m_sendDebugLinesArgs.m_numDebugLines;
  397. float* linesFrom =
  398. (float*)&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0];
  399. float* linesTo =
  400. (float*)(&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0] +
  401. numLines * 3 * sizeof(float));
  402. float* linesColor =
  403. (float*)(&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0] +
  404. 2 * numLines * 3 * sizeof(float));
  405. m_data->m_debugLinesFrom.resize(serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  406. numLines);
  407. m_data->m_debugLinesTo.resize(serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  408. numLines);
  409. m_data->m_debugLinesColor.resize(
  410. serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + numLines);
  411. for (int i = 0; i < numLines; i++) {
  412. TmpFloat3 from = CreateTmpFloat3(linesFrom[i * 3], linesFrom[i * 3 + 1],
  413. linesFrom[i * 3 + 2]);
  414. TmpFloat3 to =
  415. CreateTmpFloat3(linesTo[i * 3], linesTo[i * 3 + 1], linesTo[i * 3 + 2]);
  416. TmpFloat3 color = CreateTmpFloat3(linesColor[i * 3], linesColor[i * 3 + 1],
  417. linesColor[i * 3 + 2]);
  418. m_data
  419. ->m_debugLinesFrom[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + i] =
  420. from;
  421. m_data->m_debugLinesTo[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex + i] =
  422. to;
  423. m_data->m_debugLinesColor[serverCmd.m_sendDebugLinesArgs.m_startingLineIndex +
  424. i] = color;
  425. }
  426. break;
  427. }
  428. case CMD_RIGID_BODY_CREATION_COMPLETED:
  429. {
  430. break;
  431. }
  432. case CMD_DEBUG_LINES_OVERFLOW_FAILED: {
  433. b3Warning("Error receiving debug lines");
  434. m_data->m_debugLinesFrom.resize(0);
  435. m_data->m_debugLinesTo.resize(0);
  436. m_data->m_debugLinesColor.resize(0);
  437. break;
  438. }
  439. case CMD_CAMERA_IMAGE_COMPLETED:
  440. {
  441. if (m_data->m_verboseOutput)
  442. {
  443. b3Printf("Camera image OK\n");
  444. }
  445. int numBytesPerPixel = 4;//RGBA
  446. int numTotalPixels = serverCmd.m_sendPixelDataArguments.m_startingPixelIndex+
  447. serverCmd.m_sendPixelDataArguments.m_numPixelsCopied+
  448. serverCmd.m_sendPixelDataArguments.m_numRemainingPixels;
  449. m_data->m_cachedCameraPixelsWidth = 0;
  450. m_data->m_cachedCameraPixelsHeight = 0;
  451. int numPixels = serverCmd.m_sendPixelDataArguments.m_imageWidth*serverCmd.m_sendPixelDataArguments.m_imageHeight;
  452. m_data->m_cachedCameraPixelsRGBA.reserve(numPixels*numBytesPerPixel);
  453. m_data->m_cachedCameraDepthBuffer.resize(numTotalPixels);
  454. m_data->m_cachedSegmentationMaskBuffer.resize(numTotalPixels);
  455. m_data->m_cachedCameraPixelsRGBA.resize(numTotalPixels*numBytesPerPixel);
  456. unsigned char* rgbaPixelsReceived =
  457. (unsigned char*)&m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[0];
  458. // printf("pixel = %d\n", rgbaPixelsReceived[0]);
  459. float* depthBuffer = (float*)&(m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*4]);
  460. int* segmentationMaskBuffer = (int*)&(m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor[serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*8]);
  461. for (int i=0;i<serverCmd.m_sendPixelDataArguments.m_numPixelsCopied;i++)
  462. {
  463. m_data->m_cachedCameraDepthBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = depthBuffer[i];
  464. }
  465. for (int i=0;i<serverCmd.m_sendPixelDataArguments.m_numPixelsCopied;i++)
  466. {
  467. m_data->m_cachedSegmentationMaskBuffer[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex] = segmentationMaskBuffer[i];
  468. }
  469. for (int i=0;i<serverCmd.m_sendPixelDataArguments.m_numPixelsCopied*numBytesPerPixel;i++)
  470. {
  471. m_data->m_cachedCameraPixelsRGBA[i + serverCmd.m_sendPixelDataArguments.m_startingPixelIndex*numBytesPerPixel]
  472. = rgbaPixelsReceived[i];
  473. }
  474. break;
  475. }
  476. case CMD_CAMERA_IMAGE_FAILED:
  477. {
  478. b3Warning("Camera image FAILED\n");
  479. break;
  480. }
  481. case CMD_CALCULATED_INVERSE_DYNAMICS_COMPLETED:
  482. {
  483. break;
  484. }
  485. case CMD_CALCULATED_INVERSE_DYNAMICS_FAILED:
  486. {
  487. b3Warning("Inverse Dynamics computations failed");
  488. break;
  489. }
  490. case CMD_CONTACT_POINT_INFORMATION_COMPLETED:
  491. {
  492. if (m_data->m_verboseOutput)
  493. {
  494. b3Printf("Contact Point Information Request OK\n");
  495. }
  496. int startContactIndex = serverCmd.m_sendContactPointArgs.m_startingContactPointIndex;
  497. int numContactsCopied = serverCmd.m_sendContactPointArgs.m_numContactPointsCopied;
  498. m_data->m_cachedContactPoints.resize(startContactIndex+numContactsCopied);
  499. b3ContactPointData* contactData = (b3ContactPointData*)m_data->m_testBlock1->m_bulletStreamDataServerToClientRefactor;
  500. for (int i=0;i<numContactsCopied;i++)
  501. {
  502. m_data->m_cachedContactPoints[startContactIndex+i] = contactData[i];
  503. }
  504. break;
  505. }
  506. case CMD_CONTACT_POINT_INFORMATION_FAILED:
  507. {
  508. b3Warning("Contact Point Information Request failed");
  509. break;
  510. }
  511. case CMD_SAVE_WORLD_COMPLETED:
  512. break;
  513. case CMD_SAVE_WORLD_FAILED:
  514. {
  515. b3Warning("Saving world failed");
  516. break;
  517. }
  518. case CMD_CALCULATE_INVERSE_KINEMATICS_COMPLETED:
  519. {
  520. break;
  521. }
  522. case CMD_CALCULATE_INVERSE_KINEMATICS_FAILED:
  523. {
  524. b3Warning("Calculate Inverse Kinematics Request failed");
  525. break;
  526. }
  527. default: {
  528. b3Error("Unknown server status %d\n", serverCmd.m_type);
  529. btAssert(0);
  530. }
  531. };
  532. m_data->m_testBlock1->m_numProcessedServerCommands++;
  533. // we don't have more than 1 command outstanding (in total, either server or client)
  534. btAssert(m_data->m_testBlock1->m_numProcessedServerCommands ==
  535. m_data->m_testBlock1->m_numServerCommands);
  536. if (m_data->m_testBlock1->m_numServerCommands ==
  537. m_data->m_testBlock1->m_numProcessedServerCommands) {
  538. m_data->m_waitingForServer = false;
  539. } else {
  540. m_data->m_waitingForServer = true;
  541. }
  542. if (serverCmd.m_type == CMD_SDF_LOADING_COMPLETED)
  543. {
  544. int numBodies = serverCmd.m_sdfLoadedArgs.m_numBodies;
  545. if (numBodies>0)
  546. {
  547. m_data->m_tempBackupServerStatus = m_data->m_lastServerStatus;
  548. for (int i=0;i<numBodies;i++)
  549. {
  550. m_data->m_bodyIdsRequestInfo.push_back(serverCmd.m_sdfLoadedArgs.m_bodyUniqueIds[i]);
  551. }
  552. int bodyId = m_data->m_bodyIdsRequestInfo[m_data->m_bodyIdsRequestInfo.size()-1];
  553. m_data->m_bodyIdsRequestInfo.pop_back();
  554. SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
  555. //now transfer the information of the individual objects etc.
  556. command.m_type = CMD_REQUEST_BODY_INFO;
  557. command.m_sdfRequestInfoArgs.m_bodyUniqueId = bodyId;
  558. submitClientCommand(command);
  559. return 0;
  560. }
  561. }
  562. if (serverCmd.m_type == CMD_BODY_INFO_COMPLETED)
  563. {
  564. //are there any bodies left to be processed?
  565. if (m_data->m_bodyIdsRequestInfo.size())
  566. {
  567. int bodyId = m_data->m_bodyIdsRequestInfo[m_data->m_bodyIdsRequestInfo.size()-1];
  568. m_data->m_bodyIdsRequestInfo.pop_back();
  569. SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
  570. //now transfer the information of the individual objects etc.
  571. command.m_type = CMD_REQUEST_BODY_INFO;
  572. command.m_sdfRequestInfoArgs.m_bodyUniqueId = bodyId;
  573. submitClientCommand(command);
  574. return 0;
  575. } else
  576. {
  577. m_data->m_lastServerStatus = m_data->m_tempBackupServerStatus;
  578. }
  579. }
  580. if (serverCmd.m_type == CMD_CONTACT_POINT_INFORMATION_COMPLETED)
  581. {
  582. SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
  583. if (serverCmd.m_sendContactPointArgs.m_numRemainingContactPoints>0 && serverCmd.m_sendContactPointArgs.m_numContactPointsCopied)
  584. {
  585. command.m_type = CMD_REQUEST_CONTACT_POINT_INFORMATION;
  586. command.m_requestContactPointArguments.m_startingContactPointIndex = serverCmd.m_sendContactPointArgs.m_startingContactPointIndex+serverCmd.m_sendContactPointArgs.m_numContactPointsCopied;
  587. command.m_requestContactPointArguments.m_objectAIndexFilter = -1;
  588. command.m_requestContactPointArguments.m_objectBIndexFilter = -1;
  589. submitClientCommand(command);
  590. return 0;
  591. }
  592. }
  593. if (serverCmd.m_type == CMD_CAMERA_IMAGE_COMPLETED)
  594. {
  595. SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
  596. if (serverCmd.m_sendPixelDataArguments.m_numRemainingPixels > 0 && serverCmd.m_sendPixelDataArguments.m_numPixelsCopied)
  597. {
  598. // continue requesting remaining pixels
  599. command.m_type = CMD_REQUEST_CAMERA_IMAGE_DATA;
  600. command.m_requestPixelDataArguments.m_startPixelIndex =
  601. serverCmd.m_sendPixelDataArguments.m_startingPixelIndex +
  602. serverCmd.m_sendPixelDataArguments.m_numPixelsCopied;
  603. submitClientCommand(command);
  604. return 0;
  605. } else
  606. {
  607. m_data->m_cachedCameraPixelsWidth = serverCmd.m_sendPixelDataArguments.m_imageWidth;
  608. m_data->m_cachedCameraPixelsHeight = serverCmd.m_sendPixelDataArguments.m_imageHeight;
  609. }
  610. }
  611. if ((serverCmd.m_type == CMD_DEBUG_LINES_COMPLETED) &&
  612. (serverCmd.m_sendDebugLinesArgs.m_numRemainingDebugLines > 0)) {
  613. SharedMemoryCommand& command = m_data->m_testBlock1->m_clientCommands[0];
  614. // continue requesting debug lines for drawing
  615. command.m_type = CMD_REQUEST_DEBUG_LINES;
  616. command.m_requestDebugLinesArguments.m_startingLineIndex =
  617. serverCmd.m_sendDebugLinesArgs.m_numDebugLines +
  618. serverCmd.m_sendDebugLinesArgs.m_startingLineIndex;
  619. submitClientCommand(command);
  620. return 0;
  621. }
  622. return &m_data->m_lastServerStatus;
  623. } else {
  624. if (m_data->m_verboseOutput) {
  625. b3Printf("m_numServerStatus = %d, processed = %d\n",
  626. m_data->m_testBlock1->m_numServerCommands,
  627. m_data->m_testBlock1->m_numProcessedServerCommands);
  628. }
  629. }
  630. return 0;
  631. }
  632. bool PhysicsClientSharedMemory::canSubmitCommand() const {
  633. return (m_data->m_isConnected && !m_data->m_waitingForServer);
  634. }
  635. struct SharedMemoryCommand* PhysicsClientSharedMemory::getAvailableSharedMemoryCommand() {
  636. static int sequence = 0;
  637. m_data->m_testBlock1->m_clientCommands[0].m_sequenceNumber = sequence++;
  638. return &m_data->m_testBlock1->m_clientCommands[0];
  639. }
  640. bool PhysicsClientSharedMemory::submitClientCommand(const SharedMemoryCommand& command) {
  641. /// at the moment we allow a maximum of 1 outstanding command, so we check for this
  642. // once the server processed the command and returns a status, we clear the flag
  643. // "m_data->m_waitingForServer" and allow submitting the next command
  644. if (!m_data->m_waitingForServer) {
  645. if (&m_data->m_testBlock1->m_clientCommands[0] != &command) {
  646. m_data->m_testBlock1->m_clientCommands[0] = command;
  647. }
  648. m_data->m_testBlock1->m_numClientCommands++;
  649. m_data->m_waitingForServer = true;
  650. return true;
  651. }
  652. return false;
  653. }
  654. void PhysicsClientSharedMemory::uploadBulletFileToSharedMemory(const char* data, int len) {
  655. btAssert(len < SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
  656. if (len >= SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE) {
  657. b3Warning("uploadBulletFileToSharedMemory %d exceeds max size %d\n", len,
  658. SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE);
  659. } else {
  660. for (int i = 0; i < len; i++) {
  661. m_data->m_testBlock1->m_bulletStreamDataClientToServer[i] = data[i];
  662. }
  663. }
  664. }
  665. void PhysicsClientSharedMemory::getCachedCameraImage(struct b3CameraImageData* cameraData)
  666. {
  667. cameraData->m_pixelWidth = m_data->m_cachedCameraPixelsWidth;
  668. cameraData->m_pixelHeight = m_data->m_cachedCameraPixelsHeight;
  669. cameraData->m_depthValues = m_data->m_cachedCameraDepthBuffer.size() ? &m_data->m_cachedCameraDepthBuffer[0] : 0;
  670. cameraData->m_rgbColorData = m_data->m_cachedCameraPixelsRGBA.size() ? &m_data->m_cachedCameraPixelsRGBA[0] : 0;
  671. cameraData->m_segmentationMaskValues = m_data->m_cachedSegmentationMaskBuffer.size()?&m_data->m_cachedSegmentationMaskBuffer[0] : 0;
  672. }
  673. void PhysicsClientSharedMemory::getCachedContactPointInformation(struct b3ContactInformation* contactPointData)
  674. {
  675. contactPointData->m_numContactPoints = m_data->m_cachedContactPoints.size();
  676. contactPointData->m_contactPointData = contactPointData->m_numContactPoints? &m_data->m_cachedContactPoints[0] : 0;
  677. }
  678. const float* PhysicsClientSharedMemory::getDebugLinesFrom() const {
  679. if (m_data->m_debugLinesFrom.size()) {
  680. return &m_data->m_debugLinesFrom[0].m_x;
  681. }
  682. return 0;
  683. }
  684. const float* PhysicsClientSharedMemory::getDebugLinesTo() const {
  685. if (m_data->m_debugLinesTo.size()) {
  686. return &m_data->m_debugLinesTo[0].m_x;
  687. }
  688. return 0;
  689. }
  690. const float* PhysicsClientSharedMemory::getDebugLinesColor() const {
  691. if (m_data->m_debugLinesColor.size()) {
  692. return &m_data->m_debugLinesColor[0].m_x;
  693. }
  694. return 0;
  695. }
  696. int PhysicsClientSharedMemory::getNumDebugLines() const { return m_data->m_debugLinesFrom.size(); }