Lagrange
Loading...
Searching...
No Matches
partition_mesh_vertices.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
13#pragma once
14
15#include <lagrange/partitioning/types.h>
16
17#include <lagrange/utils/function_ref.h>
18#include <lagrange/utils/safe_cast.h>
19#include <lagrange/utils/span.h>
20
21#include <Eigen/Core>
23
24namespace lagrange {
25namespace partitioning {
26
27namespace internal {
28
41Eigen::Matrix<index_t, Eigen::Dynamic, 1> partition_mesh_vertices_raw(
42 index_t num_elems,
43 index_t num_nodes,
44 index_t elem_size,
45 function_ref<void(span<int32_t>)> copy_32,
46 function_ref<void(span<int64_t>)> copy_64,
47 index_t num_partitions);
48
49} // namespace internal
50
61template <typename DerivedF>
62Eigen::Matrix<index_t, Eigen::Dynamic, 1> partition_mesh_vertices(
63 const Eigen::MatrixBase<DerivedF>& facets,
64 index_t num_partitions)
65{
66 const auto num_elems = safe_cast<index_t>(facets.rows());
67 const auto num_nodes = safe_cast<index_t>(facets.maxCoeff() + 1);
68 const auto elem_size = safe_cast<index_t>(facets.cols());
69 auto copy_func = [&](auto dst) {
70 using value_t = typename std::decay_t<decltype(dst)>::value_type;
71 for (Eigen::Index f = 0; f < facets.rows(); ++f) {
72 for (Eigen::Index lv = 0; lv < facets.cols(); ++lv) {
73 dst[f * facets.cols() + lv] = static_cast<value_t>(facets(f, lv));
74 }
75 }
76 };
77 auto copy_32 = [&](span<int32_t> dst) { copy_func(dst); };
78 auto copy_64 = [&](span<int64_t> dst) { copy_func(dst); };
79 return internal::partition_mesh_vertices_raw(
80 num_elems,
81 num_nodes,
82 elem_size,
83 copy_32,
84 copy_64,
85 num_partitions);
86}
87
88} // namespace partitioning
89} // namespace lagrange
A lightweight non-owning reference to a callable.
Definition function_ref.h:47
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Mesh partitioning using METIS.
Definition partition_mesh_vertices.h:25
Eigen::Matrix< index_t, Eigen::Dynamic, 1 > partition_mesh_vertices(const Eigen::MatrixBase< DerivedF > &facets, index_t num_partitions)
Partition mesh vertices into num_partitions using METIS.
Definition partition_mesh_vertices.h:62
int32_t index_t
Index type used by METIS.
Definition types.h:20
Main namespace for Lagrange.