MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
src/architecture/slam/mapbuilders/particle.cpp
00001 /*
00002 *
00003 * Author: Miguel Julia <mjulia@umh.es> 
00004 * 
00005 * Date:   2008
00006 * 
00007 * Class particle
00008 *
00009 * Implements a particle in a rao-blackwellized particle filter for visual slam
00010 *
00011 *
00012 *
00013 */
00014 
00015 #include "particle.h"
00016 #include <stdio.h>
00017 #include <math.h>
00018 #include "gridMapInterface.h"
00019 
00020 // Constructors
00021 particle::particle():
00022         nrobots(0),
00023         width(0),
00024         height(0),
00025         realWidth(0),
00026         realHeight(0),
00027         xorigin(0),
00028         yorigin(0),
00029         resolution(0),
00030         rpos(0),
00031         rcell(0),
00032         vMap(0),
00033         oMap(0),
00034         precisePoseMap(0),
00035         imprecisePoseMap(0)
00036 {
00037 
00038 }
00039 
00040 particle::particle(int nRobots, float w, float h, float res, float x, float y, int gmtype, int vmtype, int nmarks, const ConfigFile& conf):
00041         nrobots(nRobots),
00042         width((int)floor(w/res+0.5)),
00043         height((int)floor(h/res+0.5)),
00044         realWidth(w),
00045         realHeight(h),
00046         xorigin(x),
00047         yorigin(y),
00048         resolution(res),
00049         weight(1.0),
00050         sumLogWeight(0.0),
00051         rpos(new pose[nRobots]()),
00052         rcell(new point[nRobots]()),
00053         vMap(visualMapFactory::Instance().CreateObject(vmtype,nmarks,conf)),
00054         oMap(0),
00055         precisePoseMap(0),
00056         imprecisePoseMap(0)
00057 {
00058         if (gmtype >=0){
00059                 oMap = gridMapFactory::Instance().CreateObject(gmtype, w, h, res, x, y);
00060                 precisePoseMap = new binMap(width, height, res, x, y);
00061                 imprecisePoseMap = new binMap(width, height, res, x, y);
00062         }
00063 }
00064 
00065 particle::particle(const particle &part):
00066         nrobots                 (part.nrobots),
00067         width                   (part.width),
00068         height                  (part.height),
00069         realWidth               (part.realWidth),
00070         realHeight              (part.realHeight),
00071         xorigin                 (part.xorigin),
00072         yorigin                 (part.yorigin),
00073         resolution              (part.resolution),
00074         weight                  (part.weight),
00075         sumLogWeight    (part.sumLogWeight),
00076         rpos                    (new pose[part.nrobots]()),
00077         rcell                   (new point[part.nrobots]()),
00078         vMap                    (part.vMap->clone()),
00079         oMap                    (0),
00080         precisePoseMap  (0),
00081         imprecisePoseMap(0)
00082 {
00083         if (part.oMap) oMap = part.oMap->clone();
00084         if (part.precisePoseMap) precisePoseMap = new binMap(*(part.precisePoseMap));
00085         if (part.imprecisePoseMap) imprecisePoseMap = new binMap(*(part.imprecisePoseMap));
00086         memcpy(rpos,part.rpos,nrobots*sizeof(pose));
00087         memcpy(rcell,part.rcell,nrobots*sizeof(point));
00088 }
00089 
00090 particle& particle::operator=(const particle& p){
00091         nrobots = p.nrobots;
00092         weight = p.weight;
00093         sumLogWeight = p.sumLogWeight;
00094         
00095         width = p.width;
00096         height = p.height;
00097         realWidth = p.realWidth;
00098         realHeight = p.realHeight;
00099         xorigin = p.xorigin;
00100         yorigin = p.yorigin;
00101         resolution = p.resolution;
00102 
00103         if (rpos) delete[] rpos;
00104         rpos = new pose[nrobots]();
00105         memcpy(rpos,p.rpos,nrobots*sizeof(pose));
00106 
00107         if (rcell) delete[] rcell;
00108         rcell = new point[nrobots]();
00109         memcpy(rcell,p.rcell,nrobots*sizeof(point));
00110 
00111         if (vMap) *vMap = *p.vMap;
00112         else vMap = p.vMap->clone();
00113 
00114         if (p.oMap){
00115                 if (oMap) *oMap = *p.oMap;
00116                 else oMap = p.oMap->clone();
00117         }
00118         if (p.precisePoseMap){
00119                 if (precisePoseMap) *precisePoseMap = *p.precisePoseMap;
00120                 else precisePoseMap = new binMap(*(p.precisePoseMap));
00121         }
00122         if (p.imprecisePoseMap){
00123                 if (imprecisePoseMap) *imprecisePoseMap = *p.imprecisePoseMap;
00124                 else imprecisePoseMap = new binMap(*(p.imprecisePoseMap));
00125         }
00126 
00127         return *this;
00128 }
00129 
00130 particle::~particle(){
00131         if (rpos) delete[] rpos;
00132         if (rcell) delete[] rcell;
00133         if (oMap) delete oMap;
00134         if (vMap) delete vMap;
00135         if (precisePoseMap) delete precisePoseMap;
00136         if (imprecisePoseMap) delete imprecisePoseMap;
00137 }
00138 
00139 // Initializers
00140 void particle::initialize(int r, float w, float h, float res, float x, float y, int gmtype, int vmtype, int nmarks, const ConfigFile& conf){
00141         width           = (int)floor(w/res+0.5);
00142         height          = (int)floor(h/res+0.5);
00143         realWidth       = w;
00144         realHeight      = h;
00145         xorigin         = x;
00146         yorigin         = y;
00147         resolution      = res;
00148         if (rpos) delete[] rpos;
00149         rpos =  new pose[r]();
00150         if (rcell) delete[] rcell;
00151         rcell = new point[r]();
00152         if (vMap) delete vMap;
00153         vMap = visualMapFactory::Instance().CreateObject(vmtype,nmarks,conf);
00154 
00155         if (gmtype >=0){
00156                 if (oMap) delete oMap;
00157                 oMap = gridMapFactory::Instance().CreateObject(gmtype, w, h, res, x, y);
00158                 if (precisePoseMap) delete precisePoseMap;
00159                 precisePoseMap = new binMap(width, height, res, x, y);
00160                 if (imprecisePoseMap) delete imprecisePoseMap;
00161                 imprecisePoseMap = new binMap(width, height, res, x, y);
00162         }
00163         nrobots = r;
00164         weight = 1.0;
00165         sumLogWeight = 0.0;
00166 }
00167 
00168 // falta calcular la celda nueva
00169 void particle::setPos(const pose& pos, int robot){
00170 
00171         rpos[robot].x = pos.x;
00172         rpos[robot].y = pos.y;
00173         rpos[robot].th = pos.th;
00174 
00175         rcell[robot].x = (int) floor((pos.x - xorigin)/resolution);
00176         rcell[robot].y = (int) floor((pos.y - yorigin)/resolution);
00177 }
 All Classes Functions Variables Typedefs