r110: *** empty log message ***
[ctsim.git] / libctsim / phantom.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 ** 
4 **     Name:                   phm.cpp
5 **     Purpose:                Routines for phantom objects
6 **     Progammer:              Kevin Rosenberg
7 **     Date Started:           Aug 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: phantom.cpp,v 1.1 2000/06/19 02:59:34 kevin Exp $
13 **
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.
17 **
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.
22 **
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 ******************************************************************************/
27
28 #include "ct.h"
29
30
31 // CLASS IDENTIFICATION
32 //   Phanton
33 //
34
35 Phantom::Phantom (void)
36 {
37   m_nPElem = 0;
38   m_xmin = 1E30;
39   m_xmax = -1E30;
40   m_ymin = 1E30;
41   m_ymax = -1E30;
42   m_diameter = 0;
43   m_composition = P_PELEMS;
44 }
45
46
47 Phantom::~Phantom (void)
48 {
49   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
50     delete *i;
51   }
52 }
53
54
55 void
56 Phantom::create (const int phmid)
57 {
58   switch (phmid) 
59     {
60     case O_PHM_HERMAN:
61       std_herman();
62       break;
63     case O_PHM_ROWLAND:
64       std_rowland();
65       break;
66     case O_PHM_BROWLAND:
67       std_rowland_bordered ();
68       break;
69     case O_PHM_UNITPULSE:
70       m_composition = P_UNIT_PULSE;
71       addPElem ("rectangle", 0., 0., 100., 100., 0., 0.);     // outline 
72       addPElem ("ellipse", 0., 0., 1., 1., 0., 1.);           // pulse 
73       break;
74     default:
75       sys_error (ERR_WARNING, "Illegal phantom id %d\n", phmid);
76       break;
77     }
78 }
79
80
81 /* METHOD IDENTIFICATION
82  *   createFromFile          Add PhantomElements from file
83  *
84  * SYNOPSIS
85  *   createFromFile (filename)
86  *
87  * RETURNS
88  *   true if pelem were added
89  *   false if an pelem not added
90  */
91
92 bool
93 Phantom::createFromFile (const char* const fname)
94 {
95   bool stoploop = false;
96   bool retval = false;
97   FILE *fp;
98
99   if ((fp = fopen (fname, "r")) == NULL)
100     return (false);
101
102   do {
103     double cx, cy, u, v, rot, dens;
104     char pelemtype[80];
105     int n = fscanf (fp, "%79s %lf %lf %lf %lf %lf %lf",
106                 pelemtype, &cx, &cy, &u, &v, &rot, &dens);
107     
108     if (n == EOF || n == 0) {   /* end of file */
109       stoploop = true;
110       retval = false;
111     } else if (n != 7) {
112       stoploop = true;
113       retval = false;
114     } else {
115       addPElem (pelemtype, cx, cy, u, v, rot, dens);
116       retval = true;
117     }
118   } while (stoploop == false);
119   
120   fclose (fp);
121
122   return (retval);
123 }
124
125
126 /* NAME
127  *   addPElem           Add pelem
128  *
129  * SYNOPSIS
130  *   addPElem (type, cx, cy, u, v, rot, atten)
131  *   char *type         type of pelem (box, ellipse, etc)
132  *   double cx, cy      pelem center
133  *   double u,v         pelem size
134  *   double rot         rotation angle of pelem (in degrees)
135  *   double atten       x-ray attenuation cooefficient
136  */
137
138 void 
139 Phantom::addPElem (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
140 {
141   PhantomElement *pelem = new PhantomElement (type, cx, cy, u, v, rot, atten);
142
143   m_listPElem.push_front (pelem);
144
145   // update phantom limits
146   if (m_xmin > pelem->xmin())    m_xmin = pelem->xmin();
147   if (m_xmax < pelem->xmax())    m_xmax = pelem->xmax();
148   if (m_ymin > pelem->ymin())    m_ymin = pelem->ymin();
149   if (m_ymax < pelem->ymax())    m_ymax = pelem->ymax();
150
151   if (m_diameter < pelem->diameter())
152     m_diameter = pelem->diameter();
153
154   //  m_diameter = lineLength(m_xmin, m_ymin, m_xmax, m_ymax);
155
156   m_nPElem++;
157 }
158
159
160 /*----------------------------------------------------------------------*/
161 /*                      Input-Output Routines                           */
162 /*----------------------------------------------------------------------*/
163
164
165 /* NAME
166  *   print                              Print vertices of Phantom pelems
167  *
168  * SYNOPSIS
169  *   print (phm)
170  */
171
172 void 
173 Phantom::print (void) const
174 {
175   printf("PRINTING Phantom\n\n");
176   printf("number of pelems in Phantom = %d\n", m_nPElem);
177   printf("limits: xmin=%8.2g  ymin=%8.2g  xmax=%8.2g  ymax=%8.2g\n",
178          m_xmin, m_ymin, m_xmax, m_ymax);
179   
180   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
181       printf("PELEM:\n");
182       printf("# pts=%3d atten = %7.4f   rot = %7.2f (deg)\n",
183              (*i)->nOutlinePoints(), (*i)->atten(), convertRadiansToDegrees ((*i)->rot()));
184     
185     printf("xmin=%7.3g  ymin=%7.3g  xmax=%7.3g  ymax=%7.3g\n",
186            (*i)->xmin(), (*i)->ymin(), (*i)->xmax(), (*i)->ymax());
187     
188     //    for (int i = 0; i < m_nPoints; i++)
189     //      printf("\t%8.3g    %8.3g\n", i->xOutline()[i], i->yOutline()[i]);
190   }
191 }
192
193
194 /* NAME
195  *   show               Show vector outline of Phantom to user
196  *
197  * SYNOPSIS
198  *   show (pic)
199  */
200
201 #ifdef HAVE_SGP
202 void 
203 Phantom::show (void) const
204 {
205   double wsize = m_xmax - m_xmin;
206   double xmin = m_xmin;
207   double ymin = m_ymin;
208   double xmax, ymax;
209   SGP_ID gid;
210
211   if ((m_ymax - m_ymin) > wsize)
212       wsize = m_ymax - m_ymin;
213   wsize *= 1.1;
214
215   xmax = xmin + wsize;
216   ymax = ymin + wsize; 
217   
218   printf("Drawing Phantom:\n\n");
219   printf("    data limits: %9.3g, %9.3g, %9.3g, %9.3g\n",
220          m_xmin, m_ymin, m_xmax, m_ymax);
221   printf("    window size: %9.3g, %9.3g, %9.3g, %9.3g\n",
222          xmin, ymin, xmax, ymax);
223
224   gid = sgp2_init(0, 0, "Phantom Show");
225   sgp2_window (xmin, ymin, xmax, ymax);
226
227   draw();
228
229   termgrf2();
230 }
231 #endif
232
233
234 /* NAME
235  *   draw               Draw vector outline of Phantom
236  *
237  * SYNOPSIS
238  *   draw ()
239  */
240
241 #ifdef HAVE_SGP
242 void 
243 Phantom::draw (void) const
244 {
245   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++)
246     sgp2_polyline_abs ((*i)->xOutline(), (*i)->yOutline(), (*i)->nOutlinePoints());
247 }
248 #endif
249
250
251 /* NAME
252  *   std_rowland                Make head phantom of S.W. Rowland
253  *
254  * SYNOPSIS
255  *   std_rowland ()
256  *
257  * REFERENCES
258  *   S. W. Rowland, "Computer Implementation of Image Reconstruction
259  *      Formulas", in "Image Reconstruction from Projections: Implementation
260  *      and Applications", edited by G. T. Herman, 1978.
261  */
262
263 void 
264 Phantom::std_rowland (void)
265 {
266   addPElem("ellipse",  0.0000,  0.0000, 0.6900,  0.9200,   0.0,  1.00);
267   addPElem("ellipse",  0.0000, -0.0184, 0.6624,  0.8740,   0.0, -0.98);
268   addPElem("ellipse",  0.2200,  0.0000, 0.1100,  0.3100, -18.0, -0.02);
269   addPElem("ellipse", -0.2200,  0.0000, 0.1600,  0.4100,  18.0, -0.02);
270   addPElem("ellipse",  0.0000,  0.3500, 0.2100,  0.2500,   0.0,  0.01);
271   addPElem("ellipse",  0.0000,  0.1000, 0.0460,  0.0460,   0.0,  0.01);
272   addPElem("ellipse",  0.0000, -0.1000, 0.0460,  0.0460,   0.0,  0.01);
273   addPElem("ellipse", -0.0800, -0.6050, 0.0460,  0.0230,   0.0,  0.01);
274   addPElem("ellipse",  0.0000, -0.6050, 0.0230,  0.0230,   0.0,  0.01);
275   addPElem("ellipse",  0.0600, -0.6050, 0.0230,  0.0230,   0.0,  0.01);
276   addPElem("ellipse",  0.5538, -0.3858, 0.0330,  0.2060, -18.0,  0.03);
277 }
278
279 void 
280 Phantom::std_rowland_bordered (void)
281 {
282   std_rowland ();
283   addPElem ("ellipse", 0.000, 0.0000, 0.7500, 1.000, 0.0, 0.00);
284 }
285
286 /* NAME
287  *   std_herman                 Standard head phantom of G. T. Herman
288  *
289  * SYNOPSIS
290  *   std_herman ()
291  *
292  * REFERENCES
293  *   G. T. Herman, "Image Reconstructions from Projections:  The Fundementals
294  *      of Computed Tomography", 1979.
295  */
296
297 void 
298 Phantom::std_herman (void)
299 {
300   addPElem("ellipse",  0.000,  1.50,  0.375, 0.3000,  90.00, -0.003);
301   addPElem("ellipse",  0.675, -0.75,  0.225, 0.1500, 140.00,  0.010);
302   addPElem("ellipse",  0.750,  1.50,  0.375, 0.2250,  50.00,  0.003);
303   addPElem("segment",  1.375, -7.50,  1.100, 0.6250,  19.20, -0.204);
304   addPElem("segment",  1.375, -7.50,  1.100, 4.3200,  19.21,  0.204);
305   addPElem("segment",  0.000, -2.25,  1.125, 0.3750,   0.00, -0.003);
306   addPElem("segment",  0.000, -2.25,  1.125, 3.0000,   0.00,  0.003);
307   addPElem("segment", -1.000,  3.75,  1.000, 0.5000, 135.00, -0.003);
308   addPElem("segment", -1.000,  3.75,  1.000, 3.0000, 135.00,  0.003);
309   addPElem("segment",  1.000,  3.75,  1.000, 0.5000, 225.00, -0.003);
310   addPElem("segment",  1.000,  3.75,  1.000, 3.0000, 225.00,  0.003);
311   addPElem("triangle", 5.025,  3.75,  1.125, 0.5000, 110.75,  0.206);
312   addPElem("triangle",-5.025,  3.75,  1.125, 0.9000,-110.75,  0.206);
313   addPElem("ellipse",  0.000,  0.00,  8.625, 6.4687,  90.00,  0.416);
314   addPElem("ellipse",  0.000,  0.00,  7.875, 5.7187,  90.00, -0.206);
315 }
316
317
318 ////////////////////////////////////////////////////////////////////////////////////////////////////////
319 // CLASS IDENTIFICATION
320 //
321 //      PhantomElement
322 //
323 // PURPOSE
324 //
325 ////////////////////////////////////////////////////////////////////////////////////////////////////////
326
327
328 PhantomElement::PhantomElement (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
329     : m_cx(cx), m_cy(cy), m_u(u), m_v(v), m_atten(atten), m_nPoints(0), m_xOutline(0), m_yOutline(0)
330 {
331   m_rot = convertDegreesToRadians (rot);   // convert angle to radians
332
333   if (strcasecmp (type, "rectangle") == 0)
334     m_type = PELEM_RECTANGLE;
335   else if (strcasecmp (type, "triangle") == 0)
336     m_type = PELEM_TRIANGLE;
337   else if (strcasecmp (type, "ellipse") == 0)
338     m_type = PELEM_ELLIPSE;
339   else if (strcasecmp (type, "sector") == 0)
340     m_type = PELEM_SECTOR;
341   else if (strcasecmp (type, "segment") == 0)
342     m_type = PELEM_SEGMENT;
343   else {
344     sys_error (ERR_WARNING, "Unknown PhantomElement type %s [PhantomElement::PhantomElement]", type);
345     m_type = PELEM_INVALID;
346   }
347
348   makeTransformMatrices ();     // calc transform matrices between phantom and normalized phantomelement
349   makeVectorOutline ();         // calculate vector outline of pelem 
350
351   // Find maximum diameter of Object
352   double r2Max = 0;
353   for (int i = 0; i < m_nPoints; i++) {
354     double r2 = (m_xOutline[i] * m_xOutline[i]) + (m_yOutline[i] * m_yOutline[i]);
355     if (r2 > r2Max)
356       r2Max = r2;
357   }
358   m_diameter = 2 * sqrt( r2Max );
359
360   m_rectLimits[0] = m_xmin;   m_rectLimits[1] = m_ymin;
361   m_rectLimits[2] = m_xmax;   m_rectLimits[3] = m_ymax;
362 }
363
364
365 PhantomElement::~PhantomElement (void)
366 {
367     delete m_xOutline;
368     delete m_yOutline;
369 }
370
371 void 
372 PhantomElement::makeTransformMatrices (void)
373 {
374   GRFMTX_2D temp;
375
376   // To map normalized Pelem coords to world Phantom 
377   //     scale by (u, v)                                       
378   //     rotate by rot                                  
379   //     translate by (cx, cy)                         
380
381   scale_mtx2 (m_xformObjToPhm, m_u, m_v);
382   rot_mtx2  (temp, m_rot);
383   mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
384   xlat_mtx2 (temp, m_cx, m_cy);
385   mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
386
387   // to map world Phantom coodinates to normalized PElem coords
388   //     translate by (-cx, -cy)
389   //     rotate by -rot
390   //     scale by (1/u, 1/v)
391
392   xlat_mtx2 (m_xformPhmToObj, -m_cx, -m_cy);
393   rot_mtx2  (temp, -m_rot);
394   mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
395   scale_mtx2 (temp, 1 / m_u, 1 / m_v);
396   mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
397 }
398
399
400 /* NAME
401  *   pelem_make_points          INTERNAL routine to calculate point array for an pelem
402  *
403  * SYNOPSIS
404  *   makepelempts (pelem)
405  *   PELEM *pelem       pelem whose points we are calculating
406  *
407  * NOTES
408  *   Called by phm_add_pelem()
409  */
410
411 static const double SCALE_PELEM_EXTENT=0.005;           // increase pelem limits by 0.5% 
412
413 void
414 PhantomElement::makeVectorOutline (void)
415 {
416   double radius, theta, start, stop;
417   double xfact, yfact;
418   int cpts;
419
420   m_nPoints = 0;
421   switch (m_type) {
422   case PELEM_RECTANGLE:
423     m_nPoints = 5;
424     m_xOutline = new double [m_nPoints];
425     m_yOutline = new double [m_nPoints];
426     m_xOutline[0] =-m_u;        m_yOutline[0] =-m_v;
427     m_xOutline[1] = m_u;        m_yOutline[1] =-m_v;
428     m_xOutline[2] = m_u;        m_yOutline[2] = m_v;
429     m_xOutline[3] =-m_u;        m_yOutline[3] = m_v;
430     m_xOutline[4] =-m_u;        m_yOutline[4] =-m_v;
431     break;
432   case PELEM_TRIANGLE:
433     m_nPoints = 4;
434     m_xOutline = new double [m_nPoints];
435     m_yOutline = new double [m_nPoints];
436     m_xOutline[0] =-m_u;        m_yOutline[0] = 0.0;
437     m_xOutline[1] = m_u;        m_yOutline[1] = 0.0;
438     m_xOutline[2] = 0.0;        m_yOutline[2] = m_v;
439     m_xOutline[3] =-m_u;        m_yOutline[3] = 0.0;
440     break;
441   case PELEM_ELLIPSE:
442     cpts = numCirclePoints (TWOPI);
443     m_nPoints = cpts;
444     m_xOutline = new double [m_nPoints];
445     m_yOutline = new double [m_nPoints];
446     calcEllipsePoints (m_xOutline, m_yOutline, cpts, m_u, m_v);
447     break;
448   case PELEM_SECTOR:
449     radius = sqrt(m_u * m_u + m_v * m_v);
450     theta = atan(m_u / m_v);            // angle with y-axis 
451     start = 3.0 * HALFPI - theta;
452     stop  = 3.0 * HALFPI + theta;
453     cpts = numCirclePoints (stop - start);
454     m_nPoints = 3 + cpts;
455     m_xOutline = new double [m_nPoints];
456     m_yOutline = new double [m_nPoints];
457     
458     m_xOutline[0] = 0.0;                m_yOutline[0] = m_v;
459     m_xOutline[1] =-m_u;                m_yOutline[1] = 0.0;
460     calcArcPoints (&m_xOutline[2], &m_yOutline[2], cpts, 0.0, m_v, radius, start, stop);
461     m_xOutline[cpts + 2] = 0.0;
462     m_yOutline[cpts + 2] = m_v;
463     break;
464   case PELEM_SEGMENT:
465     radius = sqrt(m_u * m_u + m_v * m_v);
466     theta = atan (m_u / m_v);           // angle with y-axis 
467     start = 3.0 * HALFPI - theta;
468     stop  = 3.0 * HALFPI + theta;
469     
470     cpts = numCirclePoints (stop - start);
471     m_nPoints = cpts + 1;
472     m_xOutline = new double [m_nPoints];
473     m_yOutline = new double [m_nPoints];
474     
475     calcArcPoints (m_xOutline, m_yOutline, cpts, 0.0, m_v, radius, start, stop);
476     m_xOutline[cpts] = -m_u;
477     m_yOutline[cpts] = 0.0;
478     break;
479   default:
480     sys_error(ERR_WARNING, "illegal pelem type %d [makeVectorOutline]", m_type);
481     return;
482   }
483   
484   rotate2d (m_xOutline, m_yOutline, m_nPoints, m_rot);
485   xlat2d (m_xOutline, m_yOutline, m_nPoints, m_cx, m_cy);
486   
487   minmax_array (m_xOutline, m_nPoints, m_xmin, m_xmax);
488   minmax_array (m_yOutline, m_nPoints, m_ymin, m_ymax);
489   
490   // increase pelem extent by SCALE_PELEM_EXTENT to eliminate chance of
491   //   missing actual pelem maximum due to polygonal sampling 
492
493   xfact = (m_xmax - m_xmin) * SCALE_PELEM_EXTENT;
494   yfact = (m_ymax - m_ymin) * SCALE_PELEM_EXTENT;
495
496   m_xmin -= xfact;
497   m_ymin -= yfact;
498   m_xmax += xfact;
499   m_ymax += yfact;
500 }
501
502
503
504 /* NAME
505  *   calc_arc                   Calculate outline of a arc of a circle
506  *
507  * SYNOPSIS
508  *   calc_arc (x, y, xcent, ycent, pts, r, start, stop)
509  *   double x[], y[];           Array of points
510  *   int pts                    Number of points in array
511  *   double xcent, ycent        Center of cirlce
512  *   double r                   Radius of circle
513  *   double start, stop         Beginning & ending angles
514  */
515
516 void 
517 PhantomElement::calcArcPoints (double x[], double y[], const int pts, const double xcent, const double ycent, const double r, const double start, const double stop)
518 {
519     if (r <= 0.0)
520         sys_error (ERR_WARNING, "negative or zero radius in calc_arc()");
521
522     double theta = (stop - start) / (pts - 1);  // angle incr. between points 
523     double c = cos(theta);
524     double s = sin(theta);
525   
526     x[0] = r * cos (start) + xcent;
527     y[0] = r * sin (start) + ycent;
528
529     double xp = x[0] - xcent;
530     double yp = y[0] - ycent;
531     for (int i = 1; i < pts; i++) {
532         double xc = c * xp - s * yp;
533         double yc = s * xp + c * yp;
534         x[i] = xc + xcent;
535         y[i] = yc + ycent;
536         xp = xc;  yp = yc;
537     }
538 }
539
540
541 // NAME
542 //   PhantomElement::calcEllipsePoints    Calculate outline of a ellipse
543 //
544 // SYNOPSIS
545 //   calcEllipsePoints ()
546 //
547
548
549 void 
550 PhantomElement::calcEllipsePoints (double x[], double y[], const int pts, const double u, const double v)
551 {
552     calcArcPoints (x, y, m_nPoints, 0.0, 0.0, 1.0, 0.0, TWOPI);   // make a unit circle 
553     scale2d (x, y, m_nPoints, m_u, m_v);                             // scale to ellipse 
554 }
555
556
557 /* NAME
558  *   circle_pts         Calculate number of points to use for circle segment
559  *
560  * SYNOPSIS
561  *   n = circle_pts (theta)
562  *   int n              Number of points to use for arc
563  *   double theta       Length of arc in radians
564  */
565
566 int 
567 PhantomElement::numCirclePoints (double theta) const
568 {
569     if (theta < 0.0 || theta > TWOPI)
570       sys_error(ERR_WARNING, "illegal values sent to circle_pts");
571
572     return static_cast<int> (POINTS_PER_CIRCLE * theta / TWOPI + 1.5);
573 }
574
575
576 bool
577 PhantomElement::clipLineWorldCoords (double& x1, double& y1, double& x2, double &y2) const
578 {
579   /* check if ray is outside of pelem extents */
580   double cx1 = x1, cy1 = y1, cx2 = x2, cy2 = y2;
581   if (! clip_rect (cx1, cy1, cx2, cy2, m_rectLimits))
582     return false;
583         
584   // convert phantom coordinates to pelem coordinates 
585   xform_mtx2 (m_xformPhmToObj, x1, y1);
586   xform_mtx2 (m_xformPhmToObj, x2, y2);
587         
588   if (! clipLineNormalizedCoords (x1, y1, x2, y2))
589     return false;
590
591   // convert standard pelem coordinates back to phantom coordinates 
592   xform_mtx2 (m_xformObjToPhm, x1, y1);
593   xform_mtx2 (m_xformObjToPhm, x2, y2);
594
595   return true;
596 }
597
598
599 /* NAME
600  *   pelem_clip_line                    Clip pelem against an arbitrary line
601  *
602  * SYNOPSIS
603  *   pelem_clip_line (pelem, x1, y1, x2, y2)
604  *   PhantomElement& pelem;             Pelem to be clipped
605  *   double *x1, *y1, *x2, *y2  Endpoints of line to be clipped
606  *
607  * RETURNS
608  *   true   if line passes through pelem
609  *              (x1, y1, x2, y2 hold coordinates of new line)
610  *   false  if line do not pass through pelem
611  *              (x1, y1, x2, y2 are undefined)
612  */
613
614 bool
615 PhantomElement::clipLineNormalizedCoords (double& x1, double& y1, double& x2, double& y2) const
616 {
617   bool accept = false;
618
619   switch (m_type) {
620   case PELEM_RECTANGLE:
621     double rect[4];
622     rect[0] = -1.0;  rect[1] = -1.0;
623     rect[2] = 1.0;  rect[3] = 1.0;
624     accept = clip_rect (x1, y1, x2, y2, rect);
625     break;
626   case PELEM_ELLIPSE:
627     accept = clip_circle (x1, y1, x2, y2, 0.0, 0.0, 1.0, 0.0, 0.0);
628     break;
629   case PELEM_TRIANGLE:
630     accept = clip_triangle (x1, y1, x2, y2, 1.0, 1.0, true);
631     break;
632   case PELEM_SEGMENT:
633     accept = clip_segment (x1, y1, x2, y2, m_u, m_v);
634     break;
635   case PELEM_SECTOR:
636     accept = clip_sector (x1, y1, x2, y2, m_u, m_v);
637     break;
638   default:
639     sys_error (ERR_WARNING, "Illegal pelem type %d [pelem_clip_line]", m_type);
640     break;
641   }
642
643   return(accept);
644 }
645
646
647 // METHOD IDENTIFICATION 
648 //    PhantomElement::isPointInside             Check if point is inside pelem
649 //
650 // SYNOPSIS
651 //    is_point_inside (pelem, x, y, coord_type)
652 //    double x, y               Point to see if lies in pelem
653 //    int coord_type            Coordinate type (PELEM_COORD or PHM_COORD)
654 //
655 // RETURNS
656 //    true if point lies within pelem
657 //    false if point lies outside of pelem
658
659 bool
660 PhantomElement::isPointInside (double x, double y, const CoordType coord_type)
661 {
662   if (coord_type == PHM_COORD) {
663     xform_mtx2 (m_xformPhmToObj, x, y);
664   } else if (coord_type != PELEM_COORD) {
665     sys_error(ERR_WARNING, "Illegal coordinate type in pelem_is_point_inside");
666     return (false);
667   }
668
669   switch (m_type) {
670   case PELEM_RECTANGLE:
671     if (x > 1. || x < -1. || y > 1. || y < -1.)
672       return (false);
673     else
674       return (true);
675     break;
676   case PELEM_TRIANGLE:
677     if (y < 0. || y > 1. - x || y > 1. + x)
678       return (false);
679     else
680       return (true);
681     break;
682   case PELEM_ELLIPSE:
683     if (x > 1. || x < -1. || y > 1. || y < -1.)
684       return (false);
685     if (x * x + y * y > 1.)             // check if inside unit circle
686       return (false);
687     else
688       return (true);
689     break;
690
691     // for clipping segments & sectors, must NOT scale by (1/u, 1/v)
692     // because this destroys information about size of arc component 
693
694   case PELEM_SEGMENT:
695     if (x > 1. || x < -1. || y > 0.)
696         return (false);         // clip against y > 0 
697     x *= m_u;                   // put back u & v scale 
698     y *= m_v;
699     if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
700       return (false);           // clip against circle, r = sqrt(@)
701     else
702       return (true);
703     break;
704   case PELEM_SECTOR:
705       if (x > 1. || x < -1. || y > 1.)   // extent 
706       return (false);
707       if (y > 1. - x || y > 1. + x)      // triangle    
708           return (false);                      // clip against triangle 
709       x *= m_u;                // circle: put back u & v scale 
710     y *= m_v;
711     if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
712         return (false);                // clip against circle 
713     else
714       return (true);
715   break;
716   default:
717     sys_error (ERR_WARNING, "Illegal pelem type in pelem_is_point_inside()");
718     break;
719   }
720
721   return (false);
722 }
723
724