World3DInput.C

Go to the documentation of this file.
00001 /*!@file Transport/World3DInput.C Simple 3D world */
00002 
00003 // //////////////////////////////////////////////////////////////////// //
00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the //
00005 // 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/Transport/World3DInput.C $
00035 // $Id: World3DInput.C 12999 2010-03-11 02:20:08Z lior $
00036 //
00037 
00038 #include "Transport/World3DInput.H"
00039 #include "Component/OptionManager.H"
00040 #include "Image/DrawOps.H"
00041 #include "Image/MatrixOps.H"
00042 #include "Image/Transforms.H"
00043 #include "Image/ColorOps.H"
00044 #include "Raster/GenericFrame.H"
00045 
00046 // ######################################################################
00047 World3DInput::World3DInput(OptionManager& mgr) :
00048   FrameIstream(mgr, "World3DInput Input", "World3DInputInput"),
00049   itsImageDims(320,240)
00050 {
00051   initRandomNumbersZero();
00052 
00053   itsViewPort = new ViewPort3D(320,240);
00054   double trans[3][4] = {
00055     {-0.996624, 0.070027, 0.042869, -16.907477},
00056     {-0.004359, 0.476245, -0.879302, 9.913470},
00057     {-0.081990, -0.876520, -0.474332, 276.648010}};
00058   itsViewPort->setCamera(trans);
00059 
00060   itsViewPort->initFrame(); //TODO: hack, this needs to be called because the lighting changes after the first call
00061 
00062   itsObjects.push_back (
00063       Object(Object::BOX,
00064         Point3D<float>(-90,50,15),
00065         Point3D<float>(0.0,0.00,0),
00066         Point3D<float>(1.0, 1.0, 1.0),
00067         Point3D<float>(30,30,30) ));
00068 
00069 }
00070 
00071 World3DInput::~World3DInput()
00072 {
00073 }
00074 
00075 // ######################################################################
00076 void World3DInput::setConfigInfo(const std::string& dimsstring)
00077 {
00078   // NOTE: if you modify any behavior here, then please update the
00079   // corresponding documentation for the global "--in" option inside
00080   // the OPT_InputFrameSource definition in Media/MediaOpts.C
00081 
00082   if (dimsstring.size() == 0)
00083     return;
00084 
00085   Dims d; convertFromString(dimsstring, d);
00086   this->setImageDims(d);
00087 }
00088 
00089 // ######################################################################
00090 GenericFrameSpec World3DInput::peekFrameSpec()
00091 {
00092   GenericFrameSpec result;
00093 
00094   result.nativeType = GenericFrame::RGB_U8;
00095   result.videoFormat = VIDFMT_AUTO;
00096   result.videoByteSwap = false;
00097   result.dims = itsImageDims;
00098   result.floatFlags = 0;
00099 
00100   return result;
00101 }
00102 
00103 // ######################################################################
00104 GenericFrame World3DInput::readFrame()
00105 {
00106 
00107   itsObjects[0].rotation.z += 1;
00108   if (itsObjects[0].rotation.z > 90)
00109   {
00110     itsObjects[0].rotation.z = 0;
00111 
00112     itsObjects[0].pos.x += 10;
00113     if (itsObjects[0].pos.x > 90)
00114     {
00115       itsObjects[0].pos.x = -100;
00116 
00117       itsObjects[0].pos.y -= 10;
00118       LINFO("Pos %f", itsObjects[0].pos.y);
00119       if (itsObjects[0].pos.y < -100)
00120       {
00121         itsObjects[0].pos.y = 50;
00122       }
00123     }
00124   }
00125 
00126   //itsObjects[0].pos.x = -10.80;
00127   //itsObjects[0].pos.y = -90.15;
00128   //itsObjects[0].rotation.z = 52;
00129 
00130 
00131   //itsObjects[0].pos.x = 0;
00132   //itsObjects[0].pos.y = 0;
00133   //itsObjects[0].rotation.z = 52;
00134 
00135   generateWorld();
00136 
00137   Image<PixRGB<byte> > result = getImage();
00138 
00139   rutz::shared_ptr<ObjectsData> objectsData(new ObjectsData);
00140   for(uint i=0; i<itsObjects.size(); i++)
00141     objectsData->objects.push_back(itsObjects[i]);
00142 
00143   usleep(10000);
00144   GenericFrame frame(result);
00145   frame.addMetaData(std::string("ObjectsData"), objectsData);
00146   return frame;
00147 }
00148 
00149 // ######################################################################
00150 void World3DInput::setImageDims(const Dims& s)
00151 {
00152   itsImageDims = s;
00153 }
00154 
00155 void World3DInput::drawWheelAndBolts(bool rightSide)
00156 {
00157   glColor3f(0.5,0.5,0.5);
00158 
00159   //Wheel
00160   glBegin(GL_TRIANGLE_FAN);
00161   for(int i=0; i<20; i++)
00162   {
00163     float ang = 2*M_PI*i/20;
00164     glVertex2f(cos(ang)*0.55, sin(ang)*0.55);
00165   }
00166   glEnd();
00167 
00168   for(int i=0; i<=4; i++)
00169   {
00170     glPushMatrix();
00171     glRotatef(72.0*i, 0.0, 0.0, 1.0);
00172     if (rightSide)
00173       glTranslatef(0.2, 0.0, 0.01);
00174     else
00175       glTranslatef(0.2, 0.0, -0.01);
00176     glColor3f(0.3, 0.3, 0.3);
00177 
00178     //Bolt
00179     glBegin(GL_TRIANGLE_FAN);
00180     for(int i=0; i<6; i++)
00181     {
00182       float ang = 2*M_PI*i/6;
00183       glVertex2f(cos(ang)*0.05, sin(ang)*0.05);
00184     }
00185     glEnd();
00186 
00187     glPopMatrix();
00188   }
00189 
00190 }
00191 
00192 
00193 void World3DInput::drawCar()
00194 {
00195     glColor3f(1.0,0.0,0.0);
00196 
00197     glScalef(20.0,20.0,20.0);
00198     glRotatef(90.0,1.0,0.0, 0.0);
00199     glBegin(GL_QUADS);
00200       // Front Face
00201       glNormal3f( 0.0, 0.0, 1.0);
00202       glVertex3f(-2.0, -0.5,  1.0);
00203       glVertex3f( 2.0, -0.5,  1.0);
00204       glVertex3f( 2.0,  0.5,  1.0);
00205       glVertex3f(-2.0,  0.5,  1.0);
00206       // Back Face
00207       glNormal3f( 0.0, 0.0,-1.0);
00208       glVertex3f(-2.0, -0.5, -1.0);
00209       glVertex3f(-2.0,  0.5, -1.0);
00210       glVertex3f( 2.0,  0.5, -1.0);
00211       glVertex3f( 2.0, -0.5, -1.0);
00212       // Top Face
00213       glNormal3f( 0.0, 1.0, 0.0);
00214       glVertex3f(-2.0,  0.5, -1.0);
00215       glVertex3f(-2.0,  0.5,  1.0);
00216       glVertex3f( 2.0,  0.5,  1.0);
00217       glVertex3f( 2.0,  0.5, -1.0);
00218       // Bottom Face
00219       glNormal3f( 0.0,-1.0, 0.0);
00220       glVertex3f(-2.0, -0.5, -1.0);
00221       glVertex3f( 2.0, -0.5, -1.0);
00222       glVertex3f( 2.0, -0.5,  1.0);
00223       glVertex3f(-2.0, -0.5,  1.0);
00224       // Right face
00225       glNormal3f( 1.0, 0.0, 0.0);
00226       glVertex3f( 2.0, -0.5, -1.0);
00227       glVertex3f( 2.0,  0.5, -1.0);
00228       glVertex3f( 2.0,  0.5,  1.0);
00229       glVertex3f( 2.0, -0.5,  1.0);
00230       // Left Face
00231       glNormal3f(-2.0, 0.0, 0.0);
00232       glVertex3f(-2.0, -0.5, -1.0);
00233       glVertex3f(-2.0, -0.5,  1.0);
00234       glVertex3f(-2.0,  0.5,  1.0);
00235       glVertex3f(-2.0,  0.5, -1.0);
00236 
00237       // Cabin
00238       // Front Face
00239       glNormal3f( 0.0, 0.0, 1.0);
00240       glVertex3f(-0.5,  0.5,  1.0);
00241       glVertex3f( 0.5,  0.5,  1.0);
00242       glVertex3f( 0.5,  1.0,  1.0);
00243       glVertex3f(-0.5,  1.0,  1.0);
00244       // Back Face
00245       glNormal3f( 0.0, 0.0,-1.0);
00246       glVertex3f(-0.5,  0.5, -1.0);
00247       glVertex3f(-0.5,  1.0, -1.0);
00248       glVertex3f( 0.5,  1.0, -1.0);
00249       glVertex3f( 0.5,  0.5, -1.0);
00250       // Top Face
00251       glNormal3f( 0.0, 1.0, 0.0);
00252       glVertex3f(-0.5,  1.0, -1.0);
00253       glVertex3f(-0.5,  1.0,  1.0);
00254       glVertex3f( 0.5,  1.0,  1.0);
00255       glVertex3f( 0.5,  1.0, -1.0);
00256 
00257       // Right face
00258       glNormal3f( 1.0, 0.0, 0.0);
00259       glVertex3f( 0.5,  0.5, -1.0);
00260       glVertex3f( 0.5,  1.0, -1.0);
00261       glVertex3f( 0.5,  1.0,  1.0);
00262       glVertex3f( 0.5,  0.5,  1.0);
00263       // Left Face
00264       glNormal3f(-0.5, 0.0, 0.0);
00265       glVertex3f(-0.5,  0.5, -1.0);
00266       glVertex3f(-0.5,  0.5,  1.0);
00267       glVertex3f(-0.5,  1.0,  1.0);
00268       glVertex3f(-0.5,  1.0, -1.0);
00269 
00270     glEnd();
00271 
00272     glPushMatrix();
00273       glTranslatef(-1.0,-0.5,1.01);                    // move to first wheel position
00274       drawWheelAndBolts(true);
00275     glPopMatrix();
00276 
00277     glPushMatrix();
00278       glTranslatef(-1.0,-0.5,-1.01);                   // move to 2nd wheel position
00279       drawWheelAndBolts(false);
00280     glPopMatrix();
00281 
00282     glPushMatrix();
00283       glTranslatef(1.0,-0.5,-1.01);                    // move to 3nd wheel position
00284       drawWheelAndBolts(false);
00285     glPopMatrix();
00286 
00287     glPushMatrix();
00288       glTranslatef(1.0,-0.5,1.01);                     // move to 3nd wheel position
00289       drawWheelAndBolts(true);
00290     glPopMatrix();
00291 
00292 
00293 
00294 }
00295 
00296 
00297 void World3DInput::generateWorld()
00298 {
00299 
00300   itsViewPort->initFrame();
00301 
00302   //drawCar();
00303 
00304 
00305   for(uint i=0; i<itsObjects.size(); i++)
00306   {
00307     switch (itsObjects[i].type)
00308     {
00309       case Object::BOX:
00310         itsViewPort->drawBox(
00311             itsObjects[i].pos,
00312             itsObjects[i].rotation,
00313             itsObjects[i].params,
00314             PixRGB<byte>(0,256,0));
00315         break;
00316       default:
00317         break;
00318 
00319     }
00320   }
00321 
00322   itsWorldImg = flipVertic(itsViewPort->getFrame());
00323 
00324   itsFrame++;
00325 }
00326 
00327 Image<PixRGB<byte> > World3DInput::getImage()
00328 {
00329 
00330   return itsWorldImg;
00331 
00332 }
00333 
00334 // ######################################################################
00335 /* So things look consistent in everyone's emacs... */
00336 /* Local Variables: */
00337 /* indent-tabs-mode: nil */
00338 /* End: */
Generated on Sun May 8 08:42:24 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3