btDantzigLCP.cpp 56 KB

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