Salome HOME
Merge remote-tracking branch 'origin/BR_SHP_FORMAT' into BR_v14_rc
[modules/hydro.git] / src / HYDROData / HYDROData_StreamAltitude.cxx
index b701b3af768f9aa793b258a52b8bc1265b5e6bd1..50443a315d707f139085652189815ade803b395f 100644 (file)
@@ -1,12 +1,59 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "HYDROData_StreamAltitude.h"
+
 #include "HYDROData_Document.h"
+#include "HYDROData_Profile.h"
+#include "HYDROData_Stream.h"
+#include "HYDROData_ShapesTool.h"
+#include <HYDROData_Tool.h>
+#include <BRep_Tool.hxx>
 
-#include <QStringList>
+#include <BRepBuilderAPI_MakeEdge.hxx>
+#include <BRepBuilderAPI_MakeFace.hxx>
+#include <BRepBuilderAPI_MakeWire.hxx>
+
+#include <Extrema_ExtElC.hxx>
+
+#include <GeomAPI_ProjectPointOnCurve.hxx>
+
+#include <gp_Lin.hxx>
+
+#include <Precision.hxx>
 
-#define PYTHON_STREAM_ALTITUDE_ID "KIND_STREAM_ALTITUDE"
+#include <TopExp_Explorer.hxx>
 
+#include <TopoDS.hxx>
+#include <TopoDS_Wire.hxx>
+#include <TopoDS_Edge.hxx>
 
+#include <TopTools_SequenceOfShape.hxx>
+
+#include <Geom_Line.hxx>
+
+#include <QStringList>
+
+#ifdef DEB_CLASS2D
+#include <BRepTools.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepBuilderAPI_MakeVertex.hxx>
+#endif
 IMPLEMENT_STANDARD_HANDLE(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
 
@@ -19,31 +66,243 @@ HYDROData_StreamAltitude::~HYDROData_StreamAltitude()
 {
 }
 
-QStringList HYDROData_StreamAltitude::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+Standard_Real getAltitudeFromProfile( const Handle(HYDROData_Profile)& theProfile,
+                                      const Standard_Real&             theLeftDist,
+                                      const Standard_Real&             theRightDist )
+{
+  Standard_Real aResAlt = 0.0;
+
+  gp_XY aFirstPoint, aLastPoint;
+  if ( !theProfile->GetLeftPoint( aFirstPoint, false ) ||
+       !theProfile->GetRightPoint( aLastPoint, false ) )
+    return aResAlt;
+
+  gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
+  gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
+
+  Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
+
+  Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
+
+  gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
+
+  gp_Lin aPointLine( anIntPoint, gp::DZ() );
+
+  gp_Pnt aPrevPoint;
+  gp_Lin aPrevNormal;
+  HYDROData_Profile::ProfilePoints aProfilePoints = theProfile->GetProfilePoints( false );
+  for ( int i = 1, n = aProfilePoints.Length(); i <= n; ++i )
+  {
+    gp_Pnt aProfPoint( aProfilePoints.Value( i ) );
+
+    Standard_Real aDist = aPointLine.Distance( aProfPoint );
+    if ( aDist <= gp::Resolution() )
+    {
+      // We found the intersected point
+      aResAlt = aProfPoint.Z();
+      break;
+    }
+   
+    gp_Lin aNormal = aPointLine.Normal( aProfPoint );
+    if ( i == 1 )
+    {
+      aPrevNormal = aNormal;
+      aPrevPoint = aProfPoint;
+      continue;
+    }
+
+    if ( aPrevNormal.Direction().Dot( aNormal.Direction() ) < 0 )
+    {
+      // We found the intersected edge
+      gp_Lin anEdgeLine( aPrevPoint, gp_Dir( gp_Vec( aPrevPoint, aProfPoint ) ) );
+     
+      Extrema_ExtElC anExtrema( aPointLine, anEdgeLine, Precision::Angular() );
+      if ( !anExtrema.IsParallel() )
+      {
+        Extrema_POnCurv aFirstPnt, aSecPnt;
+        anExtrema.Points( 1, aFirstPnt, aSecPnt );
+
+        const gp_Pnt& anIntPnt = aSecPnt.Value();
+        aResAlt = anIntPnt.Z();
+
+        break;
+      }
+    }
+
+    aPrevNormal = aNormal;
+    aPrevPoint = aProfPoint;
+  }
+
+  return aResAlt;
+}
+
+bool HYDROData_StreamAltitude::getBoundaryProfilesForPoint(
+  const gp_XY&               thePoint,
+  Handle(HYDROData_Profile)& theLeftProfile,
+  Handle(HYDROData_Profile)& theRightProfile ) const
 {
-  QStringList aResList;
+  Handle(HYDROData_Stream) aStream =
+    Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
+  if ( aStream.IsNull() )
+    return false;
+
+  HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
+  if ( aStreamProfiles.Length() < 2 )
+    return false;
+
+  Handle(HYDROData_Profile) aPrevProfile;
+  gp_Pnt aPrevPnt1, aPrevPnt2;
+  for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
+  {
+    Handle(HYDROData_Profile) aProfile =
+      Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
+    if ( aProfile.IsNull() )
+      continue;
+
+    gp_XY aFirstPoint, aLastPoint;
+    if ( !aProfile->GetLeftPoint( aFirstPoint, false ) ||
+         !aProfile->GetRightPoint( aLastPoint, false ) )
+      continue;
+
+    gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
+    gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
 
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
-  if ( aDocument.IsNull() )
-    return aResList;
-                             
-  QString aDocName = aDocument->GetDocPyName();
-  QString anAltitudeName = GetName();
+    if ( !aPrevProfile.IsNull() )
+    {
+      BRepBuilderAPI_MakeEdge aLeftMakeEdge( aPrevPnt1, aPrevPnt2 );
+      BRepBuilderAPI_MakeEdge aBotMakeEdge( aPrevPnt2, aPnt2 );
+      BRepBuilderAPI_MakeEdge aRightMakeEdge( aPnt2, aPnt1 );
+      BRepBuilderAPI_MakeEdge aTopMakeEdge( aPnt1, aPrevPnt1 );
 
-  aResList << QString( "%1 = %2.CreateObject( %3 );" )
-              .arg( anAltitudeName ).arg( aDocName ).arg( PYTHON_STREAM_ALTITUDE_ID );
-  aResList << QString( "%1.SetName( \"%2\" );" )
-              .arg( anAltitudeName ).arg( anAltitudeName );
+      BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(), 
+                                         aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
 
-  // TODO
+      BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
+       
+      TopoDS_Face aProfilesFace = aMakeFace.Face();
+#ifdef DEB_CLASS2D
+         TopoDS_Compound aCmp;
+      BRep_Builder aBB;
+      aBB.MakeCompound(aCmp);
+         aBB.Add(aCmp, aProfilesFace);
+         gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
+         BRepBuilderAPI_MakeVertex aMk(aPnt);
+         aBB.Add(aCmp, aMk.Vertex());
+         BRepTools::Write(aCmp, "ProfileFace.brep");
+#endif     
 
-  return aResList;
+         TopAbs_State aPointState =  HYDROData_Tool::ComputePointState(thePoint, aProfilesFace);
+
+#ifdef DEB_CLASS2D
+         cout << "Point status is = " << aPointState <<endl;
+#endif
+      if ( aPointState != TopAbs_OUT )
+      {
+        theLeftProfile = aPrevProfile;
+        theRightProfile = aProfile;
+        break;
+      }
+    }
+
+    aPrevProfile = aProfile;
+    aPrevPnt1 = aPnt1;
+    aPrevPnt2 = aPnt2;
+  }
+
+  return !theLeftProfile.IsNull() && !theRightProfile.IsNull();
 }
 
 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
 {
   double aResAltitude = GetInvalidAltitude();
 
+  Handle(HYDROData_Stream) aStream =
+    Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
+  if ( aStream.IsNull() )
+    return aResAltitude;
+
+  TopoDS_Shape aStreamShape = aStream->GetTopShape();
+  if ( aStreamShape.IsNull() )
+    return aResAltitude;
+
+  TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
+  if ( !aStreamFaceExp.More() )
+    return aResAltitude;
+
+  // Get only face because of 2d profile wires is in compound
+  TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
+
+  // Check if point is inside of stream presentation
+  TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aStreamFace);
+
+#ifdef DEB_CLASS2D
+      cout << "Point status is = " << aPointState <<endl;
+         TopoDS_Compound aCmp;
+      BRep_Builder aBB;
+      aBB.MakeCompound(aCmp);
+         aBB.Add(aCmp, aStreamFace);
+         gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
+         BRepBuilderAPI_MakeVertex aMk(aPnt);
+         aBB.Add(aCmp, aMk.Vertex());
+         BRepTools::Write(aCmp, "FCL2d.brep");
+#endif
+  if ( aPointState == TopAbs_OUT )
+    return aResAltitude;
+
+  // Find the two profiles between which the point is lies
+  Handle(HYDROData_Profile) aLeftProfile, aRightProfile;
+  if ( !getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile ) )
+    return aResAltitude;
+
+  // Find the projections of point to borders of stream
+  gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
+
+  TopoDS_Edge aStreamLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
+  TopoDS_Edge aStreamRightEdge = TopoDS::Edge( aStream->GetRightShape() );
+
+  Standard_Real aFirst = 0.0, aLast = 0.0;
+  Handle(Geom_Curve) anEdgeLeftCurve = BRep_Tool::Curve( aStreamLeftEdge, aFirst, aLast );
+  Handle(Geom_Curve) anEdgeRightCurve = BRep_Tool::Curve( aStreamRightEdge, aFirst, aLast );
+
+  GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
+  GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
+
+  Standard_Real aLeftDist = aLeftProject.LowerDistance();
+  Standard_Real aRightDist = aRightProject.LowerDistance();
+
+  // Find the altitude in profiles
+  Standard_Real aLeftAlt = getAltitudeFromProfile( aLeftProfile, aLeftDist, aRightDist );
+  Standard_Real aRightAlt = getAltitudeFromProfile( aRightProfile, aLeftDist, aRightDist );
+
+  // Interpolate altitudes
+  // Left profile line ( the segment between the firts and the last profile point )
+  HYDROData_Profile::ProfilePoints aLeftProfilePoints = aLeftProfile->GetProfilePoints( false );
+  gp_Pnt aLeftProfileP1( aLeftProfilePoints.First() );
+  aLeftProfileP1.SetZ( 0 );
+  gp_Pnt aLeftProfileP2( aLeftProfilePoints.Last() );
+  aLeftProfileP2.SetZ( 0 );
+  gp_Vec aLeftProfileVec( aLeftProfileP1, aLeftProfileP2 );
+  Handle(Geom_Line) aLeftProfileLine = new Geom_Line( gp_Ax1( aLeftProfileP1, aLeftProfileVec ) );
+  // Right profile line
+  HYDROData_Profile::ProfilePoints aRightProfilePoints = aRightProfile->GetProfilePoints( false );
+  gp_Pnt aRightProfileP1( aRightProfilePoints.First() );
+  aRightProfileP1.SetZ( 0 );
+  gp_Pnt aRightProfileP2( aRightProfilePoints.Last() );
+  aRightProfileP2.SetZ( 0 );
+  gp_Vec aRightProfileVec( aRightProfileP1, aRightProfileP2 );
+  Handle(Geom_Line) aRightProfileLine = new Geom_Line( gp_Ax1( aRightProfileP1, aRightProfileVec ) );
+  // The point projections on the left and right profiles
+  GeomAPI_ProjectPointOnCurve aLeftProfileProject( aPointToTest, aLeftProfileLine );
+  GeomAPI_ProjectPointOnCurve aRightProfileProject( aPointToTest, aRightProfileLine );
+  // The point distance to the left and right profiles
+  Standard_Real aLeftProfileDist = aLeftProfileProject.LowerDistance();
+  Standard_Real aRightProfileDist = aRightProfileProject.LowerDistance();
+  // The coefficients
+  Standard_Real aFirstCoeff = aLeftProfileDist / ( aLeftProfileDist + aRightProfileDist );
+  Standard_Real aSecCoeff = aRightProfileDist / ( aLeftProfileDist + aRightProfileDist );
+
+  aResAltitude = aLeftAlt * ( 1 - aFirstCoeff ) + aRightAlt * ( 1 - aSecCoeff );
+
   return aResAltitude;
 }