52{
53 vtkUnstructuredGrid *input= vtkUnstructuredGrid::SafeDownCast(this->GetInput());
54
55 if (!this->GetFileName())
56 {
57 vtkErrorMacro(<<"FileName not set.");
58 return;
59 }
60
61 std::string nodeFileName = this->GetFileName();
62 nodeFileName += ".node";
63
64 std::string eleFileName = this->GetFileName();
65 eleFileName += ".ele";
66
67 std::ofstream nodeStream(nodeFileName.c_str());
68 std::ofstream eleStream(eleFileName.c_str());
69
70 if (!nodeStream.good())
71 {
72 vtkErrorMacro(<<"Could not open node file for writing.");
73 return;
74 }
75
76 if (!eleStream.good())
77 {
78 vtkErrorMacro(<<"Could not open ele file for writing.");
79 return;
80 }
81
82 input->BuildLinks();
83
84 int numberOfPoints = input->GetNumberOfPoints();
85
86 int i, j;
87
88
89
90 nodeStream << numberOfPoints << " 3 0 0" << std::endl;
91
92 double point[3];
93 for (i=0; i<numberOfPoints; i++)
94 {
95 input->GetPoint(i,point);
96 nodeStream << i+1 << " " << point[0] << " " << point[1] << " " << point[2] << std::endl;
97 }
98
99#if 0
100 vtkIntArray* boundaryDataArray = vtkIntArray::New();
102 {
103 if (input->GetCellData()->GetArray(this->BoundaryDataArrayName))
104 {
105 boundaryDataArray->DeepCopy(input->GetCellData()->GetArray(this->BoundaryDataArrayName));
106 }
107 else
108 {
109 vtkErrorMacro(<<"BoundaryDataArray with name specified does not exist");
110 boundaryDataArray->Delete();
111 return;
112 }
113 }
114 else
115 {
116 boundaryDataArray->SetNumberOfValues(numberOfCells);
117 boundaryDataArray->FillComponent(0,0.0);
118 }
119#endif
120
121 vtkIdTypeArray* tetraCellIdArray = vtkIdTypeArray::New();
122 input->GetIdsOfCellsOfType(VTK_TETRA,tetraCellIdArray);
123 int numberOfTetras = tetraCellIdArray->GetNumberOfTuples();
124
125 vtkIdTypeArray* quadraticTetraCellIdArray = vtkIdTypeArray::New();
126 input->GetIdsOfCellsOfType(VTK_QUADRATIC_TETRA,quadraticTetraCellIdArray);
127 int numberOfQuadraticTetras = quadraticTetraCellIdArray->GetNumberOfTuples();
128
129 int pointsInTet = 4;
130 vtkIdTypeArray* tetIdsArray = tetraCellIdArray;
131 int numberOfOutputTetras = tetraCellIdArray->GetNumberOfTuples();
132
133 if (numberOfQuadraticTetras > numberOfTetras)
134 {
135 pointsInTet = 10;
136 tetIdsArray = quadraticTetraCellIdArray;
137 numberOfOutputTetras = quadraticTetraCellIdArray->GetNumberOfTuples();
138 }
139
140
141
142 eleStream << numberOfOutputTetras << " " << pointsInTet << " 0" << std::endl;
143
144 double point0[3], point1[3], point2[3], point3[3];
145 double cross[3], vector01[3], vector21[3], vector31[3];
146 double dot;
147 int tmp;
148
149 int cellPointIds[10];
150 for (i=0; i<numberOfOutputTetras; i++)
151 {
152 vtkIdType cellId = tetraCellIdArray->GetValue(i);
153 vtkCell* cell = input->GetCell(cellId);
154 for (j=0; j<pointsInTet; j++)
155 {
156 cellPointIds[j] = cell->GetPointId(j);
157 }
158 input->GetPoint(cellPointIds[0],point0);
159 input->GetPoint(cellPointIds[1],point1);
160 input->GetPoint(cellPointIds[2],point2);
161 input->GetPoint(cellPointIds[3],point3);
162 vector01[0] = point0[0] - point1[0];
163 vector01[1] = point0[1] - point1[1];
164 vector01[2] = point0[2] - point1[2];
165 vector21[0] = point2[0] - point1[0];
166 vector21[1] = point2[1] - point1[1];
167 vector21[2] = point2[2] - point1[2];
168 vector31[0] = point3[0] - point1[0];
169 vector31[1] = point3[1] - point1[1];
170 vector31[2] = point3[2] - point1[2];
171 vtkMath::Cross(vector21,vector31,cross);
172 dot = vtkMath::Dot(cross,vector01);
173 if (dot < 0.0)
174 {
175 tmp = cellPointIds[2];
176 cellPointIds[2] = cellPointIds[3];
177 cellPointIds[3] = tmp;
178 if (pointsInTet == 10)
179 {
180 tmp = cellPointIds[6];
181 cellPointIds[6] = cellPointIds[7];
182 cellPointIds[7] = tmp;
183 tmp = cellPointIds[5];
184 cellPointIds[5] = cellPointIds[8];
185 cellPointIds[8] = tmp;
186 }
187 }
188
189 eleStream << i+1 << " ";
190 for (j=0; j<pointsInTet; j++)
191 {
192 eleStream << cellPointIds[j]+1 << " ";
193 }
194 eleStream << std::endl;
195 }
196
197 tetraCellIdArray->Delete();
198 quadraticTetraCellIdArray->Delete();
199}