-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesher.h
308 lines (253 loc) · 9.3 KB
/
Mesher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Created by alhei on 2022/09/09.
//
#ifndef VTK_COLLECTION_MESHER_MESHER_H
#define VTK_COLLECTION_MESHER_MESHER_H
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkCamera.h>
#include <vtkSelectEnclosedPoints.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataReader.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkPolyDataNormals.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/grid/grid_out.h>
#include <deal.II/grid/tria_accessor.h>
#include <string>
#include <array>
#include <set>
#include <fstream>
#include <boost/functional/hash.hpp>
using namespace std;
using namespace dealii;
typedef pair<unsigned int, unsigned int> cell_identifier; // First -> level, Second -> cell index
typedef pair<unsigned int, unsigned int> vtk_point_identifier; // First -> vtk_object id, Second -> point id for vtk object
struct array_hash {
template<class T1, long unsigned int n_elements>
size_t operator()(const array<T1, n_elements> &p) const {
return boost::hash_range(p.begin(), p.end());
}
};
struct pairhash {
public:
template<typename T, typename U>
size_t operator()(const pair<T, U> &x) const {
size_t seed = 0;
boost::hash_combine(seed, x.first);
boost::hash_combine(seed, x.second);
return seed;
}
};
struct orderpairhash {
public:
template<typename T>
size_t operator()(const pair<T, T> &x) const {
array<T, 2> arr({x.first, x.second});
// return array_hash<T, 2>(arr);
return boost::hash_range(arr.begin(), arr.end());
}
};
template<unsigned int dim>
class Mesher {
public:
typedef pair<Point<dim>, Point<dim>> bounds;
Mesher(const string &name,
vector<vtkSmartPointer < vtkPolyData>> & vtk_objects,
const vector< unsigned int > & material_ids)
: name (name)
, vtk_objects(vtk_objects)
, material_ids(material_ids)
{
system(("mkdir -p " + name).c_str());
iota(range.begin(), range.end(), 0);
double working_bounds[2 * dim], bounds[2 * dim];
bool first = true;
for (const auto &vtk_object: vtk_objects) {
if (first) {
vtk_object->GetCellsBounds(bounds);
} else {
vtk_object->GetCellsBounds(working_bounds);
for (const auto &i: range) {
bounds[i] = min(working_bounds[i], bounds[i]);
bounds[i + dim] = max(working_bounds[i], bounds[i]);
}
}
first = false;
}
for (const auto &i: range) {
p1[i] = bounds[i*2];
p2[i] = bounds[i*2 + 1];
}
GridGenerator::hyper_rectangle(triangulation, p1, p2);
unsigned int counter = 0;
cell_identifier first_cell(0, 0);
for (const auto &vtk_object: vtk_objects) {
for (vtkIdType i = 0; i < vtk_object->GetNumberOfPoints(); i++) {
cell_to_vtk_points[first_cell].push_back(vtk_point_identifier(counter, i));
}
counter++;
}
for (auto &vtk_object: vtk_objects){
// vtkSmartPointer <vtkPolyData> polydataCopy;
// polydataCopy->DeepCopy(vtk_object);
vtkNew <vtkPolyDataNormals> normalGenerator;
normalGenerator->SetInputData(vtk_object);
normalGenerator->ComputePointNormalsOn();
normalGenerator->ComputeCellNormalsOff();
normalGenerator->AutoOrientNormalsOn();
normalGenerator->Update();
vtk_object = normalGenerator->GetOutput();
normals.push_back(vtk_object->GetPointData()->GetNormals());
}
};
void refine();
void write_mesh();
private:
const string name;
vector< vtkSmartPointer<vtkPolyData> > vtk_objects;
const vector< unsigned int > material_ids;
vector< vtkSmartPointer<vtkDataArray> > normals;
vector< bounds > vtk_bounds;
array<unsigned int, dim> range;
unsigned int current_level = 0;
unordered_map<cell_identifier, vector<vtk_point_identifier>, orderpairhash> cell_to_vtk_points;
Triangulation<dim> triangulation;
Point<dim> p1, p2;
Point<dim> get_point(const vtk_point_identifier & id){
double x[3];
Point<dim> out;
vtk_objects.at(id.first)->GetPoints()->GetPoint(id.second, x);
for (const auto & i : range) {
out[i] = x[i];
}
return out;
}
bool in_bounds(Point<dim> pt, Point<dim> p1, Point<dim> p2){
bool out = true;
for(const auto & i: range){
out = out && pt[i] <= p2[i];
out = out && p1[i] <= pt[i];
}
return out;
}
void determine_materials();
void update_cell_to_vtk_points();
};
template<unsigned int dim>
void Mesher<dim>::refine() {
CellAccessor<dim> current_cell;
unsigned int level, cell_id;
for (const auto &data: cell_to_vtk_points) {
level = data.first.first;
cell_id = data.first.second;
if (level == current_level) {
current_cell = CellAccessor<dim>(&triangulation, current_level, cell_id);
if(not current_cell.refine_flag_set())
current_cell.set_refine_flag();
}
}
triangulation.execute_coarsening_and_refinement();
current_level++;
update_cell_to_vtk_points();
// determine_materials();
}
template<unsigned int dim>
void Mesher<dim>::write_mesh() {
ofstream out("./" + name + "/" + name + "-" + to_string(current_level) + ".vtk");
GridOut grid_out;
grid_out.write_vtk(triangulation, out);
}
template<unsigned int dim>
void Mesher<dim>::update_cell_to_vtk_points() {
Point<dim> current_point;
cell_identifier child_id, parent_id;
child_id.first = current_level;
parent_id.first = current_level - 1;
for (const auto & cell: triangulation.active_cell_iterators_on_level(current_level)){
parent_id.second = cell->parent_index();
const vector<vtk_point_identifier> & parent_points = cell_to_vtk_points.at(parent_id);
child_id.second = cell->index();
for(const auto & pt: parent_points){
current_point = get_point(pt);
if(cell->point_inside(current_point)){
cell_to_vtk_points[child_id].push_back(pt);
}
}
}
}
template<unsigned int dim>
void Mesher<dim>::determine_materials() {
Point<dim> center, vtk_point;
Tensor<1, dim> diff;
cell_identifier child_id, parent_id;
child_id.first = current_level;
parent_id.first = current_level - 1;
bool contains_points;
vector<vtk_point_identifier> close_points;
double dot = 0;
vector<cell_identifier> cells_without_points;
// vector<Point<dim>> pointless_cell_centres;
vtkSmartPointer <vtkPoints> pointless_cell_centres = vtkSmartPointer<vtkPoints>::New();
for (const auto & cell: triangulation.active_cell_iterators_on_level(current_level)){
center = cell->center();
cell->set_material_id(0);
child_id.second = cell->index();
contains_points = cell_to_vtk_points.find(child_id) != cell_to_vtk_points.end();
if (contains_points){
close_points = cell_to_vtk_points.at(child_id);
}else{
parent_id.second = cell->parent_index();
close_points = cell_to_vtk_points.at(parent_id);
}
if (contains_points) {
for (const auto &pt: close_points) {
vtk_point = get_point(pt);
diff = center - vtk_point;
for (const auto &i: range) {
dot += diff[i] * normals.at(pt.first)->GetComponent(pt.second, i);
}
}
}else{
cells_without_points.push_back(child_id);
// pointless_cell_centres.push_back(center);
pointless_cell_centres->InsertNextPoint((double) center[0], (double) center[1], (double) center[2]);
}
if(dot<0)
cell->set_material_id(material_ids.at(close_points.at(0).first));
dot = 0;
contains_points = false;
}
CellAccessor<dim> current_cell;
vtkSmartPointer <vtkPolyData> pointsPolydata = vtkSmartPointer<vtkPolyData>::New();
pointsPolydata->SetPoints(pointless_cell_centres);
unsigned int obj_counter = 0;
unsigned int cell_counter = 0;
for (auto &vtk_object: vtk_objects) {
vtkSmartPointer <vtkSelectEnclosedPoints> selectEnclosedPoints = vtkSmartPointer<vtkSelectEnclosedPoints>::New();
selectEnclosedPoints->SetSurfaceData(vtk_object);
selectEnclosedPoints->SetInputData(pointsPolydata);
selectEnclosedPoints->Update();
// vector<cell_identifier> cells_without_points;
for(const auto & cell: cells_without_points){
if(selectEnclosedPoints->IsInside(cell_counter)){
current_cell = CellAccessor<dim>(&triangulation, cell.first, cell.second);
current_cell.set_material_id(material_ids.at(obj_counter));
}
cell_counter++;
}
cell_counter = 0;
obj_counter++;
}
}
#endif //VTK_COLLECTION_MESHER_MESHER_H