/*-------------------------------pixelmatrix.c----------------------------------------------------
The program generate system imaging matrix for one pixel by fast tracing
technique.
At certain view angle, any one pixel has non-zero contribution to projection
within a few bins.
  Syntax:
                        double *length, double *index: imaging matrix. (i.e
contribution of one particular pixel to each ray).
                        the memory needed has to be set up before calling this
function.
                        "length" give the intersection length, "index" gives the
location of the ray in singoram
                        *pixel: pixel's coordinate
                        double foc, ror, gamma, int bin: geometry
                        int view: number of projection
-----------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include "fpc.h"

void pixelmatrix(
    double *length, long *index,
    int *pixel, int nsize,
    double binsiz, int nbin, int nproj,
    double foc, double ror, int Nmax, double *cosbeta, double *sinbeta,
    double *P0, double *P1, double *SS)
{
  int n, j;
  double s;
  double alpha, dalpha;
  double dx, dy;
  int B, B1, B2;
  int Z;
  double p[2], q[2];
  double p0, p1, q0, q1; // unit vector
  double dd;
  double a;
  dalpha = binsiz;
  Z = 0;
  for (n = 0; n < nproj; n++)
  // for (n = 250; n < 251; n++)
  {
    // coordinates in image space;x-ray tube
    p[0] = P0[n];
    p[1] = P1[n];
    
    // identify the non-zero projection bins for this view
    dx = (pixel[0] - nsize * 0.5 + 0.5) - p[0]; // pixel[0] corresponding to x-axis location or column index
    dy = (pixel[1] - nsize * 0.5 + 0.5) - p[1]; // pixel[1] corresponding to y-axis location or row index
    dd = sqrt(dx * dx + dy * dy);
    q0 = dx / dd;
    q1 = dy / dd;
    dd = sqrt(p[0] * p[0] + p[1] * p[1]);
    p0 = -p[0] / dd;
    p1 = -p[1] / dd;
    // alpha = asin((q1*p0-q0*p1)/(p0*p0+p1*p1));
    alpha = asin((q1 * p0 - q0 * p1));
    // alpha is supposed to be within (-PI/2, PI/2).
    B = floor(nbin / 2 + (alpha / dalpha) - 0.5); // for equi-angular only, for equi-spatial need modification
    B1 = B - Nmax;
    B2 = B + 1 + Nmax;
    if (B1 < 0)
      B1 = 0;
    if (B2 > nbin - 1)
      B2 = nbin - 1;
    
    // coordinates in single pixel space;the coordinate of the x-ray tube
    // p[0]	+= ( nsize/2 - pixel[0] );
    // p[1]	+= ( -nsize/2 + pixel[1] + 1. );
    p[0] += nsize / 2 - pixel[0];
    p[1] += nsize / 2 - pixel[1];
    
    // equal-angle
    for (j = B1; j <= B2; j++)
    {
      s = SS[j];
      // coordinates in single pixel space;the coordinate of the detector
      // q[0] =  ror*sinbeta[n] + s*cosbeta[n] + nsize/2 - pixel[0];
      // q[1] = -ror*cosbeta[n] + s*sinbeta[n] - nsize/2 + pixel[1] + 1;
      q[0] = ror * sinbeta[n] + s * cosbeta[n] + nsize / 2 - pixel[0];
      q[1] = -ror * cosbeta[n] + s * sinbeta[n] + nsize / 2 - pixel[1];
      a = fpc2d(p, q);
      // printf("{%d,%d} : {%f,%f} -> {%f,%f} : %f\n", n, j, p[0],p[1], q[0],q[1],a);
      // if (a>0) printf("{%d,%d} : Z: %d, a: %f\n", n, j, Z, a);
      if (a)
      {
        index[Z] = j + n * nbin;
        length[Z] = a;
        Z++;
      }
    }
  }
}
