2 # @file bshow_select.tcl
4 # @brief Procedures to draw selections in Bshow.
6 # @author Bernard Heymann
7 # @date Created: 20060811
8 # @date Modified: 20130725
10 set seltype "rectangle"
13 ## @brief Handles selections from the image data.
20 set c [getImageCanvas $theimg]
22 if { [info exists selpoly] } { unset selpoly }
23 if ![winfo exists .wsel] {
26 # menu .wsel.menuBar -tearoff 0
27 # .wsel.menuBar add cascade -menu .wsel.menuBar.his -label "Selection" -underline 0
28 # menu .wsel.menuBar.his -tearoff 0
29 # .wsel configure -menu .wsel.menuBar
30 # .wsel.menuBar.his add command -label "Help" \
31 # -command { showHelp bshow_select.hlp } -underline 0
32 # .wsel.menuBar.his add command -label "Clear" \
34 # if { [info exists selpoly] } { unset selpoly }
37 # .wsel.menuBar.his add command -label "Close" -underline 0 \
39 # if { [info exists selpoly] } { unset selpoly }
43 frame .wsel.selectiontype
44 label .wsel.selectiontype.tag -text "Type" -width 20 -anchor w
45 tk_optionMenu .wsel.selectiontype.menu selectiontype "Square" "Rectangle" \
46 "Cube" "Circle" "Ellipse" "Sphere"
49 label .wsel.size.tag -font $helv12 -text "Size" -width 5 -anchor w
50 label .wsel.size.x -width 10 -text "" -relief sunken -bd 1 \
51 -font $helv12 -anchor w
52 label .wsel.size.y -width 10 -text "" -relief sunken -bd 1 \
53 -font $helv12 -anchor w
54 label .wsel.size.z -width 10 -text "" -relief sunken -bd 1 \
55 -font $helv12 -anchor w
58 label .wsel.num.tag -font $helv12 -text "Number" -width 25 -anchor w
59 label .wsel.num.value -width 10 -text "" -relief sunken -bd 1 \
60 -font $helv12 -anchor w
63 label .wsel.sum.tag -font $helv12 -text "Sum" -width 25 -anchor w
64 label .wsel.sum.value -width 10 -text "" -relief sunken -bd 1 \
65 -font $helv12 -anchor w
68 label .wsel.avg.tag -font $helv12 -text "Average" -width 25 -anchor w
69 label .wsel.avg.value -width 10 -text "" -relief sunken -bd 1 \
70 -font $helv12 -anchor w
73 label .wsel.std.tag -font $helv12 -text "Standard deviation" -width 25 -anchor w
74 label .wsel.std.value -width 10 -text "" -relief sunken -bd 1 \
75 -font $helv12 -anchor w
77 checkbutton .wsel.calc -text "Calculate" -variable calc_stats \
78 -command { updateSelectionStats }
81 button .wsel.buttons.clear -text Clear \
83 if { [info exists selpoly] } { unset selpoly }
86 button .wsel.buttons.close -text Close \
88 if { [info exists selpoly] } { unset selpoly }
92 pack .wsel.selectiontype.tag .wsel.selectiontype.menu -side left -pady 5 -padx 10
93 pack .wsel.size.tag .wsel.size.x .wsel.size.y .wsel.size.z -side left -pady 5 -padx 5
94 pack .wsel.num.tag .wsel.num.value -side left -pady 5 -padx 20
95 pack .wsel.sum.tag .wsel.sum.value -side left -pady 5 -padx 20
96 pack .wsel.avg.tag .wsel.avg.value -side left -pady 5 -padx 20
97 pack .wsel.std.tag .wsel.std.value -side left -pady 5 -padx 20
98 pack .wsel.buttons.clear .wsel.buttons.close -side left -expand 1
99 pack .wsel.selectiontype .wsel.size .wsel.num .wsel.sum \
100 .wsel.avg .wsel.std .wsel.calc .wsel.buttons -side top -pady 2
106 wm title .wsel "Selection"
107 wm iconname .wsel "Selection"
108 .menuBar.window entryconfigure "Selection" -state normal
109 bind $c <1> "mousePressed %W %x %y"
110 bind $c <B1-Motion> "mouseMoved %W %x %y 1"
111 bind $c <B1-ButtonRelease> "mouseReleased %W"
112 bind .wsel <Control-w> {
113 if { [info exists selpoly] } { unset selpoly }
118 ## @brief Updates selection object.
121 proc updateSelection { } {
126 global sx1 sx2 sy1 sy2 sz1 sz2
128 set c [getImageCanvas $theimg]
131 if { [winfo exists .wsel] && $tool == "select" && $sx1 != $sx2 && $sy1 != $sy2 } {
132 set wc [getControlWindow $theimg]
133 set slice_num [$wc.slice.scale get]
134 if { $sz1 <= $slice_num && $sz2 >= $slice_num } {
135 set scale [$wc.scale.scale get]
136 set h [lindex [$c cget -scrollregion] 3]
137 if { $selectiontype == "Square" || $selectiontype == "Rectangle" || $selectiontype == "Cube" } {
138 set cx1 [expr int($sx1 * $scale)]
139 set cy1 [expr int($h - $sy1 * $scale) ]
140 set cx2 [expr int($sx2 * $scale)]
141 set cy2 [expr int($h - $sy2 * $scale) ]
142 $c create rectangle $cx1 $cy1 $cx2 $cy2 -tags selection -outline red
144 if { $selectiontype == "Circle" || $selectiontype == "Ellipse" || $selectiontype == "Sphere" } {
146 set z [expr ($sz1 + $sz2)*0.5 - $slice_num]
148 set r [expr ($sz1 - $sz2)*0.5]
151 set t [expr sqrt(1.0 - $t*$t)]
152 set tn [expr 0.5*(1.0 - $t)]
153 set tp [expr 0.5*(1.0 + $t)]
154 set cx1 [expr $sx1 * $scale]
155 set cy1 [expr $h - $sy1 * $scale ]
156 set cx2 [expr $sx2 * $scale]
157 set cy2 [expr $h - $sy2 * $scale ]
158 set ccx1 [expr int($tp*$cx1 + $tn*$cx2)]
159 set ccx2 [expr int($tn*$cx1 + $tp*$cx2)]
160 set ccy1 [expr int($tp*$cy1 + $tn*$cy2)]
161 set ccy2 [expr int($tn*$cy1 + $tp*$cy2)]
162 $c create oval $ccx1 $ccy1 $ccx2 $ccy2 -tags selection -outline red
168 ## @brief Updates selection object statistics.
171 proc updateSelectionStats { } {
173 global selectiontype calc_stats
174 global sx1 sx2 sy1 sy2 sz1 sz2
175 if { ! $calc_stats } { return }
176 # set c [getImageCanvas $theimg]
177 set wc [getControlWindow $theimg]
178 set img_num [$wc.image.scale get]
179 set slice_num [$wc.slice.scale get]
180 set vlist "$sx1 $sy1 $sz1 $sx2 $sy2 $sz2"
181 if { $selectiontype == "Square" || $selectiontype == "Rectangle" || $selectiontype == "Cube" } {
182 set stats [Bimage stats $theimg $img_num 1 $vlist]
184 if { $selectiontype == "Circle" || $selectiontype == "Ellipse" || $selectiontype == "Sphere" } {
185 set stats [Bimage stats $theimg $img_num 2 $vlist]
187 if { $selectiontype == "Polygon" } {
189 append poly "$sx1 $sy1 $slice_num "
190 append poly "$sx1 $sy2 $slice_num "
191 append poly "$sx2 $sy2 $slice_num "
192 append poly "$sx2 $sy1 $slice_num "
194 set stats [Bimage stats $theimg $img_num 3 $poly]
196 .wsel.num.value config -text [lindex $stats 0]
197 .wsel.sum.value config -text [lindex $stats 1]
198 .wsel.avg.value config -text [lindex $stats 2]
199 .wsel.std.value config -text [lindex $stats 3]
200 # puts "Expected size [expr (abs($sx1 - $sx2) + 1)*(abs($sy1 - $sy2) + 1)]"
205 set c [getImageCanvas $theimg]
206 bind $c <1> "mousePressed %W %x %y"