45 void EGS_VoxelizedShape::EGS_VoxelizedShapeFormat0(
const char *fname,
58 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
59 otype =
"voxelized_shape";
63 ifstream data(fname,ios::binary);
65 egsWarning(
"%s: failed to open file %s\n",func,fname);
72 egsWarning(
"%s: failed to read endianess and format from %s\n",func,fname);
75 if (form < 0 || form > 1) {
76 egsWarning(
"%s: unknwon format %d found in file %s\n",func,(
int)form,fname);
80 egsWarning(
"%s: data in %s from a machine with different endianess.\n"
81 " Byte swaping not implemented yet.\n",func,fname);
85 data.read((
char *)&snx,
sizeof(
short));
86 data.read((
char *)&sny,
sizeof(
short));
87 data.read((
char *)&snz,
sizeof(
short));
89 egsWarning(
"%s: failed to read number of voxels from %s\n",func,fname);
92 if (snx < 1 || snx > 10000 || sny < 1 || sny > 10000 || snz < 1 || snz > 10000) {
93 egsWarning(
"%s: number of voxels seems strange: nx=%d, ny=%d, nz=%d\n",
102 egsInformation(
"Distribution has %d x %d x %d voxels\n",nx,ny,nz);
103 float *x =
new float [nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
104 data.read((
char *)x, (nx+1)*
sizeof(
float));
105 data.read((
char *)y, (ny+1)*
sizeof(
float));
106 data.read((
char *)z, (nz+1)*
sizeof(
float));
108 egsWarning(
"%s: failed to read voxel boundaries\n",func);
117 p =
new float [nreg];
119 data.read((
char *)p, nreg*
sizeof(
float));
121 egsWarning(
"%s: failed to read probabilities\n",func);
132 data.read((
char *)&nmap,
sizeof(
int));
133 if (data.fail() || nmap < 1) {
140 map =
new int [nmap];
141 p =
new float [nmap];
142 data.read((
char *)map, nmap*
sizeof(
int));
143 data.read((
char *)p, nmap*
sizeof(
float));
145 egsWarning(
"%s: failed to read probabilities and bin numbers\n",func);
154 EGS_Float *p1 =
new EGS_Float [nmap];
156 for (j=0; j<nmap; ++j) {
162 xpos =
new EGS_Float [nx+1];
163 ypos =
new EGS_Float [ny+1];
164 zpos =
new EGS_Float [nz+1];
165 for (j=0; j<=nx; ++j) {
168 for (j=0; j<=ny; ++j) {
171 for (j=0; j<=nz; ++j) {
183 prob(0), xpos(0), ypos(0), zpos(0), map(0), nx(0), ny(0), nz(0), nxy(0),
185 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
186 if (file_format == 0) {
188 EGS_VoxelizedShapeFormat0(s.c_str());
191 if (file_format != 1) {
192 egsWarning(
"%s: unknown file format = %d\n",func,file_format);
196 otype =
"voxelized_shape";
200 ifstream h_file(fname);
202 egsWarning(
"%s: failed to open file %s\n",func,fname);
205 string data_file(
"");
208 float scale_x=0.0, scale_y=0.0, scale_z=0.0;
209 for (EGS_I64 loopCount=0; loopCount<=
loopMax; ++loopCount) {
211 egsFatal(
"EGS_VoxelizedShape::EGS_VoxelizedShape: Too many iterations were required! Input may be invalid, or consider increasing loopMax.");
214 string line, key, value;
216 getline(h_file, line);
217 if (h_file.eof() || h_file.fail() || !h_file.good()) {
220 pos = line.find(
":=");
221 if (pos == string::npos) {
224 key = line.substr(0,
int(pos));
225 value = line.substr(
int(pos)+2);
226 while ((key[0] ==
'!') || (key[0] ==
' ')) {
229 while (key[key.length()-1] ==
' ') {
230 key.erase(key.length()-1, 1);
232 while (value[0] ==
' ') {
235 while (value[value.length()-1] ==
' ') {
236 value.erase(value.length()-1, 1);
238 if (key ==
"matrix size [1]") {
239 sscanf(value.c_str(),
"%u", &Nx);
241 else if (key ==
"matrix size [2]") {
242 sscanf(value.c_str(),
"%u", &Ny);
244 else if ((key ==
"number of slices") || (key ==
"number of images")) {
245 sscanf(value.c_str(),
"%u", &Nz);
247 else if (key ==
"scaling factor (mm/pixel) [1]") {
248 sscanf(value.c_str(),
"%f", &scale_x);
250 else if (key ==
"scaling factor (mm/pixel) [2]") {
251 sscanf(value.c_str(),
"%f", &scale_y);
253 else if (key ==
"slice thickness (pixels)") {
254 sscanf(value.c_str(),
"%f", &scale_z);
256 else if (key ==
"name of data file") {
259 else if (key ==
"number format") {
260 if ((value ==
"float") || (value ==
"FLOAT")) {
263 else if ((value ==
"unsigned integer") || (value ==
"UNSIGNED INTEGER")) {
266 else if ((value ==
"signed integer") || (value ==
"SIGNED INTEGER")) {
270 egsWarning(
"%s: unrecognised 'number format' type: %s \n",func,value.c_str());
274 if (Nx < 1 || Ny < 1 || Nz < 1 || scale_x <= 0.0 || scale_y <= 0.0 || scale_z <= 0.0 || data_file ==
"" || data_type == -1) {
275 egsWarning(
"%s: invalid interfile header information: "
276 "Nx=%d Ny=%d Nz=%d scale_x=%f scale_y=%f scale_z=%f "
277 "data_file='%s' number_format=%d\n",func,Nx,Ny,Nz,scale_x,scale_y,scale_z,data_file.c_str(),data_type);
286 float *x =
new float [
nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
292 for (j=0; j<=
nx; j++) {
293 x[j] = -(float)
nx*scale_x/2.0 + j*scale_x;
295 for (j=0; j<=ny; j++) {
296 y[j] = -(float)ny*scale_y/2.0 + j*scale_y;
298 for (j=0; j<=nz; j++) {
299 z[j] = -(float)nz*scale_z/2.0 + j*scale_z;
302 ifstream i_file(data_file.c_str(),ios::binary);
304 egsWarning(
"%s: failed to open interfile data "
305 "%s\n",func,data_file.c_str());
309 float *p =
new float [nreg];
310 if (data_type == 0) {
311 i_file.read((
char *)p, nreg*
sizeof(
float));
313 else if (data_type == 1) {
314 unsigned short int *p_tmp =
new unsigned short int [nreg];
315 i_file.read((
char *)p_tmp, nreg*
sizeof(
unsigned short int));
316 for (
int cc = 0; cc<nreg; cc++) {
317 p[cc] = (float)(p_tmp[cc]);
322 short int *p_tmp =
new short int [nreg];
323 i_file.read((
char *)p_tmp, nreg*
sizeof(
short int));
324 for (
int cc = 0; cc<nreg; cc++) {
325 p[cc] = (float)(p_tmp[cc]);
329 EGS_Float *p1 =
new EGS_Float [nmap];
331 for (j=0; j<nmap; ++j) {
337 xpos =
new EGS_Float [
nx+1];
338 ypos =
new EGS_Float [ny+1];
339 zpos =
new EGS_Float [nz+1];
340 for (j=0; j<=
nx; ++j) {
343 for (j=0; j<=ny; ++j) {
346 for (j=0; j<=nz; ++j) {
357 EGS_VoxelizedShape::~EGS_VoxelizedShape() {
374 static const char *func =
"createShape(voxelized shape)";
380 int err = input->
getInput(
"file name",fname);
382 int err2 = input->
getInput(
"file format",file_format);
384 egsWarning(
"%s: missing 'file name' input\n",func);
388 egsInformation(
"%s: 'file format' input missing. Using default 'binary'"
389 "file format \n",func);
393 if (!shape->isValid()) {
Base shape class. All shapes in the EGSnrc C++ class library are derived from EGS_BaseShape.
void setTransformation(EGS_Input *inp)
Set the transformation attached to this shape.
void setName(EGS_Input *inp)
Set the name of the object from the information provided by inp.
string otype
The object type.
A class for sampling random bins from a given probability distribution using the alias table techniqu...
EGS_Float * zpos
! The y-positions of the grid
EGS_Float * xpos
! The alias table for randomly picking voxels
int * map
! The z-positions of the grid
EGS_Float * ypos
! The x-positions of the grid
int nx
! Voxel map (for type=1)
EGS_VoxelizedShape(int file_format, const char *fname, const string &Name="", EGS_ObjectFactory *f=0)
Conctructor.
Global egspp functions header file.
A "voxelized shape": header.
int egsGetEndian()
Get the endianess of the machine.
EGS_InfoFunction EGS_EXPORT egsInformation
Always use this function for reporting the progress of a simulation and any other type of information...
EGS_InfoFunction EGS_EXPORT egsFatal
Always use this function for reporting fatal errors.
const EGS_I64 loopMax
The maximum number of iterations for near-infinite loops.
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.