ISeeML  1.0
ArrayPaths.hpp
1 //
2 // Definition of iSeeML::rob::ArrayPaths, example of compound robotic
3 // path made of an array of paths.
4 //
5 
6 // To only include this definition once
7 #ifndef ISEEML_ARRAY_PATHS_HH
8 #define ISEEML_ARRAY_PATHS_HH
9 
10 // This class needs the definition of iSeeML::rob::CompoundPath.
11 #include <iSeeML/rob/CompoundPath.hpp>
12 
26  class CoupleTypePointer {
27  public:
28  bool isBasicPath;
29  iSeeML::rob::Path *path;
30  CoupleTypePointer() : isBasicPath(true), path(NULL) {}
31  CoupleTypePointer(const bool basic, iSeeML::rob::Path *ptr)
32  : isBasicPath(basic), path(ptr) {}
33  int nbPieces() const {
34  return ( isBasicPath ? 1 :
35  ( (iSeeML::rob::CompoundPath*)(path) )->nbPieces() );
36  } // end of int nbPieces() const
37  }; // end of class CoupleTypePointer
38 
39  // === Object's Fields ==========================================
40 
42  CoupleTypePointer *array;
43 
45  const int nb_paths;
46 
47 public:
52  static const string ClassName;
53 
54  // === Constructors and Destructors =============================
55 
59  ArrayPaths(const int n)
60  : array(new CoupleTypePointer[n]), nb_paths(n) {}
61 
64  for(int idx = 0; idx < nb_paths; idx++)
65  free(array[idx].path);
66  free(array);
67  } // end of ~ArrayPaths()
68 
74  void setPath(const int index,
75  const bool isBasic, iSeeML::rob::Path *path) {
76 #ifdef ISEEML_CHECK_COMP_PATH_PRECOND
77  if ( (index < 0) || (index >= nb_paths) )
78  cerr << ClassName << "::setPath: incorrect index "
79  << index << " (not in 0-" << nb_paths - 1 << ")!" << endl;
80 #endif
81  array[index] = CoupleTypePointer(isBasic, path);
82  } // end of void setPath(const int, const bool, iSeeML::rob::Path*)
83 
84  // === Description Methods ======================================
85 
86  // Cf Object::clone()
87  iSeeML::Object& clone() const {
88  ArrayPaths *new_one = new ArrayPaths(nb_paths);
89  for(int idx = 0; idx < nb_paths; idx++)
90  new_one->setPath( idx, array[idx].isBasicPath,
92  (& array[idx].path->clone() ) );
93  return *( (iSeeML::Object*)(new_one) );
94  } // end of iSeeML::Object& clone() const
95 
96  // Cf CompoundPath::nbPieces()
97  int nbPieces() const {
98  int nb, idx;
99  for(nb = 0, idx = 0; idx < nb_paths; idx++)
100  nb += array[idx].nbPieces();
101  return nb;
102  } // end of int nbPieces() const
103 
104 protected:
105  // === Various Methods ==========================================
106 
107  // Cf CompoundPath::_piece
108  iSeeML::rob::BasicPath& _piece(const int index) const {
109  int idx = index, i = 0, pieces;
110  bool step;
111  do {
112  pieces = array[i].nbPieces();
113  step = (idx > pieces);
114  if (step) { idx -= pieces; i++; }
115  } while ( (i < nb_paths) && (step) );
116  // if index was too big, return the last piece
117  if (i >= nb_paths) { idx = pieces ; i = nb_paths - 1; }
118  return ( array[i].isBasicPath ?
119  *( (iSeeML::rob::BasicPath*) (array[i].path) ) :
120  ( (iSeeML::rob::CompoundPath*) (array[i].path)
121  )->piece(idx) );
122  } // end of iSeeML::rob::BasicPath& _piece(const int)
123 
124 }; // end of class iSeeML::rob::ArrayPaths
125 
126 #endif // end of definition
This class defines compound (or complex, or composed) paths, which are made of a set of basic paths...
Definition: CompoundPath.hpp:28
This class defines superclass of basic paths, from which compound paths can be built.
Definition: BasicPath.hpp:21
This class is the base class of ISeeML: every class of ISeeML inherits this one.
Definition: Object.hpp:47
static const string ClassName
The class name is public, as this class can be instanced.
Definition: ArrayPaths.hpp:52
int nbPieces() const
Description method, giving the number of pieces of the path.
Definition: ArrayPaths.hpp:97
~ArrayPaths()
The destructor frees all the paths and the array.
Definition: ArrayPaths.hpp:63
iSeeML::rob::BasicPath & _piece(const int index) const
Virtual method returning the basic path of given index in the list of which this compound path is mad...
Definition: ArrayPaths.hpp:108
void setPath(const int index, const bool isBasic, iSeeML::rob::Path *path)
Add a given (dynamically allocated) path to the array.
Definition: ArrayPaths.hpp:74
This class is the base class of all robotic paths in ISeeML.
Definition: Path.hpp:51
ArrayPaths(const int n)
The main constructor only needs a number of paths.
Definition: ArrayPaths.hpp:59
iSeeML::Object & clone() const
Description method, giving a copy of the current object.
Definition: ArrayPaths.hpp:87
This class defines compound (or complex, or composed) paths, which are made of a set of basic paths...
Definition: ArrayPaths.hpp:25
const iSeeML::rob::BasicPath & piece(const int index) const
Description method, returning the (constant) basic path of given index in the list of which this comp...
Definition: CompoundPath.hpp:114