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