Salome HOME
patch for correct compilation on Linux
[modules/hydro.git] / src / HYDROData / HYDROData_Stream.cxx
index 2e951d99a8347df1fc4de2d3fad76a5557df6182..061fa93576bcd5a669ab101be61c92a85c3c9886 100644 (file)
 #include <TopoDS_Wire.hxx>
 #include <TopoDS_Shell.hxx>
 #include <TopoDS_Face.hxx>
-
+#include <TopoDS_Edge.hxx>
+#include <TopoDS_Vertex.hxx>
+#include <TopExp.hxx>
+#include <TopExp_Explorer.hxx>
+#include <BRepProj_Projection.hxx>
+#include <BRepExtrema_ExtCC.hxx>
+#include <gp_Ax1.hxx>
+#include <gp_Ax2.hxx>
+#include <gp_Ax3.hxx>
+#include <gp_Vec.hxx>
+#include <gp_Pnt.hxx>
+#include <gp_Pln.hxx>
+#include <gp.hxx>
+#include <Precision.hxx>
 #include <QStringList>
 
 #define PYTHON_STREAM_ID "KIND_STREAM"
@@ -194,19 +207,97 @@ void HYDROData_Stream::RemoveHydraulicAxis()
   SetToUpdate( true );
 }
 
-bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile ) const
+bool HYDROData_Stream::HasIntersection( const Handle(HYDROData_Profile)& theProfile, const TopoDS_Face& thePlane,
+                                                                          Standard_Real& outPar ) const
 {
   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
   if ( theProfile.IsNull() || aHydAxis.IsNull() )
     return false; 
 
-  TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() );
+  TopoDS_Wire aHydraulicWire = TopoDS::Wire( aHydAxis->GetShape() ); //guide line
   TopoDS_Wire aProfileWire = TopoDS::Wire( theProfile->GetTopShape() );
   if ( aHydraulicWire.IsNull() || aProfileWire.IsNull() )
     return false;
 
   // TODO
-
+  return true; // temporary
+  BRepProj_Projection aProjector (aProfileWire, thePlane, gp::OZ().Direction());
+  if(!aProjector.IsDone())
+    return false;
+  TopoDS_Shape aPrjProfile = aProjector.Shape();
+  if(aPrjProfile.IsNull())
+    return false;
+  TopoDS_Vertex aV1, aV2;
+  if(aPrjProfile.ShapeType() == TopAbs_EDGE)
+         TopExp::Vertices(TopoDS::Edge(aPrjProfile), aV1, aV2);          
+  else if(aPrjProfile.ShapeType() == TopAbs_WIRE)  
+         TopExp::Vertices(TopoDS::Wire(aPrjProfile), aV1, aV2);          
+  else if(aPrjProfile.ShapeType() == TopAbs_COMPOUND){
+    TopExp_Explorer anExp(aPrjProfile, TopAbs_WIRE);
+       if(anExp.More()) {
+               TopExp::Vertices(TopoDS::Wire(anExp.Current()), aV1, aV2);        
+       } else {
+         anExp.Init(aPrjProfile, TopAbs_EDGE);
+         if(anExp.More()) {
+               TopExp::Vertices(TopoDS::Edge(anExp.Current()), aV1, aV2);        
+         }
+       }
+  }
+  if(aV1.IsNull() || aV2.IsNull())
+       return false;
+  gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
+  gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
+  aPnt1.SetZ(0.0);
+  aPnt2.SetZ(0.0);
+  BRepBuilderAPI_MakeEdge aMk(aPnt1, aPnt2); 
+  if(!aMk.IsDone())
+    return false;
+  const TopoDS_Edge& anEdg2 = aMk.Edge();//Section edge
+  Standard_Integer aNum(0);
+  
+  TopExp_Explorer anExplo(aHydraulicWire, TopAbs_EDGE);
+  for(;anExplo.More();anExplo.Next()) aNum++;
+  // check for self-intersection
+  const Standard_Real SquareTolerance = Precision::Confusion()*Precision::Confusion();
+  Standard_Boolean hasInt(false);
+  Standard_Real aSqDist(DBL_MAX);
+  Standard_Integer anIndx(0);
+  BRepExtrema_ExtCC aCC;
+  aCC.Initialize(anEdg2);
+  outPar = 0.0;
+  anExplo.Init(aHydraulicWire, TopAbs_EDGE);
+  for(Standard_Integer j=1;anExplo.More();anExplo.Next(),j++) {
+       const TopoDS_Edge& anEdg1 = TopoDS::Edge(anExplo.Current());
+       if(anEdg1.IsNull())
+         continue;     
+       aCC.Perform(anEdg1);
+    if(aCC.IsDone()) {
+    for(Standard_Integer i=1; i<= aCC.NbExt();i++)
+      if(aCC.SquareDistance(i) < aSqDist) {
+        aSqDist = aCC.SquareDistance(i);
+        anIndx = i;
+               hasInt = true;          
+         }  
+       }
+       if(hasInt) {    
+      if(aSqDist <= SquareTolerance) {
+        const gp_Pnt& aPnt = aCC.PointOnE1(anIndx);
+        if(aNum > 1) {
+          TopExp::Vertices(anEdg1, aV1, aV2, Standard_True);
+                 outPar += BRep_Tool::Pnt(aV1).Distance(aPnt);
+           } else {
+          Standard_Real aPar = aCC.ParameterOnE1(anIndx);
+                 outPar = aPar;
+           }
+           break;
+         } else {
+        if(aNum > 1) {
+          TopExp::Vertices(anEdg1, aV1, aV2);
+             outPar += BRep_Tool::Pnt(aV1).Distance(BRep_Tool::Pnt(aV2));
+           }
+         }
+       }
+  }
   return true;
 }
 
@@ -214,8 +305,15 @@ bool HYDROData_Stream::AddProfile( const Handle(HYDROData_Profile)& theProfile )
 {
   if ( theProfile.IsNull() )
     return false; 
-
-  if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile ) )
+  gp_Ax2 aX2(gp::XOY());
+  gp_Ax3 aX3(aX2);
+  gp_Pln aPln(aX3);
+  BRepBuilderAPI_MakeFace aMkr(aPln);
+  if(!aMkr.IsDone())
+       return false;
+  const TopoDS_Face& aPlane = TopoDS::Face(aMkr.Shape());
+  Standard_Real aPar(.0);
+  if ( HasReference( theProfile, DataTag_Profile ) || !HasIntersection( theProfile, aPlane, aPar ) )
     return false; // Object is already in reference list or it has no intersection
 
   insertProfileInToOrder( theProfile );
@@ -281,13 +379,20 @@ void HYDROData_Stream::updateProfilesOrder()
   Handle(HYDROData_PolylineXY) aHydAxis = GetHydraulicAxis();
   if ( aHydAxis.IsNull() )
     return;
-
+  gp_Ax2 aX2(gp::XOY());
+  gp_Ax3 aX3(aX2);
+  gp_Pln aPln(aX3);
+  BRepBuilderAPI_MakeFace aMkr(aPln);
+  if(!aMkr.IsDone())
+       return;
+  const TopoDS_Face& aPlane = TopoDS::Face(aMkr.Shape());
+  Standard_Real aPar(.0);
   HYDROData_SequenceOfObjects::Iterator anIter( aRefProfiles );
   for ( ; anIter.More(); anIter.Next() )
   {
     Handle(HYDROData_Profile) aProfile =
       Handle(HYDROData_Profile)::DownCast( anIter.Value() );
-    if ( aProfile.IsNull() || !HasIntersection( aProfile ) )
+    if ( aProfile.IsNull() || !HasIntersection( aProfile, aPlane, aPar ) )
       continue;
 
     insertProfileInToOrder( aProfile );