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>
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>
63 IMPLEMENT_STANDARD_HANDLE( HYDROData_DTM, HYDROData_Bathymetry )
64 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
66 HYDROData_DTM::CurveUZ::CurveUZ( double theXCurv, const gp_Vec2d& theProfileDir, double theDeltaZ )
67 : myXcurv( theXCurv ), myProfileDir( theProfileDir ), myDeltaZ( theDeltaZ )
71 HYDROData_DTM::CurveUZ::~CurveUZ()
75 double HYDROData_DTM::CurveUZ::Xcurv() const
80 gp_Vec2d HYDROData_DTM::CurveUZ::ProfileDir() const
85 double HYDROData_DTM::CurveUZ::DeltaZ() const
90 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator + ( const CurveUZ& c ) const
92 HYDROData_DTM::CurveUZ res( Xcurv() + c.Xcurv(), ProfileDir() + c.ProfileDir(), DeltaZ() + c.DeltaZ() );
93 size_t n = size(), n1 = c.size();
96 std::cout << "Warning: different number of points in curves: " << n << ", " << n1 << std::endl;
99 for( int i=0; i<n; i++ )
102 p.U = operator[]( i ).U + c[i].U;
103 p.Z = operator[]( i ).Z;
109 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator * ( double d ) const
111 HYDROData_DTM::CurveUZ res( Xcurv()*d, ProfileDir()*d, DeltaZ()*d );
114 for( int i=0; i<n; i++ )
117 p.U = operator[]( i ).U * d;
118 p.Z = operator[]( i ).Z;
127 HYDROData_DTM::HYDROData_DTM()
131 HYDROData_DTM::~HYDROData_DTM()
135 HYDROData_SequenceOfObjects HYDROData_DTM::GetProfiles() const
137 return GetReferenceObjects( DataTag_Profiles );
140 void HYDROData_DTM::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles )
142 SetReferenceObjects( theProfiles, DataTag_Profiles );
146 double HYDROData_DTM::GetDDZ() const
148 return GetDouble( DataTag_DDZ );
151 void HYDROData_DTM::SetDDZ( double theDDZ )
153 SetDouble( DataTag_DDZ, theDDZ );
157 double HYDROData_DTM::GetSpatialStep() const
159 return GetDouble( DataTag_SpatialStep );
162 void HYDROData_DTM::SetSpatialStep( double theSpatialStep )
164 SetDouble( DataTag_SpatialStep, theSpatialStep );
168 void HYDROData_DTM::PointsToWire(const AltitudePoints& pnts, TopoDS_Wire& W )
171 BRepBuilderAPI_MakePolygon PM;
172 for (int i = 0; i < pnts.size(); i++)
173 PM.Add(gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
178 void HYDROData_DTM::PointsToEdge(const AltitudePoints& pnts, TopoDS_Edge& E )
180 Handle(TColgp_HArray1OfPnt) gpPoints = new TColgp_HArray1OfPnt(1, (int)pnts.size());
182 for (int i = 0; i < pnts.size(); i++)
183 gpPoints->SetValue(i+1, gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
185 GeomAPI_Interpolate anInterpolator(gpPoints, Standard_False,1.0e-6);
186 anInterpolator.Perform() ;
187 if (anInterpolator.IsDone())
189 Handle(Geom_Curve) C = anInterpolator.Curve();
190 E = BRepBuilderAPI_MakeEdge(C).Edge();
194 TopTools_IndexedMapOfOrientedShape HYDROData_DTM::Create3DShape(const AltitudePoints& left,
195 const AltitudePoints& right,
196 const std::vector<AltitudePoints>& main_profiles)
198 TopTools_IndexedMapOfOrientedShape ll;
199 //TopoDS_Wire LWire, RWire;
200 //PointsToWire(left, LWire);
201 //PointsToWire(right, RWire);
202 TopoDS_Edge LEdge, REdge;
203 PointsToEdge(left, LEdge);
204 PointsToEdge(right, REdge);
206 ll.Add(LEdge.Oriented(TopAbs_FORWARD));
208 for (int k = 0; k < main_profiles.size(); k++)
211 PointsToWire(main_profiles[k], W);
212 TopAbs_Orientation Ori = TopAbs_INTERNAL;
213 if (k == 0 || k == main_profiles.size() - 1)
214 Ori = TopAbs_FORWARD;
215 ll.Add(W.Oriented(Ori));
219 ll.Add(REdge.Oriented(TopAbs_FORWARD));
220 //yes, add subshapes in this order (left + profiles + right)
221 //otherwise the projected wire will be non-manifold
227 void HYDROData_DTM::Update()
229 AltitudePoints points;
230 TopoDS_Shape Out3dPres;
231 TopoDS_Shape Out2dPres;
232 TopoDS_Shape OutLeftB;
233 TopoDS_Shape OutRightB;
234 TopoDS_Shape OutInlet;
235 TopoDS_Shape OutOutlet;
237 HYDROData_SequenceOfObjects objs = GetProfiles();
238 double ddz = GetDDZ();
239 double step = GetSpatialStep();
240 std::set<int> InvInd;
241 bool WireIntersections; //__TODO
242 CreateProfilesFromDTM( objs, ddz, step, points, Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, true, true, InvInd, -1, WireIntersections );
243 SetAltitudePoints( points );
245 SetShape( DataTag_LeftBankShape, OutLeftB);
246 SetShape( DataTag_RightBankShape, OutRightB);
247 SetShape( DataTag_InletShape, OutInlet);
248 SetShape( DataTag_OutletShape, OutOutlet );
249 SetShape( DataTag_3DShape, Out3dPres );
250 SetShape( DataTag_2DShape, Out2dPres );
252 HYDROData_Bathymetry::Update();
255 void HYDROData_DTM::GetPresentationShapes( TopoDS_Shape& Out3dPres,
256 TopoDS_Shape& Out2dPres,
257 TopoDS_Shape& OutLeftB,
258 TopoDS_Shape& OutRightB,
259 TopoDS_Shape& OutInlet,
260 TopoDS_Shape& OutOutlet )
263 OutLeftB = GetShape( DataTag_LeftBankShape);
264 OutRightB = GetShape( DataTag_RightBankShape);
265 OutInlet = GetShape( DataTag_InletShape);
266 OutOutlet = GetShape( DataTag_OutletShape );
267 Out3dPres = GetShape( DataTag_3DShape );
268 Out2dPres = GetShape( DataTag_2DShape );
270 void HYDROData_DTM::CreateProfilesFromDTM (const HYDROData_SequenceOfObjects& InpProfiles,
273 AltitudePoints& points,
274 TopoDS_Shape& Out3dPres,
275 TopoDS_Shape& Out2dPres,
276 TopoDS_Shape& OutLeftB,
277 TopoDS_Shape& OutRightB,
278 TopoDS_Shape& OutInlet,
279 TopoDS_Shape& OutOutlet,
282 std::set<int>& InvInd,
284 bool& WireIntersections)
286 int aLower = InpProfiles.Lower(), anUpper = InpProfiles.Upper();
287 size_t n = anUpper - aLower + 1;
289 std::vector<Handle_HYDROData_Profile> profiles;
290 profiles.reserve( n );
291 for( int i=aLower; i<=anUpper; i++ )
293 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( InpProfiles.Value( i ) );
294 if( !aProfile.IsNull() )
295 profiles.push_back( aProfile );
297 const double EPS = 1E-3;
299 AltitudePoints right;
300 std::vector<AltitudePoints> main_profiles;
302 if( thePntsLimit > 0 )
304 int aNbPoints = EstimateNbPoints( profiles, ddz, step );
305 if( aNbPoints < 0 || aNbPoints > thePntsLimit )
309 if( ddz>EPS && step>EPS )
310 CreateProfiles(profiles, ddz, step, left, right, points, main_profiles,
311 Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, Create3dPres, Create2dPres, InvInd, WireIntersections );
314 bool HYDROData_DTM::GetPlanarFaceFromBanks( const TopoDS_Edge& LB, const TopoDS_Edge& RB, TopoDS_Face& outF,
315 TopTools_SequenceOfShape* Boundr)
319 Handle_Geom_Plane refpl = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
321 TopoDS_Vertex VFI, VLI, VFO, VLO;
326 BRepAdaptor_Curve LBAD(LB);
327 Handle_Geom_Curve LBPC = GeomProjLib::ProjectOnPlane(LBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
328 prLB = BRepLib_MakeEdge(LBPC).Edge();
330 BRepAdaptor_Curve RBAD(RB);
331 Handle_Geom_Curve RBPC = GeomProjLib::ProjectOnPlane(RBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
332 prRB = BRepLib_MakeEdge(RBPC).Edge();
334 TopExp::Vertices(prLB, VFI, VFO, 1);
335 TopExp::Vertices(prRB, VLI, VLO, 1);
336 TopoDS_Edge prIL = BRepLib_MakeEdge(VFI, VLI).Edge();
337 TopoDS_Edge prOL = BRepLib_MakeEdge(VFO, VLO).Edge();
338 TopoDS_Wire prW = BRepLib_MakeWire(prLB, prIL, prOL, prRB).Wire();
339 outF = BRepBuilderAPI_MakeFace(refpl->Pln(), prW, 1).Face();
343 Boundr->Append(prLB);
344 Boundr->Append(prIL);
345 Boundr->Append(prOL);
346 Boundr->Append(prRB);
349 ShapeAnalysis_Wire WA(prW, outF, Precision::Confusion());
350 bool res = WA.CheckSelfIntersection();
354 void HYDROData_DTM::CreateProfiles(const std::vector<Handle_HYDROData_Profile>& theProfiles,
356 double theSpatialStep,
357 AltitudePoints& theOutLeft,
358 AltitudePoints& theOutRight,
359 AltitudePoints& theOutPoints,
360 std::vector<AltitudePoints>& theOutMainProfiles,
361 TopoDS_Shape& Out3dPres,
362 TopoDS_Shape& Out2dPres,
363 TopoDS_Shape& OutLeftB,
364 TopoDS_Shape& OutRightB,
365 TopoDS_Shape& OutInlet,
366 TopoDS_Shape& OutOutlet,
369 std::set<int>& InvInd,
372 if (theProfiles.empty())
374 theOutPoints = Interpolate( theProfiles, theDDZ, theSpatialStep, theOutLeft, theOutRight, theOutMainProfiles, InvInd );
375 //note that if Create3dPres is false => Create2dPres flag is meaningless!
376 if( theOutPoints.empty() )
381 TopTools_IndexedMapOfOrientedShape ll = Create3DShape( theOutLeft, theOutRight, theOutMainProfiles);
387 BB.MakeCompound(cmp);
388 for (int i = 1; i <= ll.Extent(); i++)
393 //same order as in HYDROData_DTM::Update()
395 OutRightB = ll(ll.Extent());
397 OutOutlet = ll(ll.Extent() - 1);
402 ProjStat = GetPlanarFaceFromBanks(TopoDS::Edge(OutLeftB), TopoDS::Edge(OutRightB), outF, NULL);
411 void HYDROData_DTM::GetProperties( const Handle_HYDROData_Profile& theProfile,
412 gp_Pnt& theLowestPoint, gp_Vec2d& theDir,
413 double& theZMin, double& theZMax )
415 theLowestPoint = theProfile->GetBottomPoint();
418 theProfile->GetLeftPoint( aLeft, true, true );
419 theProfile->GetRightPoint( aRight, true, true );
420 double x = aRight.X()-aLeft.X();
421 double y = aRight.Y()-aLeft.Y();
422 theDir = gp_Vec2d( x, y );
424 HYDROData_Profile::ProfilePoints points = theProfile->GetProfilePoints();
425 int lo = points.Lower();
426 int up = points.Upper();
427 theZMin = std::numeric_limits<double>::max();
429 for( int i=lo; i<=up; i++ )
431 double z = points.Value( i ).Z();
439 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr,
440 double& theUMin, double& theUMax )
442 gp_Pnt p = thePnt.Transformed( theTr );
449 return gp_Pnt2d( u, z );
452 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints,
453 const gp_Trsf& theTr,
454 double& theUMin, double& theUMax )
456 int low = thePoints.Lower(), up = thePoints.Upper();
457 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
458 for( int i=low; i<=up; i++ )
459 points->SetValue( i, To2D( thePoints.Value( i ), theTr, theUMin, theUMax ) );
463 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve,
464 Standard_Real theFirst, Standard_Real theLast,
465 const gp_Trsf& theTr,
466 double& theUMin, double& theUMax )
468 if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
470 gp_Pnt aFirstPnt, aLastPnt;
471 theCurve->D0( theFirst, aFirstPnt );
472 theCurve->D0( theLast, aLastPnt );
475 aFirst2d = To2D( aFirstPnt, theTr, theUMin, theUMax ),
476 aLast2d = To2D( aLastPnt, theTr, theUMin, theUMax );
478 gp_Vec2d dir( aFirst2d, aLast2d );
479 Handle_Geom2d_Line aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
480 return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
483 if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
485 Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
487 Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr, theUMin, theUMax );
488 const TColStd_Array1OfReal& knots = aSpline->Knots();
489 const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
490 int aDegree = aSpline->Degree();
492 return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
495 return Handle(Geom2d_Curve)();
498 #include <GCE2d_MakeSegment.hxx>
499 #include <Geom2dAPI_InterCurveCurve.hxx>
500 bool IsCooriented( const Handle_HYDROData_Profile& theProfile1,
501 const Handle_HYDROData_Profile& theProfile2 )
503 if( theProfile1==theProfile2 )
506 gp_XY lp1, rp1, lp2, rp2;
507 theProfile1->GetLeftPoint(lp1);
508 theProfile1->GetRightPoint(rp1);
509 theProfile2->GetLeftPoint(lp2);
510 theProfile2->GetRightPoint(rp2);
512 GCE2d_MakeSegment s1(lp1, lp2);
513 GCE2d_MakeSegment s2(rp1, rp2);
515 Geom2dAPI_InterCurveCurve inter;
517 if (inter.NbPoints() == 0)
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();
529 return Handle_Geom2d_BSplineCurve();
531 Handle_Geom2d_BSplineCurve aResult;
533 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
534 TColgp_Array1OfVec2d tangents( 1, (int)n );
535 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
537 for( size_t i = 1; i <= n; i++ )
539 Handle_HYDROData_Profile aProfile = theProfiles[i-1];
540 Handle_HYDROData_Profile aPrevProfile = i==1 ? theProfiles[i-1] : theProfiles[i-2];
541 Handle_HYDROData_Profile aNextProfile = i==n ? theProfiles[i-1] : theProfiles[i];
543 if( !IsCooriented( aProfile, aNextProfile ) )
546 aProfile->GetLeftPoint( lp, true );
547 aProfile->GetRightPoint( rp, true );
548 aProfile->SetLeftPoint( rp, true );
549 aProfile->SetRightPoint( lp, true );
557 gp_XYZ curP = aProfile->GetBottomPoint();
558 gp_XY curP2d = gp_XY(curP.X(), curP.Y());
562 nextP = aPrevProfile->GetBottomPoint(true);
564 nextP = aNextProfile->GetBottomPoint(true);
566 gp_XY nextP2d = gp_XY(nextP.X(), nextP.Y());
569 GetProperties( aProfile, aLowest, aPrTangent, zmin, zmax );
570 aNormal.SetCoord( -aPrTangent.Y(), aPrTangent.X() );
572 gp_Vec2d aDirToNextProfile(nextP2d.X() - curP2d.X(), nextP2d.Y() - curP2d.Y() );
574 aDirToNextProfile.Reverse();
575 if (aNormal.Dot(aDirToNextProfile) < 0)
580 points->SetValue( (int)i, gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
581 tangents.SetValue( (int)i, aNormal );
582 flags->SetValue( (int)i, Standard_True );
585 Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
586 anInterpolator.Load( tangents, flags );
587 anInterpolator.Perform();
588 if( anInterpolator.IsDone() )
590 aResult = anInterpolator.Curve();
592 //fill the distances vector
593 Geom2dAdaptor_Curve anAdaptor( aResult );
595 theDistances.clear();
596 theDistances.reserve( n );
597 Standard_Real aParamFirst = anAdaptor.FirstParameter(), aParamLast = anAdaptor.LastParameter();
598 for( size_t i = 1; i <= n; i++ )
600 gp_Pnt2d aPnt = points->Value( (Standard_Integer)i );
601 Geom2dAPI_ProjectPointOnCurve aProject( aPnt, aResult );
602 Standard_Real aParam = aProject.LowerDistanceParameter();
603 double aDistance = GCPnts_AbscissaPoint::Length( anAdaptor, aParamFirst, aParam );
604 theDistances.push_back( aDistance );
610 std::vector<Handle_Geom2d_Curve> HYDROData_DTM::ProfileToParametric(
611 const Handle_HYDROData_Profile& theProfile,
612 double& theUMin, double& theUMax, gp_Vec2d& theDir )
614 std::vector<Handle_Geom2d_Curve> curves;
616 // Transformation of the coordinate systems
619 GetProperties( theProfile, aLowest, theDir, zmin, zmax );
621 gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
622 gp_Ax3 aLocal( gp_Pnt( aLowest.X(), aLowest.Y(), 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( theDir.X(), theDir.Y(), 0 ) );
625 aTransf.SetTransformation( aStd3d, aLocal );
627 // Iteration via edges
628 TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
629 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
630 for( ; anExp.More(); anExp.Next() )
632 // Extract an edge from wire
633 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
635 // Extract a curve corresponding to the edge
636 TopLoc_Location aLoc;
637 Standard_Real aFirst, aLast;
638 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
640 // Convert the curve to 2d CS
641 Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf, theUMin, theUMax );
642 if( !aCurve2d.IsNull() )
643 curves.push_back( aCurve2d );
649 bool CalcMidWidth( const std::set<double>& intersections, double& theMid, double& theWid )
651 double umin = std::numeric_limits<double>::max(),
654 size_t n = intersections.size();
658 std::set<double>::const_iterator it = intersections.begin(), last = intersections.end();
659 for( ; it!=last; it++ )
667 theMid = ( umin+umax )/2;
672 void HYDROData_DTM::ProfileDiscretization( const Handle_HYDROData_Profile& theProfile,
673 double theXCurv, double theMinZ, double theMaxZ, double theDDZ,
674 CurveUZ& theMidPointCurve,
675 CurveUZ& theWidthCurve,
676 int& intersection_nb,
679 double aDblMax = std::numeric_limits<double>::max(),
684 gp_Vec2d aProfileDir;
685 std::vector<Handle_Geom2d_Curve> curves = ProfileToParametric( theProfile, aUMin, aUMax, aProfileDir );
686 size_t n = curves.size();
691 // we add the "virtual" vertical lines to simulate the intersection with profile
692 gp_Pnt2d aFirst, aLast;
693 curves[0]->D0( curves[0]->FirstParameter(), aFirst );
694 curves[n-1]->D0( curves[n-1]->LastParameter(), aLast );
695 Handle(Geom2d_Line) aV1 = new Geom2d_Line( aFirst, gp_Dir2d( 0, 1 ) );
696 Handle(Geom2d_TrimmedCurve) aT1 = new Geom2d_TrimmedCurve( aV1, 0.0, aVMax );
698 Handle(Geom2d_Line) aV2 = new Geom2d_Line( aLast, gp_Dir2d( 0, 1 ) );
699 Handle(Geom2d_TrimmedCurve) aT2 = new Geom2d_TrimmedCurve( aV2, 0.0, aVMax );
701 curves.push_back( aT1 );
702 curves.push_back( aT2 );
704 int psize = ( int )( ( theMaxZ-theMinZ ) / theDDZ + 1 );
705 theMidPointCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
706 theMidPointCurve.reserve( psize );
707 theWidthCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
708 theWidthCurve.reserve( psize );
711 // for each discrete value of z we search intersection with profile
712 for( double z1 = theMinZ; z1 <= theMaxZ; z1 += theDDZ )
714 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, z1 ), gp_Dir2d( 1, 0 ) );
715 std::set<double> intersections;
716 for( size_t i = 0; i < n; i++ )
718 Handle_Geom2d_Curve aCurve = curves[i];
719 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
720 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
721 intersections.insert( anIntersect.Point( k ).X() );
724 intersection_nb = intersections.size();
725 if( intersection_nb >= 1 )
728 if( !CalcMidWidth( intersections, u_mid, u_wid ) )
731 double z = z1 - theMinZ;
735 theMidPointCurve.push_back( p_mid );
740 theWidthCurve.push_back( p_wid );
745 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB,
746 int theNbSteps, std::vector<CurveUZ>& theInterpolation,
749 theInterpolation.clear();
750 int d = isAddSecond ? 2 : 1;
751 theInterpolation.reserve( theNbSteps+d );
752 double dt = 1.0 / double( theNbSteps + 1 );
754 theInterpolation.push_back( theCurveA );
755 for( int i=0; i<theNbSteps; i++, t+=dt )
757 CurveUZ anInterp = theCurveA*(1-t) + theCurveB*t;
758 theInterpolation.push_back( anInterp );
761 theInterpolation.push_back( theCurveB );
763 #include <BRepLib_MakeEdge2d.hxx>
764 void HYDROData_DTM::CurveTo3D( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
765 const CurveUZ& theMidCurve, const CurveUZ& theWidthCurve,
766 AltitudePoints& thePoints )
768 Geom2dAdaptor_Curve anAdaptor( theHydraulicAxis );
769 TopoDS_Edge E2d = BRepLib_MakeEdge2d(theHydraulicAxis).Edge();
770 GCPnts_AbscissaPoint ap( anAdaptor, theMidCurve.Xcurv(), anAdaptor.FirstParameter() );
771 double aParam = ap.Parameter();
774 anAdaptor.D0( aParam, point );
775 gp_Vec2d profile_dir = theMidCurve.ProfileDir();
776 //gp_Dir tangent_n( -profile_dir.Y(), profile_dir.X(), dz );
777 profile_dir.Normalize();
779 size_t n = theMidCurve.size();
780 std::map<double, AltitudePoint> sorted_points;
781 for( size_t i=0; i<n; i++ )
783 double param1 = theMidCurve[i].U - theWidthCurve[i].U / 2;
784 double param2 = theMidCurve[i].U + theWidthCurve[i].U / 2;
786 gp_Pnt2d p1 = point.Translated( param1 * profile_dir);
787 gp_Pnt2d p2 = point.Translated( param2 * profile_dir);
789 double z = theMidCurve[i].Z + theMidCurve.DeltaZ();
791 AltitudePoint p3d_1( p1.X(), p1.Y(), z ), p3d_2( p2.X(), p2.Y(), z );
793 sorted_points[param1] = p3d_1;
794 sorted_points[param2] = p3d_2;
797 thePoints.reserve( sorted_points.size() );
798 const double EPS = 1E-12;
799 std::map<double, AltitudePoint>::const_iterator it = sorted_points.begin(), last = sorted_points.end();
800 for( ; it!=last; it++ )
801 if( thePoints.empty() || thePoints.back().SquareDistance( it->second ) > EPS )
802 thePoints.push_back( it->second );
805 inline double max( double a, double b )
813 inline double min( double a, double b )
821 #include <BRepLib_MakeWire.hxx>
823 std::vector<HYDROData_Bathymetry::AltitudePoints> HYDROData_DTM::Interpolate
824 ( const Handle_Geom2d_BSplineCurve& theHydraulicAxis,
825 const Handle_HYDROData_Profile& theProfileA,
827 const Handle_HYDROData_Profile& theProfileB,
829 double theDDZ, int theNbSteps, bool isAddSecond,
830 int& inter_nb_1, int& inter_nb_2)
832 double zminA, zmaxA, zminB, zmaxB;
833 gp_Pnt lowestA, lowestB;
836 GetProperties( theProfileA, lowestA, dirA, zminA, zmaxA );
837 GetProperties( theProfileB, lowestB, dirB, zminB, zmaxB );
840 double hmax = max( zmaxA-zminA, zmaxB-zminB );
842 //double dz = zminB - zminA;
843 //double zmin = min( zminA, zminB );
844 //double zmax = max( zmaxA, zmaxB );
846 CurveUZ midA(0, gp_Vec2d(), 0), midB(0, gp_Vec2d(), 0);
847 CurveUZ widA(0, gp_Vec2d(), 0), widB(0, gp_Vec2d(), 0);
849 ProfileDiscretization( theProfileA, theXCurvA, zminA, zminA+hmax, theDDZ, midA, widA, inter_nb_1 );
850 ProfileDiscretization( theProfileB, theXCurvB, zminB, zminB+hmax, theDDZ, midB, widB, inter_nb_2 );
852 std::vector<CurveUZ> mid, wid;
853 Interpolate( midA, midB, theNbSteps, mid, isAddSecond );
854 Interpolate( widA, widB, theNbSteps, wid, isAddSecond );
856 size_t p = mid.size();
857 size_t q = p>0 ? 2*mid[0].size() : 1;
858 std::vector<AltitudePoints> points;
861 for( size_t i=0; i<p; i++ )
863 points[i].reserve( q );
864 CurveTo3D( theHydraulicAxis, mid[i], wid[i], points[i] );
870 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
871 ( const std::vector<Handle_HYDROData_Profile>& theProfiles,
872 double theDDZ, double theSpatialStep,
873 AltitudePoints& theLeft,
874 AltitudePoints& theRight,
875 std::vector<AltitudePoints>& theMainProfiles,
876 std::set<int>& invalInd)
878 AltitudePoints points;
879 size_t n = theProfiles.size();
883 std::vector<double> distances;
884 Handle_Geom2d_BSplineCurve aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
885 if( aHydraulicAxis.IsNull() )
888 theMainProfiles.reserve( n );
890 for( size_t i=0, n1=n-1; i<n1; i++ )
892 double aDistance = distances[i+1]-distances[i];
893 int aNbSteps = int(aDistance/theSpatialStep);
894 bool isAddSecond = i==n1-1;
896 // 1. Calculate interpolated profiles
897 int inter_nb_1, inter_nb_2;
898 std::vector<AltitudePoints> local_points = Interpolate( aHydraulicAxis, theProfiles[i], distances[i],
899 theProfiles[i+1], distances[i+1], theDDZ, aNbSteps, isAddSecond, inter_nb_1, inter_nb_2 );
900 int lps = local_points.size();
906 invalInd.insert(i+1);
908 // 2. Put all points into the global container
909 for( size_t j=0; j<lps; j++ )
911 const AltitudePoints& lp = local_points[j];
913 points.reserve( lp.size() * n );
914 for( size_t k=0, ks=lp.size(); k<ks; k++ )
915 points.push_back( lp[k] );
918 // 3. Get left/right banks' points
921 theLeft.reserve( lps * n );
922 theRight.reserve( lps * n );
924 for( size_t j=0; j<lps; j++ )
926 const AltitudePoints& lp = local_points[j];
927 theLeft.push_back( lp[0] );
928 theRight.push_back( lp[lp.size()-1] );
931 // 4. Get main profiles points
932 theMainProfiles.push_back( local_points[0] );
934 theMainProfiles.push_back( local_points[lps-1] );
939 int HYDROData_DTM::EstimateNbPoints( const std::vector<Handle_HYDROData_Profile>& theProfiles,
940 double theDDZ, double theSpatialStep )
942 size_t n = theProfiles.size();
945 if( theDDZ<1E-6 || theSpatialStep<1E-6 )
948 std::vector<double> distances;
949 Handle_Geom2d_BSplineCurve aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
950 if( aHydraulicAxis.IsNull() )
953 double aCompleteDistance = distances[n-1];
954 int aNbSteps = int( aCompleteDistance / theSpatialStep ) + 1;
958 GetProperties( theProfiles[0], aLowest, aDir, aZMin, aZMax );
959 int aNbZSteps = (aZMax-aZMin)/theDDZ;
961 if( aNbSteps > ( 1<<16 ) || aNbZSteps > ( 1<<16 ) )
964 return aNbSteps * aNbZSteps;