From 5684f6bf7297dc4dbb8fe8fdb15ef767f87131b5 Mon Sep 17 00:00:00 2001 From: Elisa Prana Date: Mon, 20 Jul 2020 13:16:54 +0200 Subject: [PATCH 1/3] Add: handle string attributes String attributes are stored in json file as follow : [ "size",1, "storage","int32", "strings",["/pCube1/pCubeShape1","/pSphere1/pSphereShape1"], "indices",[ "size",1, "storage","int32", "arrays",[[0,1,0,1,0,1,0,1,0,1]] ] ] Store "arrays" in new 'stringIdxs' attribute --- include/houio/HouGeo.h | 5 +++-- src/HouGeo.cpp | 32 ++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/include/houio/HouGeo.h b/include/houio/HouGeo.h index 7849855..a341dbe 100644 --- a/include/houio/HouGeo.h +++ b/include/houio/HouGeo.h @@ -44,8 +44,9 @@ namespace houio Storage m_storage; Type m_type; //std::vector data; - std::vector strings; // used in case of type==string - int numElements; + std::vector strings; // used in case of type==string + std::vector stringsIdxs; // used in case of type==string + int numElements; Attribute::Ptr m_attr; // primitives::Attribute }; diff --git a/src/HouGeo.cpp b/src/HouGeo.cpp index 1dcba53..8f30073 100644 --- a/src/HouGeo.cpp +++ b/src/HouGeo.cpp @@ -280,12 +280,17 @@ namespace houio m_type = ATTR_TYPE_STRING; //attr->storage = attrStorage; tupleSize = 1; + + // TODO : handle indices return numElements++; } std::string HouGeo::HouAttribute::getString( int index )const { - return strings[index]; + if(stringsIdxs.size() < index) + throw std::runtime_error("HouGeo::getString Index is out of stringsIdx range."); + int string_index = stringsIdxs[index]; + return strings[string_index]; } @@ -583,6 +588,10 @@ namespace houio }else if( attrType == AttributeAdapter::ATTR_TYPE_STRING ) { + attr->m_name = attrName; + attr->m_type = attrType; + attr->tupleSize = 1; + if( attrData->hasKey("strings") ) { json::ArrayPtr stringsArray = attrData->getArray("strings"); @@ -591,14 +600,21 @@ namespace houio { std::string string = stringsArray->get( i ); attr->strings.push_back(string); - //qDebug() << QString::fromStdString(string); } attr->numElements = numElements; + } - attr->m_name = attrName; - attr->m_type = attrType; - //attr->storage = attrStorage; - attr->tupleSize = 1; + if( attrData->hasKey("indices") ) + { + json::ObjectPtr indicesObject = toObject(attrData->getArray("indices")); + json::ArrayPtr indices = indicesObject->getArray("rawpagedata"); + int numElements = indices->size(); + for( int i=0;iget(i); + attr->stringsIdxs.push_back(index); + } + attr->numElements = numElements; } } @@ -886,7 +902,7 @@ namespace houio { return field->sample(i, j, k); } - + math::Vec3i HouGeo::HouVolume::getResolution()const { return field->getResolution(); @@ -994,7 +1010,7 @@ namespace houio - + From cb76fb21c3a301fd974d38ea938f350daba83125 Mon Sep 17 00:00:00 2001 From: Elisa Prana Date: Mon, 20 Jul 2020 17:12:36 +0200 Subject: [PATCH 2/3] Add: functions to manipulate string attributes --- include/houio/HouGeo.h | 7 +++++++ include/houio/HouGeoAdapter.h | 4 ++++ src/HouGeo.cpp | 28 ++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/houio/HouGeo.h b/include/houio/HouGeo.h index a341dbe..67b5af6 100644 --- a/include/houio/HouGeo.h +++ b/include/houio/HouGeo.h @@ -34,6 +34,13 @@ namespace houio virtual void getPacking( std::vector &packing )const; virtual int getNumElements()const; virtual std::string getString( int index )const; + virtual std::string getUniqueString( int index )const; + virtual int getStringIndex( int index )const; + virtual std::vector getUniqueStrings()const; + virtual std::vector getStringIndices()const; + + + virtual RawPointer::Ptr getRawPointer(); //int addV4f(math::V4f value); diff --git a/include/houio/HouGeoAdapter.h b/include/houio/HouGeoAdapter.h index ce506ba..20ad4a1 100644 --- a/include/houio/HouGeoAdapter.h +++ b/include/houio/HouGeoAdapter.h @@ -57,6 +57,10 @@ namespace houio virtual int getNumElements()const; virtual RawPointer::Ptr getRawPointer(); virtual std::string getString( int index )const=0; + virtual std::string getUniqueString( int index )const=0; + virtual int getStringIndex( int index )const=0; + virtual std::vector getUniqueStrings()const=0; + virtual std::vector getStringIndices()const=0; static Type type( const std::string &typeName ); static Storage storage( const std::string &storageName ); static int storageSize( Storage storage ); diff --git a/src/HouGeo.cpp b/src/HouGeo.cpp index 8f30073..0631c7c 100644 --- a/src/HouGeo.cpp +++ b/src/HouGeo.cpp @@ -285,14 +285,38 @@ namespace houio return numElements++; } + std::string HouGeo::HouAttribute::getUniqueString( int index )const + { + if(strings.size() < index) + throw std::runtime_error("HouGeo::getString Index is out of strings range."); + return strings[index]; + } + std::string HouGeo::HouAttribute::getString( int index )const { if(stringsIdxs.size() < index) - throw std::runtime_error("HouGeo::getString Index is out of stringsIdx range."); - int string_index = stringsIdxs[index]; + throw std::runtime_error("HouGeo::getString Index is out of strings range."); + const int string_index = stringsIdxs[index]; return strings[string_index]; } + int HouGeo::HouAttribute::getStringIndex( int index )const + { + if(stringsIdxs.size() < index) + throw std::runtime_error("HouGeo::getStringIndex Index is out of stringsIdx range."); + return stringsIdxs[index]; + } + + std::vector HouGeo::HouAttribute::getUniqueStrings()const + { + return strings; + } + + std::vector HouGeo::HouAttribute::getStringIndices()const + { + return stringsIdxs; + } + From a2e8aa10fdbdc70a5c33bf5743955177b94c12b2 Mon Sep 17 00:00:00 2001 From: Elisa Prana Date: Mon, 10 Aug 2020 14:41:14 +0200 Subject: [PATCH 3/3] Refacto : declare value outside of loops If there are many points in file, declare/undeclare can be very consuming. Also remove unused variables --- src/HouGeo.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/HouGeo.cpp b/src/HouGeo.cpp index 0631c7c..5e291ac 100644 --- a/src/HouGeo.cpp +++ b/src/HouGeo.cpp @@ -419,13 +419,12 @@ namespace houio json::ArrayPtr entries = o->getArray("sharedprimitivedata"); int numEntries = (int)entries->size()/2; + int index = 0; for( int i=0;iget(index); + index = i*2; json::ArrayPtr entry = entries->getArray(index+1); - std::string type2_questionmark = entry->get(0); std::string id = entry->get(1); json::ArrayPtr data = entry->getArray(2); @@ -620,10 +619,11 @@ namespace houio { json::ArrayPtr stringsArray = attrData->getArray("strings"); int numElements = stringsArray->size(); + std::string stringValue = ""; for( int i=0;iget( i ); - attr->strings.push_back(string); + stringValue = stringsArray->get( i ); + attr->strings.push_back(stringValue); } attr->numElements = numElements; } @@ -633,9 +633,10 @@ namespace houio json::ObjectPtr indicesObject = toObject(attrData->getArray("indices")); json::ArrayPtr indices = indicesObject->getArray("rawpagedata"); int numElements = indices->size(); + int index = 0; for( int i=0;iget(i); + index = indices->get(i); attr->stringsIdxs.push_back(index); } attr->numElements = numElements;