ImGraph
 All Classes Functions Variables Enumerations Pages
Block.h
1 #ifndef _BLOCK_IMGRAPH_HEADER_
2 #define _BLOCK_IMGRAPH_HEADER_
3 
17 #ifndef Q_MOC_RUN
18 #ifdef _WIN32
19 #pragma warning(push)
20 #pragma warning(disable:4996 4244 4251 4275 4800 4503 4190)
21 #endif
22 #include <boost/config.hpp>
23 #include <boost/thread/thread.hpp>
24 #include <boost/property_tree/ptree.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/thread/condition_variable.hpp>
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include <opencv2/core.hpp>
29 
30 #include <iostream>
31 
32 #ifdef _WIN32
33 #pragma warning(pop)
34 #endif
35 #endif
36 
37 #include "Internationalizator.h"
38 #include "blocks/ParamValue.h"
39 #include "ProcessManager.h"
40 
41 //macro to add algo to list:
42 #define BLOCK_BEGIN_INSTANTIATION(className) \
43  \
44  class className## :public Block \
45  { \
46  friend class charliesoft::ProcessManager; \
47  static std::vector<ParamDefinition> getListParams(); \
48  static std::vector<ParamDefinition> getListOutputs(); \
49  static std::vector<ParamDefinition> getListSubParams(); \
50  static bool addedToList; \
51  protected: \
52  virtual bool run(bool oneShot=false); \
53  public: \
54  className##();
55 
56 #define BLOCK_END_INSTANTIATION(className, blockType, keyName) \
57  };bool className##::addedToList = \
58  charliesoft::ProcessManager::getInstance()->addNewAlgo<##className##>(blockType, #keyName);\
59  int className##_InMyLibrary() { return 0; };
60 
61 #define BEGIN_BLOCK_INPUT_PARAMS(className) \
62  std::vector<ParamDefinition> className##::getListParams(){ \
63  std::vector<ParamDefinition> output;
64 
65 #define BEGIN_BLOCK_OUTPUT_PARAMS(className) \
66  std::vector<ParamDefinition> className##::getListOutputs(){ \
67  std::vector<ParamDefinition> output;
68 
69 #define BEGIN_BLOCK_SUBPARAMS_DEF(className) \
70  std::vector<ParamDefinition> className##::getListSubParams(){ \
71  std::vector<ParamDefinition> output;
72 
73 #define ADD_PARAMETER_FULL(show, type, name, helper, initialValue) output.push_back(ParamDefinition( \
74  show, type, name, helper, initialValue));
75 
76 #define ADD_PARAMETER(show, type, name, helper) output.push_back(ParamDefinition( \
77  show, type, name, helper));
78 
79 #define END_BLOCK_PARAMS() return output; }
80 
81 #define ADD_SUBPARAM_FROM_OPENCV_ALGO(algo, paramName, paramValName, name) { \
82  switch(algo->paramType(name)){\
83  case cv::Param::BOOLEAN: \
84  ADD_PARAMETER_FULL(false, Boolean, ((std::string)paramName) + "." + paramValName + "." + name, name, algo->get<bool>(name)); break;\
85  case cv::Param::UNSIGNED_INT: \
86  case cv::Param::UINT64: \
87  case cv::Param::UCHAR: \
88  case cv::Param::INT: \
89  ADD_PARAMETER_FULL(false, Int, ((std::string)paramName) + "." + paramValName + "." + name, name, algo->get<int>(name)); break;\
90  case cv::Param::REAL: \
91  case cv::Param::FLOAT: \
92  ADD_PARAMETER_FULL(false, Float, ((std::string)paramName) + "." + paramValName + "." + name, name, algo->get<double>(name)); break;\
93  case cv::Param::STRING: \
94  ADD_PARAMETER_FULL(false, String, ((std::string)paramName) + "." + paramValName + "." + name, name, (string)algo->get<cv::String>(name)); break;\
95  case cv::Param::MAT: \
96  ADD_PARAMETER_FULL(false, Matrix, ((std::string)paramName) + "." + paramValName + "." + name, name, algo->get<cv::Mat>(name)); break;\
97  } \
98  }
99 
100 
101 namespace charliesoft
102 {
103  class GraphOfProcess;
104  struct BlockLink;
105  class GraphOfProcess;
106  class Block;
107 
109  {
110  unsigned char category_left;
111  ParamValue opt__valueleft;
112  unsigned char category_right;
113  ParamValue opt__valueright;
114  unsigned char boolean_operator;
115  Block* _father;
116  public:
118  ConditionOfRendering(unsigned char category_left,
119  ParamValue opt__valueleft,
120  unsigned char category_right,
121  ParamValue opt__valueright,
122  unsigned char boolean_operator,
123  Block* father);
124  ConditionOfRendering& operator= (const ConditionOfRendering &b)
125  {
126  category_left = b.category_left;
127  opt__valueleft = b.opt__valueleft;
128  category_right = b.category_right;
129  opt__valueright = b.opt__valueright;
130  boolean_operator = b.boolean_operator;
131  if (b._father != NULL)
132  _father = b._father;
133  return *this;
134  }
135  unsigned char getCondition() const { return boolean_operator; }
136  unsigned char getCategory_left() const { return category_left; }
137  unsigned char getCategory_right() const { return category_right; }
138  ParamValue getOpt__valueleft() const { return opt__valueleft; }
139  ParamValue getOpt__valueright() const { return opt__valueright; }
140  Block* getFather() const { return _father; }
141 
142  std::string toString();
143 
144  boost::property_tree::ptree getXML() const;
145 
146  bool canRender(Block* blockTested);
147 
148  void setValue(bool left, ParamValue* val);
149  void setValue(bool left, ParamValue val) {
150  if (left)opt__valueleft = val;
151  else opt__valueright = val;
152  }
153  void setFather(Block* val) { _father = val; }
154  };
155 
156  enum ParamVisibility
157  {
158  notUsed = 0,
159  toBeLinked,
160  userConstant
161  };
162 
164  {
165  ParamVisibility _show;
166  ParamType _type;
167  std::string _name;
168  std::string _helper;
169  ParamValue _initVal;
170  ParamDefinition() :
171  _show(notUsed), _type(typeError), _name("_"), _helper("_"), _initVal(Not_A_Value()){};
172  ParamDefinition(ParamVisibility show, ParamType type, std::string name, std::string helper) :
173  _show(show), _type(type), _name(name), _helper(helper), _initVal(Not_A_Value()){};
174  ParamDefinition(ParamVisibility show, ParamType type, std::string name, std::string helper, ParamValue initVal) :
175  _show(show), _type(type), _name(name), _helper(helper), _initVal(initVal){};
176 
177  ParamDefinition(bool show, ParamType type, std::string name, std::string helper) :
178  ParamDefinition(show ? toBeLinked : userConstant, type, name, helper){};
179  ParamDefinition(bool show, ParamType type, std::string name, std::string helper, ParamValue initVal) :
180  ParamDefinition(show ? toBeLinked : userConstant, type, name, helper, initVal){};
181  };
182 
184  {
185  boost::posix_time::time_duration totalTime;
186  boost::posix_time::time_duration _maxTime;
187  boost::posix_time::time_duration _minTime;
188  int nbMeasures;
189  public:
190  AlgoPerformance();
191  int getMeanPerf() const;
192  int getMaxPerf() const{ return nbMeasures == 0 ? 0 : static_cast<int>(_maxTime.total_milliseconds()); };
193  int getMinPerf() const{ return nbMeasures == 0 ? 0 : static_cast<int>(_minTime.total_milliseconds()); };
194  void addNewPerf(boost::posix_time::time_duration newTime);
195  };
196 
197  class Block{
198  friend class GraphOfProcess;
199  friend class charliesoft::ProcessManager;
200  public:
207  {
208  synchrone = 0,
209  asynchrone
210  };
211 
222  {
223  waitingChild = 0,
224  consumingParams,
225  consumedParams,
226  waitingConsumers,
227  stopped,
228  paused
229  };
230  protected:
231  boost::thread::id _threadID;
232  boost::posix_time::ptime _time_start;
234 
236  BlockType _exec_type;
237  unsigned int nbRendering;
239  boost::condition_variable _cond_pause;
240  boost::mutex _mtx;
241  boost::condition_variable _wait_consume;
242  boost::condition_variable _wait_processed;
243  boost::mutex _mtx_timestamp_inc;
244  unsigned int _timestamp;
245 
246  std::string _error_msg;
247  std::string _name;
248  cv::Point2f _position;
249  cv::Point2f _sizeIncrement;
250 
251  std::string _currentPreview;
252 
253  std::vector<ParamDefinition*> _algorithmInParams;
254  std::vector<ParamDefinition*> _algorithmSubParams;
255  std::vector<ParamDefinition*> _algorithmOutParams;
256 
257 
258  std::map<std::string, ParamValue> _myOutputs;
259  std::map<std::string, ParamValue> _myInputs;
260  std::map<std::string, ParamValue> _mySubParams;
261  std::vector<ConditionOfRendering> _conditions;
262 
266  void initParameters(const std::vector<ParamDefinition>& inParam,
267  const std::vector<ParamDefinition>& outParam);
268 
269  void newProducedData();
270  void paramsFullyProcessed();
271 
272  bool _executeOnlyOnce;
273  bool _newData;
274  bool _isOneShot;
275  bool _addInputParam;
276  public:
283  Block(std::string name, bool isOneShot, BlockType typeExec = synchrone, bool addInputParam = false);
285  virtual ~Block();
286  std::string getName(){
287  return _name;
288  };
289  BlockType getTypeExec(){
290  return _exec_type;
291  };
292 
293  void operator()();
294  virtual bool run(bool oneShot = false) = 0;
295 
296  bool hasDynamicParams() const { return _addInputParam; };
301  ParamValue* addNewInput(ParamDefinition* param);
306  ParamValue* addNewOutput(ParamDefinition* param);
307 
308  virtual void init(){};
309  virtual void release(){};
310 
312  void markAsUnprocessed();
313 
314  ParamDefinition* getParamDefinition(std::string nameOfParam, bool isInput);
315  const ParamDefinition* getParamDefinition(std::string nameOfParam, bool isInput) const;
316  bool paramDefinitionExist(std::string nameOfParam, bool isInput);
317 
318  std::string getCurrentPreview() const { return _currentPreview; }
319  void setCurrentPreview(std::string val) { _currentPreview = val; }
320 
321  const AlgoPerformance& getPerf() const { return _perfCounter; }
322 
323  virtual void setGraph(GraphOfProcess* processes){
324  _processes = processes;
325  };
326  GraphOfProcess* getGraph() const { return _processes; };
327 
328  BlockState getState() const { return _state; };
329  void setState(BlockState val) { _state = val; };
330 
331  BlockType getExecType() const { return _exec_type; }
332  void setExecType(BlockType val) { _exec_type = val; }
333 
334  unsigned int getNbRendering() const { return nbRendering; }
335  unsigned int getTimestamp(){ return _timestamp; };
336  std::string getErrorInfo(){ return _error_msg; };
337  bool validateParams(std::string param, const ParamValue val);
339  bool isReadyToRun(bool realCheck=false);
340 
341  void addCondition(ConditionOfRendering& c){
342  if (_conditions.empty())
343  _conditions.push_back(c);
344  else
345  _conditions[0] = c;
346  };
347  std::vector<ConditionOfRendering>& getConditions(){
348  return _conditions;
349  };
350 
351  virtual void setParamValue(std::string nameParam_, ParamValue* value);
352  virtual void setParam(std::string nameParam_, ParamValue value);
353  virtual ParamValue* getParam(std::string nameParam_, bool input);
354 
355  virtual std::vector<cv::String> getSubParams(std::string paramName);
356 
357  bool isStartingBlock();
358  bool isAncestor(Block* other);
359  bool hasNewParameters();//true if at least one parameter has timestamp>block timestamp
360  void wakeUpFromConsumers();
361  void notifyProduction();
362  void wakeUpFromPause();
363  void waitConsumers(boost::unique_lock<boost::mutex>& lock);
364  void waitProducers(boost::unique_lock<boost::mutex>& lock);
365  boost::mutex& getMutex(){
366  return _mtx;
367  };
368  boost::mutex& getMutexTimestamp(){
369  return _mtx_timestamp_inc;
370  };
371 
372  std::vector<BlockLink> getInEdges();
373 
374  std::string getErrorMsg();
375 
376  const cv::Point2f& getPosition() const { return _position; }
377  const cv::Point2f& getSizeIncrement() const { return _sizeIncrement; }
378  void updatePosition(float x, float y) { _position.x = x; _position.y = y; };
379  void linkParam(std::string paramName, Block* dest, std::string paramNameDest);
380 
381  virtual boost::property_tree::ptree getXML();
382  virtual void initFromXML(boost::property_tree::ptree* tree,
383  std::vector < std::pair<ParamValue*, unsigned int> >& toUpdate,
384  std::map<unsigned int, ParamValue*>& addressesMap,
385  std::vector<ConditionOfRendering*>& condToUpdate);
386 
387  void setPosition(int x, int y, float incX, float incY);
388  void setIncrSize(float incX, float incY);
389  void removeCondition();
390 
392  void update();
393  bool shouldExecuteOnlyOnce() const { return _executeOnlyOnce; }
394  void setExecuteOnlyOnce(bool val) { _executeOnlyOnce = val; }
395 
396  std::vector<ParamDefinition*>& getInParams();
397  std::vector<ParamDefinition*>& getOutParams();
398  std::map<std::string, ParamValue>const & getInputsVals() const { return _myInputs; }
399  std::map<std::string, ParamValue>const & getOutputsVals() const { return _myOutputs; }
400 
401  ParamDefinition getDef(std::string name, bool isInput) const;
402  };
403 
404  struct BlockLink
405  {
406  Block* _from;
407  Block* _to;
408  std::string _fromParam;
409  std::string _toParam;
410  BlockLink(){ _from = _to = NULL; };
411  BlockLink(Block* from, Block* to, std::string fromParam, std::string toParam)
412  {
413  _from = from; _to = to; _fromParam = fromParam; _toParam = toParam;
414  }
415  bool operator==(const BlockLink &b) const{
416  return (_from == b._from) && (_to == b._to) &&
417  (_fromParam.compare(b._fromParam) == 0) && (_toParam.compare(b._toParam) == 0);
418  }
419  bool operator<(const BlockLink & record2) const{
420  if (_from < record2._from) return true;
421  if (_from > record2._from) return false;
422  if (_to < record2._to) return true;
423  if (_to > record2._to) return false;
424  if (_fromParam < record2._fromParam) return true;
425  if (_fromParam > record2._fromParam) return false;
426  return _toParam < record2._toParam;
427  }
428  bool operator<(BlockLink & record2){
429  if (_from < record2._from) return true;
430  if (_from > record2._from) return false;
431  if (_to < record2._to) return true;
432  if (_to > record2._to) return false;
433  if (_fromParam < record2._fromParam) return true;
434  if (_fromParam > record2._fromParam) return false;
435  return _toParam < record2._toParam;
436  }
437  };
438 
439 }
440 
441 #endif
std::vector< ConditionOfRendering > _conditions
Conditions needed for block rendering.
Definition: Block.h:261
std::map< std::string, ParamValue > _myOutputs
output value and name of parameters
Definition: Block.h:258
boost::condition_variable _wait_consume
parameter consumption sync condition
Definition: Block.h:241
Definition: Block.h:163
AlgoPerformance _perfCounter
Used to record algo performance.
Definition: Block.h:233
void initParameters(const std::vector< ParamDefinition > &inParam, const std::vector< ParamDefinition > &outParam)
Definition: Block.cpp:584
Definition: Graph.h:26
boost::mutex _mtx
explicit mutex declaration
Definition: Block.h:240
std::map< std::string, ParamValue > _mySubParams
sub-params value and name of parameters
Definition: Block.h:260
Definition: Block.h:108
ParamValue * addNewOutput(ParamDefinition *param)
Definition: Block.cpp:475
boost::condition_variable _cond_pause
pause condition
Definition: Block.h:239
bool canRender(Block *blockTested)
Definition: Block.cpp:141
Block(std::string name, bool isOneShot, BlockType typeExec=synchrone, bool addInputParam=false)
Definition: Block.cpp:281
Definition: ParamValue.h:33
boost::condition_variable _wait_processed
Block has processed the input datas.
Definition: Block.h:242
boost::posix_time::ptime _time_start
used to measure processing time...
Definition: Block.h:232
void markAsUnprocessed()
Use this function to reset state of process (mark every input/output as old)
Definition: Block.cpp:387
std::vector< ParamDefinition * > _algorithmSubParams
definition of each input sub-parameters
Definition: Block.h:254
Definition: Block.h:183
BlockState
Definition: Block.h:221
Definition: Block.h:197
std::string toString()
Definition: Block.cpp:91
ParamValue * addNewInput(ParamDefinition *param)
Definition: Block.cpp:465
void update()
Re-run this block. If links exist, this will also render every ancestors.
Definition: Block.cpp:363
boost::mutex _mtx_timestamp_inc
Timestamps update.
Definition: Block.h:243
std::vector< ParamDefinition * > _algorithmOutParams
definition of each output parameters
Definition: Block.h:255
BlockState _state
Current state of the block.
Definition: Block.h:235
std::vector< ParamDefinition * > _algorithmInParams
definition of each input parameters
Definition: Block.h:253
GraphOfProcess * _processes
list of process currently in use
Definition: Block.h:238
bool isReadyToRun(bool realCheck=false)
if realCheck is true, we look for links being ready to consume
Definition: Block.cpp:514
virtual ~Block()
The derived block may want a personalized destructor...
Definition: Block.cpp:259
BlockType
Definition: Block.h:206
std::map< std::string, ParamValue > _myInputs
input value and name of parameters
Definition: Block.h:259
unsigned int _timestamp
timestamp of produced values
Definition: Block.h:244
Definition: ParamValue.h:52
Definition: ProcessManager.h:33