]> Creatis software - clitk.git/blob - tools/clitkTrajectory2D.cxx
ggo case sensitivity
[clitk.git] / tools / clitkTrajectory2D.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   clitk
4   Module:    $RCSfile: clitkTrajectory2D.cxx,v $
5   Language:  C++
6   Date:      $Date: 2010/03/03 12:41:27 $
7   Version:   $Revision: 1.1 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                              
17 =========================================================================*/
18
19 #include "clitkTrajectory2D.h"
20
21 //---------------------------------------------------------------------
22 void clitk::Trajectory2D::ResampleWithTimeWarpTo(int begin, int end, Trajectory2D & output) const {
23   DD(begin);
24   DD(end);
25   int n = (int)lrint((end-begin)*GetSamplingPeriod()/output.GetSamplingPeriod());
26   output.resize(n);
27   DD(n);
28   double duration = (end-begin)*GetSamplingPeriod();
29   DD(duration);
30   double sp = output.GetSamplingPeriod();
31   DD(output.GetTotalTimeDuration());
32   //   DD(sp);
33   for(int i=0; i<n; i++) {
34     Vector2d p;
35     // DD(i);
36     GetValueAtLin((i*sp)+(begin*GetSamplingPeriod()), p);
37     // DD(p);
38     output.SetPoint(i, p);
39   }
40 }
41 //---------------------------------------------------------------------
42
43
44 //---------------------------------------------------------------------
45 bool clitk::Trajectory2D::Read(std::string filename) {
46   bool b = mU.Read(filename, 0);
47   if (!b) return false;
48   b = mV.Read(filename, 1);
49   return b;
50 }
51 //---------------------------------------------------------------------
52
53
54 //---------------------------------------------------------------------
55 void clitk::Trajectory2D::GetValueAtLin(double t, Vector2d & p) const {
56   // Can be faster, I know ...
57   p[0] = mU.GetValueAtLin(t);
58   p[1] = mV.GetValueAtLin(t);
59 }
60 //---------------------------------------------------------------------
61
62
63 //---------------------------------------------------------------------
64 void clitk::Trajectory2D::ResampleWithTimeWarpTo(Trajectory2D & output) const {
65   ResampleWithTimeWarpTo(0, size(), output);
66 }
67 //---------------------------------------------------------------------
68
69
70 //---------------------------------------------------------------------        
71 void clitk::Trajectory2D::Shift(double s) {
72   std::cout << "Shift s=" << s << std::endl;
73   int d = (int)lrint(s/GetSamplingPeriod()); // closest integer delta
74   DD(d);
75   clitk::Signal tempU((uint)size());
76   clitk::Signal tempV((uint)size());
77   for(int i=0; i<size(); i++) {
78     tempU[0] = mU[(i+d)%size()];
79     tempV[0] = mV[(i+d)%size()];
80   }
81   for(int i=0; i<size(); i++) {
82     mU[i] = tempU[i];
83     mV[i] = tempV[i];
84   }
85 }
86 //---------------------------------------------------------------------        
87
88
89 //---------------------------------------------------------------------        
90 void clitk::Trajectory2D::GetPoint(const int index, Vector2d & p) {
91   p[0] = mU[index];
92   p[1] = mV[index];
93 }
94 //---------------------------------------------------------------------        
95
96
97 //---------------------------------------------------------------------        
98 clitk::Trajectory2D & clitk::Trajectory2D::operator+(Trajectory2D & d) {
99   for(int i=0; i<size(); i++) {
100     mU[i] += d.GetU(i);
101     mV[i] += d.GetV(i);
102   }
103   return  *this;
104 }
105 //---------------------------------------------------------------------        
106
107
108 //---------------------------------------------------------------------        
109 clitk::Trajectory2D & clitk::Trajectory2D::operator-(Trajectory2D & d) {
110   for(int i=0; i<size(); i++) {
111     mU[i] -= d.GetU(i);
112     mV[i] -= d.GetV(i);
113   }
114   return  *this;
115 }
116 //---------------------------------------------------------------------        
117
118
119 //---------------------------------------------------------------------        
120 void clitk::Trajectory2D::GetMean(Vector2d & m) {
121   m[0] = 0.0;
122   m[1] = 0.0;
123   for(int i=0; i<size(); i++) {
124     m[0] += mU[i];
125     m[1] += mV[i];
126   }
127   m[0] /= size();
128   m[1] /= size();
129 }
130 //---------------------------------------------------------------------        
131
132
133 //---------------------------------------------------------------------        
134 double clitk::Trajectory2D::DistanceTo(int delta, const clitk::Trajectory2D & B) const {
135   double d = 0.0;
136   for(int n=0; n<size(); n++) {
137     int i = n;//(n)%size();
138     int j = (n+delta)%B.size();
139     // DD(j);
140 //     DD(i);
141 //     DD(n);
142     d += pow(mU[i] - B.GetU(j), 2) + pow(mV[i] - B.GetV(j), 2);
143   }
144   return d;
145 }
146 //---------------------------------------------------------------------        
147
148
149 //---------------------------------------------------------------------        
150 void clitk::Trajectory2D::Print(const std::string & name) const {
151   Print(name, 0, size());
152 }
153 //---------------------------------------------------------------------        
154
155
156 //---------------------------------------------------------------------        
157 void clitk::Trajectory2D::Print(const std::string & name, int begin, int end) const {
158   std::cout << "Traj " << name << " size = " << size() 
159             << " from " << begin << " to " << end << std ::endl;
160   for(int i=begin; i<end; i++) {
161     std::cout << mU[i] << " " << mV[i] << std::endl;
162   }
163 }
164 //---------------------------------------------------------------------        
165
166
167 //---------------------------------------------------------------------        
168 void clitk::Trajectory2D::Substract(const Vector2d & m) {
169   mU.AddValue(-m[0]);
170   mV.AddValue(-m[1]);
171 }
172 //---------------------------------------------------------------------