00001 /*!@file Qt/poolimage.cpp */ 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: 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt/poolimage.cpp $ 00035 // $Id: poolimage.cpp 12269 2009-12-17 01:23:34Z itti $ 00036 // 00037 00038 00039 #ifndef QT_POOL_IMAGE_C_DEFINED 00040 #define QT_POOL_IMAGE_C_DEFINED 00041 00042 #ifdef INVT_HAVE_QT3 00043 00044 #include "poolimage.h" 00045 #include <cmath> 00046 00047 PoolImage::PoolImage(QWidget *parent, const char *name) 00048 :QGLWidget(parent, name) 00049 { 00050 setFormat(QGLFormat(DoubleBuffer | DepthBuffer)); 00051 sizeOfList = 0; 00052 angle = 0.0F; 00053 scale = 1.0F; 00054 right = false; 00055 maxX=minX=maxY=minY=0; 00056 00057 colors[0] = yellow; 00058 colors[1] = blue; 00059 colors[2] = green; 00060 00061 colors[3] = white; 00062 colors[4] = red; 00063 colors[5] = QColor(255,85,0); 00064 colors[6] = QColor(0,170,255); 00065 } 00066 00067 00068 void PoolImage::initializeGL() 00069 { 00070 qglClearColor(black); 00071 glShadeModel(GL_FLAT); 00072 glEnable(GL_DEPTH_TEST); 00073 glEnable(GL_CULL_FACE); 00074 } 00075 00076 void PoolImage::resizeGL(int w, int h) 00077 { 00078 glViewport(0, 0, w, h); 00079 glMatrixMode(GL_PROJECTION); 00080 glLoadIdentity(); 00081 GLfloat x = (GLfloat)w/h; 00082 glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0); 00083 glMatrixMode(GL_MODELVIEW); 00084 } 00085 00086 void PoolImage::paintGL() 00087 { 00088 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00089 draw(); 00090 } 00091 00092 void PoolImage::draw() 00093 { 00094 int theone=-1, i, ci; 00095 00096 static const GLfloat coords[4][2] = 00097 {{-0.125,-0.125},{0.125,-0.125}, 00098 {0.125,0.125},{-0.125,0.125}}; 00099 00100 glMatrixMode(GL_MODELVIEW); 00101 glLoadIdentity(); 00102 glTranslatef(0.0,0.0,-10.0); 00103 // draw the selected icon 00104 for(ci=0; ci<sizeOfList ; ci++) 00105 { 00106 if(iconlist[ci].selected == 2) 00107 { 00108 glBegin(GL_QUADS); 00109 qglColor(colors[iconlist[ci].selected]); 00110 for(i=0 ; i<4 ; i++) 00111 glVertex3f(coords[i][0]+(iconlist[ci].glx), coords[i][1]+(iconlist[ci].gly), 0.0); 00112 glEnd(); 00113 theone = ci; 00114 } 00115 } 00116 00117 for(ci=0; ci<sizeOfList ; ci++) 00118 { 00119 if(ci!=theone) 00120 { 00121 glBegin(GL_QUADS); 00122 qglColor(colors[iconlist[ci].selected]); 00123 for(i=0 ; i<4 ; i++) 00124 glVertex3f(coords[i][0]+(iconlist[ci].glx), coords[i][1]+(iconlist[ci].gly), 0.0); 00125 glEnd(); 00126 } 00127 } 00128 00129 // draw the line with certain rotation which can be set from the gui 00130 qglColor(white); 00131 glRotatef(0+angle,0,0,1); 00132 for(i=0 ; i<40 ; i++) 00133 { 00134 glBegin(GL_LINES); 00135 glVertex2f(-5.0+i*0.25, -5.0); 00136 glVertex2f(-5.0+i*0.25,5.0); 00137 glEnd(); 00138 } 00139 00140 //glTranslate(1.0,1.0,0.0); 00141 glBegin(GL_TRIANGLES); 00142 glVertex2f(0.0,0.1); 00143 glVertex2f(-0.1,-0.1); 00144 glVertex2f(0.1,-0.1); 00145 glEnd(); 00146 glBegin(GL_QUADS); 00147 glVertex2f(-0.025,-0.2); 00148 glVertex2f(0.025,-0.2); 00149 glVertex2f(0.025,-0.1); 00150 glVertex2f(-0.025,-0.1); 00151 glEnd(); 00152 00153 glRotatef(90,0,0,1); 00154 for(i=0 ; i<40 ; i++) 00155 { 00156 glBegin(GL_LINES); 00157 glVertex2f(-5.0+i*0.25, -5.0); 00158 glVertex2f(-5.0+i*0.25,5.0); 00159 glEnd(); 00160 } 00161 00162 } 00163 00164 void PoolImage::mousePressEvent(QMouseEvent *event) 00165 { 00166 lastPos = event->pos(); 00167 } 00168 00169 void PoolImage::mouseDoubleClickEvent(QMouseEvent *event) 00170 { 00171 /* 00172 dragPos = event->pos(); 00173 minX = event->x(); 00174 minY = event->y(); 00175 printf("dclick:%d,%d\n", minX, minY); 00176 */ 00177 } 00178 00179 void PoolImage::mouseReleaseEvent(QMouseEvent *event) 00180 { 00181 /* 00182 maxX = event->x(); 00183 maxY = event->y(); 00184 printf("mis::%d,%d\n", dragPos.x(), dragPos.y()); 00185 printf("rclick:%d,%d\n", maxX, maxY); 00186 for(int i=0 ; i<sizeOfList ; i++) 00187 if(iconlist[i].selected == 2) 00188 iconlist[i].selected = 1; 00189 for(int i=0 ; i<sizeOfList ; i++) 00190 { 00191 if(iconlist[i].x> dragPos.x()&& iconlist[i].x<maxX && 00192 iconlist[i].y> dragPos.y()&& iconlist[i].y<maxY) 00193 { 00194 iconlist[i].selected = 2; 00195 } 00196 } 00197 //minX = maxX = minY = maxY = 0; 00198 updateGL(); 00199 */ 00200 } 00201 00202 void PoolImage::mouseMoveEvent(QMouseEvent *event) 00203 { 00204 int dx = event->x() - lastPos.x(); 00205 int dy = event->y() - lastPos.y(); 00206 lastPos = event->pos(); 00207 // check every element in the list 00208 for(int i=0 ; i<sizeOfList ; i++) 00209 { 00210 if(event->state() & LeftButton) 00211 { 00212 if(//lastPos.x() < (iconlist[i].x)+(iconlist[i].edgeSize)/2 && 00213 //lastPos.x() > (iconlist[i].x)-(iconlist[i].edgeSize)/2 && 00214 //lastPos.y() < (iconlist[i].y)+(iconlist[i].edgeSize)/2 && 00215 //lastPos.y() > (iconlist[i].y)-(iconlist[i].edgeSize)/2 && 00216 iconlist[i].selected ==2) 00217 { 00218 iconlist[i].setPosition(dx, dy); 00219 updateGL(); 00220 } 00221 } 00222 else if(event->state() & RightButton) 00223 { 00224 } 00225 00226 } 00227 } 00228 00229 void PoolImage::createIcon(int num) 00230 { 00231 int i; 00232 int oldSize = sizeOfList; 00233 sizeOfList = num; 00234 00235 // if there are previous generated iconImage objects 00236 if(oldSize != 0 && oldSize <= sizeOfList) 00237 { 00238 iconImage *temp = (iconImage *)malloc(sizeof(iconImage)*(sizeOfList+4)); 00239 for(i=0 ; i<oldSize ; i++) 00240 temp[i] = iconlist[i]; 00241 for(i = oldSize ; i<sizeOfList ; i++) 00242 { 00243 iconlist[i].x = 300; 00244 iconlist[i].y = 220; 00245 iconlist[i].glx = 0.0; 00246 iconlist[i].gly = 0.0; 00247 iconlist[i].edgeSize = 22; 00248 iconlist[i].selected = 0; 00249 } 00250 } 00251 else 00252 { 00253 iconlist = (iconImage *)malloc(sizeof(iconImage)*sizeOfList); 00254 for(i=0; i<sizeOfList ; i++) 00255 { 00256 // for the tasks and the gate 00257 iconlist[i].x = 300; 00258 iconlist[i].y = 220; 00259 iconlist[i].glx = 0.0; 00260 iconlist[i].gly = 0.0; 00261 iconlist[i].edgeSize = 22; 00262 iconlist[i].selected = 0; 00263 } 00264 } 00265 updateGL(); 00266 } 00267 00268 void PoolImage::currentItem(int item) 00269 { 00270 for(int i=0 ; i<sizeOfList ; i++) 00271 { 00272 if(i == item) 00273 iconlist[i].selected = 2; 00274 else 00275 { 00276 if( i < 4) 00277 iconlist[i].selected = 3+i; 00278 else 00279 iconlist[i].selected = 1; 00280 } 00281 } 00282 updateGL(); 00283 } 00284 00285 void PoolImage::getCoord(int i, int &x, int &y) 00286 { 00287 x = iconlist[i].x; 00288 y = iconlist[i].y; 00289 } 00290 00291 void PoolImage::setCoordByPix(int i, int x, int y) 00292 { 00293 iconlist[i].x = x; 00294 iconlist[i].y = y; 00295 iconlist[i].glx = (GLfloat)(x-300) * 0.01137F; 00296 iconlist[i].gly = -(GLfloat)(y-220) * 0.01137F; 00297 updateGL(); 00298 } 00299 00300 void PoolImage::setCoordByGL(int i, float glx, float gly) 00301 { 00302 iconlist[i].glx = glx; 00303 iconlist[i].gly = gly; 00304 iconlist[i].x = int(glx/0.01137F + 300.0F + 0.5F); 00305 iconlist[i].y = int(gly/0.01137F + 220.0F + 0.5F); 00306 updateGL(); 00307 } 00308 00309 float PoolImage::getAngle() 00310 { 00311 return angle; 00312 } 00313 00314 void PoolImage::getRealPosition(int i, float &rx, float &ry) 00315 { 00316 // translate to the original point 00317 float tempx = float(iconlist[i].glx - iconlist[0].glx); 00318 float tempy = -float(iconlist[0].gly - iconlist[i].gly); 00319 float tempa = angle; 00320 00321 if(i == 0) 00322 { 00323 tempx = iconlist[0].glx; 00324 tempy = iconlist[0].gly; 00325 } 00326 // nomalize 00327 while(tempa > 180) 00328 tempa = tempa -360; 00329 while(tempa < -180) 00330 tempa = tempa + 360; 00331 tempa = -tempa/180.0F * 3.14F; 00332 00333 // rotate and scale 00334 rx = (tempx * cos(tempa) - tempy * sin(tempa)) * scale; 00335 ry = (tempx * sin(tempa) + tempy * cos(tempa)) * scale; 00336 /* 00337 if( i == 0) 00338 { 00339 rx = 0.0F; ry = 0.0F; 00340 } 00341 */ 00342 } 00343 00344 void PoolImage::getGLPosition(float rx, float ry, float &gx, float &gy) 00345 { 00346 // scale 00347 float tx, tempx = rx / scale; 00348 float ty, tempy = ry / scale; 00349 float tempa = angle; 00350 00351 // nomalize 00352 while(tempa > 180) 00353 tempa = tempa -360; 00354 while(tempa < -180) 00355 tempa = tempa + 360; 00356 tempa = tempa/180.0F * 3.14F; 00357 00358 // rotate 00359 tx = tempx * cos(tempa) - tempy * sin(tempa); 00360 ty = tempx * sin(tempa) + tempy * cos(tempa); 00361 00362 // translate 00363 gx = tx + iconlist[0].glx; 00364 gy = ty + iconlist[0].gly; 00365 00366 } 00367 00368 #endif // INVT_HAVE_QT3 00369 00370 #endif // QT_POOL_IMAGE_C_DEFINED 00371 00372 // ###################################################################### 00373 /* So things look consistent in everyone's emacs... */ 00374 /* Local Variables: */ 00375 /* mode: c++ */ 00376 /* indent-tabs-mode: nil */ 00377 /* End: */