ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/REPOS_ERICCA/magic/app/VMM/win32/visu.cpp
Revision: 5
Committed: Tue Jun 12 20:26:34 2007 UTC (18 years, 2 months ago)
Original Path: magic/app/VMM/VMM/win32/visu.cpp
File size: 18902 byte(s)
Log Message:

File Contents

# User Rev Content
1 5 //------------------------------------------------------------
2     //------------------------------------------------------------
3     // MAGiC
4     // Jean Christophe Cuillière et Vincent FRANCOIS
5     // Département de Génie Mécanique - UQTR
6     //------------------------------------------------------------
7     // Le projet MAGIC est un projet de recherche du département
8     // de génie mécanique de l'Université du Québec à
9     // Trois Rivières
10     // Les librairies ne peuvent être utilisées sans l'accord
11     // des auteurs (contact : francois@uqtr.ca)
12     //------------------------------------------------------------
13     //------------------------------------------------------------
14     //
15     // visu.cpp
16     //
17     //------------------------------------------------------------
18     //------------------------------------------------------------
19     // COPYRIGHT 2000
20     // Version du 02/03/2006 à 11H26
21     //------------------------------------------------------------
22     //------------------------------------------------------------
23    
24    
25     #include "gestionversion.h"
26    
27     #include <vcl.h>
28     #include <stdio.h>
29     #include <string.h>
30     #include <math.h>
31     #include <algorithm>
32     #pragma hdrstop
33    
34     using namespace std;
35     #include "visu.h"
36     //---------------------------------------------------------------------------
37     #pragma package(smart_init)
38     #pragma resource "*.dfm"
39     TVisufront *Visufront;
40     //---------------------------------------------------------------------------
41    
42    
43     int TVisufront::nombre=0;
44    
45     __fastcall TVisufront::TVisufront(TComponent* Owner)
46     : TForm(Owner),lsttriangle(NULL),lstnoeud(NULL),dx(0.0),dy(0.0),zoom(0),lstsegment(NULL),arretdemande(0),lire(0)
47     {
48     }
49     //---------------------------------------------------------------------------
50     void __fastcall TVisufront::FormClose(TObject *Sender, TCloseAction &Action)
51     {
52     if (lstnoeud!=NULL) delete [] lstnoeud;
53     if (lsttriangle!=NULL) delete [] lsttriangle;
54     if (lstsegment!=NULL) delete [] lstsegment;
55     numsegment=0;
56     numnoeud=0;
57     numtriangle=0;
58     Action=caFree;
59     TVisufront::nombre=TVisufront::nombre-1;
60     }
61     //---------------------------------------------------------------------------
62     void __fastcall TVisufront::FormCreate(TObject *Sender)
63     {
64     hdc = GetDC(Handle);
65     SetPixelFormatDescriptor();
66     hrc = wglCreateContext(hdc);
67     if (hrc == NULL)
68     ShowMessage(":-)~ hrc == NULL");
69     if(wglMakeCurrent(hdc, hrc) == false)
70     ShowMessage("Could not MakeCurrent");
71    
72     }
73     //---------------------------------------------------------------------------
74     void __fastcall TVisufront::SetPixelFormatDescriptor(void)
75     {
76     PIXELFORMATDESCRIPTOR pfd = {
77     sizeof(PIXELFORMATDESCRIPTOR),
78     1,
79     PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
80     PFD_TYPE_RGBA,
81     24,
82     0,0,0,0,0,0,
83     0,0,
84     0,0,0,0,0,
85     32,
86     0,
87     0,
88     PFD_MAIN_PLANE,
89     0,
90     0,0,
91     };
92     int PixelFormat = ChoosePixelFormat(hdc, &pfd);
93     SetPixelFormat(hdc, PixelFormat, &pfd);
94     }
95     //---------------------------------------------------------------------------
96    
97     void __fastcall TVisufront::recadre(double dx,double dy,int etat)
98     {
99     wglMakeCurrent(hdc,hrc);
100     GLfloat w = ClientWidth;
101     GLfloat h = ClientHeight;
102     if (etat==0) glViewport(0, 0, w, h);
103     glMatrixMode(GL_PROJECTION);
104     glLoadIdentity();
105     double d=max(xmax-xmin,ymax-ymin);
106     d=max(d,zmax-zmin);
107     d=d*1.75/2.;
108     double aspect=w/h;
109     double xxmin,yymin,xxmax,yymax;
110     double xcentre=0.5*(xmin+xmax);
111     double ycentre=0.5*(ymin+ymax);
112     double zcentre=0.5*(zmin+zmax);
113     if (w<h) xxmin=xcentre-d+dx; else xxmin=xcentre-d*aspect+dx;
114     if (w<h) yymin=ycentre-d/aspect+dy; else yymin=ycentre-d+dy;
115     double zzmin=zcentre-100*d;
116     if (w<h) xxmax=xxmin+2.*d; else xxmax=xxmin+2.*d*aspect;
117     if (w<h) yymax=yymin+2.*d/aspect; else yymax=yymin+2.*d;
118     double zzmax=zzmin+200.*d;
119     glOrtho(xxmin,xxmax,yymin,yymax,zzmin,zzmax);
120     }
121     //---------------------------------------------------------------------------
122     void __fastcall TVisufront::FormResize(TObject *Sender)
123     {
124     int w=ClientWidth;
125     int h=ClientHeight;
126     while(w==0)
127     {
128     Width++;
129     w=ClientWidth;
130     }
131     while(h==0)
132     {
133     Height++;
134     h=ClientHeight;
135     }
136    
137     recadre(dx,dy,0);
138     redess();
139     }
140     //---------------------------------------------------------------------------
141     void __fastcall TVisufront::initvue(void)
142     {
143     wglMakeCurrent(hdc,hrc);
144     glMatrixMode(GL_MODELVIEW);
145     glLoadIdentity();
146     double xcentre=0.5*(xmin+xmax);
147     double ycentre=0.5*(ymin+ymax);
148     double zcentre=0.5*(zmin+zmax);
149     glTranslated(xcentre,ycentre,zcentre);
150     glRotatef(-90., 0.0, 1.0, 0.0);
151     glRotatef(-90., 1.0, 0.0, 0.0);
152     glRotatef(45., 0.0, 1.0, 0.0);
153     glRotatef(-45., 0.0, 0.0, 1.0);
154     glTranslated(-xcentre,-ycentre,-zcentre);
155     }
156     //---------------------------------------------------------------------------
157     void __fastcall TVisufront::redess(void)
158     {
159     wglMakeCurrent(hdc,hrc);
160     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
161     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162     if (Axe1->Checked) dessineaxe();
163     glEnable(GL_DEPTH_TEST);
164     glEnable(GL_POLYGON_OFFSET_FILL);
165     glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
166     glBegin(GL_TRIANGLES);
167     for (int i=0;i<numtriangle;i++)
168     {
169     if (lsttriangle[i].active==0) continue;
170     glColor3f(0.0f,0.75f,0.0f);
171     glVertex3f(lstnoeud[lsttriangle[i].n1].x,lstnoeud[lsttriangle[i].n1].y,lstnoeud[lsttriangle[i].n1].z);
172     glVertex3f(lstnoeud[lsttriangle[i].n2].x,lstnoeud[lsttriangle[i].n2].y,lstnoeud[lsttriangle[i].n2].z);
173     glVertex3f(lstnoeud[lsttriangle[i].n3].x,lstnoeud[lsttriangle[i].n3].y,lstnoeud[lsttriangle[i].n3].z);
174     }
175     glEnd();
176     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
177     glPolygonOffset(1.0,1.0);
178     glBegin(GL_TRIANGLES);
179     glEdgeFlag(GL_TRUE);
180     for (int i=0;i<numtriangle;i++)
181     {
182     if (lsttriangle[i].active==0) continue;
183     glColor3f(0.0f,0.0f,1.0f);
184     glVertex3f(lstnoeud[lsttriangle[i].n1].x,lstnoeud[lsttriangle[i].n1].y,lstnoeud[lsttriangle[i].n1].z);
185     glVertex3f(lstnoeud[lsttriangle[i].n2].x,lstnoeud[lsttriangle[i].n2].y,lstnoeud[lsttriangle[i].n2].z);
186     glVertex3f(lstnoeud[lsttriangle[i].n3].x,lstnoeud[lsttriangle[i].n3].y,lstnoeud[lsttriangle[i].n3].z);
187     }
188     glEnd();
189     if (Arte1->Checked)
190     {
191     glBegin(GL_LINES);
192     for (int i=0;i<numsegment;i++)
193     {
194     glColor3f(1.0f,0.0f,0.0f);
195     glVertex3f(lstnoeud[lstsegment[i].n1].x,lstnoeud[lstsegment[i].n1].y,lstnoeud[lstsegment[i].n1].z);
196     glVertex3f(lstnoeud[lstsegment[i].n2].x,lstnoeud[lstsegment[i].n2].y,lstnoeud[lstsegment[i].n2].z);
197     }
198     glEnd();
199     glPointSize(4.0);
200     glBegin(GL_POINTS);
201     for (int i=0;i<numsegment;i++)
202     {
203     glColor3f(1.0f,0.0f,0.0f);
204     glVertex3f(lstnoeud[lstsegment[i].n1].x,lstnoeud[lstsegment[i].n1].y,lstnoeud[lstsegment[i].n1].z);
205     glVertex3f(lstnoeud[lstsegment[i].n2].x,lstnoeud[lstsegment[i].n2].y,lstnoeud[lstsegment[i].n2].z);
206     }
207     glEnd();
208     }
209     glFlush();
210     SwapBuffers(hdc);
211     }
212     //---------------------------------------------------------------------------
213    
214    
215     void __fastcall TVisufront::dessineaxe(void)
216     {
217     double d=max(xmax-xmin,ymax-ymin);
218     d=max(d,zmax-zmin);
219     d=d*0.2;
220     glLineWidth(1.0f);
221     glBegin(GL_LINES);
222     glColor3f(0.0f,0.0f,1.0f);
223     glVertex3f(0.0,0.0,0.0);
224     glVertex3f(d,0.0,0.0);
225     glColor3f(1.0f,0.0f,0.0f);
226     glVertex3f(0.0,0.0,0.0);
227     glVertex3f(0.0,d,0.0);
228     glColor3f(1.0f,1.0f,1.0f);
229     glVertex3f(0.0,0.0,0.0);
230     glVertex3f(0.0,0.0,d);
231     glEnd();
232     }
233     //---------------------------------------------------------------------------
234    
235     void __fastcall TVisufront::FormPaint(TObject *Sender)
236     {
237     wglMakeCurrent(hdc,hrc);
238     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
239     redess();
240     }
241     //---------------------------------------------------------------------------
242    
243     void __fastcall TVisufront::transformation(TObject *Sender, WORD &Key,TShiftState Shift)
244     {
245     wglMakeCurrent(hdc,hrc);
246     GLfloat w = ClientWidth;
247     GLfloat h = ClientHeight;
248     glMatrixMode(GL_MODELVIEW);
249     double un=0.005*min(w,h);
250     double xcentre=0.5*(xmin+xmax);
251     double ycentre=0.5*(ymin+ymax);
252     double zcentre=0.5*(zmin+zmax);
253     if (Key==VK_LEFT)
254     {
255     dx=dx+un;
256     recadre(dx,dy,1);
257     }
258     if (Key==VK_RIGHT)
259     {
260     dx=dx-un;
261     recadre(dx,dy,1);
262     }
263     if (Key==VK_UP)
264     {
265     dy=dy-un;
266     recadre(dx,dy,1);
267     }
268     if (Key==VK_DOWN)
269     {
270     dy=dy+un;
271     recadre(dx,dy,1);
272     }
273     if(Key == VK_F1)
274     {
275     zoom++;
276     glTranslated(xcentre,ycentre,zcentre);
277     glScalef(1.1,1.1,1.1);
278     glTranslated(-xcentre,-ycentre,-zcentre);
279     }
280     if(Key == VK_F2)
281     {
282     zoom--;
283     glTranslated(xcentre,ycentre,zcentre);
284     glScalef(1./1.1,1./1.1,1./1.1);
285     glTranslated(-xcentre,-ycentre,-zcentre);
286     }
287     if(Key == VK_F3)
288     {
289     dx=0.;
290     dy=0.;
291     zoom=0;
292     recadre(dx,dy,1);
293     initvue();
294     }
295     if(Key == VK_F4)
296     {
297     if (zoom>0)
298     glScalef(1./pow(1.1,zoom),1./pow(1.1,zoom),1./pow(1.1,zoom));
299     if (zoom<0)
300     glScalef(pow(1.1,-zoom),pow(1.1,-zoom),pow(1.1,-zoom));
301     zoom=0;
302     dx=0;
303     dy=0;
304     recadre(dx,dy,1);
305     }
306     if(Key == VK_HOME)
307     {
308     glTranslated(xcentre,ycentre,zcentre);
309     glRotatef(-5, 0.0, 1.0, 0.0);
310     glTranslated(-xcentre,-ycentre,-zcentre);
311     }
312     if(Key == VK_END)
313     {
314     glTranslated(xcentre,ycentre,zcentre);
315     glRotatef(5, 0.0, 1.0, 0.0);
316     glTranslated(-xcentre,-ycentre,-zcentre);
317     }
318     if(Key == VK_INSERT)
319     {
320     glTranslated(xcentre,ycentre,zcentre);
321     glRotatef(-5, 1.0, 0.0, 0.0);
322     glTranslated(-xcentre,-ycentre,-zcentre);
323     }
324     if(Key == VK_DELETE)
325     {
326     glTranslated(xcentre,ycentre,zcentre);
327     glRotatef(5, 1.0, 0.0, 0.0);
328     glTranslated(-xcentre,-ycentre,-zcentre);
329     }
330     if(Key == VK_PRIOR)
331     {
332     glTranslated(xcentre,ycentre,zcentre);
333     glRotatef(-5, 0.0, 0.0, 1.0);
334     glTranslated(-xcentre,-ycentre,-zcentre);
335     }
336     if(Key == VK_NEXT)
337     {
338     glTranslated(xcentre,ycentre,zcentre);
339     glRotatef(5, 0.0, 0.0, 1.0);
340     glTranslated(-xcentre,-ycentre,-zcentre);
341     }
342     redess();
343     }
344     void __fastcall TVisufront::FormKeyDown(TObject *Sender, WORD &Key,
345     TShiftState Shift)
346     {
347     transformation(Sender,Key,Shift);
348     }
349     //---------------------------------------------------------------------------
350    
351     void __fastcall TVisufront::Timer1Timer(TObject *Sender)
352     {
353     static int passe=0;
354     char mess[10000];
355     if (N3D1->Checked)
356     {
357     static FILE* in;
358     AnsiString nomfich=chemin+".frt2";
359     if (passe==0) in=fopen(nomfich.c_str(),"rt");
360     fgets(mess,10000,in);
361     if (feof(in))
362     {
363     passe=0;
364     fclose(in);
365     if (!Continue1->Checked) Timer1->Enabled=false;
366     return;
367     }
368     if (arretdemande)
369     {
370     passe=0;
371     Timer1->Enabled=false;
372     arretdemande=0;
373     for (int i=0;i<numtriangle;i++) lsttriangle[i].active=0;
374     redess();
375     return;
376     }
377     for (int i=0;i<numtriangle;i++) lsttriangle[i].active=0;
378     char *p=mess;
379     int nb;
380     do
381     {
382     int num;
383     nb=sscanf(p,"%d",&num);
384     if (nb==1) lsttriangle[num].active=1;
385     p=strchr(p,' ')+1;
386     }
387     while (nb==1);
388     sprintf(mess,"Visualisateur dynamique de front 3D - %s - Itération %d",chemin.c_str(),passe);
389     }
390     if (N2D1->Checked)
391     {
392     if (passe==numtriangle2d)
393     {
394     passe=0;
395     if (!Continue1->Checked) Timer1->Enabled=false;
396     return;
397     }
398     if (arretdemande)
399     {
400     passe=0;
401     Timer1->Enabled=false;
402     for (int i=0;i<numtriangle;i++) lsttriangle[i].active=0;
403     arretdemande=0;
404     redess();
405     return;
406     }
407     for (int i=0;i<numtriangle;i++)
408     {
409     if (i<passe)
410     lsttriangle[i].active=1;
411     else
412     lsttriangle[i].active=0;
413     }
414     sprintf(mess,"Visualisateur dynamique de front 2D - %s - Itération %d",chemin.c_str(),passe);
415     }
416     passe++;
417     redess();
418     Caption=mess;
419     }
420     //---------------------------------------------------------------------------
421    
422    
423     void __fastcall TVisufront::Axe1Click(TObject *Sender)
424     {
425     Axe1->Checked=!Axe1->Checked;
426     redess();
427     }
428     //---------------------------------------------------------------------------
429    
430     void __fastcall TVisufront::Arte1Click(TObject *Sender)
431     {
432     Arte1->Checked=!Arte1->Checked;
433     redess();
434     }
435     //---------------------------------------------------------------------------
436    
437     void __fastcall TVisufront::Quitter1Click(TObject *Sender)
438     {
439     Timer1->Enabled=false;
440     Close() ;
441     }
442     //---------------------------------------------------------------------------
443    
444     void __fastcall TVisufront::rapide1Click(TObject *Sender)
445     {
446     rapide1->Checked=true;
447     moyen1->Checked=false;
448     lent1->Checked=false;
449     extrementlent1->Checked=false;
450     Timer1->Interval=1;
451     }
452     //---------------------------------------------------------------------------
453    
454     void __fastcall TVisufront::moyen1Click(TObject *Sender)
455     {
456     rapide1->Checked=false;
457     moyen1->Checked=true;
458     lent1->Checked=false;
459     extrementlent1->Checked=false;
460     Timer1->Interval=10;
461     }
462     //---------------------------------------------------------------------------
463    
464     void __fastcall TVisufront::lent1Click(TObject *Sender)
465     {
466     rapide1->Checked=false;
467     moyen1->Checked=false;
468     lent1->Checked=true;
469     extrementlent1->Checked=false;
470     Timer1->Interval=100;
471     }
472     //---------------------------------------------------------------------------
473    
474     void __fastcall TVisufront::extrementlent1Click(TObject *Sender)
475     {
476     rapide1->Checked=false;
477     moyen1->Checked=false;
478     lent1->Checked=false;
479     extrementlent1->Checked=true;
480     Timer1->Interval=1000;
481    
482     }
483     //---------------------------------------------------------------------------
484    
485     void __fastcall TVisufront::Pause1Click(TObject *Sender)
486     {
487     Pause1->Checked=!Pause1->Checked;
488     if (Pause1->Checked) Timer1->Enabled=false; else Timer1->Enabled=true;
489     }
490     //---------------------------------------------------------------------------
491    
492     void __fastcall TVisufront::Continue1Click(TObject *Sender)
493     {
494     Continue1->Checked=!Continue1->Checked;
495     }
496     //---------------------------------------------------------------------------
497    
498     void __fastcall TVisufront::Reprendre1Click(TObject *Sender)
499     {
500     if (lire==1)
501     if ( (N2D1->Checked==true) || (N3D1->Checked==true) )
502     Timer1->Enabled=true;
503     }
504     //---------------------------------------------------------------------------
505    
506    
507     void __fastcall TVisufront::Ouvrir1Click(TObject *Sender)
508     {
509     if (OpenDialog1->Execute())
510     {
511     chemin=OpenDialog1->FileName;
512     chemin.Delete(chemin.Length()-3,4);
513     Caption="Visualisateur dynamique de front 3D - " + chemin;
514     AnsiString nomfich=chemin+".frt1";
515     FILE* in=fopen(nomfich.c_str(),"rt");
516     char mess[1000];
517     fgets(mess,1000,in);
518     sscanf(mess,"%d %d",&numnoeud,&numtriangle);
519     lsttriangle=new triangle[numtriangle];
520     lstnoeud=new noeud[numnoeud];
521     for (int i=0;i<numnoeud;i++)
522     {
523     fgets(mess,1000,in);
524     double x,y,z;
525     sscanf(mess,"%lf %lf %lf",&x,&y,&z);
526     lstnoeud[i].x=x;
527     lstnoeud[i].y=y;
528     lstnoeud[i].z=z;
529     if (i==0)
530     {
531     xmin=x;xmax=x;
532     ymin=y;ymax=y;
533     zmin=z;zmax=z;
534     }
535     else
536     {
537     if (x<xmin) xmin=x;
538     if (y<ymin) ymin=y;
539     if (z<zmin) zmin=z;
540     if (x>xmax) xmax=x;
541     if (y>ymax) ymax=y;
542     if (z>zmax) zmax=z;
543     }
544     }
545     for (int i=0;i<numtriangle;i++)
546     {
547     fgets(mess,1000,in);
548     int num,n1,n2,n3;
549     sscanf(mess,"%d %d %d %d",&num,&n1,&n2,&n3);
550     lsttriangle[i].n1=n1;
551     lsttriangle[i].n2=n2;
552     lsttriangle[i].n3=n3;
553     lsttriangle[i].active=0;
554     }
555     fclose(in);
556     nomfich=chemin+"_1D.mai";
557     in=fopen(nomfich.c_str(),"rt");
558     fgets(mess,1000,in);
559     int tmp;
560     sscanf(mess,"%d %d",&tmp,&numsegment);
561     lstsegment=new segment[numsegment];
562     for (int i=0;i<tmp;i++) fgets(mess,1000,in);
563     for (int i=0;i<numsegment;i++)
564     {
565     fgets(mess,1000,in);
566     int num,n1,n2;
567     sscanf(mess,"%d %d %d",&num,&n1,&n2);
568     lstsegment[i].n1=n1-1;
569     lstsegment[i].n2=n2-1;
570     }
571     fclose(in);
572     nomfich=chemin+"_2D.mai";
573     in=fopen(nomfich.c_str(),"rt");
574     fgets(mess,1000,in);
575     sscanf(mess,"%d %d",&tmp,&numtriangle2d);
576     in=fopen(nomfich.c_str(),"rt");
577     fclose(in);
578     glClearColor(0.0f,0.0f,0.0f,0.0f);
579     lire=1;
580     recadre(dx,dy,0);
581     initvue();
582     redess();
583     N2D1->Checked=false;
584     N3D1->Checked=false;
585     Timer1->Enabled=false;
586     }
587     }
588     //---------------------------------------------------------------------------
589    
590     void __fastcall TVisufront::N2D1Click(TObject *Sender)
591     {
592     if (Timer1->Enabled==false)
593     {
594     N2D1->Checked=true;
595     N3D1->Checked=false;
596     }
597     }
598     //---------------------------------------------------------------------------
599    
600     void __fastcall TVisufront::N3D1Click(TObject *Sender)
601     {
602     if (Timer1->Enabled==false)
603     {
604     N2D1->Checked=false;
605     N3D1->Checked=true;
606     }
607     }
608     //---------------------------------------------------------------------------
609    
610     void __fastcall TVisufront::Arrt1Click(TObject *Sender)
611     {
612     arretdemande=1;
613     }
614     //---------------------------------------------------------------------------
615