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_Channel.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_Profile.h"
24 #include "HYDROData_Stream.h"
25 #include "HYDROData_ShapesTool.h"
26 #include <HYDROData_Tool.h>
27 #include <BRep_Tool.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeFace.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <Extrema_ExtElC.hxx>
35 #include <GeomAPI_ProjectPointOnCurve.hxx>
39 #include <Precision.hxx>
42 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <TopoDS_Edge.hxx>
48 #include <TopTools_SequenceOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <Geom_Line.hxx>
53 #include <QStringList>
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 bool IsPointBetweenEdges( const gp_Pnt& aFirstPnt1, const gp_Pnt& aLastPnt1,
73 const gp_Pnt& aFirstPnt2, const gp_Pnt& aLastPnt2,
74 const gp_Pnt& thePoint) {
75 BRepBuilderAPI_MakeEdge aLeftMakeEdge( aFirstPnt1, aLastPnt1 );
76 BRepBuilderAPI_MakeEdge aBotMakeEdge( aLastPnt1, aLastPnt2 );
77 BRepBuilderAPI_MakeEdge aRightMakeEdge( aLastPnt2, aFirstPnt2 );
78 BRepBuilderAPI_MakeEdge aTopMakeEdge( aFirstPnt2, aFirstPnt1 );
80 BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(),
81 aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
83 BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
85 TopoDS_Face aFace = aMakeFace.Face();
89 aBB.MakeCompound(aCmp);
91 BRepBuilderAPI_MakeVertex aMk(thePoint);
92 aBB.Add(aCmp, aMk.Vertex());
93 BRepTools::Write(aCmp, "ProfileFace.brep");
96 gp_XY anXY( thePoint.X(), thePoint.Y() );
97 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(anXY, aFace);
100 cout << "Point status is = " << aPointState <<endl;
102 return aPointState != TopAbs_OUT;
105 Standard_Real getAltitudeFromWire( const TopoDS_Wire& theWire,
106 const Standard_Real& theLeftDist,
107 const Standard_Real& theRightDist )
109 Standard_Real aResAlt = 0.0;
111 gp_XY aFirstPoint, aLastPoint;
112 TopoDS_Vertex aFirstVertex, aLastVertex;
113 TopExp::Vertices( theWire, aFirstVertex, aLastVertex );
115 gp_Pnt aPnt1( BRep_Tool::Pnt( aFirstVertex ) );
117 gp_Pnt aPnt2( BRep_Tool::Pnt( aLastVertex ) );
120 Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
122 Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
124 gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
126 return HYDROData_Tool::GetAltitudeForWire( theWire,
127 gp_XY(anIntPoint.X(), anIntPoint.Y()),
130 HYDROData_IAltitudeObject::GetInvalidAltitude() );
133 bool HYDROData_StreamAltitude::getBoundaryWiresForPoint(
134 const gp_XY& thePoint,
135 TopoDS_Wire& theLeftWire,
136 TopoDS_Wire& theRightWire ) const
138 gp_Pnt aTestPnt( thePoint.X(), thePoint.Y(), 0 );
140 Handle(HYDROData_Object) anObject =
141 Handle(HYDROData_Object)::DownCast( GetFatherObject() );
142 if ( anObject.IsNull() ) {
146 if ( anObject->GetKind() == KIND_STREAM ) {
147 Handle(HYDROData_Stream) aStream = Handle(HYDROData_Stream)::DownCast( anObject );
148 if ( aStream.IsNull() )
151 HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
152 if ( aStreamProfiles.Length() < 2 )
155 Handle(HYDROData_Profile) aPrevProfile;
156 gp_Pnt aPrevPnt1, aPrevPnt2;
157 for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
159 Handle(HYDROData_Profile) aProfile =
160 Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
161 if ( aProfile.IsNull() )
164 gp_XY aFirstPoint, aLastPoint;
165 if ( !aProfile->GetLeftPoint( aFirstPoint, false ) ||
166 !aProfile->GetRightPoint( aLastPoint, false ) )
169 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
170 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
172 if ( !aPrevProfile.IsNull() )
174 if ( IsPointBetweenEdges( aPrevPnt1, aPrevPnt2, aPnt1, aPnt2, aTestPnt ) )
176 theLeftWire = TopoDS::Wire( aPrevProfile->GetShape3D() );
177 theRightWire = TopoDS::Wire( aProfile->GetShape3D() );
182 aPrevProfile = aProfile;
186 } else if ( anObject->GetKind() == KIND_CHANNEL ) {
187 Handle(HYDROData_Channel) aChannel = Handle(HYDROData_Channel)::DownCast( anObject );
188 if ( aChannel.IsNull() )
191 TopTools_ListOfShape aWiresList;
192 TopExp_Explorer anExp( aChannel->GetShape3D(), TopAbs_WIRE );
193 for ( ; anExp.More(); anExp.Next() ) {
194 if(!anExp.Current().IsNull()) {
195 const TopoDS_Wire& aWire = TopoDS::Wire( anExp.Current() );
196 aWiresList.Append( aWire );
200 if ( aWiresList.Extent() < 2 )
203 TopoDS_Wire aPrevWire;
204 gp_Pnt aPrevPnt1, aPrevPnt2;
206 TopTools_ListIteratorOfListOfShape anIt( aWiresList );
207 for ( ; anIt.More(); anIt.Next() ) {
208 TopoDS_Wire& aWire = TopoDS::Wire( anIt.Value() );
209 if ( aWire.IsNull() )
212 TopoDS_Vertex aFirstVertex, aLastVertex;
213 TopExp::Vertices( aWire, aFirstVertex, aLastVertex );
215 gp_Pnt aPnt1( BRep_Tool::Pnt( aFirstVertex ) );
217 gp_Pnt aPnt2( BRep_Tool::Pnt( aLastVertex ) );
220 if ( !aPrevWire.IsNull() ) {
221 if ( IsPointBetweenEdges( aPrevPnt1, aPrevPnt2, aPnt1, aPnt2, aTestPnt ) ) {
222 theLeftWire = aPrevWire;
223 theRightWire = aWire;
234 return !theLeftWire.IsNull() && !theRightWire.IsNull();
237 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
239 double aResAltitude = GetInvalidAltitude();
241 Handle(HYDROData_Object) anObject =
242 Handle(HYDROData_Object)::DownCast( GetFatherObject() );
243 if ( anObject.IsNull() )
246 TopoDS_Shape aTopShape = anObject->GetTopShape();
247 if ( aTopShape.IsNull() )
250 TopExp_Explorer aFaceExp( aTopShape, TopAbs_FACE );
251 if ( !aFaceExp.More() )
254 // Get only face because of 2d profile wires is in compound
255 TopoDS_Face aFace = TopoDS::Face( aFaceExp.Current() );
257 // Check if point is inside of stream/channel presentation
258 TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aFace);
261 cout << "Point status is = " << aPointState <<endl;
262 TopoDS_Compound aCmp;
264 aBB.MakeCompound(aCmp);
265 aBB.Add(aCmp, aFace);
266 gp_Pnt aPnt (thePoint.X(), thePoint.Y(), 0.);
267 BRepBuilderAPI_MakeVertex aMk(aPnt);
268 aBB.Add(aCmp, aMk.Vertex());
269 BRepTools::Write(aCmp, "FCL2d.brep");
271 if ( aPointState == TopAbs_OUT )
274 TopoDS_Edge aLeftEdge, aRightEdge;
276 if ( anObject->GetKind() == KIND_STREAM ) {
277 Handle(HYDROData_Stream) aStream =
278 Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
279 if ( !aStream.IsNull() ) {
280 aLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
281 aRightEdge = TopoDS::Edge( aStream->GetRightShape() );
283 } else if ( anObject->GetKind() == KIND_CHANNEL ) {
284 Handle(HYDROData_Channel) aChannel =
285 Handle(HYDROData_Channel)::DownCast( GetFatherObject() );
286 if ( !aChannel.IsNull() ) {
287 aLeftEdge = TopoDS::Edge( aChannel->GetLeftShape() );
288 aRightEdge = TopoDS::Edge( aChannel->GetRightShape() );
292 // Find the two profiles between which the point is lies
293 TopoDS_Wire aLeftWire, aRightWire;
294 if ( !getBoundaryWiresForPoint( thePoint, aLeftWire, aRightWire ) )
297 // Find the projections of point to borders of stream
298 gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
300 Standard_Real aFirst = 0.0, aLast = 0.0;
301 Handle(Geom_Curve) anEdgeLeftCurve = BRep_Tool::Curve( aLeftEdge, aFirst, aLast );
302 Handle(Geom_Curve) anEdgeRightCurve = BRep_Tool::Curve( aRightEdge, aFirst, aLast );
304 GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
305 GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
307 int aNbPoints1 = aRightProject.NbPoints();
308 int aNbPoints2 = aLeftProject.NbPoints();
309 if ( aNbPoints1 < 1 || aNbPoints2 < 1)
311 Standard_Real aLeftDist = aLeftProject.LowerDistance();
312 Standard_Real aRightDist = aRightProject.LowerDistance();
314 // Find the altitude in profiles
315 Standard_Real aLeftAlt, aRightAlt;
316 gp_Pnt aLeftProfileP1, aLeftProfileP2, aRightProfileP1, aRightProfileP2;
317 aLeftAlt = getAltitudeFromWire( aLeftWire, aLeftDist, aRightDist );
318 aRightAlt = getAltitudeFromWire( aRightWire, aLeftDist, aRightDist );
320 TopoDS_Vertex aFirstVertex, aLastVertex;
321 TopExp::Vertices( aLeftWire, aFirstVertex, aLastVertex );
322 aLeftProfileP1 = BRep_Tool::Pnt( aFirstVertex );
323 aLeftProfileP2 = BRep_Tool::Pnt( aLastVertex );
325 TopExp::Vertices( aRightWire, aFirstVertex, aLastVertex );
326 aRightProfileP1 = BRep_Tool::Pnt( aFirstVertex );
327 aRightProfileP2 = BRep_Tool::Pnt( aLastVertex );
329 // Interpolate altitudes
330 // Left profile line ( the segment between the firts and the last profile point )
331 aLeftProfileP1.SetZ( 0 );
332 aLeftProfileP2.SetZ( 0 );
333 gp_Vec aLeftProfileVec( aLeftProfileP1, aLeftProfileP2 );
334 Handle(Geom_Line) aLeftProfileLine = new Geom_Line( gp_Ax1( aLeftProfileP1, aLeftProfileVec ) );
335 // Right profile line
336 aRightProfileP1.SetZ( 0 );
337 aRightProfileP2.SetZ( 0 );
338 gp_Vec aRightProfileVec( aRightProfileP1, aRightProfileP2 );
339 Handle(Geom_Line) aRightProfileLine = new Geom_Line( gp_Ax1( aRightProfileP1, aRightProfileVec ) );
340 // The point projections on the left and right profiles
341 GeomAPI_ProjectPointOnCurve aLeftProfileProject( aPointToTest, aLeftProfileLine );
342 GeomAPI_ProjectPointOnCurve aRightProfileProject( aPointToTest, aRightProfileLine );
343 // The point distance to the left and right profiles
344 Standard_Real aLeftProfileDist = aLeftProfileProject.LowerDistance();
345 Standard_Real aRightProfileDist = aRightProfileProject.LowerDistance();
347 Standard_Real aFirstCoeff = aLeftProfileDist / ( aLeftProfileDist + aRightProfileDist );
348 Standard_Real aSecCoeff = aRightProfileDist / ( aLeftProfileDist + aRightProfileDist );
350 aResAltitude = aLeftAlt * ( 1 - aFirstCoeff ) + aRightAlt * ( 1 - aSecCoeff );