-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimage.cpp
More file actions
executable file
·177 lines (151 loc) · 4.45 KB
/
Copy pathimage.cpp
File metadata and controls
executable file
·177 lines (151 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <fstream>
#include <math.h>
#include "image.h"
bool Point::inVolume(const std::vector<double>& corner, const std::vector<double>& volume_dim)
{
bool x=((*this)[0]>=corner[0]) && ((*this)[0]<=corner[0]+volume_dim[0]);
bool y=((*this)[1]>=corner[1]) && ((*this)[1]<=corner[1]+volume_dim[1]);
bool z=((*this)[2]>=corner[2]) && ((*this)[2]<=corner[2]+volume_dim[2]);
return (x && y && z);
}
bool Point::inDetector(const std::vector<double>& dim_detector, const std::vector<double>& center_detector)
{
bool x=fabs((*this)[0]-center_detector[0])<=dim_detector[0];
bool y=fabs((*this)[1]-center_detector[1])<=dim_detector[1];
bool z=fabs((*this)[2]-center_detector[2])<=dim_detector[2];
return (x && y && z);
}
Image::Image(const int* image_size, const double* voxel_size, const double* corner)
{
for (int i=0;i<3; i++)
{
DimInVoxels[i]=image_size[i];
VoxelSize[i]=voxel_size[i];
Corner[i]=corner[i];
DimInCm[i]=DimInVoxels[i]*VoxelSize[i];
}
NbVoxels=DimInVoxels[0]*DimInVoxels[1]*DimInVoxels[2];
Value.assign( NbVoxels,0.0);
}
/*
void Image::getVoxel_size( std::vector<double>& voxel_size) const
{
for (int i=0;i<3; i++) voxel_size[i]=m_voxel_size[i];
}
void Image::getCorner( std::vector<double>& corner) const
{
for (int i=0;i<3; i++) corner[i]=m_corner[i];
}
void Image::getImage_size(std::vector<int>& image_size) const
{
for (int i=0;i<3; i++) image_size[i]=m_image_size[i];
}
double Image::getIntensity(int index_voxel) const
{
return (m_volume[index_voxel]);
}
void Image::getVolume(std::vector<double>& volume ) const
{
volume=m_volume;
}
*/
bool Image::index_1Dto3D(int index_voxel, int& i, int& j, int& k) const
{
if ((index_voxel<0) || (index_voxel>=NbVoxels)) return false;
k=index_voxel / (DimInVoxels[0]*DimInVoxels[1]);
j=index_voxel % (DimInVoxels[0]*DimInVoxels[1]);
i= j % DimInVoxels[0];
j = j/DimInVoxels[0];
return true;
}
int Image::index_3Dto1D(int i,int j, int k) const
{
if ((i<0)||(i>=DimInVoxels[0])||(j<0)||(j>=DimInVoxels[1])||(k<0)||(k>=DimInVoxels[2])) return -1;
return (i+j*DimInVoxels[0]+k*DimInVoxels[0]*DimInVoxels[1]);
}
bool Image::coord2index_3D(double x, double y, double z, int& i, int& j, int& k) const
{
bool inside=((x>=Corner[0]) && (x<=Corner[0]+DimInCm[0]) && (y>=Corner[1]) && (y<=Corner[1]+DimInCm[1]) && (z>=Corner[2]) && (z<=Corner[2]+DimInCm[2]));
i=int(std::min(int(floor((x-Corner[0])/VoxelSize[0])), DimInVoxels[0]));
j=int(std::min(int(floor((y-Corner[1])/VoxelSize[1])), DimInVoxels[1]));
k=int(std::min(int(floor((z-Corner[2])/VoxelSize[2])), DimInVoxels[2]));
// std::cout<< i<< ' ' << j<< ' ' << k << ' ' << '\n';
return(inside);
}
int Image::coord2index_1D(double x, double y, double z) const
{
int i,j,k;
bool inside=Image::coord2index_3D(x,y,z,i,j,k);
if (inside)
return Image::index_3Dto1D(i,j,k);
else
return -1;
}
void Image::initialize(double value)
{
Value.assign( NbVoxels, value);
}
void Image::multiply(const std::vector<double> mult, const std::vector<double> divisor)
{
for(int i=0; i<NbVoxels; i++)
Value[i] *= mult[i]/divisor[i];
}
void Image::setIntensity(int index_voxel, double value)
{
Value[index_voxel] = value;
}
double Image::Maximum() const
{
double max = 0.0;
for (int i = 0; i<NbVoxels;i++)
if (Value[i] > max) max = Value[i];
return max;
}
int Image::readFile(char *image_file)
{
std::ifstream fp;
fp.open(image_file, std::ios::in|std::ios::binary);
if (!fp.is_open())
{
std::cout<<"Unknown file "<<image_file<< '\n';
return 1;
}
fp.read((char *)(&(Value[0])), NbVoxels*sizeof(double));
if (!fp)
{
std::cout << "Error in " << image_file << ": only "<< fp.gcount()<< " items could be read.\n" ;
return 1;
}
fp.close();
return 0;
}
const int Image::writeFile(char *image_file)
{
std::ofstream fp;
fp.open(image_file, std::ios::out|std::ios::binary);
if (!fp.is_open())
{
std::cout<<"Unknown file "<<image_file<< '\n';
return 1;
}
fp.write((char*)(&(Value[0])), NbVoxels*sizeof(double));
fp.close();
return 0;
}
bool Image::readFromFile(std::ifstream& fp)
{
fp.read((char *)(&(Value[0])), NbVoxels*sizeof(double));
return fp.eof();
/*
if (!fp)
{
std::cout << "Error in " << image_file << ": only "<< fp.gcount()<< " items could be read.\n" ;
return 1;
}
*/
}
const void Image::writeToFile(std::ofstream& fp)
{
fp.write((char*)(&(Value[0])), NbVoxels*sizeof(double));
}