00001 /*!@file Qt/simulation.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/simulation.cpp $ 00035 // $Id: simulation.cpp 5796 2005-10-27 23:57:10Z rjpeters $ 00036 // 00037 00038 #include "simulation.h" 00039 #include <cmath> 00040 00041 Simulation::Simulation(QWidget *parent, const char *name) 00042 :QGLWidget(parent, name) 00043 { 00044 setFormat(QGLFormat(DoubleBuffer | DepthBuffer)); 00045 sizeOfList = 0; 00046 angle = 0.0F; 00047 scale = 1.0F; 00048 right = false; 00049 maxX=minX=maxY=minY=0; 00050 00051 colors[0] = yellow; 00052 colors[1] = blue; 00053 colors[2] = green; 00054 00055 colors[3] = white; 00056 colors[4] = red; 00057 colors[5] = QColor(255,85,0); 00058 colors[6] = QColor(0,170,255); 00059 } 00060 00061 00062 void Simulation::initializeGL() 00063 { 00064 qglClearColor(black); 00065 glShadeModel(GL_FLAT); 00066 glEnable(GL_DEPTH_TEST); 00067 glEnable(GL_CULL_FACE); 00068 } 00069 00070 void Simulation::resizeGL(int w, int h) 00071 { 00072 glViewport(0, 0, w, h); 00073 glMatrixMode(GL_PROJECTION); 00074 glLoadIdentity(); 00075 GLfloat x = (GLfloat)w/h; 00076 glFrustum(-x, x, -1.0, 1.0, 4.0, 15.0); 00077 glMatrixMode(GL_MODELVIEW); 00078 } 00079 00080 void Simulation::paintGL() 00081 { 00082 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00083 draw(); 00084 } 00085 00086 void Simulation::draw() 00087 { 00088 int i;//, ci; 00089 00090 glMatrixMode(GL_MODELVIEW); 00091 glLoadIdentity(); 00092 glTranslatef(0.0,0.0,-10.0); 00093 00094 // draw the line with certain rotation which can be set from the gui 00095 qglColor(white); 00096 glRotatef(0+angle,0,0,1); 00097 for(i=0 ; i<40 ; i++) 00098 { 00099 glBegin(GL_LINES); 00100 glVertex2f(-5.0+i*0.25, -5.0); 00101 glVertex2f(-5.0+i*0.25,5.0); 00102 glEnd(); 00103 } 00104 00105 //glTranslate(1.0,1.0,0.0); 00106 glBegin(GL_TRIANGLES); 00107 glVertex2f(0.0,0.1); 00108 glVertex2f(-0.1,-0.1); 00109 glVertex2f(0.1,-0.1); 00110 glEnd(); 00111 glBegin(GL_QUADS); 00112 glVertex2f(-0.025,-0.2); 00113 glVertex2f(0.025,-0.2); 00114 glVertex2f(0.025,-0.1); 00115 glVertex2f(-0.025,-0.1); 00116 glEnd(); 00117 00118 } 00119 00120 void Simulation::mousePressEvent(QMouseEvent *event) 00121 { 00122 lastPos = event->pos(); 00123 } 00124 00125 void Simulation::mouseReleaseEvent(QMouseEvent *event) 00126 { 00127 /* 00128 maxX = event->x(); 00129 maxY = event->y(); 00130 printf("mis::%d,%d\n", dragPos.x(), dragPos.y()); 00131 printf("rclick:%d,%d\n", maxX, maxY); 00132 for(int i=0 ; i<sizeOfList ; i++) 00133 if(iconlist[i].selected == 2) 00134 iconlist[i].selected = 1; 00135 for(int i=0 ; i<sizeOfList ; i++) 00136 { 00137 if(iconlist[i].x> dragPos.x()&& iconlist[i].x<maxX && 00138 iconlist[i].y> dragPos.y()&& iconlist[i].y<maxY) 00139 { 00140 iconlist[i].selected = 2; 00141 } 00142 } 00143 //minX = maxX = minY = maxY = 0; 00144 updateGL(); 00145 */ 00146 } 00147 00148 void Simulation::mouseMoveEvent(QMouseEvent *event) 00149 { 00150 int dx = event->x() - lastPos.x(); 00151 int dy = event->y() - lastPos.y(); 00152 lastPos = event->pos(); 00153 // check every element in the list 00154 for(int i=0 ; i<sizeOfList ; i++) 00155 { 00156 if(event->state() & LeftButton) 00157 { 00158 if(//lastPos.x() < (iconlist[i].x)+(iconlist[i].edgeSize)/2 && 00159 //lastPos.x() > (iconlist[i].x)-(iconlist[i].edgeSize)/2 && 00160 //lastPos.y() < (iconlist[i].y)+(iconlist[i].edgeSize)/2 && 00161 //lastPos.y() > (iconlist[i].y)-(iconlist[i].edgeSize)/2 && 00162 iconlist[i].selected ==2) 00163 { 00164 iconlist[i].setPosition(dx, dy); 00165 updateGL(); 00166 } 00167 } 00168 else if(event->state() & RightButton) 00169 { 00170 } 00171 00172 } 00173 } 00174 00175 void Simulation::createIcon(int num) 00176 { 00177 int i; 00178 int oldSize = sizeOfList; 00179 sizeOfList = num; 00180 00181 // if there are previous generated iconImage objects 00182 if(oldSize != 0 && oldSize <= sizeOfList) 00183 { 00184 iconImage *temp = (iconImage *)malloc(sizeof(iconImage)*(sizeOfList+4)); 00185 for(i=0 ; i<oldSize ; i++) 00186 temp[i] = iconlist[i]; 00187 for(i = oldSize ; i<sizeOfList ; i++) 00188 { 00189 iconlist[i].x = 300; 00190 iconlist[i].y = 220; 00191 iconlist[i].glx = 0.0; 00192 iconlist[i].gly = 0.0; 00193 iconlist[i].edgeSize = 22; 00194 iconlist[i].selected = 0; 00195 } 00196 } 00197 else 00198 { 00199 iconlist = (iconImage *)malloc(sizeof(iconImage)*sizeOfList); 00200 for(i=0; i<sizeOfList ; i++) 00201 { 00202 // for the tasks and the gate 00203 iconlist[i].x = 300; 00204 iconlist[i].y = 220; 00205 iconlist[i].glx = 0.0; 00206 iconlist[i].gly = 0.0; 00207 iconlist[i].edgeSize = 22; 00208 iconlist[i].selected = 0; 00209 } 00210 } 00211 updateGL(); 00212 } 00213 00214 void Simulation::currentItem(int item) 00215 { 00216 for(int i=0 ; i<sizeOfList ; i++) 00217 { 00218 if(i == item) 00219 iconlist[i].selected = 2; 00220 else 00221 { 00222 if( i < 4) 00223 iconlist[i].selected = 3+i; 00224 else 00225 iconlist[i].selected = 1; 00226 } 00227 } 00228 updateGL(); 00229 } 00230 00231 void Simulation::getCoord(int i, int &x, int &y) 00232 { 00233 x = iconlist[i].x; 00234 y = iconlist[i].y; 00235 } 00236 00237 void Simulation::setCoordByPix(int i, int x, int y) 00238 { 00239 iconlist[i].x = x; 00240 iconlist[i].y = y; 00241 iconlist[i].glx = (GLfloat)(x-300) * 0.01137F; 00242 iconlist[i].gly = -(GLfloat)(y-220) * 0.01137F; 00243 updateGL(); 00244 } 00245 00246 void Simulation::setCoordByGL(int i, float glx, float gly) 00247 { 00248 iconlist[i].glx = glx; 00249 iconlist[i].gly = gly; 00250 iconlist[i].x = int(glx/0.01137F + 300.0F + 0.5F); 00251 iconlist[i].y = int(gly/0.01137F + 220.0F + 0.5F); 00252 updateGL(); 00253 } 00254