1 // Copyright (C) 2014-2015 EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 // Lesser General Public License for more details.
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include <HYDROData_Tool.h>
20 #include <HYDROData_ArtificialObject.h>
21 #include <HYDROData_Document.h>
22 #include <HYDROData_Entity.h>
23 #include <HYDROData_Iterator.h>
24 #include <HYDROData_NaturalObject.h>
25 #include <HYDROData_ShapesGroup.h>
26 #include <HYDROData_PolylineXY.h>
29 #include <QStringList>
30 #include <QTextStream>
32 #include <BRep_Tool.hxx>
33 #include <BRepAdaptor_Surface.hxx>
34 #include <BRepTopAdaptor_FClass2d.hxx>
36 #include <Geom_Curve.hxx>
38 #include <Quantity_Color.hxx>
39 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Wire.hxx>
48 #include <BRepTools.hxx>
49 #include <NCollection_Map.hxx>
50 #include <TopTools_ShapeMapHasher.hxx>
51 #include <BRep_Builder.hxx>
52 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
54 #include <NCollection_List.hxx>
55 #include <TopTools_ListIteratorOfListOfShape.hxx>
57 #include <BRepBuilderAPI_MakeFace.hxx>
59 #include <TopExp_Explorer.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
61 #include <TopTools_HSequenceOfShape.hxx>
63 #include <BRep_Builder.hxx>
64 #include <BRepAlgo_FaceRestrictor.hxx>
65 #include <BRepCheck_Analyzer.hxx>
67 #include <ShapeAnalysis.hxx>
68 #include <ShapeAnalysis_FreeBounds.hxx>
70 #include <HYDROData_PolylineXY.h>
71 #include <HYDROData_Polyline3D.h>
72 #include <HYDROData_Bathymetry.h>
73 #include <Geom_Line.hxx>
74 #include <Geom2d_BSplineCurve.hxx>
75 #include <Standard_Type.hxx>
76 #include <Geom2d_TrimmedCurve.hxx>
77 #include <Geom2d_Line.hxx>
78 #include <Geom_BSplineCurve.hxx>
80 #include <GeomAPI.hxx>
82 #include <Geom_Plane.hxx>
84 #define BLOCK_SIZE 10000
86 HYDROData_Tool::ExecStatus HYDROData_Tool::myTriangulationStatus = ExecStatus::None;
88 static int aMaxNameId = INT_MAX;
89 static int aMaxColorNb = 92000;
90 void HYDROData_Tool::WriteStringsToFile( QFile& theFile,
91 const QStringList& theStrings,
92 const QString& theSep )
94 if ( !theFile.isOpen() || theStrings.isEmpty() )
97 QString aWriteStr = theStrings.join( theSep );
98 if ( aWriteStr.isEmpty() )
101 QTextStream anOutStream( &theFile );
102 anOutStream << aWriteStr.toUtf8() << theSep << theSep;
105 bool HYDROData_Tool::ExtractGeneratedObjectName(const QString& theName, int& outValue, QString& thePrefName)
107 QStringList aLs = theName.split('_');
109 QString last = aLs.last();
110 outValue = last.toInt(&ok);
113 int last_len = last.length();
114 int total_len = theName.length();
115 thePrefName = theName.left(total_len-last_len-1);
119 QString HYDROData_Tool::GenerateObjectName( const Handle(HYDROData_Document)& theDoc,
120 const QString& thePrefix,
121 const QStringList& theUsedNames,
122 const bool theIsTryToUsePurePrefix )
124 QStringList aNamesList( theUsedNames );
126 // Collect all used names in the document
127 HYDROData_Iterator anIter( theDoc );
128 for( ; anIter.More(); anIter.Next() )
130 Handle(HYDROData_Entity) anObject = anIter.Current();
131 if( anObject.IsNull() )
134 QString anObjName = anObject->GetName();
135 if ( anObjName.isEmpty() )
138 aNamesList.append( anObjName );
143 if ( theIsTryToUsePurePrefix && !aNamesList.contains( thePrefix ) ) {
147 while( anId < aMaxNameId )
149 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
151 // check that there are no other objects with the same name in the document
152 if ( !aNamesList.contains( aName ) )
160 bool HYDROData_Tool::IsGeometryObject( const Handle(HYDROData_Entity)& theObject )
162 if ( theObject.IsNull() )
165 return theObject->IsKind( STANDARD_TYPE(HYDROData_ArtificialObject) ) ||
166 theObject->IsKind( STANDARD_TYPE(HYDROData_NaturalObject) );
169 void HYDROData_Tool::UpdateChildObjectName( const QString& theOldStr,
170 const QString& theNewStr,
171 const Handle(HYDROData_Entity)& theObject )
173 if ( theObject.IsNull() )
176 QString anObjName = theObject->GetName();
177 if ( theOldStr.isEmpty() )
179 while ( anObjName.startsWith( '_' ) )
180 anObjName.remove( 0, 1 );
182 anObjName.prepend( theNewStr + "_" );
184 else if ( anObjName.startsWith( theOldStr ) )
186 anObjName.replace( 0, theOldStr.length(), theNewStr );
191 theObject->SetName( anObjName );
194 QString HYDROData_Tool::GenerateNameForPython( const MapOfTreatedObjects& theTreatedObjects,
195 const QString& thePrefix )
197 QString aName = thePrefix;
198 if ( !theTreatedObjects.contains( aName ) )
202 while( anId < aMaxNameId )
204 aName = QString( "%1_%2" ).arg( thePrefix ).arg( QString::number( anId++ ) );
206 // check that there are no other objects with the same name
207 if ( !theTreatedObjects.contains( aName ) )
213 //======================================================================================================
214 TopAbs_State HYDROData_Tool::ComputePointState( const gp_XY& theXY, const TopoDS_Face& theFace )
216 TopAbs_State aState(TopAbs_UNKNOWN);
217 if(theFace.IsNull()) return aState;
218 Standard_Real aTol = BRep_Tool::Tolerance(theFace);
219 BRepAdaptor_Surface Ads ( theFace, Standard_False );
220 Standard_Real toluv = Min ( Ads.UResolution(aTol), Ads.VResolution(aTol) );
222 toluv = 0.01; // there is no need to be more precise than 1cm a any case !
223 // another solution could be to compute a tolerance related to the distance between the border nodes
224 const gp_Pln& aPlane = Ads.Surface().Plane();
225 gp_Pnt aPnt(theXY.X(), theXY.Y(), 0.);
226 Standard_Real aU1, aV1;
227 ElSLib::Parameters(aPlane,aPnt, aU1, aV1);
228 BRepTopAdaptor_FClass2d aClassifier( theFace, toluv );
229 aState = aClassifier.Perform( gp_Pnt2d(aU1, aV1), Standard_False );
233 double HYDROData_Tool::GetAltitudeForEdge( const TopoDS_Edge& theEdge,
234 const gp_XY& thePoint,
235 double theParameterTolerance,
236 double theSquareDistanceTolerance,
237 double theInvalidAltitude )
239 double aFirst, aLast;
240 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
241 if( aCurve.IsNull() )
242 return theInvalidAltitude;
244 gp_Pnt aFirstPnt, aLastPnt;
246 aCurve->D0( aFirst, aFirstPnt );
247 aCurve->D0( aLast, aLastPnt );
249 gp_Pnt2d aFirstPnt2d( aFirstPnt.X(), aFirstPnt.Y() );
250 gp_Pnt2d aLastPnt2d( aLastPnt.X(), aLastPnt.Y() );
252 double aFirstDist = 0;
253 double aLastDist = aFirstPnt2d.SquareDistance( aLastPnt2d );
254 double aNecDist = aFirstPnt2d.SquareDistance( thePoint );
256 while( fabs( aLast - aFirst ) > theParameterTolerance )
258 double aMid = ( aFirst + aLast ) / 2;
260 aCurve->D0( aMid, aMidPnt );
261 double aDist = aFirstPnt2d.SquareDistance( gp_Pnt2d( aMidPnt.X(), aMidPnt.Y() ) );
263 if( aDist < aNecDist )
269 double aMid = ( aFirst + aLast ) / 2;
271 aCurve->D0( aMid, aMidPnt );
273 gp_Pnt2d aMidPnt2d( aMidPnt.X(), aMidPnt.Y() );
274 if( aMidPnt2d.SquareDistance( thePoint ) < theSquareDistanceTolerance )
277 return theInvalidAltitude;
280 double HYDROData_Tool::GetAltitudeForWire( const TopoDS_Wire& theWire,
281 const gp_XY& thePoint,
282 double theParameterTolerance,
283 double theSquareDistanceTolerance,
284 double theInvalidAltitude )
286 TopExp_Explorer anExp( theWire, TopAbs_EDGE );
287 for( ; anExp.More(); anExp.Next() )
289 double anAltitude = GetAltitudeForEdge( TopoDS::Edge( anExp.Current() ), thePoint,
290 theParameterTolerance, theSquareDistanceTolerance, theInvalidAltitude );
291 if( anAltitude != theInvalidAltitude )
294 return theInvalidAltitude;
297 TopoDS_Shape HYDROData_Tool::getFirstShapeFromGroup( const HYDROData_SequenceOfObjects& theGroups,
298 const int theGroupId )
300 TopoDS_Shape aResShape;
301 if ( theGroupId < 1 || theGroupId > theGroups.Length() )
304 Handle(HYDROData_ShapesGroup) aGroup =
305 Handle(HYDROData_ShapesGroup)::DownCast( theGroups.Value( theGroupId ) );
306 if ( aGroup.IsNull() )
309 TopTools_SequenceOfShape aGroupShapes;
310 aGroup->GetShapes( aGroupShapes );
312 if ( !aGroupShapes.IsEmpty() )
313 aResShape = aGroupShapes.First();
318 TCollection_ExtendedString HYDROData_Tool::toExtString( const QString& theStr )
320 TCollection_ExtendedString aRes;
321 if( !theStr.isEmpty() )
323 Standard_ExtString extStr = new Standard_ExtCharacter[ ( theStr.length() + 1 ) * 2 ];
324 memcpy( (void*)extStr, theStr.unicode(), theStr.length() * 2 );
325 ((short*)extStr)[theStr.length()] = '\0';
326 aRes = TCollection_ExtendedString( extStr );
332 QString HYDROData_Tool::toQString( const TCollection_ExtendedString& theStr )
334 return QString( (QChar*)theStr.ToExtString(), theStr.Length() );
337 Quantity_Color HYDROData_Tool::toOccColor( const QColor& theColor )
339 double r = theColor.red() / 255.0;
340 double g = theColor.green() / 255.0;
341 double b = theColor.blue() / 255.0;
343 return Quantity_Color( r, g, b, Quantity_TOC_RGB );
346 QColor HYDROData_Tool::toQtColor( const Quantity_Color& theColor )
348 int r = 255 * theColor.Red();
349 int g = 255 * theColor.Green();
350 int b = 255 * theColor.Blue();
351 return QColor( r, g, b );
354 QColor HYDROData_Tool::GenerateRandColor()
356 float aHue = ( rand()%1000 ) * 0.001f;
359 aColor.setHsl( (int)(aHue*255.), 200, 128 );
360 int r = aColor.red();
361 int g = aColor.green();
362 int b = aColor.blue();
363 return ( aColor.isValid() ? aColor : Qt::darkBlue );
366 void HYDROData_Tool::GenerateRepeatableRandColors(int nbColorsToGen, QVector<QColor>& theColors)
368 for (int i = 1; i <= nbColorsToGen; i++)
369 theColors.append(HYDROData_Tool::GenerateRandColor());
372 bool HYDROData_Tool::GenerateNonRepeatableRandColors(int nbColorsToGen, QVector<QColor>& theColors)
374 if (nbColorsToGen > aMaxColorNb)
385 if (Codes.contains(Code))
387 aColor.setHsl( H, S, 128 );
388 if (aColor.isValid())
390 theColors.append(aColor);
393 } while (theColors.size() <= nbColorsToGen);
398 bool HYDROData_Tool::IsNan( double theValue )
401 return _isnan( theValue );
403 return isnan( theValue );
407 bool HYDROData_Tool::IsInf( double theValue )
410 return (!_finite( theValue ) );
412 return isinf( theValue );
416 static void MakeShellG(const NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>& FG,
420 NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>::Iterator itFG(FG);
423 //face nb > 1 => make shell
424 TopoDS_Shell outShell;
425 bb.MakeShell(outShell);
426 for (;itFG.More();itFG.Next())
427 bb.Add(outShell, itFG.Value());
430 else if (FG.Extent() == 1)
432 outSh = itFG.Value(); //one face
436 TopoDS_Shape HYDROData_Tool::RebuildCmp(const TopoDS_Shape& in)
438 TopTools_IndexedDataMapOfShapeListOfShape mE2LF;
439 TopExp::MapShapesAndAncestors(in, TopAbs_EDGE, TopAbs_FACE, mE2LF);
441 return TopoDS_Shape();
442 NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> dfm;
443 //TopExp::MapShapes(aFuseShape, TopAbs_FACE, dfm);
444 TopExp_Explorer expf(in, TopAbs_FACE);
445 for (;expf.More(); expf.Next())
446 dfm.Add(TopoDS::Face(expf.Current()));
448 int nbF = dfm.Extent();
449 TopExp_Explorer exp_f(in, TopAbs_FACE);
450 const TopoDS_Face& FF = TopoDS::Face(exp_f.Current());
451 NCollection_List<TopoDS_Face> CurrFS;
452 NCollection_List<TopoDS_Face> NeighFS;
453 NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> PrF;
455 NCollection_List<NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>> GL_F;
456 NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher> OneGr;
460 NCollection_List<TopoDS_Face>::Iterator it_currfs(CurrFS);
462 for (;it_currfs.More();it_currfs.Next())
464 const TopoDS_Face& CF = it_currfs.Value();
465 TopExp_Explorer exp_edge(CF, TopAbs_EDGE);
466 for (;exp_edge.More();exp_edge.Next())
468 const TopoDS_Shape& CE = exp_edge.Current();
469 const TopTools_ListOfShape& lsf = mE2LF.FindFromKey(CE);
470 TopTools_ListIteratorOfListOfShape ls_it(lsf); //always one face (since all faces are planar)
471 for (;ls_it.More();ls_it.Next())
473 const TopoDS_Face& F = TopoDS::Face(ls_it.Value());
476 if (!PrF.Contains(F))
487 if (NeighFS.IsEmpty())
496 NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>::Iterator itDm(dfm);
497 const TopoDS_Face& nsh = itDm.Key();
506 if (GL_F.Extent() > 1)
509 NCollection_List<NCollection_Map<TopoDS_Face, TopTools_ShapeMapHasher>>::Iterator itGL_F(GL_F);
511 bb.MakeCompound(cmp);
512 for (;itGL_F.More();itGL_F.Next())
514 MakeShellG(itGL_F.Value(), sh);
520 else if (GL_F.Extent() == 1)
522 MakeShellG(GL_F.First(), sh);
528 TopoDS_Shape HYDROData_Tool::PolyXY2Face( const Handle(HYDROData_PolylineXY)& aPolyline )
530 //DEBTRACE("generateTopShape");
531 TopoDS_Face aResultFace = TopoDS_Face(); // --- result: default = null face
533 if (!aPolyline.IsNull())
535 TopoDS_Shape aPolylineShape = aPolyline->GetShape();
537 std::string brepName = "imz.brep";
538 BRepTools::Write(aPolylineShape, brepName.c_str());
540 TopTools_ListOfShape aWiresList;
542 if (!aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE)
544 // --- only one wire: try to make a face
545 //DEBTRACE("one wire: try to build a face");
546 const TopoDS_Wire& aPolylineWire = TopoDS::Wire(aPolylineShape);
547 if (!aPolylineWire.IsNull())
549 BRepBuilderAPI_MakeFace aMakeFace(aPolylineWire, Standard_True);
551 if (aMakeFace.IsDone())
553 //DEBTRACE(" a face with the only wire given");
554 aResultFace = aMakeFace.Face();
560 // --- a list of wires ? inventory of wires and edges
561 Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
562 Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
563 TopExp_Explorer anExp(aPolylineShape, TopAbs_WIRE);
564 //DEBTRACE("list of wires ?");
565 for (; anExp.More(); anExp.Next())
567 if (!anExp.Current().IsNull())
569 const TopoDS_Wire& aWire = TopoDS::Wire(anExp.Current());
570 aWiresList.Append(aWire);
571 //DEBTRACE(" append wire");
572 TopExp_Explorer it2(aWire, TopAbs_EDGE);
573 for (; it2.More(); it2.Next())
574 aSeqEdges->Append(it2.Current());
577 if (aWiresList.IsEmpty())
578 return aResultFace; // --- no wires: null result
580 if (aSeqEdges->Length() > 1)
582 //DEBTRACE("try to connect all the edges together, build a unique wire and a face");
583 // --- try to create one wire by connecting edges with a distance tolerance (no necessity of sharing points)
584 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
586 if (aSeqWires->Length() == 1)
588 // --- one wire: try to make a face
589 const TopoDS_Wire& aPolylineWire = TopoDS::Wire(aSeqWires->Value(1));
590 if (!aPolylineWire.IsNull())
592 BRepBuilderAPI_MakeFace aMakeFace(aPolylineWire, Standard_True);
594 if (aMakeFace.IsDone())
596 //DEBTRACE(" a face from all the wires connected");
597 aResultFace = aMakeFace.Face();
603 if (aResultFace.IsNull())
605 //DEBTRACE("try to make a face with the first wire of the list and other wires as restrictions");
606 // --- try to make a face with the first wire of the list and other wires as restrictions
607 BRepAlgo_FaceRestrictor aFR;
608 TopoDS_Face aRefFace;
609 TopoDS_Shape aS = aWiresList.First();
610 BRepBuilderAPI_MakeFace aMakeFace(TopoDS::Wire(aWiresList.First()), Standard_True);
612 if (aMakeFace.IsDone())
614 //DEBTRACE(" a face with first wire");
615 aRefFace = aMakeFace.Face();
617 if (!aRefFace.IsNull())
619 aFR.Init(aRefFace, Standard_False, Standard_True);
620 TopTools_ListIteratorOfListOfShape anIt(aWiresList);
621 for (; anIt.More(); anIt.Next())
623 TopoDS_Wire& aWire = TopoDS::Wire(anIt.Value());
631 for (; aFR.More(); aFR.Next())
633 //DEBTRACE(" a restricted face");
634 aResultFace = aFR.Current();
643 if (aResultFace.IsNull())
646 //DEBTRACE("check the face");
647 BRepCheck_Analyzer anAnalyzer(aResultFace);
648 if (anAnalyzer.IsValid() && aResultFace.ShapeType() == TopAbs_FACE)
650 //DEBTRACE("face OK");
655 //DEBTRACE("bad face");
657 return TopoDS_Face();
660 void HYDROData_Tool::SetSIProgress(const Handle(Message_ProgressIndicator)& thePI)
662 StricklerInterpolationProgress() = thePI->Start();
665 const Message_ProgressRange& HYDROData_Tool::GetSIProgress()
667 return StricklerInterpolationProgress();
670 Message_ProgressRange& HYDROData_Tool::StricklerInterpolationProgress()
672 static Message_ProgressRange aPI = Message_ProgressRange();
676 void HYDROData_Tool::SetZIProgress(const Handle(Message_ProgressIndicator)& thePI)
678 BathymetryInterpolationProgress() = thePI->Start();
681 const Message_ProgressRange& HYDROData_Tool::GetZIProgress()
683 return BathymetryInterpolationProgress();
686 Message_ProgressRange& HYDROData_Tool::BathymetryInterpolationProgress()
688 static Message_ProgressRange aPI = Message_ProgressRange();
692 void HYDROData_Tool::SetTriangulationStatus(const ExecStatus& theStatus)
694 myTriangulationStatus = theStatus;
697 const HYDROData_Tool::ExecStatus& HYDROData_Tool::GetTriangulationStatus()
699 return myTriangulationStatus;
702 static bool AddXYZ(bool bImportXY,
706 std::vector<gp_XYZ>& thePointsXYZ,
707 std::vector<gp_XY>& thePointsXY)
711 if ( HYDROData_Tool::IsNan( x ) || HYDROData_Tool::IsInf( x ) ||
712 HYDROData_Tool::IsNan( y ) || HYDROData_Tool::IsInf( y ) ||
713 HYDROData_Tool::IsNan( z ) || HYDROData_Tool::IsInf( z ) )
716 if( thePointsXYZ.size()>=thePointsXYZ.capacity() )
717 thePointsXYZ.reserve( thePointsXYZ.size()+BLOCK_SIZE );
719 thePointsXYZ.push_back(gp_XYZ(x,y,z));
723 if ( HYDROData_Tool::IsNan( x ) || HYDROData_Tool::IsInf( x ) ||
724 HYDROData_Tool::IsNan( y ) || HYDROData_Tool::IsInf( y ) )
727 if( thePointsXY.size()>=thePointsXY.capacity() )
728 thePointsXY.reserve( thePointsXY.size()+BLOCK_SIZE );
730 thePointsXY.push_back(gp_XY(x,y));
735 bool HYDROData_Tool::importFromXYZ( QString& theFileName,
737 std::vector<gp_XYZ>& thePointsXYZ,
738 std::vector<gp_XY>& thePointsXY)
740 QFile aFile( theFileName );
741 if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) )
744 QString aFileSuf = QFileInfo( aFile ).suffix().toLower();
747 if ( aFileSuf == "xyz" )
750 while ( !aFile.atEnd() )
752 std::string aLine = aFile.readLine().simplified().toStdString();
760 if( sscanf( aLine.c_str(), "%lf %lf %lf", &x, &y, &z )!=3 )
766 if (!AddXYZ(bImportXY, x, y, z, thePointsXYZ, thePointsXY ))
773 else if (aFileSuf == "xy" )
775 while ( !aFile.atEnd() )
777 std::string aLine = aFile.readLine().simplified().toStdString();
784 if( sscanf( aLine.c_str(), "%lf %lf", &x, &y )!=2 )
790 if (!AddXYZ(true, x, y, 0, thePointsXYZ, thePointsXY ))
803 bool HYDROData_Tool::importPolylineFromXYZ(QString aFileName, Handle(HYDROData_Document) theDocument,
804 bool importXY, NCollection_Sequence<Handle(HYDROData_Entity)>& importedEntities)
808 std::vector<gp_XY> aPoints2d;
809 std::vector<gp_XYZ> aDPoints3d;
811 if (HYDROData_Tool::importFromXYZ(aFileName, importXY, aDPoints3d, aPoints2d))
813 QString basename = QFileInfo( aFileName ).baseName();
815 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
816 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
817 bool IsClosed = false;
818 if ((aPoints2d.front()-aPoints2d.back()).Modulus()<Precision::Confusion())
821 aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, true);
824 aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, false);
826 int n = aPoints2d.size();
830 for ( int i = 0; i < n; i++ )
832 gp_XY aSectPoint = aPoints2d[i];
833 theDocument->Transform(aSectPoint, true);
834 aPolylineXY->AddPoint( 0, aSectPoint );
837 aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
838 aPolylineXY->SetName( basename + "_PolyXY_" );
839 aPolylineXY->Update();
840 importedEntities.Append(aPolylineXY);
848 std::vector<gp_XY> aDPoints2d;
849 std::vector<gp_XYZ> aPoints3d;
850 if (HYDROData_Tool::importFromXYZ(aFileName, false, aPoints3d, aDPoints2d))
852 QString basename = QFileInfo( aFileName ).baseName();
853 Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) );
854 Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) );
855 Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) );
856 HYDROData_Bathymetry::AltitudePoints aAPoints;
857 HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE;
858 bool IsClosed = false;
859 if ((aPoints3d.front()-aPoints3d.back()).Modulus()<Precision::Confusion())
862 aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, true);
865 aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, false);
867 int n = aPoints3d.size();
871 for ( int i = 0; i < n; i++ )
873 gp_XY aSectPoint(aPoints3d[i].X(), aPoints3d[i].Y());
874 theDocument->Transform(aSectPoint, true);
875 aPolylineXY->AddPoint( 0, aSectPoint );
876 HYDROData_Bathymetry::AltitudePoint p;
877 p.X = aSectPoint.X();
878 p.Y = aSectPoint.Y();
879 p.Z = aPoints3d[i].Z();
880 aAPoints.push_back(p);
883 QString aBathName = basename + "_bath_";
884 QString aPolyXYName = basename + "_polyXY_";
885 QString aPoly3DName = basename + "_poly3D_";
887 aPolylineXY->SetName( aPolyXYName );
888 aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor());
889 aPolylineXY->Update();
891 aBath->SetAltitudePoints(aAPoints);
892 aBath->SetName( aBathName );
894 aPolylineObj->SetPolylineXY (aPolylineXY, false);
895 aPolylineObj->SetAltitudeObject(aBath);
897 aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() );
898 aPolylineObj->SetName( aPoly3DName );
900 aPolylineObj->Update();
901 importedEntities.Append(aPolylineXY);
902 importedEntities.Append(aPolylineObj);
913 Handle(Geom2d_Curve) HYDROData_Tool::BRepAdaptorTo2DCurve( const BRepAdaptor_Curve& ad )
915 if( ad.GetType() == GeomAbs_Line)
917 double f = ad.FirstParameter();
918 double l = ad.LastParameter();
919 return new Geom2d_TrimmedCurve( GeomAPI::To2d(ad.Curve().Curve(), Geom_Plane(gp::XOY()).Pln()), f, l );
922 if( ad.GetType() == GeomAbs_BSplineCurve )
924 Handle(Geom_BSplineCurve) aSpline = ad.Curve().BSpline();
925 if (aSpline.IsNull())
926 return Handle(Geom2d_Curve)();
928 return GeomAPI::To2d(aSpline, gp_Pln(gp::XOY()));
930 //TColgp_Array1OfPnt poles3d = aSpline->Poles();
931 //TColgp_HArray1OfPnt2d poles2d(poles3d.Lower(), poles3d.Upper());
932 //for (int i=poles3d.Lower(); i<=poles3d.Upper();i++)
934 // gp_XY p2d(poles3d(i).X(), poles3d(i).Y());
935 // poles2d(i).SetXY(p2d);
937 //const TColStd_Array1OfReal& knots = aSpline->Knots();
938 //const TColStd_Array1OfInteger& multiplicities = aSpline->Multiplicities();
939 //int aDegree = aSpline->Degree();
940 //return new Geom2d_BSplineCurve( poles2d, knots, multiplicities, aDegree );
942 return Handle(Geom2d_Curve)();
945 std::ostream& operator<<( std::ostream& theStream, const QString& theText )
947 theStream << theText.toStdString();
951 std::ostream& operator<<( std::ostream& theStream, const QColor& theColor )
953 theStream << "[" << theColor.red() << ", " << theColor.green() << ", " << theColor.blue() << "]";
957 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Shape& theShape )
959 theStream << "[" << theShape.TShape().operator->() << "]";
963 std::ostream& operator<<( std::ostream& theStream, const TopoDS_Face& theFace )
965 theStream << "[" << theFace.TShape().operator->() << "]";
969 std::ostream& operator<<( std::ostream& theStream, const gp_XY& theXY )
971 theStream << "(" << theXY.X() << "; " << theXY.Y() << ")";
975 bool operator == ( const gp_XY& thePoint1, const gp_XY& thePoint2 )
977 const double EPS = 1E-3;
979 fabs( thePoint1.X() - thePoint2.X() ) < EPS &&
980 fabs( thePoint1.Y() - thePoint2.Y() ) < EPS;