[1142] | 1 | // |
---|
| 2 | // File I/O manager class for writing or reading calcuated dose |
---|
| 3 | // distribution and some event information |
---|
| 4 | // |
---|
| 5 | // |
---|
| 6 | // Mar. 31, 2009 : release for the gMocrenFile driver |
---|
| 7 | // |
---|
| 8 | // Akinori Kimura |
---|
| 9 | // |
---|
| 10 | // gMocren home page: |
---|
| 11 | // http://geant4.kek.jp/gMocren/ |
---|
| 12 | // |
---|
| 13 | #ifndef GMOCRENIO_HH |
---|
| 14 | #define GMOCRENIO_HH |
---|
| 15 | |
---|
| 16 | #include <vector> |
---|
| 17 | #include <string> |
---|
| 18 | #include <fstream> |
---|
| 19 | #include <map> |
---|
| 20 | |
---|
| 21 | // |
---|
| 22 | //----- GMocrenDataPrimitive class -----// |
---|
| 23 | // data primitive class for volume data |
---|
| 24 | // |
---|
| 25 | template <typename T> class GMocrenDataPrimitive { |
---|
| 26 | protected: |
---|
| 27 | int kSize[3]; |
---|
| 28 | double kScale; |
---|
| 29 | T kMinmax[2]; |
---|
| 30 | float kCenter[3]; |
---|
| 31 | std::vector<T *> kImage; |
---|
| 32 | std::string kDataName; |
---|
| 33 | //std::vector<std::vector<T>> image; |
---|
| 34 | |
---|
| 35 | public: |
---|
| 36 | GMocrenDataPrimitive(); |
---|
| 37 | //GMocrenDataPrimitive(GMocrenDataPrimitive<T> & _prim); |
---|
| 38 | ~GMocrenDataPrimitive(); |
---|
| 39 | |
---|
| 40 | GMocrenDataPrimitive<T> & operator = (const GMocrenDataPrimitive<T> & _right); |
---|
| 41 | GMocrenDataPrimitive<T> & operator + (const GMocrenDataPrimitive<T> & _right); |
---|
| 42 | GMocrenDataPrimitive<T> & operator += (const GMocrenDataPrimitive<T> & _right); |
---|
| 43 | |
---|
| 44 | void clear(); |
---|
[1228] | 45 | void clearImage(); |
---|
[1142] | 46 | void setSize(int _size[3]); |
---|
| 47 | void getSize(int _size[3]); |
---|
| 48 | void setScale(double & _scale); |
---|
| 49 | double getScale(); |
---|
| 50 | void setMinMax(T _minmax[2]); |
---|
| 51 | void getMinMax(T _minmax[2]); |
---|
| 52 | void setImage(std::vector<T *> & _image); |
---|
| 53 | void addImage(T * _image); |
---|
| 54 | std::vector<T *> & getImage(); |
---|
| 55 | T * getImage(int _z); // get image of each layer |
---|
| 56 | void setCenterPosition(float _center[3]); |
---|
| 57 | void getCenterPosition(float _center[3]); |
---|
| 58 | void setName(std::string & _name); |
---|
| 59 | std::string getName(); |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | // |
---|
| 64 | //----- GMocrenTrack class -----// |
---|
| 65 | // |
---|
| 66 | class GMocrenTrack { |
---|
| 67 | public: |
---|
| 68 | struct Step { |
---|
| 69 | float startPoint[3]; |
---|
| 70 | float endPoint[3]; |
---|
| 71 | }; |
---|
| 72 | protected: |
---|
| 73 | std::vector<struct Step> kTrack; |
---|
| 74 | unsigned char kColor[3]; |
---|
| 75 | |
---|
| 76 | public: |
---|
| 77 | GMocrenTrack(); |
---|
| 78 | ~GMocrenTrack(){;} |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | int getNumberOfSteps() {return (int)kTrack.size();} |
---|
| 82 | void addStep(float _startx, float _starty, float _startz, |
---|
| 83 | float _endx, float _endy, float _endz); |
---|
| 84 | void getStep(float & _startx, float & _starty, float & _startz, |
---|
| 85 | float & _endx, float & _endy, float & _endz, |
---|
| 86 | int _num); |
---|
| 87 | void setTrack(std::vector<struct Step> & _aTrack) {kTrack = _aTrack;} |
---|
| 88 | void setColor(unsigned char _color[3]) { |
---|
| 89 | for(int i = 0; i < 3; i++) kColor[i] = _color[i]; |
---|
| 90 | } |
---|
| 91 | void getColor(unsigned char _color[3]) { |
---|
| 92 | for(int i = 0; i < 3; i++) _color[i] = kColor[i]; |
---|
| 93 | } |
---|
| 94 | void translate(std::vector<float> & _tranlate); |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | // |
---|
| 100 | //----- GMocrenDetector class -----// |
---|
| 101 | // |
---|
| 102 | class GMocrenDetector { |
---|
| 103 | public: |
---|
| 104 | struct Edge { |
---|
| 105 | float startPoint[3]; |
---|
| 106 | float endPoint[3]; |
---|
| 107 | }; |
---|
| 108 | protected: |
---|
| 109 | std::vector<struct Edge> kDetector; |
---|
| 110 | unsigned char kColor[3]; |
---|
| 111 | std::string kName; |
---|
| 112 | |
---|
| 113 | public: |
---|
| 114 | GMocrenDetector(); |
---|
| 115 | ~GMocrenDetector(){;} |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | int getNumberOfEdges() {return (int)kDetector.size();} |
---|
| 119 | void addEdge(float _startx, float _starty, float _startz, |
---|
| 120 | float _endx, float _endy, float _endz); |
---|
| 121 | void getEdge(float & _startx, float & _starty, float & _startz, |
---|
| 122 | float & _endx, float & _endy, float & _endz, |
---|
| 123 | int _num); |
---|
| 124 | void setDetector(std::vector<struct Edge> & _aDetector) {kDetector = _aDetector;} |
---|
| 125 | void setColor(unsigned char _color[3]) { |
---|
| 126 | for(int i = 0; i < 3; i++) kColor[i] = _color[i]; |
---|
| 127 | } |
---|
| 128 | void getColor(unsigned char _color[3]) { |
---|
| 129 | for(int i = 0; i < 3; i++) _color[i] = kColor[i]; |
---|
| 130 | } |
---|
| 131 | void setName(std::string & _name) { kName = _name;} |
---|
| 132 | std::string getName() {return kName;} |
---|
| 133 | |
---|
| 134 | void translate(std::vector<float> & _tranlate); |
---|
| 135 | }; |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | // |
---|
| 139 | //----- G4GMocrenIO class -----// |
---|
| 140 | // |
---|
| 141 | class G4GMocrenIO { |
---|
| 142 | public: |
---|
| 143 | // file id |
---|
| 144 | static std::string kId; |
---|
| 145 | |
---|
| 146 | // file version |
---|
| 147 | static std::string kVersion; |
---|
| 148 | |
---|
| 149 | // data file name |
---|
| 150 | static std::string kFileName; |
---|
| 151 | |
---|
| 152 | // file data endian: little or not |
---|
| 153 | static char kLittleEndianInput; |
---|
| 154 | static char kLittleEndianOutput; |
---|
| 155 | |
---|
| 156 | static std::string kComment; |
---|
| 157 | |
---|
| 158 | // number of events |
---|
| 159 | static int kNumberOfEvents; |
---|
| 160 | |
---|
| 161 | // pointer to the modality image data |
---|
| 162 | static unsigned int kPointerToModalityData; |
---|
| 163 | // pointer to the dose distribution image data |
---|
| 164 | static std::vector<unsigned int> kPointerToDoseDistData; |
---|
| 165 | // pointer to the ROI image data |
---|
| 166 | static unsigned int kPointerToROIData; |
---|
| 167 | // pointer to the track data |
---|
| 168 | static unsigned int kPointerToTrackData; |
---|
| 169 | // pointer to the detector data |
---|
| 170 | static unsigned int kPointerToDetectorData; |
---|
| 171 | |
---|
| 172 | // voxel spacing (universal size) |
---|
| 173 | static float kVoxelSpacing[3]; |
---|
| 174 | |
---|
| 175 | //----- modality image -----// |
---|
| 176 | static class GMocrenDataPrimitive<short> kModality; |
---|
| 177 | // density map to modality (CT) values |
---|
| 178 | static std::vector<float> kModalityImageDensityMap; |
---|
| 179 | static std::string kModalityUnit; |
---|
| 180 | |
---|
| 181 | //----- dose distribution -----// |
---|
| 182 | static std::vector<class GMocrenDataPrimitive<double> > kDose; |
---|
| 183 | //std::vector<short *> kShortDose; |
---|
| 184 | static std::string kDoseUnit; |
---|
| 185 | |
---|
| 186 | //----- RoI -----// |
---|
| 187 | static std::vector<class GMocrenDataPrimitive<short> > kRoi; |
---|
| 188 | |
---|
| 189 | //----- track information -----// |
---|
| 190 | static std::vector<float *> kSteps; // begin (x,y,z), end (x,y,z) |
---|
| 191 | static std::vector<unsigned char *> kStepColors; // r, g, b |
---|
| 192 | |
---|
| 193 | static std::vector<class GMocrenTrack> kTracks; |
---|
| 194 | bool kTracksWillBeStored; |
---|
| 195 | |
---|
| 196 | //----- detector information -----// |
---|
| 197 | static std::vector<class GMocrenDetector> kDetectors; |
---|
| 198 | |
---|
| 199 | //----- verbose information -----// |
---|
| 200 | static int kVerbose; // verbose level : 0 - 5 (none - overtalk) |
---|
| 201 | |
---|
| 202 | public: |
---|
| 203 | // constructor |
---|
| 204 | G4GMocrenIO(); |
---|
| 205 | // destructor |
---|
| 206 | ~G4GMocrenIO(); |
---|
| 207 | |
---|
| 208 | // initialize |
---|
| 209 | void initialize(); |
---|
| 210 | |
---|
| 211 | // set the gMocren data file name |
---|
| 212 | void setFileName(std::string & _filename) {kFileName = _filename;} |
---|
| 213 | void setFileName(char * _filename) {kFileName = _filename;} |
---|
| 214 | // get the gMocren data file name |
---|
| 215 | std::string & getFileName() {return kFileName;} |
---|
| 216 | // store all data in the gMocren data file |
---|
| 217 | bool storeData(char * _filename); // interface for version 4 |
---|
| 218 | bool storeData(); |
---|
| 219 | bool storeData2(char * _filename); // version 2 |
---|
| 220 | bool storeData2(); |
---|
| 221 | bool storeData3(char * _filename); // version 3 |
---|
| 222 | bool storeData3(); |
---|
| 223 | bool storeData4(char * _filename); // version 4 |
---|
| 224 | bool storeData4(); |
---|
| 225 | // retrieve all data from the gMocren data file |
---|
| 226 | bool retrieveData(char * _filename); // interface |
---|
| 227 | bool retrieveData(); |
---|
| 228 | bool retrieveData2(char * _filename); //version 2 |
---|
| 229 | bool retrieveData2(); |
---|
| 230 | bool retrieveData3(char * _filename); // version 3 |
---|
| 231 | bool retrieveData3(); |
---|
| 232 | bool retrieveData4(char * _filename); // version 4 |
---|
| 233 | bool retrieveData4(); |
---|
| 234 | |
---|
| 235 | // get & set the file id |
---|
| 236 | std::string & getID() {return kId;} |
---|
| 237 | void setID(); |
---|
| 238 | void setID(std::string & _id) {kId = _id;} |
---|
| 239 | |
---|
| 240 | // get & set the file version |
---|
| 241 | std::string & getVersion(); |
---|
| 242 | void setVersion(std::string & _version); |
---|
| 243 | |
---|
| 244 | // set endians of input/output data |
---|
| 245 | void setLittleEndianInput(bool _little); |
---|
| 246 | void setLittleEndianOutput(bool _little); |
---|
| 247 | |
---|
| 248 | // get & set comment |
---|
| 249 | std::string & getComment() {return kComment;} |
---|
| 250 | void setComment(std::string & _comment) {kComment = _comment;} |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | // voxel spacing |
---|
| 254 | void setVoxelSpacing(float _spacing[3]); |
---|
| 255 | void getVoxelSpacing(float _spacing[3]); |
---|
| 256 | |
---|
| 257 | // get & set number of events |
---|
| 258 | int & getNumberOfEvents(); |
---|
| 259 | void setNumberOfEvents(int & _numberOfEvents); |
---|
| 260 | void addOneEvent(); |
---|
| 261 | |
---|
| 262 | // set pointer the modality image data |
---|
| 263 | void setPointerToModalityData(unsigned int & _pointer); |
---|
| 264 | unsigned int getPointerToModalityData(); |
---|
| 265 | // set pointer the dose distribution image data |
---|
| 266 | void addPointerToDoseDistData(unsigned int & _pointer); |
---|
| 267 | unsigned int getPointerToDoseDistData(int _elem = 0); |
---|
| 268 | // set pointer the ROI image data |
---|
| 269 | void setPointerToROIData(unsigned int & _pointer); |
---|
| 270 | unsigned int getPointerToROIData(); |
---|
| 271 | // set pointer the track data |
---|
| 272 | void setPointerToTrackData(unsigned int & _pointer); |
---|
| 273 | unsigned int getPointerToTrackData(); |
---|
| 274 | private: |
---|
| 275 | // calculate pointers |
---|
| 276 | void calcPointers4(); |
---|
| 277 | void calcPointers3(); |
---|
| 278 | void calcPointers2(); |
---|
| 279 | |
---|
| 280 | |
---|
| 281 | //----- Modality image -----// |
---|
| 282 | public: |
---|
| 283 | // get & set the modality image size |
---|
| 284 | void getModalityImageSize(int _size[3]); |
---|
| 285 | void setModalityImageSize(int _size[3]); |
---|
| 286 | // get & set the modality image spacing size |
---|
| 287 | void getModalityImageVoxelSpacing(float _size[3]); // un-usable |
---|
| 288 | void setModalityImageVoxelSpacing(float _size[3]); // un-usable |
---|
| 289 | // get & set the modality image size |
---|
| 290 | void setModalityImageScale(double & _scale); |
---|
| 291 | double getModalityImageScale(); |
---|
| 292 | // set the modality image in CT |
---|
| 293 | void setModalityImage(short * _image); |
---|
| 294 | short * getModalityImage(int _z); |
---|
[1228] | 295 | void clearModalityImage(); |
---|
[1142] | 296 | // set/get the modality image density map |
---|
| 297 | void setModalityImageDensityMap(std::vector<float> & _map); |
---|
| 298 | std::vector<float> & getModalityImageDensityMap(); |
---|
| 299 | // set the modality image min./max. |
---|
| 300 | void setModalityImageMinMax(short _minmax[2]); |
---|
| 301 | // get min. & max. of the modality image |
---|
| 302 | void getModalityImageMinMax(short _minmax[2]); |
---|
| 303 | short getModalityImageMax(); |
---|
| 304 | short getModalityImageMin(); |
---|
| 305 | // set center of the modality image position |
---|
| 306 | void setModalityCenterPosition(float _center[3]); |
---|
| 307 | void getModalityCenterPosition(float _center[3]); |
---|
| 308 | // get & set the modality image unit |
---|
| 309 | std::string getModalityImageUnit(); |
---|
| 310 | void setModalityImageUnit(std::string & _unit); |
---|
| 311 | |
---|
| 312 | short convertDensityToHU(float & _dens); |
---|
| 313 | |
---|
| 314 | //----- Dose distribution -----// |
---|
| 315 | |
---|
| 316 | // instanciate a dose distribution data object |
---|
| 317 | void newDoseDist(); |
---|
| 318 | // get number of dose distribion data |
---|
| 319 | int getNumDoseDist(); |
---|
| 320 | // get & set the dose distribution unit |
---|
| 321 | std::string getDoseDistUnit(int _num = 0); |
---|
| 322 | void setDoseDistUnit(std::string & _unit, int _num = 0); |
---|
| 323 | // get & set the dose distribution image size |
---|
| 324 | void getDoseDistSize(int _size[3], int _num = 0); |
---|
| 325 | void setDoseDistSize(int _size[3], int _num = 0); |
---|
| 326 | // get min. & max. of the dose distribution image |
---|
| 327 | void setDoseDistMinMax(short _minmax[2], int _num = 0); |
---|
| 328 | void getDoseDistMinMax(short _minmax[2], int _num = 0); |
---|
| 329 | // get min. & max. of the dose distribution |
---|
| 330 | void setDoseDistMinMax(double _minmax[2], int _num = 0); |
---|
| 331 | void getDoseDistMinMax(double _minmax[2], int _num = 0); |
---|
| 332 | // get & set scale value of the dose distribution for the image |
---|
| 333 | void setDoseDistScale(double & _scale, int _num = 0); |
---|
| 334 | double getDoseDistScale(int _num = 0); |
---|
| 335 | // set the dose distribution image |
---|
| 336 | void setShortDoseDist(short * _image, int _num = 0); |
---|
| 337 | void getShortDoseDist(short * _data, int _z, int _num = 0); |
---|
| 338 | void getShortDoseDistMinMax(short _minmax[2], int _num = 0); |
---|
| 339 | // set the dose distribution |
---|
| 340 | void setDoseDist(double * _image, int _num = 0); |
---|
| 341 | double * getDoseDist(int _z, int _num = 0); |
---|
| 342 | // add another dose ditribution map to this map |
---|
| 343 | bool addDoseDist(std::vector<double *> & _image, int _num = 0); |
---|
| 344 | |
---|
| 345 | // get & get center position of calculated dose region |
---|
| 346 | void getDoseDistCenterPosition(float _center[3], int _num = 0); |
---|
| 347 | void setDoseDistCenterPosition(float _center[3], int _num = 0); |
---|
| 348 | |
---|
| 349 | // get & get name of calculated dose distribution |
---|
| 350 | std::string getDoseDistName(int _num = 0); |
---|
| 351 | void setDoseDistName(std::string _name, int _num = 0); |
---|
| 352 | |
---|
| 353 | // copy dose distributions |
---|
| 354 | void copyDoseDist(std::vector<class GMocrenDataPrimitive<double> > & _dose); |
---|
| 355 | // merge two dose distributions |
---|
| 356 | bool mergeDoseDist(std::vector<class GMocrenDataPrimitive<double> > & _dose); |
---|
| 357 | |
---|
| 358 | // clear all dose distributions |
---|
| 359 | void clearDoseDistAll(); |
---|
| 360 | protected: |
---|
| 361 | // check whether dose variable is empty or not |
---|
| 362 | bool isDoseEmpty(); |
---|
| 363 | // calcuated scale value to convert dose distribution into image |
---|
| 364 | void calcDoseDistScale(); |
---|
| 365 | |
---|
| 366 | public: |
---|
| 367 | //----- RoI -----// |
---|
| 368 | |
---|
| 369 | // instanciate an RoI data object |
---|
| 370 | void newROI(); |
---|
| 371 | // get number of RoI data |
---|
| 372 | int getNumROI(); |
---|
| 373 | // get & set the ROI image scale |
---|
| 374 | double getROIScale(int _num = 0); |
---|
| 375 | void setROIScale(double & _scale, int _num = 0); |
---|
| 376 | // get & set the ROI image |
---|
| 377 | short * getROI(int _z, int _num = 0); |
---|
| 378 | void setROI(short * _image, int _num = 0); |
---|
| 379 | // get & set the ROI image size |
---|
| 380 | void getROISize(int _size[3], int _num = 0); |
---|
| 381 | void setROISize(int _size[3], int _num = 0); |
---|
| 382 | // get & set position of the ROI region center |
---|
| 383 | void getROICenterPosition(float _center[3], int _num = 0); |
---|
| 384 | void setROICenterPosition(float _center[3], int _num = 0); |
---|
| 385 | // get & set the ROI image min. and max. |
---|
| 386 | void getROIMinMax(short _minmax[2], int _num = 0); |
---|
| 387 | void setROIMinMax(short _minmax[2], int _num = 0); |
---|
| 388 | void clearROIAll(); |
---|
| 389 | protected: |
---|
| 390 | // check whether RoI variable is empty or not |
---|
| 391 | bool isROIEmpty(); |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | public: |
---|
| 395 | //----- Track -----// |
---|
| 396 | // get number of tracks |
---|
| 397 | int getNumTracks(); |
---|
| 398 | int getNumTracks4(); |
---|
| 399 | // get & set tracks |
---|
| 400 | std::vector<float *> & getTracks(); |
---|
| 401 | void getTrack(int _num, std::vector<float *> & _steps, |
---|
| 402 | std::vector<unsigned char * > & _color); |
---|
| 403 | void addTrack(float * _tracks); |
---|
| 404 | void setTracks(std::vector<float *> & _tracks); |
---|
| 405 | std::vector<unsigned char *> & getTrackColors(); |
---|
| 406 | void addTrackColor(unsigned char * _colors); |
---|
| 407 | void setTrackColors(std::vector<unsigned char *> & _trackColors); |
---|
| 408 | void copyTracks(std::vector<float *> & _tracks, std::vector<unsigned char *> & _colors); |
---|
| 409 | void mergeTracks(std::vector<float *> & _tracks, std::vector<unsigned char *> & _colors); |
---|
| 410 | void addTrack(std::vector<float *> & _steps, unsigned char _color[3]); |
---|
| 411 | |
---|
| 412 | void notStoredTracks() {kTracksWillBeStored = false;}; |
---|
| 413 | void translateTracks(std::vector<float> & _translateo); |
---|
| 414 | void clearTracks() {kTracks.clear();} |
---|
| 415 | |
---|
| 416 | |
---|
| 417 | //----- Detectors -----// |
---|
| 418 | // get number of detectors |
---|
| 419 | int getNumberOfDetectors(); |
---|
| 420 | // add one detector which consists of edges (float[6]) |
---|
| 421 | void addDetector(std::string & _name, std::vector<float *> & _det, unsigned char _color[3]); |
---|
| 422 | void getDetector(int _num, std::vector<float *> & _edges, |
---|
| 423 | std::vector<unsigned char *> & _color, |
---|
| 424 | std::string & _detectorName); |
---|
| 425 | void translateDetector(std::vector<float> & _translate); |
---|
| 426 | void clearDetector() {kDetectors.clear();} |
---|
| 427 | |
---|
| 428 | protected: |
---|
| 429 | // endian conversion |
---|
| 430 | template <typename Type> void convertEndian(char *, Type &); |
---|
| 431 | // byte order inversion |
---|
| 432 | template <typename T> void invertByteOrder(char * _val, T & _rval); |
---|
| 433 | |
---|
| 434 | |
---|
| 435 | public: |
---|
| 436 | //----- verbose information -----// |
---|
| 437 | void setVerboseLevel(int _level); |
---|
| 438 | |
---|
| 439 | }; |
---|
| 440 | |
---|
| 441 | #endif |
---|
| 442 | |
---|