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

Last change on this file since 1123 was 1123, checked in by ercodmgr, 25 years ago

Ajout programme de lecture/test de table de couleur midas , Reza 31/7/2000

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