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
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 {
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
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 {
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)