proteus  1.8.1
C/C++/Fortran libraries
meshio.cpp
Go to the documentation of this file.
1 #include "meshio.h"
2 #include <string>
3 #include <fstream>
4 #include <cassert>
5 #include <cmath>
6 #include <iomanip>
7 #include <iostream>
8 
9 namespace IOutils
10 {
11 std::istream& eatline(std::istream& s)
12 {
13  while (s.get() != '\n' && s.good()) {}
14  return s;
15 }
16 
17 std::istream & eatchar(std::istream & s)
18 { s.get() ; return s ; }
19 bool iswhitespace(const char & c)
20 { return c == '\n' || c == ' ' || c == '\t' || c == '\r';}
21 std::istream & eatcomments(std::istream & s)
22 {
23  char c = s.peek() ;
24  //mwf add whitespace too, or just blank lines?
25  while ( ( c == '\n' || c == '!' || c == '%' || c == '#' || c == ';' || c == '$')
26  && s.good() )
27  { s >> eatline ; c = s.peek() ; }
28  return s ;
29 }
30 
31 }//IOUtils
32 
33 namespace meshIO
34 {
35 
36 /***********************************************************************
37 
38  **********************************************************************/
39 
40 bool
41 readTriangleMeshNodesAndElements(const char * filebase,
42  const int& indexBase,
43  int& nElements, int& nNodes,
44  std::vector<double>& nodeArray,
45  std::vector<int>&
46  elementNodesArray,
47  std::vector<int>& nodeMaterialTypes,
48  std::vector<int>& elementMaterialTypes,
49  const int& defaultElementMaterialType,
50  const int& defaultNodeMaterialType)
51 {
52  bool failed = false;
53  const int simplexDim = 2+1;
54  const int vertexDim =3; //always
55  using namespace IOutils;
56  std::string vertexFileName = std::string(filebase) + ".node" ;
57  std::string elementFileName = std::string(filebase) + ".ele" ;
58 
59  std::ifstream vertexFile(vertexFileName.c_str());
60  std::ifstream elementFile(elementFileName.c_str());
61 
62  if (!vertexFile.good())
63  {
64  std::cerr<<"readTriangleMeshNodeAndElements cannot open file "
65  <<vertexFileName<<std::endl;
66  failed = true;
67  return failed;
68  }
69  if (!elementFile.good())
70  {
71  std::cerr<<"readTriangleMeshNodeAndElements cannot open file "
72  <<elementFileName<<std::endl;
73  failed = true;
74  return failed;
75  }
76 
77  int hasMarkers(0),hasAttributes(0),nSpace(2);
78  //read vertices
79  vertexFile >> eatcomments >> nNodes >> nSpace >> hasAttributes
80  >> hasMarkers >> eatline ;
81  assert(nNodes > 0);
82  assert(nSpace == 2);
83  if (hasAttributes > 0)
84  {
85  std::cerr<<"WARNING readTriangle nodes hasAttributes= "<<hasAttributes
86  <<" > 0 will treat first value as integer id for boundary!!"<<std::endl;
87  hasMarkers = 1;
88  }
89  nodeArray.resize(vertexDim*nNodes);
90  nodeMaterialTypes.resize(nNodes,defaultNodeMaterialType);
91 
92  for (int iv = 0; iv < nNodes; iv++)
93  {
94  int nv; double x,y; int nodeId(0);
95  vertexFile >> eatcomments >> nv >> x >> y;
96  if (hasMarkers > 0)
97  vertexFile >> nodeId;
98  nv -= indexBase;
99  assert(0 <= nv && nv < nNodes && vertexFile.good());
100  nodeArray[vertexDim*nv + 0] = x;
101  nodeArray[vertexDim*nv + 1] = y;
102  nodeArray[vertexDim*nv + 2] = 0.0;//could use a default
103  if (hasMarkers > 0)
104  nodeMaterialTypes[nv] = nodeId;
105  vertexFile >> eatline;
106  }//end iv
107  vertexFile.close();
108 
109  //read elements
110  int nNodesPerSimplex(simplexDim); hasMarkers = 0;
111  elementFile >> eatcomments >> nElements >> nNodesPerSimplex >> hasMarkers >> eatline;
112  assert(nElements > 0);
113  assert(nNodesPerSimplex == simplexDim); //not allow midface nodes yet
114 
115  elementNodesArray.resize(simplexDim*nElements);
116  elementMaterialTypes.resize(nElements,defaultElementMaterialType);
117 
118  for (int ie = 0; ie < nElements; ie++)
119  {
120  int ne, nv, elementId(0);
121  long double elementId_double;
122  elementFile >> eatcomments >> ne;
123  ne -= indexBase;
124  assert(0 <= ne && ne < nElements && elementFile.good());
125  for (int iv = 0; iv < simplexDim; iv++)
126  {
127  elementFile >> nv ; nv -= indexBase;
128  assert(0 <= nv && nv < nNodes);
129  elementNodesArray[simplexDim*ne + iv] = nv;
130 #ifdef DEBUG_LEVEL_2
131  std::cout<<"readNodesAndMesh ie= "<<ie<<" "<<iv<<" ---> "
132  <<nv<<std::endl;
133 #endif
134  }//end iv
135  if (hasMarkers > 0)
136  {
137  elementFile >> elementId_double;
138  //elementId = lrintl(elementId_double);
139  elementId = static_cast<long int>(elementId_double);
140  elementMaterialTypes[ne] = elementId;
141  }
142  elementFile >> eatline;
143  }//end ie
144  elementFile.close();
145 
146  return failed;
147 }//end readTriangleMesh
148 
149 bool
150 writeTriangleMeshNodesAndElements(const char * filebase,
151  const int& indexBase,
152  const int& nElements, const int& nNodes,
153  const double* nodeArray,
154  const int* elementNodesArray,
155  const int* nodeMaterialTypes,
156  const int* elementMaterialTypes)
157 {
158  bool failed = false;
159  const int vertexDim=3; const int simplexDim = 2+1;
160 
161  std::string vertexFileName = std::string(filebase) + ".node" ;
162  std::string elementFileName = std::string(filebase) + ".ele" ;
163 
164  std::ofstream vertexFile(vertexFileName.c_str());
165  std::ofstream elementFile(elementFileName.c_str());
166 
167  if (!vertexFile.good())
168  {
169  std::cerr<<"writeTriangleMeshNodesAndElements cannot open file "
170  <<vertexFileName<<std::endl;
171  failed = true;
172  return failed;
173  }
174  if (!elementFile.good())
175  {
176  std::cerr<<"writeTriangleMeshNodeAndElements cannot open file "
177  <<elementFileName<<std::endl;
178  failed = true;
179  return failed;
180  }
181 
182  int hasNodeMarkers(0);
183  if (nodeMaterialTypes)
184  hasNodeMarkers = 1;
185  //write vertices
186  vertexFile << nNodes <<" 2 0 " << hasNodeMarkers <<" #number of vertices, dim nAttributes has Markers"<<std::endl;
187  vertexFile <<"#vertex id x y [propid], base= "<<indexBase<<std::endl;
188  for (int iv = 0; iv < nNodes; iv++)
189  {
190  int nv = iv + indexBase;
191  double x = nodeArray[vertexDim*iv + 0];
192  double y = nodeArray[vertexDim*iv + 1];
193  //don't right z
194  vertexFile << nv <<" " <<x <<" "<< y;
195  if (hasNodeMarkers > 0)
196  vertexFile <<" "<<nodeMaterialTypes[iv];
197  vertexFile << std::endl;
198  }//end iv
199  vertexFile.close();
200 
201  //write elements
202  int hasElementMarkers(0);
203  if (elementMaterialTypes)
204  hasElementMarkers = 1;
205  elementFile << nElements <<" 3 "<< hasElementMarkers <<" #number of elements, nodes per triangle has Markers "<<std::endl;
206  elementFile <<"#element id nodes 0 1 2, [propid] base= "<<indexBase<<std::endl;
207  for (int ie = 0; ie < nElements; ie++)
208  {
209  int ne = ie + indexBase;
210  elementFile << ne;
211  for (int iv = 0; iv < simplexDim; iv++)
212  {
213  int nv = elementNodesArray[simplexDim*ie + iv];
214  nv += indexBase;
215  elementFile <<" "<< nv;
216  }//end iv
217  if (hasElementMarkers)
218  elementFile<<" "<<elementMaterialTypes[ie];
219  elementFile << std::endl;
220  }//end ie
221  elementFile.close();
222 
223  return failed;
224 }//end writeTriangleMesh
225 
226 /***********************************************************************
227  just read in triangle edge file, in case we need element boundary
228  identifiers
229  **********************************************************************/
230 
231 bool
232 readTriangleElementBoundaries(const char * filebase,
233  const int& indexBase,
234  bool& hasMarkers,
235  int& nElementBoundaries,
236  std::vector<int>& elementBoundaryNodesArray,
237  std::vector<int>& elementBoundaryMaterialTypesArray,
238  const int& defaultBoundaryMaterialType)
239 
240 {
241  bool failed = false;
242  const int elementBoundaryDim = 2;
243  using namespace IOutils;
244  std::string elementBoundaryFileName = std::string(filebase) + ".edge" ;
245 
246  std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
247 
248  if (!elementBoundaryFile.good())
249  {
250  std::cerr<<"readTriangleElementBoundaries cannot open file "
251  <<elementBoundaryFileName<<std::endl;
252  failed = true;
253  return failed;
254  }
255 
256  hasMarkers = false;
257  int ihasMarkers(0);
258  //read vertices
259  elementBoundaryFile >> eatcomments >> nElementBoundaries >> ihasMarkers >> eatline ;
260  assert(nElementBoundaries > 0);
261  if (ihasMarkers > 0)
262  hasMarkers = true;
263  elementBoundaryNodesArray.resize(elementBoundaryDim*nElementBoundaries);
264  elementBoundaryMaterialTypesArray.resize(nElementBoundaries,defaultBoundaryMaterialType);
265 
266  for (int ieb = 0; ieb < nElementBoundaries; ieb++)
267  {
268  int neb,nn0,nn1; int ebId(0);
269  elementBoundaryFile >> eatcomments >> neb >> nn0 >> nn1;
270  if (ihasMarkers > 0)
271  elementBoundaryFile >> ebId;
272  neb -= indexBase;
273  nn0 -= indexBase;
274  nn1 -= indexBase;
275  assert(0 <= neb && neb < nElementBoundaries && elementBoundaryFile.good());
276  elementBoundaryNodesArray[elementBoundaryDim*neb + 0] = nn0;
277  elementBoundaryNodesArray[elementBoundaryDim*neb + 1] = nn1;
278  if (ihasMarkers > 0)
279  elementBoundaryMaterialTypesArray[neb] = ebId;
280  elementBoundaryFile >> eatline;
281  }//end iv
282  elementBoundaryFile.close();
283 
284 
285  return failed;
286 }//end readTriangleElementBoundaries
287 
288 
289 bool
290 writeTriangleElementBoundaryNodes(const char * filebase,
291  const int& indexBase,
292  const int& nElementBoundaries,
293  const int* elementBoundaryNodesArray,
294  const int* elementBoundaryMaterialTypes)
295 {
296  bool failed = false;
297  const int elementBoundaryDim=2;
298 
299  std::string elementBoundaryFileName = std::string(filebase) + ".edge" ;
300 
301  std::ofstream elementBoundaryFile(elementBoundaryFileName.c_str());
302 
303  if (!elementBoundaryFile.good())
304  {
305  std::cerr<<"writeTriangleElementBoundaryNodes cannot open file "
306  <<elementBoundaryFileName<<std::endl;
307  failed = true;
308  return failed;
309  }
310 
311  int hasElementBoundaryMarkers(0);
312  if (elementBoundaryMaterialTypes)
313  hasElementBoundaryMarkers = 1;
314  //
315  elementBoundaryFile << nElementBoundaries <<" " << hasElementBoundaryMarkers <<" #number of elementBoundaries, has Markers"<<std::endl;
316  elementBoundaryFile <<"#elementBoundary node0 node1 [id], base= "<<indexBase<<std::endl;
317  for (int ieb = 0; ieb < nElementBoundaries; ieb++)
318  {
319  int neb = ieb + indexBase;
320  int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
321  nn0 += indexBase;
322  int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
323  nn1 += indexBase;
324  elementBoundaryFile << neb <<" " <<nn0 <<" "<< nn1;
325  if (hasElementBoundaryMarkers > 0)
326  elementBoundaryFile <<" "<<elementBoundaryMaterialTypes[ieb];
327  elementBoundaryFile << std::endl;
328  }//end iv
329  elementBoundaryFile.close();
330 
331  return failed;
332 }//end writeTriangleElementBoundaryNodes
333 
334 /***********************************************************************
335 
336  **********************************************************************/
337 
338 bool
339 readTetgenMeshNodesAndElements(const char * filebase,
340  const int& indexBase,
341  int& nElements, int& nNodes,
342  std::vector<double>& nodeArray,
343  std::vector<int>&
344  elementNodesArray,
345  std::vector<int>& nodeMaterialTypes,
346  std::vector<int>& elementMaterialTypes,
347  const int& defaultElementMaterialType,
348  const int& defaultNodeMaterialType)
349 {
350  bool failed = false;
351  const int simplexDim = 3+1;
352  const int vertexDim = 3; //always
353  using namespace IOutils;
354  std::string vertexFileName = std::string(filebase) + ".node" ;
355  std::string elementFileName = std::string(filebase) + ".ele" ;
356 
357  std::ifstream vertexFile(vertexFileName.c_str());
358  std::ifstream elementFile(elementFileName.c_str());
359 
360  if (!vertexFile.good())
361  {
362  std::cerr<<"readTetgenMeshNodeAndElements cannot open file "
363  <<vertexFileName<<std::endl;
364  failed = true;
365  return failed;
366  }
367  if (!elementFile.good())
368  {
369  std::cerr<<"readTetgenMeshNodeAndElements cannot open file "
370  <<elementFileName<<std::endl;
371  failed = true;
372  return failed;
373  }
374 
375  int hasMarkers(0),hasAttributes(0),nSpace(3);
376  //read vertices
377  vertexFile >> eatcomments >> nNodes >> nSpace >> hasAttributes >> hasMarkers >> eatline ;
378  assert(nNodes > 0);
379  assert(nSpace == 3);
380  if (hasAttributes > 0)
381  {
382  std::cerr<<"WARNING readTriangle nodes hasAttributes= "<<hasAttributes
383  <<" > 0 will treat first value as integer id for boundary!!"<<std::endl;
384  hasMarkers = 1;
385  }
386 
387  nodeArray.resize(vertexDim*nNodes);
388  nodeMaterialTypes.resize(nNodes,defaultNodeMaterialType);
389 
390  for (int iv = 0; iv < nNodes; iv++)
391  {
392  int nv; double x,y,z; int nodeId(0);
393  vertexFile >> eatcomments >> nv >> x >> y >> z;
394  if (hasMarkers > 0)
395  vertexFile >> nodeId;
396  nv -= indexBase;
397  assert(0 <= nv && nv < nNodes && vertexFile.good());
398  nodeArray[vertexDim*nv + 0] = x;
399  nodeArray[vertexDim*nv + 1] = y;
400  nodeArray[vertexDim*nv + 2] = z;
401  if (hasMarkers > 0)
402  nodeMaterialTypes[nv] = nodeId;
403  vertexFile >> eatline;
404  }//end iv
405  vertexFile.close();
406 
407  //read elements
408  int nNodesPerSimplex(simplexDim); hasMarkers = 0;
409  elementFile >> eatcomments >> nElements >> nNodesPerSimplex >> hasMarkers >> eatline;
410  assert(nElements > 0);
411  assert(nNodesPerSimplex == simplexDim); //not allow midface nodes yet
412 
413  elementNodesArray.resize(simplexDim*nElements);
414  elementMaterialTypes.resize(nElements,defaultElementMaterialType);
415 
416  for (int ie = 0; ie < nElements; ie++)
417  {
418  int ne, nv, elementId(0);
419  long double elementId_double;
420  elementFile >> eatcomments >> ne;
421  ne -= indexBase;
422  assert(0 <= ne && ne < nElements && elementFile.good());
423  for (int iv = 0; iv < simplexDim; iv++)
424  {
425  elementFile >> nv ; nv -= indexBase;
426  assert(0 <= nv && nv < nNodes);
427  elementNodesArray[simplexDim*ne + iv] = nv;
428 #ifdef DEBUG_LEVEL_2
429  std::cout<<"readNodesAndMesh ie= "<<ie<<" "<<iv<<" ---> "
430  <<nv<<std::endl;
431 #endif
432  }//end iv
433  if (hasMarkers > 0)
434  {
435  elementFile >> elementId_double;
436  //elementId = lrintl(elementId_double);
437  elementId = static_cast<long int>(elementId_double);
438  elementMaterialTypes[ne] = elementId;
439  }
440  elementFile >> eatline;
441  }//end ie
442  elementFile.close();
443 
444  return failed;
445 }//end readTetgenMesh
446 
447 bool
448 readTetgenElementBoundaries(const char * filebase,
449  const int& indexBase,
450  bool& hasMarkers,
451  int& nElementBoundaries,
452  std::vector<int>& elementBoundaryNodesArray,
453  std::vector<int>& elementBoundaryMaterialTypesArray,
454  const int& defaultBoundaryMaterialType)
455 
456 {
457  bool failed = false;
458  const int elementBoundaryDim = 3;
459  using namespace IOutils;
460  std::string elementBoundaryFileName = std::string(filebase) + ".face" ;
461 
462  std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
463 
464  if (!elementBoundaryFile.good())
465  {
466  std::cerr<<"readTetgenElementBoundaries cannot open file "
467  <<elementBoundaryFileName<<std::endl;
468  failed = true;
469  return failed;
470  }
471 
472  hasMarkers = false;
473  int ihasMarkers(0);
474  //read vertices
475  elementBoundaryFile >> eatcomments >> nElementBoundaries >> ihasMarkers >> eatline ;
476  assert(nElementBoundaries > 0);
477  if (ihasMarkers > 0)
478  hasMarkers = true;
479  elementBoundaryNodesArray.resize(elementBoundaryDim*nElementBoundaries);
480  elementBoundaryMaterialTypesArray.resize(nElementBoundaries,defaultBoundaryMaterialType);
481 
482  for (int ieb = 0; ieb < nElementBoundaries; ieb++)
483  {
484  int neb,nn0,nn1,nn2; int ebId(0);
485  elementBoundaryFile >> eatcomments >> neb >> nn0 >> nn1 >> nn2;
486  if (ihasMarkers > 0)
487  elementBoundaryFile >> ebId;
488  neb -= indexBase;
489  nn0 -= indexBase;
490  nn1 -= indexBase;
491  nn2 -= indexBase;
492  assert(0 <= neb && neb < nElementBoundaries && elementBoundaryFile.good());
493  elementBoundaryNodesArray[elementBoundaryDim*neb + 0] = nn0;
494  elementBoundaryNodesArray[elementBoundaryDim*neb + 1] = nn1;
495  elementBoundaryNodesArray[elementBoundaryDim*neb + 2] = nn2;
496  if (ihasMarkers > 0)
497  elementBoundaryMaterialTypesArray[neb] = ebId;
498  elementBoundaryFile >> eatline;
499  }//end iv
500  elementBoundaryFile.close();
501 
502 
503  return failed;
504 }//end readTetgenElementBoundaries
505 
506 
507 bool
508 writeTetgenMeshNodesAndElements(const char * filebase,
509  const int& indexBase,
510  const int& nElements, const int& nNodes,
511  const double* nodeArray,
512  const int* elementNodesArray,
513  const int* nodeMaterialTypes,
514  const int* elementMaterialTypes)
515 {
516  bool failed = false;
517  const int vertexDim=3; const int simplexDim = 3+1;
518 
519  std::string vertexFileName = std::string(filebase) + ".node" ;
520  std::string elementFileName = std::string(filebase) + ".ele" ;
521 
522  std::ofstream vertexFile(vertexFileName.c_str());
523  std::ofstream elementFile(elementFileName.c_str());
524 
525  if (!vertexFile.good())
526  {
527  std::cerr<<"writeTetgenMeshNodesAndElements cannot open file "
528  <<vertexFileName<<std::endl;
529  failed = true;
530  return failed;
531  }
532  if (!elementFile.good())
533  {
534  std::cerr<<"writeTetgenMeshNodeAndElements cannot open file "
535  <<elementFileName<<std::endl;
536  failed = true;
537  return failed;
538  }
539 
540  //write vertices
541  int hasNodeMarkers(0);
542  if (nodeMaterialTypes)
543  hasNodeMarkers = 1;
544  vertexFile << nNodes <<" 3 0 " << hasNodeMarkers <<" #number of vertices, dim nAttributes has Markers"<<std::endl;
545  vertexFile <<"#vertex id x y z [propid], base= "<<indexBase<<std::endl;
546  for (int iv = 0; iv < nNodes; iv++)
547  {
548  int nv = iv + indexBase;
549  double x = nodeArray[vertexDim*iv + 0];
550  double y = nodeArray[vertexDim*iv + 1];
551  double z = nodeArray[vertexDim*iv + 2];
552  //don't right z
553  vertexFile << nv <<" " <<x <<" "<< y <<" " << z;
554  if (hasNodeMarkers > 0)
555  vertexFile <<" "<<nodeMaterialTypes[iv];
556  vertexFile << std::endl;
557  }//end iv
558  vertexFile.close();
559 
560  //write elements
561  int hasElementMarkers(0);
562  if (elementMaterialTypes)
563  hasElementMarkers = 1;
564  elementFile << nElements <<" 4 "<< hasElementMarkers <<" #number of elements, nodes per simplex has Markers "<<std::endl;
565  elementFile <<"#element id nodes 0 1 2 3, [propid] base= "<<indexBase<<std::endl;
566  for (int ie = 0; ie < nElements; ie++)
567  {
568  int ne = ie + indexBase;
569  elementFile << ne;
570  for (int iv = 0; iv < simplexDim; iv++)
571  {
572  int nv = elementNodesArray[simplexDim*ie + iv];
573  nv += indexBase;
574  elementFile <<" "<< nv;
575  }//end iv
576  if (hasElementMarkers)
577  elementFile<<" "<<elementMaterialTypes[ie];
578  elementFile << std::endl;
579  }//end ie
580  elementFile.close();
581 
582  return failed;
583 }//end writeTetgenMesh
584 
585 bool
586 writeTetgenElementBoundaryNodes(const char * filebase,
587  const int& indexBase,
588  const int& nElementBoundariesToWrite,
589  const int* elementBoundaryNodesArray,
590  const int* elementBoundaryMaterialTypes,
591  const bool& writeExteriorElementBoundariesOnly,
592  const int* exteriorElementBoundariesArray)
593 {
594  bool failed = false;
595  const int elementBoundaryDim=3;
596 
597  std::string elementBoundaryFileName = std::string(filebase) + ".face" ;
598 
599  std::ofstream elementBoundaryFile(elementBoundaryFileName.c_str());
600 
601  if (!elementBoundaryFile.good())
602  {
603  std::cerr<<"writeTetgenElementBoundaryNodes cannot open file "
604  <<elementBoundaryFileName<<std::endl;
605  failed = true;
606  return failed;
607  }
608 
609  int hasElementBoundaryMarkers(0);
610  if (elementBoundaryMaterialTypes)
611  hasElementBoundaryMarkers = 1;
612  //
613  if (writeExteriorElementBoundariesOnly)
614  elementBoundaryFile << nElementBoundariesToWrite <<" " << hasElementBoundaryMarkers
615  <<" #number of exterior elementBoundaries, has Markers"<<std::endl;
616  else
617  elementBoundaryFile << nElementBoundariesToWrite <<" " << hasElementBoundaryMarkers
618  <<" #number of elementBoundaries, has Markers"<<std::endl;
619 
620  elementBoundaryFile <<"#elementBoundary node0 node1 node2 [id], base= "<<indexBase<<std::endl;
621  if (writeExteriorElementBoundariesOnly)
622  {
623  assert(exteriorElementBoundariesArray);
624  for (int iebe = 0; iebe < nElementBoundariesToWrite; iebe++)
625  {
626  int niebe = iebe + indexBase; //number output by exterior boundary number
627  int ieb = exteriorElementBoundariesArray[iebe];
628  int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
629  nn0 += indexBase;
630  int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
631  nn1 += indexBase;
632  int nn2 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 2];
633  nn2 += indexBase;
634 
635  elementBoundaryFile << niebe <<" " <<nn0 <<" "<< nn1 <<" "<< nn2;
636  if (hasElementBoundaryMarkers > 0)
637  elementBoundaryFile <<" "<<elementBoundaryMaterialTypes[ieb];
638  elementBoundaryFile << std::endl;
639  }
640  }
641  else
642  {
643  for (int ieb = 0; ieb < nElementBoundariesToWrite; ieb++)
644  {
645  int neb = ieb + indexBase;
646  int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
647  nn0 += indexBase;
648  int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
649  nn1 += indexBase;
650  int nn2 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 2];
651  nn2 += indexBase;
652  elementBoundaryFile << neb <<" " <<nn0 <<" "<< nn1 <<" "<< nn2;
653  if (hasElementBoundaryMarkers > 0)
654  elementBoundaryFile <<" "<<elementBoundaryMaterialTypes[ieb];
655  elementBoundaryFile << std::endl;
656  }//end iv
657  }
658  elementBoundaryFile.close();
659 
660  return failed;
661 }//end writeTriangleElementBoundaryNodes
662 
663 
664 /*************************************************************
665 
666 ************************************************************/
667 bool
668 write3dmMeshNodesAndElements(const char * filebase,
669  const int& indexBase,
670  const int& nElements, const int& nNodes,
671  const double* nodeArray,
672  const int* elementNodesArray,
673  const int* elementMaterialTypes)
674 {
675 
676  bool failed = false;
677  const int vertexDim=3; const int simplexDim = 3+1;
678 
679  std::string meshFileName = std::string(filebase) + ".3dm";
680  std::ofstream meshFile(meshFileName.c_str());
681 
682  if (!meshFile.good())
683  {
684  std::cerr<<"write3dmMeshNodesAndElements cannot open file "
685  <<meshFileName<<std::endl;
686  failed = true;
687  return failed;
688  }
689  meshFile <<"MESH3D"<<std::endl;
690  for (int eN = 0; eN < nElements; eN++)
691  {
692  meshFile << "E4T "<<eN + indexBase
693  <<" " <<elementNodesArray[eN*simplexDim + 0] + indexBase
694  <<" " <<elementNodesArray[eN*simplexDim + 1] + indexBase
695  <<" " <<elementNodesArray[eN*simplexDim + 2] + indexBase
696  <<" " <<elementNodesArray[eN*simplexDim + 3] + indexBase;
697  if (!elementMaterialTypes)
698  meshFile <<" 1 "<<std::endl;
699  else
700  meshFile <<" "<<elementMaterialTypes[eN]+indexBase<<std::endl;
701  }
702  meshFile << std::endl;
703  meshFile << std::setiosflags(std::ios::scientific) << std::setprecision(8);
704  for (int nN = 0; nN < nNodes; nN++)
705  {
706  meshFile <<"ND "<<nN + indexBase
707  <<" "<<nodeArray[nN*vertexDim + 0]
708  <<" "<<nodeArray[nN*vertexDim + 1]
709  <<" "<<nodeArray[nN*vertexDim + 2]
710  <<std::endl;
711  }
712  meshFile <<"END"<<std::endl;
713  meshFile.close();
714  return false;
715 }
716 
717 bool
718 write2dmMeshNodesAndElements(const char * filebase,
719  const int& indexBase,
720  const int& nElements, const int& nNodes,
721  const double* nodeArray,
722  const int* elementNodesArray,
723  const int* elementMaterialTypes)
724 {
725 
726  bool failed = false;
727  const int vertexDim=3; const int simplexDim = 2+1;
728 
729  std::string meshFileName = std::string(filebase) + ".3dm";
730  std::ofstream meshFile(meshFileName.c_str());
731 
732  if (!meshFile.good())
733  {
734  std::cerr<<"write2dmMeshNodesAndElements cannot open file "
735  <<meshFileName<<std::endl;
736  failed = true;
737  return failed;
738  }
739  meshFile <<"MESH2D"<<std::endl;
740  for (int eN = 0; eN < nElements; eN++)
741  {
742  meshFile << "E3T"<<std::setw(11)<<eN + indexBase
743  <<std::setw(11)<<elementNodesArray[eN*simplexDim + 0] + indexBase
744  <<std::setw(11)<<elementNodesArray[eN*simplexDim + 1] + indexBase
745  <<std::setw(11)<<elementNodesArray[eN*simplexDim + 2] + indexBase;
746  if (!elementMaterialTypes)
747  meshFile <<std::setw(11)<<1<<std::endl;
748  else
749  meshFile <<std::setw(11)<<elementMaterialTypes[eN]<<std::endl;
750  }
751  meshFile << std::setiosflags(std::ios::scientific) << std::setprecision(8);
752  for (int nN = 0; nN < nNodes; nN++)
753  {
754  meshFile <<"ND"<<std::setw(11)<<nN + indexBase
755  <<std::setw(17)<<nodeArray[nN*vertexDim + 0]
756  <<std::setw(17)<<nodeArray[nN*vertexDim + 1]
757  <<std::setw(17)<<nodeArray[nN*vertexDim + 2]
758  <<std::endl;
759  }
760  meshFile.close();
761  return false;
762 }
763 
764 }//meshIO
IOutils
Definition: meshio.cpp:10
s
Double s
Definition: Headers.h:84
meshIO::writeTetgenMeshNodesAndElements
bool writeTetgenMeshNodesAndElements(const char *filebase, const int &indexBase, const int &nElements, const int &nNodes, const double *nodeArray, const int *elementNodesArray, const int *nodeMaterialTypes, const int *elementMaterialTypes)
Definition: meshio.cpp:508
meshio.h
meshIO::writeTriangleElementBoundaryNodes
bool writeTriangleElementBoundaryNodes(const char *filebase, const int &indexBase, const int &nElementBoundaries, const int *elementBoundaryNodesArray, const int *elementBoundaryMaterialTypes)
Definition: meshio.cpp:290
IOutils::eatcomments
std::istream & eatcomments(std::istream &s)
Definition: meshio.cpp:21
meshIO
Definition: meshio.cpp:34
meshIO::write2dmMeshNodesAndElements
bool write2dmMeshNodesAndElements(const char *filebase, const int &indexBase, const int &nElements, const int &nNodes, const double *nodeArray, const int *elementNodesArray, const int *elementMaterialTypes)
Definition: meshio.cpp:718
IOutils::eatchar
std::istream & eatchar(std::istream &s)
Definition: meshio.cpp:17
z
Double * z
Definition: Headers.h:49
IOutils::eatline
std::istream & eatline(std::istream &s)
Definition: meshio.cpp:11
c
Double c
Definition: Headers.h:54
meshIO::write3dmMeshNodesAndElements
bool write3dmMeshNodesAndElements(const char *filebase, const int &indexBase, const int &nElements, const int &nNodes, const double *nodeArray, const int *elementNodesArray, const int *elementMaterialTypes)
Definition: meshio.cpp:668
meshIO::readTriangleMeshNodesAndElements
bool readTriangleMeshNodesAndElements(const char *filebase, const int &indexBase, int &nElements, int &nNodes, std::vector< double > &nodeArray, std::vector< int > &elementNodesArray, std::vector< int > &nodeMaterialTypes, std::vector< int > &elementMaterialTypes, const int &defaultElementMaterialType, const int &defaultNodeMaterialType)
Definition: meshio.cpp:41
meshIO::writeTetgenElementBoundaryNodes
bool writeTetgenElementBoundaryNodes(const char *filebase, const int &indexBase, const int &nElementBoundariesToWrite, const int *elementBoundaryNodesArray, const int *elementBoundaryMaterialTypes, const bool &writeExteriorElementBoundariesOnly, const int *exteriorElementBoundariesArray)
Definition: meshio.cpp:586
IOutils::iswhitespace
bool iswhitespace(const char &c)
Definition: meshio.cpp:19
meshIO::readTetgenMeshNodesAndElements
bool readTetgenMeshNodesAndElements(const char *filebase, const int &indexBase, int &nElements, int &nNodes, std::vector< double > &nodeArray, std::vector< int > &elementNodesArray, std::vector< int > &nodeMaterialTypes, std::vector< int > &elementMaterialTypes, const int &defaultElementMaterialType, const int &defaultNodeMaterialType)
Definition: meshio.cpp:339
meshIO::writeTriangleMeshNodesAndElements
bool writeTriangleMeshNodesAndElements(const char *filebase, const int &indexBase, const int &nElements, const int &nNodes, const double *nodeArray, const int *elementNodesArray, const int *nodeMaterialTypes, const int *elementMaterialTypes)
Definition: meshio.cpp:150
meshIO::readTetgenElementBoundaries
bool readTetgenElementBoundaries(const char *filebase, const int &indexBase, bool &hasMarkers, int &nElementBoundaries, std::vector< int > &elementBoundaryNodesArray, std::vector< int > &elementBoundaryMaterialTypesArray, const int &defaultBoundaryMaterialType)
Definition: meshio.cpp:448
meshIO::readTriangleElementBoundaries
bool readTriangleElementBoundaries(const char *filebase, const int &indexBase, bool &hasMarkers, int &nElementBoundaries, std::vector< int > &elementBoundaryNodesArray, std::vector< int > &elementBoundaryMaterialTypesArray, const int &defaultBoundaryMaterialType)
Definition: meshio.cpp:232