vcsl_spatial_transformation.cxx
Go to the documentation of this file.
1 // This is core/vcsl/vcsl_spatial_transformation.cxx
2 #include <cmath>
4 #ifdef _MSC_VER
5 # include <vcl_msvc_warnings.h>
6 #endif
7 #include <cassert>
8 
9 //---------------------------------------------------------------------------
10 // Is `time' between the two time bounds ?
11 //---------------------------------------------------------------------------
13 {
14  if (beat_.size() == 0) return true;
15  return (beat_[0]<=time)&&(time<=beat_[beat_.size()-1]);
16 }
17 
18 //---------------------------------------------------------------------------
19 // Return the index of the beat inferior or equal to `time'
20 // REQUIRE: valid_time(time)
21 //---------------------------------------------------------------------------
23 {
24  // require
25  assert(valid_time(time));
26 
27  // Dichotomic research of the index
28  int inf=0;
29  int sup=beat_.size()-1;
30  while (sup-inf > 1)
31  {
32  int mid=(inf+sup)/2;
33  if (beat_[mid]>time)
34  sup=mid;
35  else
36  inf=mid;
37  }
38  return inf;
39 }
40 
41 //---------------------------------------------------------------------------
42 // Empty the time clock and interpolators, thereby making the transf static
43 //---------------------------------------------------------------------------
45 {
46  beat_.clear();
47  interpolator_.clear();
48 }
49 
50 //---------------------------------------------------------------------------
51 // Linear interpolation on scalar values
52 //---------------------------------------------------------------------------
54  double v1,
55  int index,
56  double time) const
57 {
58  assert(index>=0 && (unsigned)index+1<beat_.size());
59  double t0=beat_[index];
60  double t1=beat_[index+1];
61 
62  return (v0*(t1-time)+v1*(time-t0))/(t1-t0);
63 }
64 
65 //---------------------------------------------------------------------------
66 // Linear interpolation on vnl_vectors
67 //---------------------------------------------------------------------------
70  const vnl_vector<double> &v1,
71  int index,
72  double time) const
73 {
74  int size=v0.size();
75  assert(index>=0 && (unsigned)index+1<beat_.size());
76  double t0=beat_[index];
77  double t1=beat_[index+1];
78 
79  double denominator=1/(t1-t0);
80  double dt1=(t1-time)*denominator;
81  double dt0=(time-t0)*denominator;
82 
83  vnl_vector<double> result(size);
84  for (int i=0;i<size;++i)
85  result.put(i,v0.get(i)*dt1+v1.get(i)*dt0);
86 
87  return result;
88 }
89 
90 //---------------------------------------------------------------------------
91 // Linear interpolation on vnl_matrices
92 //---------------------------------------------------------------------------
95  const vnl_matrix<double> &m1,
96  int index,
97  double time) const
98 {
99  int rows=m0.rows();
100  int cols=m0.cols();
101  assert(index>=0 && (unsigned)index+1<beat_.size());
102  double t0=beat_[index];
103  double t1=beat_[index+1];
104 
105  double denominator=1/(t1-t0);
106  double dt1=(t1-time)*denominator;
107  double dt0=(time-t0)*denominator;
108 
109  vnl_matrix<double> result(rows,cols);
110  for (int i=0;i<rows;++i)
111  for (int j=0;j<cols;++j)
112  result.put(i,j,m0.get(i,j)*dt1+m1.get(i,j)*dt0);
113 
114  return result;
115 }
116 
117 //---------------------------------------------------------------------------
118 // Linear interpolation on quaternions
119 //---------------------------------------------------------------------------
122  const vnl_quaternion<double> &v1,
123  int index,
124  double time) const
125 {
126  assert(index>=0 && (unsigned)index+1<beat_.size());
127  double t0=beat_[index];
128  double t1=beat_[index+1];
129  double t=(time-t0)/(t1-t0);
130 
131  double cosangle=dot_product(v0.as_ref(), v1.as_ref());
132  double angle=std::acos(cosangle);
133  double invsin=1/std::sin(angle);
134  double coef1=std::sin((1-t)*angle)*invsin;
135  double coef2=std::sin(t*angle)*invsin;
136 
137  return vnl_quaternion<double>(v0.x()*coef1+v1.x()*coef2,
138  v0.y()*coef1+v1.y()*coef2,
139  v0.z()*coef1+v1.z()*coef2,
140  v0.r()*coef1+v1.r()*coef2);
141 }
unsigned int cols() const
vnl_vector< double > lvi(const vnl_vector< double > &v0, const vnl_vector< double > &v1, int index, double time) const
Linear interpolation on vnl_vectors.
vnl_quaternion< double > lqi(const vnl_quaternion< double > &v0, const vnl_quaternion< double > &v1, int index, double time) const
Linear interpolation on quaternions.
double lsi(double v0, double v1, int index, double time) const
Linear interpolation on scalar values.
vnl_matrix< double > lmi(const vnl_matrix< double > &m0, const vnl_matrix< double > &m1, int index, double time) const
Linear interpolation on vnl_matrices.
VNL_EXPORT T dot_product(m const &, m const &)
std::vector< double > beat_
List of time clocks.
Transformation between 2 spatial coordinate systems.
size_t size() const
vnl_vector_ref< T > as_ref()
std::vector< vcsl_interpolator > interpolator_
VNL_EXPORT double angle(v const &, v const &)
double get(unsigned r, unsigned c) const
void set_static()
Empty the time clock and interpolators, thereby making the transf static.
void put(size_t i, double const &v)
int matching_interval(double time) const
Return the index of the beat inferior or equal to ‘time’.
bool valid_time(double time) const
Is ‘time’ between the two time bounds ?.
unsigned int rows() const
double get(size_t i) const
void put(unsigned r, unsigned c, double const &)