From 315083cd52179509a663e60778a898e18cabec01 Mon Sep 17 00:00:00 2001 From: "yang.yongquan" <3395816735@qq.com> Date: Tue, 14 Feb 2023 18:10:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86QPainterPath?= =?UTF-8?q?=E7=9A=84=E5=8D=95=E8=B0=83=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchitectureColoredPainting.vcxproj | 2 - ...rchitectureColoredPainting.vcxproj.filters | 6 -- .../src/Editor/util/PainterPathUtil.cpp | 22 ++++++ .../src/Editor/util/PainterPathUtil.h | 1 + .../Renderer/Painting/CubicMonotonization.cpp | 77 ------------------- .../Renderer/Painting/CubicMonotonization.h | 23 ------ .../src/Renderer/Painting/Line.h | 5 ++ .../src/Renderer/Painting/LineTree.h | 2 +- 8 files changed, 29 insertions(+), 109 deletions(-) delete mode 100644 ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.cpp delete mode 100644 ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.h diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index 95a0438..6aebcea 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -127,7 +127,6 @@ - @@ -215,7 +214,6 @@ - diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters index ea62120..6da4337 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -129,9 +129,6 @@ Source Files\Renderer\Painting - - Source Files\Renderer\Painting - Source Files\Renderer\Painting @@ -360,9 +357,6 @@ Header Files\Renderer\Painting - - Header Files\Renderer\Painting - Header Files\Renderer\Painting diff --git a/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.cpp b/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.cpp index 148906e..4569c12 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.cpp +++ b/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.cpp @@ -1,8 +1,11 @@ #include "PainterPathUtil.h" #include +#include "../../Renderer/Painting/LineTree.h" using Renderer::Point; +using Renderer::Line; using std::vector; +using std::shared_ptr; vector > PainterPathUtil::transformToLines(QPainterPath& painterPath) { vector line; line.clear(); @@ -39,3 +42,22 @@ vector > PainterPathUtil::transformToLines(QPainterPath& painterP line.clear(); return lines; } + +QPainterPath PainterPathUtil::monotonization(QPainterPath& painterPath) { + QPainterPath resPath; + vector > lines = transformToLines(painterPath); + vector> linePtrVector; + Renderer::LineTree::monotonization(lines, linePtrVector); + for (auto linePtr : linePtrVector) { + vector line = linePtr->toPointVector(); + if (line.size() == 2) { + resPath.moveTo(line[0]); + resPath.lineTo(line[1]); + } + if (line.size() == 4) { + resPath.moveTo(line[0]); + resPath.cubicTo(line[1], line[2], line[3]); + } + } + return resPath; +} diff --git a/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.h b/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.h index 3e1fd13..0e045c4 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.h +++ b/ArchitectureColoredPainting/src/Editor/util/PainterPathUtil.h @@ -9,5 +9,6 @@ class PainterPathUtil { public: static std::vector > transformToLines(QPainterPath& painterPath); + static QPainterPath monotonization(QPainterPath& painterPath); }; diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.cpp deleted file mode 100644 index b2c6953..0000000 --- a/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "CubicMonotonization.h" -using namespace Renderer; -using std::vector; -using std::min; -using std::max; -using std::sort; -const double CubicMonotonization::eps = 1e-6; - - -CubicMonotonization::point CubicMonotonization::getPointByT(point a, point b, double t) const { - return { a.first * (1 - t) + b.first, a.second * (1 - t) + b.second }; -} - - -void CubicMonotonization::findPointsOfDivison(vector p, vector& res) { - double a = -p[0] + 7 * p[1] - 9 * p[2] + 3 * p[3]; - double b = 6 * p[0] - 12 * p[1] + 6 * p[2]; - double c = -3 * p[0] + 3*p[1]; - double deta = b * b - 4*a*c; - if (fabs(deta) < eps) return; - double t = 0; - if (fabs(a) <= eps) { - deta = sqrt(deta); - t = -b + deta; - if (0 < t && t < 1) { - res.push_back(t); - } - if (fabs(deta) <= eps) return ; - t = -b - deta; - if (0 < t && t < 1) { - res.push_back(t); - } - } -} - -double CubicMonotonization::getBezierPoint(vector &p, double t) const{ - double pt = 1 - t; - return pt * pt * pt * p[0] + 3 * t * pt * pt * p[1] + 3 * t * t * pt * p[2] + t * t * t * p[3]; -} - -void CubicMonotonization::splitBezierCubic(Line& p, point mid, double t, vector & res) { - Line Lf; Lf.clear(); - double pt = 1 - t; - Lf.push_back(p[0]); - point pointF = getPointByT(p[0], p[1], t); - point pointG = getPointByT(p[1], p[2], t); - point pointH = getPointByT(p[2], p[3], t); - Lf.push_back(pointF); - Lf.push_back(getPointByT(pointF, pointG, t)); - Lf.push_back(mid); - res.push_back(Lf); - p[0] = mid; - p[1] = getPointByT(pointG, pointH, t); - p[2] = pointH; -} - -void CubicMonotonization::getPointsOfDivision(Line p, vector & res) { - vector vX, vY, re; - vX.clear(); - vY.clear(); - re.clear(); - for (point now : p) { - vX.push_back(now.first); - vY.push_back(now.second); - } - findPointsOfDivison(vX, re); - findPointsOfDivison(vY, re); - sort(re.begin(), re.end()); - double lt = 0, x = 0, y = 0; - for (double &t : re) { - if (fabs(t - lt) <= eps) continue; - x = getBezierPoint(vX, t); - y = getBezierPoint(vY, t); - splitBezierCubic(p, { x, y }, t, res); - lt = t; - } -} diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.h b/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.h deleted file mode 100644 index 5207eb4..0000000 --- a/ArchitectureColoredPainting/src/Renderer/Painting/CubicMonotonization.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#include -#include -#include - -namespace Renderer -{ - class CubicMonotonization - { - typedef std::pair point; - typedef std::vector Line; - private: - static const double eps; - point getPointByT(point a, point b, double t) const; - void findPointsOfDivison(std::vector p, std::vector& res); - double getBezierPoint(std::vector& p, double t) const; - void splitBezierCubic(Line& p, point mid, double t, std::vector & res); - public: - // p 为端点、控制点、控制点、端点, res 为所有导数为0的点 - void getPointsOfDivision(Line p, std::vector & res); - }; - -} \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/Line.h b/ArchitectureColoredPainting/src/Renderer/Painting/Line.h index b1caf2b..e9accc4 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/Line.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/Line.h @@ -33,6 +33,11 @@ namespace Renderer operator glm::dvec2() { return glm::dvec2(x, y); } + + operator QPointF() { + return QPointF(x, y); + } + void show() { std::cout << '(' << x << ',' << y << ')' << ' '; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/LineTree.h b/ArchitectureColoredPainting/src/Renderer/Painting/LineTree.h index 7c1ed9e..09fd26f 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/LineTree.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/LineTree.h @@ -41,13 +41,13 @@ namespace Renderer { bool isLineEqual(PointIndexVector& a, PointIndexVector& b) const; static bool IsBorderValueResonable(double value, std::set & valueSet); static double findBorderValueNotOnLine(double value, std::set & valueSet); - static void monotonization(std::vector& inL, std::vector>& outL); public: void init(); void setLineWidth(double width); LineTree(int lineMin = 3) : requiredLineMin(lineMin), numLine(0), numPoint(0) {} void buildLineTree(std::vector& lineSet, double width, int type = 0); + static void monotonization(std::vector& inL, std::vector>& outL); std::vector getPointLineAndBvhTree(std::vector& pointSet, std::vector& lineSet); }; } \ No newline at end of file