Commit cf49c215 authored by Bertrand Thierry's avatar Bertrand Thierry

Matlab output format for mesh

parent b8c4f6d5
Pipeline #1051 canceled with stage
......@@ -40,6 +40,7 @@ int GetFileFormatFromExtension(const std::string &ext)
else if(ext == ".opt") return FORMAT_OPT;
else if(ext == ".unv") return FORMAT_UNV;
else if(ext == ".vtk") return FORMAT_VTK;
else if(ext == ".m") return FORMAT_MATLAB;
else if(ext == ".dat") return FORMAT_TOCHNOG;
else if(ext == ".txt") return FORMAT_TXT;
else if(ext == ".stl") return FORMAT_STL;
......@@ -102,6 +103,7 @@ std::string GetDefaultFileName(int format)
case FORMAT_OPT: name += ".opt"; break;
case FORMAT_UNV: name += ".unv"; break;
case FORMAT_VTK: name += ".vtk"; break;
case FORMAT_MATLAB: name += ".m"; break;
case FORMAT_TOCHNOG: name += ".dat"; break;
case FORMAT_STL: name += ".stl"; break;
case FORMAT_CGNS: name += ".cgns"; break;
......@@ -317,6 +319,12 @@ void CreateOutputFile(const std::string &fileName, int format,
CTX::instance()->bigEndian);
break;
case FORMAT_MATLAB:
GModel::current()->writeMATLAB
(name, CTX::instance()->mesh.binary, CTX::instance()->mesh.saveAll,
CTX::instance()->mesh.scalingFactor);
break;
case FORMAT_MESH:
GModel::current()->writeMESH
(name, CTX::instance()->mesh.saveElementTagType,
......
......@@ -56,6 +56,7 @@
#define FORMAT_TOCHNOG 47
#define FORMAT_TIKZ 48
#define FORMAT_NEU 49
#define FORMAT_MATLAB 50
// Element types
#define TYPE_PNT 1
......
......@@ -305,6 +305,8 @@ static int _save_mesh(const char *name){ return genericMeshFileDialog
(name, "MESH Options", FORMAT_MESH, false, true); }
static int _save_mail(const char *name){ return genericMeshFileDialog
(name, "MAIL Options", FORMAT_MAIL, false, false); }
static int _save_matlab(const char *name){ return genericMeshFileDialog
(name, "MATLAB Options", FORMAT_MATLAB, false, false); }
static int _save_bdf(const char *name){ return bdfFileDialog(name); }
static int _save_p3d(const char *name){ return genericMeshFileDialog
(name, "P3D Options", FORMAT_P3D, false, false); }
......@@ -372,6 +374,7 @@ static int _save_auto(const char *name)
case FORMAT_RMED : return _save_view_med(name);
case FORMAT_MESH : return _save_mesh(name);
case FORMAT_MAIL : return _save_mail(name);
case FORMAT_MATLAB : return _save_matlab(name);
case FORMAT_BDF : return _save_bdf(name);
case FORMAT_DIFF : return _save_diff(name);
case FORMAT_INP : return _save_inp(name);
......@@ -431,6 +434,7 @@ static void file_export_cb(Fl_Widget *w, void *data)
#endif
{"Mesh - INRIA Medit" TT "*.mesh", _save_mesh},
{"Mesh - CEA Triangulation" TT "*.mail", _save_mail},
{"Mesh - Matlab" TT "*.m", _save_matlab},
{"Mesh - Nastran Bulk Data File" TT "*.bdf", _save_bdf},
{"Mesh - Plot3D Structured Mesh" TT "*.p3d", _save_p3d},
{"Mesh - STL Surface" TT "*.stl", _save_stl},
......
......@@ -27,7 +27,7 @@ set(SRC
GModelIO_UNV.cpp GModelIO_BDF.cpp GModelIO_IR3.cpp GModelIO_DIFF.cpp
GModelIO_GEOM.cpp GModelIO_INP.cpp GModelIO_MAIL.cpp GModelIO_P3D.cpp
GModelIO_CELUM.cpp GModelIO_NEU.cpp GModelIO_ACTRAN.cpp GModelIO_SU2.cpp
GModelIO_SAMCEF.cpp GModelIO_POS.cpp
GModelIO_SAMCEF.cpp GModelIO_MATLAB.cpp GModelIO_POS.cpp
ExtrudeParams.cpp
Geo.cpp
GeoStringInterface.cpp GeoInterpolation.cpp
......
......@@ -676,6 +676,11 @@ class GModel {
bool saveAll=false, double scalingFactor=1.0,
bool bigEndian=false);
//Matlab format
int writeMATLAB(const std::string &name, bool binary=false,
bool saveAll=false, double scalingFactor=1.0);
// Tochnog format
int writeTOCHNOG(const std::string &name, bool saveGroupsOfNodes=false,
bool saveAll=false, double scalingFactor=1.0);
......
// Gmsh - Copyright (C) 1997-2017 C. Geuzaine, J.-F. Remacle
//
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to the public mailing list <gmsh@onelab.info>.
#include "GModel.h"
#include "OS.h"
#include "MPoint.h"
#include "MLine.h"
#include "MTriangle.h"
#include "MQuadrangle.h"
#include "MTetrahedron.h"
#include "MHexahedron.h"
#include "MPrism.h"
#include "MPyramid.h"
#include "StringUtils.h"
int GModel::writeMATLAB(const std::string &name, bool binary, bool saveAll, double scalingFactor)
{
if(binary)
{
Msg::Warning("Binary format not available for Matlab, saving into ASCII format");
binary = false;
//TODO !
}
FILE *fp = Fopen(name.c_str(), binary ? "wb":"w");
if(!fp){
Msg::Error("Unable to open file '%s'", name.c_str());
return 0;
}
if(noPhysicalGroups()) saveAll = true;
// get the number of vertices and index the vertices in a continuous
// sequence
int numVertices = indexMeshVertices(saveAll);
fprintf(fp, "\%\% Matlab mesh\n");
fprintf(fp, "\%\% %s, Created by Gmsh\n", getName().c_str());
if(binary)
fprintf(fp, "\%\% BINARY\n");
else
fprintf(fp, "\%\% ASCII\n");
// get all the entities in the model
std::vector<GEntity*> entities;
getEntities(entities);
// write mesh vertices
fprintf(fp, "gVertices = %d;\n", numVertices);
fprintf(fp, "gX = zeros(gVertices,1);\n", numVertices);
fprintf(fp, "gY = zeros(gVertices,1);\n", numVertices);
fprintf(fp, "gZ = zeros(gVertices,1);\n", numVertices);
int cpt = 0;
for(unsigned int i = 0; i < entities.size(); i++)
for(unsigned int j = 0; j < entities[i]->mesh_vertices.size(); j++)
{
cpt ++;
entities[i]->mesh_vertices[j]->writeMATLAB(fp, cpt, scalingFactor);
}
fprintf(fp, "\n");
// loop over all elements we need to save and count vertices
int numElements = 0, totalNumInt = 0;
for(unsigned int i = 0; i < entities.size(); i++){
if(entities[i]->physicals.size() || saveAll){
for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){
if(entities[i]->getMeshElement(j)->getTypeForMATLAB()){
numElements++;
totalNumInt += entities[i]->getMeshElement(j)->getNumVertices() + 1;
}
}
}
}
// print vertex indices in ascii or binary
fprintf(fp, "\%\%\n");
fprintf(fp, "nTri=%d;\n", numElements);
fprintf(fp, "gTri=[\n");
for(unsigned int i = 0; i < entities.size(); i++){
if(entities[i]->physicals.size() || saveAll){
for(unsigned int j = 0; j < entities[i]->getNumMeshElements(); j++){
if(entities[i]->getMeshElement(j)->getTypeForMATLAB())
entities[i]->getMeshElement(j)->writeMATLAB(fp, binary);
}
}
}
fprintf(fp, "];\n");
fclose(fp);
return 1;
}
......@@ -1329,6 +1329,22 @@ void MElement::writeVTK(FILE *fp, bool binary, bool bigEndian)
}
}
void MElement::writeMATLAB(FILE *fp, bool binary)
{
if(!getTypeForMATLAB()) return;
if(binary)
{
Msg::Warning("Binary format not available for Matlab, saving into ASCII format");
binary = false;
}
int n = getNumVertices();
for(int i = 0; i < n; i++)
fprintf(fp, " %d", getVertexMATLAB(i)->getIndex());
fprintf(fp, ";\n");
}
void MElement::writeUNV(FILE *fp, int num, int elementary, int physical)
{
int type = getTypeForUNV();
......
......@@ -111,6 +111,9 @@ class MElement
// get the vertex using the VTK ordering
virtual MVertex *getVertexVTK(int num){ return getVertex(num); }
// get the vertex using the MATLAB ordering
virtual MVertex *getVertexMATLAB(int num){ return getVertex(num); }
// get the vertex using the Tochnog ordering
virtual MVertex *getVertexTOCHNOG(int num){ return getVertex(num); }
......@@ -388,6 +391,7 @@ class MElement
virtual void writePLY2(FILE *fp);
virtual void writeUNV(FILE *fp, int num=0, int elementary=1, int physical=1);
virtual void writeVTK(FILE *fp, bool binary=false, bool bigEndian=false);
virtual void writeMATLAB(FILE *fp, bool binary=false);
virtual void writeTOCHNOG(FILE *fp, int num);
virtual void writeMESH(FILE *fp, int elementTagType=1, int elementary=1,
int physical=0);
......@@ -406,6 +410,7 @@ class MElement
virtual int getTypeForMSH() const { return 0; }
virtual int getTypeForUNV() const { return 0; }
virtual int getTypeForVTK() const { return 0; }
virtual int getTypeForMATLAB() const { return 0; }
virtual const char *getStringForTOCHNOG() const { return 0; }
virtual const char *getStringForPOS() const { return 0; }
virtual const char *getStringForBDF() const { return 0; }
......
......@@ -112,6 +112,7 @@ class MTriangle : public MElement {
virtual int getTypeForMSH() const { return MSH_TRI_3; }
virtual int getTypeForUNV() const { return 91; } // thin shell linear triangle
virtual int getTypeForVTK() const { return 5; }
virtual int getTypeForMATLAB() const { return 1; }
virtual const char *getStringForPOS() const { return "ST"; }
virtual const char *getStringForBDF() const { return "CTRIA3"; }
virtual const char *getStringForDIFF() const { return "ElmT3n2D"; }
......
......@@ -294,6 +294,15 @@ void MVertex::writeVTK(FILE *fp, bool binary, double scalingFactor, bool bigEndi
}
}
void MVertex::writeMATLAB(FILE *fp, int i, bool binary, double scalingFactor)
{
if(_index < 0) return; // negative index vertices are never saved
fprintf(fp, "gX(%d) = %.16g;\n", i, x() * scalingFactor);
fprintf(fp, "gY(%d) = %.16g;\n", i, y() * scalingFactor);
fprintf(fp, "gZ(%d) = %.16g;\n", i, z() * scalingFactor);
}
void MVertex::writeTOCHNOG(FILE *fp, int dim, double scalingFactor)
{
if(_index < 0) return; // negative index vertices are never saved
......
......@@ -101,6 +101,7 @@ class MVertex{
void writeUNV(FILE *fp, double scalingFactor=1.0);
void writeVTK(FILE *fp, bool binary=false, double scalingFactor=1.0,
bool bigEndian=false);
void writeMATLAB(FILE *fp, int index, bool binary=false, double scalingFactor=1.0);
void writeTOCHNOG(FILE *fp, int dim, double scalingFactor=1.0);
void writeMESH(FILE *fp, double scalingFactor=1.0);
void writeNEU(FILE *fp, int dim, double scalingFactor=1.0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment