1 #ifndef _BLOCK_IMGRAPH_HEADER_
2 #define _BLOCK_IMGRAPH_HEADER_
20 #pragma warning(disable:4996 4244 4251 4275 4800 4503 4190)
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>
37 #include "Internationalizator.h"
38 #include "blocks/ParamValue.h"
39 #include "ProcessManager.h"
42 #define BLOCK_BEGIN_INSTANTIATION(className) \
44 class className## :public Block \
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; \
52 virtual bool run(bool oneShot=false); \
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; };
61 #define BEGIN_BLOCK_INPUT_PARAMS(className) \
62 std::vector<ParamDefinition> className##::getListParams(){ \
63 std::vector<ParamDefinition> output;
65 #define BEGIN_BLOCK_OUTPUT_PARAMS(className) \
66 std::vector<ParamDefinition> className##::getListOutputs(){ \
67 std::vector<ParamDefinition> output;
69 #define BEGIN_BLOCK_SUBPARAMS_DEF(className) \
70 std::vector<ParamDefinition> className##::getListSubParams(){ \
71 std::vector<ParamDefinition> output;
73 #define ADD_PARAMETER_FULL(show, type, name, helper, initialValue) output.push_back(ParamDefinition( \
74 show, type, name, helper, initialValue));
76 #define ADD_PARAMETER(show, type, name, helper) output.push_back(ParamDefinition( \
77 show, type, name, helper));
79 #define END_BLOCK_PARAMS() return output; }
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;\
101 namespace charliesoft
103 class GraphOfProcess;
105 class GraphOfProcess;
110 unsigned char category_left;
112 unsigned char category_right;
114 unsigned char boolean_operator;
120 unsigned char category_right,
122 unsigned char boolean_operator,
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)
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; }
144 boost::property_tree::ptree getXML()
const;
150 if (left)opt__valueleft = val;
151 else opt__valueright = val;
153 void setFather(
Block* val) { _father = val; }
165 ParamVisibility _show;
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()){};
175 _show(show), _type(type), _name(name), _helper(helper), _initVal(initVal){};
177 ParamDefinition(
bool show, ParamType type, std::string name, std::string helper) :
178 ParamDefinition(show ? toBeLinked : userConstant, type, name, helper){};
180 ParamDefinition(show ? toBeLinked : userConstant, type, name, helper, initVal){};
185 boost::posix_time::time_duration totalTime;
186 boost::posix_time::time_duration _maxTime;
187 boost::posix_time::time_duration _minTime;
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);
231 boost::thread::id _threadID;
237 unsigned int nbRendering;
246 std::string _error_msg;
248 cv::Point2f _position;
249 cv::Point2f _sizeIncrement;
251 std::string _currentPreview;
267 const std::vector<ParamDefinition>& outParam);
269 void newProducedData();
270 void paramsFullyProcessed();
272 bool _executeOnlyOnce;
283 Block(std::string name,
bool isOneShot,
BlockType typeExec = synchrone,
bool addInputParam =
false);
286 std::string getName(){
294 virtual bool run(
bool oneShot =
false) = 0;
296 bool hasDynamicParams()
const {
return _addInputParam; };
308 virtual void init(){};
309 virtual void release(){};
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);
318 std::string getCurrentPreview()
const {
return _currentPreview; }
319 void setCurrentPreview(std::string val) { _currentPreview = val; }
321 const AlgoPerformance& getPerf()
const {
return _perfCounter; }
323 virtual void setGraph(GraphOfProcess* processes){
326 GraphOfProcess* getGraph()
const {
return _processes; };
331 BlockType getExecType()
const {
return _exec_type; }
332 void setExecType(
BlockType val) { _exec_type = val; }
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);
341 void addCondition(ConditionOfRendering& c){
347 std::vector<ConditionOfRendering>& getConditions(){
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);
355 virtual std::vector<cv::String> getSubParams(std::string paramName);
357 bool isStartingBlock();
358 bool isAncestor(
Block* other);
359 bool hasNewParameters();
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(){
368 boost::mutex& getMutexTimestamp(){
372 std::vector<BlockLink> getInEdges();
374 std::string getErrorMsg();
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);
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);
387 void setPosition(
int x,
int y,
float incX,
float incY);
388 void setIncrSize(
float incX,
float incY);
389 void removeCondition();
393 bool shouldExecuteOnlyOnce()
const {
return _executeOnlyOnce; }
394 void setExecuteOnlyOnce(
bool val) { _executeOnlyOnce = val; }
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; }
401 ParamDefinition getDef(std::string name,
bool isInput)
const;
408 std::string _fromParam;
409 std::string _toParam;
413 _from = from; _to = to; _fromParam = fromParam; _toParam = toParam;
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);
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;
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;
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
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
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
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
BlockState
Definition: Block.h:221
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