VMTK
Loading...
Searching...
No Matches
vtkvmtkTetGenReader Class Reference

Reads unstructured grid data from Tetgen node/elem format. More...

#include <vtkvmtkTetGenReader.h>

Inheritance diagram for vtkvmtkTetGenReader:
[legend]
Collaboration diagram for vtkvmtkTetGenReader:
[legend]

Public Types

typedef vtkUnstructuredGridReader Superclass

Public Member Functions

virtual int IsA (const char *type)
vtkvmtkTetGenReader * NewInstance () const
void PrintSelf (std::ostream &os, vtkIndent indent) override
int ReadMeshSimple (const std::string &fname, vtkDataObject *output) override
virtual void SetBoundaryDataArrayName (const char *)
virtual char * GetBoundaryDataArrayName ()

Static Public Member Functions

static int IsTypeOf (const char *type)
static vtkvmtkTetGenReader * SafeDownCast (vtkObjectBase *o)
static vtkvmtkTetGenReader * New ()

Protected Member Functions

virtual vtkObjectBase * NewInstanceInternal () const
 vtkvmtkTetGenReader ()
 ~vtkvmtkTetGenReader ()
void Tokenize (const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)

Protected Attributes

char * BoundaryDataArrayName

Detailed Description

Reads unstructured grid data from Tetgen node/elem format.

vtkvmtkTetGenReader reads unstructured grid data from Tetgen node/elem format Thanks to Sebastian Ordas for getting the class going.

Definition at line 40 of file vtkvmtkTetGenReader.h.

Member Typedef Documentation

◆ Superclass

typedef vtkUnstructuredGridReader vtkvmtkTetGenReader::Superclass

Definition at line 43 of file vtkvmtkTetGenReader.h.

Constructor & Destructor Documentation

◆ vtkvmtkTetGenReader()

vtkvmtkTetGenReader::vtkvmtkTetGenReader ( )
protected

Definition at line 43 of file vtkvmtkTetGenReader.cxx.

44{
45 this->BoundaryDataArrayName = NULL;
46}

◆ ~vtkvmtkTetGenReader()

vtkvmtkTetGenReader::~vtkvmtkTetGenReader ( )
protected

Definition at line 48 of file vtkvmtkTetGenReader.cxx.

49{
50 if (this->BoundaryDataArrayName)
51 {
52 delete[] this->BoundaryDataArrayName;
53 this->BoundaryDataArrayName = NULL;
54 }
55}

Member Function Documentation

◆ IsTypeOf()

int vtkvmtkTetGenReader::IsTypeOf ( const char * type)
static

◆ IsA()

virtual int vtkvmtkTetGenReader::IsA ( const char * type)
virtual

◆ SafeDownCast()

vtkvmtkTetGenReader * vtkvmtkTetGenReader::SafeDownCast ( vtkObjectBase * o)
static

◆ NewInstanceInternal()

virtual vtkObjectBase * vtkvmtkTetGenReader::NewInstanceInternal ( ) const
protectedvirtual

◆ NewInstance()

vtkvmtkTetGenReader * vtkvmtkTetGenReader::NewInstance ( ) const

◆ PrintSelf()

void vtkvmtkTetGenReader::PrintSelf ( std::ostream & os,
vtkIndent indent )
override

Definition at line 290 of file vtkvmtkTetGenReader.cxx.

291{
292 Superclass::PrintSelf(os,indent);
293}

◆ New()

vtkvmtkTetGenReader * vtkvmtkTetGenReader::New ( )
static

◆ SetBoundaryDataArrayName()

virtual void vtkvmtkTetGenReader::SetBoundaryDataArrayName ( const char * )
virtual

Set/get the name used for the point data and cell data arrays that store TetGen boundary marker values. Point boundary markers are read verbatim from the .node file (when present); the per-tetrahedron cell marker is the maximum node boundary marker found among a tetrahedron's points (excluding the first). If TetGen's .node file has no boundary marker column, no arrays are added. Commonly named "CellEntityIds".

◆ GetBoundaryDataArrayName()

virtual char * vtkvmtkTetGenReader::GetBoundaryDataArrayName ( )
virtual

Set/get the name used for the point data and cell data arrays that store TetGen boundary marker values. Point boundary markers are read verbatim from the .node file (when present); the per-tetrahedron cell marker is the maximum node boundary marker found among a tetrahedron's points (excluding the first). If TetGen's .node file has no boundary marker column, no arrays are added. Commonly named "CellEntityIds".

◆ ReadMeshSimple()

int vtkvmtkTetGenReader::ReadMeshSimple ( const std::string & fname,
vtkDataObject * output )
override

Definition at line 75 of file vtkvmtkTetGenReader.cxx.

77{
78
79 vtkDebugMacro(<<"Reading TetGen file...");
80
81 vtkUnstructuredGrid* output = vtkUnstructuredGrid::SafeDownCast(doOutput);
82
83 if(fname.empty())
84 {
85 vtkErrorMacro(<<"Input filename not set");
86 return 1;
87 }
88
89 std::string nodeFileName = fname;
90 nodeFileName += ".node";
91
92 std::string eleFileName = fname;
93 eleFileName += ".ele";
94
95 std::ifstream nodeStream(nodeFileName.c_str());
96 std::ifstream eleStream(eleFileName.c_str());
97
98 if (!nodeStream.good())
99 {
100 vtkErrorMacro(<<"Unable to open " << nodeFileName << " for reading");
101 return 0;
102 }
103
104 if (!eleStream.good())
105 {
106 vtkErrorMacro(<<"Unable to open " << eleFileName << " for reading");
107 return 0;
108 }
109
110 std::string line;
111 std::string delimiter(" ");
112 std::vector<std::string> tokens;
113
114 std::getline(nodeStream,line);
115 this->Tokenize(line,tokens,delimiter);
116
117 int nodeCount, dim, numberOfAttributes, boundaryMarkers;
118
119 //TODO: check number of entries of each line.
120 nodeCount = atoi(tokens[0].c_str());
121 dim = atoi(tokens[1].c_str());
122 numberOfAttributes = atoi(tokens[2].c_str());
123 boundaryMarkers = atoi(tokens[3].c_str());
124
125 vtkPoints* outputPoints = vtkPoints::New();
126 outputPoints->SetNumberOfPoints(nodeCount);
127
128 int i, j;
129
130 vtkDoubleArray** attributeArrays = new vtkDoubleArray*[numberOfAttributes];
131 for (j=0; j<numberOfAttributes; j++)
132 {
133 std::stringstream nameStream;
134 nameStream << "Attribute_" << j;
135 vtkDoubleArray* attributeArray = vtkDoubleArray::New();
136 attributeArray->SetName(nameStream.str().c_str());
137 attributeArray->SetNumberOfComponents(1);
138 attributeArray->SetNumberOfTuples(nodeCount);
139 attributeArrays[j] = attributeArray;
140 }
141
142 vtkIdTypeArray* boundaryDataArray = vtkIdTypeArray::New();
143 if (boundaryMarkers)
144 {
145 boundaryDataArray->SetName(this->BoundaryDataArrayName);
146 boundaryDataArray->SetNumberOfComponents(1);
147 boundaryDataArray->SetNumberOfTuples(nodeCount);
148 }
149
150 double point[3], value;
151 vtkIdType boundaryId;
152
153 int firstIndex = 0;
154
155 int index;
156 for (i=0; i<nodeCount; i++)
157 {
158 std::getline(nodeStream,line);
159 this->Tokenize(line,tokens,delimiter);
160 index = atoi(tokens[0].c_str());
161 if (i==0)
162 {
163 // Here we make the assumption that node 0 or 1 appear in the first line
164 firstIndex = index;
165 }
166 point[0] = atof(tokens[1].c_str());
167 point[1] = atof(tokens[2].c_str());
168 point[2] = atof(tokens[3].c_str());
169 outputPoints->SetPoint(index-firstIndex,point);
170 for (j=0; j<numberOfAttributes; j++)
171 {
172 value = atof(tokens[4+j].c_str());
173 attributeArrays[j]->SetValue(i,value);
174 }
175 if (boundaryMarkers)
176 {
177 boundaryId = atoi(tokens[4+numberOfAttributes].c_str());
178 boundaryDataArray->SetValue(i,boundaryId);
179 }
180 }
181
182 output->SetPoints(outputPoints);
183 outputPoints->Delete();
184
185 for (j=0; j<numberOfAttributes; j++)
186 {
187 output->GetPointData()->AddArray(attributeArrays[j]);
188 attributeArrays[j]->Delete();
189 }
190 delete[] attributeArrays;
191
192 if (boundaryMarkers)
193 {
194 output->GetPointData()->AddArray(boundaryDataArray);
195 }
196
197 std::getline(eleStream,line);
198 this->Tokenize(line,tokens,delimiter);
199
200 int tetCount, nodesPerTet, numberOfCellAttributes;
201 tetCount = atoi(tokens[0].c_str());
202 nodesPerTet = atoi(tokens[1].c_str());
203 numberOfCellAttributes = atoi(tokens[2].c_str());
204
205 vtkDoubleArray** cellAttributeArrays = new vtkDoubleArray*[numberOfCellAttributes];
206 for (j=0; j<numberOfCellAttributes; j++)
207 {
208 std::stringstream nameStream;
209 nameStream << "Attribute_" << j;
210 vtkDoubleArray* attributeArray = vtkDoubleArray::New();
211 attributeArray->SetName(nameStream.str().c_str());
212 attributeArray->SetNumberOfComponents(1);
213 attributeArray->SetNumberOfTuples(nodeCount);
214 cellAttributeArrays[j] = attributeArray;
215 }
216
217 int* outputCellTypes = new int[tetCount];
218 vtkCellArray* outputCellArray = vtkCellArray::New();
219
220 int outputCellType = VTK_TETRA;
221 if (nodesPerTet == 10)
222 {
223 outputCellType = VTK_QUADRATIC_TETRA;
224 }
225
226 vtkIdTypeArray* cellBoundaryDataArray = vtkIdTypeArray::New();
227 if (boundaryMarkers)
228 {
229 cellBoundaryDataArray->SetName(this->BoundaryDataArrayName);
230 cellBoundaryDataArray->SetNumberOfComponents(1);
231 cellBoundaryDataArray->SetNumberOfTuples(tetCount);
232 }
233
234 vtkIdType pointId, maxBoundaryId;
235 for (i=0; i<tetCount; i++)
236 {
237 std::getline(eleStream,line);
238 this->Tokenize(line,tokens,delimiter);
239 index = atoi(tokens[0].c_str());
240 outputCellArray->InsertNextCell(nodesPerTet);
241 maxBoundaryId = 0;
242 for (j=0; j<nodesPerTet; j++)
243 {
244 pointId = atoi(tokens[j+1].c_str());
245 outputCellArray->InsertCellPoint(pointId-firstIndex);
246 if (boundaryMarkers)
247 {
248 boundaryId = boundaryDataArray->GetValue(pointId-firstIndex);
249 if (j>0 && boundaryId > maxBoundaryId)
250 {
251 maxBoundaryId = boundaryId;
252 }
253 }
254 }
255 if (boundaryMarkers)
256 {
257 cellBoundaryDataArray->SetValue(i,maxBoundaryId);
258 }
259 outputCellTypes[i] = outputCellType;
260 for (j=0; j<numberOfCellAttributes; j++)
261 {
262 value = atof(tokens[1+nodesPerTet].c_str());
263 cellAttributeArrays[j]->SetValue(i,value);
264 }
265 }
266
267 for (j=0; j<numberOfCellAttributes; j++)
268 {
269 output->GetCellData()->AddArray(cellAttributeArrays[j]);
270 cellAttributeArrays[j]->Delete();
271 }
272 delete[] cellAttributeArrays;
273
274 output->SetCells(outputCellTypes,outputCellArray);
275
276 if (boundaryMarkers)
277 {
278 output->GetCellData()->AddArray(cellBoundaryDataArray);
279 }
280 cellBoundaryDataArray->Delete();
281
282 boundaryDataArray->Delete();
283
284 outputCellArray->Delete();
285 delete[] outputCellTypes;
286
287 return 1;
288}
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)

◆ Tokenize()

void vtkvmtkTetGenReader::Tokenize ( const std::string & str,
std::vector< std::string > & tokens,
const std::string & delimiters )
protected

Definition at line 57 of file vtkvmtkTetGenReader.cxx.

58{
59 if (tokens.size() > 0)
60 {
61 tokens.clear();
62 }
63
64 std::string::size_type lastPos = str.find_first_not_of(delimiters,0);
65 std::string::size_type pos = str.find_first_of(delimiters,lastPos);
66
67 while (std::string::npos != pos || std::string::npos != lastPos)
68 {
69 tokens.push_back(str.substr(lastPos,pos-lastPos));
70 lastPos = str.find_first_not_of(delimiters, pos);
71 pos = str.find_first_of(delimiters, lastPos);
72 }
73}

Member Data Documentation

◆ BoundaryDataArrayName

char* vtkvmtkTetGenReader::BoundaryDataArrayName
protected

Definition at line 71 of file vtkvmtkTetGenReader.h.


The documentation for this class was generated from the following files: