+
+/* 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, double dViewRatio, const int in_nsample, const int trace) const
+{
+ convertToImagefile (im, dViewRatio, in_nsample, trace, 0, im.nx(), true);
+}
+
+void
+Phantom::convertToImagefile (ImageFile& im, const double dViewRatio, const int in_nsample, const int trace,
+ const int colStart, const int colCount, bool bStoreAtColumnPos) const
+{
+ int iStorageOffset = (bStoreAtColumnPos ? colStart : 0);
+ convertToImagefile (im, im.nx(), dViewRatio, in_nsample, trace, colStart, colCount, iStorageOffset);
+}
+
+void
+Phantom::convertToImagefile (ImageFile& im, const int iTotalRasterCols, const double dViewRatio,
+ const int in_nsample, const int trace, const int colStart, const int colCount, int iStorageOffset) const
+{
+ const int nx = im.nx();
+ const 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 dHalflen = dViewRatio * (getDiameterBoundaryCircle() / SQRT2 / 2);
+
+ double xmin = xcent - dHalflen;
+ double xmax = xcent + dHalflen;
+ double ymin = ycent - dHalflen;
+ double ymax = ycent + dHalflen;
+
+ // 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) / (iTotalRasterCols);
+ 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++) {
+ int iColStore = ix + iStorageOffset;
+ ImageFileColumn vCol = v[iColStore];
+ for (int iy = 0; iy < ny; iy++)
+ *vCol++ = 0;
+ }
+
+#if HAVE_OPENMP
+ double x_start = xmin + (colStart * xinc);
+ for (PElemConstIterator pelem = m_listPElem.begin(); pelem != m_listPElem.end(); pelem++) {
+ const PhantomElement& rPElem = **pelem;
+ #pragma omp parallel for
+ for (int ix = 0; ix < colCount; ix++) {
+ double x = x_start + ix * xinc;
+ int iColStore = ix + iStorageOffset;
+ ImageFileColumn vCol = v[iColStore];
+ for (int iy = 0; iy < ny; iy++) {
+ double y = ymin + iy * yinc;
+ double dAtten = 0;
+ for (int kx = 0; kx < nsample; kx++) {
+ double xi = x + kxofs + kxinc * kx;
+ for (int ky = 0; ky < nsample; ky++) {
+ double yi = y + kyofs + ky * kyinc;
+ if (rPElem.isPointInside (xi, yi, PHM_COORD))
+ dAtten += rPElem.atten();
+ } // ky
+ } // kx
+ *vCol++ += dAtten;
+ } /* iy */
+ } /* ix */
+ } /* pelem */
+
+#else
+
+ 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 + iStorageOffset;
+ ImageFileColumn vCol = v[iColStore];
+ for (iy = 0, y = ymin; iy < ny; iy++, y += yinc) {
+ double dAtten = 0;
+ 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))
+ dAtten += rPElem.atten();
+ } // for kx
+ *vCol++ += dAtten;
+ } /* for iy */
+ } /* for ix */
+ } /* for pelem */
+#endif
+
+ if (nsample > 1) {
+ double factor = 1.0 / static_cast<double>(nsample * nsample);
+#if HAVE_OPENMP
+ #pragma omp parallel for
+#endif
+ for (int ix = 0; ix < colCount; ix++) {
+ int iColStore = ix + iStorageOffset;
+ ImageFileColumn vCol = v[iColStore];
+ for (int iy = 0; iy < ny; iy++)
+ *vCol++ *= factor;
+ }
+ }
+}
+