btDantzigLCP.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: russ@q12.org Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. /*
  23. THE ALGORITHM
  24. -------------
  25. solve A*x = b+w, with x and w subject to certain LCP conditions.
  26. each x(i),w(i) must lie on one of the three line segments in the following
  27. diagram. each line segment corresponds to one index set :
  28. w(i)
  29. /|\ | :
  30. | | :
  31. | |i in N :
  32. w>0 | |state[i]=0 :
  33. | | :
  34. | | : i in C
  35. w=0 + +-----------------------+
  36. | : |
  37. | : |
  38. w<0 | : |i in N
  39. | : |state[i]=1
  40. | : |
  41. | : |
  42. +-------|-----------|-----------|----------> x(i)
  43. lo 0 hi
  44. the Dantzig algorithm proceeds as follows:
  45. for i=1:n
  46. * if (x(i),w(i)) is not on the line, push x(i) and w(i) positive or
  47. negative towards the line. as this is done, the other (x(j),w(j))
  48. for j<i are constrained to be on the line. if any (x,w) reaches the
  49. end of a line segment then it is switched between index sets.
  50. * i is added to the appropriate index set depending on what line segment
  51. it hits.
  52. we restrict lo(i) <= 0 and hi(i) >= 0. this makes the algorithm a bit
  53. simpler, because the starting point for x(i),w(i) is always on the dotted
  54. line x=0 and x will only ever increase in one direction, so it can only hit
  55. two out of the three line segments.
  56. NOTES
  57. -----
  58. this is an implementation of "lcp_dantzig2_ldlt.m" and "lcp_dantzig_lohi.m".
  59. the implementation is split into an LCP problem object (btLCP) and an LCP
  60. driver function. most optimization occurs in the btLCP object.
  61. a naive implementation of the algorithm requires either a lot of data motion
  62. or a lot of permutation-array lookup, because we are constantly re-ordering
  63. rows and columns. to avoid this and make a more optimized algorithm, a
  64. non-trivial data structure is used to represent the matrix A (this is
  65. implemented in the fast version of the btLCP object).
  66. during execution of this algorithm, some indexes in A are clamped (set C),
  67. some are non-clamped (set N), and some are "don't care" (where x=0).
  68. A,x,b,w (and other problem vectors) are permuted such that the clamped
  69. indexes are first, the unclamped indexes are next, and the don't-care
  70. indexes are last. this permutation is recorded in the array `p'.
  71. initially p = 0..n-1, and as the rows and columns of A,x,b,w are swapped,
  72. the corresponding elements of p are swapped.
  73. because the C and N elements are grouped together in the rows of A, we can do
  74. lots of work with a fast dot product function. if A,x,etc were not permuted
  75. and we only had a permutation array, then those dot products would be much
  76. slower as we would have a permutation array lookup in some inner loops.
  77. A is accessed through an array of row pointers, so that element (i,j) of the
  78. permuted matrix is A[i][j]. this makes row swapping fast. for column swapping
  79. we still have to actually move the data.
  80. during execution of this algorithm we maintain an L*D*L' factorization of
  81. the clamped submatrix of A (call it `AC') which is the top left nC*nC
  82. submatrix of A. there are two ways we could arrange the rows/columns in AC.
  83. (1) AC is always permuted such that L*D*L' = AC. this causes a problem
  84. when a row/column is removed from C, because then all the rows/columns of A
  85. between the deleted index and the end of C need to be rotated downward.
  86. this results in a lot of data motion and slows things down.
  87. (2) L*D*L' is actually a factorization of a *permutation* of AC (which is
  88. itself a permutation of the underlying A). this is what we do - the
  89. permutation is recorded in the vector C. call this permutation A[C,C].
  90. when a row/column is removed from C, all we have to do is swap two
  91. rows/columns and manipulate C.
  92. */
  93. #include "btDantzigLCP.h"
  94. #include <string.h>//memcpy
  95. bool s_error = false;
  96. //***************************************************************************
  97. // code generation parameters
  98. #define btLCP_FAST // use fast btLCP object
  99. // option 1 : matrix row pointers (less data copying)
  100. #define BTROWPTRS
  101. #define BTATYPE btScalar **
  102. #define BTAROW(i) (m_A[i])
  103. // option 2 : no matrix row pointers (slightly faster inner loops)
  104. //#define NOROWPTRS
  105. //#define BTATYPE btScalar *
  106. //#define BTAROW(i) (m_A+(i)*m_nskip)
  107. #define BTNUB_OPTIMIZATIONS
  108. /* solve L*X=B, with B containing 1 right hand sides.
  109. * L is an n*n lower triangular matrix with ones on the diagonal.
  110. * L is stored by rows and its leading dimension is lskip.
  111. * B is an n*1 matrix that contains the right hand sides.
  112. * B is stored by columns and its leading dimension is also lskip.
  113. * B is overwritten with X.
  114. * this processes blocks of 2*2.
  115. * if this is in the factorizer source file, n must be a multiple of 2.
  116. */
  117. static void btSolveL1_1 (const btScalar *L, btScalar *B, int n, int lskip1)
  118. {
  119. /* declare variables - Z matrix, p and q vectors, etc */
  120. btScalar Z11,m11,Z21,m21,p1,q1,p2,*ex;
  121. const btScalar *ell;
  122. int i,j;
  123. /* compute all 2 x 1 blocks of X */
  124. for (i=0; i < n; i+=2) {
  125. /* compute all 2 x 1 block of X, from rows i..i+2-1 */
  126. /* set the Z matrix to 0 */
  127. Z11=0;
  128. Z21=0;
  129. ell = L + i*lskip1;
  130. ex = B;
  131. /* the inner loop that computes outer products and adds them to Z */
  132. for (j=i-2; j >= 0; j -= 2) {
  133. /* compute outer product and add it to the Z matrix */
  134. p1=ell[0];
  135. q1=ex[0];
  136. m11 = p1 * q1;
  137. p2=ell[lskip1];
  138. m21 = p2 * q1;
  139. Z11 += m11;
  140. Z21 += m21;
  141. /* compute outer product and add it to the Z matrix */
  142. p1=ell[1];
  143. q1=ex[1];
  144. m11 = p1 * q1;
  145. p2=ell[1+lskip1];
  146. m21 = p2 * q1;
  147. /* advance pointers */
  148. ell += 2;
  149. ex += 2;
  150. Z11 += m11;
  151. Z21 += m21;
  152. /* end of inner loop */
  153. }
  154. /* compute left-over iterations */
  155. j += 2;
  156. for (; j > 0; j--) {
  157. /* compute outer product and add it to the Z matrix */
  158. p1=ell[0];
  159. q1=ex[0];
  160. m11 = p1 * q1;
  161. p2=ell[lskip1];
  162. m21 = p2 * q1;
  163. /* advance pointers */
  164. ell += 1;
  165. ex += 1;
  166. Z11 += m11;
  167. Z21 += m21;
  168. }
  169. /* finish computing the X(i) block */
  170. Z11 = ex[0] - Z11;
  171. ex[0] = Z11;
  172. p1 = ell[lskip1];
  173. Z21 = ex[1] - Z21 - p1*Z11;
  174. ex[1] = Z21;
  175. /* end of outer loop */
  176. }
  177. }
  178. /* solve L*X=B, with B containing 2 right hand sides.
  179. * L is an n*n lower triangular matrix with ones on the diagonal.
  180. * L is stored by rows and its leading dimension is lskip.
  181. * B is an n*2 matrix that contains the right hand sides.
  182. * B is stored by columns and its leading dimension is also lskip.
  183. * B is overwritten with X.
  184. * this processes blocks of 2*2.
  185. * if this is in the factorizer source file, n must be a multiple of 2.
  186. */
  187. static void btSolveL1_2 (const btScalar *L, btScalar *B, int n, int lskip1)
  188. {
  189. /* declare variables - Z matrix, p and q vectors, etc */
  190. btScalar Z11,m11,Z12,m12,Z21,m21,Z22,m22,p1,q1,p2,q2,*ex;
  191. const btScalar *ell;
  192. int i,j;
  193. /* compute all 2 x 2 blocks of X */
  194. for (i=0; i < n; i+=2) {
  195. /* compute all 2 x 2 block of X, from rows i..i+2-1 */
  196. /* set the Z matrix to 0 */
  197. Z11=0;
  198. Z12=0;
  199. Z21=0;
  200. Z22=0;
  201. ell = L + i*lskip1;
  202. ex = B;
  203. /* the inner loop that computes outer products and adds them to Z */
  204. for (j=i-2; j >= 0; j -= 2) {
  205. /* compute outer product and add it to the Z matrix */
  206. p1=ell[0];
  207. q1=ex[0];
  208. m11 = p1 * q1;
  209. q2=ex[lskip1];
  210. m12 = p1 * q2;
  211. p2=ell[lskip1];
  212. m21 = p2 * q1;
  213. m22 = p2 * q2;
  214. Z11 += m11;
  215. Z12 += m12;
  216. Z21 += m21;
  217. Z22 += m22;
  218. /* compute outer product and add it to the Z matrix */
  219. p1=ell[1];
  220. q1=ex[1];
  221. m11 = p1 * q1;
  222. q2=ex[1+lskip1];
  223. m12 = p1 * q2;
  224. p2=ell[1+lskip1];
  225. m21 = p2 * q1;
  226. m22 = p2 * q2;
  227. /* advance pointers */
  228. ell += 2;
  229. ex += 2;
  230. Z11 += m11;
  231. Z12 += m12;
  232. Z21 += m21;
  233. Z22 += m22;
  234. /* end of inner loop */
  235. }
  236. /* compute left-over iterations */
  237. j += 2;
  238. for (; j > 0; j--) {
  239. /* compute outer product and add it to the Z matrix */
  240. p1=ell[0];
  241. q1=ex[0];
  242. m11 = p1 * q1;
  243. q2=ex[lskip1];
  244. m12 = p1 * q2;
  245. p2=ell[lskip1];
  246. m21 = p2 * q1;
  247. m22 = p2 * q2;
  248. /* advance pointers */
  249. ell += 1;
  250. ex += 1;
  251. Z11 += m11;
  252. Z12 += m12;
  253. Z21 += m21;
  254. Z22 += m22;
  255. }
  256. /* finish computing the X(i) block */
  257. Z11 = ex[0] - Z11;
  258. ex[0] = Z11;
  259. Z12 = ex[lskip1] - Z12;
  260. ex[lskip1] = Z12;
  261. p1 = ell[lskip1];
  262. Z21 = ex[1] - Z21 - p1*Z11;
  263. ex[1] = Z21;
  264. Z22 = ex[1+lskip1] - Z22 - p1*Z12;
  265. ex[1+lskip1] = Z22;
  266. /* end of outer loop */
  267. }
  268. }
  269. void btFactorLDLT (btScalar *A, btScalar *d, int n, int nskip1)
  270. {
  271. int i,j;
  272. btScalar sum,*ell,*dee,dd,p1,p2,q1,q2,Z11,m11,Z21,m21,Z22,m22;
  273. if (n < 1) return;
  274. for (i=0; i<=n-2; i += 2) {
  275. /* solve L*(D*l)=a, l is scaled elements in 2 x i block at A(i,0) */
  276. btSolveL1_2 (A,A+i*nskip1,i,nskip1);
  277. /* scale the elements in a 2 x i block at A(i,0), and also */
  278. /* compute Z = the outer product matrix that we'll need. */
  279. Z11 = 0;
  280. Z21 = 0;
  281. Z22 = 0;
  282. ell = A+i*nskip1;
  283. dee = d;
  284. for (j=i-6; j >= 0; j -= 6) {
  285. p1 = ell[0];
  286. p2 = ell[nskip1];
  287. dd = dee[0];
  288. q1 = p1*dd;
  289. q2 = p2*dd;
  290. ell[0] = q1;
  291. ell[nskip1] = q2;
  292. m11 = p1*q1;
  293. m21 = p2*q1;
  294. m22 = p2*q2;
  295. Z11 += m11;
  296. Z21 += m21;
  297. Z22 += m22;
  298. p1 = ell[1];
  299. p2 = ell[1+nskip1];
  300. dd = dee[1];
  301. q1 = p1*dd;
  302. q2 = p2*dd;
  303. ell[1] = q1;
  304. ell[1+nskip1] = q2;
  305. m11 = p1*q1;
  306. m21 = p2*q1;
  307. m22 = p2*q2;
  308. Z11 += m11;
  309. Z21 += m21;
  310. Z22 += m22;
  311. p1 = ell[2];
  312. p2 = ell[2+nskip1];
  313. dd = dee[2];
  314. q1 = p1*dd;
  315. q2 = p2*dd;
  316. ell[2] = q1;
  317. ell[2+nskip1] = q2;
  318. m11 = p1*q1;
  319. m21 = p2*q1;
  320. m22 = p2*q2;
  321. Z11 += m11;
  322. Z21 += m21;
  323. Z22 += m22;
  324. p1 = ell[3];
  325. p2 = ell[3+nskip1];
  326. dd = dee[3];
  327. q1 = p1*dd;
  328. q2 = p2*dd;
  329. ell[3] = q1;
  330. ell[3+nskip1] = q2;
  331. m11 = p1*q1;
  332. m21 = p2*q1;
  333. m22 = p2*q2;
  334. Z11 += m11;
  335. Z21 += m21;
  336. Z22 += m22;
  337. p1 = ell[4];
  338. p2 = ell[4+nskip1];
  339. dd = dee[4];
  340. q1 = p1*dd;
  341. q2 = p2*dd;
  342. ell[4] = q1;
  343. ell[4+nskip1] = q2;
  344. m11 = p1*q1;
  345. m21 = p2*q1;
  346. m22 = p2*q2;
  347. Z11 += m11;
  348. Z21 += m21;
  349. Z22 += m22;
  350. p1 = ell[5];
  351. p2 = ell[5+nskip1];
  352. dd = dee[5];
  353. q1 = p1*dd;
  354. q2 = p2*dd;
  355. ell[5] = q1;
  356. ell[5+nskip1] = q2;
  357. m11 = p1*q1;
  358. m21 = p2*q1;
  359. m22 = p2*q2;
  360. Z11 += m11;
  361. Z21 += m21;
  362. Z22 += m22;
  363. ell += 6;
  364. dee += 6;
  365. }
  366. /* compute left-over iterations */
  367. j += 6;
  368. for (; j > 0; j--) {
  369. p1 = ell[0];
  370. p2 = ell[nskip1];
  371. dd = dee[0];
  372. q1 = p1*dd;
  373. q2 = p2*dd;
  374. ell[0] = q1;
  375. ell[nskip1] = q2;
  376. m11 = p1*q1;
  377. m21 = p2*q1;
  378. m22 = p2*q2;
  379. Z11 += m11;
  380. Z21 += m21;
  381. Z22 += m22;
  382. ell++;
  383. dee++;
  384. }
  385. /* solve for diagonal 2 x 2 block at A(i,i) */
  386. Z11 = ell[0] - Z11;
  387. Z21 = ell[nskip1] - Z21;
  388. Z22 = ell[1+nskip1] - Z22;
  389. dee = d + i;
  390. /* factorize 2 x 2 block Z,dee */
  391. /* factorize row 1 */
  392. dee[0] = btRecip(Z11);
  393. /* factorize row 2 */
  394. sum = 0;
  395. q1 = Z21;
  396. q2 = q1 * dee[0];
  397. Z21 = q2;
  398. sum += q1*q2;
  399. dee[1] = btRecip(Z22 - sum);
  400. /* done factorizing 2 x 2 block */
  401. ell[nskip1] = Z21;
  402. }
  403. /* compute the (less than 2) rows at the bottom */
  404. switch (n-i) {
  405. case 0:
  406. break;
  407. case 1:
  408. btSolveL1_1 (A,A+i*nskip1,i,nskip1);
  409. /* scale the elements in a 1 x i block at A(i,0), and also */
  410. /* compute Z = the outer product matrix that we'll need. */
  411. Z11 = 0;
  412. ell = A+i*nskip1;
  413. dee = d;
  414. for (j=i-6; j >= 0; j -= 6) {
  415. p1 = ell[0];
  416. dd = dee[0];
  417. q1 = p1*dd;
  418. ell[0] = q1;
  419. m11 = p1*q1;
  420. Z11 += m11;
  421. p1 = ell[1];
  422. dd = dee[1];
  423. q1 = p1*dd;
  424. ell[1] = q1;
  425. m11 = p1*q1;
  426. Z11 += m11;
  427. p1 = ell[2];
  428. dd = dee[2];
  429. q1 = p1*dd;
  430. ell[2] = q1;
  431. m11 = p1*q1;
  432. Z11 += m11;
  433. p1 = ell[3];
  434. dd = dee[3];
  435. q1 = p1*dd;
  436. ell[3] = q1;
  437. m11 = p1*q1;
  438. Z11 += m11;
  439. p1 = ell[4];
  440. dd = dee[4];
  441. q1 = p1*dd;
  442. ell[4] = q1;
  443. m11 = p1*q1;
  444. Z11 += m11;
  445. p1 = ell[5];
  446. dd = dee[5];
  447. q1 = p1*dd;
  448. ell[5] = q1;
  449. m11 = p1*q1;
  450. Z11 += m11;
  451. ell += 6;
  452. dee += 6;
  453. }
  454. /* compute left-over iterations */
  455. j += 6;
  456. for (; j > 0; j--) {
  457. p1 = ell[0];
  458. dd = dee[0];
  459. q1 = p1*dd;
  460. ell[0] = q1;
  461. m11 = p1*q1;
  462. Z11 += m11;
  463. ell++;
  464. dee++;
  465. }
  466. /* solve for diagonal 1 x 1 block at A(i,i) */
  467. Z11 = ell[0] - Z11;
  468. dee = d + i;
  469. /* factorize 1 x 1 block Z,dee */
  470. /* factorize row 1 */
  471. dee[0] = btRecip(Z11);
  472. /* done factorizing 1 x 1 block */
  473. break;
  474. //default: *((char*)0)=0; /* this should never happen! */
  475. }
  476. }
  477. /* solve L*X=B, with B containing 1 right hand sides.
  478. * L is an n*n lower triangular matrix with ones on the diagonal.
  479. * L is stored by rows and its leading dimension is lskip.
  480. * B is an n*1 matrix that contains the right hand sides.
  481. * B is stored by columns and its leading dimension is also lskip.
  482. * B is overwritten with X.
  483. * this processes blocks of 4*4.
  484. * if this is in the factorizer source file, n must be a multiple of 4.
  485. */
  486. void btSolveL1 (const btScalar *L, btScalar *B, int n, int lskip1)
  487. {
  488. /* declare variables - Z matrix, p and q vectors, etc */
  489. btScalar Z11,Z21,Z31,Z41,p1,q1,p2,p3,p4,*ex;
  490. const btScalar *ell;
  491. int lskip2,lskip3,i,j;
  492. /* compute lskip values */
  493. lskip2 = 2*lskip1;
  494. lskip3 = 3*lskip1;
  495. /* compute all 4 x 1 blocks of X */
  496. for (i=0; i <= n-4; i+=4) {
  497. /* compute all 4 x 1 block of X, from rows i..i+4-1 */
  498. /* set the Z matrix to 0 */
  499. Z11=0;
  500. Z21=0;
  501. Z31=0;
  502. Z41=0;
  503. ell = L + i*lskip1;
  504. ex = B;
  505. /* the inner loop that computes outer products and adds them to Z */
  506. for (j=i-12; j >= 0; j -= 12) {
  507. /* load p and q values */
  508. p1=ell[0];
  509. q1=ex[0];
  510. p2=ell[lskip1];
  511. p3=ell[lskip2];
  512. p4=ell[lskip3];
  513. /* compute outer product and add it to the Z matrix */
  514. Z11 += p1 * q1;
  515. Z21 += p2 * q1;
  516. Z31 += p3 * q1;
  517. Z41 += p4 * q1;
  518. /* load p and q values */
  519. p1=ell[1];
  520. q1=ex[1];
  521. p2=ell[1+lskip1];
  522. p3=ell[1+lskip2];
  523. p4=ell[1+lskip3];
  524. /* compute outer product and add it to the Z matrix */
  525. Z11 += p1 * q1;
  526. Z21 += p2 * q1;
  527. Z31 += p3 * q1;
  528. Z41 += p4 * q1;
  529. /* load p and q values */
  530. p1=ell[2];
  531. q1=ex[2];
  532. p2=ell[2+lskip1];
  533. p3=ell[2+lskip2];
  534. p4=ell[2+lskip3];
  535. /* compute outer product and add it to the Z matrix */
  536. Z11 += p1 * q1;
  537. Z21 += p2 * q1;
  538. Z31 += p3 * q1;
  539. Z41 += p4 * q1;
  540. /* load p and q values */
  541. p1=ell[3];
  542. q1=ex[3];
  543. p2=ell[3+lskip1];
  544. p3=ell[3+lskip2];
  545. p4=ell[3+lskip3];
  546. /* compute outer product and add it to the Z matrix */
  547. Z11 += p1 * q1;
  548. Z21 += p2 * q1;
  549. Z31 += p3 * q1;
  550. Z41 += p4 * q1;
  551. /* load p and q values */
  552. p1=ell[4];
  553. q1=ex[4];
  554. p2=ell[4+lskip1];
  555. p3=ell[4+lskip2];
  556. p4=ell[4+lskip3];
  557. /* compute outer product and add it to the Z matrix */
  558. Z11 += p1 * q1;
  559. Z21 += p2 * q1;
  560. Z31 += p3 * q1;
  561. Z41 += p4 * q1;
  562. /* load p and q values */
  563. p1=ell[5];
  564. q1=ex[5];
  565. p2=ell[5+lskip1];
  566. p3=ell[5+lskip2];
  567. p4=ell[5+lskip3];
  568. /* compute outer product and add it to the Z matrix */
  569. Z11 += p1 * q1;
  570. Z21 += p2 * q1;
  571. Z31 += p3 * q1;
  572. Z41 += p4 * q1;
  573. /* load p and q values */
  574. p1=ell[6];
  575. q1=ex[6];
  576. p2=ell[6+lskip1];
  577. p3=ell[6+lskip2];
  578. p4=ell[6+lskip3];
  579. /* compute outer product and add it to the Z matrix */
  580. Z11 += p1 * q1;
  581. Z21 += p2 * q1;
  582. Z31 += p3 * q1;
  583. Z41 += p4 * q1;
  584. /* load p and q values */
  585. p1=ell[7];
  586. q1=ex[7];
  587. p2=ell[7+lskip1];
  588. p3=ell[7+lskip2];
  589. p4=ell[7+lskip3];
  590. /* compute outer product and add it to the Z matrix */
  591. Z11 += p1 * q1;
  592. Z21 += p2 * q1;
  593. Z31 += p3 * q1;
  594. Z41 += p4 * q1;
  595. /* load p and q values */
  596. p1=ell[8];
  597. q1=ex[8];
  598. p2=ell[8+lskip1];
  599. p3=ell[8+lskip2];
  600. p4=ell[8+lskip3];
  601. /* compute outer product and add it to the Z matrix */
  602. Z11 += p1 * q1;
  603. Z21 += p2 * q1;
  604. Z31 += p3 * q1;
  605. Z41 += p4 * q1;
  606. /* load p and q values */
  607. p1=ell[9];
  608. q1=ex[9];
  609. p2=ell[9+lskip1];
  610. p3=ell[9+lskip2];
  611. p4=ell[9+lskip3];
  612. /* compute outer product and add it to the Z matrix */
  613. Z11 += p1 * q1;
  614. Z21 += p2 * q1;
  615. Z31 += p3 * q1;
  616. Z41 += p4 * q1;
  617. /* load p and q values */
  618. p1=ell[10];
  619. q1=ex[10];
  620. p2=ell[10+lskip1];
  621. p3=ell[10+lskip2];
  622. p4=ell[10+lskip3];
  623. /* compute outer product and add it to the Z matrix */
  624. Z11 += p1 * q1;
  625. Z21 += p2 * q1;
  626. Z31 += p3 * q1;
  627. Z41 += p4 * q1;
  628. /* load p and q values */
  629. p1=ell[11];
  630. q1=ex[11];
  631. p2=ell[11+lskip1];
  632. p3=ell[11+lskip2];
  633. p4=ell[11+lskip3];
  634. /* compute outer product and add it to the Z matrix */
  635. Z11 += p1 * q1;
  636. Z21 += p2 * q1;
  637. Z31 += p3 * q1;
  638. Z41 += p4 * q1;
  639. /* advance pointers */
  640. ell += 12;
  641. ex += 12;
  642. /* end of inner loop */
  643. }
  644. /* compute left-over iterations */
  645. j += 12;
  646. for (; j > 0; j--) {
  647. /* load p and q values */
  648. p1=ell[0];
  649. q1=ex[0];
  650. p2=ell[lskip1];
  651. p3=ell[lskip2];
  652. p4=ell[lskip3];
  653. /* compute outer product and add it to the Z matrix */
  654. Z11 += p1 * q1;
  655. Z21 += p2 * q1;
  656. Z31 += p3 * q1;
  657. Z41 += p4 * q1;
  658. /* advance pointers */
  659. ell += 1;
  660. ex += 1;
  661. }
  662. /* finish computing the X(i) block */
  663. Z11 = ex[0] - Z11;
  664. ex[0] = Z11;
  665. p1 = ell[lskip1];
  666. Z21 = ex[1] - Z21 - p1*Z11;
  667. ex[1] = Z21;
  668. p1 = ell[lskip2];
  669. p2 = ell[1+lskip2];
  670. Z31 = ex[2] - Z31 - p1*Z11 - p2*Z21;
  671. ex[2] = Z31;
  672. p1 = ell[lskip3];
  673. p2 = ell[1+lskip3];
  674. p3 = ell[2+lskip3];
  675. Z41 = ex[3] - Z41 - p1*Z11 - p2*Z21 - p3*Z31;
  676. ex[3] = Z41;
  677. /* end of outer loop */
  678. }
  679. /* compute rows at end that are not a multiple of block size */
  680. for (; i < n; i++) {
  681. /* compute all 1 x 1 block of X, from rows i..i+1-1 */
  682. /* set the Z matrix to 0 */
  683. Z11=0;
  684. ell = L + i*lskip1;
  685. ex = B;
  686. /* the inner loop that computes outer products and adds them to Z */
  687. for (j=i-12; j >= 0; j -= 12) {
  688. /* load p and q values */
  689. p1=ell[0];
  690. q1=ex[0];
  691. /* compute outer product and add it to the Z matrix */
  692. Z11 += p1 * q1;
  693. /* load p and q values */
  694. p1=ell[1];
  695. q1=ex[1];
  696. /* compute outer product and add it to the Z matrix */
  697. Z11 += p1 * q1;
  698. /* load p and q values */
  699. p1=ell[2];
  700. q1=ex[2];
  701. /* compute outer product and add it to the Z matrix */
  702. Z11 += p1 * q1;
  703. /* load p and q values */
  704. p1=ell[3];
  705. q1=ex[3];
  706. /* compute outer product and add it to the Z matrix */
  707. Z11 += p1 * q1;
  708. /* load p and q values */
  709. p1=ell[4];
  710. q1=ex[4];
  711. /* compute outer product and add it to the Z matrix */
  712. Z11 += p1 * q1;
  713. /* load p and q values */
  714. p1=ell[5];
  715. q1=ex[5];
  716. /* compute outer product and add it to the Z matrix */
  717. Z11 += p1 * q1;
  718. /* load p and q values */
  719. p1=ell[6];
  720. q1=ex[6];
  721. /* compute outer product and add it to the Z matrix */
  722. Z11 += p1 * q1;
  723. /* load p and q values */
  724. p1=ell[7];
  725. q1=ex[7];
  726. /* compute outer product and add it to the Z matrix */
  727. Z11 += p1 * q1;
  728. /* load p and q values */
  729. p1=ell[8];
  730. q1=ex[8];
  731. /* compute outer product and add it to the Z matrix */
  732. Z11 += p1 * q1;
  733. /* load p and q values */
  734. p1=ell[9];
  735. q1=ex[9];
  736. /* compute outer product and add it to the Z matrix */
  737. Z11 += p1 * q1;
  738. /* load p and q values */
  739. p1=ell[10];
  740. q1=ex[10];
  741. /* compute outer product and add it to the Z matrix */
  742. Z11 += p1 * q1;
  743. /* load p and q values */
  744. p1=ell[11];
  745. q1=ex[11];
  746. /* compute outer product and add it to the Z matrix */
  747. Z11 += p1 * q1;
  748. /* advance pointers */
  749. ell += 12;
  750. ex += 12;
  751. /* end of inner loop */
  752. }
  753. /* compute left-over iterations */
  754. j += 12;
  755. for (; j > 0; j--) {
  756. /* load p and q values */
  757. p1=ell[0];
  758. q1=ex[0];
  759. /* compute outer product and add it to the Z matrix */
  760. Z11 += p1 * q1;
  761. /* advance pointers */
  762. ell += 1;
  763. ex += 1;
  764. }
  765. /* finish computing the X(i) block */
  766. Z11 = ex[0] - Z11;
  767. ex[0] = Z11;
  768. }
  769. }
  770. /* solve L^T * x=b, with b containing 1 right hand side.
  771. * L is an n*n lower triangular matrix with ones on the diagonal.
  772. * L is stored by rows and its leading dimension is lskip.
  773. * b is an n*1 matrix that contains the right hand side.
  774. * b is overwritten with x.
  775. * this processes blocks of 4.
  776. */
  777. void btSolveL1T (const btScalar *L, btScalar *B, int n, int lskip1)
  778. {
  779. /* declare variables - Z matrix, p and q vectors, etc */
  780. btScalar Z11,m11,Z21,m21,Z31,m31,Z41,m41,p1,q1,p2,p3,p4,*ex;
  781. const btScalar *ell;
  782. int lskip2,lskip3,i,j;
  783. /* special handling for L and B because we're solving L1 *transpose* */
  784. L = L + (n-1)*(lskip1+1);
  785. B = B + n-1;
  786. lskip1 = -lskip1;
  787. /* compute lskip values */
  788. lskip2 = 2*lskip1;
  789. lskip3 = 3*lskip1;
  790. /* compute all 4 x 1 blocks of X */
  791. for (i=0; i <= n-4; i+=4) {
  792. /* compute all 4 x 1 block of X, from rows i..i+4-1 */
  793. /* set the Z matrix to 0 */
  794. Z11=0;
  795. Z21=0;
  796. Z31=0;
  797. Z41=0;
  798. ell = L - i;
  799. ex = B;
  800. /* the inner loop that computes outer products and adds them to Z */
  801. for (j=i-4; j >= 0; j -= 4) {
  802. /* load p and q values */
  803. p1=ell[0];
  804. q1=ex[0];
  805. p2=ell[-1];
  806. p3=ell[-2];
  807. p4=ell[-3];
  808. /* compute outer product and add it to the Z matrix */
  809. m11 = p1 * q1;
  810. m21 = p2 * q1;
  811. m31 = p3 * q1;
  812. m41 = p4 * q1;
  813. ell += lskip1;
  814. Z11 += m11;
  815. Z21 += m21;
  816. Z31 += m31;
  817. Z41 += m41;
  818. /* load p and q values */
  819. p1=ell[0];
  820. q1=ex[-1];
  821. p2=ell[-1];
  822. p3=ell[-2];
  823. p4=ell[-3];
  824. /* compute outer product and add it to the Z matrix */
  825. m11 = p1 * q1;
  826. m21 = p2 * q1;
  827. m31 = p3 * q1;
  828. m41 = p4 * q1;
  829. ell += lskip1;
  830. Z11 += m11;
  831. Z21 += m21;
  832. Z31 += m31;
  833. Z41 += m41;
  834. /* load p and q values */
  835. p1=ell[0];
  836. q1=ex[-2];
  837. p2=ell[-1];
  838. p3=ell[-2];
  839. p4=ell[-3];
  840. /* compute outer product and add it to the Z matrix */
  841. m11 = p1 * q1;
  842. m21 = p2 * q1;
  843. m31 = p3 * q1;
  844. m41 = p4 * q1;
  845. ell += lskip1;
  846. Z11 += m11;
  847. Z21 += m21;
  848. Z31 += m31;
  849. Z41 += m41;
  850. /* load p and q values */
  851. p1=ell[0];
  852. q1=ex[-3];
  853. p2=ell[-1];
  854. p3=ell[-2];
  855. p4=ell[-3];
  856. /* compute outer product and add it to the Z matrix */
  857. m11 = p1 * q1;
  858. m21 = p2 * q1;
  859. m31 = p3 * q1;
  860. m41 = p4 * q1;
  861. ell += lskip1;
  862. ex -= 4;
  863. Z11 += m11;
  864. Z21 += m21;
  865. Z31 += m31;
  866. Z41 += m41;
  867. /* end of inner loop */
  868. }
  869. /* compute left-over iterations */
  870. j += 4;
  871. for (; j > 0; j--) {
  872. /* load p and q values */
  873. p1=ell[0];
  874. q1=ex[0];
  875. p2=ell[-1];
  876. p3=ell[-2];
  877. p4=ell[-3];
  878. /* compute outer product and add it to the Z matrix */
  879. m11 = p1 * q1;
  880. m21 = p2 * q1;
  881. m31 = p3 * q1;
  882. m41 = p4 * q1;
  883. ell += lskip1;
  884. ex -= 1;
  885. Z11 += m11;
  886. Z21 += m21;
  887. Z31 += m31;
  888. Z41 += m41;
  889. }
  890. /* finish computing the X(i) block */
  891. Z11 = ex[0] - Z11;
  892. ex[0] = Z11;
  893. p1 = ell[-1];
  894. Z21 = ex[-1] - Z21 - p1*Z11;
  895. ex[-1] = Z21;
  896. p1 = ell[-2];
  897. p2 = ell[-2+lskip1];
  898. Z31 = ex[-2] - Z31 - p1*Z11 - p2*Z21;
  899. ex[-2] = Z31;
  900. p1 = ell[-3];
  901. p2 = ell[-3+lskip1];
  902. p3 = ell[-3+lskip2];
  903. Z41 = ex[-3] - Z41 - p1*Z11 - p2*Z21 - p3*Z31;
  904. ex[-3] = Z41;
  905. /* end of outer loop */
  906. }
  907. /* compute rows at end that are not a multiple of block size */
  908. for (; i < n; i++) {
  909. /* compute all 1 x 1 block of X, from rows i..i+1-1 */
  910. /* set the Z matrix to 0 */
  911. Z11=0;
  912. ell = L - i;
  913. ex = B;
  914. /* the inner loop that computes outer products and adds them to Z */
  915. for (j=i-4; j >= 0; j -= 4) {
  916. /* load p and q values */
  917. p1=ell[0];
  918. q1=ex[0];
  919. /* compute outer product and add it to the Z matrix */
  920. m11 = p1 * q1;
  921. ell += lskip1;
  922. Z11 += m11;
  923. /* load p and q values */
  924. p1=ell[0];
  925. q1=ex[-1];
  926. /* compute outer product and add it to the Z matrix */
  927. m11 = p1 * q1;
  928. ell += lskip1;
  929. Z11 += m11;
  930. /* load p and q values */
  931. p1=ell[0];
  932. q1=ex[-2];
  933. /* compute outer product and add it to the Z matrix */
  934. m11 = p1 * q1;
  935. ell += lskip1;
  936. Z11 += m11;
  937. /* load p and q values */
  938. p1=ell[0];
  939. q1=ex[-3];
  940. /* compute outer product and add it to the Z matrix */
  941. m11 = p1 * q1;
  942. ell += lskip1;
  943. ex -= 4;
  944. Z11 += m11;
  945. /* end of inner loop */
  946. }
  947. /* compute left-over iterations */
  948. j += 4;
  949. for (; j > 0; j--) {
  950. /* load p and q values */
  951. p1=ell[0];
  952. q1=ex[0];
  953. /* compute outer product and add it to the Z matrix */
  954. m11 = p1 * q1;
  955. ell += lskip1;
  956. ex -= 1;
  957. Z11 += m11;
  958. }
  959. /* finish computing the X(i) block */
  960. Z11 = ex[0] - Z11;
  961. ex[0] = Z11;
  962. }
  963. }
  964. void btVectorScale (btScalar *a, const btScalar *d, int n)
  965. {
  966. btAssert (a && d && n >= 0);
  967. for (int i=0; i<n; i++) {
  968. a[i] *= d[i];
  969. }
  970. }
  971. void btSolveLDLT (const btScalar *L, const btScalar *d, btScalar *b, int n, int nskip)
  972. {
  973. btAssert (L && d && b && n > 0 && nskip >= n);
  974. btSolveL1 (L,b,n,nskip);
  975. btVectorScale (b,d,n);
  976. btSolveL1T (L,b,n,nskip);
  977. }
  978. //***************************************************************************
  979. // swap row/column i1 with i2 in the n*n matrix A. the leading dimension of
  980. // A is nskip. this only references and swaps the lower triangle.
  981. // if `do_fast_row_swaps' is nonzero and row pointers are being used, then
  982. // rows will be swapped by exchanging row pointers. otherwise the data will
  983. // be copied.
  984. static void btSwapRowsAndCols (BTATYPE A, int n, int i1, int i2, int nskip,
  985. int do_fast_row_swaps)
  986. {
  987. btAssert (A && n > 0 && i1 >= 0 && i2 >= 0 && i1 < n && i2 < n &&
  988. nskip >= n && i1 < i2);
  989. # ifdef BTROWPTRS
  990. btScalar *A_i1 = A[i1];
  991. btScalar *A_i2 = A[i2];
  992. for (int i=i1+1; i<i2; ++i) {
  993. btScalar *A_i_i1 = A[i] + i1;
  994. A_i1[i] = *A_i_i1;
  995. *A_i_i1 = A_i2[i];
  996. }
  997. A_i1[i2] = A_i1[i1];
  998. A_i1[i1] = A_i2[i1];
  999. A_i2[i1] = A_i2[i2];
  1000. // swap rows, by swapping row pointers
  1001. if (do_fast_row_swaps) {
  1002. A[i1] = A_i2;
  1003. A[i2] = A_i1;
  1004. }
  1005. else {
  1006. // Only swap till i2 column to match A plain storage variant.
  1007. for (int k = 0; k <= i2; ++k) {
  1008. btScalar tmp = A_i1[k];
  1009. A_i1[k] = A_i2[k];
  1010. A_i2[k] = tmp;
  1011. }
  1012. }
  1013. // swap columns the hard way
  1014. for (int j=i2+1; j<n; ++j) {
  1015. btScalar *A_j = A[j];
  1016. btScalar tmp = A_j[i1];
  1017. A_j[i1] = A_j[i2];
  1018. A_j[i2] = tmp;
  1019. }
  1020. # else
  1021. btScalar *A_i1 = A+i1*nskip;
  1022. btScalar *A_i2 = A+i2*nskip;
  1023. for (int k = 0; k < i1; ++k) {
  1024. btScalar tmp = A_i1[k];
  1025. A_i1[k] = A_i2[k];
  1026. A_i2[k] = tmp;
  1027. }
  1028. btScalar *A_i = A_i1 + nskip;
  1029. for (int i=i1+1; i<i2; A_i+=nskip, ++i) {
  1030. btScalar tmp = A_i2[i];
  1031. A_i2[i] = A_i[i1];
  1032. A_i[i1] = tmp;
  1033. }
  1034. {
  1035. btScalar tmp = A_i1[i1];
  1036. A_i1[i1] = A_i2[i2];
  1037. A_i2[i2] = tmp;
  1038. }
  1039. btScalar *A_j = A_i2 + nskip;
  1040. for (int j=i2+1; j<n; A_j+=nskip, ++j) {
  1041. btScalar tmp = A_j[i1];
  1042. A_j[i1] = A_j[i2];
  1043. A_j[i2] = tmp;
  1044. }
  1045. # endif
  1046. }
  1047. // swap two indexes in the n*n LCP problem. i1 must be <= i2.
  1048. static void btSwapProblem (BTATYPE A, btScalar *x, btScalar *b, btScalar *w, btScalar *lo,
  1049. btScalar *hi, int *p, bool *state, int *findex,
  1050. int n, int i1, int i2, int nskip,
  1051. int do_fast_row_swaps)
  1052. {
  1053. btScalar tmpr;
  1054. int tmpi;
  1055. bool tmpb;
  1056. btAssert (n>0 && i1 >=0 && i2 >= 0 && i1 < n && i2 < n && nskip >= n && i1 <= i2);
  1057. if (i1==i2) return;
  1058. btSwapRowsAndCols (A,n,i1,i2,nskip,do_fast_row_swaps);
  1059. tmpr = x[i1];
  1060. x[i1] = x[i2];
  1061. x[i2] = tmpr;
  1062. tmpr = b[i1];
  1063. b[i1] = b[i2];
  1064. b[i2] = tmpr;
  1065. tmpr = w[i1];
  1066. w[i1] = w[i2];
  1067. w[i2] = tmpr;
  1068. tmpr = lo[i1];
  1069. lo[i1] = lo[i2];
  1070. lo[i2] = tmpr;
  1071. tmpr = hi[i1];
  1072. hi[i1] = hi[i2];
  1073. hi[i2] = tmpr;
  1074. tmpi = p[i1];
  1075. p[i1] = p[i2];
  1076. p[i2] = tmpi;
  1077. tmpb = state[i1];
  1078. state[i1] = state[i2];
  1079. state[i2] = tmpb;
  1080. if (findex) {
  1081. tmpi = findex[i1];
  1082. findex[i1] = findex[i2];
  1083. findex[i2] = tmpi;
  1084. }
  1085. }
  1086. //***************************************************************************
  1087. // btLCP manipulator object. this represents an n*n LCP problem.
  1088. //
  1089. // two index sets C and N are kept. each set holds a subset of
  1090. // the variable indexes 0..n-1. an index can only be in one set.
  1091. // initially both sets are empty.
  1092. //
  1093. // the index set C is special: solutions to A(C,C)\A(C,i) can be generated.
  1094. //***************************************************************************
  1095. // fast implementation of btLCP. see the above definition of btLCP for
  1096. // interface comments.
  1097. //
  1098. // `p' records the permutation of A,x,b,w,etc. p is initially 1:n and is
  1099. // permuted as the other vectors/matrices are permuted.
  1100. //
  1101. // A,x,b,w,lo,hi,state,findex,p,c are permuted such that sets C,N have
  1102. // contiguous indexes. the don't-care indexes follow N.
  1103. //
  1104. // an L*D*L' factorization is maintained of A(C,C), and whenever indexes are
  1105. // added or removed from the set C the factorization is updated.
  1106. // thus L*D*L'=A[C,C], i.e. a permuted top left nC*nC submatrix of A.
  1107. // the leading dimension of the matrix L is always `nskip'.
  1108. //
  1109. // at the start there may be other indexes that are unbounded but are not
  1110. // included in `nub'. btLCP will permute the matrix so that absolutely all
  1111. // unbounded vectors are at the start. thus there may be some initial
  1112. // permutation.
  1113. //
  1114. // the algorithms here assume certain patterns, particularly with respect to
  1115. // index transfer.
  1116. #ifdef btLCP_FAST
  1117. struct btLCP
  1118. {
  1119. const int m_n;
  1120. const int m_nskip;
  1121. int m_nub;
  1122. int m_nC, m_nN; // size of each index set
  1123. BTATYPE const m_A; // A rows
  1124. btScalar *const m_x, * const m_b, *const m_w, *const m_lo,* const m_hi; // permuted LCP problem data
  1125. btScalar *const m_L, *const m_d; // L*D*L' factorization of set C
  1126. btScalar *const m_Dell, *const m_ell, *const m_tmp;
  1127. bool *const m_state;
  1128. int *const m_findex, *const m_p, *const m_C;
  1129. btLCP (int _n, int _nskip, int _nub, btScalar *_Adata, btScalar *_x, btScalar *_b, btScalar *_w,
  1130. btScalar *_lo, btScalar *_hi, btScalar *_L, btScalar *_d,
  1131. btScalar *_Dell, btScalar *_ell, btScalar *_tmp,
  1132. bool *_state, int *_findex, int *_p, int *_C, btScalar **Arows);
  1133. int getNub() const { return m_nub; }
  1134. void transfer_i_to_C (int i);
  1135. void transfer_i_to_N (int i) { m_nN++; } // because we can assume C and N span 1:i-1
  1136. void transfer_i_from_N_to_C (int i);
  1137. void transfer_i_from_C_to_N (int i, btAlignedObjectArray<btScalar>& scratch);
  1138. int numC() const { return m_nC; }
  1139. int numN() const { return m_nN; }
  1140. int indexC (int i) const { return i; }
  1141. int indexN (int i) const { return i+m_nC; }
  1142. btScalar Aii (int i) const { return BTAROW(i)[i]; }
  1143. btScalar AiC_times_qC (int i, btScalar *q) const { return btLargeDot (BTAROW(i), q, m_nC); }
  1144. btScalar AiN_times_qN (int i, btScalar *q) const { return btLargeDot (BTAROW(i)+m_nC, q+m_nC, m_nN); }
  1145. void pN_equals_ANC_times_qC (btScalar *p, btScalar *q);
  1146. void pN_plusequals_ANi (btScalar *p, int i, int sign=1);
  1147. void pC_plusequals_s_times_qC (btScalar *p, btScalar s, btScalar *q);
  1148. void pN_plusequals_s_times_qN (btScalar *p, btScalar s, btScalar *q);
  1149. void solve1 (btScalar *a, int i, int dir=1, int only_transfer=0);
  1150. void unpermute();
  1151. };
  1152. btLCP::btLCP (int _n, int _nskip, int _nub, btScalar *_Adata, btScalar *_x, btScalar *_b, btScalar *_w,
  1153. btScalar *_lo, btScalar *_hi, btScalar *_L, btScalar *_d,
  1154. btScalar *_Dell, btScalar *_ell, btScalar *_tmp,
  1155. bool *_state, int *_findex, int *_p, int *_C, btScalar **Arows):
  1156. m_n(_n), m_nskip(_nskip), m_nub(_nub), m_nC(0), m_nN(0),
  1157. # ifdef BTROWPTRS
  1158. m_A(Arows),
  1159. #else
  1160. m_A(_Adata),
  1161. #endif
  1162. m_x(_x), m_b(_b), m_w(_w), m_lo(_lo), m_hi(_hi),
  1163. m_L(_L), m_d(_d), m_Dell(_Dell), m_ell(_ell), m_tmp(_tmp),
  1164. m_state(_state), m_findex(_findex), m_p(_p), m_C(_C)
  1165. {
  1166. {
  1167. btSetZero (m_x,m_n);
  1168. }
  1169. {
  1170. # ifdef BTROWPTRS
  1171. // make matrix row pointers
  1172. btScalar *aptr = _Adata;
  1173. BTATYPE A = m_A;
  1174. const int n = m_n, nskip = m_nskip;
  1175. for (int k=0; k<n; aptr+=nskip, ++k) A[k] = aptr;
  1176. # endif
  1177. }
  1178. {
  1179. int *p = m_p;
  1180. const int n = m_n;
  1181. for (int k=0; k<n; ++k) p[k]=k; // initially unpermuted
  1182. }
  1183. /*
  1184. // for testing, we can do some random swaps in the area i > nub
  1185. {
  1186. const int n = m_n;
  1187. const int nub = m_nub;
  1188. if (nub < n) {
  1189. for (int k=0; k<100; k++) {
  1190. int i1,i2;
  1191. do {
  1192. i1 = dRandInt(n-nub)+nub;
  1193. i2 = dRandInt(n-nub)+nub;
  1194. }
  1195. while (i1 > i2);
  1196. //printf ("--> %d %d\n",i1,i2);
  1197. btSwapProblem (m_A,m_x,m_b,m_w,m_lo,m_hi,m_p,m_state,m_findex,n,i1,i2,m_nskip,0);
  1198. }
  1199. }
  1200. */
  1201. // permute the problem so that *all* the unbounded variables are at the
  1202. // start, i.e. look for unbounded variables not included in `nub'. we can
  1203. // potentially push up `nub' this way and get a bigger initial factorization.
  1204. // note that when we swap rows/cols here we must not just swap row pointers,
  1205. // as the initial factorization relies on the data being all in one chunk.
  1206. // variables that have findex >= 0 are *not* considered to be unbounded even
  1207. // if lo=-inf and hi=inf - this is because these limits may change during the
  1208. // solution process.
  1209. {
  1210. int *findex = m_findex;
  1211. btScalar *lo = m_lo, *hi = m_hi;
  1212. const int n = m_n;
  1213. for (int k = m_nub; k<n; ++k) {
  1214. if (findex && findex[k] >= 0) continue;
  1215. if (lo[k]==-BT_INFINITY && hi[k]==BT_INFINITY) {
  1216. btSwapProblem (m_A,m_x,m_b,m_w,lo,hi,m_p,m_state,findex,n,m_nub,k,m_nskip,0);
  1217. m_nub++;
  1218. }
  1219. }
  1220. }
  1221. // if there are unbounded variables at the start, factorize A up to that
  1222. // point and solve for x. this puts all indexes 0..nub-1 into C.
  1223. if (m_nub > 0) {
  1224. const int nub = m_nub;
  1225. {
  1226. btScalar *Lrow = m_L;
  1227. const int nskip = m_nskip;
  1228. for (int j=0; j<nub; Lrow+=nskip, ++j) memcpy(Lrow,BTAROW(j),(j+1)*sizeof(btScalar));
  1229. }
  1230. btFactorLDLT (m_L,m_d,nub,m_nskip);
  1231. memcpy (m_x,m_b,nub*sizeof(btScalar));
  1232. btSolveLDLT (m_L,m_d,m_x,nub,m_nskip);
  1233. btSetZero (m_w,nub);
  1234. {
  1235. int *C = m_C;
  1236. for (int k=0; k<nub; ++k) C[k] = k;
  1237. }
  1238. m_nC = nub;
  1239. }
  1240. // permute the indexes > nub such that all findex variables are at the end
  1241. if (m_findex) {
  1242. const int nub = m_nub;
  1243. int *findex = m_findex;
  1244. int num_at_end = 0;
  1245. for (int k=m_n-1; k >= nub; k--) {
  1246. if (findex[k] >= 0) {
  1247. btSwapProblem (m_A,m_x,m_b,m_w,m_lo,m_hi,m_p,m_state,findex,m_n,k,m_n-1-num_at_end,m_nskip,1);
  1248. num_at_end++;
  1249. }
  1250. }
  1251. }
  1252. // print info about indexes
  1253. /*
  1254. {
  1255. const int n = m_n;
  1256. const int nub = m_nub;
  1257. for (int k=0; k<n; k++) {
  1258. if (k<nub) printf ("C");
  1259. else if (m_lo[k]==-BT_INFINITY && m_hi[k]==BT_INFINITY) printf ("c");
  1260. else printf (".");
  1261. }
  1262. printf ("\n");
  1263. }
  1264. */
  1265. }
  1266. void btLCP::transfer_i_to_C (int i)
  1267. {
  1268. {
  1269. if (m_nC > 0) {
  1270. // ell,Dell were computed by solve1(). note, ell = D \ L1solve (L,A(i,C))
  1271. {
  1272. const int nC = m_nC;
  1273. btScalar *const Ltgt = m_L + nC*m_nskip, *ell = m_ell;
  1274. for (int j=0; j<nC; ++j) Ltgt[j] = ell[j];
  1275. }
  1276. const int nC = m_nC;
  1277. m_d[nC] = btRecip (BTAROW(i)[i] - btLargeDot(m_ell,m_Dell,nC));
  1278. }
  1279. else {
  1280. m_d[0] = btRecip (BTAROW(i)[i]);
  1281. }
  1282. btSwapProblem (m_A,m_x,m_b,m_w,m_lo,m_hi,m_p,m_state,m_findex,m_n,m_nC,i,m_nskip,1);
  1283. const int nC = m_nC;
  1284. m_C[nC] = nC;
  1285. m_nC = nC + 1; // nC value is outdated after this line
  1286. }
  1287. }
  1288. void btLCP::transfer_i_from_N_to_C (int i)
  1289. {
  1290. {
  1291. if (m_nC > 0) {
  1292. {
  1293. btScalar *const aptr = BTAROW(i);
  1294. btScalar *Dell = m_Dell;
  1295. const int *C = m_C;
  1296. # ifdef BTNUB_OPTIMIZATIONS
  1297. // if nub>0, initial part of aptr unpermuted
  1298. const int nub = m_nub;
  1299. int j=0;
  1300. for ( ; j<nub; ++j) Dell[j] = aptr[j];
  1301. const int nC = m_nC;
  1302. for ( ; j<nC; ++j) Dell[j] = aptr[C[j]];
  1303. # else
  1304. const int nC = m_nC;
  1305. for (int j=0; j<nC; ++j) Dell[j] = aptr[C[j]];
  1306. # endif
  1307. }
  1308. btSolveL1 (m_L,m_Dell,m_nC,m_nskip);
  1309. {
  1310. const int nC = m_nC;
  1311. btScalar *const Ltgt = m_L + nC*m_nskip;
  1312. btScalar *ell = m_ell, *Dell = m_Dell, *d = m_d;
  1313. for (int j=0; j<nC; ++j) Ltgt[j] = ell[j] = Dell[j] * d[j];
  1314. }
  1315. const int nC = m_nC;
  1316. m_d[nC] = btRecip (BTAROW(i)[i] - btLargeDot(m_ell,m_Dell,nC));
  1317. }
  1318. else {
  1319. m_d[0] = btRecip (BTAROW(i)[i]);
  1320. }
  1321. btSwapProblem (m_A,m_x,m_b,m_w,m_lo,m_hi,m_p,m_state,m_findex,m_n,m_nC,i,m_nskip,1);
  1322. const int nC = m_nC;
  1323. m_C[nC] = nC;
  1324. m_nN--;
  1325. m_nC = nC + 1; // nC value is outdated after this line
  1326. }
  1327. // @@@ TO DO LATER
  1328. // if we just finish here then we'll go back and re-solve for
  1329. // delta_x. but actually we can be more efficient and incrementally
  1330. // update delta_x here. but if we do this, we wont have ell and Dell
  1331. // to use in updating the factorization later.
  1332. }
  1333. void btRemoveRowCol (btScalar *A, int n, int nskip, int r)
  1334. {
  1335. btAssert(A && n > 0 && nskip >= n && r >= 0 && r < n);
  1336. if (r >= n-1) return;
  1337. if (r > 0) {
  1338. {
  1339. const size_t move_size = (n-r-1)*sizeof(btScalar);
  1340. btScalar *Adst = A + r;
  1341. for (int i=0; i<r; Adst+=nskip,++i) {
  1342. btScalar *Asrc = Adst + 1;
  1343. memmove (Adst,Asrc,move_size);
  1344. }
  1345. }
  1346. {
  1347. const size_t cpy_size = r*sizeof(btScalar);
  1348. btScalar *Adst = A + r * nskip;
  1349. for (int i=r; i<(n-1); ++i) {
  1350. btScalar *Asrc = Adst + nskip;
  1351. memcpy (Adst,Asrc,cpy_size);
  1352. Adst = Asrc;
  1353. }
  1354. }
  1355. }
  1356. {
  1357. const size_t cpy_size = (n-r-1)*sizeof(btScalar);
  1358. btScalar *Adst = A + r * (nskip + 1);
  1359. for (int i=r; i<(n-1); ++i) {
  1360. btScalar *Asrc = Adst + (nskip + 1);
  1361. memcpy (Adst,Asrc,cpy_size);
  1362. Adst = Asrc - 1;
  1363. }
  1364. }
  1365. }
  1366. void btLDLTAddTL (btScalar *L, btScalar *d, const btScalar *a, int n, int nskip, btAlignedObjectArray<btScalar>& scratch)
  1367. {
  1368. btAssert (L && d && a && n > 0 && nskip >= n);
  1369. if (n < 2) return;
  1370. scratch.resize(2*nskip);
  1371. btScalar *W1 = &scratch[0];
  1372. btScalar *W2 = W1 + nskip;
  1373. W1[0] = btScalar(0.0);
  1374. W2[0] = btScalar(0.0);
  1375. for (int j=1; j<n; ++j) {
  1376. W1[j] = W2[j] = (btScalar) (a[j] * SIMDSQRT12);
  1377. }
  1378. btScalar W11 = (btScalar) ((btScalar(0.5)*a[0]+1)*SIMDSQRT12);
  1379. btScalar W21 = (btScalar) ((btScalar(0.5)*a[0]-1)*SIMDSQRT12);
  1380. btScalar alpha1 = btScalar(1.0);
  1381. btScalar alpha2 = btScalar(1.0);
  1382. {
  1383. btScalar dee = d[0];
  1384. btScalar alphanew = alpha1 + (W11*W11)*dee;
  1385. btAssert(alphanew != btScalar(0.0));
  1386. dee /= alphanew;
  1387. btScalar gamma1 = W11 * dee;
  1388. dee *= alpha1;
  1389. alpha1 = alphanew;
  1390. alphanew = alpha2 - (W21*W21)*dee;
  1391. dee /= alphanew;
  1392. //btScalar gamma2 = W21 * dee;
  1393. alpha2 = alphanew;
  1394. btScalar k1 = btScalar(1.0) - W21*gamma1;
  1395. btScalar k2 = W21*gamma1*W11 - W21;
  1396. btScalar *ll = L + nskip;
  1397. for (int p=1; p<n; ll+=nskip, ++p) {
  1398. btScalar Wp = W1[p];
  1399. btScalar ell = *ll;
  1400. W1[p] = Wp - W11*ell;
  1401. W2[p] = k1*Wp + k2*ell;
  1402. }
  1403. }
  1404. btScalar *ll = L + (nskip + 1);
  1405. for (int j=1; j<n; ll+=nskip+1, ++j) {
  1406. btScalar k1 = W1[j];
  1407. btScalar k2 = W2[j];
  1408. btScalar dee = d[j];
  1409. btScalar alphanew = alpha1 + (k1*k1)*dee;
  1410. btAssert(alphanew != btScalar(0.0));
  1411. dee /= alphanew;
  1412. btScalar gamma1 = k1 * dee;
  1413. dee *= alpha1;
  1414. alpha1 = alphanew;
  1415. alphanew = alpha2 - (k2*k2)*dee;
  1416. dee /= alphanew;
  1417. btScalar gamma2 = k2 * dee;
  1418. dee *= alpha2;
  1419. d[j] = dee;
  1420. alpha2 = alphanew;
  1421. btScalar *l = ll + nskip;
  1422. for (int p=j+1; p<n; l+=nskip, ++p) {
  1423. btScalar ell = *l;
  1424. btScalar Wp = W1[p] - k1 * ell;
  1425. ell += gamma1 * Wp;
  1426. W1[p] = Wp;
  1427. Wp = W2[p] - k2 * ell;
  1428. ell -= gamma2 * Wp;
  1429. W2[p] = Wp;
  1430. *l = ell;
  1431. }
  1432. }
  1433. }
  1434. #define _BTGETA(i,j) (A[i][j])
  1435. //#define _GETA(i,j) (A[(i)*nskip+(j)])
  1436. #define BTGETA(i,j) ((i > j) ? _BTGETA(i,j) : _BTGETA(j,i))
  1437. inline size_t btEstimateLDLTAddTLTmpbufSize(int nskip)
  1438. {
  1439. return nskip * 2 * sizeof(btScalar);
  1440. }
  1441. void btLDLTRemove (btScalar **A, const int *p, btScalar *L, btScalar *d,
  1442. int n1, int n2, int r, int nskip, btAlignedObjectArray<btScalar>& scratch)
  1443. {
  1444. btAssert(A && p && L && d && n1 > 0 && n2 > 0 && r >= 0 && r < n2 &&
  1445. n1 >= n2 && nskip >= n1);
  1446. #ifdef BT_DEBUG
  1447. for (int i=0; i<n2; ++i)
  1448. btAssert(p[i] >= 0 && p[i] < n1);
  1449. #endif
  1450. if (r==n2-1) {
  1451. return; // deleting last row/col is easy
  1452. }
  1453. else {
  1454. size_t LDLTAddTL_size = btEstimateLDLTAddTLTmpbufSize(nskip);
  1455. btAssert(LDLTAddTL_size % sizeof(btScalar) == 0);
  1456. scratch.resize(nskip * 2+n2);
  1457. btScalar *tmp = &scratch[0];
  1458. if (r==0) {
  1459. btScalar *a = (btScalar *)((char *)tmp + LDLTAddTL_size);
  1460. const int p_0 = p[0];
  1461. for (int i=0; i<n2; ++i) {
  1462. a[i] = -BTGETA(p[i],p_0);
  1463. }
  1464. a[0] += btScalar(1.0);
  1465. btLDLTAddTL (L,d,a,n2,nskip,scratch);
  1466. }
  1467. else {
  1468. btScalar *t = (btScalar *)((char *)tmp + LDLTAddTL_size);
  1469. {
  1470. btScalar *Lcurr = L + r*nskip;
  1471. for (int i=0; i<r; ++Lcurr, ++i) {
  1472. btAssert(d[i] != btScalar(0.0));
  1473. t[i] = *Lcurr / d[i];
  1474. }
  1475. }
  1476. btScalar *a = t + r;
  1477. {
  1478. btScalar *Lcurr = L + r*nskip;
  1479. const int *pp_r = p + r, p_r = *pp_r;
  1480. const int n2_minus_r = n2-r;
  1481. for (int i=0; i<n2_minus_r; Lcurr+=nskip,++i) {
  1482. a[i] = btLargeDot(Lcurr,t,r) - BTGETA(pp_r[i],p_r);
  1483. }
  1484. }
  1485. a[0] += btScalar(1.0);
  1486. btLDLTAddTL (L + r*nskip+r, d+r, a, n2-r, nskip, scratch);
  1487. }
  1488. }
  1489. // snip out row/column r from L and d
  1490. btRemoveRowCol (L,n2,nskip,r);
  1491. if (r < (n2-1)) memmove (d+r,d+r+1,(n2-r-1)*sizeof(btScalar));
  1492. }
  1493. void btLCP::transfer_i_from_C_to_N (int i, btAlignedObjectArray<btScalar>& scratch)
  1494. {
  1495. {
  1496. int *C = m_C;
  1497. // remove a row/column from the factorization, and adjust the
  1498. // indexes (black magic!)
  1499. int last_idx = -1;
  1500. const int nC = m_nC;
  1501. int j = 0;
  1502. for ( ; j<nC; ++j) {
  1503. if (C[j]==nC-1) {
  1504. last_idx = j;
  1505. }
  1506. if (C[j]==i) {
  1507. btLDLTRemove (m_A,C,m_L,m_d,m_n,nC,j,m_nskip,scratch);
  1508. int k;
  1509. if (last_idx == -1) {
  1510. for (k=j+1 ; k<nC; ++k) {
  1511. if (C[k]==nC-1) {
  1512. break;
  1513. }
  1514. }
  1515. btAssert (k < nC);
  1516. }
  1517. else {
  1518. k = last_idx;
  1519. }
  1520. C[k] = C[j];
  1521. if (j < (nC-1)) memmove (C+j,C+j+1,(nC-j-1)*sizeof(int));
  1522. break;
  1523. }
  1524. }
  1525. btAssert (j < nC);
  1526. btSwapProblem (m_A,m_x,m_b,m_w,m_lo,m_hi,m_p,m_state,m_findex,m_n,i,nC-1,m_nskip,1);
  1527. m_nN++;
  1528. m_nC = nC - 1; // nC value is outdated after this line
  1529. }
  1530. }
  1531. void btLCP::pN_equals_ANC_times_qC (btScalar *p, btScalar *q)
  1532. {
  1533. // we could try to make this matrix-vector multiplication faster using
  1534. // outer product matrix tricks, e.g. with the dMultidotX() functions.
  1535. // but i tried it and it actually made things slower on random 100x100
  1536. // problems because of the overhead involved. so we'll stick with the
  1537. // simple method for now.
  1538. const int nC = m_nC;
  1539. btScalar *ptgt = p + nC;
  1540. const int nN = m_nN;
  1541. for (int i=0; i<nN; ++i) {
  1542. ptgt[i] = btLargeDot (BTAROW(i+nC),q,nC);
  1543. }
  1544. }
  1545. void btLCP::pN_plusequals_ANi (btScalar *p, int i, int sign)
  1546. {
  1547. const int nC = m_nC;
  1548. btScalar *aptr = BTAROW(i) + nC;
  1549. btScalar *ptgt = p + nC;
  1550. if (sign > 0) {
  1551. const int nN = m_nN;
  1552. for (int j=0; j<nN; ++j) ptgt[j] += aptr[j];
  1553. }
  1554. else {
  1555. const int nN = m_nN;
  1556. for (int j=0; j<nN; ++j) ptgt[j] -= aptr[j];
  1557. }
  1558. }
  1559. void btLCP::pC_plusequals_s_times_qC (btScalar *p, btScalar s, btScalar *q)
  1560. {
  1561. const int nC = m_nC;
  1562. for (int i=0; i<nC; ++i) {
  1563. p[i] += s*q[i];
  1564. }
  1565. }
  1566. void btLCP::pN_plusequals_s_times_qN (btScalar *p, btScalar s, btScalar *q)
  1567. {
  1568. const int nC = m_nC;
  1569. btScalar *ptgt = p + nC, *qsrc = q + nC;
  1570. const int nN = m_nN;
  1571. for (int i=0; i<nN; ++i) {
  1572. ptgt[i] += s*qsrc[i];
  1573. }
  1574. }
  1575. void btLCP::solve1 (btScalar *a, int i, int dir, int only_transfer)
  1576. {
  1577. // the `Dell' and `ell' that are computed here are saved. if index i is
  1578. // later added to the factorization then they can be reused.
  1579. //
  1580. // @@@ question: do we need to solve for entire delta_x??? yes, but
  1581. // only if an x goes below 0 during the step.
  1582. if (m_nC > 0) {
  1583. {
  1584. btScalar *Dell = m_Dell;
  1585. int *C = m_C;
  1586. btScalar *aptr = BTAROW(i);
  1587. # ifdef BTNUB_OPTIMIZATIONS
  1588. // if nub>0, initial part of aptr[] is guaranteed unpermuted
  1589. const int nub = m_nub;
  1590. int j=0;
  1591. for ( ; j<nub; ++j) Dell[j] = aptr[j];
  1592. const int nC = m_nC;
  1593. for ( ; j<nC; ++j) Dell[j] = aptr[C[j]];
  1594. # else
  1595. const int nC = m_nC;
  1596. for (int j=0; j<nC; ++j) Dell[j] = aptr[C[j]];
  1597. # endif
  1598. }
  1599. btSolveL1 (m_L,m_Dell,m_nC,m_nskip);
  1600. {
  1601. btScalar *ell = m_ell, *Dell = m_Dell, *d = m_d;
  1602. const int nC = m_nC;
  1603. for (int j=0; j<nC; ++j) ell[j] = Dell[j] * d[j];
  1604. }
  1605. if (!only_transfer) {
  1606. btScalar *tmp = m_tmp, *ell = m_ell;
  1607. {
  1608. const int nC = m_nC;
  1609. for (int j=0; j<nC; ++j) tmp[j] = ell[j];
  1610. }
  1611. btSolveL1T (m_L,tmp,m_nC,m_nskip);
  1612. if (dir > 0) {
  1613. int *C = m_C;
  1614. btScalar *tmp = m_tmp;
  1615. const int nC = m_nC;
  1616. for (int j=0; j<nC; ++j) a[C[j]] = -tmp[j];
  1617. } else {
  1618. int *C = m_C;
  1619. btScalar *tmp = m_tmp;
  1620. const int nC = m_nC;
  1621. for (int j=0; j<nC; ++j) a[C[j]] = tmp[j];
  1622. }
  1623. }
  1624. }
  1625. }
  1626. void btLCP::unpermute()
  1627. {
  1628. // now we have to un-permute x and w
  1629. {
  1630. memcpy (m_tmp,m_x,m_n*sizeof(btScalar));
  1631. btScalar *x = m_x, *tmp = m_tmp;
  1632. const int *p = m_p;
  1633. const int n = m_n;
  1634. for (int j=0; j<n; ++j) x[p[j]] = tmp[j];
  1635. }
  1636. {
  1637. memcpy (m_tmp,m_w,m_n*sizeof(btScalar));
  1638. btScalar *w = m_w, *tmp = m_tmp;
  1639. const int *p = m_p;
  1640. const int n = m_n;
  1641. for (int j=0; j<n; ++j) w[p[j]] = tmp[j];
  1642. }
  1643. }
  1644. #endif // btLCP_FAST
  1645. //***************************************************************************
  1646. // an optimized Dantzig LCP driver routine for the lo-hi LCP problem.
  1647. bool btSolveDantzigLCP (int n, btScalar *A, btScalar *x, btScalar *b,
  1648. btScalar* outer_w, int nub, btScalar *lo, btScalar *hi, int *findex, btDantzigScratchMemory& scratchMem)
  1649. {
  1650. s_error = false;
  1651. // printf("btSolveDantzigLCP n=%d\n",n);
  1652. btAssert (n>0 && A && x && b && lo && hi && nub >= 0 && nub <= n);
  1653. btAssert(outer_w);
  1654. #ifdef BT_DEBUG
  1655. {
  1656. // check restrictions on lo and hi
  1657. for (int k=0; k<n; ++k)
  1658. btAssert (lo[k] <= 0 && hi[k] >= 0);
  1659. }
  1660. # endif
  1661. // if all the variables are unbounded then we can just factor, solve,
  1662. // and return
  1663. if (nub >= n)
  1664. {
  1665. int nskip = (n);
  1666. btFactorLDLT (A, outer_w, n, nskip);
  1667. btSolveLDLT (A, outer_w, b, n, nskip);
  1668. memcpy (x, b, n*sizeof(btScalar));
  1669. return !s_error;
  1670. }
  1671. const int nskip = (n);
  1672. scratchMem.L.resize(n*nskip);
  1673. scratchMem.d.resize(n);
  1674. btScalar *w = outer_w;
  1675. scratchMem.delta_w.resize(n);
  1676. scratchMem.delta_x.resize(n);
  1677. scratchMem.Dell.resize(n);
  1678. scratchMem.ell.resize(n);
  1679. scratchMem.Arows.resize(n);
  1680. scratchMem.p.resize(n);
  1681. scratchMem.C.resize(n);
  1682. // for i in N, state[i] is 0 if x(i)==lo(i) or 1 if x(i)==hi(i)
  1683. scratchMem.state.resize(n);
  1684. // create LCP object. note that tmp is set to delta_w to save space, this
  1685. // optimization relies on knowledge of how tmp is used, so be careful!
  1686. btLCP lcp(n,nskip,nub,A,x,b,w,lo,hi,&scratchMem.L[0],&scratchMem.d[0],&scratchMem.Dell[0],&scratchMem.ell[0],&scratchMem.delta_w[0],&scratchMem.state[0],findex,&scratchMem.p[0],&scratchMem.C[0],&scratchMem.Arows[0]);
  1687. int adj_nub = lcp.getNub();
  1688. // loop over all indexes adj_nub..n-1. for index i, if x(i),w(i) satisfy the
  1689. // LCP conditions then i is added to the appropriate index set. otherwise
  1690. // x(i),w(i) is driven either +ve or -ve to force it to the valid region.
  1691. // as we drive x(i), x(C) is also adjusted to keep w(C) at zero.
  1692. // while driving x(i) we maintain the LCP conditions on the other variables
  1693. // 0..i-1. we do this by watching out for other x(i),w(i) values going
  1694. // outside the valid region, and then switching them between index sets
  1695. // when that happens.
  1696. bool hit_first_friction_index = false;
  1697. for (int i=adj_nub; i<n; ++i)
  1698. {
  1699. s_error = false;
  1700. // the index i is the driving index and indexes i+1..n-1 are "dont care",
  1701. // i.e. when we make changes to the system those x's will be zero and we
  1702. // don't care what happens to those w's. in other words, we only consider
  1703. // an (i+1)*(i+1) sub-problem of A*x=b+w.
  1704. // if we've hit the first friction index, we have to compute the lo and
  1705. // hi values based on the values of x already computed. we have been
  1706. // permuting the indexes, so the values stored in the findex vector are
  1707. // no longer valid. thus we have to temporarily unpermute the x vector.
  1708. // for the purposes of this computation, 0*infinity = 0 ... so if the
  1709. // contact constraint's normal force is 0, there should be no tangential
  1710. // force applied.
  1711. if (!hit_first_friction_index && findex && findex[i] >= 0) {
  1712. // un-permute x into delta_w, which is not being used at the moment
  1713. for (int j=0; j<n; ++j) scratchMem.delta_w[scratchMem.p[j]] = x[j];
  1714. // set lo and hi values
  1715. for (int k=i; k<n; ++k) {
  1716. btScalar wfk = scratchMem.delta_w[findex[k]];
  1717. if (wfk == 0) {
  1718. hi[k] = 0;
  1719. lo[k] = 0;
  1720. }
  1721. else {
  1722. hi[k] = btFabs (hi[k] * wfk);
  1723. lo[k] = -hi[k];
  1724. }
  1725. }
  1726. hit_first_friction_index = true;
  1727. }
  1728. // thus far we have not even been computing the w values for indexes
  1729. // greater than i, so compute w[i] now.
  1730. w[i] = lcp.AiC_times_qC (i,x) + lcp.AiN_times_qN (i,x) - b[i];
  1731. // if lo=hi=0 (which can happen for tangential friction when normals are
  1732. // 0) then the index will be assigned to set N with some state. however,
  1733. // set C's line has zero size, so the index will always remain in set N.
  1734. // with the "normal" switching logic, if w changed sign then the index
  1735. // would have to switch to set C and then back to set N with an inverted
  1736. // state. this is pointless, and also computationally expensive. to
  1737. // prevent this from happening, we use the rule that indexes with lo=hi=0
  1738. // will never be checked for set changes. this means that the state for
  1739. // these indexes may be incorrect, but that doesn't matter.
  1740. // see if x(i),w(i) is in a valid region
  1741. if (lo[i]==0 && w[i] >= 0) {
  1742. lcp.transfer_i_to_N (i);
  1743. scratchMem.state[i] = false;
  1744. }
  1745. else if (hi[i]==0 && w[i] <= 0) {
  1746. lcp.transfer_i_to_N (i);
  1747. scratchMem.state[i] = true;
  1748. }
  1749. else if (w[i]==0) {
  1750. // this is a degenerate case. by the time we get to this test we know
  1751. // that lo != 0, which means that lo < 0 as lo is not allowed to be +ve,
  1752. // and similarly that hi > 0. this means that the line segment
  1753. // corresponding to set C is at least finite in extent, and we are on it.
  1754. // NOTE: we must call lcp.solve1() before lcp.transfer_i_to_C()
  1755. lcp.solve1 (&scratchMem.delta_x[0],i,0,1);
  1756. lcp.transfer_i_to_C (i);
  1757. }
  1758. else {
  1759. // we must push x(i) and w(i)
  1760. for (;;) {
  1761. int dir;
  1762. btScalar dirf;
  1763. // find direction to push on x(i)
  1764. if (w[i] <= 0) {
  1765. dir = 1;
  1766. dirf = btScalar(1.0);
  1767. }
  1768. else {
  1769. dir = -1;
  1770. dirf = btScalar(-1.0);
  1771. }
  1772. // compute: delta_x(C) = -dir*A(C,C)\A(C,i)
  1773. lcp.solve1 (&scratchMem.delta_x[0],i,dir);
  1774. // note that delta_x[i] = dirf, but we wont bother to set it
  1775. // compute: delta_w = A*delta_x ... note we only care about
  1776. // delta_w(N) and delta_w(i), the rest is ignored
  1777. lcp.pN_equals_ANC_times_qC (&scratchMem.delta_w[0],&scratchMem.delta_x[0]);
  1778. lcp.pN_plusequals_ANi (&scratchMem.delta_w[0],i,dir);
  1779. scratchMem.delta_w[i] = lcp.AiC_times_qC (i,&scratchMem.delta_x[0]) + lcp.Aii(i)*dirf;
  1780. // find largest step we can take (size=s), either to drive x(i),w(i)
  1781. // to the valid LCP region or to drive an already-valid variable
  1782. // outside the valid region.
  1783. int cmd = 1; // index switching command
  1784. int si = 0; // si = index to switch if cmd>3
  1785. btScalar s = -w[i]/scratchMem.delta_w[i];
  1786. if (dir > 0) {
  1787. if (hi[i] < BT_INFINITY) {
  1788. btScalar s2 = (hi[i]-x[i])*dirf; // was (hi[i]-x[i])/dirf // step to x(i)=hi(i)
  1789. if (s2 < s) {
  1790. s = s2;
  1791. cmd = 3;
  1792. }
  1793. }
  1794. }
  1795. else {
  1796. if (lo[i] > -BT_INFINITY) {
  1797. btScalar s2 = (lo[i]-x[i])*dirf; // was (lo[i]-x[i])/dirf // step to x(i)=lo(i)
  1798. if (s2 < s) {
  1799. s = s2;
  1800. cmd = 2;
  1801. }
  1802. }
  1803. }
  1804. {
  1805. const int numN = lcp.numN();
  1806. for (int k=0; k < numN; ++k) {
  1807. const int indexN_k = lcp.indexN(k);
  1808. if (!scratchMem.state[indexN_k] ? scratchMem.delta_w[indexN_k] < 0 : scratchMem.delta_w[indexN_k] > 0) {
  1809. // don't bother checking if lo=hi=0
  1810. if (lo[indexN_k] == 0 && hi[indexN_k] == 0) continue;
  1811. btScalar s2 = -w[indexN_k] / scratchMem.delta_w[indexN_k];
  1812. if (s2 < s) {
  1813. s = s2;
  1814. cmd = 4;
  1815. si = indexN_k;
  1816. }
  1817. }
  1818. }
  1819. }
  1820. {
  1821. const int numC = lcp.numC();
  1822. for (int k=adj_nub; k < numC; ++k) {
  1823. const int indexC_k = lcp.indexC(k);
  1824. if (scratchMem.delta_x[indexC_k] < 0 && lo[indexC_k] > -BT_INFINITY) {
  1825. btScalar s2 = (lo[indexC_k]-x[indexC_k]) / scratchMem.delta_x[indexC_k];
  1826. if (s2 < s) {
  1827. s = s2;
  1828. cmd = 5;
  1829. si = indexC_k;
  1830. }
  1831. }
  1832. if (scratchMem.delta_x[indexC_k] > 0 && hi[indexC_k] < BT_INFINITY) {
  1833. btScalar s2 = (hi[indexC_k]-x[indexC_k]) / scratchMem.delta_x[indexC_k];
  1834. if (s2 < s) {
  1835. s = s2;
  1836. cmd = 6;
  1837. si = indexC_k;
  1838. }
  1839. }
  1840. }
  1841. }
  1842. //static char* cmdstring[8] = {0,"->C","->NL","->NH","N->C",
  1843. // "C->NL","C->NH"};
  1844. //printf ("cmd=%d (%s), si=%d\n",cmd,cmdstring[cmd],(cmd>3) ? si : i);
  1845. // if s <= 0 then we've got a problem. if we just keep going then
  1846. // we're going to get stuck in an infinite loop. instead, just cross
  1847. // our fingers and exit with the current solution.
  1848. if (s <= btScalar(0.0))
  1849. {
  1850. // printf("LCP internal error, s <= 0 (s=%.4e)",(double)s);
  1851. if (i < n) {
  1852. btSetZero (x+i,n-i);
  1853. btSetZero (w+i,n-i);
  1854. }
  1855. s_error = true;
  1856. break;
  1857. }
  1858. // apply x = x + s * delta_x
  1859. lcp.pC_plusequals_s_times_qC (x, s, &scratchMem.delta_x[0]);
  1860. x[i] += s * dirf;
  1861. // apply w = w + s * delta_w
  1862. lcp.pN_plusequals_s_times_qN (w, s, &scratchMem.delta_w[0]);
  1863. w[i] += s * scratchMem.delta_w[i];
  1864. // void *tmpbuf;
  1865. // switch indexes between sets if necessary
  1866. switch (cmd) {
  1867. case 1: // done
  1868. w[i] = 0;
  1869. lcp.transfer_i_to_C (i);
  1870. break;
  1871. case 2: // done
  1872. x[i] = lo[i];
  1873. scratchMem.state[i] = false;
  1874. lcp.transfer_i_to_N (i);
  1875. break;
  1876. case 3: // done
  1877. x[i] = hi[i];
  1878. scratchMem.state[i] = true;
  1879. lcp.transfer_i_to_N (i);
  1880. break;
  1881. case 4: // keep going
  1882. w[si] = 0;
  1883. lcp.transfer_i_from_N_to_C (si);
  1884. break;
  1885. case 5: // keep going
  1886. x[si] = lo[si];
  1887. scratchMem.state[si] = false;
  1888. lcp.transfer_i_from_C_to_N (si, scratchMem.m_scratch);
  1889. break;
  1890. case 6: // keep going
  1891. x[si] = hi[si];
  1892. scratchMem.state[si] = true;
  1893. lcp.transfer_i_from_C_to_N (si, scratchMem.m_scratch);
  1894. break;
  1895. }
  1896. if (cmd <= 3) break;
  1897. } // for (;;)
  1898. } // else
  1899. if (s_error)
  1900. {
  1901. break;
  1902. }
  1903. } // for (int i=adj_nub; i<n; ++i)
  1904. lcp.unpermute();
  1905. return !s_error;
  1906. }