Mesh: Add C++ implementaiton of topology mappings

Because they are friendlier to use in C++ code than the existing mesh
mapping API, these mappings from one domain to another were often
reimplemented in separate files. This commit moves some basic
implementations to a `mesh_topology` namespace in the existing
mesh mapping header file. These is plenty of room for performance
improvement here, particularly by not using an array of Vectors, but
that can come later.

Split from D16029
This commit is contained in:
Hans Goudey
2022-09-28 14:31:32 -05:00
parent 878dea4e0f
commit 25533dbe21
4 changed files with 63 additions and 15 deletions

View File

@@ -6,6 +6,10 @@
* \ingroup bke
*/
#ifdef __cplusplus
# include "BLI_array.hh"
#endif
#ifdef __cplusplus
extern "C" {
#endif
@@ -330,3 +334,19 @@ int *BKE_mesh_calc_smoothgroups(const struct MEdge *medge,
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
namespace blender::mesh_topology {
Array<int> build_corner_to_poly_map(Span<MPoly> polys, int loops_num);
Array<Vector<int>> build_vert_to_edge_map(Span<MEdge> edges, int verts_num);
Array<Vector<int>> build_vert_to_corner_map(Span<MLoop> loops, int verts_num);
inline int previous_poly_corner(const MPoly &poly, int corner_i)
{
return corner_i - 1 + (corner_i == poly.loopstart) * poly.totloop;
}
} // namespace blender::mesh_topology
#endif

View File

@@ -26,6 +26,7 @@
#include "MEM_guardedalloc.h"
#include "eigen_capi.h"
using blender::Array;
using blender::Map;
using blender::MutableSpan;
using blender::Span;
@@ -220,12 +221,7 @@ class MeshFairingContext : public FairingContext {
}
}
loop_to_poly_map_.reserve(mesh->totloop);
for (int i = 0; i < mesh->totpoly; i++) {
for (int l = 0; l < mpoly_[i].totloop; l++) {
loop_to_poly_map_[l + mpoly_[i].loopstart] = i;
}
}
loop_to_poly_map_ = blender::mesh_topology::build_corner_to_poly_map(mpoly_, mloop_.size());
}
~MeshFairingContext() override
@@ -259,7 +255,7 @@ class MeshFairingContext : public FairingContext {
Span<MLoop> mloop_;
Span<MPoly> mpoly_;
Span<MEdge> medge_;
Vector<int> loop_to_poly_map_;
Array<int> loop_to_poly_map_;
};
class BMeshFairingContext : public FairingContext {

View File

@@ -16,6 +16,7 @@
#include "BLI_bitmap.h"
#include "BLI_buffer.h"
#include "BLI_math.h"
#include "BLI_task.hh"
#include "BLI_utildefines.h"
#include "BKE_customdata.h"
@@ -552,6 +553,41 @@ void BKE_mesh_origindex_map_create_looptri(MeshElemMap **r_map,
*r_mem = indices;
}
namespace blender::mesh_topology {
Array<int> build_corner_to_poly_map(const Span<MPoly> polys, const int loops_num)
{
Array<int> map(loops_num);
threading::parallel_for(polys.index_range(), 1024, [&](IndexRange range) {
for (const int64_t poly_i : range) {
const MPoly &poly = polys[poly_i];
map.as_mutable_span().slice(poly.loopstart, poly.totloop).fill(int(poly_i));
}
});
return map;
}
Array<Vector<int>> build_vert_to_edge_map(const Span<MEdge> edges, const int verts_num)
{
Array<Vector<int>> map(verts_num);
for (const int64_t i : edges.index_range()) {
map[edges[i].v1].append(int(i));
map[edges[i].v2].append(int(i));
}
return map;
}
Array<Vector<int>> build_vert_to_corner_map(const Span<MLoop> loops, const int verts_num)
{
Array<Vector<int>> map(verts_num);
for (const int64_t i : loops.index_range()) {
map[loops[i].v].append(int(i));
}
return map;
}
} // namespace blender::mesh_topology
/** \} */
/* -------------------------------------------------------------------- */

View File

@@ -14,6 +14,7 @@
#include "BKE_DerivedMesh.h"
#include "BKE_customdata.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mapping.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
@@ -44,7 +45,7 @@ class TextureMarginMap {
/** Maps UV-edges to their corresponding UV-edge. */
Vector<int> loop_adjacency_map_;
/** Maps UV-edges to their corresponding polygon. */
Vector<int> loop_to_poly_map_;
Array<int> loop_to_poly_map_;
int w_, h_;
float uv_offset_[2];
@@ -289,13 +290,8 @@ class TextureMarginMap {
void build_tables()
{
loop_to_poly_map_.resize(totloop_);
for (int i = 0; i < totpoly_; i++) {
for (int j = 0; j < mpoly_[i].totloop; j++) {
int l = j + mpoly_[i].loopstart;
loop_to_poly_map_[l] = i;
}
}
loop_to_poly_map_ = blender::mesh_topology::build_corner_to_poly_map({mpoly_, totpoly_},
totloop_);
loop_adjacency_map_.resize(totloop_, -1);