btMatrixX.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///original version written by Erwin Coumans, October 2013
  14. #ifndef BT_MATRIX_X_H
  15. #define BT_MATRIX_X_H
  16. #include "LinearMath/btQuickprof.h"
  17. #include "LinearMath/btAlignedObjectArray.h"
  18. #include <stdio.h>
  19. //#define BT_DEBUG_OSTREAM
  20. #ifdef BT_DEBUG_OSTREAM
  21. #include <iostream>
  22. #include <iomanip> // std::setw
  23. #endif //BT_DEBUG_OSTREAM
  24. class btIntSortPredicate
  25. {
  26. public:
  27. bool operator() ( const int& a, const int& b ) const
  28. {
  29. return a < b;
  30. }
  31. };
  32. template <typename T>
  33. struct btVectorX
  34. {
  35. btAlignedObjectArray<T> m_storage;
  36. btVectorX()
  37. {
  38. }
  39. btVectorX(int numRows)
  40. {
  41. m_storage.resize(numRows);
  42. }
  43. void resize(int rows)
  44. {
  45. m_storage.resize(rows);
  46. }
  47. int cols() const
  48. {
  49. return 1;
  50. }
  51. int rows() const
  52. {
  53. return m_storage.size();
  54. }
  55. int size() const
  56. {
  57. return rows();
  58. }
  59. T nrm2() const
  60. {
  61. T norm = T(0);
  62. int nn = rows();
  63. {
  64. if (nn == 1)
  65. {
  66. norm = btFabs((*this)[0]);
  67. }
  68. else
  69. {
  70. T scale = 0.0;
  71. T ssq = 1.0;
  72. /* The following loop is equivalent to this call to the LAPACK
  73. auxiliary routine: CALL SLASSQ( N, X, INCX, SCALE, SSQ ) */
  74. for (int ix=0;ix<nn;ix++)
  75. {
  76. if ((*this)[ix] != 0.0)
  77. {
  78. T absxi = btFabs((*this)[ix]);
  79. if (scale < absxi)
  80. {
  81. T temp;
  82. temp = scale / absxi;
  83. ssq = ssq * (temp * temp) + BT_ONE;
  84. scale = absxi;
  85. }
  86. else
  87. {
  88. T temp;
  89. temp = absxi / scale;
  90. ssq += temp * temp;
  91. }
  92. }
  93. }
  94. norm = scale * sqrt(ssq);
  95. }
  96. }
  97. return norm;
  98. }
  99. void setZero()
  100. {
  101. if (m_storage.size())
  102. {
  103. // for (int i=0;i<m_storage.size();i++)
  104. // m_storage[i]=0;
  105. //memset(&m_storage[0],0,sizeof(T)*m_storage.size());
  106. btSetZero(&m_storage[0],m_storage.size());
  107. }
  108. }
  109. const T& operator[] (int index) const
  110. {
  111. return m_storage[index];
  112. }
  113. T& operator[] (int index)
  114. {
  115. return m_storage[index];
  116. }
  117. T* getBufferPointerWritable()
  118. {
  119. return m_storage.size() ? &m_storage[0] : 0;
  120. }
  121. const T* getBufferPointer() const
  122. {
  123. return m_storage.size() ? &m_storage[0] : 0;
  124. }
  125. };
  126. /*
  127. template <typename T>
  128. void setElem(btMatrixX<T>& mat, int row, int col, T val)
  129. {
  130. mat.setElem(row,col,val);
  131. }
  132. */
  133. template <typename T>
  134. struct btMatrixX
  135. {
  136. int m_rows;
  137. int m_cols;
  138. int m_operations;
  139. int m_resizeOperations;
  140. int m_setElemOperations;
  141. btAlignedObjectArray<T> m_storage;
  142. mutable btAlignedObjectArray< btAlignedObjectArray<int> > m_rowNonZeroElements1;
  143. T* getBufferPointerWritable()
  144. {
  145. return m_storage.size() ? &m_storage[0] : 0;
  146. }
  147. const T* getBufferPointer() const
  148. {
  149. return m_storage.size() ? &m_storage[0] : 0;
  150. }
  151. btMatrixX()
  152. :m_rows(0),
  153. m_cols(0),
  154. m_operations(0),
  155. m_resizeOperations(0),
  156. m_setElemOperations(0)
  157. {
  158. }
  159. btMatrixX(int rows,int cols)
  160. :m_rows(rows),
  161. m_cols(cols),
  162. m_operations(0),
  163. m_resizeOperations(0),
  164. m_setElemOperations(0)
  165. {
  166. resize(rows,cols);
  167. }
  168. void resize(int rows, int cols)
  169. {
  170. m_resizeOperations++;
  171. m_rows = rows;
  172. m_cols = cols;
  173. {
  174. BT_PROFILE("m_storage.resize");
  175. m_storage.resize(rows*cols);
  176. }
  177. }
  178. int cols() const
  179. {
  180. return m_cols;
  181. }
  182. int rows() const
  183. {
  184. return m_rows;
  185. }
  186. ///we don't want this read/write operator(), because we cannot keep track of non-zero elements, use setElem instead
  187. /*T& operator() (int row,int col)
  188. {
  189. return m_storage[col*m_rows+row];
  190. }
  191. */
  192. void addElem(int row,int col, T val)
  193. {
  194. if (val)
  195. {
  196. if (m_storage[col+row*m_cols]==0.f)
  197. {
  198. setElem(row,col,val);
  199. } else
  200. {
  201. m_storage[row*m_cols+col] += val;
  202. }
  203. }
  204. }
  205. void setElem(int row,int col, T val)
  206. {
  207. m_setElemOperations++;
  208. m_storage[row*m_cols+col] = val;
  209. }
  210. void mulElem(int row,int col, T val)
  211. {
  212. m_setElemOperations++;
  213. //mul doesn't change sparsity info
  214. m_storage[row*m_cols+col] *= val;
  215. }
  216. void copyLowerToUpperTriangle()
  217. {
  218. int count=0;
  219. for (int row=0;row<rows();row++)
  220. {
  221. for (int col=0;col<row;col++)
  222. {
  223. setElem(col,row, (*this)(row,col));
  224. count++;
  225. }
  226. }
  227. //printf("copyLowerToUpperTriangle copied %d elements out of %dx%d=%d\n", count,rows(),cols(),cols()*rows());
  228. }
  229. const T& operator() (int row,int col) const
  230. {
  231. return m_storage[col+row*m_cols];
  232. }
  233. void setZero()
  234. {
  235. {
  236. BT_PROFILE("storage=0");
  237. btSetZero(&m_storage[0],m_storage.size());
  238. //memset(&m_storage[0],0,sizeof(T)*m_storage.size());
  239. //for (int i=0;i<m_storage.size();i++)
  240. // m_storage[i]=0;
  241. }
  242. }
  243. void setIdentity()
  244. {
  245. btAssert(rows() == cols());
  246. setZero();
  247. for (int row=0;row<rows();row++)
  248. {
  249. setElem(row,row,1);
  250. }
  251. }
  252. void printMatrix(const char* msg)
  253. {
  254. printf("%s ---------------------\n",msg);
  255. for (int i=0;i<rows();i++)
  256. {
  257. printf("\n");
  258. for (int j=0;j<cols();j++)
  259. {
  260. printf("%2.1f\t",(*this)(i,j));
  261. }
  262. }
  263. printf("\n---------------------\n");
  264. }
  265. void rowComputeNonZeroElements() const
  266. {
  267. m_rowNonZeroElements1.resize(rows());
  268. for (int i=0;i<rows();i++)
  269. {
  270. m_rowNonZeroElements1[i].resize(0);
  271. for (int j=0;j<cols();j++)
  272. {
  273. if ((*this)(i,j)!=0.f)
  274. {
  275. m_rowNonZeroElements1[i].push_back(j);
  276. }
  277. }
  278. }
  279. }
  280. btMatrixX transpose() const
  281. {
  282. //transpose is optimized for sparse matrices
  283. btMatrixX tr(m_cols,m_rows);
  284. tr.setZero();
  285. for (int i=0;i<m_cols;i++)
  286. for (int j=0;j<m_rows;j++)
  287. {
  288. T v = (*this)(j,i);
  289. if (v)
  290. {
  291. tr.setElem(i,j,v);
  292. }
  293. }
  294. return tr;
  295. }
  296. btMatrixX operator*(const btMatrixX& other)
  297. {
  298. //btMatrixX*btMatrixX implementation, brute force
  299. btAssert(cols() == other.rows());
  300. btMatrixX res(rows(),other.cols());
  301. res.setZero();
  302. // BT_PROFILE("btMatrixX mul");
  303. for (int j=0; j < res.cols(); ++j)
  304. {
  305. {
  306. for (int i=0; i < res.rows(); ++i)
  307. {
  308. T dotProd=0;
  309. // T dotProd2=0;
  310. //int waste=0,waste2=0;
  311. {
  312. // bool useOtherCol = true;
  313. {
  314. for (int v=0;v<rows();v++)
  315. {
  316. T w = (*this)(i,v);
  317. if (other(v,j)!=0.f)
  318. {
  319. dotProd+=w*other(v,j);
  320. }
  321. }
  322. }
  323. }
  324. if (dotProd)
  325. res.setElem(i,j,dotProd);
  326. }
  327. }
  328. }
  329. return res;
  330. }
  331. // this assumes the 4th and 8th rows of B and C are zero.
  332. void multiplyAdd2_p8r (const btScalar *B, const btScalar *C, int numRows, int numRowsOther ,int row, int col)
  333. {
  334. const btScalar *bb = B;
  335. for ( int i = 0;i<numRows;i++)
  336. {
  337. const btScalar *cc = C;
  338. for ( int j = 0;j<numRowsOther;j++)
  339. {
  340. btScalar sum;
  341. sum = bb[0]*cc[0];
  342. sum += bb[1]*cc[1];
  343. sum += bb[2]*cc[2];
  344. sum += bb[4]*cc[4];
  345. sum += bb[5]*cc[5];
  346. sum += bb[6]*cc[6];
  347. addElem(row+i,col+j,sum);
  348. cc += 8;
  349. }
  350. bb += 8;
  351. }
  352. }
  353. void multiply2_p8r (const btScalar *B, const btScalar *C, int numRows, int numRowsOther, int row, int col)
  354. {
  355. btAssert (numRows>0 && numRowsOther>0 && B && C);
  356. const btScalar *bb = B;
  357. for ( int i = 0;i<numRows;i++)
  358. {
  359. const btScalar *cc = C;
  360. for ( int j = 0;j<numRowsOther;j++)
  361. {
  362. btScalar sum;
  363. sum = bb[0]*cc[0];
  364. sum += bb[1]*cc[1];
  365. sum += bb[2]*cc[2];
  366. sum += bb[4]*cc[4];
  367. sum += bb[5]*cc[5];
  368. sum += bb[6]*cc[6];
  369. setElem(row+i,col+j,sum);
  370. cc += 8;
  371. }
  372. bb += 8;
  373. }
  374. }
  375. void setSubMatrix(int rowstart,int colstart,int rowend,int colend,const T value)
  376. {
  377. int numRows = rowend+1-rowstart;
  378. int numCols = colend+1-colstart;
  379. for (int row=0;row<numRows;row++)
  380. {
  381. for (int col=0;col<numCols;col++)
  382. {
  383. setElem(rowstart+row,colstart+col,value);
  384. }
  385. }
  386. }
  387. void setSubMatrix(int rowstart,int colstart,int rowend,int colend,const btMatrixX& block)
  388. {
  389. btAssert(rowend+1-rowstart == block.rows());
  390. btAssert(colend+1-colstart == block.cols());
  391. for (int row=0;row<block.rows();row++)
  392. {
  393. for (int col=0;col<block.cols();col++)
  394. {
  395. setElem(rowstart+row,colstart+col,block(row,col));
  396. }
  397. }
  398. }
  399. void setSubMatrix(int rowstart,int colstart,int rowend,int colend,const btVectorX<T>& block)
  400. {
  401. btAssert(rowend+1-rowstart == block.rows());
  402. btAssert(colend+1-colstart == block.cols());
  403. for (int row=0;row<block.rows();row++)
  404. {
  405. for (int col=0;col<block.cols();col++)
  406. {
  407. setElem(rowstart+row,colstart+col,block[row]);
  408. }
  409. }
  410. }
  411. btMatrixX negative()
  412. {
  413. btMatrixX neg(rows(),cols());
  414. for (int i=0;i<rows();i++)
  415. for (int j=0;j<cols();j++)
  416. {
  417. T v = (*this)(i,j);
  418. neg.setElem(i,j,-v);
  419. }
  420. return neg;
  421. }
  422. };
  423. typedef btMatrixX<float> btMatrixXf;
  424. typedef btVectorX<float> btVectorXf;
  425. typedef btMatrixX<double> btMatrixXd;
  426. typedef btVectorX<double> btVectorXd;
  427. #ifdef BT_DEBUG_OSTREAM
  428. template <typename T>
  429. std::ostream& operator<< (std::ostream& os, const btMatrixX<T>& mat)
  430. {
  431. os << " [";
  432. //printf("%s ---------------------\n",msg);
  433. for (int i=0;i<mat.rows();i++)
  434. {
  435. for (int j=0;j<mat.cols();j++)
  436. {
  437. os << std::setw(12) << mat(i,j);
  438. }
  439. if (i!=mat.rows()-1)
  440. os << std::endl << " ";
  441. }
  442. os << " ]";
  443. //printf("\n---------------------\n");
  444. return os;
  445. }
  446. template <typename T>
  447. std::ostream& operator<< (std::ostream& os, const btVectorX<T>& mat)
  448. {
  449. os << " [";
  450. //printf("%s ---------------------\n",msg);
  451. for (int i=0;i<mat.rows();i++)
  452. {
  453. os << std::setw(12) << mat[i];
  454. if (i!=mat.rows()-1)
  455. os << std::endl << " ";
  456. }
  457. os << " ]";
  458. //printf("\n---------------------\n");
  459. return os;
  460. }
  461. #endif //BT_DEBUG_OSTREAM
  462. inline void setElem(btMatrixXd& mat, int row, int col, double val)
  463. {
  464. mat.setElem(row,col,val);
  465. }
  466. inline void setElem(btMatrixXf& mat, int row, int col, float val)
  467. {
  468. mat.setElem(row,col,val);
  469. }
  470. #ifdef BT_USE_DOUBLE_PRECISION
  471. #define btVectorXu btVectorXd
  472. #define btMatrixXu btMatrixXd
  473. #else
  474. #define btVectorXu btVectorXf
  475. #define btMatrixXu btMatrixXf
  476. #endif //BT_USE_DOUBLE_PRECISION
  477. #endif//BT_MATRIX_H_H