1 /*****************************************************************************
5 ** Purpose: Routines for phantom objects
6 ** Progammer: Kevin Rosenberg
7 ** Date Started: Aug 1984
9 ** This is part of the CTSim program
10 ** Copyright (c) 1983-2001 Kevin Rosenberg
12 ** $Id: phantom.cpp,v 1.32 2002/05/28 18:43:16 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
30 const int PhantomElement::POINTS_PER_CIRCLE = 360;
31 const double PhantomElement::SCALE_PELEM_EXTENT=0.000; // increase pelem limits by 0.5%
32 //const double PhantomElement::SCALE_PELEM_EXTENT=0.005; // increase pelem limits by 0.5%
34 const int Phantom::PHM_INVALID = -1;
35 const int Phantom::PHM_HERMAN = 0;
36 const int Phantom::PHM_SHEPP_LOGAN = 1;
37 const int Phantom::PHM_UNITPULSE = 2;
39 const char* Phantom::s_aszPhantomName[] =
46 const char* Phantom::s_aszPhantomTitle[] =
53 const int Phantom::s_iPhantomCount = sizeof(s_aszPhantomName) / sizeof(const char*);
56 // CLASS IDENTIFICATION
66 Phantom::Phantom (const char* const phmName)
69 createFromPhantom (phmName);
80 m_composition = P_PELEMS;
87 for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
94 Phantom::convertPhantomIDToName (int phmID)
96 static const char *name = "";
98 if (phmID >= 0 && phmID < s_iPhantomCount)
99 return (s_aszPhantomName[phmID]);
105 Phantom::convertPhantomIDToTitle (int phmID)
107 static const char *title = "";
109 if (phmID >= 0 && phmID < s_iPhantomCount)
110 return (s_aszPhantomName[phmID]);
116 Phantom::convertNameToPhantomID (const char* const phmName)
118 int id = PHM_INVALID;
120 for (int i = 0; i < s_iPhantomCount; i++)
121 if (strcasecmp (phmName, s_aszPhantomName[i]) == 0) {
131 Phantom::createFromPhantom (const char* const phmName)
133 int phmid = convertNameToPhantomID (phmName);
134 if (phmid == PHM_INVALID) {
136 m_failMessage = "Invalid phantom name ";
137 m_failMessage += phmName;
142 createFromPhantom (phmid);
147 Phantom::createFromPhantom (const int phmid)
154 case PHM_SHEPP_LOGAN:
158 m_composition = P_UNIT_PULSE;
159 addPElem ("rectangle", 0., 0., 100., 100., 0., 0.); // outline
160 addPElem ("ellipse", 0., 0., 1., 1., 0., 1.); // pulse
164 m_failMessage = "Illegal phantom id ";
165 m_failMessage += phmid;
175 /* METHOD IDENTIFICATION
176 * createFromFile Add PhantomElements from file
179 * createFromFile (filename)
182 * true if pelem were added
183 * false if an pelem not added
187 Phantom::createFromFile (const char* const fname)
189 bool bGoodFile = true;
192 if ((fp = fopen (fname, "r")) == NULL)
198 double cx, cy, u, v, rot, dens;
201 int status = fscanf (fp, "%79s %lf %lf %lf %lf %lf %lf", pelemtype, &cx, &cy, &u, &v, &rot, &dens);
203 if (status == static_cast<int>(EOF))
205 else if (status != 7) {
206 sys_error (ERR_WARNING, "Insufficient fields reading phantom file %s [Phantom::createFromFile]", fname);
209 addPElem (pelemtype, cx, cy, u, v, rot, dens);
218 Phantom::fileWrite (const char* const fname)
220 fstream file (fname, std::ios::out);
223 printDefinitions (file);
224 return ! file.fail();
231 * addPElem (type, cx, cy, u, v, rot, atten)
232 * char *type type of pelem (box, ellipse, etc)
233 * double cx, cy pelem center
234 * double u,v pelem size
235 * double rot rotation angle of pelem (in degrees)
236 * double atten x-ray attenuation cooefficient
240 Phantom::addPElem (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
242 PhantomElement *pelem = new PhantomElement (type, cx, cy, u, v, rot, atten);
244 m_listPElem.push_front (pelem);
246 // update phantom limits
247 if (m_xmin > pelem->xmin()) m_xmin = pelem->xmin();
248 if (m_xmax < pelem->xmax()) m_xmax = pelem->xmax();
249 if (m_ymin > pelem->ymin()) m_ymin = pelem->ymin();
250 if (m_ymax < pelem->ymax()) m_ymax = pelem->ymax();
256 /*----------------------------------------------------------------------*/
257 /* Input-Output Routines */
258 /*----------------------------------------------------------------------*/
262 * print Print vertices of Phantom pelems
269 Phantom::print (std::ostream& os) const
271 os << "Number of PElements: " << m_nPElem << "\n";
272 os << "Limits: xmin=" << m_xmin << ", ymin=" << m_ymin << ", xmax=" << m_xmax << ", ymax=" << m_ymax << "\n";
274 for (PElemConstIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
275 const PhantomElement& rPE = **i;
276 os << "PhantomElement: nPoints=" << rPE.nOutlinePoints();
277 os << ", atten=" << rPE.atten() << " rot=" << convertRadiansToDegrees (rPE.rot()) << "\n";
278 os << "xmin=" << rPE.xmin() << ", ymin=" << rPE.ymin() << ", xmax=" << rPE.xmax() << ", ymax=" << rPE.ymax() << "\n";
281 for (int i = 0; i < rPE.nOutlinePoints(); i++)
282 os << rPE.xOutline()[i] << "," << rPE.yOutline()[i] << "\n";
286 Phantom::print (std::ostringstream& os) const
288 os << "Number of PElements: " << m_nPElem << "\n";
289 os << "Limits: xmin=" << m_xmin << ", ymin=" << m_ymin << ", xmax=" << m_xmax << ", ymax=" << m_ymax << "\n";
291 for (PElemConstIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
292 const PhantomElement& rPE = **i;
293 os << "PhantomElement: nPoints=" << rPE.nOutlinePoints();
294 os << ", atten=" << rPE.atten() << " rot=" << convertRadiansToDegrees (rPE.rot()) << "\n";
295 os << "xmin=" << rPE.xmin() << ", ymin=" << rPE.ymin() << ", xmax=" << rPE.xmax() << ", ymax=" << rPE.ymax() << "\n";
298 for (int i = 0; i < rPE.nOutlinePoints(); i++)
299 os << rPE.xOutline()[i] << "," << rPE.yOutline()[i] << "\n";
304 Phantom::printDefinitions (std::ostream& os) const
306 for (PElemConstIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
307 const PhantomElement& rPE = **i;
308 rPE.printDefinition (os);
313 Phantom::printDefinitions (std::ostringstream& os) const
315 for (PElemConstIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
316 const PhantomElement& rPE = **i;
317 rPE.printDefinition (os);
323 * show Show vector outline of Phantom to user
331 Phantom::show () const
333 SGPDriver driverSGP ("Phantom Show");
338 std::cout << "Press return to continue";
343 Phantom::show (SGP& sgp) const
345 double wsize = m_xmax - m_xmin;
346 if ((m_ymax - m_ymin) > wsize)
347 wsize = m_ymax - m_ymin;
349 double halfWindow = wsize / 2;
351 double xcent = m_xmin + (m_xmax - m_xmin) / 2;
352 double ycent = m_ymin + (m_ymax - m_ymin) / 2;
354 sgp.setWindow (xcent - halfWindow, ycent - halfWindow, xcent + halfWindow, ycent + halfWindow);
362 * draw Draw vector outline of Phantom
370 Phantom::draw (SGP& sgp) const
372 for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++)
373 sgp.polylineAbs ((*i)->xOutline(), (*i)->yOutline(), (*i)->nOutlinePoints());
379 * addStdSheppLogan Make head phantom of Shepp-Logan
382 * S. W. Rowland, "Computer Implementation of Image Reconstruction
383 * Formulas", in "Image Reconstruction from Projections: Implementation
384 * and Applications", edited by G. T. Herman, 1978.
388 Phantom::addStdSheppLogan ()
390 addPElem ("ellipse", 0.0000, 0.0000, 0.6900, 0.9200, 0.0, 1.00);
391 addPElem ("ellipse", 0.0000, -0.0184, 0.6624, 0.8740, 0.0, -0.98);
392 addPElem ("ellipse", 0.2200, 0.0000, 0.1100, 0.3100, -18.0, -0.02);
393 addPElem ("ellipse", -0.2200, 0.0000, 0.1600, 0.4100, 18.0, -0.02);
394 addPElem ("ellipse", 0.0000, 0.3500, 0.2100, 0.2500, 0.0, 0.01);
395 addPElem ("ellipse", 0.0000, 0.1000, 0.0460, 0.0460, 0.0, 0.01);
396 addPElem ("ellipse", 0.0000, -0.1000, 0.0460, 0.0460, 0.0, 0.01);
397 addPElem ("ellipse", -0.0800, -0.6050, 0.0460, 0.0230, 0.0, 0.01);
398 addPElem ("ellipse", 0.0000, -0.6050, 0.0230, 0.0230, 0.0, 0.01);
399 addPElem ("ellipse", 0.0600, -0.6050, 0.0230, 0.0230, 0.0, 0.01);
400 addPElem ("ellipse", 0.5538, -0.3858, 0.0330, 0.2060, -18.0, 0.03);
405 * addStdHerman Standard head phantom of G. T. Herman
408 * G. T. Herman, "Image Reconstructions from Projections: The Fundementals
409 * of Computed Tomography", 1979.
413 Phantom::addStdHerman ()
415 addPElem ("ellipse", 0.000, 1.50, 0.375, 0.3000, 90.00, -0.003);
416 addPElem ("ellipse", 0.675, -0.75, 0.225, 0.1500, 140.00, 0.010);
417 addPElem ("ellipse", 0.750, 1.50, 0.375, 0.2250, 50.00, 0.003);
418 addPElem ("segment", 1.375, -7.50, 1.100, 0.6250, 19.20, -0.204);
419 addPElem ("segment", 1.375, -7.50, 1.100, 4.3200, 19.21, 0.204);
420 addPElem ("segment", 0.000, -2.25, 1.125, 0.3750, 0.00, -0.003);
421 addPElem ("segment", 0.000, -2.25, 1.125, 3.0000, 0.00, 0.003);
422 addPElem ("segment", -1.000, 3.75, 1.000, 0.5000, 135.00, -0.003);
423 addPElem ("segment", -1.000, 3.75, 1.000, 3.0000, 135.00, 0.003);
424 addPElem ("segment", 1.000, 3.75, 1.000, 0.5000, 225.00, -0.003);
425 addPElem ("segment", 1.000, 3.75, 1.000, 3.0000, 225.00, 0.003);
426 addPElem ("triangle", 5.025, 3.75, 1.125, 0.5000, 110.75, 0.206);
427 addPElem ("triangle",-5.025, 3.75, 1.125, 0.9000,-110.75, 0.206);
428 addPElem ("ellipse", 0.000, 0.00, 8.625, 6.4687, 90.00, 0.416);
429 addPElem ("ellipse", 0.000, 0.00, 7.875, 5.7187, 90.00, -0.206);
435 * convertToImagefile Make image array from Phantom
438 * pic_to_imagefile (pic, im, nsample)
439 * Phantom& pic Phantom definitions
440 * ImageFile *im Computed pixel array
441 * int nsample Number of samples along each axis for each pixel
442 * (total samples per pixel = nsample * nsample)
446 Phantom::convertToImagefile (ImageFile& im, double dViewRatio, const int in_nsample, const int trace) const
448 convertToImagefile (im, dViewRatio, in_nsample, trace, 0, im.nx(), true);
452 Phantom::convertToImagefile (ImageFile& im, const double dViewRatio, const int in_nsample, const int trace,
453 const int colStart, const int colCount, bool bStoreAtColumnPos) const
455 int iStorageOffset = (bStoreAtColumnPos ? colStart : 0);
456 convertToImagefile (im, im.nx(), dViewRatio, in_nsample, trace, colStart, colCount, iStorageOffset);
460 Phantom::convertToImagefile (ImageFile& im, const int iTotalRasterCols, const double dViewRatio,
461 const int in_nsample, const int trace, const int colStart, const int colCount, int iStorageOffset) const
463 const int nx = im.nx();
464 const int ny = im.ny();
465 if (nx < 2 || ny < 2)
468 int nsample = in_nsample;
472 double dx = m_xmax - m_xmin;
473 double dy = m_ymax - m_ymin;
474 double xcent = m_xmin + dx / 2;
475 double ycent = m_ymin + dy / 2;
476 double dHalflen = dViewRatio * (getDiameterBoundaryCircle() / SQRT2 / 2);
478 double xmin = xcent - dHalflen;
479 double xmax = xcent + dHalflen;
480 double ymin = ycent - dHalflen;
481 double ymax = ycent + dHalflen;
483 // Each pixel holds the average of the intensity of the cell with (ix,iy) at the center of the pixel
484 // Set major increments so that the last cell v[nx-1][ny-1] will start at xmax - xinc, ymax - yinc).
485 // Set minor increments so that sample points are centered in cell
487 double xinc = (xmax - xmin) / (iTotalRasterCols);
488 double yinc = (ymax - ymin) / ny;
490 double kxinc = xinc / nsample; /* interval between samples */
491 double kyinc = yinc / nsample;
492 double kxofs = kxinc / 2; /* offset of 1st point */
493 double kyofs = kyinc / 2;
495 im.setAxisExtent (xmin, xmax, ymin, ymax);
496 im.setAxisIncrement (xinc, yinc);
498 ImageFileArray v = im.getArray();
500 for (int ix = 0; ix < colCount; ix++) {
501 int iColStore = ix + iStorageOffset;
502 ImageFileColumn vCol = v[iColStore];
503 for (int iy = 0; iy < ny; iy++)
507 double x_start = xmin + (colStart * xinc);
508 for (PElemConstIterator pelem = m_listPElem.begin(); pelem != m_listPElem.end(); pelem++) {
509 const PhantomElement& rPElem = **pelem;
512 for (ix = 0, x = x_start; ix < colCount; ix++, x += xinc) {
513 int iColStore = ix + iStorageOffset;
514 ImageFileColumn vCol = v[iColStore];
515 for (iy = 0, y = ymin; iy < ny; iy++, y += yinc) {
517 for (kx = 0, xi = x + kxofs; kx < nsample; kx++, xi += kxinc) {
518 for (ky = 0, yi = y + kyofs; ky < nsample; ky++, yi += kyinc)
519 if (rPElem.isPointInside (xi, yi, PHM_COORD))
520 dAtten += rPElem.atten();
529 double factor = 1.0 / static_cast<double>(nsample * nsample);
532 for (int ix = 0; ix < colCount; ix++) {
533 int iColStore = ix + iStorageOffset;
534 ImageFileColumn vCol = v[iColStore];
535 for (int iy = 0; iy < ny; iy++)
541 ////////////////////////////////////////////////////////////////////////////////////////////////////////
542 // CLASS IDENTIFICATION
548 ////////////////////////////////////////////////////////////////////////////////////////////////////////
551 PhantomElement::PhantomElement (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
552 : m_cx(cx), m_cy(cy), m_u(u), m_v(v), m_atten(atten), m_nPoints(0), m_xOutline(0), m_yOutline(0)
554 m_rot = convertDegreesToRadians (rot); // convert angle to radians
556 m_type = convertNameToType (type);
558 makeTransformMatrices (); // calc transform matrices between phantom and normalized phantomelement
559 makeVectorOutline (); // calculate vector outline of pelem
561 m_rectLimits[0] = m_xmin; m_rectLimits[1] = m_ymin;
562 m_rectLimits[2] = m_xmax; m_rectLimits[3] = m_ymax;
567 PhantomElement::~PhantomElement ()
574 PhantomElement::printDefinition (std::ostream& os) const
576 os << convertTypeToName (m_type) << " " << m_cx << " " << m_cy << " " << m_u << " "
577 << m_v << " " << convertRadiansToDegrees (m_rot) << " " << m_atten << "\n";
581 PhantomElement::printDefinition (std::ostringstream& os) const
583 os << convertTypeToName (m_type) << " " << m_cx << " " << m_cy << " " << m_u << " "
584 << m_v << " " << convertRadiansToDegrees (m_rot) << " " << m_atten << "\n";
588 PhantomElement::convertNameToType (const char* const typeName)
590 PhmElemType type = PELEM_INVALID;
592 if (strcasecmp (typeName, "rectangle") == 0)
593 type = PELEM_RECTANGLE;
594 else if (strcasecmp (typeName, "triangle") == 0)
595 type = PELEM_TRIANGLE;
596 else if (strcasecmp (typeName, "ellipse") == 0)
597 type = PELEM_ELLIPSE;
598 else if (strcasecmp (typeName, "sector") == 0)
600 else if (strcasecmp (typeName, "segment") == 0)
601 type = PELEM_SEGMENT;
603 sys_error (ERR_WARNING, "Unknown PhantomElement type %s [PhantomElement::PhantomElement]", type);
609 PhantomElement::convertTypeToName (PhmElemType iType)
611 static char* pszType = "Unknown";
613 if (iType == PELEM_RECTANGLE)
614 pszType = "rectangle";
615 else if (iType == PELEM_TRIANGLE)
616 pszType = "triangle";
617 else if (iType == PELEM_ELLIPSE)
619 else if (iType == PELEM_SECTOR)
621 else if (iType == PELEM_SEGMENT)
629 PhantomElement::makeTransformMatrices ()
633 // To map normalized Pelem coords to world Phantom
636 // translate by (cx, cy)
638 scale_mtx2 (m_xformObjToPhm, m_u, m_v);
639 rot_mtx2 (temp, m_rot);
640 mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
641 xlat_mtx2 (temp, m_cx, m_cy);
642 mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
644 // to map world Phantom coodinates to normalized PElem coords
645 // translate by (-cx, -cy)
647 // scale by (1/u, 1/v)
649 xlat_mtx2 (m_xformPhmToObj, -m_cx, -m_cy);
650 rot_mtx2 (temp, -m_rot);
651 mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
652 scale_mtx2 (temp, 1 / m_u, 1 / m_v);
653 mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
658 * pelem_make_points INTERNAL routine to calculate point array for an pelem
661 * makepelempts (pelem)
662 * PELEM *pelem pelem whose points we are calculating
665 * Called by phm_add_pelem()
669 PhantomElement::makeVectorOutline ()
671 double radius, theta, start, stop;
677 case PELEM_RECTANGLE:
679 m_xOutline = new double [m_nPoints];
680 m_yOutline = new double [m_nPoints];
681 m_xOutline[0] =-m_u; m_yOutline[0] =-m_v;
682 m_xOutline[1] = m_u; m_yOutline[1] =-m_v;
683 m_xOutline[2] = m_u; m_yOutline[2] = m_v;
684 m_xOutline[3] =-m_u; m_yOutline[3] = m_v;
685 m_xOutline[4] =-m_u; m_yOutline[4] =-m_v;
689 m_xOutline = new double [m_nPoints];
690 m_yOutline = new double [m_nPoints];
691 m_xOutline[0] =-m_u; m_yOutline[0] = 0.0;
692 m_xOutline[1] = m_u; m_yOutline[1] = 0.0;
693 m_xOutline[2] = 0.0; m_yOutline[2] = m_v;
694 m_xOutline[3] =-m_u; m_yOutline[3] = 0.0;
697 cpts = numCirclePoints (TWOPI);
699 m_xOutline = new double [m_nPoints];
700 m_yOutline = new double [m_nPoints];
701 calcEllipsePoints (m_xOutline, m_yOutline, cpts, m_u, m_v);
704 radius = sqrt(m_u * m_u + m_v * m_v);
705 theta = atan(m_u / m_v); // angle with y-axis
706 start = 3.0 * HALFPI - theta;
707 stop = 3.0 * HALFPI + theta;
708 cpts = numCirclePoints (stop - start);
709 m_nPoints = 3 + cpts;
710 m_xOutline = new double [m_nPoints];
711 m_yOutline = new double [m_nPoints];
713 m_xOutline[0] = 0.0; m_yOutline[0] = m_v;
714 m_xOutline[1] =-m_u; m_yOutline[1] = 0.0;
715 calcArcPoints (&m_xOutline[2], &m_yOutline[2], cpts, 0.0, m_v, radius, start, stop);
716 m_xOutline[cpts + 2] = 0.0;
717 m_yOutline[cpts + 2] = m_v;
720 radius = sqrt(m_u * m_u + m_v * m_v);
721 theta = atan (m_u / m_v); // angle with y-axis
722 start = 3.0 * HALFPI - theta;
723 stop = 3.0 * HALFPI + theta;
725 cpts = numCirclePoints (stop - start);
726 m_nPoints = cpts + 1;
727 m_xOutline = new double [m_nPoints];
728 m_yOutline = new double [m_nPoints];
730 calcArcPoints (m_xOutline, m_yOutline, cpts, 0.0, m_v, radius, start, stop);
731 m_xOutline[cpts] = -m_u;
732 m_yOutline[cpts] = 0.0;
735 sys_error(ERR_WARNING, "illegal pelem type %d [makeVectorOutline]", m_type);
739 rotate2d (m_xOutline, m_yOutline, m_nPoints, m_rot);
740 xlat2d (m_xOutline, m_yOutline, m_nPoints, m_cx, m_cy);
742 minmax_array (m_xOutline, m_nPoints, m_xmin, m_xmax);
743 minmax_array (m_yOutline, m_nPoints, m_ymin, m_ymax);
745 // increase pelem extent by SCALE_PELEM_EXTENT to eliminate chance of
746 // missing actual pelem maximum due to polygonal sampling
748 xfact = (m_xmax - m_xmin) * SCALE_PELEM_EXTENT;
749 yfact = (m_ymax - m_ymin) * SCALE_PELEM_EXTENT;
759 * calc_arc Calculate outline of a arc of a circle
762 * calc_arc (x, y, xcent, ycent, pts, r, start, stop)
763 * double x[], y[]; Array of points
764 * int pts Number of points in array
765 * double xcent, ycent Center of cirlce
766 * double r Radius of circle
767 * double start, stop Beginning & ending angles
771 PhantomElement::calcArcPoints (double x[], double y[], const int pts, const double xcent, const double ycent, const double r, const double start, const double stop)
774 sys_error (ERR_WARNING, "negative or zero radius in calc_arc()");
776 double theta = (stop - start) / (pts - 1); // angle incr. between points
777 double c = cos(theta);
778 double s = sin(theta);
780 x[0] = r * cos (start) + xcent;
781 y[0] = r * sin (start) + ycent;
783 double xp = x[0] - xcent;
784 double yp = y[0] - ycent;
785 for (int i = 1; i < pts; i++) {
786 double xc = c * xp - s * yp;
787 double yc = s * xp + c * yp;
796 // PhantomElement::calcEllipsePoints Calculate outline of a ellipse
799 // calcEllipsePoints ()
804 PhantomElement::calcEllipsePoints (double x[], double y[], const int pts, const double u, const double v)
806 calcArcPoints (x, y, m_nPoints, 0.0, 0.0, 1.0, 0.0, TWOPI); // make a unit circle
807 scale2d (x, y, m_nPoints, m_u, m_v); // scale to ellipse
812 * circle_pts Calculate number of points to use for circle segment
815 * n = circle_pts (theta)
816 * int n Number of points to use for arc
817 * double theta Length of arc in radians
821 PhantomElement::numCirclePoints (double theta)
823 theta = clamp (theta, 0., TWOPI);
825 return static_cast<int> (POINTS_PER_CIRCLE * theta / TWOPI + 1.5);
830 PhantomElement::clipLineWorldCoords (double& x1, double& y1, double& x2, double &y2) const
832 /* check if ray is outside of pelem extents */
833 double cx1 = x1, cy1 = y1, cx2 = x2, cy2 = y2;
834 if (! clip_rect (cx1, cy1, cx2, cy2, m_rectLimits))
837 // convert phantom coordinates to pelem coordinates
838 xform_mtx2 (m_xformPhmToObj, x1, y1);
839 xform_mtx2 (m_xformPhmToObj, x2, y2);
841 if (! clipLineNormalizedCoords (x1, y1, x2, y2))
844 // convert standard pelem coordinates back to phantom coordinates
845 xform_mtx2 (m_xformObjToPhm, x1, y1);
846 xform_mtx2 (m_xformObjToPhm, x2, y2);
853 * pelem_clip_line Clip pelem against an arbitrary line
856 * pelem_clip_line (pelem, x1, y1, x2, y2)
857 * PhantomElement& pelem; Pelem to be clipped
858 * double *x1, *y1, *x2, *y2 Endpoints of line to be clipped
861 * true if line passes through pelem
862 * (x1, y1, x2, y2 hold coordinates of new line)
863 * false if line do not pass through pelem
864 * (x1, y1, x2, y2 are undefined)
868 PhantomElement::clipLineNormalizedCoords (double& x1, double& y1, double& x2, double& y2) const
873 case PELEM_RECTANGLE:
875 rect[0] = -1.0; rect[1] = -1.0;
876 rect[2] = 1.0; rect[3] = 1.0;
877 accept = clip_rect (x1, y1, x2, y2, rect);
880 accept = clip_circle (x1, y1, x2, y2, 0.0, 0.0, 1.0, 0.0, 0.0);
883 accept = clip_triangle (x1, y1, x2, y2, 1.0, 1.0, true);
886 accept = clip_segment (x1, y1, x2, y2, m_u, m_v);
889 accept = clip_sector (x1, y1, x2, y2, m_u, m_v);
892 sys_error (ERR_WARNING, "Illegal pelem type %d [pelem_clip_line]", m_type);
900 // METHOD IDENTIFICATION
901 // PhantomElement::isPointInside Check if point is inside pelem
904 // is_point_inside (pelem, x, y, coord_type)
905 // double x, y Point to see if lies in pelem
906 // int coord_type Coordinate type (PELEM_COORD or PHM_COORD)
909 // true if point lies within pelem
910 // false if point lies outside of pelem
913 PhantomElement::isPointInside (double x, double y, const CoordType coord_type) const
915 if (coord_type == PHM_COORD) {
916 xform_mtx2 (m_xformPhmToObj, x, y);
917 } else if (coord_type != PELEM_COORD) {
918 sys_error(ERR_WARNING, "Illegal coordinate type in pelem_is_point_inside");
923 case PELEM_RECTANGLE:
924 if (x > 1. || x < -1. || y > 1. || y < -1.)
930 if (y < 0. || y > 1. - x || y > 1. + x)
936 if (x > 1. || x < -1. || y > 1. || y < -1.)
938 if (x * x + y * y > 1.) // check if inside unit circle
944 // for clipping segments & sectors, must NOT scale by (1/u, 1/v)
945 // because this destroys information about size of arc component
948 if (x > 1. || x < -1. || y > 0.)
949 return (false); // clip against y > 0
950 x *= m_u; // put back u & v scale
952 if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
953 return (false); // clip against circle, r = sqrt(@)
958 if (x > 1. || x < -1. || y > 1.) // extent
960 if (y > 1. - x || y > 1. + x) // triangle
961 return (false); // clip against triangle
962 x *= m_u; // circle: put back u & v scale
964 if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
965 return (false); // clip against circle
970 sys_error (ERR_WARNING, "Illegal pelem type in pelem_is_point_inside()");