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>
33 #include <Bnd_Box2d.hxx>
35 #include <BRepLib_MakeEdge.hxx>
36 #include <BRepLib_MakeWire.hxx>
37 #include <BRep_Builder.hxx>
38 #include <GeomProjLib.hxx>
39 #include <Geom_TrimmedCurve.hxx>
40 #include <BRepTools_WireExplorer.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
44 #include <TopTools_IndexedMapOfOrientedShape.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <BRepLib_MakeEdge.hxx>
48 #include <BRepLib_MakeWire.hxx>
49 #include <BRep_Builder.hxx>
50 #include <ShapeAnalysis_Wire.hxx>
51 #include <BRepAlgo_NormalProjection.hxx>
52 #include <ShapeUpgrade_UnifySameDomain.hxx>
53 #include <BRepBuilderAPI_MakePolygon.hxx>
54 #include <BOPAlgo_Builder.hxx>
55 #include <BRepAdaptor_Curve.hxx>
56 #include <GeomProjLib.hxx>
58 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
59 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <TopTools_SequenceOfShape.hxx>
62 #include <NCollection_DataMap.hxx>
68 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
70 HYDROData_DTM::CurveUZ::CurveUZ( double theXCurv, const gp_Vec2d& theProfileDir, double theDeltaZ, double theMaxZ )
71 : myXcurv( theXCurv ), myProfileDir( theProfileDir ), myDeltaZ( theDeltaZ ), myMaxZ (theMaxZ)
75 HYDROData_DTM::CurveUZ::~CurveUZ()
79 double HYDROData_DTM::CurveUZ::Xcurv() const
84 gp_Vec2d HYDROData_DTM::CurveUZ::ProfileDir() const
89 double HYDROData_DTM::CurveUZ::DeltaZ() const
94 double HYDROData_DTM::CurveUZ::MaxZ() const
99 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator + ( const CurveUZ& c ) const
101 HYDROData_DTM::CurveUZ res( Xcurv() + c.Xcurv(), ProfileDir() + c.ProfileDir(), DeltaZ() + c.DeltaZ(), MaxZ() + c.MaxZ() );
102 size_t n = size(), n1 = c.size();
105 std::cout << "Warning: different number of points in curves: " << n << ", " << n1 << std::endl;
107 int q = n < n1 ? n : n1;
109 for( int i=0; i<q; i++ )
112 p.U = operator[]( i ).U + c[i].U;
113 p.Z = operator[]( i ).Z;
119 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator * ( double d ) const
121 HYDROData_DTM::CurveUZ res( Xcurv()*d, ProfileDir()*d, DeltaZ()*d, MaxZ()*d );
124 for( int i=0; i<n; i++ )
127 p.U = operator[]( i ).U * d;
128 p.Z = operator[]( i ).Z;
137 HYDROData_DTM::HYDROData_DTM()
141 HYDROData_DTM::~HYDROData_DTM()
145 HYDROData_SequenceOfObjects HYDROData_DTM::GetProfiles() const
147 return GetReferenceObjects( DataTag_Profiles );
150 void HYDROData_DTM::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles )
152 SetReferenceObjects( theProfiles, DataTag_Profiles );
156 double HYDROData_DTM::GetDDZ() const
158 return GetDouble( DataTag_DDZ );
161 void HYDROData_DTM::SetDDZ( double theDDZ )
163 SetDouble( DataTag_DDZ, theDDZ );
167 double HYDROData_DTM::GetSpatialStep() const
169 return GetDouble( DataTag_SpatialStep );
172 void HYDROData_DTM::SetSpatialStep( double theSpatialStep )
174 SetDouble( DataTag_SpatialStep, theSpatialStep );
178 void HYDROData_DTM::PointsToWire(const AltitudePoints& pnts, TopoDS_Wire& W )
181 BRepBuilderAPI_MakePolygon PM;
182 for (int i = 0; i < pnts.size(); i++)
183 PM.Add(gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
188 void HYDROData_DTM::PointsToEdge(const AltitudePoints& pnts, TopoDS_Edge& E )
190 Handle(TColgp_HArray1OfPnt) gpPoints = new TColgp_HArray1OfPnt(1, (int)pnts.size());
192 for (int i = 0; i < pnts.size(); i++)
193 gpPoints->SetValue(i+1, gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
195 GeomAPI_Interpolate anInterpolator(gpPoints, Standard_False,1.0e-6);
196 anInterpolator.Perform() ;
197 if (anInterpolator.IsDone())
199 Handle(Geom_BSplineCurve) C = anInterpolator.Curve();
200 E = BRepBuilderAPI_MakeEdge(C).Edge();
204 TopTools_IndexedMapOfOrientedShape HYDROData_DTM::Create3DShape(const AltitudePoints& left,
205 const AltitudePoints& right,
206 const std::vector<AltitudePoints>& main_profiles)
208 TopTools_IndexedMapOfOrientedShape ll;
209 //TopoDS_Wire LWire, RWire;
210 //PointsToWire(left, LWire);
211 //PointsToWire(right, RWire);
212 TopoDS_Edge LEdge, REdge;
213 PointsToEdge(left, LEdge);
214 PointsToEdge(right, REdge);
216 ll.Add(LEdge.Oriented(TopAbs_FORWARD));
218 for (int k = 0; k < main_profiles.size(); k++)
221 PointsToWire(main_profiles[k], W);
222 TopAbs_Orientation Ori = TopAbs_INTERNAL;
223 if (k == 0 || k == main_profiles.size() - 1)
224 Ori = TopAbs_FORWARD;
225 ll.Add(W.Oriented(Ori));
229 ll.Add(REdge.Oriented(TopAbs_FORWARD));
230 //yes, add subshapes in this order (left + profiles + right)
231 //otherwise the projected wire will be non-manifold
237 void HYDROData_DTM::Update()
239 AltitudePoints points;
240 TopoDS_Shape Out3dPres;
241 TopoDS_Shape Out2dPres;
242 TopoDS_Shape OutLeftB;
243 TopoDS_Shape OutRightB;
244 TopoDS_Shape OutInlet;
245 TopoDS_Shape OutOutlet;
247 HYDROData_SequenceOfObjects objs = GetProfiles();
248 double ddz = GetDDZ();
249 double step = GetSpatialStep();
250 std::set<int> InvInd;
251 bool WireIntersections; //__TODO
253 bool ToEstimateWarnings; //NOT NEEDED here
254 CreateProfilesFromDTM( objs, ddz, step, points, Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet,
255 true, true, InvInd, -1, WireIntersections, myWarnings, ToEstimateWarnings );
256 SetAltitudePoints( points );
258 SetShape( DataTag_LeftBankShape, OutLeftB);
259 SetShape( DataTag_RightBankShape, OutRightB);
260 SetShape( DataTag_InletShape, OutInlet);
261 SetShape( DataTag_OutletShape, OutOutlet );
262 SetShape( DataTag_3DShape, Out3dPres );
263 SetShape( DataTag_2DShape, Out2dPres );
265 HYDROData_Bathymetry::Update();
268 void HYDROData_DTM::GetPresentationShapes( TopoDS_Shape& Out3dPres,
269 TopoDS_Shape& Out2dPres,
270 TopoDS_Shape& OutLeftB,
271 TopoDS_Shape& OutRightB,
272 TopoDS_Shape& OutInlet,
273 TopoDS_Shape& OutOutlet )
276 OutLeftB = GetShape( DataTag_LeftBankShape);
277 OutRightB = GetShape( DataTag_RightBankShape);
278 OutInlet = GetShape( DataTag_InletShape);
279 OutOutlet = GetShape( DataTag_OutletShape );
280 Out3dPres = GetShape( DataTag_3DShape );
281 Out2dPres = GetShape( DataTag_2DShape );
283 void HYDROData_DTM::CreateProfilesFromDTM (const HYDROData_SequenceOfObjects& InpProfiles,
286 AltitudePoints& points,
287 TopoDS_Shape& Out3dPres,
288 TopoDS_Shape& Out2dPres,
289 TopoDS_Shape& OutLeftB,
290 TopoDS_Shape& OutRightB,
291 TopoDS_Shape& OutInlet,
292 TopoDS_Shape& OutOutlet,
295 std::set<int>& InvInd,
297 bool& WireIntersections,
298 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>& warnings,
299 bool& ToEstimateWarnings)
301 int aLower = InpProfiles.Lower(), anUpper = InpProfiles.Upper();
302 size_t n = anUpper - aLower + 1;
304 std::vector<Handle(HYDROData_Profile)> profiles;
305 profiles.reserve( n );
306 for( int i=aLower; i<=anUpper; i++ )
308 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( InpProfiles.Value( i ) );
309 if( !aProfile.IsNull() )
310 profiles.push_back( aProfile );
312 const double EPS = 1E-3;
314 AltitudePoints right;
315 std::vector<AltitudePoints> main_profiles;
317 bool ToEstimateWarningsOnly = false;
318 if( thePntsLimit > 0 )
320 int aNbPoints = EstimateNbPoints( profiles, ddz, step );
321 if( aNbPoints < 0 || aNbPoints > thePntsLimit )
323 ToEstimateWarningsOnly = true;
328 if( profiles.size() < 2 )
331 if( ddz>EPS && step>EPS )
332 CreateProfiles(profiles, ddz, step, left, right, points, main_profiles,
333 Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, Create3dPres,
334 Create2dPres, InvInd, WireIntersections, warnings, ToEstimateWarningsOnly );
336 ToEstimateWarnings = ToEstimateWarningsOnly;
339 bool HYDROData_DTM::GetPlanarFaceFromBanks( const TopoDS_Edge& LB, const TopoDS_Edge& RB, TopoDS_Face& outF,
340 TopTools_SequenceOfShape* Boundr)
344 Handle_Geom_Plane refpl = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
346 TopoDS_Vertex VFI, VLI, VFO, VLO;
351 BRepAdaptor_Curve LBAD(LB);
352 Handle_Geom_Curve LBPC = GeomProjLib::ProjectOnPlane(LBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
353 prLB = BRepLib_MakeEdge(LBPC).Edge();
355 BRepAdaptor_Curve RBAD(RB);
356 Handle_Geom_Curve RBPC = GeomProjLib::ProjectOnPlane(RBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
357 prRB = BRepLib_MakeEdge(RBPC).Edge();
359 TopExp::Vertices(prLB, VFI, VFO, 1);
360 TopExp::Vertices(prRB, VLI, VLO, 1);
361 TopoDS_Edge prIL = BRepLib_MakeEdge(VFI, VLI).Edge();
362 TopoDS_Edge prOL = BRepLib_MakeEdge(VFO, VLO).Edge();
363 TopoDS_Wire prW = BRepLib_MakeWire(prLB, prIL, prOL, prRB).Wire();
364 outF = BRepBuilderAPI_MakeFace(refpl->Pln(), prW, 1).Face();
368 Boundr->Append(prLB);
369 Boundr->Append(prIL);
370 Boundr->Append(prOL);
371 Boundr->Append(prRB);
374 ShapeAnalysis_Wire WA(prW, outF, Precision::Confusion());
375 bool res = WA.CheckSelfIntersection();
379 void HYDROData_DTM::CreateProfiles(const std::vector<Handle(HYDROData_Profile)>& theProfiles,
381 double theSpatialStep,
382 AltitudePoints& theOutLeft,
383 AltitudePoints& theOutRight,
384 AltitudePoints& theOutPoints,
385 std::vector<AltitudePoints>& theOutMainProfiles,
386 TopoDS_Shape& Out3dPres,
387 TopoDS_Shape& Out2dPres,
388 TopoDS_Shape& OutLeftB,
389 TopoDS_Shape& OutRightB,
390 TopoDS_Shape& OutInlet,
391 TopoDS_Shape& OutOutlet,
394 std::set<int>& InvInd,
396 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>& warnings,
397 bool ToEstimateWarningsOnly)
399 if (theProfiles.empty())
401 theOutPoints = Interpolate( theProfiles, theDDZ, theSpatialStep, theOutLeft, theOutRight,
402 theOutMainProfiles, InvInd, warnings, ToEstimateWarningsOnly );
403 //note that if Create3dPres is false => Create2dPres flag is meaningless!
404 if( theOutPoints.empty() )
409 TopTools_IndexedMapOfOrientedShape ll = Create3DShape( theOutLeft, theOutRight, theOutMainProfiles);
415 BB.MakeCompound(cmp);
416 for (int i = 1; i <= ll.Extent(); i++)
421 //same order as in HYDROData_DTM::Update()
423 OutRightB = ll(ll.Extent());
425 OutOutlet = ll(ll.Extent() - 1);
430 ProjStat = GetPlanarFaceFromBanks(TopoDS::Edge(OutLeftB), TopoDS::Edge(OutRightB), outF, NULL);
439 void HYDROData_DTM::GetProperties( const Handle(HYDROData_Profile)& theProfile,
440 gp_Pnt& theLowestPoint, gp_Vec2d& theDir,
441 double& theZMin, double& theZMax )
443 theLowestPoint = theProfile->GetBottomPoint();
446 theProfile->GetLeftPoint( aLeft, true, true );
447 theProfile->GetRightPoint( aRight, true, true );
448 double x = aRight.X()-aLeft.X();
449 double y = aRight.Y()-aLeft.Y();
450 theDir = gp_Vec2d( x, y );
452 HYDROData_Profile::ProfilePoints points = theProfile->GetProfilePoints();
453 int lo = points.Lower();
454 int up = points.Upper();
457 for( int i=lo; i<=up; i++ )
459 double z = points.Value( i ).Z();
467 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr,
468 double& theUMin, double& theUMax )
470 gp_Pnt p = thePnt.Transformed( theTr );
477 return gp_Pnt2d( u, z );
480 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints,
481 const gp_Trsf& theTr,
482 double& theUMin, double& theUMax )
484 int low = thePoints.Lower(), up = thePoints.Upper();
485 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
486 for( int i=low; i<=up; i++ )
487 points->SetValue( i, To2D( thePoints.Value( i ), theTr, theUMin, theUMax ) );
491 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve,
492 Standard_Real theFirst, Standard_Real theLast,
493 const gp_Trsf& theTr,
494 double& theUMin, double& theUMax )
496 if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
498 gp_Pnt aFirstPnt, aLastPnt;
499 theCurve->D0( theFirst, aFirstPnt );
500 theCurve->D0( theLast, aLastPnt );
503 aFirst2d = To2D( aFirstPnt, theTr, theUMin, theUMax ),
504 aLast2d = To2D( aLastPnt, theTr, theUMin, theUMax );
506 gp_Vec2d dir( aFirst2d, aLast2d );
507 Handle(Geom2d_Line) aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
508 return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
511 if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
513 Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
515 Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr, theUMin, theUMax );
516 const TColStd_Array1OfReal& knots = aSpline->Knots();
517 const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
518 int aDegree = aSpline->Degree();
520 return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
523 return Handle(Geom2d_Curve)();
526 #include <GCE2d_MakeSegment.hxx>
527 #include <Geom2dAPI_InterCurveCurve.hxx>
528 bool IsCooriented( const Handle(HYDROData_Profile)& theProfile1,
529 const Handle(HYDROData_Profile)& theProfile2 )
531 if( theProfile1==theProfile2 )
534 gp_XY lp1, rp1, lp2, rp2;
535 theProfile1->GetLeftPoint(lp1);
536 theProfile1->GetRightPoint(rp1);
537 theProfile2->GetLeftPoint(lp2);
538 theProfile2->GetRightPoint(rp2);
540 GCE2d_MakeSegment s1(lp1, lp2);
541 GCE2d_MakeSegment s2(rp1, rp2);
543 Geom2dAPI_InterCurveCurve inter;
544 inter.Init(s1.Value(), s2.Value());
545 if (inter.NbPoints() == 0)
551 Handle(Geom2d_BSplineCurve) HYDROData_DTM::CreateHydraulicAxis(
552 const std::vector<Handle(HYDROData_Profile)>& theProfiles,
553 std::vector<double>& theDistances )
555 size_t n = theProfiles.size();
557 return Handle_Geom2d_BSplineCurve();
559 Handle_Geom2d_BSplineCurve aResult;
561 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
562 TColgp_Array1OfVec2d tangents( 1, (int)n );
563 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
565 // Stage 1. Orient all profiles to be co-oriented with the first profile
566 theProfiles[0]->Update();
567 for( size_t i = 1; i < n; i++ )
569 Handle(HYDROData_Profile) aProfile = theProfiles[i];
570 Handle(HYDROData_Profile) aPrevProfile = theProfiles[i-1];
572 if( !IsCooriented( aProfile, aPrevProfile ) )
575 aProfile->GetLeftPoint( lp, true );
576 aProfile->GetRightPoint( rp, true );
577 aProfile->SetLeftPoint( rp, true );
578 aProfile->SetRightPoint( lp, true );
583 // Stage 2. Calculate normals so that each normal "points" to the next profile
584 for( size_t i = 0; i < n; i++ )
586 Handle(HYDROData_Profile) aProfile = theProfiles[i];
587 Handle(HYDROData_Profile) aNextProfile = i==n-1 ? theProfiles[i-1] : theProfiles[i+1];
593 gp_XYZ curP = aProfile->GetBottomPoint(true);
594 gp_XY curP2d = gp_XY(curP.X(), curP.Y());
596 gp_XYZ nextP = aNextProfile->GetBottomPoint(true);
597 gp_XY nextP2d = gp_XY(nextP.X(), nextP.Y());
600 GetProperties( aProfile, aLowest, aPrTangent, zmin, zmax );
601 aNormal.SetCoord( -aPrTangent.Y(), aPrTangent.X() );
603 gp_Vec2d aDirToNextProfile(nextP2d.X() - curP2d.X(), nextP2d.Y() - curP2d.Y() );
605 aDirToNextProfile.Reverse();
607 if (aNormal.Dot(aDirToNextProfile) < 0)
612 points->SetValue( (int)(i+1), gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
613 tangents.SetValue( (int)(i+1), aNormal );
614 flags->SetValue( (int)(i+1), Standard_True );
617 Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
618 anInterpolator.Load( tangents, flags );
619 anInterpolator.Perform();
620 if( anInterpolator.IsDone() )
622 aResult = anInterpolator.Curve();
624 //fill the distances vector
625 Geom2dAdaptor_Curve anAdaptor( aResult );
627 theDistances.clear();
628 theDistances.reserve( n );
629 Standard_Real aParamFirst = anAdaptor.FirstParameter(), aParamLast = anAdaptor.LastParameter();
630 for( size_t i = 1; i <= n; i++ )
632 gp_Pnt2d aPnt = points->Value( (Standard_Integer)i );
633 Geom2dAPI_ProjectPointOnCurve aProject( aPnt, aResult );
634 Standard_Real aParam = aProject.LowerDistanceParameter();
635 double aDistance = GCPnts_AbscissaPoint::Length( anAdaptor, aParamFirst, aParam );
636 theDistances.push_back( aDistance );
642 std::vector<Handle(Geom2d_Curve)> HYDROData_DTM::ProfileToParametric(
643 const Handle(HYDROData_Profile)& theProfile,
644 double& theUMin, double& theUMax, gp_Vec2d& theDir )
646 std::vector<Handle(Geom2d_Curve)> curves;
648 // Transformation of the coordinate systems
651 GetProperties( theProfile, aLowest, theDir, zmin, zmax );
653 gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
654 gp_Ax3 aLocal( gp_Pnt( aLowest.X(), aLowest.Y(), 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( theDir.X(), theDir.Y(), 0 ) );
657 aTransf.SetTransformation( aStd3d, aLocal );
659 // Iteration via edges
660 TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
661 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
662 for( ; anExp.More(); anExp.Next() )
664 // Extract an edge from wire
665 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
667 // Extract a curve corresponding to the edge
668 TopLoc_Location aLoc;
669 Standard_Real aFirst, aLast;
670 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
672 // Convert the curve to 2d CS
673 Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf, theUMin, theUMax );
674 if( !aCurve2d.IsNull() )
675 curves.push_back( aCurve2d );
681 //bool CalcMidWidth( const std::set<double>& intersections, double& theMid, double& theWid )
683 // double umin = std::numeric_limits<double>::max(),
686 // size_t n = intersections.size();
690 // std::set<double>::const_iterator it = intersections.begin(), last = intersections.end();
691 // for( ; it!=last; it++ )
699 // theMid = ( umin+umax )/2;
700 // theWid = umax-umin;
706 bool CalcMidWidth( const Bnd_Box2d& inters_bnd, double& theMid, double& theWid )
708 double umin = DBL_MAX,
711 if (inters_bnd.IsVoid())
713 //size_t n = intersections.size();
717 double xmin, ymin, xmax, ymax;
718 inters_bnd.Get(xmin, ymin, xmax, ymax);
719 if (Abs(ymax-ymin)>PREC)
722 theMid = ( xmax+xmin )/2;
729 void HYDROData_DTM::ProfileDiscretization( const Handle(HYDROData_Profile)& theProfile,
730 double theXCurv, double theMinZ, double theMaxZ, double theTopZ, double theDDZ,
731 CurveUZ& theMidPointCurve,
732 CurveUZ& theWidthCurve,
733 int& intersection_nb,
735 QSet<QString>& warnings)
738 double aDblMax = std::numeric_limits<double>::max(),
743 gp_Vec2d aProfileDir;
744 std::vector<Handle(Geom2d_Curve)> curves = ProfileToParametric( theProfile, aUMin, aUMax, aProfileDir );
745 size_t n = curves.size();
749 warnings.insert("no curves for discretization; skipped");
753 // we add the "virtual" vertical lines to simulate the intersection with profile
754 gp_Pnt2d aFirst, aLast;
755 curves[0]->D0( curves[0]->FirstParameter(), aFirst );
756 curves[n-1]->D0( curves[n-1]->LastParameter(), aLast );
757 Handle(Geom2d_Line) aV1 = new Geom2d_Line( aFirst, gp_Dir2d( 0, 1 ) );
758 Handle(Geom2d_TrimmedCurve) aT1 = new Geom2d_TrimmedCurve( aV1, 0.0, aVMax );
760 Handle(Geom2d_Line) aV2 = new Geom2d_Line( aLast, gp_Dir2d( 0, 1 ) );
761 Handle(Geom2d_TrimmedCurve) aT2 = new Geom2d_TrimmedCurve( aV2, 0.0, aVMax );
763 curves.push_back( aT1 );
764 curves.push_back( aT2 );
766 int psize = ( int )( ( theMaxZ-theMinZ ) / theDDZ + 1 );
767 theMidPointCurve = CurveUZ( theXCurv, aProfileDir, theMinZ, theTopZ);
768 theMidPointCurve.reserve( psize );
769 theWidthCurve = CurveUZ( theXCurv, aProfileDir, theMinZ, theTopZ );
770 theWidthCurve.reserve( psize );
773 bool PlatoCase = false;
774 if (Abs(aFirst.Y() - aLast.Y()) > PREC)
776 warnings.insert("One of the extreme points is higher than another");
778 double ZminExtr = Min(aFirst.Y(), aLast.Y());
779 double ZmaxExtr = Max(aFirst.Y(), aLast.Y());
780 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, ZminExtr ), gp_Dir2d( 1, 0 ) );
781 std::vector<gp_Pnt2d> intersections;
782 for( int i = 0; i < n; i++ )
784 Handle(Geom2d_Curve) aCurve = curves[i];
785 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
786 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
787 intersections.push_back(anIntersect.Point(k));
788 if (anIntersect.NbSegments() > 0 )
791 std::vector<gp_Pnt2d> interm_intersections;
792 for (int i=0;i<intersections.size();i++)
794 gp_Pnt2d int_p2d = intersections[i];
795 //check for intermid. points: shoudl be higher than ZminExtr and not in intersection with first,last points
796 if (aFirst.Distance(int_p2d) > PREC && aLast.Distance(int_p2d) > PREC && int_p2d.Y() > ZminExtr)
797 interm_intersections.push_back(int_p2d);
799 if (!interm_intersections.empty())
800 warnings.insert("One of the internal points is higher than extreme points");
801 if (ZminExtr != ZmaxExtr && !PlatoCase)
803 //additional check of plato for zmax
804 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, ZmaxExtr ), gp_Dir2d( 1, 0 ) );
805 std::vector<gp_Pnt2d> intersections;
806 for( int i = 0; i < n; i++ )
808 Handle(Geom2d_Curve) aCurve = curves[i];
809 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
810 if (anIntersect.NbSegments() > 0 )
818 warnings.insert("Plato case on extremes");
823 // for each discrete value of z we search intersection with profile
824 for( double z1 = theMinZ; z1 <= theMaxZ; z1 += theDDZ )
826 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, z1 ), gp_Dir2d( 1, 0 ) );
827 std::set<double> intersections; //TODO the solutions should be treated with some epsilon~1e-8 (computation error of intersector)
828 Bnd_Box2d intersect_bndbox;
829 for( size_t i = 0; i < n; i++ )
831 Handle(Geom2d_Curve) aCurve = curves[i];
832 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
833 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
835 intersections.insert( anIntersect.Point( k ).X() );
836 intersect_bndbox.Add( anIntersect.Point(k));
839 for( int k=1, m=anIntersect.NbSegments(); k<=m; k++ )
841 Handle(Geom2d_Curve) Curve1,Curve2;
842 anIntersect.Segment(k, Curve1, Curve2 );
843 double f = Curve2->FirstParameter();
844 double l = Curve2->LastParameter();
848 intersect_bndbox.Add( Pf );
849 intersect_bndbox.Add( Pl );
850 intersections.insert( Pf.X() );
851 intersections.insert( Pl.X() );
855 intersection_nb = intersections.size();
856 if (intersection_nb == 0)
858 warnings.insert("No intersections between profile & altitude Z-lines found; skipped");
861 else if (intersection_nb > 2)
863 warnings.insert("More than 2 intersections between profile & altitude Z-lines found");
865 double xmin, ymin, xmax, ymax;
866 intersect_bndbox.Get(xmin, ymin, xmax, ymax);
867 //if (Abs(xmax-xmin)>PREC)
868 if (intersection_nb >= 1)
871 if( !CalcMidWidth( intersect_bndbox, u_mid, u_wid ) )
874 double z = z1 - theMinZ;
878 theMidPointCurve.push_back( p_mid );
883 theWidthCurve.push_back( p_wid );
887 //intersection_nb = intersections.size();
888 //if( intersection_nb >= 1 )
890 // double u_mid, u_wid;
891 // if( !CalcMidWidth( intersections, u_mid, u_wid ) )
894 // double z = z1 - theMinZ;
898 // theMidPointCurve.push_back( p_mid );
903 // theWidthCurve.push_back( p_wid );
908 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB,
909 int theNbSteps, std::vector<CurveUZ>& theInterpolation,
912 theInterpolation.clear();
913 int d = isAddSecond ? 2 : 1;
914 theInterpolation.reserve( theNbSteps+d );
915 double dt = 1.0 / double( theNbSteps + 1 );
917 theInterpolation.push_back( theCurveA );
918 for( int i=0; i<theNbSteps; i++, t+=dt )
920 CurveUZ anInterp = theCurveA*(1-t) + theCurveB*t;
921 theInterpolation.push_back( anInterp );
924 theInterpolation.push_back( theCurveB );
926 #include <BRepLib_MakeEdge2d.hxx>
927 void HYDROData_DTM::CurveTo3D( const Handle(Geom2d_BSplineCurve)& theHydraulicAxis,
928 const CurveUZ& theMidCurve, const CurveUZ& theWidthCurve,
929 AltitudePoints& thePoints )
931 Geom2dAdaptor_Curve anAdaptor( theHydraulicAxis );
932 TopoDS_Edge E2d = BRepLib_MakeEdge2d(theHydraulicAxis).Edge();
933 GCPnts_AbscissaPoint ap( anAdaptor, theMidCurve.Xcurv(), anAdaptor.FirstParameter() );
934 double aParam = ap.Parameter();
937 anAdaptor.D0( aParam, point );
938 gp_Vec2d profile_dir = theMidCurve.ProfileDir();
939 //gp_Dir tangent_n( -profile_dir.Y(), profile_dir.X(), dz );
940 profile_dir.Normalize();
942 size_t n = theMidCurve.size();
943 std::map<double, AltitudePoint> sorted_points;
944 bool isOnTop = false;
945 for( size_t i=0; i<n; i++ ) // build the two banks of the interpolated profile, from bottom to top
947 double param1 = theMidCurve[i].U - theWidthCurve[i].U / 2;
948 double param2 = theMidCurve[i].U + theWidthCurve[i].U / 2;
950 gp_Pnt2d p1 = point.Translated( param1 * profile_dir);
951 gp_Pnt2d p2 = point.Translated( param2 * profile_dir);
953 bool arrivedOnTop = false;
955 if (theMidCurve[i].Z <= theMidCurve.MaxZ())
956 z = theMidCurve[i].Z + theMidCurve.DeltaZ();
959 z = theMidCurve.MaxZ() + theMidCurve.DeltaZ(); // limit z to linear interpolation between maxima on extremity profiles
960 arrivedOnTop = true; // do not keep points after this one
964 AltitudePoint p3d_1( p1.X(), p1.Y(), z ), p3d_2( p2.X(), p2.Y(), z );
966 sorted_points[param1] = p3d_1;
967 sorted_points[param2] = p3d_2;
970 // isOnTop =true; // do not keep points after this one (commented: leads to strange limits of 2D shape)
973 thePoints.reserve( sorted_points.size() );
974 const double EPS = 1E-12;
975 std::map<double, AltitudePoint>::const_iterator it = sorted_points.begin(), last = sorted_points.end();
976 for( ; it!=last; it++ )
977 if( thePoints.empty() || thePoints.back().SquareDistance( it->second ) > EPS )
978 thePoints.push_back( it->second );
981 //inline double max( double a, double b )
989 //inline double min( double a, double b )
997 #include <BRepLib_MakeWire.hxx>
999 std::vector<HYDROData_Bathymetry::AltitudePoints> HYDROData_DTM::Interpolate
1000 ( const Handle(Geom2d_BSplineCurve)& theHydraulicAxis,
1001 const Handle(HYDROData_Profile)& theProfileA,
1003 const Handle(HYDROData_Profile)& theProfileB,
1005 double theDDZ, int theNbSteps, bool isAddSecond,
1006 int& inter_nb_1, int& inter_nb_2,
1007 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>& warnings,
1008 bool ToEstimateWarningsOnly)
1010 double zminA, zmaxA, zminB, zmaxB;
1011 gp_Pnt lowestA, lowestB;
1012 gp_Vec2d dirA, dirB;
1014 GetProperties( theProfileA, lowestA, dirA, zminA, zmaxA );
1015 GetProperties( theProfileB, lowestB, dirB, zminB, zmaxB );
1018 double hmax = zmaxA-zminA > zmaxB-zminB ? zmaxA-zminA : zmaxB-zminB;
1020 //double dz = zminB - zminA;
1021 //double zmin = min( zminA, zminB );
1022 //double zmax = max( zmaxA, zmaxB );
1024 CurveUZ midA(0, gp_Vec2d(), 0, 0), midB(0, gp_Vec2d(), 0, 0);
1025 CurveUZ widA(0, gp_Vec2d(), 0, 0), widB(0, gp_Vec2d(), 0, 0);
1027 QSet<QString> warnings_per_profileA, warnings_per_profileB;
1029 ProfileDiscretization( theProfileA, theXCurvA, zminA, zminA+hmax, zmaxA-zminA,
1030 theDDZ, midA, widA, inter_nb_1, 1E-6, warnings_per_profileA );
1031 ProfileDiscretization( theProfileB, theXCurvB, zminB, zminB+hmax, zmaxB-zminB,
1032 theDDZ, midB, widB, inter_nb_2, 1E-6, warnings_per_profileB );
1035 if (warnings.IsBound(theProfileA))
1037 QSet<QString>& warnings_per_profileA_old = warnings.ChangeFind(theProfileA);
1038 warnings_per_profileA_old+=warnings_per_profileA;
1041 warnings.Bind(theProfileA, warnings_per_profileA);
1043 if (warnings.IsBound(theProfileB))
1045 QSet<QString>& warnings_per_profileB_old = warnings.ChangeFind(theProfileB);
1046 warnings_per_profileB_old+=warnings_per_profileB;
1049 warnings.Bind(theProfileB, warnings_per_profileB);
1052 if (ToEstimateWarningsOnly)
1053 return std::vector<AltitudePoints>();
1055 std::vector<CurveUZ> mid, wid;
1056 Interpolate( midA, midB, theNbSteps, mid, isAddSecond );
1057 Interpolate( widA, widB, theNbSteps, wid, isAddSecond );
1059 size_t p = mid.size();
1060 size_t q = p>0 ? 2*mid[0].size() : 1;
1061 std::vector<AltitudePoints> points;
1064 for( size_t i=0; i<p; i++ )
1066 points[i].reserve( q );
1067 CurveTo3D( theHydraulicAxis, mid[i], wid[i], points[i] );
1073 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
1074 ( const std::vector<Handle(HYDROData_Profile)>& theProfiles,
1075 double theDDZ, double theSpatialStep,
1076 AltitudePoints& theLeft,
1077 AltitudePoints& theRight,
1078 std::vector<AltitudePoints>& theMainProfiles,
1079 std::set<int>& invalInd,
1080 NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>& warnings,
1081 bool ToEstimateWarningsOnly)
1083 AltitudePoints points;
1084 size_t n = theProfiles.size();
1088 std::vector<double> distances;
1089 Handle(Geom2d_BSplineCurve) aHydraulicAxis;
1091 if (!ToEstimateWarningsOnly)
1093 aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
1094 if( aHydraulicAxis.IsNull() )
1098 theMainProfiles.reserve( n );
1100 for( size_t i=0, n1=n-1; i<n1; i++ )
1102 double aDistance = 0;
1104 bool isAddSecond = false;
1106 if (!ToEstimateWarningsOnly)
1108 aDistance = distances[i+1]-distances[i];
1109 aNbSteps = int(aDistance/theSpatialStep);
1110 isAddSecond = i==n1-1;
1113 // 1. Calculate interpolated profiles
1114 int inter_nb_1, inter_nb_2;
1115 std::vector<AltitudePoints> local_points = Interpolate( aHydraulicAxis, theProfiles[i],
1116 ToEstimateWarningsOnly ? 0 : distances[i],
1118 ToEstimateWarningsOnly ? 0 : distances[i+1],
1119 theDDZ, aNbSteps, isAddSecond, inter_nb_1, inter_nb_2, warnings,
1120 ToEstimateWarningsOnly);
1121 int lps = local_points.size();
1130 invalInd.insert(i+1);
1132 // 2. Put all points into the global container
1133 for( size_t j=0; j<lps; j++ )
1135 const AltitudePoints& lp = local_points[j];
1137 points.reserve( lp.size() * n );
1138 for( size_t k=0, ks=lp.size(); k<ks; k++ )
1139 points.push_back( lp[k] );
1142 // 3. Get left/right banks' points
1145 theLeft.reserve( lps * n );
1146 theRight.reserve( lps * n );
1148 for( size_t j=0; j<lps; j++ )
1150 const AltitudePoints& lp = local_points[j];
1151 theLeft.push_back( lp[0] );
1152 theRight.push_back( lp[lp.size()-1] );
1155 // 4. Get main profiles points
1156 theMainProfiles.push_back( local_points[0] );
1158 theMainProfiles.push_back( local_points[lps-1] );
1163 int HYDROData_DTM::EstimateNbPoints( const std::vector<Handle(HYDROData_Profile)>& theProfiles,
1164 double theDDZ, double theSpatialStep )
1166 size_t n = theProfiles.size();
1169 if( theDDZ<1E-6 || theSpatialStep<1E-6 )
1172 std::vector<double> distances;
1173 Handle(Geom2d_BSplineCurve) aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
1174 if( aHydraulicAxis.IsNull() )
1177 double aCompleteDistance = distances[n-1];
1178 int aNbSteps = int( aCompleteDistance / theSpatialStep ) + 1;
1181 double aZMin, aZMax;
1182 GetProperties( theProfiles[0], aLowest, aDir, aZMin, aZMax );
1183 int aNbZSteps = (aZMax-aZMin)/theDDZ;
1185 if( aNbSteps > ( 1<<16 ) || aNbZSteps > ( 1<<16 ) )
1188 return aNbSteps * aNbZSteps;
1191 void HYDROData_DTM::GetWarnings(NCollection_DataMap<Handle(HYDROData_Profile), QSet<QString>>& warnings)
1193 warnings = myWarnings;