Structural deformable models
Sensor.h
Go to the documentation of this file.
1 /* Sensor -*- C++ -*- */
2 #ifndef _SENSOR_H_
3 #define _SENSOR_H_
4 
5 #include <math.h>
6 #include <cmath>
7 #include <vector>
8 #include <set>
9 #include "common.h"
10 #include "Point.h"
11 #include "Image.h"
12 
13 #define SENSOR_GAUSS_NORMALIZATION_RANGE 2
14 
15 class SensorCollection;
16 class Dataset;
17 class ZeroSensor;
19 
21 class Sensor : public std::enable_shared_from_this<Sensor> {
22 protected:
24  Sensor();
25 public:
29  UPD_LAST=32, UPD_ALL=0xffffffff
30  };
32  virtual ~Sensor();
33 
35  virtual void changeSource(sensor_cptr _source);
36  void replaceBy(sensor_ptr target);
37  bool assignRef(sensor_cptr rhs);
38  virtual Sensor& assign(const Sensor& rhs);
39 
41  virtual float getValue(int x, int y) const {
42  return calcValue( x, y);
43  }
45  float getValue(const Point &p) const {
46  return getValue((int)p.x, (int)p.y);
47  }
49  virtual std::vector<float> getMValue(int x, int y) const {
50  return calcMValue(x,y);
51  }
53  virtual Point2D getGradient(int x, int y) const {
54  return calcGradient(x,y);
55  }
57  Point2D getGradient(const Point2D &p) const {
58 // float v = getValue(p);
59 // return Point2D(getValue(p+Point2D(1,0))-v,
60 // getValue(p+Point2D(0,1))-v);
61  return getGradient((int)p.x, (int)p.y);
62  };
63  float getMax() const {
64  if(isModified()) ((Sensor*)this)->performUpdate();
65  return maxval;
66  }
67  float getMin() const {
68  if(isModified()) ((Sensor*)this)->performUpdate();
69  return minval;
70  }
71  float getMinMaxRange() const {
72  if(isModified()) ((Sensor*)this)->performUpdate();
73  return maxval-minval;
74  }
76  float getWeightedValue(int x, int y) const {
77 #define _USE_MINMAX_NORMALIZATION_
78 #ifdef _USE_MINMAX_NORMALIZATION_
79  float range = getMinMaxRange();
80  if(range==0) return 0.f;
81  return (getValue(x,y)-minval)/range;
82 #else
84  if(range==0) range = 1;
85  float value = (getValue(x,y)-minval)/range;
86  return value > 1.f ? 1.f : value; //(value < 0.f ? 0.f : value);
87 #endif
88  }
90  Point2D getWeightedGradient(int x, int y) const {
91  float range = getMinMaxRange();
92  if(range==0) range = 1;
93  return getGradient(x,y)/range;
94  }
95 
96  void setCWeights(const std::vector<float>& weights);
97  const std::vector<float>& getCWeights() const { return cweights; }
98  void setDirection(const Point &dir);
99  const Point& getDirection() const { return dir; }
100  void setScale(float _scale);
101  float getScale() const { return scale; }
102 
108  bool isModified(dword mask=UPD_ALL) const
109  { return (toupdate & (mask & updateMask)) != 0; }
110  void setModified(dword mask=UPD_ALL) { toupdate |= mask; }
111  void unsetModified(dword mask=UPD_ALL) { toupdate &= ~mask; }
112  bool isUpdate(dword udMask) const { return updateMask & udMask; }
113  void enableUpdate(dword udMask) { updateMask |= udMask; }
114  void disableUpdate(dword udMask) { updateMask &= ~udMask; }
115 
117  virtual bool performUpdate();
118 
120  virtual int getDim1Size() const { return source->getDim1Size(); }
121  virtual int getDim2Size() const { return source->getDim2Size(); }
122  virtual int getDim3Size() const { return source->getDim3Size(); }
123  virtual int getNChannels() const { return source->getNChannels(); }
124  virtual int getSkip() const { return m_Skip; }
125  bool isValid(int x, int y) const;
126  virtual Image<float> createSensorImage() const;
127 
128  const std::string& getID() const { return m_ID; }
129  void setID(const std::string& id) { m_ID = id; }
130 
131  static dword getStringNumber(const char *sid);
132  static void getNumberString(char sid[5], dword id);
133 
134  virtual std::ostream& hprint(std::ostream &os, SensorCollection *sc) const;
135  virtual std::ostream& print(std::ostream &os) const;
136 
137 protected:
141  virtual float calcValue(int x, int y) const = 0;
145  virtual std::vector<float> calcMValue(int x, int y) const {
146  return std::vector<float>(getNChannels(), calcValue(x,y));
147  }
149  virtual Point calcGradient(int x, int y) const {
150  float v = getValue(x,y);
151  return Point2D(getValue(x+1,y)-v,
152  getValue(x,y+1)-v);
153  }
154 
155  virtual void calcMinMax();
156 
157 public:
158  void refSuperSensor(sensor_ptr super);
159  void unrefSuperSensor(sensor_ptr super);
160  void invalidateSource();
161  template <typename Derived>
162  std::shared_ptr<Derived> shared_from_base()
163  {
164  return std::static_pointer_cast<Derived>(shared_from_this());
165  }
166 
167 protected:
168  //------------------------- data ----------------
171  /* parameters */
173  float scale;
174  std::vector<float> cweights;
176  float maxval, minval;
177  float mean, stdev;
178 
181  std::string m_ID;
182  int m_Skip;
184 private:
185  std::set<sensor_ptr> superSensors;
186 };
187 
188 template<typename SensorType, typename FirstArgType, typename... Args>
189 std::shared_ptr<SensorType> makeSensor(FirstArgType source, Args... args)
190 {
191  std::shared_ptr<SensorType> s = std::make_shared<SensorType>(args...);
192  s->changeSource(source);
193  return s;
194 }
195 
196 template<typename SensorType>
197 std::shared_ptr<SensorType> makeSensor()
198 {
199  return std::make_shared<SensorType>();
200 }
201 
203 class ZeroSensor : public Sensor {
204 public:
206  {
208  unsetModified();
209  setID("0");
210  }
212  int getDim1Size() const { return 2; }
213  int getDim2Size() const { return 2; }
214  int getDim3Size() const { return 1; }
215  int getNChannels() const { return 1; }
216  float getWeightedValue(int x, int y) const { return 0; }
217  Point2D getWeightedGradient(int x, int y) const { return Point(); }
218  std::ostream& print(std::ostream &os) const
219  { return Sensor::print(os)<<"o"; }
220 
221 protected:
223  float calcValue(int x, int y) const { return 0.0f; }
224  Point calcGradient(int x, int y) const { return Point(0,0); }
225 };
226 
228 class PPSensor : public Sensor {
229 public:
230  enum PPState { PP_DONT=0, PP_DO, PP_FORCE };
231  PPSensor();
233  virtual float getValue(int x, int y) const;
235  virtual Point getGradient(int x, int y) const;
237  virtual bool performUpdate();
238  Sensor& assign(const Sensor& rhs);
243  void togglePP(enum PPState state=PP_DO) { doPP = state; }
244  dword getPPState() const { return doPP; }
245 protected:
246  void fitSheets();
247  virtual void calcAllValues();
248  virtual void calcAllGradients();
249  //virtual const Image<float> createSensorImage() const;
250 
251 private:
253  bool performFullUpdate();
254 
255 protected:
259 };
260 
261 //-----------------------------------------------------------------------------
262 //inline functions
263 
264 //--------------------------------- PPSensor
265 
266 inline float PPSensor::getValue(int x, int y) const {
267  if(doPP) {
268  if(isModified()) const_cast<PPSensor*>(this)->performUpdate();
269  int index = (int)values.getBoundedIndex(x,y);
270  if(index==-1) return 0;
271  float v;
272  if(std::isnan((v = values[index]))) {
273  if(doPP == PPSensor::PP_FORCE) {
274  assert(false); //no NAN values in forced full update at this point
275  const_cast<PPSensor*>(this)->performFullUpdate();
276  return values[index];
277  } else {
278  return (const_cast<PPSensor*>(this)->values[index] = calcValue(x,y));
279  }
280  } else return v;
281  } else return calcValue(x,y);
282 }
283 
284 inline Point PPSensor::getGradient(int x, int y) const {
285  if(doPP) {
286  if(isModified()) const_cast<PPSensor*>(this)->performUpdate();
287  int index = gradients.getBoundedIndex(x,y);
288  if(index<0) return Point();
289  if(std::isnan(gradients[index].x)) { // no precalculated value?
290 // if(doPP == PP_FORCE) {
291 // //this should not happen
292 // assert(false);
293 // const_cast<PPSensor*>(this)->performFullUpdate();
294 // return gradients[index];
295 // } else
296  return (const_cast<PPSensor*>(this)->gradients[index]
297  = calcGradient(x,y));
298  } else return gradients[index];
299  } else return calcGradient(x,y);
300 }
301 
302 #endif
int getNChannels() const
Definition: Sensor.h:215
bool assignRef(sensor_cptr rhs)
Definition: Sensor.cpp:69
float getMin() const
Definition: Sensor.h:67
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
void disableUpdate(dword udMask)
Definition: Sensor.h:114
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::ostream & print(std::ostream &os) const
Definition: Sensor.h:218
void changeSource(sensor_cptr source)
Definition: Sensor.h:211
virtual int getDim3Size() const
Definition: Sensor.h:122
float y
Definition: Point.h:224
Sensor()
Definition: Sensor.cpp:23
virtual Point2D getGradient(int x, int y) const
get gradient at discrete position
Definition: Sensor.h:53
virtual Point getGradient(int x, int y) const
Definition: Sensor.h:284
void setID(const std::string &id)
Definition: Sensor.h:129
virtual float calcValue(int x, int y) const =0
bool isValid(int x, int y) const
Definition: Sensor.cpp:197
virtual int getNChannels() const
Definition: Sensor.h:123
std::vector< float > cweights
multi-channel weights (&#39;color&#39;)
Definition: Sensor.h:174
float getMinMaxRange() const
Definition: Sensor.h:71
virtual Point calcGradient(int x, int y) const
Definition: Sensor.h:149
Definition: Sensor.h:21
std::shared_ptr< Sensor > sensor_ptr
Definition: types_fwd.h:15
virtual bool performUpdate()
Definition: Sensor.cpp:123
virtual float getValue(int x, int y) const
Definition: Sensor.h:266
virtual std::vector< float > getMValue(int x, int y) const
get multi-channel value
Definition: Sensor.h:49
int getDim2Size() const
Definition: Sensor.h:213
sensor_cptr getSource()
Definition: Sensor.h:34
#define SENSOR_GAUSS_NORMALIZATION_RANGE
Definition: Sensor.h:13
const std::vector< float > & getCWeights() const
Definition: Sensor.h:97
Point2D Point
Definition: Point.h:251
float getWeightedValue(int x, int y) const
Definition: Sensor.h:216
float getMax() const
Definition: Sensor.h:63
std::shared_ptr< Derived > shared_from_base()
Definition: Sensor.h:162
void setScale(float _scale)
Definition: Sensor.cpp:119
Point dir
direction parameter
Definition: Sensor.h:175
virtual int getSkip() const
Definition: Sensor.h:124
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
bool isUpdate(dword udMask) const
Definition: Sensor.h:112
Point2D getGradient(const Point2D &p) const
get gradient at position p using nearest neighbor interpolation
Definition: Sensor.h:57
virtual int getDim1Size() const
Definition: Sensor.h:120
void invalidateSource()
Definition: Sensor.cpp:190
void enableUpdate(dword udMask)
Definition: Sensor.h:113
float calcValue(int x, int y) const
Computes a zero.
Definition: Sensor.h:223
virtual void calcMinMax()
Definition: Sensor.cpp:152
dword toupdate
bitflag for updates
Definition: Sensor.h:179
std::shared_ptr< SensorType > makeSensor(FirstArgType source, Args...args)
Definition: Sensor.h:189
int getDim1Size() const
Definition: Sensor.h:212
UpdateParam
Definition: Sensor.h:26
unsigned long dword
Definition: simpletypes.h:6
Point2D getWeightedGradient(int x, int y) const
Definition: Sensor.h:217
float getScale() const
Definition: Sensor.h:101
Definition: Data.h:21
virtual ~Sensor()
Definition: Sensor.cpp:34
sensor_ptr getZeroSensor()
Definition: Sensor.cpp:234
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
const Point & getDirection() const
Definition: Sensor.h:99
Point2D getWeightedGradient(int x, int y) const
get range weighted gradient at discrete position
Definition: Sensor.h:90
int getDim3Size() const
Definition: Sensor.h:214
virtual std::vector< float > calcMValue(int x, int y) const
Definition: Sensor.h:145
float scale
Definition: Sensor.h:173
float mean
Definition: Sensor.h:177
std::shared_ptr< const Sensor > sensor_cptr
Definition: types_fwd.h:17
void togglePP(enum PPState state=PP_DO)
Definition: Sensor.h:243
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
const std::string & getID() const
Definition: Sensor.h:128
ZeroSensor()
Definition: Sensor.h:205
dword updateMask
bitflag to mask unimportant updates
Definition: Sensor.h:180
Point calcGradient(int x, int y) const
Definition: Sensor.h:224
sensor_cptr source
Definition: Sensor.h:170
int m_Skip
Definition: Sensor.h:182
virtual Sensor & assign(const Sensor &rhs)
Definition: Sensor.cpp:54
float x
Definition: Point.h:224
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
float getValue(const Point &p) const
get value at position p using nearest neighbour interpolation
Definition: Sensor.h:45
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
dword getPPState() const
Definition: Sensor.h:244
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