CLAM-Development  1.4.0
Segmentation.hxx
Go to the documentation of this file.
1 #ifndef Segmentation_hxx
2 #define Segmentation_hxx
3 
4 #include <vector>
5 #include <algorithm>
6 #include <iterator>
7 #include <cmath>
8 #include <sstream>
9 #include "Array.hxx"
10 #include "XMLAdapter.hxx"
11 
12 namespace CLAM
13 {
14  class Segmentation : public Component
15  {
16  public:
17  class InsertedOutOfBounds : public std::exception
18  {
19  public:
20  const char * what() const throw () { return "Segmentation point inserted out of limits";}
21  };
22  typedef std::vector<double> TimePositions;
23  public:
25  : _current(0)
26  , _maxPosition(0)
27  {
28  }
30  : _current(0)
31  , _maxPosition(maxPosition)
32  {
33  }
34  virtual ~Segmentation();
38  virtual unsigned insert(double timePosition)=0;
45  virtual void remove(unsigned segment)=0;
52  virtual unsigned pickOffset(double timePosition, double tolerance) const=0;
59  virtual unsigned pickOnset(double timePosition, double tolerance) const=0;
63  virtual unsigned pickSegmentBody(double timePosition) const=0;
69  virtual void dragOnset(unsigned segment, double newTimePosition)=0;
75  virtual void dragOffset(unsigned segment, double newTimePosition)=0;
76 
77  virtual void fillArray(DataArray& segmentation) const =0;
81  virtual void takeArray(const TData * begin, const TData * end) =0;
82 
83  const char * GetClassName() const { return "Segmentation"; }
84 
85  void StoreOn(Storage & storage) const
86  {
87  XMLAdapter<double> adapter(_maxPosition, "max", false);
88  storage.Store(adapter);
89  CLAM::DataArray array;
90  fillArray(array);
91  array.StoreOn(storage);
92  }
93 
94  void LoadFrom(Storage & storage)
95  {
96  double newmaxPosition;
97  XMLAdapter<double> adapter(newmaxPosition, "max", false);
98  if(storage.Load(adapter))
99  {
100  maxPosition(newmaxPosition);
101  }
102  else {
103  //when the maxPos is not present, set it to a large number
104  maxPosition(100000);
105  }
106  DataArray array;
107  array.LoadFrom(storage);
108  const unsigned size = array.Size();
109  const TData* ptr=array.GetPtr();
110  takeArray(ptr, ptr+size);
111 
112  }
113 
114  void select(unsigned segment)
115  {
116  _selection[segment]=true;
117  }
118  void deselect(unsigned segment)
119  {
120  _selection[segment]=false;
121  }
123  {
124  for (unsigned i=0; i<_selection.size(); i++)
125  _selection[i]=false;
126  }
127 
131  std::string boundsAsString() const
132  {
133  std::ostringstream os;
134  for (unsigned i=0; i<_offsets.size(); i++)
135  {
136  if (_selection[i]) os << "+";
137  os << "(" << _onsets[i] << "," << _offsets[i] << ") ";
138  }
139  return os.str();
140  }
141 
145  const TimePositions & onsets() const
146  {
147  return _onsets;
148  }
152  const TimePositions & offsets() const
153  {
154  return _offsets;
155  }
159  const std::vector<std::string> & labels() const
160  {
161  return _labels;
162  }
166  void setLabel(unsigned segment, std::string label)
167  {
168  if (segment>=_labels.size()) return; // Invalid segment
169  _labels[segment] = label;
170  }
174  const std::vector<bool> & selections() const
175  {
176  return _selection;
177  }
181  unsigned current() const
182  {
183  return _current;
184  }
188  void current(unsigned index)
189  {
190  if (index>=_onsets.size()) return;
191  _current = index;
192  }
193  double maxPosition() const
194  {
195  return _maxPosition;
196  }
197  virtual void maxPosition(double maxPosition)
198  {
200  }
201  void xUnits(const std::string & units)
202  {
203  _xUnits=units;
204  }
205  const std::string & xUnits() const
206  {
207  return _xUnits;
208  }
209  protected:
212  std::vector<std::string> _labels;
213  std::vector<bool> _selection;
214  unsigned _current;
215  double _maxPosition;
216  std::string _xUnits;
217  };
218 
219 }
220 
221 
222 
223 #endif//Segmentation_hxx
224