KrisLibrary  1.0.0
Tabular.h
1 #ifndef TABULAR_CSPACE_H
2 #define TABULAR_CSPACE_H
3 
5 #include <KrisLibrary/meshing/VolumeGrid.h>
6 #include <KrisLibrary/meshing/AreaGrid.h>
7 #include "CSpace.h"
8 #include "CSetHelpers.h"
9 #include "Objective.h"
10 using namespace Math3D;
11 
24 template <class T>
25 class Table1D
26 {
27 public:
28  Table1D(int n,Real a,Real b,
29  int dim=0);
30  Table1D(const std::vector<T>& values,Real a,Real b,
31  int dim=0);
32  T Lookup(Real x) const;
33  int Index(Real x,Real* u=NULL) const;
34  std::pair<Real,Real> Cell(int index) const;
35  inline T Lookup(const Config& x) const { return Lookup(x[dim]); }
36  inline int Index(const Config& x,Real* u=NULL) const { return Index(x[dim],u); }
37 
38  std::vector<T> values;
39  Real a,b;
40  int dim;
41 };
42 
46 class Tabular1DSet : public BoxSet, public Table1D<bool>
47 {
48 public:
49  Tabular1DSet(const std::vector<bool>& valid,Real a,Real b);
50  Tabular1DSet(int n,Real a,Real b);
51  virtual ~Tabular1DSet() {}
52  virtual int NumDimensions() const { return 1; }
53  virtual bool Contains(const Config& x);
54  virtual bool Project(Config& x);
55  virtual bool IsSampleable() const { return true; }
56  virtual void Sample(Config& x);
57 
58  bool calculatedValidIndices;
59  std::vector<int> validIndices;
60 };
61 
67 class Tabular2DSet : public BoxSet, public Meshing::AreaGridTemplate<bool>
68 {
69 public:
70  Tabular2DSet(const Array2D<bool>& valid,const Vector2& bmin,const Vector2& bmax);
71  Tabular2DSet(int m,int n,const Vector2& bmin,const Vector2& bmax);
72  virtual ~Tabular2DSet() {}
73  virtual int NumDimensions() const { return 2; }
74  virtual bool Contains(const Config& x);
75  virtual bool Project(Config& x);
76  virtual bool IsSampleable() const { return true; }
77  virtual void Sample(Config& x);
78 
79  bool calculatedValidIndices;
80  std::vector<IntPair> validIndices;
81 };
82 
88 class Tabular3DSet : public BoxSet, public Meshing::VolumeGridTemplate<bool>
89 {
90 public:
91  Tabular3DSet(const Array3D<bool>& valid,const Vector3& bmin,const Vector3& bmax);
92  Tabular3DSet(int m,int n, int p,const Vector3& bmin,const Vector3& bmax);
93  virtual ~Tabular3DSet() {}
94  virtual int NumDimensions() const { return 2; }
95  virtual bool Contains(const Config& x);
96  virtual bool Project(Config& x);
97  virtual bool IsSampleable() const { return true; }
98  virtual void Sample(Config& x);
99 
100  bool calculatedValidIndices;
101  std::vector<IntTriple> validIndices;
102 };
103 
111 class Tabular1DLevelSet : public BoxSet, Table1D<Real>
112 {
113 public:
114  Tabular1DLevelSet(const std::vector<Real>& f,Real a,Real b,Real threshold=0);
115  virtual ~Tabular1DLevelSet() {}
116  virtual int NumDimensions() const { return 1; }
117  virtual bool Contains(const Config& x);
118  virtual bool Project(Config& x);
119 
120  Real threshold;
121 };
122 
132 class Tabular2DLevelSet : public BoxSet
133 {
134 public:
135  Tabular2DLevelSet(const Array2D<Real>& f,const Vector2& bmin,const Vector2& bmax,Real threshold=0);
136  virtual ~Tabular2DLevelSet() {}
137  virtual int NumDimensions() const { return 2; }
138  virtual bool Contains(const Config& x);
139  virtual bool Project(Config& x);
140 
142  Real threshold;
143 };
144 
154 class Tabular3DLevelSet : public BoxSet
155 {
156 public:
157  Tabular3DLevelSet(const Array3D<Real>& f,const Vector3& bmin,const Vector3& bmax,Real threshold=0);
158  virtual ~Tabular3DLevelSet() {}
159  virtual int NumDimensions() const { return 3; }
160  virtual bool Contains(const Config& x);
161  virtual bool Project(Config& x);
162 
164  Real threshold;
165 };
166 
167 
168 
187 {
188 public:
189  Tabular1DObjective(const std::vector<Real>& differentialCosts,const std::vector<Real>& terminalCosts,
190  Real a,Real b,
191  int dim);
192  virtual ~Tabular1DObjective() {}
193  virtual const char* TypeString() { return "tabular1d"; }
194  virtual std::string Description();
195  virtual bool PathInvariant() const { return differentialCosts.values.empty(); }
196  virtual Real IncrementalCost(const Interpolator* path);
197  virtual Real TerminalCost(const Vector& qend);
198 
199  Table1D<Real> differentialCosts,terminalCosts;
200  int dim;
201 };
202 
218 {
219 public:
220  Tabular2DObjective(const Array2D<Real>& differentialCosts,const Array2D<Real>& terminalCosts,
221  const Math3D::Vector2& bmin,const Math3D::Vector2& bmax,
222  int dim0=0,int dim1=1);
223  virtual ~Tabular2DObjective() {}
224  virtual const char* TypeString() { return "tabular2d"; }
225  virtual std::string Description();
226  virtual bool PathInvariant() const { return differentialCosts.value.empty(); }
227  virtual Real IncrementalCost(const Interpolator* path);
228  virtual Real TerminalCost(const Vector& qend);
229 
230  Meshing::AreaGridTemplate<Real> differentialCosts,terminalCosts;
231  int dim0,dim1;
232 };
233 
250 {
251 public:
252  Tabular3DObjective(const Array3D<Real>& differentialCosts,const Array3D<Real>& terminalCosts,
253  const Math3D::Vector3& bmin,const Math3D::Vector3& bmax,
254  int dim0=0,int dim1=1,int dim2=2);
255  virtual ~Tabular3DObjective() {}
256  virtual const char* TypeString() { return "tabular23"; }
257  virtual std::string Description();
258  virtual bool PathInvariant() const { return differentialCosts.value.empty(); }
259  virtual Real IncrementalCost(const Interpolator* path);
260  virtual Real TerminalCost(const Vector& qend);
261 
262  Meshing::VolumeGridTemplate<Real> differentialCosts,terminalCosts;
263  int dim0,dim1,dim2;
264 };
265 
266 
267 
268 
269 
270 
271 
272 template <class T>
273 Table1D<T>::Table1D(int n,Real _a,Real _b,int _dim)
274 :values(n,T(0)),a(_a),b(_b),dim(_dim)
275 {}
276 
277 template <class T>
278 Table1D<T>::Table1D(const std::vector<T>& _values,Real _a,Real _b,int _dim)
279 :values(_values),a(_a),b(_b),dim(_dim)
280 {}
281 
282 template <class T>
283 T Table1D<T>::Lookup(Real x) const
284 {
285  Real u;
286  int i=Index(x,&u);
287  //linearly interpolate by default
288  if(u == 0)
289  return values[i];
290  else if(u > 0)
291  return values[i] + u*(values[i+1]-values[i]);
292  else
293  return values[i] + (-u)*(values[i-1]+values[i]);
294 }
295 
296 template <class T>
297 std::pair<Real,Real> Table1D<T>::Cell(int index) const
298 {
299  return std::make_pair(a + index*(b-a)/values.size(),a + (index+1)*(b-a)/values.size());
300 }
301 
302 template <class T>
303 int Table1D<T>::Index(Real x,Real* u) const
304 {
305  if(values.size() <= 1) {
306  if(u) u[0]=0;
307  return 0;
308  }
309  if(x <= a) {
310  if(u) u[0]=0;
311  return 0;
312  }
313  if(x >= b) {
314  if(u) u[0]=0;
315  return (int)values.size()-1;
316  }
317  Real xscale = (x-a)/(b-a)*(values.size()-1);
318  Real xbase = Floor(xscale);
319  if(u) u[0] = xscale-xbase-0.5;
320  return (int)xbase;
321 }
322 
323 //template specialization
324 template <>
325 bool Table1D<bool>::Lookup(Real x) const
326 {
327  int i=Index(x);
328  return values[i];
329 }
330 
331 #endif
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:137
virtual bool IsSampleable() const
If true, the Sample() function is implemented.
Definition: Tabular.h:76
virtual bool IsSampleable() const
If true, the Sample() function is implemented.
Definition: Tabular.h:55
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:116
A cost that penalizes via a 1D table lookup of a single index.
Definition: Tabular.h:186
Real Sample(const Interval &s)
Uniformly samples the given intervals.
Definition: sample.cpp:116
A 3D vector class.
Definition: math3d/primitives.h:136
virtual bool PathInvariant() const
Subclasses: planners may exploit path-invariant costs for faster performance.
Definition: Tabular.h:195
virtual bool PathInvariant() const
Subclasses: planners may exploit path-invariant costs for faster performance.
Definition: Tabular.h:258
A 2D array over an axis-aligned 2D box, containing Real values.
Definition: AreaGrid.h:58
Vector Config
an alias for Vector
Definition: RobotKinematics3D.h:14
virtual bool PathInvariant() const
Subclasses: planners may exploit path-invariant costs for faster performance.
Definition: Tabular.h:226
virtual const char * TypeString()
Subclasses: return an identifier for this goal type.
Definition: Tabular.h:193
Class declarations for useful 3D math types.
An integer tuple class.
Definition: IntTuple.h:14
A 2D level set f(x) <= threshold.
Definition: Tabular.h:132
A 3D level set f(x) <= threshold.
Definition: Tabular.h:154
virtual bool IsSampleable() const
If true, the Sample() function is implemented.
Definition: Tabular.h:97
A 1D level set f(x) <= threshold.
Definition: Tabular.h:111
A cost that penalizes via a 2D table lookup of a single index.
Definition: Tabular.h:217
A 2D set that includes some marked regions on a grid.
Definition: Tabular.h:67
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:13
A base class for all 1D interpolators.
Definition: Interpolator.h:10
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:73
virtual const char * TypeString()
Subclasses: return an identifier for this goal type.
Definition: Tabular.h:256
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:159
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:52
A 1D set that includes some marked regions on a grid.
Definition: Tabular.h:46
virtual int NumDimensions() const
Returns the number of dimensions this accepts (-1) for all dimensions.
Definition: Tabular.h:94
A 2D vector class.
Definition: math3d/primitives.h:41
A 3D array over an axis-aligned 3D volume, containing Real values.
Definition: AnyGeometry.h:11
A 1D tabular function interpolated over a grid.
Definition: Tabular.h:25
A cost that penalizes via a 3D table lookup of a single index.
Definition: Tabular.h:249
A 3D set that includes some marked regions on a grid.
Definition: Tabular.h:88
Definition: CSetHelpers.h:10
virtual const char * TypeString()
Subclasses: return an identifier for this goal type.
Definition: Tabular.h:224
A base class for objective functionals of the form J[x,u] = sum_0^N-1 L(xi,ui) dt + Phi(xN) ...
Definition: Objective.h:18