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 <BRepTools.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRepBuilderAPI_MakeVertex.hxx>
57 IMPLEMENT_STANDARD_HANDLE(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
58 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
60 HYDROData_StreamAltitude::HYDROData_StreamAltitude()
61 : HYDROData_IAltitudeObject()
65 HYDROData_StreamAltitude::~HYDROData_StreamAltitude()
69 Standard_Real getAltitudeFromProfile( const Handle(HYDROData_Profile)& theProfile,
70 const Standard_Real& theLeftDist,
71 const Standard_Real& theRightDist )
73 Standard_Real aResAlt = 0.0;
75 gp_XY aFirstPoint, aLastPoint;
76 if ( !theProfile->GetLeftPoint( aFirstPoint, false ) ||
77 !theProfile->GetRightPoint( aLastPoint, false ) )
80 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
81 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
83 Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
85 Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
87 gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
89 gp_Lin aPointLine( anIntPoint, gp::DZ() );
93 HYDROData_Profile::ProfilePoints aProfilePoints = theProfile->GetProfilePoints( false );
94 for ( int i = 1, n = aProfilePoints.Length(); i <= n; ++i )
96 gp_Pnt aProfPoint( aProfilePoints.Value( i ) );
98 Standard_Real aDist = aPointLine.Distance( aProfPoint );
99 if ( aDist <= gp::Resolution() )
101 // We found the intersected point
102 aResAlt = aProfPoint.Z();
106 gp_Lin aNormal = aPointLine.Normal( aProfPoint );
109 aPrevNormal = aNormal;
110 aPrevPoint = aProfPoint;
114 if ( aPrevNormal.Direction().Dot( aNormal.Direction() ) < 0 )
116 // We found the intersected edge
117 gp_Lin anEdgeLine( aPrevPoint, gp_Dir( gp_Vec( aPrevPoint, aProfPoint ) ) );
119 Extrema_ExtElC anExtrema( aPointLine, anEdgeLine, Precision::Angular() );
120 if ( !anExtrema.IsParallel() )
122 Extrema_POnCurv aFirstPnt, aSecPnt;
123 anExtrema.Points( 1, aFirstPnt, aSecPnt );
125 const gp_Pnt& anIntPnt = aSecPnt.Value();
126 aResAlt = anIntPnt.Z();
132 aPrevNormal = aNormal;
133 aPrevPoint = aProfPoint;
139 bool HYDROData_StreamAltitude::getBoundaryProfilesForPoint(
140 const gp_XY& thePoint,
141 Handle(HYDROData_Profile)& theLeftProfile,
142 Handle(HYDROData_Profile)& theRightProfile ) const
144 Handle(HYDROData_Stream) aStream =
145 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
146 if ( aStream.IsNull() )
149 HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
150 if ( aStreamProfiles.Length() < 2 )
153 Handle(HYDROData_Profile) aPrevProfile;
154 gp_Pnt aPrevPnt1, aPrevPnt2;
155 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
157 Handle(HYDROData_Profile) aProfile =
158 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
159 if ( aProfile.IsNull() )
162 gp_XY aFirstPoint, aLastPoint;
163 if ( !aProfile->GetLeftPoint( aFirstPoint, false ) ||
164 !aProfile->GetRightPoint( aLastPoint, false ) )
167 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
168 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
170 if ( !aPrevProfile.IsNull() )
172 BRepBuilderAPI_MakeEdge aLeftMakeEdge( aPrevPnt1, aPrevPnt2 );
173 BRepBuilderAPI_MakeEdge aBotMakeEdge( aPrevPnt2, aPnt2 );
174 BRepBuilderAPI_MakeEdge aRightMakeEdge( aPnt2, aPnt1 );
175 BRepBuilderAPI_MakeEdge aTopMakeEdge( aPnt1, aPrevPnt1 );
177 BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(),
178 aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
180 BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
182 TopoDS_Face aProfilesFace = aMakeFace.Face();
184 TopoDS_Compound aCmp;
186 aBB.MakeCompound(aCmp);
187 aBB.Add(aCmp, aProfilesFace);
188 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
189 BRepBuilderAPI_MakeVertex aMk(aPnt);
190 aBB.Add(aCmp, aMk.Vertex());
191 BRepTools::Write(aCmp, "ProfileFace.brep");
194 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aProfilesFace);
197 cout << "Point status is = " << aPointState <<endl;
199 if ( aPointState != TopAbs_OUT )
201 theLeftProfile = aPrevProfile;
202 theRightProfile = aProfile;
207 aPrevProfile = aProfile;
212 return !theLeftProfile.IsNull() && !theRightProfile.IsNull();
215 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
217 double aResAltitude = GetInvalidAltitude();
219 Handle(HYDROData_Stream) aStream =
220 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
221 if ( aStream.IsNull() )
224 TopoDS_Shape aStreamShape = aStream->GetTopShape();
225 if ( aStreamShape.IsNull() )
228 TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
229 if ( !aStreamFaceExp.More() )
232 // Get only face because of 2d profile wires is in compound
233 TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
235 // Check if point is inside of stream presentation
236 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aStreamFace);
239 cout << "Point status is = " << aPointState <<endl;
240 TopoDS_Compound aCmp;
242 aBB.MakeCompound(aCmp);
243 aBB.Add(aCmp, aStreamFace);
244 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
245 BRepBuilderAPI_MakeVertex aMk(aPnt);
246 aBB.Add(aCmp, aMk.Vertex());
247 BRepTools::Write(aCmp, "FCL2d.brep");
249 if ( aPointState == TopAbs_OUT )
252 // Find the two profiles between which the point is lies
253 Handle(HYDROData_Profile) aLeftProfile, aRightProfile;
254 if ( !getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile ) )
257 // Find the projections of point to borders of stream
258 gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
260 TopoDS_Edge aStreamLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
261 TopoDS_Edge aStreamRightEdge = TopoDS::Edge( aStream->GetRightShape() );
263 Standard_Real aFirst = 0.0, aLast = 0.0;
264 Handle(Geom_Curve) anEdgeLeftCurve = BRep_Tool::Curve( aStreamLeftEdge, aFirst, aLast );
265 Handle(Geom_Curve) anEdgeRightCurve = BRep_Tool::Curve( aStreamRightEdge, aFirst, aLast );
267 GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
268 GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
270 Standard_Real aLeftDist = aLeftProject.LowerDistance();
271 Standard_Real aRightDist = aRightProject.LowerDistance();
273 // Find the altitude in profiles
274 Standard_Real aLeftAlt = getAltitudeFromProfile( aLeftProfile, aLeftDist, aRightDist );
275 Standard_Real aRightAlt = getAltitudeFromProfile( aRightProfile, aLeftDist, aRightDist );
277 // Interpolate altitudes
278 // Left profile line ( the segment between the firts and the last profile point )
279 HYDROData_Profile::ProfilePoints aLeftProfilePoints = aLeftProfile->GetProfilePoints( false );
280 gp_Pnt aLeftProfileP1( aLeftProfilePoints.First() );
281 aLeftProfileP1.SetZ( 0 );
282 gp_Pnt aLeftProfileP2( aLeftProfilePoints.Last() );
283 aLeftProfileP2.SetZ( 0 );
284 gp_Vec aLeftProfileVec( aLeftProfileP1, aLeftProfileP2 );
285 Handle(Geom_Line) aLeftProfileLine = new Geom_Line( gp_Ax1( aLeftProfileP1, aLeftProfileVec ) );
286 // Right profile line
287 HYDROData_Profile::ProfilePoints aRightProfilePoints = aRightProfile->GetProfilePoints( false );
288 gp_Pnt aRightProfileP1( aRightProfilePoints.First() );
289 aRightProfileP1.SetZ( 0 );
290 gp_Pnt aRightProfileP2( aRightProfilePoints.Last() );
291 aRightProfileP2.SetZ( 0 );
292 gp_Vec aRightProfileVec( aRightProfileP1, aRightProfileP2 );
293 Handle(Geom_Line) aRightProfileLine = new Geom_Line( gp_Ax1( aRightProfileP1, aRightProfileVec ) );
294 // The point projections on the left and right profiles
295 GeomAPI_ProjectPointOnCurve aLeftProfileProject( aPointToTest, aLeftProfileLine );
296 GeomAPI_ProjectPointOnCurve aRightProfileProject( aPointToTest, aRightProfileLine );
297 // The point distance to the left and right profiles
298 Standard_Real aLeftProfileDist = aLeftProfileProject.LowerDistance();
299 Standard_Real aRightProfileDist = aRightProfileProject.LowerDistance();
301 Standard_Real aFirstCoeff = aLeftProfileDist / ( aLeftProfileDist + aRightProfileDist );
302 Standard_Real aSecCoeff = aRightProfileDist / ( aLeftProfileDist + aRightProfileDist );
304 aResAltitude = aLeftAlt * ( 1 - aFirstCoeff ) + aRightAlt * ( 1 - aSecCoeff );