glc_bsrep.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003  This file is part of the GLC-lib library.
00004  Copyright (C) 2005-2008 Laurent Ribon (laumaya@users.sourceforge.net)
00005  Version 2.0.0 Beta 1, packaged on April 2010.
00006 
00007  http://glc-lib.sourceforge.net
00008 
00009  GLC-lib is free software; you can redistribute it and/or modify
00010  it under the terms of the GNU General Public License as published by
00011  the Free Software Foundation; either version 2 of the License, or
00012  (at your option) any later version.
00013 
00014  GLC-lib is distributed in the hope that it will be useful,
00015  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  GNU General Public License for more details.
00018 
00019  You should have received a copy of the GNU General Public License
00020  along with GLC-lib; if not, write to the Free Software
00021  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023  *****************************************************************************/
00025 
00026 #include "glc_bsrep.h"
00027 #include "../glc_fileformatexception.h"
00028 
00029 // The binary rep suffix
00030 const QString GLC_BSRep::m_Suffix("BSRep");
00031 
00032 // The binary rep magic number
00033 const QUuid GLC_BSRep::m_Uuid("{d6f97789-36a9-4c2e-b667-0e66c27f839f}");
00034 
00035 // The binary rep version
00036 const quint32 GLC_BSRep::m_Version= 100;
00037 
00038 
00039 // Default constructor
00040 GLC_BSRep::GLC_BSRep(const QString& fileName, bool useCompression)
00041 : m_FileInfo()
00042 , m_pFile(NULL)
00043 , m_DataStream()
00044 , m_UseCompression(useCompression)
00045 , m_CompressionLevel(-1)
00046 {
00047         setAbsoluteFileName(fileName);
00048         m_DataStream.setVersion(QDataStream::Qt_4_6);
00049         m_DataStream.setFloatingPointPrecision(QDataStream::SinglePrecision);
00050 }
00051 
00052 // Copy constructor
00053 GLC_BSRep::GLC_BSRep(const GLC_BSRep& binaryRep)
00054 : m_FileInfo(binaryRep.m_FileInfo)
00055 , m_pFile(NULL)
00056 , m_DataStream()
00057 , m_UseCompression(binaryRep.m_UseCompression)
00058 , m_CompressionLevel(binaryRep.m_CompressionLevel)
00059 {
00060         m_DataStream.setVersion(QDataStream::Qt_4_6);
00061         m_DataStream.setFloatingPointPrecision(binaryRep.m_DataStream.floatingPointPrecision());
00062 }
00063 
00064 GLC_BSRep::~GLC_BSRep()
00065 {
00066         delete m_pFile;
00067 }
00068 
00069 // Return true if the binary rep is up to date
00070 bool GLC_BSRep::repIsUpToDate(const QDateTime& timeStamp)
00071 {
00072         //qDebug() << "GLC_BSRep::repIsUpToDate";
00073         bool isUpToDate= false;
00074         if (open(QIODevice::ReadOnly))
00075         {
00076                 if (headerIsOk())
00077                 {
00078                         isUpToDate= timeStampOk(timeStamp);
00079                         isUpToDate= isUpToDate && close();
00080                 }
00081                 else
00082                 {
00083                         QString message(QString("GLC_BSRep::loadRep File not recognise ") + m_FileInfo.fileName());
00084                         qDebug() << message;
00085                         GLC_FileFormatException fileFormatException(message, m_FileInfo.fileName(), GLC_FileFormatException::WrongFileFormat);
00086                         close();
00087                         throw(fileFormatException);
00088                 }
00089         }
00090         else
00091         {
00092                 QString message(QString("GLC_BSRep::loadRep Enable to open the file ") + m_FileInfo.fileName());
00093                 qDebug() << message;
00094                 GLC_FileFormatException fileFormatException(message, m_FileInfo.fileName(), GLC_FileFormatException::FileNotFound);
00095                 close();
00096                 throw(fileFormatException);
00097         }
00098 
00099         if (!isUpToDate) qDebug() << "Rep is not up to date";
00100         return isUpToDate;
00101 }
00102 
00104 // name Get Functions
00106 // Load the binary rep
00107 GLC_3DRep GLC_BSRep::loadRep()
00108 {
00109         //qDebug() << "GLC_BSRep::loadRep";
00110         GLC_3DRep loadedRep;
00111 
00112         if (open(QIODevice::ReadOnly))
00113         {
00114                 if (headerIsOk())
00115                 {
00116                         timeStampOk(QDateTime());
00117                         GLC_BoundingBox boundingBox;
00118                         m_DataStream >> boundingBox;
00119                         bool useCompression;
00120                         m_DataStream >> useCompression;
00121                         if (useCompression)
00122                         {
00123                                 QByteArray CompresseBuffer;
00124                                 m_DataStream >> CompresseBuffer;
00125                                 QByteArray uncompressedBuffer= qUncompress(CompresseBuffer);
00126                                 CompresseBuffer.clear();
00127                                 QDataStream bufferStream(uncompressedBuffer);
00128                                 bufferStream >> loadedRep;
00129                         }
00130                         else
00131                         {
00132                                 m_DataStream >> loadedRep;
00133                         }
00134 
00135                         if (!close())
00136                         {
00137                                 QString message(QString("GLC_BSRep::loadRep An error occur when loading file ") + m_FileInfo.fileName());
00138                                 qDebug() << message;
00139                                 GLC_FileFormatException fileFormatException(message, m_FileInfo.fileName(), GLC_FileFormatException::WrongFileFormat);
00140                                 throw(fileFormatException);
00141                         }
00142                 }
00143                 else
00144                 {
00145                         QString message(QString("GLC_BSRep::loadRep File not supported ") + m_FileInfo.fileName());
00146                         qDebug() << message;
00147                         GLC_FileFormatException fileFormatException(message, m_FileInfo.fileName(), GLC_FileFormatException::FileNotSupported);
00148                         close();
00149                         throw(fileFormatException);
00150                 }
00151         }
00152         else
00153         {
00154                 QString message(QString("GLC_BSRep::loadRep Enable to open the file ") + m_FileInfo.fileName());
00155                 qDebug() << message;
00156                 GLC_FileFormatException fileFormatException(message, m_FileInfo.fileName(), GLC_FileFormatException::FileNotFound);
00157                 close();
00158                 throw(fileFormatException);
00159         }
00160 
00161 
00162         return loadedRep;
00163 }
00164 
00165 // Return the bounding box of the binary representation
00166 GLC_BoundingBox GLC_BSRep::boundingBox()
00167 {
00168         GLC_BoundingBox boundingBox;
00169 
00170         if (open(QIODevice::ReadOnly))
00171         {
00172                 if (headerIsOk())
00173                 {
00174                         timeStampOk(QDateTime());
00175 
00176                         m_DataStream >> boundingBox;
00177                 }
00178         }
00179         return boundingBox;
00180 }
00181 
00182 // Return bsrep suffix
00183 QString GLC_BSRep::suffix()
00184 {
00185         return m_Suffix;
00186 }
00187 
00189 //name Set Functions
00191 // Set the binary representation file name
00192 void GLC_BSRep::setAbsoluteFileName(const QString& fileName)
00193 {
00194         m_FileInfo.setFile(fileName);
00195         if (m_FileInfo.suffix() != m_Suffix)
00196         {
00197                 m_FileInfo.setFile(fileName + '.' + m_Suffix);
00198         }
00199 
00200 }
00201 
00202 // Save the GLC_3DRep in serialised binary
00203 bool GLC_BSRep::save(const GLC_3DRep& rep)
00204 {
00205         qDebug() << "GLC_BSRep::save";
00207         bool saveOk= open(QIODevice::WriteOnly);
00208         if (saveOk)
00209         {
00210                 writeHeader(rep.lastModified());
00211 
00212                 // Representation Bounding Box
00213                 m_DataStream << rep.boundingBox();
00214 
00215                 // Compression usage
00216                 m_DataStream << m_UseCompression;
00217                 if (m_UseCompression)
00218                 {
00219                         QByteArray uncompressedBuffer;
00220                         QDataStream bufferStream(&uncompressedBuffer, QIODevice::WriteOnly);
00221                         bufferStream << rep;
00222                         m_DataStream << qCompress(uncompressedBuffer, m_CompressionLevel);
00223                 }
00224                 else
00225                 {
00226                         // Binary representation geometry
00227                         // Add the rep
00228                         m_DataStream << rep;
00229                 }
00230 
00231                 // Close the file
00232                 saveOk= close();
00233         }
00234         return saveOk;
00235 }
00236 
00237 
00238 // Open the file
00239 bool GLC_BSRep::open(QIODevice::OpenMode mode)
00240 {
00241         //qDebug() << "Open :" << m_FileInfo.fileName();
00242         bool openOk= m_FileInfo.exists();
00243         if (openOk || (mode == QIODevice::WriteOnly))
00244         {
00245                 m_DataStream.setDevice(NULL);
00246                 delete m_pFile;
00247                 m_pFile= new QFile(m_FileInfo.filePath());
00248                 openOk= m_pFile->open(mode);
00249                 if (openOk)
00250                 {
00251                         m_DataStream.setDevice(m_pFile);
00252                 }
00253         }
00254         else
00255         {
00256                 qDebug() << "File info " << m_FileInfo.filePath() << " do not exists";
00257         }
00258         return openOk;
00259 }
00260 
00261 // Close the file
00262 bool GLC_BSRep::close()
00263 {
00264         Q_ASSERT(m_pFile != NULL);
00265         Q_ASSERT(m_DataStream.device() != NULL);
00266         bool closeOk= m_DataStream.status() == QDataStream::Ok;
00267         m_DataStream.setDevice(NULL);
00268         m_pFile->close();
00269         delete m_pFile;
00270         m_pFile= NULL;
00271 
00272         return closeOk;
00273 }
00274 
00275 // Write the header
00276 void GLC_BSRep::writeHeader(const QDateTime& dateTime)
00277 {
00278         Q_ASSERT(m_pFile != NULL);
00279         Q_ASSERT(m_DataStream.device() != NULL);
00280         Q_ASSERT(m_pFile->openMode() == QIODevice::WriteOnly);
00281 
00282         // Binary representation Header
00283         // Add the magic number
00284         m_DataStream << m_Uuid;
00285         // Add the version
00286         m_DataStream << m_Version;
00287 
00288         // Set the version of the data stream
00289         m_DataStream.setVersion(QDataStream::Qt_4_6);
00290 
00291         // Add the time stamp
00292         m_DataStream << dateTime;
00293 }
00294 
00295 // Check the header
00296 bool GLC_BSRep::headerIsOk()
00297 {
00298         Q_ASSERT(m_pFile != NULL);
00299         Q_ASSERT(m_DataStream.device() != NULL);
00300         Q_ASSERT(m_pFile->openMode() == QIODevice::ReadOnly);
00301 
00302         QUuid uuid;
00303         quint32 version;
00304         m_DataStream >> uuid;
00305         m_DataStream >> version;
00306 
00307         // Set the version of the data stream
00308         m_DataStream.setVersion(QDataStream::Qt_4_6);
00309 
00310         bool headerOk= (uuid == m_Uuid);
00311         headerOk= headerOk && (version == m_Version);
00312 
00313         return headerOk;
00314 }
00315 
00316 // Check the time Stamp
00317 bool GLC_BSRep::timeStampOk(const QDateTime& timeStamp)
00318 {
00319         Q_ASSERT(m_pFile != NULL);
00320         Q_ASSERT(m_DataStream.device() != NULL);
00321         Q_ASSERT(m_pFile->openMode() == QIODevice::ReadOnly);
00322 
00323         QDateTime dateTime;
00324         m_DataStream >> dateTime;
00325 
00326         bool timeStampOk= !timeStamp.isValid() || (dateTime == timeStamp);
00327         return timeStampOk;
00328 }
00329 

SourceForge.net Logo

©2005 Laurent Ribon