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