Skip to content

Commit

Permalink
Merge pull request #42 from phmkopp/addConst
Browse files Browse the repository at this point in the history
Make dummy mesh use const& and fix types in zlib writer
  • Loading branch information
phmkopp authored Dec 9, 2021
2 parents 5d51a80 + ceb11d7 commit 4e6bebe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

_Vtu11_ is a small C++ header-only library to write unstructured grids using the vtu file format. It keeps the mess of dealing with file writing in different formats away from you. Currently it does not add any features for setting up the required data structure because this vastly differs based on the context in which _vtu11_ is used.

You can download a single header version in the latest release.

## Serial example

```cpp
Expand Down
30 changes: 19 additions & 11 deletions vtu11/impl/zlibWriter_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,36 @@ namespace detail
template<typename T>
std::vector<HeaderType> zlibCompressData( const std::vector<T>& data,
std::vector<std::vector<Byte>>& targetBlocks,
size_t blocksize = 32768 ) // 2^15
size_t blockSize = 32768 ) // 2^15
{
using IntType = uLong;

if( data.size( ) > std::numeric_limits<IntType>::max( ) ||
blockSize > std::numeric_limits<IntType>::max( ) )
{
throw std::runtime_error( "Size too large for uLong zlib type." );
}

std::vector<HeaderType> header( 3, 0 );

if( data.empty( ) )
{
return header;
}

auto blocksize = static_cast<IntType>( blockSize );

auto compressedBuffersize = compressBound( blocksize );

Byte* buffer = new Byte[compressedBuffersize];
Byte* currentByte = const_cast<Byte*>( reinterpret_cast<const Byte*>( &data[0] ) );

IntType numberOfBytes = static_cast<IntType>( data.size( ) ) * sizeof( T );
IntType numberOfBlocks = ( numberOfBytes - 1 ) / blocksize + 1;

size_t numberOfBytes = data.size( ) * sizeof( T );
size_t numberOfBlocks = ( numberOfBytes - 1 ) / blocksize + 1;

using ZlibSizeType = decltype( compressedBuffersize );

auto compressBlock = [&]( ZlibSizeType numberOfBytesInBlock )
auto compressBlock = [&]( IntType numberOfBytesInBlock )
{
ZlibSizeType compressedLength = compressedBuffersize;
IntType compressedLength = compressedBuffersize;

int errorCode = compress( buffer, &compressedLength, currentByte, numberOfBytesInBlock );

Expand All @@ -58,12 +66,12 @@ std::vector<HeaderType> zlibCompressData( const std::vector<T>& data,
currentByte += numberOfBytesInBlock;
};

for( size_t iBlock = 0; iBlock < numberOfBlocks - 1; ++iBlock )
for( IntType iBlock = 0; iBlock < numberOfBlocks - 1; ++iBlock )
{
compressBlock( blocksize );
compressBlock( static_cast<IntType>( blocksize ) );
}

size_t remainder = numberOfBytes - ( numberOfBlocks - 1 ) * blocksize;
IntType remainder = numberOfBytes - ( numberOfBlocks - 1 ) * blocksize;

compressBlock( remainder );

Expand Down
16 changes: 8 additions & 8 deletions vtu11/vtu11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ namespace vtu11

struct Vtu11UnstructuredMesh
{
std::vector<double>& points_;
std::vector<VtkIndexType>& connectivity_;
std::vector<VtkIndexType>& offsets_;
std::vector<VtkCellType>& types_;
const std::vector<double>& points_;
const std::vector<VtkIndexType>& connectivity_;
const std::vector<VtkIndexType>& offsets_;
const std::vector<VtkCellType>& types_;

std::vector<double>& points( ){ return points_; }
std::vector<VtkIndexType>& connectivity( ){ return connectivity_; }
std::vector<VtkIndexType>& offsets( ){ return offsets_; }
std::vector<VtkCellType>& types( ){ return types_; }
const std::vector<double>& points( ){ return points_; }
const std::vector<VtkIndexType>& connectivity( ){ return connectivity_; }
const std::vector<VtkIndexType>& offsets( ){ return offsets_; }
const std::vector<VtkCellType>& types( ){ return types_; }

size_t numberOfPoints( ){ return points_.size( ) / 3; }
size_t numberOfCells( ){ return types_.size( ); }
Expand Down

0 comments on commit 4e6bebe

Please sign in to comment.