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 #include <BRepLib_MakeEdge.hxx>
35 #include <BRepLib_MakeWire.hxx>
36 #include <BRep_Builder.hxx>
37 #include <GeomProjLib.hxx>
38 #include <Geom_TrimmedCurve.hxx>
39 #include <Geom_Plane.hxx>
40 #include <BRepTools_WireExplorer.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
44 #include <TopTools_IndexedMapOfOrientedShape.hxx>
46 #include <BRepLib_MakeEdge.hxx>
47 #include <BRepLib_MakeWire.hxx>
48 #include <BRep_Builder.hxx>
49 #include <ShapeAnalysis_Wire.hxx>
50 #include <BRepAlgo_NormalProjection.hxx>
54 IMPLEMENT_STANDARD_HANDLE( HYDROData_DTM, HYDROData_Bathymetry )
55 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
57 HYDROData_DTM::CurveUZ::CurveUZ( double theXCurv, const gp_Vec2d& theProfileDir, double theDeltaZ )
58 : myXcurv( theXCurv ), myProfileDir( theProfileDir ), myDeltaZ( theDeltaZ )
62 HYDROData_DTM::CurveUZ::~CurveUZ()
66 double HYDROData_DTM::CurveUZ::Xcurv() const
71 gp_Vec2d HYDROData_DTM::CurveUZ::ProfileDir() const
76 double HYDROData_DTM::CurveUZ::DeltaZ() const
81 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator + ( const CurveUZ& c ) const
83 HYDROData_DTM::CurveUZ res( Xcurv() + c.Xcurv(), ProfileDir() + c.ProfileDir(), DeltaZ() + c.DeltaZ() );
84 size_t n = size(), n1 = c.size();
87 std::cout << "Warning: different number of points in curves: " << n << ", " << n1 << std::endl;
90 for( int i=0; i<n; i++ )
93 p.U = operator[]( i ).U + c[i].U;
94 p.Z = operator[]( i ).Z;
100 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator * ( double d ) const
102 HYDROData_DTM::CurveUZ res( Xcurv()*d, ProfileDir()*d, DeltaZ()*d );
105 for( int i=0; i<n; i++ )
108 p.U = operator[]( i ).U * d;
109 p.Z = operator[]( i ).Z;
118 HYDROData_DTM::HYDROData_DTM()
122 HYDROData_DTM::~HYDROData_DTM()
126 HYDROData_SequenceOfObjects HYDROData_DTM::GetProfiles() const
128 return GetReferenceObjects( DataTag_Profiles );
131 void HYDROData_DTM::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles )
133 SetReferenceObjects( theProfiles, DataTag_Profiles );
137 double HYDROData_DTM::GetDDZ() const
139 return GetDouble( DataTag_DDZ );
142 void HYDROData_DTM::SetDDZ( double theDDZ )
144 SetDouble( DataTag_DDZ, theDDZ );
148 double HYDROData_DTM::GetSpatialStep() const
150 return GetDouble( DataTag_SpatialStep );
153 void HYDROData_DTM::SetSpatialStep( double theSpatialStep )
155 SetDouble( DataTag_SpatialStep, theSpatialStep );
159 void HYDROData_DTM::PointToWire(const AltitudePoints& pnts, TopoDS_Wire& W )
164 for (int i = 0; i < pnts.size() - 1; i++)
166 gp_Pnt p1(pnts[i].X, pnts[i].Y, pnts[i].Z);
167 gp_Pnt p2(pnts[i+1].X, pnts[i+1].Y, pnts[i+1].Z);
168 WM.Add(BRepLib_MakeEdge(p1, p2).Edge());
174 TopTools_IndexedMapOfOrientedShape HYDROData_DTM::Create3DShape(const AltitudePoints& left,
175 const AltitudePoints& right,
176 const std::vector<AltitudePoints>& main_profiles)
178 TopTools_IndexedMapOfOrientedShape ll;
179 TopoDS_Wire LWire, RWire;
180 PointToWire(left, LWire);
181 PointToWire(right, RWire);
183 ll.Add(LWire.Oriented(TopAbs_FORWARD));
185 for (int k = 0; k < main_profiles.size(); k++)
188 PointToWire(main_profiles[k], W);
189 TopAbs_Orientation Ori = TopAbs_INTERNAL;
190 if (k == 0 || k == main_profiles.size() - 1)
191 Ori = TopAbs_FORWARD;
192 ll.Add(W.Oriented(Ori));
196 ll.Add(RWire.Oriented(TopAbs_FORWARD));
197 //yes, add subshapes in this order (left + profiles + right)
198 //otherwise the projected wire will be non-manifold
204 void HYDROData_DTM::Update()
206 AltitudePoints points;
207 TopoDS_Shape Out3dPres;
208 TopoDS_Shape Out2dPres;
209 TopoDS_Shape OutLeftB;
210 TopoDS_Shape OutRightB;
211 TopoDS_Shape OutInlet;
212 TopoDS_Shape OutOutlet;
214 HYDROData_SequenceOfObjects objs = GetProfiles();
215 double ddz = GetDDZ();
216 double step = GetSpatialStep();
217 std::set<int> InvInd;
218 bool WireIntersections; //__TODO
219 CreateProfilesFromDTM( objs, ddz, step, points, Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, true, true, InvInd, -1, WireIntersections );
220 SetAltitudePoints( points );
222 SetShape( DataTag_LeftBankShape, OutLeftB);
223 SetShape( DataTag_RightBankShape, OutRightB);
224 SetShape( DataTag_InletShape, OutInlet);
225 SetShape( DataTag_OutletShape, OutOutlet );
226 SetShape( DataTag_3DShape, Out3dPres );
227 SetShape( DataTag_2DShape, Out2dPres );
229 HYDROData_Bathymetry::Update();
232 void HYDROData_DTM::GetPresentationShapes( TopoDS_Shape& Out3dPres,
233 TopoDS_Shape& Out2dPres,
234 TopoDS_Shape& OutLeftB,
235 TopoDS_Shape& OutRightB,
236 TopoDS_Shape& OutInlet,
237 TopoDS_Shape& OutOutlet )
240 OutLeftB = GetShape( DataTag_LeftBankShape);
241 OutRightB = GetShape( DataTag_RightBankShape);
242 OutInlet = GetShape( DataTag_InletShape);
243 OutOutlet = GetShape( DataTag_OutletShape );
244 Out3dPres = GetShape( DataTag_3DShape );
245 Out2dPres = GetShape( DataTag_2DShape );
247 void HYDROData_DTM::CreateProfilesFromDTM (const HYDROData_SequenceOfObjects& InpProfiles,
250 AltitudePoints& points,
251 TopoDS_Shape& Out3dPres,
252 TopoDS_Shape& Out2dPres,
253 TopoDS_Shape& OutLeftB,
254 TopoDS_Shape& OutRightB,
255 TopoDS_Shape& OutInlet,
256 TopoDS_Shape& OutOutlet,
259 std::set<int>& InvInd,
261 bool& WireIntersections)
263 int aLower = InpProfiles.Lower(), anUpper = InpProfiles.Upper();
264 size_t n = anUpper - aLower + 1;
266 std::vector<Handle_HYDROData_Profile> profiles;
267 profiles.reserve( n );
268 for( int i=aLower; i<=anUpper; i++ )
270 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( InpProfiles.Value( i ) );
271 if( !aProfile.IsNull() )
272 profiles.push_back( aProfile );
274 const double EPS = 1E-3;
276 AltitudePoints right;
277 std::vector<AltitudePoints> main_profiles;
279 if( thePntsLimit > 0 )
281 int aNbPoints = EstimateNbPoints( profiles, ddz, step );
282 if( aNbPoints < 0 || aNbPoints > thePntsLimit )
286 if( ddz>EPS && step>EPS )
287 CreateProfiles(profiles, ddz, step, left, right, points, main_profiles,
288 Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, Create3dPres, Create2dPres, InvInd, WireIntersections );
291 void HYDROData_DTM::ProjWireOnPlane(const TopoDS_Wire& inpWire, const Handle_Geom_Plane& RefPlane, TopoDS_Wire& outWire)
293 /*BRepTools_WireExplorer ex(TopoDS::Wire(inpWire.Oriented(TopAbs_FORWARD)));
295 for (;ex.More();ex.Next())
297 const TopoDS_Edge& CE = ex.Current();
299 Handle(Geom_Curve) C3d = BRep_Tool::Curve(CE, f, l);
300 Handle(Geom_Curve) ProjectedCurve = GeomProjLib::ProjectOnPlane(new Geom_TrimmedCurve(C3d, f, l), RefPlane, RefPlane->Position().Direction(), Standard_True);
301 if (!ProjectedCurve.IsNull())
303 TopoDS_Edge ProjEdge = BRepLib_MakeEdge(ProjectedCurve, f, l );
304 if (!BRep_Tool::Degenerated(ProjEdge))
305 WM.Add(ProjEdge); //auto sharing between edges if vertex is coincident
311 BB.MakeFace(F, RefPlane, Precision::Confusion());
314 BRepAlgo_NormalProjection nproj(F);
316 nproj.SetDefaultParams();
320 TopoDS_Shape projRes = nproj.Projection();
321 TopExp_Explorer exp(projRes, TopAbs_EDGE);
322 TopTools_ListOfShape llE;
323 for (;exp.More();exp.Next())
324 llE.Append(exp.Current());
329 outWire.Orientation(inpWire.Orientation()); //take from the original wire
333 bool HYDROData_DTM::Get2dFaceFrom3dPres(const TopoDS_Compound& cmp, TopoDS_Face& outF )
335 Handle_Geom_Plane refpl = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
337 TopoDS_Iterator it(cmp);
338 //TopTools_IndexedMapOfShape IntW;
339 for (;it.More(); it.Next())
341 const TopoDS_Wire& W = TopoDS::Wire(it.Value());
342 if (W.Orientation() != TopAbs_INTERNAL)
344 //use list of edges to protect againts non-manifold cases.
345 //auto sharing between edges will be added automatically
346 TopTools_IndexedMapOfShape ME;
347 TopTools_ListOfShape LE;
348 TopExp::MapShapes(W, TopAbs_EDGE, ME);
349 for (int i = 1; i <= ME.Extent(); i++)
358 ProjWireOnPlane(WM.Wire(), refpl, outW);
359 BRepBuilderAPI_MakeFace mf(refpl, outW, true); //check inside is true by def
362 ShapeAnalysis_Wire WA(outW, outF, Precision::Confusion());
363 bool res = WA.CheckSelfIntersection();
366 ///!!! the internal wires cant be added with 'internal' ori.
367 // it's possible to do with brep builder yet the result will not be correct!
368 // more proper way is to use BOP operation here.
369 /*for (int i = 1; i <= IntW.Extent(); i++)
372 const TopoDS_Wire& W = TopoDS::Wire(IntW(i));
373 ProjWireOnPlane(W, refpl, outIW);
378 void HYDROData_DTM::CreateProfiles(const std::vector<Handle_HYDROData_Profile>& theProfiles,
380 double theSpatialStep,
381 AltitudePoints& theOutLeft,
382 AltitudePoints& theOutRight,
383 AltitudePoints& theOutPoints,
384 std::vector<AltitudePoints>& theOutMainProfiles,
385 TopoDS_Shape& Out3dPres,
386 TopoDS_Shape& Out2dPres,
387 TopoDS_Shape& OutLeftB,
388 TopoDS_Shape& OutRightB,
389 TopoDS_Shape& OutInlet,
390 TopoDS_Shape& OutOutlet,
393 std::set<int>& InvInd,
394 bool& WireIntersections)
396 if (theProfiles.empty())
398 theOutPoints = Interpolate( theProfiles, theDDZ, theSpatialStep, theOutLeft, theOutRight, theOutMainProfiles, InvInd );
399 //note that if Create3dPres is false => Create2dPres flag is meaningless!
402 TopTools_IndexedMapOfOrientedShape ll = Create3DShape( theOutLeft, theOutRight, theOutMainProfiles);
408 BB.MakeCompound(cmp);
409 for (int i = 1; i <= ll.Extent(); i++)
414 //same order as in HYDROData_DTM::Update()
416 OutRightB = ll(ll.Extent());
418 OutOutlet = ll(ll.Extent() - 1);
423 WireIntersections = Get2dFaceFrom3dPres(cmp, outF); //__TODO
432 void HYDROData_DTM::GetProperties( const Handle_HYDROData_Profile& theProfile,
433 gp_Pnt& theLowestPoint, gp_Vec2d& theDir,
435 double& theZMin, double& theZMax )
437 theLowestPoint = theProfile->GetBottomPoint();
440 theProfile->GetLeftPoint( aLeft, true, true );
441 theProfile->GetRightPoint( aRight, true, true );
442 double x = aRight.X()-aLeft.X();
443 double y = aRight.Y()-aLeft.Y();
445 theDir = gp_Vec2d( -y, x );
447 theDir = gp_Vec2d( x, y );
449 HYDROData_Profile::ProfilePoints points = theProfile->GetProfilePoints();
450 int lo = points.Lower();
451 int up = points.Upper();
452 theZMin = std::numeric_limits<double>::max();
454 for( int i=lo; i<=up; i++ )
456 double z = points.Value( i ).Z();
464 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr,
465 double& theUMin, double& theUMax )
467 gp_Pnt p = thePnt.Transformed( theTr );
474 return gp_Pnt2d( u, z );
477 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints,
478 const gp_Trsf& theTr,
479 double& theUMin, double& theUMax )
481 int low = thePoints.Lower(), up = thePoints.Upper();
482 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
483 for( int i=low; i<=up; i++ )
484 points->SetValue( i, To2D( thePoints.Value( i ), theTr, theUMin, theUMax ) );
488 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve,
489 Standard_Real theFirst, Standard_Real theLast,
490 const gp_Trsf& theTr,
491 double& theUMin, double& theUMax )
493 if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
495 gp_Pnt aFirstPnt, aLastPnt;
496 theCurve->D0( theFirst, aFirstPnt );
497 theCurve->D0( theLast, aLastPnt );
500 aFirst2d = To2D( aFirstPnt, theTr, theUMin, theUMax ),
501 aLast2d = To2D( aLastPnt, theTr, theUMin, theUMax );
503 gp_Vec2d dir( aFirst2d, aLast2d );
504 Handle_Geom2d_Line aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
505 return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
508 if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
510 Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
512 Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr, theUMin, theUMax );
513 const TColStd_Array1OfReal& knots = aSpline->Knots();
514 const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
515 int aDegree = aSpline->Degree();
517 return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
520 return Handle(Geom2d_Curve)();
523 Handle_Geom2d_BSplineCurve HYDROData_DTM::CreateHydraulicAxis(
524 const std::vector<Handle_HYDROData_Profile>& theProfiles,
525 std::vector<double>& theDistances )
527 size_t n = theProfiles.size();
528 Handle_Geom2d_BSplineCurve aResult;
530 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
531 TColgp_Array1OfVec2d tangents( 1, (int)n );
532 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
534 for( size_t i = 1; i <= n; i++ )
536 Handle_HYDROData_Profile aProfile = theProfiles[i-1];
542 GetProperties( aProfile, aLowest, aTangent, true, zmin, zmax );
543 aTangent.Normalize();
545 points->SetValue( (int)i, gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
546 tangents.SetValue( (int)i, aTangent );
547 flags->SetValue( (int)i, Standard_True );
550 Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
551 anInterpolator.Load( tangents, flags );
552 anInterpolator.Perform();
553 if( anInterpolator.IsDone() )
555 aResult = anInterpolator.Curve();
557 //fill the distances vector
558 Geom2dAdaptor_Curve anAdaptor( aResult );
560 theDistances.clear();
561 theDistances.reserve( n );
562 Standard_Real aParamFirst = anAdaptor.FirstParameter(), aParamLast = anAdaptor.LastParameter();
563 for( size_t i = 1; i <= n; i++ )
565 gp_Pnt2d aPnt = points->Value( (Standard_Integer)i );
566 Geom2dAPI_ProjectPointOnCurve aProject( aPnt, aResult );
567 Standard_Real aParam = aProject.LowerDistanceParameter();
568 double aDistance = GCPnts_AbscissaPoint::Length( anAdaptor, aParamFirst, aParam );
569 theDistances.push_back( aDistance );
575 std::vector<Handle_Geom2d_Curve> HYDROData_DTM::ProfileToParametric(
576 const Handle_HYDROData_Profile& theProfile,
577 double& theUMin, double& theUMax, gp_Vec2d& theDir )
579 std::vector<Handle_Geom2d_Curve> curves;
581 // Transformation of the coordinate systems
584 GetProperties( theProfile, aLowest, theDir, false, zmin, zmax );
586 gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
587 gp_Ax3 aLocal( gp_Pnt( aLowest.X(), aLowest.Y(), 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( theDir.X(), theDir.Y(), 0 ) );
590 aTransf.SetTransformation( aStd3d, aLocal );
592 // Iteration via edges
593 TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
594 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
595 for( ; anExp.More(); anExp.Next() )
597 // Extract an edge from wire
598 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
600 // Extract a curve corresponding to the edge
601 TopLoc_Location aLoc;
602 Standard_Real aFirst, aLast;
603 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
605 // Convert the curve to 2d CS
606 Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf, theUMin, theUMax );
607 if( !aCurve2d.IsNull() )
608 curves.push_back( aCurve2d );
614 bool CalcMidWidth( const std::set<double>& intersections, double& theMid, double& theWid )
616 double umin = std::numeric_limits<double>::max(),
619 size_t n = intersections.size();
623 std::set<double>::const_iterator it = intersections.begin(), last = intersections.end();
624 for( ; it!=last; it++ )
632 theMid = ( umin+umax )/2;
637 void HYDROData_DTM::ProfileDiscretization( const Handle_HYDROData_Profile& theProfile,
638 double theXCurv, double theMinZ, double theMaxZ, double theDDZ,
639 CurveUZ& theMidPointCurve,
640 CurveUZ& theWidthCurve,
641 int& intersection_nb,
644 double aDblMax = std::numeric_limits<double>::max(),
649 gp_Vec2d aProfileDir;
650 std::vector<Handle_Geom2d_Curve> curves = ProfileToParametric( theProfile, aUMin, aUMax, aProfileDir );
651 size_t n = curves.size();
656 // we add the "virtual" vertical lines to simulate the intersection with profile
657 gp_Pnt2d aFirst, aLast;
658 curves[0]->D0( curves[0]->FirstParameter(), aFirst );
659 curves[n-1]->D0( curves[n-1]->LastParameter(), aLast );
660 Handle(Geom2d_Line) aV1 = new Geom2d_Line( aFirst, gp_Dir2d( 0, 1 ) );
661 Handle(Geom2d_TrimmedCurve) aT1 = new Geom2d_TrimmedCurve( aV1, 0.0, aVMax );
663 Handle(Geom2d_Line) aV2 = new Geom2d_Line( aLast, gp_Dir2d( 0, 1 ) );
664 Handle(Geom2d_TrimmedCurve) aT2 = new Geom2d_TrimmedCurve( aV2, 0.0, aVMax );
666 curves.push_back( aT1 );
667 curves.push_back( aT2 );
669 int psize = ( int )( ( theMaxZ-theMinZ ) / theDDZ + 1 );
670 theMidPointCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
671 theMidPointCurve.reserve( psize );
672 theWidthCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
673 theWidthCurve.reserve( psize );
676 // for each discrete value of z we search intersection with profile
677 for( double z1 = theMinZ; z1 <= theMaxZ; z1 += theDDZ )
679 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, z1 ), gp_Dir2d( 1, 0 ) );
680 std::set<double> intersections;
681 for( size_t i = 0; i < n; i++ )
683 Handle_Geom2d_Curve aCurve = curves[i];
684 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
685 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
686 intersections.insert( anIntersect.Point( k ).X() );
689 intersection_nb = intersections.size();
690 if( intersection_nb >= 1 )
693 if( !CalcMidWidth( intersections, u_mid, u_wid ) )
696 double z = z1 - theMinZ;
700 theMidPointCurve.push_back( p_mid );
705 theWidthCurve.push_back( p_wid );
710 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB,
711 int theNbSteps, std::vector<CurveUZ>& theInterpolation,
714 theInterpolation.clear();
715 int d = isAddSecond ? 2 : 1;
716 theInterpolation.reserve( theNbSteps+d );
717 double dt = 1.0 / double( theNbSteps + 1 );
719 theInterpolation.push_back( theCurveA );
720 for( int i=0; i<theNbSteps; i++, t+=dt )
722 CurveUZ anInterp = theCurveA*(1-t) + theCurveB*t;
723 theInterpolation.push_back( anInterp );
726 theInterpolation.push_back( theCurveB );
728 #include <BRepLib_MakeEdge2d.hxx>
729 void HYDROData_DTM::CurveTo3D( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
730 const CurveUZ& theMidCurve, const CurveUZ& theWidthCurve,
731 AltitudePoints& thePoints )
733 Geom2dAdaptor_Curve anAdaptor( theHydraulicAxis );
734 TopoDS_Edge E2d = BRepLib_MakeEdge2d(theHydraulicAxis).Edge();
735 GCPnts_AbscissaPoint ap( anAdaptor, theMidCurve.Xcurv(), anAdaptor.FirstParameter() );
736 double aParam = ap.Parameter();
739 anAdaptor.D0( aParam, point );
740 gp_Vec2d profile_dir = theMidCurve.ProfileDir();
741 //gp_Dir tangent_n( -profile_dir.Y(), profile_dir.X(), dz );
742 profile_dir.Normalize();
744 size_t n = theMidCurve.size();
745 std::map<double, AltitudePoint> sorted_points;
746 for( size_t i=0; i<n; i++ )
748 double param1 = theMidCurve[i].U - theWidthCurve[i].U / 2;
749 double param2 = theMidCurve[i].U + theWidthCurve[i].U / 2;
751 gp_Pnt2d p1 = point.Translated( param1 * profile_dir);
752 gp_Pnt2d p2 = point.Translated( param2 * profile_dir);
754 double z = theMidCurve[i].Z + theMidCurve.DeltaZ();
756 AltitudePoint p3d_1( p1.X(), p1.Y(), z ), p3d_2( p2.X(), p2.Y(), z );
758 sorted_points[param1] = p3d_1;
759 sorted_points[param2] = p3d_2;
762 thePoints.reserve( sorted_points.size() );
763 const double EPS = 1E-12;
764 std::map<double, AltitudePoint>::const_iterator it = sorted_points.begin(), last = sorted_points.end();
765 for( ; it!=last; it++ )
766 if( thePoints.empty() || thePoints.back().SquareDistance( it->second ) > EPS )
767 thePoints.push_back( it->second );
770 inline double max( double a, double b )
778 inline double min( double a, double b )
786 #include <BRepLib_MakeWire.hxx>
788 std::vector<HYDROData_Bathymetry::AltitudePoints> HYDROData_DTM::Interpolate
789 ( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
790 const Handle_HYDROData_Profile& theProfileA,
792 const Handle_HYDROData_Profile& theProfileB,
794 double theDDZ, int theNbSteps, bool isAddSecond,
795 int& inter_nb_1, int& inter_nb_2)
797 double zminA, zmaxA, zminB, zmaxB;
798 gp_Pnt lowestA, lowestB;
801 GetProperties( theProfileA, lowestA, dirA, false, zminA, zmaxA );
802 GetProperties( theProfileB, lowestB, dirB, false, zminB, zmaxB );
805 double hmax = max( zmaxA-zminA, zmaxB-zminB );
807 //double dz = zminB - zminA;
808 //double zmin = min( zminA, zminB );
809 //double zmax = max( zmaxA, zmaxB );
811 CurveUZ midA(0, gp_Vec2d(), 0), midB(0, gp_Vec2d(), 0);
812 CurveUZ widA(0, gp_Vec2d(), 0), widB(0, gp_Vec2d(), 0);
814 ProfileDiscretization( theProfileA, theXCurvA, zminA, zminA+hmax, theDDZ, midA, widA, inter_nb_1 );
815 ProfileDiscretization( theProfileB, theXCurvB, zminB, zminB+hmax, theDDZ, midB, widB, inter_nb_2 );
817 std::vector<CurveUZ> mid, wid;
818 Interpolate( midA, midB, theNbSteps, mid, isAddSecond );
819 Interpolate( widA, widB, theNbSteps, wid, isAddSecond );
821 size_t p = mid.size();
822 size_t q = p>0 ? 2*mid[0].size() : 1;
823 std::vector<AltitudePoints> points;
826 for( size_t i=0; i<p; i++ )
828 points[i].reserve( q );
829 CurveTo3D( theHydraulicAxis, mid[i], wid[i], points[i] );
835 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
836 ( const std::vector<Handle_HYDROData_Profile>& theProfiles,
837 double theDDZ, double theSpatialStep,
838 AltitudePoints& theLeft,
839 AltitudePoints& theRight,
840 std::vector<AltitudePoints>& theMainProfiles,
841 std::set<int>& invalInd)
843 AltitudePoints points;
844 size_t n = theProfiles.size();
848 std::vector<double> distances;
849 Handle_Geom2d_BSplineCurve aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
850 if( aHydraulicAxis.IsNull() )
853 theMainProfiles.reserve( n );
855 for( size_t i=0, n1=n-1; i<n1; i++ )
857 double aDistance = distances[i+1]-distances[i];
858 int aNbSteps = int(aDistance/theSpatialStep);
859 bool isAddSecond = i==n1-1;
861 // 1. Calculate interpolated profiles
862 int inter_nb_1, inter_nb_2;
863 std::vector<AltitudePoints> local_points = Interpolate( aHydraulicAxis, theProfiles[i], distances[i],
864 theProfiles[i+1], distances[i+1], theDDZ, aNbSteps, isAddSecond, inter_nb_1, inter_nb_2 );
865 int lps = local_points.size();
871 invalInd.insert(i+1);
873 // 2. Put all points into the global container
874 for( size_t j=0; j<lps; j++ )
876 const AltitudePoints& lp = local_points[j];
878 points.reserve( lp.size() * n );
879 for( size_t k=0, ks=lp.size(); k<ks; k++ )
880 points.push_back( lp[k] );
883 // 3. Get left/right banks' points
886 theLeft.reserve( lps * n );
887 theRight.reserve( lps * n );
889 for( size_t j=0; j<lps; j++ )
891 const AltitudePoints& lp = local_points[j];
892 theLeft.push_back( lp[0] );
893 theRight.push_back( lp[lp.size()-1] );
896 // 4. Get main profiles points
897 theMainProfiles.push_back( local_points[0] );
899 theMainProfiles.push_back( local_points[lps-1] );
904 int HYDROData_DTM::EstimateNbPoints( const std::vector<Handle_HYDROData_Profile>& theProfiles,
905 double theDDZ, double theSpatialStep )
907 size_t n = theProfiles.size();
910 if( theDDZ<1E-6 || theSpatialStep<1E-6 )
913 std::vector<double> distances;
914 Handle_Geom2d_BSplineCurve aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
915 if( aHydraulicAxis.IsNull() )
918 double aCompleteDistance = distances[n-1];
919 int aNbSteps = int( aCompleteDistance / theSpatialStep ) + 1;
923 GetProperties( theProfiles[0], aLowest, aDir, true, aZMin, aZMax );
924 int aNbZSteps = (aZMax-aZMin)/theDDZ;
926 if( aNbSteps > ( 1<<16 ) || aNbZSteps > ( 1<<16 ) )
929 return aNbSteps * aNbZSteps;