From d86004b46a37dab0f7acc52b05f41d48533eebfb Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Fri, 30 Mar 2018 22:05:47 +0800 Subject: [PATCH] Add toggleable options for generating --- src/skeletoneditgraphicsview.cpp | 33 +++++++++++++++++++++++++++++++- src/skeletoneditgraphicsview.h | 6 ++++++ src/skeletontomesh.cpp | 29 ++++++++++++++++++++++------ src/theme.cpp | 4 ++-- src/unionmesh.cpp | 27 ++++++++++++++++++++------ src/unionmesh.h | 1 + 6 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/skeletoneditgraphicsview.cpp b/src/skeletoneditgraphicsview.cpp index 099ea691..92903dc9 100644 --- a/src/skeletoneditgraphicsview.cpp +++ b/src/skeletoneditgraphicsview.cpp @@ -26,7 +26,10 @@ SkeletonEditGraphicsView::SkeletonEditGraphicsView(QWidget *parent) : m_isMovingNodeItem(false), m_backgroundLoaded(false), m_modelWidget(NULL), - m_modelWidgetProxy(NULL) + m_modelWidgetProxy(NULL), + m_combineEnabled(false), + m_unionEnabled(true), + m_subdivEnabled(false) { setScene(new QGraphicsScene()); @@ -218,6 +221,15 @@ bool SkeletonEditGraphicsView::keyPress(QKeyEvent *event, const QPointF &scenePo if (event->key() == Qt::Key_A) { toggleAddNodeMode(); processed = true; + } else if (event->key() == Qt::Key_C) { + m_combineEnabled = !m_combineEnabled; + emit nodesChanged(); + } else if (event->key() == Qt::Key_U) { + m_unionEnabled = !m_unionEnabled; + emit nodesChanged(); + } else if (event->key() == Qt::Key_S) { + m_subdivEnabled = !m_subdivEnabled; + emit nodesChanged(); } else if (event->key() == Qt::Key_Delete || event->key() ==Qt::Key_Backspace) { removeSelectedItems(); processed = true; @@ -448,6 +460,8 @@ bool SkeletonEditGraphicsView::wheel(QWheelEvent *event, const QPointF &scenePos processed = true; emit nodesChanged(); } + if (m_inAddNodeMode) + processed = true; return processed; } @@ -562,6 +576,9 @@ void SkeletonEditGraphicsView::saveToSnapshot(SkeletonSnapshot *snapshot) { snapshot->canvas["width"] = QString("%1").arg(scene()->sceneRect().width()); snapshot->canvas["height"] = QString("%1").arg(scene()->sceneRect().height()); + snapshot->canvas["combine"] = m_combineEnabled ? "true" : "false"; + snapshot->canvas["union"] = m_unionEnabled ? "true" : "false"; + snapshot->canvas["subdiv"] = m_subdivEnabled ? "true" : "false"; QList::iterator it; QList list = scene()->items(); @@ -675,4 +692,18 @@ void SkeletonEditGraphicsView::keyPressEvent(QKeyEvent *event) keyPress(event, QPointF()); } +bool SkeletonEditGraphicsView::combineEnabled() +{ + return m_combineEnabled; +} + +bool SkeletonEditGraphicsView::unionEnabled() +{ + return m_unionEnabled; +} + +bool SkeletonEditGraphicsView::subdivEnabled() +{ + return m_subdivEnabled; +} diff --git a/src/skeletoneditgraphicsview.h b/src/skeletoneditgraphicsview.h index 9cd3d23b..bd603ea6 100644 --- a/src/skeletoneditgraphicsview.h +++ b/src/skeletoneditgraphicsview.h @@ -41,6 +41,9 @@ public: void saveToSnapshot(SkeletonSnapshot *snapshot); void loadFromSnapshot(SkeletonSnapshot *snapshot); ModelWidget *modelWidget(); + bool combineEnabled(); + bool unionEnabled(); + bool subdivEnabled(); protected: void mouseMoveEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); @@ -63,6 +66,9 @@ private: bool m_backgroundLoaded; ModelWidget *m_modelWidget; QGraphicsProxyWidget *m_modelWidgetProxy; + bool m_combineEnabled; + bool m_unionEnabled; + bool m_subdivEnabled; private: void toggleAddNodeMode(); void applyAddNodeMode(); diff --git a/src/skeletontomesh.cpp b/src/skeletontomesh.cpp index e9d6bebc..463d7899 100644 --- a/src/skeletontomesh.cpp +++ b/src/skeletontomesh.cpp @@ -54,6 +54,9 @@ void SkeletonToMesh::process() float frontMiddleX = m_snapshot.nodes[rootNodeId]["x"].toFloat(); float frontMiddleY = m_snapshot.nodes[rootNodeId]["y"].toFloat(); float sideMiddleX = m_snapshot.nodes[m_snapshot.nodes[rootNodeId]["nextSidePair"]]["x"].toFloat(); + bool combineEnabled = "true" == m_snapshot.canvas["combine"]; + bool unionEnabled = "true" == m_snapshot.canvas["union"]; + bool subdivEnabled = "true" == m_snapshot.canvas["subdiv"]; m_snapshot.splitByConnectivity(&groups); std::vector meshIds; @@ -64,6 +67,7 @@ void SkeletonToMesh::process() //QRectF side = skeleton->boundingBoxSide(); //float canvasWidth = skeleton->canvas["width"].toFloat(); //float canvasHeight = skeleton->canvas["height"].toFloat(); + float canvasHeight = globalFront.height(); std::map bmeshNodeMap; @@ -75,11 +79,11 @@ void SkeletonToMesh::process() std::map>::iterator nextSidePair = skeleton->nodes.find(nodeIterator->second["nextSidePair"]); if (nextSidePair == skeleton->nodes.end()) continue; - float x = (nodeIterator->second["x"].toFloat() - frontMiddleX) / globalFront.height(); - float y = (nodeIterator->second["y"].toFloat() - frontMiddleY) / globalFront.height(); - float z = (nextSidePair->second["x"].toFloat() - sideMiddleX) / globalFront.height(); - float r = nodeIterator->second["radius"].toFloat() / globalFront.height(); - float t = nextSidePair->second["radius"].toFloat() / globalFront.height(); + float x = (nodeIterator->second["x"].toFloat() - frontMiddleX) / canvasHeight; + float y = (nodeIterator->second["y"].toFloat() - frontMiddleY) / canvasHeight; + float z = (nextSidePair->second["x"].toFloat() - sideMiddleX) / canvasHeight; + float r = nodeIterator->second["radius"].toFloat() / canvasHeight; + float t = nextSidePair->second["radius"].toFloat() / canvasHeight; int bmeshNodeId = meshlite_bmesh_add_node(meshliteContext, bmeshId, x, y, z, r, t); printf("meshlite_bmesh_add_node x:%f y:%f z:%f r:%f t:%f nodeName:%s bmeshNodeId:%d\n", x, y, z, r, t, nodeIterator->first.toUtf8().constData(), bmeshNodeId); bmeshNodeMap[nodeIterator->first] = bmeshNodeId; @@ -106,8 +110,21 @@ void SkeletonToMesh::process() } if (meshIds.size() > 0) { - int mergedMeshId = unionMeshs(meshliteContext, meshIds); + int mergedMeshId = 0; + if (unionEnabled) { + mergedMeshId = unionMeshs(meshliteContext, meshIds); + } else { + mergedMeshId = mergeMeshs(meshliteContext, meshIds); + } + if (subdivEnabled) { + if (mergedMeshId > 0) { + mergedMeshId = meshlite_subdivide(meshliteContext, mergedMeshId); + } + } if (mergedMeshId > 0) { + if (combineEnabled) { + mergedMeshId = meshlite_combine_adj_faces(meshliteContext, mergedMeshId); + } m_mesh = new Mesh(meshliteContext, mergedMeshId); } } diff --git a/src/theme.cpp b/src/theme.cpp index 91001ea2..79685aef 100644 --- a/src/theme.cpp +++ b/src/theme.cpp @@ -17,10 +17,10 @@ QColor Theme::red = QColor(0xff, 0xff, 0xff); QColor Theme::green = QColor(0xff, 0xff, 0xff); QColor Theme::blue = QColor(0x2a, 0x5a, 0xac); -float Theme::normalAlpha = 128.0 / 255; +float Theme::normalAlpha = 96.0 / 255; float Theme::branchAlpha = 64.0 / 255; float Theme::checkedAlpha = 1.0; -float Theme::edgeAlpha = Theme::normalAlpha / 2; +float Theme::edgeAlpha = 1.0; float Theme::fillAlpha = 50.0 / 255; int Theme::skeletonNodeBorderSize = 2; int Theme::skeletonEdgeWidth = 2; diff --git a/src/unionmesh.cpp b/src/unionmesh.cpp index 2f26c3f0..1444be05 100644 --- a/src/unionmesh.cpp +++ b/src/unionmesh.cpp @@ -115,7 +115,13 @@ int makeMeshliteMeshFromCgal(void *meshlite, CgalMesh *mesh) CgalMesh *unionCgalMeshs(CgalMesh *first, CgalMesh *second) { CgalMesh *mesh = new CgalMesh; - if (!PMP::corefine_and_compute_union(*first, *second, *mesh)) { + try { + if (!PMP::corefine_and_compute_union(*first, *second, *mesh)) { + delete mesh; + return NULL; + } + } catch (...) { + delete mesh; return NULL; } return mesh; @@ -236,9 +242,13 @@ int unionMeshs(void *meshliteContext, const std::vector &meshIds) } catch (...) { // ignore; } - delete mergedExternalMesh; delete externalMeshs[i]; - mergedExternalMesh = unionedExternalMesh; + if (unionedExternalMesh) { + delete mergedExternalMesh; + mergedExternalMesh = unionedExternalMesh; + } else { + // TOOD: + } } if (mergedExternalMesh) { int mergedMeshId = makeMeshliteMeshFromExternal(meshliteContext, mergedExternalMesh); @@ -247,11 +257,16 @@ int unionMeshs(void *meshliteContext, const std::vector &meshIds) } } #else + return mergeMeshs(meshliteContext, meshIds); +#endif + return 0; +} + +int mergeMeshs(void *meshliteContext, const std::vector &meshIds) +{ int mergedMeshId = meshIds[0]; for (size_t i = 1; i < meshIds.size(); i++) { mergedMeshId = meshlite_merge(meshliteContext, mergedMeshId, meshIds[i]); } return mergedMeshId; -#endif - return 0; -} \ No newline at end of file +} diff --git a/src/unionmesh.h b/src/unionmesh.h index 20a627e0..c767c6d6 100644 --- a/src/unionmesh.h +++ b/src/unionmesh.h @@ -2,6 +2,7 @@ #define UNION_MESH_H #include +int mergeMeshs(void *meshliteContext, const std::vector &meshIds); int unionMeshs(void *meshliteContext, const std::vector &meshIds); #endif