+/* NAME
+ * convertToImagefile Make image array from Phantom
+ *
+ * SYNOPSIS
+ * pic_to_imagefile (pic, im, nsample)
+ * Phantom& pic Phantom definitions
+ * ImageFile *im Computed pixel array
+ * int nsample Number of samples along each axis for each pixel
+ * (total samples per pixel = nsample * nsample)
+ */
+
+void
+Phantom::convertToImagefile (ImageFile& im, const int in_nsample, const int trace) const
+{
+ convertToImagefile (im, in_nsample, trace, 0, im.nx(), true);
+}
+
+void
+Phantom::convertToImagefile (ImageFile& im, const int in_nsample, const int trace, const int colStart, const int colCount, bool bStoreAtColumnPos) const
+{
+ int nx = im.nx();
+ int ny = im.ny();
+ if (nx < 2 || ny < 2)
+ return;
+
+ int nsample = in_nsample;
+ if (nsample < 1)
+ nsample = 1;
+
+ double dx = m_xmax - m_xmin;
+ double dy = m_ymax - m_ymin;
+ double xcent = m_xmin + dx / 2;
+ double ycent = m_ymin + dy / 2;
+ double phmlen = (dx > dy ? dx : dy);
+
+ double phmradius = phmlen / 2;
+
+ double xmin = xcent - phmradius;
+ double xmax = xcent + phmradius;
+ double ymin = ycent - phmradius;
+ double ymax = ycent + phmradius;
+
+ // Each pixel holds the average of the intensity of the cell with (ix,iy) at the center of the pixel
+ // Set major increments so that the last cell v[nx-1][ny-1] will start at xmax - xinc, ymax - yinc).
+ // Set minor increments so that sample points are centered in cell
+
+ double xinc = (xmax - xmin) / nx;
+ double yinc = (ymax - ymin) / ny;
+
+ double kxinc = xinc / nsample; /* interval between samples */
+ double kyinc = yinc / nsample;
+ double kxofs = kxinc / 2; /* offset of 1st point */
+ double kyofs = kyinc / 2;
+
+ im.setAxisExtent (xmin, xmax, ymin, ymax);
+ im.setAxisIncrement (xinc, yinc);
+
+ ImageFileArray v = im.getArray();
+
+ for (int ix = 0; ix < colCount; ix++)
+ for (int iy = 0; iy < ny; iy++) {
+ int iColStore = ix;
+ if (bStoreAtColumnPos)
+ iColStore += colStart;
+ v[iColStore][iy] = 0;
+ }
+
+ double x_start = xmin + (colStart * xinc);
+ for (PElemConstIterator pelem = m_listPElem.begin(); pelem != m_listPElem.end(); pelem++) {
+ const PhantomElement& rPElem = **pelem;
+ double x, y, xi, yi;
+ int ix, iy, kx, ky;
+ for (ix = 0, x = x_start; ix < colCount; ix++, x += xinc) {
+ int iColStore = ix;
+ if (bStoreAtColumnPos)
+ iColStore += colStart;
+ for (iy = 0, y = ymin; iy < ny; iy++, y += yinc) {
+ for (kx = 0, xi = x + kxofs; kx < nsample; kx++, xi += kxinc) {
+ for (ky = 0, yi = y + kyofs; ky < nsample; ky++, yi += kyinc)
+ if (rPElem.isPointInside (xi, yi, PHM_COORD) == TRUE)
+ v[iColStore][iy] += rPElem.atten();
+ } // for kx
+ } /* for iy */
+ } /* for ix */
+ } /* for pelem */
+
+
+ if (nsample > 1) {
+ double factor = 1.0 / static_cast<double>(nsample * nsample);
+
+\r
+ for (int ix = 0; ix < colCount; ix++) {
+ int iColStore = ix;\r
+ if (bStoreAtColumnPos)\r
+ iColStore += colStart;\r
+ for (int iy = 0; iy < ny; iy++)
+ v[iColStore][iy] *= factor;\r
+ }
+ }
+}
+