Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ShapeSLAMParticleFilter.cc

Go to the documentation of this file.
00001 //-*-c++-*-
00002 
00003 #include <iostream>
00004 #include <cmath>
00005 #if defined(PLATFORM_APERIOS) && !defined(HUGE_VALF)
00006 #  define HUGE_VALF HUGE_VAL
00007 #endif
00008 
00009 #include "Crew/MapBuilder.h"
00010 #include "Motion/WalkMC.h"
00011 #ifdef TGT_HAS_WALK
00012 #  include "Crew/Pilot.h"
00013 #endif
00014 #include "DualCoding/ShapeSpace.h"
00015 #include "DualCoding/VRmixin.h"  // for mapBuilder
00016 #include "DualCoding/ShapeLocalizationParticle.h"
00017 #include "ShapeSLAMParticleFilter.h"
00018 #include "ShapeLandmarks.h"
00019 
00020 using namespace std;
00021 
00022 namespace DualCoding {
00023 
00024   ShapeSLAMParticleEvaluator::ShapeSLAMParticleEvaluator(ShapeSpace &localShS, ShapeSpace &worldShS, float addPenalty) :
00025   LocalShapeEvaluator(localShS,worldShS), localMobile(false), worldMobile(false), ADDITION_PENALTY(addPenalty)
00026     {
00027     for ( unsigned int indexL=0; indexL < localLms.size(); indexL++ )
00028       localMobile |= localLms[indexL]->mobile;
00029     for ( unsigned int indexW=0; indexW < worldLms.size(); indexW++ )
00030       worldMobile |= worldLms[indexW]->mobile;
00031     }
00032   
00033   void ShapeSLAMParticleEvaluator::evaluate(ShapeSLAMParticle& part) {
00034     unsigned int const nLocals = localLms.size();
00035     float particleViewX[nLocals],  particleViewY[nLocals], particleViewX2[nLocals],  particleViewY2[nLocals];
00036     int localMatches[nLocals];
00037     float localScores[nLocals];
00038     LocalShapeEvaluator::evaluateWorkhorse(part, nLocals, particleViewX, particleViewY, particleViewX2, particleViewY2,
00039              localMatches, localScores);
00040     for(unsigned int indexL=0; indexL < nLocals; indexL++)
00041       part.addLocal[indexL] = ( localMatches[indexL] == -1 && localLms[indexL]->mobile );
00042     if ( localMobile )
00043       determineAdditions(part, nLocals, localMatches, localScores);
00044     if ( worldMobile )
00045       determineDeletions(part, nLocals, localMatches, particleViewX, particleViewY, particleViewX2, particleViewY2);
00046     updateWeight(part, localMatches, localScores);
00047 
00048     // << ": " << bestProb << endl;
00049   }
00050 
00051   void ShapeSLAMParticleEvaluator::determineAdditions
00052   (ShapeSLAMParticle& part, unsigned int const nLocals,  int localMatches[], float localScores[]) {
00053     for (unsigned int indexL = 0; indexL < nLocals; indexL++) {
00054       if ( localLms[indexL]->mobile  ) {
00055   float const randval = float(rand()) / (float(RAND_MAX)*6);
00056   //**** WARNING: this code assumes we're using log weights (which is true by default)
00057   if (randval >= localScores[indexL]) {
00058     part.addLocal[indexL] = true;
00059     localScores[indexL] = ADDITION_PENALTY;
00060     localMatches[indexL] = -1000 -  indexL;  // any value other than -1 will count as a match
00061     continue;
00062   }
00063   // if two local LMs match the same worldLM, treat the poorer match as an addition
00064   for (unsigned int indexL2 = (indexL+1); indexL2 < nLocals; indexL2++) {
00065     if (localMatches[indexL2] != localMatches[indexL] || localMatches[indexL2] == -1)
00066       continue;
00067     if (localScores[indexL2] > localScores[indexL]) {
00068       part.addLocal[indexL] = true;
00069       localMatches[indexL] = -1000 - indexL;
00070       localScores[indexL] = ADDITION_PENALTY;
00071     } else {
00072       part.addLocal[indexL2] = true;
00073       localMatches[indexL2] = -1000 - indexL2;
00074       localScores[indexL2] = ADDITION_PENALTY;
00075     }
00076   }
00077       }
00078     }
00079   }
00080 
00081   void ShapeSLAMParticleEvaluator::determineDeletions
00082     (ShapeSLAMParticle& part, unsigned int const nLocals, int const localMatches[],
00083      float const particleViewX[], float const particleViewY[], float const particleViewX2[], float const particleViewY2[]) {
00084     part.deleteWorld.assign(part.deleteWorld.size(),true);
00085     float minXLoc = HUGE_VALF;
00086     float minYLoc = HUGE_VALF;
00087     float maxXLoc = -HUGE_VALF;
00088     float maxYLoc = -HUGE_VALF;
00089     for (unsigned int indexL = 0; indexL<nLocals; indexL++) {
00090       if ( localMatches[indexL] != -1 )
00091   part.deleteWorld[localMatches[indexL]] = false;  // don't delete world LM if it matches
00092       if ( particleViewX[indexL] < minXLoc )
00093   minXLoc = particleViewX[indexL];
00094       else if (particleViewX[indexL] > maxXLoc)
00095   maxXLoc = particleViewX[indexL];
00096       if (particleViewY[indexL] < minYLoc)
00097   minYLoc = particleViewY[indexL];
00098       else if (particleViewY[indexL] > maxYLoc)
00099   maxYLoc = particleViewY[indexL];
00100       if ( localLms[indexL]->type == lineDataType ) {
00101   if ( particleViewX2[indexL] < minXLoc )
00102     minXLoc = particleViewX2[indexL];
00103   else if (particleViewX2[indexL] > maxXLoc)
00104     maxXLoc = particleViewX2[indexL];
00105   if (particleViewY2[indexL] < minYLoc)
00106     minYLoc = particleViewY2[indexL];
00107   else if (particleViewY2[indexL] > maxYLoc)
00108     maxYLoc = particleViewY2[indexL];
00109       }
00110     }
00111   
00112     for (unsigned int indexW = 0; indexW<worldLms.size(); indexW++)
00113       if ( ! worldLms[indexW]->mobile ||
00114      !( worldLms[indexW]->x >= minXLoc && worldLms[indexW]->x <= maxXLoc &&
00115         worldLms[indexW]->y >= minYLoc && worldLms[indexW]->y <= maxYLoc ) )
00116   part.deleteWorld[indexW] = false; // don't delete world LM if not mobile, or it was outside local view
00117   }
00118 
00119 
00120   void ShapeSLAMParticleFilter::setAgent() const {
00121 #ifdef TGT_HAS_WALK
00122     //    const ShapeSLAMParticleFilter::particle_type& best;
00123     //    VRmixin::pilot->setAgent(Point(best.x,best.y), best.theta, true);
00124 #endif
00125   }
00126 
00127   void ShapeSLAMParticleFilter::displayParticles(float const howmany) const {
00128     cout << "displayParticles not available for ShapeSLAMParticleFilter at present." << endl;
00129     return;
00130     ShapeSpace &wShS = sensorModel->getWorldShS();
00131     wShS.deleteShapes<LocalizationParticleData>();
00132     NEW_SHAPE(best, LocalizationParticleData, NULL); //new LocalizationParticleData(wShS,particles,bestIndex));
00133     best->setColor(VRmixin::mapBuilder->getAgent()->getColor());
00134     if ( howmany <= 0 ) return;
00135     int increment;
00136     if ( howmany <= 1.0 )
00137       increment = (int)ceil(1.0/howmany);
00138     else
00139       increment = (int)ceil(particles.size()/howmany);
00140     for (unsigned int i=0; i<particles.size(); i+=increment)
00141       continue;
00142       //      if ( i != bestIndex ) {
00143       //  NEW_SHAPE(pt, LocalizationParticleData, NULL); //new LocalizationParticleData(wShS,particles,i));
00144       //      }
00145   }
00146 
00147   ostream& operator << (ostream& os, const ShapeSLAMParticle &p){
00148     os << "Particle(p=" << p.weight
00149        << ", dx=" << p.x
00150        << ", dy=" << p.y
00151        << ", th=" << p.theta;
00152   
00153     os << ", add=";
00154     for (unsigned int i = 0; i<p.addLocal.size(); i++)
00155       os << p.addLocal[i];
00156   
00157     os << ", del=";
00158     for (unsigned int i = 0; i<p.deleteWorld.size(); i++)
00159       os << p.deleteWorld[i];
00160   
00161     os << ")";
00162     return os;
00163   }
00164 
00165 } // namespace

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:50 2016 by Doxygen 1.6.3