From 6555e4db7228b31b60bcedf3a364788df34c16cc Mon Sep 17 00:00:00 2001 From: wuyize Date: Thu, 20 Oct 2022 14:09:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3SvgParser=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ArchitectureColoredPainting/MainWindow.ui | 4 +- ArchitectureColoredPainting/Model.cpp | 23 +- .../Shaders/painting.comp | 20 +- ArchitectureColoredPainting/ShortCutTree.cpp | 1 + ArchitectureColoredPainting/SvgParser.cpp | 1381 +++++++++-------- README.md | 4 +- 6 files changed, 744 insertions(+), 689 deletions(-) diff --git a/ArchitectureColoredPainting/MainWindow.ui b/ArchitectureColoredPainting/MainWindow.ui index 06b2055..f8cf55a 100644 --- a/ArchitectureColoredPainting/MainWindow.ui +++ b/ArchitectureColoredPainting/MainWindow.ui @@ -6,8 +6,8 @@ 0 0 - 800 - 400 + 1374 + 722 diff --git a/ArchitectureColoredPainting/Model.cpp b/ArchitectureColoredPainting/Model.cpp index 72b79f2..25e0050 100644 --- a/ArchitectureColoredPainting/Model.cpp +++ b/ArchitectureColoredPainting/Model.cpp @@ -193,17 +193,18 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod //vector> lineSet = SvgParser("M100,100C-.5,100,0,100.5,0,0L40,.07C40,59.5,39.5,60,100,60Z", 100, 100).parse(); //vector> lineSet = SvgParser("M308.49,212.25l23,28.38-82,78.31c-14.28,13.64-26.34-20.6-53.44,9.32l-30.24-13.4,63.56-51.59L190.71,215.6l-32.92,26.72L149.5,232.1l32.92-26.72L173.2,194l-32.91,26.72-7.38-9.08L165.83,185l-38.69-47.66L94.22,164,85,152.65l32.91-26.72-9.21-11.35L75.79,141.3l-5.53-6.81,32.92-26.72L94,96.42,61.05,123.14l-12-14.76L37.72,117.6l12,14.75L30.41,148,0,110.55,136.2,0l30.4,37.46L147.31,53.12l-12-14.76L124,47.58l12,14.75L103.05,89.05l9.21,11.35,32.92-26.72,5.52,6.81-32.91,26.72L127,118.56l32.92-26.72,9.21,11.35-32.91,26.72,38.69,47.67,32.91-26.72,7.37,9.08-32.91,26.72L191.49,198l32.92-26.72,8.29,10.22-32.92,26.71,38.7,47.68L302,204.3l6.45,7.95Z", 331.52, 328.26).parse(); - vector < vector > lineSet = { - {{-0.5,0.9}, {0.1,0.3}}, - {{0.1,0.3}, {0.0204656,0.379534}, {-0.2,0.0632573}, {-0.2,0.175736}}, - {{-0.2,0.175736}, {-0.2,0.288215}, {-0.579534,0.220466}, {-0.5,0.3}}, - {{-0.5,0.3}, {-0.420466,0.379534}, {-0.736743,0.6}, {-0.624264,0.6}}, - {{-0.624264,0.6}, {-0.511785,0.6}, {-0.579534,0.979534}, {-0.5,0.9}} - }; + //vector < vector > lineSet = { + // {{-0.5,0.9}, {0.1,0.3}}, + // {{0.1,0.3}, {0.0204656,0.379534}, {-0.2,0.0632573}, {-0.2,0.175736}}, + // {{-0.2,0.175736}, {-0.2,0.288215}, {-0.579534,0.220466}, {-0.5,0.3}}, + // {{-0.5,0.3}, {-0.420466,0.379534}, {-0.736743,0.6}, {-0.624264,0.6}}, + // {{-0.624264,0.6}, {-0.511785,0.6}, {-0.579534,0.979534}, {-0.5,0.9}} + //}; + vector> lineSet = SvgParser("M377,459.61a11.26,11.26,0,0,1,11.27-11.27H696.12a11.27,11.27,0,0,0,11-8.62A359.84,359.84,0,0,0,708,280.56a11.26,11.26,0,0,0-11-8.73H388.27A11.26,11.26,0,0,1,377,260.57h0a11.26,11.26,0,0,1,11.27-11.26H683.71A11.32,11.32,0,0,0,694.28,234C649.8,113.69,542.57,23.85,412.3,4.12a11.22,11.22,0,0,0-12.76,11.17v158.9a11.26,11.26,0,0,0,11.26,11.27H583.12a11.32,11.32,0,0,0,9.26-17.75c-31.67-46.59-78.51-75.2-109.11-90.07a11.25,11.25,0,0,0-16.13,10.17V115.2a11.24,11.24,0,0,0,6.22,10.07l7.51,3.76a11.28,11.28,0,0,1,5,15.12h0a11.27,11.27,0,0,1-15.11,5l-20-10a11.27,11.27,0,0,1-6.22-10.07V54a11.27,11.27,0,0,1,14.62-10.75c5.11,1.59,125.66,40.35,172.24,149A11.27,11.27,0,0,1,621.11,208H388.27A11.26,11.26,0,0,1,377,196.73V11.36A11.32,11.32,0,0,0,365.89.08C363.34,0,360.79,0,358.22,0s-5.11,0-7.66.08a11.32,11.32,0,0,0-11.11,11.28V196.74A11.26,11.26,0,0,1,328.18,208H95.35A11.27,11.27,0,0,1,85,192.3c46.57-108.67,167.12-147.42,172.23-149A11.26,11.26,0,0,1,271.86,54v75.11a11.25,11.25,0,0,1-6.23,10.07l-20,10a11.27,11.27,0,0,1-15.11-5h0a11.26,11.26,0,0,1,5-15.11l7.52-3.76a11.27,11.27,0,0,0,6.22-10.07V87.82a11.25,11.25,0,0,0-16.14-10.16c-30.6,14.87-77.45,43.48-109.1,90.07a11.3,11.3,0,0,0,9.25,17.74H305.66a11.26,11.26,0,0,0,11.27-11.26V15.31A11.22,11.22,0,0,0,304.17,4.14C173.88,23.86,66.66,113.71,22.17,234a11.32,11.32,0,0,0,10.56,15.29H328.18a11.26,11.26,0,0,1,11.27,11.26v0a11.26,11.26,0,0,1-11.27,11.26H19.52a11.26,11.26,0,0,0-11,8.72,359.84,359.84,0,0,0,.83,159.16,11.26,11.26,0,0,0,11,8.61H328.18a11.26,11.26,0,0,1,11.27,11.27h0a11.26,11.26,0,0,1-11.27,11.26h-294a11.32,11.32,0,0,0-10.53,15.4C69,604.65,175.3,692.78,304.16,712.3a11.21,11.21,0,0,0,12.76-11.16V542.22A11.26,11.26,0,0,0,305.66,531h-166c-9.53,0-14.89,11.22-8.69,18.47,34.09,39.77,74.45,65.66,101.77,80.18a11.25,11.25,0,0,0,16.53-10V591a11.26,11.26,0,0,1,11.26-11.26h0A11.26,11.26,0,0,1,271.85,591v63.85A11.27,11.27,0,0,1,256.8,665.5c-4.45-1.59-109.58-40-171-139.9a11.27,11.27,0,0,1,9.59-17.17H328.18a11.26,11.26,0,0,1,11.27,11.26V705.08a11.32,11.32,0,0,0,11.11,11.28q3.82.07,7.66.08c2.57,0,5.12,0,7.67-.08A11.32,11.32,0,0,0,377,705.08V519.69a11.25,11.25,0,0,1,11.27-11.26H621.1a11.26,11.26,0,0,1,9.59,17.16c-61.46,99.87-166.59,138.3-171,139.9a11.27,11.27,0,0,1-15-10.61V591a11.26,11.26,0,0,1,11.26-11.26h0A11.26,11.26,0,0,1,467.14,591v28.6a11.25,11.25,0,0,0,16.53,10c27.33-14.53,67.68-40.42,101.77-80.19,6.2-7.23.85-18.46-8.69-18.46h-166a11.26,11.26,0,0,0-11.26,11.26V701.12a11.21,11.21,0,0,0,12.76,11.17c128.86-19.51,235.14-107.66,280.48-226a11.33,11.33,0,0,0-10.53-15.41h-294A11.25,11.25,0,0,1,377,459.61ZM35.27,399.53V316.9a11.26,11.26,0,0,1,11.27-11.26H669.92a11.25,11.25,0,0,1,11.26,11.26v82.63a11.25,11.25,0,0,1-11.26,11.26H46.54a11.27,11.27,0,0,1-11.27-11.26Z", 716.45, 716.44).parse(); qDebug() << lineSet.size(); - for (vector line : lineSet) + for (vector& line : lineSet) { - for (Point p : line) + for (Point& p : line) p.show(); std::cout << std::endl; } @@ -211,7 +212,7 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod //vector> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} }; //vector> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} }; - ShortCutTree shortCutTree(3); + ShortCutTree shortCutTree(100); shortCutTree.buildShortCutTree(lineSet); vector pointVector; vector lineVector; @@ -261,7 +262,7 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod //fillType 0, //µ¥É« //fillColorMetallicRoughness - 1,1,0, 0,0.8, + 240/255.,220/255.,160/255., 0.996,0.18, }; elementData.insert(elementData.begin(), pointVector.begin(), pointVector.end()); qDebug() << elementIndex; diff --git a/ArchitectureColoredPainting/Shaders/painting.comp b/ArchitectureColoredPainting/Shaders/painting.comp index 132e694..3f87d1a 100644 --- a/ArchitectureColoredPainting/Shaders/painting.comp +++ b/ArchitectureColoredPainting/Shaders/painting.comp @@ -711,7 +711,7 @@ float cubic_bezier_dis(vec2 uv, vec2 p0, vec2 p1, vec2 p2, vec2 p3, bool roundEn return sqrt(d0); } -bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, inout vec3 debugBVH = vec3(0)) +bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, out vec2 metallicRoughness, inout vec3 debugBVH = vec3(0)) { bool hitElement = false; vec4 elementColor = vec4(-1); @@ -741,7 +741,7 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, inout vec3 deb uint styleIndex = bvhChildren[elementBvhRoot + elementBvhIndex].y; // for(int i = 0; i<200;i++) - if (elementData[styleIndex] == 0.||true) //Ãæ + if (elementData[styleIndex] == 0.) //Ãæ { uint contourIndex = leftChild - 0x80000000; @@ -787,6 +787,7 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, inout vec3 deb if(elementData[styleIndex+1]==0) { elementColor = vec4(elementData[styleIndex+2],elementData[styleIndex+3],elementData[styleIndex+4],0); + metallicRoughness = vec2(elementData[styleIndex+5],elementData[styleIndex+6]); } @@ -824,6 +825,7 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, inout vec3 deb if(elementData[styleIndex+3]==0) { elementColor = vec4(elementData[styleIndex+7],elementData[styleIndex+8],elementData[styleIndex+9],1); + metallicRoughness = vec2(elementData[styleIndex+10],elementData[styleIndex+11]); } } } @@ -860,13 +862,12 @@ void main() if(paintingIndex==0) return; - imageStore(gMetallicRoughness, pixelLocation, vec4(0 /*½ðÊô¶È*/, 0.8 /*´Ö²Ú¶È*/, 0, 1)); - vec2 uv = imageLoad(gPaintingTexCoord, pixelLocation).rg; vec3 debugBVH = vec3(0); //bool debugHit = false; - vec4 color = vec4(-1); + vec4 color = vec4(1,1,1,-1); + vec2 metallicRoughness = vec2(0, 0.8); stack.top = 0; uint index = 0, visitTime = 0; uint bvhLength = paintingOffsets[paintingIndex-1].y; @@ -895,8 +896,12 @@ void main() debugBVH = vec3(0); vec3 elementColor; - if(drawElement(leftChild - 0x80000000, localUV, elementColor, debugBVH)) + vec2 elementMetallicRoughness; + if(drawElement(leftChild - 0x80000000, localUV, elementColor, elementMetallicRoughness, debugBVH)) + { color = vec4(elementColor, zIndex); + metallicRoughness = elementMetallicRoughness; + } } @@ -920,7 +925,8 @@ void main() } imageStore(gBaseColor, pixelLocation, vec4(color.rgb,1)); - //return; + imageStore(gMetallicRoughness, pixelLocation, vec4(metallicRoughness, 0, 1)); + return; if (color.a!=-1&&debugBVH==vec3(0)) imageStore(gBaseColor, pixelLocation, vec4(vec3(1, 1, 0),1)); else diff --git a/ArchitectureColoredPainting/ShortCutTree.cpp b/ArchitectureColoredPainting/ShortCutTree.cpp index 555e7bb..25fda07 100644 --- a/ArchitectureColoredPainting/ShortCutTree.cpp +++ b/ArchitectureColoredPainting/ShortCutTree.cpp @@ -288,6 +288,7 @@ vector ShortCutTree::getPointLineAndBvhTree(vector& resPoint for (auto& nowTreeNode : restOfTreeNodes) { BvhTreeData oneData; oneData.leftSon = resLines.size(); + oneData.rightSon = resPoints.size(); oneData.bound = nowTreeNode.bound; std::cout << nowTreeNode.lineSet.size() << ' '; resLines.push_back(nowTreeNode.lineSet.size()); diff --git a/ArchitectureColoredPainting/SvgParser.cpp b/ArchitectureColoredPainting/SvgParser.cpp index bfc33e1..d8bcfa6 100644 --- a/ArchitectureColoredPainting/SvgParser.cpp +++ b/ArchitectureColoredPainting/SvgParser.cpp @@ -6,482 +6,482 @@ using namespace std; namespace SVGParser { -SVGCommandType getSVGCommand(char c) -{ - switch (c) - { - case 'm': - return SVG_MOVE_TO_REL; - break; - case 'M': - return SVG_MOVE_TO_ABS; - break; + SVGCommandType getSVGCommand(char c) + { + switch (c) + { + case 'm': + return SVG_MOVE_TO_REL; + break; + case 'M': + return SVG_MOVE_TO_ABS; + break; - case 'l': - return SVG_LINE_TO_REL; - break; - case 'L': - return SVG_LINE_TO_ABS; - break; + case 'l': + return SVG_LINE_TO_REL; + break; + case 'L': + return SVG_LINE_TO_ABS; + break; - case 'c': - return SVG_CUBIC_CURVE_TO_REL; - break; - case 'C': - return SVG_CUBIC_CURVE_TO_ABS; - break; + case 'c': + return SVG_CUBIC_CURVE_TO_REL; + break; + case 'C': + return SVG_CUBIC_CURVE_TO_ABS; + break; - case 'q': - return SVG_QUADRATIC_CURVE_TO_REL; - break; - case 'Q': - return SVG_QUADRATIC_CURVE_TO_ABS; - break; + case 'q': + return SVG_QUADRATIC_CURVE_TO_REL; + break; + case 'Q': + return SVG_QUADRATIC_CURVE_TO_ABS; + break; - case 'a': - return SVG_ARC_TO_REL; - break; - case 'A': - return SVG_ARC_TO_ABS; - break; + case 'a': + return SVG_ARC_TO_REL; + break; + case 'A': + return SVG_ARC_TO_ABS; + break; - case 't': - return SVG_SMOOTH_QUADRATIC_CURVE_TO_REL; - break; - case 'T': - return SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS; - break; + case 't': + return SVG_SMOOTH_QUADRATIC_CURVE_TO_REL; + break; + case 'T': + return SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS; + break; - case 's': - return SVG_SMOOTH_CUBIC_CURVE_TO_REL; - break; - case 'S': - return SVG_SMOOTH_CUBIC_CURVE_TO_ABS; - break; + case 's': + return SVG_SMOOTH_CUBIC_CURVE_TO_REL; + break; + case 'S': + return SVG_SMOOTH_CUBIC_CURVE_TO_ABS; + break; - case 'h': - return SVG_HORIZONTAL_LINE_TO_REL; - break; - case 'H': - return SVG_HORIZONTAL_LINE_TO_ABS; - break; + case 'h': + return SVG_HORIZONTAL_LINE_TO_REL; + break; + case 'H': + return SVG_HORIZONTAL_LINE_TO_ABS; + break; - case 'v': - return SVG_VERTICAL_LINE_TO_REL; - break; - case 'V': - return SVG_VERTICAL_LINE_TO_ABS; - break; + case 'v': + return SVG_VERTICAL_LINE_TO_REL; + break; + case 'V': + return SVG_VERTICAL_LINE_TO_ABS; + break; - case 'z': - case 'Z': - return SVG_CLOSE_PATH; - break; - default: - return SVG_INVALID; - break; - } -}; -int getCommandLength(SVGCommandType t) -{ - switch (t) - { - case SVG_MOVE_TO_REL: - case SVG_LINE_TO_REL: - case SVG_MOVE_TO_ABS: - case SVG_LINE_TO_ABS: - return 2; - break; - case SVG_HORIZONTAL_LINE_TO_REL: - case SVG_HORIZONTAL_LINE_TO_ABS: - case SVG_VERTICAL_LINE_TO_REL: - case SVG_VERTICAL_LINE_TO_ABS: - return 1; - break; + case 'z': + case 'Z': + return SVG_CLOSE_PATH; + break; + default: + return SVG_INVALID; + break; + } + }; + int getCommandLength(SVGCommandType t) + { + switch (t) + { + case SVG_MOVE_TO_REL: + case SVG_LINE_TO_REL: + case SVG_MOVE_TO_ABS: + case SVG_LINE_TO_ABS: + return 2; + break; + case SVG_HORIZONTAL_LINE_TO_REL: + case SVG_HORIZONTAL_LINE_TO_ABS: + case SVG_VERTICAL_LINE_TO_REL: + case SVG_VERTICAL_LINE_TO_ABS: + return 1; + break; - case SVG_QUADRATIC_CURVE_TO_REL: - case SVG_QUADRATIC_CURVE_TO_ABS: - return 4; - break; - case SVG_CUBIC_CURVE_TO_REL: - case SVG_CUBIC_CURVE_TO_ABS: - return 6; - break; + case SVG_QUADRATIC_CURVE_TO_REL: + case SVG_QUADRATIC_CURVE_TO_ABS: + return 4; + break; + case SVG_CUBIC_CURVE_TO_REL: + case SVG_CUBIC_CURVE_TO_ABS: + return 6; + break; - case SVG_SMOOTH_CUBIC_CURVE_TO_REL: - case SVG_SMOOTH_CUBIC_CURVE_TO_ABS: - return 4; - break; + case SVG_SMOOTH_CUBIC_CURVE_TO_REL: + case SVG_SMOOTH_CUBIC_CURVE_TO_ABS: + return 4; + break; - case SVG_SMOOTH_QUADRATIC_CURVE_TO_REL: - case SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS: - return 2; - break; + case SVG_SMOOTH_QUADRATIC_CURVE_TO_REL: + case SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS: + return 2; + break; - case SVG_ARC_TO_REL: - case SVG_ARC_TO_ABS: - return 7; - break; - case SVG_CLOSE_PATH: - case SVG_INVALID: - default: - return 0; - break; - } -}; -bool isAbsolute(SVGCommandType t) -{ - if (t == SVG_CLOSE_PATH) - return false; - return t % 2; -} -SVGPath createCommandChunk(SVGCommandType t, vector elements) -{ - int commandLength = getCommandLength(t); - SVGPath chunk; - bool abs = isAbsolute(t); - if (t == SVG_CLOSE_PATH) - chunk.push_back(make_shared()); - if (commandLength > 0) - for (int i = 0; i < int(elements.size() / commandLength); i++) - { - switch (t) - { - case SVG_LINE_TO_REL: - case SVG_LINE_TO_ABS: - chunk.push_back( - make_shared(elements[i * commandLength + 0], elements[i * commandLength + 1], abs)); - break; - case SVG_MOVE_TO_REL: - case SVG_MOVE_TO_ABS: - chunk.push_back( - make_shared(elements[i * commandLength + 0], elements[i * commandLength + 1], abs)); - break; - case SVG_CUBIC_CURVE_TO_REL: - case SVG_CUBIC_CURVE_TO_ABS: - chunk.push_back(make_shared( - elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], - elements[i * commandLength + 3], elements[i * commandLength + 4], elements[i * commandLength + 5], - abs)); - break; + case SVG_ARC_TO_REL: + case SVG_ARC_TO_ABS: + return 7; + break; + case SVG_CLOSE_PATH: + case SVG_INVALID: + default: + return 0; + break; + } + }; + bool isAbsolute(SVGCommandType t) + { + if (t == SVG_CLOSE_PATH) + return false; + return t % 2; + } + SVGPath createCommandChunk(SVGCommandType t, vector elements) + { + int commandLength = getCommandLength(t); + SVGPath chunk; + bool abs = isAbsolute(t); + if (t == SVG_CLOSE_PATH) + chunk.push_back(make_shared()); + if (commandLength > 0) + for (int i = 0; i < int(elements.size() / commandLength); i++) + { + switch (t) + { + case SVG_LINE_TO_REL: + case SVG_LINE_TO_ABS: + chunk.push_back( + make_shared(elements[i * commandLength + 0], elements[i * commandLength + 1], abs)); + break; + case SVG_MOVE_TO_REL: + case SVG_MOVE_TO_ABS: + chunk.push_back( + make_shared(elements[i * commandLength + 0], elements[i * commandLength + 1], abs)); + break; + case SVG_CUBIC_CURVE_TO_REL: + case SVG_CUBIC_CURVE_TO_ABS: + chunk.push_back(make_shared( + elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], + elements[i * commandLength + 3], elements[i * commandLength + 4], elements[i * commandLength + 5], + abs)); + break; - case SVG_SMOOTH_CUBIC_CURVE_TO_REL: - case SVG_SMOOTH_CUBIC_CURVE_TO_ABS: - chunk.push_back(make_shared( - elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], - elements[i * commandLength + 3], abs)); - break; + case SVG_SMOOTH_CUBIC_CURVE_TO_REL: + case SVG_SMOOTH_CUBIC_CURVE_TO_ABS: + chunk.push_back(make_shared( + elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], + elements[i * commandLength + 3], abs)); + break; - case SVG_QUADRATIC_CURVE_TO_REL: - case SVG_QUADRATIC_CURVE_TO_ABS: - chunk.push_back(make_shared( - elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], - elements[i * commandLength + 3], abs)); - break; + case SVG_QUADRATIC_CURVE_TO_REL: + case SVG_QUADRATIC_CURVE_TO_ABS: + chunk.push_back(make_shared( + elements[i * commandLength + 0], elements[i * commandLength + 1], elements[i * commandLength + 2], + elements[i * commandLength + 3], abs)); + break; - case SVG_SMOOTH_QUADRATIC_CURVE_TO_REL: - case SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS: - chunk.push_back(make_shared(elements[i * commandLength + 0], - elements[i * commandLength + 1], abs)); - break; + case SVG_SMOOTH_QUADRATIC_CURVE_TO_REL: + case SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS: + chunk.push_back(make_shared(elements[i * commandLength + 0], + elements[i * commandLength + 1], abs)); + break; - case SVG_ARC_TO_REL: - case SVG_ARC_TO_ABS: - chunk.push_back(make_shared( - elements[i * commandLength + 0], elements[i * commandLength + 1], // rx, ry - elements[i * commandLength + 2], // rotation, - int(elements[i * commandLength + 3]) == 1, - int(elements[i * commandLength + 4]) == 1, // large arc, sweep - elements[i * commandLength + 5], elements[i * commandLength + 6], // end x, end y - abs)); - break; - case SVG_HORIZONTAL_LINE_TO_REL: - case SVG_HORIZONTAL_LINE_TO_ABS: - chunk.push_back(make_shared(elements[i * commandLength + 0], abs)); - break; - case SVG_VERTICAL_LINE_TO_REL: - case SVG_VERTICAL_LINE_TO_ABS: - chunk.push_back(make_shared(elements[i * commandLength + 0], abs)); - break; - } - // This here is funky. - if (t == SVG_MOVE_TO_REL || t == SVG_MOVE_TO_ABS) - t = SVGCommandType(int(t) + 2); - } - return chunk; -} -SVGPath parsePath(istream &ss) -{ - SVGPath commandArray; - // istringstream ss(path); - // bool absolute = false; - SVGCommandType command = SVG_INVALID; - vector elements; + case SVG_ARC_TO_REL: + case SVG_ARC_TO_ABS: + chunk.push_back(make_shared( + elements[i * commandLength + 0], elements[i * commandLength + 1], // rx, ry + elements[i * commandLength + 2], // rotation, + int(elements[i * commandLength + 3]) == 1, + int(elements[i * commandLength + 4]) == 1, // large arc, sweep + elements[i * commandLength + 5], elements[i * commandLength + 6], // end x, end y + abs)); + break; + case SVG_HORIZONTAL_LINE_TO_REL: + case SVG_HORIZONTAL_LINE_TO_ABS: + chunk.push_back(make_shared(elements[i * commandLength + 0], abs)); + break; + case SVG_VERTICAL_LINE_TO_REL: + case SVG_VERTICAL_LINE_TO_ABS: + chunk.push_back(make_shared(elements[i * commandLength + 0], abs)); + break; + } + // This here is funky. + if (t == SVG_MOVE_TO_REL || t == SVG_MOVE_TO_ABS) + t = SVGCommandType(int(t) + 2); + } + return chunk; + } + SVGPath parsePath(istream& ss) + { + SVGPath commandArray; + // istringstream ss(path); + // bool absolute = false; + SVGCommandType command = SVG_INVALID; + vector elements; - while (ss.good()) - { - char c = ss.get(); - SVGCommandType cmd = getSVGCommand(c); + while (ss.good()) + { + char c = ss.get(); + SVGCommandType cmd = getSVGCommand(c); - if (cmd != SVG_INVALID) - { - SVGPath chunk = createCommandChunk(command, elements); - commandArray.insert(commandArray.end(), chunk.begin(), chunk.end()); - command = cmd; - elements.clear(); - } - else if (!(isspace(c) || c == ',')) - { // its a number... - double x; - ss.unget(); - ss >> x; - elements.push_back(x); - } // else its a separator - } - SVGPath chunk = createCommandChunk(command, elements); - commandArray.insert(commandArray.end(), chunk.begin(), chunk.end()); - return commandArray; -} + if (cmd != SVG_INVALID) + { + SVGPath chunk = createCommandChunk(command, elements); + commandArray.insert(commandArray.end(), chunk.begin(), chunk.end()); + command = cmd; + elements.clear(); + } + else if (!(isspace(c) || c == ',')) + { // its a number... + double x; + ss.unget(); + ss >> x; + elements.push_back(x); + } // else its a separator + } + SVGPath chunk = createCommandChunk(command, elements); + commandArray.insert(commandArray.end(), chunk.begin(), chunk.end()); + return commandArray; + } -SVGCommand::SVGCommand(double _x, double _y, bool abs) : x(_x), y(_y), absolute(abs) -{ -} -SVGLineTo::SVGLineTo(double x, double y, bool abs) : SVGCommand(x, y, abs) -{ -} + SVGCommand::SVGCommand(double _x, double _y, bool abs) : x(_x), y(_y), absolute(abs) + { + } + SVGLineTo::SVGLineTo(double x, double y, bool abs) : SVGCommand(x, y, abs) + { + } -const string SVGLineTo::toString() const -{ - stringstream s; - s << (absolute ? "L" : "l") << x << "," << y; - return s.str(); -} -string SVGLineTo::toString2() -{ - stringstream s; - s << "Line to (" << (absolute ? "absolute" : "relative") << ") " << x << "x" << y; - return s.str(); -} + const string SVGLineTo::toString() const + { + stringstream s; + s << (absolute ? "L" : "l") << x << "," << y; + return s.str(); + } + string SVGLineTo::toString2() + { + stringstream s; + s << "Line to (" << (absolute ? "absolute" : "relative") << ") " << x << "x" << y; + return s.str(); + } -SVGCommandType SVGLineTo::getType() -{ - return absolute ? SVG_LINE_TO_ABS : SVG_LINE_TO_REL; -} + SVGCommandType SVGLineTo::getType() + { + return absolute ? SVG_LINE_TO_ABS : SVG_LINE_TO_REL; + } -SVGHLineTo::SVGHLineTo(double x, bool abs) : SVGCommand(x, 0, abs) -{ -} -const string SVGHLineTo::toString() const -{ - stringstream s; - s << (absolute ? "H" : "h") << x; - return s.str(); -} -string SVGHLineTo::toString2() -{ - stringstream s; - s << "Horizontal Line (" << (absolute ? "absolute" : "relative") << ") " << x << "px"; - return s.str(); -} + SVGHLineTo::SVGHLineTo(double x, bool abs) : SVGCommand(x, 0, abs) + { + } + const string SVGHLineTo::toString() const + { + stringstream s; + s << (absolute ? "H" : "h") << x; + return s.str(); + } + string SVGHLineTo::toString2() + { + stringstream s; + s << "Horizontal Line (" << (absolute ? "absolute" : "relative") << ") " << x << "px"; + return s.str(); + } -SVGCommandType SVGHLineTo::getType() -{ - return absolute ? SVG_HORIZONTAL_LINE_TO_ABS : SVG_HORIZONTAL_LINE_TO_REL; -} + SVGCommandType SVGHLineTo::getType() + { + return absolute ? SVG_HORIZONTAL_LINE_TO_ABS : SVG_HORIZONTAL_LINE_TO_REL; + } -SVGVLineTo::SVGVLineTo(double y, bool abs) : SVGCommand(0, y, abs) -{ -} + SVGVLineTo::SVGVLineTo(double y, bool abs) : SVGCommand(0, y, abs) + { + } -const string SVGVLineTo::toString() const -{ - stringstream s; - s << (absolute ? "V" : "v") << y; - return s.str(); -} -string SVGVLineTo::toString2() -{ - stringstream s; - s << "Vertical Line (" << (absolute ? "absolute" : "relative") << ") " << y << "px"; - return s.str(); -} -SVGCommandType SVGVLineTo::getType() -{ - return absolute ? SVG_VERTICAL_LINE_TO_ABS : SVG_VERTICAL_LINE_TO_REL; -} + const string SVGVLineTo::toString() const + { + stringstream s; + s << (absolute ? "V" : "v") << y; + return s.str(); + } + string SVGVLineTo::toString2() + { + stringstream s; + s << "Vertical Line (" << (absolute ? "absolute" : "relative") << ") " << y << "px"; + return s.str(); + } + SVGCommandType SVGVLineTo::getType() + { + return absolute ? SVG_VERTICAL_LINE_TO_ABS : SVG_VERTICAL_LINE_TO_REL; + } -SVGMoveTo::SVGMoveTo(double x, double y, bool abs) : SVGCommand(x, y, abs) -{ -} -const string SVGMoveTo::toString() const -{ - stringstream s; - s << (absolute ? "M" : "m") << x << "," << y; - return s.str(); -} -string SVGMoveTo::toString2() -{ - stringstream s; - s << "Move to (" << (absolute ? "absolute" : "relative") << ") to " << x << "x" << y; - return s.str(); -} -SVGCommandType SVGMoveTo::getType() -{ - return absolute ? SVG_MOVE_TO_ABS : SVG_MOVE_TO_REL; -} + SVGMoveTo::SVGMoveTo(double x, double y, bool abs) : SVGCommand(x, y, abs) + { + } + const string SVGMoveTo::toString() const + { + stringstream s; + s << (absolute ? "M" : "m") << x << "," << y; + return s.str(); + } + string SVGMoveTo::toString2() + { + stringstream s; + s << "Move to (" << (absolute ? "absolute" : "relative") << ") to " << x << "x" << y; + return s.str(); + } + SVGCommandType SVGMoveTo::getType() + { + return absolute ? SVG_MOVE_TO_ABS : SVG_MOVE_TO_REL; + } -SVGCubicCurveTo::SVGCubicCurveTo(double _x0, double _y0, double _x1, double _y1, double x, double y, bool abs) - : SVGCommand(x, y, abs), x0(_x0), y0(_y0), x1(_x1), y1(_y1) -{ -} + SVGCubicCurveTo::SVGCubicCurveTo(double _x0, double _y0, double _x1, double _y1, double x, double y, bool abs) + : SVGCommand(x, y, abs), x0(_x0), y0(_y0), x1(_x1), y1(_y1) + { + } -const string SVGCubicCurveTo::toString() const -{ - stringstream s; - s << (absolute ? "C" : "c") << x0 << "," << y0 << " " << x1 << "," << y1 << " " << x << "," << y; - return s.str(); -} -string SVGCubicCurveTo::toString2() -{ - stringstream s; - s << "Cubic Curve (" << (absolute ? "absolute" : "relative") << ") control points: " << x0 << "," << y0 << " " << x1 - << "," << y1 << " to " << x << "x" << y; - return s.str(); -} -SVGCommandType SVGCubicCurveTo::getType() -{ - return absolute ? SVG_CUBIC_CURVE_TO_ABS : SVG_CUBIC_CURVE_TO_REL; -} + const string SVGCubicCurveTo::toString() const + { + stringstream s; + s << (absolute ? "C" : "c") << x0 << "," << y0 << " " << x1 << "," << y1 << " " << x << "," << y; + return s.str(); + } + string SVGCubicCurveTo::toString2() + { + stringstream s; + s << "Cubic Curve (" << (absolute ? "absolute" : "relative") << ") control points: " << x0 << "," << y0 << " " << x1 + << "," << y1 << " to " << x << "x" << y; + return s.str(); + } + SVGCommandType SVGCubicCurveTo::getType() + { + return absolute ? SVG_CUBIC_CURVE_TO_ABS : SVG_CUBIC_CURVE_TO_REL; + } -SVGSmoothCubicCurveTo::SVGSmoothCubicCurveTo(double _x1, double _y1, double x, double y, bool abs) - : SVGCommand(x, y, abs), x1(_x1), y1(_y1) -{ -} + SVGSmoothCubicCurveTo::SVGSmoothCubicCurveTo(double _x1, double _y1, double x, double y, bool abs) + : SVGCommand(x, y, abs), x1(_x1), y1(_y1) + { + } -const string SVGSmoothCubicCurveTo::toString() const -{ - stringstream s; - s << (absolute ? "S" : "s") << x1 << "," << y1 << " " << x << "," << y; - return s.str(); -} -string SVGSmoothCubicCurveTo::toString2() -{ - stringstream s; - s << "Smooth cubic curve to (" << (absolute ? "absolute" : "relative") << ") control point: " << x1 << "," << y1 - << " to " << x << "x" << y; - return s.str(); -} -SVGCommandType SVGSmoothCubicCurveTo::getType() -{ - return absolute ? SVG_SMOOTH_CUBIC_CURVE_TO_ABS : SVG_SMOOTH_CUBIC_CURVE_TO_REL; -} + const string SVGSmoothCubicCurveTo::toString() const + { + stringstream s; + s << (absolute ? "S" : "s") << x1 << "," << y1 << " " << x << "," << y; + return s.str(); + } + string SVGSmoothCubicCurveTo::toString2() + { + stringstream s; + s << "Smooth cubic curve to (" << (absolute ? "absolute" : "relative") << ") control point: " << x1 << "," << y1 + << " to " << x << "x" << y; + return s.str(); + } + SVGCommandType SVGSmoothCubicCurveTo::getType() + { + return absolute ? SVG_SMOOTH_CUBIC_CURVE_TO_ABS : SVG_SMOOTH_CUBIC_CURVE_TO_REL; + } -SVGQuadraticCurveTo::SVGQuadraticCurveTo(double _x0, double _y0, double x, double y, bool abs) - : SVGCommand(x, y, abs), x0(_x0), y0(_y0) -{ -} -const string SVGQuadraticCurveTo::toString() const -{ - stringstream s; - s << (absolute ? "Q" : "q") << x0 << "," << y0 << " " << x << "," << y; - return s.str(); -} -string SVGQuadraticCurveTo::toString2() -{ - stringstream s; - s << "quadratic curve (" << (absolute ? "absolute" : "relative") << ") control point: " << x0 << "," << y0 << " to " - << x << "x" << y; - return s.str(); -} -SVGCommandType SVGQuadraticCurveTo::getType() -{ - return absolute ? SVG_QUADRATIC_CURVE_TO_ABS : SVG_QUADRATIC_CURVE_TO_REL; -} -SVGSmoothQuadraticCurveTo::SVGSmoothQuadraticCurveTo(double x, double y, bool abs) : SVGCommand(x, y, abs) -{ -} -const string SVGSmoothQuadraticCurveTo::toString() const -{ - stringstream s; - s << (absolute ? "T" : "t") << x << "," << y; - return s.str(); -} -string SVGSmoothQuadraticCurveTo::toString2() -{ - stringstream s; - s << "Smooth quadratic curve (" << (absolute ? "absolute" : "relative") << ") to " << x << "x" << y; - return s.str(); -} -SVGCommandType SVGSmoothQuadraticCurveTo::getType() -{ - return absolute ? SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS : SVG_SMOOTH_QUADRATIC_CURVE_TO_REL; -} + SVGQuadraticCurveTo::SVGQuadraticCurveTo(double _x0, double _y0, double x, double y, bool abs) + : SVGCommand(x, y, abs), x0(_x0), y0(_y0) + { + } + const string SVGQuadraticCurveTo::toString() const + { + stringstream s; + s << (absolute ? "Q" : "q") << x0 << "," << y0 << " " << x << "," << y; + return s.str(); + } + string SVGQuadraticCurveTo::toString2() + { + stringstream s; + s << "quadratic curve (" << (absolute ? "absolute" : "relative") << ") control point: " << x0 << "," << y0 << " to " + << x << "x" << y; + return s.str(); + } + SVGCommandType SVGQuadraticCurveTo::getType() + { + return absolute ? SVG_QUADRATIC_CURVE_TO_ABS : SVG_QUADRATIC_CURVE_TO_REL; + } + SVGSmoothQuadraticCurveTo::SVGSmoothQuadraticCurveTo(double x, double y, bool abs) : SVGCommand(x, y, abs) + { + } + const string SVGSmoothQuadraticCurveTo::toString() const + { + stringstream s; + s << (absolute ? "T" : "t") << x << "," << y; + return s.str(); + } + string SVGSmoothQuadraticCurveTo::toString2() + { + stringstream s; + s << "Smooth quadratic curve (" << (absolute ? "absolute" : "relative") << ") to " << x << "x" << y; + return s.str(); + } + SVGCommandType SVGSmoothQuadraticCurveTo::getType() + { + return absolute ? SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS : SVG_SMOOTH_QUADRATIC_CURVE_TO_REL; + } -SVGArcTo::SVGArcTo(double rx, double ry, double rotxaxis, bool large, bool sweep, double x, double y, bool abs) - : SVGCommand(x, y, abs) -{ - this->rx = rx; - this->ry = ry; + SVGArcTo::SVGArcTo(double rx, double ry, double rotxaxis, bool large, bool sweep, double x, double y, bool abs) + : SVGCommand(x, y, abs) + { + this->rx = rx; + this->ry = ry; - this->rot = rotxaxis; + this->rot = rotxaxis; - this->large = large; - this->sweep = sweep; -} -const string SVGArcTo::toString() const -{ - stringstream s; - s << (absolute ? "A" : "a") << rx << "," << ry << " " << rot << " " << large << "," << sweep << " " << x << "," - << y; - return s.str(); -} + this->large = large; + this->sweep = sweep; + } + const string SVGArcTo::toString() const + { + stringstream s; + s << (absolute ? "A" : "a") << rx << "," << ry << " " << rot << " " << large << "," << sweep << " " << x << "," + << y; + return s.str(); + } -string SVGArcTo::toString2() -{ - stringstream s; - s << "Arc (" << (absolute ? "absolute" : "relative") << "x-radii: " << rx << " y-radii: " << ry - << " x-rotation: " << rot << " large-arc: " << large << " sweep: " << sweep << " to: " << x << "x" << y; - return s.str(); -} + string SVGArcTo::toString2() + { + stringstream s; + s << "Arc (" << (absolute ? "absolute" : "relative") << "x-radii: " << rx << " y-radii: " << ry + << " x-rotation: " << rot << " large-arc: " << large << " sweep: " << sweep << " to: " << x << "x" << y; + return s.str(); + } -SVGCommandType SVGArcTo::getType() -{ - return absolute ? SVG_ARC_TO_ABS : SVG_ARC_TO_REL; -} + SVGCommandType SVGArcTo::getType() + { + return absolute ? SVG_ARC_TO_ABS : SVG_ARC_TO_REL; + } -SVGClosePath::SVGClosePath(bool abs) : SVGCommand(0, 0, abs) -{ -} + SVGClosePath::SVGClosePath(bool abs) : SVGCommand(0, 0, abs) + { + } -const string SVGClosePath::toString() const -{ - stringstream s; - s << (absolute ? "Z" : "z"); - return s.str(); -} -string SVGClosePath::toString2() -{ - stringstream s; - s << "Close path"; - return s.str(); -} -SVGCommandType SVGClosePath::getType() -{ - return SVG_CLOSE_PATH; -} -std::ostream &operator<<(std::ostream &out, const SVGParser::SVGPath &path) -{ - for (SVGParser::SVGPath::const_iterator it = path.begin(); it != path.end(); it++) - { - out << (*it)->toString(); - } - return out; -} -std::ostream &operator<<(std::ostream &out, const SVGParser::SVGCommand *cmd) -{ - out << cmd->toString(); - return out; -} + const string SVGClosePath::toString() const + { + stringstream s; + s << (absolute ? "Z" : "z"); + return s.str(); + } + string SVGClosePath::toString2() + { + stringstream s; + s << "Close path"; + return s.str(); + } + SVGCommandType SVGClosePath::getType() + { + return SVG_CLOSE_PATH; + } + std::ostream& operator<<(std::ostream& out, const SVGParser::SVGPath& path) + { + for (SVGParser::SVGPath::const_iterator it = path.begin(); it != path.end(); it++) + { + out << (*it)->toString(); + } + return out; + } + std::ostream& operator<<(std::ostream& out, const SVGParser::SVGCommand* cmd) + { + out << cmd->toString(); + return out; + } }; // namespace SVGParser @@ -491,263 +491,310 @@ SvgParser::SvgParser(const std::string path, double width, double height) : path Point SvgParser::convertPoint(double x, double y) { - return Point{x / width * 2. - 1., 1. - y / height * 2.}; + return Point{ x / width * 2. - 1., 1. - y / height * 2. }; } Point SvgParser::clampPoint(Point p) { - return Point{std::clamp(p.x, -1., 1.), std::clamp(p.y, -1., 1.)}; + return Point{ std::clamp(p.x, -1., 1.), std::clamp(p.y, -1., 1.) }; } Point SvgParser::convertAbsPoint(double x, double y) { - return clampPoint(convertPoint(x, y)); + return clampPoint(convertPoint(x, y)); } Point SvgParser::convertRelPoint(Point pointBegin, double x, double y) { - return clampPoint(pointBegin + Point{x / width * 2., -y / height * 2.}); + return clampPoint(pointBegin + Point{ x / width * 2., -y / height * 2. }); } using namespace SVGParser; vector> SvgParser::parse() { - vector line; - vector> lines; + Point startPoint; + vector line; + vector> lines; - istringstream stringStream(path); - SVGPath svgPath = parsePath(stringStream); - for (shared_ptr svgCmd : svgPath) - { - switch (svgCmd->getType()) - { - case SVG_MOVE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertAbsPoint(cmd->x, cmd->y)); - break; - } - case SVG_LINE_TO_REL: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertRelPoint(line.back(), cmd->x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_LINE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertAbsPoint(cmd->x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_HORIZONTAL_LINE_TO_REL: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertRelPoint(line.back(), cmd->x, 0)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_HORIZONTAL_LINE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertAbsPoint(cmd->x, line.back().y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_VERTICAL_LINE_TO_REL: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertRelPoint(line.back(), 0, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_VERTICAL_LINE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertAbsPoint(line.back().x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_QUADRATIC_CURVE_TO_REL: { - shared_ptr cmd = static_pointer_cast(svgCmd); - Point pointBegin = line.back(); - Point pointControl = convertRelPoint(pointBegin, cmd->x0, cmd->y0); - Point pointEnd = convertRelPoint(pointBegin, cmd->x, cmd->y); - line.push_back(pointBegin + (pointControl - pointBegin) * (2. / 3.)); - line.push_back(pointEnd + (pointControl - pointEnd) * (2. / 3.)); - line.push_back(pointEnd); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_QUADRATIC_CURVE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - Point pointBegin = line.back(); - Point pointControl = convertAbsPoint(cmd->x0, cmd->y0); - Point pointEnd = convertAbsPoint(cmd->x, cmd->y); - line.push_back(pointBegin + (pointControl - pointBegin) * (2. / 3.)); - line.push_back(pointEnd + (pointControl - pointEnd) * (2. / 3.)); - line.push_back(pointEnd); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_CUBIC_CURVE_TO_REL: { - shared_ptr cmd = static_pointer_cast(svgCmd); - Point pointBegin = line.back(); - line.push_back(convertRelPoint(pointBegin, cmd->x0, cmd->y0)); - line.push_back(convertRelPoint(pointBegin, cmd->x1, cmd->y1)); - line.push_back(convertRelPoint(pointBegin, cmd->x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - case SVG_CUBIC_CURVE_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - line.push_back(convertAbsPoint(cmd->x0, cmd->y0)); - line.push_back(convertAbsPoint(cmd->x1, cmd->y1)); - line.push_back(convertAbsPoint(cmd->x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - } - /*case SVG_ARC_TO_REL: - { - shared_ptr cmd = static_pointer_cast(svgCmd); - Point pointBegin = line.back(); - line.push_back(convertRelPoint(pointBegin, cmd->x0, cmd->y0)); - line.push_back(convertRelPoint(pointBegin, cmd->x1, cmd->y1)); - line.push_back(convertRelPoint(pointBegin, cmd->x, cmd->y)); - lines.push_back(line); - line.erase(line.begin(), line.end() - 1); - break; - }*/ - case SVG_ARC_TO_ABS: { - shared_ptr cmd = static_pointer_cast(svgCmd); - ellipticalArcConverter(line.back(), cmd->rx, cmd->ry, cmd->rot, cmd->large, cmd->sweep, - convertAbsPoint(cmd->x, cmd->y), lines); - break; - } - case SVG_CLOSE_PATH: { - line.push_back(lines[0][0]); - lines.push_back(line); - break; - } - default: - cout << "PARSE ERROR" << endl; - break; - } - } - return lines; + istringstream stringStream(path); + SVGPath svgPath = parsePath(stringStream); + for (shared_ptr svgCmd : svgPath) + { + switch (svgCmd->getType()) + { + case SVG_MOVE_TO_REL: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + startPoint = convertRelPoint(line.back(), cmd->x, cmd->y); + line.clear(); + line.push_back(startPoint); + break; + } + case SVG_MOVE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + startPoint = convertAbsPoint(cmd->x, cmd->y); + line.clear(); + line.push_back(startPoint); + break; + } + case SVG_LINE_TO_REL: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_LINE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertAbsPoint(cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_HORIZONTAL_LINE_TO_REL: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), cmd->x, 0)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_HORIZONTAL_LINE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(Point{ convertAbsPoint(cmd->x, 0).x, line.back().y }); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_VERTICAL_LINE_TO_REL: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), 0, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_VERTICAL_LINE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(Point{ line.back().x, convertAbsPoint(0, cmd->y).y }); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_QUADRATIC_CURVE_TO_REL: { + shared_ptr cmd = static_pointer_cast(svgCmd); + Point pointBegin = line.back(); + Point pointControl = convertRelPoint(pointBegin, cmd->x0, cmd->y0); + Point pointEnd = convertRelPoint(pointBegin, cmd->x, cmd->y); + line.push_back(pointBegin + (pointControl - pointBegin) * (2. / 3.)); + line.push_back(pointEnd + (pointControl - pointEnd) * (2. / 3.)); + line.push_back(pointEnd); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_QUADRATIC_CURVE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + Point pointBegin = line.back(); + Point pointControl = convertAbsPoint(cmd->x0, cmd->y0); + Point pointEnd = convertAbsPoint(cmd->x, cmd->y); + line.push_back(pointBegin + (pointControl - pointBegin) * (2. / 3.)); + line.push_back(pointEnd + (pointControl - pointEnd) * (2. / 3.)); + line.push_back(pointEnd); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_CUBIC_CURVE_TO_REL: { + shared_ptr cmd = static_pointer_cast(svgCmd); + Point pointBegin = line.back(); + line.push_back(convertRelPoint(pointBegin, cmd->x0, cmd->y0)); + line.push_back(convertRelPoint(pointBegin, cmd->x1, cmd->y1)); + line.push_back(convertRelPoint(pointBegin, cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_CUBIC_CURVE_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertAbsPoint(cmd->x0, cmd->y0)); + line.push_back(convertAbsPoint(cmd->x1, cmd->y1)); + line.push_back(convertAbsPoint(cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_SMOOTH_CUBIC_CURVE_TO_REL: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_SMOOTH_CUBIC_CURVE_TO_ABS: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertAbsPoint(cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_SMOOTH_QUADRATIC_CURVE_TO_REL: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + case SVG_SMOOTH_QUADRATIC_CURVE_TO_ABS: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertAbsPoint(cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + break; + } + + case SVG_ARC_TO_REL: + { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertRelPoint(line.back(), cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + //ellipticalArcConverter(line.back(), cmd->rx, cmd->ry, cmd->rot, cmd->large, cmd->sweep, + // convertRelPoint(line.back(), cmd->x, cmd->y), lines); + break; + } + case SVG_ARC_TO_ABS: { + shared_ptr cmd = static_pointer_cast(svgCmd); + line.push_back(convertAbsPoint(cmd->x, cmd->y)); + lines.push_back(line); + line.erase(line.begin(), line.end() - 1); + //ellipticalArcConverter(line.back(), cmd->rx, cmd->ry, cmd->rot, cmd->large, cmd->sweep, + // convertAbsPoint(cmd->x, cmd->y), lines); + break; + } + + case SVG_CLOSE_PATH: { + line.push_back(startPoint); + lines.push_back(line); + break; + } + default: + cout << "PARSE ERROR" << endl; + break; + } + } + return lines; } void SvgParser::ellipticalArcConverter(Point beginPoint, double radiusX, double radiusY, double phi, bool flagA, - bool flagS, Point endPoint, vector> &lines) + bool flagS, Point endPoint, vector>& lines) { - auto &start = beginPoint; - auto &end = endPoint; - if (fabs(start.x - end.x) < eps && fabs(start.y - end.y) < eps) - return; - if (fabs(radiusX) < eps || fabs(radiusY) < eps) - { - lines.emplace_back(vector{start, end}); - return; - } - radiusX = fabs(radiusX); - radiusY = fabs(radiusY); - auto center = centerConverter(start, radiusX, radiusY, phi, flagA, flagS, end); - Point r{center[0], center[1]}; - Point c{center[2], center[3]}; - double theta_1 = center[4]; - double deltTheta = center[5]; - double segments = deltTheta * 4.0 / PI; - int n = floor(segments); - Point p_1 = eConverter(c, r, cos(phi), sin(phi), theta_1); - Point e_1_ = e2Converter(c, r, cos(phi), sin(phi), theta_1); - double alpha = 0.26511477349130245; - double t = theta_1 + PI / 4.0; - for (int i = 1; i <= n; i++) - { - Point p_2 = eConverter(c, r, cos(phi), sin(phi), t); - Point e_2_ = e2Converter(c, r, cos(phi), sin(phi), t); - Point q_1 = p_1 + e_1_ * alpha; - Point q_2 = p_2 - e_2_ * alpha; - lines.emplace_back(vector{p_1, q_1, q_2, p_2}); - p_1 = p_2; - e_1_ = e_2_; - t = t + PI / 4.0; - } - if (n != ceil(segments)) { - double step = t - PI / 4.0; - double t = theta_1 + deltTheta; - double alpha_t = tan(step / 2.0); - alpha = sin(step) * (sqrt(4 + 3.0 * alpha_t * alpha_t) - 1.0) / 3.0; - Point p_2 = eConverter(c, r, cos(phi), sin(phi), t); - Point e_2_ = e2Converter(c, r, cos(phi), sin(phi), t); - Point q_1 = p_1 + e_1_ * alpha; - Point q_2 = p_2 - e_2_ * alpha; - lines.emplace_back(vector{p_1, q_1, q_2, p_2}); - } + auto& start = beginPoint; + auto& end = endPoint; + if (fabs(start.x - end.x) < eps && fabs(start.y - end.y) < eps) + return; + if (fabs(radiusX) < eps || fabs(radiusY) < eps) + { + lines.emplace_back(vector{start, end}); + return; + } + radiusX = fabs(radiusX); + radiusY = fabs(radiusY); + auto center = centerConverter(start, radiusX, radiusY, phi, flagA, flagS, end); + Point r{ center[0], center[1] }; + Point c{ center[2], center[3] }; + double theta_1 = center[4]; + double deltTheta = center[5]; + double segments = deltTheta * 4.0 / PI; + int n = floor(segments); + Point p_1 = eConverter(c, r, cos(phi), sin(phi), theta_1); + Point e_1_ = e2Converter(c, r, cos(phi), sin(phi), theta_1); + double alpha = 0.26511477349130245; + double t = theta_1 + PI / 4.0; + for (int i = 1; i <= n; i++) + { + Point p_2 = eConverter(c, r, cos(phi), sin(phi), t); + Point e_2_ = e2Converter(c, r, cos(phi), sin(phi), t); + Point q_1 = p_1 + e_1_ * alpha; + Point q_2 = p_2 - e_2_ * alpha; + lines.emplace_back(vector{p_1, q_1, q_2, p_2}); + p_1 = p_2; + e_1_ = e_2_; + t = t + PI / 4.0; + } + if (n != ceil(segments)) { + double step = t - PI / 4.0; + double t = theta_1 + deltTheta; + double alpha_t = tan(step / 2.0); + alpha = sin(step) * (sqrt(4 + 3.0 * alpha_t * alpha_t) - 1.0) / 3.0; + Point p_2 = eConverter(c, r, cos(phi), sin(phi), t); + Point e_2_ = e2Converter(c, r, cos(phi), sin(phi), t); + Point q_1 = p_1 + e_1_ * alpha; + Point q_2 = p_2 - e_2_ * alpha; + lines.emplace_back(vector{p_1, q_1, q_2, p_2}); + } } vector SvgParser::centerConverter(Point beginPoint, double radiusX, double radiusY, double phi, bool flagA, - bool flagS, Point endPoint) + bool flagS, Point endPoint) { - Point r{radiusX, radiusY}; - double x_half = (beginPoint.x - endPoint.x) / 2.0; - double y_half = (beginPoint.y - endPoint.y) / 2.0; - Point r_2{radiusX * radiusX, radiusY * radiusY}; - double x_1 = x_half * cos(phi) + y_half * sin(phi); - double x_12 = x_1 * x_1; - double y_1 = y_half * cos(phi) + x_half * sin(phi); - double y_12 = y_1 * y_1; - double c_r = x_12 / r_2.x + y_12 / r_2.y; - if (fabs(c_r - 1.0) > eps) - { - c_r = sqrt(c_r); - r = Point{r.x * c_r, r.y * c_r}; - r_2 = Point{r.x * r.x, r.y * r.y}; - } - double d_q = r_2.x * y_12 + r_2.y * x_12; - double p_q = (r_2.x * r_2.y - d_q) / d_q; - double s_c = sqrt(max(0.0, p_q)); - if (flagA == flagS) - { - s_c *= -1.0; - } - Point c{r.x * s_c * y_1 / r.y, -r.y * s_c * x_1 / r.x}; - double c_x = c.x * cos(phi) - c.y * sin(phi) + (beginPoint.x + endPoint.x) / 2.0; - double c_y = c.x * sin(phi) + c.y * cos(phi) + (beginPoint.y + endPoint.y) / 2.0; - Point v{(x_1 - c.x) / r.x, (y_1 - c.y) / r.y}; - double theta_1 = angleConverter(Point{1, 0}, v); - double deltTheta = angleConverter(v, Point{(-x_1 - c.x) / r.x, (-y_1 - c.y) / r.y}); - while (deltTheta - 2.0 * PI < eps) - deltTheta += 2.0 * PI; - while (deltTheta - 2.0 * PI >= eps) - deltTheta -= 2.0 * PI; - if (!flagS) - { - deltTheta -= 2.0 * PI; - } - return vector{r.x, r.y, c_x, c_y, theta_1, deltTheta}; + Point r{ radiusX, radiusY }; + double x_half = (beginPoint.x - endPoint.x) / 2.0; + double y_half = (beginPoint.y - endPoint.y) / 2.0; + Point r_2{ radiusX * radiusX, radiusY * radiusY }; + double x_1 = x_half * cos(phi) + y_half * sin(phi); + double x_12 = x_1 * x_1; + double y_1 = y_half * cos(phi) + x_half * sin(phi); + double y_12 = y_1 * y_1; + double c_r = x_12 / r_2.x + y_12 / r_2.y; + if (fabs(c_r - 1.0) > eps) + { + c_r = sqrt(c_r); + r = Point{ r.x * c_r, r.y * c_r }; + r_2 = Point{ r.x * r.x, r.y * r.y }; + } + double d_q = r_2.x * y_12 + r_2.y * x_12; + double p_q = (r_2.x * r_2.y - d_q) / d_q; + double s_c = sqrt(max(0.0, p_q)); + if (flagA == flagS) + { + s_c *= -1.0; + } + Point c{ r.x * s_c * y_1 / r.y, -r.y * s_c * x_1 / r.x }; + double c_x = c.x * cos(phi) - c.y * sin(phi) + (beginPoint.x + endPoint.x) / 2.0; + double c_y = c.x * sin(phi) + c.y * cos(phi) + (beginPoint.y + endPoint.y) / 2.0; + Point v{ (x_1 - c.x) / r.x, (y_1 - c.y) / r.y }; + double theta_1 = angleConverter(Point{ 1, 0 }, v); + double deltTheta = angleConverter(v, Point{ (-x_1 - c.x) / r.x, (-y_1 - c.y) / r.y }); + while (deltTheta - 2.0 * PI < eps) + deltTheta += 2.0 * PI; + while (deltTheta - 2.0 * PI >= eps) + deltTheta -= 2.0 * PI; + if (!flagS) + { + deltTheta -= 2.0 * PI; + } + return vector{r.x, r.y, c_x, c_y, theta_1, deltTheta}; } double SvgParser::angleConverter(Point u, Point v) { - double n_u = sqrt(u.x * u.x + u.y * u.y); - double n_v = sqrt(v.x * v.x + v.y * v.y); - double a_c = (u.x * v.x + u.y * v.y) / n_u / n_v; - if (a_c - 1.0 > eps) - a_c = 1.0; - else if (a_c + 1.0 < eps) - a_c = -1.0; - a_c = acos(a_c); - if (u.x * v.y < u.y * v.x) - a_c = -a_c; - return a_c; + double n_u = sqrt(u.x * u.x + u.y * u.y); + double n_v = sqrt(v.x * v.x + v.y * v.y); + double a_c = (u.x * v.x + u.y * v.y) / n_u / n_v; + if (a_c - 1.0 > eps) + a_c = 1.0; + else if (a_c + 1.0 < eps) + a_c = -1.0; + a_c = acos(a_c); + if (u.x * v.y < u.y * v.x) + a_c = -a_c; + return a_c; } Point SvgParser::eConverter(Point c, Point r, double cosPhi, double sinPhi, double t) { - double a = r.x * cos(t); - double b = r.y * sin(t); - return Point{c.x + a * cosPhi - b * sinPhi, c.y + a * sinPhi - b * cosPhi}; + double a = r.x * cos(t); + double b = r.y * sin(t); + return Point{ c.x + a * cosPhi - b * sinPhi, c.y + a * sinPhi - b * cosPhi }; } Point SvgParser::e2Converter(Point c, Point r, double cosPhi, double sinPhi, double t) { - double a = -r.x * cos(t); - double b = r.y * sin(t); - return Point{a * cosPhi - b * sinPhi, a * sinPhi - b * cosPhi}; + double a = -r.x * cos(t); + double b = r.y * sin(t); + return Point{ a * cosPhi - b * sinPhi, a * sinPhi - b * cosPhi }; } diff --git a/README.md b/README.md index 7080096..b091f37 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## ä¾èµ– - Qt 5.15.2 (MSVC 2019 64-bit) -- Assimp (x64-windows) +- [Assimp](https://github.com/assimp/assimp) (x64-windows,建议使用vcpkg安装) ## 构建 @@ -17,4 +17,4 @@ 已实现BVH加速结构的建立 -基本实现å•å›¾å…ƒçš„拆分,å³åˆ’分网格并生æˆå¿«æ·æ®µå’Œç¼ ç»•å¢žé‡ï¼Œå°šæœªéªŒè¯æ­£ç¡®æ€§ \ No newline at end of file +已实现å•å›¾å…ƒçš„拆分,å³åˆ’分网格并生æˆå¿«æ·æ®µå’Œç¼ ç»•å¢žé‡ \ No newline at end of file