| 1 |
greg |
2.1 |
#ifndef lint
|
| 2 |
|
|
static const char RCSid[] = "$Id$";
|
| 3 |
|
|
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* Interpolate BSDF data from radial basis functions in advection mesh.
|
| 6 |
|
|
*
|
| 7 |
|
|
* G. Ward
|
| 8 |
|
|
*/
|
| 9 |
|
|
|
| 10 |
|
|
#define _USE_MATH_DEFINES
|
| 11 |
|
|
#include <stdio.h>
|
| 12 |
|
|
#include <stdlib.h>
|
| 13 |
|
|
#include <string.h>
|
| 14 |
|
|
#include <math.h>
|
| 15 |
|
|
#include "bsdfrep.h"
|
| 16 |
|
|
/* migration edges drawn in raster fashion */
|
| 17 |
|
|
MIGRATION *mig_grid[GRIDRES][GRIDRES];
|
| 18 |
|
|
|
| 19 |
|
|
#ifdef DEBUG
|
| 20 |
|
|
#include "random.h"
|
| 21 |
|
|
#include "bmpfile.h"
|
| 22 |
|
|
/* Hash pointer to byte value (must return 0 for NULL) */
|
| 23 |
|
|
static int
|
| 24 |
|
|
byte_hash(const void *p)
|
| 25 |
|
|
{
|
| 26 |
|
|
size_t h = (size_t)p;
|
| 27 |
|
|
h ^= (size_t)p >> 8;
|
| 28 |
|
|
h ^= (size_t)p >> 16;
|
| 29 |
|
|
h ^= (size_t)p >> 24;
|
| 30 |
|
|
return(h & 0xff);
|
| 31 |
|
|
}
|
| 32 |
|
|
/* Write out BMP image showing edges */
|
| 33 |
|
|
static void
|
| 34 |
|
|
write_edge_image(const char *fname)
|
| 35 |
|
|
{
|
| 36 |
|
|
BMPHeader *hdr = BMPmappedHeader(GRIDRES, GRIDRES, 0, 256);
|
| 37 |
|
|
BMPWriter *wtr;
|
| 38 |
|
|
int i, j;
|
| 39 |
|
|
|
| 40 |
|
|
fprintf(stderr, "Writing incident mesh drawing to '%s'\n", fname);
|
| 41 |
|
|
hdr->compr = BI_RLE8;
|
| 42 |
|
|
for (i = 256; --i; ) { /* assign random color map */
|
| 43 |
|
|
hdr->palette[i].r = random() & 0xff;
|
| 44 |
|
|
hdr->palette[i].g = random() & 0xff;
|
| 45 |
|
|
hdr->palette[i].b = random() & 0xff;
|
| 46 |
|
|
/* reject dark colors */
|
| 47 |
|
|
i += (hdr->palette[i].r + hdr->palette[i].g +
|
| 48 |
|
|
hdr->palette[i].b < 128);
|
| 49 |
|
|
}
|
| 50 |
|
|
hdr->palette[0].r = hdr->palette[0].g = hdr->palette[0].b = 0;
|
| 51 |
|
|
/* open output */
|
| 52 |
|
|
wtr = BMPopenOutputFile(fname, hdr);
|
| 53 |
|
|
if (wtr == NULL) {
|
| 54 |
|
|
free(hdr);
|
| 55 |
|
|
return;
|
| 56 |
|
|
}
|
| 57 |
|
|
for (i = 0; i < GRIDRES; i++) { /* write scanlines */
|
| 58 |
|
|
for (j = 0; j < GRIDRES; j++)
|
| 59 |
|
|
wtr->scanline[j] = byte_hash(mig_grid[i][j]);
|
| 60 |
|
|
if (BMPwriteScanline(wtr) != BIR_OK)
|
| 61 |
|
|
break;
|
| 62 |
|
|
}
|
| 63 |
|
|
BMPcloseOutput(wtr); /* close & clean up */
|
| 64 |
|
|
}
|
| 65 |
|
|
#endif
|
| 66 |
|
|
|
| 67 |
|
|
/* Draw edge list into mig_grid array */
|
| 68 |
|
|
void
|
| 69 |
|
|
draw_edges(void)
|
| 70 |
|
|
{
|
| 71 |
|
|
int nnull = 0, ntot = 0;
|
| 72 |
|
|
MIGRATION *ej;
|
| 73 |
|
|
int p0[2], p1[2];
|
| 74 |
|
|
|
| 75 |
|
|
memset(mig_grid, 0, sizeof(mig_grid));
|
| 76 |
|
|
for (ej = mig_list; ej != NULL; ej = ej->next) {
|
| 77 |
|
|
++ntot;
|
| 78 |
|
|
pos_from_vec(p0, ej->rbfv[0]->invec);
|
| 79 |
|
|
pos_from_vec(p1, ej->rbfv[1]->invec);
|
| 80 |
|
|
if ((p0[0] == p1[0]) & (p0[1] == p1[1])) {
|
| 81 |
|
|
++nnull;
|
| 82 |
|
|
mig_grid[p0[0]][p0[1]] = ej;
|
| 83 |
|
|
continue;
|
| 84 |
|
|
}
|
| 85 |
|
|
if (abs(p1[0]-p0[0]) > abs(p1[1]-p0[1])) {
|
| 86 |
|
|
const int xstep = 2*(p1[0] > p0[0]) - 1;
|
| 87 |
|
|
const double ystep = (double)((p1[1]-p0[1])*xstep) /
|
| 88 |
|
|
(double)(p1[0]-p0[0]);
|
| 89 |
|
|
int x;
|
| 90 |
|
|
double y;
|
| 91 |
|
|
for (x = p0[0], y = p0[1]+.5; x != p1[0];
|
| 92 |
|
|
x += xstep, y += ystep)
|
| 93 |
|
|
mig_grid[x][(int)y] = ej;
|
| 94 |
|
|
mig_grid[x][(int)y] = ej;
|
| 95 |
|
|
} else {
|
| 96 |
|
|
const int ystep = 2*(p1[1] > p0[1]) - 1;
|
| 97 |
|
|
const double xstep = (double)((p1[0]-p0[0])*ystep) /
|
| 98 |
|
|
(double)(p1[1]-p0[1]);
|
| 99 |
|
|
int y;
|
| 100 |
|
|
double x;
|
| 101 |
|
|
for (y = p0[1], x = p0[0]+.5; y != p1[1];
|
| 102 |
|
|
y += ystep, x += xstep)
|
| 103 |
|
|
mig_grid[(int)x][y] = ej;
|
| 104 |
|
|
mig_grid[(int)x][y] = ej;
|
| 105 |
|
|
}
|
| 106 |
|
|
}
|
| 107 |
|
|
if (nnull)
|
| 108 |
|
|
fprintf(stderr, "Warning: %d of %d edges are null\n",
|
| 109 |
|
|
nnull, ntot);
|
| 110 |
|
|
#ifdef DEBUG
|
| 111 |
|
|
write_edge_image("bsdf_edges.bmp");
|
| 112 |
|
|
#endif
|
| 113 |
|
|
}
|
| 114 |
|
|
|
| 115 |
|
|
/* Identify enclosing triangle for this position (flood fill raster check) */
|
| 116 |
|
|
static int
|
| 117 |
|
|
identify_tri(MIGRATION *miga[3], unsigned char vmap[GRIDRES][(GRIDRES+7)/8],
|
| 118 |
|
|
int px, int py)
|
| 119 |
|
|
{
|
| 120 |
|
|
const int btest = 1<<(py&07);
|
| 121 |
|
|
|
| 122 |
|
|
if (vmap[px][py>>3] & btest) /* already visited here? */
|
| 123 |
|
|
return(1);
|
| 124 |
|
|
/* else mark it */
|
| 125 |
|
|
vmap[px][py>>3] |= btest;
|
| 126 |
|
|
|
| 127 |
|
|
if (mig_grid[px][py] != NULL) { /* are we on an edge? */
|
| 128 |
|
|
int i;
|
| 129 |
|
|
for (i = 0; i < 3; i++) {
|
| 130 |
|
|
if (miga[i] == mig_grid[px][py])
|
| 131 |
|
|
return(1);
|
| 132 |
|
|
if (miga[i] != NULL)
|
| 133 |
|
|
continue;
|
| 134 |
|
|
miga[i] = mig_grid[px][py];
|
| 135 |
|
|
return(1);
|
| 136 |
|
|
}
|
| 137 |
|
|
return(0); /* outside triangle! */
|
| 138 |
|
|
}
|
| 139 |
|
|
/* check neighbors (flood) */
|
| 140 |
|
|
if (px > 0 && !identify_tri(miga, vmap, px-1, py))
|
| 141 |
|
|
return(0);
|
| 142 |
|
|
if (px < GRIDRES-1 && !identify_tri(miga, vmap, px+1, py))
|
| 143 |
|
|
return(0);
|
| 144 |
|
|
if (py > 0 && !identify_tri(miga, vmap, px, py-1))
|
| 145 |
|
|
return(0);
|
| 146 |
|
|
if (py < GRIDRES-1 && !identify_tri(miga, vmap, px, py+1))
|
| 147 |
|
|
return(0);
|
| 148 |
|
|
return(1); /* this neighborhood done */
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
/* Insert vertex in ordered list */
|
| 152 |
|
|
static void
|
| 153 |
|
|
insert_vert(RBFNODE **vlist, RBFNODE *v)
|
| 154 |
|
|
{
|
| 155 |
|
|
int i, j;
|
| 156 |
|
|
|
| 157 |
|
|
for (i = 0; vlist[i] != NULL; i++) {
|
| 158 |
|
|
if (v == vlist[i])
|
| 159 |
|
|
return;
|
| 160 |
|
|
if (v->ord < vlist[i]->ord)
|
| 161 |
|
|
break;
|
| 162 |
|
|
}
|
| 163 |
|
|
for (j = i; vlist[j] != NULL; j++)
|
| 164 |
|
|
;
|
| 165 |
|
|
while (j > i) {
|
| 166 |
|
|
vlist[j] = vlist[j-1];
|
| 167 |
|
|
--j;
|
| 168 |
|
|
}
|
| 169 |
|
|
vlist[i] = v;
|
| 170 |
|
|
}
|
| 171 |
|
|
|
| 172 |
|
|
/* Sort triangle edges in standard order */
|
| 173 |
|
|
static int
|
| 174 |
|
|
order_triangle(MIGRATION *miga[3])
|
| 175 |
|
|
{
|
| 176 |
|
|
RBFNODE *vert[7];
|
| 177 |
|
|
MIGRATION *ord[3];
|
| 178 |
|
|
int i;
|
| 179 |
|
|
/* order vertices, first */
|
| 180 |
|
|
memset(vert, 0, sizeof(vert));
|
| 181 |
|
|
for (i = 3; i--; ) {
|
| 182 |
|
|
if (miga[i] == NULL)
|
| 183 |
|
|
return(0);
|
| 184 |
|
|
insert_vert(vert, miga[i]->rbfv[0]);
|
| 185 |
|
|
insert_vert(vert, miga[i]->rbfv[1]);
|
| 186 |
|
|
}
|
| 187 |
|
|
/* should be just 3 vertices */
|
| 188 |
|
|
if ((vert[3] == NULL) | (vert[4] != NULL))
|
| 189 |
|
|
return(0);
|
| 190 |
|
|
/* identify edge 0 */
|
| 191 |
|
|
for (i = 3; i--; )
|
| 192 |
|
|
if (miga[i]->rbfv[0] == vert[0] &&
|
| 193 |
|
|
miga[i]->rbfv[1] == vert[1]) {
|
| 194 |
|
|
ord[0] = miga[i];
|
| 195 |
|
|
break;
|
| 196 |
|
|
}
|
| 197 |
|
|
if (i < 0)
|
| 198 |
|
|
return(0);
|
| 199 |
|
|
/* identify edge 1 */
|
| 200 |
|
|
for (i = 3; i--; )
|
| 201 |
|
|
if (miga[i]->rbfv[0] == vert[1] &&
|
| 202 |
|
|
miga[i]->rbfv[1] == vert[2]) {
|
| 203 |
|
|
ord[1] = miga[i];
|
| 204 |
|
|
break;
|
| 205 |
|
|
}
|
| 206 |
|
|
if (i < 0)
|
| 207 |
|
|
return(0);
|
| 208 |
|
|
/* identify edge 2 */
|
| 209 |
|
|
for (i = 3; i--; )
|
| 210 |
|
|
if (miga[i]->rbfv[0] == vert[0] &&
|
| 211 |
|
|
miga[i]->rbfv[1] == vert[2]) {
|
| 212 |
|
|
ord[2] = miga[i];
|
| 213 |
|
|
break;
|
| 214 |
|
|
}
|
| 215 |
|
|
if (i < 0)
|
| 216 |
|
|
return(0);
|
| 217 |
|
|
/* reassign order */
|
| 218 |
|
|
miga[0] = ord[0]; miga[1] = ord[1]; miga[2] = ord[2];
|
| 219 |
|
|
return(1);
|
| 220 |
|
|
}
|
| 221 |
|
|
|
| 222 |
|
|
/* Find edge(s) for interpolating the given vector, applying symmetry */
|
| 223 |
|
|
int
|
| 224 |
|
|
get_interp(MIGRATION *miga[3], FVECT invec)
|
| 225 |
|
|
{
|
| 226 |
|
|
miga[0] = miga[1] = miga[2] = NULL;
|
| 227 |
|
|
if (single_plane_incident) { /* isotropic BSDF? */
|
| 228 |
|
|
RBFNODE *rbf; /* find edge we're on */
|
| 229 |
|
|
for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) {
|
| 230 |
|
|
if (input_orient*rbf->invec[2] < input_orient*invec[2])
|
| 231 |
|
|
break;
|
| 232 |
|
|
if (rbf->next != NULL &&
|
| 233 |
|
|
input_orient*rbf->next->invec[2] <
|
| 234 |
|
|
input_orient*invec[2]) {
|
| 235 |
|
|
for (miga[0] = rbf->ejl; miga[0] != NULL;
|
| 236 |
|
|
miga[0] = nextedge(rbf,miga[0]))
|
| 237 |
|
|
if (opp_rbf(rbf,miga[0]) == rbf->next)
|
| 238 |
|
|
return(0);
|
| 239 |
|
|
break;
|
| 240 |
|
|
}
|
| 241 |
|
|
}
|
| 242 |
|
|
return(-1); /* outside range! */
|
| 243 |
|
|
}
|
| 244 |
|
|
{ /* else use triangle mesh */
|
| 245 |
|
|
const int sym = use_symmetry(invec);
|
| 246 |
|
|
unsigned char floodmap[GRIDRES][(GRIDRES+7)/8];
|
| 247 |
|
|
int pstart[2];
|
| 248 |
|
|
RBFNODE *vother;
|
| 249 |
|
|
MIGRATION *ej;
|
| 250 |
|
|
int i;
|
| 251 |
|
|
|
| 252 |
|
|
pos_from_vec(pstart, invec);
|
| 253 |
|
|
memset(floodmap, 0, sizeof(floodmap));
|
| 254 |
|
|
/* call flooding function */
|
| 255 |
|
|
if (!identify_tri(miga, floodmap, pstart[0], pstart[1]))
|
| 256 |
|
|
return(-1); /* outside mesh */
|
| 257 |
|
|
if ((miga[0] == NULL) | (miga[2] == NULL))
|
| 258 |
|
|
return(-1); /* should never happen */
|
| 259 |
|
|
if (miga[1] == NULL)
|
| 260 |
|
|
return(sym); /* on edge */
|
| 261 |
|
|
/* verify triangle */
|
| 262 |
|
|
if (!order_triangle(miga)) {
|
| 263 |
|
|
#ifdef DEBUG
|
| 264 |
|
|
fputs("Munged triangle in get_interp()\n", stderr);
|
| 265 |
|
|
#endif
|
| 266 |
|
|
vother = NULL; /* find triangle from edge */
|
| 267 |
|
|
for (i = 3; i--; ) {
|
| 268 |
|
|
RBFNODE *tpair[2];
|
| 269 |
|
|
if (get_triangles(tpair, miga[i]) &&
|
| 270 |
|
|
(vother = tpair[ is_rev_tri(
|
| 271 |
|
|
miga[i]->rbfv[0]->invec,
|
| 272 |
|
|
miga[i]->rbfv[1]->invec,
|
| 273 |
|
|
invec) ]) != NULL)
|
| 274 |
|
|
break;
|
| 275 |
|
|
}
|
| 276 |
|
|
if (vother == NULL) { /* couldn't find 3rd vertex */
|
| 277 |
|
|
#ifdef DEBUG
|
| 278 |
|
|
fputs("No triangle in get_interp()\n", stderr);
|
| 279 |
|
|
#endif
|
| 280 |
|
|
return(-1);
|
| 281 |
|
|
}
|
| 282 |
|
|
/* reassign other two edges */
|
| 283 |
|
|
for (ej = vother->ejl; ej != NULL;
|
| 284 |
|
|
ej = nextedge(vother,ej)) {
|
| 285 |
|
|
RBFNODE *vorig = opp_rbf(vother,ej);
|
| 286 |
|
|
if (vorig == miga[i]->rbfv[0])
|
| 287 |
|
|
miga[(i+1)%3] = ej;
|
| 288 |
|
|
else if (vorig == miga[i]->rbfv[1])
|
| 289 |
|
|
miga[(i+2)%3] = ej;
|
| 290 |
|
|
}
|
| 291 |
|
|
if (!order_triangle(miga)) {
|
| 292 |
|
|
#ifdef DEBUG
|
| 293 |
|
|
fputs("Bad triangle in get_interp()\n", stderr);
|
| 294 |
|
|
#endif
|
| 295 |
|
|
return(-1);
|
| 296 |
|
|
}
|
| 297 |
|
|
}
|
| 298 |
|
|
return(sym); /* return in standard order */
|
| 299 |
|
|
}
|
| 300 |
|
|
}
|
| 301 |
|
|
|
| 302 |
|
|
/* Advect and allocate new RBF along edge */
|
| 303 |
|
|
static RBFNODE *
|
| 304 |
|
|
e_advect_rbf(const MIGRATION *mig, const FVECT invec)
|
| 305 |
|
|
{
|
| 306 |
|
|
RBFNODE *rbf;
|
| 307 |
|
|
int n, i, j;
|
| 308 |
|
|
double t, full_dist;
|
| 309 |
|
|
/* get relative position */
|
| 310 |
|
|
t = acos(DOT(invec, mig->rbfv[0]->invec));
|
| 311 |
|
|
if (t < M_PI/GRIDRES) { /* near first DSF */
|
| 312 |
|
|
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1);
|
| 313 |
|
|
rbf = (RBFNODE *)malloc(n);
|
| 314 |
|
|
if (rbf == NULL)
|
| 315 |
|
|
goto memerr;
|
| 316 |
|
|
memcpy(rbf, mig->rbfv[0], n); /* just duplicate */
|
| 317 |
|
|
return(rbf);
|
| 318 |
|
|
}
|
| 319 |
|
|
full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec));
|
| 320 |
|
|
if (t > full_dist-M_PI/GRIDRES) { /* near second DSF */
|
| 321 |
|
|
n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1);
|
| 322 |
|
|
rbf = (RBFNODE *)malloc(n);
|
| 323 |
|
|
if (rbf == NULL)
|
| 324 |
|
|
goto memerr;
|
| 325 |
|
|
memcpy(rbf, mig->rbfv[1], n); /* just duplicate */
|
| 326 |
|
|
return(rbf);
|
| 327 |
|
|
}
|
| 328 |
|
|
t /= full_dist;
|
| 329 |
|
|
n = 0; /* count migrating particles */
|
| 330 |
|
|
for (i = 0; i < mtx_nrows(mig); i++)
|
| 331 |
|
|
for (j = 0; j < mtx_ncols(mig); j++)
|
| 332 |
|
|
n += (mig->mtx[mtx_ndx(mig,i,j)] > FTINY);
|
| 333 |
|
|
#ifdef DEBUG
|
| 334 |
|
|
fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n",
|
| 335 |
|
|
mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n);
|
| 336 |
|
|
#endif
|
| 337 |
|
|
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
|
| 338 |
|
|
if (rbf == NULL)
|
| 339 |
|
|
goto memerr;
|
| 340 |
|
|
rbf->next = NULL; rbf->ejl = NULL;
|
| 341 |
|
|
VCOPY(rbf->invec, invec);
|
| 342 |
|
|
rbf->nrbf = n;
|
| 343 |
|
|
rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal;
|
| 344 |
|
|
n = 0; /* advect RBF lobes */
|
| 345 |
|
|
for (i = 0; i < mtx_nrows(mig); i++) {
|
| 346 |
|
|
const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i];
|
| 347 |
|
|
const float peak0 = rbf0i->peak;
|
| 348 |
|
|
const double rad0 = R2ANG(rbf0i->crad);
|
| 349 |
|
|
FVECT v0;
|
| 350 |
|
|
float mv;
|
| 351 |
|
|
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
|
| 352 |
|
|
for (j = 0; j < mtx_ncols(mig); j++)
|
| 353 |
|
|
if ((mv = mig->mtx[mtx_ndx(mig,i,j)]) > FTINY) {
|
| 354 |
|
|
const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j];
|
| 355 |
|
|
double rad1 = R2ANG(rbf1j->crad);
|
| 356 |
|
|
FVECT v;
|
| 357 |
|
|
int pos[2];
|
| 358 |
|
|
rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal;
|
| 359 |
|
|
rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) +
|
| 360 |
|
|
rad1*rad1*t));
|
| 361 |
|
|
ovec_from_pos(v, rbf1j->gx, rbf1j->gy);
|
| 362 |
|
|
geodesic(v, v0, v, t, GEOD_REL);
|
| 363 |
|
|
pos_from_vec(pos, v);
|
| 364 |
|
|
rbf->rbfa[n].gx = pos[0];
|
| 365 |
|
|
rbf->rbfa[n].gy = pos[1];
|
| 366 |
|
|
++n;
|
| 367 |
|
|
}
|
| 368 |
|
|
}
|
| 369 |
|
|
rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */
|
| 370 |
|
|
return(rbf);
|
| 371 |
|
|
memerr:
|
| 372 |
|
|
fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname);
|
| 373 |
|
|
exit(1);
|
| 374 |
|
|
return(NULL); /* pro forma return */
|
| 375 |
|
|
}
|
| 376 |
|
|
|
| 377 |
|
|
/* Partially advect between recorded incident angles and allocate new RBF */
|
| 378 |
|
|
RBFNODE *
|
| 379 |
|
|
advect_rbf(const FVECT invec)
|
| 380 |
|
|
{
|
| 381 |
|
|
FVECT sivec;
|
| 382 |
|
|
MIGRATION *miga[3];
|
| 383 |
|
|
RBFNODE *rbf;
|
| 384 |
|
|
int sym;
|
| 385 |
|
|
float mbfact, mcfact;
|
| 386 |
|
|
int n, i, j, k;
|
| 387 |
|
|
FVECT v0, v1, v2;
|
| 388 |
|
|
double s, t;
|
| 389 |
|
|
|
| 390 |
|
|
VCOPY(sivec, invec); /* find triangle/edge */
|
| 391 |
|
|
sym = get_interp(miga, sivec);
|
| 392 |
|
|
if (sym < 0) /* can't interpolate? */
|
| 393 |
|
|
return(NULL);
|
| 394 |
|
|
if (miga[1] == NULL) { /* advect along edge? */
|
| 395 |
|
|
rbf = e_advect_rbf(miga[0], sivec);
|
| 396 |
|
|
rev_rbf_symmetry(rbf, sym);
|
| 397 |
|
|
return(rbf);
|
| 398 |
|
|
}
|
| 399 |
|
|
#ifdef DEBUG
|
| 400 |
|
|
if (miga[0]->rbfv[0] != miga[2]->rbfv[0] |
|
| 401 |
|
|
miga[0]->rbfv[1] != miga[1]->rbfv[0] |
|
| 402 |
|
|
miga[1]->rbfv[1] != miga[2]->rbfv[1]) {
|
| 403 |
|
|
fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname);
|
| 404 |
|
|
exit(1);
|
| 405 |
|
|
}
|
| 406 |
|
|
#endif
|
| 407 |
|
|
/* figure out position */
|
| 408 |
|
|
fcross(v0, miga[2]->rbfv[0]->invec, miga[2]->rbfv[1]->invec);
|
| 409 |
|
|
normalize(v0);
|
| 410 |
|
|
fcross(v2, miga[1]->rbfv[0]->invec, miga[1]->rbfv[1]->invec);
|
| 411 |
|
|
normalize(v2);
|
| 412 |
|
|
fcross(v1, sivec, miga[1]->rbfv[1]->invec);
|
| 413 |
|
|
normalize(v1);
|
| 414 |
|
|
s = acos(DOT(v0,v1)) / acos(DOT(v0,v2));
|
| 415 |
|
|
geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec,
|
| 416 |
|
|
s, GEOD_REL);
|
| 417 |
|
|
t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec));
|
| 418 |
|
|
n = 0; /* count migrating particles */
|
| 419 |
|
|
for (i = 0; i < mtx_nrows(miga[0]); i++)
|
| 420 |
|
|
for (j = 0; j < mtx_ncols(miga[0]); j++)
|
| 421 |
|
|
for (k = (miga[0]->mtx[mtx_ndx(miga[0],i,j)] > FTINY) *
|
| 422 |
|
|
mtx_ncols(miga[2]); k--; )
|
| 423 |
|
|
n += (miga[2]->mtx[mtx_ndx(miga[2],i,k)] > FTINY &&
|
| 424 |
|
|
miga[1]->mtx[mtx_ndx(miga[1],j,k)] > FTINY);
|
| 425 |
|
|
#ifdef DEBUG
|
| 426 |
|
|
fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n",
|
| 427 |
|
|
miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf,
|
| 428 |
|
|
miga[2]->rbfv[1]->nrbf, n);
|
| 429 |
|
|
#endif
|
| 430 |
|
|
rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1));
|
| 431 |
|
|
if (rbf == NULL) {
|
| 432 |
|
|
fprintf(stderr, "%s: Out of memory in advect_rbf()\n", progname);
|
| 433 |
|
|
exit(1);
|
| 434 |
|
|
}
|
| 435 |
|
|
rbf->next = NULL; rbf->ejl = NULL;
|
| 436 |
|
|
VCOPY(rbf->invec, sivec);
|
| 437 |
|
|
rbf->nrbf = n;
|
| 438 |
|
|
n = 0; /* compute RBF lobes */
|
| 439 |
|
|
mbfact = s * miga[0]->rbfv[1]->vtotal/miga[0]->rbfv[0]->vtotal *
|
| 440 |
|
|
(1.-t + t*miga[1]->rbfv[1]->vtotal/miga[1]->rbfv[0]->vtotal);
|
| 441 |
|
|
mcfact = (1.-s) *
|
| 442 |
|
|
(1.-t + t*miga[2]->rbfv[1]->vtotal/miga[2]->rbfv[0]->vtotal);
|
| 443 |
|
|
for (i = 0; i < mtx_nrows(miga[0]); i++) {
|
| 444 |
|
|
const RBFVAL *rbf0i = &miga[0]->rbfv[0]->rbfa[i];
|
| 445 |
|
|
const float w0i = rbf0i->peak;
|
| 446 |
|
|
const double rad0i = R2ANG(rbf0i->crad);
|
| 447 |
|
|
ovec_from_pos(v0, rbf0i->gx, rbf0i->gy);
|
| 448 |
|
|
for (j = 0; j < mtx_ncols(miga[0]); j++) {
|
| 449 |
|
|
const float ma = miga[0]->mtx[mtx_ndx(miga[0],i,j)];
|
| 450 |
|
|
const RBFVAL *rbf1j;
|
| 451 |
|
|
double rad1j, srad2;
|
| 452 |
|
|
if (ma <= FTINY)
|
| 453 |
|
|
continue;
|
| 454 |
|
|
rbf1j = &miga[0]->rbfv[1]->rbfa[j];
|
| 455 |
|
|
rad1j = R2ANG(rbf1j->crad);
|
| 456 |
|
|
srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j;
|
| 457 |
|
|
ovec_from_pos(v1, rbf1j->gx, rbf1j->gy);
|
| 458 |
|
|
geodesic(v1, v0, v1, s, GEOD_REL);
|
| 459 |
|
|
for (k = 0; k < mtx_ncols(miga[2]); k++) {
|
| 460 |
|
|
float mb = miga[1]->mtx[mtx_ndx(miga[1],j,k)];
|
| 461 |
|
|
float mc = miga[2]->mtx[mtx_ndx(miga[2],i,k)];
|
| 462 |
|
|
const RBFVAL *rbf2k;
|
| 463 |
|
|
double rad2k;
|
| 464 |
|
|
FVECT vout;
|
| 465 |
|
|
int pos[2];
|
| 466 |
|
|
if ((mb <= FTINY) | (mc <= FTINY))
|
| 467 |
|
|
continue;
|
| 468 |
|
|
rbf2k = &miga[2]->rbfv[1]->rbfa[k];
|
| 469 |
|
|
rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact);
|
| 470 |
|
|
rad2k = R2ANG(rbf2k->crad);
|
| 471 |
|
|
rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k));
|
| 472 |
|
|
ovec_from_pos(v2, rbf2k->gx, rbf2k->gy);
|
| 473 |
|
|
geodesic(vout, v1, v2, t, GEOD_REL);
|
| 474 |
|
|
pos_from_vec(pos, vout);
|
| 475 |
|
|
rbf->rbfa[n].gx = pos[0];
|
| 476 |
|
|
rbf->rbfa[n].gy = pos[1];
|
| 477 |
|
|
++n;
|
| 478 |
|
|
}
|
| 479 |
|
|
}
|
| 480 |
|
|
}
|
| 481 |
|
|
rbf->vtotal = miga[0]->rbfv[0]->vtotal * (mbfact + mcfact);
|
| 482 |
|
|
rev_rbf_symmetry(rbf, sym);
|
| 483 |
|
|
return(rbf);
|
| 484 |
|
|
}
|