diff --git a/bindings/python/opengeode_io_doc.txt b/bindings/python/opengeode_io_doc.txt index c5686e4..cc1f6d9 100644 --- a/bindings/python/opengeode_io_doc.txt +++ b/bindings/python/opengeode_io_doc.txt @@ -35,6 +35,14 @@ Formats (extensions) by type - Time series: a PVD file is loaded onto a BRep with opengeode.load_brep_time_series(brep, "file.pvd"). +A dxf file (ASCII) gives one PolygonalSurface3D per polyface mesh POLYLINE, +named after its XDATA "Name" or "Nom de Galerie" (else _), and +one per layer for its 3DFACE entities, named after the layer: read it with +opengeode.load_all. The other entities (lines, points, blocks...) are skipped. +An obj file gives one surface per object (g or o), named after it (unnamed +without g or o), with the textures of its materials; an stl file one per solid; +a ply file a single surface. Read a multi-object file with opengeode.load_all. + Use opengeode.input_extensions(cls) and opengeode.output_extensions(cls) for the exact list of a type. diff --git a/bindings/python/tests/mesh_io/test-py-mesh-io-generic-api.py b/bindings/python/tests/mesh_io/test-py-mesh-io-generic-api.py index b6f6c80..b5af79a 100644 --- a/bindings/python/tests/mesh_io/test-py-mesh-io-generic-api.py +++ b/bindings/python/tests/mesh_io/test-py-mesh-io-generic-api.py @@ -151,6 +151,25 @@ def test_load_all(): opengeode.TetrahedralSolid3D, os.path.join(DATA, "two_pieces.vtu") ) check([solid.nb_vertices() for solid in solids] == [3, 4], "Wrong pieces of vtu") + # One surface per polyface mesh of a DXF file + surfaces = opengeode.load_all( + opengeode.PolygonalSurface3D, os.path.join(DATA, "polyface_surfaces.dxf") + ) + check(len(surfaces) == 3, "Wrong number of DXF polyface surfaces") + check( + [surface.name() for surface in surfaces] + == ["Galerie A", "Baie décantation 1", "OTHER_3C"], + "Wrong names of DXF polyface surfaces", + ) + check( + [surface.nb_polygons() for surface in surfaces] == [3, 2, 1], + "Wrong polygons of DXF polyface surfaces", + ) + expect_data_error( + opengeode.load, + opengeode.PolygonalSurface3D, + os.path.join(DATA, "polyface_surfaces.dxf"), + ) # load needs exactly one object expect_data_error( opengeode.load, opengeode.RegularGrid3D, os.path.join(DATA, "two_pieces.vti") diff --git a/include/geode/io/mesh/internal/assimp_input.hpp b/include/geode/io/mesh/internal/assimp_input.hpp index c52828a..31b9a22 100644 --- a/include/geode/io/mesh/internal/assimp_input.hpp +++ b/include/geode/io/mesh/internal/assimp_input.hpp @@ -27,6 +27,8 @@ #include +#include + #include #include @@ -59,7 +61,9 @@ namespace geode void read_meshes( const aiScene* assimp_scene ); - std::unique_ptr< Mesh > merge_meshes(); + std::unique_ptr< Mesh > merge_meshes( + absl::Span< const index_t > mesh_ids, + std::string_view surface_name ); private: std::vector< std::unique_ptr< Mesh > > surfaces_; diff --git a/src/geode/io/mesh/assimp_input.cpp b/src/geode/io/mesh/assimp_input.cpp index 864507e..6eaf4d9 100644 --- a/src/geode/io/mesh/assimp_input.cpp +++ b/src/geode/io/mesh/assimp_input.cpp @@ -32,6 +32,7 @@ #include #include +#include #include #include @@ -51,14 +52,123 @@ namespace { + // Corners (positions in the face) kept to build the polygon: consecutive + // corners on the same vertex or on equal points (Assimp duplicates the + // vertices per corner) are merged, including the last and first ones. + // Empty if the face is not a supported polygon. template < typename Mesh > - bool is_face_supported( const aiFace& face ) + absl::InlinedVector< geode::index_t, 4 > face_corners( + const aiMesh& assimp_mesh, const aiFace& face ) { + absl::InlinedVector< geode::index_t, 4 > corners; + if( face.mNumIndices >= geode::NO_LID ) + { + return corners; + } + const auto same_corners = [&assimp_mesh, &face]( + geode::index_t c0, geode::index_t c1 ) { + const auto v0 = face.mIndices[c0]; + const auto v1 = face.mIndices[c1]; + return v0 == v1 + || assimp_mesh.mVertices[v0] == assimp_mesh.mVertices[v1]; + }; + for( const auto c : geode::Range{ face.mNumIndices } ) + { + if( corners.empty() || !same_corners( corners.back(), c ) ) + { + corners.push_back( c ); + } + } + while( corners.size() > 1 + && same_corners( corners.back(), corners.front() ) ) + { + corners.pop_back(); + } if constexpr( std::is_same_v< Mesh, geode::TriangulatedSurface3D > ) { - return face.mNumIndices == 3; + if( corners.size() != 3 ) + { + corners.clear(); + } + } + else if( corners.size() < 3 ) + { + corners.clear(); + } + return corners; + } + + struct MeshGroup + { + std::string name; + std::vector< geode::index_t > meshes; + }; + + // Assimp names "defaultobject" the OBJ object without g/o statement + std::string group_name( std::string name ) + { + static constexpr std::string_view ASSIMP_DEFAULT_OBJECT_NAME{ + "defaultobject" + }; + if( name == ASSIMP_DEFAULT_OBJECT_NAME ) + { + return {}; + } + return name; + } + + // One group per scene node referencing meshes (e.g. OBJ object, STL + // solid), in node order. Meshes referenced by no node are their own group. + std::vector< MeshGroup > mesh_groups( const aiScene& assimp_scene ) + { + std::vector< MeshGroup > groups; + std::vector< bool > used( assimp_scene.mNumMeshes, false ); + std::vector< const aiNode* > nodes; + if( assimp_scene.mRootNode ) + { + nodes.push_back( assimp_scene.mRootNode ); + } + while( !nodes.empty() ) + { + const auto& node = *nodes.back(); + nodes.pop_back(); + MeshGroup group; + for( const auto m : geode::Range{ node.mNumMeshes } ) + { + const auto mesh_id = node.mMeshes[m]; + if( mesh_id < assimp_scene.mNumMeshes && !used[mesh_id] ) + { + used[mesh_id] = true; + group.meshes.push_back( mesh_id ); + } + } + if( !group.meshes.empty() ) + { + group.name = node.mName.C_Str(); + if( group.name.empty() ) + { + group.name = assimp_scene.mMeshes[group.meshes.front()] + ->mName.C_Str(); + } + group.name = group_name( std::move( group.name ) ); + groups.emplace_back( std::move( group ) ); + } + for( auto c = node.mNumChildren; c > 0; c-- ) + { + nodes.push_back( node.mChildren[c - 1] ); + } + } + for( const auto mesh_id : geode::Range{ assimp_scene.mNumMeshes } ) + { + if( !used[mesh_id] ) + { + groups.push_back( + { group_name( + assimp_scene.mMeshes[mesh_id]->mName.C_Str() ), + { mesh_id } } ); + } } - return face.mNumIndices >= 3 && face.mNumIndices < geode::NO_LID; + return groups; } template < typename Mesh > @@ -75,14 +185,15 @@ namespace for( const auto p : geode::Range{ assimp_mesh.mNumFaces } ) { const auto& face = assimp_mesh.mFaces[p]; - if( !is_face_supported< Mesh >( face ) ) + const auto corners = face_corners< Mesh >( assimp_mesh, face ); + if( corners.empty() ) { nb_skipped_faces++; continue; } - for( const auto i : geode::Range{ face.mNumIndices } ) + for( const auto corner : corners ) { - auto& new_vertex = new_vertices[face.mIndices[i]]; + auto& new_vertex = new_vertices[face.mIndices[corner]]; if( new_vertex == geode::NO_ID ) { new_vertex = nb_vertices++; @@ -114,15 +225,16 @@ namespace for( const auto p : geode::Range{ assimp_mesh.mNumFaces } ) { const auto& face = assimp_mesh.mFaces[p]; - if( !is_face_supported< Mesh >( face ) ) + const auto corners = face_corners< Mesh >( assimp_mesh, face ); + if( corners.empty() ) { continue; } absl::FixedArray< geode::index_t > polygon_vertices( - face.mNumIndices ); + corners.size() ); for( const auto i : geode::LIndices{ polygon_vertices } ) { - polygon_vertices[i] = new_vertices[face.mIndices[i]]; + polygon_vertices[i] = new_vertices[face.mIndices[corners[i]]]; } const auto polygon = builder->create_polygon( polygon_vertices ); if( !texture ) @@ -132,7 +244,7 @@ namespace for( const auto i : geode::LIndices{ polygon_vertices } ) { const auto& coord = - assimp_mesh.mTextureCoords[0][face.mIndices[i]]; + assimp_mesh.mTextureCoords[0][face.mIndices[corners[i]]]; texture->set_texture_coordinates( { polygon, i }, geode::Point2D{ { coord.x, coord.y } } ); } @@ -172,9 +284,16 @@ namespace geode importer.GetErrorString() ); read_materials( assimp_scene ); read_meshes( assimp_scene ); + const auto groups = mesh_groups( *assimp_scene ); importer.FreeScene(); std::vector< std::unique_ptr< Mesh > > meshes; - meshes.emplace_back( merge_meshes() ); + for( const auto& group : groups ) + { + if( auto mesh = merge_meshes( group.meshes, group.name ) ) + { + meshes.emplace_back( std::move( mesh ) ); + } + } return meshes; } @@ -211,33 +330,49 @@ namespace geode Logger::warning( "[AssimpMeshInput::read_meshes] Skipped ", total_skipped_faces, " unsupported faces (", std::is_same_v< Mesh, TriangulatedSurface3D > - ? "non-triangle faces" - : "lines, points, empty faces or polygons with too " - "many vertices", + ? "non-triangle or degenerate faces" + : "lines, points, empty or degenerate faces or " + "polygons with too many vertices", ") in file ", file_ ); } } template < typename Mesh > - std::unique_ptr< Mesh > AssimpMeshInput< Mesh >::merge_meshes() + std::unique_ptr< Mesh > AssimpMeshInput< Mesh >::merge_meshes( + absl::Span< const index_t > mesh_ids, + std::string_view surface_name ) { + std::vector< index_t > group_surfaces; std::vector< std::reference_wrapper< const SurfaceMesh3D > > ref_surfaces; - ref_surfaces.reserve( surfaces_.size() ); - for( const auto& surface : surfaces_ ) + for( const auto mesh_id : mesh_ids ) { - ref_surfaces.emplace_back( *surface ); + if( surfaces_[mesh_id]->nb_polygons() == 0 ) + { + continue; + } + group_surfaces.push_back( mesh_id ); + ref_surfaces.emplace_back( *surfaces_[mesh_id] ); + } + if( ref_surfaces.empty() ) + { + return nullptr; } detail::SurfaceMeshMerger3D merger{ ref_surfaces }; std::unique_ptr< Mesh > merged{ dynamic_cast< Mesh* >( merger.merge( GLOBAL_EPSILON ).release() ) }; - Mesh::Builder::create( *merged )->compute_polygon_adjacencies(); + auto builder = Mesh::Builder::create( *merged ); + builder->compute_polygon_adjacencies(); + if( !surface_name.empty() ) + { + builder->set_name( surface_name ); + } auto merged_manager = merged->texture_manager(); - for( const auto s : Indices{ surfaces_ } ) + for( const auto s : Indices{ group_surfaces } ) { - const auto& mesh = surfaces_[s]; + const auto& mesh = surfaces_[group_surfaces[s]]; auto manager = mesh->texture_manager(); for( const auto& name : manager.texture_names() ) { diff --git a/src/geode/io/mesh/dxf_input.cpp b/src/geode/io/mesh/dxf_input.cpp index 47030d3..2990a58 100644 --- a/src/geode/io/mesh/dxf_input.cpp +++ b/src/geode/io/mesh/dxf_input.cpp @@ -23,29 +23,706 @@ #include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + #include #include #include #include -#include +#include + +namespace +{ + constexpr std::string_view BINARY_SENTINEL{ "AutoCAD Binary DXF" }; + constexpr std::string_view UTF8_BOM{ "\xEF\xBB\xBF" }; + constexpr int POLYFACE_MESH_FLAG{ 64 }; + constexpr int POLYFACE_VERTEX_FLAG{ 64 }; + constexpr int FACE_RECORD_FLAG{ 128 }; + constexpr int FIRST_FACE_INDEX_CODE{ 71 }; + constexpr int NB_FACE_INDICES{ 4 }; + constexpr int NB_3DFACE_CORNERS{ 4 }; + constexpr int XDATA_APPLICATION_CODE{ 1001 }; + constexpr int XDATA_STRING_CODE{ 1000 }; + constexpr int XDATA_REAL_CODE{ 1040 }; + constexpr int XDATA_INTEGER_CODE{ 1070 }; + constexpr std::array< std::string_view, 2 > NAME_APPLICATIONS{ "Name", + "Nom de Galerie" }; + + struct GroupPair + { + int code{ 0 }; + std::string value; + }; + + struct Entity + { + std::string type; + std::vector< GroupPair > groups; + }; + + struct SurfaceData + { + std::string name; + std::vector< geode::Point3D > points; + std::vector< std::vector< geode::index_t > > polygons; + bool colocate{ false }; + }; + + struct SkippedFaces + { + geode::index_t degenerate{ 0 }; + geode::index_t out_of_range{ 0 }; + geode::index_t repeated_vertex{ 0 }; + + [[nodiscard]] geode::index_t total() const + { + return degenerate + out_of_range + repeated_vertex; + } + }; + + enum struct PolygonStatus + { + valid, + degenerate, + repeated_vertex + }; + + /*! + * Removes consecutive duplicates, including the wrap-around + * (a,b,c,a -> a,b,c), and tells if the polygon is usable. + */ + template < typename Value > + PolygonStatus clean_polygon( std::vector< Value >& vertices ) + { + std::vector< Value > cleaned; + cleaned.reserve( vertices.size() ); + for( const auto& vertex : vertices ) + { + if( cleaned.empty() || !( cleaned.back() == vertex ) ) + { + cleaned.push_back( vertex ); + } + } + while( cleaned.size() > 1 && cleaned.back() == cleaned.front() ) + { + cleaned.pop_back(); + } + vertices = std::move( cleaned ); + if( vertices.size() < 3 ) + { + return PolygonStatus::degenerate; + } + for( const auto i : geode::Indices{ vertices } ) + { + for( const auto j : geode::Range{ i + 1, vertices.size() } ) + { + if( vertices[i] == vertices[j] ) + { + return PolygonStatus::repeated_vertex; + } + } + } + return PolygonStatus::valid; + } + + void count_skipped( PolygonStatus status, SkippedFaces& skipped ) + { + if( status == PolygonStatus::degenerate ) + { + skipped.degenerate++; + } + else if( status == PolygonStatus::repeated_vertex ) + { + skipped.repeated_vertex++; + } + } + + void append_utf8( std::string& output, unsigned int code_point ) + { + if( code_point < 0x80 ) + { + output.push_back( static_cast< char >( code_point ) ); + } + else if( code_point < 0x800 ) + { + output.push_back( + static_cast< char >( 0xC0 | ( code_point >> 6 ) ) ); + output.push_back( + static_cast< char >( 0x80 | ( code_point & 0x3F ) ) ); + } + else + { + output.push_back( + static_cast< char >( 0xE0 | ( code_point >> 12 ) ) ); + output.push_back( + static_cast< char >( 0x80 | ( ( code_point >> 6 ) & 0x3F ) ) ); + output.push_back( + static_cast< char >( 0x80 | ( code_point & 0x3F ) ) ); + } + } + + /*! + * Decodes the \U+XXXX escapes of DXF strings to UTF-8. + */ + std::string decode_unicode_escapes( std::string_view value ) + { + static constexpr std::string_view ESCAPE{ "\\U+" }; + static constexpr size_t NB_HEX_DIGITS{ 4 }; + std::string result; + result.reserve( value.size() ); + size_t position{ 0 }; + while( position < value.size() ) + { + if( value.substr( position, ESCAPE.size() ) == ESCAPE + && position + ESCAPE.size() + NB_HEX_DIGITS <= value.size() ) + { + const auto digits = + value.substr( position + ESCAPE.size(), NB_HEX_DIGITS ); + unsigned int code_point{ 0 }; + if( absl::SimpleHexAtoi( digits, &code_point ) ) + { + append_utf8( result, code_point ); + position += ESCAPE.size() + NB_HEX_DIGITS; + continue; + } + } + result.push_back( value[position] ); + position++; + } + return result; + } + + class DXFReader + { + public: + explicit DXFReader( std::string_view filename ) + : filename_{ filename }, + file_{ geode::to_string( filename ), std::ios::binary } + { + geode::OpenGeodeIOMeshException::check_exception( file_.good(), + nullptr, geode::OpenGeodeException::TYPE::data, + "[DXFInput] Cannot open file ", filename_ ); + } + + std::vector< SurfaceData > read() + { + // The content after the ENTITIES section is not needed + while( const auto pair = next_pair() ) + { + if( pair->code == 0 && pair->value == "EOF" ) + { + break; + } + if( pair->code != 0 || pair->value != "SECTION" ) + { + continue; + } + const auto section = next_pair(); + if( section && section->code == 2 + && section->value == "ENTITIES" ) + { + read_entities(); + break; + } + } + std::vector< SurfaceData > result; + for( auto& surface : surfaces_ ) + { + if( !surface.polygons.empty() ) + { + result.push_back( std::move( surface ) ); + } + } + return result; + } + + void log_warnings() const + { + if( skipped_faces_.total() > 0 ) + { + geode::Logger::warning( "[DXFInput] ", skipped_faces_.total(), + " faces skipped in ", filename_, ": ", + skipped_faces_.degenerate, + " with less than 3 distinct vertices, ", + skipped_faces_.out_of_range, + " with out of range vertex indices, ", + skipped_faces_.repeated_vertex, " with a repeated vertex" ); + } + if( !skipped_entities_.empty() ) + { + std::vector< std::string > entities; + entities.reserve( skipped_entities_.size() ); + for( const auto& [type, count] : skipped_entities_ ) + { + entities.push_back( + absl::StrCat( type, " (", count, ")" ) ); + } + std::sort( entities.begin(), entities.end() ); + geode::Logger::warning( + "[DXFInput] Unsupported entities skipped " + "in ", + filename_, ": ", absl::StrJoin( entities, ", " ) ); + } + } + + private: + std::optional< GroupPair > next_pair() + { + if( pending_ ) + { + auto pair = std::move( pending_ ); + pending_.reset(); + return pair; + } + std::string code_line; + if( !std::getline( file_, code_line ) ) + { + return std::nullopt; + } + line_number_++; + auto code_text = absl::StripAsciiWhitespace( code_line ); + if( line_number_ == 1 ) + { + absl::ConsumePrefix( &code_text, UTF8_BOM ); + } + check_line( line_number_ != 1 + || !absl::StartsWith( code_text, BINARY_SENTINEL ), + "Binary DXF files are not supported" ); + GroupPair pair; + check_line( absl::SimpleAtoi( code_text, &pair.code ), + "invalid group code \"", code_text, "\"" ); + std::string value_line; + check_line( + static_cast< bool >( std::getline( file_, value_line ) ), + "missing value of group code ", pair.code ); + line_number_++; + pair.value = + std::string{ absl::StripAsciiWhitespace( value_line ) }; + return pair; + } + + std::optional< Entity > next_entity() + { + if( pending_entity_ ) + { + auto entity = std::move( pending_entity_ ); + pending_entity_.reset(); + return entity; + } + auto start = next_pair(); + // Pairs before the first entity (e.g. 999 comments) are skipped + while( start && start->code != 0 ) + { + start = next_pair(); + } + if( !start ) + { + return std::nullopt; + } + Entity entity; + entity.type = std::move( start->value ); + if( entity.type == "EOF" || entity.type == "ENDSEC" ) + { + // Nothing is read after the end of the entities + return entity; + } + while( auto pair = next_pair() ) + { + if( pair->code == 0 ) + { + pending_ = std::move( pair ); + break; + } + entity.groups.push_back( std::move( pair.value() ) ); + } + return entity; + } + + void read_entities() + { + while( auto entity = next_entity() ) + { + if( entity->type == "ENDSEC" || entity->type == "EOF" ) + { + return; + } + if( entity->type == "3DFACE" ) + { + read_3dface( *entity ); + } + else if( entity->type == "POLYLINE" ) + { + read_polyline( *entity ); + } + else if( entity->type != "VIEWPORT" ) + { + // AutoCAD writes VIEWPORT entities routinely: skipped + // without warning + skipped_entities_[entity->type]++; + } + } + } + + void read_3dface( const Entity& entity ) + { + std::string layer{ "0" }; + std::array< geode::Point3D, NB_3DFACE_CORNERS > corners; + std::array< std::array< bool, 3 >, NB_3DFACE_CORNERS > given{}; + for( const auto& group : entity.groups ) + { + if( group.code == 8 ) + { + layer = decode_unicode_escapes( group.value ); + } + else if( group.code >= 10 && group.code < 40 ) + { + const auto axis = group.code / 10 - 1; + const auto corner = group.code % 10; + if( corner < NB_3DFACE_CORNERS ) + { + corners[static_cast< size_t >( corner )].set_value( + static_cast< geode::local_index_t >( axis ), + parse_double( group.value ) ); + given[static_cast< size_t >( corner )] + [static_cast< size_t >( axis )] = true; + } + } + } + const auto is_complete = []( const std::array< bool, 3 >& axes ) { + return axes[0] && axes[1] && axes[2]; + }; + const auto is_absent = []( const std::array< bool, 3 >& axes ) { + return !axes[0] && !axes[1] && !axes[2]; + }; + constexpr size_t LAST_CORNER{ NB_3DFACE_CORNERS - 1 }; + if( is_absent( given[LAST_CORNER] ) ) + { + // A triangle: the fourth corner is the third one + corners[LAST_CORNER] = corners[LAST_CORNER - 1]; + given[LAST_CORNER] = given[LAST_CORNER - 1]; + } + if( !std::all_of( given.begin(), given.end(), is_complete ) ) + { + skipped_faces_.degenerate++; + return; + } + std::vector< geode::Point3D > polygon( + corners.begin(), corners.end() ); + const auto status = clean_polygon( polygon ); + if( status != PolygonStatus::valid ) + { + count_skipped( status, skipped_faces_ ); + return; + } + auto& surface = layer_surface( layer ); + std::vector< geode::index_t > vertices; + vertices.reserve( polygon.size() ); + for( const auto& point : polygon ) + { + vertices.push_back( + static_cast< geode::index_t >( surface.points.size() ) ); + surface.points.push_back( point ); + } + surface.polygons.push_back( std::move( vertices ) ); + } + + SurfaceData& layer_surface( const std::string& layer ) + { + const auto [it, inserted] = + layer_surfaces_.try_emplace( layer, surfaces_.size() ); + if( inserted ) + { + auto& surface = surfaces_.emplace_back(); + surface.name = layer; + surface.colocate = true; + } + return surfaces_[it->second]; + } + + void read_polyline( const Entity& polyline ) + { + std::string layer{ "0" }; + std::string handle; + int flag{ 0 }; + absl::flat_hash_map< std::string, std::string > xdata; + std::optional< std::string > application; + for( const auto& group : polyline.groups ) + { + if( group.code == 8 ) + { + layer = group.value; + } + else if( group.code == 5 ) + { + handle = group.value; + } + else if( group.code == 70 ) + { + flag = parse_int( group.value ); + } + else if( group.code == XDATA_APPLICATION_CODE ) + { + application = group.value; + } + else if( application + && ( group.code == XDATA_STRING_CODE + || group.code == XDATA_REAL_CODE + || group.code == XDATA_INTEGER_CODE ) ) + { + xdata.try_emplace( application.value(), group.value ); + application.reset(); + } + } + const auto is_polyface = ( flag & POLYFACE_MESH_FLAG ) != 0; + SurfaceData surface; + std::vector< std::vector< geode::index_t > > faces; + while( auto vertex = next_entity() ) + { + if( vertex->type == "SEQEND" ) + { + break; + } + if( vertex->type != "VERTEX" ) + { + // Missing SEQEND: the entity is read by the caller + pending_entity_ = std::move( vertex ); + break; + } + if( is_polyface ) + { + read_polyface_vertex( *vertex, surface.points, faces ); + } + } + if( !is_polyface ) + { + skipped_entities_["POLYLINE (not a polyface mesh)"]++; + return; + } + surface.name = polyline_name( xdata, layer, handle ); + for( auto& face : faces ) + { + add_polyface_polygon( surface, std::move( face ) ); + } + surfaces_.push_back( std::move( surface ) ); + } + + void read_polyface_vertex( const Entity& vertex, + std::vector< geode::Point3D >& points, + std::vector< std::vector< geode::index_t > >& faces ) const + { + int flag{ 0 }; + geode::Point3D position; + std::array< int, NB_FACE_INDICES > indices{ 0, 0, 0, 0 }; + for( const auto& group : vertex.groups ) + { + if( group.code == 70 ) + { + flag = parse_int( group.value ); + } + else if( group.code == 10 || group.code == 20 + || group.code == 30 ) + { + position.set_value( static_cast< geode::local_index_t >( + group.code / 10 - 1 ), + parse_double( group.value ) ); + } + else if( group.code >= FIRST_FACE_INDEX_CODE + && group.code + < FIRST_FACE_INDEX_CODE + NB_FACE_INDICES ) + { + indices[static_cast< size_t >( + group.code - FIRST_FACE_INDEX_CODE )] = + parse_int( group.value ); + } + } + if( ( flag & POLYFACE_VERTEX_FLAG ) != 0 ) + { + points.push_back( position ); + } + else if( ( flag & FACE_RECORD_FLAG ) != 0 ) + { + auto& face = faces.emplace_back(); + for( const auto index : indices ) + { + if( index != 0 ) + { + // The sign of the index is the edge visibility. + // Widened: the absolute value of INT_MIN is defined. + face.push_back( static_cast< geode::index_t >( std::abs( + static_cast< std::int64_t >( index ) ) ) ); + } + } + } + } + + void add_polyface_polygon( + SurfaceData& surface, std::vector< geode::index_t > face ) + { + for( auto& index : face ) + { + if( index > surface.points.size() ) + { + skipped_faces_.out_of_range++; + return; + } + index--; + } + const auto status = clean_polygon( face ); + if( status != PolygonStatus::valid ) + { + count_skipped( status, skipped_faces_ ); + return; + } + surface.polygons.push_back( std::move( face ) ); + } + + static std::string polyline_name( + const absl::flat_hash_map< std::string, std::string >& xdata, + std::string_view layer, + std::string_view handle ) + { + for( const auto application : NAME_APPLICATIONS ) + { + const auto it = xdata.find( application ); + if( it != xdata.end() && !it->second.empty() ) + { + return decode_unicode_escapes( it->second ); + } + } + auto name = decode_unicode_escapes( layer ); + if( handle.empty() ) + { + return name; + } + return absl::StrCat( name, "_", handle ); + } + + [[nodiscard]] int parse_int( std::string_view value ) const + { + int result{ 0 }; + check_line( absl::SimpleAtoi( value, &result ), + "invalid integer \"", value, "\"" ); + return result; + } + + [[nodiscard]] double parse_double( std::string_view value ) const + { + double result{ 0 }; + check_line( + absl::SimpleAtod( value, &result ) && std::isfinite( result ), + "invalid real \"", value, "\"" ); + return result; + } + + template < typename... Args > + void check_line( bool condition, const Args&... message ) const + { + geode::OpenGeodeIOMeshException::check_exception( condition, + nullptr, geode::OpenGeodeException::TYPE::data, + "[DXFInput] Line ", line_number_, " of file ", filename_, ": ", + message... ); + } + + private: + std::string_view filename_; + std::ifstream file_; + geode::index_t line_number_{ 0 }; + std::optional< GroupPair > pending_; + std::optional< Entity > pending_entity_; + std::vector< SurfaceData > surfaces_; + absl::flat_hash_map< std::string, geode::index_t > layer_surfaces_; + absl::flat_hash_map< std::string, geode::index_t > skipped_entities_; + SkippedFaces skipped_faces_; + }; + + std::unique_ptr< geode::PolygonalSurface3D > build_surface( + SurfaceData& data, const geode::MeshImpl& impl ) + { + auto surface = geode::PolygonalSurface3D::create( impl ); + auto builder = geode::PolygonalSurfaceBuilder3D::create( *surface ); + builder->set_name( data.name ); + if( data.colocate ) + { + const geode::NNSearch3D search{ std::move( data.points ) }; + const auto colocated = + search.colocated_index_mapping( geode::GLOBAL_EPSILON ); + data.points = colocated.unique_points; + for( auto& polygon : data.polygons ) + { + for( auto& vertex : polygon ) + { + vertex = colocated.colocated_mapping[vertex]; + } + } + } + builder->create_vertices( data.points.size() ); + for( const auto v : geode::Indices{ data.points } ) + { + builder->set_point( v, data.points[v] ); + } + for( auto& polygon : data.polygons ) + { + // Colocation may merge distinct close corners + if( clean_polygon( polygon ) == PolygonStatus::valid ) + { + builder->create_polygon( polygon ); + } + } + builder->compute_polygon_adjacencies(); + return surface; + } +} // namespace namespace geode { namespace internal { std::vector< std::unique_ptr< PolygonalSurface3D > > DXFInput::read( - const MeshImpl& /*unused*/ ) + const MeshImpl& impl ) { - AssimpMeshInput< PolygonalSurface3D > reader{ filename() }; - return reader.read_file(); + DXFReader reader{ filename() }; + auto data = reader.read(); + reader.log_warnings(); + std::vector< std::unique_ptr< PolygonalSurface3D > > surfaces; + surfaces.reserve( data.size() ); + for( auto& surface : data ) + { + surfaces.push_back( build_surface( surface, impl ) ); + } + return surfaces; } Percentage DXFInput::is_loadable() const { - AssimpMeshInput< PolygonalSurface3D > reader{ filename() }; - return reader.is_loadable(); + try + { + const auto data = DXFReader{ filename() }.read(); + return Percentage{ data.empty() ? 0. : 1. }; + } + catch( const OpenGeodeException& ) + { + return Percentage{ 0 }; + } } } // namespace internal } // namespace geode diff --git a/tests/data/polyface_surfaces.dxf b/tests/data/polyface_surfaces.dxf new file mode 100644 index 0000000..2363328 --- /dev/null +++ b/tests/data/polyface_surfaces.dxf @@ -0,0 +1,544 @@ + 0 +SECTION + 2 +HEADER + 9 +$ACADVER + 1 +AC1027 + 0 +ENDSEC + 0 +SECTION + 2 +BLOCKS + 0 +BLOCK + 8 +0 + 2 +IGNORED + 0 +3DFACE + 8 +BLOCK_LAYER + 10 +0.0 + 20 +0.0 + 30 +1.0 + 11 +1.0 + 21 +0.0 + 31 +1.0 + 12 +2.0 + 22 +0.0 + 32 +1.0 + 13 +3.0 + 23 +0.0 + 33 +1.0 + 0 +ENDBLK + 8 +0 + 0 +ENDSEC + 0 +SECTION + 2 +ENTITIES + 0 +POLYLINE + 5 +1A + 8 +GALERIES + 66 +1 + 10 +0.0 + 20 +0.0 + 30 +0.0 + 70 +64 + 71 +6 + 72 +6 +1001 +Dimensions de Galerie +1000 +4.5L X 4.5H +1001 +Nom de Galerie +1000 +Ignored +1001 +_AREA +1040 +40.8 +1001 +Name +1000 +Galerie A + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +1 + 20 +0 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +1 + 20 +1 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +1 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +2 + 20 +0 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +2 + 20 +1 + 30 +0 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +2 + 73 +4 + 74 +1 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +2 + 72 +-3 + 73 +4 + 74 +0 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +2 + 72 +5 + 73 +6 + 74 +3 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +3 + 72 +3 + 73 +3 + 74 +3 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +2 + 73 +9 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +2 + 73 +1 + 74 +3 + 0 +SEQEND + 8 +GALERIES + 0 +LINE + 8 +GALERIES + 10 +5.0 + 20 +5.0 + 30 +5.0 + 11 +6.0 + 21 +6.0 + 31 +6.0 + 0 +POLYLINE + 5 +1B + 8 +GALERIES + 66 +1 + 70 +8 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +5 + 70 +32 + 0 +VERTEX + 8 +GALERIES + 10 +1 + 20 +0 + 30 +5 + 70 +32 + 0 +SEQEND + 8 +GALERIES + 0 +POLYLINE + 5 +2B + 8 +GALERIES + 66 +1 + 10 +0.0 + 20 +0.0 + 30 +0.0 + 70 +64 + 71 +4 + 72 +2 +1001 +Name +1000 + +1001 +Nom de Galerie +1000 +Baie d\U+00E9cantation 1 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +10 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +1 + 20 +0 + 30 +10 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +1 + 20 +1 + 30 +10 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +1 + 30 +10 + 70 +192 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +2 + 73 +3 + 74 +1 + 0 +VERTEX + 8 +GALERIES + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +3 + 73 +4 + 74 +1 + 0 +SEQEND + 8 +GALERIES + 0 +POLYLINE + 5 +3C + 8 +OTHER + 66 +1 + 10 +0.0 + 20 +0.0 + 30 +0.0 + 70 +64 + 71 +3 + 72 +1 + 0 +VERTEX + 8 +OTHER + 10 +0 + 20 +0 + 30 +20 + 70 +192 + 0 +VERTEX + 8 +OTHER + 10 +1 + 20 +0 + 30 +20 + 70 +192 + 0 +VERTEX + 8 +OTHER + 10 +0 + 20 +1 + 30 +20 + 70 +192 + 0 +VERTEX + 8 +OTHER + 10 +0 + 20 +0 + 30 +0 + 70 +128 + 71 +1 + 72 +2 + 73 +3 + 0 +SEQEND + 8 +OTHER + 0 +ENDSEC + 0 +EOF diff --git a/tests/mesh/test-dxf.cpp b/tests/mesh/test-dxf.cpp index 75c3b18..9f050a9 100644 --- a/tests/mesh/test-dxf.cpp +++ b/tests/mesh/test-dxf.cpp @@ -23,8 +23,21 @@ #include +#include +#include +#include +#include +#include +#include + +#include + #include #include +#include +#include + +#include #include #include @@ -32,38 +45,457 @@ #include -int main() +namespace { - try + /* Records the warnings to check what the reader reports */ + class WarningRecorder : public geode::LoggerClient { - geode::OpenGeodeIOMeshLibrary::initialize(); - // Load file - const auto surface_filename = - absl::StrCat( geode::DATA_PATH, "3D_faces.dxf" ); - auto surface = geode::load_one( - geode::load_polygonal_surface< 3 >( surface_filename ), - surface_filename ); - geode::OpenGeodeIOMeshException::test( surface->nb_vertices() == 49323, - "Number of vertices in the loaded Surface is not correct" ); - geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 97966, - "Number of polygons in the loaded Surface is not correct" ); - - // Save file + public: + static void start() + { + static const auto registered = [] { + geode::LoggerManager::register_client( + std::make_unique< WarningRecorder >() ); + return true; + }(); + geode_unused( registered ); + warnings().clear(); + } + + [[nodiscard]] static bool has_warning( std::string_view text ) + { + for( const auto& warning : warnings() ) + { + if( absl::StrContains( warning, text ) ) + { + return true; + } + } + return false; + } + + void trace( const std::string& /*unused*/ ) override {} + void debug( const std::string& /*unused*/ ) override {} + void info( const std::string& /*unused*/ ) override {} + void warning( const std::string& message ) override + { + warnings().push_back( message ); + } + void error( const std::string& /*unused*/ ) override {} + void critical( const std::string& /*unused*/ ) override {} + + private: + static std::vector< std::string >& warnings() + { + static std::vector< std::string > recorded; + return recorded; + } + }; + + void write_file( const std::string& filename, std::string_view content ) + { + std::ofstream file{ filename, std::ios::binary }; + file << content; + } + + void check_surface( const geode::PolygonalSurface3D& surface, + std::string_view name, + geode::index_t nb_vertices, + geode::index_t nb_polygons ) + { + geode::OpenGeodeIOMeshException::test( + surface.name().value_or( "" ) == name, "Wrong surface name: \"", + surface.name().value_or( "" ), "\" instead of \"", name, "\"" ); + geode::OpenGeodeIOMeshException::test( + surface.nb_vertices() == nb_vertices, + "Wrong number of vertices in ", name, ": ", surface.nb_vertices() ); + geode::OpenGeodeIOMeshException::test( + surface.nb_polygons() == nb_polygons, + "Wrong number of polygons in ", name, ": ", surface.nb_polygons() ); + } + + void check_adjacency( const geode::PolygonalSurface3D& surface, + geode::PolygonEdge edge, + std::optional< geode::index_t > adjacent ) + { + geode::OpenGeodeIOMeshException::test( + surface.polygon_adjacent( edge ) == adjacent, "Wrong adjacency of ", + surface.name().value_or( "" ), " polygon ", edge.polygon_id, + " edge ", edge.edge_id ); + } + + void expect_data_error( + std::string_view filename, std::string_view expected_message ) + { + try + { + const auto surfaces = + geode::load_polygonal_surface< 3 >( filename ); + } + catch( const geode::OpenGeodeException& exception ) + { + geode::OpenGeodeIOMeshException::test( + exception.type() == geode::OpenGeodeException::TYPE::data + && absl::StrContains( exception.what(), expected_message ), + "Wrong error loading ", filename, ": ", exception.what() ); + return; + } + throw geode::OpenGeodeIOMeshException{ nullptr, + geode::OpenGeodeException::TYPE::internal, "Loading ", filename, + " should raise a data error" }; + } + + void test_polyface_surfaces() + { + const auto filename = + absl::StrCat( geode::DATA_PATH, "polyface_surfaces.dxf" ); + geode::OpenGeodeIOMeshException::test( + geode::is_polygonal_surface_loadable< 3 >( filename ).value() == 1, + "polyface_surfaces.dxf should be loadable" ); + const auto surfaces = geode::load_polygonal_surface< 3 >( filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 3, + "Wrong number of polyface surfaces: ", surfaces.size() ); + + const auto& galerie = *surfaces[0]; + check_surface( galerie, "Galerie A", 6, 3 ); + const std::array< std::vector< geode::index_t >, 3 > expected{ { + { 0, 1, 3 }, + { 1, 2, 3 }, + { 1, 4, 5, 2 }, + } }; + for( const auto p : geode::LIndices{ expected } ) + { + geode::OpenGeodeIOMeshException::test( + galerie.nb_polygon_vertices( p ) == expected[p].size(), + "Wrong size of polygon ", p ); + for( const auto v : geode::LIndices{ expected[p] } ) + { + geode::OpenGeodeIOMeshException::test( + galerie.polygon_vertex( { p, v } ) == expected[p][v], + "Wrong vertex ", v, " of polygon ", p ); + } + } + geode::OpenGeodeIOMeshException::test( + galerie.point( 5 ).inexact_equal( geode::Point3D{ { 2, 1, 0 } } ), + "Wrong position of vertex 5" ); + check_adjacency( galerie, { 0, 0 }, std::nullopt ); + check_adjacency( galerie, { 0, 1 }, 1 ); + check_adjacency( galerie, { 1, 2 }, 0 ); + check_adjacency( galerie, { 1, 0 }, 2 ); + check_adjacency( galerie, { 2, 3 }, 1 ); + + const auto& baie = *surfaces[1]; + check_surface( baie, + "Baie d\xC3\xA9" + "cantation 1", + 4, 2 ); + check_adjacency( baie, { 0, 2 }, 1 ); + check_adjacency( baie, { 1, 0 }, 0 ); + geode::OpenGeodeIOMeshException::test( + baie.point( 0 ).inexact_equal( geode::Point3D{ { 0, 0, 10 } } ), + "Wrong position of vertex 0 in second surface" ); + + check_surface( *surfaces[2], "OTHER_3C", 3, 1 ); + } + + void test_3d_faces() + { + // Expected values computed by a script independent of the reader + const std::array< + std::tuple< std::string_view, geode::index_t, geode::index_t >, 14 > + expected{ { + { "S00L020001", 5523, 10236 }, + { "B003001003", 7597, 14814 }, + { "S00L030003", 2535, 4715 }, + { "S00L040003", 3329, 6291 }, + { "S00L050003", 3531, 6687 }, + { "S00L020003", 1697, 3082 }, + { "S00L060003", 3389, 6284 }, + { "S00L070003", 1883, 3333 }, + { "S00L030001", 5857, 11102 }, + { "S00L040001", 5052, 9644 }, + { "S00L050001", 4441, 8419 }, + { "B004001002", 3934, 7690 }, + { "S00L060001", 2548, 4705 }, + { "S00L070001", 568, 964 }, + } }; + const auto filename = absl::StrCat( geode::DATA_PATH, "3D_faces.dxf" ); + const auto surfaces = geode::load_polygonal_surface< 3 >( filename ); + geode::OpenGeodeIOMeshException::test( + surfaces.size() == expected.size(), + "Wrong number of 3DFACE surfaces: ", surfaces.size() ); + geode::index_t nb_polygons{ 0 }; + for( const auto s : geode::LIndices{ expected } ) + { + const auto& [name, nb_vertices, nb_layer_polygons] = expected[s]; + check_surface( *surfaces[s], name, nb_vertices, nb_layer_polygons ); + nb_polygons += surfaces[s]->nb_polygons(); + } + geode::OpenGeodeIOMeshException::test( nb_polygons == 97966, + "Wrong total number of polygons: ", nb_polygons ); + for( const auto p : geode::Range{ surfaces[0]->nb_polygons() } ) + { + geode::OpenGeodeIOMeshException::test( + surfaces[0]->polygon_adjacent( { p, 0 } ) + || surfaces[0]->polygon_adjacent( { p, 1 } ) + || surfaces[0]->polygon_adjacent( { p, 2 } ), + "3DFACE polygons should be connected" ); + } + const auto output_file_og = - absl::StrCat( "dxf.", surface->native_extension() ); - geode::save_polygonal_surface( *surface, output_file_og ); + absl::StrCat( "dxf.", surfaces[0]->native_extension() ); + geode::save_polygonal_surface( *surfaces[0], output_file_og ); + } - const auto face_and_line_filename = + void test_face_and_line() + { + const auto filename = absl::StrCat( geode::DATA_PATH, "face_and_line.dxf" ); auto face_and_line = geode::load_one( - geode::load_polygonal_surface< 3 >( face_and_line_filename ), - face_and_line_filename ); + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *face_and_line, "0", 3, 1 ); + } + + void test_binary_dxf() + { + const std::string filename{ "binary.dxf" }; + { + std::ofstream file{ filename, std::ios::binary }; + file << "AutoCAD Binary DXF\r\n"; + file.put( '\x1a' ); + file.put( '\0' ); + } geode::OpenGeodeIOMeshException::test( - face_and_line->nb_polygons() == 1, - "DXF lines should not be loaded as polygons" ); + geode::is_polygonal_surface_loadable< 3 >( filename ).value() == 0, + "A binary DXF should not be loadable" ); + expect_data_error( filename, "Binary DXF" ); + } + + void test_trailing_content() + { + // Content after the ENTITIES section or after EOF is not read + const std::string filename{ "trailing_content.dxf" }; + { + std::ofstream file{ filename, std::ios::binary }; + file << " 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nLAYER\n" + " 10\n0.0\n 20\n0.0\n 30\n0.0\n 11\n1.0\n 21\n0.0\n" + " 31\n0.0\n 12\n0.0\n 22\n1.0\n 32\n0.0\n 13\n0.0\n" + " 23\n1.0\n 33\n0.0\n 0\nENDSEC\n 0\nSECTION\n" + " 2\nOBJECTS\nnot a group code\n 0\nENDSEC\n 0\nEOF\n\n"; + } + geode::OpenGeodeIOMeshException::test( + geode::is_polygonal_surface_loadable< 3 >( filename ).value() == 1, + "A DXF with trailing content should be loadable" ); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "LAYER", 3, 1 ); + + const std::string eof_filename{ "trailing_blank_line.dxf" }; + { + std::ofstream file{ eof_filename, std::ios::binary }; + file << " 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nLAYER\n" + " 10\n0.0\n 20\n0.0\n 30\n0.0\n 11\n1.0\n 21\n0.0\n" + " 31\n0.0\n 12\n0.0\n 22\n1.0\n 32\n0.0\n 0\nEOF\n\n"; + } + geode::OpenGeodeIOMeshException::test( + geode::is_polygonal_surface_loadable< 3 >( eof_filename ).value() + == 1, + "A DXF with a blank line after EOF should be loadable" ); + auto eof_surface = geode::load_one( + geode::load_polygonal_surface< 3 >( eof_filename ), eof_filename ); + check_surface( *eof_surface, "LAYER", 3, 1 ); + + const std::string endsec_filename{ "trailing_after_endsec.dxf" }; + { + std::ofstream file{ endsec_filename, std::ios::binary }; + file << " 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nLAYER\n" + " 10\n0.0\n 20\n0.0\n 30\n0.0\n 11\n1.0\n 21\n0.0\n" + " 31\n0.0\n 12\n0.0\n 22\n1.0\n 32\n0.0\n 0\nENDSEC\n" + "\n"; + } + auto endsec_surface = geode::load_one( + geode::load_polygonal_surface< 3 >( endsec_filename ), + endsec_filename ); + check_surface( *endsec_surface, "LAYER", 3, 1 ); + } + + void test_no_face() + { + const std::string filename{ "no_face.dxf" }; + { + std::ofstream file{ filename, std::ios::binary }; + file << " 0\nSECTION\n 2\nENTITIES\n 0\nLINE\n 8\n0\n 10\n0.0\n" + " 20\n0.0\n 30\n0.0\n 11\n1.0\n 21\n1.0\n 31\n1.0\n" + " 0\nENDSEC\n 0\nEOF\n"; + } geode::OpenGeodeIOMeshException::test( - face_and_line->nb_vertices() == 3, - "DXF line vertices should not be loaded" ); + geode::is_polygonal_surface_loadable< 3 >( filename ).value() == 0, + "A DXF without faces should not be loadable" ); + expect_data_error( filename, "No object found" ); + } + void test_3dface_corners() + { + // No 13/23/33 codes: the fourth corner is the third one + const std::string filename{ "3dface_corners.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nTRIANGLE\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n" + // Second corner without z: incomplete face + " 0\n3DFACE\n 8\nINCOMPLETE\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nENDSEC\n 0\nEOF\n" ); + WarningRecorder::start(); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "TRIANGLE", 3, 1 ); + for( const auto v : geode::Range{ surface->nb_vertices() } ) + { + geode::OpenGeodeIOMeshException::test( + surface->point( v ) != geode::Point3D{ { 0, 0, 0 } }, + "A 3DFACE without fourth corner should not have a vertex at " + "the origin" ); + } + geode::OpenGeodeIOMeshException::test( + WarningRecorder::has_warning( "1 faces skipped" ) + && WarningRecorder::has_warning( + "1 with less than 3 distinct vertices" ), + "The incomplete 3DFACE should be reported as skipped" ); + } + + void test_polyface_minimum_index() + { + const std::string filename{ "polyface_minimum_index.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n 0\nPOLYLINE\n 8\nLAYER\n" + " 70\n64\n" + " 0\nVERTEX\n 10\n0.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n1.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n0.0\n 20\n1.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 70\n128\n 71\n1\n 72\n2\n 73\n3\n" + " 0\nVERTEX\n 70\n128\n 71\n1\n 72\n2\n 73\n-2147483648\n" + " 0\nSEQEND\n 0\nENDSEC\n 0\nEOF\n" ); + WarningRecorder::start(); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "LAYER", 3, 1 ); + geode::OpenGeodeIOMeshException::test( + WarningRecorder::has_warning( + "1 with out of range vertex indices" ), + "The face with index -2147483648 should be out of range" ); + } + + void test_missing_seqend() + { + // The POLYLINE has no SEQEND: the next entity is still read + const std::string filename{ "missing_seqend.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n 0\nPOLYLINE\n 8\nPOLYFACE\n" + " 5\n2A\n 70\n64\n" + " 0\nVERTEX\n 10\n0.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n1.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n0.0\n 20\n1.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 70\n128\n 71\n1\n 72\n2\n 73\n3\n" + " 0\n3DFACE\n 8\nFACES\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nENDSEC\n 0\nEOF\n" ); + const auto surfaces = geode::load_polygonal_surface< 3 >( filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 2, + "Wrong number of surfaces without SEQEND: ", surfaces.size() ); + check_surface( *surfaces[0], "POLYFACE_2A", 3, 1 ); + check_surface( *surfaces[1], "FACES", 3, 1 ); + } + + void test_utf8_bom() + { + const std::string filename{ "utf8_bom.dxf" }; + write_file( filename, + "\xEF\xBB\xBF 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nBOM\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nENDSEC\n 0\nEOF\n" ); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "BOM", 3, 1 ); + } + + void test_comment_before_entities() + { + const std::string filename{ "comment_before_entities.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n999\nA comment\n 0\n3DFACE\n" + " 8\nCOMMENTED\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nENDSEC\n 0\nEOF\n" ); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "COMMENTED", 3, 1 ); + } + + void test_unicode_layer_names() + { + const std::string filename{ "unicode_layers.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n 0\n3DFACE\n 8\nCouche\\U+00E9\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nPOLYLINE\n 8\nPoly\\U+00E9\n 5\n2A\n 70\n64\n" + " 0\nVERTEX\n 10\n0.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n1.0\n 20\n0.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 10\n0.0\n 20\n1.0\n 30\n0.0\n 70\n192\n" + " 0\nVERTEX\n 70\n128\n 71\n1\n 72\n2\n 73\n3\n" + " 0\nSEQEND\n 0\nENDSEC\n 0\nEOF\n" ); + const auto surfaces = geode::load_polygonal_surface< 3 >( filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 2, + "Wrong number of surfaces with unicode layers: ", surfaces.size() ); + check_surface( *surfaces[0], "Couche\xC3\xA9", 3, 1 ); + check_surface( *surfaces[1], "Poly\xC3\xA9_2A", 3, 1 ); + } + + void test_viewport_not_reported() + { + const std::string filename{ "viewport.dxf" }; + write_file( filename, + " 0\nSECTION\n 2\nENTITIES\n 0\nVIEWPORT\n 8\n0\n 10\n0.0\n" + " 20\n0.0\n 30\n0.0\n 0\nLINE\n 8\n0\n 10\n0.0\n 20\n0.0\n" + " 30\n0.0\n 11\n1.0\n 21\n1.0\n 31\n1.0\n 0\n3DFACE\n 8\nVIEW\n" + " 10\n5.0\n 20\n5.0\n 30\n5.0\n 11\n6.0\n 21\n5.0\n 31\n5.0\n" + " 12\n5.0\n 22\n6.0\n 32\n5.0\n 13\n5.0\n 23\n6.0\n 33\n5.0\n" + " 0\nENDSEC\n 0\nEOF\n" ); + WarningRecorder::start(); + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + check_surface( *surface, "VIEW", 3, 1 ); + geode::OpenGeodeIOMeshException::test( + WarningRecorder::has_warning( "LINE (1)" ), + "Skipped LINE should be reported" ); + geode::OpenGeodeIOMeshException::test( + !WarningRecorder::has_warning( "VIEWPORT" ), + "Skipped VIEWPORT should not be reported" ); + } +} // namespace + +int main() +{ + try + { + geode::OpenGeodeIOMeshLibrary::initialize(); + test_polyface_surfaces(); + test_3d_faces(); + test_face_and_line(); + test_binary_dxf(); + test_trailing_content(); + test_no_face(); + test_3dface_corners(); + test_polyface_minimum_index(); + test_missing_seqend(); + test_utf8_bom(); + test_comment_before_entities(); + test_unicode_layer_names(); + test_viewport_not_reported(); geode::Logger::info( "TEST SUCCESS" ); return 0; diff --git a/tests/mesh/test-obj.cpp b/tests/mesh/test-obj.cpp index 89489df..512b09b 100644 --- a/tests/mesh/test-obj.cpp +++ b/tests/mesh/test-obj.cpp @@ -111,26 +111,46 @@ namespace return nb_checked; } + void check_name( + const geode::PolygonalSurface3D& surface, std::string_view expected ) + { + geode::OpenGeodeIOMeshException::test( + surface.name() && surface.name().value() == expected, + "Surface should be named ", expected ); + } + void test_untextured_then_textured_groups() { const auto surface_filename = absl::StrCat( geode::DATA_PATH, "two_groups_textured.obj" ); - auto surface = geode::load_one( - geode::load_polygonal_surface< 3 >( surface_filename ), - surface_filename ); - geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 2, - "Number of polygons in two_groups_textured is not correct" ); + const auto surfaces = + geode::load_polygonal_surface< 3 >( surface_filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 2, + "two_groups_textured should give one surface per group" ); + const auto& untextured = *surfaces[0]; + const auto& textured = *surfaces[1]; + check_name( untextured, "untextured" ); + check_name( textured, "textured" ); + geode::OpenGeodeIOMeshException::test( + untextured.nb_polygons() == 1 && untextured.nb_vertices() == 3, + "Wrong untextured group in two_groups_textured" ); + geode::OpenGeodeIOMeshException::test( + !untextured.texture_manager().texture_exists( "textured_material" ), + "Untextured group should have no texture" ); + geode::OpenGeodeIOMeshException::test( + textured.nb_polygons() == 1 && textured.nb_vertices() == 3, + "Wrong textured group in two_groups_textured" ); const std::array< CornerUV, 3 > expected{ { { geode::Point3D{ { 2, 0, 0 } }, geode::Point2D{ { 0.1, 0.2 } } }, { geode::Point3D{ { 3, 0, 0 } }, geode::Point2D{ { 0.3, 0.4 } } }, { geode::Point3D{ { 2, 1, 0 } }, geode::Point2D{ { 0.5, 0.6 } } }, } }; geode::OpenGeodeIOMeshException::test( - check_texture_coordinates( *surface, "textured_material", expected ) + check_texture_coordinates( textured, "textured_material", expected ) == 3, "Wrong number of textured corners in two_groups_textured" ); geode::OpenGeodeIOMeshException::test( - surface->texture_manager() + textured.texture_manager() .find_texture( "textured_material" ) .image() .nb_cells() @@ -142,24 +162,179 @@ namespace { const auto surface_filename = absl::StrCat( geode::DATA_PATH, "duplicated_textured.obj" ); - auto surface = geode::load_one( - geode::load_polygonal_surface< 3 >( surface_filename ), - surface_filename ); - geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 1, - "Number of polygons in duplicated_textured is not correct" ); + const auto surfaces = + geode::load_polygonal_surface< 3 >( surface_filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 2, + "duplicated_textured should give one surface per group" ); const std::array< CornerUV, 3 > expected{ { { geode::Point3D{ { 0, 0, 0 } }, geode::Point2D{ { 0.1, 0.2 } } }, { geode::Point3D{ { 1, 0, 0 } }, geode::Point2D{ { 0.3, 0.4 } } }, { geode::Point3D{ { 0, 1, 0 } }, geode::Point2D{ { 0.5, 0.6 } } }, } }; - for( const auto& texture_name : - { "first_material", "second_material" } ) + const std::array< std::string_view, 2 > names{ "first", "second" }; + const std::array< std::string_view, 2 > materials{ "first_material", + "second_material" }; + for( const auto s : geode::LRange{ 2 } ) { + const auto& surface = *surfaces[s]; + check_name( surface, names[s] ); + geode::OpenGeodeIOMeshException::test( surface.nb_polygons() == 1, + "Number of polygons in duplicated_textured is not correct" ); geode::OpenGeodeIOMeshException::test( - check_texture_coordinates( *surface, texture_name, expected ) + check_texture_coordinates( surface, materials[s], expected ) == 3, "Wrong number of textured corners in duplicated_textured" ); + geode::OpenGeodeIOMeshException::test( + !surface.texture_manager().texture_exists( materials[1 - s] ), + "Each group should only have its own texture" ); + } + } + + void test_repeated_corners() + { + const auto filename = "repeated_corners.obj"; + { + std::ofstream file{ filename }; + file << "v 0 0 0\nv 1 0 0\nv 1 0 0\nv 0 1 0\nv 1 1 0\n" + // Consecutive corners on equal points + << "f 1 2 3 4\n" + // Repeated index, wrapping around + << "f 2 5 4 2\n" + // Degenerate face + << "f 1 2 1\n"; + } + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 2, + "Faces with repeated corners should be loaded as triangles" ); + geode::OpenGeodeIOMeshException::test( surface->nb_vertices() == 4, + "Wrong number of vertices with repeated corners" ); + for( const auto p : geode::Range{ surface->nb_polygons() } ) + { + geode::OpenGeodeIOMeshException::test( + surface->nb_polygon_vertices( p ) == 3, + "Repeated corners should be removed" ); + } + } + + // Checks the corners of the polygon located on one of the given points + // have the expected texture coordinates. Returns the number of checked + // corners. + geode::index_t check_polygon_texture_coordinates( + const geode::PolygonalSurface3D& surface, + geode::index_t polygon, + std::string_view texture_name, + absl::Span< const CornerUV > expected ) + { + const auto& texture = + surface.texture_manager().find_texture( texture_name ); + geode::index_t nb_checked{ 0 }; + for( const auto v : + geode::LRange{ surface.nb_polygon_vertices( polygon ) } ) + { + const auto& point = + surface.point( surface.polygon_vertex( { polygon, v } ) ); + for( const auto& corner : expected ) + { + if( point != corner.point ) + { + continue; + } + geode::OpenGeodeIOMeshException::test( + texture.texture_coordinates( { polygon, v } ) + .inexact_equal( corner.uv ), + "Wrong texture coordinates on polygon ", polygon, + " vertex ", v, " of texture ", texture_name ); + nb_checked++; + } + } + return nb_checked; + } + + void test_multi_material_object() + { + const auto filename = "multi_material.obj"; + { + std::ofstream mtl{ "multi_material.mtl" }; + mtl << "newmtl first_material\nKd 1 1 1\n" + << "newmtl second_material\nKd 1 1 1\n"; + std::ofstream obj{ filename }; + obj << "mtllib multi_material.mtl\n" + << "v 0 0 0\nv 1 0 0\nv 0 1 0\nv 1 1 0\n" + << "v 5 5 5\nv 6 5 5\nv 5 6 5\n" + << "vt 0.1 0.2\nvt 0.3 0.4\nvt 0.5 0.6\nvt 0.7 0.8\n" + // A single material group first, so that the global mesh + // ids differ from the positions in the next group + << "g single\nusemtl second_material\nf 5/1 6/2 7/3\n" + << "g obj\nusemtl first_material\nf 1/1 2/2 3/3\n" + << "usemtl second_material\nf 2/2 4/4 3/3\n"; } + const auto surfaces = geode::load_polygonal_surface< 3 >( filename ); + geode::OpenGeodeIOMeshException::test( surfaces.size() == 2, + "multi_material should give one surface per group" ); + check_name( *surfaces[0], "single" ); + geode::OpenGeodeIOMeshException::test( surfaces[0]->nb_polygons() == 1, + "Wrong number of polygons in the single material group" ); + const auto& surface = *surfaces[1]; + check_name( surface, "obj" ); + geode::OpenGeodeIOMeshException::test( surface.nb_polygons() == 2, + "Multi material object should have its 2 polygons" ); + geode::OpenGeodeIOMeshException::test( surface.nb_vertices() == 4, + "Multi material object polygons should share their vertices" ); + const std::array< CornerUV, 3 > first_expected{ { + { geode::Point3D{ { 0, 0, 0 } }, geode::Point2D{ { 0.1, 0.2 } } }, + { geode::Point3D{ { 1, 0, 0 } }, geode::Point2D{ { 0.3, 0.4 } } }, + { geode::Point3D{ { 0, 1, 0 } }, geode::Point2D{ { 0.5, 0.6 } } }, + } }; + const std::array< CornerUV, 3 > second_expected{ { + { geode::Point3D{ { 1, 0, 0 } }, geode::Point2D{ { 0.3, 0.4 } } }, + { geode::Point3D{ { 1, 1, 0 } }, geode::Point2D{ { 0.7, 0.8 } } }, + { geode::Point3D{ { 0, 1, 0 } }, geode::Point2D{ { 0.5, 0.6 } } }, + } }; + geode::index_t nb_first{ 0 }; + geode::index_t nb_second{ 0 }; + for( const auto p : geode::Range{ surface.nb_polygons() } ) + { + bool is_first{ false }; + for( const auto v : + geode::LRange{ surface.nb_polygon_vertices( p ) } ) + { + if( surface.point( surface.polygon_vertex( { p, v } ) ) + == geode::Point3D{ { 0, 0, 0 } } ) + { + is_first = true; + } + } + if( is_first ) + { + nb_first += check_polygon_texture_coordinates( + surface, p, "first_material", first_expected ); + } + else + { + nb_second += check_polygon_texture_coordinates( + surface, p, "second_material", second_expected ); + } + } + geode::OpenGeodeIOMeshException::test( nb_first == 3 && nb_second == 3, + "Each material polygon should have its 3 textured corners" ); + } + + void test_no_object_name() + { + // No g/o statement: Assimp names the object "defaultobject" + const auto filename = "no_object_name.obj"; + { + std::ofstream file{ filename }; + file << "v 0 0 0\nv 1 0 0\nv 0 1 0\nf 1 2 3\n"; + } + auto surface = geode::load_one( + geode::load_polygonal_surface< 3 >( filename ), filename ); + geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 1, + "Wrong number of polygons without object name" ); + geode::OpenGeodeIOMeshException::test( !surface->name(), + "OBJ without g/o: the Surface should not be named, not \"", + surface->name().value_or( "" ), "\"" ); } void test_lines_and_points() @@ -287,6 +462,7 @@ int main() "Number of vertices in the loaded Surface is not correct" ); geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 380, "Number of polygons in the loaded Surface is not correct" ); + check_name( *surface, "TopHat3:pCylinder3" ); // Save file geode::save_polygonal_surface( @@ -310,6 +486,9 @@ int main() check_adjacencies( *surface ); test_untextured_then_textured_groups(); test_duplicated_polygon_textures(); + test_repeated_corners(); + test_multi_material_object(); + test_no_object_name(); test_lines_and_points(); test_too_large_polygon(); test_absolute_texture_path(); diff --git a/tests/mesh/test-ply.cpp b/tests/mesh/test-ply.cpp index b177ce5..6bf8172 100644 --- a/tests/mesh/test-ply.cpp +++ b/tests/mesh/test-ply.cpp @@ -75,6 +75,8 @@ int main() "Number of vertices in the loaded Surface is not correct" ); geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 345944, "Number of polygons in the loaded Surface is not correct" ); + geode::OpenGeodeIOMeshException::test( !surface->name(), + "PLY files have no object name: the Surface should not be named" ); check_adjacencies( *surface ); diff --git a/tests/mesh/test-stl.cpp b/tests/mesh/test-stl.cpp index 29625c9..41ba8ea 100644 --- a/tests/mesh/test-stl.cpp +++ b/tests/mesh/test-stl.cpp @@ -88,6 +88,9 @@ int main() "Number of vertices in the loaded Surface is not correct" ); geode::OpenGeodeIOMeshException::test( surface->nb_polygons() == 1027, "Number of polygons in the loaded Surface is not correct" ); + geode::OpenGeodeIOMeshException::test( + surface->name() && surface->name().value() == "thumbwheel", + "Loaded Surface should be named after the STL solid" ); // Save file geode::save_triangulated_surface( *surface,