PolyFEM
Loading...
Searching...
No Matches
PolygonalBasis3d.cpp
Go to the documentation of this file.
1
3#include "LagrangeBasis3d.hpp"
4
15
17
18#include <random>
19#include <memory>
21
22namespace polyfem
23{
24 using namespace assembler;
25 using namespace mesh;
26 using namespace quadrature;
27 using namespace utils;
28
29 namespace basis
30 {
31 namespace
32 {
33
34 // -----------------------------------------------------------------------------
35
36 std::vector<int> compute_nonzero_bases_ids(const Mesh3D &mesh, const int c,
37 const std::vector<ElementBases> &bases,
38 const std::map<int, InterfaceData> &poly_face_to_data)
39 {
40 std::vector<int> local_to_global;
41
42 for (int lf = 0; lf < mesh.n_cell_faces(c); ++lf)
43 {
44 auto index = mesh.get_index_from_element(c, lf, 0);
45 const int c2 = mesh.switch_element(index).element;
46 assert(c2 >= 0); // no boundary polytope
47 assert(poly_face_to_data.count(index.face) > 0);
48 const InterfaceData &bdata = poly_face_to_data.at(index.face);
49 const ElementBases &b = bases[c2];
50 for (int other_local_basis_id : bdata.local_indices)
51 {
52 for (const auto &x : b.bases[other_local_basis_id].global())
53 {
54 const int global_node_id = x.index;
55 local_to_global.push_back(global_node_id);
56 }
57 }
58 }
59
60 std::sort(local_to_global.begin(), local_to_global.end());
61 auto it = std::unique(local_to_global.begin(), local_to_global.end());
62 local_to_global.resize(std::distance(local_to_global.begin(), it));
63
64 return local_to_global;
65 }
66
67 // -----------------------------------------------------------------------------
68
69 // Canonical triangle mesh in parametric domain
70 void compute_canonical_pattern(int n_samples_per_edge, Eigen::MatrixXd &V, Eigen::MatrixXi &F)
71 {
72 regular_2d_grid(n_samples_per_edge, false, V, F);
73
74 // igl::opengl::glfw::Viewer viewer;
75 // viewer.data().set_mesh(V, F);
76 // viewer.launch();
77 }
78
79 // -----------------------------------------------------------------------------
80
81 // Needs to be consistent between `evalFunc` and `compute_quad_mesh_from_cell`
82 constexpr int lv0 = 3;
83
84 // Assemble the surface quad mesh (V, F) corresponding to the polyhedron c
85 GetAdjacentLocalEdge compute_quad_mesh_from_cell(
86 const Mesh3D &mesh, int c, Eigen::MatrixXd &V, Eigen::MatrixXi &F)
87 {
88 std::vector<std::array<int, 4>> quads(mesh.n_cell_faces(c));
89 typedef std::tuple<int, int, bool> QuadLocalEdge;
90 std::vector<std::array<QuadLocalEdge, 4>> adj(mesh.n_cell_faces(c));
91 int num_vertices = 0;
92 std::map<int, int> vertex_g2l;
93 std::map<int, int> face_g2l;
94 for (int lf = 0; lf < mesh.n_cell_faces(c); ++lf)
95 {
96 face_g2l.emplace(mesh.get_index_from_element(c, lf, lv0).face, lf);
97 }
98 for (int lf = 0; lf < mesh.n_cell_faces(c); ++lf)
99 {
100 auto index = mesh.get_index_from_element(c, lf, lv0);
101 assert(mesh.n_face_vertices(index.face) == 4);
102 for (int lv = 0; lv < 4; ++lv)
103 {
104 if (!vertex_g2l.count(index.vertex))
105 {
106 vertex_g2l.emplace(index.vertex, num_vertices++);
107 }
108 quads[lf][lv] = vertex_g2l.at(index.vertex);
109
110 // Set adjacency info
111 auto index2 = mesh.switch_face(index);
112 int lf2 = face_g2l.at(index2.face);
113 std::get<0>(adj[lf][lv]) = lf2;
114 auto index3 = mesh.get_index_from_element(c, lf2, lv0);
115 for (int lv2 = 0; lv2 < 4; ++lv2)
116 {
117 if (index3.edge == index2.edge)
118 {
119 std::get<1>(adj[lf][lv]) = lv2;
120 if (index2.vertex != index3.vertex)
121 {
122 assert(mesh.switch_vertex(index3).vertex == index2.vertex);
123 std::get<2>(adj[lf][lv]) = true;
124 }
125 else
126 {
127 std::get<2>(adj[lf][lv]) = false;
128 }
129 }
130 index3 = mesh.next_around_face(index3);
131 }
132
133 index = mesh.next_around_face(index);
134 }
135 }
136 V.resize(num_vertices, 3);
137 for (const auto &kv : vertex_g2l)
138 {
139 V.row(kv.second) = mesh.point(kv.first);
140 }
141 F.resize(quads.size(), 4);
142 int f = 0;
143 for (auto q : quads)
144 {
145 F.row(f++) << q[0], q[1], q[2], q[3];
146 }
147
148 return [adj](int q, int lv) {
149 return adj[q][lv];
150 };
151 }
152
153 // -----------------------------------------------------------------------------
154
155 void compute_offset_kernels(const Eigen::MatrixXd &QV, const Eigen::MatrixXi &QF,
156 int n_kernels_per_edge, double eps, const Eigen::RowVector3d &interior_point, Eigen::MatrixXd &kernel_centers,
157 Eigen::MatrixXd &KV, Eigen::MatrixXi &KF,
158 EvalParametersFunc evalFuncGeom, GetAdjacentLocalEdge getAdjLocalEdge)
159 {
160 Eigen::MatrixXd PV;
161 Eigen::MatrixXi PF;
162 Eigen::VectorXd D;
163 compute_canonical_pattern(n_kernels_per_edge, PV, PF);
164 instantiate_pattern(QV, QF, PV, PF, KV, KF, nullptr, evalFuncGeom, getAdjLocalEdge);
165 orient_closed_surface(KV, KF);
166 double volume = std::pow(signed_volume(KV, KF), 1.0 / 3.0);
167
168 offset_surface_vertices(KV, KF, interior_point, eps * volume, kernel_centers);
169
170 // std::default_random_engine gen;
171 // std::uniform_real_distribution<double> dist(-1.0, 1.0);
172 // for (int v = 0; v < kernel_centers.rows(); ++v) {
173 // kernel_centers.row(v) = KV.row(v) + dist(gen) * KN.row(v);
174 // }
175 assert(kernel_centers.cols() == 3);
176 signed_squared_distances(KV, KF, kernel_centers, D);
177 std::vector<Eigen::RowVector3d> remap;
178 std::vector<Eigen::RowVector3d> rejected;
179 for (int v = 0; v < kernel_centers.rows(); ++v)
180 {
181 if (std::sqrt(D(v)) > 0.8 * eps * volume)
182 {
183 remap.push_back(kernel_centers.row(v));
184 }
185 else
186 {
187 // rejected.push_back(kernel_centers.row(v));
188 }
189 }
190 kernel_centers.resize(remap.size(), 3);
191 for (int v = 0; v < kernel_centers.rows(); ++v)
192 {
193 kernel_centers.row(v) = remap[v];
194 }
195 }
196
197 // -----------------------------------------------------------------------------
198
200 void sample_polyhedra(
201 const int element_index,
202 const int n_quadrature_vertices_per_edge,
203 const int n_kernels_per_edge,
204 int n_samples_per_edge,
205 const int quadrature_order,
206 const int mass_quadrature_order,
207 const Mesh3D &mesh,
208 const std::map<int, InterfaceData> &poly_face_to_data,
209 const std::vector<ElementBases> &bases,
210 const std::vector<ElementBases> &gbases,
211 const double eps,
212 std::vector<int> &local_to_global,
213 Eigen::MatrixXd &collocation_points,
214 Eigen::MatrixXd &kernel_centers,
215 Eigen::MatrixXd &rhs,
216 Eigen::MatrixXd &triangulated_vertices,
217 Eigen::MatrixXi &triangulated_faces,
218 Quadrature &quadrature,
219 Quadrature &mass_quadrature,
220 double &scaling,
221 Eigen::RowVector3d &translation)
222 {
223 // Local ids of nonzero bases over the polygon
224 local_to_global = compute_nonzero_bases_ids(mesh, element_index, bases, poly_face_to_data);
225
226 // Compute the image of the canonical pattern vertices through the geometric mapping
227 // of the given local face
228 auto evalFunc = [&](const Eigen::MatrixXd &uv, Eigen::MatrixXd &mapped, int lf) {
229 const auto &u = uv.col(0).array();
230 const auto &v = uv.col(1).array();
231 auto index = mesh.get_index_from_element(element_index, lf, lv0);
232 index = mesh.switch_element(index);
233 // Eigen::MatrixXd abcd = LagrangeBasis3d::linear_hex_face_local_nodes_coordinates(mesh, index);
234 const auto indices = LagrangeBasis3d::hex_face_local_nodes(false, 1, mesh, index);
235 assert(indices.size() == 4);
236 Eigen::MatrixXd abcd;
238 Eigen::RowVector3d a = abcd.row(indices(0));
239 Eigen::RowVector3d b = abcd.row(indices(1));
240 Eigen::RowVector3d c = abcd.row(indices(2));
241 Eigen::RowVector3d d = abcd.row(indices(3));
242 mapped = ((1 - u) * (1 - v)).matrix() * a + (u * (1 - v)).matrix() * b + (u * v).matrix() * c + ((1 - u) * v).matrix() * d;
243 mapped = mapped.array().max(0.0).min(1.0);
244 assert(mapped.maxCoeff() >= 0.0);
245 assert(mapped.maxCoeff() <= 1.0);
246 };
247 auto evalFuncGeom = [&](const Eigen::MatrixXd &uv, Eigen::MatrixXd &mapped, int lf) {
248 Eigen::MatrixXd samples;
249 evalFunc(uv, samples, lf);
250 auto index = mesh.get_index_from_element(element_index, lf, lv0);
251 index = mesh.switch_element(index);
252 const ElementBases &gb = gbases[index.element];
253 gb.eval_geom_mapping(samples, mapped);
254 };
255
256 Eigen::MatrixXd QV, KV;
257 Eigen::MatrixXi QF, KF;
258 auto getAdjLocalEdge = compute_quad_mesh_from_cell(mesh, element_index, QV, QF);
259
260 // Compute kernel centers
261 compute_offset_kernels(QV, QF, n_kernels_per_edge, eps, mesh.kernel(element_index), kernel_centers, KV, KF,
262 evalFuncGeom, getAdjLocalEdge);
263
264 // Compute collocation points
265 Eigen::MatrixXd PV, UV;
266 Eigen::MatrixXi PF, CF, UF;
267 Eigen::VectorXi uv_sources, uv_ranges;
268 compute_canonical_pattern(n_samples_per_edge, PV, PF);
269 instantiate_pattern(QV, QF, PV, PF, UV, UF, &uv_sources, evalFunc, getAdjLocalEdge);
270 orient_closed_surface(UV, UF);
271 instantiate_pattern(QV, QF, PV, PF, collocation_points, CF, nullptr, evalFuncGeom, getAdjLocalEdge);
272 orient_closed_surface(collocation_points, CF);
273 reorder_mesh(collocation_points, CF, uv_sources, uv_ranges);
274 reorder_mesh(UV, UF, uv_sources, uv_ranges);
275 assert(uv_ranges.size() == mesh.n_cell_faces(element_index) + 1);
276
277 // Compute coarse surface surface for visualization
278 compute_canonical_pattern(n_quadrature_vertices_per_edge, PV, PF);
279 instantiate_pattern(QV, QF, PV, PF, triangulated_vertices, triangulated_faces,
280 nullptr, evalFuncGeom, getAdjLocalEdge);
281 orient_closed_surface(triangulated_vertices, triangulated_faces);
282
283 // for (int f = 0; f < KF.rows(); ++f) {
284 // triangulated_faces.row(f) = KF.row(f).reverse();
285 // }
286
287 // {
288 // Eigen::MatrixXd V;
289 // evalFuncGeom(PV, V, 0);
290 // igl::write_triangle_mesh("foo_dense.obj", collocation_points, CF);
291 // igl::write_triangle_mesh("foo_small.obj", triangulated_vertices, triangulated_faces);
292 // igl::opengl::glfw::Viewer viewer;
293 // viewer.data().set_points(kernel_centers, Eigen::RowVector3d(1,0,1));
294 // viewer.data().set_mesh(KV, KF);
295 // viewer.launch();
296 // }
297
298 // igl::opengl::glfw::Viewer viewer;
299 // viewer.data().set_mesh(collocation_points, CF);
300 // viewer.data().add_points(kernel_centers, Eigen::RowVector3d(0,1,1));
301 // for (int lf = 0; lf < mesh.n_cell_faces(element_index); ++lf) {
302 // Eigen::MatrixXd samples;
303 // samples = UV.middleRows(uv_ranges(lf), uv_ranges(lf+1) - uv_ranges(lf));
304 // Eigen::RowVector3d c = Eigen::RowVector3d::Random();
305 // viewer.data().add_points(samples, c);
306 // }
307 // viewer.launch();
308
309 // Compute right-hand side constraints for setting the harmonic kernels
310 Eigen::MatrixXd samples;
311 std::vector<AssemblyValues> basis_val;
312 rhs.resize(UV.rows(), local_to_global.size());
313 rhs.setZero();
314 for (int lf = 0; lf < mesh.n_cell_faces(element_index); ++lf)
315 {
316 auto index = mesh.get_index_from_element(element_index, lf, 0);
317 const int c2 = mesh.switch_element(index).element;
318 assert(c2 >= 0); // no boundary polytope
319
320 const InterfaceData &bdata = poly_face_to_data.at(index.face);
321 const ElementBases &b = bases[c2];
322
323 samples = UV.middleRows(uv_ranges(lf), uv_ranges(lf + 1) - uv_ranges(lf));
324 b.evaluate_bases(samples, basis_val);
325
326 // Evaluate field basis and set up the rhs
327 for (int other_local_basis_id : bdata.local_indices)
328 {
329 // b.bases[other_local_basis_id].basis(samples, basis_val);
330
331 for (const auto &x : b.bases[other_local_basis_id].global())
332 {
333 const int global_node_id = x.index;
334 const double weight = x.val;
335
336 const int poly_local_basis_id = std::distance(local_to_global.begin(),
337 std::find(local_to_global.begin(), local_to_global.end(), global_node_id));
338 rhs.block(uv_ranges(lf), poly_local_basis_id, basis_val[other_local_basis_id].val.size(), 1) += basis_val[other_local_basis_id].val * weight;
339 }
340 }
341 }
342
343 // Compute quadrature points + normalize kernels and collocation points
344 Eigen::MatrixXd NV = triangulated_vertices;
345 // scaling = (NV.colwise().maxCoeff() - NV.colwise().minCoeff()).maxCoeff();
346 // translation = NV.colwise().minCoeff();
347 scaling = 1.0;
348 translation.setZero();
349 // NV = (NV.rowwise() - translation) / scaling;
350 PolyhedronQuadrature::get_quadrature(NV, triangulated_faces, mesh.kernel(element_index),
351 quadrature_order, quadrature);
352 PolyhedronQuadrature::get_quadrature(NV, triangulated_faces, mesh.kernel(element_index),
353 mass_quadrature_order, mass_quadrature);
354
355 // Normalization
356 // collocation_points = (collocation_points.rowwise() - translation) / scaling;
357 // kernel_centers = (kernel_centers.rowwise() - translation) / scaling;
358 // KV = (KV.rowwise() - translation) / scaling;
359
360 triangulated_vertices = KV;
361 triangulated_faces = KF;
362 }
363
364 } // anonymous namespace
365
367
368 // Compute the integral constraints for each basis of the mesh
370 const Assembler &assembler,
371 const Mesh3D &mesh,
372 const int n_bases,
373 const std::vector<ElementBases> &bases,
374 const std::vector<ElementBases> &gbases,
375 Eigen::MatrixXd &basis_integrals)
376 {
377 assert(mesh.is_volume());
378
379 basis_integrals.resize(n_bases, 9);
380 basis_integrals.setZero();
381 Eigen::MatrixXd rhs(n_bases, 9);
382 rhs.setZero();
383
384 const int n_elements = mesh.n_elements();
385 for (int e = 0; e < n_elements; ++e)
386 {
387 if (mesh.is_polytope(e))
388 {
389 continue;
390 }
391 // ElementAssemblyValues vals = values[e];
392 // const ElementAssemblyValues &gvals = gvalues[e];
394 vals.compute(e, mesh.is_volume(), bases[e], gbases[e]);
395
396 // Computes the discretized integral of the PDE over the element
397 const int n_local_bases = int(vals.basis_values.size());
398 for (int j = 0; j < n_local_bases; ++j)
399 {
400 const AssemblyValues &v = vals.basis_values[j];
401 const double integral_100 = (v.grad_t_m.col(0).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
402 const double integral_010 = (v.grad_t_m.col(1).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
403 const double integral_001 = (v.grad_t_m.col(2).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
404
405 const double integral_110 = ((vals.val.col(1).array() * v.grad_t_m.col(0).array() + vals.val.col(0).array() * v.grad_t_m.col(1).array()) * vals.det.array() * vals.quadrature.weights.array()).sum();
406 const double integral_011 = ((vals.val.col(2).array() * v.grad_t_m.col(1).array() + vals.val.col(1).array() * v.grad_t_m.col(2).array()) * vals.det.array() * vals.quadrature.weights.array()).sum();
407 const double integral_101 = ((vals.val.col(0).array() * v.grad_t_m.col(2).array() + vals.val.col(2).array() * v.grad_t_m.col(0).array()) * vals.det.array() * vals.quadrature.weights.array()).sum();
408
409 const double integral_200 = 2 * (vals.val.col(0).array() * v.grad_t_m.col(0).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
410 const double integral_020 = 2 * (vals.val.col(1).array() * v.grad_t_m.col(1).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
411 const double integral_002 = 2 * (vals.val.col(2).array() * v.grad_t_m.col(2).array() * vals.det.array() * vals.quadrature.weights.array()).sum();
412
413 const double area = (v.val.array() * vals.det.array() * vals.quadrature.weights.array()).sum();
414
415 for (size_t ii = 0; ii < v.global.size(); ++ii)
416 {
417 basis_integrals(v.global[ii].index, 0) += integral_100 * v.global[ii].val;
418 basis_integrals(v.global[ii].index, 1) += integral_010 * v.global[ii].val;
419 basis_integrals(v.global[ii].index, 2) += integral_001 * v.global[ii].val;
420
421 basis_integrals(v.global[ii].index, 3) += integral_110 * v.global[ii].val;
422 basis_integrals(v.global[ii].index, 4) += integral_011 * v.global[ii].val;
423 basis_integrals(v.global[ii].index, 5) += integral_101 * v.global[ii].val;
424
425 basis_integrals(v.global[ii].index, 6) += integral_200 * v.global[ii].val;
426 basis_integrals(v.global[ii].index, 7) += integral_020 * v.global[ii].val;
427 basis_integrals(v.global[ii].index, 8) += integral_002 * v.global[ii].val;
428
429 rhs(v.global[ii].index, 6) += -2.0 * area * v.global[ii].val;
430 rhs(v.global[ii].index, 7) += -2.0 * area * v.global[ii].val;
431 rhs(v.global[ii].index, 8) += -2.0 * area * v.global[ii].val;
432 }
433 }
434 }
435
436 basis_integrals -= rhs;
437 }
438
439 // -----------------------------------------------------------------------------
440
441 // Distance from harmonic kernels to polygon boundary
442 double compute_epsilon(const Mesh3D &mesh, int e)
443 {
444 // double area = 0;
445 // const int n_edges = mesh.n_element_vertices(e);
446 // for (int i = 0; i < n_edges; ++i) {
447 // const int ip = (i + 1) % n_edges;
448
449 // Eigen::RowVector2d p0 = mesh.point(mesh.vertex_global_index(e, i));
450 // Eigen::RowVector2d p1 = mesh.point(mesh.vertex_global_index(e, ip));
451
452 // Eigen::Matrix2d det_mat;
453 // det_mat.row(0) = p0;
454 // det_mat.row(1) = p1;
455
456 // area += det_mat.determinant();
457 // }
458 // area = std::fabs(area);
459 // // const double eps = use_harmonic ? (0.08*area) : 0;
460 // const double eps = 0.08*area;
461
462 return 0.1; // will be relative to the volume of the poly
463 }
464
466 const LinearAssembler &assembler,
467 const int nn_samples_per_edge,
468 const Mesh3D &mesh,
469 const int n_bases,
470 const int quadrature_order,
471 const int mass_quadrature_order,
472 const int integral_constraints,
473 std::vector<ElementBases> &bases,
474 const std::vector<ElementBases> &gbases,
475 const std::map<int, InterfaceData> &poly_face_to_data,
476 std::map<int, std::pair<Eigen::MatrixXd, Eigen::MatrixXi>> &mapped_boundary)
477 {
478 assert(mesh.is_volume());
479 if (poly_face_to_data.empty())
480 {
481 return 0;
482 }
483 int n_kernels_per_edge = 4; //(int) std::round(n_samples_per_edge / 3.0);
484 int n_samples_per_edge = 3 * n_kernels_per_edge;
485
486 // Step 1: Compute integral constraints
487 Eigen::MatrixXd basis_integrals;
488 compute_integral_constraints(assembler, mesh, n_bases, bases, gbases, basis_integrals);
489
490 // Step 2: Compute the rest =)
491 for (int e = 0; e < mesh.n_elements(); ++e)
492 {
493 if (!mesh.is_polytope(e))
494 {
495 continue;
496 }
497 // No boundary polytope
498 // assert(element_type[e] != ElementType::BOUNDARY_POLYTOPE);
499
500 // Kernel distance to polygon boundary
501 const double eps = compute_epsilon(mesh, e);
502
503 std::vector<int> local_to_global; // map local basis id (the ones that are nonzero on the polygon boundary) to global basis id
504 Eigen::MatrixXd collocation_points, kernel_centers, triangulated_vertices;
505 Eigen::MatrixXi triangulated_faces;
506 Eigen::MatrixXd rhs; // 1 row per collocation point, 1 column per basis that is nonzero on the polygon boundary
507
508 ElementBases &b = bases[e];
509 b.has_parameterization = false;
510
511 Quadrature tmp_quadrature, tmp_mass_quadrature;
512 double scaling;
513 Eigen::RowVector3d translation;
514 sample_polyhedra(e, 2, n_kernels_per_edge, n_samples_per_edge,
515 quadrature_order > 0 ? quadrature_order : AssemblerUtils::quadrature_order(assembler.name(), 2, AssemblerUtils::BasisType::POLY, 3),
516 mass_quadrature_order > 0 ? mass_quadrature_order : AssemblerUtils::quadrature_order("Mass", 2, AssemblerUtils::BasisType::POLY, 3),
517 mesh, poly_face_to_data, bases, gbases, eps, local_to_global,
518 collocation_points, kernel_centers, rhs, triangulated_vertices,
519 triangulated_faces, tmp_quadrature, tmp_mass_quadrature, scaling, translation);
520
521 b.set_quadrature([tmp_quadrature](Quadrature &quad) { quad = tmp_quadrature; });
522 b.set_mass_quadrature([tmp_mass_quadrature](Quadrature &quad) { quad = tmp_mass_quadrature; });
523 // b.scaling_ = scaling;
524 // b.translation_ = translation;
525
526 // igl::opengl::glfw::Viewer & viewer = UIState::ui_state().viewer;
527 // viewer.data().clear();
528 // viewer.data().set_mesh(triangulated_vertices, triangulated_faces);
529 // viewer.data().add_points(kernel_centers, Eigen::Vector3d(0,1,1).transpose());
530 // add_spheres(viewer, kernel_centers, 0.005);
531
532 // Eigen::MatrixXd pts = triangulated_vertices, normals;
533 // Eigen::MatrixXi tris = triangulated_faces;
534 // igl::per_corner_normals(pts, tris, 20, normals);
535 // viewer.data().set_normals(normals);
536 // viewer.data().set_face_based(false);
537 // viewer.launch();
538
539 // for(int a = 0; rhs.cols();++a)
540 // {
541 // igl::opengl::glfw::Viewer viewer;
542 // Eigen::MatrixXd asd(collocation_points.rows(), 3);
543 // asd.col(0)=collocation_points.col(0);
544 // asd.col(1)=collocation_points.col(1);
545 // asd.col(2)=collocation_points.col(2);
546 // Eigen::VectorXd S = rhs.col(a);
547 // Eigen::MatrixXd C;
548 // igl::colormap(igl::COLOR_MAP_TYPE_VIRIDIS, S, true, C);
549 // viewer.data().add_points(asd, C);
550 // viewer.launch();
551 // }
552
553 // for(int asd = 0; asd < collocation_points.rows(); ++asd) {
554 // viewer.data().add_label(collocation_points.row(asd), std::to_string(asd));
555 // }
556
557 // Compute the weights of the RBF kernels
558 Eigen::MatrixXd local_basis_integrals(rhs.cols(), basis_integrals.cols());
559 for (long k = 0; k < rhs.cols(); ++k)
560 {
561 local_basis_integrals.row(k) = -basis_integrals.row(local_to_global[k]);
562 }
563 auto set_rbf = [&b](auto rbf) {
564 b.set_bases_func([rbf](const Eigen::MatrixXd &uv, std::vector<AssemblyValues> &val) {
565 Eigen::MatrixXd tmp;
566 rbf->bases_values(uv, tmp);
567 val.resize(tmp.cols());
568 assert(tmp.rows() == uv.rows());
569
570 for (size_t i = 0; i < tmp.cols(); ++i)
571 {
572 val[i].val = tmp.col(i);
573 }
574 });
575 b.set_grads_func([rbf](const Eigen::MatrixXd &uv, std::vector<AssemblyValues> &val) {
576 Eigen::MatrixXd tmpx, tmpy, tmpz;
577
578 rbf->bases_grads(0, uv, tmpx);
579 rbf->bases_grads(1, uv, tmpy);
580 rbf->bases_grads(2, uv, tmpz);
581
582 val.resize(tmpx.cols());
583 assert(tmpx.cols() == tmpy.cols());
584 assert(tmpx.cols() == tmpz.cols());
585 assert(tmpx.rows() == uv.rows());
586 for (size_t i = 0; i < tmpx.cols(); ++i)
587 {
588 val[i].grad.resize(uv.rows(), uv.cols());
589 val[i].grad.col(0) = tmpx.col(i);
590 val[i].grad.col(1) = tmpy.col(i);
591 val[i].grad.col(2) = tmpz.col(i);
592 }
593 });
594 };
595 if (integral_constraints == 0)
596 {
597 set_rbf(std::make_shared<RBFWithLinear>(
598 kernel_centers, collocation_points, local_basis_integrals, tmp_quadrature, rhs, false));
599 }
600 else if (integral_constraints == 1)
601 {
602 set_rbf(std::make_shared<RBFWithLinear>(
603 kernel_centers, collocation_points, local_basis_integrals, tmp_quadrature, rhs));
604 }
605 else if (integral_constraints == 2)
606 {
607 set_rbf(std::make_shared<RBFWithQuadratic>(
608 // set_rbf(std::make_shared<RBFWithQuadraticLagrange>(
609 assembler, kernel_centers, collocation_points, local_basis_integrals, tmp_quadrature, rhs));
610 }
611 else
612 {
613 throw std::runtime_error(fmt::format("Unsupported constraint order: {:d}", integral_constraints));
614 }
615
616 // Set the bases which are nonzero inside the polygon
617 const int n_poly_bases = int(local_to_global.size());
618 b.bases.resize(n_poly_bases);
619 for (int i = 0; i < n_poly_bases; ++i)
620 {
621 b.bases[i].init(-2, local_to_global[i], i, Eigen::MatrixXd::Constant(1, 3, std::nan("")));
622 }
623
624 // Polygon boundary after geometric mapping from neighboring elements
625 orient_closed_surface(triangulated_vertices, triangulated_faces, false); // stupid viewer is flipping all the faces
626 mapped_boundary[e].first = triangulated_vertices;
627 mapped_boundary[e].second = triangulated_faces;
628 }
629
630 return 0;
631 }
632 } // namespace basis
633} // namespace polyfem
std::vector< std::list< int > > adj
int V
double val
ElementAssemblyValues vals
Quadrature quadrature
int x
virtual std::string name() const =0
static int quadrature_order(const std::string &assembler, const int basis_degree, const BasisType &b_type, const int dim)
utility for retrieving the needed quadrature order to precisely integrate the given form on the given...
stores per local bases evaluations
std::vector< basis::Local2Global > global
stores per element basis values at given quadrature points and geometric mapping
void compute(const int el_index, const bool is_volume, const Eigen::MatrixXd &pts, const basis::ElementBases &basis, const basis::ElementBases &gbasis)
computes the per element values at the local (ref el) points (pts) sets basis_values,...
assemble matrix based on the local assembler local assembler is eg Laplace, LinearElasticity etc
Stores the basis functions for a given element in a mesh (facet in 2d, cell in 3d).
static Eigen::VectorXi hex_face_local_nodes(const bool serendipity, const int q, const mesh::Mesh3D &mesh, mesh::Navigation3D::Index index)
static int build_bases(const assembler::LinearAssembler &assembler, const int n_samples_per_edge, const mesh::Mesh3D &mesh, const int n_bases, const int quadrature_order, const int mass_quadrature_order, const int integral_constraints, std::vector< ElementBases > &bases, const std::vector< ElementBases > &gbases, const std::map< int, InterfaceData > &poly_face_to_data, std::map< int, std::pair< Eigen::MatrixXd, Eigen::MatrixXi > > &mapped_boundary)
Build bases over the remaining polygons of a mesh.
static void compute_integral_constraints(const assembler::Assembler &assembler, const mesh::Mesh3D &mesh, const int n_bases, const std::vector< ElementBases > &bases, const std::vector< ElementBases > &gbases, Eigen::MatrixXd &basis_integrals)
3D mesh interface: the elements are the cells (tetrahedra, hexahedra, prisms, pyramids or general pol...
Definition Mesh3D.hpp:22
bool is_volume() const override
checks if mesh is volume
Definition Mesh3D.hpp:31
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
Definition Mesh.hpp:181
bool is_polytope(const int el_id) const
checks if element is polygon compatible
Definition Mesh.cpp:374
static void get_quadrature(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::RowVector3d &kernel, const int order, Quadrature &quadr)
Gets the quadrature points & weights for a polyhedron.
list indices
Definition p_bases.py:259
void q_nodes_3d(const int q, Eigen::MatrixXd &val)
double compute_epsilon(const Mesh2D &mesh, int e)
void orient_closed_surface(const Eigen::MatrixXd &V, Eigen::MatrixXi &F, bool positive=true)
Orient a triangulated surface to have positive volume.
double signed_volume(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F)
Compute the signed volume of a surface mesh.
void offset_surface_vertices(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::RowVector3d &interior_point, double distance, Eigen::MatrixXd &offset)
Offset along area-weighted surface normals, using the direction away from an interior point where inc...
void reorder_mesh(Eigen::MatrixXd &V, Eigen::MatrixXi &F, const Eigen::VectorXi &C, Eigen::VectorXi &R)
Reorder vertices of a mesh using color tags, so that vertices are ordered by increasing colors.
void signed_squared_distances(const Eigen::MatrixXd &V, const Eigen::MatrixXi &F, const Eigen::MatrixXd &P, Eigen::VectorXd &D)
Computes the signed squared distance from a list of points to a triangle mesh.
bool instantiate_pattern(const Eigen::MatrixXd &IV, const Eigen::MatrixXi &IF, const Eigen::MatrixXd &PV, const Eigen::MatrixXi &PF, Eigen::MatrixXd &OV, Eigen::MatrixXi &OF, Eigen::VectorXi *SF=nullptr, EvalParametersFunc evalFunc=nullptr, GetAdjacentLocalEdge getAdjLocalEdge=nullptr)
std::function< void(const Eigen::MatrixXd &, Eigen::MatrixXd &, int)> EvalParametersFunc
std::function< std::tuple< int, int, bool >(int, int)> GetAdjacentLocalEdge
void regular_2d_grid(const int n, bool tri, Eigen::MatrixXd &V, Eigen::MatrixXi &F)
Generate a canonical triangle/quad subdivided from a regular grid.