CLAM-Development
1.1
|
00001 #ifndef Segmentation_hxx 00002 #define Segmentation_hxx 00003 00004 #include <vector> 00005 #include <algorithm> 00006 #include <iterator> 00007 #include <cmath> 00008 #include <sstream> 00009 #include "Array.hxx" 00010 #include "XMLAdapter.hxx" 00011 00012 namespace CLAM 00013 { 00014 class Segmentation : public Component 00015 { 00016 public: 00017 class InsertedOutOfBounds : public std::exception 00018 { 00019 public: 00020 const char * what() const throw () { return "Segmentation point inserted out of limits";} 00021 }; 00022 typedef std::vector<double> TimePositions; 00023 public: 00024 Segmentation() 00025 : _current(0) 00026 , _maxPosition(0) 00027 { 00028 } 00029 Segmentation(double maxPosition) 00030 : _current(0) 00031 , _maxPosition(maxPosition) 00032 { 00033 } 00034 virtual ~Segmentation(); 00038 virtual unsigned insert(double timePosition)=0; 00045 virtual void remove(unsigned segment)=0; 00052 virtual unsigned pickOffset(double timePosition, double tolerance) const=0; 00059 virtual unsigned pickOnset(double timePosition, double tolerance) const=0; 00063 virtual unsigned pickSegmentBody(double timePosition) const=0; 00069 virtual void dragOnset(unsigned segment, double newTimePosition)=0; 00075 virtual void dragOffset(unsigned segment, double newTimePosition)=0; 00076 00077 virtual void fillArray(DataArray& segmentation) const =0; 00081 virtual void takeArray(const TData * begin, const TData * end) =0; 00082 00083 const char * GetClassName() const { return "Segmentation"; } 00084 00085 void StoreOn(Storage & storage) const 00086 { 00087 XMLAdapter<double> adapter(_maxPosition, "max", false); 00088 storage.Store(adapter); 00089 CLAM::DataArray array; 00090 fillArray(array); 00091 array.StoreOn(storage); 00092 } 00093 00094 void LoadFrom(Storage & storage) 00095 { 00096 double newmaxPosition; 00097 XMLAdapter<double> adapter(newmaxPosition, "max", false); 00098 if(storage.Load(adapter)) 00099 { 00100 maxPosition(newmaxPosition); 00101 } 00102 else { 00103 //when the maxPos is not present, set it to a large number 00104 maxPosition(100000); 00105 } 00106 DataArray array; 00107 array.LoadFrom(storage); 00108 const unsigned size = array.Size(); 00109 const TData* ptr=array.GetPtr(); 00110 takeArray(ptr, ptr+size); 00111 00112 } 00113 00114 void select(unsigned segment) 00115 { 00116 _selection[segment]=true; 00117 } 00118 void deselect(unsigned segment) 00119 { 00120 _selection[segment]=false; 00121 } 00122 void clearSelection() 00123 { 00124 for (unsigned i=0; i<_selection.size(); i++) 00125 _selection[i]=false; 00126 } 00127 00131 std::string boundsAsString() const 00132 { 00133 std::ostringstream os; 00134 for (unsigned i=0; i<_offsets.size(); i++) 00135 { 00136 if (_selection[i]) os << "+"; 00137 os << "(" << _onsets[i] << "," << _offsets[i] << ") "; 00138 } 00139 return os.str(); 00140 } 00141 00145 const TimePositions & onsets() const 00146 { 00147 return _onsets; 00148 } 00152 const TimePositions & offsets() const 00153 { 00154 return _offsets; 00155 } 00159 const std::vector<std::string> & labels() const 00160 { 00161 return _labels; 00162 } 00166 void setLabel(unsigned segment, std::string label) 00167 { 00168 if (segment>=_labels.size()) return; // Invalid segment 00169 _labels[segment] = label; 00170 } 00174 const std::vector<bool> & selections() const 00175 { 00176 return _selection; 00177 } 00181 unsigned current() const 00182 { 00183 return _current; 00184 } 00188 void current(unsigned index) 00189 { 00190 if (index>=_onsets.size()) return; 00191 _current = index; 00192 } 00193 double maxPosition() const 00194 { 00195 return _maxPosition; 00196 } 00197 virtual void maxPosition(double maxPosition) 00198 { 00199 _maxPosition = maxPosition; 00200 } 00201 void xUnits(const std::string & units) 00202 { 00203 _xUnits=units; 00204 } 00205 const std::string & xUnits() const 00206 { 00207 return _xUnits; 00208 } 00209 protected: 00210 TimePositions _onsets; 00211 TimePositions _offsets; 00212 std::vector<std::string> _labels; 00213 std::vector<bool> _selection; 00214 unsigned _current; 00215 double _maxPosition; 00216 std::string _xUnits; 00217 }; 00218 00219 } 00220 00221 00222 00223 #endif//Segmentation_hxx 00224