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
//
// GENERATED FILE
//
use super::*;
use f2rust_std::*;
struct SaveVars {
BSIZE: i32,
J: i32,
K: i32,
MAXPXX: i32,
MAXPXY: i32,
MINPXX: i32,
MINPXY: i32,
NGRID: i32,
RNGMAX: i32,
}
impl SaveInit for SaveVars {
fn new() -> Self {
let mut BSIZE: i32 = 0;
let mut J: i32 = 0;
let mut K: i32 = 0;
let mut MAXPXX: i32 = 0;
let mut MAXPXY: i32 = 0;
let mut MINPXX: i32 = 0;
let mut MINPXY: i32 = 0;
let mut NGRID: i32 = 0;
let mut RNGMAX: i32 = 0;
Self {
BSIZE,
J,
K,
MAXPXX,
MAXPXY,
MINPXX,
MINPXY,
NGRID,
RNGMAX,
}
}
}
//$Procedure RC2GRD ( DSKBRIEF, rectangles to pixel grid )
pub fn RC2GRD(
NREC: i32,
BNDS1: &[f64],
BNDS2: &[f64],
MAXGRD: i32,
MAXORD: i32,
VALUE: bool,
ORD1: &mut [i32],
ORD2: &mut [i32],
CIVOR1: &mut [i32],
CIVOR2: &mut [i32],
PXMAP1: &mut [i32],
PXMAP2: &mut [i32],
NROWS: &mut i32,
NCOLS: &mut i32,
GRID: &mut [bool],
ctx: &mut Context,
) -> f2rust_std::Result<()> {
let save = ctx.get_vars::<SaveVars>();
let save = &mut *save.borrow_mut();
let BNDS1 = DummyArray2D::new(BNDS1, 1..=2, 1..=NREC);
let BNDS2 = DummyArray2D::new(BNDS2, 1..=2, 1..=NREC);
let mut ORD1 = DummyArrayMut::new(ORD1, 1..);
let mut ORD2 = DummyArrayMut::new(ORD2, 1..);
let mut CIVOR1 = DummyArrayMut::new(CIVOR1, 1..);
let mut CIVOR2 = DummyArrayMut::new(CIVOR2, 1..);
let mut PXMAP1 = DummyArrayMut::new(PXMAP1, 1..);
let mut PXMAP2 = DummyArrayMut::new(PXMAP2, 1..);
let mut GRID = DummyArrayMut::new(GRID, 1..);
//
// SPICELIB functions
//
//
// Local variables
//
//
// Saved values
//
if spicelib::RETURN(ctx) {
return Ok(());
}
spicelib::CHKIN(b"RC2GRD", ctx)?;
//
// Check input size arguments for obvious initialization errors.
//
if (NREC < 1) {
spicelib::SETMSG(b"NREC is #; must be positive.", ctx);
spicelib::ERRINT(b"#", NREC, ctx);
spicelib::SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
if (MAXGRD < 1) {
spicelib::SETMSG(b"MAXGRD is #; must be positive.", ctx);
spicelib::ERRINT(b"#", MAXGRD, ctx);
spicelib::SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
if (MAXORD < 1) {
spicelib::SETMSG(b"MAXORD is #; must be positive.", ctx);
spicelib::ERRINT(b"#", MAXORD, ctx);
spicelib::SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
//
// All input rectangle heights and widths must be strictly
// positive.
//
for I in 1..=NREC {
if (BNDS1[[2, I]] <= BNDS1[[1, I]]) {
spicelib::SETMSG(
b"BNDS1(2,#) = #; BNDS1(1,#) = #. Rectangle widths (and heights) must be positive.",
ctx,
);
spicelib::ERRINT(b"#", I, ctx);
spicelib::ERRDP(b"#", BNDS1[[2, I]], ctx);
spicelib::ERRINT(b"#", I, ctx);
spicelib::ERRDP(b"#", BNDS1[[1, I]], ctx);
spicelib::SIGERR(b"SPICE(INVALIDBOUNDS)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
if (BNDS2[[2, I]] <= BNDS2[[1, I]]) {
spicelib::SETMSG(
b"BNDS2(2,#) = #; BNDS2(1,#) = #. Rectangle heights (and widths) must be positive.",
ctx,
);
spicelib::ERRINT(b"#", I, ctx);
spicelib::ERRDP(b"#", BNDS2[[2, I]], ctx);
spicelib::ERRINT(b"#", I, ctx);
spicelib::ERRDP(b"#", BNDS2[[1, I]], ctx);
spicelib::SIGERR(b"SPICE(INVALIDBOUNDS)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
}
//
// Find the order of the array of X bounds. We treat the array as a
// one-dimensional array of length 2*NREC.
//
// Produce the corresponding "compressed" inverse order vector. By
// "compressed" we mean: suppose the set of input values were sorted
// and compressed so that it contained no duplicates. For each
// member of the original value array, map the member's index to the
// index of the member in the compressed, sorted array. The
// compressed inverse order vector contains this mapping.
//
save.BSIZE = (2 * NREC);
IOVCMP(
BNDS1.as_slice(),
save.BSIZE,
ORD1.as_slice_mut(),
CIVOR1.as_slice_mut(),
&mut save.RNGMAX,
);
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
for I in 1..=save.BSIZE {
save.J = ORD1[I];
save.K = (((save.J - 1) / 2) + 1);
}
//
// The width of the pixel grid is one less than the number of
// distinct X bound values.
//
*NCOLS = (save.RNGMAX - 1);
//
// Get the order vector and compressed inverse order vector of
// the Y bounds. (Note we have the same number of X and Y
// bounds.)
//
IOVCMP(
BNDS2.as_slice(),
save.BSIZE,
ORD2.as_slice_mut(),
CIVOR2.as_slice_mut(),
&mut save.RNGMAX,
);
if spicelib::FAILED(ctx) {
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
//
// The height of the pixel grid is one less than the number of
// distinct Y bound values.
//
*NROWS = (save.RNGMAX - 1);
//
// Check the grid size again, now that we know how large it
// needs to be.
//
save.NGRID = (*NROWS * *NCOLS);
if (MAXGRD < save.NGRID) {
spicelib::SETMSG(b"MAXGRD is #; must be have size at least # in order to hold pixels for current set of rectangles.", ctx);
spicelib::ERRINT(b"#", MAXGRD, ctx);
spicelib::ERRINT(b"#", save.NGRID, ctx);
spicelib::SIGERR(b"SPICE(VALUEOUTOFRANGE)", ctx)?;
spicelib::CHKOUT(b"RC2GRD", ctx)?;
return Ok(());
}
//
// A program using this routine normally will need to map pixel
// coordinates back to their corresponding d.p. values. Create
// arrays to represent these mappings.
//
// Note that, since the bounds arrays are generally larger than the
// corresponding pixel grid dimensions, the mappings we're about to
// perform (which map the integers 1:BIZE into the ranges of the
// compressed inverse order vectors) are not 1-1. They're still
// valid; the process is just a bit ungainly because it can involve
// overwriting elements of the output array. Each time this happens,
// the affected output array element gets overwritten with the same
// value it already had.
//
// We'll store the mappings in the arrays PXMAP1 and PXMAP2.
// The pixel coordinates
//
// ( I, J )
//
// correspond to the double precision coordinates
//
// BNDS1( PXMAP1(I) )
// BNDS2( PXMAP2(J) )
//
// where we're treating BNDS1 and BNDS2 as one-dimensional
// arrays of length BSIZE.
//
for I in 1..=save.BSIZE {
PXMAP1[CIVOR1[I]] = I;
PXMAP2[CIVOR2[I]] = I;
}
//
// Now map all rectangles to the integer indices of their
// bounds in pixel space. Note that the pixel grid has
// dimensions
//
// ( NROWS, NCOLS )
//
// and the ranges of the integer coordinates of the
// rectangle boundaries are
//
// 1 : NROWS + 1
// 1 : NCOLS + 1
//
// We'll fill in the pixel grid to indicate which pixels are
// covered by rectangles, and which ones lie in gaps.
//
// Initialize the grid to indicate that it consists of one
// large gap.
//
for I in 1..=save.NGRID {
GRID[I] = !VALUE;
}
//
// For each input rectangle, mark the corresponding pixels
// covered by the rectangle. Note that maximum pixel indices
// are less by one than those of the corresponding rectangle
// upper bound indices.
//
for I in 1..=NREC {
//
// Compute the bounds of the current rectangle in pixel
// space. Recall that the all bounds for a given coordinate
// (X or Y) are combined in a sequence of size 2*NREC.
//
save.J = ((2 * (I - 1)) + 1);
save.MINPXX = CIVOR1[save.J];
save.MINPXY = CIVOR2[save.J];
save.K = (2 * I);
save.MAXPXX = CIVOR1[save.K];
save.MAXPXY = CIVOR2[save.K];
for COL in save.MINPXX..=(save.MAXPXX - 1) {
for ROW in save.MINPXY..=(save.MAXPXY - 1) {
//
// Mark the pixel at indices (ROW, COL) as
// covered.
//
save.J = ((*NROWS * (COL - 1)) + ROW);
GRID[save.J] = VALUE;
}
}
}
spicelib::CHKOUT(b"RC2GRD", ctx)?;
Ok(())
}