2 #include <HYDROData_DTM.h>
3 #include <HYDROData_Profile.h>
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>
11 #include <TopoDS_Edge.hxx>
12 #include <TopoDS_Wire.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <BRep_Tool.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 <GeomAPI_Interpolate.hxx>
22 #include <TColStd_Array1OfReal.hxx>
23 #include <TColStd_Array1OfInteger.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColgp_Array1OfVec.hxx>
26 #include <TColgp_HArray1OfPnt.hxx>
27 #include <Geom2dAPI_InterCurveCurve.hxx>
28 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
29 #include <Geom2dAdaptor_Curve.hxx>
30 #include <GCPnts_AbscissaPoint.hxx>
31 #include <BRepBuilderAPI_MakeEdge.hxx>
34 IMPLEMENT_STANDARD_HANDLE( HYDROData_DTM, HYDROData_Bathymetry )
35 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
38 HYDROData_DTM::CurveUZ::CurveUZ( double theXCurv, const gp_Vec2d& theProfileDir )
39 : myXcurv( theXCurv ), myProfileDir( theProfileDir )
43 HYDROData_DTM::CurveUZ::~CurveUZ()
47 double HYDROData_DTM::CurveUZ::Xcurv() const
52 gp_Vec2d HYDROData_DTM::CurveUZ::ProfileDir() const
57 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator + ( const CurveUZ& c ) const
59 HYDROData_DTM::CurveUZ res( Xcurv() + c.Xcurv(), ProfileDir() + c.ProfileDir() );
62 for( int i=0; i<n; i++ )
65 p.U = operator[]( i ).U + c[i].U;
66 p.Z = operator[]( i ).Z;
72 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator * ( double d ) const
74 HYDROData_DTM::CurveUZ res( Xcurv()*d, ProfileDir()*d );
77 for( int i=0; i<n; i++ )
80 p.U = operator[]( i ).U * d;
81 p.Z = operator[]( i ).Z;
87 void HYDROData_DTM::Bank::reserve( int theNbPoints )
89 myPoints.reserve( theNbPoints );
90 myDirs.reserve( theNbPoints );
93 void HYDROData_DTM::Bank::push_back( const gp_Pnt& thePnt, const gp_Dir& theTangent )
95 myPoints.push_back( thePnt );
96 myDirs.push_back( theTangent );
99 void HYDROData_DTM::Bank::clear()
105 TopoDS_Edge HYDROData_DTM::Bank::createEdge3d() const
107 size_t n = myPoints.size();
109 return TopoDS_Edge();
111 Handle_Geom_BSplineCurve aCurve;
113 Handle(TColgp_HArray1OfPnt) points = new TColgp_HArray1OfPnt( 1, (int)n );
114 TColgp_Array1OfVec tangents( 1, (int)n );
115 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
117 for( size_t i = 1; i <= n; i++ )
119 gp_Pnt aPnt = myPoints[i-1];
120 gp_Vec aVec = myDirs[i-1];
121 points->SetValue( (int)i, aPnt );
122 tangents.SetValue( (int)i, aVec );
123 flags->SetValue( (int)i, Standard_True );
126 GeomAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
127 anInterpolator.Load( tangents, flags );
128 anInterpolator.Perform();
129 if( anInterpolator.IsDone() )
131 aCurve = anInterpolator.Curve();
132 return BRepBuilderAPI_MakeEdge( aCurve ).Edge();
135 return TopoDS_Edge();
140 HYDROData_DTM::HYDROData_DTM()
144 HYDROData_DTM::~HYDROData_DTM()
148 HYDROData_SequenceOfObjects HYDROData_DTM::GetProfiles() const
150 return GetReferenceObjects( DataTag_Profiles );
153 void HYDROData_DTM::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles )
155 SetReferenceObjects( theProfiles, DataTag_Profiles );
158 double HYDROData_DTM::GetDDZ() const
160 return GetDouble( DataTag_DDZ );
163 void HYDROData_DTM::SetDDZ( double theDDZ )
165 SetDouble( DataTag_DDZ, theDDZ );
168 double HYDROData_DTM::GetSpatialStep() const
170 return GetDouble( DataTag_SpatialStep );
173 void HYDROData_DTM::SetSpatialStep( double theSpatialStep )
175 SetDouble( DataTag_SpatialStep, theSpatialStep );
178 void HYDROData_DTM::Update()
180 HYDROData_SequenceOfObjects objs = GetProfiles();
181 int aLower = objs.Lower(), anUpper = objs.Upper();
182 size_t n = anUpper-aLower+1;
184 std::vector<Handle_HYDROData_Profile> profiles;
185 profiles.reserve( n );
186 for( int i=aLower; i<=anUpper; i++ )
188 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( objs.Value( i ) );
189 if( !aProfile.IsNull() )
190 profiles.push_back( aProfile );
193 double ddz = GetDDZ();
194 double step = GetSpatialStep();
195 const double EPS = 1E-3;
196 AltitudePoints points;
200 if( ddz>EPS && step>EPS )
201 points = Interpolate( profiles, ddz, step, &myLeft, &myRight );
202 SetAltitudePoints( points );
214 void HYDROData_DTM::GetProperties( const Handle_HYDROData_Profile& theProfile,
215 gp_Pnt& theLowestPoint, gp_Vec2d& theDir,
217 double& theZMin, double& theZMax )
219 theLowestPoint = theProfile->GetBottomPoint();
222 theProfile->GetLeftPoint( aLeft, true, true );
223 theProfile->GetRightPoint( aRight, true, true );
224 double x = aRight.X()-aLeft.X();
225 double y = aRight.Y()-aLeft.Y();
227 theDir = gp_Vec2d( -y, x );
229 theDir = gp_Vec2d( x, y );
231 HYDROData_Profile::ProfilePoints points = theProfile->GetProfilePoints();
232 int lo = points.Lower();
233 int up = points.Upper();
234 theZMin = std::numeric_limits<double>::max();
236 for( int i=lo; i<=up; i++ )
238 double z = points.Value( i ).Z();
246 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr,
247 double& theUMin, double& theUMax )
249 gp_Pnt p = thePnt.Transformed( theTr );
256 return gp_Pnt2d( u, z );
259 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints,
260 const gp_Trsf& theTr,
261 double& theUMin, double& theUMax )
263 int low = thePoints.Lower(), up = thePoints.Upper();
264 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
265 for( int i=low; i<=up; i++ )
266 points->SetValue( i, To2D( thePoints.Value( i ), theTr, theUMin, theUMax ) );
270 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve,
271 Standard_Real theFirst, Standard_Real theLast,
272 const gp_Trsf& theTr,
273 double& theUMin, double& theUMax )
275 if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
277 gp_Pnt aFirstPnt, aLastPnt;
278 theCurve->D0( theFirst, aFirstPnt );
279 theCurve->D0( theLast, aLastPnt );
282 aFirst2d = To2D( aFirstPnt, theTr, theUMin, theUMax ),
283 aLast2d = To2D( aLastPnt, theTr, theUMin, theUMax );
285 gp_Vec2d dir( aFirst2d, aLast2d );
286 Handle_Geom2d_Line aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
287 return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
290 if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
292 Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
294 Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr, theUMin, theUMax );
295 const TColStd_Array1OfReal& knots = aSpline->Knots();
296 const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
297 int aDegree = aSpline->Degree();
299 return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
302 return Handle(Geom2d_Curve)();
305 Handle_Geom2d_BSplineCurve HYDROData_DTM::CreateHydraulicAxis(
306 const std::vector<Handle_HYDROData_Profile>& theProfiles,
307 std::vector<double>& theDistances )
309 size_t n = theProfiles.size();
310 Handle_Geom2d_BSplineCurve aResult;
312 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
313 TColgp_Array1OfVec2d tangents( 1, (int)n );
314 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
316 for( size_t i = 1; i <= n; i++ )
318 Handle_HYDROData_Profile aProfile = theProfiles[i-1];
324 GetProperties( aProfile, aLowest, aTangent, true, zmin, zmax );
325 aTangent.Normalize();
327 points->SetValue( (int)i, gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
328 tangents.SetValue( (int)i, aTangent );
329 flags->SetValue( (int)i, Standard_True );
332 Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
333 anInterpolator.Load( tangents, flags );
334 anInterpolator.Perform();
335 if( anInterpolator.IsDone() )
337 aResult = anInterpolator.Curve();
339 //fill the distances vector
340 Geom2dAdaptor_Curve anAdaptor( aResult );
342 theDistances.clear();
343 theDistances.reserve( n );
344 Standard_Real aParamFirst = anAdaptor.FirstParameter(), aParamLast = anAdaptor.LastParameter();
345 for( size_t i = 1; i <= n; i++ )
347 gp_Pnt2d aPnt = points->Value( (Standard_Integer)i );
348 Geom2dAPI_ProjectPointOnCurve aProject( aPnt, aResult );
349 Standard_Real aParam = aProject.LowerDistanceParameter();
350 double aDistance = GCPnts_AbscissaPoint::Length( anAdaptor, aParamFirst, aParam );
351 theDistances.push_back( aDistance );
357 std::vector<Handle_Geom2d_Curve> HYDROData_DTM::ProfileToParametric(
358 const Handle_HYDROData_Profile& theProfile,
359 double& theUMin, double& theUMax, gp_Vec2d& theDir )
361 std::vector<Handle_Geom2d_Curve> curves;
363 // Transformation of the coordinate systems
366 GetProperties( theProfile, aLowest, theDir, false, zmin, zmax );
368 gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
369 gp_Ax3 aLocal( aLowest, gp_Dir( 0, 0, 1 ), gp_Dir( theDir.X(), theDir.Y(), 0 ) );
372 aTransf.SetTransformation( aStd3d, aLocal );
374 // Iteration via edges
375 TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
376 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
377 for( ; anExp.More(); anExp.Next() )
379 // Extract an edge from wire
380 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
382 // Extract a curve corresponding to the edge
383 TopLoc_Location aLoc;
384 Standard_Real aFirst, aLast;
385 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
387 // Convert the curve to 2d CS
388 Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf, theUMin, theUMax );
389 if( !aCurve2d.IsNull() )
390 curves.push_back( aCurve2d );
396 bool CalcMidWidth( const std::vector<gp_Pnt2d>& intersections, double& theMid, double& theWid )
398 double umin = std::numeric_limits<double>::max(),
401 size_t n = intersections.size();
405 for( size_t i = 0; i < n; i++ )
407 double u = intersections[i].X();
413 theMid = ( umin+umax )/2;
418 void HYDROData_DTM::ProfileDiscretization( const Handle_HYDROData_Profile& theProfile,
419 double theXCurv, double theMinZ, double theMaxZ, double theDDZ,
420 CurveUZ& theMidPointCurve,
421 CurveUZ& theWidthCurve,
422 double theTolerance )
424 double aDblMax = std::numeric_limits<double>::max(),
429 gp_Vec2d aProfileDir;
430 std::vector<Handle_Geom2d_Curve> curves = ProfileToParametric( theProfile, aUMin, aUMax, aProfileDir );
431 size_t n = curves.size();
436 // we add the "virtual" vertical lines to simulate the intersection with profile
437 gp_Pnt2d aFirst, aLast;
438 curves[0]->D0( curves[0]->FirstParameter(), aFirst );
439 curves[n-1]->D0( curves[n-1]->LastParameter(), aLast );
440 Handle(Geom2d_Line) aV1 = new Geom2d_Line( aFirst, gp_Dir2d( 0, 1 ) );
441 Handle(Geom2d_TrimmedCurve) aT1 = new Geom2d_TrimmedCurve( aV1, 0.0, aVMax );
443 Handle(Geom2d_Line) aV2 = new Geom2d_Line( aLast, gp_Dir2d( 0, 1 ) );
444 Handle(Geom2d_TrimmedCurve) aT2 = new Geom2d_TrimmedCurve( aV2, 0.0, aVMax );
446 curves.push_back( aT1 );
447 curves.push_back( aT2 );
449 int psize = ( int )( ( theMaxZ-theMinZ ) / theDDZ + 1 );
450 theMidPointCurve = CurveUZ( theXCurv, aProfileDir );
451 theMidPointCurve.reserve( psize );
452 theWidthCurve = CurveUZ( theXCurv, aProfileDir );
453 theWidthCurve.reserve( psize );
456 // for each discrete value of z we search intersection with profile
457 for( double z = theMinZ; z <= theMaxZ; z += theDDZ )
459 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, z ), gp_Dir2d( 1, 0 ) );
460 std::vector<gp_Pnt2d> intersections;
461 for( size_t i = 0; i < n; i++ )
463 Handle_Geom2d_Curve aCurve = curves[i];
464 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
465 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
466 intersections.push_back( anIntersect.Point( k ) );
469 if( intersections.size() >= 2 )
472 if( !CalcMidWidth( intersections, u_mid, u_wid ) )
478 theMidPointCurve.push_back( p_mid );
483 theWidthCurve.push_back( p_wid );
488 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB,
489 int theNbSteps, std::vector<CurveUZ>& theInterpolation,
492 theInterpolation.clear();
493 int d = isAddSecond ? 2 : 1;
494 theInterpolation.reserve( theNbSteps+d );
495 double dt = 1.0 / double( theNbSteps + 1 );
497 theInterpolation.push_back( theCurveA );
498 for( int i=0; i<theNbSteps; i++, t+=dt )
500 CurveUZ anInterp = theCurveA*(1-t) + theCurveB*t;
501 theInterpolation.push_back( anInterp );
504 theInterpolation.push_back( theCurveB );
506 #include <BRepLib_MakeEdge2d.hxx>
507 void HYDROData_DTM::CurveTo3D( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
508 const CurveUZ& theMidCurve, const CurveUZ& theWidthCurve,
509 AltitudePoints& thePoints,
510 Bank* theLeft, Bank* theRight, double dz )
512 Geom2dAdaptor_Curve anAdaptor( theHydraulicAxis );
513 TopoDS_Edge E2d = BRepLib_MakeEdge2d(theHydraulicAxis).Edge();
514 GCPnts_AbscissaPoint ap( anAdaptor, theMidCurve.Xcurv(), anAdaptor.FirstParameter() );
515 double aParam = ap.Parameter();
518 anAdaptor.D0( aParam, point );
519 gp_Vec2d profile_dir = theMidCurve.ProfileDir();
520 gp_Dir tangent_n( -profile_dir.Y(), profile_dir.X(), dz );
521 profile_dir.Normalize();
523 size_t n = theMidCurve.size();
524 double min_param = 1E+15;
525 double max_param = -1E+15;
527 for( size_t i=0; i<n; i++ )
529 double param1 = theMidCurve[i].U - theWidthCurve[i].U / 2;
530 double param2 = theMidCurve[i].U + theWidthCurve[i].U / 2;
532 gp_Pnt2d p1 = point.Translated( param1 * profile_dir);
533 gp_Pnt2d p2 = point.Translated( param2 * profile_dir);
535 double z = theMidCurve[i].Z;
537 if( param1 < min_param )
542 if( param2 < min_param )
547 if( param1 > max_param )
552 if( param2 > max_param )
558 AltitudePoint p3d_1( p1.X(), p1.Y(), z ), p3d_2( p2.X(), p2.Y(), z );
559 thePoints.push_back( p3d_1 );
560 thePoints.push_back( p3d_2 );
565 gp_Pnt2d left2d = point.Translated( min_param * profile_dir );
566 gp_Pnt left( left2d.X(), left2d.Y(), z1 );
567 theLeft->push_back( left, tangent_n );
571 gp_Pnt2d right2d = point.Translated( max_param * profile_dir );
572 gp_Pnt right( right2d.X(), right2d.Y(), z2 );
573 theRight->push_back( right, tangent_n );
577 inline double max( double a, double b )
585 #include <BRepLib_MakeWire.hxx>
587 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
588 ( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
589 const Handle_HYDROData_Profile& theProfileA,
591 const Handle_HYDROData_Profile& theProfileB,
593 double theDDZ, int theNbSteps, bool isAddSecond,
594 Bank* theLeft, Bank* theRight )
596 double zminA, zmaxA, zminB, zmaxB;
597 gp_Pnt lowestA, lowestB;
600 GetProperties( theProfileA, lowestA, dirA, false, zminA, zmaxA );
601 GetProperties( theProfileB, lowestB, dirB, false, zminB, zmaxB );
603 double dz = zminB - zminA;
605 double zmin = max( zminA, zminB );
606 double zmax = max( zmaxA, zmaxB );
608 CurveUZ midA(0, gp_Vec2d()), midB(0, gp_Vec2d());
609 CurveUZ widA(0, gp_Vec2d()), widB(0, gp_Vec2d());
611 ProfileDiscretization( theProfileA, theXCurvA, zmin, zmax, theDDZ, midA, widA );
612 ProfileDiscretization( theProfileB, theXCurvB, zmin, zmax, theDDZ, midB, widB );
614 std::vector<CurveUZ> mid, wid;
615 Interpolate( midA, midB, theNbSteps, mid, isAddSecond );
616 Interpolate( widA, widB, theNbSteps, wid, isAddSecond );
618 size_t p = mid.size();
619 size_t q = p>0 ? 2*mid[0].size() : 1;
620 AltitudePoints points;
621 points.reserve( p*q );
622 for( size_t i=0; i<p; i++ )
623 CurveTo3D( theHydraulicAxis, mid[i], wid[i], points, theLeft, theRight, dz );
626 for (int i =0; i < theLeft->myPoints.size() - 1; i++)
627 WM.Add(BRepLib_MakeEdge(theLeft->myPoints[i], theLeft->myPoints[i+1]).Edge());
628 TopoDS_Wire W = WM.Wire();
632 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
633 ( const std::vector<Handle_HYDROData_Profile>& theProfiles,
634 double theDDZ, double theSpatialStep,
638 AltitudePoints points;
639 size_t n = theProfiles.size();
643 std::vector<double> distances;
644 Handle_Geom2d_BSplineCurve aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
645 if( aHydraulicAxis.IsNull() )
648 int aNbStepsComplete = 0;
649 for( size_t i=0, n1=n-1; i<n1; i++ )
651 double aDistance = distances[i+1]-distances[i];
652 aNbStepsComplete += ( int(aDistance/theSpatialStep) /*+ 1*/ );
655 theLeft->reserve( aNbStepsComplete );
657 theRight->reserve( aNbStepsComplete );
659 for( size_t i=0, n1=n-1; i<n1; i++ )
661 double aDistance = distances[i+1]-distances[i];
662 int aNbSteps = int(aDistance/theSpatialStep) /*+ 1*/;
663 bool isAddSecond = i==n1-1;
665 AltitudePoints local_points = Interpolate( aHydraulicAxis, theProfiles[i], distances[i],
666 theProfiles[i+1], distances[i+1], theDDZ, aNbSteps, isAddSecond, theLeft, theRight );
669 points.reserve( local_points.size() * ( n-1 ) );
671 for( size_t j=0, m=local_points.size(); j<m; j++ )
672 points.push_back( local_points[j] );
677 void HYDROData_DTM::CreateBankShapes( TopoDS_Edge& theLeft, TopoDS_Edge& theRight ) const
679 theLeft = myLeft.createEdge3d();
680 theRight = myRight.createEdge3d();