OdbReader: Made sure metadata are available before using them

Fix of #308,
Also avoid loading any results when selecting an empty/none-existing
result (Partial fix of #316)
This commit is contained in:
Jacob Støren
2015-06-09 11:17:54 +02:00
parent 7201e3330b
commit 05315bc7a9
2 changed files with 43 additions and 30 deletions

View File

@@ -235,20 +235,22 @@ bool RifOdbReader::openFile(const std::string& fileName, std::string* errorMessa
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifOdbReader::buildMetaData()
void RifOdbReader::assertMetaDataLoaded()
{
CVF_ASSERT(m_odb != NULL);
m_resultsMetaData = resultsMetaData(m_odb);
return true;
if (m_resultsMetaData.empty())
{
m_resultsMetaData = readResultsMetaData(m_odb);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::map< RifOdbReader::RifOdbResultKey, std::vector<std::string> > RifOdbReader::resultsMetaData(odb_Odb* odb)
std::map< RifOdbReader::RifOdbResultKey, std::vector<std::string> > RifOdbReader::readResultsMetaData(odb_Odb* odb)
{
CVF_ASSERT(odb != NULL);
@@ -631,10 +633,14 @@ size_t RifOdbReader::resultItemCount(const std::string& fieldName, int partIndex
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RifOdbReader::componentIndex(const RifOdbResultKey& result, const std::string& componentName) const
int RifOdbReader::componentIndex(const RifOdbResultKey& result, const std::string& componentName)
{
std::vector<std::string> compNames = componentNames(result);
// If there are no component names, we expect the field to be a pure scalar.
// Then an empty string is the valid component name
if (!compNames.size() && componentName == "") return 0;
for (size_t i = 0; i < compNames.size(); i++)
{
if (compNames[i] == componentName)
@@ -643,24 +649,27 @@ int RifOdbReader::componentIndex(const RifOdbResultKey& result, const std::strin
}
}
return 0;
return -1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<std::string> RifOdbReader::componentNames(const RifOdbResultKey& result) const
std::vector<std::string> RifOdbReader::componentNames(const RifOdbResultKey& result)
{
std::vector<std::string> compNames;
assertMetaDataLoaded();
std::map< RifOdbResultKey, std::vector<std::string> >::const_iterator resMapIt = m_resultsMetaData.find(result);
if (resMapIt != m_resultsMetaData.end())
{
std::vector<std::string> compNames;
compNames = resMapIt->second;
return compNames;
}
return compNames;
CVF_ASSERT(false);
return std::vector<std::string>();
}
@@ -669,10 +678,7 @@ std::vector<std::string> RifOdbReader::componentNames(const RifOdbResultKey& res
//--------------------------------------------------------------------------------------------------
std::map<std::string, std::vector<std::string> > RifOdbReader::fieldAndComponentNames(RifOdbResultKey::ResultPosition position)
{
if (m_resultsMetaData.empty())
{
buildMetaData();
}
assertMetaDataLoaded();
std::map<std::string, std::vector<std::string> > fieldsAndComponents;
@@ -703,11 +709,13 @@ void RifOdbReader::readScalarNodeField(const std::string& fieldName, const std::
size_t dataSize = nodeIdToIdxMap.size();
CVF_ASSERT(dataSize > 0);
resultValues->resize(dataSize);
resultValues->assign(dataSize, std::numeric_limits<float>::infinity());
int compIndex = componentIndex(RifOdbResultKey(RifOdbResultKey::NODAL, fieldName), componentName);
CVF_ASSERT(compIndex >= 0);
if (compIndex < 0) return;
resultValues->resize(dataSize);
resultValues->assign(dataSize, std::numeric_limits<float>::infinity());
const odb_Frame& frame = stepFrame(stepIndex, frameIndex);
const odb_FieldOutput& instanceFieldOutput = frame.fieldOutputs()[fieldName.c_str()].getSubset(*partInstance);
@@ -737,7 +745,9 @@ void RifOdbReader::readScalarNodeField(const std::string& fieldName, const std::
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifOdbReader::readScalarElementNodeField(const std::string& fieldName, const std::string& componentName, int partIndex, int stepIndex, int frameIndex, std::vector<float>* resultValues)
void RifOdbReader::readScalarElementNodeField(const std::string& fieldName, const std::string& componentName,
int partIndex, int stepIndex, int frameIndex,
std::vector<float>* resultValues)
{
CVF_ASSERT(resultValues);
@@ -747,6 +757,9 @@ void RifOdbReader::readScalarElementNodeField(const std::string& fieldName, cons
std::map<int, int>& elementIdToIdxMap = m_elementIdToIdxMaps[partIndex];
CVF_ASSERT(elementIdToIdxMap.size() > 0);
int compIndex = componentIndex(RifOdbResultKey(RifOdbResultKey::ELEMENT_NODAL, fieldName), componentName);
if (compIndex < 0) return;
size_t dataSize = resultItemCount(fieldName, partIndex, stepIndex, frameIndex);
if (dataSize > 0)
{
@@ -754,9 +767,6 @@ void RifOdbReader::readScalarElementNodeField(const std::string& fieldName, cons
resultValues->assign(dataSize, std::numeric_limits<float>::infinity());
}
int compIndex = componentIndex(RifOdbResultKey(RifOdbResultKey::ELEMENT_NODAL, fieldName), componentName);
CVF_ASSERT(compIndex >= 0);
const odb_Frame& frame = stepFrame(stepIndex, frameIndex);
const odb_FieldOutput& instanceFieldOutput = frame.fieldOutputs()[fieldName.c_str()].getSubset(*partInstance);
const odb_FieldOutput& fieldOutput = instanceFieldOutput.getSubset(odb_Enum::ELEMENT_NODAL);
@@ -796,7 +806,9 @@ void RifOdbReader::readScalarElementNodeField(const std::string& fieldName, cons
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RifOdbReader::readScalarIntegrationPointField(const std::string& fieldName, const std::string& componentName, int partIndex, int stepIndex, int frameIndex, std::vector<float>* resultValues)
void RifOdbReader::readScalarIntegrationPointField( const std::string& fieldName, const std::string& componentName,
int partIndex, int stepIndex, int frameIndex,
std::vector<float>* resultValues)
{
CVF_ASSERT(resultValues);
@@ -806,16 +818,17 @@ void RifOdbReader::readScalarIntegrationPointField(const std::string& fieldName,
std::map<int, int>& elementIdToIdxMap = m_elementIdToIdxMaps[partIndex];
CVF_ASSERT(elementIdToIdxMap.size() > 0);
size_t dataSize = resultItemCount(fieldName, partIndex, stepIndex, frameIndex);
int compIndex = componentIndex(RifOdbResultKey(RifOdbResultKey::INTEGRATION_POINT, fieldName), componentName);
if (compIndex < 0) return;
size_t dataSize = resultItemCount(fieldName, partIndex, stepIndex, frameIndex);
if (dataSize > 0)
{
resultValues->resize(dataSize);
resultValues->assign(dataSize, std::numeric_limits<float>::infinity());
}
int compIndex = componentIndex(RifOdbResultKey(RifOdbResultKey::INTEGRATION_POINT, fieldName), componentName);
CVF_ASSERT(compIndex >= 0);
const odb_Frame& frame = stepFrame(stepIndex, frameIndex);
const odb_FieldOutput& instanceFieldOutput = frame.fieldOutputs()[fieldName.c_str()].getSubset(*partInstance);
const odb_FieldOutput& fieldOutput = instanceFieldOutput.getSubset(odb_Enum::INTEGRATION_POINT);

View File

@@ -86,15 +86,15 @@ private:
}
};
bool buildMetaData();
void assertMetaDataLoaded();
void close();
size_t resultItemCount(const std::string& fieldName, int partIndex, int stepIndex, int frameIndex);
const odb_Frame& stepFrame(int stepIndex, int frameIndex) const;
odb_Instance* instance(int instanceIndex);
int componentIndex(const RifOdbResultKey& result, const std::string& componentName) const;
std::vector<std::string> componentNames(const RifOdbResultKey& result) const;
int componentIndex(const RifOdbResultKey& result, const std::string& componentName);
std::vector<std::string> componentNames(const RifOdbResultKey& result);
std::map< std::string, std::vector<std::string> > fieldAndComponentNames(RifOdbResultKey::ResultPosition position);
std::map< RifOdbResultKey, std::vector<std::string> > resultsMetaData(odb_Odb* odb);
std::map< RifOdbResultKey, std::vector<std::string> > readResultsMetaData(odb_Odb* odb);
private:
odb_Odb* m_odb;