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

BCC/Unimodal/3d/1B/1B.cpp

Go to the documentation of this file.
00001 #include "1B.h"
00002 #include <stdio.h>
00003 
00004 //----------------------------------------------------------------------------
00005 //------------------------- public read() ------------------------------------
00006 //----------------------------------------------------------------------------
00007 
00008 bool vu15121::read(void)
00009 {
00010     if (m_FileName.isEmpty()) return setError("No file name specified.");
00011     
00012     FILE *file = fopen(m_FileName,"rb");
00013     if (file != NULL)
00014     {
00015         bool success = read(file);
00016         fclose(file);
00017         return success;
00018     }
00019     else
00020         return setError("Could not open the specified file.");
00021 }
00022 
00023 //----------------------------------------------------------------------------
00024 //------------------------- public write() -----------------------------------
00025 //----------------------------------------------------------------------------
00026 
00027 bool vu15121::write(void)
00028 {
00029     if (m_FileName.isEmpty()) return setError("No file name specified.");
00030     
00031     FILE *file = fopen(m_FileName,"wb");
00032     if (file != NULL)
00033     {
00034         bool success = write(file);
00035         fclose(file);
00036         return success;
00037     }
00038     else
00039         return setError("Could not open the specified file.");
00040 }
00041 
00042 //----------------------------------------------------------------------------
00043 //------------------------- protected read() ---------------------------------
00044 //----------------------------------------------------------------------------
00045 
00046 bool vu15121::read(FILE *file)
00047 {
00048     int ret = 0;
00049     dword size = 0;
00050     int len = 0;
00051 
00052     //Read in the base data.
00053     bool success = vu1512::read(file);
00054     if (!success) return false;
00055 
00056     //Read in the name of the data
00057     char dataName[64];
00058     ret = fscanf(file,"SCALARS %s ",dataName);
00059     if (ret != 1) return setInvalidFormatError();
00060     //Store the name of the data
00061     m_DataName = dataName;
00062 
00063     //Read in the type of data and the colour lookup table.
00064     fscanf(file,"byte LOOKUP_TABLE default%n",&len);
00065     if ((len < 25) || (fgetc(file) != '\n')) return setInvalidFormatError();
00066 
00067     //Allocate memory for the volume data
00068     m_Data = new byte[m_DataSize];
00069 
00070     //Read in the volume data according to the format of the file
00071     if (m_Binary)
00072         size = fread(m_Data,sizeof(byte),m_DataSize,file);
00073     else
00074     {
00075         ret = 1;
00076         dword i = 0;
00077         while ((ret > 0) && (i < m_DataSize))
00078             ret = fscanf(file," %c",&m_Data[i++]);
00079         size = i;
00080     }
00081     
00082     //Make sure that the right amount of data was read in
00083     if (size != m_DataSize) return setInvalidFormatError();
00084 
00085     return true;
00086 }
00087 
00088 //----------------------------------------------------------------------------
00089 //------------------------- protected write() --------------------------------
00090 //----------------------------------------------------------------------------
00091 
00092 bool vu15121::write(FILE *file)
00093 {
00094     int ret;
00095     dword size = 0;
00096 
00097     m_Binary = true;
00098 
00099     bool success = vu1512::write(file);
00100     if (!success) return false;
00101     
00102     fprintf(file,"SCALARS ");
00103 
00104     //Write the name of the data
00105     if (!m_DataName.isEmpty())
00106         fprintf(file,"%s ",m_DataName.c_str());
00107     else
00108         fprintf(file,"data ");
00109 
00110     fprintf(file,"byte\nLOOKUP_TABLE default\n");
00111 
00112     //Write the volume data according to it's format
00113     if (m_Binary)
00114         size = fwrite(m_Data,sizeof(byte),m_DataSize,file);
00115     else
00116     {
00117         ret = 1;
00118         dword i = 0;
00119         while ((ret > 0) && (i < m_DataSize))
00120             ret = fprintf(file," %d",m_Data[i++]);
00121         size = i;
00122     }
00123     
00124     if (size == m_DataSize)
00125         return true;
00126     else
00127         return setWriteError();
00128 }
00129 
00130 unsigned char vu15121::getDataValue(unsigned int x, unsigned int y, unsigned int z)
00131 {
00132   if ( (x<0) || (x>=m_Dim1Size) || (y<0) || (y>=m_Dim2Size) || (z<0) || (z>=m_Dim3Size) )
00133     return 0;
00134   else
00135     return m_Data[x+y*m_Dim1Size+z*m_Dim1Size*m_Dim2Size];
00136 }
00137 
00138 double vu15121::getDataValue(vuVector point)
00139 {
00140   //
00141   //nearest neighbor
00142   //
00143 
00144   int x1, y1, z1, x2, y2, z2;
00145 
00146   // nearest neighbor in primary grid 
00147   x1 =   int(point[0]/T + 0.5);
00148   y1 =   int(point[1]/T + 0.5);
00149   z1 = 2*int(point[2]/T + 0.5);
00150 
00151   // nearest neighbor in secondary grid 
00152   x2 =   int(point[0]/T);
00153   y2 =   int(point[1]/T);
00154   z2 = 2*int(point[2]/T) + 1;
00155 
00156   double d1, d2;
00157 
00158   d1 = ((point[0] - float(x1)*T    )*(point[0] - float(x1)*T    ) +
00159         (point[1] - float(y1)*T    )*(point[1] - float(y1)*T    ) +
00160         (point[2] - float(z1)*T*0.5)*(point[2] - float(z1)*T*0.5));
00161   //SB011027: BUG fixed. (?)
00162   d2 = ((point[0] - (float(x2) + 0.5)*T    )*(point[0] - (float(x2) + 0.5)*T    ) +
00163         (point[1] - (float(y2) + 0.5)*T    )*(point[1] - (float(y2) + 0.5)*T    ) +
00164         (point[2] - (float(z2) + 0.5)*T*0.5)*(point[2] - (float(z2) + 0.5)*T*0.5));
00165 
00166   if (d1 < d2)
00167     return getDataValue(x1, y1, z1);
00168   else
00169     return getDataValue(x2, y2, z2);
00170 }
00171 
00172 vuVector vu15121::getGradient(unsigned int i, unsigned int j, unsigned int k)
00173 {
00174   i += (k % 2);
00175   j += (k % 2);
00176 
00177   vuVector t(0.5*(double(getDataValue(i+1, j  , k  ) - getDataValue(i-1, j  , k  ))),
00178              0.5*(double(getDataValue(i  , j+1, k  ) - getDataValue(i  , j-1, k  ))),
00179              0.5*(double(getDataValue(i  , j  , k+2) - getDataValue(i  , j  , k-2))));
00180 
00181   return t;
00182 }
00183 
00184 vuVector vu15121::getGradient(vuVector point)
00185 {
00186   //
00187   //nearest neighbor
00188   //
00189 
00190   int x1, y1, z1, x2, y2, z2;
00191 
00192   // nearest neighbor in primary grid 
00193   x1 =   int(point[0]/T + 0.5);
00194   y1 =   int(point[1]/T + 0.5);
00195   z1 = 2*int(point[2]/T + 0.5);
00196 
00197   // nearest neighbor in secondary grid 
00198   x2 =   int(point[0]/T);
00199   y2 =   int(point[1]/T);
00200   z2 = 2*int(point[2]/T) + 1;
00201 
00202   double d1, d2;
00203 
00204   d1 = ((point[0] - float(x1)*T    )*(point[0] - float(x1)*T    ) +
00205         (point[1] - float(y1)*T    )*(point[1] - float(y1)*T    ) +
00206         (point[2] - float(z1)*T*0.5)*(point[2] - float(z1)*T*0.5));
00207 
00208   d2 = ((point[0] - (float(x2) + 0.5)*T    )*(point[0] - (float(x2) + 0.5)*T    ) +
00209         (point[1] - (float(y2) + 0.5)*T    )*(point[1] - (float(y2) + 0.5)*T    ) +
00210         (point[2] - (float(z2) + 0.5)*T*0.5)*(point[2] - (float(z2) + 0.5)*T*0.5));
00211 
00212   if (d1 < d2)
00213     return getGradient(x1, y1, z1);
00214   else
00215     return getGradient(x2, y2, z2);
00216 }

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