Salome HOME
quick optimization patch (bytearray for images)
[modules/hydro.git] / src / HYDROData / HYDROData_Tool.cxx
index 7d1339241db68abd290f6b348fa52791943294ba..b45c2970d3c220b85e956b9e578b388318c434c3 100644 (file)
 #include <limits>
 #include <math.h>
 
-static int aMaxNameId = std::numeric_limits<int>::max();
 
+#include <BRepTools.hxx>
+#include <NCollection_Map.hxx>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS_Face.hxx>
+#include <TopTools_ShapeMapHasher.hxx>
+#include <BRep_Builder.hxx>
+#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
+#include <TopExp.hxx>
+#include <NCollection_List.hxx>
+#include <TopTools_ListIteratorOfListOfShape.hxx>
+
+static int aMaxNameId = INT_MAX;
 void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
                                          const QStringList& theStrings,
                                          const QString&     theSep )
@@ -56,7 +67,7 @@ void HYDROData_Tool::WriteStringsToFile( QFile&             theFile,
     return;
 
   QTextStream anOutStream( &theFile );
-  anOutStream << aWriteStr << theSep << theSep;
+  anOutStream << aWriteStr.toUtf8() << theSep << theSep;
 }
 
 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
@@ -309,6 +320,119 @@ bool HYDROData_Tool::IsInf( double theValue )
 #endif  
 }
 
+static void MakeShellG(const NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>& FG,
+  TopoDS_Shape& outSh)
+{
+  BRep_Builder bb;
+  NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>::Iterator itFG(FG);
+  if (FG.Extent() > 1)
+  {
+    //face nb > 1 => make shell
+    TopoDS_Shell outShell;
+    bb.MakeShell(outShell);
+    for (;itFG.More();itFG.Next())
+      bb.Add(outShell, itFG.Value());
+    outSh = outShell;
+  }
+  else if (FG.Extent() == 1)
+  {
+    outSh = itFG.Value(); //one face
+  }
+}
+
+TopoDS_Shape HYDROData_Tool::RebuildCmp(const TopoDS_Shape& in)
+{
+  TopTools_IndexedDataMapOfShapeListOfShape mE2LF;
+  TopExp::MapShapesAndAncestors(in, TopAbs_EDGE, TopAbs_FACE, mE2LF);
+  if (mE2LF.IsEmpty())
+    return TopoDS_Shape();
+  NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> dfm;
+  //TopExp::MapShapes(aFuseShape, TopAbs_FACE, dfm);
+  TopExp_Explorer expf(in, TopAbs_FACE);
+  for (;expf.More(); expf.Next())
+    dfm.Add(TopoDS::Face(expf.Current()));
+
+  int nbF = dfm.Extent();
+  TopExp_Explorer exp_f(in, TopAbs_FACE);
+  const TopoDS_Face& FF = TopoDS::Face(exp_f.Current());
+  NCollection_List<TopoDS_Face> CurrFS;
+  NCollection_List<TopoDS_Face> NeighFS;
+  NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> PrF;
+  CurrFS.Append(FF);
+  NCollection_List<NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>> GL_F;
+  NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> OneGr;
+  bool end = false;
+  while (!end)
+  {
+    NCollection_List<TopoDS_Face>::Iterator it_currfs(CurrFS);
+    NeighFS.Clear();
+    for (;it_currfs.More();it_currfs.Next())
+    {
+      const TopoDS_Face& CF = it_currfs.Value();
+      TopExp_Explorer exp_edge(CF, TopAbs_EDGE);
+      for (;exp_edge.More();exp_edge.Next())
+      {
+        const TopoDS_Shape& CE = exp_edge.Current();
+        const TopTools_ListOfShape& lsf = mE2LF.FindFromKey(CE);
+        TopTools_ListIteratorOfListOfShape ls_it(lsf); //always one face (since all faces are planar)
+        for (;ls_it.More();ls_it.Next())
+        {
+          const TopoDS_Face& F = TopoDS::Face(ls_it.Value());
+          if (F.IsSame(CF))
+            continue;
+          if (!PrF.Contains(F))
+          {
+            OneGr.Add(F);
+            NeighFS.Append(F);
+            PrF.Add(F);
+          }
+        }
+      }
+      OneGr.Add(CF);
+      PrF.Add(CF);
+    }
+    if (NeighFS.IsEmpty())
+    {
+      GL_F.Append(OneGr);
+      OneGr.Clear();
+      dfm.Subtract(PrF);
+      if (dfm.IsEmpty())
+        end = true;
+      else
+      {
+        NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>::Iterator itDm(dfm);
+        const TopoDS_Face& nsh = itDm.Key();
+        NeighFS.Append(nsh);
+      }
+    }
+    CurrFS = NeighFS;
+  }
+
+  TopoDS_Shape sh;
+
+  if (GL_F.Extent() > 1)
+  {
+    TopoDS_Compound cmp;
+    NCollection_List<NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>>::Iterator itGL_F(GL_F);  
+    BRep_Builder bb;
+    bb.MakeCompound(cmp);
+    for (;itGL_F.More();itGL_F.Next())
+    {
+      MakeShellG(itGL_F.Value(), sh);
+      if (!sh.IsNull())
+        bb.Add(cmp, sh);
+    }
+    return  cmp;
+  }
+  else if (GL_F.Extent() == 1)
+  {
+    MakeShellG(GL_F.First(), sh);
+    return sh;
+  }
+  
+}
+
+
 std::ostream& operator<<( std::ostream& theStream, const QString& theText )
 {
   theStream << theText.toStdString();