651e1ebd8a3a1275648edee15eaf6b05186a49a5
[ctsim.git] / include / phantom.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          phantom.h
5 **   Purpose:       Header file for Phantom objects
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  July 1, 1984
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: phantom.h,v 1.16 2000/12/06 01:46:43 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 #ifndef PHANTOM_H
29 #define PHANTOM_H
30
31 #include <list>
32 #include "ctsupport.h"
33
34 using namespace std;
35
36 typedef enum {
37     PELEM_INVALID,
38     PELEM_RECTANGLE,
39     PELEM_TRIANGLE,
40     PELEM_ELLIPSE,
41     PELEM_SECTOR,
42     PELEM_SEGMENT
43 } PhmElemType;
44
45 /* Codes for Coordinate Types      */
46 /* Defines coords for isPointInside() */
47
48 typedef enum {
49   PELEM_COORD,         /* Normalized PElem Coordinates */
50   PHM_COORD           /* World phantom Coordinates */
51 } CoordType;
52
53
54 class PhantomElement
55 {
56  public:
57     PhantomElement (const char* const type, const double cx, const double cy, const double u, const double v, const double rot, const double atten);
58
59     ~PhantomElement ();
60
61     bool isPointInside (double x, double y, const CoordType coord_type) const;
62
63     bool clipLineNormalizedCoords (double& x1, double& y1, double& x2, double& y2) const;
64  
65     bool clipLineWorldCoords (double& x1, double& y1, double& x2, double& y2) const;
66
67     const int nOutlinePoints() const {return m_nPoints;}
68     double* rectLimits() {return m_rectLimits;}
69     double* xOutline() {return m_xOutline;}
70     double* yOutline() {return m_yOutline;}
71     const double atten() const {return m_atten;}
72     const double diameter() const {return m_diameter;}
73     const double xmin() const {return m_xmin;}
74     const double xmax() const {return m_xmax;}
75     const double ymin() const {return m_ymin;}
76     const double ymax() const {return m_ymax;}
77     const double rot() const {return m_rot;}
78     const double cx() const {return m_cx;}
79     const double cy() const {return m_cy;}
80     const double u() const {return m_u;}
81     const double v() const {return m_v;}
82
83  private:
84     PhmElemType m_type;      // pelem type (box, ellipse, etc)
85     double m_cx, m_cy;       // center of pelem 
86     double m_u, m_v;                 // size of pelem 
87     double m_atten;          // X-ray attenuation coefficient
88     double m_rot;                    // pelem rotation angle (in radians) 
89     double *m_x, *m_y;       // ptr to array of points in obj world coord 
90     int m_nPoints;                   // number of points in outline arrays 
91     double m_xmin, m_xmax, m_ymin, m_ymax;  // pelem limits 
92     double m_diameter;       
93     GRFMTX_2D m_xformPhmToObj;        // map from phantom to normalized pelem coords
94     GRFMTX_2D m_xformObjToPhm;        // map from normalized pelem coords to phantom coords 
95     double* m_xOutline;
96     double* m_yOutline;
97     double  m_rectLimits[4];
98
99     static const int POINTS_PER_CIRCLE;
100     static const double SCALE_PELEM_EXTENT;  // increase pelem limits by 0.5% 
101
102     static PhmElemType PhantomElement::convertNameToType (const char* const typeName);
103
104     void makeTransformMatrices ();
105
106     void makeVectorOutline ();
107
108     void calcArcPoints (double x[], double y[], const int pts, const double xcent, const double ycent, const double r, const double start, const double stop);
109
110     void calcEllipsePoints (double x[], double y[], const int pts, const double u, const double v);
111
112     static int numCirclePoints (double theta);
113
114     PhantomElement (const PhantomElement& rhs);        // copy constructor
115     PhantomElement& operator= (const PhantomElement&); // assignment operator
116 };
117
118
119 typedef enum {
120     P_PELEMS,        // Phantom made of PElems
121     P_UNIT_PULSE,   // Special phantom, not made of pelems 
122     P_FILTER      // defined only by this type
123 } PhantomComposition;
124
125
126 //////////////////////////////////////////////////////
127 // Phantom Class Declaration
128 //////////////////////////////////////////////////////
129
130 class SGP;
131 class ImageFile;
132 class Phantom
133 {
134  public:
135     static const int PHM_INVALID;
136     static const int PHM_HERMAN;
137     static const int PHM_B_HERMAN;
138     static const int PHM_SHEPP_LOGAN;
139     static const int PHM_B_SHEPP_LOGAN;
140     static const int PHM_UNITPULSE;
141
142     Phantom ();
143     Phantom (const char* const phmName);
144
145     ~Phantom ();
146
147     void setComposition (PhantomComposition composition)
148         { m_composition = composition; }
149
150     const PhantomComposition getComposition () const
151         { return m_composition; }
152
153     bool createFromPhantom (const char* const phmName);
154
155     bool createFromPhantom (const int phmid);
156
157     bool createFromFile (const char* const fname);
158
159     void addPElem (const PhantomElement& pelem);
160
161     void addPElem (const char* const composition, const double cx, const double cy, const double u, const double v, const double rot, const double atten);
162
163     void convertToImagefile (ImageFile& im, const int in_nsample, const int trace, const int colStart, const int colCount, bool bStoreAtColumnPos) const;
164
165     void convertToImagefile (ImageFile& im, const int in_nsample, const int trace) const;
166
167     bool fail() const             {return m_fail;}
168     const string& failMessage() const {return m_failMessage;}
169     const string& name() const     {return m_name;}
170     const int id() const     {return m_id;}
171
172 #if HAVE_SGP
173     void show () const;
174     void show (SGP& sgp) const;
175     void draw (SGP& sgp) const;
176 #endif
177     
178     void addStdHerman ();
179     void addStdHermanBordered ();
180     void addStdSheppLogan ();
181     void addStdSheppLoganBordered ();
182
183     void print () const;
184
185     const double maxAxisLength () const {return (((m_xmax - m_xmin) > (m_ymax - m_ymin)) ? (m_xmax - m_xmin) : (m_ymax - m_ymin));}
186
187     const double diameter() const {return m_diameter;}
188     const double xmin() const {return m_xmin;}
189     const double xmax() const {return m_xmax;}
190     const double ymin() const {return m_ymin;}
191     const double ymax() const {return m_ymax;}
192     list<PhantomElement*>& listPElem() {return m_listPElem;}
193     const list<PhantomElement*>& listPElem() const {return m_listPElem;}
194     const int nPElem() const {return m_nPElem;}
195
196     static const int getPhantomCount() {return s_iPhantomCount;}
197     static const char** getPhantomNameArray() {return s_aszPhantomName;}
198     static const char** getPhantomTitleArray() {return s_aszPhantomTitle;}
199     static int convertNameToPhantomID (const char* const phmName);
200     static const char* convertPhantomIDToName (const int phmID);
201     static const char* convertPhantomIDToTitle (const int phmID);
202
203  private:
204     PhantomComposition m_composition;
205     int m_nPElem;                           // number of pelems in phantom 
206     double m_xmin, m_xmax, m_ymin, m_ymax;  // extent of pelems in pelem coordinates
207     double m_diameter;                        // diameter of object
208     mutable list<PhantomElement*> m_listPElem;      // pelem lists
209     string m_name;
210     int m_id;
211     bool m_fail;
212     string m_failMessage;
213
214     static const char* s_aszPhantomName[];
215     static const char* s_aszPhantomTitle[];
216     static const int s_iPhantomCount;
217
218     void init();
219
220     Phantom (const Phantom& rhs);        // copy constructor
221     Phantom& operator= (const Phantom&); // assignment operator
222 };
223
224 typedef list<PhantomElement*>::iterator PElemIterator;
225 typedef list<PhantomElement*>::const_iterator PElemConstIterator;
226
227 #endif