Structural deformable models
Sensor.cpp
Go to the documentation of this file.
1 #ifdef WIN32
2 #include <windows.h>
3 #endif
4 #include <typeinfo>
5 #include <math.h>
6 #include "Sensor.h"
7 #include "SensorColl.h"
8 #include "airnan.h"
9 #include <string.h>
10 
11 using namespace std;
12 
13 Sensor::Sensor(dword _updateMask)
14  : source(),
15  scale(0.f),
16  maxval(), minval(), mean(), stdev(),
17  toupdate(), updateMask(_updateMask),
18  m_ID(""), m_Skip(0), m_AddSkip(0)
19 {
20  cweights.resize(1, 1.f);
21 }
22 
24  : source(getZeroSensor()),
25  scale(0.f),
26  maxval(), minval(), mean(), stdev(),
28  m_ID(""), m_Skip(0), m_AddSkip(0)
29 {
30  cweights.resize(1, 1.f);
31  setModified();
32 }
33 
35 {
36  if(source && source != getZeroSensor())
37  std::const_pointer_cast<Sensor>(source)->unrefSuperSensor( shared_from_this() );
38  for(set<sensor_ptr>::iterator ss = superSensors.begin();
39  ss != superSensors.end();
40  ss++)
41  (*ss)->invalidateSource();
42 }
43 
45  for(set<sensor_ptr>::iterator ss = superSensors.begin();
46  ss != superSensors.end();)
47  {
48  set<sensor_ptr>::iterator last = ss;
49  ss++;
50  (*last)->changeSource(target);
51  }
52 }
53 
55 {
56  if(&rhs != this) {
57  if(source != rhs.source) changeSource(rhs.source);
58  if(scale != rhs.scale) setScale(rhs.scale);
59  if(dir != rhs.dir) setDirection(rhs.dir);
60  if(cweights != rhs.cweights) setCWeights(rhs.cweights);
61  m_ID = rhs.m_ID;
62  m_Skip = rhs.m_Skip;
63  m_AddSkip = rhs.m_AddSkip;
64  //superSensors are not changed here (no sensor fusion)
65  }
66  return *this;
67 }
68 
70  if(typeid(*this) != typeid(*rhs)) return false;
71  assign(*rhs);
72  return true;
73 }
74 
75 dword Sensor::getStringNumber(const char *sid) {
76  if(!sid) return 0;
77  else {
78  dword val;
79  int i;
80  for(i=0, val=0; i<4 && sid[i]!=0; i++) {
81  if(sid[i]>32)
82  val |= ((dword)(sid[i]))<<(8*i);
83  }
84  return val;
85  }
86 }
87 
88 void Sensor::getNumberString(char sid[5], dword id)
89 {
90  int i;
91  for(i=0;i<4 && id!=0;i++,id=id>>8)
92  sid[i] = char(id);
93  sid[i] = 0;
94 }
95 
97  assert(source);
98  std::const_pointer_cast<Sensor>(source)->unrefSuperSensor( shared_from_this() );
99  if(!_source)
100  source = getZeroSensor();
101  else
102  source = _source;
103  if(cweights.size() != size_t(source->getNChannels()))
104  cweights.resize(source->getNChannels(), 1.f);
105  std::const_pointer_cast<Sensor>(source)->refSuperSensor( shared_from_this() );
107 }
108 
109 void Sensor::setCWeights(const vector<float>& weights) {
110  //if((int)weights.size() == source->getNChannels()) {
111  cweights = weights;
113  //}
114 }
116  this->dir = dir;
118 }
119 void Sensor::setScale(float _scale) {
120  scale = _scale;
122 }
124  bool modified = false;
125  if(isModified()) {
126  if(isModified(UPD_DATA)) {
127  m_Skip = source->getSkip()+m_AddSkip;
128  //if(cweights.size() != source->getNChannels()) {
129  // cweights.resize(source->getNChannels(),1.f);
130  //modified = true;
131  //}
132  }
133  unsetModified();
134  if(updateMask&UPD_MINMAX) {
135  calcMinMax();
136  }
137  // do nothing for this sensor, but for the others...
138  for(set<sensor_ptr>::iterator ss = superSensors.begin();
139  ss != superSensors.end(); ss++)
140  {
141  (*ss)->setModified(Sensor::UPD_DATA);
142  }
143  for(set<sensor_ptr>::iterator ss = superSensors.begin();
144  ss != superSensors.end(); ss++)
145  {
146  (*ss)->performUpdate();
147  }
148  }
149  return modified; // no modification to *this
150 }
151 
153  if(!source) {
154  minval = maxval = 0;
155  mean = stdev = 0;
156  return;
157  }
158  int i,j;
159  minval=numeric_limits<float>::max();
160  maxval=numeric_limits<float>::min();
161  double avg=0, avg2=0;
162  double size = double(source->getDim1Size()*source->getDim2Size());
163  if(size > 0.) {
164  for(j=0; j<source->getDim2Size(); j++) {
165  for(i=0; i<source->getDim1Size(); i++) {
166  float val = getValue(i,j);
167  if(val>maxval) maxval = val;
168  if(val<minval) minval = val;
169  avg+=double(val);
170  avg2+=double(val*val);
171  }
172  }
173  avg /= size;
174  avg2 /= size;
175  } else {
176  minval = maxval = 0;
177  }
178  mean = float(avg);
179  stdev = float(sqrt(avg2-avg*avg))*1.5;
180 }
181 
183  if(super != getZeroSensor())
184  superSensors.insert(super);
185 }
187  if(super != getZeroSensor())
188  superSensors.erase(super);
189 }
191  if(source && source != getZeroSensor()) {
192  std::const_pointer_cast<Sensor>(source)->unrefSuperSensor( shared_from_this() );
193  source = getZeroSensor();
194  }
195 }
196 
197 bool Sensor::isValid(int x, int y) const
198 {
199  return x>=m_Skip && x<getDim1Size()-m_Skip
200  && y>=m_Skip && y<getDim2Size()-m_Skip;
201 }
202 
205  int i,j;
206  for(j=0;j<getDim2Size();j++)
207  for(i=0;i<getDim1Size();i++) {
208  img.setPixel(i,j, getWeightedValue(i,j));
209  }
210  return img;
211 }
212 
213 ostream& Sensor::print(ostream &os) const
214 {
215  os << "s " << m_ID << " ";
216  if(source && source->getID() != "d0" && source != getZeroSensor()) {
217  os << "source " << source->getID() << " ";
218  }
219  return os;
220 }
221 
222 ostream& Sensor::hprint(ostream &os, SensorCollection *sc) const
223 {
224  if(sc->isPrinted(m_ID)) return os;
225  if(source && source != getZeroSensor()) source->hprint(os, sc);
226  if(sc->isPrinted(m_ID)) return os;
227  sc->setPrinted(m_ID);
228  return (print(os) << endl);
229 }
230 
231 //-----------------------------------------------------------------------------
232 //class ZeroSensor
233 
235 {
236  static sensor_ptr zeros = std::make_shared<ZeroSensor>();
237  return zeros;
238 }
239 
240 //-----------------------------------------------------------------------------
241 //class PPSensor
243  : values(source->getDim1Size(), source->getDim2Size(), AIR_NAN),
244  gradients(source->getDim1Size(), source->getDim2Size(),
246  doPP(PPSensor::PP_DONT)
247 {}
248 
250  if(values.getSizeX() != source->getDim1Size() ||
251  values.getSizeY() != source->getDim2Size()) {
252  values.setSize(source->getDim1Size(), source->getDim2Size());
253  gradients.setSize(source->getDim1Size(), source->getDim2Size());
254  }
255  //this is also done later on by Sensor::performUpdate,
256  //but we might need it earlier
257  //if(cweights.size() != source->getNChannels()) {
258 // cweights.resize(source->getNChannels());
259 // }
260 }
261 
263  if(values.getSizeX() == source->getDim1Size() &&
264  values.getSizeY() == source->getDim2Size())
265  {
266  int i,j;
267  Image<float>::iterator v=values.begin();
268  for(j=0; j<source->getDim2Size(); j++) {
269  for(i=0; i<source->getDim1Size(); i++, v++)
270  {
271  *v = calcValue(i,j);
272  assert(!isnan(*v));
273  }
274  }
275  }
276 }
277 
279  if(gradients.getSizeX() == source->getDim1Size() &&
280  gradients.getSizeY() == source->getDim2Size())
281  {
282  int i,j;
284  for(j=0; j<source->getDim2Size()-1; j++, g++) {
285  for(i=0; i<source->getDim1Size()-1; i++, g++) {
286  *g = calcGradient(i,j);
287  }
288  }
289  }
290 }
291 
292 bool PPSensor::performFullUpdate()
293 {
294  if(isModified()) {
295  fitSheets();
296  dword toupdatesave = toupdate;
297  unsetModified(); // otherwise getValue reenters performUpdate
298  calcAllValues();
300  setModified(toupdatesave);
302  return true;
303  } else return false;
304 }
305 
307 {
308  if(isModified()) {
309  if(doPP == PPSensor::PP_FORCE) return performFullUpdate();
310  else {
311  fitSheets();
312  for(Image<float>::iterator v=values.begin(); v!=values.end(); v++) {
313  *v = AIR_NAN;
314  }
315  for(Image<Point>::iterator g=gradients.begin(); g!=gradients.end(); g++) {
316  g->x = AIR_NAN;
317  }
319  return true;
320  }
321  }else return false;
322 }
323 
325 {
326  if(&rhs != this) {
327  Sensor::assign(rhs);
328  if(typeid(&rhs) == typeid(PPSensor*)) {
329  const PPSensor& crhs = (const PPSensor&) rhs;
330  doPP = crhs.doPP;
331  }
332  }
333  return *this;
334 }
335 
336 /*
337  const Image<float> PPSensor::createSensorImage() const
338  {
339  dword sPP = doPP;
340  ((PPSensor*)this)->doPP = PPSensor::PP_FORCE;
341  ((PPSensor*)this)->performUpdate();
342  ((PPSensor*)this)->doPP = sPP;
343  return Image<float>(values);
344  }
345 */
bool assignRef(sensor_cptr rhs)
Definition: Sensor.cpp:69
bool isModified(dword mask=UPD_ALL) const
Definition: Sensor.h:108
virtual std::ostream & hprint(std::ostream &os, SensorCollection *sc) const
Definition: Sensor.cpp:222
float minval
overall minimum and maximum
Definition: Sensor.h:176
Image< float > values
caching sheet for sensed values
Definition: Sensor.h:256
int m_AddSkip
Definition: Sensor.h:183
virtual void changeSource(sensor_cptr _source)
Definition: Sensor.cpp:96
void replaceBy(sensor_ptr target)
Definition: Sensor.cpp:44
std::vector< T >::iterator iterator
Definition: Image.h:19
Sensor()
Definition: Sensor.cpp:23
STL namespace.
void fitSheets()
Definition: Sensor.cpp:249
virtual float calcValue(int x, int y) const =0
bool isValid(int x, int y) const
Definition: Sensor.cpp:197
int getSizeX() const
Definition: Image.h:42
std::vector< float > cweights
multi-channel weights (&#39;color&#39;)
Definition: Sensor.h:174
virtual Point calcGradient(int x, int y) const
Definition: Sensor.h:149
Definition: Sensor.h:21
virtual void calcAllValues()
Definition: Sensor.cpp:262
std::shared_ptr< Sensor > sensor_ptr
Definition: types_fwd.h:15
virtual bool performUpdate()
Definition: Sensor.cpp:123
sensor_ptr getZeroSensor()
Definition: Sensor.cpp:234
#define AIR_NAN
Definition: airnan.h:80
void setScale(float _scale)
Definition: Sensor.cpp:119
Point dir
direction parameter
Definition: Sensor.h:175
float getWeightedValue(int x, int y) const
get max normalized value at discrete position range [0,1]
Definition: Sensor.h:76
void unrefSuperSensor(sensor_ptr super)
Definition: Sensor.cpp:186
void setPrinted(const std::string &id)
Definition: SensorColl.cpp:80
virtual int getDim1Size() const
Definition: Sensor.h:120
PPSensor()
Definition: Sensor.cpp:242
void invalidateSource()
Definition: Sensor.cpp:190
virtual void calcMinMax()
Definition: Sensor.cpp:152
virtual void calcAllGradients()
Definition: Sensor.cpp:278
dword toupdate
bitflag for updates
Definition: Sensor.h:179
Sensor & assign(const Sensor &rhs)
Definition: Sensor.cpp:324
unsigned long dword
Definition: simpletypes.h:6
virtual bool performUpdate()
Definition: Sensor.cpp:306
virtual ~Sensor()
Definition: Sensor.cpp:34
bool isPrinted(const std::string &id) const
Definition: SensorColl.cpp:75
Image< Point > gradients
caching sheet for gradients
Definition: Sensor.h:257
void refSuperSensor(sensor_ptr super)
Definition: Sensor.cpp:182
virtual float getValue(int x, int y) const
get value at discrete position
Definition: Sensor.h:41
void setSize(int nx, int ny)
Definition: Image.h:268
DMatrix< T > avg(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:90
float scale
Definition: Sensor.h:173
float mean
Definition: Sensor.h:177
std::shared_ptr< const Sensor > sensor_cptr
Definition: types_fwd.h:17
DMatrix< T > stdev(const DMatrix< T > &mat)
Definition: DMatrixUtil.h:106
static dword getStringNumber(const char *sid)
Definition: Sensor.cpp:75
std::string m_ID
Definition: Sensor.h:181
Definition: Point.h:16
void setCWeights(const std::vector< float > &weights)
Definition: Sensor.cpp:109
void setPixel(int x, int y, const T &value)
Definition: Image.h:62
dword updateMask
bitflag to mask unimportant updates
Definition: Sensor.h:180
sensor_cptr source
Definition: Sensor.h:170
int m_Skip
Definition: Sensor.h:182
virtual Sensor & assign(const Sensor &rhs)
Definition: Sensor.cpp:54
void setDirection(const Point &dir)
Definition: Sensor.cpp:115
dword doPP
do preprocessing?
Definition: Sensor.h:258
float stdev
overall mean and stdev
Definition: Sensor.h:177
virtual Image< float > createSensorImage() const
Definition: Sensor.cpp:203
virtual int getDim2Size() const
Definition: Sensor.h:121
void setModified(dword mask=UPD_ALL)
Definition: Sensor.h:110
void unsetModified(dword mask=UPD_ALL)
Definition: Sensor.h:111
float maxval
Definition: Sensor.h:176
static void getNumberString(char sid[5], dword id)
Definition: Sensor.cpp:88
virtual std::ostream & print(std::ostream &os) const
Definition: Sensor.cpp:213
DMatrix< T > & sqrt(DMatrix< T > &mat)
Definition: DMatrixUtil.h:81
int getSizeY() const
Definition: Image.h:43