source: Sophya/trunk/SophyaPI/PI/Tests/midasitt.cc@ 1933

Last change on this file since 1933 was 1933, checked in by cmv, 24 years ago

pd de compilation cmv 13/3/2002

File size: 16.1 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <math.h>
5
6#include <iostream.h>
7
8#include "pisysdep.h"
9
10#include PIAPP_H
11#include PIMENU_H
12#include PISTDWDG_H
13#include PIWIN_H
14#include PIPERIODIC_H
15
16#include "pidrawwin.h"
17#include "piscdrawwdg.h"
18#include "piimage.h"
19#include "parradapter.h"
20
21#include "psfile.h"
22
23#include "piimgtools.h"
24#include "pidrwtools.h"
25
26#include "pisurfdr.h"
27#include "piyfxdrw.h"
28
29// ---------- classe PITApp -----------------------
30class PITApp : public PIApplication {
31public:
32 PITApp(int narg=0, char* arg[]=NULL);
33 ~PITApp();
34 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
35
36 PIMenu* m[3];
37 PIText* txt;
38 PIPixmap* zoom;
39 PICMapView* cmapv;
40 PIWindow *mCurW;
41 PIColorMap midas_cmap;
42};
43
44/* Tableau pour lire les composantes RGB de MIDAS_ITT */
45static double rgb_r[512];
46static double rgb_g[512];
47static double rgb_b[512];
48static int rgb_ncol = 0;
49
50/* --Methode-- */
51PITApp::PITApp(int narg, char* arg[])
52 : PIApplication(360, 130, narg, arg) , midas_cmap("MIDAS_ColMap", rgb_ncol)
53{
54
55m[0] = new PIMenu(Menubar(),"Fichier");
56m[0]->AppendItem("Info", 10101);
57m[0]->AppendItem("Close", 10110);
58m[0]->AppendItem("Exit", 10150);
59// On accroche le menu au Menubar
60AppendMenu(m[0]);
61
62// 2eme Menu
63
64m[1] = new PIMenu(Menubar(),"Graphics");
65m[1]->AppendItem("Sin(x)/x", 10201);
66m[1]->AppendItem("YfXDrawer", 10202);
67m[1]->AppendItem("2-Drawers", 10203);
68m[1]->AppendItem("Surf-3D", 10204);
69m[1]->AppendItem("Surf-3D-Color", 10205);
70m[1]->AppendItem("Image", 10206);
71m[1]->AppendItem("Image Gauss", 10207);
72
73AppendMenu(m[1]);
74
75
76m[2] = new PIMenu(Menubar(),"Tools");
77m[2]->AppendItem("ShowImageTools", 10301);
78m[2]->AppendItem("ShowDrawerTools", 10302);
79m[2]->AppendItem("PostScript (EPS)", 10305);
80AppendMenu(m[2]);
81
82MainWin()->SetSize(360,130);
83zoom = new PIPixmap(MainWin(), "Zoom", 90, 90, 5, 5);
84zoom->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
85// zoom->SetBinding(PIBK_fixed,PIBK_fixed,PIBK_elastic,PIBK_fixed);
86cmapv = new PICMapView(MainWin(), "CMapView", 350, 25, 5, 100);
87cmapv->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
88// cmapv->SetBinding(PIBK_fixed,PIBK_free,PIBK_fixed,PIBK_free);
89// Creation d' zone texte multiligne
90txt = new PIText(MainWin(), "mltext", 250, 90, 105, 5);
91txt->SetBinding(PIBK_elastic,PIBK_elastic,PIBK_elastic,PIBK_elastic);
92// txt->SetBinding(PIBK_fixed,PIBK_fixed,PIBK_fixed,PIBK_free);
93txt->SetMutiLineMode(true);
94txt->SetTextEditable(false);
95txt->SetText("");
96
97// On alloue les couleurs de la MIDAS_ITT
98 cout << " Allocating MIDAS ITT colors in midas_cmap NCol=" << rgb_ncol << endl;
99PIColor col; // Les couleurs PI sont des entiers de 0 a 65535 (16 bits)
100for(int kc=0; kc<rgb_ncol; kc++) {
101 col.red = (short unsigned)(rgb_r[kc]*65535);
102 col.green = (short unsigned)(rgb_g[kc]*65535);
103 col.blue = (short unsigned)(rgb_b[kc]*65535);
104 midas_cmap.AllocColor(col, kc);
105 }
106SetReady();
107}
108
109/* --Methode-- */
110PITApp::~PITApp()
111{
112for(int i=0; i<3; i++) delete m[i];
113delete txt;
114delete cmapv;
115delete zoom;
116}
117
118
119static int nbwin = 0;
120static int numpsf = 0;
121static int fgswcol = 0;
122
123static double fsinc(double x) {if ( (x>-1.e-8) && (x<1.e-8) ) return(0.); else return (sin(x)/x);}
124
125/* --Methode-- */
126void PITApp::Process(PIMessage msg, PIMsgHandler* sender, void* data)
127{
128char strg[64];
129int i,j;
130
131if ( ( ModMsg(msg) == PIMsg_Active) || (ModMsg(msg) == PIMsg_Close) ) {
132 PIWdg *sndw;
133 sndw = (PIWdg *)sender;
134 if (sndw->kind() == PIWindow::ClassId) {
135 if (ModMsg(msg) == PIMsg_Close) {
136 printf("PITApp::Process()/Debug : Closing Window %lx \n", (long)sender);
137 delete (PIWindow *)sender;
138 mCurW = NULL; }
139 else mCurW = (PIWindow *)sender;
140 }
141}
142else
143 {
144 switch(UserMsg(msg))
145 {
146
147// --------- 1er menu ----------
148 case 10101:
149 txt->SetText("pitgr: PI Graphics Tests \n * 2-D graphics \n * 3-D Graphics \n Image display ");
150 break;
151
152 case 10110 :
153 if (mCurW) { mCurW->Hide(); delete mCurW;}
154 mCurW = NULL;
155 break;
156
157 case 10150: // Sortie d'application
158 Stop();
159 break;
160
161
162// ---------- 3eme menu --------
163 case 10301 :
164 PIImgTools::ShowPIImgTools();
165 break;
166 case 10302 :
167 PIDrwTools::ShowPIDrwTools();
168 break;
169 case 10305 :
170 if(mCurW) {
171 char buff[64];
172 sprintf(buff, "pigr_%d.eps", numpsf); numpsf++;
173 printf("Creating EPS File %s (Encapsulated PostScript) \n", buff);
174 PSFile *mps;
175 mps = new PSFile(buff);
176 mCurW->PSPrint(mps,0,0);
177 delete mps;
178 }
179 break;
180
181// ---------- 2eme menu --------
182
183 case 10201 : // Trace de la fonction sin(x)/x
184 {
185 SetBusy(); // souris -> montre
186 txt->SetText("PIScDrawWdg Creation\n Sin(x)/x \n Button 1 -> Coord \n Button 2 Zoom \n <Alt>O <Alt>V \n Try DrawerTools");
187
188 PIFuncDrawer* mfd = new PIFuncDrawer(fsinc);
189 mfd->GetGraphicAtt().SetColAtt(PI_Gold);
190 mfd->GetGraphicAtt().SetLineAtt(PI_NormalLine);
191 nbwin++; sprintf(strg,"2D Graphics (PI W %d)", nbwin);
192 PIWindow* wp = new PIWindow(this, strg, PIWK_normal, 300, 300, 150, 150);
193 wp->SetAutoDelChilds(true);
194 PIScDrawWdg* scd = new PIScDrawWdg(wp, "scwdg-sinc", 300, 300, 0, 0);
195 scd->SetBinding(PIBK_fixed,PIBK_fixed,PIBK_fixed, PIBK_fixed);
196 scd->SetLimits(0.1, 30., -1., 1.);
197 scd->AddScDrawer(mfd,true);
198 scd->BaseDrawer()->ElAddText(2, 0.8, "Y = Sin(x)/x", PI_Red);
199 scd->SetTitles(" --- PIFuncDrawer (Sin(x)/x) --- ");
200 wp->Show(); // On affiche la fenetre
201 SetReady(); // souris->pointeur , on n'est plus occupe
202 }
203 break;
204
205
206 case 10202 : // Trace Test de PIYfXDrawer
207 {
208 SetBusy(); // souris -> montre
209 txt->SetText("PIScDrawWdg Creation\n PIYfXDrawer \n Button 1 -> Coord \n Button 2 Zoom \n <Alt>O <Alt>V \n Try DrawerTools");
210
211 float* vpx = new float[30];
212 float* vpy = new float[30];
213 int i; double x;
214 for(i=0; i<30; i++) { x = 8.*i/30; vpx[i] = x; vpy[i] = 3*cos(x)+2*sin(x); }
215 PIYfXDrawer* mxyd = new PIYfXDrawer( new P1DAdapter<float>(vpx, 30, true),
216 new P1DAdapter<float>(vpy, 30, true), true);
217 mxyd->GetGraphicAtt().SetColAtt(PI_Red);
218 mxyd->GetGraphicAtt().SetMarkerAtt(7, PI_FTriangleMarker);
219
220 nbwin++; sprintf(strg,"2D Graphics (PI W %d)", nbwin);
221 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, 300, 300, 300, 300);
222 PIScDrawWdg* scd = new PIScDrawWdg(awp, "scwdg-YfX", 300, 300, 0, 0);
223 scd->SetBinding(PIBK_fixed,PIBK_fixed,PIBK_fixed, PIBK_fixed);
224 scd->AddScDrawer(mxyd,true);
225 scd->SetTitles(" --- PIYfXDrawer (3*cons(x)+2*sin(x)) --- ");
226 awp->SetAutoDelChilds(true);
227 awp->Show();
228 SetReady();
229 }
230 break;
231
232 case 10203 : // FunDrawer + PIYfXDrawer
233 {
234 SetBusy(); // souris -> montre
235 txt->SetText("PIScDrawWdg Creation\n PIYfXDrawer + FuncDrawer(Sin(x)/x) \n Button 1 -> Coord \n Button 2 Zoom \n <Alt>O <Alt>V \n Try DrawerTools");
236
237 float* vpx = new float[30];
238 float* vpy = new float[30];
239 int i; double x;
240 for(i=0; i<30; i++) { x = 8.*i/30; vpx[i] = x; vpy[i] = 3*cos(x)+2*sin(x); }
241 PIYfXDrawer* mxyd = new PIYfXDrawer( new P1DAdapter<float>(vpx, 30, true),
242 new P1DAdapter<float>(vpy, 30, true), true);
243
244 mxyd->GetGraphicAtt().SetColAtt(PI_Magenta);
245 mxyd->GetGraphicAtt().SetMarkerAtt(7, PI_FBoxMarker);
246
247 PIFuncDrawer* mfd = new PIFuncDrawer(fsinc);
248 mfd->GetGraphicAtt().SetColAtt(PI_Blue);
249 mfd->GetGraphicAtt().SetLineAtt(PI_NormalLine);
250
251 nbwin++; sprintf(strg,"2D Graphics (PI W %d)", nbwin);
252 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, 400, 400, 300, 300);
253 PIScDrawWdg* scd = new PIScDrawWdg(awp, "scwdg-YfX", 400, 400, 0, 0);
254 scd->SetBinding(PIBK_fixed,PIBK_fixed,PIBK_fixed, PIBK_fixed);
255 scd->AddScDrawer(mxyd,true);
256 scd->AddScDrawer(mfd,true);
257 scd->SetTitles(" --- PIYfXDrawer + PIFuncDrawer --- ");
258 awp->SetAutoDelChilds(true);
259 awp->Show();
260
261 SetReady();
262 }
263 break;
264
265 case 10207 :
266 {
267 SetBusy(); // souris -> montre
268 txt->SetText("PIImage Creation\n Image Display \n Button 1 -> Coord/value \n Button 2 Select Rectangle \n Butt on 3 Move \n <Alt>O <Alt>V <Alt>C \n Try ImageTools");
269
270 int sx=400, sy=600;
271 printf(" Creating Image (%dx%d) ... ", sx, sy);
272 float* img = new float[sx*sy];
273 int i, j;
274 for(i=0;i<sx;i++) {
275 double x = (i-sx/2.)/(sx/5.);
276 for(j=0;j<sy;j++) {
277 double y = (j-sy/2.)/(sy/5.);
278 y = -0.5*(x*x+y*y);
279 if(y<-80.) y=0.;
280 img[j*sx+i] = 1000.*exp(-0.5*(x*x+y*y));
281 }
282 }
283 printf(" >> OK \n");
284 nbwin++; sprintf(strg,"Image (PI W %d)", nbwin);
285 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, sx, sy, 300, 300);
286 PIImage * pii = new PIImage(awp, "fitsimage", sx, sy, 0, 0);
287 pii->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
288 pii->SetZoomWin(zoom, false);
289 pii->SetCMapWin(cmapv, false);
290 pii->ShowCursor(true);
291 pii->SetColMap(&midas_cmap, false);
292 awp->SetAutoDelChilds(true);
293 awp->Show();
294 pii->SetImage(new P2DAdapter<float>(img, sx, sy, true), true);
295 SetReady();
296 }
297 break;
298
299 case 10206 :
300 {
301 SetBusy(); // souris -> montre
302 txt->SetText("PIImage Creation\n Image Display \n Button 1 -> Coord/value \n Button 2 Select Rectangle \n Butt on 3 Move \n <Alt>O <Alt>V <Alt>C \n Try ImageTools");
303
304 int sx, sy;
305 sx = 400;
306 sy = 600;
307 printf(" Creating Image (%dx%d) ... ", sx, sy);
308 float* img = new float[sx*sy];
309 int i, j, k, kk, i0, j0;
310 double lamp, amp,x,y;
311 for(k=0; k<sx*sy; k++) img[k] = 1000.+(random()%200-100);
312// On genere 500 etoiles
313 for(k=0; k<500; k++) {
314 i0 = 20+random()%(sx-20);
315 j0 = 20+random()%(sy-20);
316 lamp = 2.0+0.012*(random()%100);
317 amp = pow(10., lamp);
318// if (k%5 == 0) printf(" DBG %d %d - %g \n", i0, j0, flux);
319 for(i=i0-15; i<i0+15; i++) {
320 if(i<0 || i>=sx) continue;
321 for(j=j0-15; j<j0+15; j++) {
322 if(j<0 || j>=sy) continue;
323 x = i-i0; y = j-j0;
324 kk = j*sx+i;
325 img[kk] += amp*exp(-(x*x)/7.-(y*y)/8.);
326 }
327 }
328 }
329 printf(" >> OK \n");
330
331 nbwin++; sprintf(strg,"Image (PI W %d)", nbwin);
332 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, sx, sy, 300, 300);
333 PIImage * pii = new PIImage(awp, "fitsimage", sx, sy, 0, 0);
334 pii->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
335 pii->SetZoomWin(zoom, false);
336 pii->SetCMapWin(cmapv, false);
337 pii->ShowCursor(true);
338 pii->SetColMap(&midas_cmap, false);
339 awp->SetAutoDelChilds(true);
340 awp->Show();
341 pii->SetImage(new P2DAdapter<float>(img, sx, sy, true), true);
342 SetReady();
343 }
344 break;
345
346
347// Dessins 3D
348 case 10204:
349 {
350 SetBusy(); // souris -> montre
351 txt->SetText("PIDraw3DWdg Creation\n 3-D Surf Display \n Button 1 \n (Shift) Button 2 Rotate \n Button Zoom \n <Alt>O <Alt>V \n Try DrawerTools");
352
353 printf(" Creating Matrix 80x60 ... ");
354 float* tf = new float[60*80];
355 int i,j,k;
356 double xp,yp,rp;
357 for(i=0; i<80; i++)
358 for(j=0; j<60; j++) {
359 xp = i-30; yp = j-20;
360 rp = (xp*xp)/16.+(yp*yp)/9.;
361 tf[j*80+i] = 10.*exp(-rp);
362 xp = i-50; yp = j-28;
363 rp = (xp*xp)/9.+(yp*yp)/9.;
364 tf[j*80+i] += 4.*exp(-rp);
365 }
366 printf(" >> OK \n");
367
368 nbwin++; sprintf(strg,"3D Surf (PI W %d)", nbwin);
369 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, 500, 500, 200, 200);
370 PISurfaceDrawer* sd = new PISurfaceDrawer(new P2DAdapter<float>(tf, 80, 60, true), true, true, true);
371 PIDraw3DWdg* drw3 = new PIDraw3DWdg(awp, "ex3ddrw", 500, 500, 0, 0);
372 drw3->SetBinding(PIBK_fixed , PIBK_fixed , PIBK_fixed, PIBK_fixed);
373 drw3->AddDrawer3D(sd, false);
374 drw3->SetTitles(" --- PISurfaceDrawer (80x60) --- ");
375 awp->SetAutoDelChilds(true);
376 awp->Show();
377 SetReady();
378 }
379 break;
380
381 case 10205:
382 {
383 SetBusy(); // souris -> montre
384 txt->SetText("PIDraw3DWdg Creation\n 3-D Surf Display \n Button 1 \n (Shift) Button 2 Rotate \n Button Zoom \n <Alt>O <Alt>V \n Try DrawerTools");
385
386 printf(" Creating Matrix 50x50 ... ");
387 float * tf = new float[2500];
388 int i,j;
389 double xp,yp,rp;
390 for(i=0; i<50; i++)
391 for(j=0; j<50; j++) {
392 xp = i-15; yp = j-25;
393 rp = (xp*xp)/16.+(yp*yp)/16.;
394 tf[j*50+i] = 11.*exp(-rp);
395 xp = i-35; yp = j-15;
396 rp = (xp*xp)/9.+(yp*yp)/9.;
397 tf[j*50+i] += 3.*exp(-rp);
398 xp = i-45; yp = j-40;
399 rp = (xp*xp)/10.+(yp*yp)/20.;
400 tf[j*50+i] += 6.*exp(-rp);
401 }
402 printf(" >> OK \n");
403
404 nbwin++; sprintf(strg,"PI W# %d 3D Surf", nbwin);
405 PIWindow* awp = new PIWindow(this, strg, PIWK_normal, 500, 500, 200, 200);
406 PISurfaceDrawer* sd = new PISurfaceDrawer(new P2DAdapter<float>(tf, 50, 50, true), true, true, true);
407 if (fgswcol%3 == 0) sd->GetGraphicAtt().SetColMapId(CMAP_COLRJ32);
408 else if (fgswcol%3 == 1) sd->GetGraphicAtt().SetColMapId(CMAP_COLBR32);
409 else sd->GetGraphicAtt().SetColMapId(CMAP_COLRV32);
410 fgswcol++;
411 PIDraw3DWdg* drw3 = new PIDraw3DWdg(awp, "ex3ddrw", 600, 600, 0, 0);
412 drw3->SetBinding(PIBK_fixed , PIBK_fixed , PIBK_fixed, PIBK_fixed);
413 drw3->AddDrawer3D(sd, false);
414 drw3->SetTitles(" --- PISurfaceDrawer (50x50) --- ");
415 awp->SetAutoDelChilds(true);
416 awp->Show();
417 SetReady();
418 }
419 break;
420
421 default :
422// printf("PITApp::Process() Msg= %d (%d - %d)??? \n",
423// (int)msg, (int)UserMsg(msg),(int)ModMsg(msg));
424 break;
425 }
426 }
427return;
428}
429
430
431// ................................................................
432// programme principal
433// ................................................................
434
435
436
437int main(int narg, char *arg[])
438{
439
440if ( (narg < 2) || (strcmp(arg[1],"-h") == 0) ) {
441 printf("midasitt : MIDAS ITT color table test \n");
442 printf(" Usage : midasitt [Xt options] \n");
443 return(0);
444 }
445
446
447string midas_itt_file = arg[1];
448
449cout << " Reading RGB values from MIDAS_ITT file " << (string)arg[1];
450FILE * fip = fopen(arg[1], "r");
451if (fip == NULL) {
452 cout << "midasitt ERROR opening file " << (string)arg[1] << endl;
453 return(2);
454 }
455
456char lineb[512];
457int nlread = 0;
458int numl = 0;
459while ( (fgets(lineb,512,fip) != NULL) && (nlread < 512) ) {
460 lineb[9] = '\0';
461 numl = atoi(lineb);
462 if (numl != (nlread+1) ) continue;
463 lineb[25] = '\0';
464 rgb_r[nlread] = atof(lineb+14);
465 lineb[41] = '\0';
466 rgb_g[nlread] = atof(lineb+30);
467 lineb[60] = '\0';
468 rgb_b[nlread] = atof(lineb+46);
469 nlread++;
470}
471
472fclose(fip);
473if (nlread != 256) {
474 cout << "midasitt ERROR number of lines != 256 " << nlread << endl;
475 return(3);
476}
477 rgb_ncol = nlread;
478
479int kl;
480cout << "MIDAS_ITT: RGB - Red ( File" << (string)arg[1] << ")" << endl;
481for(kl=0; kl<rgb_ncol; kl++) {
482 cout << rgb_r[kl] << ", " ;
483 if ((kl%8) == 0) cout << endl;
484}
485cout << endl;
486cout << "MIDAS_ITT: RGB - Green ( File" << (string)arg[1] << ")" << endl;
487for(kl=0; kl<rgb_ncol; kl++) {
488 cout << rgb_g[kl] << ", " ;
489 if ((kl%8) == 0) cout << endl;
490}
491cout << endl;
492cout << "MIDAS_ITT: RGB - Blue ( File" << (string)arg[1] << ")" << endl;
493for(kl=0; kl<rgb_ncol; kl++) {
494 cout << rgb_b[kl] << ", " ;
495 if ((kl%8) == 0) cout << endl;
496}
497
498cout << "\n \n" << endl;
499
500
501PITApp* app = new PITApp(narg, arg);
502int sx,sy;
503PIApplicationPrefCompSize(sx, sy);
504printf("##### pit-main() PrefCompSize= %d %d ##### \n", sx, sy);
505PIApplicationScreenSize(sx, sy);
506printf("##### pit-main() ScreenSize= %d %d ##### \n", sx, sy);
507PIColorMap* cmap = new PIColorMap(CMAP_GREY32);
508printf("##### pit-main() NbTotColors=%d Already Allocated= %d ##### \n",
509 cmap->TotNbColors(), cmap->NbAllocColors());
510
511
512app->Run();
513
514delete cmap;
515delete app;
516exit(0);
517}
518
519
Note: See TracBrowser for help on using the repository browser.