00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "rt_nonfinite.h"
00012 #include "Optimal_affine_tracking_3d16_fast_realtime.h"
00013 #include "svd.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 static void b_eml_xaxpy(int32_T n, real_T a, const real_T x[16], int32_T ix0,
00025 real_T y[4], int32_T iy0);
00026 static void b_eml_xscal(int32_T n, real_T a, real_T x[4], int32_T ix0);
00027 static void c_eml_xaxpy(int32_T n, real_T a, const real_T x[4], int32_T ix0,
00028 real_T y[16], int32_T iy0);
00029 static real_T c_eml_xnrm2(int32_T n, const real_T x[16], int32_T ix0);
00030 static real_T d_eml_xnrm2(int32_T n, const real_T x[4], int32_T ix0);
00031 static real_T eml_div(real_T x, real_T y);
00032 static void eml_xaxpy(int32_T n, real_T a, int32_T ix0, real_T y[16], int32_T
00033 iy0);
00034 static real_T eml_xdotc(int32_T n, const real_T x[16], int32_T ix0, const real_T
00035 y[16], int32_T iy0);
00036 static void eml_xgesvd(const real_T A[16], real_T U[16], real_T S[4], real_T V
00037 [16]);
00038 static void eml_xrot(real_T x[16], int32_T ix0, int32_T iy0, real_T c, real_T s);
00039 static void eml_xrotg(real_T *a, real_T *b, real_T *c, real_T *s);
00040 static void eml_xscal(int32_T n, real_T a, real_T x[16], int32_T ix0);
00041 static void eml_xswap(real_T x[16], int32_T ix0, int32_T iy0);
00042
00043
00044 static void b_eml_xaxpy(int32_T n, real_T a, const real_T x[16], int32_T ix0,
00045 real_T y[4], int32_T iy0)
00046 {
00047 int32_T ix;
00048 int32_T iy;
00049 int32_T k;
00050 if (a == 0.0) {
00051 } else {
00052 ix = ix0 - 1;
00053 iy = iy0 - 1;
00054 for (k = 0; k <= n - 1; k++) {
00055 y[iy] += a * x[ix];
00056 ix++;
00057 iy++;
00058 }
00059 }
00060 }
00061
00062 static void b_eml_xscal(int32_T n, real_T a, real_T x[4], int32_T ix0)
00063 {
00064 int32_T i28;
00065 int32_T k;
00066 i28 = (ix0 + n) - 1;
00067 for (k = ix0; k <= i28; k++) {
00068 x[k - 1] *= a;
00069 }
00070 }
00071
00072 static void c_eml_xaxpy(int32_T n, real_T a, const real_T x[4], int32_T ix0,
00073 real_T y[16], int32_T iy0)
00074 {
00075 int32_T ix;
00076 int32_T iy;
00077 int32_T k;
00078 if (a == 0.0) {
00079 } else {
00080 ix = ix0 - 1;
00081 iy = iy0 - 1;
00082 for (k = 0; k <= n - 1; k++) {
00083 y[iy] += a * x[ix];
00084 ix++;
00085 iy++;
00086 }
00087 }
00088 }
00089
00090 static real_T c_eml_xnrm2(int32_T n, const real_T x[16], int32_T ix0)
00091 {
00092 real_T y;
00093 real_T scale;
00094 int32_T kend;
00095 int32_T k;
00096 real_T absxk;
00097 real_T t;
00098 y = 0.0;
00099 scale = 2.2250738585072014E-308;
00100 kend = (ix0 + n) - 1;
00101 for (k = ix0; k <= kend; k++) {
00102 absxk = fabs(x[k - 1]);
00103 if (absxk > scale) {
00104 t = scale / absxk;
00105 y = 1.0 + y * t * t;
00106 scale = absxk;
00107 } else {
00108 t = absxk / scale;
00109 y += t * t;
00110 }
00111 }
00112
00113 return scale * sqrt(y);
00114 }
00115
00116 static real_T d_eml_xnrm2(int32_T n, const real_T x[4], int32_T ix0)
00117 {
00118 real_T y;
00119 real_T scale;
00120 int32_T kend;
00121 int32_T k;
00122 real_T absxk;
00123 real_T t;
00124 y = 0.0;
00125 scale = 2.2250738585072014E-308;
00126 kend = (ix0 + n) - 1;
00127 for (k = ix0; k <= kend; k++) {
00128 absxk = fabs(x[k - 1]);
00129 if (absxk > scale) {
00130 t = scale / absxk;
00131 y = 1.0 + y * t * t;
00132 scale = absxk;
00133 } else {
00134 t = absxk / scale;
00135 y += t * t;
00136 }
00137 }
00138
00139 return scale * sqrt(y);
00140 }
00141
00142 static real_T eml_div(real_T x, real_T y)
00143 {
00144 return x / y;
00145 }
00146
00147 static void eml_xaxpy(int32_T n, real_T a, int32_T ix0, real_T y[16], int32_T
00148 iy0)
00149 {
00150 int32_T ix;
00151 int32_T iy;
00152 int32_T k;
00153 if ((n < 1) || (a == 0.0)) {
00154 } else {
00155 ix = ix0 - 1;
00156 iy = iy0 - 1;
00157 for (k = 0; k <= n - 1; k++) {
00158 y[iy] += a * y[ix];
00159 ix++;
00160 iy++;
00161 }
00162 }
00163 }
00164
00165 static real_T eml_xdotc(int32_T n, const real_T x[16], int32_T ix0, const real_T
00166 y[16], int32_T iy0)
00167 {
00168 real_T d;
00169 int32_T ix;
00170 int32_T iy;
00171 int32_T k;
00172 d = 0.0;
00173 if (n < 1) {
00174 } else {
00175 ix = ix0;
00176 iy = iy0;
00177 for (k = 1; k <= n; k++) {
00178 d += x[ix - 1] * y[iy - 1];
00179 ix++;
00180 iy++;
00181 }
00182 }
00183
00184 return d;
00185 }
00186
00187 static void eml_xgesvd(const real_T A[16], real_T U[16], real_T S[4], real_T V
00188 [16])
00189 {
00190 real_T b_A[16];
00191 real_T s[4];
00192 real_T e[4];
00193 real_T work[4];
00194 int32_T i;
00195 int32_T q;
00196 int32_T qp1;
00197 int32_T mm1;
00198 real_T ztest0;
00199 int32_T iter;
00200 int32_T qs;
00201 int32_T m;
00202 real_T rt;
00203 real_T ztest;
00204 real_T tiny;
00205 real_T snorm;
00206 boolean_T exitg3;
00207 boolean_T exitg2;
00208 real_T sn;
00209 real_T varargin_1[5];
00210 boolean_T exitg1;
00211 real_T sqds;
00212 real_T b;
00213 memcpy(&b_A[0], &A[0], sizeof(real_T) << 4);
00214 for (i = 0; i < 4; i++) {
00215 s[i] = 0.0;
00216 e[i] = 0.0;
00217 work[i] = 0.0;
00218 }
00219
00220 for (i = 0; i < 16; i++) {
00221 U[i] = 0.0;
00222 V[i] = 0.0;
00223 }
00224
00225 for (q = 0; q < 3; q++) {
00226 qp1 = q + 2;
00227 mm1 = q + (q << 2);
00228 i = 4 - q;
00229 ztest0 = c_eml_xnrm2(i, b_A, mm1 + 1);
00230 if (ztest0 > 0.0) {
00231 if (b_A[mm1] < 0.0) {
00232 ztest0 = -ztest0;
00233 }
00234
00235 s[q] = ztest0;
00236 eml_xscal(i, eml_div(1.0, s[q]), b_A, mm1 + 1);
00237 b_A[mm1]++;
00238 s[q] = -s[q];
00239 } else {
00240 s[q] = 0.0;
00241 }
00242
00243 for (iter = qp1; iter < 5; iter++) {
00244 qs = q + ((iter - 1) << 2);
00245 if (s[q] != 0.0) {
00246 eml_xaxpy(i, -eml_div(eml_xdotc(i, b_A, mm1 + 1, b_A, qs + 1), b_A[q +
00247 (q << 2)]), mm1 + 1, b_A, qs + 1);
00248 }
00249
00250 e[iter - 1] = b_A[qs];
00251 }
00252
00253 for (i = q; i + 1 < 5; i++) {
00254 U[i + (q << 2)] = b_A[i + (q << 2)];
00255 }
00256
00257 if (q + 1 <= 2) {
00258 ztest0 = d_eml_xnrm2(3 - q, e, qp1);
00259 if (ztest0 == 0.0) {
00260 e[q] = 0.0;
00261 } else {
00262 if (e[qp1 - 1] < 0.0) {
00263 ztest0 = -ztest0;
00264 }
00265
00266 e[q] = ztest0;
00267 b_eml_xscal(3 - q, eml_div(1.0, e[q]), e, qp1);
00268 e[qp1 - 1]++;
00269 }
00270
00271 e[q] = -e[q];
00272 if (e[q] != 0.0) {
00273 for (i = qp1; i < 5; i++) {
00274 work[i - 1] = 0.0;
00275 }
00276
00277 for (iter = qp1; iter < 5; iter++) {
00278 b_eml_xaxpy(3 - q, e[iter - 1], b_A, qp1 + ((iter - 1) << 2), work,
00279 qp1);
00280 }
00281
00282 for (iter = qp1; iter < 5; iter++) {
00283 c_eml_xaxpy(3 - q, eml_div(-e[iter - 1], e[qp1 - 1]), work, qp1, b_A,
00284 qp1 + ((iter - 1) << 2));
00285 }
00286 }
00287
00288 while (qp1 < 5) {
00289 V[(qp1 + (q << 2)) - 1] = e[qp1 - 1];
00290 qp1++;
00291 }
00292 }
00293 }
00294
00295 m = 4;
00296 s[3] = b_A[15];
00297 e[2] = b_A[14];
00298 e[3] = 0.0;
00299 for (i = 0; i < 4; i++) {
00300 U[12 + i] = 0.0;
00301 }
00302
00303 U[15] = 1.0;
00304 for (q = 2; q > -1; q += -1) {
00305 mm1 = q + (q << 2);
00306 if (s[q] != 0.0) {
00307 for (iter = q + 1; iter + 1 < 5; iter++) {
00308 qs = (q + (iter << 2)) + 1;
00309 eml_xaxpy(4 - q, -eml_div(eml_xdotc(4 - q, U, mm1 + 1, U, qs), U[mm1]),
00310 mm1 + 1, U, qs);
00311 }
00312
00313 for (i = q; i + 1 < 5; i++) {
00314 U[i + (q << 2)] = -U[i + (q << 2)];
00315 }
00316
00317 U[mm1]++;
00318 for (i = 1; i <= q; i++) {
00319 U[(i + (q << 2)) - 1] = 0.0;
00320 }
00321 } else {
00322 for (i = 0; i < 4; i++) {
00323 U[i + (q << 2)] = 0.0;
00324 }
00325
00326 U[mm1] = 1.0;
00327 }
00328 }
00329
00330 for (q = 3; q > -1; q += -1) {
00331 if ((q + 1 <= 2) && (e[q] != 0.0)) {
00332 qp1 = q + 2;
00333 i = qp1 + (q << 2);
00334 for (iter = qp1; iter < 5; iter++) {
00335 qs = qp1 + ((iter - 1) << 2);
00336 eml_xaxpy(3 - q, -eml_div(eml_xdotc(3 - q, V, i, V, qs), V[i - 1]), i, V,
00337 qs);
00338 }
00339 }
00340
00341 for (i = 0; i < 4; i++) {
00342 V[i + (q << 2)] = 0.0;
00343 }
00344
00345 V[q + (q << 2)] = 1.0;
00346 }
00347
00348 for (q = 0; q < 4; q++) {
00349 ztest0 = e[q];
00350 if (s[q] != 0.0) {
00351 rt = fabs(s[q]);
00352 ztest = eml_div(s[q], rt);
00353 s[q] = rt;
00354 if (q + 1 < 4) {
00355 ztest0 = eml_div(e[q], ztest);
00356 }
00357
00358 eml_xscal(4, ztest, U, (q << 2) + 1);
00359 }
00360
00361 if ((q + 1 < 4) && (ztest0 != 0.0)) {
00362 rt = fabs(ztest0);
00363 ztest = eml_div(rt, ztest0);
00364 ztest0 = rt;
00365 s[q + 1] *= ztest;
00366 eml_xscal(4, ztest, V, ((q + 1) << 2) + 1);
00367 }
00368
00369 e[q] = ztest0;
00370 }
00371
00372 iter = 0;
00373 tiny = eml_div(2.2250738585072014E-308, 2.2204460492503131E-16);
00374 snorm = 0.0;
00375 for (i = 0; i < 4; i++) {
00376 ztest0 = fabs(s[i]);
00377 ztest = fabs(e[i]);
00378 if ((ztest0 >= ztest) || rtIsNaN(ztest)) {
00379 ztest = ztest0;
00380 }
00381
00382 if ((snorm >= ztest) || rtIsNaN(ztest)) {
00383 } else {
00384 snorm = ztest;
00385 }
00386 }
00387
00388 while ((m > 0) && (!(iter >= 75))) {
00389 q = m - 1;
00390 exitg3 = FALSE;
00391 while (!((exitg3 == 1U) || (q == 0))) {
00392 ztest0 = fabs(e[q - 1]);
00393 if ((ztest0 <= 2.2204460492503131E-16 * (fabs(s[q - 1]) + fabs(s[q]))) ||
00394 (ztest0 <= tiny) || ((iter > 20) && (ztest0 <= 2.2204460492503131E-16 *
00395 snorm))) {
00396 e[q - 1] = 0.0;
00397 exitg3 = TRUE;
00398 } else {
00399 q--;
00400 }
00401 }
00402
00403 if (q == m - 1) {
00404 i = 4;
00405 } else {
00406 qs = m;
00407 i = m;
00408 exitg2 = FALSE;
00409 while ((exitg2 == 0U) && (i >= q)) {
00410 qs = i;
00411 if (i == q) {
00412 exitg2 = TRUE;
00413 } else {
00414 ztest0 = 0.0;
00415 if (i < m) {
00416 ztest0 = fabs(e[i - 1]);
00417 }
00418
00419 if (i > q + 1) {
00420 ztest0 += fabs(e[i - 2]);
00421 }
00422
00423 ztest = fabs(s[i - 1]);
00424 if ((ztest <= 2.2204460492503131E-16 * ztest0) || (ztest <= tiny)) {
00425 s[i - 1] = 0.0;
00426 exitg2 = TRUE;
00427 } else {
00428 i--;
00429 }
00430 }
00431 }
00432
00433 if (qs == q) {
00434 i = 3;
00435 } else if (qs == m) {
00436 i = 1;
00437 } else {
00438 i = 2;
00439 q = qs;
00440 }
00441 }
00442
00443 switch (i) {
00444 case 1:
00445 ztest = e[m - 2];
00446 e[m - 2] = 0.0;
00447 for (qs = m - 2; qs + 1 >= q + 1; qs--) {
00448 ztest0 = s[qs];
00449 eml_xrotg(&ztest0, &ztest, &rt, &sn);
00450 s[qs] = ztest0;
00451 if (qs + 1 > q + 1) {
00452 i = qs - 1;
00453 ztest = -sn * e[i];
00454 e[i] *= rt;
00455 }
00456
00457 eml_xrot(V, (qs << 2) + 1, ((m - 1) << 2) + 1, rt, sn);
00458 }
00459 break;
00460
00461 case 2:
00462 i = q - 1;
00463 ztest = e[i];
00464 e[i] = 0.0;
00465 while (q + 1 <= m) {
00466 eml_xrotg(&s[q], &ztest, &rt, &sn);
00467 ztest = -sn * e[q];
00468 e[q] *= rt;
00469 eml_xrot(U, (q << 2) + 1, (i << 2) + 1, rt, sn);
00470 q++;
00471 }
00472 break;
00473
00474 case 3:
00475 mm1 = m - 2;
00476 varargin_1[0] = fabs(s[m - 1]);
00477 varargin_1[1] = fabs(s[mm1]);
00478 varargin_1[2] = fabs(e[mm1]);
00479 varargin_1[3] = fabs(s[q]);
00480 varargin_1[4] = fabs(e[q]);
00481 i = 1;
00482 sn = varargin_1[0];
00483 if (rtIsNaN(varargin_1[0])) {
00484 qs = 2;
00485 exitg1 = FALSE;
00486 while ((exitg1 == 0U) && (qs < 6)) {
00487 i = qs;
00488 if (!rtIsNaN(varargin_1[qs - 1])) {
00489 sn = varargin_1[qs - 1];
00490 exitg1 = TRUE;
00491 } else {
00492 qs++;
00493 }
00494 }
00495 }
00496
00497 if (i < 5) {
00498 while (i + 1 < 6) {
00499 if (varargin_1[i] > sn) {
00500 sn = varargin_1[i];
00501 }
00502
00503 i++;
00504 }
00505 }
00506
00507 rt = eml_div(s[m - 1], sn);
00508 ztest0 = eml_div(s[mm1], sn);
00509 ztest = eml_div(e[mm1], sn);
00510 sqds = eml_div(s[q], sn);
00511 b = eml_div((ztest0 + rt) * (ztest0 - rt) + ztest * ztest, 2.0);
00512 ztest0 = rt * ztest;
00513 ztest0 *= ztest0;
00514 ztest = 0.0;
00515 if ((b != 0.0) || (ztest0 != 0.0)) {
00516 ztest = sqrt(b * b + ztest0);
00517 if (b < 0.0) {
00518 ztest = -ztest;
00519 }
00520
00521 ztest = eml_div(ztest0, b + ztest);
00522 }
00523
00524 ztest += (sqds + rt) * (sqds - rt);
00525 ztest0 = sqds * eml_div(e[q], sn);
00526 for (qs = q; qs + 1 <= mm1 + 1; qs++) {
00527 i = qs + 1;
00528 eml_xrotg(&ztest, &ztest0, &rt, &sn);
00529 if (qs + 1 > q + 1) {
00530 e[qs - 1] = ztest;
00531 }
00532
00533 ztest0 = rt * s[qs];
00534 ztest = sn * e[qs];
00535 e[qs] = rt * e[qs] - sn * s[qs];
00536 b = s[i];
00537 s[i] *= rt;
00538 eml_xrot(V, (qs << 2) + 1, ((qs + 1) << 2) + 1, rt, sn);
00539 s[qs] = ztest0 + ztest;
00540 ztest0 = sn * b;
00541 eml_xrotg(&s[qs], &ztest0, &rt, &sn);
00542 ztest = rt * e[qs] + sn * s[i];
00543 s[i] = -sn * e[qs] + rt * s[i];
00544 ztest0 = sn * e[i];
00545 e[i] *= rt;
00546 eml_xrot(U, (qs << 2) + 1, ((qs + 1) << 2) + 1, rt, sn);
00547 }
00548
00549 e[m - 2] = ztest;
00550 iter++;
00551 break;
00552
00553 default:
00554 if (s[q] < 0.0) {
00555 s[q] = -s[q];
00556 eml_xscal(4, -1.0, V, (q << 2) + 1);
00557 }
00558
00559 qp1 = q + 1;
00560 while ((q + 1 < 4) && (s[q] < s[qp1])) {
00561 rt = s[q];
00562 s[q] = s[qp1];
00563 s[qp1] = rt;
00564 eml_xswap(V, (q << 2) + 1, ((q + 1) << 2) + 1);
00565 eml_xswap(U, (q << 2) + 1, ((q + 1) << 2) + 1);
00566 q = qp1;
00567 qp1++;
00568 }
00569
00570 iter = 0;
00571 m--;
00572 break;
00573 }
00574 }
00575
00576 for (qs = 0; qs < 4; qs++) {
00577 S[qs] = s[qs];
00578 }
00579 }
00580
00581 static void eml_xrot(real_T x[16], int32_T ix0, int32_T iy0, real_T c, real_T s)
00582 {
00583 int32_T ix;
00584 int32_T iy;
00585 int32_T k;
00586 real_T y;
00587 real_T b_y;
00588 ix = ix0 - 1;
00589 iy = iy0 - 1;
00590 for (k = 0; k < 4; k++) {
00591 y = c * x[ix];
00592 b_y = s * x[iy];
00593 x[iy] = c * x[iy] - s * x[ix];
00594 x[ix] = y + b_y;
00595 iy++;
00596 ix++;
00597 }
00598 }
00599
00600 static void eml_xrotg(real_T *a, real_T *b, real_T *c, real_T *s)
00601 {
00602 real_T roe;
00603 real_T absa;
00604 real_T absb;
00605 real_T scale;
00606 real_T ads;
00607 real_T bds;
00608 roe = *b;
00609 absa = fabs(*a);
00610 absb = fabs(*b);
00611 if (absa > absb) {
00612 roe = *a;
00613 }
00614
00615 scale = absa + absb;
00616 if (scale == 0.0) {
00617 *s = 0.0;
00618 *c = 1.0;
00619 ads = 0.0;
00620 scale = 0.0;
00621 } else {
00622 ads = absa / scale;
00623 bds = absb / scale;
00624 ads = scale * sqrt(ads * ads + bds * bds);
00625 if (roe < 0.0) {
00626 ads = -ads;
00627 }
00628
00629 *c = *a / ads;
00630 *s = *b / ads;
00631 if (absa > absb) {
00632 scale = *s;
00633 } else if (*c != 0.0) {
00634 scale = 1.0 / *c;
00635 } else {
00636 scale = 1.0;
00637 }
00638 }
00639
00640 *a = ads;
00641 *b = scale;
00642 }
00643
00644 static void eml_xscal(int32_T n, real_T a, real_T x[16], int32_T ix0)
00645 {
00646 int32_T i27;
00647 int32_T k;
00648 i27 = (ix0 + n) - 1;
00649 for (k = ix0; k <= i27; k++) {
00650 x[k - 1] *= a;
00651 }
00652 }
00653
00654 static void eml_xswap(real_T x[16], int32_T ix0, int32_T iy0)
00655 {
00656 int32_T ix;
00657 int32_T iy;
00658 int32_T k;
00659 real_T temp;
00660 ix = ix0 - 1;
00661 iy = iy0 - 1;
00662 for (k = 0; k < 4; k++) {
00663 temp = x[ix];
00664 x[ix] = x[iy];
00665 x[iy] = temp;
00666 ix++;
00667 iy++;
00668 }
00669 }
00670
00671 void svd(const real_T A[16], real_T U[16], real_T S[16], real_T V[16])
00672 {
00673 real_T s[4];
00674 int32_T k;
00675 eml_xgesvd(A, U, s, V);
00676 memset(&S[0], 0, sizeof(real_T) << 4);
00677 for (k = 0; k < 4; k++) {
00678 S[k + (k << 2)] = s[k];
00679 }
00680 }
00681
00682