From cc8452a54510dae56eb76d818a0d22cb6b33dc38 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 10 Feb 2024 19:47:37 +0100 Subject: [PATCH] Part: use of ShapeMapHasher for code simplification --- src/Mod/Import/App/ImportOCAF.cpp | 91 ++++------------------- src/Mod/Import/App/ImportOCAF.h | 1 - src/Mod/Import/App/ImportOCAFAssembly.cpp | 5 +- src/Mod/Import/App/ImportOCAFAssembly.h | 1 - src/Mod/Part/App/ImportStep.cpp | 8 +- src/Mod/Part/App/TopoShapePyImp.cpp | 13 +--- src/Mod/Part/Gui/ViewProviderExt.cpp | 22 ++---- 7 files changed, 30 insertions(+), 111 deletions(-) diff --git a/src/Mod/Import/App/ImportOCAF.cpp b/src/Mod/Import/App/ImportOCAF.cpp index 7277d0fb19..40a3177970 100644 --- a/src/Mod/Import/App/ImportOCAF.cpp +++ b/src/Mod/Import/App/ImportOCAF.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include "ImportOCAF.h" @@ -151,11 +152,7 @@ void ImportOCAF::loadShapes(const TDF_Label& label, std::vector localValue; if (aShapeTool->GetShape(label, aShape)) { -#if OCC_VERSION_HEX >= 0x070800 - hash = std::hash {}(aShape); -#else - hash = aShape.HashCode(HashUpper); -#endif + hash = Part::ShapeMapHasher {}(aShape); } Handle(TDataStd_Name) name; @@ -227,11 +224,7 @@ void ImportOCAF::loadShapes(const TDF_Label& label, if (isRef || myRefShapes.find(hash) == myRefShapes.end()) { TopoDS_Shape aShape; if (isRef && aShapeTool->GetShape(label, aShape)) { -#if OCC_VERSION_HEX >= 0x070800 - myRefShapes.insert(std::hash {}(aShape)); -#else - myRefShapes.insert(aShape.HashCode(HashUpper)); -#endif + myRefShapes.insert(Part::ShapeMapHasher {}(aShape)); } if (aShapeTool->IsSimpleShape(label) && (isRef || aShapeTool->IsFree(label))) { @@ -558,11 +551,7 @@ void ImportXCAF::createShape(const TopoDS_Shape& shape, bool perface, bool setna part->Label.setValue(default_name); part->Shape.setValue(shape); std::map::const_iterator jt; -#if OCC_VERSION_HEX >= 0x070800 - jt = myColorMap.find(std::hash {}(shape)); -#else - jt = myColorMap.find(shape.HashCode(INT_MAX)); -#endif + jt = myColorMap.find(Part::ShapeMapHasher {}(shape)); App::Color partColor(0.8f, 0.8f, 0.8f); #if 0 // TODO @@ -583,11 +572,7 @@ void ImportXCAF::createShape(const TopoDS_Shape& shape, bool perface, bool setna // set label name if defined if (setname && !myNameMap.empty()) { std::map::const_iterator jt; -#if OCC_VERSION_HEX >= 0x070800 - jt = myNameMap.find(std::hash {}(shape)); -#else - jt = myNameMap.find(shape.HashCode(INT_MAX)); -#endif + jt = myNameMap.find(Part::ShapeMapHasher {}(shape)); if (jt != myNameMap.end()) { part->Label.setValue(jt->second); } @@ -607,11 +592,7 @@ void ImportXCAF::createShape(const TopoDS_Shape& shape, bool perface, bool setna faceColors.resize(faces.Extent(), partColor); xp.Init(shape, TopAbs_FACE); while (xp.More()) { -#if OCC_VERSION_HEX >= 0x070800 - jt = myColorMap.find(std::hash {}(xp.Current())); -#else - jt = myColorMap.find(xp.Current().HashCode(INT_MAX)); -#endif + jt = myColorMap.find(Part::ShapeMapHasher {}(xp.Current())); if (jt != myColorMap.end()) { int index = faces.FindIndex(xp.Current()); faceColors[index - 1] = convertColor(jt->second); @@ -646,57 +627,29 @@ void ImportXCAF::loadShapes(const TDF_Label& label) // add the shapes TopExp_Explorer xp; for (xp.Init(aShape, TopAbs_SOLID); xp.More(); xp.Next(), ctSolids++) { -#if OCC_VERSION_HEX >= 0x070800 - this->mySolids[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->mySolids[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->mySolids[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } for (xp.Init(aShape, TopAbs_SHELL, TopAbs_SOLID); xp.More(); xp.Next(), ctShells++) { -#if OCC_VERSION_HEX >= 0x070800 - this->myShells[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myShells[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myShells[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } // if no solids and no shells were found then go for compounds if (ctSolids == 0 && ctShells == 0) { for (xp.Init(aShape, TopAbs_COMPOUND); xp.More(); xp.Next(), ctComps++) { -#if OCC_VERSION_HEX >= 0x070800 - this->myCompds[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myCompds[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myCompds[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } } if (ctComps == 0) { for (xp.Init(aShape, TopAbs_FACE, TopAbs_SHELL); xp.More(); xp.Next()) { -#if OCC_VERSION_HEX >= 0x070800 - this->myShapes[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myShapes[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } for (xp.Init(aShape, TopAbs_WIRE, TopAbs_FACE); xp.More(); xp.Next()) { -#if OCC_VERSION_HEX >= 0x070800 - this->myShapes[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myShapes[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } for (xp.Init(aShape, TopAbs_EDGE, TopAbs_WIRE); xp.More(); xp.Next()) { -#if OCC_VERSION_HEX >= 0x070800 - this->myShapes[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myShapes[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } for (xp.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); xp.More(); xp.Next()) { -#if OCC_VERSION_HEX >= 0x070800 - this->myShapes[std::hash {}(xp.Current())] = (xp.Current()); -#else - this->myShapes[xp.Current().HashCode(INT_MAX)] = (xp.Current()); -#endif + this->myShapes[Part::ShapeMapHasher {}(xp.Current())] = (xp.Current()); } } } @@ -707,11 +660,7 @@ void ImportXCAF::loadShapes(const TDF_Label& label) || hColors->GetColor(label, XCAFDoc_ColorSurf, col) || hColors->GetColor(label, XCAFDoc_ColorCurv, col)) { // add defined color -#if OCC_VERSION_HEX >= 0x070800 - myColorMap[std::hash {}(aShape)] = col; -#else - myColorMap[aShape.HashCode(INT_MAX)] = col; -#endif + myColorMap[Part::ShapeMapHasher {}(aShape)] = col; } else { // http://www.opencascade.org/org/forum/thread_17107/ @@ -721,11 +670,7 @@ void ImportXCAF::loadShapes(const TDF_Label& label) || hColors->GetColor(it.Value(), XCAFDoc_ColorSurf, col) || hColors->GetColor(it.Value(), XCAFDoc_ColorCurv, col)) { // add defined color -#if OCC_VERSION_HEX >= 0x070800 - myColorMap[std::hash {}(it.Value())] = col; -#else - myColorMap[it.Value().HashCode(INT_MAX)] = col; -#endif + myColorMap[Part::ShapeMapHasher {}(it.Value())] = col; } } } @@ -738,11 +683,7 @@ void ImportXCAF::loadShapes(const TDF_Label& label) extstr.ToUTF8CString(str); std::string labelName(str); if (!labelName.empty()) { -#if OCC_VERSION_HEX >= 0x070800 - myNameMap[std::hash {}(aShape)] = labelName; -#else - myNameMap[aShape.HashCode(INT_MAX)] = labelName; -#endif + myNameMap[Part::ShapeMapHasher {}(aShape)] = labelName; } delete[] str; } diff --git a/src/Mod/Import/App/ImportOCAF.h b/src/Mod/Import/App/ImportOCAF.h index 3b323b55a4..f933d17716 100644 --- a/src/Mod/Import/App/ImportOCAF.h +++ b/src/Mod/Import/App/ImportOCAF.h @@ -94,7 +94,6 @@ private: bool merge {true}; std::string default_name; std::set myRefShapes; - static const int HashUpper = INT_MAX; }; class ImportExport ImportOCAFCmd: public ImportOCAF diff --git a/src/Mod/Import/App/ImportOCAFAssembly.cpp b/src/Mod/Import/App/ImportOCAFAssembly.cpp index 84df9e9c8e..2778f00a77 100644 --- a/src/Mod/Import/App/ImportOCAFAssembly.cpp +++ b/src/Mod/Import/App/ImportOCAFAssembly.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include "ImportOCAFAssembly.h" @@ -109,7 +110,7 @@ void ImportOCAFAssembly::loadShapes(const TDF_Label& label, int hash = 0; TopoDS_Shape aShape; if (aShapeTool->GetShape(label, aShape)) { - hash = aShape.HashCode(HashUpper); + hash = Part::ShapeMapHasher {}(aShape); } Handle(TDataStd_Name) name; @@ -192,7 +193,7 @@ void ImportOCAFAssembly::loadShapes(const TDF_Label& label, if (isRef || myRefShapes.find(hash) == myRefShapes.end()) { TopoDS_Shape aShape; if (isRef && aShapeTool->GetShape(label, aShape)) { - myRefShapes.insert(aShape.HashCode(HashUpper)); + myRefShapes.insert(Part::ShapeMapHasher {}(aShape)); } if (aShapeTool->IsSimpleShape(label) && (isRef || aShapeTool->IsFree(label))) { diff --git a/src/Mod/Import/App/ImportOCAFAssembly.h b/src/Mod/Import/App/ImportOCAFAssembly.h index 5de3a62759..fea051f30b 100644 --- a/src/Mod/Import/App/ImportOCAFAssembly.h +++ b/src/Mod/Import/App/ImportOCAFAssembly.h @@ -86,7 +86,6 @@ private: Handle(XCAFDoc_ColorTool) aColorTool; std::string default_name; std::set myRefShapes; - static const int HashUpper = INT_MAX; }; diff --git a/src/Mod/Part/App/ImportStep.cpp b/src/Mod/Part/App/ImportStep.cpp index 1d02eb2d63..208bd94697 100644 --- a/src/Mod/Part/App/ImportStep.cpp +++ b/src/Mod/Part/App/ImportStep.cpp @@ -48,6 +48,7 @@ #include "ImportStep.h" #include "encodeFilename.h" +#include "ShapeMapHasher.h" #include "PartFeature.h" #include "ProgressIndicator.h" @@ -137,12 +138,7 @@ int Part::ImportStepParts(App::Document *pcDoc, const char* Name) // This is a trick to access the GUI via Python and set the color property // of the associated view provider. If no GUI is up an exception is thrown // and cleared immediately -#if OCC_VERSION_HEX >= 0x070800 - std::hash hasher; - std::map::iterator it = hash_col.find(hasher(aSolid)); -#else - std::map::iterator it = hash_col.find(aSolid.HashCode(INT_MAX)); -#endif + std::map::iterator it = hash_col.find(ShapeMapHasher{}(aSolid)); if (it != hash_col.end()) { try { Py::Object obj(pcFeature->getPyObject(), true); diff --git a/src/Mod/Part/App/TopoShapePyImp.cpp b/src/Mod/Part/App/TopoShapePyImp.cpp index f4471391c9..ce449d5714 100644 --- a/src/Mod/Part/App/TopoShapePyImp.cpp +++ b/src/Mod/Part/App/TopoShapePyImp.cpp @@ -92,6 +92,7 @@ #include "OCCError.h" #include "PartPyCXX.h" +#include "ShapeMapHasher.h" using namespace Part; @@ -1306,11 +1307,7 @@ PyObject* TopoShapePy::ancestorsOfType(PyObject *args) TopTools_ListIteratorOfListOfShape it(ancestors); for (; it.More(); it.Next()) { // make sure to avoid duplicates -#if OCC_VERSION_HEX >= 0x070800 - const size_t code = std::hash{}(static_cast(it.Value())); -#else - Standard_Integer code = it.Value().HashCode(INT_MAX); -#endif + Standard_Integer code = ShapeMapHasher{}(it.Value()); if (hashes.find(code) == hashes.end()) { list.append(shape2pyshape(it.Value())); hashes.insert(code); @@ -1932,11 +1929,7 @@ PyObject* TopoShapePy::hashCode(PyObject *args) if (!PyArg_ParseTuple(args, "|i",&upper)) return nullptr; -#if OCC_VERSION_HEX >= 0x070800 - int hc = std::hash{}(getTopoShapePtr()->getShape()); -#else - int hc = getTopoShapePtr()->getShape().HashCode(upper); -#endif + int hc = ShapeMapHasher{}(getTopoShapePtr()->getShape()); return Py_BuildValue("i", hc); } diff --git a/src/Mod/Part/Gui/ViewProviderExt.cpp b/src/Mod/Part/Gui/ViewProviderExt.cpp index c5a8d9ef00..8b425d3b99 100644 --- a/src/Mod/Part/Gui/ViewProviderExt.cpp +++ b/src/Mod/Part/Gui/ViewProviderExt.cpp @@ -82,6 +82,7 @@ #include #include #include +#include #include #include "ViewProviderExt.h" @@ -981,12 +982,9 @@ void ViewProviderPartExt::updateVisual() } TopExp_Explorer xp; - for (xp.Init(faceMap(i),TopAbs_EDGE);xp.More();xp.Next()) -#if OCC_VERSION_HEX >= 0x070800 - faceEdges.insert(std::hash{}(xp.Current())); -#else - faceEdges.insert(xp.Current().HashCode(INT_MAX)); -#endif + for (xp.Init(faceMap(i),TopAbs_EDGE);xp.More();xp.Next()) { + faceEdges.insert(Part::ShapeMapHasher{}(xp.Current())); + } numFaces++; } @@ -1014,11 +1012,7 @@ void ViewProviderPartExt::updateVisual() // So, we have to store the hashes of the edges associated to a face. // If the hash of a given edge is not in this list we know it's really // a free edge. -#if OCC_VERSION_HEX >= 0x070800 - int hash = std::hash{}(aEdge); -#else - int hash = aEdge.HashCode(INT_MAX); -#endif + int hash = Part::ShapeMapHasher{}(aEdge); if (faceEdges.find(hash) == faceEdges.end()) { Handle(Poly_Polygon3D) aPoly = Part::Tools::polygonOfEdge(aEdge, aLoc); if (!aPoly.IsNull()) { @@ -1217,11 +1211,7 @@ void ViewProviderPartExt::updateVisual() TopLoc_Location aLoc; // handling of the free edge that are not associated to a face -#if OCC_VERSION_HEX >= 0x070800 - int hash = std::hash{}(aEdge); -#else - int hash = aEdge.HashCode(INT_MAX); -#endif + int hash = Part::ShapeMapHasher{}(aEdge); if (faceEdges.find(hash) == faceEdges.end()) { Handle(Poly_Polygon3D) aPoly = Part::Tools::polygonOfEdge(aEdge, aLoc); if (!aPoly.IsNull()) {