Salome HOME
a80edf2202d3ddae55e07a21006e11a61d27cc06
[modules/hydro.git] / src / HYDROData / HYDROData_DTM.cxx
1
2 #include <HYDROData_DTM.h>
3 #include <HYDROData_Profile.h>
4
5 #include <Geom2d_BSplineCurve.hxx>
6 #include <Geom2dAPI_Interpolate.hxx>
7 #include <TColgp_HArray1OfPnt2d.hxx>
8 #include <TColgp_Array1OfVec2d.hxx>
9 #include <TColStd_HArray1OfBoolean.hxx>
10 #include <TopoDS.hxx>
11 #include <TopoDS_Edge.hxx>
12 #include <TopoDS_Wire.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <BRep_Tool.hxx>
15 #include <gp_Ax3.hxx>
16 #include <Geom_Line.hxx>
17 #include <Geom2d_Line.hxx>
18 #include <Geom2d_TrimmedCurve.hxx>
19 #include <Geom_BSplineCurve.hxx>
20 #include <Geom2d_BSplineCurve.hxx>
21 #include <TColStd_Array1OfReal.hxx>
22 #include <TColStd_Array1OfInteger.hxx>
23 #include <TColgp_Array1OfPnt.hxx>
24
25 IMPLEMENT_STANDARD_HANDLE( HYDROData_DTM, HYDROData_Bathymetry )
26 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
27
28 HYDROData_DTM::HYDROData_DTM()
29 {
30 }
31
32 HYDROData_DTM::~HYDROData_DTM()
33 {
34 }
35
36
37 void GetProperties( const Handle_HYDROData_Profile& theProfile,
38                     gp_Pnt& theLowestPoint,
39                     gp_Vec2d& theDir,
40                     bool isNormalDir )
41 {
42   theLowestPoint = theProfile->GetBottomPoint();
43   
44   gp_XY aLeft, aRight;
45   theProfile->GetLeftPoint( aLeft, true, true );
46   theProfile->GetRightPoint( aRight, true, true );
47   double x = aRight.X()-aLeft.X();
48   double y = aRight.Y()-aLeft.Y();
49   if( isNormalDir )
50     theDir = gp_Vec2d( -y, x );
51   else
52     theDir = gp_Vec2d( x, y );
53 }
54
55 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr )
56 {
57   gp_Pnt p = thePnt.Transformed( theTr );
58   return gp_Pnt2d( p.X(), p.Z() );
59 }
60
61 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints, const gp_Trsf& theTr )
62 {
63   int low = thePoints.Lower(), up = thePoints.Upper();
64   Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
65   for( int i=low; i<=up; i++ )
66     points->SetValue( i, To2D( thePoints.Value( i ), theTr ) );
67   return points;
68 }
69
70 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve, 
71                                 Standard_Real theFirst, Standard_Real theLast,
72                                 const gp_Trsf& theTr )
73 {
74   if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
75   {
76     gp_Pnt aFirstPnt, aLastPnt;
77     theCurve->D0( theFirst, aFirstPnt );
78     theCurve->D0( theLast, aLastPnt );
79
80     gp_Pnt2d
81       aFirst2d = To2D( aFirstPnt, theTr ),
82       aLast2d = To2D( aLastPnt, theTr );
83
84     gp_Vec2d dir( aFirst2d, aLast2d );
85     Handle_Geom2d_Line aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
86     return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
87   }
88
89   if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
90   {
91     Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
92
93     Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr );
94     const TColStd_Array1OfReal& knots = aSpline->Knots();
95     const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
96     int aDegree = aSpline->Degree();
97
98     return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
99   }
100
101   return Handle(Geom2d_Curve)();
102 }
103
104 Handle_Geom2d_BSplineCurve HYDROData_DTM::CreateHydraulicAxis( 
105   const std::vector<Handle_HYDROData_Profile>& theProfiles )
106 {
107   size_t n = theProfiles.size();
108   Handle_Geom2d_BSplineCurve aResult;
109
110   Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
111   TColgp_Array1OfVec2d tangents( 1, (int)n );
112   Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
113
114   for( size_t i = 1; i <= n; i++ )
115   {
116     Handle_HYDROData_Profile aProfile = theProfiles[i-1];
117     gp_Pnt aLowest;
118     gp_Vec2d aTangent;
119     GetProperties( aProfile, aLowest, aTangent, true );
120     aTangent.Normalize();
121
122     points->SetValue( (int)i, gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
123     tangents.SetValue( (int)i, aTangent );
124     flags->SetValue( (int)i, Standard_True );
125   }
126
127   Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
128   anInterpolator.Load( tangents, flags );
129   anInterpolator.Perform();
130   if( anInterpolator.IsDone() )
131     aResult = anInterpolator.Curve();
132   return aResult;
133 }
134
135 std::vector<Handle_Geom2d_Curve> HYDROData_DTM::ProfileToParametric( const Handle_HYDROData_Profile& theProfile )
136 {
137   std::vector<Handle_Geom2d_Curve> curves;
138   theProfile->Update();
139   
140   // Transformation of the coordinate systems
141   gp_Pnt aLowest;
142   gp_Vec2d aDir;
143   GetProperties( theProfile, aLowest, aDir, false );
144
145   gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
146   gp_Ax3 aLocal( aLowest, gp_Dir( 0, 0, 1 ), gp_Dir( aDir.X(), aDir.Y(), 0 ) );
147
148   gp_Trsf aTransf;
149   aTransf.SetTransformation( aStd3d, aLocal );
150
151   // Iteration via edges
152   TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
153   TopExp_Explorer anExp( aWire, TopAbs_EDGE );
154   for( ; anExp.More(); anExp.Next() )
155   {
156     // Extract an edge from wire
157     TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
158
159     // Extract a curve corresponding to the edge
160     TopLoc_Location aLoc;
161     Standard_Real aFirst, aLast;
162     Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
163
164     // Convert the curve to 2d CS
165     Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf );
166     if( !aCurve2d.IsNull() )
167       curves.push_back( aCurve2d );
168   }
169   return curves;
170 }
171
172 void HYDROData_DTM::ProfileDiscretization( const Handle_HYDROData_Profile& theProfile, 
173                                            double theMinZ, double theMaxZ, double theDDZ,
174                                            CurveUZ& theMidPointCurve,
175                                            CurveUZ& theWidthCurve )
176 {
177   /*for( double z = theMinZ; z<=theMaxZ; z += theDDZ )
178   {
179
180   }*/
181 }
182
183 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB, 
184                                  int theNbSteps, std::vector<CurveUZ>& theInterpolation )
185 {
186 }
187
188 void HYDROData_DTM::CurveTo3d( const CurveUZ& theCurve, const CurveUZ& theCurveB, 
189                                int theNbSteps, std::vector<CurveUZ>& theInterpolation )
190 {
191 }