cgv
mfunc.h
1 #pragma once
2 
3 #include <cgv/math/vec.h>
4 
5 namespace cgv {
6  namespace math {
7 
14 template <typename X, typename T>
15 class mfunc
16 {
17 public:
23  virtual ~mfunc() {}
25  virtual unsigned get_nr_independent_variables() const = 0;
27  virtual T evaluate(const pnt_type& p) const = 0;
31  virtual vec_type evaluate_gradient(const pnt_type& p) const {
32  static X epsilon = (X)1e-5;
33  static X inv_2_eps = (X)(0.5/epsilon);
34  unsigned n = p.size();
35  vec_type g(n);
36  pnt_type q(p);
37  for (unsigned i=0; i<n; ++i) {
38  q(i) += epsilon;
39  g(i) = evaluate(q);
40  q(i) = p(i)-epsilon;
41  g(i) -= evaluate(q);
42  g(i) *= inv_2_eps;
43  q(i) = p(i);
44  }
45  return g;
46  }
47 };
48 
51 template <typename X, typename T>
52 class v2_func : public mfunc<X,T>
53 {
54 public:
56  unsigned int get_nr_independent_variables() const { return 2; }
57 };
58 
61 template <typename X, typename T>
62 class v3_func : public mfunc<X,T>
63 {
64 public:
66  unsigned int get_nr_independent_variables() const { return 3; }
67 };
68 
69  }
70 }
cgv::math::mfunc::vec_type
cgv::math::vec< X > vec_type
vectors must have get_nr_independent_variables() components
Definition: mfunc.h:21
cgv::math::mfunc::pnt_type
cgv::math::vec< X > pnt_type
points must have get_nr_independent_variables() components
Definition: mfunc.h:19
cgv::math::vec
A column vector class.
Definition: fvec.h:13
cgv::math::vec::size
unsigned size() const
number of elements
Definition: vec.h:97
cgv::math::v2_func::get_nr_independent_variables
unsigned int get_nr_independent_variables() const
returns 2
Definition: mfunc.h:56
cgv::math::v3_func
Definition: mfunc.h:63
cgv::math::mfunc::get_nr_independent_variables
virtual unsigned get_nr_independent_variables() const =0
return the number of independent variables that are mapped by the function
cgv::math::v2_func
Definition: mfunc.h:53
cgv::math::mfunc::~mfunc
virtual ~mfunc()
virtual destructor
Definition: mfunc.h:23
cgv::math::mfunc
Definition: mfunc.h:16
cgv::math::mfunc::evaluate
virtual T evaluate(const pnt_type &p) const =0
interface for evaluation of the multivariate function
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::math::mfunc::evaluate_gradient
virtual vec_type evaluate_gradient(const pnt_type &p) const
Definition: mfunc.h:31
cgv::math::v3_func::get_nr_independent_variables
unsigned int get_nr_independent_variables() const
returns 3
Definition: mfunc.h:66