From: Anthony Geay Date: Thu, 20 Dec 2018 09:22:49 +0000 (+0100) Subject: Example of non trivial writeVTK method X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8b19a001ef20556beb250623f58c69525f7ce0cf;p=tools%2Fmedcoupling.git Example of non trivial writeVTK method --- diff --git a/src/MEDCoupling/MEDCouplingMemArray.cxx b/src/MEDCoupling/MEDCouplingMemArray.cxx index 38c32cbb1..0308df167 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.cxx +++ b/src/MEDCoupling/MEDCouplingMemArray.cxx @@ -3690,47 +3690,6 @@ DataArrayInt32 *DataArrayInt32::deepCopy() const return new DataArrayInt32(*this); } -void DataArrayInt::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const -{ - static const char SPACE[4]={' ',' ',' ',' '}; - checkAllocated(); - std::string idt(indent,' '); - ofs << idt << "getNumberOfTuples() << "\">"; - if(std::string(type)=="Int32") - { - const char *data(reinterpret_cast(begin())); - std::size_t sz(getNbOfElems()*sizeof(int)); - byteArr->insertAtTheEnd(data,data+sz); - byteArr->insertAtTheEnd(SPACE,SPACE+4); - } - else if(std::string(type)=="Int8") - { - INTERP_KERNEL::AutoPtr tmp(new char[getNbOfElems()]); - std::copy(begin(),end(),(char *)tmp); - byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+getNbOfElems()); - byteArr->insertAtTheEnd(SPACE,SPACE+4); - } - else if(std::string(type)=="UInt8") - { - INTERP_KERNEL::AutoPtr tmp(new unsigned char[getNbOfElems()]); - std::copy(begin(),end(),(unsigned char *)tmp); - byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+getNbOfElems()); - byteArr->insertAtTheEnd(SPACE,SPACE+4); - } - else - throw INTERP_KERNEL::Exception("DataArrayInt::writeVTK : Only Int32, Int8 and UInt8 supported !"); - } - else - { - ofs << " RangeMin=\"" << getMinValueInArray() << "\" RangeMax=\"" << getMaxValueInArray() << "\" format=\"ascii\">\n" << idt; - std::copy(begin(),end(),std::ostream_iterator(ofs," ")); - } - ofs << std::endl << idt << "\n"; -} - /*! * Computes distribution of values of \a this one-dimensional array between given value * ranges (casts). This method is typically useful for entity number splitting by types, diff --git a/src/MEDCoupling/MEDCouplingMemArray.hxx b/src/MEDCoupling/MEDCouplingMemArray.hxx index 74da75e29..4a0946256 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.hxx +++ b/src/MEDCoupling/MEDCouplingMemArray.hxx @@ -545,6 +545,7 @@ namespace MEDCoupling MEDCOUPLING_EXPORT void reprCppStream(const std::string& varName, std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverview(std::ostream& stream) const; MEDCOUPLING_EXPORT void reprQuickOverviewData(std::ostream& stream, std::size_t maxNbOfByteInRepr) const; + MEDCOUPLING_EXPORT void writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const; protected: template void switchOnTupleAlg(T val, std::vector& vec, ALG algo) const; @@ -570,7 +571,6 @@ namespace MEDCoupling MEDCOUPLING_EXPORT int intValue() const; MEDCOUPLING_EXPORT DataArrayInt32 *deepCopy() const;//ok MEDCOUPLING_EXPORT DataArrayInt32 *buildNewEmptyInstance() const { return DataArrayInt32::New(); }//ok - MEDCOUPLING_EXPORT void writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const; MEDCOUPLING_EXPORT void transformWithIndArr(const int *indArrBg, const int *indArrEnd); MEDCOUPLING_EXPORT void transformWithIndArr(const MapKeyVal& m); MEDCOUPLING_EXPORT DataArrayInt32 *transformWithIndArrR(const int *indArr2Bg, const int *indArrEnd) const; diff --git a/src/MEDCoupling/MEDCouplingMemArray.txx b/src/MEDCoupling/MEDCouplingMemArray.txx index 2c5fe1975..027f2fd68 100644 --- a/src/MEDCoupling/MEDCouplingMemArray.txx +++ b/src/MEDCoupling/MEDCouplingMemArray.txx @@ -3853,6 +3853,52 @@ struct NotInRange stream << "]"; } + template + void DataArrayDiscrete::writeVTK(std::ostream& ofs, int indent, const std::string& type, const std::string& nameInFile, DataArrayByte *byteArr) const + { + static const char SPACE[4]={' ',' ',' ',' '}; + DataArrayTemplate::checkAllocated(); + std::string idt(indent,' '); + ofs << idt << "::getNumberOfComponents() << "\""; + if(byteArr) + { + ofs << " format=\"appended\" offset=\"" << byteArr->getNumberOfTuples() << "\">"; + if(std::string(type)==Traits::VTKReprStr) + { + const char *data(reinterpret_cast(DataArrayTemplate::begin())); + std::size_t sz(DataArrayTemplate::getNbOfElems()*sizeof(T)); + byteArr->insertAtTheEnd(data,data+sz); + byteArr->insertAtTheEnd(SPACE,SPACE+4); + } + else if(std::string(type)=="Int8") + { + INTERP_KERNEL::AutoPtr tmp(new char[DataArrayTemplate::getNbOfElems()]); + std::copy(DataArrayTemplate::begin(),DataArrayTemplate::end(),(char *)tmp); + byteArr->insertAtTheEnd((char *)tmp,(char *)tmp+DataArrayTemplate::getNbOfElems()); + byteArr->insertAtTheEnd(SPACE,SPACE+4); + } + else if(std::string(type)=="UInt8") + { + INTERP_KERNEL::AutoPtr tmp(new unsigned char[DataArrayTemplate::getNbOfElems()]); + std::copy(DataArrayTemplate::begin(),DataArrayTemplate::end(),(unsigned char *)tmp); + byteArr->insertAtTheEnd((unsigned char *)tmp,(unsigned char *)tmp+DataArrayTemplate::getNbOfElems()); + byteArr->insertAtTheEnd(SPACE,SPACE+4); + } + else + { + std::ostringstream oss; + oss << Traits::ArrayTypeName << "::writeVTK : Only " << Traits::VTKReprStr << ", Int8 and UInt8 supported !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + } + else + { + ofs << " RangeMin=\"" << DataArrayTemplate::getMinValueInArray() << "\" RangeMax=\"" << DataArrayTemplate::getMaxValueInArray() << "\" format=\"ascii\">\n" << idt; + std::copy(DataArrayTemplate::begin(),DataArrayTemplate::end(),std::ostream_iterator(ofs," ")); + } + ofs << std::endl << idt << "\n"; + } + //////////////////////////////////// /*! diff --git a/src/MEDCoupling/MEDCouplingTraits.cxx b/src/MEDCoupling/MEDCouplingTraits.cxx index 46e3f7b1f..8febd9af4 100644 --- a/src/MEDCoupling/MEDCouplingTraits.cxx +++ b/src/MEDCoupling/MEDCouplingTraits.cxx @@ -46,6 +46,8 @@ const char Traits::NPYStr[]="INT32"; const char Traits::ReprStr[]="int"; +const char Traits::VTKReprStr[]="Int32"; + const char Traits::ArrayTypeName[]="DataArrayChar"; const char Traits::ArrayTypeName[]="DataArrayInt64"; @@ -55,3 +57,5 @@ const char Traits::FieldTypeName[]="MEDCouplingFieldInt64"; const char Traits::NPYStr[]="INT64"; const char Traits::ReprStr[]="int64"; + +const char Traits::VTKReprStr[]="Int64"; diff --git a/src/MEDCoupling/MEDCouplingTraits.hxx b/src/MEDCoupling/MEDCouplingTraits.hxx index a14232eec..7059106c1 100644 --- a/src/MEDCoupling/MEDCouplingTraits.hxx +++ b/src/MEDCoupling/MEDCouplingTraits.hxx @@ -78,6 +78,7 @@ namespace MEDCoupling static const char FieldTypeName[]; static const char NPYStr[]; static const char ReprStr[]; + static const char VTKReprStr[]; typedef DataArrayInt32 ArrayType; typedef DataArrayInt32 ArrayTypeCh; typedef MEDCouplingFieldInt FieldType; @@ -91,6 +92,7 @@ namespace MEDCoupling static const char FieldTypeName[]; static const char NPYStr[]; static const char ReprStr[]; + static const char VTKReprStr[]; typedef DataArrayInt64 ArrayType; typedef DataArrayInt64 ArrayTypeCh; //typedef MEDCouplingFieldInt64 FieldType;