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
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <mach.h>
include <gio.h>
include <gki.h>
include "gtr.h"
include "grc.h"
# GRC_SCRTOWCS -- Transform screen coordinates (raw cursor coordinates) to
# world coordinates. This is not terribly efficient, but it does not matter
# for cursor mode applications which do not involve many coordinate
# transformations.
procedure grc_scrtowcs (stream, sx, sy, raster, rx, ry, wx, wy, wcs)
int stream #I graphics stream
real sx, sy #I screen coordinates
int raster #I raster number
real rx, ry #I raster coordinates
real wx, wy #O world coordinates
int wcs #O world coordinate system
pointer w, tr
real mx, my
real ct[LEN_CT]
int grc_selectwcs()
pointer gtr_init()
errchk gtr_init
begin
tr = gtr_init (stream)
# Convert screen (raster 0) to NDC coordinates, undoing the effects
# of the workstation transformation. This is not done for raster
# coordinates since these are already raster-normalized coordinates
# as returned by the server.
if (raster == 0)
call grc_scrtondc (rx, ry, mx, my)
else {
mx = rx
my = ry
}
# Select a WCS. The TR_WCS variable is set only if the user
# explicitly fixes the WCS to override automatic selection. The
# best WCS for the raster is used if there is one, otherwise the
# best screen WCS is used.
if (TR_WCS(tr) == NULL) {
wcs = grc_selectwcs (tr, raster, mx, my)
if (wcs == 0) {
call grc_scrtondc (sx, sy, mx, my)
wcs = grc_selectwcs (tr, 0, mx, my)
}
} else
wcs = TR_WCS(tr)
# Set up the coordinate transformation.
w = TR_WCSPTR(tr,wcs)
call grc_settran (w, ct)
# Transform NDC coordinates to WCS coordinates.
call grc_ndctowcs (ct, mx, my, wx, wy)
end
# GRC_SETTRAN -- Set up the coordinate transformation parameters for a given
# world coordinate system.
procedure grc_settran (w, ct)
pointer w # window descriptor
real ct[LEN_CT] # transformation descriptor
real worigin, scale
real m1, m2, w1, w2
int transformation, ax
bool fp_equalr()
real elogr()
begin
# Compute world -> NDC coordinate transformation.
do ax = 1, 2 {
if (ax == 1) {
transformation = WCS_XTRAN(w)
w1 = WCS_WX1(w)
w2 = WCS_WX2(w)
m1 = WCS_SX1(w)
m2 = WCS_SX2(w)
} else {
transformation = WCS_YTRAN(w)
w1 = WCS_WY1(w)
w2 = WCS_WY2(w)
m1 = WCS_SY1(w)
m2 = WCS_SY2(w)
}
if (transformation == LINEAR) {
worigin = w1
if (fp_equalr (w1, w2))
scale = 1.0
else
scale = (m2 - m1) / (w2 - w1)
} else if (transformation == LOG && w1 > 0 && w2 > 0) {
worigin = log10 (w1)
if (fp_equalr (log10(w2), worigin))
scale = 1.0
else
scale = (m2 - m1) / (log10(w2) - worigin)
} else {
worigin = elogr (w1)
if (fp_equalr (elogr(w2), worigin))
scale = 1.0
else
scale = (m2 - m1) / (elogr(w2) - worigin)
}
ct[ax,CT_TRAN] = transformation
ct[ax,CT_SCALE] = scale
ct[ax,CT_WORIGIN] = worigin
ct[ax,CT_MORIGIN] = m1
}
end
# GRC_WCSTONDC -- Transform world coordinates to NDC coordinates using the
# computed transformation parameters.
procedure grc_wcstondc (ct, wx, wy, mx, my)
real ct[LEN_CT] # coordinate transformation descriptor
real wx, wy # world coordinates of point
real mx, my # ndc coordinates of point
real v
int transformation, ax
real elogr()
begin
do ax = 1, 2 {
transformation = nint (ct[ax,CT_TRAN])
if (ax == 1)
v = wx
else
v = wy
if (transformation == LINEAR)
;
else if (transformation == LOG)
v = log10 (v)
else
v = elogr (v)
v = ((v - ct[ax,CT_WORIGIN]) * ct[ax,CT_SCALE]) + ct[ax,CT_MORIGIN]
if (ax == 1)
mx = v
else
my = v
}
end
# GRC_NDCTOWCS -- Transform NDC coordinates to world coordinates using the
# computed transformation parameters.
procedure grc_ndctowcs (ct, mx, my, wx, wy)
real ct[LEN_CT] # coordinate transformation descriptor
real mx, my # ndc coordinates of point
real wx, wy # world coordinates of point
real v
int transformation, ax
real aelogr()
begin
do ax = 1, 2 {
transformation = nint (ct[ax,CT_TRAN])
if (ax == 1)
v = mx
else
v = my
v = ((v - ct[ax,CT_MORIGIN]) / ct[ax,CT_SCALE]) + ct[ax,CT_WORIGIN]
if (transformation == LINEAR)
;
else if (transformation == LOG)
v = 10.0 ** v
else
v = aelogr (v)
if (ax == 1)
wx = v
else
wy = v
}
end
# GRC_SELECTWCS -- Select the WCS nearest to the given position in NDC
# coordinates. If the point falls within a single WCS then that WCS is
# selected. If the point falls within multiple WCS then the closest WCS
# is selected. If multiple (non unitary) WCS are defined at the same
# distance, e.g., when the WCS share the same viewport, then the highest
# numbered WCS is selected.
int procedure grc_selectwcs (tr, raster, mx, my)
pointer tr #I GTR descriptor
int raster #I raster number
real mx, my #I NDC coordinates of point
pointer w
int wcs, closest_wcs, flags
real tol, sx1, sx2, sy1, sy2
real distance, old_distance, xcen, ycen
int nin, in[MAX_WCS]
begin
nin = 0
closest_wcs = 0
old_distance = 1.0
tol = EPSILON * 10.0
# Inspect each WCS. All WCS are passed even though only one or two
# WCS will be set to nonunitary values for a given plot. Omitting
# the unitary WCS, determine the closest WCS and make a list of the
# WCS containing the given point.
do wcs = 1, MAX_WCS {
w = TR_WCSPTR(tr,wcs)
# Cache WCS params in local storage.
sx1 = WCS_SX1(w)
sx2 = WCS_SX2(w)
sy1 = WCS_SY1(w)
sy2 = WCS_SY2(w)
flags = WCS_FLAGS(w)
xcen = (sx1 + sx2) / 2.0
ycen = (sy1 + sy2) / 2.0
# Skip to next WCS if the raster number doesn't match.
if (WF_RASTER(flags) != raster)
next
# Skip to next WCS if this one is not defined.
if (and (flags, WF_NEWFORMAT) == 0) {
# Preserve old semantics if passed old format WCS.
if (sx1 == 0 && sx2 == 0 || sy1 == 0 && sy2 == 0)
next
if (abs ((sx2-sx1) - 1.0) < tol && abs ((sy2-sy1) - 1.0) < tol)
next
} else if (and (flags, WF_DEFINED) == 0)
next
# Determine closest WCS to point (mx,my).
distance = ((mx - xcen) ** 2) + ((my - ycen) ** 2)
if (distance <= old_distance) {
closest_wcs = wcs
old_distance = distance
}
# Check if point is inside this WCS.
if (mx >= sx1 && mx <= sx2 && my >= sy1 && my <= sy2) {
nin = nin + 1
in[nin] = wcs
}
}
# If point is inside exactly one non-unitary WCS then select that WCS.
if (nin == 1)
return (in[1])
# If point is inside more than one WCS, or if point is not inside any
# WCS, select the closest WCS. If multiple WCS are at the same
# distance we have already selected the higher numbered WCS due to
# the way the distance test is conducted, above.
return (closest_wcs)
end
|