8 #include "GmshConfig.h"
14 #if defined(HAVE_POST)
20 std::vector<MVertex *> &vertices)
22 for(
int i = 0; i < num; i++) {
23 if(indices[i] < 0 || indices[i] > (
int)(vec.size() - 1)) {
28 vertices.push_back(vec[indices[i]]);
33 #if defined(HAVE_POST)
34 static bool getProperties(
int num,
int *indices, std::vector<double> &vec,
35 std::vector<double> &properties)
37 for(
int i = 0; i < num; i++) {
38 if(indices[i] < 0 || indices[i] > (
int)(vec.size() - 1)) {
43 properties.push_back(vec[indices[i]]);
51 FILE *fp =
Fopen(name.c_str(),
"rb");
53 Msg::Error(
"Unable to open file '%s'", name.c_str());
57 std::vector<MVertex *> vertexVector;
58 std::map<int, std::vector<MElement *> > elements[5];
59 std::map<int, std::vector<double> > properties;
61 char buffer[256], str[256], str2[256], str3[256];
67 std::vector<std::string> propName;
71 if(!fgets(buffer,
sizeof(buffer), fp))
break;
72 if(buffer[0] !=
'#') {
73 sscanf(buffer,
"%s %s", str, str2);
74 if(!strcmp(str,
"element") && !strcmp(str2,
"vertex")) {
75 sscanf(buffer,
"%s %s %d", str, str2, &nbv);
77 if(!strcmp(str,
"format") && strcmp(str2,
"ascii")) {
78 Msg::Warning(
"Reading binary PLY files is experimental");
80 if(!strcmp(str2,
"binary_big_endian")) {
81 Msg::Debug(
"Reading binary PLY file as big-endian");
85 if(!strcmp(str,
"property") && strcmp(str2,
"list")) {
87 sscanf(buffer,
"%s %s %s", str, str2, str3);
88 if(nbprop > 3) propName.push_back(s1 + str3);
90 if(!strcmp(str,
"element") && !strcmp(str2,
"face")) {
91 sscanf(buffer,
"%s %s %d", str, str2, &nbf);
93 if(!strcmp(str,
"end_header")) {
98 vertexVector.resize(nbv);
100 for(
int i = 0; i < nbv; i++) {
102 char line[10000], *pEnd, *pEnd2, *pEnd3;
103 if(!fgets(
line,
sizeof(
line), fp)) {
107 x = strtod(
line, &pEnd);
108 y = strtod(pEnd, &pEnd2);
109 z = strtod(pEnd2, &pEnd3);
110 vertexVector[i] =
new MVertex(x, y,
z);
113 std::vector<double> prop(nbView);
114 for(
int k = 0; k < nbView; k++) {
115 prop[k] = strtod(pEnd, &pEnd2);
117 properties[k].push_back(prop[k]);
121 for(
int i = 0; i < nbf; i++) {
122 if(!fgets(buffer,
sizeof(buffer), fp))
break;
124 sscanf(buffer,
"%d %d %d %d", &nbe, &n[0], &n[1], &n[2]);
134 std::size_t num_coords = 3 * nbv;
135 std::vector<float> coord(num_coords);
136 if(fread(&coord[0],
sizeof(
float), num_coords, fp) != num_coords) {
139 for(
int i = 0; i < nbv; i++) {
141 new MVertex(coord[3 * i], coord[3 * i + 1], coord[3 * i + 2]);
144 for(
int i = 0; i < nbf; i++) {
147 if(fread(&nbe,
sizeof(
char), 1, fp) != 1) {
151 if(fread(n,
sizeof(
int), 3, fp) != 3) {
168 for(
int i = 0; i < (int)(
sizeof(elements) /
sizeof(elements[0])); i++)
173 #if defined(HAVE_POST)
175 std::vector<GEntity *> _entities;
177 for(
int iV = 0; iV < nbView; iV++) {
180 for(std::size_t ii = 0; ii < _entities.size(); ii++) {
181 for(std::size_t i = 0; i < _entities[ii]->getNumMeshElements(); i++) {
182 MElement *e = _entities[ii]->getMeshElement(i);
184 std::vector<double> x(numNodes), y(numNodes),
z(numNodes);
186 for(
int nod = 0; nod < numNodes; nod++)
187 out->push_back((e->
getVertex(nod))->x());
188 for(
int nod = 0; nod < numNodes; nod++)
189 out->push_back((e->
getVertex(nod))->y());
190 for(
int nod = 0; nod < numNodes; nod++)
191 out->push_back((e->
getVertex(nod))->z());
192 std::vector<double> props;
197 if(!getProperties(3, n, properties[iV], props)) {
201 for(
int nod = 0; nod < numNodes; nod++) out->push_back(props[nod]);
205 data->
Time.push_back(0);
218 FILE *fp =
Fopen(name.c_str(),
"r");
220 Msg::Error(
"Unable to open file '%s'", name.c_str());
224 std::vector<MVertex *> vertexVector;
225 std::map<int, std::vector<MElement *> > elements[5];
231 if(!fgets(buffer,
sizeof(buffer), fp))
break;
232 if(buffer[0] !=
'#') {
233 sscanf(buffer,
"%d", &nbv);
234 if(!fgets(buffer,
sizeof(buffer), fp))
break;
235 sscanf(buffer,
"%d", &nbf);
238 vertexVector.resize(nbv);
239 for(
int i = 0; i < nbv; i++) {
240 if(!fgets(buffer,
sizeof(buffer), fp))
break;
242 int nb = sscanf(buffer,
"%lf %lf %lf", &x, &y, &
z);
244 if(!fgets(buffer,
sizeof(buffer), fp))
break;
245 sscanf(buffer,
"%lf", &y);
246 if(!fgets(buffer,
sizeof(buffer), fp))
break;
247 sscanf(buffer,
"%lf", &
z);
249 vertexVector[i] =
new MVertex(x, y,
z);
251 for(
int i = 0; i < nbf; i++) {
252 if(!fgets(buffer,
sizeof(buffer), fp))
break;
254 int nb = sscanf(buffer,
"%d %d %d %d", &nbe, &n[0], &n[1], &n[2]);
256 if(!fgets(buffer,
sizeof(buffer), fp))
break;
257 sscanf(buffer,
"%d", &n[0]);
258 if(!fgets(buffer,
sizeof(buffer), fp))
break;
259 sscanf(buffer,
"%d", &n[1]);
260 if(!fgets(buffer,
sizeof(buffer), fp))
break;
261 sscanf(buffer,
"%d", &n[2]);
273 for(
int i = 0; i < (int)(
sizeof(elements) /
sizeof(elements[0])); i++)
284 FILE *fp =
Fopen(name.c_str(),
"w");
286 Msg::Error(
"Unable to open file '%s'", name.c_str());
291 int numTriangles = 0;
293 numTriangles += (*it)->triangles.size();
296 fprintf(fp,
"%d\n", numVertices);
297 fprintf(fp,
"%d\n", numTriangles);
299 std::vector<GEntity *> entities;
301 for(std::size_t i = 0; i < entities.size(); i++)
302 for(std::size_t j = 0; j < entities[i]->mesh_vertices.size(); j++)
303 entities[i]->mesh_vertices[j]->
writePLY2(fp);
306 for(std::size_t i = 0; i < (*it)->triangles.size(); i++)
307 (*it)->triangles[i]->writePLY2(fp);