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