18 using namespace MoFEM;
24 #include <metaImage.h>
32 const vector<float> &vec_density,
33 const vector<float> &vec_dist
39 const float x,
const float y,
const float z,
45 vector<float> &vec_dist,
46 vector<float> &vec_density,
51 int main(
int argc,
char *argv[]) {
52 PetscInitialize(&argc,&argv,(
char *)0,
help);
54 PetscBool flg = PETSC_TRUE;
55 char meta_file_name_1[255];
56 PetscBool meta_flg_file_1 = PETSC_FALSE;
57 char meta_file_name_2[255];
58 PetscBool meta_flg_file_2 = PETSC_FALSE;
60 char output_name[255] =
"newImage.mhd";
61 PetscBool gauss_flag = PETSC_FALSE;
65 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,
"",
"Meta merge",
"none"); CHKERRQ(
ierr);
68 ierr = PetscOptionsString(
71 "file.mhd",meta_file_name_1,255,&meta_flg_file_1
74 ierr = PetscOptionsString(
77 "newImage.mhd",output_name,255,PETSC_NULL
82 ierr = PetscOptionsString(
85 "file.mhd",meta_file_name_2,255,&meta_flg_file_2
88 ierr = PetscOptionsEnd(); CHKERRQ(
ierr);
89 int my_vector[3] = {0, 0, 0};
90 int my_vector_gauss[2] = {3, 2};
97 MetaImage meta_file1(meta_file_name_1);
98 cout <<
"Quantity of file 1: " << meta_file1.Quantity() << endl;
99 const int *dim_size1 = meta_file1.DimSize();
100 cout <<
"Image dimension: " << dim_size1[0] <<
" " << dim_size1[1] <<
" " << dim_size1[2] << endl;
101 const double *element_size1 = meta_file1.ElementSize();
102 cout <<
"Image element size: " << element_size1[0] <<
" " << element_size1[1] <<
" " << element_size1[2] << endl;
104 MetaImage meta_file2(meta_file_name_2);
105 cout <<
"\nQuantity of file 2: " << meta_file2.Quantity() << endl;
106 const int *dim_size2 = meta_file2.DimSize();
107 cout <<
"Image dimension: " << dim_size2[0] <<
" " << dim_size2[1] <<
" " << dim_size2[2] << endl;
108 const double *element_size2 = meta_file2.ElementSize();
109 cout <<
"Image element size: " << element_size2[0] <<
" " << element_size2[1] <<
" " << element_size2[2] << endl;
111 cout<<
"\nTranslation vector: " << my_vector[0] <<
" " << my_vector[1] <<
" " << my_vector[2] << endl;
122 int quantity = meta_file1.Quantity();
123 if(meta_flg_file_2) quantity += meta_file2.Quantity();
124 std::vector<float> data_vec(quantity);
125 float *data = &data_vec[0];
136 for(
int i0 = 0; i0 != dim_size1[2]; i0++) {
137 for(
int i1 = 0; i1 != dim_size1[1]; i1++) {
138 for(
int i2 = 0; i2 != dim_size1[0]; i2++) {
140 vector<float> vec_density, vec_dist;
141 vector<int> vec_ix,vec_iy,vec_iz, vec_idx;
143 data[
i] = meta_file1.ElementData(
i);
171 if(meta_flg_file_2) {
173 for(
int i0 = 0; i0 != dim_size2[2]; i0++) {
174 for(
int i1 = 0; i1 != dim_size2[1]; i1++) {
175 for(
int i2 = 0; i2 != dim_size2[0]; i2++) {
177 int z = dim_size2[0]*dim_size2[1] * ((i0 - my_vector[2])%dim_size2[2]);
178 int y = ((i1 - my_vector[1])%dim_size2[1])*dim_size2[1];
179 int x = (i2 - my_vector[0])%dim_size2[0];
180 if (i0 - my_vector[2] >= dim_size2[2] || i1 - my_vector[1] >= dim_size2[1] || i2 - my_vector[0] >= dim_size2[0] ||
181 i0 - my_vector[2] < 0 || i1 - my_vector[1] < 0 || i2 - my_vector[0] < 0){
182 data[
i++] = -1024;
continue;
184 data[
i++] = meta_file2.ElementData(x + y + z);
189 int Z_dimensions = dim_size1[2];
191 if(meta_flg_file_2) Z_dimensions += dim_size2[2];
192 int global_dimsize[3] = {dim_size1[0], dim_size1[1], Z_dimensions};
193 cout <<
"MHD converter. Writing file." << endl;
195 MetaImage newImage(3, global_dimsize, element_size1, MET_CHAR, 1, data);
196 newImage.FileName(output_name);
197 newImage.ObjectTypeName(
"Image");
200 newImage.BinaryData(
true);
201 newImage.ElementType(MET_FLOAT);
202 newImage.Write(output_name);
203 if (gauss_flag) printf(
"Sum without smoothing: %g Sum with smoothing: %g \n",sum1,sum2 );
204 cout <<
"File saved." << endl;
210 PetscFunctionReturn(0);
215 const vector<float> &vec_density,
216 const vector<float> &vec_dist
220 vector<float> kErnel;
221 kErnel.resize(vec_density.size());
225 const float k = 2 * sigma*sigma;
226 const float m = (1 / sqrt( M_PI *
k));
227 for(
int ii = 0; ii < vec_dist.size(); ii++) {
228 kErnel[
i] =
m * exp(- (vec_dist[ii]) /
k);
235 float smooth_data = 0;
236 for(vector<float>::const_iterator vec_itr = vec_density.begin();vec_itr!=vec_density.end();vec_itr++) {
238 smooth_data += (*vec_itr) * kErnel[ii++];
240 if (sum == 0)
return 0;
246 const float x,
const float y,
const float z,
251 vector<int> &vec_idx,
252 vector<float> &vec_dist,
253 vector<float> &vec_density,
259 const float elemet_size[3] = {1,1,1};
260 const int *dim_size = meta_file.DimSize();
262 int nx = ceil(size/ (elemet_size[0]));
263 int ny = ceil(size/ (elemet_size[1]));
264 int nz = ceil(size/ (elemet_size[2]));
267 float x_befor_offset = x;
268 float y_befor_offset = y;
269 float z_befor_offset = z;
274 int ix = ceil(x_befor_offset/ (elemet_size[0]));
275 int iy = ceil(y_befor_offset/ (elemet_size[1]));
276 int iz = ceil(z_befor_offset/ (elemet_size[2]));
283 for(
int i = 0;
i<2*nx;
i++) {
285 if(idx >= dim_size[0] || idx < 0)
continue;
290 for(
int i = 0;
i<2*ny;
i++) {
292 if(idx >= dim_size[1] || idx < 0)
continue;
297 for(
int i = 0;
i<2*nz;
i++) {
299 if(idx >= dim_size[2] || idx < 0)
continue;
307 vec_idx.resize(vec_iz.size() * vec_ix.size() * vec_iy.size());
308 vec_dist.resize(vec_iz.size() * vec_ix.size() * vec_iy.size());
309 vec_density.resize(vec_iz.size() * vec_ix.size() * vec_iy.size());
310 int dm_size0_dim_size1 = dim_size[0] * dim_size[1];
333 for(vector<int>::iterator it_iz = vec_iz.begin(); it_iz != vec_iz.end();it_iz++) {
334 float a = (*it_iz) * dm_size0_dim_size1 ;
335 float dz2 = (*it_iz*(elemet_size[2]) - z_befor_offset) * (*it_iz*(elemet_size[2]) - z_befor_offset);
336 for(vector<int>::iterator it_iy = vec_iy.begin(); it_iy != vec_iy.end();it_iy++) {
337 float b = (*it_iy) * dim_size[0];
338 float dy2 = (*it_iy*(elemet_size[1]) - y_befor_offset) * (*it_iy*(elemet_size[1]) - y_befor_offset);
339 for(vector<int>::iterator it_ix = vec_ix.begin(); it_ix != vec_ix.end();it_ix++) {
340 vec_idx[ii] =
a + b + (*it_ix);
341 vec_density[ii] = meta_file.ElementData(vec_idx[ii]);
342 vec_dist[ii] = ( dz2 + dy2 + (*it_ix*(elemet_size[0]) - x_befor_offset)
343 * (*it_ix*(elemet_size[0]) - x_befor_offset) );
367 PetscFunctionReturn(0);
371 #endif // WITH_METAIO