Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/DTWWD.cpp
Go to the documentation of this file.
00001 #include "DTWWD.h"
00002 
00003 using std::ostringstream;
00004 using namespace Marsyas;
00005 
00006 DTWWD::DTWWD(mrs_string name):MarSystem("DTWWD", name)
00007 {
00008   addControls();
00009 }
00010 
00011 DTWWD::DTWWD(const DTWWD& a):MarSystem(a)
00012 {
00013   ctrl_mode_ = getctrl("mrs_string/mode");
00014   ctrl_localPath_ = getctrl("mrs_string/localPath");
00015   ctrl_startPos_ = getctrl("mrs_string/startPos");
00016   ctrl_lastPos_ = getctrl("mrs_string/lastPos");
00017   ctrl_totalDis_ = getctrl("mrs_real/totalDistance");
00018   ctrl_sizes_ = getctrl("mrs_realvec/sizes");
00019   ctrl_weight_ = getctrl("mrs_bool/weight");
00020   ctrl_delta_ = getctrl("mrs_realvec/delta");
00021   ctrl_deltaWeight_ = getctrl("mrs_real/deltaWeight");
00022 }
00023 
00024 DTWWD::~DTWWD()
00025 {
00026 }
00027 
00028 MarSystem*
00029 DTWWD::clone() const
00030 {
00031   return new DTWWD(*this);
00032 }
00033 
00034 void
00035 DTWWD::addControls()
00036 {
00037   totalDis_ = 0;
00038   addControl("mrs_string/mode", "normal", ctrl_mode_);
00039   addControl("mrs_string/localPath", "normal", ctrl_localPath_);
00040   addControl("mrs_string/startPos", "zero", ctrl_startPos_);
00041   addControl("mrs_string/lastPos", "end", ctrl_lastPos_);
00042   addControl("mrs_real/totalDistance", totalDis_, ctrl_totalDis_);
00043   addControl("mrs_realvec/sizes", realvec(), ctrl_sizes_);
00044   addControl("mrs_bool/weight", false, ctrl_weight_);
00045   addControl("mrs_realvec/delta", realvec(), ctrl_delta_);
00046   addControl("mrs_real/deltaWeight", 1.0, ctrl_deltaWeight_);
00047 }
00048 
00049 void DTWWD::myUpdate(MarControlPtr sender)
00050 {
00051   (void) sender;  //suppress warning of unused parameter(s)
00052   ctrl_onSamples_->setValue(2,NOUPDATE);
00053   ctrl_onObservations_->setValue(ctrl_inSamples_+ctrl_inObservations_, NOUPDATE);
00054   ctrl_osrate_->setValue(ctrl_osrate_,NOUPDATE);
00055   ostringstream oss;
00056   for(mrs_natural o=0; o<ctrl_onObservations_->to<mrs_natural>(); ++o)
00057     oss << "DTWWD_" << o << ",";
00058   ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00059 
00060   MarControlAccessor acc(ctrl_sizes_);
00061   realvec& tmpvec = acc.to<mrs_realvec>();
00062   if(tmpvec.getRows() == 1 && tmpvec.getCols() >= 2)
00063   {
00064     sizes_.create(tmpvec.getCols());
00065     for(mrs_natural i=0; i<tmpvec.getCols(); ++i)
00066     {
00067       sizes_(i) = (mrs_natural)tmpvec(0,i);
00068     }
00069   }
00070   else if(tmpvec.getRows() >= 2 && tmpvec.getCols() == 1)
00071   {
00072     sizes_.create(tmpvec.getRows());
00073     for(mrs_natural i=0; i<tmpvec.getRows(); ++i)
00074     {
00075       sizes_(i) = (mrs_natural)tmpvec(i,0);
00076     }
00077   }
00078 
00079   alignment_.create(ctrl_inObservations_->to<mrs_natural>(), ctrl_inSamples_->to<mrs_natural>());
00080   if(ctrl_localPath_->to<mrs_string>() == "normal")
00081   {
00082     costMatrix_.create(ctrl_inObservations_->to<mrs_natural>(), 2);
00083     matrixPos_.create(2);
00084   }
00085   else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00086   {
00087     costMatrix_.create(ctrl_inObservations_->to<mrs_natural>(), 3);
00088     matrixPos_.create(3);
00089   }
00090   if(ctrl_mode_->to<mrs_string>() == "OnePass")
00091   {
00092     mrs_natural nTemplates = sizes_.getSize()-1;
00093     beginPos_.create(nTemplates);
00094     endPos_.create(nTemplates);
00095     beginPos_(0) = 0;
00096     for(mrs_natural l=1; l<nTemplates; l++)
00097     {
00098       beginPos_(l) = sizes_(l) + beginPos_(l-1);
00099     }
00100     for(mrs_natural l=0; l<nTemplates; l++)
00101     {
00102       endPos_(l) = beginPos_(l) + sizes_(l+1);
00103 
00104     }
00105   }
00106 
00107   delta_ = ctrl_delta_->to<mrs_realvec>();
00108   if(delta_.getSize() <= 0) {
00109     delta_.create(inSamples_);
00110   }
00111   weight_ = ctrl_deltaWeight_->to<mrs_real>();
00112 
00113 }
00114 
00115 void
00116 DTWWD::myProcess(realvec& in, realvec& out)
00117 {
00118   mrs_natural i, j, k, l;
00119   j = 0;
00120 
00121   mrs_real nObs = in.getRows();
00122   mrs_real nSmp = in.getCols();
00123   mrs_real tmpReal = 0.0;
00124   mrs_bool weight = ctrl_weight_->to<mrs_bool>();
00125 
00126   if(inSamples_ > 0)
00127   {
00128     if(ctrl_mode_->to<mrs_string>() == "normal")
00129     {
00130       if(ctrl_localPath_->to<mrs_string>() == "normal" || ((nSmp > 2*nObs || nObs > 2*nSmp) && ctrl_localPath_->to<mrs_string>() == "diagonal"))
00131       {
00132         if((nSmp > 2*nObs || nObs > 2*nSmp) && ctrl_localPath_->to<mrs_string>() == "diagonal") {
00133           MRSWARN("DTWWD::myProcess - invalid local path control: diagonal (processes with normal local path)");
00134         }
00135 
00136         for(i=0; i<2; ++i)
00137         {
00138           matrixPos_(i) = i;
00139         }
00140         // |vertical:1, /diagonal:2, _horizonal:3
00141 
00142         if(ctrl_startPos_->to<mrs_string>() == "zero")
00143         {
00144           // copying first SimilarityMatrix
00145           costMatrix_(0,(mrs_natural)matrixPos_(0)) = in(0,0);
00146           alignment_(0,0) = 0;
00147           // calculating other cost of the first col
00148           for(j=1; j<nObs; j++)
00149           {
00150             costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0)+costMatrix_(j-1,(mrs_natural)matrixPos_(0));
00151             alignment_(j,0) = 1;
00152           }
00153         }
00154         else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00155         {
00156           // copying first col of SimilarityMatrix
00157           for(j=0; j<nObs; j++)
00158           {
00159             costMatrix_(j, (mrs_natural)matrixPos_(0)) = in(j,0);
00160             alignment_(j,0) = 0;
00161           }
00162         }
00163         // after first col
00164         for(i=1; i<nSmp; ++i)
00165         {
00166           costMatrix_(0,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(0,i);
00167           alignment_(0,i) = 3;
00168           for(j=1; j<nObs; j++)
00169           {
00170             costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00171             alignment_(j,i) = 1;
00172             tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i);
00173             if(weight)
00174               tmpReal += in(j,i);
00175             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00176             {
00177               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00178               alignment_(j,i) = 2;
00179             }
00180             tmpReal = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in(j,i);
00181             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00182             {
00183               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00184               alignment_(j,i) = 3;
00185             }
00186           }
00187           matrixPos_(0) = 1-matrixPos_(0);
00188           matrixPos_(1) = 1-matrixPos_(1);
00189         }
00190 
00191         // backtrace
00192         for(i=0; i<out.getRows(); ++i)
00193         {
00194           for(j=0; j<out.getCols(); j++)
00195           {
00196             out(i,j) = -1;
00197           }
00198         }
00199         if(ctrl_lastPos_->to<mrs_string>() == "end")
00200         {
00201           totalDis_ = costMatrix_((mrs_natural)nObs-1,(mrs_natural)matrixPos_(0));
00202           ctrl_totalDis_->setValue(totalDis_);
00203           i = (mrs_natural)nSmp-1;
00204           j = (mrs_natural)nObs-1;
00205         }
00206         else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00207         {
00208           tmpReal = costMatrix_(0, (mrs_natural)matrixPos_(0));
00209           j = 0;
00210           for(i=1; i<nObs; ++i)
00211           {
00212             if(costMatrix_(i, (mrs_natural)matrixPos_(0)) < tmpReal)
00213             {
00214               tmpReal = costMatrix_(i, (mrs_natural)matrixPos_(0));
00215               j = i;
00216             }
00217           }
00218           i = (mrs_natural)nSmp-1;
00219           totalDis_ = tmpReal;
00220           ctrl_totalDis_->setValue(totalDis_);
00221         }
00222         k = (mrs_natural)nSmp + (mrs_natural)nObs - 1;
00223         while(alignment_(j,i) != 0 && k>=0)
00224         {
00225           if(alignment_(j,i) == 1)
00226           {
00227             out(k,0) = i;
00228             out(k,1) = j;
00229             j--;
00230             k--;
00231           }
00232           else if(alignment_(j,i) == 2)
00233           {
00234             out(k,0) = i;
00235             out(k,1) = j;
00236             k--;
00237             if(weight)
00238             {
00239               out(k,0) = i;
00240               out(k,1) = j;
00241               k--;
00242             }
00243             i--;
00244             j--;
00245           }
00246           else if(alignment_(j,i) == 3)
00247           {
00248             out(k,0) = i;
00249             out(k,1) = j;
00250             k--;
00251             i--;
00252           }
00253         }
00254         out(k,0) = i;
00255         out(k,1) = j;
00256       }
00257 
00258       else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00259       {
00260         for(i=0; i<3; ++i)
00261         {
00262           matrixPos_(i) = i;
00263         }
00264         // /|diagonal,vertical:1, /diagonal:2, /-diagonal,horizonal:3
00265 
00266         if(ctrl_startPos_->to<mrs_string>() == "zero")
00267         {
00268           // copying the first SimilarityMatrix
00269           costMatrix_(0,(mrs_natural)matrixPos_(0)) = in(0,0);
00270           alignment_(0,0) = 0;
00271           // calculating the second col
00272           costMatrix_(1,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1);
00273           if(weight)
00274             costMatrix_(1,(mrs_natural)matrixPos_(1)) += in(1,1);
00275           costMatrix_(2,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(2,1);
00276           if(weight)
00277             costMatrix_(2,(mrs_natural)matrixPos_(1)) += in(1,1);
00278           alignment_(1,1) = 2;
00279           alignment_(2,1) = 1;
00280           // calculating the third col
00281           costMatrix_(1,(mrs_natural)matrixPos_(2)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(1,2);
00282           if(weight)
00283             costMatrix_(1,(mrs_natural)matrixPos_(2)) += in(1,1);
00284           alignment_(1,2) = 3;
00285           costMatrix_(2,(mrs_natural)matrixPos_(2)) = costMatrix_(1,(mrs_natural)matrixPos_(1)) + in(2,2);
00286           if(weight)
00287             costMatrix_(2,(mrs_natural)matrixPos_(2)) += in(2,2);
00288           alignment_(2,2) = 2;
00289           costMatrix_(3,(mrs_natural)matrixPos_(2)) = costMatrix_(2,(mrs_natural)matrixPos_(1)) + in(3,2);
00290           if(weight)
00291             costMatrix_(3,(mrs_natural)matrixPos_(2)) += in(3,2);
00292           alignment_(3,2) = 2;
00293           tmpReal = costMatrix_(1,(mrs_natural)matrixPos_(1)) + in(2,2) + in(3,2);
00294           if(weight)
00295             tmpReal += in(2,2);
00296           if(tmpReal < costMatrix_(3,(mrs_natural)matrixPos_(2)))
00297           {
00298             costMatrix_(3,(mrs_natural)matrixPos_(2)) = tmpReal;
00299             alignment_(3,2) = 1;
00300           }
00301         }
00302         else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00303         {
00304           // copying first col of SimilarityMatrix
00305           for(j=0; j<nObs; j++)
00306           {
00307             costMatrix_(j,(mrs_natural) matrixPos_(0)) = in(j,0);
00308             alignment_(j,0) = 0;
00309           }
00310           // calculating the second col
00311           costMatrix_(1,(mrs_natural)matrixPos_(1)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1);
00312           if(weight)
00313             costMatrix_(1,(mrs_natural)matrixPos_(1)) += in(1,1);
00314           alignment_(1,1) = 2;
00315           for(j=2; j<nObs; j++)
00316           {
00317             costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1);
00318             if(weight)
00319               costMatrix_(j,(mrs_natural)matrixPos_(1)) += in(j,1);
00320             alignment_(j,1) = 2;
00321             tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(0)) + in(j-1,1) + in(j,1);
00322             if(weight)
00323               tmpReal += in(j-1,1);
00324             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00325             {
00326               costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00327               alignment_(j,1) = 1;
00328             }
00329           }
00330           // calculating the third col
00331           costMatrix_(1,(mrs_natural)matrixPos_(2)) = costMatrix_(0,(mrs_natural)matrixPos_(0)) + in(1,1) + in(1,2);
00332           if(weight)
00333             costMatrix_(1,(mrs_natural)matrixPos_(2)) += in(1,1);
00334           alignment_(1,2) = 3;
00335           for(j=2; j<nObs; j++)
00336           {
00337             costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,2);
00338             if(weight)
00339               costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,2);
00340             alignment_(j,2) = 2;
00341             if(alignment_(j-2,2) != 0)
00342             {
00343               tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,2) + in(j,2);
00344               if(weight)
00345                 tmpReal += in(j-1,2);
00346               if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00347               {
00348                 costMatrix_(j,(mrs_natural)matrixPos_(2));
00349                 alignment_(j,2) = 1;
00350               }
00351             }
00352             tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1) + in(j,2);
00353             if(weight)
00354               tmpReal += in(j,1);
00355             if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00356             {
00357               costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00358               alignment_(j,2) = 3;
00359             }
00360           }
00361         }
00362         for(i=0; i<3; ++i)
00363         {
00364           matrixPos_(i)++;
00365           if(matrixPos_(i)>=3)
00366             matrixPos_(i) = 0;
00367         }
00368         // after third col
00369         for(i=3; i<nSmp; ++i)
00370         {
00371           for(j=2; j<nObs; j++)
00372           {
00373             if(alignment_(j-1,i-2) != 0)
00374             {
00375               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i-1) + in(j,i);
00376               if(weight)
00377                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i-1);
00378               alignment_(j,i) = 3;
00379               if(alignment_(j-1,i-1) != 0)
00380               {
00381                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00382                 if(weight)
00383                   tmpReal += in(j,i);
00384                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00385                 {
00386                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00387                   alignment_(j,i) = 2;
00388                 }
00389               }
00390               if(alignment_(j-2,i-1) != 0)
00391               {
00392                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00393                 if(weight)
00394                   tmpReal += in(j-1,i);
00395                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00396                 {
00397                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00398                   alignment_(j,i) = 1;
00399                 }
00400               }
00401             }
00402             else if(alignment_(j-1,i-1) != 0)
00403             {
00404               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00405               if(weight)
00406                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i);
00407               alignment_(j,i) = 2;
00408               if(alignment_(j-2,i-1) != 0)
00409               {
00410                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00411                 if(weight)
00412                   tmpReal += in(j-1,i);
00413                 alignment_(j,i) = 1;
00414               }
00415             }
00416             else if(alignment_(j-2,i-1) != 0)
00417             {
00418               costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00419               if(weight)
00420                 costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j-1,i);
00421               alignment_(j,i) = 1;
00422             }
00423           }
00424           for(j=0; j<3; j++)
00425           {
00426             matrixPos_(j)++;
00427             if(matrixPos_(j) >= 3)
00428               matrixPos_(j) = 0;
00429           }
00430         }
00431 
00432         // backtrace
00433         for(i=0; i<out.getRows(); ++i)
00434         {
00435           for(j=0; j<out.getCols(); j++)
00436           {
00437             out(i,j) = -1;
00438           }
00439         }
00440         if(ctrl_lastPos_->to<mrs_string>() == "end")
00441         {
00442           totalDis_ = costMatrix_((mrs_natural)nObs-1,(mrs_natural)matrixPos_(1));
00443           ctrl_totalDis_->setValue(totalDis_);
00444           i = (mrs_natural)nSmp-1;
00445           j = (mrs_natural)nObs-1;
00446         }
00447         else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00448         {
00449           tmpReal = costMatrix_((mrs_natural)nObs-1, (mrs_natural)matrixPos_(1));
00450           j = (mrs_natural)nObs-1;
00451           for(i=0; i<nObs-1; ++i)
00452           {
00453             if(costMatrix_(i, (mrs_natural)matrixPos_(1)) < tmpReal && alignment_(i,(mrs_natural)nSmp-1) != 0)
00454             {
00455               tmpReal = costMatrix_(i, (mrs_natural)matrixPos_(1));
00456               j = i;
00457             }
00458           }
00459           i = (mrs_natural)nSmp-1;
00460           totalDis_ = tmpReal;
00461           ctrl_totalDis_->setValue(totalDis_);
00462         }
00463         k = (mrs_natural)nSmp + (mrs_natural)nObs - 1;
00464         while(alignment_(j,i) != 0 && k>=0)
00465         {
00466           if(alignment_(j,i) == 1)
00467           {
00468             out(k,0) = i;
00469             out(k,1) = j;
00470             j--;
00471             k--;
00472             out(k,0) = i;
00473             out(k,1) = j;
00474             k--;
00475             if(weight)
00476             {
00477               out(k,0) = i;
00478               out(k,1) = j;
00479               k--;
00480             }
00481             i--;
00482             j--;
00483           }
00484           else if(alignment_(j,i) == 2)
00485           {
00486             out(k,0) = i;
00487             out(k,1) = j;
00488             k--;
00489             if(weight)
00490             {
00491               out(k,0) = i;
00492               out(k,1) = j;
00493               k--;
00494             }
00495             i--;
00496             j--;
00497           }
00498           else if(alignment_(j,i) == 3)
00499           {
00500             out(k,0) = i;
00501             out(k,1) = j;
00502             k--;
00503             i--;
00504             out(k,0) = i;
00505             out(k,1) = j;
00506             k--;
00507             if(weight)
00508             {
00509               out(k,0) = i;
00510               out(k,1) = j;
00511               k--;
00512             }
00513             i--;
00514             j--;
00515           }
00516         }
00517         out(k,0) = i;
00518         out(k,1) = j;
00519       }
00520     }
00521 
00522     else if(ctrl_mode_->to<mrs_string>() == "OnePass")
00523     {
00524       mrs_natural nTemplates = sizes_.getSize()-1;
00525       if(sizes_.getSize() > 0)
00526       {
00527 
00528         if(ctrl_localPath_->to<mrs_string>() == "normal")
00529         {
00530           for(i=0; i<2; ++i)
00531           {
00532             matrixPos_(i) = i;
00533           }
00534           // |vertical:1, /diagonal:2, _horizonal:3
00535 
00536           if(ctrl_startPos_->to<mrs_string>() == "zero")
00537           {
00538             // copying first SimilarityMatrix
00539             for(l=0; l<nTemplates; l++)
00540             {
00541               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) = in((mrs_natural)beginPos_(l),0) - weight_*delta_(0);
00542               alignment_((mrs_natural)beginPos_(l),0) = 0;
00543             }
00544             // calculating other cost of the first col
00545             for(l=0; l<nTemplates; l++)
00546             {
00547               for(j=(mrs_natural)beginPos_(l)+1; j<(mrs_natural)endPos_(l); j++)
00548               {
00549                 costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0) + costMatrix_(j-1,(mrs_natural)matrixPos_(0));
00550                 alignment_(j,0) = 1;
00551               }
00552             }
00553           }
00554           else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00555           {
00556             // copying first col of SimilarityMatrix
00557             for(j=0; j<nObs; j++)
00558             {
00559               costMatrix_(j,(mrs_natural)matrixPos_(0)) = in(j,0) - weight_*delta_(0);
00560               alignment_(j,0) = 0;
00561             }
00562           }
00563           // after first col
00564           for(i=1; i<nSmp; ++i)
00565           {
00566             for(l=0; l<nTemplates; l++)
00567             {
00568               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),i);
00569               if(weight)
00570                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l),i);
00571               alignment_((mrs_natural)beginPos_(l),i) = -1*((mrs_natural)endPos_(l)-1);
00572               tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),i);
00573               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)))
00574               {
00575                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = tmpReal;
00576                 alignment_((mrs_natural)beginPos_(l), i) = 3;
00577               }
00578               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) -= weight_*delta_(i);
00579               for(j=(mrs_natural)beginPos_(l)+1; j<(mrs_natural)endPos_(l); j++)
00580               {
00581                 costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00582                 alignment_(j,i) = 1;
00583                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i);
00584                 if(weight)
00585                   tmpReal += in(j,i);
00586                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00587                 {
00588                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00589                   alignment_(j,i) = 2;
00590                 }
00591                 tmpReal = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in(j,i);
00592                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00593                 {
00594                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00595                   alignment_(j,i) = 3;
00596                 }
00597               }
00598             }
00599             matrixPos_(0) = 1-matrixPos_(0);
00600             matrixPos_(1) = 1-matrixPos_(1);
00601           }
00602 
00603           // backtrace
00604           for(i=0; i<out.getRows(); ++i)
00605           {
00606             for(j=0; j<out.getCols(); j++)
00607             {
00608               out(i,j) = -1;
00609             }
00610           }
00611           if(ctrl_lastPos_->to<mrs_string>() == "end")
00612           {
00613             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(0));
00614             j = (mrs_natural)endPos_(0)-1;
00615             for(l=1; l<nTemplates; l++)
00616             {
00617               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) < tmpReal)
00618               {
00619                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0));
00620                 j = (mrs_natural)endPos_(l)-1;
00621               }
00622             }
00623             totalDis_ = tmpReal;
00624             ctrl_totalDis_->setValue(totalDis_);
00625             i = (mrs_natural)nSmp-1;
00626           }
00627           else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
00628           {
00629             tmpReal = costMatrix_(0,(mrs_natural) matrixPos_(0));
00630             j=0;
00631             for(i=1; i<nObs; ++i)
00632             {
00633               if(costMatrix_(i,(mrs_natural)matrixPos_(0)) < tmpReal)
00634               {
00635                 tmpReal = costMatrix_(i,(mrs_natural) matrixPos_(0));
00636                 j = i;
00637               }
00638             }
00639             i = (mrs_natural)nSmp-1;
00640             totalDis_ = tmpReal;
00641             ctrl_totalDis_->setValue(totalDis_);
00642           }
00643           k = 3*(mrs_natural)nSmp - 1;//+ nObs - 1;
00644           while(alignment_(j,i) != 0 && k>=0)
00645           {
00646             if(alignment_(j,i) == 1)
00647             {
00648               out(k,0) = i;
00649               out(k,1) = j;
00650               j--;
00651               k--;
00652             }
00653             else if(alignment_(j,i) == 2)
00654             {
00655               out(k,0) = i;
00656               out(k,1) = j;
00657               k--;
00658               if(weight)
00659               {
00660                 out(k,0) = i;
00661                 out(k,1) = j;
00662                 k--;
00663               }
00664               i--;
00665               j--;
00666             }
00667             else if(alignment_(j,i) == 3)
00668             {
00669               out(k,0) = i;
00670               out(k,1) = j;
00671               k--;
00672               i--;
00673             }
00674             else if(alignment_(j,i) < 0)
00675             {
00676               out(k,0) = i;
00677               out(k,1) = j;
00678               k--;
00679               if(weight)
00680               {
00681                 out(k,0) = i;
00682                 out(k,1) = j;
00683                 k--;
00684               }
00685               j = -1*(mrs_natural)alignment_(j,i);
00686               i--;
00687             }
00688           }
00689           out(k,0) = i;
00690           out(k,1) = j;
00691         }
00692         else if(ctrl_localPath_->to<mrs_string>() == "diagonal")
00693         {
00694           for(i=0; i<3; ++i)
00695           {
00696             matrixPos_(i) = i;
00697           }
00698           // /|diagonal,vertical:1, /diagonal:2, /-diagonal,holizonal:3
00699 
00700           if(ctrl_startPos_->to<mrs_string>() == "zero")
00701           {
00702             // copying first SimilarityMatrix
00703             for(l=0; l<nTemplates; l++)
00704             {
00705               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) = in((mrs_natural)beginPos_(l),0) - weight_*delta_(0);
00706               alignment_((mrs_natural)beginPos_(l),0) = 0;
00707             }
00708             // calculating the second col
00709             for(l=0; l<nTemplates; l++)
00710             {
00711               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1);
00712               if(weight)
00713                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00714               costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+2,1);
00715               if(weight)
00716                 costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00717               alignment_((mrs_natural)beginPos_(l)+1,1) = 2;
00718               alignment_((mrs_natural)beginPos_(l)+2,1) = 1;
00719             }
00720             // calculating the third col
00721             for(l=0; l<nTemplates; l++)
00722             {
00723               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+1,2);
00724               if(weight)
00725                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,1);
00726               alignment_((mrs_natural)beginPos_(l)+1,2) = 3;
00727               costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+2,2);
00728               if(weight)
00729                 costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+2,2);
00730               alignment_((mrs_natural)beginPos_(l)+2,2) = 2;
00731               costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l)+2,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+3,2);
00732               if(weight)
00733                 costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+3,2);
00734               alignment_((mrs_natural)beginPos_(l)+3,2) = 2;
00735               tmpReal = costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+2,2) + in((mrs_natural)beginPos_(l)+3,2);
00736               if(weight)
00737                 tmpReal += in((mrs_natural)beginPos_(l)+2,2);
00738               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)))
00739               {
00740                 costMatrix_((mrs_natural)beginPos_(l)+3,(mrs_natural)matrixPos_(2)) = tmpReal;
00741                 alignment_((mrs_natural)beginPos_(l)+3,2) = 1;
00742               }
00743             }
00744           }
00745           else if(ctrl_startPos_->to<mrs_string>() == "lowest")
00746           {
00747             // copying first col of SimilarityMatrix
00748             for(j=0; j<nObs; j++)
00749             {
00750               costMatrix_(j, (mrs_natural)matrixPos_(0)) = in(j,0) - weight_*delta_(0);
00751               alignment_(j,0) = 0;
00752             }
00753             // calculating the second col
00754             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(0));
00755             j=(mrs_natural)endPos_(0)-1;
00756             for(l=1; l<nTemplates; l++)
00757             {
00758               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0)) < tmpReal)
00759               {
00760                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(0));
00761                 j=(mrs_natural)endPos_(l)-1;
00762               }
00763             }
00764             for(l=0; l<nTemplates; l++)
00765             {
00766               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) = costMatrix_(j,(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l),1) - weight_*delta_(1);
00767               if(weight)
00768                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l),1);
00769               alignment_((mrs_natural)beginPos_(l),1) = -1*j;
00770             }
00771             for(l=0; l<nTemplates; l++)
00772             {
00773               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1);
00774               if(weight)
00775                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(1)) += in((mrs_natural)beginPos_(l)+1,1);
00776               alignment_((mrs_natural)beginPos_(l)+1,1) = 2;
00777               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00778               {
00779                 costMatrix_(j,(mrs_natural)matrixPos_(1)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1);
00780                 if(weight)
00781                   costMatrix_(j,(mrs_natural)matrixPos_(1)) += in(j,1);
00782                 alignment_(j,1) = 2;
00783                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(0)) + in(j-1,1) + in(j,1);
00784                 if(weight)
00785                   tmpReal += in(j-1,1);
00786                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(1)))
00787                 {
00788                   costMatrix_(j,(mrs_natural)matrixPos_(1)) = tmpReal;
00789                   alignment_(j,1) = 1;
00790                 }
00791               }
00792             }
00793             // calculating the third col
00794             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(1));
00795             j=(mrs_natural)endPos_(0)-1;
00796             for(l=1; l<nTemplates; l++)
00797             {
00798               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1)) < tmpReal)
00799               {
00800                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1, (mrs_natural)matrixPos_(1));
00801                 j=(mrs_natural)endPos_(l)-1;
00802               }
00803             }
00804             for(l=0; l<nTemplates; l++)
00805             {
00806               costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) = costMatrix_(j,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l),2) - weight_*delta_(1);
00807               if(weight)
00808                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l),2);
00809               alignment_((mrs_natural)beginPos_(l),2) = -1*j;
00810             }
00811             //tmpReal = costMatrix_(endPos_(0)-1,matrixPos_(1));
00812             //j=endPos_(0)-1;
00813             //for(l=1; l<nTemplates; l++)
00814             //{
00815             //  if(costMatrix_(endPos_(l)-1,matrixPos_(1)) < tmpReal)
00816             //    {
00817             //      tmpReal = costMatrix_(endPos_(l)-1, matrixPos_(1));
00818             //      j=endPos_(l)-1;
00819             //    }
00820             //}
00821             //for(l=0; l<nTemplates; l++)
00822             //{
00823             //  tmpReal = costMatrix_(j,matrixPos_(1)) + 2.0*in(beginPos_(l),matrixPos_(2));
00824             //  if(tmpReal < costMatrix_(beginPos_(l),matrixPos_(2)))
00825             //    {
00826             //      costMatrix_(beginPos_(l),matrixPos_(2)) = tmpReal;
00827             //      alignment_(beginPos_(l),2) = -1*j;
00828             //    }
00829             //}
00830             for(l=0; l<nTemplates; l++)
00831             {
00832               costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+1,2);
00833               if(weight)
00834                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,2);
00835               alignment_((mrs_natural)beginPos_(l)+1,2) = 2;
00836               tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,1) + in((mrs_natural)beginPos_(l)+1,2);
00837               if(weight)
00838                 tmpReal += in((mrs_natural)beginPos_(l)+1,1);
00839               if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)))
00840               {
00841                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = tmpReal;
00842                 alignment_((mrs_natural)beginPos_(l)+1,2) = 3;
00843               }
00844               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00845               {
00846                 costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,1) + in(j,2);
00847                 if(weight)
00848                   costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,1);
00849                 alignment_(j,2) = 3;
00850                 tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,2);
00851                 if(weight)
00852                   tmpReal += in(j,2);
00853                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00854                 {
00855                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00856                   alignment_(j,2) = 2;
00857                 }
00858                 tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,2) + in(j,2);
00859                 if(weight)
00860                   tmpReal += in(j-1,2);
00861                 if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00862                 {
00863                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00864                   alignment_(j,2) = 1;
00865                 }
00866               }
00867             }
00868           }
00869           for(i=0; i<3; ++i)
00870           {
00871             matrixPos_(i)++;
00872             if(matrixPos_(i)>=3)
00873               matrixPos_(i) = 0;
00874           }
00875           // after third col
00876           for(i=3; i<nSmp; ++i)
00877           {
00878             j = -1;
00879             for(l=0; l<nTemplates; l++)
00880             {
00881               if(alignment_((mrs_natural)endPos_(l)-1,i-1) != 0)
00882               {
00883                 if(j<0 || (j>=0&&costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1))<tmpReal))
00884                 {
00885                   tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1));
00886                   j = (mrs_natural)endPos_(l)-1;
00887                 }
00888               }
00889             }
00890             if(j>=0)
00891             {
00892               for(l=0; l<nTemplates; l++)
00893               {
00894                 costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) = costMatrix_(j,(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l),i) - weight_*delta_(i);
00895                 if(weight)
00896                   costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l),i);
00897                 alignment_((mrs_natural)beginPos_(l),i) = -1*j;
00898               }
00899               //j = -1;
00900               //for(l=0; l<nTemplates; l++)
00901               //  {
00902               //    if(alignment_(endPos_(l)-1,i-2) != 0)
00903               //    {
00904               //if(j<0 || (j>=0&&costMatrix_(endPos_(l)-1,matrixPos_(0))<tmpReal))
00905               //    {
00906               //      tmpReal = costMatrix_(endPos_(l)-1,matrixPos_(0));
00907               //      j = endPos_(l)-1;
00908               //    }
00909               //}
00910               //}
00911               //if(j>=0)
00912               //  {
00913               //    for(l=0; l<nTemplates; l++)
00914               //    {
00915               //      tmpReal = costMatrix_(j,matrixPos_(0)) + 2.0*in(beginPos_(l),i-1) + in(beginPos_(l),i);
00916               //      if(tmpReal < costMatrix_(beginPos_(l),matrixPos_(2)))
00917               //        {
00918               //          costMatrix_(beginPos_(l),matrixPos_(2)) = tmpReal;
00919               //          alignment_(beginPos_(l),i) = -1*j;
00920               //        }
00921               //    }
00922               //}
00923             }/*
00924               else
00925             {
00926               j = -1;
00927               for(l=0; l<nTemplates; l++)
00928                 {
00929                   if(alignment_(endPos_(l)-1,i-2) != 0)
00930                 {
00931                   if(j<0 || (j>=0&&costMatrix_(endPos_(l)-1,matrixPos_(0))<tmpReal))
00932                     {
00933                       tmpReal = costMatrix_(endPos_(l)-1,matrixPos_(0));
00934                       j = endPos_(l)-1;
00935                     }
00936                 }
00937                 }
00938               if(j>=0)
00939                 {
00940                   for(l=0; l<nTemplates; l++)
00941                 {
00942                   costMatrix_(beginPos_(l),matrixPos_(2)) = costMatrix_(j,matrixPos_(0)) + 2.0*in(beginPos_(l),i-1) + in(beginPos_(l),i);
00943                   alignment_(beginPos_(l),i) = -1*j;
00944                 }
00945                 }
00946                 }*/
00947             for(l=0; l<nTemplates; l++)
00948             {
00949               if(alignment_((mrs_natural)beginPos_(l),i-1) != 0)
00950               {
00951                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(1)) + in((mrs_natural)beginPos_(l)+1,i);
00952                 if(weight)
00953                   costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,i);
00954                 alignment_((mrs_natural)beginPos_(l)+1,i) = 2;
00955                 if(alignment_((mrs_natural)beginPos_(l),i-2) != 0)
00956                 {
00957                   tmpReal = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,i-1) + in((mrs_natural)beginPos_(l)+1,i);
00958                   if(weight)
00959                     tmpReal += in((mrs_natural)beginPos_(l)+1,i-1);
00960                   if(tmpReal < costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)))
00961                   {
00962                     costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = tmpReal;
00963                     alignment_((mrs_natural)beginPos_(l)+1,i) = 3;
00964                   }
00965                 }
00966               }
00967               else if(alignment_((mrs_natural)beginPos_(l),i-2) != 0)
00968               {
00969                 costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) = costMatrix_((mrs_natural)beginPos_(l),(mrs_natural)matrixPos_(0)) + in((mrs_natural)beginPos_(l)+1,i-1) + in((mrs_natural)beginPos_(l)+1,i);
00970                 if(weight)
00971                   costMatrix_((mrs_natural)beginPos_(l)+1,(mrs_natural)matrixPos_(2)) += in((mrs_natural)beginPos_(l)+1,i-1);
00972                 alignment_((mrs_natural)beginPos_(l)+1,i) = 3;
00973               }
00974               for(j=(mrs_natural)beginPos_(l)+2; j<(mrs_natural)endPos_(l); j++)
00975               {
00976                 if(alignment_(j-1,i-2) != 0)
00977                 {
00978                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(0)) + in(j,i-1) + in(j,i);
00979                   if(weight)
00980                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i-1);
00981                   alignment_(j,i) = 3;
00982                   if(alignment_(j-1,i-1) != 0)
00983                   {
00984                     tmpReal = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
00985                     if(weight)
00986                       tmpReal += in(j,i);
00987                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00988                     {
00989                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
00990                       alignment_(j,i) = 2;
00991                     }
00992                   }
00993                   if(alignment_(j-2,i-1) != 0)
00994                   {
00995                     tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
00996                     if(weight)
00997                       tmpReal += in(j-1,i);
00998                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
00999                     {
01000                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
01001                       alignment_(j,i) = 1;
01002                     }
01003                   }
01004                 }
01005                 else if(alignment_(j-1,i-1) != 0)
01006                 {
01007                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-1,(mrs_natural)matrixPos_(1)) + in(j,i);
01008                   if(weight)
01009                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j,i);
01010                   alignment_(j,i) = 2;
01011                   if(alignment_(j-2,i-1) != 0)
01012                   {
01013                     tmpReal = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
01014                     if(weight)
01015                       tmpReal += in(j-1,i);
01016                     if(tmpReal < costMatrix_(j,(mrs_natural)matrixPos_(2)))
01017                     {
01018                       costMatrix_(j,(mrs_natural)matrixPos_(2)) = tmpReal;
01019                       alignment_(j,i) = 1;
01020                     }
01021 
01022                   }
01023                 }
01024                 else if(alignment_(j-2,i-1) != 0)
01025                 {
01026                   costMatrix_(j,(mrs_natural)matrixPos_(2)) = costMatrix_(j-2,(mrs_natural)matrixPos_(1)) + in(j-1,i) + in(j,i);
01027                   if(weight)
01028                     costMatrix_(j,(mrs_natural)matrixPos_(2)) += in(j-1,i);
01029                   alignment_(j,i) = 1;
01030                 }
01031               }
01032             }
01033             for(j=0; j<3; j++)
01034             {
01035               matrixPos_(j)++;
01036               if(matrixPos_(j) >= 3)
01037                 matrixPos_(j) = 0;
01038             }
01039           }
01040 
01041           // backtrace
01042           for(i=0; i<out.getRows(); ++i)
01043           {
01044             for(j=0; j<out.getCols(); j++)
01045             {
01046               out(i,j) = -1;
01047             }
01048           }
01049           if(ctrl_lastPos_->to<mrs_string>() == "end")
01050           {
01051             tmpReal = costMatrix_((mrs_natural)endPos_(0)-1,(mrs_natural)matrixPos_(1));
01052             j = (mrs_natural)endPos_(0)-1;
01053             for(l=1; l<nTemplates; l++)
01054             {
01055               if(costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1)) < tmpReal)
01056               {
01057                 tmpReal = costMatrix_((mrs_natural)endPos_(l)-1,(mrs_natural)matrixPos_(1));
01058                 j = (mrs_natural)endPos_(l)-1;
01059               }
01060             }
01061             totalDis_ = tmpReal;
01062             ctrl_totalDis_->setValue(totalDis_);
01063             i = (mrs_natural)nSmp-1;
01064           }
01065           else if(ctrl_lastPos_->to<mrs_string>() == "lowest")
01066           {
01067             tmpReal = costMatrix_(0,(mrs_natural)matrixPos_(1));
01068             j=0;
01069             for(i=1; i<nObs; ++i)
01070             {
01071               if(costMatrix_(i,(mrs_natural)matrixPos_(1)) < tmpReal)
01072               {
01073                 tmpReal = costMatrix_(i,(mrs_natural)matrixPos_(1));
01074                 j = i;
01075               }
01076             }
01077             i = (mrs_natural)nSmp-1;
01078             totalDis_ = tmpReal;
01079             ctrl_totalDis_->setValue(totalDis_);
01080           }
01081           k = 3*(mrs_natural)nSmp -1;// + nObs - 1;
01082           while(alignment_(j,i) != 0 && k>=0)
01083           {
01084             if(alignment_(j,i) == 1)
01085             {
01086               out(k,0) = i;
01087               out(k,1) = j;
01088               j--;
01089               k--;
01090               out(k,0) = i;
01091               out(k,1) = j;
01092               k--;
01093               if(weight)
01094               {
01095                 out(k,0) = i;
01096                 out(k,1) = j;
01097                 k--;
01098               }
01099               j--;
01100               i--;
01101             }
01102             else if(alignment_(j,i) == 2)
01103             {
01104               out(k,0) = i;
01105               out(k,1) = j;
01106               k--;
01107               if(weight)
01108               {
01109                 out(k,0) = i;
01110                 out(k,1) = j;
01111                 k--;
01112               }
01113               i--;
01114               j--;
01115             }
01116             else if(alignment_(j,i) == 3)
01117             {
01118               out(k,0) = i;
01119               out(k,1) = j;
01120               k--;
01121               i--;
01122               out(k,0) = i;
01123               out(k,1) = j;
01124               k--;
01125               if(weight)
01126               {
01127                 out(k,0) = i;
01128                 out(k,1) = j;
01129                 k--;
01130               }
01131               i--;
01132               j--;
01133             }
01134             else if(alignment_(j,i) < 0)
01135             {
01136               out(k,0) = i;
01137               out(k,1) = j;
01138               k--;
01139               if(weight)
01140               {
01141                 out(k,0) = i;
01142                 out(k,1) = j;
01143                 k--;
01144               }
01145               j = -1*(mrs_natural)alignment_(j,i);
01146               i--;
01147             }
01148           }
01149           out(k,0) = i;
01150           out(k,1) = j;
01151         }
01152       }
01153       else
01154       {
01155         MRSWARN("DTWWD::myProcess - invalid sizes vector (does not output a real value)!");
01156       }
01157     }
01158   }
01159 }
01160 
01161 
01162 
01163 
01164