-
Notifications
You must be signed in to change notification settings - Fork 38
/
cgaspect.pro
195 lines (176 loc) · 8.07 KB
/
cgaspect.pro
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
; docformat = 'rst'
;
; NAME:
; cgAspect
;
; PURPOSE:
; The purpose of this function is to calculate a position in a graphics window with
; a specified aspect ratio (ysize/xsize).
;
;******************************************************************************************;
; ;
; Copyright (c) 2013, by Fanning Software Consulting, Inc. All rights reserved. ;
; ;
; Redistribution and use in source and binary forms, with or without ;
; modification, are permitted provided that the following conditions are met: ;
; ;
; * Redistributions of source code must retain the above copyright ;
; notice, this list of conditions and the following disclaimer. ;
; * Redistributions in binary form must reproduce the above copyright ;
; notice, this list of conditions and the following disclaimer in the ;
; documentation and/or other materials provided with the distribution. ;
; * Neither the name of Fanning Software Consulting, Inc. nor the names of its ;
; contributors may be used to endorse or promote products derived from this ;
; software without specific prior written permission. ;
; ;
; THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY ;
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ;
; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT ;
; SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, ;
; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;
; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ;
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ;
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;
;******************************************************************************************;
;
;+
; The purpose of this function is to calculate a position in a graphics window with
; a specified aspect ratio (ysize/xsize).
;
; :Categories:
; Utility
;
; :Params:
; aspect_ratio: in, required, type=float
; The requested aspect ratio. Be careful to specify this as a float.
;
; :Keywords:
; align: in, optional, type=string, default='center'
; Normally, the output position is centered in the intial input position. This
; keyword can alter the placement of the final position with respect to the initial
; position. Allowed values are "top", "right", "bottom", "left", and "center".
; aspect: in, optional, type=array
; This keyword provides an alternative way of specifying the `aspect_rato` value. Pass
; this keyword any 2D array, and the aspect ratio will be calculated to match the 2D array.
; position: in, optional, type=array
; Use this keyword to specify a four-element normalized starting position for calculating
; the final position in the graphics window. By default, the staring position is given
; by [0.0, 0.0, 1.0, 1.0] (the entire window). If the `Single_Plot` keyword is set, the
; default position is given by [0.125, 0.125, 0.9, 0.9].
; single_plot: in, optional, type=boolean, default=0
; Set this keyword to change the default starting `Position` to [0.125, 0.125, 0.9, 0.9].
; waspect: in, optional, type=float
; Set this keyword to the window aspect ratio. If not used, the window aspect is calculated from
; the current graphics window as Float(!D.Y_VSize) / !D.X_VSize.
;
; :Examples:
; Here is how to use this program to display two differently sized images aligned at their tops::
; img1 = cgDemoData(7)
; img2 = congrid(img1, 360, 180)
; cgDisplay, 900, 450
;
; pos = cgLayout([2,1], OYMargin=[4, 11], OXMargin=[5, 8], XGap=6)
; pos1 = pos[*,0]
; img1pos = cgAspect(Position=pos1, Aspect=img1, Align='top')
; cgImage, img1, CTIndex=3, /AXES, POSITION=img1pos, OPOSITION=op
; cgColorbar, CTIndex=3, /Fit
;
; pos2 = pos[*,1]
; img2pos = cgAspect(Position=pos2, Aspect=img2, Align='top')
; cgImage, img2, /AXES, POSITION=img2pos, CTIndex=2, /NoErase
; cgColorbar, CTIndex=2, /Fit
;
; :Author:
; FANNING SOFTWARE CONSULTING::
; David W. Fanning
; 1645 Sheely Drive
; Fort Collins, CO 80526 USA
; Phone: 970-221-0438
; E-mail: [email protected]
; Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; :History:
; Change History::
; Written, 12 February 2013 by David W. Fanning.
;
; :Copyright:
; Copyright (c) 2013, Fanning Software Consulting, Inc.
;-
FUNCTION cgAspect, aspect_ratio, $
ALIGN=align, $
ASPECT=aspect, $
POSITION=position, $
SINGLE_PLOT=single_plot, $
WASPECT=waspect
; Return to caller in the event of an error.
On_Error, 2
; Is there a 2D array to use to set the requested aspect_ratio?
IF N_Elements(aspect) NE 0 THEN BEGIN
dims = Size(aspect, /Dimensions)
aspect_ratio = Double(dims[1]) / dims[0]
ENDIF
; Do you have an aspect ratio?
IF N_Elements(aspect_ratio) EQ 0 THEN BEGIN
Message, 'Calling Syntax: position = cgAspect(aspect_ratio)', /Informational
RETURN, Replicate(0.0,4)
ENDIF ELSE aspect_ratio = Double(aspect_ratio)
; Do we need a position in the window?
IF N_Elements(position) EQ 0 THEN BEGIN
IF Keyword_Set(single_plot) THEN BEGIN
position = [0.125d, 0.125d, 0.9d, 0.9d]
ENDIF ELSE BEGIN
position = [0.0d, 0.0d, 1.0d, 1.0d]
ENDELSE
ENDIF
; Do we need a window aspect ratio?
IF N_Elements(waspect) EQ 0 THEN BEGIN
waspect = Double(!D.Y_VSize) / !D.X_VSize
ENDIF
; Do you have an alignment?
IF N_Elements(align) EQ 0 THEN align = 'center'
align = StrUpCase(align)
; Find the proposed size in pixels without aspect considerations.
xpixSize = (position[2] - position[0]) * !D.X_VSize
ypixSize = (position[3] - position[1]) * !D.Y_VSize
; Try to fit the width. If you can't maintain the aspect ratio, fit the height.
trialX = xpixSize
trialY = trialX * aspect_ratio
IF trialY GT ypixSize THEN BEGIN
trialY = ypixSize
trialX = trialY / aspect_ratio
ENDIF
; Recalculate the position in the window.
pos = FltArr(4)
pos[0] = (((xpixSize - trialX) / 2.0) / !D.X_VSize) + position[0]
pos[2] = pos[0] + (trialX/FLOAT(!D.X_VSize))
pos[1] = (((ypixSize - trialY) / 2.0) / !D.Y_VSize) + position[1]
pos[3] = pos[1] + (trialY/FLOAT(!D.Y_VSize))
CASE align OF
'RIGHT': BEGIN
xdiff = position[2] - pos[2]
pos[0] = pos[0] + xdiff
pos[2] = position[2]
END
'TOP': BEGIN
ydiff = position[3] - pos[3]
pos[3] = position[3]
pos[1] = pos[1] + ydiff
END
'LEFT': BEGIN
xdiff = pos[0] - position[0]
pos[0] = position[0]
pos[2] = position[2] - xdiff
END
'BOTTOM': BEGIN
ydiff = pos[1] - position[1]
pos[1] = position[1]
pos[3] = pos[3] - ydiff
END
'CENTER':
ELSE: Message, 'Unknown alignment parameter: ' + align
ENDCASE
; Return the position
RETURN, pos
END