diff --git a/Common/CreateFile.cpp b/Common/CreateFile.cpp index 262d2e7005580317b9bbbd0f71213a730f14db58..367c6cc1cc1bc71c0f4712836c82bba0da982f30 100644 --- a/Common/CreateFile.cpp +++ b/Common/CreateFile.cpp @@ -156,7 +156,7 @@ void CreateOutputFile(const char *filename, int format) case FORMAT_VTK: GModel::current()->writeVTK(name, CTX.mesh.binary, CTX.mesh.save_all, - CTX.mesh.scaling_factor); + CTX.mesh.scaling_factor, CTX.big_endian); break; case FORMAT_MESH: diff --git a/Geo/GModel.h b/Geo/GModel.h index e5ce013e2fa76d6ef980f3b0178ccb60523aed5b..60dec101f17a491bfad61bcfe25a70c5c23295b1 100644 --- a/Geo/GModel.h +++ b/Geo/GModel.h @@ -322,7 +322,8 @@ class GModel // VTK format int readVTK(const std::string &name); int writeVTK(const std::string &name, bool binary=false, - bool saveAll=false, double scalingFactor=1.0); + bool saveAll=false, double scalingFactor=1.0, + bool bigEndian=false); }; #endif diff --git a/Geo/GModelIO_Mesh.cpp b/Geo/GModelIO_Mesh.cpp index 67c21b817ad057be03ce8b28346391baad7d228a..537d953e42d677763d2f1e52ef7fcd73215ee41c 100644 --- a/Geo/GModelIO_Mesh.cpp +++ b/Geo/GModelIO_Mesh.cpp @@ -1927,14 +1927,8 @@ int GModel::writeP3D(const std::string &name, bool saveAll, double scalingFactor } int GModel::writeVTK(const std::string &name, bool binary, bool saveAll, - double scalingFactor) + double scalingFactor, bool bigEndian) { - Msg::Error("VTK export is experimental:"); - Msg::Error(" * vertex ordering for second order elements is wrong"); - Msg::Error(" * binary export crashes paraview on Mac: can somebody test"); - Msg::Error(" on another platform? I *think* I followed the spec, but"); - Msg::Error(" I probably missed something..."); - FILE *fp = fopen(name.c_str(), binary ? "wb" : "w"); if(!fp){ Msg::Error("Unable to open file '%s'", name.c_str()); @@ -1963,7 +1957,7 @@ int GModel::writeVTK(const std::string &name, bool binary, bool saveAll, fprintf(fp, "POINTS %d double\n", numVertices); for(unsigned int i = 0; i < entities.size(); i++) for(unsigned int j = 0; j < entities[i]->mesh_vertices.size(); j++) - entities[i]->mesh_vertices[j]->writeVTK(fp, binary, scalingFactor); + entities[i]->mesh_vertices[j]->writeVTK(fp, binary, scalingFactor, bigEndian); fprintf(fp, "\n"); // loop over all elements we need to save and count vertices @@ -1985,7 +1979,7 @@ int GModel::writeVTK(const std::string &name, bool binary, bool saveAll, if(entities[i]->physicals.size() || saveAll){ for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){ if(entities[i]->getMeshElement(j)->getTypeForVTK()) - entities[i]->getMeshElement(j)->writeVTK(fp, binary); + entities[i]->getMeshElement(j)->writeVTK(fp, binary, bigEndian); } } } @@ -1998,8 +1992,11 @@ int GModel::writeVTK(const std::string &name, bool binary, bool saveAll, for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){ int type = entities[i]->getMeshElement(j)->getTypeForVTK(); if(type){ - if(binary) + if(binary){ + // VTK always expects big endian binary data + if(!bigEndian) SwapBytes((char*)&type, sizeof(int), 1); fwrite(&type, sizeof(int), 1, fp); + } else fprintf(fp, "%d\n", type); } diff --git a/Geo/MElement.cpp b/Geo/MElement.cpp index b3d5a05ebadfa45ab78a8962383fe02b9aa2a450..7a46276fb45b9df09154d50d719b65970211b6e7 100644 --- a/Geo/MElement.cpp +++ b/Geo/MElement.cpp @@ -10,6 +10,7 @@ #include "GFace.h" #include "FunctionSpace.h" #include "Message.h" +#include "StringUtils.h" #if defined(HAVE_GMSH_EMBEDDED) #include "GmshEmbedded.h" @@ -747,7 +748,7 @@ void MElement::writeVRML(FILE *fp) fprintf(fp, "-1,\n"); } -void MElement::writeVTK(FILE *fp, bool binary) +void MElement::writeVTK(FILE *fp, bool binary, bool bigEndian) { int type = getTypeForUNV(); if(!type) return; @@ -760,6 +761,8 @@ void MElement::writeVTK(FILE *fp, bool binary) verts[0] = n; for(int i = 0; i < n; i++) verts[i + 1] = getVertexVTK(i)->getIndex() - 1; + // VTK always expects big endian binary data + if(!bigEndian) SwapBytes((char*)verts, sizeof(int), n + 1); fwrite(verts, sizeof(int), n + 1, fp); } else{ diff --git a/Geo/MElement.h b/Geo/MElement.h index 162bf45198c76575a4491e4d565659b65307f9f0..f942e958e202a8d26c8f2515adc891380c4e787b 100644 --- a/Geo/MElement.h +++ b/Geo/MElement.h @@ -213,7 +213,7 @@ class MElement virtual void writeSTL(FILE *fp, bool binary=false, double scalingFactor=1.0); virtual void writeVRML(FILE *fp); virtual void writeUNV(FILE *fp, int num=0, int elementary=1, int physical=1); - virtual void writeVTK(FILE *fp, bool binary=false); + virtual void writeVTK(FILE *fp, bool binary=false, bool bigEndian=false); virtual void writeMESH(FILE *fp, int elementary=1); virtual void writeBDF(FILE *fp, int format=0, int elementary=1); @@ -344,7 +344,7 @@ class MLine : public MElement { virtual void getFaceRep(int num, double *x, double *y, double *z, SVector3 *n){} virtual int getTypeForMSH() const { return MSH_LIN_2; } virtual int getTypeForUNV() const { return 21; } // linear beam - virtual int getTypeForVTK() const { return 3; } + //virtual int getTypeForVTK() const { return 3; } virtual const char *getStringForPOS() const { return "SL"; } virtual const char *getStringForBDF() const { return "CBAR"; } virtual void revert() @@ -409,7 +409,7 @@ class MLine3 : public MLine { } virtual int getTypeForMSH() const { return MSH_LIN_3; } virtual int getTypeForUNV() const { return 24; } // parabolic beam - virtual int getTypeForVTK() const { return 21; } + //virtual int getTypeForVTK() const { return 21; } virtual const char *getStringForPOS() const { return "SL2"; } }; @@ -662,7 +662,7 @@ class MTriangle6 : public MTriangle { } virtual int getTypeForMSH() const { return MSH_TRI_6; } virtual int getTypeForUNV() const { return 92; } // thin shell parabolic triangle - virtual int getTypeForVTK() const { return 22; } + //virtual int getTypeForVTK() const { return 22; } virtual const char *getStringForPOS() const { return "ST2"; } virtual const char *getStringForBDF() const { return "CTRIA6"; } virtual void revert() @@ -960,7 +960,7 @@ class MQuadrangle8 : public MQuadrangle { } virtual int getTypeForMSH() const { return MSH_QUA_8; } virtual int getTypeForUNV() const { return 95; } // shell parabolic quadrilateral - virtual int getTypeForVTK() const { return 23; } + //virtual int getTypeForVTK() const { return 23; } virtual const char *getStringForBDF() const { return "CQUAD8"; } virtual void revert() { @@ -1312,7 +1312,7 @@ class MTetrahedron10 : public MTetrahedron { } virtual int getTypeForMSH() const { return MSH_TET_10; } virtual int getTypeForUNV() const { return 118; } // solid parabolic tetrahedron - virtual int getTypeForVTK() const { return 24; } + //virtual int getTypeForVTK() const { return 24; } virtual const char *getStringForPOS() const { return "SS2"; } virtual const char *getStringForBDF() const { return "CTETRA"; } virtual void revert() @@ -1817,7 +1817,7 @@ class MHexahedron20 : public MHexahedron { } virtual int getTypeForMSH() const { return MSH_HEX_20; } virtual int getTypeForUNV() const { return 116; } // solid parabolic brick - virtual int getTypeForVTK() const { return 25; } + //virtual int getTypeForVTK() const { return 25; } virtual const char *getStringForBDF() const { return "CHEXA"; } virtual void revert() { diff --git a/Geo/MVertex.cpp b/Geo/MVertex.cpp index 2da12d72fe08070f00f6e10157ed4dc5f6de3e1f..d39a7eb15432933d69465806fb06622439f0f46f 100644 --- a/Geo/MVertex.cpp +++ b/Geo/MVertex.cpp @@ -9,6 +9,7 @@ #include "GEdge.h" #include "GFace.h" #include "Message.h" +#include "StringUtils.h" int MVertex::_globalNum = 0; double MVertexLessThanLexicographic::tolerance = 1.e-6; @@ -64,12 +65,14 @@ void MVertex::writeUNV(FILE *fp, double scalingFactor) fprintf(fp, tmp); } -void MVertex::writeVTK(FILE *fp, bool binary, double scalingFactor) +void MVertex::writeVTK(FILE *fp, bool binary, double scalingFactor, bool bigEndian) { if(_index < 0) return; // negative index vertices are never saved if(binary){ double data[3] = {x() * scalingFactor, y() * scalingFactor, z() * scalingFactor}; + // VTK always expects big endian binary data + if(!bigEndian) SwapBytes((char*)data, sizeof(double), 3); fwrite(data, sizeof(double), 3, fp); } else{ diff --git a/Geo/MVertex.h b/Geo/MVertex.h index 9f894222a709a0a3207a40c50c7ac31c3ff544d8..e2ef9cdc47755a6a000450bb552da923e32f4f7d 100644 --- a/Geo/MVertex.h +++ b/Geo/MVertex.h @@ -109,7 +109,8 @@ class MVertex{ void writeMSH(FILE *fp, bool binary=false, double scalingFactor=1.0); void writeVRML(FILE *fp, double scalingFactor=1.0); void writeUNV(FILE *fp, double scalingFactor=1.0); - void writeVTK(FILE *fp, bool binary=false, double scalingFactor=1.0); + void writeVTK(FILE *fp, bool binary=false, double scalingFactor=1.0, + bool bigEndian=false); void writeMESH(FILE *fp, double scalingFactor=1.0); void writeBDF(FILE *fp, int format=0, double scalingFactor=1.0); };