test-viewport3D.C

Go to the documentation of this file.
00001 /*!@file AppMedia/test-viewport3D.C test the opengl viewport */
00002 
00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2003   //
00004 // by the University of Southern California (USC) and the iLab at USC.  //
00005 // See http://iLab.usc.edu for information about this project.          //
00006 // //////////////////////////////////////////////////////////////////// //
00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected //
00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency //
00009 // in Visual Environments, and Applications'' by Christof Koch and      //
00010 // Laurent Itti, California Institute of Technology, 2001 (patent       //
00011 // pending; application number 09/912,225 filed July 23, 2001; see      //
00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status).     //
00013 // //////////////////////////////////////////////////////////////////// //
00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit.       //
00015 //                                                                      //
00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can   //
00017 // redistribute it and/or modify it under the terms of the GNU General  //
00018 // Public License as published by the Free Software Foundation; either  //
00019 // version 2 of the License, or (at your option) any later version.     //
00020 //                                                                      //
00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope  //
00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the   //
00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
00024 // PURPOSE.  See the GNU General Public License for more details.       //
00025 //                                                                      //
00026 // You should have received a copy of the GNU General Public License    //
00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write   //
00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,   //
00029 // Boston, MA 02111-1307 USA.                                           //
00030 // //////////////////////////////////////////////////////////////////// //
00031 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu>
00032 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppMedia/test-viewport3D.C $
00033 // $Id: test-viewport3D.C 14486 2011-02-08 05:50:33Z lior $
00034 
00035 
00036 #include "GUI/ViewPort3D.H"
00037 #include "GUI/SuperQuadric.H"
00038 #include "Util/log.H"
00039 #include "Util/WorkThreadServer.H"
00040 #include "Util/JobWithSemaphore.H"
00041 #include "Component/ModelManager.H"
00042 #include "Raster/GenericFrame.H"
00043 #include "Image/Layout.H"
00044 #include "Image/MatrixOps.H"
00045 #include "Image/DrawOps.H"
00046 #include "GUI/DebugWin.H"
00047 #include "Media/FrameSeries.H"
00048 #include "Transport/FrameInfo.H"
00049 #include <stdlib.h>
00050 #include <math.h>
00051 
00052 
00053 int main(int argc, char *argv[]){
00054 
00055   ModelManager manager("Test Viewport");
00056 
00057   nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00058   manager.addSubComponent(ofs);
00059 
00060   // Parse command-line:
00061   if (manager.parseCommandLine(argc, argv, "", 0, 0) == false) return(1);
00062   // let's get all our ModelComponent instances started:
00063   manager.start();
00064 
00065   bool testTexture = false;
00066   bool testFeedback = false;
00067   bool testSQ = false;
00068   bool testGetPosition = true;
00069 
00070 
00071   //Test texture
00072   if (testTexture)
00073   {
00074     ViewPort3D vp(320,240, false, false, false);
00075     vp.setCamera(Point3D<float>(0,0,300), Point3D<float>(0,0,0));
00076 
00077     vp.initFrame();
00078     Image<PixRGB<byte> > texture1(64,64,ZEROS);
00079     for(int i=0; i<texture1.getHeight(); i++)
00080       for(int j=0; j<texture1.getWidth(); j++)
00081       {
00082         int c = ((((i&0x8)==0)^((j&0x8)==0)))*255;
00083         texture1.setVal(j,i, PixRGB<byte>(0,c,c));
00084       }
00085     uint checkBoard1 = vp.addTexture(texture1);
00086 
00087     for(uint i=0; i<360*2; i++)
00088     {
00089       LINFO("i %i", i);
00090 
00091       vp.initFrame();
00092       vp.bindTexture(checkBoard1);
00093 
00094       glRotatef(i, 0,1,0);
00095       glBegin(GL_QUADS);
00096 
00097 
00098       glTexCoord2f(0.0, 0.0); 
00099       glVertex3f( 40, 40, 0);                        // Top Right Of The Quad (Front)
00100 
00101       glTexCoord2f(0.0, 1.0); 
00102       glVertex3f(-40, 40, 0);                        // Top Left Of The Quad (Front)
00103 
00104       glTexCoord2f(1.0, 1.0); 
00105       glVertex3f(-40,-40, 0);                        // Bottom Left Of The Quad (Front)
00106 
00107       glTexCoord2f(1.0, 0.0); 
00108       glVertex3f( 40,-40, 0);                        // Bottom Right Of The Quad (Front)
00109       glEnd();
00110 
00111       Image<PixRGB<byte> > img = flipVertic(vp.getFrame());
00112       ofs->writeRGB(img, "ViewPort3D", FrameInfo("ViewPort3D", SRC_POS));
00113       usleep(10000);
00114     }
00115   }
00116 
00117 
00118   //Check Feedback
00119   if (testFeedback)
00120   {
00121     ViewPort3D vp(320,240, true, true, true);
00122 
00123     vp.setCamera(Point3D<float>(0,0,350), Point3D<float>(0,0,0));
00124 
00125     vp.initFrame();
00126 
00127     glPassThrough (1.0);
00128     vp.drawBox( Point3D<float>(60,60,0), //Position
00129         Point3D<float>(0,0,0), //Rotation
00130         Point3D<float>(30, 30, 50), //size
00131         PixRGB<byte>(0,256,0)
00132         );
00133     glPassThrough (2.0);
00134 
00135     vp.drawCylinder( Point3D<float>(-60,-60,0), //Position
00136         Point3D<float>(0,0,0), //Rotation
00137         30, //radius
00138         50, //length
00139         PixRGB<byte>(256,256,0)
00140         );
00141     
00142 
00143     //Check the feedback
00144     Image<PixRGB<byte> > tmp(320, 240, ZEROS);
00145     std::vector<ViewPort3D::Line> lines = vp.getFrameLines();
00146     for(uint i=0; i<lines.size(); i++)
00147       drawLine(tmp, Point2D<int>(lines[i].p1), Point2D<int>(lines[i].p2), PixRGB<byte>(255,0,0));
00148     LINFO("Lines feedback");
00149     SHOWIMG(tmp);
00150 
00151     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
00152     
00153   }
00154 
00155 
00156 
00157   //Normal operation
00158 
00159 
00160   if (testGetPosition)
00161   {
00162     ViewPort3D vp(320,240, false, false, false);
00163     vp.setProjectionMatrix(90, 320/240, 0.005, 500);
00164     vp.setCamera(Point3D<float>(0,0,30), Point3D<float>(-200,0,0));
00165     while(1)
00166     {
00167       vp.initFrame();
00168 
00169       vp.setColor(PixRGB<byte>(255,255,255));
00170       vp.drawGrid(Dims(300,300), Dims(10,10));
00171       vp.drawCircle(Point3D<float>(0,0,0),
00172           Point3D<float>(0,0,0), //Rotation
00173           1, PixRGB<byte>(255,0,0));
00174       
00175       Image<PixRGB<byte> > img = flipVertic(vp.getFrame());
00176 
00177       Point3D<float> loc2D(159,164,1);
00178       Point3D<float> loc3D = vp.getPosition(loc2D);
00179       LINFO("2D loc %f,%f,%f => 3D loc %f,%f,%f",
00180           loc2D.x, loc2D.y, loc2D.z,
00181           loc3D.x, loc3D.y, loc3D.z);
00182 
00183       loc2D = Point3D<float>(159,23,1);
00184       loc3D = vp.getPosition(loc2D);
00185       LINFO("2D loc %f,%f,%f => 3D loc %f,%f,%f",
00186           loc2D.x, loc2D.y, loc2D.z,
00187           loc3D.x, loc3D.y, loc3D.z);
00188 
00189       SHOWIMG(img);
00190       ofs->writeRGB(img, "ViewPort3D", FrameInfo("ViewPort3D", SRC_POS));
00191     }
00192 
00193 
00194     vp.initFrame();
00195 
00196     vp.drawGrid(Dims(300,300), Dims(10,10));
00197     
00198     Point3D<float> loc2D(158,120,1);
00199     Point3D<float> loc3D = vp.getPosition(loc2D);
00200     LINFO("2D loc %f,%f,%f => 3D loc %f,%f,%f",
00201         loc2D.x, loc2D.y, loc2D.z,
00202         loc3D.x, loc3D.y, loc3D.z);
00203 
00204     loc2D = Point3D<float>(213,160,1);
00205     loc3D = vp.getPosition(loc2D);
00206     LINFO("2D loc %f,%f,%f => 3D loc %f,%f,%f",
00207         loc2D.x, loc2D.y, loc2D.z,
00208         loc3D.x, loc3D.y, loc3D.z);
00209 
00210     //Check the feedback
00211     Image<PixRGB<byte> > tmp(320, 240, ZEROS);
00212     std::vector<ViewPort3D::Line> lines = vp.getFrameLines();
00213     for(uint i=0; i<lines.size(); i++)
00214       drawLine(tmp, Point2D<int>(lines[i].p1), Point2D<int>(lines[i].p2), PixRGB<byte>(255,0,0));
00215     LINFO("Lines feedback");
00216     SHOWIMG(tmp);
00217 
00218   }
00219 
00220   ViewPort3D vp(320,240, false, true, false);
00221   vp.setCamera(Point3D<float>(0,0,350), Point3D<float>(-180,0,0));
00222 
00223 
00224   if (testSQ)
00225   {
00226       SuperQuadric superQuadric;
00227       while(1)
00228       {
00229         for(float n=0; n<1; n+=0.5)
00230           for(float e=0; e<1; e+=0.5)
00231           {
00232             for(float rot=0; rot<360; rot++)
00233             {
00234               vp.initFrame();
00235 
00236               vp.setColor(PixRGB<byte>(255,255,255));
00237               glRotatef(rot, 0,0,1);
00238               glRotatef(45, 1,0,0);
00239 
00240 
00241               superQuadric.its_a1 =30; 
00242               superQuadric.its_a2 = 30;
00243               superQuadric.its_a3 = 30;
00244               superQuadric.its_n = n;
00245               superQuadric.its_e = e;
00246               superQuadric.its_u1 = -M_PI / 2;
00247               superQuadric.its_u2 = M_PI / 2;
00248               superQuadric.its_v1 = -M_PI;
00249               superQuadric.its_v2 = M_PI;
00250               superQuadric.its_s1 = 0.0f;
00251               superQuadric.its_t1 = 0.0f;
00252               superQuadric.its_s2 = 1.0f;
00253               superQuadric.its_t2 = 1.0f;
00254 
00255               //glDisable (GL_CULL_FACE);
00256 
00257               superQuadric.solidEllipsoid();
00258 
00259               Image<PixRGB<byte> > img = flipVertic(vp.getFrame());
00260               ofs->writeRGB(img, "ViewPort3D", FrameInfo("ViewPort3D", SRC_POS));
00261               usleep(10000);
00262             }
00263           }
00264       }
00265   }
00266 
00267   int rot = 0;
00268   while(1)
00269   {
00270     vp.initFrame();
00271 
00272     rot = ((rot +1)%360);
00273 
00274     //vp.drawGround(Point2D<float>(100,100),
00275     //              PixRGB<byte>(255,255,255));
00276 
00277     ////////Test Contour display (counter needs to be specified counter clockwise
00278     std::vector<Point2D<float> > contour;
00279 
00280     float scale = 5;
00281     contour.push_back(Point2D<float>(0, 3)*scale);
00282     contour.push_back(Point2D<float>(1, 1)*scale);
00283     contour.push_back(Point2D<float>(5, 1)*scale);
00284     contour.push_back(Point2D<float>(8, 4)*scale);
00285     contour.push_back(Point2D<float>(10, 4)*scale);
00286     contour.push_back(Point2D<float>(11, 5)*scale);
00287     contour.push_back(Point2D<float>(11, 11.5)*scale);
00288     contour.push_back(Point2D<float>(13, 12)*scale);
00289     contour.push_back(Point2D<float>(13, 13)*scale);
00290     contour.push_back(Point2D<float>(10, 13.5)*scale);
00291     contour.push_back(Point2D<float>(13, 14)*scale);
00292     contour.push_back(Point2D<float>(13, 15)*scale);
00293     contour.push_back(Point2D<float>(11, 16)*scale);
00294     contour.push_back(Point2D<float>(8, 16)*scale);
00295     contour.push_back(Point2D<float>(7, 15)*scale);
00296     contour.push_back(Point2D<float>(7, 13)*scale);
00297     contour.push_back(Point2D<float>(8, 12)*scale);
00298     contour.push_back(Point2D<float>(7, 11)*scale);
00299     contour.push_back(Point2D<float>(6, 6)*scale);
00300     contour.push_back(Point2D<float>(4, 3)*scale);
00301     contour.push_back(Point2D<float>(3, 2)*scale);
00302     contour.push_back(Point2D<float>(1, 2)*scale);
00303 
00304     //Center the contour
00305     Point2D<float> center = centroid(contour);
00306     for(uint i=0; i<contour.size(); i++)
00307       contour[i] -= center;
00308 
00309     vp.drawExtrudedContour(contour,
00310         Point3D<float>(-60,60,0), //Position
00311         Point3D<float>(rot,rot,0), //Rotation
00312         30.0F,
00313         PixRGB<byte>(256,0,0));
00314 
00315 
00316     //Test the box display
00317     vp.drawBox( Point3D<float>(60,60,0), //Position
00318         Point3D<float>(rot,rot,0), //Rotation
00319         Point3D<float>(30, 30, 50), //size
00320         PixRGB<byte>(0,256,0)
00321         );
00322 
00323     //Test the sphere display
00324     vp.drawSphere( Point3D<float>(60,-60,0), //Position
00325         Point3D<float>(rot,rot,0), //Rotation
00326         Point3D<float>(30, 30, 60), //size
00327         PixRGB<byte>(0,0,256)
00328         );
00329 
00330     //Test the cylinder display
00331     vp.drawCone( Point3D<float>(-60,-60,0), //Position
00332         Point3D<float>(rot,rot,0), //Rotation
00333         30, //radius
00334         50, //length
00335         PixRGB<byte>(256,256,0)
00336         );
00337 
00338     Image<PixRGB<byte> > img = flipVertic(vp.getFrame());
00339     ofs->writeRGB(img, "ViewPort3D", FrameInfo("ViewPort3D", SRC_POS));
00340     usleep(10000);
00341   }
00342 
00343 
00344   exit(0);
00345 
00346 }
00347 
00348 
Generated on Sun May 8 08:04:11 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3