44 void EGS_VoxelizedShape::EGS_VoxelizedShapeFormat0(
const char *fname,
57 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
58 otype =
"voxelized_shape";
62 ifstream data(fname,ios::binary);
64 egsWarning(
"%s: failed to open file %s\n",func,fname);
71 egsWarning(
"%s: failed to read endianess and format from %s\n",func,fname);
74 if (form < 0 || form > 1) {
75 egsWarning(
"%s: unknwon format %d found in file %s\n",func,(
int)form,fname);
79 egsWarning(
"%s: data in %s from a machine with different endianess.\n"
80 " Byte swaping not implemented yet.\n",func,fname);
84 data.read((
char *)&snx,
sizeof(
short));
85 data.read((
char *)&sny,
sizeof(
short));
86 data.read((
char *)&snz,
sizeof(
short));
88 egsWarning(
"%s: failed to read number of voxels from %s\n",func,fname);
91 if (snx < 1 || snx > 10000 || sny < 1 || sny > 10000 || snz < 1 || snz > 10000) {
92 egsWarning(
"%s: number of voxels seems strange: nx=%d, ny=%d, nz=%d\n",
101 egsInformation(
"Distribution has %d x %d x %d voxels\n",nx,ny,nz);
102 float *x =
new float [nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
103 data.read((
char *)x, (nx+1)*
sizeof(
float));
104 data.read((
char *)y, (ny+1)*
sizeof(
float));
105 data.read((
char *)z, (nz+1)*
sizeof(
float));
107 egsWarning(
"%s: failed to read voxel boundaries\n",func);
116 p =
new float [nreg];
118 data.read((
char *)p, nreg*
sizeof(
float));
120 egsWarning(
"%s: failed to read probabilities\n",func);
131 data.read((
char *)&nmap,
sizeof(
int));
132 if (data.fail() || nmap < 1) {
139 map =
new int [nmap];
140 p =
new float [nmap];
141 data.read((
char *)map, nmap*
sizeof(
int));
142 data.read((
char *)p, nmap*
sizeof(
float));
144 egsWarning(
"%s: failed to read probabilities and bin numbers\n",func);
153 EGS_Float *p1 =
new EGS_Float [nmap];
155 for (j=0; j<nmap; ++j) {
161 xpos =
new EGS_Float [nx+1];
162 ypos =
new EGS_Float [ny+1];
163 zpos =
new EGS_Float [nz+1];
164 for (j=0; j<=nx; ++j) {
167 for (j=0; j<=ny; ++j) {
170 for (j=0; j<=nz; ++j) {
182 prob(0), xpos(0), ypos(0), zpos(0), map(0), nx(0), ny(0), nz(0), nxy(0),
184 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
185 if (file_format == 0) {
187 EGS_VoxelizedShapeFormat0(s.c_str());
190 if (file_format != 1) {
191 egsWarning(
"%s: unknown file format = %d\n",func,file_format);
195 otype =
"voxelized_shape";
199 ifstream h_file(fname);
201 egsWarning(
"%s: failed to open file %s\n",func,fname);
204 string data_file(
"");
207 float scale_x=0.0, scale_y=0.0, scale_z=0.0;
208 for (EGS_I64 loopCount=0; loopCount<=
loopMax; ++loopCount) {
210 egsFatal(
"EGS_VoxelizedShape::EGS_VoxelizedShape: Too many iterations were required! Input may be invalid, or consider increasing loopMax.");
213 string line, key, value;
215 getline(h_file, line);
216 if (h_file.eof() || h_file.fail() || !h_file.good()) {
219 pos = line.find(
":=");
220 if (pos == string::npos) {
223 key = line.substr(0,
int(pos));
224 value = line.substr(
int(pos)+2);
225 while ((key[0] ==
'!') || (key[0] ==
' ')) {
228 while (key[key.length()-1] ==
' ') {
229 key.erase(key.length()-1, 1);
231 while (value[0] ==
' ') {
234 while (value[value.length()-1] ==
' ') {
235 value.erase(value.length()-1, 1);
237 if (key ==
"matrix size [1]") {
238 sscanf(value.c_str(),
"%u", &Nx);
240 else if (key ==
"matrix size [2]") {
241 sscanf(value.c_str(),
"%u", &Ny);
243 else if ((key ==
"number of slices") || (key ==
"number of images")) {
244 sscanf(value.c_str(),
"%u", &Nz);
246 else if (key ==
"scaling factor (mm/pixel) [1]") {
247 sscanf(value.c_str(),
"%f", &scale_x);
249 else if (key ==
"scaling factor (mm/pixel) [2]") {
250 sscanf(value.c_str(),
"%f", &scale_y);
252 else if (key ==
"slice thickness (pixels)") {
253 sscanf(value.c_str(),
"%f", &scale_z);
255 else if (key ==
"name of data file") {
258 else if (key ==
"number format") {
259 if ((value ==
"float") || (value ==
"FLOAT")) {
262 else if ((value ==
"unsigned integer") || (value ==
"UNSIGNED INTEGER")) {
265 else if ((value ==
"signed integer") || (value ==
"SIGNED INTEGER")) {
269 egsWarning(
"%s: unrecognised 'number format' type: %s \n",func,value.c_str());
273 if (Nx < 1 || Ny < 1 || Nz < 1 || scale_x <= 0.0 || scale_y <= 0.0 || scale_z <= 0.0 || data_file ==
"" || data_type == -1) {
274 egsWarning(
"%s: invalid interfile header information: "
275 "Nx=%d Ny=%d Nz=%d scale_x=%f scale_y=%f scale_z=%f "
276 "data_file='%s' number_format=%d\n",func,Nx,Ny,Nz,scale_x,scale_y,scale_z,data_file.c_str(),data_type);
285 float *x =
new float [
nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
291 for (j=0; j<=
nx; j++) {
292 x[j] = -(float)
nx*scale_x/2.0 + j*scale_x;
294 for (j=0; j<=ny; j++) {
295 y[j] = -(float)ny*scale_y/2.0 + j*scale_y;
297 for (j=0; j<=nz; j++) {
298 z[j] = -(float)nz*scale_z/2.0 + j*scale_z;
301 ifstream i_file(data_file.c_str(),ios::binary);
303 egsWarning(
"%s: failed to open interfile data "
304 "%s\n",func,data_file.c_str());
308 float *p =
new float [nreg];
309 if (data_type == 0) {
310 i_file.read((
char *)p, nreg*
sizeof(
float));
312 else if (data_type == 1) {
313 unsigned short int *p_tmp =
new unsigned short int [nreg];
314 i_file.read((
char *)p_tmp, nreg*
sizeof(
unsigned short int));
315 for (
int cc = 0; cc<nreg; cc++) {
316 p[cc] = (float)(p_tmp[cc]);
321 short int *p_tmp =
new short int [nreg];
322 i_file.read((
char *)p_tmp, nreg*
sizeof(
short int));
323 for (
int cc = 0; cc<nreg; cc++) {
324 p[cc] = (float)(p_tmp[cc]);
328 EGS_Float *p1 =
new EGS_Float [nmap];
330 for (j=0; j<nmap; ++j) {
336 xpos =
new EGS_Float [
nx+1];
337 ypos =
new EGS_Float [ny+1];
338 zpos =
new EGS_Float [nz+1];
339 for (j=0; j<=
nx; ++j) {
342 for (j=0; j<=ny; ++j) {
345 for (j=0; j<=nz; ++j) {
356 EGS_VoxelizedShape::~EGS_VoxelizedShape() {
373 static const char *func =
"createShape(voxelized shape)";
379 int err = input->
getInput(
"file name",fname);
381 int err2 = input->
getInput(
"file format",file_format);
383 egsWarning(
"%s: missing 'file name' input\n",func);
387 egsInformation(
"%s: 'file format' input missing. Using default 'binary'"
388 "file format \n",func);
392 if (!shape->isValid()) {
const EGS_I64 loopMax
The maximum number of iterations for near-infinite loops.
int nx
! Voxel map (for type=1)
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
Global egspp functions header file.
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
EGS_VoxelizedShape(int file_format, const char *fname, const string &Name="", EGS_ObjectFactory *f=0)
Conctructor.
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
A "voxelized shape".
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
A class for sampling random bins from a given probability distribution using the alias table techniqu...
int egsGetEndian()
Get the endianess of the machine.
EGS_Float * xpos
! The alias table for randomly picking voxels
A "voxelized shape": header.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
EGS_Float * zpos
! The y-positions of the grid
int * map
! The z-positions of the grid
EGS_Float * ypos
! The x-positions of the grid
string otype
The object type.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.