Structural deformable models
SensorColl.cpp
Go to the documentation of this file.
1 #include <typeinfo>
2 #include "SensorColl.h"
3 #include "SensorSet.h"
4 #include "Data.h"
5 #include "Errors.h"
6 #include "Model.h"
7 using namespace std;
8 
9 //#define VERBOSE
10 #ifdef VERBOSE
11 #define SHOW(a) TRACE(a)
12 #else
13 #define SHOW(a)
14 #endif
15 
17 {
18  addSensor(getZeroSensor()); // this is our zeroth sensor
19 }
20 
22 { }
23 
25 {
26  s->setID(key);
27  return addSensor(s);
28 }
29 
31  if(!s) return NULL;
32  if(s->getID().empty()) return NULL; //s->setID(size());
33  sensor_ptr &spr = operator[](s->getID());
34  selectSensor(s->getID());
35  if(spr == NULL) {
36  spr = s;
37  return s;
38  }else {
39  SHOW("replacing sensor "<<s->getID());
40  if(spr->assignRef(s)) {
41  SHOW("assigning");
42  s->replaceBy(spr);
43  } else {
44  SHOW("replacing");
45  spr->replaceBy(s);
46  spr = s;
47  }
48  return spr;
49  }
50 }
51 
53  iterator fs = find(id);
54  if(fs != end()) {
55  return fs->second;
56  } else {
57 // char sid[5];
58 // Sensor::getNumberString(sid,id);
59 // cout << "zeroing " << sid << endl;
60  sensor_ptr zs = makeSensor<ZeroSensor>();
61  zs->setID(id);
62  return addSensor(zs);
63  }
64 }
65 
66 void SensorCollection::clearPrintList(bool skipdefaults)
67 {
68  m_Printlist.clear();
69  if(skipdefaults) {
70  m_Printlist.insert("d0");
71  m_Printlist.insert("0");
72  }
73 }
74 
75 bool SensorCollection::isPrinted( const string& id ) const
76 {
77  return m_Printlist.find(id) != m_Printlist.end();
78 }
79 
80 void SensorCollection::setPrinted( const string& id )
81 {
82  m_Printlist.insert(id);
83 }
84 
85 ostream& operator<<(ostream& os, const SensorCollection& sc)
86 {
87  ((SensorCollection*)&sc)->m_Printlist.clear();
88  for(map<string,sensor_ptr>::const_iterator s=sc.begin(); s!=sc.end(); s++) {
89  if (s->second->getID() != "0" && // zero sensor
90  !s->second->getID().empty() && // uninitialized sensor
91  s->second->getID() != "d0" &&
92  !sc.isPrinted(s->second->getID())
93  )
94  {
95  s->second->hprint(os, (SensorCollection*)&sc);
96  }
97  }
98  return os;
99 }
100 
102 {
103  if(!is.getNextLine()) return NULL;
104  if(is.getKey() != "s") {
105  is.pushLine();
106  return NULL;
107  }
108  istringstream vs(is.getValue());
109  vs >> ws; // read key
110  string skey;
111  //vs.width(2047);
112  vs >> skey;
113  vs >> ws; // read type
114  string para1;
115  vs >> para1;
116  char stype;
117  sensor_cptr source = NULL;
118  if (para1 == "source") {
119  string sname;
120  vs >> sname;
121  source = getSensor(sname);
122  if(std::dynamic_pointer_cast<const ZeroSensor>(source))
123  cout << "Using zero sensor as source. Source name was: "
124  << sname <<endl;
125  else {
126  SHOW("Using source " << source->getID());
127  }
128  vs >> stype;
129  } else {
130  stype = para1[0];
131  }
132  if(source == NULL) {
133  source = getSensor(string("d0"));
134  }
135  sensor_ptr s = NULL;
136  switch(stype) {
137  case 'i' :
138  s = makeSensor<IntensitySensor>(source);
139  SHOW("created intensity sensor");
140  break;
141  case 'g' :
142  {
143  float scale=0;
144  vs >> scale;
145  s = makeSensor<SmoothIntensitySensor>(source, scale);
146  SHOW("created Gaussian sensor of scale " << scale);
147  break;
148  }
149  case 'd' :
150  {
151  //float scale=0;
152  //vs >> scale;
153  s = makeSensor<GradMagSensor>(source);
154  SHOW("created gm sensor");// of scale " << scale);
155  break;
156  }
157  case 'c' :
158  {
159 // float scale=0;
160 // vs >> scale;
161  s = makeSensor<CornerSensor>(source);
162  SHOW("created corner sensor");// of scale " << scale);
163  break;
164  }
165  case 'K' :
166  s = makeSensor<CombiSensor>();
167  std::dynamic_pointer_cast<CombiSensor>(s)->normalizeInput(false);
168  // is legally constructed in the next case 'k'
169  case 'k' :
170  {
171  int nsensors=0;
172  //vs >> nsensors;
173  if(!s) s = makeSensor<CombiSensor>(); //normalized input
174  string sname;
175  while(vs >> sname) {
176  float weight=1;
177  vs >> weight;
178  sensor_ptr ss = getSensor(sname);
179  if(ss == getZeroSensor())
180  cout << "added zero sensor" << endl;
181  std::dynamic_pointer_cast<CombiSensor>(s)->setSource(ss,weight);
182  nsensors++;
183  }
184  SHOW("created combi sensor with " << nsensors<<" sub-sensors.");
185  break;
186  }
187  case 'm' :
188  {
189  int nc = source ? source->getNChannels() : 0;
190  vector<float> weights;
191  weights.reserve(nc);
192  float weight=1;
193  while(vs >> weight) {
194  weights.push_back(weight);
195  cout << weight << " ";
196  }
197  cout << endl;
198  s = makeSensor<MCIntensitySensor>(source);
199  s->setCWeights(weights);
200  SHOW("created multi-channel sensor.");
201  break;
202  }
203  case 'M' :
204  {
205  s = makeSensor<MCGSensor>(source);
206  SHOW("created multi-channel gradient sensor.");
207  break;
208  }
209  case 'r' :
210  {
211  s = makeSensor<CRSensor>(source);
212  SHOW("created multi-channel chrominance sensor.");
213  break;
214  }
215  case 'C' :
216  {
217  string fname;
218  vs >> fname;
219  if(!fname.empty()) {
220  s = makeSensor<MahalSensor>(source, fname);
221  if(!std::dynamic_pointer_cast<MahalSensor>(s)->getFilename().empty()) {
222  SHOW("created colour classification sensor");
223  } else {
224  SHOW("error loading configuration file for colour sensor "
225  << fname);
226  s = sensor_ptr();
227  }
228  }
229  break;
230  }
231  case 'f' :
232  {
233  string mapping;
234  vs >> mapping;
235  s = makeSensor<MappingSensor>(source, mapping);
236  std::dynamic_pointer_cast<MappingSensor>(s)->readParams(vs);
237  SHOW("created mapping sensor of type " <<
238  std::dynamic_pointer_cast<MappingSensor>(s)->getMappingName());
239  break;
240  }
241  case 'o' :
242  {
243  s = makeSensor<ZeroSensor>();
244  SHOW("created zero sensor");
245  break;
246  }
247  }
248  if(!s) {
249  is.setParseError(string("unknown sensor type ")+stype);
250  throw new IOException("Error reading sensor.");
251  }
252  s->setID(skey);
253  return s;
254 }
255 
257 {
258  if(this == &rhs) return *this;
259  for(map<string, sensor_ptr >::iterator si = rhs.begin();
260  si != rhs.end(); si++)
261  {
262  if(!si->first.empty() && si->first!="0") {
263  sensor_ptr spr = operator[](si->first); // sensor from lhs of collection assignment
264  if(!spr) {
265  spr = si->second;
266  } else {
267  bool lnzs = !std::dynamic_pointer_cast<ZeroSensor>(spr); // lhs sensor not zero
268  bool rnzs = !std::dynamic_pointer_cast<ZeroSensor>(si->second); // rhs sensor not zero
269  // TODO: inverted original logic, test if still valid
270  if(rnzs) {
271  spr->replaceBy(si->second);
272  // spr = si->second;
273  } else {
274  si->second->replaceBy(spr);
275  }
276  }
277  }
278  }
279  return *this;
280 }
281 
283 {
284  m_Models.insert(model);
285 }
286 
288 {
289  m_Models.erase(model);
290 }
291 
293 {
294  for(set<Model*>::iterator mo = m_Models.begin();
295  mo != m_Models.end(); mo++)
296  (*mo)->reattachSensors();
297 }
void clearPrintList(bool skipdefaults=true)
Definition: SensorColl.cpp:66
#define NULL
Definition: simpletypes.h:9
void replaceBy(sensor_ptr target)
Definition: Sensor.cpp:44
void updateModels() const
Definition: SensorColl.cpp:292
STL namespace.
std::shared_ptr< Sensor > sensor_ptr
Definition: types_fwd.h:15
void refModel(Model *model) const
Definition: SensorColl.cpp:282
sensor_ptr getSensor(const std::string &id)
Definition: SensorColl.cpp:52
const std::string & getValue() const
Definition: ParseFile.h:57
sensor_ptr getZeroSensor()
Definition: Sensor.cpp:234
void setPrinted(const std::string &id)
Definition: SensorColl.cpp:80
const std::string & getKey() const
Definition: ParseFile.h:56
Definition: Model.h:33
void unrefModel(Model *model) const
Definition: SensorColl.cpp:287
bool isPrinted(const std::string &id) const
Definition: SensorColl.cpp:75
void setParseError(const std::string &msg="")
Definition: ParseFile.h:80
std::shared_ptr< const Sensor > sensor_cptr
Definition: types_fwd.h:17
ostream & operator<<(ostream &os, const SensorCollection &sc)
Definition: SensorColl.cpp:85
SensorCollection & merge(SensorCollection &rhs)
Definition: SensorColl.cpp:256
void pushLine(const std::string &line)
Definition: ParseFile.h:74
#define SHOW(a)
Definition: SensorColl.cpp:13
sensor_ptr readSensor(ParseFile &is)
Definition: SensorColl.cpp:101
const bool getNextLine()
Definition: ParseFile.h:59
sensor_ptr addSensor(const std::string &key, sensor_ptr s)
Definition: SensorColl.cpp:24