Skip to content

Commit b4a5b91

Browse files
authored
fromBinaryStl: support systems where floats must be 4-bytes aligned (#5433)
1 parent 1a73b16 commit b4a5b91

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

source/MRMesh/MRMeshLoad.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "MRPch/MRTBB.h"
2121

2222
#include <array>
23+
#include <bit>
2324
#include <future>
2425
#include <sstream>
2526

@@ -405,15 +406,17 @@ Expected<Mesh> fromBinaryStl( std::istream& in, const MeshLoadSettings& settings
405406
MeshBuilder::VertexIdentifier vi;
406407
vi.reserve( numTris );
407408

408-
#pragma pack(push, 1)
409+
using Pos3f = std::array<char, 12>;
409410
struct StlTriangle
410411
{
411-
Vector3f normal;
412-
Vector3f vert[3];
413-
std::uint16_t attr;
412+
Pos3f normal;
413+
Pos3f coords[3];
414+
char attrs[2];
415+
// floats in Vector3f must be 4-bytes aligned on some platforms, so we use chars and cast them in Vector3f on access
416+
Vector3f vertex( int i ) const { return std::bit_cast<Vector3f>( coords[i] ); }
414417
};
415-
#pragma pack(pop)
416-
static_assert( sizeof( StlTriangle ) == 50, "check your padding" );
418+
static_assert( sizeof( StlTriangle ) == 50 );
419+
static_assert( alignof( StlTriangle ) <= 2 );
417420

418421
const auto itemsInBuffer = std::min( numTris, 32768u );
419422
std::vector<StlTriangle> buffer( itemsInBuffer ), nextBuffer( itemsInBuffer );
@@ -437,7 +440,7 @@ Expected<Mesh> fromBinaryStl( std::istream& in, const MeshLoadSettings& settings
437440
chunk.resize( buffer.size() );
438441
for ( int i = 0; i < buffer.size(); ++i )
439442
for ( int j = 0; j < 3; ++j )
440-
chunk[i][j] = buffer[i].vert[j];
443+
chunk[i][j] = buffer[i].vertex( j );
441444
vi.addTriangles( chunk );
442445
} );
443446

0 commit comments

Comments
 (0)