2 #include "HYDROData_StreamAltitude.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Profile.h"
6 #include "HYDROData_Stream.h"
7 #include "HYDROData_ShapesTool.h"
8 #include <HYDROData_Tool.h>
9 #include <BRep_Tool.hxx>
11 #include <BRepBuilderAPI_MakeEdge.hxx>
12 #include <BRepBuilderAPI_MakeFace.hxx>
13 #include <BRepBuilderAPI_MakeWire.hxx>
15 #include <Extrema_ExtElC.hxx>
17 #include <GeomAPI_ProjectPointOnCurve.hxx>
21 #include <Precision.hxx>
23 #include <TopExp_Explorer.hxx>
26 #include <TopoDS_Wire.hxx>
27 #include <TopoDS_Edge.hxx>
29 #include <TopTools_SequenceOfShape.hxx>
31 #include <QStringList>
34 #include <BRepTools.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
41 HYDROData_StreamAltitude::HYDROData_StreamAltitude()
42 : HYDROData_IAltitudeObject()
46 HYDROData_StreamAltitude::~HYDROData_StreamAltitude()
50 Standard_Real getAltitudeFromProfile( const Handle(HYDROData_Profile)& theProfile,
51 const Standard_Real& theLeftDist,
52 const Standard_Real& theRightDist )
54 Standard_Real aResAlt = 0.0;
56 gp_XY aFirstPoint, aLastPoint;
57 if ( !theProfile->GetLeftPoint( aFirstPoint ) ||
58 !theProfile->GetRightPoint( aLastPoint ) )
61 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
62 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
64 Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
66 Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
68 gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
70 gp_Lin aPointLine( anIntPoint, gp::DZ() );
74 HYDROData_Profile::ProfilePoints aProfilePoints = theProfile->GetProfilePoints();
75 for ( int i = 1, n = aProfilePoints.Length(); i <= n; ++i )
77 gp_Pnt aProfPoint( aProfilePoints.Value( i ) );
79 Standard_Real aDist = aPointLine.Distance( aProfPoint );
80 if ( aDist <= gp::Resolution() )
82 // We found the intersected point
83 aResAlt = aProfPoint.Z();
87 gp_Lin aNormal = aPointLine.Normal( aProfPoint );
90 aPrevNormal = aNormal;
91 aPrevPoint = aProfPoint;
95 if ( aPrevNormal.Direction().Dot( aNormal.Direction() ) < 0 )
97 // We found the intersected edge
98 gp_Lin anEdgeLine( aPrevPoint, gp_Dir( gp_Vec( aPrevPoint, aProfPoint ) ) );
100 Extrema_ExtElC anExtrema( aPointLine, anEdgeLine, Precision::Angular() );
101 if ( !anExtrema.IsParallel() )
103 Extrema_POnCurv aFirstPnt, aSecPnt;
104 anExtrema.Points( 1, aFirstPnt, aSecPnt );
106 const gp_Pnt& anIntPnt = aSecPnt.Value();
107 aResAlt = anIntPnt.Z();
113 aPrevNormal = aNormal;
114 aPrevPoint = aProfPoint;
120 bool HYDROData_StreamAltitude::getBoundaryProfilesForPoint(
121 const gp_XY& thePoint,
122 Handle(HYDROData_Profile)& theLeftProfile,
123 Handle(HYDROData_Profile)& theRightProfile ) const
125 Handle(HYDROData_Stream) aStream =
126 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
127 if ( aStream.IsNull() )
130 HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
131 if ( aStreamProfiles.Length() < 2 )
134 Handle(HYDROData_Profile) aPrevProfile;
135 gp_Pnt aPrevPnt1, aPrevPnt2;
136 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
138 Handle(HYDROData_Profile) aProfile =
139 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
140 if ( aProfile.IsNull() )
143 gp_XY aFirstPoint, aLastPoint;
144 if ( !aProfile->GetLeftPoint( aFirstPoint ) || !aProfile->GetRightPoint( aLastPoint ) )
147 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
148 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
150 if ( !aPrevProfile.IsNull() )
152 BRepBuilderAPI_MakeEdge aLeftMakeEdge( aPrevPnt1, aPrevPnt2 );
153 BRepBuilderAPI_MakeEdge aBotMakeEdge( aPrevPnt2, aPnt2 );
154 BRepBuilderAPI_MakeEdge aRightMakeEdge( aPnt2, aPnt1 );
155 BRepBuilderAPI_MakeEdge aTopMakeEdge( aPnt1, aPrevPnt1 );
157 BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(),
158 aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
160 BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
162 TopoDS_Face aProfilesFace = aMakeFace.Face();
164 TopoDS_Compound aCmp;
166 aBB.MakeCompound(aCmp);
167 aBB.Add(aCmp, aProfilesFace);
168 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
169 BRepBuilderAPI_MakeVertex aMk(aPnt);
170 aBB.Add(aCmp, aMk.Vertex());
171 BRepTools::Write(aCmp, "ProfileFace.brep");
174 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aProfilesFace);
177 cout << "Point status is = " << aPointState <<endl;
179 if ( aPointState != TopAbs_OUT )
181 theLeftProfile = aPrevProfile;
182 theRightProfile = aProfile;
187 aPrevProfile = aProfile;
192 return !theLeftProfile.IsNull() && !theRightProfile.IsNull();
195 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
197 double aResAltitude = GetInvalidAltitude();
199 Handle(HYDROData_Stream) aStream =
200 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
201 if ( aStream.IsNull() )
204 TopoDS_Shape aStreamShape = aStream->GetTopShape();
205 if ( aStreamShape.IsNull() )
208 TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
209 if ( !aStreamFaceExp.More() )
212 // Get only face because of 2d profile wires is in compound
213 TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
215 // Check if point is inside of stream presentation
216 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aStreamFace);
219 cout << "Point status is = " << aPointState <<endl;
220 TopoDS_Compound aCmp;
222 aBB.MakeCompound(aCmp);
223 aBB.Add(aCmp, aStreamFace);
224 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
225 BRepBuilderAPI_MakeVertex aMk(aPnt);
226 aBB.Add(aCmp, aMk.Vertex());
227 BRepTools::Write(aCmp, "FCL2d.brep");
229 if ( aPointState == TopAbs_OUT )
232 // Find the two profiles between which the point is lies
233 Handle(HYDROData_Profile) aLeftProfile, aRightProfile;
234 if ( !getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile ) )
237 // Find the projections of point to borders of stream
238 gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
240 TopoDS_Edge aStreamLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
241 TopoDS_Edge aStreamRightEdge = TopoDS::Edge( aStream->GetRightShape() );
243 Standard_Real aFirst = 0.0, aLast = 0.0;
244 Handle(Geom_Curve) anEdgeLeftCurve = BRep_Tool::Curve( aStreamLeftEdge, aFirst, aLast );
245 Handle(Geom_Curve) anEdgeRightCurve = BRep_Tool::Curve( aStreamRightEdge, aFirst, aLast );
247 GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
248 GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
250 Standard_Real aLeftDist = aLeftProject.LowerDistance();
251 Standard_Real aRightDist = aRightProject.LowerDistance();
253 // Find the altitude in profiles
254 Standard_Real aLeftAlt = getAltitudeFromProfile( aLeftProfile, aLeftDist, aRightDist );
255 Standard_Real aRightAlt = getAltitudeFromProfile( aRightProfile, aLeftDist, aRightDist );
257 // Interpolate altitudes
258 Standard_Real aFirstCoeff = aLeftDist / ( aLeftDist + aRightDist );
259 Standard_Real aSecCoeff = aRightDist / ( aLeftDist + aRightDist );
261 aResAltitude = aLeftAlt * aFirstCoeff + aRightAlt * aSecCoeff;