From 73837c2f82f66af0ba382d857f1c67f5bf60ab36 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Tue, 26 Apr 2022 11:30:55 +0200 Subject: [PATCH] Add more comments --- src/libslic3r/Format/SL1_SVG.cpp | 17 ++++++++++++++++- src/libslic3r/Format/SLAArchiveReader.cpp | 10 +++++++++- src/libslic3r/Format/ZipperArchiveImport.cpp | 7 +++++-- src/libslic3r/Format/ZipperArchiveImport.hpp | 13 ++++++++++++- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/libslic3r/Format/SL1_SVG.cpp b/src/libslic3r/Format/SL1_SVG.cpp index 127c9f9c9..b67901814 100644 --- a/src/libslic3r/Format/SL1_SVG.cpp +++ b/src/libslic3r/Format/SL1_SVG.cpp @@ -20,6 +20,7 @@ namespace { size_t constexpr coord_t_bufsize = 40; +// A fast and locale independent implementation of int=>str char const* decimal_from(coord_t snumber, char* buffer) { std::make_unsigned_t number = 0; @@ -54,6 +55,7 @@ inline std::string coord2str(coord_t crd) return decimal_from(crd, buf); } +// Apply the sla::RasterBase::Trafo onto an ExPolygon void transform(ExPolygon &ep, const sla::RasterBase::Trafo &tr, const BoundingBox &bb) { if (tr.flipXY) { @@ -75,6 +77,7 @@ void transform(ExPolygon &ep, const sla::RasterBase::Trafo &tr, const BoundingBo } } +// Append the svg string representation of a Polygon to the input 'buf' void append_svg(std::string &buf, const Polygon &poly) { if (poly.points.empty()) @@ -173,6 +176,8 @@ public: Trafo trafo() const override { return m_trafo; } + // The encoder is ignored here, the svg text does not need any further + // encoding. sla::EncodedRaster encode(sla::RasterEncoder /*encoder*/) const override { std::vector data; @@ -220,6 +225,7 @@ std::unique_ptr SL1_SVGArchive::create_raster() const return std::make_unique(svgarea, sla::Resolution{res_x, res_y}, tr); } +// SVG does not need additional binary encoding. sla::RasterEncoder SL1_SVGArchive::get_encoder() const { return nullptr; @@ -230,6 +236,9 @@ void SL1_SVGArchive::export_print(const std::string fname, const ThumbnailsList &thumbnails, const std::string &projectname) { + // Export code is completely identical to SL1, only the compression level + // is elevated, as the SL1 has already compressed PNGs with deflate, + // but the svg is just text. Zipper zipper{fname, Zipper::TIGHT_COMPRESSION}; SL1Archive::export_print(zipper, print, thumbnails, projectname); @@ -268,6 +277,8 @@ ConfigSubstitutions SL1_SVGReader::read(std::vector &slices, st.stop = !m_progr(int(curr)); } + // Don't want to use dirty casts for the buffer to be usable in + // the NanoSVGParser until performance is not a bottleneck here. auto svgtxt = reserve_vector(entry.buf.size()); std::copy(entry.buf.begin(), entry.buf.end(), std::back_inserter(svgtxt)); NanoSVGParser svgp(svgtxt.data()); @@ -284,7 +295,11 @@ ConfigSubstitutions SL1_SVGReader::read(std::vector &slices, } } - ExPolygons expolys = union_ex(polys); + // Create the slice from the read polygons. Here, the fill rule has to + // be the same as stated in the svg file which is `nonzero` when exported + // using SL1_SVGArchive. Would be better to parse it from the svg file, + // but if it's different, the file is probably corrupted anyways. + ExPolygons expolys = union_ex(polys, ClipperLib::pftNonZero); invert_raster_trafo(expolys, rstp.trafo, rstp.width, rstp.height); slices.emplace_back(expolys); } diff --git a/src/libslic3r/Format/SLAArchiveReader.cpp b/src/libslic3r/Format/SLAArchiveReader.cpp index cddc9ec31..e62b7e061 100644 --- a/src/libslic3r/Format/SLAArchiveReader.cpp +++ b/src/libslic3r/Format/SLAArchiveReader.cpp @@ -16,17 +16,20 @@ namespace Slic3r { namespace { +// Factory function that returns an implementation of SLAArchiveReader. using ArchiveFactory = std::function< std::unique_ptr(const std::string &fname, SLAImportQuality quality, const ProgrFn & progr)>; +// Entry in the global registry of readable archive formats. struct ArchiveEntry { const char *descr; std::vector extensions; ArchiveFactory factoryfn; }; +// This is where the readable archive formats are registered. static const std::map REGISTERED_ARCHIVES { { "SL1", @@ -38,7 +41,7 @@ static const std::map REGISTERED_ARCHIVES { { L("SL2 archive files"), {"sl2", "sl1_svg", "zip"}, [] (const std::string &fname, SLAImportQuality quality, const ProgrFn &progr) { return std::make_unique(fname, quality, progr); }} }, - // TODO: pwmx + // TODO: pwmx and future others. }; } // namespace @@ -48,6 +51,11 @@ std::unique_ptr SLAArchiveReader::create( SLAImportQuality quality, const ProgrFn & progr) { + // Create an instance of SLAArchiveReader using the registered archive + // reader implementations. Only checking the file extension and comparing + // with the registered readers advertised extensions. + // The first match will be used. + std::string ext = boost::filesystem::path(fname).extension().string(); boost::algorithm::to_lower(ext); diff --git a/src/libslic3r/Format/ZipperArchiveImport.cpp b/src/libslic3r/Format/ZipperArchiveImport.cpp index 526e89df3..5b0ecff6e 100644 --- a/src/libslic3r/Format/ZipperArchiveImport.cpp +++ b/src/libslic3r/Format/ZipperArchiveImport.cpp @@ -12,6 +12,7 @@ namespace Slic3r { namespace { +// Read an ini file into boost property tree boost::property_tree::ptree read_ini(const mz_zip_archive_file_stat &entry, MZ_Archive &zip) { @@ -27,6 +28,7 @@ boost::property_tree::ptree read_ini(const mz_zip_archive_file_stat &entry, return tree; } +// Read an arbitrary file into EntryBuffer EntryBuffer read_entry(const mz_zip_archive_file_stat &entry, MZ_Archive &zip, const std::string &name) @@ -122,8 +124,9 @@ std::pair extract_profile( } } - // If the archive contains an empty profile, use the one that was passed as output argument - // then replace it with the readed profile to report that it was empty. + // If the archive contains an empty profile, use the one that was passed + // as output argument then replace it with the readed profile to report + // that it was empty. profile_use = profile_in.empty() ? profile_out : profile_in; profile_out = profile_in; diff --git a/src/libslic3r/Format/ZipperArchiveImport.hpp b/src/libslic3r/Format/ZipperArchiveImport.hpp index 105de4753..adddcacd4 100644 --- a/src/libslic3r/Format/ZipperArchiveImport.hpp +++ b/src/libslic3r/Format/ZipperArchiveImport.hpp @@ -11,26 +11,37 @@ namespace Slic3r { +// Buffer for arbitraryfiles inside a zipper archive. struct EntryBuffer { std::vector buf; std::string fname; }; +// Structure holding the data read from a zipper archive. struct ZipperArchive { boost::property_tree::ptree profile, config; std::vector entries; }; +// Names of the files containing metadata inside the archive. const constexpr char *CONFIG_FNAME = "config.ini"; const constexpr char *PROFILE_FNAME = "prusaslicer.ini"; +// Read an archive that was written using the Zipper class. +// The includes parameter is a set of file name substrings that the entries +// must contain to be included in ZipperArchive. +// The excludes parameter may contain substrings that filenames must not +// contain. +// Every file in the archive is read into ZipperArchive::entries +// except the files CONFIG_FNAME, and PROFILE_FNAME which are read into +// ZipperArchive::config and ZipperArchive::profile structures. ZipperArchive read_zipper_archive(const std::string &zipfname, const std::vector &includes, const std::vector &excludes); -// Extract the print profile form the archive onto 'out'. +// Extract the print profile form the archive into 'out'. // Returns a profile that has correct parameters to use for model reconstruction // even if the needed parameters were not fully found in the archive's metadata. // The inout argument shall be a usable fallback profile if the archive