From 14b65e193b3ec3471101ca3721b422f252a10474 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Thu, 2 Jan 2020 07:58:33 +0930 Subject: [PATCH] Improve Marker pen Take front view contour into calculation for x-axis. --- src/contourtopartconverter.cpp | 29 ++++++++++++++++++++++++++++- src/contourtopartconverter.h | 2 ++ src/skeletongraphicswidget.cpp | 9 +++++++-- src/skeletongraphicswidget.h | 4 ++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/contourtopartconverter.cpp b/src/contourtopartconverter.cpp index 2bfbb185..4355d494 100644 --- a/src/contourtopartconverter.cpp +++ b/src/contourtopartconverter.cpp @@ -187,16 +187,43 @@ void ContourToPartConverter::optimizeNodes() } } +void ContourToPartConverter::alignSkeleton(const std::vector> &referenceSkeleton, + std::vector> &adjustSkeleton) +{ + if (referenceSkeleton.empty() || adjustSkeleton.empty()) + return; + float sumOfDistance2 = 0.0; + float reversedSumOfDistance2 = 0.0; + for (size_t i = 0; i < adjustSkeleton.size(); ++i) { + size_t j = ((float)i / adjustSkeleton.size()) * referenceSkeleton.size(); + if (j >= referenceSkeleton.size()) + continue; + size_t k = referenceSkeleton.size() - 1 - j; + sumOfDistance2 += std::pow(adjustSkeleton[i].first.y() - referenceSkeleton[j].first.y(), 2.0f); + reversedSumOfDistance2 += std::pow(adjustSkeleton[i].first.y() - referenceSkeleton[k].first.y(), 2.0f); + } + if (sumOfDistance2 <= reversedSumOfDistance2) + return; + std::reverse(adjustSkeleton.begin(), adjustSkeleton.end()); +} + void ContourToPartConverter::convert() { std::vector> sideSkeleton; + std::vector> mainSkeleton; auto mainBoundingBox = m_mainProfile.boundingRect(); extractSkeleton(m_sideProfile, &sideSkeleton); + extractSkeleton(m_mainProfile, &mainSkeleton); + smoothRadius(&sideSkeleton); + smoothRadius(&mainSkeleton); if (!sideSkeleton.empty()) { - float x = mainBoundingBox.center().x() / m_canvasSize.height(); + alignSkeleton(sideSkeleton, mainSkeleton); + float defaultX = mainBoundingBox.center().x() / m_canvasSize.height(); m_nodes.reserve(sideSkeleton.size()); for (size_t i = 0; i < sideSkeleton.size(); ++i) { const auto &it = sideSkeleton[i]; + size_t j = ((float)i / sideSkeleton.size()) * mainSkeleton.size(); + float x = j < mainSkeleton.size() ? mainSkeleton[j].first.x() : defaultX; m_nodes.push_back(std::make_pair(QVector3D(x, it.first.y(), it.first.x()), it.second)); } diff --git a/src/contourtopartconverter.h b/src/contourtopartconverter.h index e9d904b9..1dd02272 100644 --- a/src/contourtopartconverter.h +++ b/src/contourtopartconverter.h @@ -34,6 +34,8 @@ private: const std::set> &black); void nodesToSnapshot(); void smoothRadius(std::vector> *skeleton); + void alignSkeleton(const std::vector> &referenceSkeleton, + std::vector> &adjustSkeleton); void optimizeNodes(); }; diff --git a/src/skeletongraphicswidget.cpp b/src/skeletongraphicswidget.cpp index ccc6a594..68fdc428 100644 --- a/src/skeletongraphicswidget.cpp +++ b/src/skeletongraphicswidget.cpp @@ -141,7 +141,8 @@ void SkeletonGraphicsWidget::setBackgroundBlur(float turnaroundOpacity) void SkeletonGraphicsWidget::shortcutEscape() { - if (SkeletonDocumentEditMode::Add == m_document->editMode) { + if (SkeletonDocumentEditMode::Add == m_document->editMode || + SkeletonDocumentEditMode::Mark == m_document->editMode) { emit setEditMode(SkeletonDocumentEditMode::Select); return; } @@ -725,6 +726,10 @@ void SkeletonGraphicsWidget::updateCursor() m_cursorNodeItem->hide(); } + if (SkeletonDocumentEditMode::Mark != m_document->editMode) { + m_markerItem->reset(); + } + switch (m_document->editMode) { case SkeletonDocumentEditMode::Add: setCursor(QCursor(Theme::awesome()->icon(fa::plus).pixmap(Theme::toolIconFontSize, Theme::toolIconFontSize))); @@ -1455,6 +1460,7 @@ bool SkeletonGraphicsWidget::mouseRelease(QMouseEvent *event) const QPolygonF &previousPolygon = m_markerItem->previousPolygon(); if (previousPolygon.empty()) { m_markerItem->save(); + m_markerItem->toggleProfile(); } else { if (m_markerItem->isMainProfile()) { emit addPartByPolygons(m_markerItem->polygon(), previousPolygon, sceneRect().size()); @@ -1464,7 +1470,6 @@ bool SkeletonGraphicsWidget::mouseRelease(QMouseEvent *event) m_markerItem->reset(); } m_markerItem->hide(); - m_markerItem->toggleProfile(); updateCursor(); } else { m_markerItem->clear(); diff --git a/src/skeletongraphicswidget.h b/src/skeletongraphicswidget.h index f10bfa6f..00921dc7 100644 --- a/src/skeletongraphicswidget.h +++ b/src/skeletongraphicswidget.h @@ -171,6 +171,10 @@ public: { m_previousPolygon.clear(); clear(); + if (!m_mainProfile) { + m_mainProfile = true; + updateAppearance(); + } } private: QPolygonF m_polygon;