Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
update MDAL to 0.0.5
  • Loading branch information
PeterPetrik authored and wonder-sk committed Jul 19, 2018
1 parent 64b18c6 commit 245d6e2
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 327 deletions.
99 changes: 69 additions & 30 deletions external/mdal/api/mdal.h
Expand Up @@ -53,6 +53,7 @@ enum MDAL_Status
Err_IncompatibleMesh,
Err_InvalidData,
Err_IncompatibleDataset,
Err_IncompatibleDatasetGroup,
Err_MissingDriver,
// Warnings
Warn_UnsupportedElement,
Expand All @@ -64,80 +65,118 @@ enum MDAL_Status

//! Mesh
typedef void *MeshH;
typedef void *DatasetGroupH;
typedef void *DatasetH;

//! Return MDAL version
//! Returns MDAL version
MDAL_EXPORT const char *MDAL_Version();

//! Return last status message
//! Returns last status message
MDAL_EXPORT MDAL_Status MDAL_LastStatus();

//! Load mesh file. On error see MDAL_LastStatus for error type This effectively loads whole mesh in-memory
///////////////////////////////////////////////////////////////////////////////////////
/// MESH
///////////////////////////////////////////////////////////////////////////////////////

//! Loads mesh file. On error see MDAL_LastStatus for error type This effectively loads whole mesh in-memory
MDAL_EXPORT MeshH MDAL_LoadMesh( const char *meshFile );
//! Close mesh, free the memory
//! Closes mesh, frees the memory
MDAL_EXPORT void MDAL_CloseMesh( MeshH mesh );
//! Return vertex count for the mesh

//! Returns vertex count for the mesh
MDAL_EXPORT int MDAL_M_vertexCount( MeshH mesh );
//! Return vertex X coord for the mesh
//! Returns vertex X coord for the mesh
MDAL_EXPORT double MDAL_M_vertexXCoordinatesAt( MeshH mesh, int index );
//! Return vertex Y coord for the mesh
//! Returns vertex Y coord for the mesh
MDAL_EXPORT double MDAL_M_vertexYCoordinatesAt( MeshH mesh, int index );
//! Return face count for the mesh
//! Returns face count for the mesh
MDAL_EXPORT int MDAL_M_faceCount( MeshH mesh );
//! Return number of vertices face consist of, e.g. 3 for triangle
//! Returns number of vertices face consist of, e.g. 3 for triangle
MDAL_EXPORT int MDAL_M_faceVerticesCountAt( MeshH mesh, int index );
//! Return vertex index for face
//! Returns vertex index for face
MDAL_EXPORT int MDAL_M_faceVerticesIndexAt( MeshH mesh, int face_index, int vertex_index );

/**
* Load dataset file. On error see MDAL_LastStatus for error type.
* Loads dataset file. On error see MDAL_LastStatus for error type.
* This may effectively load whole dataset in-memory for some providers
* Datasets will be closed automatically on mesh destruction or memory
* can be freed manually with MDAL_CloseDataset if needed
*/
MDAL_EXPORT void MDAL_M_LoadDatasets( MeshH mesh, const char *datasetFile );

//! Free the memory used to get dataset values
MDAL_EXPORT void MDAL_M_CloseDataset( DatasetH dataset );
//! Returns dataset groups count
MDAL_EXPORT int MDAL_M_datasetGroupCount( MeshH mesh );

//! Returns dataset group handle
MDAL_EXPORT DatasetGroupH MDAL_M_datasetGroup( MeshH mesh, int index );

///////////////////////////////////////////////////////////////////////////////////////
/// DATASET GROUPS
///////////////////////////////////////////////////////////////////////////////////////

//! Return dataset count
MDAL_EXPORT int MDAL_M_datasetCount( MeshH mesh );
//! Returns dataset count in group
MDAL_EXPORT int MDAL_G_datasetCount( DatasetGroupH group );

//! Return dataset handle
MDAL_EXPORT DatasetH MDAL_M_dataset( MeshH mesh, int index );
//! Returns dataset handle
MDAL_EXPORT DatasetH MDAL_G_dataset( DatasetGroupH group, int index );

//! Returns dataset handle for Maximums. Could be null for some formats
MDAL_EXPORT DatasetH MDAL_G_maxiumumsDataset( DatasetGroupH group );

//! Returns number of metadata values
MDAL_EXPORT int MDAL_G_metadataCount( DatasetGroupH group );

/**
* Returns dataset metadata key
* not thread-safe and valid only till next call
*/
MDAL_EXPORT const char *MDAL_G_metadataKey( DatasetGroupH group, int index );

/**
* Returns dataset metadata value
* not thread-safe and valid only till next call
*/
MDAL_EXPORT const char *MDAL_G_metadataValue( DatasetGroupH group, int index );

/**
* Returns dataset group name
* not thread-safe and valid only till next call
*/
MDAL_EXPORT const char *MDAL_G_name( DatasetGroupH group );

//! Whether dataset has scalar data associated
MDAL_EXPORT bool MDAL_D_hasScalarData( DatasetH dataset );
MDAL_EXPORT bool MDAL_G_hasScalarData( DatasetGroupH group );

//! Whether dataset is on vertices
MDAL_EXPORT bool MDAL_D_isOnVertices( DatasetH dataset );
MDAL_EXPORT bool MDAL_G_isOnVertices( DatasetGroupH group );

//! Return number of metadata values
MDAL_EXPORT int MDAL_D_metadataCount( DatasetH dataset );
///////////////////////////////////////////////////////////////////////////////////////
/// DATASETS
///////////////////////////////////////////////////////////////////////////////////////

//! Return dataset metadata key
MDAL_EXPORT const char *MDAL_D_metadataKey( DatasetH dataset, int index );
//! Returns dataset parent group
MDAL_EXPORT DatasetGroupH MDAL_D_group( DatasetH dataset );

//! Return dataset metadata value
MDAL_EXPORT const char *MDAL_D_metadataValue( DatasetH dataset, int index );
//! Returns dataset time
MDAL_EXPORT double MDAL_D_time( DatasetH dataset );

//! Return number of values
//! Returns number of values
MDAL_EXPORT int MDAL_D_valueCount( DatasetH dataset );

/**
* Return scalar value associated with the index from the dataset
* Returns scalar value associated with the index from the dataset
* for nodata return numeric_limits<double>:quiet_NaN
*/
MDAL_EXPORT double MDAL_D_value( DatasetH dataset, int valueIndex );

/**
* Return X value associated with the index from the vector dataset
* Returns X value associated with the index from the vector dataset
* for nodata return numeric_limits<double>:quiet_NaN
*/
MDAL_EXPORT double MDAL_D_valueX( DatasetH dataset, int valueIndex );

/**
* Return Y value associated with the index from the vector dataset
* Returns Y value associated with the index from the vector dataset
* for nodata return numeric_limits<double>:quiet_NaN
*/
MDAL_EXPORT double MDAL_D_valueY( DatasetH dataset, int valueIndex );
Expand All @@ -148,7 +187,7 @@ MDAL_EXPORT double MDAL_D_valueY( DatasetH dataset, int valueIndex );
*/
MDAL_EXPORT bool MDAL_D_active( DatasetH dataset, int faceIndex );

//! Return whether dataset is valid
//! Returns whether dataset is valid
MDAL_EXPORT bool MDAL_D_isValid( DatasetH dataset );

#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion external/mdal/frmts/mdal_2dm.hpp
Expand Up @@ -9,7 +9,7 @@
#include <string>
#include <memory>

#include "mdal_defines.hpp"
#include "mdal_data_model.hpp"
#include "mdal.h"

namespace MDAL
Expand Down
107 changes: 54 additions & 53 deletions external/mdal/frmts/mdal_ascii_dat.cpp
Expand Up @@ -52,34 +52,38 @@ void MDAL::LoaderAsciiDat::load( MDAL::Mesh *mesh, MDAL_Status *status )
return;
}
line = trim( line );
std::string basename = baseName( mDatFile );

// http://www.xmswiki.com/xms/SMS:ASCII_Dataset_Files_*.dat
// Apart from the format specified above, there is an older supported format used in BASEMENT (and SMS?)
// which is simpler (has only one dataset in one file, no status flags etc)
bool oldFormat;
bool isVector = false;
bool readingDataset = false; // whether we are in process of reading the dataset

std::string baseDatasetName( basename );
std::vector<std::shared_ptr<Dataset>> datOutputs; // DAT outputs data
std::shared_ptr<DatasetGroup> group; // DAT outputs data
std::string name( MDAL::baseName( mDatFile ) );

if ( line == "DATASET" )
oldFormat = false;
else if ( line == "SCALAR" || line == "VECTOR" )
{
oldFormat = true;
isVector = ( line == "VECTOR" );
baseDatasetName = basename;
readingDataset = true;

group.reset( new DatasetGroup() );
group->uri = mDatFile;
group->setName( name );
group->isScalar = !isVector;
}
else
EXIT_WITH_ERROR( MDAL_Status::Err_UnknownFormat );

// see if it contains element-centered results - supported by BASEMENT
bool elementCentered = false;
if ( !oldFormat && contains( basename, "_els_" ) )
elementCentered = true;
// see if it contains face-centered results - supported by BASEMENT
bool faceCentered = false;
if ( !oldFormat && contains( name, "_els_" ) )
faceCentered = true;

if ( group )
group->isOnVertices = !faceCentered;

while ( std::getline( in, line ) )
{
Expand Down Expand Up @@ -107,28 +111,32 @@ void MDAL::LoaderAsciiDat::load( MDAL::Mesh *mesh, MDAL_Status *status )
}
else if ( !oldFormat && ( cardType == "BEGSCL" || cardType == "BEGVEC" ) )
{
if ( readingDataset )
if ( group )
{
debug( "New dataset while previous one is still active!" );
EXIT_WITH_ERROR( MDAL_Status::Err_UnknownFormat );
}
isVector = cardType == "BEGVEC";
baseDatasetName = basename;
readingDataset = true;

group.reset( new DatasetGroup() );
group->uri = mDatFile;
group->setName( name );
group->isScalar = !isVector;
group->isOnVertices = !faceCentered;
}
else if ( !oldFormat && cardType == "ENDDS" )
{
if ( !readingDataset )
if ( !group )
{
debug( "ENDDS card for no active dataset!" );
EXIT_WITH_ERROR( MDAL_Status::Err_UnknownFormat );
}

addDatasets( mesh, baseDatasetName, datOutputs );
mesh->datasetGroups.push_back( group );
group.reset();
}
else if ( !oldFormat && cardType == "NAME" && items.size() >= 2 )
{
if ( !readingDataset )
if ( !group )
{
debug( "NAME card for no active dataset!" );
EXIT_WITH_ERROR( MDAL_Status::Err_UnknownFormat );
Expand All @@ -137,7 +145,7 @@ void MDAL::LoaderAsciiDat::load( MDAL::Mesh *mesh, MDAL_Status *status )
size_t quoteIdx1 = line.find( '\"' );
size_t quoteIdx2 = line.find( '\"', quoteIdx1 + 1 );
if ( quoteIdx1 != std::string::npos && quoteIdx2 != std::string::npos )
baseDatasetName = line.substr( quoteIdx1 + 1, quoteIdx2 - quoteIdx1 - 1 );
group->setName( line.substr( quoteIdx1 + 1, quoteIdx2 - quoteIdx1 - 1 ) );
}
else if ( oldFormat && ( cardType == "SCALAR" || cardType == "VECTOR" ) )
{
Expand All @@ -147,14 +155,14 @@ void MDAL::LoaderAsciiDat::load( MDAL::Mesh *mesh, MDAL_Status *status )
{
double t = toDouble( items[oldFormat ? 1 : 2] );

if ( elementCentered )
if ( faceCentered )
{
readFaceTimestep( mesh, datOutputs, t, isVector, in );
readFaceTimestep( mesh, group, t, isVector, in );
}
else
{
bool hasStatus = ( oldFormat ? false : toBool( items[1] ) );
readVertexTimestep( mesh, datOutputs, t, isVector, hasStatus, in );
readVertexTimestep( mesh, group, t, isVector, hasStatus, in );
}

}
Expand All @@ -168,45 +176,32 @@ void MDAL::LoaderAsciiDat::load( MDAL::Mesh *mesh, MDAL_Status *status )

if ( oldFormat )
{
if ( datOutputs.size() == 0 )
if ( !group || group->datasets.size() == 0 )
EXIT_WITH_ERROR( MDAL_Status::Err_UnknownFormat );

addDatasets( mesh, baseDatasetName, datOutputs );
mesh->datasetGroups.push_back( group );
group.reset();
}
}

void MDAL::LoaderAsciiDat::addDatasets( MDAL::Mesh *mesh,
const std::string &name,
const Datasets &datOutputs ) const
void MDAL::LoaderAsciiDat::readVertexTimestep(
const MDAL::Mesh *mesh,
std::shared_ptr<DatasetGroup> group,
double t,
bool isVector,
bool hasStatus,
std::ifstream &stream )
{
for ( const std::shared_ptr<Dataset> &ds : datOutputs )
{
ds->uri = mDatFile;
ds->setName( name );
ds->isValid = true;
}

//https://stackoverflow.com/a/2551785/2838364
mesh->datasets.insert(
mesh->datasets.end(),
datOutputs.begin(),
datOutputs.end()
);
}
assert( group );

void MDAL::LoaderAsciiDat::readVertexTimestep( const MDAL::Mesh *mesh, Datasets &datOutputs,
double t, bool isVector,
bool hasStatus, std::ifstream &stream )
{
size_t vertexCount = mesh->vertices.size();
size_t faceCount = mesh->faces.size();

std::shared_ptr<MDAL::Dataset> dataset( new MDAL::Dataset );
dataset->isScalar = !isVector; //everything else to be set in addDatasets
dataset->setMetadata( "time", std::to_string( t / 3600. ) );
dataset->time = t / 3600.; // TODO read TIMEUNITS
dataset->values.resize( vertexCount );
dataset->active.resize( faceCount );
dataset->isOnVertices = true;
dataset->parent = group.get();

// only for new format
for ( size_t i = 0; i < faceCount; ++i )
Expand Down Expand Up @@ -258,18 +253,24 @@ void MDAL::LoaderAsciiDat::readVertexTimestep( const MDAL::Mesh *mesh, Datasets
}
}

datOutputs.push_back( std::move( dataset ) );
group->datasets.push_back( dataset );
}

void MDAL::LoaderAsciiDat::readFaceTimestep( const MDAL::Mesh *mesh, Datasets &datOutputs, double t, bool isVector, std::ifstream &stream )
void MDAL::LoaderAsciiDat::readFaceTimestep(
const MDAL::Mesh *mesh,
std::shared_ptr<DatasetGroup> group,
double t,
bool isVector,
std::ifstream &stream )
{
assert( group );

size_t faceCount = mesh->faces.size();

std::shared_ptr<MDAL::Dataset> dataset( new MDAL::Dataset );
dataset->isScalar = !isVector; //everything else to be set in addDatasets
dataset->setMetadata( "time", std::to_string( t / 3600. ) );
dataset->time = t / 3600.;
dataset->values.resize( faceCount );
dataset->isOnVertices = false;
dataset->parent = group.get();

// TODO: hasStatus
for ( size_t index = 0; index < faceCount; ++index )
Expand Down Expand Up @@ -303,5 +304,5 @@ void MDAL::LoaderAsciiDat::readFaceTimestep( const MDAL::Mesh *mesh, Datasets &d
}
}

datOutputs.push_back( std::move( dataset ) );
group->datasets.push_back( dataset );
}

0 comments on commit 245d6e2

Please sign in to comment.