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 <BRepTools_WireExplorer.hxx>
40 #include <TopTools_IndexedMapOfShape.hxx>
41 #include <BRepBuilderAPI_MakeFace.hxx>
43 #include <TopTools_IndexedMapOfOrientedShape.hxx>
44 #include <TopTools_IndexedDataMapOfShapeListOfShape.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>
51 #include <ShapeUpgrade_UnifySameDomain.hxx>
52 #include <BRepBuilderAPI_MakePolygon.hxx>
53 #include <BOPAlgo_Builder.hxx>
54 #include <BRepAdaptor_Curve.hxx>
55 #include <GeomProjLib.hxx>
57 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <TopTools_SequenceOfShape.hxx>
62 IMPLEMENT_STANDARD_RTTIEXT( HYDROData_DTM, HYDROData_Bathymetry )
64 HYDROData_DTM::CurveUZ::CurveUZ( double theXCurv, const gp_Vec2d& theProfileDir, double theDeltaZ )
65 : myXcurv( theXCurv ), myProfileDir( theProfileDir ), myDeltaZ( theDeltaZ )
69 HYDROData_DTM::CurveUZ::~CurveUZ()
73 double HYDROData_DTM::CurveUZ::Xcurv() const
78 gp_Vec2d HYDROData_DTM::CurveUZ::ProfileDir() const
83 double HYDROData_DTM::CurveUZ::DeltaZ() const
88 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator + ( const CurveUZ& c ) const
90 HYDROData_DTM::CurveUZ res( Xcurv() + c.Xcurv(), ProfileDir() + c.ProfileDir(), DeltaZ() + c.DeltaZ() );
91 size_t n = size(), n1 = c.size();
94 std::cout << "Warning: different number of points in curves: " << n << ", " << n1 << std::endl;
97 for( int i=0; i<n; i++ )
100 p.U = operator[]( i ).U + c[i].U;
101 p.Z = operator[]( i ).Z;
107 HYDROData_DTM::CurveUZ HYDROData_DTM::CurveUZ::operator * ( double d ) const
109 HYDROData_DTM::CurveUZ res( Xcurv()*d, ProfileDir()*d, DeltaZ()*d );
112 for( int i=0; i<n; i++ )
115 p.U = operator[]( i ).U * d;
116 p.Z = operator[]( i ).Z;
125 HYDROData_DTM::HYDROData_DTM()
129 HYDROData_DTM::~HYDROData_DTM()
133 HYDROData_SequenceOfObjects HYDROData_DTM::GetProfiles() const
135 return GetReferenceObjects( DataTag_Profiles );
138 void HYDROData_DTM::SetProfiles( const HYDROData_SequenceOfObjects& theProfiles )
140 SetReferenceObjects( theProfiles, DataTag_Profiles );
144 double HYDROData_DTM::GetDDZ() const
146 return GetDouble( DataTag_DDZ );
149 void HYDROData_DTM::SetDDZ( double theDDZ )
151 SetDouble( DataTag_DDZ, theDDZ );
155 double HYDROData_DTM::GetSpatialStep() const
157 return GetDouble( DataTag_SpatialStep );
160 void HYDROData_DTM::SetSpatialStep( double theSpatialStep )
162 SetDouble( DataTag_SpatialStep, theSpatialStep );
166 void HYDROData_DTM::PointsToWire(const AltitudePoints& pnts, TopoDS_Wire& W )
169 BRepBuilderAPI_MakePolygon PM;
170 for (int i = 0; i < pnts.size(); i++)
171 PM.Add(gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
176 void HYDROData_DTM::PointsToEdge(const AltitudePoints& pnts, TopoDS_Edge& E )
178 Handle(TColgp_HArray1OfPnt) gpPoints = new TColgp_HArray1OfPnt(1, (int)pnts.size());
180 for (int i = 0; i < pnts.size(); i++)
181 gpPoints->SetValue(i+1, gp_Pnt(pnts[i].X, pnts[i].Y, pnts[i].Z));
183 GeomAPI_Interpolate anInterpolator(gpPoints, Standard_False,1.0e-6);
184 anInterpolator.Perform() ;
185 if (anInterpolator.IsDone())
187 Handle(Geom_BSplineCurve) C = anInterpolator.Curve();
188 E = BRepBuilderAPI_MakeEdge(C).Edge();
192 TopTools_IndexedMapOfOrientedShape HYDROData_DTM::Create3DShape(const AltitudePoints& left,
193 const AltitudePoints& right,
194 const std::vector<AltitudePoints>& main_profiles)
196 TopTools_IndexedMapOfOrientedShape ll;
197 //TopoDS_Wire LWire, RWire;
198 //PointsToWire(left, LWire);
199 //PointsToWire(right, RWire);
200 TopoDS_Edge LEdge, REdge;
201 PointsToEdge(left, LEdge);
202 PointsToEdge(right, REdge);
204 ll.Add(LEdge.Oriented(TopAbs_FORWARD));
206 for (int k = 0; k < main_profiles.size(); k++)
209 PointsToWire(main_profiles[k], W);
210 TopAbs_Orientation Ori = TopAbs_INTERNAL;
211 if (k == 0 || k == main_profiles.size() - 1)
212 Ori = TopAbs_FORWARD;
213 ll.Add(W.Oriented(Ori));
217 ll.Add(REdge.Oriented(TopAbs_FORWARD));
218 //yes, add subshapes in this order (left + profiles + right)
219 //otherwise the projected wire will be non-manifold
225 void HYDROData_DTM::Update()
227 AltitudePoints points;
228 TopoDS_Shape Out3dPres;
229 TopoDS_Shape Out2dPres;
230 TopoDS_Shape OutLeftB;
231 TopoDS_Shape OutRightB;
232 TopoDS_Shape OutInlet;
233 TopoDS_Shape OutOutlet;
235 HYDROData_SequenceOfObjects objs = GetProfiles();
236 double ddz = GetDDZ();
237 double step = GetSpatialStep();
238 std::set<int> InvInd;
239 bool WireIntersections; //__TODO
240 CreateProfilesFromDTM( objs, ddz, step, points, Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, true, true, InvInd, -1, WireIntersections );
241 SetAltitudePoints( points );
243 SetShape( DataTag_LeftBankShape, OutLeftB);
244 SetShape( DataTag_RightBankShape, OutRightB);
245 SetShape( DataTag_InletShape, OutInlet);
246 SetShape( DataTag_OutletShape, OutOutlet );
247 SetShape( DataTag_3DShape, Out3dPres );
248 SetShape( DataTag_2DShape, Out2dPres );
250 HYDROData_Bathymetry::Update();
253 void HYDROData_DTM::GetPresentationShapes( TopoDS_Shape& Out3dPres,
254 TopoDS_Shape& Out2dPres,
255 TopoDS_Shape& OutLeftB,
256 TopoDS_Shape& OutRightB,
257 TopoDS_Shape& OutInlet,
258 TopoDS_Shape& OutOutlet )
261 OutLeftB = GetShape( DataTag_LeftBankShape);
262 OutRightB = GetShape( DataTag_RightBankShape);
263 OutInlet = GetShape( DataTag_InletShape);
264 OutOutlet = GetShape( DataTag_OutletShape );
265 Out3dPres = GetShape( DataTag_3DShape );
266 Out2dPres = GetShape( DataTag_2DShape );
268 void HYDROData_DTM::CreateProfilesFromDTM (const HYDROData_SequenceOfObjects& InpProfiles,
271 AltitudePoints& points,
272 TopoDS_Shape& Out3dPres,
273 TopoDS_Shape& Out2dPres,
274 TopoDS_Shape& OutLeftB,
275 TopoDS_Shape& OutRightB,
276 TopoDS_Shape& OutInlet,
277 TopoDS_Shape& OutOutlet,
280 std::set<int>& InvInd,
282 bool& WireIntersections)
284 int aLower = InpProfiles.Lower(), anUpper = InpProfiles.Upper();
285 size_t n = anUpper - aLower + 1;
287 std::vector<Handle(HYDROData_Profile)> profiles;
288 profiles.reserve( n );
289 for( int i=aLower; i<=anUpper; i++ )
291 Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast( InpProfiles.Value( i ) );
292 if( !aProfile.IsNull() )
293 profiles.push_back( aProfile );
295 const double EPS = 1E-3;
297 AltitudePoints right;
298 std::vector<AltitudePoints> main_profiles;
300 if( thePntsLimit > 0 )
302 int aNbPoints = EstimateNbPoints( profiles, ddz, step );
303 if( aNbPoints < 0 || aNbPoints > thePntsLimit )
307 if( ddz>EPS && step>EPS )
308 CreateProfiles(profiles, ddz, step, left, right, points, main_profiles,
309 Out3dPres, Out2dPres, OutLeftB, OutRightB, OutInlet, OutOutlet, Create3dPres, Create2dPres, InvInd, WireIntersections );
312 bool HYDROData_DTM::GetPlanarFaceFromBanks( const TopoDS_Edge& LB, const TopoDS_Edge& RB, TopoDS_Face& outF,
313 TopTools_SequenceOfShape* Boundr)
317 Handle_Geom_Plane refpl = new Geom_Plane(gp_Pnt(0,0,0), gp_Dir(0,0,1));
319 TopoDS_Vertex VFI, VLI, VFO, VLO;
324 BRepAdaptor_Curve LBAD(LB);
325 Handle_Geom_Curve LBPC = GeomProjLib::ProjectOnPlane(LBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
326 prLB = BRepLib_MakeEdge(LBPC).Edge();
328 BRepAdaptor_Curve RBAD(RB);
329 Handle_Geom_Curve RBPC = GeomProjLib::ProjectOnPlane(RBAD.Curve().Curve(), refpl, gp_Dir(0, 0, -1), 1 );
330 prRB = BRepLib_MakeEdge(RBPC).Edge();
332 TopExp::Vertices(prLB, VFI, VFO, 1);
333 TopExp::Vertices(prRB, VLI, VLO, 1);
334 TopoDS_Edge prIL = BRepLib_MakeEdge(VFI, VLI).Edge();
335 TopoDS_Edge prOL = BRepLib_MakeEdge(VFO, VLO).Edge();
336 TopoDS_Wire prW = BRepLib_MakeWire(prLB, prIL, prOL, prRB).Wire();
337 outF = BRepBuilderAPI_MakeFace(refpl->Pln(), prW, 1).Face();
341 Boundr->Append(prLB);
342 Boundr->Append(prIL);
343 Boundr->Append(prOL);
344 Boundr->Append(prRB);
347 ShapeAnalysis_Wire WA(prW, outF, Precision::Confusion());
348 bool res = WA.CheckSelfIntersection();
352 void HYDROData_DTM::CreateProfiles(const std::vector<Handle(HYDROData_Profile)>& theProfiles,
354 double theSpatialStep,
355 AltitudePoints& theOutLeft,
356 AltitudePoints& theOutRight,
357 AltitudePoints& theOutPoints,
358 std::vector<AltitudePoints>& theOutMainProfiles,
359 TopoDS_Shape& Out3dPres,
360 TopoDS_Shape& Out2dPres,
361 TopoDS_Shape& OutLeftB,
362 TopoDS_Shape& OutRightB,
363 TopoDS_Shape& OutInlet,
364 TopoDS_Shape& OutOutlet,
367 std::set<int>& InvInd,
370 if (theProfiles.empty())
372 theOutPoints = Interpolate( theProfiles, theDDZ, theSpatialStep, theOutLeft, theOutRight, theOutMainProfiles, InvInd );
373 //note that if Create3dPres is false => Create2dPres flag is meaningless!
374 if( theOutPoints.empty() )
379 TopTools_IndexedMapOfOrientedShape ll = Create3DShape( theOutLeft, theOutRight, theOutMainProfiles);
385 BB.MakeCompound(cmp);
386 for (int i = 1; i <= ll.Extent(); i++)
391 //same order as in HYDROData_DTM::Update()
393 OutRightB = ll(ll.Extent());
395 OutOutlet = ll(ll.Extent() - 1);
400 ProjStat = GetPlanarFaceFromBanks(TopoDS::Edge(OutLeftB), TopoDS::Edge(OutRightB), outF, NULL);
409 void HYDROData_DTM::GetProperties( const Handle(HYDROData_Profile)& theProfile,
410 gp_Pnt& theLowestPoint, gp_Vec2d& theDir,
411 double& theZMin, double& theZMax )
413 theLowestPoint = theProfile->GetBottomPoint();
416 theProfile->GetLeftPoint( aLeft, true, true );
417 theProfile->GetRightPoint( aRight, true, true );
418 double x = aRight.X()-aLeft.X();
419 double y = aRight.Y()-aLeft.Y();
420 theDir = gp_Vec2d( x, y );
422 HYDROData_Profile::ProfilePoints points = theProfile->GetProfilePoints();
423 int lo = points.Lower();
424 int up = points.Upper();
425 theZMin = std::numeric_limits<double>::max();
427 for( int i=lo; i<=up; i++ )
429 double z = points.Value( i ).Z();
437 inline gp_Pnt2d To2D( const gp_Pnt& thePnt, const gp_Trsf& theTr,
438 double& theUMin, double& theUMax )
440 gp_Pnt p = thePnt.Transformed( theTr );
447 return gp_Pnt2d( u, z );
450 Handle(TColgp_HArray1OfPnt2d) To2D( const TColgp_Array1OfPnt& thePoints,
451 const gp_Trsf& theTr,
452 double& theUMin, double& theUMax )
454 int low = thePoints.Lower(), up = thePoints.Upper();
455 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( low, up );
456 for( int i=low; i<=up; i++ )
457 points->SetValue( i, To2D( thePoints.Value( i ), theTr, theUMin, theUMax ) );
461 Handle(Geom2d_Curve) CurveTo2D( const Handle(Geom_Curve)& theCurve,
462 Standard_Real theFirst, Standard_Real theLast,
463 const gp_Trsf& theTr,
464 double& theUMin, double& theUMax )
466 if( theCurve->IsKind( STANDARD_TYPE( Geom_Line ) ) )
468 gp_Pnt aFirstPnt, aLastPnt;
469 theCurve->D0( theFirst, aFirstPnt );
470 theCurve->D0( theLast, aLastPnt );
473 aFirst2d = To2D( aFirstPnt, theTr, theUMin, theUMax ),
474 aLast2d = To2D( aLastPnt, theTr, theUMin, theUMax );
476 gp_Vec2d dir( aFirst2d, aLast2d );
477 Handle(Geom2d_Line) aLine2d = new Geom2d_Line( aFirst2d, gp_Dir2d( dir.X(), dir.Y() ) );
478 return new Geom2d_TrimmedCurve( aLine2d, 0, aLast2d.Distance( aFirst2d ) );
481 if( theCurve->IsKind( STANDARD_TYPE( Geom_BSplineCurve ) ) )
483 Handle(Geom_BSplineCurve) aSpline = Handle(Geom_BSplineCurve)::DownCast( theCurve );
485 Handle(TColgp_HArray1OfPnt2d) poles = To2D( aSpline->Poles(), theTr, theUMin, theUMax );
486 const TColStd_Array1OfReal& knots = aSpline->Knots();
487 const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
488 int aDegree = aSpline->Degree();
490 return new Geom2d_BSplineCurve( poles->Array1(), knots, multiplicities, aDegree );
493 return Handle(Geom2d_Curve)();
496 #include <GCE2d_MakeSegment.hxx>
497 #include <Geom2dAPI_InterCurveCurve.hxx>
498 bool IsCooriented( const Handle(HYDROData_Profile)& theProfile1,
499 const Handle(HYDROData_Profile)& theProfile2 )
501 if( theProfile1==theProfile2 )
504 gp_XY lp1, rp1, lp2, rp2;
505 theProfile1->GetLeftPoint(lp1);
506 theProfile1->GetRightPoint(rp1);
507 theProfile2->GetLeftPoint(lp2);
508 theProfile2->GetRightPoint(rp2);
510 GCE2d_MakeSegment s1(lp1, lp2);
511 GCE2d_MakeSegment s2(rp1, rp2);
513 Geom2dAPI_InterCurveCurve inter;
514 inter.Init(s1.Value(), s2.Value());
515 if (inter.NbPoints() == 0)
521 Handle(Geom2d_BSplineCurve) HYDROData_DTM::CreateHydraulicAxis(
522 const std::vector<Handle(HYDROData_Profile)>& theProfiles,
523 std::vector<double>& theDistances )
525 size_t n = theProfiles.size();
527 return Handle_Geom2d_BSplineCurve();
529 Handle_Geom2d_BSplineCurve aResult;
531 Handle(TColgp_HArray1OfPnt2d) points = new TColgp_HArray1OfPnt2d( 1, (int)n );
532 TColgp_Array1OfVec2d tangents( 1, (int)n );
533 Handle(TColStd_HArray1OfBoolean) flags = new TColStd_HArray1OfBoolean( 1, (int)n );
535 // Stage 1. Orient all profiles to be co-oriented with the first profile
536 theProfiles[0]->Update();
537 for( size_t i = 1; i < n; i++ )
539 Handle(HYDROData_Profile) aProfile = theProfiles[i];
540 Handle(HYDROData_Profile) aPrevProfile = theProfiles[i-1];
542 if( !IsCooriented( aProfile, aPrevProfile ) )
545 aProfile->GetLeftPoint( lp, true );
546 aProfile->GetRightPoint( rp, true );
547 aProfile->SetLeftPoint( rp, true );
548 aProfile->SetRightPoint( lp, true );
553 // Stage 2. Calculate normals so that each normal "points" to the next profile
554 for( size_t i = 0; i < n; i++ )
556 Handle(HYDROData_Profile) aProfile = theProfiles[i];
557 Handle(HYDROData_Profile) aNextProfile = i==n-1 ? theProfiles[i-1] : theProfiles[i+1];
563 gp_XYZ curP = aProfile->GetBottomPoint(true);
564 gp_XY curP2d = gp_XY(curP.X(), curP.Y());
566 gp_XYZ nextP = aNextProfile->GetBottomPoint(true);
567 gp_XY nextP2d = gp_XY(nextP.X(), nextP.Y());
570 GetProperties( aProfile, aLowest, aPrTangent, zmin, zmax );
571 aNormal.SetCoord( -aPrTangent.Y(), aPrTangent.X() );
573 gp_Vec2d aDirToNextProfile(nextP2d.X() - curP2d.X(), nextP2d.Y() - curP2d.Y() );
575 aDirToNextProfile.Reverse();
577 if (aNormal.Dot(aDirToNextProfile) < 0)
582 points->SetValue( (int)(i+1), gp_Pnt2d( aLowest.X(), aLowest.Y() ) );
583 tangents.SetValue( (int)(i+1), aNormal );
584 flags->SetValue( (int)(i+1), Standard_True );
587 Geom2dAPI_Interpolate anInterpolator( points, Standard_False, Standard_False );
588 anInterpolator.Load( tangents, flags );
589 anInterpolator.Perform();
590 if( anInterpolator.IsDone() )
592 aResult = anInterpolator.Curve();
594 //fill the distances vector
595 Geom2dAdaptor_Curve anAdaptor( aResult );
597 theDistances.clear();
598 theDistances.reserve( n );
599 Standard_Real aParamFirst = anAdaptor.FirstParameter(), aParamLast = anAdaptor.LastParameter();
600 for( size_t i = 1; i <= n; i++ )
602 gp_Pnt2d aPnt = points->Value( (Standard_Integer)i );
603 Geom2dAPI_ProjectPointOnCurve aProject( aPnt, aResult );
604 Standard_Real aParam = aProject.LowerDistanceParameter();
605 double aDistance = GCPnts_AbscissaPoint::Length( anAdaptor, aParamFirst, aParam );
606 theDistances.push_back( aDistance );
612 std::vector<Handle(Geom2d_Curve)> HYDROData_DTM::ProfileToParametric(
613 const Handle(HYDROData_Profile)& theProfile,
614 double& theUMin, double& theUMax, gp_Vec2d& theDir )
616 std::vector<Handle(Geom2d_Curve)> curves;
618 // Transformation of the coordinate systems
621 GetProperties( theProfile, aLowest, theDir, zmin, zmax );
623 gp_Ax3 aStd3d( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) );
624 gp_Ax3 aLocal( gp_Pnt( aLowest.X(), aLowest.Y(), 0 ), gp_Dir( 0, 0, 1 ), gp_Dir( theDir.X(), theDir.Y(), 0 ) );
627 aTransf.SetTransformation( aStd3d, aLocal );
629 // Iteration via edges
630 TopoDS_Wire aWire = TopoDS::Wire( theProfile->GetShape3D() );
631 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
632 for( ; anExp.More(); anExp.Next() )
634 // Extract an edge from wire
635 TopoDS_Edge anEdge = TopoDS::Edge( anExp.Current() );
637 // Extract a curve corresponding to the edge
638 TopLoc_Location aLoc;
639 Standard_Real aFirst, aLast;
640 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aLoc, aFirst, aLast );
642 // Convert the curve to 2d CS
643 Handle(Geom2d_Curve) aCurve2d = CurveTo2D( aCurve, aFirst, aLast, aTransf, theUMin, theUMax );
644 if( !aCurve2d.IsNull() )
645 curves.push_back( aCurve2d );
651 bool CalcMidWidth( const std::set<double>& intersections, double& theMid, double& theWid )
653 double umin = std::numeric_limits<double>::max(),
656 size_t n = intersections.size();
660 std::set<double>::const_iterator it = intersections.begin(), last = intersections.end();
661 for( ; it!=last; it++ )
669 theMid = ( umin+umax )/2;
674 void HYDROData_DTM::ProfileDiscretization( const Handle(HYDROData_Profile)& theProfile,
675 double theXCurv, double theMinZ, double theMaxZ, double theDDZ,
676 CurveUZ& theMidPointCurve,
677 CurveUZ& theWidthCurve,
678 int& intersection_nb,
681 double aDblMax = std::numeric_limits<double>::max(),
686 gp_Vec2d aProfileDir;
687 std::vector<Handle(Geom2d_Curve)> curves = ProfileToParametric( theProfile, aUMin, aUMax, aProfileDir );
688 size_t n = curves.size();
693 // we add the "virtual" vertical lines to simulate the intersection with profile
694 gp_Pnt2d aFirst, aLast;
695 curves[0]->D0( curves[0]->FirstParameter(), aFirst );
696 curves[n-1]->D0( curves[n-1]->LastParameter(), aLast );
697 Handle(Geom2d_Line) aV1 = new Geom2d_Line( aFirst, gp_Dir2d( 0, 1 ) );
698 Handle(Geom2d_TrimmedCurve) aT1 = new Geom2d_TrimmedCurve( aV1, 0.0, aVMax );
700 Handle(Geom2d_Line) aV2 = new Geom2d_Line( aLast, gp_Dir2d( 0, 1 ) );
701 Handle(Geom2d_TrimmedCurve) aT2 = new Geom2d_TrimmedCurve( aV2, 0.0, aVMax );
703 curves.push_back( aT1 );
704 curves.push_back( aT2 );
706 int psize = ( int )( ( theMaxZ-theMinZ ) / theDDZ + 1 );
707 theMidPointCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
708 theMidPointCurve.reserve( psize );
709 theWidthCurve = CurveUZ( theXCurv, aProfileDir, theMinZ );
710 theWidthCurve.reserve( psize );
713 // for each discrete value of z we search intersection with profile
714 for( double z1 = theMinZ; z1 <= theMaxZ; z1 += theDDZ )
716 Handle(Geom2d_Line) aLine = new Geom2d_Line( gp_Pnt2d( 0, z1 ), gp_Dir2d( 1, 0 ) );
717 std::set<double> intersections;
718 for( size_t i = 0; i < n; i++ )
720 Handle(Geom2d_Curve) aCurve = curves[i];
721 Geom2dAPI_InterCurveCurve anIntersect( aCurve, aLine, theTolerance );
722 for( int k=1, m=anIntersect.NbPoints(); k<=m; k++ )
723 intersections.insert( anIntersect.Point( k ).X() );
726 intersection_nb = intersections.size();
727 if( intersection_nb >= 1 )
730 if( !CalcMidWidth( intersections, u_mid, u_wid ) )
733 double z = z1 - theMinZ;
737 theMidPointCurve.push_back( p_mid );
742 theWidthCurve.push_back( p_wid );
747 void HYDROData_DTM::Interpolate( const CurveUZ& theCurveA, const CurveUZ& theCurveB,
748 int theNbSteps, std::vector<CurveUZ>& theInterpolation,
751 theInterpolation.clear();
752 int d = isAddSecond ? 2 : 1;
753 theInterpolation.reserve( theNbSteps+d );
754 double dt = 1.0 / double( theNbSteps + 1 );
756 theInterpolation.push_back( theCurveA );
757 for( int i=0; i<theNbSteps; i++, t+=dt )
759 CurveUZ anInterp = theCurveA*(1-t) + theCurveB*t;
760 theInterpolation.push_back( anInterp );
763 theInterpolation.push_back( theCurveB );
765 #include <BRepLib_MakeEdge2d.hxx>
766 void HYDROData_DTM::CurveTo3D( const Handle(Geom2d_BSplineCurve)& theHydraulicAxis,
767 const CurveUZ& theMidCurve, const CurveUZ& theWidthCurve,
768 AltitudePoints& thePoints )
770 Geom2dAdaptor_Curve anAdaptor( theHydraulicAxis );
771 TopoDS_Edge E2d = BRepLib_MakeEdge2d(theHydraulicAxis).Edge();
772 GCPnts_AbscissaPoint ap( anAdaptor, theMidCurve.Xcurv(), anAdaptor.FirstParameter() );
773 double aParam = ap.Parameter();
776 anAdaptor.D0( aParam, point );
777 gp_Vec2d profile_dir = theMidCurve.ProfileDir();
778 //gp_Dir tangent_n( -profile_dir.Y(), profile_dir.X(), dz );
779 profile_dir.Normalize();
781 size_t n = theMidCurve.size();
782 std::map<double, AltitudePoint> sorted_points;
783 for( size_t i=0; i<n; i++ )
785 double param1 = theMidCurve[i].U - theWidthCurve[i].U / 2;
786 double param2 = theMidCurve[i].U + theWidthCurve[i].U / 2;
788 gp_Pnt2d p1 = point.Translated( param1 * profile_dir);
789 gp_Pnt2d p2 = point.Translated( param2 * profile_dir);
791 double z = theMidCurve[i].Z + theMidCurve.DeltaZ();
793 AltitudePoint p3d_1( p1.X(), p1.Y(), z ), p3d_2( p2.X(), p2.Y(), z );
795 sorted_points[param1] = p3d_1;
796 sorted_points[param2] = p3d_2;
799 thePoints.reserve( sorted_points.size() );
800 const double EPS = 1E-12;
801 std::map<double, AltitudePoint>::const_iterator it = sorted_points.begin(), last = sorted_points.end();
802 for( ; it!=last; it++ )
803 if( thePoints.empty() || thePoints.back().SquareDistance( it->second ) > EPS )
804 thePoints.push_back( it->second );
807 inline double max( double a, double b )
815 inline double min( double a, double b )
823 #include <BRepLib_MakeWire.hxx>
825 std::vector<HYDROData_Bathymetry::AltitudePoints> HYDROData_DTM::Interpolate
826 ( const Handle(Geom2d_BSplineCurve)& theHydraulicAxis,
827 const Handle(HYDROData_Profile)& theProfileA,
829 const Handle(HYDROData_Profile)& theProfileB,
831 double theDDZ, int theNbSteps, bool isAddSecond,
832 int& inter_nb_1, int& inter_nb_2)
834 double zminA, zmaxA, zminB, zmaxB;
835 gp_Pnt lowestA, lowestB;
838 GetProperties( theProfileA, lowestA, dirA, zminA, zmaxA );
839 GetProperties( theProfileB, lowestB, dirB, zminB, zmaxB );
842 double hmax = max( zmaxA-zminA, zmaxB-zminB );
844 //double dz = zminB - zminA;
845 //double zmin = min( zminA, zminB );
846 //double zmax = max( zmaxA, zmaxB );
848 CurveUZ midA(0, gp_Vec2d(), 0), midB(0, gp_Vec2d(), 0);
849 CurveUZ widA(0, gp_Vec2d(), 0), widB(0, gp_Vec2d(), 0);
851 ProfileDiscretization( theProfileA, theXCurvA, zminA, zminA+hmax, theDDZ, midA, widA, inter_nb_1 );
852 ProfileDiscretization( theProfileB, theXCurvB, zminB, zminB+hmax, theDDZ, midB, widB, inter_nb_2 );
854 std::vector<CurveUZ> mid, wid;
855 Interpolate( midA, midB, theNbSteps, mid, isAddSecond );
856 Interpolate( widA, widB, theNbSteps, wid, isAddSecond );
858 size_t p = mid.size();
859 size_t q = p>0 ? 2*mid[0].size() : 1;
860 std::vector<AltitudePoints> points;
863 for( size_t i=0; i<p; i++ )
865 points[i].reserve( q );
866 CurveTo3D( theHydraulicAxis, mid[i], wid[i], points[i] );
872 HYDROData_Bathymetry::AltitudePoints HYDROData_DTM::Interpolate
873 ( const std::vector<Handle(HYDROData_Profile)>& theProfiles,
874 double theDDZ, double theSpatialStep,
875 AltitudePoints& theLeft,
876 AltitudePoints& theRight,
877 std::vector<AltitudePoints>& theMainProfiles,
878 std::set<int>& invalInd)
880 AltitudePoints points;
881 size_t n = theProfiles.size();
885 std::vector<double> distances;
886 Handle(Geom2d_BSplineCurve) aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
887 if( aHydraulicAxis.IsNull() )
890 theMainProfiles.reserve( n );
892 for( size_t i=0, n1=n-1; i<n1; i++ )
894 double aDistance = distances[i+1]-distances[i];
895 int aNbSteps = int(aDistance/theSpatialStep);
896 bool isAddSecond = i==n1-1;
898 // 1. Calculate interpolated profiles
899 int inter_nb_1, inter_nb_2;
900 std::vector<AltitudePoints> local_points = Interpolate( aHydraulicAxis, theProfiles[i], distances[i],
901 theProfiles[i+1], distances[i+1], theDDZ, aNbSteps, isAddSecond, inter_nb_1, inter_nb_2 );
902 int lps = local_points.size();
908 invalInd.insert(i+1);
910 // 2. Put all points into the global container
911 for( size_t j=0; j<lps; j++ )
913 const AltitudePoints& lp = local_points[j];
915 points.reserve( lp.size() * n );
916 for( size_t k=0, ks=lp.size(); k<ks; k++ )
917 points.push_back( lp[k] );
920 // 3. Get left/right banks' points
923 theLeft.reserve( lps * n );
924 theRight.reserve( lps * n );
926 for( size_t j=0; j<lps; j++ )
928 const AltitudePoints& lp = local_points[j];
929 theLeft.push_back( lp[0] );
930 theRight.push_back( lp[lp.size()-1] );
933 // 4. Get main profiles points
934 theMainProfiles.push_back( local_points[0] );
936 theMainProfiles.push_back( local_points[lps-1] );
941 int HYDROData_DTM::EstimateNbPoints( const std::vector<Handle(HYDROData_Profile)>& theProfiles,
942 double theDDZ, double theSpatialStep )
944 size_t n = theProfiles.size();
947 if( theDDZ<1E-6 || theSpatialStep<1E-6 )
950 std::vector<double> distances;
951 Handle(Geom2d_BSplineCurve) aHydraulicAxis = CreateHydraulicAxis( theProfiles, distances );
952 if( aHydraulicAxis.IsNull() )
955 double aCompleteDistance = distances[n-1];
956 int aNbSteps = int( aCompleteDistance / theSpatialStep ) + 1;
960 GetProperties( theProfiles[0], aLowest, aDir, aZMin, aZMax );
961 int aNbZSteps = (aZMax-aZMin)/theDDZ;
963 if( aNbSteps > ( 1<<16 ) || aNbZSteps > ( 1<<16 ) )
966 return aNbSteps * aNbZSteps;