13 while (
s.get() !=
'\n' &&
s.good()) {}
18 {
s.get() ;
return s ; }
20 {
return c ==
'\n' ||
c ==
' ' ||
c ==
'\t' ||
c ==
'\r';}
25 while ( (
c ==
'\n' ||
c ==
'!' ||
c ==
'%' ||
c ==
'#' ||
c ==
';' ||
c ==
'$')
43 int& nElements,
int& nNodes,
44 std::vector<double>& nodeArray,
47 std::vector<int>& nodeMaterialTypes,
48 std::vector<int>& elementMaterialTypes,
49 const int& defaultElementMaterialType,
50 const int& defaultNodeMaterialType)
53 const int simplexDim = 2+1;
54 const int vertexDim =3;
56 std::string vertexFileName = std::string(filebase) +
".node" ;
57 std::string elementFileName = std::string(filebase) +
".ele" ;
59 std::ifstream vertexFile(vertexFileName.c_str());
60 std::ifstream elementFile(elementFileName.c_str());
62 if (!vertexFile.good())
64 std::cerr<<
"readTriangleMeshNodeAndElements cannot open file "
65 <<vertexFileName<<std::endl;
69 if (!elementFile.good())
71 std::cerr<<
"readTriangleMeshNodeAndElements cannot open file "
72 <<elementFileName<<std::endl;
77 int hasMarkers(0),hasAttributes(0),nSpace(2);
79 vertexFile >>
eatcomments >> nNodes >> nSpace >> hasAttributes
83 if (hasAttributes > 0)
85 std::cerr<<
"WARNING readTriangle nodes hasAttributes= "<<hasAttributes
86 <<
" > 0 will treat first value as integer id for boundary!!"<<std::endl;
89 nodeArray.resize(vertexDim*nNodes);
90 nodeMaterialTypes.resize(nNodes,defaultNodeMaterialType);
92 for (
int iv = 0; iv < nNodes; iv++)
94 int nv;
double x,y;
int nodeId(0);
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;
104 nodeMaterialTypes[nv] = nodeId;
110 int nNodesPerSimplex(simplexDim); hasMarkers = 0;
112 assert(nElements > 0);
113 assert(nNodesPerSimplex == simplexDim);
115 elementNodesArray.resize(simplexDim*nElements);
116 elementMaterialTypes.resize(nElements,defaultElementMaterialType);
118 for (
int ie = 0; ie < nElements; ie++)
120 int ne, nv, elementId(0);
121 long double elementId_double;
124 assert(0 <= ne && ne < nElements && elementFile.good());
125 for (
int iv = 0; iv < simplexDim; iv++)
127 elementFile >> nv ; nv -= indexBase;
128 assert(0 <= nv && nv < nNodes);
129 elementNodesArray[simplexDim*ne + iv] = nv;
131 std::cout<<
"readNodesAndMesh ie= "<<ie<<
" "<<iv<<
" ---> "
137 elementFile >> elementId_double;
139 elementId =
static_cast<long int>(elementId_double);
140 elementMaterialTypes[ne] = elementId;
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)
159 const int vertexDim=3;
const int simplexDim = 2+1;
161 std::string vertexFileName = std::string(filebase) +
".node" ;
162 std::string elementFileName = std::string(filebase) +
".ele" ;
164 std::ofstream vertexFile(vertexFileName.c_str());
165 std::ofstream elementFile(elementFileName.c_str());
167 if (!vertexFile.good())
169 std::cerr<<
"writeTriangleMeshNodesAndElements cannot open file "
170 <<vertexFileName<<std::endl;
174 if (!elementFile.good())
176 std::cerr<<
"writeTriangleMeshNodeAndElements cannot open file "
177 <<elementFileName<<std::endl;
182 int hasNodeMarkers(0);
183 if (nodeMaterialTypes)
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++)
190 int nv = iv + indexBase;
191 double x = nodeArray[vertexDim*iv + 0];
192 double y = nodeArray[vertexDim*iv + 1];
194 vertexFile << nv <<
" " <<x <<
" "<< y;
195 if (hasNodeMarkers > 0)
196 vertexFile <<
" "<<nodeMaterialTypes[iv];
197 vertexFile << std::endl;
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++)
209 int ne = ie + indexBase;
211 for (
int iv = 0; iv < simplexDim; iv++)
213 int nv = elementNodesArray[simplexDim*ie + iv];
215 elementFile <<
" "<< nv;
217 if (hasElementMarkers)
218 elementFile<<
" "<<elementMaterialTypes[ie];
219 elementFile << std::endl;
233 const int& indexBase,
235 int& nElementBoundaries,
236 std::vector<int>& elementBoundaryNodesArray,
237 std::vector<int>& elementBoundaryMaterialTypesArray,
238 const int& defaultBoundaryMaterialType)
242 const int elementBoundaryDim = 2;
244 std::string elementBoundaryFileName = std::string(filebase) +
".edge" ;
246 std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
248 if (!elementBoundaryFile.good())
250 std::cerr<<
"readTriangleElementBoundaries cannot open file "
251 <<elementBoundaryFileName<<std::endl;
260 assert(nElementBoundaries > 0);
263 elementBoundaryNodesArray.resize(elementBoundaryDim*nElementBoundaries);
264 elementBoundaryMaterialTypesArray.resize(nElementBoundaries,defaultBoundaryMaterialType);
266 for (
int ieb = 0; ieb < nElementBoundaries; ieb++)
268 int neb,nn0,nn1;
int ebId(0);
269 elementBoundaryFile >>
eatcomments >> neb >> nn0 >> nn1;
271 elementBoundaryFile >> ebId;
275 assert(0 <= neb && neb < nElementBoundaries && elementBoundaryFile.good());
276 elementBoundaryNodesArray[elementBoundaryDim*neb + 0] = nn0;
277 elementBoundaryNodesArray[elementBoundaryDim*neb + 1] = nn1;
279 elementBoundaryMaterialTypesArray[neb] = ebId;
280 elementBoundaryFile >>
eatline;
282 elementBoundaryFile.close();
291 const int& indexBase,
292 const int& nElementBoundaries,
293 const int* elementBoundaryNodesArray,
294 const int* elementBoundaryMaterialTypes)
297 const int elementBoundaryDim=2;
299 std::string elementBoundaryFileName = std::string(filebase) +
".edge" ;
301 std::ofstream elementBoundaryFile(elementBoundaryFileName.c_str());
303 if (!elementBoundaryFile.good())
305 std::cerr<<
"writeTriangleElementBoundaryNodes cannot open file "
306 <<elementBoundaryFileName<<std::endl;
311 int hasElementBoundaryMarkers(0);
312 if (elementBoundaryMaterialTypes)
313 hasElementBoundaryMarkers = 1;
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++)
319 int neb = ieb + indexBase;
320 int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
322 int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
324 elementBoundaryFile << neb <<
" " <<nn0 <<
" "<< nn1;
325 if (hasElementBoundaryMarkers > 0)
326 elementBoundaryFile <<
" "<<elementBoundaryMaterialTypes[ieb];
327 elementBoundaryFile << std::endl;
329 elementBoundaryFile.close();
340 const int& indexBase,
341 int& nElements,
int& nNodes,
342 std::vector<double>& nodeArray,
345 std::vector<int>& nodeMaterialTypes,
346 std::vector<int>& elementMaterialTypes,
347 const int& defaultElementMaterialType,
348 const int& defaultNodeMaterialType)
351 const int simplexDim = 3+1;
352 const int vertexDim = 3;
354 std::string vertexFileName = std::string(filebase) +
".node" ;
355 std::string elementFileName = std::string(filebase) +
".ele" ;
357 std::ifstream vertexFile(vertexFileName.c_str());
358 std::ifstream elementFile(elementFileName.c_str());
360 if (!vertexFile.good())
362 std::cerr<<
"readTetgenMeshNodeAndElements cannot open file "
363 <<vertexFileName<<std::endl;
367 if (!elementFile.good())
369 std::cerr<<
"readTetgenMeshNodeAndElements cannot open file "
370 <<elementFileName<<std::endl;
375 int hasMarkers(0),hasAttributes(0),nSpace(3);
377 vertexFile >>
eatcomments >> nNodes >> nSpace >> hasAttributes >> hasMarkers >>
eatline ;
380 if (hasAttributes > 0)
382 std::cerr<<
"WARNING readTriangle nodes hasAttributes= "<<hasAttributes
383 <<
" > 0 will treat first value as integer id for boundary!!"<<std::endl;
387 nodeArray.resize(vertexDim*nNodes);
388 nodeMaterialTypes.resize(nNodes,defaultNodeMaterialType);
390 for (
int iv = 0; iv < nNodes; iv++)
392 int nv;
double x,y,
z;
int nodeId(0);
395 vertexFile >> nodeId;
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;
402 nodeMaterialTypes[nv] = nodeId;
408 int nNodesPerSimplex(simplexDim); hasMarkers = 0;
410 assert(nElements > 0);
411 assert(nNodesPerSimplex == simplexDim);
413 elementNodesArray.resize(simplexDim*nElements);
414 elementMaterialTypes.resize(nElements,defaultElementMaterialType);
416 for (
int ie = 0; ie < nElements; ie++)
418 int ne, nv, elementId(0);
419 long double elementId_double;
422 assert(0 <= ne && ne < nElements && elementFile.good());
423 for (
int iv = 0; iv < simplexDim; iv++)
425 elementFile >> nv ; nv -= indexBase;
426 assert(0 <= nv && nv < nNodes);
427 elementNodesArray[simplexDim*ne + iv] = nv;
429 std::cout<<
"readNodesAndMesh ie= "<<ie<<
" "<<iv<<
" ---> "
435 elementFile >> elementId_double;
437 elementId =
static_cast<long int>(elementId_double);
438 elementMaterialTypes[ne] = elementId;
449 const int& indexBase,
451 int& nElementBoundaries,
452 std::vector<int>& elementBoundaryNodesArray,
453 std::vector<int>& elementBoundaryMaterialTypesArray,
454 const int& defaultBoundaryMaterialType)
458 const int elementBoundaryDim = 3;
460 std::string elementBoundaryFileName = std::string(filebase) +
".face" ;
462 std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
464 if (!elementBoundaryFile.good())
466 std::cerr<<
"readTetgenElementBoundaries cannot open file "
467 <<elementBoundaryFileName<<std::endl;
476 assert(nElementBoundaries > 0);
479 elementBoundaryNodesArray.resize(elementBoundaryDim*nElementBoundaries);
480 elementBoundaryMaterialTypesArray.resize(nElementBoundaries,defaultBoundaryMaterialType);
482 for (
int ieb = 0; ieb < nElementBoundaries; ieb++)
484 int neb,nn0,nn1,nn2;
int ebId(0);
485 elementBoundaryFile >>
eatcomments >> neb >> nn0 >> nn1 >> nn2;
487 elementBoundaryFile >> ebId;
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;
497 elementBoundaryMaterialTypesArray[neb] = ebId;
498 elementBoundaryFile >>
eatline;
500 elementBoundaryFile.close();
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)
517 const int vertexDim=3;
const int simplexDim = 3+1;
519 std::string vertexFileName = std::string(filebase) +
".node" ;
520 std::string elementFileName = std::string(filebase) +
".ele" ;
522 std::ofstream vertexFile(vertexFileName.c_str());
523 std::ofstream elementFile(elementFileName.c_str());
525 if (!vertexFile.good())
527 std::cerr<<
"writeTetgenMeshNodesAndElements cannot open file "
528 <<vertexFileName<<std::endl;
532 if (!elementFile.good())
534 std::cerr<<
"writeTetgenMeshNodeAndElements cannot open file "
535 <<elementFileName<<std::endl;
541 int hasNodeMarkers(0);
542 if (nodeMaterialTypes)
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++)
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];
553 vertexFile << nv <<
" " <<x <<
" "<< y <<
" " <<
z;
554 if (hasNodeMarkers > 0)
555 vertexFile <<
" "<<nodeMaterialTypes[iv];
556 vertexFile << std::endl;
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++)
568 int ne = ie + indexBase;
570 for (
int iv = 0; iv < simplexDim; iv++)
572 int nv = elementNodesArray[simplexDim*ie + iv];
574 elementFile <<
" "<< nv;
576 if (hasElementMarkers)
577 elementFile<<
" "<<elementMaterialTypes[ie];
578 elementFile << std::endl;
587 const int& indexBase,
588 const int& nElementBoundariesToWrite,
589 const int* elementBoundaryNodesArray,
590 const int* elementBoundaryMaterialTypes,
591 const bool& writeExteriorElementBoundariesOnly,
592 const int* exteriorElementBoundariesArray)
595 const int elementBoundaryDim=3;
597 std::string elementBoundaryFileName = std::string(filebase) +
".face" ;
599 std::ofstream elementBoundaryFile(elementBoundaryFileName.c_str());
601 if (!elementBoundaryFile.good())
603 std::cerr<<
"writeTetgenElementBoundaryNodes cannot open file "
604 <<elementBoundaryFileName<<std::endl;
609 int hasElementBoundaryMarkers(0);
610 if (elementBoundaryMaterialTypes)
611 hasElementBoundaryMarkers = 1;
613 if (writeExteriorElementBoundariesOnly)
614 elementBoundaryFile << nElementBoundariesToWrite <<
" " << hasElementBoundaryMarkers
615 <<
" #number of exterior elementBoundaries, has Markers"<<std::endl;
617 elementBoundaryFile << nElementBoundariesToWrite <<
" " << hasElementBoundaryMarkers
618 <<
" #number of elementBoundaries, has Markers"<<std::endl;
620 elementBoundaryFile <<
"#elementBoundary node0 node1 node2 [id], base= "<<indexBase<<std::endl;
621 if (writeExteriorElementBoundariesOnly)
623 assert(exteriorElementBoundariesArray);
624 for (
int iebe = 0; iebe < nElementBoundariesToWrite; iebe++)
626 int niebe = iebe + indexBase;
627 int ieb = exteriorElementBoundariesArray[iebe];
628 int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
630 int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
632 int nn2 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 2];
635 elementBoundaryFile << niebe <<
" " <<nn0 <<
" "<< nn1 <<
" "<< nn2;
636 if (hasElementBoundaryMarkers > 0)
637 elementBoundaryFile <<
" "<<elementBoundaryMaterialTypes[ieb];
638 elementBoundaryFile << std::endl;
643 for (
int ieb = 0; ieb < nElementBoundariesToWrite; ieb++)
645 int neb = ieb + indexBase;
646 int nn0 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 0];
648 int nn1 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 1];
650 int nn2 = elementBoundaryNodesArray[elementBoundaryDim*ieb + 2];
652 elementBoundaryFile << neb <<
" " <<nn0 <<
" "<< nn1 <<
" "<< nn2;
653 if (hasElementBoundaryMarkers > 0)
654 elementBoundaryFile <<
" "<<elementBoundaryMaterialTypes[ieb];
655 elementBoundaryFile << std::endl;
658 elementBoundaryFile.close();
669 const int& indexBase,
670 const int& nElements,
const int& nNodes,
671 const double* nodeArray,
672 const int* elementNodesArray,
673 const int* elementMaterialTypes)
677 const int vertexDim=3;
const int simplexDim = 3+1;
679 std::string meshFileName = std::string(filebase) +
".3dm";
680 std::ofstream meshFile(meshFileName.c_str());
682 if (!meshFile.good())
684 std::cerr<<
"write3dmMeshNodesAndElements cannot open file "
685 <<meshFileName<<std::endl;
689 meshFile <<
"MESH3D"<<std::endl;
690 for (
int eN = 0; eN < nElements; eN++)
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;
700 meshFile <<
" "<<elementMaterialTypes[eN]+indexBase<<std::endl;
702 meshFile << std::endl;
703 meshFile << std::setiosflags(std::ios::scientific) << std::setprecision(8);
704 for (
int nN = 0; nN < nNodes; nN++)
706 meshFile <<
"ND "<<nN + indexBase
707 <<
" "<<nodeArray[nN*vertexDim + 0]
708 <<
" "<<nodeArray[nN*vertexDim + 1]
709 <<
" "<<nodeArray[nN*vertexDim + 2]
712 meshFile <<
"END"<<std::endl;
719 const int& indexBase,
720 const int& nElements,
const int& nNodes,
721 const double* nodeArray,
722 const int* elementNodesArray,
723 const int* elementMaterialTypes)
727 const int vertexDim=3;
const int simplexDim = 2+1;
729 std::string meshFileName = std::string(filebase) +
".3dm";
730 std::ofstream meshFile(meshFileName.c_str());
732 if (!meshFile.good())
734 std::cerr<<
"write2dmMeshNodesAndElements cannot open file "
735 <<meshFileName<<std::endl;
739 meshFile <<
"MESH2D"<<std::endl;
740 for (
int eN = 0; eN < nElements; eN++)
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;
749 meshFile <<std::setw(11)<<elementMaterialTypes[eN]<<std::endl;
751 meshFile << std::setiosflags(std::ios::scientific) << std::setprecision(8);
752 for (
int nN = 0; nN < nNodes; nN++)
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]