Skip to content

Commit

Permalink
Fix 2dm coordinates saving (#491)
Browse files Browse the repository at this point in the history
* update doubleToString() so that scientific notation can be forced

* 2dm file shoud have XYZ coordinates written as scientific notation with precision of 8 decimal places

* add test case for scientific notation file

* fix style

* version

---------

Co-authored-by: Jan Caha <[email protected]>
  • Loading branch information
JanCaha and Jan Caha authored Oct 31, 2024
1 parent ac1531b commit b02eb94
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ QGIS contains internal copy of MDAL library in following versions:
| 3.30.0 | 1.0.2 | |
| 3.36.0 | 1.1.0 | Mike21 format support read/write |
| 3.38.0 | 1.2.0 | Groundwater / surface water meshes for 3Di format |
| 3.42.0 | 1.3.0 | Fix 2dm format coordinates saving |

versions `X.Y.9Z` are development versions or alpha/beta releases (e.g. `0.4.90`, `0.4.91`, ...)

Expand Down
2 changes: 1 addition & 1 deletion mdal/frmts/mdal_2dm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ void MDAL::Driver2dm::save( const std::string &fileName, const std::string &, MD
for ( size_t j = 0; j < 2; ++j )
{
line.append( " " );
line.append( MDAL::coordinateToString( vertex[j] ) );
line.append( MDAL::doubleToString( vertex[j], 8, true ) );
}
line.append( " " );
line.append( MDAL::doubleToString( vertex[2] ) );
Expand Down
2 changes: 1 addition & 1 deletion mdal/mdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static const char *EMPTY_STR = "";

const char *MDAL_Version()
{
return "1.2.0";
return "1.3.0";
}

MDAL_Status MDAL_LastStatus()
Expand Down
6 changes: 5 additions & 1 deletion mdal/mdal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,14 @@ std::string MDAL::coordinateToString( double coordinate, int precision )
return returnString;
}

std::string MDAL::doubleToString( double value, int precision )
std::string MDAL::doubleToString( double value, int precision, bool forceScientific )
{
std::ostringstream oss;
oss.precision( precision );
if ( forceScientific )
{
oss.setf( std::ios::scientific );
}
oss << value;
return oss.str();
}
Expand Down
3 changes: 2 additions & 1 deletion mdal/mdal_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ namespace MDAL

//! Returns a string with scientific format
//! precision is the number of signifiant digits
std::string doubleToString( double value, int precision = 6 );
//! forceScientific forces the scientific notation of the number even if not necessary
std::string doubleToString( double value, int precision = 6, bool forceScientific = false );

/**
* Splits by deliminer and skips empty parts.
Expand Down
8 changes: 8 additions & 0 deletions tests/data/2dm/quad_and_triangle_scientific.2dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MESH2D
ND 1 1.00000000e+03 2.00000000e+03 20
ND 2 2.00000000e+03 2.00000000e+03 30
ND 3 3.00000000e+03 2.00000000e+03 40
ND 4 2.00000000e+03 3.00000000e+03 50
ND 5 1.00000000e+03 3.00000000e+03 10
E4Q 1 1 2 4 5
E3T 2 2 3 4
25 changes: 25 additions & 0 deletions tests/test_2dm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,31 @@ TEST( Mesh2DMTest, Save1DMeshToFile )
);
}

TEST( Mesh2DMTest, QuadAndTriangleScientificNotationFile )
{
std::string path = test_file( "/2dm/quad_and_triangle_scientific.2dm" );
EXPECT_EQ( MDAL_MeshNames( path.c_str() ), "2DM:\"" + path + "\"" );
MDAL_MeshH m_scientific = MDAL_LoadMesh( path.c_str() );
EXPECT_NE( m_scientific, nullptr );
MDAL_Status s = MDAL_LastStatus();
EXPECT_EQ( MDAL_Status::None, s );

int v_count = MDAL_M_vertexCount( m_scientific );
EXPECT_EQ( v_count, 5 );

int f_count = MDAL_M_faceCount( m_scientific );
EXPECT_EQ( 2, f_count );

path = test_file( "/2dm/quad_and_triangle.2dm" );
EXPECT_EQ( MDAL_MeshNames( path.c_str() ), "2DM:\"" + path + "\"" );
MDAL_MeshH m_not_scientific = MDAL_LoadMesh( path.c_str() );

compareMeshFrames( m_scientific, m_not_scientific );

MDAL_CloseMesh( m_scientific );
MDAL_CloseMesh( m_not_scientific );
}

int main( int argc, char **argv )
{
testing::InitGoogleTest( &argc, argv );
Expand Down

0 comments on commit b02eb94

Please sign in to comment.