增加zIndex,减少重复contour的生成

dev-wuyize
yang.yongquan 2023-03-22 13:02:43 +08:00
parent 2e7d20d0d7
commit 56777c1f00
2 changed files with 48 additions and 22 deletions

View File

@ -13,12 +13,12 @@ using std::max;
using std::shared_ptr; using std::shared_ptr;
using std::make_shared; using std::make_shared;
using std::min; using std::min;
using std::queue;
struct LayerNode { int PaintingUtil::zIndexCount = 0;
LayerWrapper* nowLayer;
QTransform transfrom; void PaintingUtil::clear() {
}; zIndexCount = 0;
}
QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) { QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) {
QFile jsonFile(jsonFilePath); QFile jsonFile(jsonFilePath);
@ -34,9 +34,15 @@ QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) {
Painting PaintingUtil::transfromToPainting(QString jsonFilePath) { Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
QJsonObject jsonObj = readJsonFile(jsonFilePath); QJsonObject jsonObj = readJsonFile(jsonFilePath);
//qDebug() << jsonObj; //qDebug() << jsonObj;
std::unordered_map<LayerWrapper*, Contour> contourMap;
Painting painting(Renderer::Material(jsonObj.value("background-color").toVariant().value<QColor>())); Painting painting(Renderer::Material(jsonObj.value("background-color").toVariant().value<QColor>()));
shared_ptr<ElementManager> elementManager = make_shared<ElementManager>(jsonObj, QFileInfo(jsonFilePath).absolutePath()); shared_ptr<ElementManager> elementManager = make_shared<ElementManager>(jsonObj, QFileInfo(jsonFilePath).absolutePath());
shared_ptr<LayerManager> layerManager = make_shared<LayerManager>(jsonObj, elementManager.get()); shared_ptr<LayerManager> layerManager = make_shared<LayerManager>(jsonObj, elementManager.get());
LayerWrapper* root = layerManager->getRoot();
root->getCache();
handleLayerWrapper(root, QTransform(), painting, contourMap);
clear();
return painting;
//qDebug() << elementManager->toJson(); //qDebug() << elementManager->toJson();
//qDebug() << layerManager->toJson(); //qDebug() << layerManager->toJson();
//qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath; //qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath;
@ -45,12 +51,11 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
//qDebug() << layerManager->toJson(); //qDebug() << layerManager->toJson();
//qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath; //qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath;
//qDebug() << ((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->getCache().painterPath; //qDebug() << ((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->getCache().painterPath;
queue<LayerNode> layerQueue; //queue<LayerNode> layerQueue;
LayerWrapper* root = layerManager->getRoot();
root->getCache();
//double maxLineWidth = getMaxLineWidth(root); //double maxLineWidth = getMaxLineWidth(root);
layerQueue.push({ root, QTransform()}); //layerQueue.push({ root, QTransform()});
while (!layerQueue.empty()) { /*while (!layerQueue.empty()) {
auto layerNode = layerQueue.front(); auto layerNode = layerQueue.front();
layerQueue.pop(); layerQueue.pop();
FolderLayerWrapper* nowLayer = handleLayerWrapper(layerNode.nowLayer, layerNode.transfrom, painting); FolderLayerWrapper* nowLayer = handleLayerWrapper(layerNode.nowLayer, layerNode.transfrom, painting);
@ -59,13 +64,14 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
layerQueue.push({ sonLayer.get(), layerNode.transfrom }); layerQueue.push({ sonLayer.get(), layerNode.transfrom });
} }
} }
} }*/
return painting;
} }
FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, Painting& painting) { void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Painting& painting, std::unordered_map<LayerWrapper*, Contour>& contourMap) {
LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer); LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer);
qDebug() << nowLayer;
transform = nowLayer->property.transform * transform; transform = nowLayer->property.transform * transform;
@ -73,8 +79,8 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr
GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement); GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement);
if (wrapperElement != nullptr) { if (wrapperElement != nullptr) {
transform = wrapperElement->sourceLayer->property.transform * transform; handleLayerWrapper(wrapperElement->sourceLayer, transform, painting, contourMap);
return wrapperElement->sourceLayer; return;
} }
PixelPath pixelPath = nowLayer->getCache(); PixelPath pixelPath = nowLayer->getCache();
@ -84,18 +90,28 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr
// transform to -1£¬ 1 // transform to -1£¬ 1
QTransform trans; QTransform trans;
double maxLen = std::max(bound.width(), bound.height()) * 1.00001; double maxLen = std::max(bound.width(), bound.height()) * 1.00001;
qDebug() << maxLen << bound; //qDebug() << maxLen << bound;
trans.scale(1 / maxLen, 1 / maxLen); trans.scale(1 / maxLen, 1 / maxLen);
trans.translate(-bound.center().x(), -bound.center().y()); trans.translate(-bound.center().x(), -bound.center().y());
painterPath = trans.map(painterPath); //std::shared_ptr<LayerWrapper> keyLayerPtr(nowLayer);
shared_ptr<vector<vector<Renderer::Point> >> contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(painterPath)); auto iterContour = contourMap.find(nowLayer);
PaintingUtil::Contour contour = nullptr;
if (iterContour != contourMap.end()) {
contour = iterContour->second;
}
else {
painterPath = trans.map(painterPath);
contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(painterPath));
contourMap.insert({ nowLayer, contour });
; }
QSize screenSize = QSize(1080, 1080); QSize screenSize = QSize(1080, 1080);
ElementTransform elementTransform; ElementTransform elementTransform;
QTransform leafTransform = trans.inverted() * transform * QTransform::fromScale(2. / screenSize.width(), 2. / screenSize.height()) * QTransform::fromTranslate(-1, -1) * QTransform::fromScale(1, -1); QTransform leafTransform = trans.inverted() * transform * QTransform::fromScale(2. / screenSize.width(), 2. / screenSize.height()) * QTransform::fromTranslate(-1, -1) * QTransform::fromScale(1, -1);
QTransform transformInverted = leafTransform.inverted(); QTransform transformInverted = leafTransform.inverted();
++zIndexCount;
auto baseStyles = leafLayer->styles.toBaseStyles(); auto baseStyles = leafLayer->styles.toBaseStyles();
for (auto& baseStyle : baseStyles) { for (auto& baseStyle : baseStyles) {
@ -123,13 +139,18 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr
transformInverted.m11(), transformInverted.m12(), transformInverted.m21(), transformInverted.m11(), transformInverted.m12(), transformInverted.m21(),
transformInverted.m22(), transformInverted.m31(), transformInverted.m32() transformInverted.m22(), transformInverted.m31(), transformInverted.m32()
); );
elementTransform.zIndex = 0; elementTransform.zIndex = zIndexCount;
painting.addElement(BaseElement{ contour, material }, elementTransform); painting.addElement(BaseElement{ contour, material }, elementTransform);
} }
return nullptr; return;
} }
FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer); FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer);
return folderLayer; if (folderLayer != nullptr) {
for (auto sonLayer : folderLayer->children) {
handleLayerWrapper(sonLayer.get(), transform, painting, contourMap);
}
}
return;
} }

View File

@ -2,12 +2,17 @@
#include "../../Renderer/Painting/Painting.h" #include "../../Renderer/Painting/Painting.h"
#include <QJsonObject> #include <QJsonObject>
#include <ElementManager.h> #include <ElementManager.h>
#include <map>
class PaintingUtil class PaintingUtil
{ {
using Contour = std::shared_ptr<vector<vector<Renderer::Point> > >;
private: private:
static int zIndexCount;
static void clear();
//static const double pi; //static const double pi;
static QJsonObject readJsonFile(QString jsonFilePath); static QJsonObject readJsonFile(QString jsonFilePath);
static FolderLayerWrapper* handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, Renderer::Painting& painting); static void handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Renderer::Painting& painting, std::unordered_map<LayerWrapper*, Contour>& contourMap);
//static double getMaxLineWidth(LayerWrapper* root); //static double getMaxLineWidth(LayerWrapper* root);
public: public:
static Renderer::Painting transfromToPainting(QString jsonFilePath); static Renderer::Painting transfromToPainting(QString jsonFilePath);