diff --git a/Common/gmsh.cpp b/Common/gmsh.cpp index b650792c0694be42f44033305cede6b621098700..7c5834846886c44624c0cf096eb8acda1f0fe8e7 100644 --- a/Common/gmsh.cpp +++ b/Common/gmsh.cpp @@ -2040,19 +2040,103 @@ void gmshViewAddModelData(const int tag, const std::string &modelName, #endif } -/* -void gmshViewAddListData(const int tag, ...); -void gmshViewAddStringData(const int tag, ...); -void gmshViewAddXYData(const int tag, const std::vector<double> &x, - const std::vector<double> &y); -void gmshViewAddXYZData(const int tag, const std::vector<double> &x, - const std::vector<double> &y, - const std::vector<double> &z); - -void gmshViewGetValue(tag, x, y, z, step, &vector_double); -void gmshViewGetRawData(tag, &double_vector); -void gmshViewSetRawData(tag, const &double_vector); -*/ +void gmshViewAddListData(const int tag, const std::string &type, const int numEle, + const std::vector<double> &data) +{ + if(!_isInitialized()){ throw -1; } +#if defined(HAVE_POST) + PView *view = PView::getViewByTag(tag); + if(!view){ + Msg::Error("Unknown view with tag %d", tag); + throw 2; + } + PViewDataList *d = dynamic_cast<PViewDataList*>(view->getData()); + if(!d){ // change the view type + std::string name = view->getData()->getName(); + delete view->getData(); + d = new PViewDataList(); + d->setName(name); + d->setFileName(name + ".pos"); + view->setData(d); + } + const char *types[] = {"SP", "VP", "TP", "SL", "VL", "TL", "ST", "VT", "TT", + "SQ", "VQ", "TQ", "SS", "VS", "TS", "SH", "VH", "TH", + "SI", "VI", "TI", "SY", "VY", "TY"}; + for(int idxtype = 0; idxtype < 24; idxtype++){ + if(type == types[idxtype]){ + d->importList(idxtype, numEle, data, true); + return; + } + } + Msg::Error("Unknown data type for list import"); + throw 2; +#else + Msg::Error("Views require the post-processing module"); + throw -1; +#endif +} + +void gmshViewProbe(const int tag, const double x, const double y, + const double z, std::vector<double> &value, + const int step, const int numComp, + const bool gradient, const double tolerance, + const std::vector<double> xElemCoord, + const std::vector<double> yElemCoord, + const std::vector<double> zElemCoord) +{ + if(!_isInitialized()){ throw -1; } +#if defined(HAVE_POST) + PView *view = PView::getViewByTag(tag); + if(!view){ + Msg::Error("Unknown view with tag %d", tag); + throw 2; + } + PViewData *data = view->getData(); + if(!data){ throw 2; } + value.clear(); + std::vector<double> val(9 * data->getNumTimeSteps()); + bool found = false; + int qn = 0; + double *qx = 0, *qy = 0, *qz = 0; + if(xElemCoord.size() && yElemCoord.size() && zElemCoord.size() && + xElemCoord.size() == yElemCoord.size() && + xElemCoord.size() == zElemCoord.size()){ + qn = xElemCoord.size(); + qx = (double*)&xElemCoord[0]; + qy = (double*)&yElemCoord[0]; + qz = (double*)&zElemCoord[0]; + } + switch(numComp){ + case 1: + found = data->searchScalarWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + break; + case 3: + found = data->searchVectorWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + break; + case 9: + found = data->searchTensorWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + break; + default: + found = data->searchScalarWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + if(!found) + found = data->searchVectorWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + if(!found) + found = data->searchTensorWithTol + (x, y, z, &val[0], step, 0, tolerance, qn, qx, qy, qz, gradient); + break; + } + if(found) + value.insert(value.end(), val.begin(), val.end()); +#else + Msg::Error("Views require the post-processing module"); + throw -1; +#endif +} void gmshViewExport(const int tag, const std::string &fileName, const bool append) diff --git a/Common/gmsh.h b/Common/gmsh.h index 07ddc05a5ae1b75b2b4bef76866977c545c0d990..314bf525be16374932a9c4ddd187ae2bd1a30f01 100644 --- a/Common/gmsh.h +++ b/Common/gmsh.h @@ -811,6 +811,26 @@ GMSH_API void gmshViewAddModelData(const int tag, const std::string &modelName, const int numComponents = -1, const int partition = 0); +// Adds list-based post-processing data to the view with tag `tag'. `type' +// identifies the data: "SP" for scalar points, "VP", for vector points, +// etc. `numEle' gives the number of elements in the data. `data' contains the +// data for the `numEle' elements. +GMSH_API void gmshViewAddListData(const int tag, const std::string &type, + const int numEle, const std::vector<double> &data); + +// Probes the view `tag' for its `value' at point (`x', `y', `z'). +GMSH_API void gmshViewProbe(const int tag, const double x, const double y, + const double z, std::vector<double> &value, + const int step = -1, const int numComp = -1, + const bool gradient = false, + const double tolerance = 0., + const std::vector<double> xElemCoord + = std::vector<double>(), + const std::vector<double> yElemCoord + = std::vector<double>(), + const std::vector<double> zElemCoord + = std::vector<double>()); + // Exports the view to a file. The export format is determined by the file // extension. GMSH_API void gmshViewExport(const int tag, const std::string &fileName, diff --git a/Post/PViewDataList.h b/Post/PViewDataList.h index 39e62626823afc33e2cc40ed955710d8e078da58..30c9fa6c2d83d958b1d0f009fd7c0989df6bf73b 100644 --- a/Post/PViewDataList.h +++ b/Post/PViewDataList.h @@ -137,6 +137,7 @@ class PViewDataList : public PViewData { bool forceNodeData=false, bool forceElementData=false); virtual void importLists(int N[24], std::vector<double> *V[24]); virtual void getListPointers(int N[24], std::vector<double> *V[24]); + void importList(int index, int n, const std::vector<double> &v, bool finalize); }; #endif diff --git a/Post/PViewDataListIO.cpp b/Post/PViewDataListIO.cpp index d6ef15e8113df02ce42cc59ca0e178758d450357..ddbb768ae650e003ce10a72415f261e570bf442e 100644 --- a/Post/PViewDataListIO.cpp +++ b/Post/PViewDataListIO.cpp @@ -723,6 +723,21 @@ void PViewDataList::importLists(int N[24], std::vector<double> *V[24]) finalize(); } +void PViewDataList::importList(int index, int n, const std::vector<double> &v, + bool fin) +{ + if(index < 0 || index >= 24){ + Msg::Error("Wrong list index to import"); + return; + } + std::vector<double> *list = 0; + int *nbe = 0, nbc, nbn; + _getRawData(index, &list, &nbe, &nbc, &nbn); + *nbe = n; + *list = v; // deep copy + if(fin) finalize(); +} + void PViewDataList::getListPointers(int N[24], std::vector<double> *V[24]) { for(int i = 0; i < 24; i++){ diff --git a/demos/api/viewlist.cpp b/demos/api/viewlist.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cede58c223501b37852440e154dceb93aecc057 --- /dev/null +++ b/demos/api/viewlist.cpp @@ -0,0 +1,35 @@ +#include <gmsh.h> + +int main(int argc, char **argv) +{ + gmshInitialize(); + gmshOptionSetNumber("General.Terminal", 1); + + std::vector<double> tri1 = {0., 1., 1., + 0., 0., 1., + 0., 0., 0.}; + std::vector<double> tri2 = {0., 1., 0., + 0., 1., 1., + 0., 0., 0.}; + + for(int step = 0; step < 10; step++){ + tri1.push_back(10.); + tri1.push_back(10.); + tri1.push_back(12. + step); + tri2.push_back(10.); + tri2.push_back(12. + step); + tri2.push_back(13. + step); + } + + int t = gmshViewCreate("some data"); + std::vector<double> data; + data.insert(data.end(), tri1.begin(), tri1.end()); + data.insert(data.end(), tri2.begin(), tri2.end()); + + gmshViewAddListData(t, "ST", 2, data); + + gmshViewExport(t, "data.pos"); + + gmshFinalize(); + return 0; +} diff --git a/demos/api/viewlist.py b/demos/api/viewlist.py new file mode 100644 index 0000000000000000000000000000000000000000..95d607393f5f4e051fe8f6fc667b3d107ce69454 --- /dev/null +++ b/demos/api/viewlist.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +from gmsh import * +import sys + +gmshInitialize(sys.argv) +gmshOptionSetNumber("General.Terminal", 1) + +tri1 = [0., 1., 1., + 0., 0., 1., + 0., 0., 0.]; +tri2 = [0., 1., 0., + 0., 1., 1., + 0., 0., 0.]; + +for step in range(0, 10): + tri1.append(10.); tri1.append(10.); tri1.append(12. + step) + tri2.append(10.); tri2.append(12. + step); tri2.append(13. + step) + +t = gmshViewCreate("some data") + +gmshViewAddListData(t, "ST", 2, tri1 + tri2) + +gmshViewExport(t, "data.pos") + +gmshFinalize()