0b66b49077e2bfd8dc81b60972c33c78d9c98363
[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-2009 Kevin Rosenberg
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25
26 #ifndef PHANTOM_H
27 #define PHANTOM_H
28
29 #include <list>
30 #include "ctsupport.h"
31
32 typedef enum {
33     PELEM_INVALID,
34     PELEM_RECTANGLE,
35     PELEM_TRIANGLE,
36     PELEM_ELLIPSE,
37     PELEM_SECTOR,
38     PELEM_SEGMENT
39 } PhmElemType;
40
41 /* Codes for Coordinate Types      */
42 /* Defines coords for isPointInside() */
43
44 typedef enum {
45   PELEM_COORD,         /* Normalized PElem Coordinates */
46   PHM_COORD           /* World phantom Coordinates */
47 } CoordType;
48
49
50 class PhantomElement
51 {
52  public:
53     PhantomElement (const char* const type, const double cx, const double cy, const double u, const double v, const double rot, const double atten);
54
55     ~PhantomElement ();
56
57     bool isPointInside (double x, double y, const CoordType coord_type) const;
58
59     bool clipLineNormalizedCoords (double& x1, double& y1, double& x2, double& y2) const;
60
61     bool clipLineWorldCoords (double& x1, double& y1, double& x2, double& y2) const;
62
63     const int nOutlinePoints() const {return m_nPoints;}
64     double* rectLimits() {return m_rectLimits;}
65     double* xOutline() {return m_xOutline;}
66     double* yOutline() {return m_yOutline;}
67     double* const xOutline() const {return m_xOutline;}
68     double* const yOutline() const {return m_yOutline;}
69     const double atten() const {return m_atten;}
70     const double xmin() const {return m_xmin;}
71     const double xmax() const {return m_xmax;}
72     const double ymin() const {return m_ymin;}
73     const double ymax() const {return m_ymax;}
74     const double rot() const {return m_rot;}
75     const double cx() const {return m_cx;}
76     const double cy() const {return m_cy;}
77     const double u() const {return m_u;}
78     const double v() const {return m_v;}
79
80     static PhmElemType convertNameToType (const char* const typeName);
81
82     void printDefinition (std::ostream& os) const;
83     void printDefinition (std::ostringstream& os) const;
84
85  private:
86     PhmElemType m_type;      // pelem type (box, ellipse, etc)
87     double m_cx, m_cy;       // center of pelem
88     double m_u, m_v;                 // size of pelem
89     double m_atten;          // X-ray attenuation coefficient
90     double m_rot;                    // pelem rotation angle (in radians)
91     double *m_x, *m_y;       // ptr to array of points in obj world coord
92     int m_nPoints;                   // number of points in outline arrays
93     double m_xmin, m_xmax, m_ymin, m_ymax;  // pelem limits
94     GRFMTX_2D m_xformPhmToObj;        // map from phantom to normalized pelem coords
95     GRFMTX_2D m_xformObjToPhm;        // map from normalized pelem coords to phantom coords
96     double* m_xOutline;
97     double* m_yOutline;
98     double  m_rectLimits[4];
99
100     static const int POINTS_PER_CIRCLE;
101     static const double SCALE_PELEM_EXTENT;  // increase pelem limits by 0.5%
102
103     static const char* const convertTypeToName (PhmElemType iType);
104
105     void makeTransformMatrices ();
106
107     void makeVectorOutline ();
108
109     void calcArcPoints (double x[], double y[], const int pts, const double xcent, const double ycent, const double r, const double start, const double stop);
110
111     void calcEllipsePoints (double x[], double y[], const int pts, const double u, const double v);
112
113     static int numCirclePoints (double theta);
114
115     PhantomElement (const PhantomElement& rhs);        // copy constructor
116     PhantomElement& operator= (const PhantomElement&); // assignment operator
117 };
118
119
120 typedef enum {
121     P_PELEMS,        // Phantom made of PElems
122     P_UNIT_PULSE,   // Special phantom, not made of pelems
123     P_FILTER      // defined only by this type
124 } PhantomComposition;
125
126
127 //////////////////////////////////////////////////////
128 // Phantom Class Declaration
129 //////////////////////////////////////////////////////
130
131 class SGP;
132 class ImageFile;
133 class Phantom
134 {
135  public:
136     static const int PHM_INVALID;
137     static const int PHM_HERMAN;
138     static const int PHM_SHEPP_LOGAN;
139     static const int PHM_UNITPULSE;
140
141     Phantom ();
142     Phantom (const char* const phmName);
143
144     ~Phantom ();
145
146     void setComposition (PhantomComposition composition)
147         { m_composition = composition; }
148
149     const PhantomComposition getComposition () const
150         { return m_composition; }
151
152     bool createFromPhantom (const char* const phmName);
153
154     bool createFromPhantom (const int phmid);
155
156     bool createFromFile (const char* const fname);
157
158     bool fileWrite (const char* const fname);
159
160     void addPElem (const PhantomElement& pelem);
161
162     void addPElem (const char* const composition, const double cx, const double cy, const double u, const double v, const double rot, const double atten);
163
164     void convertToImagefile (ImageFile& im, double dViewRatio, const int in_nsample, const int trace) const;
165     void convertToImagefile (ImageFile& im, double dViewRatio, const int in_nsample, const int trace,
166       const int colStart, const int colCount, bool bStoreAtColumnPos) const;
167     void convertToImagefile (ImageFile& im, int iNX, double dViewRatio, const int in_nsample, const int trace,
168       const int colStart, const int colCount, int iStorageOffset) const;
169
170     void printDefinitions (std::ostream& os) const;
171     void printDefinitions (std::ostringstream& os) const;
172
173     bool fail() const             {return m_fail;}
174     const std::string& failMessage() const {return m_failMessage;}
175     const std::string& name() const     {return m_name;}
176     const int id() const     {return m_id;}
177
178 #ifdef HAVE_SGP
179     void show () const;
180     void show (SGP& sgp) const;
181     void draw (SGP& sgp) const;
182 #endif
183
184     void addStdHerman ();
185     void addStdSheppLogan ();
186
187     void print (std::ostream& os) const;
188     void print (std::ostringstream& os) const;
189
190     double maxAxisLength () const
191     {  return maxValue<double> (m_xmax - m_xmin, m_ymax - m_ymin); }
192
193     double getDiameterBoundaryCircle() const
194     { return SQRT2 * maxAxisLength(); }
195
196     const double xmin() const {return m_xmin;}
197     const double xmax() const {return m_xmax;}
198     const double ymin() const {return m_ymin;}
199     const double ymax() const {return m_ymax;}
200           std::list<PhantomElement*>& listPElem() {return m_listPElem;}
201     const std::list<PhantomElement*>& listPElem() const {return m_listPElem;}
202     const int nPElem() const {return m_nPElem;}
203
204     static const int getPhantomCount() {return s_iPhantomCount;}
205     static const char** getPhantomNameArray() {return s_aszPhantomName;}
206     static const char** getPhantomTitleArray() {return s_aszPhantomTitle;}
207     static int convertNameToPhantomID (const char* const phmName);
208     static const char* convertPhantomIDToName (const int phmID);
209     static const char* convertPhantomIDToTitle (const int phmID);
210
211  private:
212     PhantomComposition m_composition;
213     int m_nPElem;                           // number of pelems in phantom
214     double m_xmin, m_xmax, m_ymin, m_ymax;  // extent of pelems in pelem coordinates
215     mutable std::list<PhantomElement*> m_listPElem;      // pelem lists
216     std::string m_name;
217     int m_id;
218     bool m_fail;
219     std::string m_failMessage;
220     static const char* s_aszPhantomName[];
221     static const char* s_aszPhantomTitle[];
222     static const int s_iPhantomCount;
223
224     void init();
225
226     Phantom (const Phantom& rhs);        // copy constructor
227     Phantom& operator= (const Phantom&); // assignment operator
228 };
229
230 typedef std::list<PhantomElement*>::iterator PElemIterator;
231 typedef std::list<PhantomElement*>::const_iterator PElemConstIterator;
232
233 #endif