MRXT: The Multi-Robot eXploration Tool
Multi-Robot autonomous exploration and mapping simulator.
src/architecture/reactive/localPotentialField.cpp
00001 #include <opencv2/opencv.hpp>
00002 #include "localPotentialField.h"
00003 #include <stdio.h>
00004 
00005 localPotentialField::localPotentialField(int lzwidth, int lzheight):
00006         width(lzwidth),
00007         height(lzheight),
00008         m(lzwidth, lzheight)
00009 {
00010 }
00011 
00012 localPotentialField::localPotentialField(const localPotentialField &lpf):
00013         width(lpf.width),
00014         height(lpf.height),
00015         m(lpf.m)
00016 {
00017 }
00018 
00019 localPotentialField::localPotentialField(){
00020 }
00021 
00022 localPotentialField::~localPotentialField(){
00023 }
00024 
00025 localPotentialField& localPotentialField::operator=(const localPotentialField& p){
00026         m = ((localPotentialField*) &p)->m;
00027         return *this;
00028 }
00029 
00030 localPotentialField localPotentialField::operator*(const float& f){
00031         localPotentialField np(*this);
00032         np*=f;
00033         return np;
00034 }
00035 
00036 localPotentialField localPotentialField::operator+(const localPotentialField& p){
00037         localPotentialField np(*this);
00038         np += p;
00039         return np;
00040 }
00041 
00042 localPotentialField& localPotentialField::operator+=(const localPotentialField& p){
00043         m += ((localPotentialField*) &p)->m;
00044         return *this;
00045 }
00046 
00047 localPotentialField& localPotentialField::operator*=(const float& f){
00048         m *= f;
00049         return *this;
00050 }
00051 
00052 void localPotentialField::reset(){
00053         m.clear();
00054 }
00055 
00056 void localPotentialField::normalize(){
00057         
00058         float max=m.get(0,0);
00059         float min=m.get(0,0);
00060         int i,j;
00061 
00062         for (i = 0; i< width; i++){
00063                 for(j = 0; j< height; j++){
00064                         if (max<get(i,j)) max=get(i,j);
00065                         if (min>get(i,j)) min=get(i,j);
00066                 }
00067         }
00068 
00069         for (i = 0; i< width; i++)
00070                 for(j = 0; j< height; j++){
00071                         if(max>min)
00072                                 m.set(i,j,(m.get(i,j)-min)/(max-min));  
00073                         else
00074                                 m.set(i,j,0);   
00075                 }
00076 }
00077 
00078 void localPotentialField::savePotentialAsImage(char* file){
00079 
00080         IplImage* img = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1); 
00081         
00082         float max=m.get(0,0);
00083         float min=m.get(0,0);
00084         int i,j;
00085 
00086         for (i = 0; i< width; i++){
00087                 for(j = 0; j< height; j++){
00088                         if (max<get(i,j)) max=get(i,j);
00089                         if (min>get(i,j)) min=get(i,j);
00090                 }
00091         }
00092 
00093         for (i = 0; i< width; i++)
00094                 for( j = 0; j< height; j++)
00095                         ((uchar*)(img->imageData + img->widthStep*(height-1-j)))[i] = (uchar) (255.0f*(m.get(i,j)-min)/(max-min));              
00096 
00097         cvSaveImage(file, img);
00098 
00099         cvReleaseImage(&img);
00100 }
00101 
 All Classes Functions Variables Typedefs