76 vtkDataObject* doOutput)
79 vtkDebugMacro(<<
"Reading TetGen file...");
81 vtkUnstructuredGrid* output = vtkUnstructuredGrid::SafeDownCast(doOutput);
85 vtkErrorMacro(<<
"Input filename not set");
89 std::string nodeFileName = fname;
90 nodeFileName +=
".node";
92 std::string eleFileName = fname;
93 eleFileName +=
".ele";
95 std::ifstream nodeStream(nodeFileName.c_str());
96 std::ifstream eleStream(eleFileName.c_str());
98 if (!nodeStream.good())
100 vtkErrorMacro(<<
"Unable to open " << nodeFileName <<
" for reading");
104 if (!eleStream.good())
106 vtkErrorMacro(<<
"Unable to open " << eleFileName <<
" for reading");
111 std::string delimiter(
" ");
112 std::vector<std::string> tokens;
114 std::getline(nodeStream,line);
115 this->
Tokenize(line,tokens,delimiter);
117 int nodeCount, dim, numberOfAttributes, boundaryMarkers;
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());
125 vtkPoints* outputPoints = vtkPoints::New();
126 outputPoints->SetNumberOfPoints(nodeCount);
130 vtkDoubleArray** attributeArrays =
new vtkDoubleArray*[numberOfAttributes];
131 for (j=0; j<numberOfAttributes; j++)
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;
142 vtkIdTypeArray* boundaryDataArray = vtkIdTypeArray::New();
146 boundaryDataArray->SetNumberOfComponents(1);
147 boundaryDataArray->SetNumberOfTuples(nodeCount);
150 double point[3], value;
151 vtkIdType boundaryId;
156 for (i=0; i<nodeCount; i++)
158 std::getline(nodeStream,line);
159 this->
Tokenize(line,tokens,delimiter);
160 index = atoi(tokens[0].c_str());
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++)
172 value = atof(tokens[4+j].c_str());
173 attributeArrays[j]->SetValue(i,value);
177 boundaryId = atoi(tokens[4+numberOfAttributes].c_str());
178 boundaryDataArray->SetValue(i,boundaryId);
182 output->SetPoints(outputPoints);
183 outputPoints->Delete();
185 for (j=0; j<numberOfAttributes; j++)
187 output->GetPointData()->AddArray(attributeArrays[j]);
188 attributeArrays[j]->Delete();
190 delete[] attributeArrays;
194 output->GetPointData()->AddArray(boundaryDataArray);
197 std::getline(eleStream,line);
198 this->
Tokenize(line,tokens,delimiter);
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());
205 vtkDoubleArray** cellAttributeArrays =
new vtkDoubleArray*[numberOfCellAttributes];
206 for (j=0; j<numberOfCellAttributes; j++)
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;
217 int* outputCellTypes =
new int[tetCount];
218 vtkCellArray* outputCellArray = vtkCellArray::New();
220 int outputCellType = VTK_TETRA;
221 if (nodesPerTet == 10)
223 outputCellType = VTK_QUADRATIC_TETRA;
226 vtkIdTypeArray* cellBoundaryDataArray = vtkIdTypeArray::New();
230 cellBoundaryDataArray->SetNumberOfComponents(1);
231 cellBoundaryDataArray->SetNumberOfTuples(tetCount);
234 vtkIdType pointId, maxBoundaryId;
235 for (i=0; i<tetCount; i++)
237 std::getline(eleStream,line);
238 this->
Tokenize(line,tokens,delimiter);
239 index = atoi(tokens[0].c_str());
240 outputCellArray->InsertNextCell(nodesPerTet);
242 for (j=0; j<nodesPerTet; j++)
244 pointId = atoi(tokens[j+1].c_str());
245 outputCellArray->InsertCellPoint(pointId-firstIndex);
248 boundaryId = boundaryDataArray->GetValue(pointId-firstIndex);
249 if (j>0 && boundaryId > maxBoundaryId)
251 maxBoundaryId = boundaryId;
257 cellBoundaryDataArray->SetValue(i,maxBoundaryId);
259 outputCellTypes[i] = outputCellType;
260 for (j=0; j<numberOfCellAttributes; j++)
262 value = atof(tokens[1+nodesPerTet].c_str());
263 cellAttributeArrays[j]->SetValue(i,value);
267 for (j=0; j<numberOfCellAttributes; j++)
269 output->GetCellData()->AddArray(cellAttributeArrays[j]);
270 cellAttributeArrays[j]->Delete();
272 delete[] cellAttributeArrays;
274 output->SetCells(outputCellTypes,outputCellArray);
278 output->GetCellData()->AddArray(cellBoundaryDataArray);
280 cellBoundaryDataArray->Delete();
282 boundaryDataArray->Delete();
284 outputCellArray->Delete();
285 delete[] outputCellTypes;