00001 #include "spheric.h"
00002 #include "vuCamera/vuParallelCamera.h"
00003 #include "vuLightfield/vuSphericView.h"
00004 #include "vuLinAlg/vuSpherical.h"
00005 #include "vuLinAlg/vuMatrix.h"
00006 #include <math.h>
00007
00008 template <int SIZE, class TYPE> vu1611<SIZE,TYPE>::vu1611()
00009 {
00010 m_width = 0;
00011 m_height = 0;
00012 m_numberOfViews = 0;
00013 m_views = NULL;
00014 m_Camera = new vuParallelCamera();
00015 }
00016
00018 template <int SIZE, class TYPE>
00019 vu1611<SIZE,TYPE>::vu1611(const vu1611& inst)
00020 {
00021 operator=(inst);
00022 }
00023
00025 template <int SIZE, class TYPE>
00026 vu1611<SIZE,TYPE>& vu1611<SIZE,TYPE>::operator=(const vu1611& rhs)
00027 {
00028 if (this != &rhs) {
00029 vu161::operator=(rhs);
00030 m_width = rhs.m_width;
00031 m_height = rhs.m_height;
00032 m_numberOfViews = rhs.m_numberOfViews;
00033
00034 _initViews();
00035 }
00036 return *this;
00037 }
00038
00039 template <int SIZE, class TYPE>
00040 vu1611<SIZE,TYPE>::~vu1611()
00041 {
00042 if (m_views != NULL) {
00043 delete [] m_views;
00044 m_views = NULL;
00045 m_numberOfViews = 0;
00046 }
00047 }
00048
00049 template <int SIZE, class TYPE>
00050 dword vu1611<SIZE,TYPE>::getWidth(void) const
00051 {
00052 return m_width;
00053 }
00054
00055 template <int SIZE, class TYPE>
00056 dword vu1611<SIZE,TYPE>::getHeight(void) const
00057 {
00058 return m_height;
00059 }
00060
00061 template <int SIZE, class TYPE>
00062 dword vu1611<SIZE,TYPE>::getNumberOfViews(void) const
00063 {
00064 return m_numberOfViews;
00065 }
00066
00067 template <int SIZE, class TYPE>
00068 vuSphericView<SIZE,TYPE> *vu1611<SIZE,TYPE>::getView(dword i)
00069 {
00070 return &m_views[i];
00071 }
00072
00073 template <int SIZE, class TYPE>
00074 int vu1611<SIZE,TYPE>::getIndexOfView(vuSphericView<SIZE,TYPE> *view)
00075 {
00076 for (dword i=0; i<m_numberOfViews; i++) {
00077 if (view == &m_views[i]) return i;
00078 }
00079 return -1;
00080 }
00081
00082 template <int SIZE, class TYPE>
00083 vuSphericView<SIZE,TYPE>* &vu1611<SIZE,TYPE>::getViews()
00084 {
00085 return m_views;
00086 }
00087
00088 template <int SIZE, class TYPE>
00089 const char *vu1611<SIZE,TYPE>::_typeName()
00090 {
00091 if (sizeof(TYPE) == 1)
00092 return "byte";
00093 else
00094 return "float";
00095 }
00096
00097 template <int SIZE, class TYPE>
00098 void vu1611<SIZE,TYPE>::setViewVectors(const vuVector&,const vuVector&,const vuVector&)
00099 {
00100 cerr << "vu1611.setViewVectors(): is depricated and should not be";
00101 cerr << " supported anymore";
00102 throw "vu1611.setViewVectors(): is depricated and should not be"
00103 " supported anymore";
00104 }
00105
00106 template <int SIZE, class TYPE>
00107 bool vu1611<SIZE,TYPE>::read(FILE *file)
00108 {
00109 int ret = 0;
00110 dword size = 0;
00111 int len = 0;
00112
00113 if (m_views != NULL) {
00114 delete [] m_views;
00115 m_views = NULL;
00116 m_numberOfViews = 0;
00117 }
00118 if (m_Data != NULL) {
00119 delete [] m_Data;
00120 m_Data = NULL;
00121 m_DataSize = 0;
00122 }
00123
00124
00125 bool success = vu161::read(file);
00126 if (!success) return false;
00127
00128
00129 ret = fscanf(file,"SPHERIC %lu %lu %lu ", &m_width, &m_height,
00130 &m_numberOfViews);
00131 if (ret != 3)
00132 return setError("Could not read width, height or numberOfViews");
00133
00134
00135 if (m_width <= 1) return setError("Could not read width");
00136 if (m_height <= 1) return setError("Could not read height");
00137 if (m_numberOfViews <=1) return setError("Could not read numberOfViews");
00138
00139
00140 ret = fscanf(file,"DATA_SIZE %lu ", &m_DataSize);
00141 if (ret != 1)
00142 return setError("Could not read DataSize");
00143 if (!_isDataSizeValid(m_DataSize))
00144 return setError("DataSize is not valid");
00145
00146
00147 int typeSize;
00148 char dataName[64];
00149 char typeName[32];
00150 ret = fscanf(file,"FIELDS %s %d %s", dataName, &typeSize, typeName);
00151 if (ret != 3)
00152 return setError("Could not read dataName, typeSize or typeName");
00153 if (typeSize != SIZE)
00154 return setError("typeSize is not valid");
00155 if (strcmp(typeName, _typeName()) != 0)
00156 return setError("typeName is not valid");
00157
00158 m_DataName = dataName;
00159
00160
00161 fscanf(file,"\nLOOKUP_TABLE default%n",&len);
00162 if ((len < 21) || (fgetc(file) != '\n'))
00163 return setError("colour lookup table is not valid");
00164
00165
00166 m_Data = new byte[m_DataSize];
00167
00168
00169 if (m_Binary)
00170 size = fread(m_Data,sizeof(byte),m_DataSize,file);
00171 else {
00172 ret = 1;
00173 dword i = 0;
00174 while ((ret > 0) && (i < m_DataSize))
00175 ret = fscanf(file," %c",&m_Data[i++]);
00176 size = i;
00177 }
00178
00179
00180 if (size != m_DataSize) return setInvalidFormatError();
00181
00182
00183 _initViews();
00184
00185 m_Camera->setPosition(m_Camera->getLookAtVector() * -1);
00186 m_Camera->init();
00187
00188 return true;
00189 }
00190
00191 template <int SIZE, class TYPE>
00192 bool vu1611<SIZE,TYPE>::write(FILE *file)
00193 {
00194 int ret;
00195 dword size = 0;
00196
00197 m_Binary = true;
00198
00199 bool success = vu161::write(file);
00200 if (!success) return setWriteError();
00201
00202
00203 ret = fprintf(file,"SPHERIC %lu %lu %lu\n",m_width,m_height,
00204 m_numberOfViews);
00205 if (ret <3) return setWriteError();
00206
00207
00208 ret = fprintf(file,"DATA_SIZE %lu\n",m_DataSize);
00209 if (ret == 0) return setWriteError();
00210
00211 fprintf(file,"FIELDS ");
00212
00213
00214 if (!m_DataName.isEmpty())
00215 fprintf(file,"%s ",m_DataName.c_str());
00216 else
00217 fprintf(file,"data ");
00218
00219 fprintf(file,"%d %s\nLOOKUP_TABLE default\n", SIZE, _typeName());
00220
00221 _syncViewsToBuffer();
00222
00223
00224 if (m_Binary)
00225 size = fwrite(m_Data,sizeof(byte),m_DataSize,file);
00226 else
00227 {
00228 ret = 1;
00229 dword i = 0;
00230 while ((ret > 0) && (i < m_DataSize))
00231 ret = fprintf(file," %d",m_Data[i++]);
00232 size = i;
00233 }
00234
00235 if (size != m_DataSize)
00236 return setWriteError();
00237 return true;
00238 }
00239
00240 template <int SIZE, class TYPE>
00241 void vu1611<SIZE,TYPE>::glResize(dword width, dword height)
00242 {
00243 if (m_Camera != NULL) {
00244 m_Camera->setWidth(width);
00245 m_Camera->setHeight(height);
00246 m_Camera->init();
00247 }
00248 else {
00249 cerr << "Warning: vu1611::glResize() no camera set!" << endl;
00250 }
00251 }
00252
00253
00254
00255
00256
00257 template <int SIZE, class TYPE>
00258 void vu1611<SIZE,TYPE>::_initViews()
00259 {
00260 if (m_Data != NULL) {
00261 if (m_views != NULL) delete [] m_views;
00262 m_views = new vuSphericView<SIZE,TYPE>[m_numberOfViews];
00263
00264
00265 byte *ptr = m_Data;
00266 dword step = vuSphericView<SIZE,TYPE>::getSizeInByte(m_width, m_height);
00267
00268 for (dword i=0; i<m_numberOfViews; i++) {
00269 m_views[i].readFromBuffer(ptr, m_width, m_height);
00270 ptr += step;
00271 }
00272 }
00273 }
00274
00275 template <int SIZE, class TYPE>
00276 bool vu1611<SIZE,TYPE>::_isDataSizeValid(dword dataSize)
00277 {
00278 dword size = vuSphericView<SIZE,TYPE>::getSizeInByte(m_width, m_height);
00279 return (dataSize == (size * m_numberOfViews));
00280 }
00281
00282 template <int SIZE, class TYPE>
00283 void vu1611<SIZE,TYPE>::_syncViewsToBuffer()
00284 {
00285 byte *ptr = m_Data;
00286 dword step = vuSphericView<SIZE,TYPE>::getSizeInByte(m_width, m_height);
00287
00288 for (dword i=0; i<m_numberOfViews; i++) {
00289 m_views[i].writeIntoBuffer(ptr, m_width, m_height);
00290 ptr += step;
00291 }
00292 }
00293
00294 template <int SIZE, class TYPE>
00295 vuVector vu1611<SIZE,TYPE>::getCenter() const { return vuVector(0,0,0); }
00296
00297
00298 template <int SIZE, class TYPE>
00299 bool vu1611<SIZE,TYPE>::read(void)
00300 {
00301 if (m_FileName.isEmpty()) return setError("No file name specified.");
00302
00303 FILE *file = fopen(m_FileName,"rb");
00304 if (file != NULL) {
00305 bool success = read(file);
00306 fclose(file);
00307 return success;
00308 }
00309 else
00310 return setError("Could not open the specified file.");
00311 }
00312
00313 template <int SIZE, class TYPE>
00314 bool vu1611<SIZE,TYPE>::write(void)
00315 {
00316 if (m_FileName.isEmpty()) return setError("No file name specified.");
00317
00318 FILE *file = fopen(m_FileName,"wb");
00319 if (file != NULL)
00320 {
00321 bool success = write(file);
00322 fclose(file);
00323 return success;
00324 }
00325 else
00326 return setError("Could not open the specified file.");
00327 }
00328
00329 template <int SIZE, class TYPE>
00330 bool vu1611<SIZE,TYPE>::write(const vuString &fileName)
00331 {
00332 vuString tmp = m_FileName;
00333 bool result = false;
00334 m_FileName = fileName;
00335 result = write();
00336 m_FileName = tmp;
00337 return result;
00338 }