1 // This is core/vcsl/vcsl_scale.cxx
2 #include "vcsl_scale.h"
3 #include <cassert>
4 #ifdef _MSC_VER
5 #  include "vcl_msvc_warnings.h"
6 #endif
7 
8 //---------------------------------------------------------------------------
9 // Is `this' invertible at time `time'?
10 // REQUIRE: valid_time(time)
11 //---------------------------------------------------------------------------
12 bool
is_invertible(double time) const13 vcsl_scale::is_invertible(double time) const
14 {
15   // require
16   assert(valid_time(time));
17 
18   return ((this->duration() == 0) && (scale_[0] != 0.0)) || (scale_value(time) != 0.0);
19 }
20 
21 //---------------------------------------------------------------------------
22 // Set the scale value of a static scale
23 //---------------------------------------------------------------------------
24 void
set_static(double new_scale)25 vcsl_scale::set_static(double new_scale)
26 {
27   scale_.clear();
28   scale_.push_back(new_scale);
29   vcsl_spatial_transformation::set_static();
30 }
31 
32 //---------------------------------------------------------------------------
33 // Image of `v' by `this'
34 // REQUIRE: is_valid()
35 //---------------------------------------------------------------------------
36 vnl_vector<double>
execute(const vnl_vector<double> & v,double time) const37 vcsl_scale::execute(const vnl_vector<double> & v, double time) const
38 {
39   // require
40   assert(is_valid());
41 
42   double value = scale_value(time);
43   vnl_vector<double> result(v.size());
44   for (unsigned int i = 0; i < v.size(); ++i)
45     result.put(i, value * v.get(i));
46 
47   return result;
48 }
49 
50 //---------------------------------------------------------------------------
51 // Image of `v' by the inverse of `this'
52 // REQUIRE: is_valid()
53 // REQUIRE: is_invertible(time)
54 //---------------------------------------------------------------------------
55 vnl_vector<double>
inverse(const vnl_vector<double> & v,double time) const56 vcsl_scale::inverse(const vnl_vector<double> & v, double time) const
57 {
58   // require
59   assert(is_valid());
60   assert(is_invertible(time));
61 
62   double value = scale_value(time);
63   vnl_vector<double> result(v.size());
64   for (unsigned int i = 0; i < v.size(); ++i)
65     result.put(i, v.get(i) / value);
66 
67   return result;
68 }
69 
70 //---------------------------------------------------------------------------
71 // Compute the value of the parameter at time `time'
72 //---------------------------------------------------------------------------
73 double
scale_value(double time) const74 vcsl_scale::scale_value(double time) const
75 {
76   if (this->duration() == 0) // static
77     return scale_[0];
78   else
79   {
80     int i = matching_interval(time);
81     switch (interpolator_[i])
82     {
83       case vcsl_linear:
84         return lsi(scale_[i], scale_[i + 1], i, time);
85       case vcsl_cubic:
86         assert(!"vcsl_cubic net yet implemented");
87         break;
88       case vcsl_spline:
89         assert(!"vcsl_spline net yet implemented");
90         break;
91       default:
92         assert(!"This is impossible");
93         break;
94     }
95   }
96   return 0.0; // never reached if asserts are in effect
97 }
98