• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PolyVoxUtil/include/PolyVoxUtil/Serialization.inl

Go to the documentation of this file.
00001 /*******************************************************************************
00002 Copyright (c) 2005-2009 David Williams
00003 
00004 This software is provided 'as-is', without any express or implied
00005 warranty. In no event will the authors be held liable for any damages
00006 arising from the use of this software.
00007 
00008 Permission is granted to anyone to use this software for any purpose,
00009 including commercial applications, and to alter it and redistribute it
00010 freely, subject to the following restrictions:
00011 
00012     1. The origin of this software must not be misrepresented; you must not
00013     claim that you wrote the original software. If you use this software
00014     in a product, an acknowledgment in the product documentation would be
00015     appreciated but is not required.
00016 
00017     2. Altered source versions must be plainly marked as such, and must not be
00018     misrepresented as being the original software.
00019 
00020     3. This notice may not be removed or altered from any source
00021     distribution.   
00022 *******************************************************************************/
00023 
00024 namespace PolyVox
00025 {
00026     //Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
00027     //FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
00028     template< template<typename> class VolumeType, typename VoxelType>
00029     polyvox_shared_ptr< VolumeType<VoxelType> > loadVolumeRaw(std::istream& stream, VolumeSerializationProgressListener* progressListener)
00030     {
00031         assert(false); //THIS FUNCTION IS DEPRECATED. REMOVE THIS ASSERT TO CONTINUE, BUT SWITCH TO 'loadVolume()' ASAP.
00032 
00033         //Read volume dimensions
00034         uint8_t volumeWidthPower = 0;
00035         uint8_t volumeHeightPower = 0;
00036         uint8_t volumeDepthPower = 0;
00037         stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
00038         stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
00039         stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
00040 
00041         uint16_t volumeWidth = 0x0001 << volumeWidthPower;
00042         uint16_t volumeHeight = 0x0001 << volumeHeightPower;
00043         uint16_t volumeDepth = 0x0001 << volumeDepthPower;
00044 
00045         //FIXME - need to support non cubic volumes
00046         polyvox_shared_ptr< VolumeType<VoxelType> > volume(new LargeVolume<VoxelType>(volumeWidth, volumeHeight, volumeDepth));
00047 
00048         //Read data
00049         for(uint16_t z = 0; z < volumeDepth; ++z)
00050         {
00051             //Update progress once per slice.
00052             if(progressListener)
00053             {
00054                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00055                 progressListener->onProgressUpdated(fProgress);
00056             }
00057 
00058             for(uint16_t y = 0; y < volumeHeight; ++y)
00059             {
00060                 for(uint16_t x = 0; x < volumeWidth; ++x)
00061                 {
00062                     VoxelType value;
00063                     stream.read(reinterpret_cast<char*>(&value), sizeof(value));
00064 
00065                     volume->setVoxelAt(x,y,z,value);
00066                 }
00067             }
00068         }
00069 
00070         //Finished
00071         if(progressListener)
00072         {
00073             progressListener->onProgressUpdated(1.0f);
00074         }
00075 
00076         return volume;
00077     }
00078 
00079     template< template<typename> class VolumeType, typename VoxelType>
00080     void saveVolumeRaw(std::ostream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00081     {
00082         assert(false); //THIS FUNCTION IS DEPRECATED. REMOVE THIS ASSERT TO CONTINUE, BUT SWITCH TO 'saveVolume()' ASAP.
00083 
00084         //Write volume dimensions
00085         uint16_t volumeWidth = volume.getWidth();
00086         uint16_t volumeHeight = volume.getHeight();
00087         uint16_t volumeDepth  = volume.getDepth();
00088 
00089         uint8_t volumeWidthPower = logBase2(volumeWidth);
00090         uint8_t volumeHeightPower = logBase2(volumeHeight);
00091         uint8_t volumeDepthPower = logBase2(volumeDepth);
00092 
00093         stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
00094         stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
00095         stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
00096 
00097         //Write data
00098         VolumeType<VoxelType>::Sampler volIter(&volume);
00099         for(uint16_t z = 0; z < volumeDepth; ++z)
00100         {
00101             //Update progress once per slice.
00102             if(progressListener)
00103             {
00104                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00105                 progressListener->onProgressUpdated(fProgress);
00106             }
00107 
00108             for(uint16_t y = 0; y < volumeHeight; ++y)
00109             {
00110                 for(uint16_t x = 0; x < volumeWidth; ++x)
00111                 {
00112                     volIter.setPosition(x,y,z);
00113                     VoxelType value = volIter.getVoxel();
00114                     stream.write(reinterpret_cast<char*>(&value), sizeof(value));
00115                 }
00116             }
00117         }
00118 
00119         //Finished
00120         if(progressListener)
00121         {
00122             progressListener->onProgressUpdated(1.0f);
00123         }
00124     }
00125 
00126     //Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
00127     //FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
00128     template< template<typename> class VolumeType, typename VoxelType>
00129     polyvox_shared_ptr< VolumeType<VoxelType> > loadVolumeRle(std::istream& stream, VolumeSerializationProgressListener* progressListener)
00130     {
00131         assert(false); //THIS FUNCTION IS DEPRECATED. REMOVE THIS ASSERT TO CONTINUE, BUT SWITCH TO 'loadVolume()' ASAP.
00132 
00133         //Read volume dimensions
00134         uint8_t volumeWidthPower = 0;
00135         uint8_t volumeHeightPower = 0;
00136         uint8_t volumeDepthPower = 0;
00137         stream.read(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
00138         stream.read(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
00139         stream.read(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
00140 
00141         uint16_t volumeWidth = 0x0001 << volumeWidthPower;
00142         uint16_t volumeHeight = 0x0001 << volumeHeightPower;
00143         uint16_t volumeDepth = 0x0001 << volumeDepthPower;
00144 
00145         //FIXME - need to support non cubic volumes
00146         polyvox_shared_ptr< VolumeType<VoxelType> > volume(new LargeVolume<VoxelType>(volumeWidth, volumeHeight, volumeDepth));
00147 
00148         //Read data
00149         bool firstTime = true;
00150         uint32_t runLength = 0;
00151         VoxelType value;
00152         stream.read(reinterpret_cast<char*>(&value), sizeof(value));
00153         stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00154         for(uint16_t z = 0; z < volumeDepth; ++z)
00155         {
00156             //Update progress once per slice.
00157             if(progressListener)
00158             {
00159                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00160                 progressListener->onProgressUpdated(fProgress);
00161             }
00162 
00163             for(uint16_t y = 0; y < volumeHeight; ++y)
00164             {
00165                 for(uint16_t x = 0; x < volumeWidth; ++x)
00166                 {   
00167                     if(runLength != 0)
00168                     {
00169                         volume->setVoxelAt(x,y,z,value);
00170                         runLength--;
00171                     }
00172                     else
00173                     {
00174                         stream.read(reinterpret_cast<char*>(&value), sizeof(value));
00175                         stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00176 
00177                         volume->setVoxelAt(x,y,z,value);
00178                         runLength--;
00179                     }
00180                 }
00181             }           
00182         }
00183 
00184         //Finished
00185         if(progressListener)
00186         {
00187             progressListener->onProgressUpdated(1.0f);
00188         }
00189 
00190         return volume;
00191     }
00192 
00193     template< template<typename> class VolumeType, typename VoxelType>
00194     void saveVolumeRle(std::ostream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00195     {
00196         assert(false); //THIS FUNCTION IS DEPRECATED. REMOVE THIS ASSERT TO CONTINUE, BUT SWITCH TO 'saveVolume()' ASAP.
00197 
00198         //Write volume dimensions
00199         uint16_t volumeWidth = volume.getWidth();
00200         uint16_t volumeHeight = volume.getHeight();
00201         uint16_t volumeDepth  = volume.getDepth();
00202 
00203         uint8_t volumeWidthPower = logBase2(volumeWidth);
00204         uint8_t volumeHeightPower = logBase2(volumeHeight);
00205         uint8_t volumeDepthPower = logBase2(volumeDepth);
00206 
00207         stream.write(reinterpret_cast<char*>(&volumeWidthPower), sizeof(volumeWidthPower));
00208         stream.write(reinterpret_cast<char*>(&volumeHeightPower), sizeof(volumeHeightPower));
00209         stream.write(reinterpret_cast<char*>(&volumeDepthPower), sizeof(volumeDepthPower));
00210 
00211         //Write data
00212         VolumeType<VoxelType>::Sampler volIter(&volume);
00213         VoxelType current;
00214         uint32_t runLength = 0;
00215         bool firstTime = true;
00216         for(uint16_t z = 0; z < volumeDepth; ++z)
00217         {
00218             //Update progress once per slice.
00219             if(progressListener)
00220             {
00221                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00222                 progressListener->onProgressUpdated(fProgress);
00223             }
00224 
00225             for(uint16_t y = 0; y < volumeHeight; ++y)
00226             {
00227                 for(uint16_t x = 0; x < volumeWidth; ++x)
00228                 {       
00229                     volIter.setPosition(x,y,z);
00230                     VoxelType value = volIter.getVoxel();
00231                     if(firstTime)
00232                     {
00233                         current = value;
00234                         runLength = 1;
00235                         firstTime = false;
00236                     }
00237                     else
00238                     {
00239                         if(value == current)
00240                         {
00241                             runLength++;
00242                         }
00243                         else
00244                         {
00245                             stream.write(reinterpret_cast<char*>(&current), sizeof(current));
00246                             stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00247                             current = value;
00248                             runLength = 1;
00249                         }
00250                     }                   
00251                 }
00252             }
00253         }
00254         stream.write(reinterpret_cast<char*>(&current), sizeof(current));
00255         stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00256 
00257         //Finished
00258         if(progressListener)
00259         {
00260             progressListener->onProgressUpdated(1.0f);
00261         }
00262     }
00263 
00265     // New version of load/save code with versioning
00267 
00268     template< template<typename> class VolumeType, typename VoxelType>
00269     bool loadVolume(std::istream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00270     {
00271         char pIdentifier[8];
00272         stream.read(pIdentifier, 7);
00273         pIdentifier[7] = '\0'; //Set the null terminator
00274         if(strcmp(pIdentifier, "PolyVox") != 0)
00275         {
00276             return false;
00277         }
00278 
00279         uint16_t uVersion;
00280         stream.read(reinterpret_cast<char*>(&uVersion), sizeof(uVersion));
00281 
00282         switch(uVersion)
00283         {
00284             case 0:
00285                 return loadVersion0(stream, volume, progressListener);
00286                 //Return means no need to break...
00287             default:
00288                 return false;
00289         }
00290         
00291     }
00292 
00293     template< template<typename> class VolumeType, typename VoxelType>
00294     bool saveVolume(std::ostream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00295     {
00296         char pIdentifier[] = "PolyVox";
00297         stream.write(pIdentifier, 7);
00298 
00299         uint16_t uVersion = 0;
00300         stream.write(reinterpret_cast<const char*>(&uVersion), sizeof(uVersion));
00301 
00302         return saveVersion0(stream, volume, progressListener);
00303     }
00304 
00305     //Note: we don't do much error handling in here - exceptions will simply be propergated up to the caller.
00306     //FIXME - think about pointer ownership issues. Or could return volume by value if the copy constructor is shallow
00307     template< template<typename> class VolumeType, typename VoxelType>
00308     bool loadVersion0(std::istream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00309     {
00310         //Read volume dimensions
00311         uint16_t volumeWidth = 0;
00312         uint16_t volumeHeight = 0;
00313         uint16_t volumeDepth = 0;
00314         stream.read(reinterpret_cast<char*>(&volumeWidth), sizeof(volumeWidth));
00315         stream.read(reinterpret_cast<char*>(&volumeHeight), sizeof(volumeHeight));
00316         stream.read(reinterpret_cast<char*>(&volumeDepth), sizeof(volumeDepth));
00317 
00318         //Resize the volume
00319         //HACK - Forces block size to 32. This functions needs reworking anyway due to large volume support.
00320         volume.resize(Region(Vector3DInt32(0,0,0), Vector3DInt32(volumeWidth, volumeHeight, volumeDepth)), 32);
00321 
00322         //Read data
00323         bool firstTime = true;
00324         uint32_t runLength = 0;
00325         VoxelType value;
00326         stream.read(reinterpret_cast<char*>(&value), sizeof(value));
00327         stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00328         for(uint16_t z = 0; z < volumeDepth; ++z)
00329         {
00330             //Update progress once per slice.
00331             if(progressListener)
00332             {
00333                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00334                 progressListener->onProgressUpdated(fProgress);
00335             }
00336 
00337             for(uint16_t y = 0; y < volumeHeight; ++y)
00338             {
00339                 for(uint16_t x = 0; x < volumeWidth; ++x)
00340                 {   
00341                     if(runLength != 0)
00342                     {
00343                         volume.setVoxelAt(x,y,z,value);
00344                         runLength--;
00345                     }
00346                     else
00347                     {
00348                         stream.read(reinterpret_cast<char*>(&value), sizeof(value));
00349                         stream.read(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00350 
00351                         volume.setVoxelAt(x,y,z,value);
00352                         runLength--;
00353                     }
00354                 }
00355             }           
00356         }
00357 
00358         //Finished
00359         if(progressListener)
00360         {
00361             progressListener->onProgressUpdated(1.0f);
00362         }
00363 
00364         return true;
00365     }
00366 
00367     template< template<typename> class VolumeType, typename VoxelType>
00368     bool saveVersion0(std::ostream& stream, VolumeType<VoxelType>& volume, VolumeSerializationProgressListener* progressListener)
00369     {
00370         //Write volume dimensions
00371         uint16_t volumeWidth = volume.getWidth();
00372         uint16_t volumeHeight = volume.getHeight();
00373         uint16_t volumeDepth  = volume.getDepth();
00374 
00375         stream.write(reinterpret_cast<char*>(&volumeWidth), sizeof(volumeWidth));
00376         stream.write(reinterpret_cast<char*>(&volumeHeight), sizeof(volumeHeight));
00377         stream.write(reinterpret_cast<char*>(&volumeDepth), sizeof(volumeDepth));
00378 
00379         //Write data
00380         VolumeType<VoxelType>::Sampler volIter(&volume);
00381         VoxelType current;
00382         uint32_t runLength = 0;
00383         bool firstTime = true;
00384         for(uint16_t z = 0; z < volumeDepth; ++z)
00385         {
00386             //Update progress once per slice.
00387             if(progressListener)
00388             {
00389                 float fProgress = static_cast<float>(z) / static_cast<float>(volumeDepth);
00390                 progressListener->onProgressUpdated(fProgress);
00391             }
00392 
00393             for(uint16_t y = 0; y < volumeHeight; ++y)
00394             {
00395                 for(uint16_t x = 0; x < volumeWidth; ++x)
00396                 {       
00397                     volIter.setPosition(x,y,z);
00398                     VoxelType value = volIter.getVoxel();
00399                     if(firstTime)
00400                     {
00401                         current = value;
00402                         runLength = 1;
00403                         firstTime = false;
00404                     }
00405                     else
00406                     {
00407                         if(value == current)
00408                         {
00409                             runLength++;
00410                         }
00411                         else
00412                         {
00413                             stream.write(reinterpret_cast<char*>(&current), sizeof(current));
00414                             stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00415                             current = value;
00416                             runLength = 1;
00417                         }
00418                     }                   
00419                 }
00420             }
00421         }
00422         stream.write(reinterpret_cast<char*>(&current), sizeof(current));
00423         stream.write(reinterpret_cast<char*>(&runLength), sizeof(runLength));
00424 
00425         //Finished
00426         if(progressListener)
00427         {
00428             progressListener->onProgressUpdated(1.0f);
00429         }
00430 
00431         return true;
00432     }
00433 }

Generated on Sat Nov 19 2011 00:27:31 for PolyVox by  doxygen 1.7.1