Salome HOME
50443a315d707f139085652189815ade803b395f
[modules/hydro.git] / src / HYDROData / HYDROData_StreamAltitude.cxx
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.
6 //
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.
11 //
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
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_StreamAltitude.h"
20
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>
27
28 #include <BRepBuilderAPI_MakeEdge.hxx>
29 #include <BRepBuilderAPI_MakeFace.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31
32 #include <Extrema_ExtElC.hxx>
33
34 #include <GeomAPI_ProjectPointOnCurve.hxx>
35
36 #include <gp_Lin.hxx>
37
38 #include <Precision.hxx>
39
40 #include <TopExp_Explorer.hxx>
41
42 #include <TopoDS.hxx>
43 #include <TopoDS_Wire.hxx>
44 #include <TopoDS_Edge.hxx>
45
46 #include <TopTools_SequenceOfShape.hxx>
47
48 #include <Geom_Line.hxx>
49
50 #include <QStringList>
51
52 #ifdef DEB_CLASS2D
53 #include <BRepTools.hxx>
54 #include <BRep_Builder.hxx>
55 #include <BRepBuilderAPI_MakeVertex.hxx>
56 #endif
57 IMPLEMENT_STANDARD_HANDLE(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
58 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_StreamAltitude, HYDROData_IAltitudeObject)
59
60 HYDROData_StreamAltitude::HYDROData_StreamAltitude()
61 : HYDROData_IAltitudeObject()
62 {
63 }
64
65 HYDROData_StreamAltitude::~HYDROData_StreamAltitude()
66 {
67 }
68
69 Standard_Real getAltitudeFromProfile( const Handle(HYDROData_Profile)& theProfile,
70                                       const Standard_Real&             theLeftDist,
71                                       const Standard_Real&             theRightDist )
72 {
73   Standard_Real aResAlt = 0.0;
74
75   gp_XY aFirstPoint, aLastPoint;
76   if ( !theProfile->GetLeftPoint( aFirstPoint, false ) ||
77        !theProfile->GetRightPoint( aLastPoint, false ) )
78     return aResAlt;
79
80   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
81   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
82
83   Standard_Real aProfileDist = aPnt1.Distance( aPnt2 );
84
85   Standard_Real aCoeff = aProfileDist / ( theLeftDist + theRightDist );
86
87   gp_Pnt anIntPoint( aPnt1.XYZ() + ( aCoeff * theLeftDist ) * gp_Dir( gp_Vec( aPnt1, aPnt2 ) ).XYZ() );
88
89   gp_Lin aPointLine( anIntPoint, gp::DZ() );
90
91   gp_Pnt aPrevPoint;
92   gp_Lin aPrevNormal;
93   HYDROData_Profile::ProfilePoints aProfilePoints = theProfile->GetProfilePoints( false );
94   for ( int i = 1, n = aProfilePoints.Length(); i <= n; ++i )
95   {
96     gp_Pnt aProfPoint( aProfilePoints.Value( i ) );
97
98     Standard_Real aDist = aPointLine.Distance( aProfPoint );
99     if ( aDist <= gp::Resolution() )
100     {
101       // We found the intersected point
102       aResAlt = aProfPoint.Z();
103       break;
104     }
105    
106     gp_Lin aNormal = aPointLine.Normal( aProfPoint );
107     if ( i == 1 )
108     {
109       aPrevNormal = aNormal;
110       aPrevPoint = aProfPoint;
111       continue;
112     }
113
114     if ( aPrevNormal.Direction().Dot( aNormal.Direction() ) < 0 )
115     {
116       // We found the intersected edge
117       gp_Lin anEdgeLine( aPrevPoint, gp_Dir( gp_Vec( aPrevPoint, aProfPoint ) ) );
118      
119       Extrema_ExtElC anExtrema( aPointLine, anEdgeLine, Precision::Angular() );
120       if ( !anExtrema.IsParallel() )
121       {
122         Extrema_POnCurv aFirstPnt, aSecPnt;
123         anExtrema.Points( 1, aFirstPnt, aSecPnt );
124
125         const gp_Pnt& anIntPnt = aSecPnt.Value();
126         aResAlt = anIntPnt.Z();
127
128         break;
129       }
130     }
131
132     aPrevNormal = aNormal;
133     aPrevPoint = aProfPoint;
134   }
135
136   return aResAlt;
137 }
138
139 bool HYDROData_StreamAltitude::getBoundaryProfilesForPoint(
140   const gp_XY&               thePoint,
141   Handle(HYDROData_Profile)& theLeftProfile,
142   Handle(HYDROData_Profile)& theRightProfile ) const
143 {
144   Handle(HYDROData_Stream) aStream =
145     Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
146   if ( aStream.IsNull() )
147     return false;
148
149   HYDROData_SequenceOfObjects aStreamProfiles = aStream->GetProfiles();
150   if ( aStreamProfiles.Length() < 2 )
151     return false;
152
153   Handle(HYDROData_Profile) aPrevProfile;
154   gp_Pnt aPrevPnt1, aPrevPnt2;
155   for ( int i = 1, n = aStreamProfiles.Length(); i <= n; ++i )
156   {
157     Handle(HYDROData_Profile) aProfile =
158       Handle(HYDROData_Profile)::DownCast( aStreamProfiles.Value( i ) );
159     if ( aProfile.IsNull() )
160       continue;
161
162     gp_XY aFirstPoint, aLastPoint;
163     if ( !aProfile->GetLeftPoint( aFirstPoint, false ) ||
164          !aProfile->GetRightPoint( aLastPoint, false ) )
165       continue;
166
167     gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
168     gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
169
170     if ( !aPrevProfile.IsNull() )
171     {
172       BRepBuilderAPI_MakeEdge aLeftMakeEdge( aPrevPnt1, aPrevPnt2 );
173       BRepBuilderAPI_MakeEdge aBotMakeEdge( aPrevPnt2, aPnt2 );
174       BRepBuilderAPI_MakeEdge aRightMakeEdge( aPnt2, aPnt1 );
175       BRepBuilderAPI_MakeEdge aTopMakeEdge( aPnt1, aPrevPnt1 );
176
177       BRepBuilderAPI_MakeWire aMakeWire( aLeftMakeEdge.Edge(), aBotMakeEdge.Edge(), 
178                                          aRightMakeEdge.Edge(), aTopMakeEdge.Edge() );
179
180       BRepBuilderAPI_MakeFace aMakeFace( aMakeWire.Wire() );
181         
182       TopoDS_Face aProfilesFace = aMakeFace.Face();
183 #ifdef DEB_CLASS2D
184           TopoDS_Compound aCmp;
185       BRep_Builder aBB;
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");
192 #endif     
193
194           TopAbs_State aPointState =  HYDROData_Tool::ComputePointState(thePoint, aProfilesFace);
195
196 #ifdef DEB_CLASS2D
197           cout << "Point status is = " << aPointState <<endl;
198 #endif
199       if ( aPointState != TopAbs_OUT )
200       {
201         theLeftProfile = aPrevProfile;
202         theRightProfile = aProfile;
203         break;
204       }
205     }
206
207     aPrevProfile = aProfile;
208     aPrevPnt1 = aPnt1;
209     aPrevPnt2 = aPnt2;
210   }
211
212   return !theLeftProfile.IsNull() && !theRightProfile.IsNull();
213 }
214
215 double HYDROData_StreamAltitude::GetAltitudeForPoint( const gp_XY& thePoint ) const
216 {
217   double aResAltitude = GetInvalidAltitude();
218
219   Handle(HYDROData_Stream) aStream =
220     Handle(HYDROData_Stream)::DownCast( GetFatherObject() );
221   if ( aStream.IsNull() )
222     return aResAltitude;
223
224   TopoDS_Shape aStreamShape = aStream->GetTopShape();
225   if ( aStreamShape.IsNull() )
226     return aResAltitude;
227
228   TopExp_Explorer aStreamFaceExp( aStreamShape, TopAbs_FACE );
229   if ( !aStreamFaceExp.More() )
230     return aResAltitude;
231
232   // Get only face because of 2d profile wires is in compound
233   TopoDS_Face aStreamFace = TopoDS::Face( aStreamFaceExp.Current() );
234
235   // Check if point is inside of stream presentation
236   TopAbs_State aPointState = HYDROData_Tool::ComputePointState(thePoint, aStreamFace);
237
238 #ifdef DEB_CLASS2D
239       cout << "Point status is = " << aPointState <<endl;
240           TopoDS_Compound aCmp;
241       BRep_Builder aBB;
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");
248 #endif
249   if ( aPointState == TopAbs_OUT )
250     return aResAltitude;
251
252   // Find the two profiles between which the point is lies
253   Handle(HYDROData_Profile) aLeftProfile, aRightProfile;
254   if ( !getBoundaryProfilesForPoint( thePoint, aLeftProfile, aRightProfile ) )
255     return aResAltitude;
256
257   // Find the projections of point to borders of stream
258   gp_XYZ aPointToTest( thePoint.X(), thePoint.Y(), 0.0 );
259
260   TopoDS_Edge aStreamLeftEdge = TopoDS::Edge( aStream->GetLeftShape() );
261   TopoDS_Edge aStreamRightEdge = TopoDS::Edge( aStream->GetRightShape() );
262
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 );
266
267   GeomAPI_ProjectPointOnCurve aLeftProject( aPointToTest, anEdgeLeftCurve );
268   GeomAPI_ProjectPointOnCurve aRightProject( aPointToTest, anEdgeRightCurve );
269
270   Standard_Real aLeftDist = aLeftProject.LowerDistance();
271   Standard_Real aRightDist = aRightProject.LowerDistance();
272
273   // Find the altitude in profiles
274   Standard_Real aLeftAlt = getAltitudeFromProfile( aLeftProfile, aLeftDist, aRightDist );
275   Standard_Real aRightAlt = getAltitudeFromProfile( aRightProfile, aLeftDist, aRightDist );
276
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();
300   // The coefficients
301   Standard_Real aFirstCoeff = aLeftProfileDist / ( aLeftProfileDist + aRightProfileDist );
302   Standard_Real aSecCoeff = aRightProfileDist / ( aLeftProfileDist + aRightProfileDist );
303
304   aResAltitude = aLeftAlt * ( 1 - aFirstCoeff ) + aRightAlt * ( 1 - aSecCoeff );
305
306   return aResAltitude;
307 }
308
309
310
311