From 44a6e5661daf6fa71074b76778926c96cf80c7d7 Mon Sep 17 00:00:00 2001 From: Pierre Moulon Date: Wed, 1 Feb 2023 21:18:21 -0800 Subject: [PATCH] [SVG] Refactor svgDrawer #1658 --- src/openMVG/features/sift/sift_test.cpp | 2 +- src/openMVG/matching/svg_matches.cpp | 2 +- .../multiview/translation_averaging_test.hpp | 2 +- src/openMVG/numeric/lm_test.cpp | 2 +- .../robust_estimator_ACRansac_test.cpp | 2 +- src/openMVG/sfm/sfm_report.cpp | 2 +- .../features_kvld_filter/kvld_filter.cpp | 2 +- .../main_pano_converter.cpp | 2 +- .../robust_estimation_tutorial_homography.cpp | 2 +- .../robust_fundamental_guided.cpp | 2 +- .../robust_homography_guided.cpp | 2 +- src/software/SfM/main_ComputeVLAD.cpp | 7 +- src/software/SfM/main_ListMatchingPairs.cpp | 3 +- src/third_party/vectorGraphics/svgDrawer.hpp | 265 ------------------ 14 files changed, 16 insertions(+), 281 deletions(-) delete mode 100644 src/third_party/vectorGraphics/svgDrawer.hpp diff --git a/src/openMVG/features/sift/sift_test.cpp b/src/openMVG/features/sift/sift_test.cpp index 26e5d1fa79..0484f05a58 100644 --- a/src/openMVG/features/sift/sift_test.cpp +++ b/src/openMVG/features/sift/sift_test.cpp @@ -10,7 +10,7 @@ #include "openMVG/features/sift/SIFT_Anatomy_Image_Describer.hpp" #include "openMVG/image/image_io.hpp" #include "openMVG/system/timer.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "testing/testing.h" diff --git a/src/openMVG/matching/svg_matches.cpp b/src/openMVG/matching/svg_matches.cpp index a60bda3afc..7500bf0197 100644 --- a/src/openMVG/matching/svg_matches.cpp +++ b/src/openMVG/matching/svg_matches.cpp @@ -8,7 +8,7 @@ #include #include -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" namespace openMVG { namespace matching { diff --git a/src/openMVG/multiview/translation_averaging_test.hpp b/src/openMVG/multiview/translation_averaging_test.hpp index 567e5f230f..872d6045d0 100644 --- a/src/openMVG/multiview/translation_averaging_test.hpp +++ b/src/openMVG/multiview/translation_averaging_test.hpp @@ -20,9 +20,9 @@ #include "openMVG/multiview/test_data_sets.hpp" #include "openMVG/multiview/translation_averaging_common.hpp" #include "openMVG/numeric/numeric.h" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "testing/testing.h" -#include "openMVG/vector_graphics/svgDrawer.hpp" int modifiedMod ( diff --git a/src/openMVG/numeric/lm_test.cpp b/src/openMVG/numeric/lm_test.cpp index bb9a4509fa..c52d381948 100644 --- a/src/openMVG/numeric/lm_test.cpp +++ b/src/openMVG/numeric/lm_test.cpp @@ -8,9 +8,9 @@ #include "openMVG/numeric/numeric.h" #include "openMVG/numeric/lm.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "testing/testing.h" -#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/openMVG/robust_estimation/robust_estimator_ACRansac_test.cpp b/src/openMVG/robust_estimation/robust_estimator_ACRansac_test.cpp index 352c8ebebb..0b57688af8 100644 --- a/src/openMVG/robust_estimation/robust_estimator_ACRansac_test.cpp +++ b/src/openMVG/robust_estimation/robust_estimator_ACRansac_test.cpp @@ -10,9 +10,9 @@ #include "openMVG/robust_estimation/robust_estimator_ACRansac.hpp" #include "openMVG/robust_estimation/robust_estimator_lineKernel_test.hpp" #include "openMVG/numeric/extract_columns.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "testing/testing.h" -#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/openMVG/sfm/sfm_report.cpp b/src/openMVG/sfm/sfm_report.cpp index bf1fb65a06..06f5a37ff0 100644 --- a/src/openMVG/sfm/sfm_report.cpp +++ b/src/openMVG/sfm/sfm_report.cpp @@ -14,11 +14,11 @@ #include #include "openMVG/sfm/sfm_data.hpp" +#include "openMVG/vector_graphics/svgHistogram.hpp" #include "third_party/histogram/histogram.hpp" #include "third_party/htmlDoc/htmlDoc.hpp" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgHistogram.hpp" namespace openMVG { namespace sfm { diff --git a/src/openMVG_Samples/features_kvld_filter/kvld_filter.cpp b/src/openMVG_Samples/features_kvld_filter/kvld_filter.cpp index 91406947ce..f34573d2a6 100644 --- a/src/openMVG_Samples/features_kvld_filter/kvld_filter.cpp +++ b/src/openMVG_Samples/features_kvld_filter/kvld_filter.cpp @@ -17,7 +17,7 @@ #include "third_party/cmdLine/cmdLine.h" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/openMVG_Samples/image_spherical_to_pinholes/main_pano_converter.cpp b/src/openMVG_Samples/image_spherical_to_pinholes/main_pano_converter.cpp index 200734ecfc..0d1095b29b 100644 --- a/src/openMVG_Samples/image_spherical_to_pinholes/main_pano_converter.cpp +++ b/src/openMVG_Samples/image_spherical_to_pinholes/main_pano_converter.cpp @@ -11,7 +11,7 @@ #include "third_party/cmdLine/cmdLine.h" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/openMVG_Samples/multiview_robust_estimation_tutorial/robust_estimation_tutorial_homography.cpp b/src/openMVG_Samples/multiview_robust_estimation_tutorial/robust_estimation_tutorial_homography.cpp index 0650d21dab..244a03c963 100644 --- a/src/openMVG_Samples/multiview_robust_estimation_tutorial/robust_estimation_tutorial_homography.cpp +++ b/src/openMVG_Samples/multiview_robust_estimation_tutorial/robust_estimation_tutorial_homography.cpp @@ -33,7 +33,7 @@ #include "openMVG/robust_estimation/robust_estimator_ACRansacKernelAdaptator.hpp" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/openMVG_Samples/multiview_robust_fundamental_guided/robust_fundamental_guided.cpp b/src/openMVG_Samples/multiview_robust_fundamental_guided/robust_fundamental_guided.cpp index b897512821..5c12e7f244 100644 --- a/src/openMVG_Samples/multiview_robust_fundamental_guided/robust_fundamental_guided.cpp +++ b/src/openMVG_Samples/multiview_robust_fundamental_guided/robust_fundamental_guided.cpp @@ -19,7 +19,7 @@ #include "openMVG/robust_estimation/robust_estimator_ACRansac.hpp" #include "openMVG/robust_estimation/robust_estimator_ACRansacKernelAdaptator.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" #include diff --git a/src/openMVG_Samples/multiview_robust_homography_guided/robust_homography_guided.cpp b/src/openMVG_Samples/multiview_robust_homography_guided/robust_homography_guided.cpp index 16f02be24d..ea5d438dd4 100644 --- a/src/openMVG_Samples/multiview_robust_homography_guided/robust_homography_guided.cpp +++ b/src/openMVG_Samples/multiview_robust_homography_guided/robust_homography_guided.cpp @@ -19,10 +19,10 @@ #include "openMVG/robust_estimation/robust_estimator_ACRansac.hpp" #include "openMVG/robust_estimation/robust_estimator_ACRansacKernelAdaptator.hpp" #include "openMVG/types.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "third_party/cmdLine/cmdLine.h" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/software/SfM/main_ComputeVLAD.cpp b/src/software/SfM/main_ComputeVLAD.cpp index 5c2d3a5b75..9cd7ba7c0a 100644 --- a/src/software/SfM/main_ComputeVLAD.cpp +++ b/src/software/SfM/main_ComputeVLAD.cpp @@ -21,10 +21,11 @@ #include "openMVG/stl/stl.hpp" #include "openMVG/system/loggerprogress.hpp" #include "openMVG/system/timer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "third_party/cmdLine/cmdLine.h" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "third_party/vectorGraphics/svgDrawer.hpp" + #include #include @@ -62,7 +63,7 @@ void saveRetrievalMatrix( const std::string ref_view_filename = stlplus::create_filespec(sfm_data.s_root_path, ref_view->s_Img_path); - svg_stream.drawImage(ref_view_filename, size, size, x_offset * size, + svg_stream << svg::drawImage(ref_view_filename, size, size, x_offset * size, y_offset * size); const auto &retrieval_list = result_it.second; @@ -74,7 +75,7 @@ void saveRetrievalMatrix( const std::string found_view_filename = stlplus::create_filespec( sfm_data.s_root_path, found_view->s_Img_path); - svg_stream.drawImage(found_view_filename, size, size, x_offset * size, + svg_stream << svg::drawImage(found_view_filename, size, size, x_offset * size, y_offset * size); } x_offset = 0; diff --git a/src/software/SfM/main_ListMatchingPairs.cpp b/src/software/SfM/main_ListMatchingPairs.cpp index 23ff22494d..caf5d22df8 100644 --- a/src/software/SfM/main_ListMatchingPairs.cpp +++ b/src/software/SfM/main_ListMatchingPairs.cpp @@ -11,12 +11,11 @@ #include "openMVG/sfm/sfm_data.hpp" #include "openMVG/sfm/sfm_data_io.hpp" #include "openMVG/system/loggerprogress.hpp" - #include "openMVG/system/timer.hpp" +#include "openMVG/vector_graphics/svgDrawer.hpp" #include "third_party/cmdLine/cmdLine.h" #include "third_party/stlplus3/filesystemSimplified/file_system.hpp" -#include "openMVG/vector_graphics/svgDrawer.hpp" #include #include diff --git a/src/third_party/vectorGraphics/svgDrawer.hpp b/src/third_party/vectorGraphics/svgDrawer.hpp deleted file mode 100644 index 1207b39365..0000000000 --- a/src/third_party/vectorGraphics/svgDrawer.hpp +++ /dev/null @@ -1,265 +0,0 @@ - -/** - * @file svgDrawer.hpp - * @brief Simple svg drawing from C++ - * @author Pierre MOULON - * - * Copyright (c) 2011, 2012, 2013 Pierre MOULON - * All rights reserved. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ - -#ifndef THE_FRENCH_LEAF_SVG_DRAWER_HPP -#define THE_FRENCH_LEAF_SVG_DRAWER_HPP - -#include -#include -#include -#include -#include - -namespace svg { - -/// Basic svg style -class svgStyle -{ - std::string sFillCol_, sStrokeCol_, sToolTip_; - float strokeW_; - float opacity_; // Must be in the range [0; 1] -public: - - svgStyle():sFillCol_(""), sStrokeCol_("black"), sToolTip_(""), - strokeW_(1.0f), opacity_(-1.0) {} - - // Configure fill color - svgStyle & fill(const std::string & sFillCol) - { sFillCol_ = sFillCol; return * this;} - - // Configure stroke color and width - svgStyle & stroke(const std::string & sStrokeCol, float strokeWitdh = 1.f) - { sStrokeCol_ = sStrokeCol; strokeW_ = strokeWitdh; return * this;} - - // Configure with no stroke - svgStyle & noStroke() - { sStrokeCol_ = ""; strokeW_ = 0.f; return * this;} - - // Configure tooltip - svgStyle & tooltip(const std::string & sTooltip) - { sToolTip_ = sTooltip; return * this;} - - svgStyle & opacity(const float & opacity) - { opacity_ = opacity; return *this; } - - const std::string getSvgStream() const { - std::ostringstream os; - - if (!sStrokeCol_.empty()) - os << " stroke=\"" << sStrokeCol_ << "\" stroke-width=\"" << strokeW_ << "\""; - if (sFillCol_.empty()) - os << " fill=\"none\""; - else - os << " fill=\"" << sFillCol_ << "\""; - if (opacity_ > 0) - os << " opacity=\"" << opacity_ << "\""; - if (!sToolTip_.empty()) - os << " tooltip=\"enable\">" - << "" << sToolTip_ << ""; - - return os.str(); - } - - bool bTooltip() const { return !sToolTip_.empty();} -}; - - -/// Basic class to handle simple SVG draw. -/// You can draw line, square, rectangle, text and image (xlink) -class svgDrawer{ -public: - ///Constructor - svgDrawer(size_t W = 0, size_t H = 0) - { - svgStream_ << "\n"; - - svgStream_ << "" << std::endl - << " 0 && H > 0) - svgStream_ <<"width=\"" << W << "px\" height=\""<< H << "px\"" - << " preserveAspectRatio=\"xMinYMin meet\"" - << " viewBox=\"0 0 " << W << ' ' << H <<"\""; - - svgStream_ <<" version=\"1.1\">" << std::endl; - } - ///Circle draw -> x,y position and radius - void drawCircle(float cx, float cy, float r, - const svgStyle & style) - { - svgStream_ << "" : "/>\n"); } - - ///Line draw -> start and end point - void drawLine(float ax, float ay, float bx, float by, - const svgStyle & style) - { - svgStream_ << - "" : "/>\n"); - } - - ///Reference to an image (path must be relative to the svg file) - void drawImage(const std::string & simagePath, int W, int H, - int posx = 0, int posy = 0, float opacity =1.f) - { - svgStream_ << - "\n"; - } - - ///Square draw -> x,y position and size - void drawSquare(float cx, float cy, float W, - const svgStyle & style) - { - drawRectangle(cx, cy, W, W, style); - } - - ///Circle draw -> x,y position and width and height - void drawRectangle(float cx, float cy, float W, float H, - const svgStyle & style) - { - svgStream_ << "" : "/>\n"); - } - - ///Text display -> x,y position, font size - void drawText(float cx, float cy, float fontSize = 1.0f, const std::string & stext ="", - const std::string & scol = "") - { - svgStream_ << "" << stext << "\n"; - } - - template< typename DataInputIteratorX, typename DataInputIteratorY> - void drawPolyline(DataInputIteratorX xStart, DataInputIteratorX xEnd, - DataInputIteratorY yStart, DataInputIteratorY yEnd, - const svgStyle & style) - { - svgStream_ << "" : "/>\n"); - } - - template< typename DataInputIteratorXY> - void drawPolyline(DataInputIteratorXY points, - const svgStyle & style) - { - svgStream_ << "(svgStream_, ",")); - - svgStream_ << "\"" - << style.getSvgStream() + (style.bTooltip() ? "" : "/>\n"); - } - - ///Close the svg tag. - std::ostringstream & closeSvgFile() - { - svgStream_ <<""; - return svgStream_; - } -private: - std::ostringstream svgStream_; -}; - -/// Helper to draw a SVG histogram -/// ____ -/// | | ___ | -/// | |__| | | -/// | | | | | -/// -----------| -struct svgHisto -{ - template - static std::string stringifier(const T & t) - { - std::ostringstream os; - os << t; - return os.str(); - } - template - void draw(const std::vector & vec_value, - const std::pair & range, - const std::string & sFilename, - const float W, const float H) - { - if (vec_value.empty()) { - return; - } - //-- Max value - const T maxi = *max_element(vec_value.begin(), vec_value.end()); - const size_t n = vec_value.size(); - - const float scaleFactorY = H / static_cast(maxi); - const float scaleFactorX = W / static_cast(n); - - svgDrawer svgStream; - - for (size_t i = 0; i < vec_value.size(); ++i) - { - const size_t dist = i; - const T val = vec_value[i]; - std::ostringstream os; - os << '(' << range.first + dist/float(n) * (range.second-range.first) << ',' << val << ')'; - svgStyle style = svgStyle().fill("blue").stroke("black", 1.0).tooltip(os.str()); - svgStream.drawRectangle( - scaleFactorX * dist, H-val * scaleFactorY, - scaleFactorX, val * scaleFactorY, - style); - //_________ - //| |_________ - //| || | - //| || | - //| || | - //0 sFactorX 2*sFactorX - } - svgStyle styleAxis = svgStyle().stroke("black", 1.0f); - // Draw X Axis - svgStream.drawText(.05f*W, 1.2f*H, .1f*H, stringifier(range.first), "black"); - svgStream.drawText( W, 1.2f*H, .1f*H, stringifier(range.second), "black"); - svgStream.drawLine(0, 1.1f*H, W, 1.1f*H, styleAxis); - // Draw Y Axis - svgStream.drawText(1.2f*W, .1f*H, .1f*H, stringifier(maxi), "black"); - svgStream.drawText(1.2f*W, H, .1f*H, "0", "black"); - svgStream.drawLine(1.1f*W, 0, 1.1f*W, H, styleAxis); - - std::ofstream svgFileStream( sFilename.c_str()); - svgFileStream << svgStream.closeSvgFile().str(); - } -}; - -} // namespace svg - -#endif // THE_FRENCH_LEAF_SVG_DRAWER_HPP