From 6b83807dc6b4979e6371d8b693c26ff8dba38d8e Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 19 Jan 2012 15:42:34 +0000 Subject: [PATCH] + simplify testing .qm files + some triangulation stuff git-svn-id: https://free-cad.svn.sourceforge.net/svnroot/free-cad/trunk@5417 e8eeb9e2-ec13-0410-a4a9-efa5cf37419d --- src/Gui/CommandTest.cpp | 21 ++++++ src/Mod/Mesh/App/Core/Algorithm.cpp | 61 ++++++++++------- src/Mod/Mesh/App/Core/TopoAlgorithm.cpp | 90 +++++++++++-------------- src/Mod/Mesh/App/Core/Triangulation.cpp | 46 ++++++++++++- src/Mod/Mesh/App/Core/Triangulation.h | 32 +++++++-- src/Mod/Test/InitGui.py | 2 +- 6 files changed, 167 insertions(+), 85 deletions(-) diff --git a/src/Gui/CommandTest.cpp b/src/Gui/CommandTest.cpp index c43fc494a..a02a7d8e8 100644 --- a/src/Gui/CommandTest.cpp +++ b/src/Gui/CommandTest.cpp @@ -86,6 +86,26 @@ void Std_TestQM::activated(int iMsg) } } +//=========================================================================== +// Std_TestReloadQM +//=========================================================================== +DEF_STD_CMD(Std_TestReloadQM); + +Std_TestReloadQM::Std_TestReloadQM() + : Command("Std_TestReloadQM") +{ + sGroup = "Standard-Test"; + sMenuText = "Reload translation files"; + sToolTipText = "Test function to check .qm translation files"; + sWhatsThis = sToolTipText; + sStatusTip = sToolTipText; +} + +void Std_TestReloadQM::activated(int iMsg) +{ + Translator::instance()->activateLanguage(Translator::instance()->activeLanguage().c_str()); +} + //=========================================================================== // Std_Test1 //=========================================================================== @@ -626,6 +646,7 @@ void CreateTestCommands(void) CommandManager &rcCmdMgr = Application::Instance->commandManager(); rcCmdMgr.addCommand(new Std_TestQM()); + rcCmdMgr.addCommand(new Std_TestReloadQM()); rcCmdMgr.addCommand(new FCCmdTest1()); rcCmdMgr.addCommand(new FCCmdTest2()); rcCmdMgr.addCommand(new FCCmdTest3()); diff --git a/src/Mod/Mesh/App/Core/Algorithm.cpp b/src/Mod/Mesh/App/Core/Algorithm.cpp index 512b98e77..cf051fef9 100644 --- a/src/Mod/Mesh/App/Core/Algorithm.cpp +++ b/src/Mod/Mesh/App/Core/Algorithm.cpp @@ -626,23 +626,23 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, unsigned long refPoint0 = *(boundary.begin()); unsigned long refPoint1 = *(boundary.begin()+1); if (pP2FStructure) { - const std::set& ring = (*pP2FStructure)[refPoint0]; - bool found = false; - for (std::set::const_iterator it = ring.begin(); it != ring.end() && !found; ++it) { - for (int i=0; i<3; i++) { - if (pP2FStructure->getFacet(*it)->_aulPoints[i] == refPoint1) { - rFace = *pP2FStructure->getFacet(*it); - rTriangle = _rclMesh.GetFacet(rFace); - found = true; - break; - } - } - } - } else { + const std::set& ring1 = (*pP2FStructure)[refPoint0]; + const std::set& ring2 = (*pP2FStructure)[refPoint1]; + std::vector f_int; + std::set_intersection(ring1.begin(), ring1.end(), ring2.begin(), ring2.end(), + std::back_insert_iterator >(f_int)); + if (f_int.size() != 1) + return false; // error, this must be an open edge! + + rFace = _rclMesh._aclFacetArray[f_int.front()]; + rTriangle = _rclMesh.GetFacet(rFace); + } + else { bool ready = false; for (MeshFacetArray::_TConstIterator it = _rclMesh._aclFacetArray.begin(); it != _rclMesh._aclFacetArray.end(); ++it) { for (int i=0; i<3; i++) { - if ((it->_aulPoints[i] == refPoint0) && (it->_aulPoints[(i+1)%3] == refPoint1)) { + if ((it->_aulPoints[i] == refPoint0) && (it->_aulPoints[(i+1)%3] == refPoint1) || + (it->_aulPoints[i] == refPoint1) && (it->_aulPoints[(i+1)%3] == refPoint0)) { rFace = *it; rTriangle = _rclMesh.GetFacet(*it); ready = true; @@ -663,7 +663,9 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, } // remove the last added point if it is duplicated + std::vector bounds = boundary; if (boundary.front() == boundary.back()) { + bounds.pop_back(); polygon.pop_back(); rPoints.pop_back(); } @@ -672,6 +674,7 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, // Afterwards we can compare the normals of the created triangles with the z-direction of our local coordinate system. // If the scalar product is positive it was a hole, otherwise not. cTria.SetPolygon(polygon); + cTria.SetIndices(bounds); std::vector surf_pts = cTria.GetPolygon(); if (pP2FStructure && level > 0) { @@ -685,7 +688,7 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, if (cTria.TriangulatePolygon()) { // if we have enough points then we fit a surface through the points and project // the added points onto this surface - cTria.ProjectOntoSurface(surf_pts); + cTria.PostProcessing(surf_pts); // get the facets and add the additional points to the array rFaces.insert(rFaces.end(), cTria.GetFacets().begin(), cTria.GetFacets().end()); std::vector newVertices = cTria.AddedPoints(); @@ -693,7 +696,7 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, rPoints.push_back((*pt)); } - // Unfortunately, the CDT algorithm does not care about the orientation of the polygon so we cannot rely on the normal + // Unfortunately, some algorithms do not care about the orientation of the polygon so we cannot rely on the normal // criterion to decide whether it's a hole or not. // std::vector faces = cTria.GetFacets(); @@ -701,11 +704,13 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, // Special case handling for a hole with three edges: the resulting facet might be coincident with the // reference facet if (faces.size()==1){ - MeshFacet first; - first._aulPoints[0] = boundary[faces.front()._aulPoints[0]]; - first._aulPoints[1] = boundary[faces.front()._aulPoints[1]]; - first._aulPoints[2] = boundary[faces.front()._aulPoints[2]]; - if ( first.IsEqual(rFace) ) { + MeshFacet first = faces.front(); + if (cTria.NeedsReindexing()) { + first._aulPoints[0] = boundary[first._aulPoints[0]]; + first._aulPoints[1] = boundary[first._aulPoints[1]]; + first._aulPoints[2] = boundary[first._aulPoints[2]]; + } + if (first.IsEqual(rFace)) { rFaces.clear(); rPoints.clear(); cTria.Discard(); @@ -717,9 +722,14 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, MeshFacet facet; unsigned short ref_side = rFace.Side(refPoint0, refPoint1); unsigned short tri_side = USHRT_MAX; + if (cTria.NeedsReindexing()) { + // the referenced indices of the polyline + refPoint0 = 0; + refPoint1 = 1; + } if (ref_side < USHRT_MAX) { for (std::vector::iterator it = faces.begin(); it != faces.end(); ++it) { - tri_side = it->Side(0, 1); + tri_side = it->Side(refPoint0, refPoint1); if (tri_side < USHRT_MAX) { facet = *it; break; @@ -737,10 +747,9 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, return false; } +#if 1 MeshGeomFacet triangle; - triangle._aclPoints[0] = rPoints[facet._aulPoints[0]]; - triangle._aclPoints[1] = rPoints[facet._aulPoints[1]]; - triangle._aclPoints[2] = rPoints[facet._aulPoints[2]]; + triangle = cTria.GetTriangle(rPoints, facet); // Now we have two adjacent triangles which we check for overlaps. // Therefore we build a separation plane that must separate the two diametrically opposed points. @@ -763,6 +772,7 @@ bool MeshAlgorithm::FillupHole(const std::vector& boundary, for (MeshFacetArray::_TIterator it = rFaces.begin(); it != rFaces.end(); ++it) it->FlipNormal(); } +#endif return true; } @@ -1764,6 +1774,7 @@ void MeshRefPointToFacets::SearchNeighbours(MeshFacetArray::_TConstIterator f_it rclNb.push_back(f_it); f_it->SetFlag(MeshFacet::VISIT); + MeshPointArray::_TConstIterator p_beg = _rclMesh.GetPoints().begin(); MeshFacetArray::_TConstIterator f_beg = _rclMesh.GetFacets().begin(); for (int i = 0; i < 3; i++) { diff --git a/src/Mod/Mesh/App/Core/TopoAlgorithm.cpp b/src/Mod/Mesh/App/Core/TopoAlgorithm.cpp index 86eefb06e..11ff5c764 100644 --- a/src/Mod/Mesh/App/Core/TopoAlgorithm.cpp +++ b/src/Mod/Mesh/App/Core/TopoAlgorithm.cpp @@ -1237,55 +1237,20 @@ void MeshTopoAlgorithm::FillupHoles(unsigned long length, int level, std::list >& aFailed) { // get the mesh boundaries as an array of point indices - std::list > aBorders; + std::list > aBorders, aFillBorders; MeshAlgorithm cAlgo(_rclMesh); cAlgo.GetMeshBorders(aBorders); // split boundary loops if needed cAlgo.SplitBoundaryLoops(aBorders); - // get the facets to a point - MeshRefPointToFacets cPt2Fac(_rclMesh); - - MeshFacetArray newFacets; - MeshPointArray newPoints; - unsigned long numberOfOldPoints = _rclMesh._aclPointArray.size(); - for ( std::list >::iterator it = aBorders.begin(); it != aBorders.end(); ++it ) { - if ( it->size()-1 > length ) - continue; // boundary with too many edges - MeshFacetArray cFacets; - MeshPointArray cPoints; - if (cAlgo.FillupHole(*it, cTria, cFacets, cPoints, level, &cPt2Fac)) { - if (it->front() == it->back()) - it->pop_back(); - // the triangulation may produce additional points which we must take into account when appending to the mesh - unsigned long countBoundaryPoints = it->size(); - unsigned long countDifference = cPoints.size() - countBoundaryPoints; - if (countDifference > 0) { - MeshPointArray::_TIterator pt = cPoints.begin() + countBoundaryPoints; - for (unsigned long i=0; ipush_back(numberOfOldPoints++); - newPoints.push_back(*pt); - } - } - for (MeshFacetArray::_TIterator kt = cFacets.begin(); kt != cFacets.end(); ++kt ) { - kt->_aulPoints[0] = (*it)[kt->_aulPoints[0]]; - kt->_aulPoints[1] = (*it)[kt->_aulPoints[1]]; - kt->_aulPoints[2] = (*it)[kt->_aulPoints[2]]; - newFacets.push_back(*kt); - } - } - else { - aFailed.push_back(*it); - } + for (std::list >::iterator it = aBorders.begin(); it != aBorders.end(); ++it) { + if (it->size()-1 <= length) // ignore boundary with too many edges + aFillBorders.push_back(*it); } - // insert new points and faces into the mesh structure - _rclMesh._aclPointArray.insert(_rclMesh._aclPointArray.end(), newPoints.begin(), newPoints.end()); - for (MeshPointArray::_TIterator it = newPoints.begin(); it != newPoints.end(); ++it) - _rclMesh._clBoundBox &= *it; - if (!newFacets.empty()) - _rclMesh.AddFacets(newFacets); + if (!aFillBorders.empty()) + FillupHoles(level, cTria, aFillBorders, aFailed); } void MeshTopoAlgorithm::FillupHoles(int level, AbstractPolygonTriangulator& cTria, @@ -1307,20 +1272,27 @@ void MeshTopoAlgorithm::FillupHoles(int level, AbstractPolygonTriangulator& cTri if (bound.front() == bound.back()) bound.pop_back(); // the triangulation may produce additional points which we must take into account when appending to the mesh - unsigned long countBoundaryPoints = bound.size(); - unsigned long countDifference = cPoints.size() - countBoundaryPoints; - if (countDifference > 0) { + if (cPoints.size() > bound.size()) { + unsigned long countBoundaryPoints = bound.size(); + unsigned long countDifference = cPoints.size() - countBoundaryPoints; MeshPointArray::_TIterator pt = cPoints.begin() + countBoundaryPoints; for (unsigned long i=0; i_aulPoints[0] = bound[kt->_aulPoints[0]]; - kt->_aulPoints[1] = bound[kt->_aulPoints[1]]; - kt->_aulPoints[2] = bound[kt->_aulPoints[2]]; - newFacets.push_back(*kt); + if (cTria.NeedsReindexing()) { + for (MeshFacetArray::_TIterator kt = cFacets.begin(); kt != cFacets.end(); ++kt ) { + kt->_aulPoints[0] = bound[kt->_aulPoints[0]]; + kt->_aulPoints[1] = bound[kt->_aulPoints[1]]; + kt->_aulPoints[2] = bound[kt->_aulPoints[2]]; + newFacets.push_back(*kt); + } + } + else { + for (MeshFacetArray::_TIterator kt = cFacets.begin(); kt != cFacets.end(); ++kt ) { + newFacets.push_back(*kt); + } } } else { @@ -1332,8 +1304,24 @@ void MeshTopoAlgorithm::FillupHoles(int level, AbstractPolygonTriangulator& cTri _rclMesh._aclPointArray.insert(_rclMesh._aclPointArray.end(), newPoints.begin(), newPoints.end()); for (MeshPointArray::_TIterator it = newPoints.begin(); it != newPoints.end(); ++it) _rclMesh._clBoundBox &= *it; - if (!newFacets.empty()) - _rclMesh.AddFacets(newFacets); + if (!newFacets.empty()) { + // Do some checks for invalid point indices + MeshFacetArray addFacets; + addFacets.reserve(newFacets.size()); + unsigned long ctPoints = _rclMesh.CountPoints(); + for (MeshFacetArray::_TIterator it = newFacets.begin(); it != newFacets.end(); ++it) { + if (it->_aulPoints[0] >= ctPoints || + it->_aulPoints[1] >= ctPoints || + it->_aulPoints[2] >= ctPoints) { + Base::Console().Log("Ignore invalid face <%d, %d, %d> (%d vertices)\n", + it->_aulPoints[0], it->_aulPoints[1], it->_aulPoints[2], ctPoints); + } + else { + addFacets.push_back(*it); + } + } + _rclMesh.AddFacets(addFacets); + } } void MeshTopoAlgorithm::FindHoles(unsigned long length, diff --git a/src/Mod/Mesh/App/Core/Triangulation.cpp b/src/Mod/Mesh/App/Core/Triangulation.cpp index 6fecbd780..68babf99d 100644 --- a/src/Mod/Mesh/App/Core/Triangulation.cpp +++ b/src/Mod/Mesh/App/Core/Triangulation.cpp @@ -30,6 +30,7 @@ #include #include "Triangulation.h" #include "Approximation.h" +#include "Algorithm.h" #include "MeshKernel.h" #include @@ -121,7 +122,7 @@ std::vector AbstractPolygonTriangulator::ProjectToFitPlane() return proj; } -void AbstractPolygonTriangulator::ProjectOntoSurface(const std::vector& points) +void AbstractPolygonTriangulator::PostProcessing(const std::vector& points) { // For a good approximation we should have enough points, i.e. for 9 parameters // for the fit function we should have at least 50 points. @@ -144,9 +145,23 @@ void AbstractPolygonTriangulator::ProjectOntoSurface(const std::vector_points.size() != this->_indices.size()) { + Base::Console().Log("Triangulation: %d points <> %d indices\n", _points.size(), _indices.size()); + return false; + } bool ok = Triangulate(); if (ok) Done(); return ok; @@ -177,6 +192,10 @@ void AbstractPolygonTriangulator::Discard() } } +void AbstractPolygonTriangulator::Reset() +{ +} + void AbstractPolygonTriangulator::Done() { _info.push_back(_points.size()); @@ -625,7 +644,7 @@ bool FlatTriangulator::Triangulate() return succeeded; } -void FlatTriangulator::ProjectOntoSurface(const std::vector&) +void FlatTriangulator::PostProcessing(const std::vector&) { } @@ -669,7 +688,8 @@ bool ConstraintDelaunayTriangulator::Triangulate() // ------------------------------------------------------------- -Triangulator::Triangulator(const MeshKernel& k) : _kernel(k) +#if 0 +Triangulator::Triangulator(const MeshKernel& k, bool flat) : _kernel(k) { } @@ -681,3 +701,23 @@ bool Triangulator::Triangulate() { return false; } + +MeshGeomFacet Triangulator::GetTriangle(const MeshPointArray&, + const MeshFacet& facet) const +{ + return MeshGeomFacet(); +} + +void Triangulator::PostProcessing(const std::vector&) +{ +} + +void Triangulator::Discard() +{ + AbstractPolygonTriangulator::Discard(); +} + +void Triangulator::Reset() +{ +} +#endif diff --git a/src/Mod/Mesh/App/Core/Triangulation.h b/src/Mod/Mesh/App/Core/Triangulation.h index a33b5a0d9..0d75e2a3b 100644 --- a/src/Mod/Mesh/App/Core/Triangulation.h +++ b/src/Mod/Mesh/App/Core/Triangulation.h @@ -39,6 +39,15 @@ public: /** Sets the polygon to be triangulated. */ void SetPolygon(const std::vector& raclPoints); + void SetIndices(const std::vector& d) {_indices = d;} + /** Usually the created faces use the indices of the polygon points + * from [0, n]. If the faces should be appended to an existing mesh + * they may need to be reindexed from the calling instance. + * However, there may other algorithms that directly use the indices + * of the mesh and thus do not need to be touched afterwards. In this + * case the method should be reimplemented to return false. + */ + virtual bool NeedsReindexing() const { return true; } /** Get the polygon points to be triangulated. The points may be * projected onto its average plane. */ @@ -46,7 +55,7 @@ public: /** The triangulation algorithm may create new points when * calling Triangulate(). This method returns these added * points. - * @note: Make sure to have called ProjectOntoSurface() before using + * @note: Make sure to have called PostProcessing() before using * this method if you want the surface points the new points are lying on. */ std::vector AddedPoints() const; @@ -65,11 +74,13 @@ public: /** If points were added then we get the 3D points by projecting the added * 2D points onto a surface which fits into the given points. */ - virtual void ProjectOntoSurface(const std::vector&); + virtual void PostProcessing(const std::vector&); /** Returns the geometric triangles of the polygon. */ const std::vector& GetTriangles() const { return _triangles;} /** Returns the topologic facets of the polygon. */ const std::vector& GetFacets() const { return _facets;} + /** Returns the the triangle to a given topologic facet. */ + virtual MeshGeomFacet GetTriangle(const MeshPointArray&, const MeshFacet&) const; /** Returns the length of the polygon */ float GetLength() const; /** Get information about the pol GetInfo() const; - void Discard(); + virtual void Discard(); + /** Resets some internals. The default implementation does nothing.*/ + virtual void Reset(); protected: /** Computes the triangulation of a polygon. The resulting facets can @@ -89,6 +102,7 @@ protected: protected: bool _discard; Base::Matrix4D _inverse; + std::vector _indices; std::vector _points; std::vector _newpoints; std::vector _triangles; @@ -168,7 +182,7 @@ public: FlatTriangulator(); ~FlatTriangulator(); - void ProjectOntoSurface(const std::vector&); + void PostProcessing(const std::vector&); protected: bool Triangulate(); @@ -187,17 +201,25 @@ private: float fMaxArea; }; +#if 0 class MeshExport Triangulator : public AbstractPolygonTriangulator { public: - Triangulator(const MeshKernel&); + Triangulator(const MeshKernel&, bool flat); ~Triangulator(); + void Discard(); + void Reset(); + + bool NeedsReindexing() const { return false; } + MeshGeomFacet GetTriangle(const MeshPointArray&, const MeshFacet&) const; + void PostProcessing(const std::vector&); protected: bool Triangulate(); const MeshKernel& _kernel; }; +#endif } // namespace MeshCore diff --git a/src/Mod/Test/InitGui.py b/src/Mod/Test/InitGui.py index 9a7575865..22cebd4d0 100644 --- a/src/Mod/Test/InitGui.py +++ b/src/Mod/Test/InitGui.py @@ -63,7 +63,7 @@ class TestWorkbench ( Workbench ): self.appendToolbar("TestTools",list) menu = ["Test &Commands","TestToolsGui"] - list = ["Std_TestQM","Test_Test","Test_TestAll","Test_TestDoc","Test_TestBase"] + list = ["Std_TestQM","Std_TestReloadQM","Test_Test","Test_TestAll","Test_TestDoc","Test_TestBase"] self.appendCommandbar("TestToolsGui",list) self.appendMenu(menu,list)