Templates -- Meow  1.2.11
A C++ template contains kinds of interesting classes and functions
Transformations.h
Go to the documentation of this file.
1 #ifndef math_Transformations_H__
2 #define math_Transformations_H__
3 
4 #include "Transformation.h"
5 #include "Matrix.h"
6 #include "utility.h"
7 #include "../Self.h"
8 
9 #include <cstdlib>
10 
11 namespace meow {
12 
49 template<class Scalar>
50 class BallProjection: public Transformation<Scalar> {
51 private:
52  struct Myself {
53  size_t dimension_;
54  Scalar radius_;
55 
56  Myself(size_t d): dimension_(1), radius_(1) {
57  }
58 
59  Myself(size_t d, Scalar const& r): dimension_(d), radius_(r) {
60  }
61 
62  Myself(Myself const& m): dimension_(m.dimension_), radius_(m.radius_) {
63  }
64  };
65 
66  Self<Myself> const self;
67 public:
73  self(b.self, Self<Myself>::COPY_FROM) {
74  }
75 
80  BallProjection(size_t d): Transformation<Scalar>(d, 1, d, 1, 1),
81  self(Myself(d)) {
82  radius(1);
83  }
84 
90  BallProjection(size_t d, Scalar const& r): Transformation<Scalar>(d,1,d,1,1),
91  self(Myself(d, r)) {
92  radius(r);
93  }
94 
102  copyFrom(b);
103  return *this;
104  }
105 
113  referenceFrom(b);
114  return *this;
115  }
116 
120  Scalar parameter(size_t i) const {
121  return radius();
122  }
123 
127  Scalar parameter(size_t i, Scalar const& s) {
128  return radius(s);
129  }
130 
134  Scalar radius() const {
135  return self->radius_;
136  }
137 
144  Scalar radius(Scalar const& r) {
145  self()->radius_ = r;
146  return radius();
147  }
148 
152  size_t dimension() const {
153  return self->dimension_;
154  }
155 
156 
173  Matrix<Scalar> ret(x);
174  for (size_t c = 0, C = ret.cols(); c < C; c++) {
175  Scalar sum(0);
176  for (size_t i = 0; i < self->dimension_; i++) {
177  sum = sum + squ(ret(i, c));
178  }
179  Scalar len(sqrt(double(sum)));
180  for (size_t i = 0; i < self->dimension_; i++) {
181  ret(i, c, ret(i, c) * radius() / len);
182  }
183  }
184  return ret;
185  }
186 
216  Scalar sum(0);
217  for(size_t i = 0, I = dimension(); i < I; ++i)
218  sum = sum + squ(x(i, 0));
219  Scalar len(sqrt(double(sum)));
220  Matrix<Scalar> ret(dimension(), dimension(), Scalar(0.0));
221  for(size_t i = 0, I = dimension(); i < I; ++i)
222  for(size_t j = 0; j < I; ++j)
223  if (i == j) {
224  ret(i, j, radius() * (squ(len) - squ(x(i, 0))) / cub(len));
225  }
226  else {
227  ret(i, j, radius() * (-x(i, 0) * x(j, 0) / cub(len)));
228  }
229  return ret;
230  }
231 
260  Matrix<Scalar> jacobian(Matrix<Scalar> const& x, size_t i) const {
261  Matrix<Scalar> ret(dimension(), 1, Scalar(0.0));
262  Scalar sum(0);
263  for(size_t i = 0, I = dimension(); i < I; i++) {
264  sum = sum + squ(x(i, 0));
265  }
266  return ret / Scalar(sqrt(double(sum)));
267  }
268 
273  return copyFrom(b);
274  }
275 
280  return transformate(v);
281  }
282 };
283 
284 
324 template<class Scalar>
325 class PhotoProjection: public Transformation<Scalar> {
326 private:
327  struct Myself {
328  Scalar focal_;
329  size_t dimension_;
330 
331  Myself() {
332  }
333 
334  Myself(size_t d, Scalar f): focal_(f), dimension_(d) {
335  }
336 
337  Myself(Myself const& b): focal_(b.focal_), dimension_(b.dimension_) {
338  }
339 
340  ~Myself() {
341  }
342  };
343 
344  Self<Myself> const self;
345 public:
350  Transformation<Scalar>(dimension, 1, dimension, 1, 1),
351  self(Myself(dimension, 1)) {
352  }
353 
357  PhotoProjection(size_t dimension, Scalar const& f):
358  Transformation<Scalar>(dimension, 1, dimension, 1, 1),
359  self(Myself(dimension, f)) {
360  }
361 
366  self(p.self, Self<Myself>::COPY_FROM) {
367  }
368 
376  self().copyFrom(b.self);
377  return *this;
378  }
379 
387  self().referenceFrom(b.self);
388  return *this;
389  }
390 
394  Scalar parameter(size_t i) const {
395  return focal();
396  }
397 
401  Scalar parameter(size_t i, Scalar const& s){
402  return focal(s);
403  }
404 
409  Scalar focal() const {
410  return self->focal_;
411  }
412 
419  Scalar focal(Scalar const& f){
420  self()->focal_ = f;
421  return focal();
422  }
423 
427  size_t dimension() const {
428  return self->dimension_;
429  }
430 
449  Matrix<Scalar> ret(x);
450  for (size_t c = 0, C = ret.cols(); c < C; c++) {
451  for (size_t i = 0, I = dimension(); i < I; ++i) {
452  ret(i, c, -ret(i, c) * focal() / ret(I - 1, c));
453  }
454  }
455  return ret;
456  }
457 
488  Matrix<Scalar> ret(dimension(), dimension(), Scalar(0.0));
489  for(ssize_t i = 0, I = (ssize_t)dimension() - 1; i < I; i++){
490  ret(i, i, -focal() / x(I, 0) );
491  ret(i, dimension() - 1, focal() / squ(x(I, 0)));
492  }
493  return ret;
494  }
495 
525  Matrix<Scalar> jacobian(Matrix<Scalar> const& x, size_t i) const{
526  Matrix<Scalar> ret(dimension(), 1, Scalar(0.0));
527  for(size_t i = 0, I = dimension(); i < I; ++i) {
528  ret(i, 0, -x(i, 0) / x(I - 1, 0));
529  }
530  return ret;
531  }
532 
537  return copyFrom(b);
538  }
539 
544  return transformate(v);
545  }
546 };
547 
548 } // meow
549 
550 #endif // Transformations_H__
PhotoProjection & operator=(PhotoProjection const &b)
Same as copyFrom(b)
Matrix< Scalar > jacobian(Matrix< Scalar > const &x) const
Return the jacobian matrix (derivate by the input vector) of this projection.
Scalar parameter(size_t i) const
Same as focal()
PhotoProjection & copyFrom(PhotoProjection const &b)
Matrix< Scalar > jacobian(Matrix< Scalar > const &x, size_t i) const
Return the jacobian matrix (derivate by radius) of this projection.
BallProjection(BallProjection const &b)
BallProjection & copyFrom(BallProjection const &b)
Copy settings from another one.
size_t cols() const
Return number of cols.
Definition: Matrix.h:134
PhotoProjection & referenceFrom(PhotoProjection const &b)
BallProjection(size_t d, Scalar const &r)
PhotoProjection(PhotoProjection const &p)
Scalar parameter(size_t i) const
same as radius()
size_t dimension() const
Get the dimension of this projection.
BallProjection & referenceFrom(BallProjection const &b)
Reference settings from another one.
A ball projection is to project the given vector to a hyper-sphere.
Scalar focal() const
Get the focal length.
Matrix< Scalar > operator()(Matrix< Scalar > const &v) const
Same as transformate(v)
Matrix< Scalar > transformate(Matrix< Scalar > const &x) const
Project the input vector(s) onto the plane.
PhotoProjection(size_t dimension)
Scalar radius(Scalar const &r)
Setup the radius.
Scalar parameter(size_t i, Scalar const &s)
Same as focal(s)
Scalar focal(Scalar const &f)
Set the focal length.
Matrix< Scalar > jacobian(Matrix< Scalar > const &x) const
Return the jacobian matrix (derivate by the input vector) of this projection.
Matrix< Scalar > operator()(Matrix< Scalar > const &v) const
Same as transformate(v)
Transformation & copyFrom(Transformation const &b)
Copy from the specified one.
T cub(T const &x)
x*x*x
Definition: utility.h:85
Transformation & referenceFrom(Transformation const &b)
reference from the specified one
size_t dimension() const
Get the dimension of this projection.
Matrix< Scalar > transformate(Matrix< Scalar > const &x) const
Project the input vector(s) onto the hyper-sphere and return it.
A photo projection is a kind of transformation that project point/vector to a flat photo...
A base class for implementing kinds of transformations.
PhotoProjection(size_t dimension, Scalar const &f)
Matrix< Scalar > jacobian(Matrix< Scalar > const &x, size_t i) const
Return the jacobian matrix (derivate by the focus length) of this projection.
BallProjection & operator=(BallProjection const &b)
Same as copyFrom(b)
Scalar radius() const
Return the value of the radius.
T squ(T const &x)
x*x
Definition: utility.h:77
Scalar parameter(size_t i, Scalar const &s)
same as radius(s)