39 #include <visp3/tt_mi/vpTemplateTrackerMIBSpline.h>
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 void vpTemplateTrackerMIBSpline::PutPVBsplineD(
double *Prt,
int cr,
double er,
int ct,
double et,
int Nc,
double val,
48 PutPVBsplineD4(Prt, cr, er, ct, et, Nc, val);
51 PutPVBsplineD3(Prt, cr, er, ct, et, Nc, val);
55 void vpTemplateTrackerMIBSpline::PutPVBsplineD3(
double *Prt,
int cr,
double er,
int ct,
double et,
int Nc,
double val)
67 double *pt = &Prt[((cr + sr) * Nc + (ct + st)) * 9];
68 if (std::fabs(val - 1.) > std::numeric_limits<double>::epsilon()) {
69 for (
int ir = -1; ir <= 1; ir++) {
70 double Bspline3_diff_r_ = Bspline3(ir - er);
71 for (
int it = -1; it <= 1; it++) {
72 *pt++ += Bspline3_diff_r_ * Bspline3(it - et) * val;
77 for (
int ir = -1; ir <= 1; ir++) {
78 double Bspline3_diff_r_ = Bspline3(ir - er);
79 for (
int it = -1; it <= 1; it++) {
80 *pt++ += Bspline3_diff_r_ * Bspline3(it - et);
86 void vpTemplateTrackerMIBSpline::PutPVBsplineD4(
double *Prt,
int cr,
double er,
int ct,
double et,
int Nc,
double val)
90 double *ptBti = &Bti[0];
91 for (
int it = -1; it <= 2; it++) {
92 *ptBti++ = Bspline4i(it - et, it);
95 double *pt = &Prt[(cr * Nc + ct) * 16];
96 if (std::fabs(val - 1.) > std::numeric_limits<double>::epsilon()) {
97 for (
int ir = -1; ir <= 2; ir++) {
98 double Br = Bspline4i(ir - er, ir);
100 for (
int it = -1; it <= 2; it++) {
101 *pt++ += Br * (*ptBti++) * val;
106 for (
int ir = -1; ir <= 2; ir++) {
107 double Br = Bspline4i(ir - er, ir);
109 for (
int it = -1; it <= 2; it++) {
110 *pt++ += Br * (*ptBti++);
116 double vpTemplateTrackerMIBSpline::Bspline3(
double diff)
118 double aDiff = std::fabs(diff);
122 return (-(aDiff * aDiff) + 0.75);
124 double tmp_ = 1.5 - aDiff;
125 return (0.5 * tmp_ * tmp_);
131 double vpTemplateTrackerMIBSpline::Bspline4i(
double diff,
int &interv)
136 double tmp_ = 2. + diff;
137 return (tmp_ * tmp_ * tmp_ / 6.);
140 double diff2_ = diff * diff;
141 return (-diff2_ * diff / 2. - diff2_ + 4. / 6.);
144 double diff2_ = diff * diff;
145 return (diff2_ * diff / 2. - diff2_ + 4. / 6.);
152 double vpTemplateTrackerMIBSpline::dBspline3(
double diff)
154 if ((diff > -1.5) && (diff <= -0.5))
156 else if ((diff > -0.5) && (diff <= 0.5))
158 else if ((diff > 0.5) && (diff <= 1.5))
164 double vpTemplateTrackerMIBSpline::dBspline4(
double diff)
166 if ((diff > -2.) && (diff <= -1.)) {
167 double diff_2_ = diff + 2.;
168 return (diff_2_ * diff_2_ * 0.5);
170 else if ((diff > -1.) && (diff <= 0.)) {
171 return -1.5 * diff * diff - 2. * diff;
173 else if ((diff > 0.) && (diff <= 1.)) {
174 return 1.5 * diff * diff - 2. * diff;
176 else if ((diff > 1.) && (diff <= 2.)) {
177 double diff_2_ = diff - 2.;
178 return (-0.5 * diff_2_ * diff_2_);
185 double vpTemplateTrackerMIBSpline::d2Bspline3(
double diff)
187 if ((diff > -1.5) && (diff <= -0.5))
189 else if ((diff > -0.5) && (diff <= 0.5))
191 else if ((diff > 0.5) && (diff <= 1.5))
197 double vpTemplateTrackerMIBSpline::d2Bspline4(
double diff)
199 if ((diff > -2.) && (diff <= -1.))
201 else if ((diff > -1.) && (diff <= 0.))
202 return -3. * diff - 2.;
203 else if ((diff > 0.) && (diff <= 1.))
204 return 3. * diff - 2.;
205 else if ((diff > 1.) && (diff <= 2.))
211 void vpTemplateTrackerMIBSpline::PutTotPVBspline(
double *Prt,
int cr,
double &er,
int ct,
double &et,
int Nc,
212 double *val,
unsigned int &NbParam,
int °ree)
216 PutTotPVBspline4(Prt, cr, er, ct, et, Nc, val, NbParam);
219 PutTotPVBspline3(Prt, cr, er, ct, et, Nc, val, NbParam);
223 void vpTemplateTrackerMIBSpline::PutTotPVBspline(
double *Prt,
double *dPrt,
double *d2Prt,
int cr,
double &er,
int ct,
224 double &et,
int Ncb,
double *val,
unsigned int &NbParam,
int °ree)
228 PutTotPVBspline4(Prt, dPrt, d2Prt, cr, er, ct, et, Ncb, val, NbParam);
231 PutTotPVBspline3(Prt, dPrt, d2Prt, cr, er, ct, et, Ncb, val, NbParam);
235 void vpTemplateTrackerMIBSpline::PutTotPVBspline3(
double *Prt,
int cr,
double &er,
int ct,
double &et,
int Nc,
236 double *val,
unsigned int &NbParam)
253 double *ptBti = &Bti[0];
254 double *ptdBti = &dBti[0];
255 double *ptd2Bti = &d2Bti[0];
257 for (
short int it = 1; it >= -1; it--) {
258 *ptBti++ = Bspline3(it + et);
259 *ptdBti++ = dBspline3(it + et);
260 *ptd2Bti++ = d2Bspline3(it + et);
263 double *pt = &Prt[((cr + sr) * Nc + (ct + st)) * 9 * (1 + (
int)(NbParam + NbParam * NbParam))];
264 for (
short int ir = -1; ir <= 1; ++ir) {
265 double Br = Bspline3(-ir + er);
267 for (
short unsigned int it = 0; it <= 2; ++it) {
268 *pt++ += Br * (Bti[it]);
270 double v1 = Br * (dBti[it]);
271 for (
short unsigned int ip = 0; ip < NbParam; ++ip) {
272 *pt++ -= v1 * val[ip];
273 double v2 = Br * (d2Bti[it]) * val[ip];
274 for (
short unsigned int ip2 = 0; ip2 < NbParam; ++ip2)
275 *pt++ += v2 * val[ip2];
281 void vpTemplateTrackerMIBSpline::PutTotPVBspline3(
double *Prt,
double *dPrt,
double *d2Prt,
int cr,
double &er,
int ct,
282 double &et,
int Ncb,
double *val,
unsigned int &NbParam)
299 double *ptBti = &Bti[0];
300 double *ptdBti = &dBti[0];
301 double *ptd2Bti = &d2Bti[0];
303 for (
short int it = 1; it >= -1; it--) {
304 *ptBti++ = Bspline3(it + et);
305 *ptdBti++ = dBspline3(it + et);
306 *ptd2Bti++ = d2Bspline3(it + et);
309 int NbParam_ = (int)NbParam;
310 for (
short int ir = -1; ir <= 1; ++ir) {
311 double Br = Bspline3(-ir + er);
312 short int irInd = ir + 1;
313 short int ind = (cr + sr + irInd) * Ncb;
314 for (
short int it = 0; it <= 2; ++it) {
315 Prt[ind + (ct + st + it)] += Br * (Bti[it]);
317 double v1 = Br * (dBti[it]);
318 int ind1 = ((cr + sr + irInd) * Ncb + (ct + st + it)) * NbParam_;
319 for (
int ip = 0; ip < NbParam_; ++ip) {
320 dPrt[ind1 + ip] -= v1 * val[ip];
321 double v2 = Br * (d2Bti[it]) * val[ip];
322 int ind2 = ((cr + sr + irInd) * Ncb + (ct + st + it)) * NbParam_ * NbParam_ + ip * NbParam_;
323 for (
short int ip2 = 0; ip2 < NbParam_; ++ip2)
324 d2Prt[ind2 + ip2] += v2 * val[ip2];
330 void vpTemplateTrackerMIBSpline::PutTotPVBspline3(
double *Prt,
double &er,
double *bt,
unsigned int size)
334 double *bt0 = &bt[0];
339 for (
int ir = -1; ir <= 1; ++ir) {
340 double Br = Bspline3(-ir + er);
341 const double *btend = bt0 + size;
346 for (; bt < btend; bt += LSIZE) {
347 *Prt++ += Br * bt[0];
348 *Prt++ += Br * bt[1];
349 *Prt++ += Br * bt[2];
350 *Prt++ += Br * bt[3];
351 *Prt++ += Br * bt[4];
352 *Prt++ += Br * bt[5];
353 *Prt++ += Br * bt[6];
354 *Prt++ += Br * bt[7];
355 *Prt++ += Br * bt[8];
356 *Prt++ += Br * bt[9];
357 *Prt++ += Br * bt[10];
358 *Prt++ += Br * bt[11];
362 for (; bt < btend; *Prt++ += Br * *bt++) {
368 void vpTemplateTrackerMIBSpline::PutTotPVBspline4(
double *Prt,
int cr,
double er,
int ct,
double et,
int Nc,
369 double *val,
unsigned int &NbParam)
375 double *ptBti = &Bti[0];
376 double *ptdBti = &dBti[0];
377 double *ptd2Bti = &d2Bti[0];
378 for (
char it = -1; it <= 2; it++) {
379 *ptBti++ = vpTemplateTrackerBSpline::Bspline4(-it + et);
380 *ptdBti++ = dBspline4(-it + et);
381 *ptd2Bti++ = d2Bspline4(-it + et);
384 int NbParam_ = (int)NbParam;
386 double *pt = &Prt[(cr * Nc + ct) * 16 * (1 + NbParam_ + NbParam_ * NbParam_)];
387 for (
char ir = -1; ir <= 2; ir++) {
388 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
392 for (
char it = -1; it <= 2; it++) {
393 double Br_ptBti_ = Br * (*ptBti);
394 double Br_ptdBti_ = Br * (*ptdBti);
395 double Br_ptd2Bti_ = Br * (*ptd2Bti);
397 for (
short int ip = 0; ip < NbParam_; ip++) {
398 *pt++ -= Br_ptdBti_ * val[ip];
399 for (
short int ip2 = 0; ip2 < NbParam_; ip2++) {
400 *pt++ += Br_ptd2Bti_ * val[ip] * val[ip2];
410 void vpTemplateTrackerMIBSpline::PutTotPVBspline4(
double *Prt,
double *dPrt,
double *d2Prt,
int cr,
double er,
int ct,
411 double et,
int Ncb,
double *val,
unsigned int &NbParam)
417 for (
size_t i = 0; i < 4; ++i) {
423 double *ptBti = &Bti[0];
424 double *ptdBti = &dBti[0];
425 double *ptd2Bti = &d2Bti[0];
426 for (
char it = -1; it <= 2; it++) {
427 *ptBti++ = vpTemplateTrackerBSpline::Bspline4(-it + et);
428 *ptdBti++ = dBspline4(-it + et);
429 *ptd2Bti++ = d2Bspline4(-it + et);
432 int NbParam_ = (int)NbParam;
434 for (
int ir = -1; ir <= 2; ir++) {
435 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
437 int ind = (cr + irInd) * Ncb + ct;
442 for (
int it = -1; it <= 2; it++) {
443 Prt[ind + it] += Br * *ptBti;
444 int ind1 = ((cr + irInd) * Ncb + (ct + it)) * NbParam_;
445 int ind2 = ind1 * NbParam_;
446 double Br_ptdBti_ = Br * (*ptdBti);
447 double Br_ptd2Bti_ = Br * (*ptd2Bti);
448 for (
int ip = 0; ip < NbParam_; ip++) {
449 dPrt[ind1 + ip] -= Br_ptdBti_ * val[ip];
450 int ind3 = ind2 + ip * NbParam_;
451 for (
int ip2 = 0; ip2 < NbParam_; ip2++)
452 d2Prt[ind3 + ip2] += Br_ptd2Bti_ * val[ip] * val[ip2];
461 void vpTemplateTrackerMIBSpline::PutTotPVBspline4(
double *Prt,
double &er,
double *bt,
unsigned int size)
464 double *bt0 = &bt[0];
466 for (
int ir = -1; ir <= 2; ++ir) {
467 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
468 const double *btend = bt0 + size;
473 for (; bt < btend; bt += LSIZE) {
474 *Prt++ += Br * bt[0];
475 *Prt++ += Br * bt[1];
476 *Prt++ += Br * bt[2];
477 *Prt++ += Br * bt[3];
478 *Prt++ += Br * bt[4];
479 *Prt++ += Br * bt[5];
480 *Prt++ += Br * bt[6];
481 *Prt++ += Br * bt[7];
482 *Prt++ += Br * bt[8];
483 *Prt++ += Br * bt[9];
484 *Prt++ += Br * bt[10];
485 *Prt++ += Br * bt[11];
489 for (; bt < btend; *Prt++ += Br * *bt++) {
495 void vpTemplateTrackerMIBSpline::PutTotPVBsplineNoSecond(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
int &Nc,
496 double *val,
unsigned int &NbParam,
int °ree)
500 PutTotPVBspline4NoSecond(Prt, cr, er, ct, et, Nc, val, NbParam);
503 PutTotPVBspline3NoSecond(Prt, cr, er, ct, et, Nc, val, NbParam);
507 void vpTemplateTrackerMIBSpline::PutTotPVBsplineNoSecond(
double *Prt,
double *dPrt,
int &cr,
double &er,
int &ct,
508 double &et,
int &Ncb,
double *val,
unsigned int &NbParam,
513 PutTotPVBspline4NoSecond(Prt, dPrt, cr, er, ct, et, Ncb, val, NbParam);
516 PutTotPVBspline3NoSecond(Prt, dPrt, cr, er, ct, et, Ncb, val, NbParam);
520 void vpTemplateTrackerMIBSpline::PutTotPVBspline3NoSecond(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
521 int &Nc,
double *val,
unsigned int &NbParam)
537 double *ptBti = &Bti[0];
538 double *ptdBti = &dBti[0];
539 for (
char it = -1; it <= 1; it++) {
540 *ptBti++ = Bspline3(-it + et);
541 *ptdBti++ = dBspline3(-it + et);
544 int NbParam_ = (int)NbParam;
546 double *pt = &Prt[((cr + sr) * Nc + (ct + st)) * 9 * (1 + NbParam_ + NbParam_ * NbParam_)];
547 for (
char ir = -1; ir <= 1; ir++) {
548 double Br = Bspline3(-ir + er);
551 for (
char it = -1; it <= 1; it++) {
552 *pt++ += Br * *ptBti;
553 double Br_ptdBti_ = Br * (*ptdBti);
554 for (
unsigned int ip = 0; ip < NbParam; ip++) {
555 *pt++ -= Br_ptdBti_ * val[ip];
565 void vpTemplateTrackerMIBSpline::PutTotPVBspline3NoSecond(
double *Prt,
double *dPrt,
int &cr,
double &er,
int &ct,
566 double &et,
int &Ncb,
double *val,
unsigned int &NbParam)
582 double *ptBti = &Bti[0];
583 double *ptdBti = &dBti[0];
584 for (
char it = -1; it <= 1; it++) {
585 *ptBti++ = Bspline3(-it + et);
586 *ptdBti++ = dBspline3(-it + et);
589 int NbParam_ =
static_cast<int>(NbParam);
590 int ct_st_ = ct + st;
591 int cr_sr_ = cr + sr;
593 for (
char ir = -1; ir <= 1; ir++) {
594 double Br = Bspline3(-ir + er);
597 int ind = (cr_sr_ + irInd) * Ncb;
601 double Br_ptBti_ = Br * (*ptBti);
602 double Br_ptdBti_ = Br * (*ptdBti);
603 for (
char it = -1; it <= 1; it++) {
604 Prt[ind + (ct_st_ + it)] += Br_ptBti_;
605 int ind1 = (ind + (ct_st_ + it)) * NbParam_;
606 for (
short int ip = 0; ip < NbParam_; ip++) {
607 dPrt[ind1 + ip] -= Br_ptdBti_ * val[ip];
615 void vpTemplateTrackerMIBSpline::PutTotPVBspline4NoSecond(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
616 int &Nc,
double *val,
unsigned int &NbParam)
618 double Bti[4] = { 0, 0, 0, 0 };
619 double dBti[4] = { 0, 0, 0, 0 };
621 for (
char it = -1; it <= 2; it++) {
622 Bti[it + 1] = vpTemplateTrackerBSpline::Bspline4(-it + et);
623 dBti[it + 1] = dBspline4(-it + et);
626 int NbParam_ = (int)NbParam;
628 double *pt = &Prt[(cr * Nc + ct) * 16 * (1 + NbParam_ + NbParam_ * NbParam_)];
629 for (
int ir = -1; ir <= 2; ir++) {
630 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
631 for (
int it = 0; it <= 3; it++) {
632 (*pt++) += Br * Bti[it];
634 double Br_dBti_ = Br * dBti[it];
635 for (
int ip = 0; ip < NbParam_; ip++) {
636 (*pt++) -= Br_dBti_ * val[ip];
644 void vpTemplateTrackerMIBSpline::PutTotPVBspline4NoSecond(
double *Prt,
double *dPrt,
int &cr,
double &er,
int &ct,
645 double &et,
int &Ncb,
double *val,
unsigned int &NbParam)
647 double Bti[4] = { 0, 0, 0, 0 };
648 double dBti[4] = { 0, 0, 0, 0 };
650 for (
char it = -1; it <= 2; it++) {
651 Bti[it + 1] = vpTemplateTrackerBSpline::Bspline4(-it + et);
652 dBti[it + 1] = dBspline4(-it + et);
655 int NbParam_ =
static_cast<int>(NbParam);
657 for (
int ir = -1; ir <= 2; ir++) {
658 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
660 int ind = (cr + irInd) * Ncb;
663 for (
int it = 0; it <= 3; it++) {
664 Prt[ind1 + it] += Br * Bti[it];
665 int ind2 = (ind + (ct + it)) * NbParam_;
667 double Br_dBti_ = Br * dBti[it];
668 for (
int ip = 0; ip < NbParam_; ip++) {
669 dPrt[ind2 + ip] -= Br_dBti_ * val[ip];
675 void vpTemplateTrackerMIBSpline::PutTotPVBsplinePrtTout(
double *PrtTout,
int &cr,
double &er,
int &ct,
double &et,
676 int &Nc,
unsigned int &NbParam,
int °ree)
680 PutTotPVBspline4PrtTout(PrtTout, cr, er, ct, et, Nc, NbParam);
683 PutTotPVBspline3PrtTout(PrtTout, cr, er, ct, et, Nc, NbParam);
687 void vpTemplateTrackerMIBSpline::PutTotPVBspline3PrtTout(
double *PrtTout,
int &cr,
double &er,
int &ct,
double &et,
688 int &Nc,
unsigned int &NbParam)
701 double Bti[3] = { 0, 0, 0 };
703 for (
char it = -1; it <= 1; it++) {
704 Bti[it + 1] = Bspline3(-it + et);
707 int NbParam_ =
static_cast<int>(NbParam);
708 int NbParam_val = NbParam_ + NbParam_ * NbParam_;
710 double *pt = &PrtTout[(
unsigned int)(((cr + sr) * Nc + (ct + st)) * 9 * (1 + NbParam_val))];
711 for (
int ir = -1; ir <= 1; ir++) {
712 double Br = Bspline3(-ir + er);
713 for (
int it = 0; it <= 2; it++) {
714 (*pt++) += Br * Bti[it];
719 void vpTemplateTrackerMIBSpline::PutTotPVBspline4PrtTout(
double *PrtTout,
int &cr,
double &er,
int &ct,
double &et,
720 int &Nc,
unsigned int &NbParam)
722 double Bti[4] = { 0, 0, 0, 0 };
724 for (
char it = -1; it <= 2; it++) {
725 Bti[it + 1] = vpTemplateTrackerBSpline::Bspline4(-it + et);
728 int NbParam_ =
static_cast<int>(NbParam);
729 int NbParam_val = NbParam_ + NbParam_ * NbParam_;
730 double *pt = &PrtTout[(
unsigned int)((cr * Nc + ct) * 16 * (1 + NbParam_val))];
731 for (
int ir = -1; ir <= 2; ir++) {
732 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
733 for (
int it = 0; it <= 3; it++) {
734 (*pt++) += Br * Bti[it];
740 void vpTemplateTrackerMIBSpline::PutTotPVBsplinePrt(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
int &Ncb,
741 unsigned int &NbParam,
int °ree)
745 PutTotPVBspline4PrtTout(Prt, cr, er, ct, et, Ncb, NbParam);
748 PutTotPVBspline3PrtTout(Prt, cr, er, ct, et, Ncb, NbParam);
752 void vpTemplateTrackerMIBSpline::PutTotPVBspline3Prt(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
int &Ncb)
766 double Bti[3] = { 0, 0, 0 };
768 for (
char it = -1; it <= 1; it++) {
769 Bti[it + 1] = Bspline3(-it + et);
772 int ct_st_ = ct + st;
773 int cr_sr_ = cr + sr;
774 for (
int ir = -1; ir <= 1; ir++) {
775 double Br = Bspline3(-ir + er);
778 int ind = (cr_sr_ + irInd) * Ncb;
779 for (
int it = 0; it <= 2; it++) {
780 Prt[ind + (ct_st_ + it)] += Br * Bti[it];
785 void vpTemplateTrackerMIBSpline::PutTotPVBspline4Prt(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
int &Ncb)
787 double Bti[4] = { 0, 0, 0, 0 };
789 for (
char it = -1; it <= 2; it++) {
790 Bti[it + 1] = vpTemplateTrackerBSpline::Bspline4(-it + et);
793 for (
int ir = -1; ir <= 2; ir++) {
794 double Br = vpTemplateTrackerBSpline::Bspline4(-ir + er);
796 int ind = (cr + irInd) * Ncb + ct;
798 for (
int it = 0; it <= 3; it++) {
799 Prt[ind + it] += Br * Bti[it];
804 void vpTemplateTrackerMIBSpline::computeProbabilities(
double *Prt,
int &cr,
double &er,
int &ct,
double &et,
int &Nc,
805 double *dW,
unsigned int &NbParam,
int &bspline,
807 bool use_hessien_des)
811 PutTotPVBspline3NoSecond(Prt, cr, er, ct, et, Nc, dW, NbParam);
813 PutTotPVBspline4NoSecond(Prt, cr, er, ct, et, Nc, dW, NbParam);
817 PutTotPVBspline3(Prt, cr, er, ct, et, Nc, dW, NbParam);
819 PutTotPVBspline4(Prt, cr, er, ct, et, Nc, dW, NbParam);
vpHessienApproximationType