1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_StreamAltitude.h"
21 #include "HYDROData_Document.h"
22 #include "HYDROData_Profile.h"
23 #include "HYDROData_Stream.h"
24 #include "HYDROData_ShapesTool.h"
25 #include <HYDROData_Tool.h>
26 #include <BRep_Tool.hxx>
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <Extrema_ExtElC.hxx>
34 #include <GeomAPI_ProjectPointOnCurve.hxx>
38 #include <Precision.hxx>
40 #include <TopExp_Explorer.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopoDS_Edge.hxx>
46 #include <TopTools_SequenceOfShape.hxx>
48 #include <Geom_Line.hxx>
50 #include <QStringList>
53 #include "HYDRO_trace.hxx"
56 #include <BRepTools.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BRepBuilderAPI_MakeVertex.hxx>
60 IMPLEMENT_STANDARD_HANDLE(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
61 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
63 HYDROData_StreamAltitude::HYDROData_StreamAltitude()
64 : HYDROData_IAltitudeObject()
68 HYDROData_StreamAltitude::~HYDROData_StreamAltitude()
72 Standard_Real getAltitudeFromProfile( const Handle(HYDROData_Profile)& theProfile,
73 const Standard_Real& theLeftDist,
74 const Standard_Real& theRightDist )
76 Standard_Real aResAlt = 0.0;
78 gp_XY aFirstPoint, aLastPoint;
79 if ( !theProfile->GetLeftPoint( aFirstPoint, false ) ||
80 !theProfile->GetRightPoint( aLastPoint, false ) )
83 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
84 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
86 Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
88 Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
90 gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
92 gp_Lin aPointLine( anIntPoint, gp::DZ() );
96 HYDROData_Profile::ProfilePoints aProfilePoints = theProfile->GetProfilePoints( false );
97 for ( int i = 1, n = aProfilePoints.Length(); i <= n; ++i )
99 gp_Pnt aProfPoint( aProfilePoints.Value( i ) );
101 Standard_Real aDist = aPointLine.Distance( aProfPoint );
102 if ( aDist <= gp::Resolution() )
104 // We found the intersected point
105 aResAlt = aProfPoint.Z();
109 gp_Lin aNormal = aPointLine.Normal( aProfPoint );
112 aPrevNormal = aNormal;
113 aPrevPoint = aProfPoint;
117 if ( aPrevNormal.Direction().Dot( aNormal.Direction() ) < 0 )
119 // We found the intersected edge
120 gp_Lin anEdgeLine( aPrevPoint, gp_Dir( gp_Vec( aPrevPoint, aProfPoint ) ) );
122 Extrema_ExtElC anExtrema( aPointLine, anEdgeLine, Precision::Angular() );
123 if ( !anExtrema.IsParallel() )
125 Extrema_POnCurv aFirstPnt, aSecPnt;
126 anExtrema.Points( 1, aFirstPnt, aSecPnt );
128 const gp_Pnt& anIntPnt = aSecPnt.Value();
129 aResAlt = anIntPnt.Z();
135 aPrevNormal = aNormal;
136 aPrevPoint = aProfPoint;
142 bool HYDROData_StreamAltitude::getBoundaryProfilesForPoint(
143 const gp_XY& thePoint,
144 Handle(HYDROData_Profile)& theLeftProfile,
145 Handle(HYDROData_Profile)& theRightProfile ) const
147 Handle(HYDROData_Stream) aStream =
148 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
149 if ( aStream.IsNull() )
152 HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
153 if ( aStreamProfiles.Length() < 2 )
156 Handle(HYDROData_Profile) aPrevProfile;
157 gp_Pnt aPrevPnt1, aPrevPnt2;
158 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
160 Handle(HYDROData_Profile) aProfile =
161 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
162 if ( aProfile.IsNull() )
165 gp_XY aFirstPoint, aLastPoint;
166 if ( !aProfile->GetLeftPoint( aFirstPoint, false ) ||
167 !aProfile->GetRightPoint( aLastPoint, false ) )
170 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
171 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
173 if ( !aPrevProfile.IsNull() )
175 BRepBuilderAPI_MakeEdge aLeftMakeEdge( aPrevPnt1, aPrevPnt2 );
176 BRepBuilderAPI_MakeEdge aBotMakeEdge( aPrevPnt2, aPnt2 );
177 BRepBuilderAPI_MakeEdge aRightMakeEdge( aPnt2, aPnt1 );
178 BRepBuilderAPI_MakeEdge aTopMakeEdge( aPnt1, aPrevPnt1 );
180 BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(),
181 aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
183 BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
185 TopoDS_Face aProfilesFace = aMakeFace.Face();
187 TopoDS_Compound aCmp;
189 aBB.MakeCompound(aCmp);
190 aBB.Add(aCmp, aProfilesFace);
191 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
192 BRepBuilderAPI_MakeVertex aMk(aPnt);
193 aBB.Add(aCmp, aMk.Vertex());
194 BRepTools::Write(aCmp, "ProfileFace.brep");
197 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aProfilesFace);
200 cout << "Point status is = " << aPointState <<endl;
202 if ( aPointState != TopAbs_OUT )
204 theLeftProfile = aPrevProfile;
205 theRightProfile = aProfile;
210 aPrevProfile = aProfile;
215 return !theLeftProfile.IsNull() && !theRightProfile.IsNull();
218 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
220 DEBTRACE("GetAltitudeForPoint p(" << thePoint.X() << ", " << thePoint.Y() << ")");
221 double aResAltitude = GetInvalidAltitude();
223 Handle(HYDROData_Stream) aStream =
224 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
225 if ( aStream.IsNull() )
227 DEBTRACE("aStream.IsNull()");
231 TopoDS_Shape aStreamShape = aStream->GetTopShape();
232 if ( aStreamShape.IsNull() )
234 DEBTRACE("aStreamShape.IsNull()");
238 TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
239 if ( !aStreamFaceExp.More() )
241 DEBTRACE("!aStreamFaceExp.More()");
245 // Get only face because of 2d profile wires is in compound
246 TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
248 // Check if point is inside of stream presentation
249 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aStreamFace);
252 cout << "Point status is = " << aPointState <<endl;
253 TopoDS_Compound aCmp;
255 aBB.MakeCompound(aCmp);
256 aBB.Add(aCmp, aStreamFace);
257 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
258 BRepBuilderAPI_MakeVertex aMk(aPnt);
259 aBB.Add(aCmp, aMk.Vertex());
260 BRepTools::Write(aCmp, "FCL2d.brep");
262 if ( aPointState == TopAbs_OUT )
264 DEBTRACE("aPointState == TopAbs_OUT");
268 // Find the two profiles between which the point is lies
269 Handle(HYDROData_Profile) aLeftProfile, aRightProfile;
270 if ( !getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile ) )
272 DEBTRACE("!getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile )");
276 // Find the projections of point to borders of stream
277 gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
279 TopoDS_Edge aStreamLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
280 TopoDS_Edge aStreamRightEdge = TopoDS::Edge( aStream->GetRightShape() );
282 Standard_Real aFirst = 0.0, aLast = 0.0;
283 Handle(Geom_Curve) anEdgeLeftCurve = BRep_Tool::Curve( aStreamLeftEdge, aFirst, aLast );
284 Handle(Geom_Curve) anEdgeRightCurve = BRep_Tool::Curve( aStreamRightEdge, aFirst, aLast );
286 GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
287 GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
289 Standard_Real aLeftDist = aLeftProject.LowerDistance();
290 Standard_Real aRightDist = aRightProject.LowerDistance();
292 // Find the altitude in profiles
293 Standard_Real aLeftAlt = getAltitudeFromProfile( aLeftProfile, aLeftDist, aRightDist );
294 Standard_Real aRightAlt = getAltitudeFromProfile( aRightProfile, aLeftDist, aRightDist );
296 // Interpolate altitudes
297 // Left profile line ( the segment between the firts and the last profile point )
298 HYDROData_Profile::ProfilePoints aLeftProfilePoints = aLeftProfile->GetProfilePoints( false );
299 gp_Pnt aLeftProfileP1( aLeftProfilePoints.First() );
300 aLeftProfileP1.SetZ( 0 );
301 gp_Pnt aLeftProfileP2( aLeftProfilePoints.Last() );
302 aLeftProfileP2.SetZ( 0 );
303 gp_Vec aLeftProfileVec( aLeftProfileP1, aLeftProfileP2 );
304 Handle(Geom_Line) aLeftProfileLine = new Geom_Line( gp_Ax1( aLeftProfileP1, aLeftProfileVec ) );
305 // Right profile line
306 HYDROData_Profile::ProfilePoints aRightProfilePoints = aRightProfile->GetProfilePoints( false );
307 gp_Pnt aRightProfileP1( aRightProfilePoints.First() );
308 aRightProfileP1.SetZ( 0 );
309 gp_Pnt aRightProfileP2( aRightProfilePoints.Last() );
310 aRightProfileP2.SetZ( 0 );
311 gp_Vec aRightProfileVec( aRightProfileP1, aRightProfileP2 );
312 Handle(Geom_Line) aRightProfileLine = new Geom_Line( gp_Ax1( aRightProfileP1, aRightProfileVec ) );
313 // The point projections on the left and right profiles
314 GeomAPI_ProjectPointOnCurve aLeftProfileProject( aPointToTest, aLeftProfileLine );
315 GeomAPI_ProjectPointOnCurve aRightProfileProject( aPointToTest, aRightProfileLine );
316 // The point distance to the left and right profiles
317 Standard_Real aLeftProfileDist = aLeftProfileProject.LowerDistance();
318 Standard_Real aRightProfileDist = aRightProfileProject.LowerDistance();
320 Standard_Real aFirstCoeff = aLeftProfileDist / ( aLeftProfileDist + aRightProfileDist );
321 Standard_Real aSecCoeff = aRightProfileDist / ( aLeftProfileDist + aRightProfileDist );
323 aResAltitude = aLeftAlt * ( 1 - aFirstCoeff ) + aRightAlt * ( 1 - aSecCoeff );
324 DEBTRACE("aResAltitude=" << aResAltitude);