00001
00002
00003 #include "stdafx.h"
00004 #include "math.h"
00005 #include "data.h"
00006
00008 gradient_t operator * (gradient_t g, float s)
00009 {
00010 gradient_t res;
00011 res.x = g.x * s;
00012 res.y = g.y * s;
00013 res.z = g.z * s;
00014 return res;
00015 }
00016
00018 gradient_t operator + (gradient_t g1, gradient_t g2)
00019 {
00020 gradient_t res;
00021 res.x = g1.x + g2.x;
00022 res.y = g1.y + g2.y;
00023 res.z = g1.z + g2.z;
00024 return res;
00025 }
00026
00029 int
00030 Data::GetDensity(int x, int y, int z)
00031 {
00032 if(x <0) x =0;
00033 if(y<0) y=0;
00034 if(z<0) z=0;
00035 if(x > xDim - 1) x = xDim-1;
00036 if(y > yDim - 1) y = yDim-1;
00037 if(z > zDim - 1) z = zDim-1;
00038 return (int)data[z*xyDim+y*xDim+x];
00039 }
00040
00043 bool
00044 Data::CalcDensityHistogram()
00045 {
00046 for (int i = 0; i < 4096; i++)
00047 {
00048 histogram[i] = 0;
00049 }
00050
00051 for (int j = 0; j < size; j++)
00052 {
00053 histogram[data[j]] += 1;
00054 }
00055
00056 return true;
00057 }
00058
00061 int
00062 Data::GetHistogram(int density)
00063 {
00064 if(density < 0) density = 0;
00065 if(density >= 4096) density = 4095;
00066 return histogram[density];
00067 }
00068
00071
00072 gradient_t
00073 Data::CalcGrad(int x, int y, int z) {
00074 if(precalc && gradients == NULL) CalcGradients();
00075 if(x < 0) x = 0;
00076 if(x > xDim-1) x = xDim-1;
00077 if(z < 0) z = 0;
00078 if(z > zDim-1) z = zDim-1;
00079 if(y < 0) y = 0;
00080 if(y > yDim-1) y = yDim-1;
00081 gradient_t res;
00082 if(!precalc) {
00083 res.x = ((float)(GetDensity(x+1,y,z) - GetDensity(x-1,y,z)))/2.0;
00084 res.y = ((float)(GetDensity(x,y+1,z) - GetDensity(x,y-1,z)))/2.0;
00085 res.z = ((float)(GetDensity(x,y,z+1) - GetDensity(x,y,z-1)))/2.0;
00086 } else {
00087 res = gradients[z*xyDim+y*xDim+x];
00088 }
00089 return res;
00090 }
00091
00093 bool
00094 Data::CalcGradients() {
00095 if (gradients != NULL) return false;
00096 gradients = new gradient_t[xDim*yDim*zDim];
00097
00098 for(int z = 0; z < zDim; z++) {
00099 for(int y = 0; y < yDim; y++) {
00100 for(int x = 0; x < xDim; x++) {
00101 gradients[z*xyDim+y*xDim+x].x = ((float)(GetDensity(x+1,y,z) - GetDensity(x-1,y,z)))/2.0;
00102 gradients[z*xyDim+y*xDim+x].y = ((float)(GetDensity(x,y+1,z) - GetDensity(x,y-1,z)))/2.0;
00103 gradients[z*xyDim+y*xDim+x].z = ((float)(GetDensity(x,y,z+1) - GetDensity(x,y,z-1)))/2.0;
00104 }
00105 }
00106 }
00107
00108 return true;
00109 }
00110
00113 void
00114 Data::LoadData(char* fname)
00115 {
00116
00117 char *filename = (char *)fname;
00118
00119 FILE *filePtr;
00120
00121
00122 filePtr = fopen(filename, "rb");
00123 if(!filePtr) return;
00124
00125
00126 fread(&xDim, sizeof(short),1,filePtr);
00127 fread(&yDim, sizeof(short),1,filePtr);
00128 fread(&zDim, sizeof(short),1,filePtr);
00129
00130
00131 xyDim = (int)xDim*yDim;
00132
00133
00134 size = xyDim * zDim;
00135 data = new short[size];
00136
00137
00138 CWnd* pFrame = AfxGetMainWnd();
00139
00140 int loadSize = (size) / 100;
00141 int counter = loadSize;
00142 int pos = 0;
00143 int fileSize = 0;
00144
00145
00146
00147
00148
00149
00150 if ((m_FileThreshold != 0) &&
00151 (fileSize = DATASET_TO_MB(size)) >= m_FileThreshold) {
00152 int pieces = size / m_DataPackets;
00153
00154
00155
00156
00157 pFrame->SendMessage(MYWM_PROGRESS_MIN_MAX, 0, pieces);
00158
00159 int tempSize = size;
00160
00161 for (int i = 0; i < pieces; i++) {
00162 pFrame->SendMessage(MYWM_PROGRESS, i);
00163 fread(data + i * m_DataPackets, sizeof(short), m_DataPackets, filePtr);
00164 tempSize -= m_DataPackets;
00165 }
00166
00167 pFrame->SendMessage(MYWM_PROGRESS, i);
00168 if (tempSize > 0)
00169 fread(data + i * m_DataPackets, sizeof(short), tempSize, filePtr);
00170 }
00171 else {
00172 fread(data,sizeof(short),size,filePtr);
00173 }
00174
00175 fclose(filePtr);
00176
00177 pFrame->SendMessage(MYWM_PROGRESS_MIN_MAX, 0, 100);
00178
00179
00180 for(int z=0; z < zDim; z++) {
00181 for(int y=0; y < yDim; y++) {
00182 for(int x=0; x < xDim; x++) {
00183 data[z*xyDim+y*xDim+x] &= 0x0fff;
00184
00185
00186 if (counter-- <= 0) {
00187 counter = loadSize;
00188 pFrame->SendMessage(MYWM_PROGRESS, pos++);
00189 }
00190 }
00191 }
00192 }
00193
00194 pFrame->SendMessage(MYWM_PROGRESS, 0);
00195
00196
00197 this->filename = filename;
00198
00199 if(!CalcDensityHistogram()) return;
00200
00201
00202 }
00203
00205 short
00206 Data::GetXDim()
00207 {
00208 return xDim;
00209 }
00210
00212 short
00213 Data::GetYDim()
00214 {
00215 return yDim;
00216 }
00217
00219 short
00220 Data::GetZDim()
00221 {
00222 return zDim;
00223 }