VMTK
Loading...
Searching...
No Matches
vtkvmtkTetGenReader.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: vtkvmtkTetGenReader.cxx
5Language: C++
6Date: Sat Feb 19 15:14:48 CET 2011
7Version: Revision: 1.0
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
22#include "vtkvmtkTetGenReader.h"
23#include "vtkDoubleArray.h"
24#include "vtkIntArray.h"
25#include "vtkObjectFactory.h"
26#include "vtkPoints.h"
27#include "vtkPointData.h"
28#include "vtkCellData.h"
29#include "vtkCellArray.h"
30#include "vtkUnsignedCharArray.h"
31#include "vtkUnstructuredGrid.h"
32#include "vtkIdTypeArray.h"
33#include "vtkInformation.h"
34#include "vtkInformationVector.h"
35#include "vtkStreamingDemandDrivenPipeline.h"
36#include "vtkvmtkConstants.h"
37
38#include <sstream>
39
40
42
47
49{
50 if (this->BoundaryDataArrayName)
51 {
52 delete[] this->BoundaryDataArrayName;
53 this->BoundaryDataArrayName = NULL;
54 }
55}
56
57void vtkvmtkTetGenReader::Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
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}
74
75int vtkvmtkTetGenReader::ReadMeshSimple(const std::string& fname,
76 vtkDataObject* doOutput)
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}
289
290void vtkvmtkTetGenReader::PrintSelf(std::ostream& os, vtkIndent indent)
291{
292 Superclass::PrintSelf(os,indent);
293}
Reads unstructured grid data from Tetgen node/elem format.
int ReadMeshSimple(const std::string &fname, vtkDataObject *output) override
void PrintSelf(std::ostream &os, vtkIndent indent) override
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters)
vtkStandardNewMacro(vtkvmtkTetGenReader)