// 顺时针旋转90度, 根据边界范围旋转完成后自行调整图形位置 void Tetris::rotate(int min_x, int max_x, int min_y, int max_y) { if (key < 0) { return; } int maxX = min_x, minX = max_x, maxY = min_y, minY = max_y; // TODO 越界处理 for (int i = 0; i < 4; i++) { BYTE x = positions[i].x, y = positions[i].y; positions[i].x = y - positions[key].y + positions[key].x; positions[i].y = positions[key].x + positions[key].y - x; } getMaxMin(minX, maxX, minY, maxY); // 越界纠正 if (maxX > max_x) { changeX(max_x - maxX); } if (minX < min_x) { changeX(min_x - minX); } if (maxY > max_y) { changeY(max_y - maxY); } if (minY < min_y) { changeY(min_y - minY); } }
void Normalize(pfs::Array2D* lum_map, int nrows, int ncols) { float maxLum, minLum; getMaxMin(lum_map, maxLum, minLum); float range = maxLum - minLum; for (int y = 0; y < nrows; y++) for (int x = 0; x < ncols; x++) (*lum_map)(x, y) = ((*lum_map)(x, y) - minLum) / range; }
void ThickPlateMesh::draw(DataGraphic &data) { double *x; switch (data.var) { case W: x = results[0]; break; case RX: x = results[1]; break; case RY: x = results[2]; break; // case MX: // x = results[3]; // break; // case MY: // x = results[4]; // break; // case MXY: // x = results[5]; // break; // case QX: // x = results[6]; // break; // case QY: // x = results[7]; // break; default: return; break; } double T0, T1, T2, T3, T4, Tn, R, G, B; getMaxMin(x, nNodes, T4, T0); T2 = (T0+T4)/2.; T1 = (T0+T2)/2.; T3 = (T2+T4)/2.; int k[4]; if(data.def) for(int i=0; i<nElements; i++){ glBegin(GL_QUADS); for(int ii = 0; ii<npy-1; ii++) for(int jj = 0; jj<npx-1; jj++) { k[0] = elements[i]->nodes[ii*npx+jj]->index; k[1] = elements[i]->nodes[ii*npx+jj+1]->index; k[2] = elements[i]->nodes[(ii+1)*npx+jj+1]->index; k[3] = elements[i]->nodes[(ii+1)*npx+jj]->index; for(int p = 0; p<4; p++){ Tn = x[k[p]]; R = Tn<T2? 0. : (Tn>T3? 1. : (Tn-T2)/(T3-T2)); B = Tn>T2? 0. : (Tn<T1? 1. : (T2-Tn)/(T2-T1)); G = Tn<T1? (Tn-T0)/(T1-T0) : Tn>T3 ? (T4-Tn)/(T4-T3) : 1.; glColor4d(R,G,B,0.8); glVertex3d(nodes[k[p]]->x, nodes[k[p]]->y, data.factor*results[0][k[p]]); } } glEnd(); } else for(int i=0; i<nElements; i++){ glBegin(GL_QUADS); for(int ii = 0; ii<npy-1; ii++) for(int jj = 0; jj<npx-1; jj++) { k[0] = elements[i]->nodes[ii*npx+jj]->index; k[1] = elements[i]->nodes[ii*npx+jj+1]->index; k[2] = elements[i]->nodes[(ii+1)*npx+jj+1]->index; k[3] = elements[i]->nodes[(ii+1)*npx+jj]->index; for(int p = 0; p<4; p++){ Tn = x[k[p]]; R = Tn<T2? 0. : (Tn>T3? 1. : (Tn-T2)/(T3-T2)); B = Tn>T2? 0. : (Tn<T1? 1. : (T2-Tn)/(T2-T1)); G = Tn<T1? (Tn-T0)/(T1-T0) : Tn>T3 ? (T4-Tn)/(T4-T3) : 1.; glColor4d(R,G,B,0.8); glVertex2d(nodes[k[p]]->x, nodes[k[p]]->y); } } glEnd(); } if(data.elements) for(int i=0; i<nElements; i++) elements[i]->draw(); if(data.load){ for(int i=0; i<nNodes; i++) nodes[i]->draw_lock(); for(int i=0; i<nNodes; i++) nodes[i]->draw_load(); } if(data.nodes){ for(int i=0; i<nNodes; i++) nodes[i]->draw(); } }
/* The main drawing function. */ void DrawGLScene(){ glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); double RotationAngleX = 0.0; double RotationAngleY = 0.0; double RotationAngleZ = 0.0; if( dim3d==1 ) RotationAngleX = -60.0; if( dim3d==2 ) RotationAngleX = -90.0; glTranslatef( 0.0 , 0.0 , -CAM_BACKUP ); glRotatef(RotationAngleX, 1, 0, 0); glRotatef(RotationAngleY, 0, 1, 0); glRotatef(RotationAngleZ, 0, 0, 1); int q = valq; getMaxMin(); if( print_vals==0 ){ printf("Max = %e Min = %e\n",maxval,minval); print_vals=1; } double camdist = 0.0;//-CAM_BACKUP; double xoff = offx; double yoff = offy; double zoff = 0.0; if( draw_1d ){ glColor3f(1.0,1.0,1.0); glBegin(GL_QUADS); glVertex3f(-1./rescale-xoff,-1./rescale-yoff, camdist + zoff ); glVertex3f( 1./rescale-xoff,-1./rescale-yoff, camdist + zoff ); glVertex3f( 1./rescale-xoff, 1./rescale-yoff, camdist + zoff ); glVertex3f(-1./rescale-xoff, 1./rescale-yoff, camdist + zoff ); glEnd(); glLineWidth( 3.0f ); glColor3f(0.0,0.0,0.0); glBegin( GL_LINE_STRIP ); int j; // for( j=0 ; j<Nr ; ++j ){ for( j=0 ; j<Nr ; ++j ){ //if(logscale) val = log(getval(theZones[i],q))/log(10.); double rm = r_jph[j-1]; double rp = r_jph[j]; double val = theRadialData[j][q]/max_1d;//*pow(.5*(rp+rm),1.5);//2.*(theRadialData[j][q]-minval)/(maxval-minval) - 1.;//getval(theZones[i],q); if( q==1 ) val *= 1e2; val = 2.*val - 1.; double c0 = 2.*((.5*(rp+rm)-r_jph[-1])/(r_jph[Nr-1]-r_jph[-1]) - .5)/rescale; double c1 = val/rescale; glVertex3f( c0-xoff, c1-yoff, camdist-zoff+.001 ); } glEnd(); /* glColor3f(0.5,0.5,0.5); glBegin( GL_LINE_STRIP ); for( j=0 ; j<Nr ; ++j ){ //if(logscale) val = log(getval(theZones[i],q))/log(10.); double rm = r_jph[j-1]; double rp = r_jph[j]; double r = .5*(rp+rm); double val = pow(r,-1.5)/max_1d;// *pow(.5*(rp+rm),1.5);//2.*(theRadialData[j][q]-minval)/(maxval-minval) - 1.;//getval(theZones[i],q); if( q==1 ) val *= 1e2; val = 2.*val - 1.; double c0 = 2.*((.5*(rp+rm)-r_jph[-1])/(r_jph[Nr-1]-r_jph[-1]) - .5)/rescale; double c1 = val/rescale; glVertex3f( c0-xoff, c1-yoff, camdist-zoff+.001 ); } glEnd(); */ if( draw_planet ){ double eps = 0.01; int p; for( p=0 ; p<Npl ; ++p ){ double r = thePlanets[p][0]; double x = 2.*(( r - r_jph[-1] )/( r_jph[Nr-1]-r_jph[-1] ) - .5)/rescale; glColor3f(0.0,0.0,0.0); glLineWidth(2.0); glBegin(GL_LINE_LOOP); glVertex3f( x-xoff+eps , 1./rescale-yoff+eps , camdist+.001 ); glVertex3f( x-xoff-eps , 1./rescale-yoff+eps , camdist+.001 ); glVertex3f( x-xoff-eps , 1./rescale-yoff-eps , camdist+.001 ); glVertex3f( x-xoff+eps , 1./rescale-yoff-eps , camdist+.001 ); glEnd(); } } }else{ int Num_Draws = 1+reflect+draw_border; int count; int i,j; for( count = 0 ; count < Num_Draws ; ++count ){ int draw_border_now = 0; int draw_reflected_now = 0; if( count==reflect+1 ) draw_border_now = 1; if( reflect && count==1 ) draw_reflected_now = 1; if( draw_border_now ) zoff -= .0001; for( j=0 ; j<Nr ; ++j ){ double rm = r_jph[j-1]/rescale; double rp = r_jph[j]/rescale; double rc = .5*(rp+rm); double dr = rp-rm; if( !draw_border ){ rm = rc-.55*dr; rp = rc+.55*dr; } for( i=0 ; i<Np[j] ; ++i ){ double phip = p_iph[j][i]; double phim; if( i==0 ) phim = p_iph[j][Np[j]-1]; else phim = p_iph[j][i-1]; if( t_off && !p_off ){ phip -= t; phim -= t; } if( p_off ){ phip -= thePlanets[1][1]; phim -= thePlanets[1][1]; } double dp = phip-phim; while( dp < 0.0 ) dp += 2.*M_PI; while( dp > 2.*M_PI ) dp -= 2.*M_PI; double phi = phim + 0.5*dp; if( !draw_border ){ phip = phi+.55*dp; phim = phi-.55*dp; } double val = (getval(theZones[j][i],q)-minval)/(maxval-minval); if(logscale) val = (log(getval(theZones[j][i],q))/log(10.)-minval)/(maxval-minval); if( val > 1.0 ) val = 1.0; if( val < 0.0 ) val = 0.0; //double u = getval( theZones[j][i] , 2 ); //if( uMax < u ) uMax = u; float rrr,ggg,bbb; get_rgb( val , &rrr , &ggg , &bbb , cmap ); //if( (!dim3d || (sin(phi)>0 || cos(phi+.25)<0.0)) && dim3d !=2 ){ if( dp < 1.5 && (!dim3d || (sin(phi)>0 && cos(phi)>0.0)) && dim3d !=2 ){ if( !draw_border_now ){ glColor3f( rrr , ggg , bbb ); glBegin(GL_POLYGON); }else{ glLineWidth(3.0f); glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); } double z0 = 0.0; if( dim3d ) z0 = z_kph[Nz-1]/rescale; double c0 = rm*cos(phim); double c1 = rm*sin(phim); glVertex3f( c0-xoff, c1-yoff, camdist-zoff+z0 ); c0 = rp*cos(phim); c1 = rp*sin(phim); glVertex3f( c0-xoff, c1-yoff, camdist-zoff+z0 ); c0 = rp*cos(phi)/cos(.5*dp); c1 = rp*sin(phi)/cos(.5*dp); glVertex3f( c0-xoff, c1-yoff, camdist-zoff+z0 ); c0 = rp*cos(phip); c1 = rp*sin(phip); glVertex3f( c0-xoff, c1-yoff, camdist-zoff+z0 ); c0 = rm*cos(phip); c1 = rm*sin(phip); glVertex3f( c0-xoff, c1-yoff, camdist-zoff+z0 ); glEnd(); } } if( dim3d ){ int k; for( k=0 ; k<Nz ; ++k ){ int jk = j*Nz+k; double rp = r_jph[j]/rescale; double rm = r_jph[j-1]/rescale; double zp = z_kph[k]/rescale; double zm = z_kph[k-1]/rescale; double phi = 0.0;//rzZones[jk][Nq]; double val = (getval(rzZones[jk],q)-minval)/(maxval-minval); if(logscale) val = (log(getval(rzZones[jk],q))/log(10.)-minval)/(maxval-minval); if( val > 1.0 ) val = 1.0; if( val < 0.0 ) val = 0.0; float rrr,ggg,bbb; get_rgb( val , &rrr , &ggg , &bbb , cmap ); if( !draw_border_now ){ glColor3f( rrr , ggg , bbb ); glBegin(GL_POLYGON); }else{ glLineWidth(2.0f); glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); } glVertex3f( rp*cos(phi) - xoff , rp*sin(phi) - yoff + zoff, zp ); glVertex3f( rm*cos(phi) - xoff , rm*sin(phi) - yoff + zoff, zp ); glVertex3f( rm*cos(phi) - xoff , rm*sin(phi) - yoff + zoff, zm ); glVertex3f( rp*cos(phi) - xoff , rp*sin(phi) - yoff + zoff, zm ); glEnd(); } if( draw_border_now || dim3d == 1 ){ glLineWidth(2.0f); glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); double rp = r_jph[Nr-1]/rescale; //double rm =-r_jph[Nr-1]/rescale; double rm = r_jph[-1]/rescale; double zp = z_kph[Nz-1]/rescale; double zm = z_kph[ -1]/rescale; glVertex3f( rp-xoff , -yoff+zoff , zp ); glVertex3f( rm-xoff , -yoff+zoff , zp ); glVertex3f( rm-xoff , -yoff+zoff , zm ); glVertex3f( rp-xoff , -yoff+zoff , zm ); glEnd(); } } } } if( draw_bar ){ double xb = 0.6; double hb = 1.0; double wb = 0.02; int Nb = 1000; double dy = hb/(double)Nb; int k; for( k=0 ; k<Nb ; ++k ){ double y = (double)k*dy - .5*hb; double val = (double)k/(double)Nb; float rrr,ggg,bbb; get_rgb( val , &rrr , &ggg , &bbb , cmap ); glLineWidth(0.0f); glColor3f( rrr , ggg , bbb ); glBegin(GL_POLYGON); glVertex3f( xb , y , camdist + .001 ); glVertex3f( xb+wb , y , camdist + .001 ); glVertex3f( xb+wb , y+dy , camdist + .001 ); glVertex3f( xb , y+dy , camdist + .001 ); glEnd(); } int Nv = 8; for( k=0 ; k<Nv ; ++k ){ double y = (double)k*hb/(double)(Nv-1) - .5*hb; double val = (double)k/(double)(Nv-1)*(maxval-minval) + minval; char valname[256]; sprintf(valname,"%+.2e",val); glLineWidth(1.0f); glColor3f(0.0,0.0,0.0); glBegin(GL_LINE_LOOP); glVertex3f( xb , y , camdist + .0011 ); glVertex3f( xb+wb , y , camdist + .0011 ); glEnd(); glutPrint( xb+1.5*wb , y-.007 , camdist + .001 ,glutFonts[6] , valname , 0.0f, 0.0f , 0.0f , 0.5f ); } } if( draw_planet ){ double eps = 0.01; int p; for( p=0 ; p<Npl ; ++p ){ double r = thePlanets[p][0]/rescale; double phi = thePlanets[p][1]; if( t_off && !p_off ) phi -= t; if( p_off ) phi -= thePlanets[1][1]; glColor3f(0.0,0.0,0.0); glLineWidth(2.0); glBegin(GL_LINE_LOOP); glVertex3f( r*cos(phi)-xoff+eps , r*sin(phi)-yoff+eps , camdist+.001 ); glVertex3f( r*cos(phi)-xoff-eps , r*sin(phi)-yoff+eps , camdist+.001 ); glVertex3f( r*cos(phi)-xoff-eps , r*sin(phi)-yoff-eps , camdist+.001 ); glVertex3f( r*cos(phi)-xoff+eps , r*sin(phi)-yoff-eps , camdist+.001 ); glEnd(); } } if( draw_spiral ){ double rp = 5.0;//thePlanets[1][0]; double Mach = 3.65; double p0 = 1.2; double dr = .07; int Nr = 200; double Rmin = 0.5; double Rmax = 1.1; int k; glLineWidth(3.0f); glColor3f(0.0,0.0,0.0); //glColor3f(1.0,1.0,1.0); glBegin(GL_LINE_LOOP); for( k=0 ; k<Nr ; ++k ){ //double phi0 = ((double)k+.5)/(double)Nr*2.*M_PI;//(3.-2.*sqrt(1./r)-r)*20.; double x = ((double)k+.5)/(double)Nr; //double r = .001*pow(.5/.001,x); double r = .5*pow(1.5/.5,x); //double phi0 = p0-log(r/0.001)*Mach;//(3.-2.*sqrt(1./r)-r)*5.; double phi0 = (3.-2.*sqrt(1./r)-r)*20.; if( r<1. ) phi0 = -phi0; // double x0 = rp + dr*cos(phi0); // double y0 = dr*sin(phi0); double phi = phi0; // double r = rp; // double phi = atan2(y0,x0); // double phi = ((double)k+.5)/(double)Nr*2.*M_PI*9.32 + 4.*M_PI; // double r = sqrt(x0*x0+y0*y0); // double r = pow(phi/10.,-2.); // double r = 2./(1.+sin(phi));//1.0;//((double)k+0.5)/(double)Nr*(Rmax-Rmin) + Rmin; // if( r<1. ) phi = -phi; r /= rescale; glVertex3f( r*cos(phi)-xoff , r*sin(phi)-yoff , camdist + .0011 ); if( k%2==1 ){ glEnd(); glBegin(GL_LINE_LOOP); } } glEnd(); /* e += 0.01; glBegin(GL_LINE_LOOP); for( k=0 ; k<Nr ; ++k ){ double phi0 = ((double)k-.5)/(double)Nr*2.*M_PI;//(3.-2.*sqrt(1./r)-r)*20.; double x0 = 1.+e*cos(phi0); double y0 = e*sin(phi0); double phi = atan2(y0,x0); double r = sqrt(x0*x0+y0*y0); // double phi = (double)k/(double)Nr*2.*M_PI;//(3.-2.*sqrt(1./r)-r)*20.; // double r = 2./(1.+sin(phi));//1.0;//((double)k+0.5)/(double)Nr*(Rmax-Rmin) + Rmin; // if( r<1. ) phi = -phi; r /= rescale; glVertex3f( r*cos(phi)-xoff , r*sin(phi)-yoff , camdist + .0011 ); if( k%2==1 ){ glEnd(); glBegin(GL_LINE_LOOP); } } glEnd(); */ } } if( draw_t ){ char tprint[256]; sprintf(tprint,"t = %.2e",t/2./M_PI); glutPrint( -.6 , .5 , camdist + .001 , glutFonts[6] , tprint , 0.0f, 0.0f , 0.0f , 0.5f ); // sprintf(tprint,"uMax = %.1f",uMax); // glutPrint( -.8 , .4 , camdist + .001 , glutFonts[6] , tprint , 0.0f, 0.0f , 0.0f , 0.5f ); } if( help_screen ){ int NLines = 9; double dy = -.04; char help[NLines][256]; sprintf(help[0],"Help Display:"); sprintf(help[1],"b - Toggle Colorbar"); sprintf(help[2],"c - Change Colormap"); sprintf(help[3],"f - Toggle Max/Min Floors"); sprintf(help[4],"p - Toggle Planet Data"); sprintf(help[5],"1-9 - Choose Primitive Variable to Display"); sprintf(help[6],"wasd - Move Camera"); sprintf(help[7],"z/x - Zoom in/out"); sprintf(help[8],"h - Toggle Help Screen"); int i; for( i=0 ; i<NLines ; ++i ){ glutPrint( -.8 , i*dy , camdist + .001 , glutFonts[6] , help[i] , 0.0f, 0.0f , 0.0f , 0.5f ); } } if( CommandMode ){ TakeScreenshot("out.ppm"); exit(1); } glutSwapBuffers(); }
void ImageProcessing::getDisparity() { int windowSize = 9; int DSR = 20; //char *leftImgPath, *rightImgPath; //cout<<"Enter full path of Left image "; //cin>>leftImgPath; //cout<<"Enter full path of Left image "; //cin>>rightImgPath; IplImage *LeftinputImage = cvLoadImage("../outputs/raw/left-0.pgm", 0); IplImage *RightinputImage = cvLoadImage("../outputs/raw/right-0.pgm", 0); // int width = LeftinputImage->width; // int height = LeftinputImage->height; /****************8U to 32F**********************/ IplImage *LeftinputImage32 = cvCreateImage(cvSize(LeftinputImage->width, LeftinputImage->height), 32, 1); //IPL_DEPTH_32F IplImage *RightinputImage32 = cvCreateImage(cvSize(LeftinputImage->width, LeftinputImage->height), 32, 1); cvConvertScale(LeftinputImage, LeftinputImage32, 1 / 255.); cvConvertScale(RightinputImage, RightinputImage32, 1 / 255.); int offset = floor((double) windowSize / 2); int height = LeftinputImage32->height; int width = LeftinputImage32->width; double *localNCC = new double[DSR]; int x = 0, y = 0, d = 0, m = 0; int N = windowSize; IplImage *leftWinImg = cvCreateImage(cvSize(N, N), 32, 1); //mySubImage(LeftinputImage32,cvRect(0,0,N,N)); IplImage *rightWinImg = cvCreateImage(cvSize(N, N), 32, 1); //mySubImage(RightinputImage32,cvRect(0,0,N,N)); IplImage *disparity = cvCreateImage(cvSize(width, height), 8, 1); //or IPL_DEPTH_8U BwImage imgA(disparity); for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { imgA[y][x] = 0; } } CvScalar s1; CvScalar s2; for (y = 0; y < height - N; y++) { //height-N for (x = 0; x < width - N; x++) { //width-N //getWindow(i,j,leftim,wl,N); cvSetImageROI(LeftinputImage32, cvRect(x, y, N, N)); s1 = cvAvg(LeftinputImage32, NULL); cvSubS(LeftinputImage32, s1, leftWinImg, NULL); //zero-means cvNormalize(leftWinImg, leftWinImg, 1, 0, CV_L2, NULL); d = 0; //initialise localNCC for (m = 0; m < DSR; m++) { localNCC[m] = 0; } do { if (x - d >= 0) { cvSetImageROI(RightinputImage32, cvRect(x - d, y, N, N)); s2 = cvAvg(RightinputImage32, NULL); cvSubS(RightinputImage32, s2, rightWinImg, NULL); //zero-means cvNormalize(rightWinImg, rightWinImg, 1, 0, CV_L2, NULL); } else { break; } localNCC[d] = cvDotProduct(leftWinImg, rightWinImg); cvResetImageROI(RightinputImage32); d++; } while (d <= DSR); //to find the best d and store imgA[y + offset][x + offset] = getMaxMin(localNCC, DSR, 1) *16; cvResetImageROI(LeftinputImage32); } //x if (y % 10 == 0) cout << "row=" << y << " of " << height << endl; } //y cvReleaseImage(&leftWinImg); cvReleaseImage(&rightWinImg); cvSaveImage("disparity.pgm", disparity); waitHere(); //cv::imwrite("disparity.pgm",&disparity); cout << "Displaying Disparity image" << endl; // cvShowImage( "Disparity", disparity); //cv::waitKey(0); //return disparity; }