From: isn Date: Wed, 12 Dec 2018 12:53:23 +0000 (+0300) Subject: lot 6 :: import polylines form XY/XYZ + tests X-Git-Tag: HYDRO_V2_0_0^2 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=4a4a927ca9e3a323e57ac9a6b1877b2d7a2e9e63;p=modules%2Fhydro.git lot 6 :: import polylines form XY/XYZ + tests --- diff --git a/src/HYDROData/HYDROData_Tool.cxx b/src/HYDROData/HYDROData_Tool.cxx index f5f8d9e7..9ad6eccc 100755 --- a/src/HYDROData/HYDROData_Tool.cxx +++ b/src/HYDROData/HYDROData_Tool.cxx @@ -67,6 +67,12 @@ #include #include +#include +#include +#include + +#include +#define BLOCK_SIZE 10000 HYDROData_Tool::ExecStatus HYDROData_Tool::myTriangulationStatus = ExecStatus::None; @@ -684,6 +690,203 @@ const HYDROData_Tool::ExecStatus& HYDROData_Tool::GetTriangulationStatus() return myTriangulationStatus; } +static bool AddXYZ(bool bImportXY, + double x, + double y, + double z, + std::vector& thePointsXYZ, + std::vector& thePointsXY) +{ + if (!bImportXY) + { + if ( HYDROData_Tool::IsNan( x ) || HYDROData_Tool::IsInf( x ) || + HYDROData_Tool::IsNan( y ) || HYDROData_Tool::IsInf( y ) || + HYDROData_Tool::IsNan( z ) || HYDROData_Tool::IsInf( z ) ) + return false; + + if( thePointsXYZ.size()>=thePointsXYZ.capacity() ) + thePointsXYZ.reserve( thePointsXYZ.size()+BLOCK_SIZE ); + + thePointsXYZ.push_back(gp_XYZ(x,y,z)); + } + else + { + if ( HYDROData_Tool::IsNan( x ) || HYDROData_Tool::IsInf( x ) || + HYDROData_Tool::IsNan( y ) || HYDROData_Tool::IsInf( y ) ) + return false; + + if( thePointsXY.size()>=thePointsXY.capacity() ) + thePointsXY.reserve( thePointsXY.size()+BLOCK_SIZE ); + + thePointsXY.push_back(gp_XY(x,y)); + } + return true; +} + +bool HYDROData_Tool::importFromXYZ( QString& theFileName, + bool bImportXY, + std::vector& thePointsXYZ, + std::vector& thePointsXY) +{ + QFile aFile( theFileName ); + if ( !aFile.exists() || !aFile.open( QIODevice::ReadOnly ) ) + return false; + + QString aFileSuf = QFileInfo( aFile ).suffix().toLower(); + + double x,y; + if ( aFileSuf == "xyz" ) + { + double z; + while ( !aFile.atEnd() ) + { + std::string aLine = aFile.readLine().simplified().toStdString(); + if ( aLine.empty() ) + continue; + + x = 0; + y = 0; + z = 0; + + if( sscanf( aLine.c_str(), "%lf %lf %lf", &x, &y, &z )!=3 ) + return false; + + if (!AddXYZ(bImportXY, x, y, z, thePointsXYZ, thePointsXY )) + return false; + } + } + else if (aFileSuf == "xy" ) + { + while ( !aFile.atEnd() ) + { + std::string aLine = aFile.readLine().simplified().toStdString(); + if ( aLine.empty() ) + continue; + + x = 0; + y = 0; + + if( sscanf( aLine.c_str(), "%lf %lf", &x, &y )!=2 ) + return false; + + if (!AddXYZ(true, x, y, 0, thePointsXYZ, thePointsXY )) + return false; + } + } + + aFile.close(); + + return true; +} + +bool HYDROData_Tool::importPolylineFromXYZ(QString aFileName, Handle(HYDROData_Document) theDocument, + bool importXY, NCollection_Sequence& importedEntities) +{ + if (importXY) + { + std::vector aPoints2d; + std::vector aDPoints3d; + + if (HYDROData_Tool::importFromXYZ(aFileName, importXY, aDPoints3d, aPoints2d)) + { + QString basename = QFileInfo( aFileName ).baseName(); + + Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) ); + HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; + bool IsClosed = false; + if ((aPoints2d.front()-aPoints2d.back()).Modulus()AddSection( TCollection_AsciiString("poly_section"), aSectType, true); + } + else + aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, false); + + int n = aPoints2d.size(); + if (IsClosed) + n--; + + for ( int i = 0; i < n; i++ ) + { + gp_XY aSectPoint = aPoints2d[i]; + theDocument->Transform(aSectPoint, true); + aPolylineXY->AddPoint( 0, aSectPoint ); + } + + aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() ); + aPolylineXY->SetName( basename + "_PolyXY_" ); + aPolylineXY->Update(); + importedEntities.Append(aPolylineXY); + return true; + } + else + return false; + } + else //xyz + { + std::vector aDPoints2d; + std::vector aPoints3d; + if (HYDROData_Tool::importFromXYZ(aFileName, false, aPoints3d, aDPoints2d)) + { + QString basename = QFileInfo( aFileName ).baseName(); + Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast( theDocument->CreateObject( KIND_POLYLINEXY ) ); + Handle(HYDROData_Polyline3D) aPolylineObj = Handle(HYDROData_Polyline3D)::DownCast( theDocument->CreateObject( KIND_POLYLINE ) ); + Handle(HYDROData_Bathymetry) aBath = Handle(HYDROData_Bathymetry)::DownCast( theDocument->CreateObject( KIND_BATHYMETRY ) ); + HYDROData_Bathymetry::AltitudePoints aAPoints; + HYDROData_PolylineXY::SectionType aSectType = HYDROData_PolylineXY::SECTION_POLYLINE; + bool IsClosed = false; + if ((aPoints3d.front()-aPoints3d.back()).Modulus()AddSection( TCollection_AsciiString("poly_section"), aSectType, true); + } + else + aPolylineXY->AddSection( TCollection_AsciiString("poly_section"), aSectType, false); + + int n = aPoints3d.size(); + if (IsClosed) + n--; + + for ( int i = 0; i < n; i++ ) + { + gp_XY aSectPoint(aPoints3d[i].X(), aPoints3d[i].Y()); + theDocument->Transform(aSectPoint, true); + aPolylineXY->AddPoint( 0, aSectPoint ); + HYDROData_Bathymetry::AltitudePoint p; + p.X = aSectPoint.X(); + p.Y = aSectPoint.Y(); + p.Z = aPoints3d[i].Z(); + aAPoints.push_back(p); + } + + QString aBathName = basename + "_bath_"; + QString aPolyXYName = basename + "_polyXY_"; + QString aPoly3DName = basename + "_poly3D_"; + + aPolylineXY->SetName( aPolyXYName ); + aPolylineXY->SetWireColor(HYDROData_PolylineXY::DefaultWireColor()); + aPolylineXY->Update(); + + aBath->SetAltitudePoints(aAPoints); + aBath->SetName( aBathName ); + + aPolylineObj->SetPolylineXY (aPolylineXY, false); + aPolylineObj->SetAltitudeObject(aBath); + + aPolylineObj->SetBorderColor( aPolylineObj->DefaultBorderColor() ); + aPolylineObj->SetName( aPoly3DName ); + + aPolylineObj->Update(); + importedEntities.Append(aPolylineXY); + importedEntities.Append(aPolylineObj); + return true; + } + else + return false; + } +} + + std::ostream& operator<<( std::ostream& theStream, const QString& theText ) { theStream << theText.toStdString(); diff --git a/src/HYDROData/HYDROData_Tool.h b/src/HYDROData/HYDROData_Tool.h index 0047995a..c7bd4fe6 100755 --- a/src/HYDROData/HYDROData_Tool.h +++ b/src/HYDROData/HYDROData_Tool.h @@ -154,6 +154,13 @@ public: static TopoDS_Shape PolyXY2Face(const Handle(HYDROData_PolylineXY)& aPolyline); + static bool importFromXYZ( QString& theFileName, bool bImportXY, std::vector& thePointsXYZ, + std::vector& thePointsXY); + + static bool importPolylineFromXYZ(QString aFilename, Handle(HYDROData_Document) theDocument, + bool importXY, NCollection_Sequence& importedEntities); + + static void SetSIProgress(const Handle(Message_ProgressIndicator)& thePI); static const Handle(Message_ProgressIndicator)& GetSIProgress(); diff --git a/src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx b/src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx index bc83c0f2..6697b420 100644 --- a/src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx @@ -28,6 +28,7 @@ #include #include #include +#include #include @@ -39,6 +40,7 @@ #include #include #include +#include HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule ) @@ -77,7 +79,16 @@ NCollection_Sequence HYDROGUI_ImportPolylineOp::Import continue; QString anExt = aFileName.split('.', QString::SkipEmptyParts).back(); - + anExt.toLower(); + bool importXY = false; + if (anExt == "xyz") + { + importXY = SUIT_MessageBox::question( module->getApp()->desktop(), + tr( "IMPORT_POLYLINE" ), + tr( "IMPORT_POLYLINE_XY_PART_ONLY" ), + QMessageBox::Yes | QMessageBox::No, + SUIT_MessageBox::Yes) == SUIT_MessageBox::Yes; + } if (anExt == "shp") { HYDROData_ShapeFile anImporter; @@ -186,6 +197,14 @@ NCollection_Sequence HYDROGUI_ImportPolylineOp::Import } importedEntities.Append(theEntities); } + else if ( anExt == "xy" || (importXY && anExt == "xyz")) + { + HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities); + } + else if (anExt == "xyz") + { + HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, false, importedEntities); + } } return importedEntities; } diff --git a/src/HYDROGUI/resources/HYDROGUI_msg_en.ts b/src/HYDROGUI/resources/HYDROGUI_msg_en.ts index 0f46924b..3b654ee6 100755 --- a/src/HYDROGUI/resources/HYDROGUI_msg_en.ts +++ b/src/HYDROGUI/resources/HYDROGUI_msg_en.ts @@ -2554,7 +2554,11 @@ file cannot be correctly imported for an Obstacle definition. POLYLINE_FILTER - Shape files (*.shp) + Shape files (*.shp);;XY files(*.xy);;XYZ files(*.xyz) + + + IMPORT_POLYLINE_XY_PART_ONLY + Do you wish to import the XY part only? (Click 'Yes' to import as polylineXY and 'No' to import as Polyline3D) BAD_IMPORTED_POLYLINE_FILES_TLT diff --git a/src/HYDRO_tests/reference_data/CMakeLists.txt b/src/HYDRO_tests/reference_data/CMakeLists.txt index 6732da78..cb701d8b 100644 --- a/src/HYDRO_tests/reference_data/CMakeLists.txt +++ b/src/HYDRO_tests/reference_data/CMakeLists.txt @@ -151,6 +151,9 @@ SET(REFERENCE_DATA bathy_prs_fit_selected.png zone_random_colors.png diff_image.png + Polyline_import_XY.png + Polyline_import_XYZ.png + Polyline_import_XYZ_3D.png ) # Application tests diff --git a/src/HYDRO_tests/reference_data/linux/Polyline_import_XY.png b/src/HYDRO_tests/reference_data/linux/Polyline_import_XY.png new file mode 100644 index 00000000..c71b5b64 Binary files /dev/null and b/src/HYDRO_tests/reference_data/linux/Polyline_import_XY.png differ diff --git a/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ.png b/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ.png new file mode 100644 index 00000000..c71b5b64 Binary files /dev/null and b/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ.png differ diff --git a/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ_3D.png b/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ_3D.png new file mode 100644 index 00000000..c71b5b64 Binary files /dev/null and b/src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ_3D.png differ diff --git a/src/HYDRO_tests/reference_data/win32/Polyline_import_XY.png b/src/HYDRO_tests/reference_data/win32/Polyline_import_XY.png new file mode 100644 index 00000000..009410d5 Binary files /dev/null and b/src/HYDRO_tests/reference_data/win32/Polyline_import_XY.png differ diff --git a/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ.png b/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ.png new file mode 100644 index 00000000..009410d5 Binary files /dev/null and b/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ.png differ diff --git a/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ_3D.png b/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ_3D.png new file mode 100644 index 00000000..009410d5 Binary files /dev/null and b/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ_3D.png differ diff --git a/src/HYDRO_tests/test_HYDROData_PolylineXY.cxx b/src/HYDRO_tests/test_HYDROData_PolylineXY.cxx index 7ed3645f..b804b1ab 100644 --- a/src/HYDRO_tests/test_HYDROData_PolylineXY.cxx +++ b/src/HYDRO_tests/test_HYDROData_PolylineXY.cxx @@ -46,6 +46,9 @@ #include #include #include +#include + +extern QString REF_DATA_PATH; void test_HYDROData_PolylineXY::test_polyline() { @@ -539,3 +542,33 @@ void test_HYDROData_PolylineXY::test_split_straight_refs_634() aDoc->Close(); } + +void test_HYDROData_PolylineXY::test_import_from_xyz() +{ + NCollection_Sequence ents; + Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 ); + TCollection_AsciiString fname = REF_DATA_PATH.toLatin1().data(); + fname += "/profiles1.xyz"; + + NCollection_Sequence importedEntities; + bool stat = HYDROData_Tool::importPolylineFromXYZ(QString(fname.ToCString()), aDoc, true, importedEntities); + CPPUNIT_ASSERT (stat); + Handle(HYDROData_PolylineXY) aPolyXY = Handle(HYDROData_PolylineXY)::DownCast(importedEntities.First()); + CPPUNIT_ASSERT_EQUAL (importedEntities.Size(), 1); + CPPUNIT_ASSERT (!aPolyXY.IsNull()); + TestViewer::show( aPolyXY->GetShape(), 0, true, "Polyline_import_XY" ); + CPPUNIT_ASSERT_IMAGES + importedEntities.Clear(); + + stat = HYDROData_Tool::importPolylineFromXYZ(QString(fname.ToCString()), aDoc, false, importedEntities); + CPPUNIT_ASSERT (stat); + CPPUNIT_ASSERT_EQUAL (importedEntities.Size(), 2); + Handle(HYDROData_Polyline3D) aPoly3D; + aPolyXY = Handle(HYDROData_PolylineXY)::DownCast(importedEntities.First()); + aPoly3D = Handle(HYDROData_Polyline3D)::DownCast(importedEntities.Last()); + CPPUNIT_ASSERT (!aPolyXY.IsNull()); + CPPUNIT_ASSERT (!aPoly3D.IsNull()); + TestViewer::show( aPolyXY->GetShape(), 0, false, "Polyline_import_XYZ" ); + TestViewer::show( aPoly3D->GetShape3D(), 0, false, "Polyline_import_XYZ_3D" ); + CPPUNIT_ASSERT_IMAGES +} \ No newline at end of file diff --git a/src/HYDRO_tests/test_HYDROData_PolylineXY.h b/src/HYDRO_tests/test_HYDROData_PolylineXY.h index d51b20f5..b03ac734 100644 --- a/src/HYDRO_tests/test_HYDROData_PolylineXY.h +++ b/src/HYDRO_tests/test_HYDROData_PolylineXY.h @@ -30,6 +30,8 @@ class test_HYDROData_PolylineXY : public CppUnit::TestFixture { CPPUNIT_TEST( test_custom_polylines ); CPPUNIT_TEST( test_merge_refs_630 ); CPPUNIT_TEST( test_split_straight_refs_634 ); + CPPUNIT_TEST( test_import_from_xyz ); + CPPUNIT_TEST_SUITE_END(); private: @@ -55,6 +57,8 @@ public: void test_custom_polylines(); void test_merge_refs_630(); void test_split_straight_refs_634(); + void test_import_from_xyz(); + }; CPPUNIT_TEST_SUITE_REGISTRATION(test_HYDROData_PolylineXY);