]> SALOME platform Git repositories - modules/hydro.git/commitdiff
Salome HOME
lot 6 :: import polylines form XY/XYZ + tests
authorisn <isn@opencascade.com>
Wed, 12 Dec 2018 12:53:23 +0000 (15:53 +0300)
committerisn <isn@opencascade.com>
Wed, 12 Dec 2018 13:38:59 +0000 (16:38 +0300)
13 files changed:
src/HYDROData/HYDROData_Tool.cxx
src/HYDROData/HYDROData_Tool.h
src/HYDROGUI/HYDROGUI_ImportPolylineOp.cxx
src/HYDROGUI/resources/HYDROGUI_msg_en.ts
src/HYDRO_tests/reference_data/CMakeLists.txt
src/HYDRO_tests/reference_data/linux/Polyline_import_XY.png [new file with mode: 0644]
src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ.png [new file with mode: 0644]
src/HYDRO_tests/reference_data/linux/Polyline_import_XYZ_3D.png [new file with mode: 0644]
src/HYDRO_tests/reference_data/win32/Polyline_import_XY.png [new file with mode: 0644]
src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ.png [new file with mode: 0644]
src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ_3D.png [new file with mode: 0644]
src/HYDRO_tests/test_HYDROData_PolylineXY.cxx
src/HYDRO_tests/test_HYDROData_PolylineXY.h

index f5f8d9e77df9fe165d036381260ac7b53ea84f9b..9ad6eccc7fcd662ab8365db5bc0ee51f1e6ee4c5 100755 (executable)
 #include <ShapeAnalysis.hxx>
 #include <ShapeAnalysis_FreeBounds.hxx>
 
+#include <HYDROData_PolylineXY.h>
+#include <HYDROData_Polyline3D.h>
+#include <HYDROData_Bathymetry.h>
+
+#include <QFileInfo>
+#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<gp_XYZ>& thePointsXYZ,
+                   std::vector<gp_XY>& 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<gp_XYZ>& thePointsXYZ,
+                                    std::vector<gp_XY>& 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<Handle(HYDROData_Entity)>& importedEntities)
+{
+  if (importXY)
+  {
+    std::vector<gp_XY> aPoints2d;
+    std::vector<gp_XYZ> 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()<Precision::Confusion())
+      {
+        IsClosed = true;
+        aPolylineXY->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<gp_XY> aDPoints2d;
+    std::vector<gp_XYZ> 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()<Precision::Confusion())
+      {
+        IsClosed = true;
+        aPolylineXY->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();
index 0047995a169da453dbf1672a338f20375ea8da49..c7bd4fe6a53831e8e96d7f98700f487726c90831 100755 (executable)
@@ -154,6 +154,13 @@ public:
 
   static TopoDS_Shape PolyXY2Face(const Handle(HYDROData_PolylineXY)& aPolyline);
 
+  static bool importFromXYZ( QString& theFileName, bool bImportXY, std::vector<gp_XYZ>& thePointsXYZ,
+                             std::vector<gp_XY>& thePointsXY);
+
+  static bool importPolylineFromXYZ(QString aFilename, Handle(HYDROData_Document) theDocument, 
+                                    bool importXY, NCollection_Sequence<Handle(HYDROData_Entity)>& importedEntities);
+  
+
   static void SetSIProgress(const Handle(Message_ProgressIndicator)& thePI);
   static const Handle(Message_ProgressIndicator)& GetSIProgress();
 
index bc83c0f2abe7506a9200f7a357e2a839c835c4f3..6697b42010576b0f68c513458fd002f654cf7009 100644 (file)
@@ -28,6 +28,7 @@
 #include <HYDROData_Bathymetry.h>
 #include <HYDROData_Iterator.h>
 #include <HYDROData_ShapeFile.h>
+#include <HYDROData_Tool.h>
 
 #include <HYDROData_Profile.h>
 
@@ -39,6 +40,7 @@
 #include <QFile>
 #include <QFileInfo>
 #include <SUIT_MessageBox.h>
+#include <gp_XY.hxx>
 
 
 HYDROGUI_ImportPolylineOp::HYDROGUI_ImportPolylineOp( HYDROGUI_Module* theModule )
@@ -77,7 +79,16 @@ NCollection_Sequence<Handle(HYDROData_Entity)> 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<Handle(HYDROData_Entity)> 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;
 }
index 0f46924bf5dd0198bca5eeae0aca98f15d6a69dc..3b654ee684a5f49ca59ae42fb6efef2a74770cf3 100755 (executable)
@@ -2554,7 +2554,11 @@ file cannot be correctly imported for an Obstacle definition.</translation>
     </message>\r
     <message>\r
       <source>POLYLINE_FILTER</source>\r
-      <translation>Shape files (*.shp)</translation>\r
+      <translation>Shape files (*.shp);;XY files(*.xy);;XYZ files(*.xyz)</translation>
+    </message>
+    <message>
+      <source>IMPORT_POLYLINE_XY_PART_ONLY</source>
+      <translation>Do you wish to import the XY part only? (Click 'Yes' to import as polylineXY and 'No' to import as Polyline3D)</translation>
     </message>\r
     <message>\r
       <source>BAD_IMPORTED_POLYLINE_FILES_TLT</source>\r
index 6732da7866c8161986270bc889759dd5cdd16870..cb701d8b042a5e5b4cd37e2c75e11a64382959fa 100644 (file)
@@ -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 (file)
index 0000000..c71b5b6
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 (file)
index 0000000..c71b5b6
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 (file)
index 0000000..c71b5b6
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 (file)
index 0000000..009410d
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 (file)
index 0000000..009410d
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 (file)
index 0000000..009410d
Binary files /dev/null and b/src/HYDRO_tests/reference_data/win32/Polyline_import_XYZ_3D.png differ
index 7ed3645f951f5f6dcf60a0d2c97b760058ffdba4..b804b1abad5b2b81a6518753bd4cb6ace287b316 100644 (file)
@@ -46,6 +46,9 @@
 #include <TopoDS_Vertex.hxx>
 #include <TopoDS_Wire.hxx>
 #include <gp_XY.hxx>
+#include <HYDROData_Tool.h>
+
+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<Handle(HYDROData_Entity)> ents;
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( 1 );
+  TCollection_AsciiString fname = REF_DATA_PATH.toLatin1().data();
+  fname += "/profiles1.xyz";
+
+  NCollection_Sequence<Handle(HYDROData_Entity)> 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
index d51b20f51f101aaf9bcb08cefe67c6ecbc286c8e..b03ac734eec5e16e32ad8f99007a34e91fccd32b 100644 (file)
@@ -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);