|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "bioimage_cpp/array_view.hxx" |
| 4 | +#include "bioimage_cpp/detail/threading.hxx" |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <cstddef> |
| 8 | +#include <cstdint> |
| 9 | +#include <numeric> |
| 10 | +#include <stdexcept> |
| 11 | +#include <string> |
| 12 | +#include <utility> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +namespace bioimage_cpp { |
| 16 | + |
| 17 | +namespace detail::mesh_smoothing { |
| 18 | + |
| 19 | +template <class I> |
| 20 | +struct Adjacency { |
| 21 | + std::vector<std::size_t> offsets; |
| 22 | + std::vector<I> neighbours; |
| 23 | +}; |
| 24 | + |
| 25 | +template <class I> |
| 26 | +Adjacency<I> build_adjacency(const ConstArrayView<I> &faces, std::ptrdiff_t n_verts) { |
| 27 | + const std::ptrdiff_t n_faces = faces.shape[0]; |
| 28 | + |
| 29 | + std::vector<std::pair<I, I>> edges; |
| 30 | + edges.reserve(static_cast<std::size_t>(n_faces) * 3); |
| 31 | + |
| 32 | + const auto signed_n_verts = static_cast<std::int64_t>(n_verts); |
| 33 | + auto check_index = [&](const I value) { |
| 34 | + const auto signed_value = static_cast<std::int64_t>(value); |
| 35 | + if (signed_value < 0 || signed_value >= signed_n_verts) { |
| 36 | + throw std::invalid_argument( |
| 37 | + "faces contains index " + std::to_string(signed_value) |
| 38 | + + " out of range [0, " + std::to_string(n_verts) + ")" |
| 39 | + ); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + for (std::ptrdiff_t f = 0; f < n_faces; ++f) { |
| 44 | + const I a = faces.data[f * 3 + 0]; |
| 45 | + const I b = faces.data[f * 3 + 1]; |
| 46 | + const I c = faces.data[f * 3 + 2]; |
| 47 | + check_index(a); |
| 48 | + check_index(b); |
| 49 | + check_index(c); |
| 50 | + edges.emplace_back(std::min(a, b), std::max(a, b)); |
| 51 | + edges.emplace_back(std::min(b, c), std::max(b, c)); |
| 52 | + edges.emplace_back(std::min(a, c), std::max(a, c)); |
| 53 | + } |
| 54 | + |
| 55 | + std::sort(edges.begin(), edges.end()); |
| 56 | + edges.erase(std::unique(edges.begin(), edges.end()), edges.end()); |
| 57 | + |
| 58 | + std::vector<std::size_t> offsets(static_cast<std::size_t>(n_verts) + 1, 0); |
| 59 | + for (const auto &edge : edges) { |
| 60 | + ++offsets[static_cast<std::size_t>(edge.first) + 1]; |
| 61 | + ++offsets[static_cast<std::size_t>(edge.second) + 1]; |
| 62 | + } |
| 63 | + for (std::size_t i = 1; i < offsets.size(); ++i) { |
| 64 | + offsets[i] += offsets[i - 1]; |
| 65 | + } |
| 66 | + |
| 67 | + std::vector<I> neighbours(offsets.back()); |
| 68 | + std::vector<std::size_t> insert_pos(offsets.begin(), offsets.end() - 1); |
| 69 | + for (const auto &edge : edges) { |
| 70 | + neighbours[insert_pos[static_cast<std::size_t>(edge.first)]++] = edge.second; |
| 71 | + neighbours[insert_pos[static_cast<std::size_t>(edge.second)]++] = edge.first; |
| 72 | + } |
| 73 | + |
| 74 | + return Adjacency<I>{std::move(offsets), std::move(neighbours)}; |
| 75 | +} |
| 76 | + |
| 77 | +} // namespace detail::mesh_smoothing |
| 78 | + |
| 79 | +// Laplacian smoothing of a triangular mesh: each vertex (and corresponding |
| 80 | +// normal) is replaced by the mean of itself and its 1-ring neighbours, |
| 81 | +// repeated for `iterations` passes. `verts` and `normals` are (n_verts, dim); |
| 82 | +// `faces` is (n_faces, 3) with values in [0, n_verts). Adjacency is built once |
| 83 | +// and reused across iterations. For `iterations == 0`, inputs are copied to |
| 84 | +// outputs unchanged. |
| 85 | +template <class V, class I> |
| 86 | +void smooth_mesh( |
| 87 | + const ConstArrayView<V> &verts, |
| 88 | + const ConstArrayView<V> &normals, |
| 89 | + const ConstArrayView<I> &faces, |
| 90 | + std::size_t iterations, |
| 91 | + int n_threads, |
| 92 | + const ArrayView<V> &out_verts, |
| 93 | + const ArrayView<V> &out_normals |
| 94 | +) { |
| 95 | + if (verts.shape.size() != 2) { |
| 96 | + throw std::invalid_argument( |
| 97 | + "verts must have ndim=2, got ndim=" + std::to_string(verts.shape.size()) |
| 98 | + ); |
| 99 | + } |
| 100 | + if (normals.shape != verts.shape) { |
| 101 | + throw std::invalid_argument("normals shape must match verts shape"); |
| 102 | + } |
| 103 | + if (faces.shape.size() != 2 || faces.shape[1] != 3) { |
| 104 | + throw std::invalid_argument("faces must have shape (n_faces, 3)"); |
| 105 | + } |
| 106 | + if (out_verts.shape != verts.shape) { |
| 107 | + throw std::invalid_argument("out_verts shape must match verts shape"); |
| 108 | + } |
| 109 | + if (out_normals.shape != verts.shape) { |
| 110 | + throw std::invalid_argument("out_normals shape must match verts shape"); |
| 111 | + } |
| 112 | + |
| 113 | + const std::ptrdiff_t n_verts = verts.shape[0]; |
| 114 | + const std::ptrdiff_t dim = verts.shape[1]; |
| 115 | + const std::size_t n_total = static_cast<std::size_t>(n_verts) * static_cast<std::size_t>(dim); |
| 116 | + |
| 117 | + if (iterations == 0) { |
| 118 | + std::copy(verts.data, verts.data + n_total, out_verts.data); |
| 119 | + std::copy(normals.data, normals.data + n_total, out_normals.data); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + const auto adjacency = detail::mesh_smoothing::build_adjacency<I>(faces, n_verts); |
| 124 | + |
| 125 | + std::vector<V> scratch_verts(n_total); |
| 126 | + std::vector<V> scratch_normals(n_total); |
| 127 | + |
| 128 | + // Choose initial write target so the final write lands in out_*. |
| 129 | + // iter 0 reads from `verts`/`normals` (input); subsequent iters alternate |
| 130 | + // between out_* and scratch. The last write must be to out_*, so when |
| 131 | + // iterations is even, iter 0 writes scratch (and iter 1 writes out). |
| 132 | + V *buf_a_verts = out_verts.data; |
| 133 | + V *buf_a_normals = out_normals.data; |
| 134 | + V *buf_b_verts = scratch_verts.data(); |
| 135 | + V *buf_b_normals = scratch_normals.data(); |
| 136 | + if (iterations % 2 == 0) { |
| 137 | + std::swap(buf_a_verts, buf_b_verts); |
| 138 | + std::swap(buf_a_normals, buf_b_normals); |
| 139 | + } |
| 140 | + |
| 141 | + const auto &offsets = adjacency.offsets; |
| 142 | + const auto &neighbours = adjacency.neighbours; |
| 143 | + const std::size_t threads = detail::normalize_thread_count( |
| 144 | + n_threads < 0 ? 0 : static_cast<std::size_t>(n_threads), |
| 145 | + static_cast<std::size_t>(n_verts) |
| 146 | + ); |
| 147 | + |
| 148 | + auto smooth_pass = [&](const V *src_verts, const V *src_normals, V *dst_verts, V *dst_normals) { |
| 149 | + detail::parallel_for_chunks( |
| 150 | + threads, |
| 151 | + static_cast<std::size_t>(n_verts), |
| 152 | + [&](std::size_t, std::size_t begin, std::size_t end) { |
| 153 | + for (std::size_t v = begin; v < end; ++v) { |
| 154 | + const std::size_t beg = offsets[v]; |
| 155 | + const std::size_t fin = offsets[v + 1]; |
| 156 | + const auto count = static_cast<V>(fin - beg + 1); |
| 157 | + const std::size_t row = v * static_cast<std::size_t>(dim); |
| 158 | + for (std::ptrdiff_t d = 0; d < dim; ++d) { |
| 159 | + V sum_v = src_verts[row + static_cast<std::size_t>(d)]; |
| 160 | + V sum_n = src_normals[row + static_cast<std::size_t>(d)]; |
| 161 | + for (std::size_t k = beg; k < fin; ++k) { |
| 162 | + const std::size_t nbr_row = |
| 163 | + static_cast<std::size_t>(neighbours[k]) * static_cast<std::size_t>(dim); |
| 164 | + sum_v += src_verts[nbr_row + static_cast<std::size_t>(d)]; |
| 165 | + sum_n += src_normals[nbr_row + static_cast<std::size_t>(d)]; |
| 166 | + } |
| 167 | + dst_verts[row + static_cast<std::size_t>(d)] = sum_v / count; |
| 168 | + dst_normals[row + static_cast<std::size_t>(d)] = sum_n / count; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + ); |
| 173 | + }; |
| 174 | + |
| 175 | + // First iteration: read from inputs, write to buf_a. |
| 176 | + smooth_pass(verts.data, normals.data, buf_a_verts, buf_a_normals); |
| 177 | + |
| 178 | + for (std::size_t it = 1; it < iterations; ++it) { |
| 179 | + const V *src_verts = (it % 2 == 1) ? buf_a_verts : buf_b_verts; |
| 180 | + const V *src_normals = (it % 2 == 1) ? buf_a_normals : buf_b_normals; |
| 181 | + V *dst_verts = (it % 2 == 1) ? buf_b_verts : buf_a_verts; |
| 182 | + V *dst_normals = (it % 2 == 1) ? buf_b_normals : buf_a_normals; |
| 183 | + smooth_pass(src_verts, src_normals, dst_verts, dst_normals); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace bioimage_cpp |
0 commit comments