Allink  v0.1
ElPolyDrawControl.cpp
00001 /***********************************************************************
00002 ElPolyDrawControl: OpenGL menus, keyboards and mouse functions
00003 Copyright (C) 2008-2010 by Giovanni Marelli <sabeiro@virgilio.it>
00004 
00005 
00006 This program is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2 of the License, or
00009 (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 You should have received a copy of the GNU General Public License
00016 along with this program; If not, see <http://www.gnu.org/licenses/>.
00017 ***********************************************************************/
00018 #include "ElPoly.h"
00019 
00020 #ifdef __glut_h__
00021 extern Draw *Dr;
00023 enum ElType{
00025   EL_EVERY  = 0x0021 ,
00027   EL_POLY    ,
00029   EL_HOMO    ,
00031   EL_ADDED   ,
00033   EL_UP      ,
00035   EL_DOWN    ,
00037   EL_INNER   ,
00039   EL_OUTER   ,
00041   EL_STRETCH ,
00043   EL_FLABBY  ,
00045   EL_TILTED  ,
00047   EL_PHOB    ,
00049   EL_PHIL    ,
00051   EL_OIL     ,
00053   EL_ALL     
00054 };
00055 enum ElExtra{
00056   EL_FULL =   0x0031,
00057   EL_BOND,
00058   EL_PICTURE,
00059   EL_RESTORE,
00060   EL_SCRIPT,
00061   EL_RENDER,
00062 };
00063 enum ElEffect{
00064   EL_NANOPOS = 0x0041,
00065   EL_DENSPOS,
00066   EL_CMPOS,
00067   EL_CICCIA
00068 };
00069 enum ElBackFold{
00070   EL_BF_NANO = 0x0051,
00071   EL_BF_CM,
00072   EL_BF_PARTICLE
00073 };
00074 enum ElBlend{
00075   EL_BLEND1 = 0x0061,
00076   EL_BLEND2,
00077   EL_BLEND3,
00078   EL_BLEND4,
00079   EL_BLEND5,
00080   EL_BLEND6,
00081   EL_BLEND7,
00082   EL_BLEND8,
00083   EL_BLEND9,
00084   EL_BLEND10
00085 };
00086 enum ElBlock{
00087   EL_BLOCK = 0x0071,
00088 };
00089 enum ElLineSize{
00090   EL_LSIZE1 = 0x0081,
00091   EL_LSIZE2  ,
00092   EL_LSIZE3  ,
00093   EL_LSIZE4  ,
00094   EL_LSIZE5  ,
00095   EL_LSIZE6  ,
00096   EL_LSIZE7  ,
00097   EL_LSIZE20 ,
00098 };
00099 void ElPoly::CreateMenu(){
00100   int SubMenu1 = 0, SubMenu2 = 0, SubMenu3 = 0;
00101   int SubMenu4 = 0, SubMenu5 = 0, SubMenu6 = 0;
00102   int SubMenu7 = 0, SubMenu8 = 0, SubMenu9 = 0;
00103   SubMenu1 = glutCreateMenu(MenuVisual);
00104   glutAddMenuEntry("Particles",EL_PART);
00105   glutAddMenuEntry("Chains",EL_CHAIN);
00106   glutAddMenuEntry("Vectors",EL_VECTORS);
00107   glutAddMenuEntry("Triangulate",EL_TRIA);
00108   glutAddMenuEntry("IsoLevel",EL_ISOLEVEL);
00109   glutAddMenuEntry("Color",EL_COLOR);
00110   glutAddMenuEntry("Quad",EL_QUAD);
00111   glutAddMenuEntry("Quad1",EL_QUAD1);
00112   glutAddMenuEntry("Potential",EL_POTENTIAL);
00113   glutAddMenuEntry("Density",EL_DENS);
00114   glutAddMenuEntry("Polygon",EL_POLYGON);
00115   glutAddMenuEntry("SquareMesh",EL_SQUAREMESH);
00116   glutAddMenuEntry("Sample",EL_SAMPLE);
00117   glutAddMenuEntry("Spectrum",EL_SPECTRUM);
00118   glutAddMenuEntry("Surface",EL_SURF);
00119   glutAddMenuEntry("Voronoi",EL_VORONOI);
00120   glutAddMenuEntry("Skin",EL_SKIN);
00121   glutAddMenuEntry("Mesh",EL_MESH);
00122   glutAddMenuEntry("Isoipse",EL_ISOIPSE);
00123   SubMenu2 = glutCreateMenu(MenuChoise);
00124   glutAddMenuEntry("Every",EL_EVERY);
00125   glutAddMenuEntry("Poly",EL_POLY);
00126   glutAddMenuEntry("Homo",EL_HOMO);
00127   glutAddMenuEntry("Added",EL_ADDED);
00128   glutAddMenuEntry("Up",EL_UP);
00129   glutAddMenuEntry("Down",EL_DOWN);
00130   glutAddMenuEntry("Inner",EL_INNER);
00131   glutAddMenuEntry("Outer",EL_OUTER);
00132   glutAddMenuEntry("Stretch",EL_STRETCH);
00133   glutAddMenuEntry("Flabby",EL_FLABBY);
00134   glutAddMenuEntry("Tilted",EL_TILTED);
00135   SubMenu3 = glutCreateMenu(MenuChoise);
00136   glutAddMenuEntry("All",EL_ALL);
00137   glutAddMenuEntry("Phob",EL_PHOB);
00138   glutAddMenuEntry("Phil",EL_PHIL);
00139   glutAddMenuEntry("Oil",EL_OIL);
00140   SubMenu4 = glutCreateMenu(MenuChoise);
00141   glutAddMenuEntry("NanoPos",EL_NANOPOS);
00142   glutAddMenuEntry("DensPos",EL_DENSPOS);
00143   glutAddMenuEntry("CmPos",EL_CMPOS);
00144   SubMenu5 = glutCreateMenu(MenuChoise);
00145   glutAddMenuEntry("Part",EL_BF_PARTICLE);
00146   glutAddMenuEntry("Nano",EL_BF_NANO);
00147   glutAddMenuEntry("CM",EL_BF_CM);
00148   SubMenu6 = glutCreateMenu(MenuChoise);
00149   glutAddMenuEntry("1",EL_BLEND1);
00150   glutAddMenuEntry("2",EL_BLEND2);
00151   glutAddMenuEntry("3",EL_BLEND3);
00152   glutAddMenuEntry("4",EL_BLEND4);
00153   glutAddMenuEntry("5",EL_BLEND5);
00154   glutAddMenuEntry("6",EL_BLEND6);
00155   glutAddMenuEntry("7",EL_BLEND7);
00156   glutAddMenuEntry("8",EL_BLEND8);
00157   glutAddMenuEntry("9",EL_BLEND9);
00158   glutAddMenuEntry("10",EL_BLEND10);
00159   SubMenu7 = glutCreateMenu(MenuChoise);
00160   glutAddMenuEntry("Every",EL_BLOCK+pNBlock());
00161   for(int b=0;b<pNBlock();b++){
00162     glutAddMenuEntry(Block[b].Name,b+EL_BLOCK);
00163   }
00164   SubMenu8 = glutCreateMenu(MenuChoise);
00165   glutAddMenuEntry("1",EL_LSIZE1);
00166   glutAddMenuEntry("2",EL_LSIZE2);
00167   glutAddMenuEntry("3",EL_LSIZE3);
00168   glutAddMenuEntry("4",EL_LSIZE4);
00169   glutAddMenuEntry("5",EL_LSIZE5);
00170   glutAddMenuEntry("6",EL_LSIZE6);
00171   glutAddMenuEntry("7",EL_LSIZE7);
00172   glutAddMenuEntry("20",EL_LSIZE20);
00173   glutCreateMenu(MenuChoise);
00174   glutAddSubMenu("Visualisation",SubMenu1);
00175   glutAddSubMenu("Chain",SubMenu2);
00176   glutAddSubMenu("Bead",SubMenu3);
00177   glutAddSubMenu("Move",SubMenu4);
00178   glutAddSubMenu("BackFold",SubMenu5);
00179   glutAddSubMenu("Blend",SubMenu6);
00180   glutAddSubMenu("Block",SubMenu7);
00181   glutAddSubMenu("Line size",SubMenu8);
00182   glutAddMenuEntry("Fullscreen",EL_FULL);
00183   glutAddMenuEntry("Bond",EL_BOND);
00184   glutAddMenuEntry("Picture",EL_PICTURE);
00185   glutAddMenuEntry("Restore",EL_RESTORE);
00186   glutAddMenuEntry("Script",EL_SCRIPT);
00187   glutAddMenuEntry("Render",EL_RENDER);
00188   glutAttachMenu(GLUT_RIGHT_BUTTON);
00189 }
00190 extern void ElPoly::ElMenuVisual(int option){
00191   InvScaleUn = 1./pEdge(0);
00192   for(int d=0;d<3;d++) Dr->Edge[d] = pEdge(d);
00193   Dr->InvScaleUn = InvScaleUn;
00194   Point = Dr->DefPoint();
00195   What2Draw = option;
00196   //option = EL_PART;
00197   switch(option){
00198   case EL_PART:
00199     DrPartList();
00200     break;
00201   case EL_COLOR:
00202     //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_DST_COLOR);  
00203     DrColor();
00204     break;
00205   case EL_CHAIN:
00206     BfDefChain();
00207     Hexagon = Dr->DefHexagon();
00208     DrChains();
00209     break;
00210   case EL_VECTORS:
00211     Arrow = Dr->DefArrowThin();
00212     DrVectors();
00213     break;
00214   case EL_SURF:
00215     DrSurface();
00216     break;
00217   case EL_POLYGON:
00218     DrPolygon();
00219     break;
00220   case EL_SQUAREMESH:
00221     DrSquareMesh();
00222     break;
00223   case EL_SAMPLE:
00224     DrSample(NEdge);
00225     //ChooseDraw("sample");
00226     break;
00227   case EL_SKIN:
00228     //DrStalk();
00229     //DefineSkin(5);
00230     //Tile();
00231     break;
00232   case EL_TRIA:
00233     DrTriangulate();
00234     break;
00235   case EL_MESH:
00236     //DefineSurf();
00237     //DrGenMesh();
00238     break;
00239   case EL_ISOLEVEL:
00240     DrIsolevel(10,ExtParam);
00241     break;
00242   case EL_ISOIPSE:
00243     DrIsoipse(100,10,1);
00244     break;
00245   case EL_QUAD:
00246     Quad = Dr->DefQuad(NEdge-2);
00247     DrQuad();
00248     break;
00249   case EL_QUAD1:
00250     Quad = Dr->DefQuad(NEdge-2);
00251     DrQuad1();
00252     break;
00253   case EL_POTENTIAL:
00254     DrPotential();
00255     break;
00256   case EL_DENS:
00257     Dr->xp = .5*pEdge(0)*InvScaleUn;
00258     Dr->yp = 0.;
00259     Dr->zp = .5*pEdge(2)*InvScaleUn;
00260     DrDensity();
00261     break;
00262   case EL_CROSS:
00263     DrCrossLinks();
00264     break;
00265   }
00266 }
00267 extern void ElPoly::ElMenuChoise(int option){
00268   int OldIf = 0;
00269   InvScaleUn = 1./pEdge(0);
00270   for(int d=0;d<3;d++) Dr->Edge[d] = pEdge(d);
00271   Dr->InvScaleUn = InvScaleUn;
00272   Point = Dr->DefPoint();
00273   //option = EL_PART;
00274   switch(option){
00275   case EL_EVERY:
00276     NChType = CHAIN_EVERY;
00277     //sprintf(Dr->info,"Chain type %d",NChType);
00278     RenderPart();
00279     break;
00280   case EL_POLY:
00281     NChType = CHAIN_POLY;
00282     RenderPart();
00283     break;
00284   case EL_HOMO:
00285     NChType = CHAIN_HOMO;
00286     RenderPart();
00287     break;
00288   case EL_ADDED:
00289     NChType = CHAIN_ADDED;
00290     RenderPart();
00291     break;
00292   case EL_UP:
00293     NChType = CHAIN_UP;
00294     RenderPart();
00295     break;
00296   case EL_DOWN:
00297     NChType = CHAIN_DOWN;
00298     RenderPart();
00299     break;
00300   case EL_INNER:
00301     NChType = CHAIN_INNER;
00302     RenderPart();
00303     break;
00304   case EL_OUTER:
00305     NChType = CHAIN_OUTER;
00306     RenderPart();
00307     break;
00308   case EL_STRETCH:
00309     NChType = CHAIN_STRETCH;
00310     RenderPart();
00311     break;
00312   case EL_FLABBY:
00313     NChType = CHAIN_FLABBY;
00314     RenderPart();
00315     break;
00316   case EL_TILTED:
00317     NChType = CHAIN_TILTED;
00318     RenderPart();
00319     break;
00320   case EL_PHOB:
00321     NPType = BEAD_PHOB;
00322     RenderPart();
00323     break;
00324   case EL_PHIL:
00325     NPType = BEAD_PHIL;
00326     RenderPart();
00327     break;
00328   case EL_OIL:
00329     NPType = BEAD_OIL;
00330     RenderPart();
00331     break;
00332   case EL_ALL:
00333     NPType = BEAD_EVERY;
00334     RenderPart();
00335     break;
00336   case EL_FULL:
00337     glutFullScreen();
00338     glutPostRedisplay();
00339     break;
00340   case EL_BOND:
00341     if(!pNLink()) IfLine += 1;
00342     if(IfLine == 2) IfLine =0;
00343     RenderPart();
00344     break;
00345   case EL_PICTURE:
00346     Dr->Step++;
00347     Dr->IfInfo = 0;
00348     Dr->Picture();
00349     Dr->IfInfo = 1;
00350     break;
00351   case EL_RESTORE:
00352     glutIdleFunc(NULL);
00353     RenderPart();
00354     Dr->InitConstant();
00355     glutPostRedisplay();
00356     break;
00357   case EL_SCRIPT:
00358     Dr->IfScript = !Dr->IfScript;
00359     Dr->ReadScript();
00360     break;
00361   case EL_RENDER:
00362     DrawOutput = EL_POVRAY;
00363     RenderPart();
00364     DrawOutput = EL_OPENGL;
00365     //Conv2Povray();
00366     break;
00367   case EL_NANOPOS:
00368     Dr->xp = pNanoPos(0,0)*InvScaleUn;
00369     Dr->yp = pNanoPos(0,1)*InvScaleUn;
00370     Dr->zp = pNanoPos(0,2)*InvScaleUn;
00371     break;
00372   case EL_DENSPOS:
00373     Dr->xp = .5*pEdge(0)*InvScaleUn;
00374     Dr->yp = 0.;
00375     Dr->zp = .5*pEdge(2)*InvScaleUn;
00376     break;
00377   case EL_CMPOS:
00378     ShiftSys(SHIFT_CM);
00379     break;
00380   case EL_SAMPLE:
00381     DrSample(NEdge);
00382     break;
00383   case EL_SPECTRUM:
00384     DrSpectrum();
00385     break;
00386   case EL_VORONOI:
00387     DrVoronoi();
00388     break;
00389   case EL_BF_NANO:
00390     BackFold(BF_NANO);
00391     RenderPart();
00392     break;
00393   case EL_BF_PARTICLE:
00394     BackFold(BF_PART);
00395     RenderPart();
00396     break;
00397   case EL_BF_CM:
00398     BackFold(BF_CHAIN);
00399     RenderPart();
00400     break;
00401   case EL_BLEND1:
00402     Dr->ChooseBlend(1);
00403     break;
00404   case EL_BLEND2:
00405     Dr->ChooseBlend(2);
00406     break;
00407   case EL_BLEND3:
00408     Dr->ChooseBlend(3);
00409     break;
00410   case EL_BLEND4:
00411     Dr->ChooseBlend(4);
00412     break;
00413   case EL_BLEND5:
00414     Dr->ChooseBlend(5);
00415     break;
00416   case EL_BLEND6:
00417     Dr->ChooseBlend(6);
00418     break;
00419   case EL_BLEND7:
00420     Dr->ChooseBlend(7);
00421     break;
00422   case EL_BLEND8:
00423     Dr->ChooseBlend(8);
00424     break;
00425   case EL_BLEND9:
00426     Dr->ChooseBlend(9);
00427     break;
00428   case EL_BLEND10:
00429     Dr->ChooseBlend(10);
00430     break;
00431   case EL_LSIZE1:
00432     glLineWidth(1);
00433     glPointSize(1);
00434     break;
00435   case EL_LSIZE2:
00436     glLineWidth(2);
00437     glPointSize(2);
00438     break;
00439   case EL_LSIZE3:
00440     glLineWidth(3);
00441     glPointSize(3);
00442     break;
00443   case EL_LSIZE4:
00444     glLineWidth(4);
00445     glPointSize(4);
00446     break;
00447   case EL_LSIZE5:
00448     glLineWidth(5);
00449     glPointSize(5);
00450     break;
00451   case EL_LSIZE6:
00452     glLineWidth(6);
00453     glPointSize(6);
00454     break;
00455   case EL_LSIZE7:
00456     glLineWidth(7);
00457     glPointSize(7);
00458     break;
00459   case EL_LSIZE20:
00460     glLineWidth(20);
00461     glPointSize(20);
00462     break;
00463     //DrPosCol();
00464     //DrQuad1();
00465     //DrInterpSurface();
00466   }
00467   for(int b=0;b<pNBlock();b++){
00468     if(option == EL_BLOCK + b){
00469       sprintf(Block2Draw,"%s",Block[b].Name);
00470       RenderPart();
00471     }
00472   }
00473   if(option == EL_BLOCK + pNBlock()){
00474     sprintf(Block2Draw,"ALL");
00475     RenderPart();
00476   }
00477   //glEndList();
00478 }
00479 void ElPoly::RenderPart(){
00480   ElMenuVisual(What2Draw);
00481 }
00482 extern void ElPoly::ElDrawMouse(int button, int state,int x,int y){
00483   Dr->Dmouse(button,state,x,y);
00484   switch (button){
00485   case GLUT_WHEEL_UP:
00486     if( glutGetModifiers() == GLUT_ACTIVE_CTRL){
00487       ScaleFact += .2;
00488       Dr->zp -= .12*pEdge(CNorm)*InvScaleUn;
00489       ElMenuVisual(What2Draw);
00490       glutPostRedisplay();
00491     }
00492     break;
00493   case GLUT_WHEEL_DOWN:
00494     if( glutGetModifiers() == GLUT_ACTIVE_CTRL){
00495       ScaleFact -= .2;
00496       Dr->zp += .12*pEdge(CNorm)*InvScaleUn;
00497       ElMenuVisual(What2Draw);
00498       glutPostRedisplay();
00499     }
00500     break; 
00501   default:
00502     break;
00503   }
00504 }
00505 extern void ElPoly::keyboard(unsigned char key,int x, int y){
00506   Dr->keyboardDraw(key);
00507   char string[60];
00508   switch (key){
00509   case '+':
00510     quando+=10;
00511     if(quando >= NFileTot){
00512       quando = 0;
00513     }
00514     if(quando >= 0){
00515       OpenFile(quando);
00516       sprintf(Dr->info,"%s %d",cFile[quando],NFile[0]);
00517     }
00518     RenderPart();
00519     glutPostRedisplay();
00520     break;
00521   case '-':
00522     quando-=10;
00523     if(quando < 0){
00524       quando = NFileTot-1;
00525     }
00526     if(quando <=NFileTot){
00527       OpenFile(quando);
00528       sprintf(Dr->info,"%s %d",cFile[quando],NFile[0]);
00529     }
00530     RenderPart();
00531     glutPostRedisplay();
00532     break;
00533   case '>':
00534     quando++;
00535     if(quando >= NFileTot){
00536       quando = 0;
00537     }
00538     if(quando >= 0){
00539       OpenFile(quando);
00540       sprintf(Dr->info,"%s %d",cFile[quando],NFile[0]);
00541     }
00542     RenderPart();
00543     glutPostRedisplay();
00544     break;
00545   case '<':
00546     quando--;
00547     if(quando < 0){
00548       quando = NFileTot-1;
00549     }
00550     if(quando <=NFileTot){
00551       OpenFile(quando);
00552       sprintf(Dr->info,"%s %d",cFile[quando],NFile[0]);
00553     }
00554     RenderPart();
00555     glutPostRedisplay();
00556     break;
00557   case 'b':
00558     if(!pNLink()) IfLine = !IfLine;
00559     sprintf(Dr->info,"Bonding visualisation");
00560     RenderPart();
00561     glutPostRedisplay();
00562     break;
00563   case 'c':
00564     NBackFold++;
00565     if(NBackFold > 3)
00566       NBackFold = 0;
00567     if(NBackFold == BF_PART)
00568       sprintf(Dr->info,"Particle back fold");
00569     else if(NBackFold == BF_CHAIN)
00570       sprintf(Dr->info,"Center of mass back fold");
00571     else if(NBackFold == BF_NANO)
00572       sprintf(Dr->info,"Nano particle back fold");
00573     else if(NBackFold == BF_NO)
00574       sprintf(Dr->info,"No back fold");
00575     BackFold(NBackFold);
00576     RenderPart();
00577     glutPostRedisplay();
00578     break;
00579   case 'C':
00580     Conv2Povray();
00581     break;
00582   case 'i':
00583     IfIntorno += 1;
00584     if(IfIntorno == 3) IfIntorno = 0;
00585     if(IfIntorno == 1)
00586       sprintf(Dr->info,"Center of mass proximity");
00587     if(IfIntorno == 2)
00588       sprintf(Dr->info,"Particle proximity");
00589     RenderPart();
00590     glutPostRedisplay();
00591     break;
00592   case 'I':
00593     char cSystem[STRSIZE];
00594     SysInfo(cSystem);
00595     printf("%s %d %s\n",cSystem,NFile[0],cFile[NFile[0]]);
00596     sprintf(Dr->info,"%s %d %s\n",cSystem,NFile[0],cFile[NFile[0]]);
00597     glutPostRedisplay();
00598     break;
00599   case 'k':
00600     Dr->IfInfo = 0;
00601     glutPostRedisplay();
00602     Dr->Picture();
00603     Dr->IfInfo = 1;
00604     break;
00605   case 'K':
00606     sprintf(string,"rm %s",cFile[quando]);
00607     system(string);
00608     quando++;
00609     if(quando >= NFileTot){
00610       quando = 0;
00611     }
00612     if(quando >= 0){
00613       OpenFile(quando);
00614       sprintf(Dr->info,"%s %d",cFile[quando],NFile[0]);
00615     }
00616     RenderPart();
00617     glutPostRedisplay();
00618     break;
00619   case 'l':
00620     LineSize += 1;
00621     glLineWidth(LineSize);
00622     glPointSize(LineSize);
00623     RenderPart();
00624     glutPostRedisplay();
00625     break;
00626   case 'L':
00627     LineSize -= 1.;
00628     glLineWidth(LineSize);
00629     glPointSize(LineSize);
00630     RenderPart();
00631     glutPostRedisplay();
00632     break;
00633   case 'm':
00634     glutPostRedisplay();
00635     break;
00636   case 'n':
00637     Saturation *= 1.1;
00638     RenderPart();
00639     break;
00640   case 'N':
00641     Saturation /= 1.1;
00642     RenderPart();
00643     break;
00644   case 'p':
00645     ExtParam *= 1.5;
00646     RenderPart();
00647     break;
00648   case 'P':
00649     ExtParam /= 1.5;
00650     RenderPart();
00651     break;
00652    case 'r':
00653     glutIdleFunc(NULL);
00654     RenderPart();
00655     Dr->InitConstant();
00656     sprintf(Dr->info,"initial configuration");
00657     glutPostRedisplay();
00658     break;
00659   case 'R':
00660     RenderPart();
00661     glutPostRedisplay();
00662     break;
00663   case 'S':
00664     glutIdleFunc(Slide);
00665     break;
00666   case 't':
00667     Transform(2);
00668     RenderPart();
00669     break;
00670   case 27:
00671     exit(0);
00672     break;
00673   case 40:
00674     //glutIdleFunc(Expand);
00675     break;
00676   default:
00677     break;
00678   }
00679 }
00680 #endif
00681