1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdfrep.c,v 2.17 2013/10/24 16:11:38 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Support BSDF representation as radial basis functions. |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#define _USE_MATH_DEFINES |
11 |
#include <stdlib.h> |
12 |
#include <string.h> |
13 |
#include <math.h> |
14 |
#include "rtio.h" |
15 |
#include "resolu.h" |
16 |
#include "bsdfrep.h" |
17 |
/* active grid resolution */ |
18 |
int grid_res = GRIDRES; |
19 |
|
20 |
/* coverage/symmetry using INP_QUAD? flags */ |
21 |
int inp_coverage = 0; |
22 |
/* all incident angles in-plane so far? */ |
23 |
int single_plane_incident = -1; |
24 |
|
25 |
/* input/output orientations */ |
26 |
int input_orient = 0; |
27 |
int output_orient = 0; |
28 |
|
29 |
/* BSDF histogram */ |
30 |
unsigned long bsdf_hist[HISTLEN]; |
31 |
|
32 |
/* BSDF value for boundary regions */ |
33 |
double bsdf_min = 0; |
34 |
|
35 |
/* processed incident DSF measurements */ |
36 |
RBFNODE *dsf_list = NULL; |
37 |
|
38 |
/* RBF-linking matrices (edges) */ |
39 |
MIGRATION *mig_list = NULL; |
40 |
|
41 |
/* current input direction */ |
42 |
double theta_in_deg, phi_in_deg; |
43 |
|
44 |
/* Register new input direction */ |
45 |
int |
46 |
new_input_direction(double new_theta, double new_phi) |
47 |
{ |
48 |
if (!input_orient) /* check input orientation */ |
49 |
input_orient = 1 - 2*(new_theta > 90.); |
50 |
else if (input_orient > 0 ^ new_theta < 90.) { |
51 |
fprintf(stderr, |
52 |
"%s: Cannot handle input angles on both sides of surface\n", |
53 |
progname); |
54 |
return(0); |
55 |
} |
56 |
/* normalize angle ranges */ |
57 |
while (new_theta < -180.) |
58 |
new_theta += 360.; |
59 |
while (new_theta > 180.) |
60 |
new_theta -= 360.; |
61 |
if (new_theta < 0) { |
62 |
new_theta = -new_theta; |
63 |
new_phi += 180.; |
64 |
} |
65 |
if ((theta_in_deg = new_theta) < 1.0) |
66 |
return(1); /* don't rely on phi near normal */ |
67 |
while (new_phi < 0) |
68 |
new_phi += 360.; |
69 |
while (new_phi >= 360.) |
70 |
new_phi -= 360.; |
71 |
if (single_plane_incident > 0) /* check input coverage */ |
72 |
single_plane_incident = (round(new_phi) == round(phi_in_deg)); |
73 |
else if (single_plane_incident < 0) |
74 |
single_plane_incident = 1; |
75 |
phi_in_deg = new_phi; |
76 |
if ((1. < new_phi) & (new_phi < 89.)) |
77 |
inp_coverage |= INP_QUAD1; |
78 |
else if ((91. < new_phi) & (new_phi < 179.)) |
79 |
inp_coverage |= INP_QUAD2; |
80 |
else if ((181. < new_phi) & (new_phi < 269.)) |
81 |
inp_coverage |= INP_QUAD3; |
82 |
else if ((271. < new_phi) & (new_phi < 359.)) |
83 |
inp_coverage |= INP_QUAD4; |
84 |
return(1); |
85 |
} |
86 |
|
87 |
/* Apply symmetry to the given vector based on distribution */ |
88 |
int |
89 |
use_symmetry(FVECT vec) |
90 |
{ |
91 |
const double phi = get_phi360(vec); |
92 |
|
93 |
switch (inp_coverage) { |
94 |
case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4: |
95 |
break; |
96 |
case INP_QUAD1|INP_QUAD2: |
97 |
if ((-FTINY > phi) | (phi > 180.+FTINY)) |
98 |
goto mir_y; |
99 |
break; |
100 |
case INP_QUAD2|INP_QUAD3: |
101 |
if ((90.-FTINY > phi) | (phi > 270.+FTINY)) |
102 |
goto mir_x; |
103 |
break; |
104 |
case INP_QUAD3|INP_QUAD4: |
105 |
if ((180.-FTINY > phi) | (phi > 360.+FTINY)) |
106 |
goto mir_y; |
107 |
break; |
108 |
case INP_QUAD4|INP_QUAD1: |
109 |
if ((270.-FTINY > phi) & (phi > 90.+FTINY)) |
110 |
goto mir_x; |
111 |
break; |
112 |
case INP_QUAD1: |
113 |
if ((-FTINY > phi) | (phi > 90.+FTINY)) |
114 |
switch ((int)(phi*(1./90.))) { |
115 |
case 1: goto mir_x; |
116 |
case 2: goto mir_xy; |
117 |
case 3: goto mir_y; |
118 |
} |
119 |
break; |
120 |
case INP_QUAD2: |
121 |
if ((90.-FTINY > phi) | (phi > 180.+FTINY)) |
122 |
switch ((int)(phi*(1./90.))) { |
123 |
case 0: goto mir_x; |
124 |
case 2: goto mir_y; |
125 |
case 3: goto mir_xy; |
126 |
} |
127 |
break; |
128 |
case INP_QUAD3: |
129 |
if ((180.-FTINY > phi) | (phi > 270.+FTINY)) |
130 |
switch ((int)(phi*(1./90.))) { |
131 |
case 0: goto mir_xy; |
132 |
case 1: goto mir_y; |
133 |
case 3: goto mir_x; |
134 |
} |
135 |
break; |
136 |
case INP_QUAD4: |
137 |
if ((270.-FTINY > phi) | (phi > 360.+FTINY)) |
138 |
switch ((int)(phi*(1./90.))) { |
139 |
case 0: goto mir_y; |
140 |
case 1: goto mir_xy; |
141 |
case 2: goto mir_x; |
142 |
} |
143 |
break; |
144 |
default: |
145 |
fprintf(stderr, "%s: Illegal input coverage (%d)\n", |
146 |
progname, inp_coverage); |
147 |
exit(1); |
148 |
} |
149 |
return(0); /* in range */ |
150 |
mir_x: |
151 |
vec[0] = -vec[0]; |
152 |
return(MIRROR_X); |
153 |
mir_y: |
154 |
vec[1] = -vec[1]; |
155 |
return(MIRROR_Y); |
156 |
mir_xy: |
157 |
vec[0] = -vec[0]; |
158 |
vec[1] = -vec[1]; |
159 |
return(MIRROR_X|MIRROR_Y); |
160 |
} |
161 |
|
162 |
/* Reverse symmetry based on what was done before */ |
163 |
void |
164 |
rev_symmetry(FVECT vec, int sym) |
165 |
{ |
166 |
if (sym & MIRROR_X) |
167 |
vec[0] = -vec[0]; |
168 |
if (sym & MIRROR_Y) |
169 |
vec[1] = -vec[1]; |
170 |
} |
171 |
|
172 |
/* Reverse symmetry for an RBF distribution */ |
173 |
void |
174 |
rev_rbf_symmetry(RBFNODE *rbf, int sym) |
175 |
{ |
176 |
int n; |
177 |
|
178 |
rev_symmetry(rbf->invec, sym); |
179 |
if (sym & MIRROR_X) |
180 |
for (n = rbf->nrbf; n-- > 0; ) |
181 |
rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx; |
182 |
if (sym & MIRROR_Y) |
183 |
for (n = rbf->nrbf; n-- > 0; ) |
184 |
rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; |
185 |
} |
186 |
|
187 |
/* Rotate RBF to correspond to given incident vector */ |
188 |
void |
189 |
rotate_rbf(RBFNODE *rbf, const FVECT invec) |
190 |
{ |
191 |
static const FVECT vnorm = {.0, .0, 1.}; |
192 |
const double phi = atan2(invec[1],invec[0]) - |
193 |
atan2(rbf->invec[1],rbf->invec[0]); |
194 |
FVECT outvec; |
195 |
int pos[2]; |
196 |
int n; |
197 |
|
198 |
for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { |
199 |
ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); |
200 |
spinvector(outvec, outvec, vnorm, phi); |
201 |
pos_from_vec(pos, outvec); |
202 |
rbf->rbfa[n].gx = pos[0]; |
203 |
rbf->rbfa[n].gy = pos[1]; |
204 |
} |
205 |
VCOPY(rbf->invec, invec); |
206 |
} |
207 |
|
208 |
/* Compute outgoing vector from grid position */ |
209 |
void |
210 |
ovec_from_pos(FVECT vec, int xpos, int ypos) |
211 |
{ |
212 |
double uv[2]; |
213 |
double r2; |
214 |
|
215 |
SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res); |
216 |
/* uniform hemispherical projection */ |
217 |
r2 = uv[0]*uv[0] + uv[1]*uv[1]; |
218 |
vec[0] = vec[1] = sqrt(2. - r2); |
219 |
vec[0] *= uv[0]; |
220 |
vec[1] *= uv[1]; |
221 |
vec[2] = output_orient*(1. - r2); |
222 |
} |
223 |
|
224 |
/* Compute grid position from normalized input/output vector */ |
225 |
void |
226 |
pos_from_vec(int pos[2], const FVECT vec) |
227 |
{ |
228 |
double sq[2]; /* uniform hemispherical projection */ |
229 |
double norm = 1./sqrt(1. + fabs(vec[2])); |
230 |
|
231 |
SDdisk2square(sq, vec[0]*norm, vec[1]*norm); |
232 |
|
233 |
pos[0] = (int)(sq[0]*grid_res); |
234 |
pos[1] = (int)(sq[1]*grid_res); |
235 |
} |
236 |
|
237 |
/* Compute volume associated with Gaussian lobe */ |
238 |
double |
239 |
rbf_volume(const RBFVAL *rbfp) |
240 |
{ |
241 |
double rad = R2ANG(rbfp->crad); |
242 |
FVECT odir; |
243 |
double elev, integ; |
244 |
/* infinite integral approximation */ |
245 |
integ = (2.*M_PI) * rbfp->peak * rad*rad; |
246 |
/* check if we're near horizon */ |
247 |
ovec_from_pos(odir, rbfp->gx, rbfp->gy); |
248 |
elev = output_orient*odir[2]; |
249 |
/* apply cut-off correction if > 1% */ |
250 |
if (elev < 2.8*rad) { |
251 |
/* elev = asin(elev); /* this is so crude, anyway... */ |
252 |
integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad)); |
253 |
} |
254 |
return(integ); |
255 |
} |
256 |
|
257 |
/* Evaluate RBF for DSF at the given normalized outgoing direction */ |
258 |
double |
259 |
eval_rbfrep(const RBFNODE *rp, const FVECT outvec) |
260 |
{ |
261 |
const double rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res); |
262 |
double minval = bsdf_min*output_orient*outvec[2]; |
263 |
int pos[2]; |
264 |
double res = 0; |
265 |
const RBFVAL *rbfp; |
266 |
FVECT odir; |
267 |
double rad2; |
268 |
int n; |
269 |
/* check for wrong side */ |
270 |
if (outvec[2] > 0 ^ output_orient > 0) |
271 |
return(.0); |
272 |
/* use minimum if no information avail. */ |
273 |
if (rp == NULL) |
274 |
return(minval); |
275 |
/* optimization for fast lobe culling */ |
276 |
pos_from_vec(pos, outvec); |
277 |
/* sum radial basis function */ |
278 |
rbfp = rp->rbfa; |
279 |
for (n = rp->nrbf; n--; rbfp++) { |
280 |
int d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) + |
281 |
(pos[1]-rbfp->gy)*(pos[1]-rbfp->gy); |
282 |
rad2 = R2ANG(rbfp->crad); |
283 |
rad2 *= rad2; |
284 |
if (d2 > rad2*rfact2) |
285 |
continue; |
286 |
ovec_from_pos(odir, rbfp->gx, rbfp->gy); |
287 |
res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); |
288 |
} |
289 |
if (res < minval) /* never return less than minval */ |
290 |
return(minval); |
291 |
return(res); |
292 |
} |
293 |
|
294 |
/* Insert a new directional scattering function in our global list */ |
295 |
int |
296 |
insert_dsf(RBFNODE *newrbf) |
297 |
{ |
298 |
RBFNODE *rbf, *rbf_last; |
299 |
int pos; |
300 |
/* check for redundant meas. */ |
301 |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
302 |
if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) { |
303 |
fprintf(stderr, |
304 |
"%s: Duplicate incident measurement (ignored)\n", |
305 |
progname); |
306 |
free(newrbf); |
307 |
return(-1); |
308 |
} |
309 |
/* keep in ascending theta order */ |
310 |
for (rbf_last = NULL, rbf = dsf_list; rbf != NULL; |
311 |
rbf_last = rbf, rbf = rbf->next) |
312 |
if (single_plane_incident && input_orient*rbf->invec[2] < |
313 |
input_orient*newrbf->invec[2]) |
314 |
break; |
315 |
if (rbf_last == NULL) { /* insert new node in list */ |
316 |
newrbf->ord = 0; |
317 |
newrbf->next = dsf_list; |
318 |
dsf_list = newrbf; |
319 |
} else { |
320 |
newrbf->ord = rbf_last->ord + 1; |
321 |
newrbf->next = rbf; |
322 |
rbf_last->next = newrbf; |
323 |
} |
324 |
rbf_last = newrbf; |
325 |
while (rbf != NULL) { /* update ordinal positions */ |
326 |
rbf->ord = rbf_last->ord + 1; |
327 |
rbf_last = rbf; |
328 |
rbf = rbf->next; |
329 |
} |
330 |
return(newrbf->ord); |
331 |
} |
332 |
|
333 |
/* Get the DSF indicated by its ordinal position */ |
334 |
RBFNODE * |
335 |
get_dsf(int ord) |
336 |
{ |
337 |
RBFNODE *rbf; |
338 |
|
339 |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) |
340 |
if (rbf->ord == ord) |
341 |
return(rbf); |
342 |
return(NULL); |
343 |
} |
344 |
|
345 |
/* Get triangle surface orientation (unnormalized) */ |
346 |
void |
347 |
tri_orient(FVECT vres, const FVECT v1, const FVECT v2, const FVECT v3) |
348 |
{ |
349 |
FVECT v2minus1, v3minus2; |
350 |
|
351 |
VSUB(v2minus1, v2, v1); |
352 |
VSUB(v3minus2, v3, v2); |
353 |
VCROSS(vres, v2minus1, v3minus2); |
354 |
} |
355 |
|
356 |
/* Determine if vertex order is reversed (inward normal) */ |
357 |
int |
358 |
is_rev_tri(const FVECT v1, const FVECT v2, const FVECT v3) |
359 |
{ |
360 |
FVECT tor; |
361 |
|
362 |
tri_orient(tor, v1, v2, v3); |
363 |
|
364 |
return(DOT(tor, v2) < 0.); |
365 |
} |
366 |
|
367 |
/* Find vertices completing triangles on either side of the given edge */ |
368 |
int |
369 |
get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) |
370 |
{ |
371 |
const MIGRATION *ej1, *ej2; |
372 |
RBFNODE *tv; |
373 |
|
374 |
rbfv[0] = rbfv[1] = NULL; |
375 |
if (mig == NULL) |
376 |
return(0); |
377 |
for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; |
378 |
ej1 = nextedge(mig->rbfv[0],ej1)) { |
379 |
if (ej1 == mig) |
380 |
continue; |
381 |
tv = opp_rbf(mig->rbfv[0],ej1); |
382 |
for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) |
383 |
if (opp_rbf(tv,ej2) == mig->rbfv[1]) { |
384 |
rbfv[is_rev_tri(mig->rbfv[0]->invec, |
385 |
mig->rbfv[1]->invec, |
386 |
tv->invec)] = tv; |
387 |
break; |
388 |
} |
389 |
} |
390 |
return((rbfv[0] != NULL) + (rbfv[1] != NULL)); |
391 |
} |
392 |
|
393 |
/* Clear our BSDF representation and free memory */ |
394 |
void |
395 |
clear_bsdf_rep(void) |
396 |
{ |
397 |
while (mig_list != NULL) { |
398 |
MIGRATION *mig = mig_list; |
399 |
mig_list = mig->next; |
400 |
free(mig); |
401 |
} |
402 |
while (dsf_list != NULL) { |
403 |
RBFNODE *rbf = dsf_list; |
404 |
dsf_list = rbf->next; |
405 |
free(rbf); |
406 |
} |
407 |
inp_coverage = 0; |
408 |
single_plane_incident = -1; |
409 |
input_orient = output_orient = 0; |
410 |
grid_res = GRIDRES; |
411 |
} |
412 |
|
413 |
/* Write our BSDF mesh interpolant out to the given binary stream */ |
414 |
void |
415 |
save_bsdf_rep(FILE *ofp) |
416 |
{ |
417 |
RBFNODE *rbf; |
418 |
MIGRATION *mig; |
419 |
int i, n; |
420 |
/* finish header */ |
421 |
fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); |
422 |
fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); |
423 |
fprintf(ofp, "GRIDRES=%d\n", grid_res); |
424 |
fprintf(ofp, "BSDFMIN=%g\n", bsdf_min); |
425 |
fputformat(BSDFREP_FMT, ofp); |
426 |
fputc('\n', ofp); |
427 |
/* write each DSF */ |
428 |
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { |
429 |
putint(rbf->ord, 4, ofp); |
430 |
putflt(rbf->invec[0], ofp); |
431 |
putflt(rbf->invec[1], ofp); |
432 |
putflt(rbf->invec[2], ofp); |
433 |
putflt(rbf->vtotal, ofp); |
434 |
putint(rbf->nrbf, 4, ofp); |
435 |
for (i = 0; i < rbf->nrbf; i++) { |
436 |
putflt(rbf->rbfa[i].peak, ofp); |
437 |
putint(rbf->rbfa[i].crad, 2, ofp); |
438 |
putint(rbf->rbfa[i].gx, 1, ofp); |
439 |
putint(rbf->rbfa[i].gy, 1, ofp); |
440 |
} |
441 |
} |
442 |
putint(-1, 4, ofp); /* terminator */ |
443 |
/* write each migration matrix */ |
444 |
for (mig = mig_list; mig != NULL; mig = mig->next) { |
445 |
int zerocnt = 0; |
446 |
putint(mig->rbfv[0]->ord, 4, ofp); |
447 |
putint(mig->rbfv[1]->ord, 4, ofp); |
448 |
/* write out as sparse data */ |
449 |
n = mtx_nrows(mig) * mtx_ncols(mig); |
450 |
for (i = 0; i < n; i++) { |
451 |
if (zerocnt == 0xff) { |
452 |
putint(0xff, 1, ofp); zerocnt = 0; |
453 |
} |
454 |
if (mig->mtx[i] != 0) { |
455 |
putint(zerocnt, 1, ofp); zerocnt = 0; |
456 |
putflt(mig->mtx[i], ofp); |
457 |
} else |
458 |
++zerocnt; |
459 |
} |
460 |
putint(zerocnt, 1, ofp); |
461 |
} |
462 |
putint(-1, 4, ofp); /* terminator */ |
463 |
putint(-1, 4, ofp); |
464 |
if (fflush(ofp) == EOF) { |
465 |
fprintf(stderr, "%s: error writing BSDF interpolant\n", |
466 |
progname); |
467 |
exit(1); |
468 |
} |
469 |
} |
470 |
|
471 |
/* Check header line for critical information */ |
472 |
static int |
473 |
headline(char *s, void *p) |
474 |
{ |
475 |
char fmt[32]; |
476 |
|
477 |
if (!strncmp(s, "SYMMETRY=", 9)) { |
478 |
inp_coverage = atoi(s+9); |
479 |
single_plane_incident = !inp_coverage; |
480 |
return(0); |
481 |
} |
482 |
if (!strncmp(s, "IO_SIDES=", 9)) { |
483 |
sscanf(s+9, "%d %d", &input_orient, &output_orient); |
484 |
return(0); |
485 |
} |
486 |
if (!strncmp(s, "GRIDRES=", 8)) { |
487 |
sscanf(s+8, "%d", &grid_res); |
488 |
return(0); |
489 |
} |
490 |
if (!strncmp(s, "BSDFMIN=", 8)) { |
491 |
sscanf(s+8, "%lf", &bsdf_min); |
492 |
return(0); |
493 |
} |
494 |
if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) |
495 |
return(-1); |
496 |
return(0); |
497 |
} |
498 |
|
499 |
/* Read a BSDF mesh interpolant from the given binary stream */ |
500 |
int |
501 |
load_bsdf_rep(FILE *ifp) |
502 |
{ |
503 |
RBFNODE rbfh; |
504 |
int from_ord, to_ord; |
505 |
int i; |
506 |
|
507 |
clear_bsdf_rep(); |
508 |
if (ifp == NULL) |
509 |
return(0); |
510 |
if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | |
511 |
!input_orient | !output_orient) { |
512 |
fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", |
513 |
progname); |
514 |
return(0); |
515 |
} |
516 |
memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */ |
517 |
while ((rbfh.ord = getint(4, ifp)) >= 0) { |
518 |
RBFNODE *newrbf; |
519 |
|
520 |
rbfh.invec[0] = getflt(ifp); |
521 |
rbfh.invec[1] = getflt(ifp); |
522 |
rbfh.invec[2] = getflt(ifp); |
523 |
if (normalize(rbfh.invec) == 0) { |
524 |
fprintf(stderr, "%s: zero incident vector\n", progname); |
525 |
return(0); |
526 |
} |
527 |
rbfh.vtotal = getflt(ifp); |
528 |
rbfh.nrbf = getint(4, ifp); |
529 |
newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + |
530 |
sizeof(RBFVAL)*(rbfh.nrbf-1)); |
531 |
if (newrbf == NULL) |
532 |
goto memerr; |
533 |
*newrbf = rbfh; |
534 |
for (i = 0; i < rbfh.nrbf; i++) { |
535 |
newrbf->rbfa[i].peak = getflt(ifp); |
536 |
newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff; |
537 |
newrbf->rbfa[i].gx = getint(1, ifp) & 0xff; |
538 |
newrbf->rbfa[i].gy = getint(1, ifp) & 0xff; |
539 |
} |
540 |
if (feof(ifp)) |
541 |
goto badEOF; |
542 |
/* insert in global list */ |
543 |
if (insert_dsf(newrbf) != rbfh.ord) { |
544 |
fprintf(stderr, "%s: error adding DSF\n", progname); |
545 |
return(0); |
546 |
} |
547 |
} |
548 |
/* read each migration matrix */ |
549 |
while ((from_ord = getint(4, ifp)) >= 0 && |
550 |
(to_ord = getint(4, ifp)) >= 0) { |
551 |
RBFNODE *from_rbf = get_dsf(from_ord); |
552 |
RBFNODE *to_rbf = get_dsf(to_ord); |
553 |
MIGRATION *newmig; |
554 |
int n; |
555 |
|
556 |
if ((from_rbf == NULL) | (to_rbf == NULL)) { |
557 |
fprintf(stderr, |
558 |
"%s: bad DSF reference in migration edge\n", |
559 |
progname); |
560 |
return(0); |
561 |
} |
562 |
n = from_rbf->nrbf * to_rbf->nrbf; |
563 |
newmig = (MIGRATION *)malloc(sizeof(MIGRATION) + |
564 |
sizeof(float)*(n-1)); |
565 |
if (newmig == NULL) |
566 |
goto memerr; |
567 |
newmig->rbfv[0] = from_rbf; |
568 |
newmig->rbfv[1] = to_rbf; |
569 |
memset(newmig->mtx, 0, sizeof(float)*n); |
570 |
for (i = 0; ; ) { /* read sparse data */ |
571 |
int zc = getint(1, ifp) & 0xff; |
572 |
if ((i += zc) >= n) |
573 |
break; |
574 |
if (zc == 0xff) |
575 |
continue; |
576 |
newmig->mtx[i++] = getflt(ifp); |
577 |
} |
578 |
if (feof(ifp)) |
579 |
goto badEOF; |
580 |
/* insert in edge lists */ |
581 |
newmig->enxt[0] = from_rbf->ejl; |
582 |
from_rbf->ejl = newmig; |
583 |
newmig->enxt[1] = to_rbf->ejl; |
584 |
to_rbf->ejl = newmig; |
585 |
/* push onto global list */ |
586 |
newmig->next = mig_list; |
587 |
mig_list = newmig; |
588 |
} |
589 |
return(1); /* success! */ |
590 |
memerr: |
591 |
fprintf(stderr, "%s: Out of memory in load_bsdf_rep()\n", progname); |
592 |
exit(1); |
593 |
badEOF: |
594 |
fprintf(stderr, "%s: Unexpected EOF in load_bsdf_rep()\n", progname); |
595 |
return(0); |
596 |
} |