1 /*
2  *  ffglut.hpp
3  *  ff
4  *
5  *  Created by FrÈdÈric Hecht on 04/11/08.
6  *  Copyright 2008 UPMC. All rights reserved.
7  *
8  */
9 #include <complex>
10 typedef std::complex<double> Complex;
11 extern int debug;
12 static const double dinfty=numeric_limits<double>::max();
13 void DefColor(float & r, float & g, float & b,
14               int k,int nb, bool hsv,bool grey,KN<R> colors);
15 void hsvToRgb (float h, float s, float v, float & r, float & g, float & b);
16 
17 
18 class ThePlot;
19 class OneWindow;
20 
21 struct OnePlot
22 {
23     int dim;
24     R3 Pmin,Pmax;
25     double fmin,fmax;
26     double vmax2;
27     long what;
28     GLsizei ngllists;
29     GLint gllists;
30     KN<int> oklist;
31     int setgllists;
32     virtual void Draw(OneWindow *win) =0;
NextCaseOnePlot33     virtual bool NextCase() {return false;}
bbOnePlot34     void bb(R3 & Pmn,R3 &Pmx) const
35     {
36         Pmn=Minc(Pmin,Pmn);
37         Pmx=Maxc(Pmax,Pmx);
38     }
39 
bfvOnePlot40     void bfv(R & fmn,R &fmx,R & vmx) const
41     {
42         // cout << "\t\t\t\t  f min, max v max :" << fmin << " " << fmax << " " << vmax2 << endl;
43         fmn=Min(fmin,fmn);
44         fmx=Max(fmax,fmx);
45         vmx=Max(vmax2,vmx);
46     }
47 
dyn_bfvOnePlot48     virtual void dyn_bfv(OneWindow *win,R & fmn,R &fmx,R & vmn,R & vmx) const
49     {//  compute the function bound and arrow bound view ....
50     }
51 
OnePlotOnePlot52     OnePlot(long w,int ddim=2,int nbgllist=0) :
53     dim(ddim),
54     Pmin(dinfty,dinfty,dinfty),Pmax(-dinfty,-dinfty,-dinfty),
55     fmin(dinfty),fmax(-dinfty),vmax2(0),
56     what(w),ngllists(nbgllist),gllists(0),
57     oklist(nbgllist),setgllists(0){
58     }
59 
initlistOnePlot60     void initlist()
61     {
62         if(! setgllists)
63             if(ngllists>0)
64             {
65                 oklist=0; //
66                 gllists= glGenLists(ngllists);
67                 setgllists=1;
68                 assert(gllists);
69             }
70     }
71 
72     void GLDraw(OneWindow *win);
~OnePlotOnePlot73     virtual ~OnePlot() {
74         if(setgllists) glDeleteLists(gllists,ngllists);
75     };
76 private: // pas de copy  car il y a des destructeurs dans les classes derives
77     OnePlot(const OnePlot & );
78     OnePlot & operator=(const OnePlot & );
79 };
80 
81 template<class Mesh>
82 struct OnePlotMesh : public OnePlot
83 {
84     const Mesh *Th;
OnePlotMeshOnePlotMesh85     OnePlotMesh(const Mesh *T)
86     : OnePlot(0,2,3),Th(T)
87     {
88         R2 P0,P1;
89         Th->BoundingBox(P0,P1);
90         Pmin=P0;
91         Pmax=P1;
92     }
93     void Draw(OneWindow *win);
94 
95 };
96 
97 struct OnePlotMesh3 : public OnePlot
98 {
99     const Mesh3 *Th;
OnePlotMesh3OnePlotMesh3100     OnePlotMesh3(const Mesh3 *T)
101     : OnePlot(0,3,3),Th(T)
102     {
103         Pmin=Th->Pmin;
104         Pmax=Th->Pmax;
105         //Th->BoundingBox(Pmin,Pmax);
106     }
107     void Draw(OneWindow *win);
108 };
109 
110 struct OnePlotMeshS : public OnePlot
111 {
112     const MeshS *Th;
OnePlotMeshSOnePlotMeshS113     OnePlotMeshS(const MeshS *T)
114     : OnePlot(0,3,3),Th(T)
115     {
116         Pmin=Th->Pmin;
117         Pmax=Th->Pmax;
118         //Th->BoundingBox(Pmin,Pmax);
119     }
120     void Draw(OneWindow *win);
121 };
122 
123 struct OnePlotMeshL : public OnePlot
124 {
125     const MeshL *Th;
OnePlotMeshLOnePlotMeshL126     OnePlotMeshL(const MeshL *T)
127     : OnePlot(0,3,3),Th(T)
128     {
129         Pmin=Th->Pmin;
130         Pmax=Th->Pmax;
131         //Th->BoundingBox(Pmin,Pmax);
132     }
133     void Draw(OneWindow *win);
134 };
135 
136 template<class Mesh>
137 struct OnePlotFE: public OnePlot
138 {
139     const Mesh *Th;
140     // long nsub;
141     KN<double> v;
142     KN<Complex> vc;
143     KN<R2> Psub;
144     KN<int> Ksub;
145     int cas; // in cas of complex  chaage interpertation of complex value
146     OnePlotFE(const Mesh *T,long w,PlotStream & f);
147     void Draw(OneWindow *win);
148     void dyn_bfv(OneWindow *win,R & fmn,R &fmx,R & vmn,R & vmx) const ;
149     bool  vc2v();
NextCaseOnePlotFE150     bool  NextCase() { cas++; return vc2v();}
151 };
152 
153 struct OnePlotFE3: public OnePlot
154 {
155     const Mesh3 *Th;
156     long nsub;
157     KN<double> v;
158     KN<Complex> vc;
159     KN<R3> Psub;
160     KN<int> Ksub;
161     int cas; // in cas of complex  chaage interpertation of complex value
162 
OnePlotFE3OnePlotFE3163     OnePlotFE3(const Mesh3 *T,long w,PlotStream & f)
164     :OnePlot(w,3,5),Th(T),cas(2)
165     {
166         Pmin=Th->Pmin;
167         Pmax=Th->Pmax;
168 
169         f >> Psub ;
170         f >> Ksub ;
171         if(what<10)
172             f >>  v;
173         else
174             f >> vc;
175         vc2v();
176         if(debug>3) cout << "OnePlotFE3" << Th <<" " << what<< " " << nsub <<" " << v.N() << endl
177             << "       Pmin " << Pmin << " Pmax  " << Pmax << endl;
178         ffassert(f.good());
179 
180     }
181     void Draw(OneWindow *win);
182     bool  vc2v();
NextCaseOnePlotFE3183     bool  NextCase() { cas++; return vc2v();}
184 };
185 
186 
187 struct OnePlotFES: public OnePlot
188 {
189     const MeshS *Th;
190     long nsub;
191     KN<double> v;
192     KN<Complex> vc;
193     KN<R2> Psub;
194     KN<int> Ksub;
195     int cas; // in cas of complex  chaage interpertation of complex value
196 
OnePlotFESOnePlotFES197     OnePlotFES(const MeshS *T,long w,PlotStream & f)
198     :OnePlot(w,3,5),Th(T),cas(2)
199     {
200         Pmin=Th->Pmin;
201         Pmax=Th->Pmax;
202 
203         f >> Psub ;
204         f >> Ksub ;
205         if(what<10)
206             f >>  v;
207         else
208             f >> vc;
209         vc2v();
210         if(debug>3) cout << "OnePlotFES :" << Th <<" " << what<< " " << nsub <<" " << v.N() << endl
211             << "       Pmin " << Pmin << " Pmax  " << Pmax << endl;
212         ffassert(f.good());
213 
214     }
215     void Draw(OneWindow *win);
216     bool  vc2v();
NextCaseOnePlotFES217     bool  NextCase() { cas++; return vc2v();}
218 };
219 
220 struct OnePlotFEL: public OnePlot
221 {
222     const MeshL *Th;
223     long nsub;
224     KN<double> v;
225     KN<Complex> vc;
226     KN<R1> Psub;
227     KN<int> Ksub;
228     int cas; // in cas of complex  chaage interpertation of complex value
229 
OnePlotFELOnePlotFEL230     OnePlotFEL(const MeshL *T,long w,PlotStream & f)
231     :OnePlot(w,3,5),Th(T),cas(2)
232     {
233         Pmin=Th->Pmin;
234         Pmax=Th->Pmax;
235 
236         f >> Psub ;
237         f >> Ksub ;
238         if(what<16)
239             f >>  v;
240         else
241             f >> vc;
242         vc2v();
243         if(debug>3) cout << "OnePlotFEL :" << Th <<" " << what<< " " << nsub <<" " << v.N() << endl
244             << "       Pmin " << Pmin << " Pmax  " << Pmax << endl;
245         ffassert(f.good());
246 
247     }
248     void Draw(OneWindow *win);
249     bool  vc2v();
NextCaseOnePlotFEL250     bool  NextCase() { cas++; return vc2v();}
251 };
252 
253 struct OnePlotCurve: public OnePlot {
254     KN<double> xx,yy,zz,cc;
255     int cas;
256     OnePlotCurve(PlotStream & f,int nfield=2,ThePlot *theplot=0);
257     void Draw(OneWindow *win);
258     void  dyn_bfv(OneWindow *win,R & fmn,R &fmx,R & vmn2,R & vmx2) const;
NextCaseOnePlotCurve259      bool  NextCase() { cas++; return false;}
260 };
261 
262 struct OnePlotBorder: public OnePlot {
263     vector<vector<pair<long,R3> > > data;
264     OnePlotBorder(PlotStream & f);
265     void Draw(OneWindow *win);
266 };
267 
268 
269 struct OnePlotHMatrix: public OnePlot
270 {
271     int si;
272     int sj;
273     long nbdense;
274     long nblr;
275     std::vector<std::pair<int,int>> offsetsdense;
276     std::vector<std::pair<int,int>> sizesdense;
277     std::vector<std::pair<int,int>> offsetslr;
278     std::vector<std::pair<int,int>> sizeslr;
279     std::vector<int> rankslr;
280     std::vector<double> compression;
281 
OnePlotHMatrixOnePlotHMatrix282     OnePlotHMatrix(long w, PlotStream & f)
283     :OnePlot(w)
284     {
285         //dim = 2;
286         Pmin = R3(0,0,0);
287         Pmax = R3(1,1,0);
288 
289         int offseti, offsetj;
290         int sizei, sizej;
291         int rank;
292 
293         f >> si;
294         f >> sj;
295         f >> nbdense;
296         f >> nblr;
297 
298         offsetsdense.resize(nbdense);
299         sizesdense.resize(nbdense);
300         offsetslr.resize(nblr);
301         sizeslr.resize(nblr);
302         rankslr.resize(nblr);
303         compression.resize(nblr);
304 
305         for (int i=0;i<nbdense;i++) {
306           f >> offseti;
307           f >> offsetj;
308           f >> sizei;
309           f >> sizej;
310           offsetsdense[i] = std::pair<int,int>(offseti,offsetj);
311           sizesdense[i] = std::pair<int,int>(sizei,sizej);
312         }
313 
314         for (int i=0;i<nblr;i++) {
315           f >> offseti;
316           f >> offsetj;
317           f >> sizei;
318           f >> sizej;
319           f >> rankslr[i];
320           f >> compression[i];
321           offsetslr[i] = std::pair<int,int>(offseti,offsetj);
322           sizeslr[i] = std::pair<int,int>(sizei,sizej);
323         }
324 
325         ffassert(f.good());
326     }
327     void Draw(OneWindow *win);
328 };
329 
330 // add 11/12/2008 for gestion of error FH  (what -1)
331 struct OnePlotError: public OnePlot {
332     long item;
OnePlotErrorOnePlotError333     OnePlotError(PlotStream & f)
334     : OnePlot(-1)
335     {    f >> item;
336     }
337     void Draw(OneWindow *win) ;
338 
339 };
340 
341 
342 
343 class ThePlot
344 {
345 public:
346     int count;
347     int state; // 0 new
348     GLint gllist;
349     KN<R> colors;
350     bool hsv; // hsv  type
351     KN<R> boundingbox;
352     double coeff;
353     bool wait;
354     bool value;
355     bool fill;
356     bool aspectratio;
357     bool clean;
358     bool uaspectratio;
359     bool pViso,pVarrow;
360     bool withiso;
361     bool witharrow;
362 
363     long  Niso,Narrow;
364     R3 Pmin,Pmax,PminT,PmaxT;//  with R -> true bound
365     R  fmin,fmax,fminT,fmaxT; // with  bound with previous plot.
366     R  vmax2;
367     KN<R> Viso,Varrow;
368     int nbN; // intensity for plotting surface normal
369     bool bw;
370     string * psfile;
371     string * cm;
372 
373     bool grey;
374     bool greyo;
375     bool drawborder;
376     bool drawmeshes;
377     bool add,keepPV,pNormalT;
378     int periodNormalT;
379     double echelle;
380     double ArrowSize;
381     vector<Mesh *> Ths;
382     vector<Mesh2 *> Ths2;
383     vector<Mesh3 *> Ths3;
384     vector<MeshS *> ThsS;
385     vector<MeshL *> ThsL;
386     list<OnePlot *> plots;
387     bool changeViso,changeVarrow,changeColor,changeBorder,changeFill;
388     R3 Pvue,Peyes;
389     R alpha;
390     R coefr;
391     R z0; //  z pour les objets 2d.
392     R ZScale;
393     //  for 3d plot jan 2009
394     int  plotdim;
395     bool blockwin, plotNormalT, changePlotdim;
396     R theta, phi, dcoef, focal;
397     int datadim;
398     // 2D
399     long winnum;
400     bool NextCase();
401 
Change() const402     bool Change() const  { return changeViso||changeVarrow||changeColor||changeBorder||changeFill;}
~ThePlot()403     ~ThePlot()
404     {
405         for (list<OnePlot *>::iterator i= plots.begin();i != plots.end(); ++i)
406             if(*i) delete *i;
407         for (vector<Mesh *>::iterator i= Ths.begin();i != Ths.end(); ++i)
408             if(*i) delete *i;
409         for (vector<Mesh2 *>::iterator i= Ths2.begin();i != Ths2.end(); ++i)
410             if(*i) delete *i;
411         for (vector<Mesh3 *>::iterator i= Ths3.begin();i != Ths3.end(); ++i)
412             if(*i) delete *i;
413         for (vector<MeshS *>::iterator i= ThsS.begin();i != ThsS.end(); ++i)
414             if(*i) delete *i;
415         for (vector<MeshL *>::iterator i= ThsL.begin();i != ThsL.end(); ++i)
416             if(*i) delete *i;
417 
418     }
419     ThePlot(PlotStream & fin,ThePlot *old , int kcount);
420 
421     void Draw(OneWindow *win) ;
422     void DrawHelp(OneWindow *win) ;
423 
424     struct RGB  { float r,g,b;
setThePlot::RGB425         void set(int k,int nb,ThePlot *theplot )
426         {
427             DefColor(r,g,b, k,nb,theplot->hsv,theplot->grey,theplot->colors);
428         }
429     } ;
430     vector<RGB>  tbc;
color(int i,R alpha=1.)431     void color(int i,R alpha=1.) {
432         ffassert(tbc.size());
433         RGB c(tbc[min(max(0,i),(const int) tbc.size())]);
434         if(alpha<1) {
435             R a1=1.-alpha;
436             c.r=c.r*alpha+a1;
437             c.g=c.g*alpha+a1;
438             c.b=c.b*alpha+a1;
439 
440             // cout << " aaaa" << alpha << endl;
441         }
442         glColor4f(c.r,c.g,c.b,alpha);
443     }
444     void  SetColorTable(int nb);
445     void SetDefIsoV(int niso=0,int narr=0,R fmn=1.,R fmx=-1.,R vmn=1.,R vmx=-1.);
446     void DrawIsoT(const R2 Pt[3],const R ff[3],const R * Viso,int NbIso, R rapz=1);
447     void DrawIsoTfill(const R2 Pt[3],const R ff[3],const R * Viso,int NbIso, R rapz=1);
448     void DrawIsoT(const R3 Pt[3],const R ff[3],const R3 Nt[3],const R * Viso,int NbIso,bool changePlotdim, int viewdim,R rapz=1);
449     void DrawIsoTfill(const R3 Pt[3],const R ff[3],const R3 Nt[3],const R * Viso,int NbIso,bool changePlotdim,int viewdim, R rapz=1);
450     void DrawIsoEfill(const R3 Pt[2],const R ff[2],const R3 Nt[2],const R * Viso,int NbIso,bool changePlotdim,int viewdim, R rapz=1);
451     void dyn_bfv(OneWindow *win,R & fmn,R &fmx,R & vmn,R & vmx) const ;
452 
453 };
454 
455 class OneWindow {
456 public:
457     int countdisplay;// for bug mojave
458     ThePlot *theplot;
459     list<ThePlot*> lplots;//  to store some plot
460     list<ThePlot*>::iterator icurrentPlot;//=lplots.begin();
461     int lplotssize;
462 
463     int height,width;
464     R2 Bmin,Bmax;
465     R2 oBmin,oBmax;// orign box
466     R zmin,zmax;
467     R hpixel;// taille pixel en coordonne x,y,z
468     int xold,yold;
469     bool windowdump,help;
470 
471     GLdouble modelMatrix[16];
472     GLdouble projMatrix[16];
473     GLint viewport[4];
474 
475     int  plotdim, viewdim;
476     R theta, phi, coef_dist, focal, dtheta;
477     R  rapz,rapz0;
478     R3 Bmin3,Bmax3,Pvue3;
479     R3 cam;
480     bool withlight;
481     bool changearrow,changeiso;// to rebuild de graphic list if neccessary
482     bool keepPV,init,pNormalT,changePlotdim;
483     //double  aspx, aspy, echx,echy,ech,rxmin,rxmax,rymin,rymax;
484     OneWindow(int h,int w,ThePlot *p);
485     void DefaultView(int state) ;
486     bool  NextCase() ;
487     void  SetView() ;
488     void MoveXView(R dx,R dy) ;
489     void set(ThePlot *p);
490     void add(ThePlot *p);
491     void cadre(R2 A,R2 B);
492     void cadreortho(R2 A,R2 B);
493     void getcadre(double &xmin,double &xmax,double &ymin,double &ymax);
494     void Display();
495     void resize(int w,int h);
496     void zoom(int w,int h,R coef);
497     void zoom(R coef);
GetHeigthFont()498     float GetHeigthFont(){return 10;}
color(int i,R alpha=1)499     void color(int i,R alpha=1) {theplot->color(i,alpha);}
500     void FillRect(R x0,R y0,R x1,R y1);
501     void PlotValue(const KN_<double> & Viso,int  k0,const char * cmm);
502     void DrawCommentaire(const char * cm,R x,R y);
503     void SetScreenView() const ;
504     void Show(const char *str,int i);
505     void   setLighting();
506     void unsetLighting();
Seg(R2 A,R2 B) const507     void Seg(R2 A, R2 B) const  {
508         glVertex3d(A.x,A.y,theplot->z0);
509         glVertex3d(B.x,B.y,theplot->z0);
510     }
Seg3(R3 A,R3 B) const511     void Seg3(R3 A, R3 B) const  {
512         glVertex3d(A.x,A.y,A.z);
513         glVertex3d(B.x,B.y,B.z);
514     }
515 
InRecScreen(R2 P1,R2 P2) const516     int InRecScreen(R2 P1,R2 P2) const
517     {
518         R2 Pmn=Minc(P1,P2),Pmx=Maxc(P1,P2);
519         return (Pmx.x >= Bmin.x) && (Pmn.x <= Bmax.x) && (Pmx.y >= Bmin.y) && (Pmn.y <= Bmax.y);
520     }
InRecScreen(R3 P1,R3 P2) const521     int InRecScreen(R3 P1,R3 P2) const
522     {
523         R3 Pmn=Minc(P1,P2),Pmx=Maxc(P1,P2);
524         return (Pmx.x >= viewport[0]) && (Pmn.x <= viewport[2]) && (Pmx.y >= viewport[1]) && (Pmn.y <= viewport[3]);
525     }
526 
527 
528 };
529 
530 void plot(double x,double y,const char *cmm,int font=-1);
531 void plot(double x,double y,double i,int fint = -1);
532 
533 extern map<int,OneWindow*> AllWindows;
534 
CurrentWin()535 inline OneWindow* CurrentWin()
536 {
537     OneWindow* w= AllWindows[glutGetWindow()];
538     ffassert(w);
539     return w;
540 }
541 
542