/* IO(1) */

load x9_10;

/*
 * two linear tracks which cross in the middle
 */

void get_x( int _K)
{
  int i, j;
  vector jj;
  matrix ppp;
  matrix tc;
  vector up;

  if( narg == 0) {

    LOADED=10;

    MISSED = 1;         /* if non-zero, add extra state(s) for missed target events */

    TEST = 1;           /* for testing */

    N = 3;		/* N data samples */
    MM = 4;		/* MM measurements per unit time */
    K = 1;		/* find K best completely unmerged paths */

    M = MM + MISSED;    /* M states */

    M_clutter = MM - 2; /* number of clutter points per unit time */


    if( MISSED)
      print( "Using missed target events.");
    else
      print( "Not using missed target events.");

    print( M_clutter);


/*** note: dt is global and is used in get_cost() below ***/

    dt = 4/(N-1);	/* time will go from 0 to 4 */

/*  x = 0 ... 4
 *  y = 0 ... 4
 */
    xy1 = dt * [0:(N-1); 0:(N-1)]';
     v1 = ones(N,2);

/*  x = 0 ... 4
 *  y = 4 ... 0
 */
    xy2 = dt * [0:(N-1); (N-1):0]';
     v2 = [ones(N,1), -ones(N,1)];

    if( MISSED) {

if(0) {
      xy1([6 11 18],:) = [1 0; 2 0; 3 4]; /* 4*rand(3,2); */
      xy2([8 13 18],:) = [1 4; 2 4; 3 0]; /* 4*rand(3,2); */
} else {
/* for testing with N=3 */
	xy1(2,:) = [10 10];
	xy2(2,:) = [10 -10];
}

      missed_cost_1 = log(16);
      missed_cost_MM = MM * missed_cost_1;
    }

if(TEST) { /*** testing with clutter as HUGE_VAL's ***/

    xyr = vr = HUGE_VAL * ones( M_clutter*N, 2);

} else {

    xyr = 4 * rand( M_clutter*N, 2);
     vr = 2 * rand( M_clutter*N, 2) - 1;
}

    xyvr = reshape( [xyr vr], N, 4*M_clutter);

    xv = reshape( [xy1 v1 xy2 v2 xyvr], MM*N, 4);

/* add some noise */

   Kn = 0.01;	/* measurement noise variance */

if(TEST)	/* testing... */
 Kn=0;

   xv += sqrt(Kn) * rand( rows(xv), cols(xv), "gauss");


/* randomize the observation rows */

if(0) {
    ppp = permutations(1:MM);

    for( i = 1; i <= N; ++i) {
      j = 1 + (i-1) * MM;
      jj = [j:j+MM-1];
      xv(jj) = xv( jj( ppp( rand(1,1,"int",MM))));
    }
}

    zstate = 0;

    z = zeros(1,N);	/* dummy */

    return;
  }

  /* narg > 0 */

  if( LOADED != 10) {
    print( "get_x: call get_x with no args first.");
    return;
  }

  if( _K > MM) {
    printf( "get_x: K > MM not allowed!");
    return;
  }

  K = _K;

  setup_combos(); /* setup global variables: perms, combos, unique_perms, MP, and M */

  zstate = 0;
}
