46 static bool EGS_VOXELIZED_SHAPE_LOCAL inputSet =
false;
47 static shared_ptr<EGS_BlockInput> EGS_VOXELIZED_SHAPE_LOCAL shapeBlockInput = make_shared<EGS_BlockInput>(
"shape");
49 void EGS_VoxelizedShape::EGS_VoxelizedShapeFormat0(
const char *fname,
62 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
63 otype =
"voxelized_shape";
67 ifstream data(fname,ios::binary);
69 egsWarning(
"%s: failed to open file %s\n",func,fname);
76 egsWarning(
"%s: failed to read endianess and format from %s\n",func,fname);
79 if (form < 0 || form > 1) {
80 egsWarning(
"%s: unknwon format %d found in file %s\n",func,(
int)form,fname);
84 egsWarning(
"%s: data in %s from a machine with different endianess.\n"
85 " Byte swaping not implemented yet.\n",func,fname);
89 data.read((
char *)&snx,
sizeof(
short));
90 data.read((
char *)&sny,
sizeof(
short));
91 data.read((
char *)&snz,
sizeof(
short));
93 egsWarning(
"%s: failed to read number of voxels from %s\n",func,fname);
96 if (snx < 1 || snx > 10000 || sny < 1 || sny > 10000 || snz < 1 || snz > 10000) {
97 egsWarning(
"%s: number of voxels seems strange: nx=%d, ny=%d, nz=%d\n",
106 egsInformation(
"Distribution has %d x %d x %d voxels\n",nx,ny,nz);
107 float *x =
new float [nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
108 data.read((
char *)x, (nx+1)*
sizeof(
float));
109 data.read((
char *)y, (ny+1)*
sizeof(
float));
110 data.read((
char *)z, (nz+1)*
sizeof(
float));
112 egsWarning(
"%s: failed to read voxel boundaries\n",func);
121 p =
new float [nreg];
123 data.read((
char *)p, nreg*
sizeof(
float));
125 egsWarning(
"%s: failed to read probabilities\n",func);
136 data.read((
char *)&nmap,
sizeof(
int));
137 if (data.fail() || nmap < 1) {
144 map =
new int [nmap];
145 p =
new float [nmap];
146 data.read((
char *)map, nmap*
sizeof(
int));
147 data.read((
char *)p, nmap*
sizeof(
float));
149 egsWarning(
"%s: failed to read probabilities and bin numbers\n",func);
158 EGS_Float *p1 =
new EGS_Float [nmap];
160 for (j=0; j<nmap; ++j) {
166 xpos =
new EGS_Float [nx+1];
167 ypos =
new EGS_Float [ny+1];
168 zpos =
new EGS_Float [nz+1];
169 for (j=0; j<=nx; ++j) {
172 for (j=0; j<=ny; ++j) {
175 for (j=0; j<=nz; ++j) {
187 prob(0), xpos(0), ypos(0), zpos(0), map(0), nx(0), ny(0), nz(0), nxy(0),
189 const static char *func =
"EGS_VoxelizedShape::EGS_VoxelizedShape";
190 if (file_format == 0) {
192 EGS_VoxelizedShapeFormat0(s.c_str());
195 if (file_format != 1) {
196 egsWarning(
"%s: unknown file format = %d\n",func,file_format);
200 otype =
"voxelized_shape";
204 ifstream h_file(fname);
206 egsWarning(
"%s: failed to open file %s\n",func,fname);
209 string data_file(
"");
212 float scale_x=0.0, scale_y=0.0, scale_z=0.0;
213 for (EGS_I64 loopCount=0; loopCount<=
loopMax; ++loopCount) {
215 egsFatal(
"EGS_VoxelizedShape::EGS_VoxelizedShape: Too many iterations were required! Input may be invalid, or consider increasing loopMax.");
218 string line, key, value;
220 getline(h_file, line);
221 if (h_file.eof() || h_file.fail() || !h_file.good()) {
224 pos = line.find(
":=");
225 if (pos == string::npos) {
228 key = line.substr(0,
int(pos));
229 value = line.substr(
int(pos)+2);
230 while ((key[0] ==
'!') || (key[0] ==
' ')) {
233 while (key[key.length()-1] ==
' ') {
234 key.erase(key.length()-1, 1);
236 while (value[0] ==
' ') {
239 while (value[value.length()-1] ==
' ') {
240 value.erase(value.length()-1, 1);
242 if (key ==
"matrix size [1]") {
243 sscanf(value.c_str(),
"%u", &Nx);
245 else if (key ==
"matrix size [2]") {
246 sscanf(value.c_str(),
"%u", &Ny);
248 else if ((key ==
"number of slices") || (key ==
"number of images")) {
249 sscanf(value.c_str(),
"%u", &Nz);
251 else if (key ==
"scaling factor (mm/pixel) [1]") {
252 sscanf(value.c_str(),
"%f", &scale_x);
254 else if (key ==
"scaling factor (mm/pixel) [2]") {
255 sscanf(value.c_str(),
"%f", &scale_y);
257 else if (key ==
"slice thickness (pixels)") {
258 sscanf(value.c_str(),
"%f", &scale_z);
260 else if (key ==
"name of data file") {
263 else if (key ==
"number format") {
264 if ((value ==
"float") || (value ==
"FLOAT")) {
267 else if ((value ==
"unsigned integer") || (value ==
"UNSIGNED INTEGER")) {
270 else if ((value ==
"signed integer") || (value ==
"SIGNED INTEGER")) {
274 egsWarning(
"%s: unrecognised 'number format' type: %s \n",func,value.c_str());
278 if (Nx < 1 || Ny < 1 || Nz < 1 || scale_x <= 0.0 || scale_y <= 0.0 || scale_z <= 0.0 || data_file ==
"" || data_type == -1) {
279 egsWarning(
"%s: invalid interfile header information: "
280 "Nx=%d Ny=%d Nz=%d scale_x=%f scale_y=%f scale_z=%f "
281 "data_file='%s' number_format=%d\n",func,Nx,Ny,Nz,scale_x,scale_y,scale_z,data_file.c_str(),data_type);
290 float *x =
new float [
nx+1], *y =
new float [ny+1], *z =
new float [nz+1];
296 for (j=0; j<=
nx; j++) {
297 x[j] = -(float)
nx*scale_x/2.0 + j*scale_x;
299 for (j=0; j<=ny; j++) {
300 y[j] = -(float)ny*scale_y/2.0 + j*scale_y;
302 for (j=0; j<=nz; j++) {
303 z[j] = -(float)nz*scale_z/2.0 + j*scale_z;
306 ifstream i_file(data_file.c_str(),ios::binary);
308 egsWarning(
"%s: failed to open interfile data "
309 "%s\n",func,data_file.c_str());
313 float *p =
new float [nreg];
314 if (data_type == 0) {
315 i_file.read((
char *)p, nreg*
sizeof(
float));
317 else if (data_type == 1) {
318 unsigned short int *p_tmp =
new unsigned short int [nreg];
319 i_file.read((
char *)p_tmp, nreg*
sizeof(
unsigned short int));
320 for (
int cc = 0; cc<nreg; cc++) {
321 p[cc] = (float)(p_tmp[cc]);
326 short int *p_tmp =
new short int [nreg];
327 i_file.read((
char *)p_tmp, nreg*
sizeof(
short int));
328 for (
int cc = 0; cc<nreg; cc++) {
329 p[cc] = (float)(p_tmp[cc]);
333 EGS_Float *p1 =
new EGS_Float [nmap];
335 for (j=0; j<nmap; ++j) {
341 xpos =
new EGS_Float [
nx+1];
342 ypos =
new EGS_Float [ny+1];
343 zpos =
new EGS_Float [nz+1];
344 for (j=0; j<=
nx; ++j) {
347 for (j=0; j<=ny; ++j) {
350 for (j=0; j<=nz; ++j) {
361 EGS_VoxelizedShape::~EGS_VoxelizedShape() {
376 static void setInputs() {
379 setShapeInputs(shapeBlockInput);
380 shapeBlockInput->getSingleInput(
"library")->setValues({
"egs_voxelized_shape"});
382 shapeBlockInput->addSingleInput(
"file name",
true,
"The filename for a binary file that contains sampling probabilities for an XYZ voxel grid. See the documentation for file format details.");
385 EGS_VOXELIZED_SHAPE_EXPORT
string getExample() {
389 # Example of egs_voxelized_shape
391 library = egs_voxelized_shape
392 file name = some_file
398 EGS_VOXELIZED_SHAPE_EXPORT shared_ptr<EGS_BlockInput> getInputs() {
402 return shapeBlockInput;
408 static const char *func =
"createShape(voxelized shape)";
414 int err = input->
getInput(
"file name",fname);
416 int err2 = input->
getInput(
"file format",file_format);
418 egsWarning(
"%s: missing 'file name' input\n",func);
422 egsInformation(
"%s: 'file format' input missing. Using default 'binary'"
423 "file format \n",func);
427 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.