aboutsummaryrefslogtreecommitdiff
path: root/vendor/x11iraf/ximtool/gui.bak/plots.tcl
blob: c4abd426f095964caa12401cff301c59f8b943b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659


################################################################################
# Cut-plot handling routines.
################################################################################

set doHcut	0
set doVcut	0
set plotSpeed	1 ; send plotSpeed  set on True
set curJump	1 ; send curJump  set on True
set curTrack	1 ; send curTrack set on True

set cutXPos	[expr "$winWidth  / 2"]
set cutYPos	[expr "$winHeight / 2"]
set cutXScale 	1.0
set cutYScale 	1.0



# Change the cursor to the crosshair when in the plot
proc cutCursor { widget event args } \
{
    global doHcut doVcut curTrack

    if {! $curTrack} \
	return

    if { $event == "enterNotify" } {
	send $widget setCursorType ginMode

	# Disable the update of the graph we're in while in the plot window.
	if {$widget == "hcutPlot"} { set doHcut 0 } else { set doVcut 0 }
    } elseif { $event == "leaveNotify" } {
        send $widget setCursorType idle

	# Enable the update of the graph we're leaving.
	if {$widget == "hcutPlot"} { set doHcut 1 } else { set doVcut 1 }
    }
    cutPlotRefresh
}
foreach w {hcutPlot vcutPlot} {
    send $w addEventHandler cutCursor enterWindowMask
    send $w addEventHandler cutCursor leaveWindowMask
}

proc cutPlotRefresh args \
{
    global doHcut doVcut cutXPos cutYPos

    if {$doHcut} {
        send hcutPlot clearScreen
        hcutInit
        send hcutAxes1 redraw ; send hcutAxes2 redraw
        cutPlots $cutXPos $cutYPos
    }
    if {$doVcut} {
        send vcutPlot clearScreen
        vcutInit
        send vcutAxes1 redraw ; send vcutAxes2 redraw
        cutPlots $cutXPos $cutYPos
    }
} ; send imagewin addEventHandler cutPlotRefresh enterWindowMask

proc cutPlotRedraw args \
{
    global doHcut doVcut cutXPos cutYPos

    if {$doHcut} {
        send hcutAxes1 redraw ; send hcutAxes2 redraw
        cutPlots $cutXPos $cutYPos
    }
    if {$doVcut} {
        send vcutAxes1 redraw ; send vcutAxes2 redraw
        cutPlots $cutXPos $cutYPos
    }
}


# Disable the options when we first start up.
#send plotOpts "set height 1 ; set width 1 ; unmap"
foreach w {plotOpts hcutFrame vcutFrame} { send $w unmap }


# Cut-Plot options callback.
proc doPlotOpts { widget type state args } \
{
    global plotSpeed curJump curTrack doHcut doVcut
    global cutXPos cutYPos

    if {$state} { set not 0 } else { set not 1 }

    switch $widget {
    plotSpeed 	 { if {$state} {
    		       send plotAccurate set on 0 ; send plotImgPix set on 0
		   } else {
    		       send plotSpeed set on True ; 
		   }
		   set plotSpeed $not
		 }
    plotAccurate { if {$state} {
    		       send plotImgPix set on 0 ; send plotSpeed set on 0
		   } else {
    		       send plotSpeed set on True ; 
		   }
		   set plotSpeed $not
		 }
    plotImgPix   { if {$state} {
    		       send plotAccurate set on 0 ; send plotSpeed set on 0
		   } else {
    		       send plotSpeed set on True ; 
		   }
		   set plotSpeed $not
		 }
    curJump	 { send curSmooth set on $not    ; set curJump   $state }
    curSmooth	 { send curJump set on $not      ; set curJump   $not 	}
    curTrack	 { set curTrack $state 					}
    }

    # Redraw the plots right away.
    if {$widget == "plotSpeed" || $widget == "plotAccurate"} { 
	cutPlots $cutXPos $cutYPos
    }
}
foreach w { plotSpeed plotAccurate plotImgPix curJump curSmooth curTrack } {
    send $w addCallback doPlotOpts 
}


# Toggle the display of the horizontal or vertical cut plot windows.

proc cutPlotToggle { widget type state args } \
{
    global doHcut doVcut cutXPos cutYPos
    set    debug  0

    set hstate [send hcut get state]
    set vstate [send vcut get state]
    set w [send display get width]
    set h [send display get height]

    if {$debug} { print " " ; print [format "display: %d x %d\n" $w $h] }

    if {$widget == "hcut"} {
	set hfw [expr [send hcutFrame get width] - 4]
	set hpw [send hcutPlot  get width]
        if {$state} {
	    # Enable the plot and resize the main window
	    if {$vstate} { 
		send plotOpts set width 134 
	    }
	    send hcutFrame "set width $hpw ; set height 132; map"
	    send hcutPlot "set width $hfw ; set height 128"
	    send display "set height [ expr ($h + 132) ]; set width $w"
            drawHcutAxes  1
	    setHcutCursor 1
	    if {$vstate} { 
		send plotOpts "set height 134 ; map"
	        vcutInit
	    }
	    hcutInit 				;# Initialize the plot.
        } else {
	    # Disable the plot and resize the main window
	    setHcutCursor 0
            drawHcutAxes  0
	    send hcutPlot clearScreen
	    send plotOpts "unmap; set height 4"
	    send hcutFrame "unmap; set width $hfw; set height 4"
	    send plotOpts "set width 4"
	    send display "set height [ expr ($h - 128) ] ; set width $w"
	    if {$vstate} { 
	        vcutInit
	    }
        }
        set doHcut $state
    } else {
	set vfh  [expr [send vcutFrame get height] - 4]
	set vph  [send vcutPlot  get height]
        if {$state} {
	    # Enable the plot and resize the main window
	    if {$hstate} { 
		send plotOpts set height 134
	    }
	    send vcutFrame "set height $vph ; set width 132 ; map"
	    send vcutPlot "set height $vfh ; set width 128"
	    send display "set height $h; set width [ expr ($w + 132) ]"
            drawVcutAxes  1
	    setVcutCursor 1
	    if {$hstate} { 
		send plotOpts "set height 134 ; set width 134; map"
     	        hcutInit
	    }
     	    vcutInit 				;# Initialize the plot.
        } else {
	    # Disable the plot and resize the main window
	    setVcutCursor 0
            drawVcutAxes  0
	    send vcutPlot clearScreen
	    send plotOpts "unmap; set width 4"
	    send vcutFrame "unmap; set height $vfh; set width 4"
	    send plotOpts "set height 4"
	    send display "set width [ expr ($w - 128) ] ; set height $h"
	    if {$hstate} { 
	        hcutInit
	    }
        }
        set doVcut $state
    }
		
    if {$debug} {
	print [format " hFrame: %d x %d\n" \
		[send hcutFrame get width] [send hcutFrame get height] ]
	print [format "  hPlot: %d x %d\n" \
		[send hcutPlot get width] [send hcutPlot get height] ]
	print [format " vFrame: %d x %d\n" \
		[send vcutFrame get width] [send vcutFrame get height] ]
	print [format "  vPlot: %d x %d\n" \
		[send vcutPlot get width] [send vcutPlot get height] ]
	print [format "state: %d  %d\n" $hstate $vstate]
	print [format "display: %d x %d\n" $w $h]
    }

    cutPlots $cutXPos $cutYPos
} ; foreach w { hcut vcut } { send $w addCallback cutPlotToggle }


# Draw the cut plots.
proc cutPlots { xpos ypos args } \
{
    global doHcut doVcut cutXPos cutYPos

    catch {
        if {$doHcut} { plotHcut $xpos $ypos }
        if {$doVcut} { plotVcut $xpos $ypos }
    }

    set cutXPos $xpos  ;  set cutYPos $ypos
}


################################################################################
# Horizontal Cut-Plot Routines
################################################################################

set hcutVec	{}

# Initiailize the horizontal cut-plot
proc hcutInit args \
{
    global logz cutXScale winWidth cutXPos cutYPos

    # Just get some dummy pixels, we only want the z1/z2 values so we can 
    # initialize the labels.
    set xp   [expr [send imagewin get width] / 2 ]
    set yp   [expr [send imagewin get height] / 2 ]
    set pix  [send client getPixels $xp $yp 2 2 ]
    set z1   [lindex $pix 0]
    set z2   [lindex $pix 1]

    send hcutPlot getPhysRes  xr  yr
    send hcutPlot setLogRes  $xr $yr

    set logx    [send imagewin get width]
    set logz    [expr ($z2 - $z1)]
    set cutXScale [expr ($xr * 1.0) / ($logx * 1.0)]

    # Initialize the labels.
    send vcutPlot "setColorIndex 6"
    drawHcutLabels $z1 $z2
}

# Draw the horizontal cut-plot.
proc plotHcut { xpos ypos } \
{
    global doHcut cutXScale
    global hcutVec cutXPos plotSpeed


    if { ($xpos == 0 && $ypos == 0) || ! $doHcut } \
	return

    # Do the horizontal cut plot.
    set width [send imagewin get width]
    if {$plotSpeed} {
        set pix  [send client getPixels 0 $ypos $width 1 2 5 $cutXScale]
    } else {
        set pix  [send client getPixels 0 $ypos $width 1 2 1 $cutXScale]
    }
    set z1   [lindex $pix 0]
    set z2   [lindex $pix 1]
    set vec  [lrange $pix 2 end]

    # Erase the last plot rather than clear the screen and redraw 
    # the new vector.
    send hcutPlot setColorIndex background
    send hcutPlot drawPolyline $hcutVec
    send hcutPlot setColorIndex foreground
    send hcutPlot drawPolyline $vec
    set  hcutVec $vec 			;# save for later erasure

    # Mark the cursor position.
    drawHcutIndicator $xpos

    # Minimize the screen refreshes to speed things up.
    if { [expr "$ypos % 3"] == 0} {
	catch {
            drawHcutLabels $z1 $z2	;# redraw the labels
	}
        send hcutAxes1 redraw		;# redraw the axes markers
        send hcutAxes2 redraw
    }
}


# Create markers to indicate axes on the horizontal cut-plot.
proc drawHcutAxes { state } \
{
    if {$state} {
        send hcutPlot createMarker hcutAxes1 \
            type            box \
            createMode      noninteractive \
            lineColor       gray60 \
            lineStyle       0 \
            x               1 \
            y               60 \
            height          30 \
	    width           4096 \
            activated       True \
            visible         True \
            sensitive       False
        send hcutPlot createMarker hcutAxes2 \
            type            box \
            createMode      noninteractive \
            lineColor       gray60 \
            lineStyle       0 \
            x               1 \
            y               1 \
            height          60 \
	    width           4096 \
            activated       True \
            visible         True \
            sensitive       False
    } else {
	send hcutAxes1 destroy ; send hcutAxes2 destroy
    }
}

# Create a marker to be used as the cursor indicator.
proc setHcutCursor { state } \
{
    if {$state} {
	set pts { {252 10} {260 10} {256 1} }

        send hcutPlot createMarker hcutCursor \
            type          polygon \
            createMode    noninteractive \
            lineColor     black \
            fill     	  True \
            fillColor     yellow \
            x             256 \
            y             12 \
            width         8 \
            height        10 \
            knotSize      0 \
            activated     True \
            visible       False \
            sensitive     False
        send hcutCursor setVertices $pts
        send hcutCursor set visible True

    } else {
	send hcutCursor destroy
    }
}

# Label the axes on the horizontal cut plot.
proc drawHcutLabels { z1 z2 } \
{
    set mid  [expr "($z2-$z1)/2.0+$z1"]
    set low [expr "($mid-$z1)/2.0+$z1"]
    set high  [expr "($z2-$mid)/2.0+$mid"]

    send hcutPlot "setColorIndex 6 ; \
	 drawAlphaText 2 10  [format "%.1f" $z2] ; \
         drawAlphaText 2 34  [format "%.1f" $high] ; \
         drawAlphaText 2 64  [format "%.1f" $mid] ; \
         drawAlphaText 2 94  [format "%.1f" $low] ; \
         drawAlphaText 2 120 [format "%.1f" $z1]"
}

# Draw the cursor position indicator on the horizontal cut plot.
proc drawHcutIndicator { xpos } \
{
    global cutXScale cutXPos

    send hcutCursor move [expr ($xpos * $cutXScale)] 12
    set cutXPos $xpos
}

# Track the cursor while in the cut-graph window.
proc hcutWCSUpdate { x y args } \
{
    global cutYPos curTrack
    if {$curTrack} { 
        wcsUpdate $x $cutYPos 
    }
    drawHcutIndicator $x
}




################################################################################
# Vertical Cut-Plot Routines
################################################################################

set vcutVec	{}

# Initiailize the vertical cut-plot
proc vcutInit args \
{
    global cutYScale winWidth cutXPos cutYPos

    # Just get some dummy pixels, we only want the z1/z2 values so we can 
    # initialize the labels.
    set xp   [expr [send imagewin get width] / 2 ]
    set yp   [expr [send imagewin get height] / 2 ]
    set pix  [send client getPixels $xp $yp 2 2 ]
    set z1   [lindex $pix 0]
    set z2   [lindex $pix 1]

    send vcutPlot getPhysRes  xr  yr
    send vcutPlot setLogRes  $xr $yr

    set logy    [send imagewin get height]
    set logz    [expr ($z2 - $z1)]
    set cutYScale [expr ($yr * 1.0) / ($logy * 1.0)]

    # Initialize the labels.
    send vcutPlot "setColorIndex 6; reset"
    drawVcutLabels $z1 $z2
}

# Draw the horizontal cut-plot.
proc plotVcut { xpos ypos } \
{
    global doVcut cutYScale
    global vcutVec cutXPos plotSpeed


    if { ($xpos == 0 && $ypos == 0) || ! $doVcut } \
	return

    # Do the vertical cut plot.
    set height [send imagewin get height]
    if {$plotSpeed} {
        set pix  [send client getPixels $xpos 0 1 $height 3 5 $cutYScale]
    } else {
        set pix  [send client getPixels $xpos 0 1 $height 3 1 $cutYScale]
    }
    set z1   [lindex $pix 0]
    set z2   [lindex $pix 1]
    set vec  [lrange $pix 2 end]

    # Draw the vector.
    send vcutPlot setColorIndex background
    send vcutPlot drawPolyline $vcutVec
    send vcutPlot setColorIndex foreground
    send vcutPlot drawPolyline $vec
    set  vcutVec $vec 			;# save for later erasure

    # Mark the cursor position.
    drawVcutIndicator $ypos

    # Minimize the screen refreshes to speed things up.
    if { [expr "$xpos % 3"] == 0} {
	catch {
            drawVcutLabels $z1 $z2	;# redraw the labels
	}
        send vcutAxes1 redraw		;# redraw the axes markers
        send vcutAxes2 redraw
    }
}

# Erase the last plot rather than clear the screen and redraw it all. The
# erase is done by redrawing the last vector in the the background color.
proc eraseOldVcut args \
{
    global cutYPos vcutVec

    send vcutPlot setColorIndex background
    send vcutPlot drawPolyline $vcutVec
    send vcutPlot setColorIndex foreground
}

# Draw the horizontal cut-plot.
# Create markers to indicate axes on the vertical cut-plot.
proc drawVcutAxes { state } \
{
    if {$state} {
        send vcutPlot createMarker vcutAxes1 \
            type            box \
            createMode      noninteractive \
            lineColor       gray60 \
            lineStyle       0 \
            x               60 \
            y               1 \
            height          4096 \
            width           30 \
            activated       True \
            visible         True \
            sensitive       False
        send vcutPlot createMarker vcutAxes2 \
            type            box \
            createMode      noninteractive \
            lineColor       gray60 \
            lineStyle       0 \
            x               1 \
            y               1 \
            width           60 \
            height          4096 \
            activated       True \
            visible         True \
            sensitive       False
    } else {
	send vcutAxes1 destroy ; send vcutAxes2 destroy
    }
}

# Create a marker to be used as the cursor indicator.
proc setVcutCursor { state } \
{
    if {$state} {
	set pts { {10 252} {10 260} {1 256} }

        send vcutPlot createMarker vcutCursor \
            type          polygon \
            createMode    noninteractive \
            lineColor     black \
            fill     	  True \
            fillColor     yellow \
            x             12 \
            y             256 \
            width         10 \
            height        8 \
            knotSize      0 \
            activated     True \
            visible       False \
            sensitive     False
        send vcutCursor setVertices $pts
        send vcutCursor set visible True

    } else {
	send vcutCursor destroy
    }
}

# Label the axes on the vertical cut plot.
proc drawVcutLabels { z1 z2 } \
{
    set mid  [expr "($z2-$z1)/2.0+$z1"]
    set low  [expr "($mid-$z1)/2.0+$z1"]
    set high [expr "($z2-$mid)/2.0+$mid"]

    # Initialize the label strings and positions.
    set labels {}
    foreach i [list $z2 $high $mid $low $z1] {
	lappend labels  [ format "%.1f" $i ]
    }
    set xposns { 2 28 58 88 112 }

    send vcutPlot "setColorIndex 6"

    # Draw each label vertically down the position since we can't rotate
    # the text.
    set xp  0
    foreach lab $labels {
	set chars [split $lab {} ]
        set yp  12
	set xpos [lindex $xposns $xp]
        foreach ch $chars {
	    if {$ch == "."} { incr yp -4 }
    	    send vcutPlot drawAlphaText $xpos $yp $ch
	    incr yp 10
        }
	incr xp
    }
}


# Draw the cursor position indicator on the horizontal cut plot.
proc drawVcutIndicator { ypos } \
{
    global cutYScale cutYPos

    send vcutCursor move 12 [expr ($ypos * $cutYScale)]
    set cutYPos $ypos
}


# Track the cursor while in the cut-graph window.
proc vcutWCSUpdate { x y args } \
{
    global cutXPos curTrack
    if {$curTrack} { 
        wcsUpdate $cutXPos $y 
    }
    drawVcutIndicator $y
}



################################################################################
# UTILITY ROUTINES
################################################################################


# TICSTEP -- Utility routine to compute nice ticmark steps in plots.
# [ NOT CURRENTLY USED. ]

proc ticstep { range nsteps } \
{
    set t2 0.301029996
    set t5 0.698970004
    set df [ expr "$range / double($nsteps + 1)" ]
    if {$df > 0.0} {
        set p1 [ expr "log10(double($df))" ]
    } else {
        set p1 [ expr "log10(double(-$df))" ]
    }
    set p2 [ expr "int($p1)" ]
    set p3 [ expr "$p1 - $p2" ]

    if { $p3 < 0.0 } {
        set p3 [ expr "$p2 + 1.0" ]
        set p2 [ expr "$p2 - 1.0" ]
    }

    if { $p3 < 1.0e-10 } {
       set ticstep [ expr "pow(double(10.0),double($p2))" ]
    } elseif { $p3 > 0. &&  $p3 <=  $t2 } {
       set ticstep [ expr "pow(double(10.0),double($p2 + $t2))" ]
    } elseif { $p3 >  $t2 &&  $p3 <=  $t5 } {
       set ticstep [ expr "pow(double(10.0),double($p2 + $t5))" ]
    } elseif { $p3 >  $t5 &&  $p3 <= 1.0 } {
       set ticstep [ expr "pow(double(10.0),double($p2 + 1.))" ]
    } else {
       set ticstep $df
    }

    set logtic  [ expr "int(log10($ticstep)) - 1" ]
    set scale   [ expr "pow(double(10.0),double($logtic))" ]
    set ticstep [ expr "int( ($ticstep / $scale) * $scale)" ]

    if {$ticstep < 0.1} { set ticstep 0.10 }

    return $ticstep
}