/* -- translated by f2c (version 20000121). */


#include <extUtil.h>

/*  nearNodes(po, n, p, node, k, flag) */
/*     determines the k closest nodes out of n with coordinates in */
/*     po(3,n) to the point with coordinates in p(3); flag determines */
/*     whether the subroutine has already been called with the same */
/*     po(3,n): 0:no; 1:yes ; */



/* Subroutine */ int quickx_(bb, ixbb, imax)
double *bb;
int *ixbb, *imax;
{
    static int ixhb;
    static double x, hb;
    static int ii, jj, is, ibb, ili, ire;
    static int ist[62]	/* was [31][2] */;



    /* Parameter adjustments */
    --ixbb;
    --bb;

    /* Function Body */
    is = 1;
/* ...  LINKS */
    ist[0] = 1;
/* ...  RECHTS */
    ist[31] = (int) (*imax);

L10:
    ili = ist[is - 1];
    ire = ist[is + 30];
    --is;

L20:
    ii = ili;
    jj = ire;
    ibb = (ili + ire) / 2;
    x = bb[ibb];

L30:
/*              .GT. WENN ABSTEIGEND SORTIERT */
    if (bb[ii] < x) {
	++ii;
	goto L30;
    }

L40:
/*              .LT. WENN ABSTEIGEND SORTIERT */
    if (bb[jj] > x) {
	--jj;
	goto L40;
    }

    if (ii <= jj) {
	hb = bb[ii];
	bb[ii] = bb[jj];
	bb[jj] = hb;
	ixhb = ixbb[ii];
	ixbb[ii] = ixbb[jj];
	ixbb[jj] = ixhb;
	++ii;
	--jj;
    }

    if (ii <= jj) {
	goto L30;
    }

    if (ii < ire) {
	++is;
	ist[is - 1] = (int) ii;
	ist[is + 30] = (int) ire;
    }

    ire = jj;

    if (ili < ire) {
	goto L20;
    }

    if (is > 0) {
	goto L10;
    }

    return 0;
} /* quickx_ */





/* Subroutine */ int ident_(x, px, n, id)
double *x, *px;
int *n, *id;
{
    static int i__, m, n1, n2;

    /* Parameter adjustments */
    --x;

    /* Function Body */
    n1 = 0;
    n2 = *n + 1;
    for (i__ = 1; i__ <= 5000; ++i__) {
	m = (n2 + n1) / 2;
	if (*px >= x[m]) {
	    n1 = m;
	}
	if (*px < x[m]) {
	    n2 = m;
	}
	if (n2 - n1 == 1) {
	    goto L2;
	}
/* L1: */
    }
L2:
    *id = n1;
    return 0;
} /* ident_ */



/* Subroutine */ int nearNodes(po, n, p, node, k, flag)
double *po;
int *n;
double *p;
int *node, *k, *flag;
{
    /* System generated locals */
    int i__1, i__2;
    double d__1;

    /* Local variables */
    static int i__, j, m;
    static double aanod, aamin, aasod, aanwd, aaswd;
    static double aanou, aasou, aanwu, aaswu;
    static int nboundary, it, ks, iz;
    static double rr;
    static double xp, yp, zp, xr, yr, zr, xx, yy, zz;
    static double aad, aan, aao, aas, aau, aaw;
    static int idx, idy, idz;

    static double *r__=NULL,*x=NULL, *y=NULL, *z=NULL, *xo=NULL, *yo=NULL, *zo=NULL;
    static int  *ni=NULL, *nx=NULL, *ny=NULL, *nz=NULL;


    /* Parameter adjustments */
    --node;
    --p;
    po -= 4;

    if ( (r__ = (double *)realloc((double *)r__, (*k + 7) * sizeof(double))) == NULL )
       printf("\n\n ERROR: realloc failed r__\n\n") ;
    if ( (ni = (int *)realloc((int *)ni , (*k + 7) * sizeof(int))) == NULL )
       printf("\n\n ERROR: realloc failed ni\n\n") ;

    if (*flag == 0) {
      if ( (x = (double *)realloc((double *)x, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed x\n\n") ;
      if ( (y = (double *)realloc((double *)y, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed y\n\n") ;
      if ( (z = (double *)realloc((double *)z, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed z\n\n") ;

      if ( (xo = (double *)realloc((double *)xo, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed xo\n\n") ;
      if ( (yo = (double *)realloc((double *)yo, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed yo\n\n") ;
      if ( (zo = (double *)realloc((double *)zo, (*n + 2) * sizeof(double))) == NULL )
        printf("\n\n ERROR: realloc failed zo\n\n") ;

      if ( (nx = (int *)realloc((int *)nx , (*n + 2) * sizeof(int))) == NULL )
        printf("\n\n ERROR: realloc failed nx \n\n") ;
      if ( (ny = (int *)realloc((int *)ny , (*n + 2) * sizeof(int))) == NULL )
        printf("\n\n ERROR: realloc failed ny \n\n") ;
      if ( (nz = (int *)realloc((int *)nz , (*n + 2) * sizeof(int))) == NULL )
        printf("\n\n ERROR: realloc failed nz \n\n") ;

	i__1 = *n;
	for (i__ = 1; i__ <= i__1; ++i__) {
	    nx[i__ - 1] = i__;
	    ny[i__ - 1] = i__;
	    nz[i__ - 1] = i__;
	    x[i__ - 1] = po[i__ * 3 + 1];
	    y[i__ - 1] = po[i__ * 3 + 2];
	    z[i__ - 1] = po[i__ * 3 + 3];
	    xo[i__ - 1] = x[i__ - 1];
	    yo[i__ - 1] = y[i__ - 1];
	    zo[i__ - 1] = z[i__ - 1];
	}

	quickx_(x, nx, n);
	quickx_(y, ny, n);
	quickx_(z, nz, n);

    }

    xp = p[1];
    yp = p[2];
    zp = p[3];

    ident_(x, &xp, n, &idx);
    ident_(y, &yp, n, &idy);
    ident_(z, &zp, n, &idz);

    ks = 0;

/*     DETERMINATION OF THE k NEAREST POINTS */

    i__1 = *k;
    for (i__ = 1; i__ <= i__1; ++i__) {
	xr = xo[i__ - 1] - xp;
	yr = yo[i__ - 1] - yp;
	zr = zo[i__ - 1] - zp;
	r__[i__ - 1] = sqrt(xr * xr + yr * yr + zr * zr);
	ni[i__ - 1] = i__;
    }

    quickx_(r__, ni, k);

    i__1 = *n;
    for (i__ = 1; i__ <= i__1; ++i__) {

	iz = 0;

/*     WEST */

	m = idx - i__ + 1;
	if (m <= 0) {
	    aaw = r__[*k - 1];
	    goto L4;
	}

	xx = xo[nx[m - 1] - 1];
	yy = yo[nx[m - 1] - 1];
	zz = zo[nx[m - 1] - 1];
	aaw = xx - xp;
	yr = yy - yp;
	zr = zz - zp;
	rr = sqrt(aaw * aaw + yr * yr + zr * zr);

	if (rr >= r__[*k - 1]) {
	    goto L4;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (nx[m - 1] == ni[j - 1]) {
		goto L4;
	    }
/* L8: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = nx[m - 1];

/*     EAST */

L4:
	m = idx + i__;
	if (m > *n) {
	    aao = r__[*k - 1];
	    goto L6;
	}

	xx = xo[nx[m - 1] - 1];
	yy = yo[nx[m - 1] - 1];
	zz = zo[nx[m - 1] - 1];
	aao = xx - xp;
	yr = yy - yp;
	zr = zz - zp;
	rr = sqrt(aao * aao + yr * yr + zr * zr);

	if (rr >= r__[*k - 1]) {
	    goto L6;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (nx[m - 1] == ni[j - 1]) {
		goto L6;
	    }
/* L10: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = nx[m - 1];

/*     SOUTH */

L6:
	m = idy - i__ + 1;
	if (m <= 0) {
	    aas = r__[*k - 1];
	    goto L5;
	}

	xx = xo[ny[m - 1] - 1];
	yy = yo[ny[m - 1] - 1];
	zz = zo[ny[m - 1] - 1];
	xr = xx - xp;
	aas = yy - yp;
	zr = zz - zp;
	rr = sqrt(xr * xr + aas * aas + zr * zr);

	if (rr >= r__[*k - 1]) {
	    goto L5;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (ny[m - 1] == ni[j - 1]) {
		goto L5;
	    }
/* L9: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = ny[m - 1];

/*     NORTH */

L5:
	m = idy + i__;
	if (m > *n) {
	    aan = r__[*k - 1];
	    goto L7;
	}

	xx = xo[ny[m - 1] - 1];
	yy = yo[ny[m - 1] - 1];
	zz = zo[ny[m - 1] - 1];
	xr = xx - xp;
	aan = yy - yp;
	zr = zz - zp;
	rr = sqrt(xr * xr + aan * aan + zr * zr);

	if (rr >= r__[*k - 1]) {
	    goto L7;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (ny[m - 1] == ni[j - 1]) {
		goto L7;
	    }
/* L11: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = ny[m - 1];

/*     up */

L7:
	m = idz - i__ + 1;
	if (m <= 0) {
	    aau = r__[*k - 1];
	    goto L20;
	}

	xx = xo[nz[m - 1] - 1];
	yy = yo[nz[m - 1] - 1];
	zz = zo[nz[m - 1] - 1];
	xr = xx - xp;
	yr = yy - yp;
	aau = zz - zp;
	rr = sqrt(xr * xr + yr * yr + aau * aau);

	if (rr >= r__[*k - 1]) {
	    goto L20;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (nz[m - 1] == ni[j - 1]) {
		goto L20;
	    }
/* L21: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = nz[m - 1];

/*     down */

L20:
	m = idz + i__;
	if (m > *n) {
	    aad = r__[*k - 1];
	    goto L22;
	}

	xx = xo[nz[m - 1] - 1];
	yy = yo[nz[m - 1] - 1];
	zz = zo[nz[m - 1] - 1];
	xr = xx - xp;
	yr = yy - yp;
	aad = zz - zp;
	rr = sqrt(xr * xr + yr * yr + aad * aad);

	if (rr >= r__[*k - 1]) {
	    goto L22;
	}
	it = iz + *k;
	i__2 = it;
	for (j = 1; j <= i__2; ++j) {
	    if (nz[m - 1] == ni[j - 1]) {
		goto L22;
	    }
/* L23: */
	}
	++iz;
	r__[iz + *k - 1] = rr;
	ni[iz + *k - 1] = nz[m - 1];

L22:
	aanod = sqrt(aan * aan + aao * aao + aad * aad);
	aanou = sqrt(aan * aan + aao * aao + aau * aau);
	aasod = sqrt(aas * aas + aao * aao + aad * aad);
	aasou = sqrt(aas * aas + aao * aao + aau * aau);
	aaswd = sqrt(aas * aas + aaw * aaw + aad * aad);
	aaswu = sqrt(aas * aas + aaw * aaw + aau * aau);
	aanwd = sqrt(aan * aan + aaw * aaw + aad * aad);
	aanwu = sqrt(aan * aan + aaw * aaw + aau * aau);
/* Computing MIN */
	d__1 = smin(aanod,aanou), d__1 = smin(d__1,aasod), d__1 = smin(d__1,
		aasou), d__1 = smin(d__1,aaswd), d__1 = smin(d__1,aaswu), d__1 =
		 smin(d__1,aanwd);
	aamin = smin(d__1,aanwu);

	if (iz != 0) {
	    iz += *k;
	    quickx_(r__, ni, &iz);
	}

	nboundary = ks;
	i__2 = *k;
	for (j = nboundary + 1; j <= i__2; ++j) {
	    if (r__[j - 1] <= aamin) {
		ks = j;
		if (ks == *k) {
		    goto L99;
		}
	    } else {
		goto L88;
	    }
	}

L88:
	;
    }
L99:

    i__1 = *k;
    for (i__ = 1; i__ <= i__1; ++i__) {
	node[i__] = ni[i__ - 1]-1;
    }

    return(0);
} /* near_3d__ */


