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 b5af79a..88bcfa6 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 @@ -68,10 +68,12 @@ def test_extensions(): check_extensions( opengeode.PolygonalSurface3D, ["obj", "ply", "vtp", "vtu", "dxf"], - ["obj", "ply", "vtp"], + ["obj", "ply", "vtp", "dxf"], ) check_extensions( - opengeode.TriangulatedSurface3D, ["stl", "smesh", "vtu"], ["obj", "stl", "vtp"] + opengeode.TriangulatedSurface3D, + ["stl", "smesh", "vtu"], + ["obj", "stl", "vtp", "dxf"], ) check_extensions(opengeode.EdgedCurve3D, ["smesh"], ["vtp"]) check_extensions(opengeode.PolygonalSurface2D, [], ["vtp", "dot"]) diff --git a/include/geode/io/mesh/internal/dxf_common.hpp b/include/geode/io/mesh/internal/dxf_common.hpp new file mode 100644 index 0000000..2dff46e --- /dev/null +++ b/include/geode/io/mesh/internal/dxf_common.hpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#pragma once + +#include +#include + +#include + +namespace geode +{ + namespace internal + { + /*! + * Number of corners of a 3DFACE. A triangle repeats its third corner + * as fourth one. + */ + inline constexpr local_index_t NB_3DFACE_CORNERS{ 4 }; + + /*! + * Encodes the non-ASCII characters of a UTF-8 string as the \U+XXXX + * escapes of DXF strings. Invalid UTF-8 sequences and code points + * above U+FFFF, which cannot be escaped, are replaced by '_'. + */ + [[nodiscard]] std::string encode_unicode_escapes( + std::string_view utf8 ); + + /*! + * Decodes the \U+XXXX escapes of DXF strings to UTF-8. + */ + [[nodiscard]] std::string decode_unicode_escapes( + std::string_view value ); + } // namespace internal +} // namespace geode diff --git a/include/geode/io/mesh/internal/dxf_polygonal_output.hpp b/include/geode/io/mesh/internal/dxf_polygonal_output.hpp new file mode 100644 index 0000000..dc1ddb2 --- /dev/null +++ b/include/geode/io/mesh/internal/dxf_polygonal_output.hpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#pragma once + +#include +#include + +#include + +namespace geode +{ + FORWARD_DECLARATION_DIMENSION_CLASS( PolygonalSurface ); + ALIAS_3D( PolygonalSurface ); +} // namespace geode + +namespace geode +{ + namespace internal + { + /*! + * Writes an ASCII DXF with one 3DFACE per polygon, + * which must have 3 or 4 vertices: see + * write_dxf_surface. + */ + class DXFPolygonalOutput final : public PolygonalSurfaceOutput< 3 > + { + public: + explicit DXFPolygonalOutput( std::string_view filename ) + : PolygonalSurfaceOutput< 3 >( filename ) + { + } + + static std::string_view extension() + { + static constexpr auto EXT = "dxf"; + return EXT; + } + + std::vector< std::string > write( + const PolygonalSurface3D &surface ) const final; + }; + } // namespace internal +} // namespace geode diff --git a/include/geode/io/mesh/internal/dxf_surface_output.hpp b/include/geode/io/mesh/internal/dxf_surface_output.hpp new file mode 100644 index 0000000..8b95c14 --- /dev/null +++ b/include/geode/io/mesh/internal/dxf_surface_output.hpp @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#pragma once + +#include + +#include + +namespace geode +{ + FORWARD_DECLARATION_DIMENSION_CLASS( SurfaceMesh ); + ALIAS_3D( SurfaceMesh ); +} // namespace geode + +namespace geode +{ + namespace internal + { + /*! + * Writes an ASCII DXF (R12) with one 3DFACE per polygon, on a layer + * named after the surface. A triangle is a 3DFACE whose fourth corner + * is the third one. Polygons with more than 4 vertices cannot be + * written: a data exception is raised before creating the file. + * + * The layer name is not strictly R12: R12 only allows up to 31 + * characters among A-Z 0-9 $ - _, whereas the surface name is kept + * (spaces, case, length, non-ASCII characters as \U+XXXX escapes), + * only characters forbidden by all DXF versions being replaced by '_'. + * Lenient readers, including the OpenGeode-IO DXF reader, accept it. + * A newer version (e.g. AC1015) is not declared: it requires handles + * and complete TABLES, BLOCKS and OBJECTS sections. + */ + void write_dxf_surface( + std::string_view filename, const SurfaceMesh3D& surface ); + } // namespace internal +} // namespace geode diff --git a/include/geode/io/mesh/internal/dxf_triangulated_output.hpp b/include/geode/io/mesh/internal/dxf_triangulated_output.hpp new file mode 100644 index 0000000..8028c9c --- /dev/null +++ b/include/geode/io/mesh/internal/dxf_triangulated_output.hpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#pragma once + +#include +#include + +#include + +namespace geode +{ + FORWARD_DECLARATION_DIMENSION_CLASS( TriangulatedSurface ); + ALIAS_3D( TriangulatedSurface ); +} // namespace geode + +namespace geode +{ + namespace internal + { + /*! + * Writes an ASCII DXF with one 3DFACE per triangle: see + * write_dxf_surface. + */ + class DXFTriangulatedOutput final + : public TriangulatedSurfaceOutput< 3 > + { + public: + explicit DXFTriangulatedOutput( std::string_view filename ) + : TriangulatedSurfaceOutput< 3 >( filename ) + { + } + + static std::string_view extension() + { + static constexpr auto EXT = "dxf"; + return EXT; + } + + std::vector< std::string > write( + const TriangulatedSurface3D &surface ) const final; + }; + } // namespace internal +} // namespace geode diff --git a/src/geode/io/mesh/CMakeLists.txt b/src/geode/io/mesh/CMakeLists.txt index 18aeaec..60c1c15 100644 --- a/src/geode/io/mesh/CMakeLists.txt +++ b/src/geode/io/mesh/CMakeLists.txt @@ -28,7 +28,11 @@ add_geode_library( "csv_input_helpers.cpp" "dot_polygonal_output.cpp" "dot_triangulated_output.cpp" + "dxf_common.cpp" "dxf_input.cpp" + "dxf_polygonal_output.cpp" + "dxf_surface_output.cpp" + "dxf_triangulated_output.cpp" "gexf_output.cpp" "obj_input.cpp" "obj_polygonal_output.cpp" @@ -97,7 +101,11 @@ add_geode_library( "internal/assimp_input.hpp" "internal/assimp_output.hpp" "internal/csv_input.hpp" + "internal/dxf_common.hpp" "internal/dxf_input.hpp" + "internal/dxf_polygonal_output.hpp" + "internal/dxf_surface_output.hpp" + "internal/dxf_triangulated_output.hpp" "internal/gexf_output.hpp" "internal/obj_input.hpp" "internal/obj_polygonal_output.hpp" diff --git a/src/geode/io/mesh/common.cpp b/src/geode/io/mesh/common.cpp index 5d8eba1..0aa634e 100644 --- a/src/geode/io/mesh/common.cpp +++ b/src/geode/io/mesh/common.cpp @@ -51,6 +51,8 @@ #include #include +#include +#include #include #include #include @@ -127,6 +129,9 @@ namespace geode::PolygonalSurfaceOutputFactory3D::register_creator< geode::internal::OBJPolygonalOutput >( geode::internal::OBJPolygonalOutput::extension().data() ); + geode::PolygonalSurfaceOutputFactory3D::register_creator< + geode::internal::DXFPolygonalOutput >( + geode::internal::DXFPolygonalOutput::extension().data() ); geode::PolygonalSurfaceOutputFactory2D::register_creator< geode::detail::VTPPolygonalOutput< 2 > >( geode::detail::VTPPolygonalOutput< 2 >::extension().data() ); @@ -149,6 +154,9 @@ namespace geode::TriangulatedSurfaceOutputFactory3D::register_creator< geode::internal::OBJTriangulatedOutput >( geode::internal::OBJTriangulatedOutput::extension().data() ); + geode::TriangulatedSurfaceOutputFactory3D::register_creator< + geode::internal::DXFTriangulatedOutput >( + geode::internal::DXFTriangulatedOutput::extension().data() ); geode::TriangulatedSurfaceOutputFactory2D::register_creator< geode::detail::VTPTriangulatedOutput< 2 > >( geode::detail::VTPTriangulatedOutput< 2 >::extension().data() ); diff --git a/src/geode/io/mesh/dxf_common.cpp b/src/geode/io/mesh/dxf_common.cpp new file mode 100644 index 0000000..684f21f --- /dev/null +++ b/src/geode/io/mesh/dxf_common.cpp @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +#include +#include + +#include + +namespace +{ + constexpr std::string_view ESCAPE{ "\\U+" }; + constexpr size_t NB_HEX_DIGITS{ 4 }; + constexpr char REPLACEMENT{ '_' }; + constexpr unsigned int MAX_ESCAPED_CODE_POINT{ 0xFFFF }; + constexpr unsigned int FIRST_MULTI_BYTE{ 0x80 }; + constexpr unsigned int FIRST_LEADING_BYTE{ 0xC0 }; + constexpr unsigned int FIRST_3_BYTES_LEADING_BYTE{ 0xE0 }; + constexpr unsigned int FIRST_4_BYTES_LEADING_BYTE{ 0xF0 }; + constexpr unsigned int CONTINUATION_BITS{ 0x3F }; + constexpr unsigned int FIRST_2_BYTES_CODE_POINT{ 0x80 }; + constexpr unsigned int FIRST_3_BYTES_CODE_POINT{ 0x800 }; + + size_t utf8_sequence_length( unsigned int leading_byte ) + { + if( leading_byte >= FIRST_4_BYTES_LEADING_BYTE ) + { + return 4; + } + if( leading_byte >= FIRST_3_BYTES_LEADING_BYTE ) + { + return 3; + } + if( leading_byte >= FIRST_LEADING_BYTE ) + { + return 2; + } + // Unexpected continuation byte + return 1; + } + + void append_utf8( std::string& output, unsigned int code_point ) + { + if( code_point < FIRST_2_BYTES_CODE_POINT ) + { + output.push_back( static_cast< char >( code_point ) ); + } + else if( code_point < FIRST_3_BYTES_CODE_POINT ) + { + 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 ) ) ); + } + } +} // namespace + +namespace geode +{ + namespace internal + { + std::string encode_unicode_escapes( std::string_view utf8 ) + { + std::string result; + result.reserve( utf8.size() ); + size_t position{ 0 }; + while( position < utf8.size() ) + { + const auto byte = + static_cast< unsigned char >( utf8[position] ); + if( byte < FIRST_MULTI_BYTE ) + { + result.push_back( utf8[position] ); + position++; + continue; + } + const auto length = utf8_sequence_length( byte ); + if( length == 1 || position + length > utf8.size() ) + { + result.push_back( REPLACEMENT ); + position++; + continue; + } + unsigned int code_point = byte & ( 0x7FU >> length ); + for( const auto i : Range{ 1, length } ) + { + code_point = + ( code_point << 6U ) + | ( static_cast< unsigned char >( utf8[position + i] ) + & CONTINUATION_BITS ); + } + if( code_point > MAX_ESCAPED_CODE_POINT ) + { + result.push_back( REPLACEMENT ); + } + else + { + absl::StrAppendFormat( &result, "\\U+%04X", code_point ); + } + position += length; + } + return result; + } + + std::string decode_unicode_escapes( std::string_view value ) + { + 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; + } + } // namespace internal +} // namespace geode diff --git a/src/geode/io/mesh/dxf_input.cpp b/src/geode/io/mesh/dxf_input.cpp index 2990a58..82790ae 100644 --- a/src/geode/io/mesh/dxf_input.cpp +++ b/src/geode/io/mesh/dxf_input.cpp @@ -50,6 +50,7 @@ #include #include +#include namespace { @@ -60,7 +61,6 @@ namespace 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 }; @@ -157,61 +157,6 @@ namespace } } - 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: @@ -386,19 +331,23 @@ namespace 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{}; + std::array< geode::Point3D, geode::internal::NB_3DFACE_CORNERS > + corners; + std::array< std::array< bool, 3 >, + geode::internal::NB_3DFACE_CORNERS > + given{}; for( const auto& group : entity.groups ) { if( group.code == 8 ) { - layer = decode_unicode_escapes( group.value ); + layer = + geode::internal::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 ) + if( corner < geode::internal::NB_3DFACE_CORNERS ) { corners[static_cast< size_t >( corner )].set_value( static_cast< geode::local_index_t >( axis ), @@ -414,7 +363,8 @@ namespace 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 }; + constexpr size_t LAST_CORNER{ geode::internal::NB_3DFACE_CORNERS + - 1 }; if( is_absent( given[LAST_CORNER] ) ) { // A triangle: the fourth corner is the third one @@ -606,10 +556,11 @@ namespace const auto it = xdata.find( application ); if( it != xdata.end() && !it->second.empty() ) { - return decode_unicode_escapes( it->second ); + return geode::internal::decode_unicode_escapes( + it->second ); } } - auto name = decode_unicode_escapes( layer ); + auto name = geode::internal::decode_unicode_escapes( layer ); if( handle.empty() ) { return name; diff --git a/src/geode/io/mesh/dxf_polygonal_output.cpp b/src/geode/io/mesh/dxf_polygonal_output.cpp new file mode 100644 index 0000000..b4c358c --- /dev/null +++ b/src/geode/io/mesh/dxf_polygonal_output.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +#include +#include + +#include + +#include + +namespace geode +{ + namespace internal + { + std::vector< std::string > DXFPolygonalOutput::write( + const PolygonalSurface3D& surface ) const + { + write_dxf_surface( filename(), surface ); + return { to_string( filename() ) }; + } + } // namespace internal +} // namespace geode diff --git a/src/geode/io/mesh/dxf_surface_output.cpp b/src/geode/io/mesh/dxf_surface_output.cpp new file mode 100644 index 0000000..2625a4a --- /dev/null +++ b/src/geode/io/mesh/dxf_surface_output.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include + +namespace +{ + constexpr std::string_view DEFAULT_LAYER{ "0" }; + constexpr std::string_view FORBIDDEN_LAYER_CHARACTERS{ "<>/\\\":;?*|=`" }; + constexpr unsigned char FIRST_PRINTABLE{ 0x20 }; + + /*! + * Layer names cannot contain some characters, which are replaced by '_'. + * Non-ASCII characters are written as \U+XXXX escapes. + * The R12 restrictions (31 characters among A-Z 0-9 $ - _) are not + * applied, to keep the surface name: see write_dxf_surface. + */ + std::string layer_name( std::string_view name ) + { + if( name.empty() ) + { + return std::string{ DEFAULT_LAYER }; + } + // Replaced before encoding: the '\' of the escapes is kept + std::string layer{ name }; + for( auto& character : layer ) + { + // Only ASCII characters match: UTF-8 sequences are kept + if( static_cast< unsigned char >( character ) < FIRST_PRINTABLE + || FORBIDDEN_LAYER_CHARACTERS.find( character ) + != std::string_view::npos ) + { + character = '_'; + } + } + return geode::internal::encode_unicode_escapes( layer ); + } + + class DXFSurfaceWriter + { + public: + DXFSurfaceWriter( + std::string_view filename, const geode::SurfaceMesh3D& mesh ) + : filename_{ filename }, mesh_( mesh ) + { + // Checked before creating the file: no partial file is left + for( const auto polygon : geode::Range{ mesh_.nb_polygons() } ) + { + const auto nb_vertices = mesh_.nb_polygon_vertices( polygon ); + geode::OpenGeodeIOMeshException::check_exception( + nb_vertices <= geode::internal::NB_3DFACE_CORNERS, nullptr, + geode::OpenGeodeException::TYPE::data, + "[DXFOutput] Cannot write ", filename_, ": polygon ", + polygon, " has ", nb_vertices, + " vertices, a 3DFACE has at most ", + geode::internal::NB_3DFACE_CORNERS ); + } + file_.open( geode::to_string( filename ) ); + geode::OpenGeodeIOMeshException::check_exception( file_.good(), + nullptr, geode::OpenGeodeException::TYPE::data, + "[DXFOutput] Error while opening file: ", filename_ ); + file_.imbue( std::locale::classic() ); + file_.precision( std::numeric_limits< double >::max_digits10 ); + } + + void write() + { + write_group( 0, "SECTION" ); + write_group( 2, "HEADER" ); + write_group( 9, "$ACADVER" ); + // R12, except for the layer names: see write_dxf_surface + write_group( 1, "AC1009" ); + write_group( 0, "ENDSEC" ); + write_group( 0, "SECTION" ); + write_group( 2, "ENTITIES" ); + const auto layer = layer_name( mesh_.name().value_or( "" ) ); + for( const auto polygon : geode::Range{ mesh_.nb_polygons() } ) + { + write_face( polygon, layer ); + } + write_group( 0, "ENDSEC" ); + write_group( 0, "EOF" ); + file_ << std::flush; + geode::OpenGeodeIOMeshException::check_exception( file_.good(), + nullptr, geode::OpenGeodeException::TYPE::data, + "[DXFOutput] Error while writing file: ", filename_ ); + } + + private: + template < typename Value > + void write_group( int code, const Value& value ) + { + file_ << std::setw( 3 ) << code << '\n' << value << '\n'; + } + + void write_face( geode::index_t polygon, const std::string& layer ) + { + write_group( 0, "3DFACE" ); + write_group( 8, layer ); + const auto nb_vertices = mesh_.nb_polygon_vertices( polygon ); + for( const auto corner : + geode::LRange{ geode::internal::NB_3DFACE_CORNERS } ) + { + // A triangle repeats its last vertex as fourth corner + const geode::local_index_t vertex = + corner < nb_vertices ? corner : nb_vertices - 1; + const auto& point = + mesh_.point( mesh_.polygon_vertex( { polygon, vertex } ) ); + for( const auto axis : geode::LRange{ 3 } ) + { + write_group( + 10 * ( axis + 1 ) + corner, point.value( axis ) ); + } + } + } + + private: + std::string_view filename_; + std::ofstream file_; + const geode::SurfaceMesh3D& mesh_; + }; +} // namespace + +namespace geode +{ + namespace internal + { + void write_dxf_surface( + std::string_view filename, const SurfaceMesh3D& surface ) + { + DXFSurfaceWriter writer{ filename, surface }; + writer.write(); + } + } // namespace internal +} // namespace geode diff --git a/src/geode/io/mesh/dxf_triangulated_output.cpp b/src/geode/io/mesh/dxf_triangulated_output.cpp new file mode 100644 index 0000000..34d4628 --- /dev/null +++ b/src/geode/io/mesh/dxf_triangulated_output.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 - 2026 Geode-solutions + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +#include + +#include +#include + +#include + +#include + +namespace geode +{ + namespace internal + { + std::vector< std::string > DXFTriangulatedOutput::write( + const TriangulatedSurface3D& surface ) const + { + write_dxf_surface( filename(), surface ); + return { to_string( filename() ) }; + } + } // namespace internal +} // namespace geode diff --git a/tests/data/galeries_and_cylinder.og_tsf3d b/tests/data/galeries_and_cylinder.og_tsf3d new file mode 100644 index 0000000..c168823 Binary files /dev/null and b/tests/data/galeries_and_cylinder.og_tsf3d differ diff --git a/tests/mesh/test-dxf.cpp b/tests/mesh/test-dxf.cpp index 9f050a9..56d3c3a 100644 --- a/tests/mesh/test-dxf.cpp +++ b/tests/mesh/test-dxf.cpp @@ -39,9 +39,14 @@ #include +#include +#include #include +#include #include #include +#include +#include #include @@ -476,6 +481,161 @@ namespace !WarningRecorder::has_warning( "VIEWPORT" ), "Skipped VIEWPORT should not be reported" ); } + + void check_same_triangles( const geode::TriangulatedSurface3D& expected, + const geode::PolygonalSurface3D& reloaded ) + { + geode::OpenGeodeIOMeshException::test( + reloaded.nb_polygons() == expected.nb_polygons(), + "Wrong number of reloaded triangles: ", reloaded.nb_polygons() ); + for( const auto p : geode::Range{ expected.nb_polygons() } ) + { + geode::OpenGeodeIOMeshException::test( + reloaded.nb_polygon_vertices( p ) == 3, "Reloaded polygon ", p, + " is not a triangle" ); + for( const auto v : geode::LRange{ 3 } ) + { + geode::OpenGeodeIOMeshException::test( + reloaded.point( reloaded.polygon_vertex( { p, v } ) ) + .inexact_equal( expected.point( + expected.polygon_vertex( { p, v } ) ) ), + "Wrong position of vertex ", v, " of reloaded triangle ", + p ); + } + } + } + + void test_triangulated_output() + { + const auto filename = + absl::StrCat( geode::DATA_PATH, "galeries_and_cylinder.og_tsf3d" ); + const auto surface = geode::load_one( + geode::load_triangulated_surface< 3 >( filename ), filename ); + const std::string output{ "galeries_and_cylinder.dxf" }; + geode::save_triangulated_surface( *surface, output ); + + const auto reloaded = geode::load_one( + geode::load_polygonal_surface< 3 >( output ), output ); + geode::OpenGeodeIOMeshException::test( + reloaded->nb_vertices() == surface->nb_vertices(), + "Wrong number of reloaded vertices: ", reloaded->nb_vertices(), + " instead of ", surface->nb_vertices() ); + check_same_triangles( *surface, *reloaded ); + } + + void test_triangulated_output_precision_and_name() + { + auto surface = geode::TriangulatedSurface3D::create(); + auto builder = geode::TriangulatedSurfaceBuilder3D::create( *surface ); + // Forbidden characters in layer names are replaced, others kept + builder->set_name( "Galerie \xC3\xA9:1" ); + builder->create_point( + geode::Point3D{ { 650000.123456789, 4100000.987654321, -0.1 } } ); + builder->create_point( + geode::Point3D{ { 650010.321, 4100000.654, 100.987 } } ); + builder->create_point( + geode::Point3D{ { 650000.111, 4100010.222, 101.333 } } ); + builder->create_point( + geode::Point3D{ { 650010.5, 4100010.5, 1e-7 } } ); + builder->create_triangle( { 0, 1, 2 } ); + builder->create_triangle( { 2, 1, 3 } ); + const std::string output{ "triangulated_output.dxf" }; + geode::save_triangulated_surface( *surface, output ); + + const auto reloaded = geode::load_one( + geode::load_polygonal_surface< 3 >( output ), output ); + check_surface( *reloaded, "Galerie \xC3\xA9_1", 4, 2 ); + check_same_triangles( *surface, *reloaded ); + for( const auto v : geode::Range{ surface->nb_vertices() } ) + { + bool found{ false }; + for( const auto r : geode::Range{ reloaded->nb_vertices() } ) + { + found = found || reloaded->point( r ) == surface->point( v ); + } + geode::OpenGeodeIOMeshException::test( found, "Point ", + surface->point( v ).string(), + " lost precision in DXF round trip" ); + } + check_adjacency( *reloaded, { 0, 1 }, 1 ); + } + + void test_polygonal_output() + { + auto surface = geode::PolygonalSurface3D::create(); + auto builder = geode::PolygonalSurfaceBuilder3D::create( *surface ); + // 3-byte UTF-8 character: encoded by the writer, decoded by the reader + builder->set_name( "Baie \xE2\x82\xAC" ); + const std::array< geode::Point3D, 5 > points{ { + geode::Point3D{ { 0, 0, 0 } }, + geode::Point3D{ { 1, 0, 0.5 } }, + geode::Point3D{ { 1, 1, 0.5 } }, + geode::Point3D{ { 0, 1, 0 } }, + geode::Point3D{ { 2, 0.5, 1 } }, + } }; + for( const auto& point : points ) + { + builder->create_point( point ); + } + builder->create_polygon( { 0, 1, 2, 3 } ); + builder->create_polygon( { 1, 4, 2 } ); + const std::string output{ "polygonal_output.dxf" }; + geode::save_polygonal_surface( *surface, output ); + + const auto reloaded = geode::load_one( + geode::load_polygonal_surface< 3 >( output ), output ); + check_surface( *reloaded, "Baie \xE2\x82\xAC", 5, 2 ); + for( const auto p : geode::Range{ surface->nb_polygons() } ) + { + const auto nb_vertices = surface->nb_polygon_vertices( p ); + geode::OpenGeodeIOMeshException::test( + reloaded->nb_polygon_vertices( p ) == nb_vertices, + "Wrong size of reloaded polygon ", p ); + for( const auto v : geode::LRange{ nb_vertices } ) + { + geode::OpenGeodeIOMeshException::test( + reloaded->point( reloaded->polygon_vertex( { p, v } ) ) + == surface->point( + surface->polygon_vertex( { p, v } ) ), + "Wrong position of vertex ", v, " of reloaded polygon ", + p ); + } + } + check_adjacency( *reloaded, { 0, 1 }, 1 ); + check_adjacency( *reloaded, { 1, 2 }, 0 ); + } + + void test_polygonal_output_too_many_vertices() + { + auto surface = geode::PolygonalSurface3D::create(); + auto builder = geode::PolygonalSurfaceBuilder3D::create( *surface ); + builder->create_point( geode::Point3D{ { 0, 0, 0 } } ); + builder->create_point( geode::Point3D{ { 1, 0, 0 } } ); + builder->create_point( geode::Point3D{ { 2, 1, 0 } } ); + builder->create_point( geode::Point3D{ { 1, 2, 0 } } ); + builder->create_point( geode::Point3D{ { 0, 1, 0 } } ); + builder->create_polygon( { 0, 1, 2, 3, 4 } ); + const std::string output{ "pentagon.dxf" }; + try + { + geode::save_polygonal_surface( *surface, output ); + } + catch( const geode::OpenGeodeException& exception ) + { + geode::OpenGeodeIOMeshException::test( + exception.type() == geode::OpenGeodeException::TYPE::data + && absl::StrContains( + exception.what(), "a 3DFACE has at most 4" ), + "Wrong error saving a pentagon: ", exception.what() ); + geode::OpenGeodeIOMeshException::test( + !std::ifstream{ output }.good(), + "No file should be written for a pentagon" ); + return; + } + throw geode::OpenGeodeIOMeshException{ nullptr, + geode::OpenGeodeException::TYPE::internal, + "Saving a pentagon as DXF should raise a data error" }; + } } // namespace int main() @@ -496,6 +656,10 @@ int main() test_comment_before_entities(); test_unicode_layer_names(); test_viewport_not_reported(); + test_triangulated_output(); + test_triangulated_output_precision_and_name(); + test_polygonal_output(); + test_polygonal_output_too_many_vertices(); geode::Logger::info( "TEST SUCCESS" ); return 0;