00001 /*! @file pbot/test-motionModel.C test the various motion models */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/pbot/test-motionModel.C $ 00035 // $Id: test-motionModel.C 10794 2009-02-08 06:21:09Z itti $ 00036 // 00037 00038 00039 #include "Component/ModelManager.H" 00040 #include "Image/Image.H" 00041 #include "Image/Transforms.H" 00042 #include "Image/DrawOps.H" 00043 #include "Image/FilterOps.H" 00044 #include "Image/Rectangle.H" 00045 #include "Image/ShapeOps.H" 00046 #include "Image/ColorOps.H" 00047 #include "Image/Rectangle.H" 00048 #include "Image/MathOps.H" 00049 #include "Image/Layout.H" 00050 #include "Media/FrameSeries.H" 00051 #include "Transport/FrameInfo.H" 00052 #include "Raster/GenericFrame.H" 00053 #include "Raster/Raster.H" 00054 #include "GUI/XWinManaged.H" 00055 00056 #define KEY_UP 98 00057 #define KEY_DOWN 104 00058 #define KEY_LEFT 100 00059 #define KEY_RIGHT 102 00060 00061 struct State 00062 { 00063 int x; 00064 int y; //robot position 00065 int ori; //Robot angle in deg 00066 00067 State(int _x, int _y, int _ori) : 00068 x(_x), y(_y), ori(_ori) 00069 {} 00070 }; 00071 00072 00073 void drawRobot(Image<PixRGB<byte> > &img, const State& robotState) 00074 { 00075 drawCircle(img, 00076 Point2D<int>(img.getWidth()/2 + robotState.x, img.getHeight()/2+robotState.y), 00077 15, PixRGB<byte>(0,255,0)); 00078 00079 00080 float ori = (float)robotState.ori*M_PI/180; 00081 int x1 = int(cos(ori)*10); 00082 int y1 = int(sin(ori)*10); 00083 00084 drawLine(img, 00085 Point2D<int>(img.getWidth()/2 + robotState.x, img.getHeight()/2+robotState.y), 00086 Point2D<int>(img.getWidth()/2 + robotState.x+x1, img.getHeight()/2+robotState.y+y1), 00087 PixRGB<byte>(255,0,0)); 00088 00089 00090 } 00091 00092 State moveRobot (State& curState, float tranVel, float rotVel) 00093 { 00094 00095 rotVel += 1.0e-10; //Avoid divide by 0 00096 float dt = 1.0; 00097 State newState(0,0,0); 00098 float ori = (float)curState.ori*M_PI/180; 00099 newState.x = curState.x + (int)(-tranVel/rotVel*sin(ori) + tranVel/rotVel*sin(ori+rotVel*dt)); 00100 newState.y = curState.y + (int)(tranVel/rotVel*cos(ori) - tranVel/rotVel*cos(ori+rotVel*dt)); 00101 newState.ori = curState.ori + (int)((rotVel*dt)*180/M_PI); 00102 return newState; 00103 00104 } 00105 00106 float sample(float b) 00107 { 00108 int dist = 1; //normal dist 00109 switch (dist) 00110 { 00111 case 0: //normal dist 00112 { 00113 float sum =0; 00114 //normal dist 00115 for(int i=0; i<12; i++) 00116 sum += (randomDouble()*b*2)-b; 00117 return 0.5*sum; 00118 } 00119 case 1: //Triangular dist 00120 { 00121 return sqrt(6.0)/2.0 * 00122 (((randomDouble()*b*2)-b) + ((randomDouble()*b*2)-b)); 00123 } 00124 } 00125 00126 return 0; 00127 } 00128 00129 00130 void drawSamples(Image<PixRGB<byte> > &img, float tranVel, float rotVel, const State& curState) 00131 { 00132 rotVel += 1.0e-10; //Avoid divide by 0 00133 float dt = 1.0; 00134 00135 float alpha_1 = 1.5; float alpha_2 = 0; 00136 float alpha_3 = 0.5; float alpha_4 = 0; 00137 float alpha_5 = 0.2; float alpha_6 = 0; 00138 00139 00140 float tranVel_hat = tranVel + sample(alpha_1+alpha_2); 00141 float rotVel_hat = rotVel + sample(alpha_3 + alpha_4); 00142 float gamma_hat = sample(alpha_5 + alpha_6); 00143 00144 State predictState(0,0,0); 00145 float ori = (float)curState.ori*M_PI/180; 00146 predictState.x = curState.x + (int)(-tranVel_hat/rotVel_hat*sin(ori) + tranVel_hat/rotVel_hat*sin(ori+rotVel_hat*dt)); 00147 predictState.y = curState.y + (int)(tranVel_hat/rotVel_hat*cos(ori) - tranVel_hat/rotVel_hat*cos(ori+rotVel_hat*dt)); 00148 predictState.ori = curState.ori + (int)((rotVel_hat*dt)*180/M_PI) + (int)((gamma_hat*dt)*180/M_PI); 00149 00150 Point2D<int> pPt(img.getWidth()/2 + predictState.x, img.getHeight()/2+predictState.y); 00151 if (img.coordsOk(pPt)) 00152 img.setVal(pPt, PixRGB<byte>(255,0,255)); 00153 00154 } 00155 00156 00157 int main(const int argc, const char **argv) 00158 { 00159 MYLOGVERB = LOG_INFO; 00160 ModelManager *mgr = new ModelManager("Test Motion Models"); 00161 00162 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(*mgr)); 00163 mgr->addSubComponent(ofs); 00164 00165 mgr->exportOptions(MC_RECURSE); 00166 00167 if (mgr->parseCommandLine( 00168 (const int)argc, (const char**)argv, "", 0, 0) == false) 00169 return 1; 00170 00171 mgr->start(); 00172 00173 initRandomNumbers(); 00174 00175 State robotState(0,0,0); 00176 00177 00178 while(1) 00179 { 00180 Image<PixRGB<byte> > world(512,512, ZEROS); 00181 LINFO("State: %i,%i %i", robotState.x, robotState.y, robotState.ori); 00182 drawRobot(world, robotState); 00183 00184 float transVel = 100; 00185 float rotVel = 1; 00186 00187 for(int i=0; i<100; i++) 00188 drawSamples(world, transVel, rotVel, robotState); 00189 00190 robotState = moveRobot(robotState, transVel, rotVel); 00191 00192 ofs->writeRGB(world, "Output", FrameInfo("Output", SRC_POS)); 00193 getchar(); 00194 00195 } 00196 mgr->stop(); 00197 00198 return 0; 00199 } 00200