diff --git a/src/Mod/Mesh/App/Mesh.cpp b/src/Mod/Mesh/App/Mesh.cpp index fb5084b64..11fd04fd2 100644 --- a/src/Mod/Mesh/App/Mesh.cpp +++ b/src/Mod/Mesh/App/Mesh.cpp @@ -832,23 +832,9 @@ void MeshObject::crossSections(const std::vector& planes, st } } -void MeshObject::cut(const std::vector& polygon, MeshObject::CutType type) +void MeshObject::cut(const Base::Polygon2D& polygon2d, + const Base::ViewProjMethod& proj, MeshObject::CutType type) { - MeshCore::FlatTriangulator tria; - tria.SetPolygon(polygon); - // this gives us the inverse matrix - Base::Matrix4D inv = tria.GetTransformToFitPlane(); - // compute the matrix for the coordinate transformation - Base::Matrix4D mat = inv; - mat.inverseOrthogonal(); - - std::vector poly = tria.ProjectToFitPlane(); - - Base::ViewProjMatrix proj(mat); - Base::Polygon2D polygon2d; - for (std::vector::const_iterator it = poly.begin(); it != poly.end(); ++it) - polygon2d.Add(Base::Vector2D(it->x, it->y)); - MeshCore::MeshAlgorithm meshAlg(this->_kernel); std::vector check; @@ -868,22 +854,9 @@ void MeshObject::cut(const std::vector& polygon, MeshObject::Cut this->deleteFacets(check); } -void MeshObject::trim(const std::vector& polygon, MeshObject::CutType type) +void MeshObject::trim(const Base::Polygon2D& polygon2d, + const Base::ViewProjMethod& proj, MeshObject::CutType type) { - MeshCore::FlatTriangulator tria; - tria.SetPolygon(polygon); - // this gives us the inverse matrix - Base::Matrix4D inv = tria.GetTransformToFitPlane(); - // compute the matrix for the coordinate transformation - Base::Matrix4D mat = inv; - mat.inverseOrthogonal(); - - std::vector poly = tria.ProjectToFitPlane(); - - Base::ViewProjMatrix proj(mat); - Base::Polygon2D polygon2d; - for (std::vector::const_iterator it = poly.begin(); it != poly.end(); ++it) - polygon2d.Add(Base::Vector2D(it->x, it->y)); MeshCore::MeshTrimming trim(this->_kernel, &proj, polygon2d); std::vector check; std::vector triangle; diff --git a/src/Mod/Mesh/App/Mesh.h b/src/Mod/Mesh/App/Mesh.h index a88a06ec3..18a883d2c 100644 --- a/src/Mod/Mesh/App/Mesh.h +++ b/src/Mod/Mesh/App/Mesh.h @@ -49,6 +49,11 @@ namespace Py { class List; } +namespace Base { + class Polygon2D; + class ViewProjMethod; +} + namespace MeshCore { class AbstractPolygonTriangulator; } @@ -200,8 +205,8 @@ public: Base::Vector3d getPointNormal(unsigned long) const; void crossSections(const std::vector&, std::vector §ions, float fMinEps = 1.0e-2f, bool bConnectPolygons = false) const; - void cut(const std::vector& polygon, CutType); - void trim(const std::vector& polygon, CutType); + void cut(const Base::Polygon2D& polygon, const Base::ViewProjMethod& proj, CutType); + void trim(const Base::Polygon2D& polygon, const Base::ViewProjMethod& proj, CutType); //@} /** @name Selection */ diff --git a/src/Mod/Mesh/App/MeshPyImp.cpp b/src/Mod/Mesh/App/MeshPyImp.cpp index c25c6322b..158ef0d43 100644 --- a/src/Mod/Mesh/App/MeshPyImp.cpp +++ b/src/Mod/Mesh/App/MeshPyImp.cpp @@ -1295,7 +1295,21 @@ PyObject* MeshPy::cut(PyObject *args) polygon.push_back(Base::convertTo(pnt)); } - getMeshObjectPtr()->cut(polygon, MeshObject::CutType(mode)); + MeshCore::FlatTriangulator tria; + tria.SetPolygon(polygon); + // this gives us the inverse matrix + Base::Matrix4D inv = tria.GetTransformToFitPlane(); + // compute the matrix for the coordinate transformation + Base::Matrix4D mat = inv; + mat.inverseOrthogonal(); + + polygon = tria.ProjectToFitPlane(); + + Base::ViewProjMatrix proj(mat); + Base::Polygon2D polygon2d; + for (std::vector::const_iterator it = polygon.begin(); it != polygon.end(); ++it) + polygon2d.Add(Base::Vector2D(it->x, it->y)); + getMeshObjectPtr()->cut(polygon2d, proj, MeshObject::CutType(mode)); Py_Return; } @@ -1315,7 +1329,21 @@ PyObject* MeshPy::trim(PyObject *args) polygon.push_back(Base::convertTo(pnt)); } - getMeshObjectPtr()->trim(polygon, MeshObject::CutType(mode)); + MeshCore::FlatTriangulator tria; + tria.SetPolygon(polygon); + // this gives us the inverse matrix + Base::Matrix4D inv = tria.GetTransformToFitPlane(); + // compute the matrix for the coordinate transformation + Base::Matrix4D mat = inv; + mat.inverseOrthogonal(); + + polygon = tria.ProjectToFitPlane(); + + Base::ViewProjMatrix proj(mat); + Base::Polygon2D polygon2d; + for (std::vector::const_iterator it = polygon.begin(); it != polygon.end(); ++it) + polygon2d.Add(Base::Vector2D(it->x, it->y)); + getMeshObjectPtr()->trim(polygon2d, proj, MeshObject::CutType(mode)); Py_Return; } diff --git a/src/Mod/Mesh/Gui/MeshSelection.cpp b/src/Mod/Mesh/Gui/MeshSelection.cpp index a724f96ce..f04272801 100644 --- a/src/Mod/Mesh/Gui/MeshSelection.cpp +++ b/src/Mod/Mesh/Gui/MeshSelection.cpp @@ -29,6 +29,7 @@ # include # include # include +# include #endif #include "MeshSelection.h" @@ -42,6 +43,7 @@ #include #include #include +#include #include #include #include @@ -372,7 +374,10 @@ void MeshSelection::selectGLCallback(void * ud, SoEventCallback * n) const MeshCore::MeshKernel& kernel = mesh.getKernel(); // simply get all triangles under the polygon - vp->getFacetsFromPolygon(polygon, *view, true, faces); + SoCamera* cam = view->getCamera(); + SbViewVolume vv = cam->getViewVolume(); + Gui::ViewVolumeProjection proj(vv); + vp->getFacetsFromPolygon(polygon, proj, true, faces); if (self->onlyVisibleTriangles) { const SbVec2s& sz = view->getViewportRegion().getWindowSize(); short width,height; sz.getValue(width,height); diff --git a/src/Mod/Mesh/Gui/ViewProvider.cpp b/src/Mod/Mesh/Gui/ViewProvider.cpp index 5bd84aaa6..21c5da129 100644 --- a/src/Mod/Mesh/Gui/ViewProvider.cpp +++ b/src/Mod/Mesh/Gui/ViewProvider.cpp @@ -674,10 +674,13 @@ void ViewProviderMesh::clipMeshCallback(void * ud, SoEventCallback * n) if (!views.empty()) { Gui::Application::Instance->activeDocument()->openCommand("Cut"); for (std::vector::iterator it = views.begin(); it != views.end(); ++it) { - ViewProviderMesh* that = static_cast(*it); - if (that->getEditingMode() > -1) { - that->finishEditing(); - that->cutMesh(clPoly, *view, clip_inner); + ViewProviderMesh* self = static_cast(*it); + if (self->getEditingMode() > -1) { + self->finishEditing(); + SoCamera* cam = view->getCamera(); + SbViewVolume vv = cam->getViewVolume(); + Gui::ViewVolumeProjection proj(vv); + self->cutMesh(clPoly, proj, clip_inner); } } @@ -709,10 +712,13 @@ void ViewProviderMesh::trimMeshCallback(void * ud, SoEventCallback * n) if (!views.empty()) { Gui::Application::Instance->activeDocument()->openCommand("Cut"); for (std::vector::iterator it = views.begin(); it != views.end(); ++it) { - ViewProviderMesh* that = static_cast(*it); - if (that->getEditingMode() > -1) { - that->finishEditing(); - that->trimMesh(clPoly, *view, clip_inner); + ViewProviderMesh* self = static_cast(*it); + if (self->getEditingMode() > -1) { + self->finishEditing(); + SoCamera* cam = view->getCamera(); + SbViewVolume vv = cam->getViewVolume(); + Gui::ViewVolumeProjection proj(vv); + self->trimMesh(clPoly, proj, clip_inner); } } @@ -889,15 +895,12 @@ void ViewProviderMesh::selectGLCallback(void * ud, SoEventCallback * n) } void ViewProviderMesh::getFacetsFromPolygon(const std::vector& picked, - Gui::View3DInventorViewer &Viewer, + const Base::ViewProjMethod& proj, SbBool inner, std::vector& indices) const { #if 1 bool ok = true; - SoCamera* cam = Viewer.getCamera(); - SbViewVolume vv = cam->getViewVolume(); - Gui::ViewVolumeProjection proj(vv); Base::Polygon2D polygon; for (std::vector::const_iterator it = picked.begin(); it != picked.end(); ++it) polygon.Add(Base::Vector2D((*it)[0],(*it)[1])); @@ -1115,69 +1118,27 @@ std::vector ViewProviderMesh::getVisibleFacets(const SbViewportRe } void ViewProviderMesh::cutMesh(const std::vector& picked, - Gui::View3DInventorViewer &Viewer, SbBool inner) + const Base::ViewProjMethod& proj, SbBool inner) { // Get the facet indices inside the tool mesh std::vector indices; - getFacetsFromPolygon(picked, Viewer, inner, indices); + getFacetsFromPolygon(picked, proj, inner, indices); removeFacets(indices); } void ViewProviderMesh::trimMesh(const std::vector& polygon, - Gui::View3DInventorViewer& viewer, SbBool inner) + const Base::ViewProjMethod& proj, SbBool inner) { - // get the drawing plane - SbViewVolume vol = viewer.getCamera()->getViewVolume(); - SbPlane drawPlane = vol.getPlane(viewer.getCamera()->focalDistance.getValue()); - - std::vector indices; Mesh::MeshObject* mesh = static_cast(pcObject)->Mesh.startEditing(); - MeshCore::MeshFacetGrid meshGrid(mesh->getKernel()); - MeshCore::MeshAlgorithm meshAlg(mesh->getKernel()); -#if 0 - for (std::vector::const_iterator it = polygon.begin(); it != polygon.end(); ++it) { - // the following element - std::vector::const_iterator nt = it + 1; - if (nt == polygon.end()) - break; - else if (*it == *nt) - continue; // two adjacent vertices are equal - - SbVec3f p1,p2; - SbLine l1, l2; - vol.projectPointToLine(*it, l1); - drawPlane.intersect(l1, p1); - vol.projectPointToLine(*nt, l2); - drawPlane.intersect(l2, p2); - - SbPlane plane(l1.getPosition(), l2.getPosition(), - l1.getPosition()+l1.getDirection()); - const SbVec3f& n = plane.getNormal(); - float d = plane.getDistanceFromOrigin(); - meshAlg.GetFacetsFromPlane(meshGrid, - Base::Vector3f(n[0],n[1],n[2]), d, - Base::Vector3f(p1[0],p1[1],p1[2]), - Base::Vector3f(p2[0],p2[1],p2[2]), indices); - } -#endif - - Gui::ViewVolumeProjection proj(vol); Base::Polygon2D polygon2d; for (std::vector::const_iterator it = polygon.begin(); it != polygon.end(); ++it) polygon2d.Add(Base::Vector2D((*it)[0],(*it)[1])); - MeshCore::MeshTrimming trim(mesh->getKernel(), &proj, polygon2d); - std::vector check; - std::vector triangle; - trim.SetInnerOrOuter(inner ? MeshCore::MeshTrimming::INNER : MeshCore::MeshTrimming::OUTER); - trim.CheckFacets(meshGrid, check); - trim.TrimFacets(check, triangle); - mesh->deleteFacets(check); - if (!triangle.empty()) { - mesh->getKernel().AddFacets(triangle); - } - //Remove the facets from the mesh and open a transaction object for the undo/redo stuff - //mesh->deleteFacets(indices); + + Mesh::MeshObject::CutType type = inner ? + Mesh::MeshObject::INNER : + Mesh::MeshObject::OUTER; + mesh->trim(polygon2d, proj, type); static_cast(pcObject)->Mesh.finishEditing(); pcObject->purgeTouched(); } diff --git a/src/Mod/Mesh/Gui/ViewProvider.h b/src/Mod/Mesh/Gui/ViewProvider.h index 29b98065a..79648b1b9 100644 --- a/src/Mod/Mesh/Gui/ViewProvider.h +++ b/src/Mod/Mesh/Gui/ViewProvider.h @@ -52,6 +52,10 @@ namespace App { class Color; } +namespace Base { + class ViewProjMethod; +} + namespace Gui { class View3DInventorViewer; class SoFCSelection; @@ -139,7 +143,7 @@ public: void clearSelection(); void deleteSelection(); void getFacetsFromPolygon(const std::vector& picked, - Gui::View3DInventorViewer &Viewer, SbBool inner, + const Base::ViewProjMethod& proj, SbBool inner, std::vector& indices) const; std::vector getFacetsOfRegion(const SbViewportRegion&, const SbViewportRegion&, SoCamera*) const; std::vector getVisibleFacetsAfterZoom(const SbBox2s&, const SbViewportRegion&, SoCamera*) const; @@ -156,8 +160,8 @@ protected: void onChanged(const App::Property* prop); virtual void showOpenEdges(bool); void setOpenEdgeColorFrom(const App::Color& col); - virtual void cutMesh(const std::vector& picked, Gui::View3DInventorViewer &Viewer, SbBool inner); - virtual void trimMesh(const std::vector& picked, Gui::View3DInventorViewer &Viewer, SbBool inner); + virtual void cutMesh(const std::vector& picked, const Base::ViewProjMethod& proj, SbBool inner); + virtual void trimMesh(const std::vector& picked, const Base::ViewProjMethod& proj, SbBool inner); virtual void splitMesh(const MeshCore::MeshKernel& toolMesh, const Base::Vector3f& normal, SbBool inner); virtual void segmentMesh(const MeshCore::MeshKernel& toolMesh, const Base::Vector3f& normal, SbBool inner); virtual void faceInfo(unsigned long facet);