r158: *** 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.10 2000/07/22 15:45:33 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 const int Phantom::PHM_INVALID = -1;
32 const int Phantom::PHM_HERMAN = 0;
33 const int Phantom::PHM_BHERMAN = 1;
34 const int Phantom::PHM_ROWLAND = 2;
35 const int Phantom::PHM_BROWLAND = 3;
36 const int Phantom::PHM_UNITPULSE = 4;
37
38 const char* Phantom::s_aszPhantomName[] = 
39 {
40   {"herman"},
41   {"bherman"},
42   {"rowland"},
43   {"browland"},
44   {"unitpulse"},
45 };
46
47 const char* Phantom::s_aszPhantomTitle[] = 
48 {
49   {"Herman Head"},
50   {"Herman Head Bordered"},
51   {"Rowland Head"},
52   {"Rowland Head Bordered"},
53   {"Unit Pulse"},
54 };
55
56 const int Phantom::s_iPhantomCount = sizeof(s_aszPhantomName) / sizeof(const char*);
57
58
59 // CLASS IDENTIFICATION
60 //   Phantom
61 //
62
63 Phantom::Phantom ()
64 {
65   init ();
66 }
67
68
69 Phantom::Phantom (const char* const phmName)
70 {
71   init ();
72   createFromPhantom (phmName);
73 }
74
75 void 
76 Phantom::init ()
77 {
78   m_nPElem = 0;
79   m_xmin = 1E30;
80   m_xmax = -1E30;
81   m_ymin = 1E30;
82   m_ymax = -1E30;
83   m_diameter = 0;
84   m_composition = P_PELEMS;
85   m_fail = false;
86   m_id = PHM_INVALID;
87 }
88
89 Phantom::~Phantom ()
90 {
91   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
92     delete *i;
93   }
94 }
95
96
97 const char*
98 Phantom::convertPhantomIDToName (int phmID)
99 {
100   static const char *name = "";
101
102   if (phmID >= 0 && phmID < s_iPhantomCount)
103       return (s_aszPhantomName[phmID]);
104
105   return (name);
106 }
107
108 const char*
109 Phantom::convertPhantomIDToTitle (int phmID)
110 {
111   static const char *title = "";
112
113   if (phmID >= 0 && phmID < s_iPhantomCount)
114       return (s_aszPhantomName[phmID]);
115
116   return (title);
117 }
118       
119 int
120 Phantom::convertNameToPhantomID (const char* const phmName) 
121 {
122   int id = PHM_INVALID;
123
124   for (int i = 0; i < s_iPhantomCount; i++)
125       if (strcasecmp (phmName, s_aszPhantomName[i]) == 0) {
126           id = i;
127           break;
128       }
129
130   return (id);
131 }
132   
133
134 bool
135 Phantom::createFromPhantom (const char* const phmName)
136 {
137   int phmid = convertNameToPhantomID (phmName);
138   if (phmid == PHM_INVALID) {
139     m_fail = true;
140     m_failMessage = "Invalid phantom name ";
141     m_failMessage += phmName;
142     return false;
143   }
144
145   m_name = phmName;
146   createFromPhantom (phmid);
147   return true;
148 }
149
150 bool
151 Phantom::createFromPhantom (const int phmid)
152 {
153   switch (phmid) 
154     {
155     case PHM_HERMAN:
156       addStdHerman();
157       break;
158     case PHM_BHERMAN:
159       addStdHermanBordered();
160       break;
161     case PHM_ROWLAND:
162       addStdRowland();
163       break;
164     case PHM_BROWLAND:
165       addStdRowlandBordered ();
166       break;
167     case PHM_UNITPULSE:
168       m_composition = P_UNIT_PULSE;
169       addPElem ("rectangle", 0., 0., 100., 100., 0., 0.);     // outline 
170       addPElem ("ellipse", 0., 0., 1., 1., 0., 1.);           // pulse 
171       break;
172     default:
173       m_fail = true;
174       m_failMessage = "Illegal phantom id ";
175       m_failMessage += phmid;
176       return false;
177     }
178
179   m_id = phmid;
180
181   return true;
182 }
183
184
185 /* METHOD IDENTIFICATION
186  *   createFromFile          Add PhantomElements from file
187  *
188  * SYNOPSIS
189  *   createFromFile (filename)
190  *
191  * RETURNS
192  *   true if pelem were added
193  *   false if an pelem not added
194  */
195
196 bool
197 Phantom::createFromFile (const char* const fname)
198 {
199   bool stoploop = false;
200   bool retval = false;
201   FILE *fp;
202
203   if ((fp = fopen (fname, "r")) == NULL)
204     return (false);
205
206   do {
207     double cx, cy, u, v, rot, dens;
208     char pelemtype[80];
209     int n = fscanf (fp, "%79s %lf %lf %lf %lf %lf %lf",
210                 pelemtype, &cx, &cy, &u, &v, &rot, &dens);
211     
212     if (n == EOF || n == 0) {   /* end of file */
213       stoploop = true;
214       retval = false;
215     } else if (n != 7) {
216       stoploop = true;
217       retval = false;
218     } else {
219       addPElem (pelemtype, cx, cy, u, v, rot, dens);
220       retval = true;
221     }
222   } while (stoploop == false);
223   
224   fclose (fp);
225
226   return (retval);
227 }
228
229
230 /* NAME
231  *   addPElem           Add pelem
232  *
233  * SYNOPSIS
234  *   addPElem (type, cx, cy, u, v, rot, atten)
235  *   char *type         type of pelem (box, ellipse, etc)
236  *   double cx, cy      pelem center
237  *   double u,v         pelem size
238  *   double rot         rotation angle of pelem (in degrees)
239  *   double atten       x-ray attenuation cooefficient
240  */
241
242 void 
243 Phantom::addPElem (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
244 {
245   PhantomElement *pelem = new PhantomElement (type, cx, cy, u, v, rot, atten);
246
247   m_listPElem.push_front (pelem);
248
249   // update phantom limits
250   if (m_xmin > pelem->xmin())    m_xmin = pelem->xmin();
251   if (m_xmax < pelem->xmax())    m_xmax = pelem->xmax();
252   if (m_ymin > pelem->ymin())    m_ymin = pelem->ymin();
253   if (m_ymax < pelem->ymax())    m_ymax = pelem->ymax();
254
255   if (m_diameter < pelem->diameter())
256     m_diameter = pelem->diameter();
257
258   //  m_diameter = lineLength(m_xmin, m_ymin, m_xmax, m_ymax);
259
260   m_nPElem++;
261 }
262
263
264 /*----------------------------------------------------------------------*/
265 /*                      Input-Output Routines                           */
266 /*----------------------------------------------------------------------*/
267
268
269 /* NAME
270  *   print                              Print vertices of Phantom pelems
271  *
272  * SYNOPSIS
273  *   print (phm)
274  */
275
276 void 
277 Phantom::print () const
278 {
279   printf("PRINTING Phantom\n\n");
280   printf("number of pelems in Phantom = %d\n", m_nPElem);
281   printf("limits: xmin=%8.2g  ymin=%8.2g  xmax=%8.2g  ymax=%8.2g\n",
282          m_xmin, m_ymin, m_xmax, m_ymax);
283   
284   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++) {
285       printf("PELEM:\n");
286       printf("# pts=%3d atten = %7.4f   rot = %7.2f (deg)\n",
287              (*i)->nOutlinePoints(), (*i)->atten(), convertRadiansToDegrees ((*i)->rot()));
288     
289     printf("xmin=%7.3g  ymin=%7.3g  xmax=%7.3g  ymax=%7.3g\n",
290            (*i)->xmin(), (*i)->ymin(), (*i)->xmax(), (*i)->ymax());
291     
292     //    for (int i = 0; i < m_nPoints; i++)
293     //      printf("\t%8.3g    %8.3g\n", i->xOutline()[i], i->yOutline()[i]);
294   }
295 }
296
297
298 /* NAME
299  *   show               Show vector outline of Phantom to user
300  *
301  * SYNOPSIS
302  *   show (pic)
303  */
304
305 #ifdef HAVE_SGP
306 void 
307 Phantom::show () const
308 {
309   double wsize = m_xmax - m_xmin;
310   double xmin = m_xmin;
311   double ymin = m_ymin;
312   double xmax, ymax;
313   SGP_ID gid;
314
315   if ((m_ymax - m_ymin) > wsize)
316       wsize = m_ymax - m_ymin;
317   wsize *= 1.1;
318
319   xmax = xmin + wsize;
320   ymax = ymin + wsize; 
321   
322   printf("Drawing Phantom:\n\n");
323   printf("    data limits: %9.3g, %9.3g, %9.3g, %9.3g\n", m_xmin, m_ymin, m_xmax, m_ymax);
324   printf("    window size: %9.3g, %9.3g, %9.3g, %9.3g\n", xmin, ymin, xmax, ymax);
325
326   gid = sgp2_init (0, 0, "Phantom Show");
327   sgp2_window (xmin, ymin, xmax, ymax);
328
329   draw();
330
331   sgp2_close (gid);
332 }
333 #endif
334
335
336 /* NAME
337  *   draw               Draw vector outline of Phantom
338  *
339  * SYNOPSIS
340  *   draw ()
341  */
342
343 #ifdef HAVE_SGP
344 void 
345 Phantom::draw () const
346 {
347   for (PElemIterator i = m_listPElem.begin(); i != m_listPElem.end(); i++)
348     sgp2_polyline_abs ((*i)->xOutline(), (*i)->yOutline(), (*i)->nOutlinePoints());
349 }
350 #endif
351
352
353 /* NAME
354  *   addStdRowland              Make head phantom of S.W. Rowland
355  *
356  * REFERENCES
357  *   S. W. Rowland, "Computer Implementation of Image Reconstruction
358  *      Formulas", in "Image Reconstruction from Projections: Implementation
359  *      and Applications", edited by G. T. Herman, 1978.
360  */
361
362 void 
363 Phantom::addStdRowland ()
364 {
365   addPElem ("ellipse",  0.0000,  0.0000, 0.6900,  0.9200,   0.0,  1.00);
366   addPElem ("ellipse",  0.0000, -0.0184, 0.6624,  0.8740,   0.0, -0.98);
367   addPElem ("ellipse",  0.2200,  0.0000, 0.1100,  0.3100, -18.0, -0.02);
368   addPElem ("ellipse", -0.2200,  0.0000, 0.1600,  0.4100,  18.0, -0.02);
369   addPElem ("ellipse",  0.0000,  0.3500, 0.2100,  0.2500,   0.0,  0.01);
370   addPElem ("ellipse",  0.0000,  0.1000, 0.0460,  0.0460,   0.0,  0.01);
371   addPElem ("ellipse",  0.0000, -0.1000, 0.0460,  0.0460,   0.0,  0.01);
372   addPElem ("ellipse", -0.0800, -0.6050, 0.0460,  0.0230,   0.0,  0.01);
373   addPElem ("ellipse",  0.0000, -0.6050, 0.0230,  0.0230,   0.0,  0.01);
374   addPElem ("ellipse",  0.0600, -0.6050, 0.0230,  0.0230,   0.0,  0.01);
375   addPElem ("ellipse",  0.5538, -0.3858, 0.0330,  0.2060, -18.0,  0.03);
376 }
377
378 void 
379 Phantom::addStdRowlandBordered ()
380 {
381   addStdRowland ();
382   addPElem ("ellipse", 0.000, 0.0000, 0.7500, 1.000, 0.0, 0.00);
383 }
384
385 /* NAME
386  *   addStdHerman                       Standard head phantom of G. T. Herman
387  *
388  * REFERENCES
389  *   G. T. Herman, "Image Reconstructions from Projections:  The Fundementals
390  *      of Computed Tomography", 1979.
391  */
392
393 void 
394 Phantom::addStdHerman ()
395 {
396   addPElem ("ellipse",  0.000,  1.50,  0.375, 0.3000,  90.00, -0.003);
397   addPElem ("ellipse",  0.675, -0.75,  0.225, 0.1500, 140.00,  0.010);
398   addPElem ("ellipse",  0.750,  1.50,  0.375, 0.2250,  50.00,  0.003);
399   addPElem ("segment",  1.375, -7.50,  1.100, 0.6250,  19.20, -0.204);
400   addPElem ("segment",  1.375, -7.50,  1.100, 4.3200,  19.21,  0.204);
401   addPElem ("segment",  0.000, -2.25,  1.125, 0.3750,   0.00, -0.003);
402   addPElem ("segment",  0.000, -2.25,  1.125, 3.0000,   0.00,  0.003);
403   addPElem ("segment", -1.000,  3.75,  1.000, 0.5000, 135.00, -0.003);
404   addPElem ("segment", -1.000,  3.75,  1.000, 3.0000, 135.00,  0.003);
405   addPElem ("segment",  1.000,  3.75,  1.000, 0.5000, 225.00, -0.003);
406   addPElem ("segment",  1.000,  3.75,  1.000, 3.0000, 225.00,  0.003);
407   addPElem ("triangle", 5.025,  3.75,  1.125, 0.5000, 110.75,  0.206);
408   addPElem ("triangle",-5.025,  3.75,  1.125, 0.9000,-110.75,  0.206);
409   addPElem ("ellipse",  0.000,  0.00,  8.625, 6.4687,  90.00,  0.416);
410   addPElem ("ellipse",  0.000,  0.00,  7.875, 5.7187,  90.00, -0.206);
411 }
412
413 void
414 Phantom::addStdHermanBordered ()
415 {
416   addStdHerman();
417   addPElem ("ellipse",  0.000, 0.000, 8.650, 8.650,  0.00, 0.000);
418 }
419
420
421 /* NAME
422  *    convertToImagefile                Make image array from Phantom
423  *
424  * SYNOPSIS
425  *    pic_to_imagefile (pic, im, nsample)
426  *    Phantom& pic              Phantom definitions
427  *    ImageFile  *im            Computed pixel array
428  *    int nsample               Number of samples along each axis for each pixel
429  *                              (total samples per pixel = nsample * nsample)
430  */
431
432 void
433 Phantom::convertToImagefile (ImageFile& im, const int in_nsample, const int trace) const
434 {
435   convertToImagefile (im, in_nsample, trace, 0, im.nx());
436 }
437
438 void 
439 Phantom::convertToImagefile (ImageFile& im, const int in_nsample, const int trace, const int colStart, const int colCount) const
440 {
441   int nx = im.nx();
442   int ny = im.ny();
443   if (nx < 2 || ny < 2)
444       return;
445
446   int nsample = in_nsample;
447   if (nsample < 1)  
448     nsample = 1;
449
450   double dx = m_xmax - m_xmin;
451   double dy = m_ymax - m_ymin;
452   double xcent = m_xmin + dx / 2;
453   double ycent = m_ymin + dy / 2;
454   double phmlen = (dx > dy ? dx : dy);
455
456   double phmradius = phmlen / 2;
457
458   double xmin = xcent - phmradius;
459   double xmax = xcent + phmradius;
460   double ymin = ycent - phmradius;
461   double ymax = ycent + phmradius;
462
463   // Each pixel holds the average of the intensity of the cell with (ix,iy) at the center of the pixel
464   // Set major increments so that the last cell v[nx-1][ny-1] will start at xmax - xinc, ymax - yinc).
465   // Set minor increments so that sample points are centered in cell
466
467   double xinc = (xmax - xmin) / nx;
468   double yinc = (ymax - ymin) / ny;
469
470   double kxinc = xinc / nsample;                /* interval between samples */
471   double kyinc = yinc / nsample;
472   double kxofs = kxinc / 2;             /* offset of 1st point */
473   double kyofs = kyinc / 2;
474
475   im.setAxisExtent (xmin, xmax, ymin, ymax);
476   im.setAxisIncrement (xinc, yinc);
477
478   ImageFileArray v = im.getArray();
479
480   for (int ix = 0; ix < colCount; ix++)
481       for (int iy = 0; iy < ny; iy++)
482           v[ix][iy] = 0;
483
484   double x_start = xmin + (colStart * xinc);
485   for (PElemConstIterator pelem = m_listPElem.begin(); pelem != m_listPElem.end(); pelem++) {
486       const PhantomElement& rPElem = **pelem;
487       double x, y, xi, yi;
488       int ix, iy, kx, ky;
489       for (ix = 0, x = x_start; ix < colCount; ix++, x += xinc) {
490           for (iy = 0, y = ymin; iy < ny; iy++, y += yinc) {
491               for (kx = 0, xi = x + kxofs; kx < nsample; kx++, xi += kxinc) {
492                   for (ky = 0, yi = y + kyofs; ky < nsample; ky++, yi += kyinc)
493                       if (rPElem.isPointInside (xi, yi, PHM_COORD) == TRUE)
494                           v[ix][iy] += rPElem.atten();
495               } // for kx
496           } /* for iy */
497       }  /* for ix */
498   }  /* for pelem */
499   
500
501   if (nsample > 1) {
502     double factor = 1.0 / (nsample * nsample);
503
504     for (int ix = 0; ix < colCount; ix++)
505       for (int iy = 0; iy < ny; iy++)
506         v[ix][iy] *= factor;
507   }
508 }
509
510 ////////////////////////////////////////////////////////////////////////////////////////////////////////
511 // CLASS IDENTIFICATION
512 //
513 //      PhantomElement
514 //
515 // PURPOSE
516 //
517 ////////////////////////////////////////////////////////////////////////////////////////////////////////
518
519
520 PhantomElement::PhantomElement (const char *type, const double cx, const double cy, const double u, const double v, const double rot, const double atten)
521     : m_cx(cx), m_cy(cy), m_u(u), m_v(v), m_atten(atten), m_nPoints(0), m_xOutline(0), m_yOutline(0)
522 {
523   m_rot = convertDegreesToRadians (rot);   // convert angle to radians
524
525   m_type = convertNameToType (type);
526
527   makeTransformMatrices ();     // calc transform matrices between phantom and normalized phantomelement
528   makeVectorOutline ();         // calculate vector outline of pelem 
529
530   // Find maximum diameter of Object
531   double r2Max = 0;
532   for (int i = 0; i < m_nPoints; i++) {
533     double r2 = (m_xOutline[i] * m_xOutline[i]) + (m_yOutline[i] * m_yOutline[i]);
534     if (r2 > r2Max)
535       r2Max = r2;
536   }
537   m_diameter = 2 * sqrt( r2Max );
538
539   m_rectLimits[0] = m_xmin;   m_rectLimits[1] = m_ymin;
540   m_rectLimits[2] = m_xmax;   m_rectLimits[3] = m_ymax;
541 }
542
543
544 PhantomElement::~PhantomElement ()
545 {
546     delete m_xOutline;
547     delete m_yOutline;
548 }
549
550 PhmElemType
551 PhantomElement::convertNameToType (const char* const typeName)
552 {
553     PhmElemType type = PELEM_INVALID;
554
555     if (strcasecmp (typeName, "rectangle") == 0)
556         type = PELEM_RECTANGLE;
557     else if (strcasecmp (typeName, "triangle") == 0)
558         type = PELEM_TRIANGLE;
559     else if (strcasecmp (typeName, "ellipse") == 0)
560         type = PELEM_ELLIPSE;
561     else if (strcasecmp (typeName, "sector") == 0)
562         type = PELEM_SECTOR;
563     else if (strcasecmp (typeName, "segment") == 0)
564       type = PELEM_SEGMENT;
565     else
566         sys_error (ERR_WARNING, "Unknown PhantomElement type %s [PhantomElement::PhantomElement]", type);
567
568     return (type);
569 }
570
571 void 
572 PhantomElement::makeTransformMatrices ()
573 {
574   GRFMTX_2D temp;
575
576   // To map normalized Pelem coords to world Phantom 
577   //     scale by (u, v)                                       
578   //     rotate by rot                                  
579   //     translate by (cx, cy)                         
580
581   scale_mtx2 (m_xformObjToPhm, m_u, m_v);
582   rot_mtx2  (temp, m_rot);
583   mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
584   xlat_mtx2 (temp, m_cx, m_cy);
585   mult_mtx2 (m_xformObjToPhm, temp, m_xformObjToPhm);
586
587   // to map world Phantom coodinates to normalized PElem coords
588   //     translate by (-cx, -cy)
589   //     rotate by -rot
590   //     scale by (1/u, 1/v)
591
592   xlat_mtx2 (m_xformPhmToObj, -m_cx, -m_cy);
593   rot_mtx2  (temp, -m_rot);
594   mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
595   scale_mtx2 (temp, 1 / m_u, 1 / m_v);
596   mult_mtx2 (m_xformPhmToObj, temp, m_xformPhmToObj);
597 }
598
599
600 /* NAME
601  *   pelem_make_points          INTERNAL routine to calculate point array for an pelem
602  *
603  * SYNOPSIS
604  *   makepelempts (pelem)
605  *   PELEM *pelem       pelem whose points we are calculating
606  *
607  * NOTES
608  *   Called by phm_add_pelem()
609  */
610
611 void
612 PhantomElement::makeVectorOutline ()
613 {
614   double radius, theta, start, stop;
615   double xfact, yfact;
616   int cpts;
617
618   m_nPoints = 0;
619   switch (m_type) {
620   case PELEM_RECTANGLE:
621     m_nPoints = 5;
622     m_xOutline = new double [m_nPoints];
623     m_yOutline = new double [m_nPoints];
624     m_xOutline[0] =-m_u;        m_yOutline[0] =-m_v;
625     m_xOutline[1] = m_u;        m_yOutline[1] =-m_v;
626     m_xOutline[2] = m_u;        m_yOutline[2] = m_v;
627     m_xOutline[3] =-m_u;        m_yOutline[3] = m_v;
628     m_xOutline[4] =-m_u;        m_yOutline[4] =-m_v;
629     break;
630   case PELEM_TRIANGLE:
631     m_nPoints = 4;
632     m_xOutline = new double [m_nPoints];
633     m_yOutline = new double [m_nPoints];
634     m_xOutline[0] =-m_u;        m_yOutline[0] = 0.0;
635     m_xOutline[1] = m_u;        m_yOutline[1] = 0.0;
636     m_xOutline[2] = 0.0;        m_yOutline[2] = m_v;
637     m_xOutline[3] =-m_u;        m_yOutline[3] = 0.0;
638     break;
639   case PELEM_ELLIPSE:
640     cpts = numCirclePoints (TWOPI);
641     m_nPoints = cpts;
642     m_xOutline = new double [m_nPoints];
643     m_yOutline = new double [m_nPoints];
644     calcEllipsePoints (m_xOutline, m_yOutline, cpts, m_u, m_v);
645     break;
646   case PELEM_SECTOR:
647     radius = sqrt(m_u * m_u + m_v * m_v);
648     theta = atan(m_u / m_v);            // angle with y-axis 
649     start = 3.0 * HALFPI - theta;
650     stop  = 3.0 * HALFPI + theta;
651     cpts = numCirclePoints (stop - start);
652     m_nPoints = 3 + cpts;
653     m_xOutline = new double [m_nPoints];
654     m_yOutline = new double [m_nPoints];
655     
656     m_xOutline[0] = 0.0;                m_yOutline[0] = m_v;
657     m_xOutline[1] =-m_u;                m_yOutline[1] = 0.0;
658     calcArcPoints (&m_xOutline[2], &m_yOutline[2], cpts, 0.0, m_v, radius, start, stop);
659     m_xOutline[cpts + 2] = 0.0;
660     m_yOutline[cpts + 2] = m_v;
661     break;
662   case PELEM_SEGMENT:
663     radius = sqrt(m_u * m_u + m_v * m_v);
664     theta = atan (m_u / m_v);           // angle with y-axis 
665     start = 3.0 * HALFPI - theta;
666     stop  = 3.0 * HALFPI + theta;
667     
668     cpts = numCirclePoints (stop - start);
669     m_nPoints = cpts + 1;
670     m_xOutline = new double [m_nPoints];
671     m_yOutline = new double [m_nPoints];
672     
673     calcArcPoints (m_xOutline, m_yOutline, cpts, 0.0, m_v, radius, start, stop);
674     m_xOutline[cpts] = -m_u;
675     m_yOutline[cpts] = 0.0;
676     break;
677   default:
678     sys_error(ERR_WARNING, "illegal pelem type %d [makeVectorOutline]", m_type);
679     return;
680   }
681   
682   rotate2d (m_xOutline, m_yOutline, m_nPoints, m_rot);
683   xlat2d (m_xOutline, m_yOutline, m_nPoints, m_cx, m_cy);
684   
685   minmax_array (m_xOutline, m_nPoints, m_xmin, m_xmax);
686   minmax_array (m_yOutline, m_nPoints, m_ymin, m_ymax);
687   
688   // increase pelem extent by SCALE_PELEM_EXTENT to eliminate chance of
689   //   missing actual pelem maximum due to polygonal sampling 
690
691   xfact = (m_xmax - m_xmin) * SCALE_PELEM_EXTENT;
692   yfact = (m_ymax - m_ymin) * SCALE_PELEM_EXTENT;
693
694   m_xmin -= xfact;
695   m_ymin -= yfact;
696   m_xmax += xfact;
697   m_ymax += yfact;
698 }
699
700
701 /* NAME
702  *   calc_arc                   Calculate outline of a arc of a circle
703  *
704  * SYNOPSIS
705  *   calc_arc (x, y, xcent, ycent, pts, r, start, stop)
706  *   double x[], y[];           Array of points
707  *   int pts                    Number of points in array
708  *   double xcent, ycent        Center of cirlce
709  *   double r                   Radius of circle
710  *   double start, stop         Beginning & ending angles
711  */
712
713 void 
714 PhantomElement::calcArcPoints (double x[], double y[], const int pts, const double xcent, const double ycent, const double r, const double start, const double stop)
715 {
716     if (r <= 0.0)
717         sys_error (ERR_WARNING, "negative or zero radius in calc_arc()");
718
719     double theta = (stop - start) / (pts - 1);  // angle incr. between points 
720     double c = cos(theta);
721     double s = sin(theta);
722   
723     x[0] = r * cos (start) + xcent;
724     y[0] = r * sin (start) + ycent;
725
726     double xp = x[0] - xcent;
727     double yp = y[0] - ycent;
728     for (int i = 1; i < pts; i++) {
729         double xc = c * xp - s * yp;
730         double yc = s * xp + c * yp;
731         x[i] = xc + xcent;
732         y[i] = yc + ycent;
733         xp = xc;  yp = yc;
734     }
735 }
736
737
738 // NAME
739 //   PhantomElement::calcEllipsePoints    Calculate outline of a ellipse
740 //
741 // SYNOPSIS
742 //   calcEllipsePoints ()
743 //
744
745
746 void 
747 PhantomElement::calcEllipsePoints (double x[], double y[], const int pts, const double u, const double v)
748 {
749     calcArcPoints (x, y, m_nPoints, 0.0, 0.0, 1.0, 0.0, TWOPI);   // make a unit circle 
750     scale2d (x, y, m_nPoints, m_u, m_v);                             // scale to ellipse 
751 }
752
753
754 /* NAME
755  *   circle_pts         Calculate number of points to use for circle segment
756  *
757  * SYNOPSIS
758  *   n = circle_pts (theta)
759  *   int n              Number of points to use for arc
760  *   double theta       Length of arc in radians
761  */
762
763 int 
764 PhantomElement::numCirclePoints (double theta)
765 {
766     theta = clamp (theta, 0., TWOPI);
767
768     return static_cast<int> (POINTS_PER_CIRCLE * theta / TWOPI + 1.5);
769 }
770
771
772 bool
773 PhantomElement::clipLineWorldCoords (double& x1, double& y1, double& x2, double &y2) const
774 {
775   /* check if ray is outside of pelem extents */
776   double cx1 = x1, cy1 = y1, cx2 = x2, cy2 = y2;
777   if (! clip_rect (cx1, cy1, cx2, cy2, m_rectLimits))
778     return false;
779         
780   // convert phantom coordinates to pelem coordinates 
781   xform_mtx2 (m_xformPhmToObj, x1, y1);
782   xform_mtx2 (m_xformPhmToObj, x2, y2);
783         
784   if (! clipLineNormalizedCoords (x1, y1, x2, y2))
785     return false;
786
787   // convert standard pelem coordinates back to phantom coordinates 
788   xform_mtx2 (m_xformObjToPhm, x1, y1);
789   xform_mtx2 (m_xformObjToPhm, x2, y2);
790
791   return true;
792 }
793
794
795 /* NAME
796  *   pelem_clip_line                    Clip pelem against an arbitrary line
797  *
798  * SYNOPSIS
799  *   pelem_clip_line (pelem, x1, y1, x2, y2)
800  *   PhantomElement& pelem;             Pelem to be clipped
801  *   double *x1, *y1, *x2, *y2  Endpoints of line to be clipped
802  *
803  * RETURNS
804  *   true   if line passes through pelem
805  *              (x1, y1, x2, y2 hold coordinates of new line)
806  *   false  if line do not pass through pelem
807  *              (x1, y1, x2, y2 are undefined)
808  */
809
810 bool
811 PhantomElement::clipLineNormalizedCoords (double& x1, double& y1, double& x2, double& y2) const
812 {
813   bool accept = false;
814
815   switch (m_type) {
816   case PELEM_RECTANGLE:
817     double rect[4];
818     rect[0] = -1.0;  rect[1] = -1.0;
819     rect[2] = 1.0;  rect[3] = 1.0;
820     accept = clip_rect (x1, y1, x2, y2, rect);
821     break;
822   case PELEM_ELLIPSE:
823     accept = clip_circle (x1, y1, x2, y2, 0.0, 0.0, 1.0, 0.0, 0.0);
824     break;
825   case PELEM_TRIANGLE:
826     accept = clip_triangle (x1, y1, x2, y2, 1.0, 1.0, true);
827     break;
828   case PELEM_SEGMENT:
829     accept = clip_segment (x1, y1, x2, y2, m_u, m_v);
830     break;
831   case PELEM_SECTOR:
832     accept = clip_sector (x1, y1, x2, y2, m_u, m_v);
833     break;
834   default:
835     sys_error (ERR_WARNING, "Illegal pelem type %d [pelem_clip_line]", m_type);
836     break;
837   }
838
839   return(accept);
840 }
841
842
843 // METHOD IDENTIFICATION 
844 //    PhantomElement::isPointInside             Check if point is inside pelem
845 //
846 // SYNOPSIS
847 //    is_point_inside (pelem, x, y, coord_type)
848 //    double x, y               Point to see if lies in pelem
849 //    int coord_type            Coordinate type (PELEM_COORD or PHM_COORD)
850 //
851 // RETURNS
852 //    true if point lies within pelem
853 //    false if point lies outside of pelem
854
855 bool
856 PhantomElement::isPointInside (double x, double y, const CoordType coord_type) const
857 {
858   if (coord_type == PHM_COORD) {
859     xform_mtx2 (m_xformPhmToObj, x, y);
860   } else if (coord_type != PELEM_COORD) {
861     sys_error(ERR_WARNING, "Illegal coordinate type in pelem_is_point_inside");
862     return (false);
863   }
864
865   switch (m_type) {
866   case PELEM_RECTANGLE:
867     if (x > 1. || x < -1. || y > 1. || y < -1.)
868       return (false);
869     else
870       return (true);
871     break;
872   case PELEM_TRIANGLE:
873     if (y < 0. || y > 1. - x || y > 1. + x)
874       return (false);
875     else
876       return (true);
877     break;
878   case PELEM_ELLIPSE:
879     if (x > 1. || x < -1. || y > 1. || y < -1.)
880       return (false);
881     if (x * x + y * y > 1.)             // check if inside unit circle
882       return (false);
883     else
884       return (true);
885     break;
886
887     // for clipping segments & sectors, must NOT scale by (1/u, 1/v)
888     // because this destroys information about size of arc component 
889
890   case PELEM_SEGMENT:
891     if (x > 1. || x < -1. || y > 0.)
892         return (false);         // clip against y > 0 
893     x *= m_u;                   // put back u & v scale 
894     y *= m_v;
895     if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
896       return (false);           // clip against circle, r = sqrt(@)
897     else
898       return (true);
899     break;
900   case PELEM_SECTOR:
901       if (x > 1. || x < -1. || y > 1.)   // extent 
902       return (false);
903       if (y > 1. - x || y > 1. + x)      // triangle    
904           return (false);                      // clip against triangle 
905       x *= m_u;                // circle: put back u & v scale 
906     y *= m_v;
907     if (x * x + (y-m_v) * (y-m_v) > m_u * m_u + m_v * m_v)
908         return (false);                // clip against circle 
909     else
910       return (true);
911   break;
912   default:
913     sys_error (ERR_WARNING, "Illegal pelem type in pelem_is_point_inside()");
914     break;
915   }
916
917   return (false);
918 }
919
920