Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Fourier/Unimodal/3d/3d.cpp

Go to the documentation of this file.
00001 #include "3d.h"
00002 #include <fstream.h>
00003 #include <stdio.h>
00004 
00005 //----------------------------------------------------------------------------
00006 //------------------------- The default constructor --------------------------
00007 //----------------------------------------------------------------------------
00008 
00009 template <int S>
00010 vu1712<S>::vu1712()
00011 {
00012     m_Dim1Size = 0;
00013     m_Dim2Size = 0;
00014     m_Dim3Size = 0;
00015 
00016     m_Spacing = 0;
00017 
00018     m_Dim1Origin = 0;
00019     m_Dim2Origin = 0;
00020     m_Dim3Origin = 0;
00021 }
00022 
00023 //----------------------------------------------------------------------------
00024 //------------------------- The copy constructor -----------------------------
00025 //----------------------------------------------------------------------------
00026 
00027 template <int S>
00028 vu1712<S>::vu1712(const vu1712& inst) : vu171(inst)
00029 {
00030     m_Dim1Size = inst.m_Dim1Size;
00031     m_Dim2Size = inst.m_Dim2Size;
00032     m_Dim3Size = inst.m_Dim3Size;
00033 
00034     m_Spacing = inst.m_Spacing;
00035 
00036     m_Dim1Origin = inst.m_Dim1Origin;
00037     m_Dim2Origin = inst.m_Dim2Origin;
00038     m_Dim3Origin = inst.m_Dim3Origin;
00039 }
00040 
00041 //----------------------------------------------------------------------------
00042 //------------------------- The assignment operator --------------------------
00043 //----------------------------------------------------------------------------
00044 
00045 template <int S>
00046 vu1712<S>& vu1712<S>::operator=(const vu1712<S>& rhs)
00047 {
00048     if (this != &rhs)
00049     {
00050         m_Dim1Size = rhs.m_Dim1Size;
00051         m_Dim2Size = rhs.m_Dim2Size;
00052         m_Dim3Size = rhs.m_Dim3Size;
00053 
00054         m_Spacing = rhs.m_Spacing;
00055 
00056         m_Dim1Origin = rhs.m_Dim1Origin;
00057         m_Dim2Origin = rhs.m_Dim2Origin;
00058         m_Dim3Origin = rhs.m_Dim3Origin;
00059         vu171::operator=(rhs);
00060     }
00061     return *this;
00062 }
00063 
00064 //----------------------------------------------------------------------------
00065 //------------------------- The set/get methods ------------------------------
00066 //----------------------------------------------------------------------------
00067 
00068 template <int S>
00069 dword vu1712<S>::getDim1Size(void) const
00070 {
00071     return m_Dim1Size;
00072 }
00073 
00074 template <int S>
00075 dword vu1712<S>::getDim2Size(void) const
00076 {
00077     return m_Dim2Size;
00078 }
00079 
00080 template <int S>
00081 dword vu1712<S>::getDim3Size(void) const
00082 {
00083     return m_Dim3Size;
00084 }
00085 
00086 template <int S>
00087 dword vu1712<S>::getSpacing(void) const
00088 {
00089     return m_Spacing;
00090 }
00091 
00092 template <int S>
00093 int vu1712<S>::getDim1Origin(void) const
00094 {
00095     return m_Dim1Origin;
00096 }
00097 
00098 template <int S>
00099 int vu1712<S>::getDim2Origin(void) const
00100 {
00101     return m_Dim2Origin;
00102 }
00103 
00104 template <int S>
00105 int vu1712<S>::getDim3Origin(void) const
00106 {
00107     return m_Dim3Origin;
00108 }
00109 
00110 
00111 //----------------------------------------------------------------------------
00112 //------------------------- protected readHeader() ---------------------------
00113 //----------------------------------------------------------------------------
00114 
00115 template <int S>
00116 bool vu1712<S>::readHeader(FILE *file)
00117 {
00118     int ret = 0;
00119 
00120     bool success = vu171::read(file);
00121     if (!success) return false;
00122 
00123     //Read in the dimensions of the data
00124     dword d4=0;
00125     ret = fscanf(file,"DIMENSIONS %lu %lu %lu %lu ",&m_Dim1Size,&m_Dim2Size,
00126           &m_Dim3Size,&d4);
00127     if (ret != 4) return setInvalidFormatError();
00128 
00129     //Read in the origin of the data
00130     int do4=0;
00131     ret = fscanf(file,"ORIGIN %i %i %i %i ",&m_Dim1Origin,&m_Dim2Origin,
00132                  &m_Dim3Origin,&do4);
00133     if (ret != 4) return setInvalidFormatError();
00134     //Read in the spacing of the data
00135     dword ds1,ds2,ds3,ds4;
00136     ret = fscanf(file,"SPACING %lu %lu %lu %lu ",&ds1,&ds2,&ds3,&ds4);
00137     if (ret != 4) return setInvalidFormatError();
00138     m_Spacing = ds1;
00139     //Read in the number of data points stored in the file.
00140     ret = fscanf(file,"DATA_SIZE %lu ",&m_DataSize);
00141     if (ret != 1) return setInvalidFormatError();
00142     //Make sure that this is 3d data and that it's valid.
00143     if ( (m_Dim1Size<=1) || (m_Dim2Size<=1) || (m_Dim3Size<=1) || (d4!=1) )
00144         return setInvalidFormatError();
00145     else if ( (ds1 != ds2) || (ds2 != ds3)) // has to be equally spaced
00146         return setInvalidFormatError();
00147 
00148     // Read name, number of channels and base type
00149     char dataName[64];
00150     char typeName[16];
00151     int  channels;
00152     ret = fscanf(file,"FIELDS %s %d %s ", dataName, &channels, typeName);
00153     if (ret != 3)
00154       return setError("Could not read dataName or number of channels");
00155     if (channels != S)
00156       return setError("wrong number of channels");
00157     if (strcmp(typeName, "float") != 0)
00158       return setError("typeName is not valid");
00159     //Store the name of the data
00160     m_DataName = dataName;
00161 
00162     // --- read volume ----------------------------------------------------
00163 
00164     return true;
00165 }
00166 
00167 template <int S>
00168 bool vu1712<S>::readData(FILE *file, float* vol,
00169                          dword XX, dword YY, dword ZZ,
00170                          dword XXsmall, dword YYsmall, dword ZZsmall)
00171 {
00172   if (file == NULL) return false;
00173 
00174   float* v_ptr = vol;
00175 
00176   dword lowX = (XX - XXsmall) / 2;
00177   dword lowY = (YY - YYsmall) / 2;
00178   dword lowZ = (ZZ - ZZsmall) / 2;
00179 
00180   dword hiX = XX - lowX - XXsmall;
00181   dword hiY = YY - lowY - YYsmall;
00182   dword hiZ = ZZ - lowZ - ZZsmall;
00183 
00184   dword bufSize = XXsmall * 2 * S;
00185   dword size    = 0;
00186 
00187   v_ptr = pad(v_ptr, lowZ * XX * YY * 2 * S);
00188   for (dword k = 0; k < ZZsmall; ++k) {
00189 
00190     v_ptr = pad(v_ptr, lowY * XX * 2 * S);
00191     for (dword j = 0; j < YYsmall; ++j) {
00192 
00193       v_ptr = pad(v_ptr, lowX * 2 * S);
00194 
00195       size = fread(v_ptr, sizeof(float), bufSize, file);
00196       if (size != bufSize) return false;
00197 
00198       v_ptr += bufSize;
00199       v_ptr = pad(v_ptr, hiX * 2 * S);
00200 
00201     }
00202     v_ptr = pad(v_ptr, hiY * XX * 2 * S);
00203 
00204   }
00205   v_ptr = pad(v_ptr, hiZ * XX * YY * 2 * S);
00206 
00207   return true;
00208 }
00209 
00210 //----------------------------------------------------------------------------
00211 //------------------------- protected write() --------------------------------
00212 //----------------------------------------------------------------------------
00213 
00214 template <int S>
00215 bool vu1712<S>::writeHeader(FILE *file)
00216 {
00217     int ret = 0;
00218 
00219     bool success = vu171::write(file);
00220     if (!success) return false;
00221 
00222     //Write the dimensions of the data
00223     ret = fprintf(file,"DIMENSIONS %lu %lu %lu %lu\n",m_Dim1Size,m_Dim2Size,
00224           m_Dim3Size,(dword)1);
00225 
00226     //Write the origin of the data
00227     ret = fprintf(file,"ORIGIN %i %i %i %i\n",m_Dim1Origin,m_Dim2Origin,
00228           m_Dim3Origin,0);
00229     
00230     //Write the spacing of the data
00231     ret = fprintf(file,"SPACING %lu %lu %lu %lu\n",m_Spacing,m_Spacing,
00232           m_Spacing,(dword)0);
00233 
00234     //Write the total number of data points.
00235     ret = fprintf(file,"DATA_SIZE %lu\n",m_DataSize);
00236 
00237     if (m_DataName.isEmpty()) m_DataName = "data";
00238     //Write dataName, number of channels and base type
00239     ret = fprintf(file,"FIELDS %s %d float\n", m_DataName.c_str(), S);
00240     
00241     if (ret > 0)
00242         return true;
00243     else
00244         return setWriteError();
00245 }
00246 
00247 template <int S>
00248 bool vu1712<S>::writeData(FILE *file, float* vol,
00249                           dword XX, dword YY, dword ZZ,
00250                           dword XXsmall, dword YYsmall, dword ZZsmall)
00251 {
00252   if (file == NULL) return false;
00253 
00254   float* v_ptr = vol;
00255 
00256   dword lowX = (XX - XXsmall) / 2;
00257   dword lowY = (YY - YYsmall) / 2;
00258   dword lowZ = (ZZ - ZZsmall) / 2;
00259 
00260   dword hiX = XX - lowX - XXsmall;
00261   dword hiY = YY - lowY - YYsmall;
00262 
00263   dword bufSize = XXsmall * 2 * S;
00264   dword size    = 0;
00265 
00266   v_ptr += lowZ * XX * YY * 2 * S;
00267   for (dword k = 0; k < ZZsmall; ++k) {
00268 
00269     v_ptr += lowY * XX * 2 * S;
00270     for (dword j = 0; j < YYsmall; ++j) {
00271 
00272       v_ptr += lowX * 2 * S;
00273 
00274       size = fwrite(v_ptr, sizeof(float), bufSize, file);
00275       if (size != bufSize) return false;
00276 
00277       v_ptr += bufSize;
00278       v_ptr += hiX * 2 * S;
00279     }
00280     v_ptr += hiY * XX * 2 * S;
00281   }
00282 
00283   return true;
00284 }
00285 
00286 template <int S>
00287 bool vu1712<S>::writeHeader(FILE *file, dword XSize, dword YSize, dword ZSize)
00288 {
00289   if (file == NULL) return false;
00290 
00291   fprintf(file, "# vu DataFile Version 1.0\n"
00292                 "Volume Data\n"
00293                 "BINARY\n"
00294                 "DATASET FOURIER\n"
00295                 "UNIMODAL\n"
00296                 "DIMENSIONS %lu %lu %lu 1\n", XSize, YSize, ZSize);
00297 
00298   fprintf(file, "ORIGIN 0 0 0 0\n"
00299                 "SPACING 0 0 0 0\n"
00300                 "DATA_SIZE %lu\n", XSize * YSize * ZSize * S);
00301 
00302   fprintf(file, "FIELDS dataName %d float\n", S);
00303   return true;
00304 }
00305 
00306 template <int S>
00307 bool vu1712<S>::readHeader(FILE *file,dword &XSize,dword &YSize,dword &ZSize)
00308 {
00309   if (file == NULL) return false;
00310 
00311   int   i_tmp = 0;
00312   dword d_tmp = 0;
00313   int   ret   = 0;
00314   int   len   = 0;
00315 
00316   //Read in the standard vuVolume header to assert the file type.
00317   ret = fscanf(file,"# vu DataFile Version 1.0\n%n",&len);
00318   if ((len != 26)) {
00319     cerr << "no standard vuVolume header" << endl;
00320     return false;
00321   }
00322 
00323   //Volume Data?
00324   ret = fscanf(file,"Volume Data\n%n",&len);
00325   if ((len != 12)) {
00326     cerr << "no standard vuVolume header" << endl;
00327     return false;
00328   }
00329                 
00330   //Read in the format of the file.
00331   ret = fscanf(file,"BINARY\n%n",&len);
00332   if (len != 7) {
00333     cerr << "no BINARAY file!" << endl;
00334     return false;
00335   }
00336 
00337   //Read in the Dataset type, checking if it's fourier
00338   ret = fscanf(file,"DATASET FOURIER\n%n",&len);
00339   if (len != 16) {
00340     cerr << "no fourier file" << endl;
00341     return false;
00342   }
00343 
00344   //Read in the modality of the data
00345   ret = fscanf(file,"UNIMODAL\n%n",&len);
00346   if (len != 9) {
00347     cerr << "no unimodal file" << endl;
00348     return false;
00349   }
00350 
00351   //Read in the dimensions of the data
00352   ret = fscanf(file,"DIMENSIONS %lu %lu %lu %lu ",&XSize,&YSize,&ZSize,&d_tmp);
00353   if (ret != 4) {
00354     cerr << " XSize=" << d_tmp  << endl;
00355     cerr << "wrong number of dimensions" << endl;
00356     return false;
00357   }
00358 
00359   //Read in the origin of the data
00360   ret = fscanf(file,"ORIGIN %i %i %i %i ", &i_tmp, &i_tmp, &i_tmp, &i_tmp);
00361   if (ret != 4) {
00362     cerr << "origin not set" << endl;
00363     return false;
00364   }
00365 
00366   //Read in the spacing of the data
00367   ret = fscanf(file,"SPACING %lu %lu %lu %lu ", &d_tmp,&d_tmp,&d_tmp,&d_tmp);
00368   if (ret != 4) {
00369     cerr << "spacing not set properly" << endl;
00370     return false;
00371   }
00372 
00373   //Read in the number of data points stored in the file.
00374   ret = fscanf(file,"DATA_SIZE %lu ",&d_tmp);
00375   if (ret != 1) {
00376     cerr << "no DATA_SIZE given" << endl;
00377     return false;
00378   }
00379 
00380   // Read name, number of channels and base type
00381   char dataName[64];
00382   char typeName[16];
00383   int  channels;
00384   ret = fscanf(file,"FIELDS %s %d %s ", dataName, &channels, typeName);
00385   if (ret != 3) {
00386     cerr << "Could not read dataName or number of channels" << endl;
00387     return false;
00388   }
00389   if (channels != S) {
00390     cerr << "wrong number of channels" << endl;
00391     return false;
00392   }
00393 
00394   if (strcmp(typeName, "float") != 0) {
00395     cerr << "typeName is not valid" << endl;
00396     return false;
00397   }
00398 
00399   return true;
00400 }
00401 
00402 // pad n spaces
00403 template <int S>
00404 float* vu1712<S>::pad(float* v, dword n)
00405 {
00406   for (dword i = 0; i < n; ++i) {
00407     *(v++) = 0;
00408   }
00409   return v;
00410 }

Generated on Wed Dec 15 21:20:27 2004 for vuVolume by  doxygen 1.3.9.1