diff --git a/src/libstl/stla.cpp b/src/libstl/stla.cpp index c736176..c695423 100644 --- a/src/libstl/stla.cpp +++ b/src/libstl/stla.cpp @@ -1,18 +1,17 @@ -#include "abstract_stream.h" #include "stla.h" +#include "../abstract_stream.h" #include namespace foug { -namespace stl { -namespace asc { +namespace stla { bool Io::read(AbstractGeometryBuilder* builder, Int64 streamSize) { return false; } -bool Io::write(const AbstractGeometry& geom, const std::string& solidName) +bool Io::write(const stl::AbstractGeometry& geom, const std::string& solidName) { return false; } @@ -25,6 +24,5 @@ void AbstractGeometryBuilder::endSolid(const std::string& /*name*/) { } -} // namespace asc -} // namespace stl +} // namespace stla } // namespace foug diff --git a/src/libstl/stla.h b/src/libstl/stla.h index 2901c7a..f5eb054 100644 --- a/src/libstl/stla.h +++ b/src/libstl/stla.h @@ -3,18 +3,17 @@ #include "stl_global.h" #include "abstract_geometry.h" -#include "io_base.h" +#include "../io_base.h" #include namespace foug { -namespace stl { -namespace asc { +namespace stla { class FOUG_STL_EXPORT AbstractGeometryBuilder { public: virtual void beginSolid(const std::string& name); - virtual void nextTriangle(const Triangle& triangle) = 0; + virtual void nextTriangle(const stl::Triangle& triangle) = 0; virtual void endSolid(const std::string& name); }; @@ -24,11 +23,10 @@ public: Io(AbstractStream* stream = 0); bool read(AbstractGeometryBuilder* builder, Int64 streamSize = -1); - bool write(const AbstractGeometry& geom, const std::string& solidName); + bool write(const stl::AbstractGeometry& geom, const std::string& solidName); }; -} // namespace asc -} // namespace stl +} // namespace stla } // namespace foug #endif // FOUG_STLA_H diff --git a/src/libstl/stlb.cpp b/src/libstl/stlb.cpp index 019a216..5e7695f 100644 --- a/src/libstl/stlb.cpp +++ b/src/libstl/stlb.cpp @@ -1,6 +1,5 @@ -#include "abstract_stream.h" #include "stlb.h" -#include "abstract_task_progress.h" +#include "../abstract_task_progress.h" #include #include @@ -56,15 +55,13 @@ void toLittleEndian(NUMERIC n, foug::UInt8* bytes) } namespace foug { -namespace stl { -namespace bin { +namespace stlb { -static const int stlHeaderSize = 80; static const int stlFacetSize = 50; static const int stlMinFileSize = 284; static const int stlTriangleDataSize = (4 * 3) * sizeof(foug::Real32) + sizeof(foug::UInt16); -void AbstractGeometryBuilder::header(const Header& /*data*/) +void AbstractGeometryBuilder::processHeader(const Header& /*data*/) { } @@ -76,32 +73,12 @@ void AbstractGeometryBuilder::endTriangles() { } -Io::ReadError streamRead(AbstractStream* stream, char* buffer, Int64 size) -{ - const Int64 result = stream->read(buffer, size); - if (result == -1) - return Io::StreamReadError; - else if (result != size) - return Io::UnexpectedSizeReadError; - return Io::NoReadError; -} - -Io::ReadError getReadError(Int64 readResult, Int64 expectedSize) -{ - if (readResult == -1) - return Io::StreamReadError; - else if (readResult != expectedSize) - return Io::UnexpectedSizeReadError; - else - return Io::NoReadError; -} - Io::Io(AbstractStream *stream) : IoBase(stream) { } -Io::ReadError Io::read(AbstractGeometryBuilder* builder) +bool Io::read(AbstractGeometryBuilder* builder) { // // Check file size // const qint64 fileSize = stream->size(); @@ -115,7 +92,6 @@ Io::ReadError Io::read(AbstractGeometryBuilder* builder) AbstractStream* istream = this->stream(); AbstractTaskProgress* progress = this->taskProgress(); - // Io::ReadError readErr = Io::NoReadError; const UInt32 chunkSize = stlTriangleDataSize * 163; UInt8 buffer[8192]; @@ -123,15 +99,13 @@ Io::ReadError Io::read(AbstractGeometryBuilder* builder) // Read header Header headerData; - istream->read(reinterpret_cast(&headerData), 80); - // if ((readErr = streamRead(istream, reinterpret_cast(&headerData), 80)) != NoReadError) - // return readErr; - builder->header(headerData); + if (istream->read(reinterpret_cast(&headerData), HeaderSize) != HeaderSize) + return false; + builder->processHeader(headerData); // Read facet count - istream->read(charBuffer, sizeof(UInt32)); - // if ((readErr = streamRead(istream, charBuffer, sizeof(UInt32))) != NoReadError) - // return readErr; + if (istream->read(charBuffer, sizeof(UInt32)) != sizeof(UInt32)) + return false; const UInt32 facetCount = ::fromLittleEndian(buffer); builder->beginTriangles(facetCount); @@ -143,7 +117,7 @@ Io::ReadError Io::read(AbstractGeometryBuilder* builder) // Read triangles const UInt64 totalFacetSize = stlTriangleDataSize * facetCount; UInt64 amountReadSize = 0; - Triangle triangle; + stl::Triangle triangle; bool streamError = false; while (amountReadSize < totalFacetSize && !streamError) { const Int64 iReadSize = istream->read(charBuffer, chunkSize); @@ -176,7 +150,7 @@ Io::ReadError Io::read(AbstractGeometryBuilder* builder) ::fromLittleEndian(buffer + 12*sizeof(Real32) + bufferOffset); // Add triangle - builder->nextTriangle(triangle, attributeByteCount); + builder->processNextTriangle(triangle, attributeByteCount); bufferOffset += stlTriangleDataSize; } @@ -193,10 +167,10 @@ Io::ReadError Io::read(AbstractGeometryBuilder* builder) builder->endTriangles(); - return NoReadError; + return !streamError; } -Io::WriteError Io::write(const AbstractGeometry& geom, const AbstractGeometryExtraData* extraData) +bool Io::write(const stl::AbstractGeometry& geom, const AbstractGeometryExtraData* extraData) { AbstractStream* ostream = this->stream(); AbstractTaskProgress* progress = this->taskProgress(); @@ -206,16 +180,18 @@ Io::WriteError Io::write(const AbstractGeometry& geom, const AbstractGeometryExt // Write header Header headerData; if (extraData != 0) - extraData->getHeaderData(headerData); + extraData->getHeader(headerData); else - std::fill(headerData, headerData + 80, 0); + std::fill(headerData, headerData + HeaderSize, 0); - ostream->write(reinterpret_cast(&headerData), 80); + if (ostream->write(reinterpret_cast(&headerData), HeaderSize) != HeaderSize) + return false; // Write facet count const UInt32 facetCount = geom.triangleCount(); ::toLittleEndian(facetCount, buffer); - ostream->write(reinterpret_cast(&buffer), sizeof(UInt32)); + if (ostream->write(reinterpret_cast(&buffer), sizeof(UInt32)) != sizeof(UInt32)) + return false; if (progress != 0) { progress->reset(); @@ -223,7 +199,7 @@ Io::WriteError Io::write(const AbstractGeometry& geom, const AbstractGeometryExt } // Write triangles - Triangle triangle; + stl::Triangle triangle; for (UInt32 facet = 0; facet < facetCount; ++facet) { geom.getTriangle(facet, &triangle); @@ -252,15 +228,16 @@ Io::WriteError Io::write(const AbstractGeometry& geom, const AbstractGeometryExt ::toLittleEndian(attrByteCount, buffer + 12*sizeof(Real32)); // Write to stream - ostream->write(reinterpret_cast(buffer), stlTriangleDataSize); + if (ostream->write(reinterpret_cast(buffer), stlTriangleDataSize) + != stlTriangleDataSize) + return false; if (progress != 0) progress->setValue(facet + 1); } - return NoWriteError; + return true; } -} // namespace bin -} // namespace stl +} // namespace stlb } // namespace foug diff --git a/src/libstl/stlb.h b/src/libstl/stlb.h index b2bcaab..eca87fb 100644 --- a/src/libstl/stlb.h +++ b/src/libstl/stlb.h @@ -1,30 +1,30 @@ #ifndef FOUG_STLB_H #define FOUG_STLB_H -#include "stl_global.h" #include "abstract_geometry.h" -#include "io_base.h" -#include +#include "stl_global.h" +#include "../abstract_stream.h" +#include "../io_base.h" namespace foug { -namespace stl { -namespace bin { +namespace stlb { -typedef UInt8 Header[80]; +enum { HeaderSize = 80 }; +typedef UInt8 Header[HeaderSize]; class FOUG_STL_EXPORT AbstractGeometryBuilder { public: - virtual void header(const Header& data); + virtual void processHeader(const Header& data); virtual void beginTriangles(UInt32 count); - virtual void nextTriangle(const stl::Triangle& triangle, UInt16 attributeByteCount) = 0; + virtual void processNextTriangle(const stl::Triangle& triangle, UInt16 attributeByteCount) = 0; virtual void endTriangles(); }; class FOUG_STL_EXPORT AbstractGeometryExtraData { public: - virtual void getHeaderData(Header& data) const = 0; + virtual void getHeader(Header& data) const = 0; virtual UInt16 attributeByteCount(UInt32 triangleIndex) const = 0; }; @@ -33,23 +33,11 @@ class FOUG_STL_EXPORT Io : public IoBase public: Io(AbstractStream* stream = 0); - enum ReadError - { - NoReadError, - StreamReadError, - UnexpectedSizeReadError - }; - ReadError read(AbstractGeometryBuilder* builder); - - enum WriteError - { - NoWriteError - }; - WriteError write(const AbstractGeometry& geom, const AbstractGeometryExtraData* extraData = 0); + bool read(AbstractGeometryBuilder* builder); + bool write(const stl::AbstractGeometry& geom, const AbstractGeometryExtraData* extraData = 0); }; -} // namespace bin -} // namespace stl +} // namespace stlb } // namespace foug #endif // FOUG_STLB_H diff --git a/src/streams/qt4_stream.h b/src/streams/qt4_stream.h index 8b1412b..e0c9b2c 100644 --- a/src/streams/qt4_stream.h +++ b/src/streams/qt4_stream.h @@ -1,7 +1,7 @@ #ifndef FOUG_QT4_STREAM_H #define FOUG_QT4_STREAM_H -#include "abstract_stream.h" +#include "../abstract_stream.h" class QIODevice; namespace foug { diff --git a/src/streams/std_io_stream.h b/src/streams/std_io_stream.h index 7d3fd55..90961dd 100644 --- a/src/streams/std_io_stream.h +++ b/src/streams/std_io_stream.h @@ -1,7 +1,7 @@ #ifndef FOUG_STD_IO_STREAM_H #define FOUG_STD_IO_STREAM_H -#include "abstract_stream.h" +#include "../abstract_stream.h" namespace foug { diff --git a/tests/cmd/main.cpp b/tests/cmd/main.cpp index 7386bf8..7efc88b 100644 --- a/tests/cmd/main.cpp +++ b/tests/cmd/main.cpp @@ -33,7 +33,7 @@ public: class DummyGeometryBuilder : public foug::stl::bin::AbstractGeometryBuilder { public: - void header(const foug::stl::bin::Header& data) + void processHeader(const foug::stl::bin::Header& data) { qDebug() << "HEADER : \n" << QString::fromAscii(reinterpret_cast(data), 79); } @@ -44,7 +44,7 @@ public: qDebug() << "Triangle count" << count; } - void nextTriangle(const foug::stl::Triangle& triangle, foug::UInt16 attributeByteCount) + void processNextTriangle(const foug::stl::Triangle& triangle, foug::UInt16 attributeByteCount) { // qDebug() << "TRIANGLE " << m_triangleCounter++; // qDebug() << " normal" << triangle.normal.x << triangle.normal.y << triangle.normal.z; @@ -77,7 +77,7 @@ public: // -- foug::stl::bin::AbstractGeometryBuilder - void header(const foug::stl::bin::Header& data) + void processHeader(const foug::stlb::Header& data) { std::memcpy(m_header, data, 80); } @@ -89,7 +89,7 @@ public: m_triangles.resize(0); } - void nextTriangle(const foug::stl::Triangle& triangle, foug::UInt16 attributeByteCount) + void processNextTriangle(const foug::stl::Triangle& triangle, foug::UInt16 attributeByteCount) { TriangleData data; data.triangle = triangle; @@ -106,7 +106,7 @@ public: foug::UInt16 attributeByteCount(foug::UInt32 triangleIndex) const { - return m_triangles[triangleIndex].attributeByteCount; + return m_triangles.at(triangleIndex).attributeByteCount; } // -- foug::stl::AbstractGeometry @@ -118,7 +118,7 @@ public: void getTriangle(foug::UInt32 index, foug::stl::Triangle* triangle) const { - *triangle = m_triangles[index].triangle; + *triangle = m_triangles.at(index).triangle; } private: