source: SH_SHM/branches/2011.2/source/img/mapmatrix.c @ 364

Revision 16, 34.9 KB checked in by marcus, 14 years ago (diff)

r1 | svn | 2007-12-13 11:10:29 +0100 (Do, 13 Dez 2007) | 2 lines

Initial import

Line 
1
2/* file mapmatrix.c
3 *      ===========
4 *
5 * version 11, 22-Dec-2003
6 *
7 * maps matrix
8 * K. Stammler, 22-Jun-94
9 */
10
11
12
13#include <stdio.h>
14#include <math.h>
15#include "../basecnst.h"
16#include <Xm/Xm.h>
17#include <Mrm/MrmPublic.h>
18#undef BC_DEFINE_TRUE_FALSE
19#ifdef BC_INC_STDLIB
20#include BC_INC_STDLIB
21#endif
22#include BC_SYSBASE
23#include BC_CPAR
24#include "../motif/pixmaps.h"
25#include "../tcusrdef.h"
26
27#define MY_PI 3.14159265358979323846
28#define MY_E 2.7182818284590452354
29
30
31
32#define w_toplevel 0
33#define w_main 1
34#define w_draw 2
35#define w_menu_main 5
36#define w_entry_quit 51
37#define w_entry_plot 52
38#define w_entry_plot_log 53
39#define w_entry_plot_p2 54
40#define w_entry_plot_p4 55
41#define w_entry_radial_grid 56
42#define w_entry_colors_green_red 57
43#define w_entry_colors_blue_red 58
44#define w_entry_colors_black_white 59
45#define w_entry_colors_blue_yellow 60
46#define MmcMAX_WIDGET 100
47
48#define MmcMAX_COLOR 101
49#define MmcMAXCMT 10
50
51#define MmcCHARHEIGHT 30
52
53#define MmcCOLORS_GREEN_RED 1
54#define MmcCOLORS_BLUE_RED 2
55#define MmcCOLORS_BLACK_WHITE 3
56#define MmcCOLORS_BLUE_YELLOW 4
57
58
59
60/* local types */
61
62typedef struct {
63        float     **val;        /* pointer to matrix */
64        int       width;        /* width of matrix */
65        int       height;       /* height of matrix */
66        float     minval;       /* minimum value */
67        float     maxval;       /* maximum value */
68} MmtMATRIX;
69
70typedef struct {
71        int       colnum;       /* number of colors */
72        unsigned  margin_l;     /* left margin */
73        unsigned  margin_r;     /* right margin */
74        unsigned  margin_t;     /* top margin */
75        unsigned  margin_b;     /* bottom margin */
76} MmtDSP_SETUP;
77
78
79
80/* global variables */
81static char             *mmv_vec[]={"mapmatrix.uid"};   /* MRM database file list */
82static Display *mmv_display;                   /* display */
83static Widget  mmv_w[MmcMAX_WIDGET];           /* widget array */
84static char    mmv_pname[BC_FILELTH+1];        /* program name */
85static MmtMATRIX         mmv_matrix;           /* matrix */
86static MmtDSP_SETUP      mmv_setup;            /* display setup */
87static GC                mmv_gc[MmcMAX_COLOR]; /* color gc's */
88
89static char    mmv_cmt[MmcMAXCMT][BC_LINELTH+1];  /* comment lines */
90static int     mmv_cmtcnt;                        /* comment counter */
91static float   mmv_scale;                         /* scale factor */
92static float   mmv_yscale;                        /* y-scale factor (cart.) */
93static BOOLEAN mmv_pixinit=FALSE;                 /* pixel buffer initialized */
94static BOOLEAN mmv_grid_on=TRUE;                  /* draw grid */
95static BOOLEAN mmv_cartesian;                     /* cartesian coo. system */
96static char    mmv_abstime[cBcTimeLth+1];         /* absolute time */
97static float   mmv_ymin;                          /* y start value (cart) */
98
99
100
101
102/* prototypes */
103static void cb_call_create_widget( Widget widget, int *tag,
104        XmAnyCallbackStruct *data );
105static void cb_call_activate( Widget widget, int *tag,
106        XmToggleButtonCallbackStruct *data );
107void cb_action_motion_rad( Widget w, XEvent *ev, String *params,
108        Cardinal *parno );
109void cb_action_motion_cart( Widget w, XEvent *ev, String *params,
110        Cardinal *parno );
111static void cb_call_expose( Widget widget, char *tag,
112        XmDrawingAreaCallbackStruct *data );
113
114static void mm_prepare_gc( Widget w, MmtDSP_SETUP *setup, GC gc[] );
115static void mm_read_matrix( char filename[], BOOLEAN sqr, MmtMATRIX *mat );
116static void mm_display_matrix( Widget w, GC gc[], MmtDSP_SETUP *setup,
117        MmtMATRIX *mat, int power );
118static void mm_draw_radial_grid( Widget w );
119static void mm_change_gc( Widget w, int mode, MmtDSP_SETUP *setup, GC gc[] );
120
121
122/* routines to register */
123static MrmCount         mmv_regnum = 3 ;
124static MrmRegisterArg   mmv_regvec[] = {
125        { "call_create_widget", (caddr_t)cb_call_create_widget },
126        { "call_activate", (caddr_t)cb_call_activate },
127        { "call_expose", (caddr_t)cb_call_expose }
128};
129
130
131
132/* action routines */
133static   XtActionsRec mmv_new_actions_rad[] = {
134        {"cb_action_motion_rad", cb_action_motion_rad}
135};
136static   XtActionsRec mmv_new_actions_cart[] = {
137        {"cb_action_motion_cart", cb_action_motion_cart}
138};
139
140
141
142int main( int argc, char *argv[] )
143{
144        /* local variables */
145        MrmHierarchy      hierarchy;        /* hierarchy */
146        MrmCode           class;            /* class */
147        XtAppContext      app_context;      /* application contex */
148        Arg arglist[2];
149        int n;
150        char              inpfile[BC_FILELTH+1];  /* name of input file */
151        int               pix_w, pix_h;     /* size of window in pixel */
152        BOOLEAN           square_it;        /* square input data */
153        TSyStatus         locstat;          /* local status */
154
155        /* executable code */
156
157        pa_init( argc, argv );
158        pix_w = pix_h = 0;
159
160        if  (pa_pnumber() != 3)  {
161                fprintf( stderr, "*** Usage: %s <file> <colnum> <scale> ***\n", argv[0] );
162                fprintf( stderr, "      qualifiers:\n" );
163                fprintf( stderr, "      -w=<pixwidth>    width of window in pixel\n" );
164                fprintf( stderr, "      -h=<pixheight>   height of window in pixel\n" );
165                return 1;
166        } /*endif*/
167
168        /* set margins */
169        mmv_setup.margin_l = 20;
170        mmv_setup.margin_r = 320;
171        mmv_setup.margin_t = 20;
172        mmv_setup.margin_b = 20;
173
174        /* strcpy( mmv_pname, argv[0] ); */
175        strcpy( mmv_pname, "mapmatrix" );
176        strcpy( inpfile, pa_pvalue(1) );
177        sscanf( pa_pvalue(2), "%d", &mmv_setup.colnum );
178        sscanf( pa_pvalue(3), "%f", &mmv_scale );
179        if  (mmv_setup.colnum < 2)  mmv_setup.colnum = 2;
180        if  (mmv_setup.colnum > 500)  mmv_setup.colnum = 500;
181        if  (pa_qspecified("-w"))  {
182                sscanf( pa_qvalue("-w"), "%d", &pix_w );
183                if  (pix_w < 400)  pix_w = 400;
184        } /*endif*/
185        if  (pa_qspecified("-h"))  {
186                sscanf( pa_qvalue("-h"), "%d", &pix_h );
187                if  (pix_h < 400)  pix_h = 400;
188        } /*endif*/
189        if  (pa_qspecified("-r"))
190                sscanf( pa_qvalue("-r"), "%d", &mmv_setup.margin_r );
191        if  (pa_qspecified("-nogrid"))  mmv_grid_on = FALSE;
192        square_it = FALSE;
193        if  (pa_qspecified("-square"))  square_it = TRUE;
194        mmv_cartesian = FALSE;
195        if  (pa_qspecified("-ymax"))  {
196                mmv_cartesian = TRUE;
197                sscanf( pa_qvalue("-ymax"), "%f", &mmv_yscale );
198        } /*endif*/
199        mmv_ymin = 0.0;
200        if  (pa_qspecified("-ymin"))
201                sscanf( pa_qvalue("-ymin"), "%f", &mmv_ymin );
202        *mmv_abstime = '\0';
203        if  (pa_qspecified("-xtime"))
204                strcpy( mmv_abstime, pa_qvalue("-xtime") );
205
206        if  (mmv_ymin != 0.0)
207                mmv_yscale -= mmv_ymin;
208
209        MrmInitialize ();
210        XtToolkitInitialize();
211        app_context = XtCreateApplicationContext();
212        mmv_display = XtOpenDisplay(app_context, NULL, mmv_pname, "mapmatrix",
213                NULL, 0, &argc, argv);
214        if (mmv_display == NULL) {
215                fprintf(stderr, "%s:  Can't open display\n", mmv_pname );
216                exit(1);
217        } /*endif*/
218
219        if  (mmv_cartesian)
220                XtAppAddActions( app_context, mmv_new_actions_cart, 1 );
221        else
222                XtAppAddActions( app_context, mmv_new_actions_rad, 1 );
223
224        n = 0;
225        /* XtSetArg(arglist[n], XmNallowShellResize, True);  n++; */
226        mmv_w[w_toplevel] = XtAppCreateShell( mmv_pname, NULL,
227                applicationShellWidgetClass, mmv_display, arglist, n);
228
229        if  (MrmOpenHierarchy(1,mmv_vec,NULL,&hierarchy) != MrmSUCCESS) {
230                fprintf ( stderr, "can't open hierarchy\n" );
231                exit( 1 );
232        } /*endif*/
233
234        if  (MrmRegisterNames(mmv_regvec,mmv_regnum) != MrmSUCCESS)  {
235                fprintf( stderr, "can't register names\n" );
236                exit( 1 );
237        } /*endif*/
238
239        if (MrmFetchWidget(hierarchy,"main_window",mmv_w[w_toplevel],
240                mmv_w+w_main,&class) != MrmSUCCESS)  {
241                fprintf( stderr, "can't fetch widget\n" );
242                exit( 1 );
243        } /*endif*/
244
245        n = 0;
246        if  (pix_w > 0)  {XtSetArg( arglist[n], XmNwidth, pix_w ); n++;}
247        if  (pix_h > 0)  {XtSetArg( arglist[n], XmNheight, pix_h ); n++;}
248        if  (n > 0)  XtSetValues( mmv_w[w_draw], arglist, n );
249        /*if  (n > 0)  XtSetValues( mmv_w[w_main], arglist, n );*/
250
251        XtManageChild( mmv_w[w_main] );
252        XtRealizeWidget( mmv_w[w_toplevel] );
253
254        /* read in data file */
255        mm_read_matrix( inpfile, square_it, &mmv_matrix );
256        mm_prepare_gc( mmv_w[w_draw], &mmv_setup, mmv_gc );
257
258        if  (!mmv_pixinit)  {
259                locstat = BC_NOERROR;
260                pix_create_window_buffer( XtDisplay(mmv_w[w_draw]),
261                        XtWindow(mmv_w[w_draw]), TRUE, &locstat );
262                if  (Severe(&locstat))  {
263                        fprintf( stderr, "mapmatrix: error creating window buffer\n" );
264                        exit( 1 );
265                } /*endif*/
266                mmv_pixinit = TRUE;
267        } /*endif*/
268        mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, 1 );
269        if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
270
271        XtAppMainLoop(app_context);
272
273        return 0;
274
275} /* end of main */
276
277
278
279/*--------------------------------------------------------------------------*/
280
281
282
283static void cb_call_create_widget( Widget widget, int *tag,
284        XmAnyCallbackStruct *data )
285
286/* Callback routine on createing widgets
287 *
288 * parameters of routine
289 * Widget     widget;          input; widget number of drawing area
290 * int        *tag;            input; widget number
291 * XmAnyCallbackStruct *data;  input; not used here
292 */
293{
294        /* local variables */
295        int      wno = *tag;                 /* widget number */
296        char     acttable[BC_LONGSTRLTH+1];  /* action table */
297        XtTranslations new_table;            /* translated table */
298        Arg      al[1];
299
300        /* executable code */
301
302        if  (wno >= MmcMAX_WIDGET || wno < 0)  {
303                fprintf( stderr, "--> illegal widget number %d\n", wno );
304                return;
305        } /*endif*/
306
307        /* printf( "[%d]", wno ); */
308        mmv_w[wno] = widget;
309
310        if  (wno == w_draw)  {
311                if  (mmv_cartesian)  {
312                        strcpy( acttable, "<Motion>: cb_action_motion_cart()\n" );
313                        strcat( acttable, "<Btn1Down>: cb_action_motion_cart()\n" );
314                        strcat( acttable, "<Btn1Up>: cb_action_motion_cart()\n" );
315                        strcat( acttable, "<Btn2Down>: cb_action_motion_cart()\n" );
316                        strcat( acttable, "<Btn2Up>: cb_action_motion_cart()\n" );
317                        strcat( acttable, "<Btn3Down>: cb_action_motion_cart()\n" );
318                        strcat( acttable, "<Btn3Up>: cb_action_motion_cart()\n" );
319                } else {
320                        strcpy( acttable, "<Motion>: cb_action_motion_rad()\n" );
321                        strcat( acttable, "<Btn1Down>: cb_action_motion_rad()\n" );
322                        strcat( acttable, "<Btn1Up>: cb_action_motion_rad()\n" );
323                        strcat( acttable, "<Btn2Down>: cb_action_motion_rad()\n" );
324                        strcat( acttable, "<Btn2Up>: cb_action_motion_rad()\n" );
325                        strcat( acttable, "<Btn3Down>: cb_action_motion_rad()\n" );
326                        strcat( acttable, "<Btn3Up>: cb_action_motion_rad()\n" );
327                } /*endif*/
328                new_table = XtParseTranslationTable( acttable );
329                XtSetArg( al[0], XmNtranslations, new_table );
330                XtSetValues( mmv_w[w_draw], al, 1 );
331        } /*endif*/
332
333} /* end of cb_call_create_widget */
334
335
336
337/*--------------------------------------------------------------------------*/
338
339
340
341static void cb_call_activate( Widget widget, int *tag,
342        XmToggleButtonCallbackStruct *data )
343
344/* Callback routine on button selection
345 *
346 * parameters of routine
347 * Widget     widget;          input; widget number of drawing area
348 * int        *tag;            input; tag entry
349 * XmPushButtonCallbackStruct *data;  input;
350 */
351{
352        /* local variables */
353        static int power=1;           /* current power */
354        STATUS   locstat;             /* local status */
355
356        /* executable code */
357
358        if  (!mmv_pixinit)  {
359                locstat = BC_NOERROR;
360                pix_create_window_buffer( XtDisplay(mmv_w[w_draw]),
361                        XtWindow(mmv_w[w_draw]), TRUE, &locstat );
362                if  (Severe(&locstat))  {
363                        fprintf( stderr, "mapmatrix: error creating window buffer\n" );
364                        exit( 1 );
365                } /*endif*/
366                mmv_pixinit = TRUE;
367        } /*endif*/
368
369        switch  (*tag)  {
370        case w_entry_quit:
371                exit( 0 );
372        case w_entry_plot:
373                power=1;
374                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, 1 );
375                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
376                break;
377        case w_entry_plot_log:
378                power = 0;
379                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, 0 );
380                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
381                break;
382        case w_entry_plot_p2:
383                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix,
384                        ++power );
385                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
386                break;
387        case w_entry_plot_p4:
388                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix,
389                        --power );
390                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
391                break;
392        case w_entry_radial_grid:
393                mmv_grid_on = !mmv_grid_on;
394                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
395                else mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix,
396                        power );
397                break;
398        case w_entry_colors_green_red:
399                mm_change_gc( mmv_w[w_draw], MmcCOLORS_GREEN_RED, &mmv_setup, mmv_gc );
400                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, power);
401                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
402                break;
403        case w_entry_colors_blue_red:
404                mm_change_gc( mmv_w[w_draw], MmcCOLORS_BLUE_RED, &mmv_setup, mmv_gc );
405                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, power);
406                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
407                break;
408        case w_entry_colors_black_white:
409                mm_change_gc( mmv_w[w_draw], MmcCOLORS_BLACK_WHITE, &mmv_setup, mmv_gc );
410                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, power);
411                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
412                break;
413        case w_entry_colors_blue_yellow:
414                mm_change_gc( mmv_w[w_draw], MmcCOLORS_BLUE_YELLOW, &mmv_setup, mmv_gc );
415                mm_display_matrix( mmv_w[w_draw], mmv_gc, &mmv_setup, &mmv_matrix, power);
416                if  (mmv_grid_on)  mm_draw_radial_grid( mmv_w[w_draw] );
417                break;
418        default:
419                fprintf( stderr, "illegal widget activated (%d)\n", *tag );
420                return;
421        } /*endswitch*/
422
423} /* end of cb_call_activate */
424
425
426
427/*--------------------------------------------------------------------------*/
428
429
430
431static void cb_call_expose( Widget widget, char *tag,
432        XmDrawingAreaCallbackStruct *data )
433
434/* Callback routine on creation of drawing area
435 *
436 * parameters of routine
437 * Widget     widget;          input; widget number of drawing area
438 * char       *tag;            input; not used here
439 * XmDrawingAreaCallbackStruct *data;  input;
440 */
441{
442        /* local variables */
443
444        /* executable code */
445
446        if  (mmv_pixinit)
447                pix_manage_exposure( &(data->event->xexpose) );
448
449} /* end of cb_call_expose */
450
451
452
453/*--------------------------------------------------------------------------*/
454
455
456#define POSTEXT_X 10
457#define POSTEXT_Y 10
458
459
460
461void cb_action_motion_rad( Widget w, XEvent *ev, String *params,
462        Cardinal *parno )
463
464/* action routine for pointer motion
465 *
466 * parameters of routine
467 * Widget     w;      input; widget ID
468 * XEvent     *ev;    input; current event
469 * ...   (don't know much about it)
470 */
471{
472        /* local variables */
473        static BOOLEAN is_init=FALSE;       /* already initialized */
474        static int     pos_x, pos_y;        /* draw position */
475        static GC      xgc;                 /* clear GC */
476        static char    oldstr[BC_LINELTH+1];/* old string */
477        static char    oldstr2[BC_LINELTH+1];/* old string */
478        static int     eff_w, eff_h;        /* effective size of window */
479        char     postext[BC_LINELTH+1];     /* position text */
480        char     postext2[BC_LINELTH+1];    /* position text */
481        Window   root;                      /* root window */
482        int      w_x, w_y;                  /* window position */
483        unsigned w_w, w_h;                  /* size of window */
484   unsigned border, depth;             /* window pars (not used) */
485        float    slow_x, slow_y;            /* slowness position */
486        float    slowness, azimuth;         /* slowness and azimuth */
487        float    veloc;                     /* current velocity */
488
489        /* executable code */
490
491        if  (w != mmv_w[w_draw])  {printf("--> motion: ill wdw\n"); return;}
492
493        if  (!is_init)  {
494                XGetGeometry( XtDisplay(w), XtWindow(w), &root, &w_x, &w_y, &w_w, &w_h,
495                        &border, &depth );
496                pos_x = w_w - mmv_setup.margin_r + POSTEXT_X;
497                pos_y = w_h - mmv_setup.margin_b - POSTEXT_Y;
498                eff_w = w_w - mmv_setup.margin_l - mmv_setup.margin_r;
499                eff_h = w_h - mmv_setup.margin_t - mmv_setup.margin_b;
500                xgc = XCreateGC( XtDisplay(w), XtWindow(w), 0, NULL );
501                /* XSetForeground( XtDisplay(w), xgc, BlackPixel(XtDisplay(w),0) ); */
502                /* XSetBackground( XtDisplay(w), xgc, WhitePixel(XtDisplay(w),0) ); */
503                XCopyGC( XtDisplay(w), mmv_gc[mmv_setup.colnum], GCFont, xgc );
504                XSetForeground( XtDisplay(w), xgc, WhitePixel(XtDisplay(w),0) );
505                XSetBackground( XtDisplay(w), xgc, BlackPixel(XtDisplay(w),0) );
506                /* XSetFunction( XtDisplay(w), xgc, GXxor ); */
507                is_init = TRUE;
508        } /*endif*/
509
510        if  (ev->type == MotionNotify)  {
511                if  (ev->xmotion.x < mmv_setup.margin_l
512                        || ev->xmotion.y < mmv_setup.margin_t
513                        || ev->xmotion.x > mmv_setup.margin_l+eff_w
514                        || ev->xmotion.y > mmv_setup.margin_t+eff_h)  {
515                        *postext = *postext2 = '\0';
516                } else {
517                        slow_x = (float)(ev->xmotion.x - mmv_setup.margin_l);
518                        slow_y = (float)(ev->xmotion.y - mmv_setup.margin_t);
519                        slow_x = slow_x / (float)eff_w * mmv_scale * 2.0 - mmv_scale;
520                        slow_y = -(slow_y / (float)eff_h * mmv_scale * 2.0 - mmv_scale);
521                        slowness = sqrt( slow_x*slow_x + slow_y*slow_y );
522                        if  (slow_x == 0.0 && slow_y == 0.0)  azimuth = 0.0;
523                        else azimuth = atan2( slow_x, slow_y ) / (2.0*MY_PI) * 360.0;
524                        if  (azimuth < 0.0)  azimuth += 360.0;
525                        if  (slowness > 0.0)  veloc = 1.0 / slowness;
526                        sprintf( postext, " curr. slowness  : %6.2f,%6.2f", slow_x, slow_y );
527                        sprintf( postext2, " vel, slo, az: %6.2f %6.2f  %6.2f",
528                                veloc, slowness, azimuth );
529                } /*endif*/
530                if  (*oldstr != '\0')  {
531                        pix_DrawString( XtDisplay(w), XtWindow(w), xgc,
532                                pos_x, pos_y, oldstr, strlen(oldstr) );
533                        pix_DrawString( XtDisplay(w), XtWindow(w), xgc,
534                                pos_x, pos_y+20, oldstr2, strlen(oldstr2) );
535                } /*endif*/
536                if  (*postext != '\0')  {
537                        pix_DrawString( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
538                                pos_x, pos_y, postext, strlen(postext) );
539                        pix_DrawString( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
540                                pos_x, pos_y+20, postext2, strlen(postext2) );
541                } /*endif*/
542                strcpy( oldstr, postext );
543                strcpy( oldstr2, postext2 );
544                XFlush( XtDisplay(w) );
545        } /*endif*/
546
547} /* end of cb_action_motion_rad */
548
549
550
551/*--------------------------------------------------------------------------*/
552
553
554
555
556void cb_action_motion_cart( Widget w, XEvent *ev, String *params,
557        Cardinal *parno )
558
559/* action routine for pointer motion
560 *
561 * parameters of routine
562 * Widget     w;      input; widget ID
563 * XEvent     *ev;    input; current event
564 * ...   (don't know much about it)
565 */
566{
567        /* local variables */
568        static BOOLEAN is_init=FALSE;       /* already initialized */
569        static int     pos_x, pos_y;        /* draw position */
570        static GC      xgc;                 /* clear GC */
571        static char    oldstr[BC_LINELTH+1];/* old string */
572        static char    oldstr2[BC_LINELTH+1];/* old string */
573        static int     eff_w, eff_h;        /* effective size of window */
574        char     postext[BC_LINELTH+1];     /* position text */
575        char     postext2[BC_LINELTH+1];    /* position text */
576        Window   root;                      /* root window */
577        int      w_x, w_y;                  /* window position */
578        unsigned w_w, w_h;                  /* size of window */
579   unsigned border, depth;             /* window pars (not used) */
580        float    slow_x, slow_y;            /* slowness position */
581        char     ctime[cBcTimeLth+1];       /* current absolute time */
582        STATUS   locstat;                   /* local status */
583
584        /* executable code */
585
586        if  (w != mmv_w[w_draw])  {printf("--> motion: ill wdw\n"); return;}
587
588        if  (!is_init)  {
589                XGetGeometry( XtDisplay(w), XtWindow(w), &root, &w_x, &w_y, &w_w, &w_h,
590                        &border, &depth );
591                pos_x = w_w - mmv_setup.margin_r + POSTEXT_X;
592                pos_y = w_h - mmv_setup.margin_b - POSTEXT_Y;
593                eff_w = w_w - mmv_setup.margin_l - mmv_setup.margin_r;
594                eff_h = w_h - mmv_setup.margin_t - mmv_setup.margin_b;
595                xgc = XCreateGC( XtDisplay(w), XtWindow(w), 0, NULL );
596                /* XSetForeground( XtDisplay(w), xgc, BlackPixel(XtDisplay(w),0) ); */
597                /* XSetBackground( XtDisplay(w), xgc, WhitePixel(XtDisplay(w),0) ); */
598                XCopyGC( XtDisplay(w), mmv_gc[mmv_setup.colnum], GCFont, xgc );
599                XSetForeground( XtDisplay(w), xgc, WhitePixel(XtDisplay(w),0) );
600                XSetBackground( XtDisplay(w), xgc, BlackPixel(XtDisplay(w),0) );
601                /* XSetFunction( XtDisplay(w), xgc, GXxor ); */
602                is_init = TRUE;
603        } /*endif*/
604
605        if  (ev->type == MotionNotify)  {
606                if  (ev->xmotion.x < mmv_setup.margin_l
607                        || ev->xmotion.y < mmv_setup.margin_t
608                        || ev->xmotion.x > mmv_setup.margin_l+eff_w
609                        || ev->xmotion.y > mmv_setup.margin_t+eff_h)  {
610                        *postext = *postext2 = '\0';
611                } else {
612                        slow_x = (float)(ev->xmotion.x - mmv_setup.margin_l);
613                        slow_y = (float)(ev->xmotion.y - mmv_setup.margin_t);
614                        slow_x = slow_x / (float)eff_w * mmv_scale;
615                        slow_y = mmv_ymin + mmv_yscale - (slow_y / (float)eff_h * mmv_yscale);
616                        locstat = cBcNoError;
617                        if  (*mmv_abstime == '\0')  {
618                                sprintf( postext,  " curr. time: %6.2f", slow_x );
619                        } else {
620                                tc_tadd( mmv_abstime, slow_x, ctime, &locstat );
621                                strcpy( postext, ctime );
622                        } /*endif*/
623                        sprintf( postext2, " slowness  : %6.2f", slow_y );
624                } /*endif*/
625                if  (*oldstr != '\0')  {
626                        pix_DrawString( XtDisplay(w), XtWindow(w), xgc,
627                                pos_x, pos_y, oldstr, strlen(oldstr) );
628                        pix_DrawString( XtDisplay(w), XtWindow(w), xgc,
629                                pos_x, pos_y+20, oldstr2, strlen(oldstr2) );
630                } /*endif*/
631                if  (*postext != '\0')  {
632                        pix_DrawString( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
633                                pos_x, pos_y, postext, strlen(postext) );
634                        pix_DrawString( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
635                                pos_x, pos_y+20, postext2, strlen(postext2) );
636                } /*endif*/
637                strcpy( oldstr, postext );
638                strcpy( oldstr2, postext2 );
639                XFlush( XtDisplay(w) );
640        } /*endif*/
641
642} /* end of cb_action_motion_cart */
643
644
645
646/*--------------------------------------------------------------------------*/
647
648
649
650static void mm_read_matrix( char filename[], BOOLEAN sqr, MmtMATRIX *mat )
651
652/* Allocates memory for matrix and reads it from file
653 *
654 * parameters of routine
655 * char       filename[];        input; name of input file
656 * BOOLEAN    sqr;               input; square input
657 * MmtMATRIX  *mat;              output; matrix
658 */
659{
660        /* local variables */
661        FILE     *fp;         /* pointer to input file */
662        char     line[BC_LINELTH+1];   /* current line in input file */
663        int      i, j;        /* counters */
664
665        /* executable code */
666
667        fp = fopen( filename, "r" );
668        if  (fp == NULL)  {
669                fprintf( stderr, "%s: couldn't open input file %s\n",
670                        mmv_pname, filename );
671                exit( 1 );
672        } /*endif*/
673
674        /* read off comments */
675        mmv_cmtcnt = 0;
676        while  (fgets(line,BC_LINELTH,fp) != NULL)  {
677                if  (*line != '!')  break;
678                if  (line[1] == '*' && mmv_cmtcnt < MmcMAXCMT)  {
679                        strcpy( mmv_cmt[mmv_cmtcnt], line+2 );
680                        i = strlen( mmv_cmt[mmv_cmtcnt] ) - 1;
681                        if  (mmv_cmt[mmv_cmtcnt][i] == '\n')
682                                mmv_cmt[mmv_cmtcnt][i] = '\0';
683                        mmv_cmtcnt++;
684                } /*endif*/
685        } /*endwhile*/
686
687        if  (sscanf(line,"%d %d",&(mat->width),&(mat->height)) != 2)  {
688                fprintf( stderr, "%s: read error on input file %s\n",
689                        mmv_pname, filename );
690                exit( 1 );
691        } /*endif*/
692
693        /* allocate pointers */
694        mat->val = (float **)malloc( sizeof(float *) * mat->height );
695        if  (mat->val == NULL)  {
696                fprintf( stderr, "%s: allocation error (pointers)\n", mmv_pname );
697                exit( 1 );
698        } /*endif*/
699        /* allocate rows */
700        for  (i=0; i<(mat->height); i++)  {
701                mat->val[i] = (float *)malloc( sizeof(float) * mat->width );
702                if  (mat->val[i] == NULL)  {
703                        fprintf( stderr, "%s: allocation error (row %d)\n", mmv_pname, i );
704                        exit( 1 );
705                } /*endif*/
706        } /*endfor*/
707
708        /* read in data */
709        for  (i=0; i<(mat->height); i++)
710                for  (j=0; j<(mat->width); j++)  {
711                        fscanf( fp, "%f\n", (mat->val[i])+j );
712                        if  (sqr)  mat->val[i][j] = (mat->val[i][j])*(mat->val[i][j]);
713                } /*endfor*/
714
715        fclose( fp );
716
717        /* find min & max, test output */
718        mat->minval = mat->maxval = mat->val[0][0];
719        for  (i=0; i<(mat->height); i++)  {
720                for  (j=0; j<(mat->width); j++)  {
721                        /* printf( "%f ", mat->val[i][j] ); */
722                        if  (mat->val[i][j] > mat->maxval)  mat->maxval = mat->val[i][j];
723                        if  (mat->val[i][j] < mat->minval)  mat->minval = mat->val[i][j];
724                } /*endfor*/
725                /* printf( "\n" ); */
726        } /*endfor*/
727
728        /*printf( "--> min, max: %f %f\n", mat->minval, mat->maxval );*/
729
730} /* end of mm_read_matrix */
731
732
733
734/*--------------------------------------------------------------------------*/
735
736
737
738
739static void mm_prepare_gc( Widget w, MmtDSP_SETUP *setup, GC gc[] )
740
741/* creates GC's for display
742 *
743 * parameters of routine
744 * Widget     w;          input; widget of drawing area
745 * MmtDSP_SETUP *setup;   input; display setup
746 * GC         gc[];       output; array of GC's
747 */
748{
749        /* local variables */
750        static char *visual_class[] = {
751                "StaticGray", "GrayScale", "StaticColor", "PseudoColor",
752                "TrueColor", "DirectColor"
753        };
754        int      default_depth;       /* default depth */
755        Visual   *default_visual;     /* default visual */
756        XColor   color;               /* color */
757        Colormap default_cmap;        /* default colormap */
758        XVisualInfo visual_info;      /* visual info */
759        unsigned i;                   /* counter */
760        float    frac;                /* color fraction */
761        float    frac_x, c;           /* scratch */
762        int      screen_num;          /* screen number */
763        Font     out_font;            /* font for text */
764
765        /* executable code */
766
767        screen_num = 0;
768
769        default_depth = DefaultDepth( XtDisplay(w), screen_num );
770        default_visual = DefaultVisual( XtDisplay(w), screen_num );
771        default_cmap = DefaultColormap( XtDisplay(w), screen_num );
772        if  (default_depth == 1)  {
773                fprintf( stderr, "%s: StaticGray visual not supported\n",
774                        mmv_pname );
775                exit( 1 );
776        } /*endif*/
777
778        i = 5;
779        while  (!XMatchVisualInfo(XtDisplay(w),screen_num,default_depth,
780                i--,&visual_info))
781                ;
782
783        printf( "%s: found a %s class visual at default depth %d\n",
784                mmv_pname, visual_class[++i] );
785        if  (i < StaticColor)  {
786                fprintf( stderr, "%s: visual class %s is not supported\n",
787                        mmv_pname, visual_class[i] );
788                exit( 1 );
789        } /*endif*/
790
791        if  (visual_info.visual != default_visual)
792                printf( "%s: %s class visual at default depth is not default visual\n",
793                        mmv_pname, visual_class[i] );
794
795        if  (setup->colnum > MmcMAX_COLOR-1)  {
796                fprintf( stderr, "%s: too many colors.  Only %d permitted\n",
797                        mmv_pname, MmcMAX_COLOR-1 );
798                exit( 1 );
799        } /*endif*/
800
801        for  (i=0; i<(setup->colnum); i++)  {
802                frac = (float)i / (float)((setup->colnum)-1);
803                        /* red increases from 0 to c and then stays 1 */
804                c = 0.5; /*0.4;*/
805                frac_x = (frac < c) ? frac/c : 1.0;
806                color.red = Nint( frac_x * 65535.0 );
807                /* green increases from c to 1 and is zero before c */
808                c = 0.5;
809                frac_x = (frac > c) ? (frac-c)/(1.0-c) : 0.0;
810                color.green = Nint( frac_x * 65535.0 );
811                /* blue decreases from 1 to 0 between 0 and c and then stays 0 */
812                c = 0.5; /*0.8;*/
813                frac_x = (frac < c) ? (1.0-frac/c) : 0.0;
814                color.blue = Nint( frac_x * 65535.0 );
815#               ifdef XXX
816                color.red = Nint(frac*65535.0);
817                color.green = Nint((1-frac)*65535.0);
818                color.blue = 0;
819#               endif
820                color.flags = DoRed | DoGreen | DoBlue;
821                if  (!XAllocColor(XtDisplay(w),default_cmap,&color))  {
822                        fprintf( stderr, "%s: error allocating color\n", mmv_pname );
823                        exit( 1 );
824                } /*endif*/
825                gc[i] = XCreateGC( XtDisplay(w), XtWindow(w), 0, NULL );
826                XSetForeground( XtDisplay(w), gc[i], color.pixel );
827                XSetBackground( XtDisplay(w), gc[i],
828                        WhitePixel(XtDisplay(w),screen_num) );
829        } /*endfor*/
830
831        out_font = XLoadFont( XtDisplay(w),
832                "-b&h-lucidatypewriter-bold-r-normal-sans-*-100-*-*-*-*-*-1" );
833
834        i = setup->colnum;
835        gc[i] = XCreateGC( XtDisplay(w), XtWindow(w), 0, NULL );
836        XSetForeground( XtDisplay(w), gc[i], BlackPixel(XtDisplay(w),screen_num) );
837        XSetBackground( XtDisplay(w), gc[i], WhitePixel(XtDisplay(w),screen_num) );
838        XSetFont( XtDisplay(w), gc[i], out_font );
839
840} /* end of mm_prepare_gc */
841
842
843
844/*--------------------------------------------------------------------------*/
845
846
847
848static void mm_display_matrix( Widget w, GC gc[], MmtDSP_SETUP *setup,
849        MmtMATRIX *mat, int power )
850
851/* displays matrix on screen
852 *
853 * parameters of routine
854 * Widget     w;           input; widget of drawing area
855 * GC         gc[];        input; GC's
856 * MmtDSP_SETUP *setup;    input; display setup
857 * MmtMATRIX  *mat;        input; matrix
858 * int        power;       input; power on amplitudes
859 */
860{
861        /* local variables */
862        Window   root;        /* root window */
863        int      w_x, w_y;    /* position of window */
864        unsigned w_w, w_h;    /* size of window */
865        unsigned border;      /* border width */
866        unsigned depth;       /* depth of window */
867        float    rect_width;  /* width of rectangle */
868        float    rect_height; /* height of rectangle */
869        unsigned i, j;        /* counters */
870        float    rect_x, rect_y; /* position of rectangle */
871        float    frac;        /* scratch */
872        unsigned colstep;     /* color number */
873        int      cmt_x, cmt_y;/* comment position */
874
875        /* executable code */
876
877        XGetGeometry( XtDisplay(w), XtWindow(w), &root, &w_x, &w_y, &w_w, &w_h,
878                &border, &depth );
879
880        /* get width and height of rectangle */
881        rect_width = (float)(w_w-(setup->margin_l)-(setup->margin_r))
882                / (float)(mat->width);
883#ifdef XXX
884        if  (rect_width <= 1.0)  {
885                fprintf( stderr, "%s: illegal window size\n", mmv_pname );
886                exit( 1 );
887        } /*endif*/
888#endif
889        rect_height = (float)(w_h-(setup->margin_t)-(setup->margin_b))
890                / (float)(mat->height);
891#ifdef XXX
892        if  (rect_height <= 1.0)  {
893                fprintf( stderr, "%s: illegal window size\n", mmv_pname );
894                exit( 1 );
895        } /*endif*/
896#endif
897
898        cmt_x = w_w - setup->margin_r + 10;
899        cmt_y = MmcCHARHEIGHT;
900        for  (i=0; i<mmv_cmtcnt; i++)  {
901                pix_DrawString( XtDisplay(w), XtWindow(w), gc[setup->colnum], cmt_x,
902                        cmt_y, mmv_cmt[i], strlen(mmv_cmt[i]) );
903                cmt_y += MmcCHARHEIGHT;
904        } /*endfor*/
905
906        for  (i=0; i<(mat->height); i++)  {
907                for  (j=0; j<(mat->width); j++)  {
908#                       ifdef XXX  /* this is original */
909                        /* rect_x = (float)(mat->width-1-i) * rect_width; this is mirrored */
910                        rect_x = (float)i * rect_width;
911                        rect_y = (float)j * rect_height;
912#                       endif
913                        rect_x = (float)j * rect_width;
914                        rect_y = (float)(mat->height-1-i) * rect_height;
915                        frac = (mat->val[i][j]-mat->minval) / (mat->maxval-mat->minval);
916                        if  (power == 0)  {
917                                frac = log( 1.0 + (MY_E-1.0)*frac );
918                        } else if  (power > 1)  {
919                                frac = pow( frac, (float)power );
920                        } else if  (power < 0)  {
921                                frac = pow( frac, 1.0/(float)(-power+1) );
922                        } /*endif*/
923                        colstep = Nint( frac * (float)(setup->colnum-1) );
924                        pix_FillRectangle( XtDisplay(w), XtWindow(w), gc[colstep],
925                                Nint(rect_x)+setup->margin_l, Nint(rect_y)+setup->margin_t,
926                                Nint(rect_width)+1, Nint(rect_height)+1 );
927                } /*endfor*/
928        } /*endfor*/
929
930        XFlush( XtDisplay(w) );
931
932} /* end of mm_display_matrix */
933
934
935
936/*--------------------------------------------------------------------------*/
937
938
939
940static void mm_draw_radial_grid( Widget w )
941
942/* Draws radial grid into the map.
943 *
944 * parameters of routine
945 * Widget     w;               input; widget ID
946 */
947{
948        /* local variables */
949        Window   root;        /* root window */
950        int      w_x, w_y;    /* position of window */
951        unsigned w_w, w_h;    /* size of window */
952        unsigned border;      /* border width */
953        unsigned depth;       /* depth of window */
954        unsigned eff_w, eff_h;/* effective width and height */
955        int      center_x;    /* center of coordinate system in pixel */
956        int      center_y;    /* center of coordinate system in pixel */
957        int      nice_num_i;  /* integer nice number */
958        float    nice_num;    /* nice number of grid */
959        float    radius;      /* radius */
960        float    azimuth;     /* azimuth */
961        int      pix_radius_x;/* length of x-radius in pixel */
962        int      pix_radius_y;/* length of y-radius in pixel */
963        float    fx, fy;      /* coordinate position */
964        int      cx, cy;      /* pixel position */
965        char     str[BC_LINELTH+1]; /* scratch string */
966
967        /* executable code */
968
969        XGetGeometry( XtDisplay(w), XtWindow(w), &root, &w_x, &w_y, &w_w, &w_h,
970                &border, &depth );
971
972        /* eff_w and eff_h (both are usually the same) tell how many pixels
973         * are used for two times mmv_scale.
974         */
975        eff_w = w_w - mmv_setup.margin_l - mmv_setup.margin_r;
976        eff_h = w_h - mmv_setup.margin_t - mmv_setup.margin_b;
977        center_x = eff_w/2 + mmv_setup.margin_l;
978        center_y = eff_h/2 + mmv_setup.margin_t;
979
980        nice_num_i = 1;
981        while  (mmv_scale/(float)nice_num_i > 8.0)  {
982                sprintf( str, " %d", nice_num_i );
983                switch  (str[1])  {
984                case '1':   str[1] = '2';  break;
985                case '2':   str[1] = '5';  break;
986                case '5':   str[0] = '1'; str[1] = '0';  break;
987                default:  strcpy( str, "1000000" );  break;
988                } /*endswitch*/
989                sscanf( str, "%d", &nice_num_i );
990        } /*endwhile*/
991        nice_num = (float)nice_num_i;
992
993        for  (radius=nice_num; radius<=mmv_scale; radius += nice_num)  {
994                pix_radius_x = Nint( radius / mmv_scale * (float)(eff_w/2) );
995                pix_radius_y = Nint( radius / mmv_scale * (float)(eff_h/2) );
996                pix_DrawArc( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
997                        center_x-pix_radius_x, center_y-pix_radius_y,
998                        2*pix_radius_x, 2*pix_radius_y, 0, 360*64 );
999        } /*endfor*/
1000
1001        radius -= nice_num;
1002        for  (azimuth=0.0; azimuth<360.0; azimuth += 30.0)  {
1003                fx = radius * cos( azimuth/180.0*MY_PI );
1004                fy = radius * sin( azimuth/180.0*MY_PI );
1005                cx = center_x + Nint( fx / mmv_scale * (float)(eff_w/2) );
1006                cy = center_y + Nint( fy / mmv_scale * (float)(eff_h/2) );
1007                pix_DrawLine( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum],
1008                        center_x, center_y, cx, cy );
1009        } /*endfor*/
1010
1011        sprintf( str, " radial grid step %g", nice_num );
1012        cx = w_w - mmv_setup.margin_r + 10;
1013        cy = (mmv_cmtcnt+1) * MmcCHARHEIGHT;
1014        pix_DrawString( XtDisplay(w), XtWindow(w), mmv_gc[mmv_setup.colnum], cx, cy,
1015                str, strlen(str) );
1016
1017        XFlush( XtDisplay(w) );
1018
1019} /* end of mm_draw_radial_grid */
1020
1021
1022
1023/*--------------------------------------------------------------------------*/
1024
1025
1026
1027static void mm_change_gc( Widget w, int mode, MmtDSP_SETUP *setup, GC gc[] )
1028
1029/* changes gc's to other colors
1030 *
1031 * parameters of routine
1032 * Widget     w;         input; widget ID
1033 * int        mode;      input; which colors
1034 * MmtDSP_SETUP *setup;  input; setup
1035 * GC         gc[];      modify; GC's to change
1036 */
1037{
1038        /* local variables */
1039        int      i;           /* counter */
1040        float    frac;        /* color fraction */
1041        float    frac_x;      /* scratch */
1042        float    c, c1;       /* scratch */
1043        XColor   color;       /* color info */
1044        Colormap default_cmap;/* default colormap */
1045        int      screen_num;  /* screen number */
1046
1047        /* executable code */
1048
1049        screen_num = 0;
1050
1051        default_cmap = DefaultColormap( XtDisplay(w), screen_num );
1052
1053        for  (i=0; i<(setup->colnum); i++)  {
1054                frac = (float)i / (float)((setup->colnum)-1);
1055                switch  (mode)  {
1056                case MmcCOLORS_GREEN_RED:
1057                        color.red = Nint(frac*65535.0);
1058                        color.green = Nint((1-frac)*65535.0);
1059                        color.blue = 0;
1060                        break;
1061                case MmcCOLORS_BLUE_RED:
1062                        color.red = Nint(frac*65535.0);
1063                        color.green = 0;
1064                        color.blue = Nint((1-frac)*65535.0);
1065                        break;
1066                case MmcCOLORS_BLUE_YELLOW:
1067#ifdef XXX
1068                        frac_x = (frac < 0.5) ? 2.0*frac : 1.0;
1069                        color.red = Nint( frac_x * 65535.0 );
1070                        frac_x = (frac > 0.5) ? 2.0*(frac-0.5) : 0.0;
1071                        color.green = Nint( frac_x * 65535.0 );
1072                        frac_x = (frac < 0.5) ? (1.0-2.0*frac) : 0.0;
1073                        color.blue = Nint( frac_x * 65535.0 );
1074#endif
1075                        /* red increases from 0 to c and then stays 1 */
1076                        c = 0.5; /*0.4;*/
1077                        frac_x = (frac < c) ? frac/c : 1.0;
1078                        color.red = Nint( frac_x * 65535.0 );
1079                        /* green increases from c to 1 and is zero before c */
1080                        c = 0.5;
1081                        frac_x = (frac > c) ? (frac-c)/(1.0-c) : 0.0;
1082                        color.green = Nint( frac_x * 65535.0 );
1083                        /* blue decreases from 1 to 0 between 0 and c and then stays 0 */
1084                        c = 0.5; /*0.8;*/
1085                        frac_x = (frac < c) ? (1.0-frac/c) : 0.0;
1086                        color.blue = Nint( frac_x * 65535.0 );
1087                        break;
1088                default:
1089                        color.red = Nint((1-frac)*65535.0);
1090                        color.green = color.blue = color.red;
1091                        break;
1092                } /*endswitch*/
1093                color.flags = DoRed | DoGreen | DoBlue;
1094                if  (!XAllocColor(XtDisplay(w),default_cmap,&color))  {
1095                        fprintf( stderr, "%s: error allocating color\n", mmv_pname );
1096                        exit( 1 );
1097                } /*endif*/
1098                XSetForeground( XtDisplay(w), gc[i], color.pixel );
1099                XSetBackground( XtDisplay(w), gc[i],
1100                        WhitePixel(XtDisplay(w),screen_num) );
1101        } /*endfor*/
1102
1103} /* end of mm_change_gc */
1104
1105
1106
1107/*--------------------------------------------------------------------------*/
Note: See TracBrowser for help on using the repository browser.