pointFilter.hpp

Go to the documentation of this file.
00001 
00002 
00003 const bool  PointFilterKey::trueValue_m(1);
00004 const bool  PointFilterKey::falseValue_m(0);
00005 
00006 const  CFilterStrategy  CFilterStrategy::FilterRandomStart_Strategy_s( CFilterStrategy::CF_FILTER_RANDOM_START );
00007 const  CFilterStrategy  CFilterStrategy::FilterRandomSync_Strategy_s( CFilterStrategy::CF_FILTER_RANDOM_SYNC );
00008 const  CFilterStrategy  CFilterStrategy::FilterSaveFirst_Strategy_s( CFilterStrategy::CF_FILTER_SAVE_FIRST );
00009 
00010 
00011 const   std::string     CFilterStrategy::cfRandomStartStr_s( "randomStart" );
00012 const   std::string     CFilterStrategy::cfRandomSyncStr_s ( "randomSync" );
00013 const   std::string     CFilterStrategy::cfSaveFirstStr_s  ( "saveFirst"  );
00014 
00015 
00016 
00017 inline PointFilterKey::PointFilterKey():        exactVanishedFocalValues_m( 0 ),
00018                                                         jacobianRank_m( 0 ), 
00019                                                         smooth_m( 0 ), 
00020                                                         quadricsRank_m( 0 )
00021 {
00022         initRule();
00023 }
00024 
00025                  
00026         
00027 inline PointFilterKey::PointFilterKey(  uint16_t exactVanishedFocalValues,      
00028                                                         uint16_t        jacobianRank,
00029                                                         bool    smooth,
00030                                                         uint16_t        quadricsRank
00031                                                         ):      
00032                                                                 exactVanishedFocalValues_m( exactVanishedFocalValues ),
00033                                                                 jacobianRank_m( jacobianRank ), 
00034                                                                 smooth_m( smooth ), 
00035                                                                 quadricsRank_m( quadricsRank )
00036 {}
00037         
00038 
00039 inline PointFilterKey::PointFilterKey(std::string str) 
00040 {
00041         initRule();
00042         //bool parsed=false;
00043         
00044         std::cerr << " creating PointFilterKey from  " << str << std::endl;
00045 
00046         smooth_m=-1;
00047                 parse_info<>   pi =  boost::spirit::parse(str.c_str(), *pointFilterKeyRule_m, boost::spirit::space_p);
00048 
00049         if ( ! pi.full )
00050         {
00051                 std::cerr << " creating PointFilterKey from  " << str << " failed" << std::endl;
00052 
00053                 // beib Testen (CF_TEST) sollte kein harter assert stattfinden, sondern vielleicht ein Throw
00054                 // in der scharfen Version sollte dagegen ein assert stattfinden.
00055                 assert(false); 
00056         }
00057         assert(smooth_m!=-1);
00058         assert(smooth_m==falseValue_m ||smooth_m==trueValue_m);
00059         print( std::cerr );
00060         std::cerr << std::endl;
00061 };
00062 
00063 const rule<> *   PointFilterKey::initRule()
00064 {
00065 
00067 
00068         pointFilterKeyRule_m = new rule<> (space_rule_g >> str_p("(") >> space_rule_g >> 
00069                         int_p[ assign_a( exactVanishedFocalValues_m ) ] >> spacy_colon_g >> 
00070                         int_p [ assign_a( jacobianRank_m ) ] >> spacy_colon_g  >> 
00071                         (str_p("false")[ assign_a( smooth_m,falseValue_m )  ] | str_p("true")[ assign_a( smooth_m,trueValue_m ) ] )  >> spacy_colon_g  >>
00072                         int_p[ assign_a(quadricsRank_m)] >> space_rule_g >>  
00073         str_p(")") >> space_rule_g);
00074         
00075         return pointFilterKeyRule_m;
00076 }
00077 
00078 
00079 inline void     PointFilterKey::print(std::ostream &os) const
00080 {
00081         os << "( "<< exactVanishedFocalValues_m<< "," << jacobianRank_m << "," ;
00082         if (smooth_m)
00083                 os << "true, ";
00084         else
00085                 os << "false, ";
00086         os << quadricsRank_m << " )" ;
00087 }
00088 inline bool PointFilterKey::operator<(const PointFilterKey& pfkey)      const
00089 {
00090 
00091         /*std::cerr << "key comparison" << std::endl;
00092         this->print(std::cerr);
00093         std::cerr <<   std::endl;
00094         pfkey.print(std::cerr);
00095         std::cerr <<   std::endl;*/
00096 
00097         if (exactVanishedFocalValues_m!=pfkey.exactVanishedFocalValues_m)
00098                 return exactVanishedFocalValues_m<pfkey.exactVanishedFocalValues_m;
00099 
00100         if (jacobianRank_m!=pfkey.jacobianRank_m)
00101                 return jacobianRank_m<pfkey.jacobianRank_m;
00102 
00103         if (smooth_m!=pfkey.smooth_m)
00104                 return smooth_m<pfkey.smooth_m;
00105 
00106         return quadricsRank_m<pfkey.quadricsRank_m;
00107 }
00108 
00109 
00110 
00111 
00112 
00113 
00114 inline  PointFilterRule::PointFilterRule(): key_m(),
00115                                                                 passFilterRate_m(1) 
00116 {
00117         initRule();
00118 };
00119         
00120 inline  PointFilterRule::PointFilterRule(std::string str)  :  key_m(),
00121                                                                                         passFilterRate_m(1) 
00122 {
00123 
00124         initRule();
00125         //bool parsed=false;
00126         
00127 
00128                 parse_info<>   pi =  boost::spirit::parse(str.c_str(), *pointFilterRule_m, boost::spirit::space_p);
00129 
00130         if ( ! pi.full )
00131         {
00132                 std::cerr << " creating PointFilterRule from  " << str << " failed" << std::endl;
00133 
00134                 // beib Testen (CF_TEST) sollte kein harter assert stattfinden, sondern vielleicht ein Throw
00135                 // in der scharfen Version sollte dagegen ein assert stattfinden.
00136                 assert(false); 
00137         }
00138 };
00139 
00140 
00141 inline  PointFilterRule::PointFilterRule(       const PointFilterKey & key, 
00142                         uint64_t        filterRate              ) :     key_m(key), 
00143                                                                         passFilterRate_m(filterRate)
00144 {
00145         initRule();
00146 }
00147         
00148 inline  PointFilterRule::PointFilterRule(       uint16_t exactVanishedFocalValues,      
00149                                                                 uint16_t        jacobianRank,
00150                                                                 bool    smooth,
00151                                                                 uint16_t        quadricsRank,
00152                                                                 uint64_t        filterRate
00153                                                                 ):      key_m( exactVanishedFocalValues,
00154                                                                                  jacobianRank, smooth,  quadricsRank ),
00155                                                                         passFilterRate_m(filterRate)
00156 {
00157         initRule();
00158 }
00159 
00160 inline  const rule<>*   PointFilterRule::initRule()
00161 {
00162         const rule<>& keyRuleRef=key_m.getConstRuleRef();
00163 
00164         
00165         pointFilterRule_m = new rule<>( space_rule_g >> str_p("[") >> space_rule_g >>  
00166                                                         keyRuleRef>>  spacy_colon_g >>
00167                                                                 int_p [ assign_a( passFilterRate_m ) ] >> space_rule_g  
00168                                                         >> str_p("]") >> space_rule_g 
00169                                                 );
00170         return pointFilterRule_m;
00171 }
00172 
00173 
00174 void            PointFilterRule::print(std::ostream & os)
00175 {
00176         os << "[ " ;
00177         key_m.print(os);
00178         os << ", "<< passFilterRate_m << " ] " ;
00179 
00180 }
00181 
00182 
00183 
00184 inline  CFilterStrategy::CFilterStrategy( CFilterStrategy::FilterStrategy strategy ):   strategy_m(strategy)
00185 { }
00186 
00187 inline  CFilterStrategy::CFilterStrategy(std::string strFilterStrategy)
00188 {
00189                 
00190         strategy_m=CF_FILTER_UNDEFINED;
00191         const rule<>    filterStrategyRule = ( str_p(cfRandomStartStr_s.c_str())[  assign_a(strategy_m,CF_FILTER_RANDOM_START ) ] |
00192                                                         str_p(cfRandomSyncStr_s.c_str())[ assign_a(strategy_m,CF_FILTER_RANDOM_SYNC ) ] |
00193                                                         str_p(cfSaveFirstStr_s.c_str())[ assign_a(strategy_m,CF_FILTER_SAVE_FIRST ) ] 
00194                                         );
00195 
00196 
00197 
00198 
00199         parse_info<>   pi =  boost::spirit::parse(strFilterStrategy.c_str(), filterStrategyRule, boost::spirit::space_p);
00200 
00201         if ( ! pi.full )
00202         {
00203                 std::cerr << " creating CFilterStrategy from  " << strFilterStrategy << " failed" << std::endl;
00204 
00205                 // beib Testen (CF_TEST) sollte kein harter assert stattfinden, sondern vielleicht ein Throw
00206                 // in der scharfen Version sollte dagegen ein assert stattfinden.
00207                 assert(false); 
00208         }
00209 
00210         assert( strategy_m==CF_FILTER_RANDOM_START || 
00211                         strategy_m==CF_FILTER_RANDOM_SYNC || 
00212                         strategy_m==CF_FILTER_SAVE_FIRST
00213                 );
00214 
00215 }
00216 
00217 inline std::string      CFilterStrategy::getValueAsString()
00218 {
00219         switch (strategy_m)
00220         {
00221                 case CF_FILTER_RANDOM_START:
00222                         return cfRandomStartStr_s ;
00223                         break;
00224                 case CF_FILTER_RANDOM_SYNC:
00225                         return cfRandomStartStr_s ;
00226                         break;
00227                 case CF_FILTER_SAVE_FIRST:
00228                         return cfRandomStartStr_s ;
00229                         break;
00230                 default:
00231                         assert(false);
00232         }
00233         
00234 }
00235 
00236 
00237 inline void     CFPointFilter::onRuleScanned(std::string str)
00238 {
00239         //PointFilterRule*  pfrule = new PointFilterRule(str);
00240         //addFilterRule(*pfrule);
00241         std::cerr << "rule "<<  str<< " scanned " << std::endl;
00242         PointFilterRule  pfrule(str);
00243         addFilterRule(pfrule);
00244 }
00245 
00246 inline  CFPointFilter::CFPointFilter( std::string str ,
00247                                   CFilterStrategy strategy, std::string name ) :        randomSeed_m( CFPointFilter::seedFromTime() ),
00248                                                                                                         randomSeedRef_m(randomSeed_m),
00249                                                                                                         strategy_m(strategy),
00250                                                                                                         singleRuleScannedTrigger_m( new rule_term_scanned(*this)),
00251                                                                                                         name_m(name)
00252 {
00253         
00254         PointFilterRule tmp;
00255 
00256         const rule<> &  filterRuleRef= tmp.getRuleRef();
00257 
00259         // nicht zutreffenden Regel. 
00260         rule<> *pointFilterRule=        new rule<>(     space_rule_g  >> (((  filterRuleRef[*singleRuleScannedTrigger_m]   ) >>
00261                                                                  (  +(  spacy_colon_g >> filterRuleRef[*singleRuleScannedTrigger_m])  | boost::spirit:: eps_p  ) ) | boost::spirit:: eps_p)  >>  
00262                                                         space_rule_g 
00263                                                  );
00264         
00265 
00266         // Achtung: eps_p muss immer ans ende, sonst endlosschleife!
00267         //rule<> *pointFilterRule=      new rule<>(      space_rule_g  >> (   filterRuleRef[*singleRuleScannedTrigger_m]  | boost::spirit:: eps_p) >> space_rule_g );
00268 
00269         
00270                 parse_info<>   pi =  boost::spirit::parse(str.c_str(), *pointFilterRule, boost::spirit::space_p);
00271 
00272         if ( ! pi.full )
00273         {
00274                 std::cerr << " creating CFPointFilter from  " << str << " failed" << std::endl;
00275 
00276                 // beib Testen (CF_TEST) sollte kein harter assert stattfinden, sondern vielleicht ein Throw
00277                 // in der scharfen Version sollte dagegen ein assert stattfinden.
00278                 assert(false); 
00279         }
00280         
00281 }
00282 
00283 inline CFPointFilter::CFPointFilter(     CFilterStrategy        strategy,
00284                                         std::vector<PointFilterRule>    filterRules,
00285                                          std::string name,
00286                                         long &  randomSeedRef   )               :randomSeedRef_m(randomSeedRef),
00287                                                                                                 strategy_m(strategy),
00288                                                                                                 singleRuleScannedTrigger_m(NULL),
00289                                                                                                 name_m(name)
00290 {
00291 
00292 // fuer alle filterRules einen Eintrag in hierarchicalFilterRules_m erzeugen.
00293          
00294         BOOST_FOREACH(const PointFilterRule currentFilterRule, filterRules )
00295         {
00296                 addFilterRule(currentFilterRule);
00297         }
00298 }
00299 
00300 inline CFPointFilter::CFPointFilter(     CFilterStrategy        strategy, 
00301                                         std::vector<PointFilterRule>    filterRules,
00302                                         std::string name
00303                                                                                 )       :       randomSeed_m( CFPointFilter::seedFromTime() ),
00304                                                                                                 randomSeedRef_m(randomSeed_m),
00305                                                                                                 strategy_m(strategy),
00306                                                                                                 singleRuleScannedTrigger_m(NULL),
00307                                                                                                 name_m(name)
00308 {
00309 
00310 // fuer alle filterRules einen Eintrag in hierarchicalFilterRules_m erzeugen.
00311          
00312                 BOOST_FOREACH(const PointFilterRule currentFilterRule, filterRules )
00313         {
00314                 addFilterRule(currentFilterRule);
00315         }
00316 }
00317 
00318 inline CFPointFilter::CFPointFilter(     CFilterStrategy        strategy,       
00319                                                 std::string     name    ) :   randomSeed_m( CFPointFilter::seedFromTime() ),
00320                                                                                                 randomSeedRef_m(randomSeed_m),
00321                                                                                                 strategy_m(strategy),
00322                                                                                                 singleRuleScannedTrigger_m(NULL),
00323                                                                                                 name_m(name)
00324 {
00325 
00326          
00327 }
00328 
00329 inline bool CFPointFilter::pointPassedFilter(   uint16_t        exactVanishedFocalValues,
00330                                                 uint16_t        jacobianRank,
00331                                                 bool    smooth,
00332                                                 uint16_t        quadricsRank) const
00333 {
00334         PointFilterKey  key(exactVanishedFocalValues, jacobianRank, smooth, quadricsRank);
00335         
00336 
00337         hierarchicalFilterRuleMap_Type::const_iterator   it;
00338 
00339         it=hierarchicalFilterRules_m.find(key);
00340         if ( it==hierarchicalFilterRules_m.end() )
00341         {
00342                 //std::cerr << "no filter key" << std::endl;
00343                 //key.print(std::cerr);
00344                 //std::cerr <<   std::endl;
00345                 return true ;
00346         }
00347         else
00348         {
00349                 //std::cerr << "found filter key" << std::endl;
00350                 //key.print(std::cerr);
00351                 //std::cerr <<   std::endl;
00352 
00353                 switch (strategy_m.integral() )
00354                 {
00355                         case CFilterStrategy::CF_FILTER_RANDOM_START:
00356                                 it->second.counter_m++;
00357                                 break;
00358                         case CFilterStrategy::CF_FILTER_RANDOM_SYNC:
00359                                 it->second.counter_m=randomUInt32( &randomSeedRef_m, it->second.getPassFilterRate()-1)+1;
00360                                 //std::cerr << "counter" << it->second.counter_m<< std::endl;
00361                                 break;
00362                         case CFilterStrategy::CF_FILTER_SAVE_FIRST:
00363                                 //std::cerr << "CF_FILTER_SAVE_FIRST counter" << it->second.counter_m<< std::endl;
00364                                 it->second.counter_m++;
00365                 }
00366                 if ( it->second.getPassFilterRate()==it->second.counter_m )
00367                 {       
00368                         switch (strategy_m.integral())
00369                         {
00370                                 case CFilterStrategy::CF_FILTER_RANDOM_START:
00371                                         it->second.counter_m=0;
00372                                         break;
00373                                 case CFilterStrategy::CF_FILTER_RANDOM_SYNC:
00374                                         break;
00375                                 case CFilterStrategy::CF_FILTER_SAVE_FIRST:
00376                                         it->second.counter_m=0;
00377                         }
00378                         return true;
00379                 }
00380         }
00381         return false;
00382 }
00383 
00384 
00385 
00386 inline void     CFPointFilter::addFilterRule(const PointFilterRule & rule)      
00387 {
00388         uint32_t counter=0;
00389         assert(rule.getPassFilterRate()>=1);
00390         switch ( strategy_m.integral() )
00391         {
00392                 case CFilterStrategy::CF_FILTER_RANDOM_START:
00393                         counter=randomUInt32( &randomSeedRef_m, rule.getPassFilterRate()-1)+1;
00394                         break;
00395 
00396                 case CFilterStrategy::CF_FILTER_RANDOM_SYNC:
00397                         counter=randomUInt32( &randomSeedRef_m, rule.getPassFilterRate()-1)+1;
00398                         break;
00399 
00400                 case CFilterStrategy::CF_FILTER_SAVE_FIRST:
00401                         counter=rule.getPassFilterRate()-1;
00402                         break;
00403                 default:
00404                         assert(false);
00405         }
00406         hierarchicalFilterRuleMap_Type::iterator         it  = hierarchicalFilterRules_m.find(rule.getKeyConstRef() );
00407         if ( it != hierarchicalFilterRules_m.end() )
00408         {
00409                 std::cerr << " hierarchicalFilterRules_m  dublicate entry" << std::endl;
00410                 assert(false); 
00411         }
00412         else
00413         {
00414                 hierarchicalFilterRules_m[ rule.getKeyConstRef() ] = CFPointFilter::FilterRateCounterPair(rule.getPassFilterRate(), counter );
00415         }
00416 }
00417 
00418 
00419 inline void  CFPointFilter::addFilterRule(      uint16_t        exactVanishedFocalValues,
00420                                                 uint16_t        jacobianRank,
00421                                                 bool    smooth,
00422                                                 uint16_t        quadricsRank,
00423                                                 uint32_t        filterRate )    
00424 {
00425         PointFilterKey  key(exactVanishedFocalValues, jacobianRank,smooth,quadricsRank);
00426 
00427         PointFilterRule rule(key, filterRate);
00428         
00429         addFilterRule(rule);
00430 }
00431 
00432 
00433 inline void             CFPointFilter::print(std::ostream & os)
00434 {
00435         hierarchicalFilterRuleMap_Type::iterator         it  = hierarchicalFilterRules_m.begin();
00436         while( it != hierarchicalFilterRules_m.end() )
00437         {
00438                 if ( it != hierarchicalFilterRules_m.begin() )
00439                         os << "\n\t";
00440                 PointFilterRule pfrule( (*it).first,(*it).second.getPassFilterRate() );
00441                 pfrule.print(os);
00442         
00443                 it++;
00444         }
00445 }
00446 
00447 inline  void            CFPointFilter::printInMacaulayStyle(std::ostream & os)
00448 {
00449         os << name_m << "  =  ";
00450         print(os);
00451         os << " ;";
00452 }
00453 
Generated on Tue Nov 23 13:10:52 2010 for centerfocus by  doxygen 1.6.3