ldpk
ldpk_generic_radial_distortion.h
Go to the documentation of this file.
1 #ifndef ldpk_generic_radial_distortion_sdv
2 #define ldpk_generic_radial_distortion_sdv
3 
6 
8 #include <ldpk/ldpk_power_ct.h>
9 #include <iostream>
10 
11 namespace ldpk
12  {
16  template <class VEC2,class MAT2,int N>
18  {
19  public:
21  typedef VEC2 vec2_type;
22  typedef MAT2 mat2_type;
23  private:
24 // The coefficients
25  double _c[N];
26  public:
28  {
29  for(int i = 0;i < N;++i) _c[i] = 0.0;
30  }
32  double get_coeff(int i) const
33  {
35  return _c[i];
36  }
38  void set_coeff(int i,double q)
39  {
41  _c[i] = q;
42  }
44  vec2_type operator()(const vec2_type& p_dn) const
45  {
46 // Compute powers of r^2: 1,r^2,r^4,...,r^(2N)
47  double r2pow[N + 1];
48  double q(1.0),r2 = dotsq(p_dn);
49  power_ct<double,N + 1>().build(r2,r2pow);
50 
51  for(int i = 0;i < N;++i)
52  {
53  q += _c[i] * r2pow[i + 1];
54  }
55  return vec2_type(p_dn[0] * q,p_dn[1] * q);
56  }
59  mat2_type jacobi(const vec2_type& p_dn) const
60  {
61 // Compute powers of r^2: 1,r^2,r^4,...,r^(2N)
62  double r2pow[N + 1];
63  double r2 = dotsq(p_dn);
64  power_ct<double,N + 1>().build(r2,r2pow);
65 
66  double qa(1.0),qb(0.0);
67  for(int i = 0;i < N;++i)
68  {
69  qa += _c[i] * r2pow[i + 1];
70  qb += _c[i] * (2 * i + 2) * r2pow[i];
71  }
72  return mat2_type(qa) + mat2_type(tensq(p_dn) * qb);
73  }
76  void derive(double* dg,int n_parameters,const vec2_type& p_dn) const
77  {
78  int size = 2 * n_parameters;
79 // Compute powers of r^2: 1,r^2,r^4,...,r^(2N)
80  double r2pow[N + 1];
81  double r2 = dotsq(p_dn);
82  power_ct<double,N + 1>().build(r2,r2pow);
83 
84  int k = 0;
85  for(int i = 0;i < N;++i)
86  {
87  dg[k++] = p_dn[0] * r2pow[i + 1];
88  dg[k++] = p_dn[1] * r2pow[i + 1];
89 // We calculate the first n derivatives, where n is the size of g.
90  if(k == size) return;
91  }
92 // Unreachable
93  std::cerr << "example_radial_distortion: n_parameters out of range" << std::endl;
94  }
95  std::ostream& out(std::ostream& cout) const
96  {
97  int p = cout.precision();
98  cout.precision(5);
99  for(int i = 0;i < N;++i)
100  {
101  cout << " C(" << 2 * i + 2 << "): " << std::right << std::fixed << _c[i] << "\n";
102  }
103  cout.precision(p);
104  return cout;
105  }
106  };
107  }
108 
109 #endif
mat2d tensq(const vec2d &a)
Tensor (dyadic) product square.
Definition: ldpk_vec2d.h:186
Base class for a distortion model with N parameters. You may find it useful to derive your own distor...
Definition: ldpk_generic_distortion_base.h:21
void check_range(int i) const
A derived class may check if the index is valid.
Definition: ldpk_generic_distortion_base.h:43
vec2_type operator()(const vec2_type &p_dn) const
Remove distortion. p_dn is a point in diagonally normalized coordinates.
Definition: ldpk_generic_radial_distortion.h:44
Base class for distortion models.
A polynomial model with N coefficients for radially symmetric lenses. This class contains the model f...
Definition: ldpk_generic_radial_distortion.h:17
Computing N powers of a given number, where N is a compile-time constant.
Definition: ldpk_power_ct.h:33
mat2_type jacobi(const vec2_type &p_dn) const
Analytic version of the Jacobi-matrix, about 2.5 times faster than the base class version which uses ...
Definition: ldpk_generic_radial_distortion.h:59
void derive(double *dg, int n_parameters, const vec2_type &p_dn) const
Derivative with respect to distortion coefficients. dg points to an array with 2 * N Elements...
Definition: ldpk_generic_radial_distortion.h:76
void set_coeff(int i, double q)
Set coefficient c[i], 0 <= i < N.
Definition: ldpk_generic_radial_distortion.h:38
std::ostream & out(std::ostream &cout) const
The derived class implements a method for printing values inside 3DE4's matrix tool dialog...
Definition: ldpk_generic_radial_distortion.h:95
double get_coeff(int i) const
Get coefficient c[i], 0 <= i < N (i.e. coefficient power r^(2i))
Definition: ldpk_generic_radial_distortion.h:32