2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2013-12-12 16:26:11 +11:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2013-12-12 16:26:11 +11:00
|
|
|
*
|
|
|
|
|
* Functions for accessing mesh connectivity data.
|
2023-07-24 22:06:55 +02:00
|
|
|
* eg: faces connected to verts, UVs connected to verts.
|
2013-12-12 16:26:11 +11:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2023-08-28 22:32:31 +02:00
|
|
|
#include "atomic_ops.h"
|
|
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
#include "DNA_meshdata_types.h"
|
2015-04-09 16:12:20 +02:00
|
|
|
#include "DNA_vec_types.h"
|
2013-12-12 16:26:11 +11:00
|
|
|
|
2022-09-19 18:44:51 -05:00
|
|
|
#include "BLI_array.hh"
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
#include "BLI_bitmap.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_buffer.h"
|
2023-02-09 21:55:32 -05:00
|
|
|
#include "BLI_function_ref.hh"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_geom.h"
|
|
|
|
|
#include "BLI_math_vector.h"
|
2022-09-28 14:31:32 -05:00
|
|
|
#include "BLI_task.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2013-12-12 16:26:11 +11:00
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_customdata.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh.hh"
|
|
|
|
|
#include "BKE_mesh_mapping.hh"
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
#include "BLI_memarena.h"
|
2013-12-12 16:26:11 +11:00
|
|
|
|
|
|
|
|
#include "BLI_strict_flags.h"
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Connectivity Mapping
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
UvVertMap *BKE_mesh_uv_vert_map_create(const blender::OffsetIndices<int> faces,
|
Mesh: Move hide flags to generic attributes
This commit moves the hide status of mesh vertices, edges, and faces
from the `ME_FLAG` to optional generic boolean attributes. Storing this
data as generic attributes can significantly simplify and improve code,
as described in T95965.
The attributes are called `.hide_vert`, `.hide_edge`, and `.hide_poly`,
using the attribute name semantics discussed in T97452. The `.` prefix
means they are "UI attributes", so they still contain original data
edited by users, but they aren't meant to be accessed procedurally by
the user in arbitrary situations. They are also be hidden in the
spreadsheet and the attribute list by default,
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when the hide status is used. When the flags are removed
completely, requirements will decrease when hiding is unused.
Further notes:
* Some code can be further simplified to skip some processing when the
hide attributes don't exist.
* The data is still stored in flags for `BMesh`, necessitating some
complexity in the conversion to and from `Mesh`.
* Access to the "hide" property of mesh elements in RNA is slower.
The separate boolean arrays should be used where possible.
Ref T95965
Differential Revision: https://developer.blender.org/D14685
2022-08-11 12:54:24 -04:00
|
|
|
const bool *hide_poly,
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
const bool *select_poly,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int *corner_verts,
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const float (*mloopuv)[2],
|
2021-06-18 14:27:41 +10:00
|
|
|
uint totvert,
|
2015-05-17 23:04:34 +10:00
|
|
|
const float limit[2],
|
|
|
|
|
const bool selected,
|
|
|
|
|
const bool use_winding)
|
2013-12-12 16:26:11 +11:00
|
|
|
{
|
2023-01-31 14:22:22 +11:00
|
|
|
/* NOTE: N-gon version WIP, based on #BM_uv_vert_map_create. */
|
|
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
UvVertMap *vmap;
|
|
|
|
|
UvMapVert *buf;
|
|
|
|
|
int i, totuv, nverts;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-09 16:12:20 +02:00
|
|
|
BLI_buffer_declare_static(vec2f, tf_uv_buf, BLI_BUFFER_NOP, 32);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-09 16:17:51 +02:00
|
|
|
totuv = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/* generate UvMapVert array */
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t a : faces.index_range()) {
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
if (!selected || (!(hide_poly && hide_poly[a]) && (select_poly && select_poly[a]))) {
|
2023-07-24 22:06:55 +02:00
|
|
|
totuv += int(faces[a].size());
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (totuv == 0) {
|
2022-10-05 13:44:02 -05:00
|
|
|
return nullptr;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
vmap = (UvVertMap *)MEM_callocN(sizeof(*vmap), "UvVertMap");
|
2022-09-25 18:33:28 +10:00
|
|
|
buf = vmap->buf = (UvMapVert *)MEM_callocN(sizeof(*vmap->buf) * size_t(totuv), "UvMapVert");
|
2015-06-20 19:28:51 +10:00
|
|
|
vmap->vert = (UvMapVert **)MEM_callocN(sizeof(*vmap->vert) * totvert, "UvMapVert*");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
if (!vmap->vert || !vmap->buf) {
|
|
|
|
|
BKE_mesh_uv_vert_map_free(vmap);
|
2022-10-05 13:44:02 -05:00
|
|
|
return nullptr;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-01-28 10:50:59 +13:00
|
|
|
bool *winding = nullptr;
|
|
|
|
|
if (use_winding) {
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
winding = static_cast<bool *>(
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_calloc_arrayN(sizeof(*winding), size_t(faces.size()), "winding"));
|
2023-01-28 10:50:59 +13:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t a : faces.index_range()) {
|
|
|
|
|
const blender::IndexRange face = faces[a];
|
Mesh: Move selection flags to generic attributes
Using the attribute name semantics from T97452, this patch moves the
selection status of mesh elements from the `SELECT` of vertices, and
edges, and the `ME_FACE_SEL` of faces to generic boolean attribute
Storing this data as generic attributes can significantly simplify and
improve code, as described in T95965.
The attributes are called `.select_vert`, `.select_edge`, and
`.select_poly`. The `.` prefix means they are "UI attributes",so they
still contain original data edited by users, but they aren't meant to
be accessed procedurally by the user in arbitrary situations. They are
also be hidden in the spreadsheet and the attribute list.
Until 4.0, the attributes are still written to and read from the mesh
in the old way, so neither forward nor backward compatibility are
affected. This means memory requirements will be increased by one byte
per element when selection is used. When the flags are removed
completely, requirements will decrease.
Further notes:
* The `MVert` flag is empty at runtime now, so it can be ignored.
* `BMesh` is unchanged, otherwise the change would be much larger.
* Many tests have slightly different results, since the selection
attribute uses more generic propagation. Previously you couldn't
really rely on edit mode selections being propagated procedurally.
Now it mostly works as expected.
Similar to 2480b55f216c
Ref T95965
Differential Revision: https://developer.blender.org/D15795
2022-09-23 09:38:37 -05:00
|
|
|
if (!selected || (!(hide_poly && hide_poly[a]) && (select_poly && select_poly[a]))) {
|
2022-10-05 13:44:02 -05:00
|
|
|
float(*tf_uv)[2] = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-20 19:28:51 +10:00
|
|
|
if (use_winding) {
|
2023-07-24 22:06:55 +02:00
|
|
|
tf_uv = (float(*)[2])BLI_buffer_reinit_data(&tf_uv_buf, vec2f, size_t(face.size()));
|
2015-06-20 19:28:51 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
nverts = int(face.size());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
for (i = 0; i < nverts; i++) {
|
2023-07-24 22:06:55 +02:00
|
|
|
buf->loop_of_face_index = ushort(i);
|
|
|
|
|
buf->face_index = uint(a);
|
2022-10-05 13:44:02 -05:00
|
|
|
buf->separate = false;
|
2023-07-24 22:06:55 +02:00
|
|
|
buf->next = vmap->vert[corner_verts[face[i]]];
|
|
|
|
|
vmap->vert[corner_verts[face[i]]] = buf;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-20 19:28:51 +10:00
|
|
|
if (use_winding) {
|
2023-07-24 22:06:55 +02:00
|
|
|
copy_v2_v2(tf_uv[i], mloopuv[face[i]]);
|
2015-06-20 19:28:51 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
buf++;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-06-20 19:28:51 +10:00
|
|
|
if (use_winding) {
|
2022-09-25 18:33:28 +10:00
|
|
|
winding[a] = cross_poly_v2(tf_uv, uint(nverts)) > 0;
|
2015-06-20 19:28:51 +10:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/* sort individual uvs for each vert */
|
2023-03-03 10:50:54 -05:00
|
|
|
for (uint a = 0; a < totvert; a++) {
|
2022-10-05 13:44:02 -05:00
|
|
|
UvMapVert *newvlist = nullptr, *vlist = vmap->vert[a];
|
2013-12-12 16:26:11 +11:00
|
|
|
UvMapVert *iterv, *v, *lastv, *next;
|
2018-07-16 15:58:12 +02:00
|
|
|
const float *uv, *uv2;
|
|
|
|
|
float uvdiff[2];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
while (vlist) {
|
|
|
|
|
v = vlist;
|
|
|
|
|
vlist = vlist->next;
|
|
|
|
|
v->next = newvlist;
|
|
|
|
|
newvlist = v;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
uv = mloopuv[faces[v->face_index].start() + v->loop_of_face_index];
|
2022-10-05 13:44:02 -05:00
|
|
|
lastv = nullptr;
|
2013-12-12 16:26:11 +11:00
|
|
|
iterv = vlist;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
while (iterv) {
|
|
|
|
|
next = iterv->next;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
uv2 = mloopuv[faces[iterv->face_index].start() + iterv->loop_of_face_index];
|
2013-12-12 16:26:11 +11:00
|
|
|
sub_v2_v2v2(uvdiff, uv2, uv);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-09 16:12:20 +02:00
|
|
|
if (fabsf(uv[0] - uv2[0]) < limit[0] && fabsf(uv[1] - uv2[1]) < limit[1] &&
|
2023-07-24 22:06:55 +02:00
|
|
|
(!use_winding || winding[iterv->face_index] == winding[v->face_index]))
|
2018-07-16 15:37:27 +02:00
|
|
|
{
|
2019-04-22 09:39:35 +10:00
|
|
|
if (lastv) {
|
2013-12-12 16:26:11 +11:00
|
|
|
lastv->next = next;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2013-12-12 16:26:11 +11:00
|
|
|
vlist = next;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
iterv->next = newvlist;
|
|
|
|
|
newvlist = iterv;
|
|
|
|
|
}
|
2019-04-22 09:39:35 +10:00
|
|
|
else {
|
2013-12-12 16:26:11 +11:00
|
|
|
lastv = iterv;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
iterv = next;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-05 13:44:02 -05:00
|
|
|
newvlist->separate = true;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
vmap->vert[a] = newvlist;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-06-02 15:38:04 +10:00
|
|
|
if (use_winding) {
|
2015-06-20 19:28:51 +10:00
|
|
|
MEM_freeN(winding);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-09 16:12:20 +02:00
|
|
|
BLI_buffer_free(&tf_uv_buf);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
return vmap;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-18 14:27:41 +10:00
|
|
|
UvMapVert *BKE_mesh_uv_vert_map_get_vert(UvVertMap *vmap, uint v)
|
2013-12-12 16:26:11 +11:00
|
|
|
{
|
|
|
|
|
return vmap->vert[v];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_uv_vert_map_free(UvVertMap *vmap)
|
|
|
|
|
{
|
|
|
|
|
if (vmap) {
|
2019-04-22 09:39:35 +10:00
|
|
|
if (vmap->vert) {
|
2013-12-12 16:26:11 +11:00
|
|
|
MEM_freeN(vmap->vert);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
if (vmap->buf) {
|
2013-12-12 16:26:11 +11:00
|
|
|
MEM_freeN(vmap->buf);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
MEM_freeN(vmap);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
void BKE_mesh_vert_looptri_map_create(MeshElemMap **r_map,
|
|
|
|
|
int **r_mem,
|
|
|
|
|
const int totvert,
|
|
|
|
|
const MLoopTri *mlooptri,
|
|
|
|
|
const int totlooptri,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int *corner_verts,
|
2022-10-03 17:37:25 -05:00
|
|
|
const int /*totloop*/)
|
2016-05-21 12:38:02 +02:00
|
|
|
{
|
2022-09-25 18:33:28 +10:00
|
|
|
MeshElemMap *map = MEM_cnew_array<MeshElemMap>(size_t(totvert), __func__);
|
|
|
|
|
int *indices = static_cast<int *>(MEM_mallocN(sizeof(int) * size_t(totlooptri) * 3, __func__));
|
2016-05-21 12:38:02 +02:00
|
|
|
int *index_step;
|
|
|
|
|
const MLoopTri *mlt;
|
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
/* count face users */
|
|
|
|
|
for (i = 0, mlt = mlooptri; i < totlooptri; mlt++, i++) {
|
|
|
|
|
for (int j = 3; j--;) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
map[corner_verts[mlt->tri[j]]].count++;
|
2016-05-21 12:38:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
/* create offsets */
|
|
|
|
|
index_step = indices;
|
|
|
|
|
for (i = 0; i < totvert; i++) {
|
|
|
|
|
map[i].indices = index_step;
|
|
|
|
|
index_step += map[i].count;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
/* re-count, using this as an index below */
|
|
|
|
|
map[i].count = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
/* assign looptri-edge users */
|
|
|
|
|
for (i = 0, mlt = mlooptri; i < totlooptri; mlt++, i++) {
|
|
|
|
|
for (int j = 3; j--;) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
MeshElemMap *map_ele = &map[corner_verts[mlt->tri[j]]];
|
2016-05-21 12:38:02 +02:00
|
|
|
map_ele->indices[map_ele->count++] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-05-21 12:38:02 +02:00
|
|
|
*r_map = map;
|
|
|
|
|
*r_mem = indices;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 08:36:10 +02:00
|
|
|
void BKE_mesh_origindex_map_create(MeshElemMap **r_map,
|
|
|
|
|
int **r_mem,
|
|
|
|
|
const int totsource,
|
|
|
|
|
const int *final_origindex,
|
|
|
|
|
const int totfinal)
|
2013-12-12 17:11:27 +11:00
|
|
|
{
|
2022-09-25 18:33:28 +10:00
|
|
|
MeshElemMap *map = MEM_cnew_array<MeshElemMap>(size_t(totsource), __func__);
|
|
|
|
|
int *indices = static_cast<int *>(MEM_mallocN(sizeof(int) * size_t(totfinal), __func__));
|
2013-12-12 17:11:27 +11:00
|
|
|
int *index_step;
|
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 17:11:27 +11:00
|
|
|
/* count face users */
|
|
|
|
|
for (i = 0; i < totfinal; i++) {
|
|
|
|
|
if (final_origindex[i] != ORIGINDEX_NONE) {
|
|
|
|
|
BLI_assert(final_origindex[i] < totsource);
|
|
|
|
|
map[final_origindex[i]].count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 17:11:27 +11:00
|
|
|
/* create offsets */
|
|
|
|
|
index_step = indices;
|
|
|
|
|
for (i = 0; i < totsource; i++) {
|
|
|
|
|
map[i].indices = index_step;
|
|
|
|
|
index_step += map[i].count;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 17:11:27 +11:00
|
|
|
/* re-count, using this as an index below */
|
|
|
|
|
map[i].count = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-26 15:23:26 +10:00
|
|
|
/* Assign face-tessellation users. */
|
2013-12-12 17:11:27 +11:00
|
|
|
for (i = 0; i < totfinal; i++) {
|
|
|
|
|
if (final_origindex[i] != ORIGINDEX_NONE) {
|
|
|
|
|
MeshElemMap *map_ele = &map[final_origindex[i]];
|
|
|
|
|
map_ele->indices[map_ele->count++] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 17:11:27 +11:00
|
|
|
*r_map = map;
|
|
|
|
|
*r_mem = indices;
|
|
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
|
2015-07-23 15:08:27 +10:00
|
|
|
void BKE_mesh_origindex_map_create_looptri(MeshElemMap **r_map,
|
|
|
|
|
int **r_mem,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices<int> faces,
|
|
|
|
|
const int *looptri_faces,
|
2015-07-23 15:08:27 +10:00
|
|
|
const int looptri_num)
|
|
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
MeshElemMap *map = MEM_cnew_array<MeshElemMap>(size_t(faces.size()), __func__);
|
2022-09-25 18:33:28 +10:00
|
|
|
int *indices = static_cast<int *>(MEM_mallocN(sizeof(int) * size_t(looptri_num), __func__));
|
2015-07-23 15:08:27 +10:00
|
|
|
int *index_step;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 15:08:27 +10:00
|
|
|
/* create offsets */
|
|
|
|
|
index_step = indices;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t i : faces.index_range()) {
|
2015-07-23 15:08:27 +10:00
|
|
|
map[i].indices = index_step;
|
2023-09-07 08:36:43 -04:00
|
|
|
index_step += blender::bke::mesh::face_triangles_num(int(faces[i].size()));
|
2015-07-23 15:08:27 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-26 15:23:26 +10:00
|
|
|
/* Assign face-tessellation users. */
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
for (int i = 0; i < looptri_num; i++) {
|
2023-07-24 22:06:55 +02:00
|
|
|
MeshElemMap *map_ele = &map[looptri_faces[i]];
|
2015-07-23 15:08:27 +10:00
|
|
|
map_ele->indices[map_ele->count++] = i;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-23 15:08:27 +10:00
|
|
|
*r_map = map;
|
|
|
|
|
*r_mem = indices;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
namespace blender::bke::mesh {
|
|
|
|
|
|
|
|
|
|
static Array<int> create_reverse_offsets(const Span<int> indices, const int items_num)
|
|
|
|
|
{
|
|
|
|
|
Array<int> offsets(items_num + 1, 0);
|
2023-07-03 18:47:03 -04:00
|
|
|
offset_indices::build_reverse_offsets(indices, offsets);
|
2023-05-24 13:16:57 +02:00
|
|
|
return offsets;
|
|
|
|
|
}
|
2022-09-28 14:31:32 -05:00
|
|
|
|
2023-08-28 22:32:31 +02:00
|
|
|
static void sort_small_groups(const OffsetIndices<int> groups,
|
|
|
|
|
const int grain_size,
|
|
|
|
|
MutableSpan<int> indices)
|
|
|
|
|
{
|
|
|
|
|
threading::parallel_for(groups.index_range(), grain_size, [&](const IndexRange range) {
|
|
|
|
|
for (const int64_t index : range) {
|
|
|
|
|
MutableSpan<int> group = indices.slice(groups[index]);
|
|
|
|
|
std::sort(group.begin(), group.end());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Array<int> reverse_indices_in_groups(const Span<int> group_indices,
|
|
|
|
|
const OffsetIndices<int> offsets)
|
|
|
|
|
{
|
2023-09-02 03:55:42 +02:00
|
|
|
if (group_indices.is_empty()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-08-28 22:32:31 +02:00
|
|
|
BLI_assert(*std::max_element(group_indices.begin(), group_indices.end()) < offsets.size());
|
|
|
|
|
BLI_assert(*std::min_element(group_indices.begin(), group_indices.end()) >= 0);
|
2023-09-08 16:18:38 +02:00
|
|
|
|
|
|
|
|
/* `counts` keeps track of how many elements have been added to each group, and is incremented
|
|
|
|
|
* atomically by many threads in parallel. `calloc` can be measurably faster than a parallel fill
|
|
|
|
|
* of zero. Alternatively the offsets could be copied and incremented directly, but the cost of
|
|
|
|
|
* the copy is slightly higher than the cost of `calloc`. */
|
|
|
|
|
int *counts = MEM_cnew_array<int>(size_t(offsets.size()), __func__);
|
|
|
|
|
BLI_SCOPED_DEFER([&]() { MEM_freeN(counts); })
|
2023-08-28 22:32:31 +02:00
|
|
|
Array<int> results(group_indices.size());
|
|
|
|
|
threading::parallel_for(group_indices.index_range(), 1024, [&](const IndexRange range) {
|
|
|
|
|
for (const int64_t i : range) {
|
|
|
|
|
const int group_index = group_indices[i];
|
2023-09-08 16:18:38 +02:00
|
|
|
const int index_in_group = atomic_fetch_and_add_int32(&counts[group_index], 1);
|
2023-08-28 22:32:31 +02:00
|
|
|
results[offsets[group_index][index_in_group]] = int(i);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
sort_small_groups(offsets, 1024, results);
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 18:22:47 -05:00
|
|
|
/* A version of #reverse_indices_in_groups that stores face indices instead of corner indices. */
|
|
|
|
|
static void reverse_group_indices_in_groups(const OffsetIndices<int> groups,
|
|
|
|
|
const Span<int> group_to_elem,
|
|
|
|
|
const OffsetIndices<int> offsets,
|
|
|
|
|
MutableSpan<int> results)
|
|
|
|
|
{
|
|
|
|
|
int *counts = MEM_cnew_array<int>(size_t(offsets.size()), __func__);
|
|
|
|
|
BLI_SCOPED_DEFER([&]() { MEM_freeN(counts); })
|
|
|
|
|
threading::parallel_for(groups.index_range(), 1024, [&](const IndexRange range) {
|
|
|
|
|
for (const int64_t face : range) {
|
|
|
|
|
for (const int elem : group_to_elem.slice(groups[face])) {
|
|
|
|
|
const int index_in_group = atomic_fetch_and_add_int32(&counts[elem], 1);
|
|
|
|
|
results[offsets[elem][index_in_group]] = int(face);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
sort_small_groups(offsets, 1024, results);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 22:32:31 +02:00
|
|
|
static GroupedSpan<int> gather_groups(const Span<int> group_indices,
|
|
|
|
|
const int groups_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
|
|
|
|
{
|
|
|
|
|
r_offsets = create_reverse_offsets(group_indices, groups_num);
|
|
|
|
|
r_indices = reverse_indices_in_groups(group_indices, r_offsets.as_span());
|
|
|
|
|
return {OffsetIndices<int>(r_offsets), r_indices};
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<int> build_loop_to_face_map(const OffsetIndices<int> faces)
|
2022-09-28 14:31:32 -05:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<int> map(faces.total_size());
|
|
|
|
|
offset_indices::build_reverse_map(faces, map);
|
2022-09-28 14:31:32 -05:00
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
GroupedSpan<int> build_vert_to_edge_map(const Span<int2> edges,
|
|
|
|
|
const int verts_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
2022-09-28 14:31:32 -05:00
|
|
|
{
|
2023-05-24 13:16:57 +02:00
|
|
|
r_offsets = create_reverse_offsets(edges.cast<int>(), verts_num);
|
2023-11-21 18:22:47 -05:00
|
|
|
const OffsetIndices<int> offsets(r_offsets);
|
|
|
|
|
r_indices.reinitialize(offsets.total_size());
|
2023-05-24 13:16:57 +02:00
|
|
|
|
2023-11-21 18:22:47 -05:00
|
|
|
/* Version of #reverse_indices_in_groups that accounts for storing two indices for each edge. */
|
|
|
|
|
int *counts = MEM_cnew_array<int>(size_t(offsets.size()), __func__);
|
|
|
|
|
BLI_SCOPED_DEFER([&]() { MEM_freeN(counts); })
|
|
|
|
|
threading::parallel_for(edges.index_range(), 1024, [&](const IndexRange range) {
|
|
|
|
|
for (const int64_t edge : range) {
|
|
|
|
|
for (const int vert : {edges[edge][0], edges[edge][1]}) {
|
|
|
|
|
const int index_in_group = atomic_fetch_and_add_int32(&counts[vert], 1);
|
|
|
|
|
r_indices[offsets[vert][index_in_group]] = int(edge);
|
|
|
|
|
}
|
2023-05-24 13:16:57 +02:00
|
|
|
}
|
2023-11-21 18:22:47 -05:00
|
|
|
});
|
2023-11-29 14:16:37 +01:00
|
|
|
sort_small_groups(offsets, 1024, r_indices);
|
2023-11-21 18:22:47 -05:00
|
|
|
return {offsets, r_indices};
|
2022-09-28 14:31:32 -05:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:41:59 +02:00
|
|
|
void build_vert_to_face_indices(const OffsetIndices<int> faces,
|
|
|
|
|
const Span<int> corner_verts,
|
|
|
|
|
const OffsetIndices<int> offsets,
|
|
|
|
|
MutableSpan<int> r_indices)
|
|
|
|
|
{
|
2023-11-21 18:22:47 -05:00
|
|
|
reverse_group_indices_in_groups(faces, corner_verts, offsets, r_indices);
|
2023-08-30 23:41:59 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
GroupedSpan<int> build_vert_to_face_map(const OffsetIndices<int> faces,
|
2023-05-24 13:16:57 +02:00
|
|
|
const Span<int> corner_verts,
|
|
|
|
|
const int verts_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
2022-11-27 23:03:38 -06:00
|
|
|
{
|
2023-05-24 13:16:57 +02:00
|
|
|
r_offsets = create_reverse_offsets(corner_verts, verts_num);
|
|
|
|
|
r_indices.reinitialize(r_offsets.last());
|
2023-08-30 23:41:59 +02:00
|
|
|
build_vert_to_face_indices(faces, corner_verts, OffsetIndices<int>(r_offsets), r_indices);
|
2023-05-24 13:16:57 +02:00
|
|
|
return {OffsetIndices<int>(r_offsets), r_indices};
|
2022-11-27 23:03:38 -06:00
|
|
|
}
|
|
|
|
|
|
2023-08-30 23:41:59 +02:00
|
|
|
Array<int> build_vert_to_corner_indices(const Span<int> corner_verts,
|
|
|
|
|
const OffsetIndices<int> offsets)
|
|
|
|
|
{
|
|
|
|
|
return reverse_indices_in_groups(corner_verts, offsets);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
GroupedSpan<int> build_vert_to_loop_map(const Span<int> corner_verts,
|
|
|
|
|
const int verts_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
2022-09-28 14:31:32 -05:00
|
|
|
{
|
2023-08-28 22:32:31 +02:00
|
|
|
return gather_groups(corner_verts, verts_num, r_offsets, r_indices);
|
2022-11-20 15:42:10 -06:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
GroupedSpan<int> build_edge_to_loop_map(const Span<int> corner_edges,
|
|
|
|
|
const int edges_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
2023-02-09 16:27:20 +01:00
|
|
|
{
|
2023-08-28 22:32:31 +02:00
|
|
|
return gather_groups(corner_edges, edges_num, r_offsets, r_indices);
|
2023-02-09 16:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
GroupedSpan<int> build_edge_to_face_map(const OffsetIndices<int> faces,
|
2023-05-24 13:16:57 +02:00
|
|
|
const Span<int> corner_edges,
|
|
|
|
|
const int edges_num,
|
|
|
|
|
Array<int> &r_offsets,
|
|
|
|
|
Array<int> &r_indices)
|
2022-11-20 15:42:10 -06:00
|
|
|
{
|
2023-05-24 13:16:57 +02:00
|
|
|
r_offsets = create_reverse_offsets(corner_edges, edges_num);
|
|
|
|
|
r_indices.reinitialize(r_offsets.last());
|
2023-11-21 18:22:47 -05:00
|
|
|
reverse_group_indices_in_groups(faces, corner_edges, OffsetIndices<int>(r_offsets), r_indices);
|
2023-05-24 13:16:57 +02:00
|
|
|
return {OffsetIndices<int>(r_offsets), r_indices};
|
2022-11-20 15:42:10 -06:00
|
|
|
}
|
|
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
} // namespace blender::bke::mesh
|
2022-09-28 14:31:32 -05:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2023-07-24 22:06:55 +02:00
|
|
|
/** \name Mesh loops/face islands.
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
* Used currently for UVs and 'smooth groups'.
|
2013-12-12 16:26:11 +11:00
|
|
|
* \{ */
|
|
|
|
|
|
2015-04-28 14:15:27 +02:00
|
|
|
/**
|
2023-07-24 22:06:55 +02:00
|
|
|
* Callback deciding whether the given face/loop/edge define an island boundary or not.
|
2013-12-12 16:26:11 +11:00
|
|
|
*/
|
2023-02-09 21:55:32 -05:00
|
|
|
using MeshRemap_CheckIslandBoundary =
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::FunctionRef<bool(int face_index,
|
2023-02-09 21:55:32 -05:00
|
|
|
int loop_index,
|
|
|
|
|
int edge_index,
|
|
|
|
|
int edge_user_count,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::Span<int> edge_face_map_elem)>;
|
2023-02-09 21:55:32 -05:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
static void face_edge_loop_islands_calc(const int totedge,
|
|
|
|
|
const blender::OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const blender::Span<int> corner_edges,
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::GroupedSpan<int> edge_face_map,
|
2016-07-21 16:53:00 +02:00
|
|
|
const bool use_bitflags,
|
|
|
|
|
MeshRemap_CheckIslandBoundary edge_boundary_check,
|
2023-07-24 22:06:55 +02:00
|
|
|
int **r_face_groups,
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
int *r_totgroup,
|
|
|
|
|
BLI_bitmap **r_edge_borders,
|
|
|
|
|
int *r_totedgeborder)
|
2013-12-12 16:26:11 +11:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_groups;
|
|
|
|
|
int *face_stack;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-05 13:44:02 -05:00
|
|
|
BLI_bitmap *edge_borders = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
int num_edgeborders = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
int face_prev = 0;
|
|
|
|
|
const int temp_face_group_id = 3; /* Placeholder value. */
|
2019-07-02 22:17:22 +10:00
|
|
|
|
|
|
|
|
/* Group we could not find any available bit, will be reset to 0 at end. */
|
2023-07-24 22:06:55 +02:00
|
|
|
const int face_group_id_overflowed = 5;
|
2019-07-02 22:17:22 +10:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
int tot_group = 0;
|
|
|
|
|
bool group_id_overflow = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (faces.size() == 0) {
|
2013-12-12 16:26:11 +11:00
|
|
|
*r_totgroup = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
*r_face_groups = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (r_edge_borders) {
|
2022-10-05 13:44:02 -05:00
|
|
|
*r_edge_borders = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
*r_totedgeborder = 0;
|
|
|
|
|
}
|
|
|
|
|
return;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (r_edge_borders) {
|
|
|
|
|
edge_borders = BLI_BITMAP_NEW(totedge, __func__);
|
|
|
|
|
*r_totedgeborder = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
blender::Array<int> edge_to_face_src_offsets;
|
|
|
|
|
blender::Array<int> edge_to_face_src_indices;
|
|
|
|
|
if (edge_face_map.is_empty()) {
|
|
|
|
|
edge_face_map = blender::bke::mesh::build_edge_to_face_map(
|
|
|
|
|
faces, corner_edges, totedge, edge_to_face_src_offsets, edge_to_face_src_indices);
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_groups = static_cast<int *>(MEM_callocN(sizeof(int) * size_t(faces.size()), __func__));
|
|
|
|
|
face_stack = static_cast<int *>(MEM_mallocN(sizeof(int) * size_t(faces.size()), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
while (true) {
|
2023-07-24 22:06:55 +02:00
|
|
|
int face;
|
|
|
|
|
int bit_face_group_mask = 0;
|
|
|
|
|
int face_group_id;
|
2013-12-12 16:26:11 +11:00
|
|
|
int ps_curr_idx = 0, ps_end_idx = 0; /* stack indices */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (face = face_prev; face < int(faces.size()); face++) {
|
|
|
|
|
if (face_groups[face] == 0) {
|
2013-12-12 16:26:11 +11:00
|
|
|
break;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (face == int(faces.size())) {
|
2013-12-12 16:26:11 +11:00
|
|
|
/* all done */
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_group_id = use_bitflags ? temp_face_group_id : ++tot_group;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/* start searching from here next time */
|
2023-07-24 22:06:55 +02:00
|
|
|
face_prev = face + 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_groups[face] = face_group_id;
|
|
|
|
|
face_stack[ps_end_idx++] = face;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
while (ps_curr_idx != ps_end_idx) {
|
2023-07-24 22:06:55 +02:00
|
|
|
face = face_stack[ps_curr_idx++];
|
|
|
|
|
BLI_assert(face_groups[face] == face_group_id);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t loop : faces[face]) {
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int edge = corner_edges[loop];
|
2023-07-24 22:06:55 +02:00
|
|
|
/* loop over face users */
|
|
|
|
|
const blender::Span<int> map_ele = edge_face_map[edge];
|
2023-05-24 13:16:57 +02:00
|
|
|
const int *p = map_ele.data();
|
|
|
|
|
int i = int(map_ele.size());
|
2023-07-24 22:06:55 +02:00
|
|
|
if (!edge_boundary_check(face, int(loop), edge, i, map_ele)) {
|
2013-12-12 16:26:11 +11:00
|
|
|
for (; i--; p++) {
|
|
|
|
|
/* if we meet other non initialized its a bug */
|
2023-07-24 22:06:55 +02:00
|
|
|
BLI_assert(ELEM(face_groups[*p], 0, face_group_id));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (face_groups[*p] == 0) {
|
|
|
|
|
face_groups[*p] = face_group_id;
|
|
|
|
|
face_stack[ps_end_idx++] = *p;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
|
|
|
|
}
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
else {
|
2023-02-09 21:55:32 -05:00
|
|
|
if (edge_borders && !BLI_BITMAP_TEST(edge_borders, edge)) {
|
|
|
|
|
BLI_BITMAP_ENABLE(edge_borders, edge);
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
num_edgeborders++;
|
|
|
|
|
}
|
|
|
|
|
if (use_bitflags) {
|
2019-04-27 12:07:07 +10:00
|
|
|
/* Find contiguous smooth groups already assigned,
|
|
|
|
|
* these are the values we can't reuse! */
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
for (; i--; p++) {
|
2023-07-24 22:06:55 +02:00
|
|
|
int bit = face_groups[*p];
|
|
|
|
|
if (!ELEM(bit, 0, face_group_id, face_group_id_overflowed) &&
|
|
|
|
|
!(bit_face_group_mask & bit)) {
|
|
|
|
|
bit_face_group_mask |= bit;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
/* And now, we have all our face from current group in face_stack
|
2019-04-27 12:07:07 +10:00
|
|
|
* (from 0 to (ps_end_idx - 1)),
|
2023-07-24 22:06:55 +02:00
|
|
|
* as well as all smoothgroups bits we can't use in bit_face_group_mask.
|
2013-12-12 16:26:11 +11:00
|
|
|
*/
|
|
|
|
|
if (use_bitflags) {
|
|
|
|
|
int i, *p, gid_bit = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
face_group_id = 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/* Find first bit available! */
|
2023-07-24 22:06:55 +02:00
|
|
|
for (; (face_group_id & bit_face_group_mask) && (gid_bit < 32); gid_bit++) {
|
|
|
|
|
face_group_id <<= 1; /* will 'overflow' on last possible iteration. */
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
|
|
|
|
if (UNLIKELY(gid_bit > 31)) {
|
|
|
|
|
/* All bits used in contiguous smooth groups, we can't do much!
|
2021-07-03 23:08:40 +10:00
|
|
|
* NOTE: this is *very* unlikely - theoretically, four groups are enough,
|
2020-11-20 11:39:03 +11:00
|
|
|
* I don't think we can reach this goal with such a simple algorithm,
|
2019-04-27 12:07:07 +10:00
|
|
|
* but I don't think either we'll never need all 32 groups!
|
2013-12-12 16:26:11 +11:00
|
|
|
*/
|
|
|
|
|
printf(
|
|
|
|
|
"Warning, could not find an available id for current smooth group, faces will me "
|
|
|
|
|
"marked "
|
|
|
|
|
"as out of any smooth group...\n");
|
2019-07-02 22:17:22 +10:00
|
|
|
|
|
|
|
|
/* Can't use 0, will have to set them to this value later. */
|
2023-07-24 22:06:55 +02:00
|
|
|
face_group_id = face_group_id_overflowed;
|
2019-07-02 22:17:22 +10:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
group_id_overflow = true;
|
|
|
|
|
}
|
|
|
|
|
if (gid_bit > tot_group) {
|
|
|
|
|
tot_group = gid_bit;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
/* And assign the final smooth group id to that face group! */
|
|
|
|
|
for (i = ps_end_idx, p = face_stack; i--; p++) {
|
|
|
|
|
face_groups[*p] = face_group_id;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-10-13 10:32:59 +02:00
|
|
|
if (use_bitflags) {
|
|
|
|
|
/* used bits are zero-based. */
|
|
|
|
|
tot_group++;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
if (UNLIKELY(group_id_overflow)) {
|
2023-07-24 22:06:55 +02:00
|
|
|
int i = int(faces.size()), *gid = face_groups;
|
2013-12-12 16:26:11 +11:00
|
|
|
for (; i--; gid++) {
|
2023-07-24 22:06:55 +02:00
|
|
|
if (*gid == face_group_id_overflowed) {
|
2013-12-12 16:26:11 +11:00
|
|
|
*gid = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-13 10:32:59 +02:00
|
|
|
/* Using 0 as group id adds one more group! */
|
|
|
|
|
tot_group++;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_freeN(face_stack);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-10-13 10:32:59 +02:00
|
|
|
*r_totgroup = tot_group;
|
2023-07-24 22:06:55 +02:00
|
|
|
*r_face_groups = face_groups;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (r_edge_borders) {
|
|
|
|
|
*r_edge_borders = edge_borders;
|
|
|
|
|
*r_totedgeborder = num_edgeborders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 21:59:47 -04:00
|
|
|
int *BKE_mesh_calc_smoothgroups(int edges_num,
|
|
|
|
|
const blender::OffsetIndices<int> faces,
|
|
|
|
|
const blender::Span<int> corner_edges,
|
2023-01-10 16:12:14 -05:00
|
|
|
const bool *sharp_edges,
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
const bool *sharp_faces,
|
2018-07-13 08:36:10 +02:00
|
|
|
int *r_totgroup,
|
2023-07-25 21:59:47 -04:00
|
|
|
bool use_bitflags)
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_groups = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
auto face_is_smooth = [&](const int i) { return !(sharp_faces && sharp_faces[i]); };
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
auto face_is_island_boundary_smooth = [&](const int face_index,
|
2023-02-09 21:55:32 -05:00
|
|
|
const int /*loop_index*/,
|
|
|
|
|
const int edge_index,
|
|
|
|
|
const int edge_user_count,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::Span<int> edge_face_map_elem) {
|
|
|
|
|
/* Edge is sharp if one of its faces is flat, or edge itself is sharp,
|
|
|
|
|
* or edge is not used by exactly two faces. */
|
|
|
|
|
if (face_is_smooth(face_index) && !(sharp_edges && sharp_edges[edge_index]) &&
|
2023-02-09 21:55:32 -05:00
|
|
|
(edge_user_count == 2))
|
|
|
|
|
{
|
2023-07-24 22:06:55 +02:00
|
|
|
/* In that case, edge appears to be smooth, but we need to check its other face too. */
|
|
|
|
|
const int other_face_index = (face_index == edge_face_map_elem[0]) ? edge_face_map_elem[1] :
|
|
|
|
|
edge_face_map_elem[0];
|
|
|
|
|
return !face_is_smooth(other_face_index);
|
2023-02-09 21:55:32 -05:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-25 21:59:47 -04:00
|
|
|
face_edge_loop_islands_calc(edges_num,
|
|
|
|
|
faces,
|
|
|
|
|
corner_edges,
|
2023-05-24 13:16:57 +02:00
|
|
|
{},
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
use_bitflags,
|
2023-07-24 22:06:55 +02:00
|
|
|
face_is_island_boundary_smooth,
|
|
|
|
|
&face_groups,
|
2016-07-21 16:53:00 +02:00
|
|
|
r_totgroup,
|
2022-10-05 13:44:02 -05:00
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
return face_groups;
|
2013-12-12 16:26:11 +11:00
|
|
|
}
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
|
|
|
|
|
#define MISLAND_DEFAULT_BUFSIZE 64
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_loop_islands_init(MeshIslandStore *island_store,
|
|
|
|
|
const short item_type,
|
|
|
|
|
const int items_num,
|
|
|
|
|
const short island_type,
|
|
|
|
|
const short innercut_type)
|
|
|
|
|
{
|
|
|
|
|
MemArena *mem = island_store->mem;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-05 13:44:02 -05:00
|
|
|
if (mem == nullptr) {
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
mem = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
|
|
|
|
|
island_store->mem = mem;
|
|
|
|
|
}
|
|
|
|
|
/* else memarena should be cleared */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
BLI_assert(
|
|
|
|
|
ELEM(item_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));
|
|
|
|
|
BLI_assert(ELEM(
|
|
|
|
|
island_type, MISLAND_TYPE_VERT, MISLAND_TYPE_EDGE, MISLAND_TYPE_POLY, MISLAND_TYPE_LOOP));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
island_store->item_type = item_type;
|
|
|
|
|
island_store->items_to_islands_num = items_num;
|
2022-09-19 18:44:51 -05:00
|
|
|
island_store->items_to_islands = static_cast<int *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
BLI_memarena_alloc(mem, sizeof(*island_store->items_to_islands) * size_t(items_num)));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
island_store->island_type = island_type;
|
|
|
|
|
island_store->islands_num_alloc = MISLAND_DEFAULT_BUFSIZE;
|
2022-09-19 18:44:51 -05:00
|
|
|
island_store->islands = static_cast<MeshElemMap **>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*island_store->islands) * island_store->islands_num_alloc));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
island_store->innercut_type = innercut_type;
|
2022-09-19 18:44:51 -05:00
|
|
|
island_store->innercuts = static_cast<MeshElemMap **>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*island_store->innercuts) * island_store->islands_num_alloc));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_loop_islands_clear(MeshIslandStore *island_store)
|
|
|
|
|
{
|
|
|
|
|
island_store->item_type = MISLAND_TYPE_NONE;
|
|
|
|
|
island_store->items_to_islands_num = 0;
|
2022-10-05 13:44:02 -05:00
|
|
|
island_store->items_to_islands = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
|
|
|
|
|
island_store->island_type = MISLAND_TYPE_NONE;
|
|
|
|
|
island_store->islands_num = 0;
|
2022-10-05 13:44:02 -05:00
|
|
|
island_store->islands = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
|
|
|
|
|
island_store->innercut_type = MISLAND_TYPE_NONE;
|
2022-10-05 13:44:02 -05:00
|
|
|
island_store->innercuts = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
|
|
|
|
|
if (island_store->mem) {
|
|
|
|
|
BLI_memarena_clear(island_store->mem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
island_store->islands_num_alloc = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_loop_islands_free(MeshIslandStore *island_store)
|
|
|
|
|
{
|
|
|
|
|
if (island_store->mem) {
|
|
|
|
|
BLI_memarena_free(island_store->mem);
|
2022-10-05 13:44:02 -05:00
|
|
|
island_store->mem = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_loop_islands_add(MeshIslandStore *island_store,
|
|
|
|
|
const int item_num,
|
2020-07-13 11:27:09 +02:00
|
|
|
const int *items_indices,
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
const int num_island_items,
|
|
|
|
|
int *island_item_indices,
|
|
|
|
|
const int num_innercut_items,
|
|
|
|
|
int *innercut_item_indices)
|
|
|
|
|
{
|
|
|
|
|
MemArena *mem = island_store->mem;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
MeshElemMap *isld, *innrcut;
|
|
|
|
|
const int curr_island_idx = island_store->islands_num++;
|
2022-09-25 20:27:46 +10:00
|
|
|
const size_t curr_num_islands = size_t(island_store->islands_num);
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
int i = item_num;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
while (i--) {
|
|
|
|
|
island_store->items_to_islands[items_indices[i]] = curr_island_idx;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (UNLIKELY(curr_num_islands > island_store->islands_num_alloc)) {
|
|
|
|
|
MeshElemMap **islds, **innrcuts;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
island_store->islands_num_alloc *= 2;
|
2022-09-19 18:44:51 -05:00
|
|
|
islds = static_cast<MeshElemMap **>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*islds) * island_store->islands_num_alloc));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
memcpy(islds, island_store->islands, sizeof(*islds) * (curr_num_islands - 1));
|
|
|
|
|
island_store->islands = islds;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-19 18:44:51 -05:00
|
|
|
innrcuts = static_cast<MeshElemMap **>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*innrcuts) * island_store->islands_num_alloc));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
memcpy(innrcuts, island_store->innercuts, sizeof(*innrcuts) * (curr_num_islands - 1));
|
|
|
|
|
island_store->innercuts = innrcuts;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-19 18:44:51 -05:00
|
|
|
island_store->islands[curr_island_idx] = isld = static_cast<MeshElemMap *>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*isld)));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
isld->count = num_island_items;
|
2022-09-19 18:44:51 -05:00
|
|
|
isld->indices = static_cast<int *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
BLI_memarena_alloc(mem, sizeof(*isld->indices) * size_t(num_island_items)));
|
|
|
|
|
memcpy(isld->indices, island_item_indices, sizeof(*isld->indices) * size_t(num_island_items));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-19 18:44:51 -05:00
|
|
|
island_store->innercuts[curr_island_idx] = innrcut = static_cast<MeshElemMap *>(
|
|
|
|
|
BLI_memarena_alloc(mem, sizeof(*innrcut)));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
innrcut->count = num_innercut_items;
|
2022-09-19 18:44:51 -05:00
|
|
|
innrcut->indices = static_cast<int *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
BLI_memarena_alloc(mem, sizeof(*innrcut->indices) * size_t(num_innercut_items)));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
memcpy(innrcut->indices,
|
|
|
|
|
innercut_item_indices,
|
2022-09-25 20:27:46 +10:00
|
|
|
sizeof(*innrcut->indices) * size_t(num_innercut_items));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
static bool mesh_calc_islands_loop_face_uv(const int totedge,
|
2023-03-01 14:13:05 +01:00
|
|
|
const bool *uv_seams,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int *corner_verts,
|
|
|
|
|
const int *corner_edges,
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
const int totloop,
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const float (*luvs)[2],
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
MeshIslandStore *r_island_store)
|
|
|
|
|
{
|
2023-05-24 13:16:57 +02:00
|
|
|
using namespace blender;
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_groups = nullptr;
|
|
|
|
|
int num_face_groups;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
int *face_indices;
|
2015-02-20 17:09:40 +11:00
|
|
|
int *loop_indices;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
int num_pidx, num_lidx;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-27 12:07:07 +10:00
|
|
|
/* Those are used to detect 'inner cuts', i.e. edges that are borders,
|
2023-07-24 22:06:55 +02:00
|
|
|
* and yet have two or more faces of a same group using them
|
2019-04-27 12:07:07 +10:00
|
|
|
* (typical case: seam used to unwrap properly a cylinder). */
|
2022-10-05 13:44:02 -05:00
|
|
|
BLI_bitmap *edge_borders = nullptr;
|
2015-07-24 05:04:33 +10:00
|
|
|
int num_edge_borders = 0;
|
2022-10-05 13:44:02 -05:00
|
|
|
char *edge_border_count = nullptr;
|
|
|
|
|
int *edge_innercut_indices = nullptr;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
int num_einnercuts = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
int grp_idx;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
BKE_mesh_loop_islands_clear(r_island_store);
|
|
|
|
|
BKE_mesh_loop_islands_init(
|
|
|
|
|
r_island_store, MISLAND_TYPE_LOOP, totloop, MISLAND_TYPE_POLY, MISLAND_TYPE_EDGE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
Array<int> edge_to_face_offsets;
|
|
|
|
|
Array<int> edge_to_face_indices;
|
|
|
|
|
const GroupedSpan<int> edge_to_face_map = bke::mesh::build_edge_to_face_map(
|
|
|
|
|
faces, {corner_edges, totloop}, totedge, edge_to_face_offsets, edge_to_face_indices);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-24 13:16:57 +02:00
|
|
|
Array<int> edge_to_loop_offsets;
|
|
|
|
|
Array<int> edge_to_loop_indices;
|
|
|
|
|
GroupedSpan<int> edge_to_loop_map;
|
2016-07-21 16:53:00 +02:00
|
|
|
if (luvs) {
|
2023-05-24 13:16:57 +02:00
|
|
|
edge_to_loop_map = bke::mesh::build_edge_to_loop_map(
|
|
|
|
|
{corner_edges, totloop}, totedge, edge_to_loop_offsets, edge_to_loop_indices);
|
2016-07-21 16:53:00 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-09 21:55:32 -05:00
|
|
|
/* TODO: I'm not sure edge seam flag is enough to define UV islands?
|
|
|
|
|
* Maybe we should also consider UV-maps values
|
|
|
|
|
* themselves (i.e. different UV-edges for a same mesh-edge => boundary edge too?).
|
|
|
|
|
* Would make things much more complex though,
|
|
|
|
|
* and each UVMap would then need its own mesh mapping, not sure we want that at all!
|
|
|
|
|
*/
|
2023-07-24 22:06:55 +02:00
|
|
|
auto mesh_check_island_boundary_uv = [&](const int /*face_index*/,
|
2023-02-09 21:55:32 -05:00
|
|
|
const int loop_index,
|
|
|
|
|
const int edge_index,
|
|
|
|
|
const int /*edge_user_count*/,
|
2023-07-24 22:06:55 +02:00
|
|
|
const Span<int> /*edge_face_map_elem*/) -> bool {
|
2023-02-09 21:55:32 -05:00
|
|
|
if (luvs) {
|
2023-05-24 13:16:57 +02:00
|
|
|
const Span<int> edge_to_loops = edge_to_loop_map[corner_edges[loop_index]];
|
|
|
|
|
|
|
|
|
|
BLI_assert(edge_to_loops.size() >= 2 && (edge_to_loops.size() % 2) == 0);
|
|
|
|
|
|
|
|
|
|
const int v1 = corner_verts[edge_to_loops[0]];
|
|
|
|
|
const int v2 = corner_verts[edge_to_loops[1]];
|
|
|
|
|
const float *uvco_v1 = luvs[edge_to_loops[0]];
|
|
|
|
|
const float *uvco_v2 = luvs[edge_to_loops[1]];
|
|
|
|
|
for (int i = 2; i < edge_to_loops.size(); i += 2) {
|
|
|
|
|
if (corner_verts[edge_to_loops[i]] == v1) {
|
|
|
|
|
if (!equals_v2v2(uvco_v1, luvs[edge_to_loops[i]]) ||
|
|
|
|
|
!equals_v2v2(uvco_v2, luvs[edge_to_loops[i + 1]]))
|
2023-02-09 21:55:32 -05:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-05-24 13:16:57 +02:00
|
|
|
BLI_assert(corner_verts[edge_to_loops[i]] == v2);
|
2023-02-09 21:55:32 -05:00
|
|
|
UNUSED_VARS_NDEBUG(v2);
|
2023-05-24 13:16:57 +02:00
|
|
|
if (!equals_v2v2(uvco_v2, luvs[edge_to_loops[i]]) ||
|
|
|
|
|
!equals_v2v2(uvco_v1, luvs[edge_to_loops[i + 1]]))
|
2023-02-09 21:55:32 -05:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Edge is UV boundary if tagged as seam. */
|
2023-03-01 14:13:05 +01:00
|
|
|
return uv_seams && uv_seams[edge_index];
|
2023-02-09 21:55:32 -05:00
|
|
|
};
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_edge_loop_islands_calc(totedge,
|
|
|
|
|
faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
{corner_edges, totloop},
|
2023-07-24 22:06:55 +02:00
|
|
|
edge_to_face_map,
|
2018-07-13 08:36:10 +02:00
|
|
|
false,
|
|
|
|
|
mesh_check_island_boundary_uv,
|
2023-07-24 22:06:55 +02:00
|
|
|
&face_groups,
|
|
|
|
|
&num_face_groups,
|
2018-07-13 08:36:10 +02:00
|
|
|
&edge_borders,
|
|
|
|
|
&num_edge_borders);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (!num_face_groups) {
|
2015-07-24 05:04:33 +10:00
|
|
|
if (edge_borders) {
|
|
|
|
|
MEM_freeN(edge_borders);
|
|
|
|
|
}
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (num_edge_borders) {
|
2022-09-19 18:44:51 -05:00
|
|
|
edge_border_count = static_cast<char *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
MEM_mallocN(sizeof(*edge_border_count) * size_t(totedge), __func__));
|
2022-09-19 18:44:51 -05:00
|
|
|
edge_innercut_indices = static_cast<int *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
MEM_mallocN(sizeof(*edge_innercut_indices) * size_t(num_edge_borders), __func__));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
face_indices = static_cast<int *>(
|
|
|
|
|
MEM_mallocN(sizeof(*face_indices) * size_t(faces.size()), __func__));
|
2022-09-19 18:44:51 -05:00
|
|
|
loop_indices = static_cast<int *>(
|
2022-09-25 20:27:46 +10:00
|
|
|
MEM_mallocN(sizeof(*loop_indices) * size_t(totloop), __func__));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: here we ignore '0' invalid group - this should *never* happen in this case anyway? */
|
2023-07-24 22:06:55 +02:00
|
|
|
for (grp_idx = 1; grp_idx <= num_face_groups; grp_idx++) {
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
num_pidx = num_lidx = 0;
|
|
|
|
|
if (num_edge_borders) {
|
|
|
|
|
num_einnercuts = 0;
|
2022-09-25 20:27:46 +10:00
|
|
|
memset(edge_border_count, 0, sizeof(*edge_border_count) * size_t(totedge));
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int64_t p_idx : faces.index_range()) {
|
|
|
|
|
if (face_groups[p_idx] != grp_idx) {
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
face_indices[num_pidx++] = int(p_idx);
|
|
|
|
|
for (const int64_t corner : faces[p_idx]) {
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
const int edge_i = corner_edges[corner];
|
|
|
|
|
loop_indices[num_lidx++] = int(corner);
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
if (num_edge_borders && BLI_BITMAP_TEST(edge_borders, edge_i) &&
|
|
|
|
|
(edge_border_count[edge_i] < 2)) {
|
|
|
|
|
edge_border_count[edge_i]++;
|
|
|
|
|
if (edge_border_count[edge_i] == 2) {
|
|
|
|
|
edge_innercut_indices[num_einnercuts++] = edge_i;
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-07-13 08:36:10 +02:00
|
|
|
BKE_mesh_loop_islands_add(r_island_store,
|
|
|
|
|
num_lidx,
|
|
|
|
|
loop_indices,
|
|
|
|
|
num_pidx,
|
2023-07-24 22:06:55 +02:00
|
|
|
face_indices,
|
2018-07-13 08:36:10 +02:00
|
|
|
num_einnercuts,
|
|
|
|
|
edge_innercut_indices);
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_freeN(face_indices);
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
MEM_freeN(loop_indices);
|
2023-07-24 22:06:55 +02:00
|
|
|
MEM_freeN(face_groups);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-24 05:04:33 +10:00
|
|
|
if (edge_borders) {
|
|
|
|
|
MEM_freeN(edge_borders);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
BKE: Add 'mesh remap' code.
This is the (big!) core of mesh transfer data, it defines a set of structures
to represent a mapping of mesh elements (verts, edges, polys of loops) between
two arbitrary meshes, and code to compute such mappings.
No similarity is required between source and destination meshes (though results
when using complete different meshes are rather unlikely to be useful!).
This code is not bound to data transfer, it is defined to be as generic as possible,
and easy to reuse or extend as needs arise.
Several methods of mapping generation are defined for each element type,
we probably will have to adjust that in future (remove useless ones, add
new ones...).
For loops, you can also define islands (for UVs e.g.) so that loops of a same
destination polygon do not 'spread' across several source islands.
Heavily reviewed and enhanced by Campbell, thanks a lot!
2015-01-09 18:23:17 +01:00
|
|
|
if (num_edge_borders) {
|
|
|
|
|
MEM_freeN(edge_border_count);
|
|
|
|
|
MEM_freeN(edge_innercut_indices);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
bool BKE_mesh_calc_islands_loop_face_edgeseam(const float (*vert_positions)[3],
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totvert,
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
const blender::int2 *edges,
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totedge,
|
2023-03-01 14:13:05 +01:00
|
|
|
const bool *uv_seams,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int *corner_verts,
|
|
|
|
|
const int *corner_edges,
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totloop,
|
|
|
|
|
MeshIslandStore *r_island_store)
|
|
|
|
|
{
|
2023-03-01 14:13:05 +01:00
|
|
|
UNUSED_VARS(vert_positions, totvert, edges);
|
2023-07-24 22:06:55 +02:00
|
|
|
return mesh_calc_islands_loop_face_uv(
|
|
|
|
|
totedge, uv_seams, faces, corner_verts, corner_edges, totloop, nullptr, r_island_store);
|
2016-07-21 16:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
bool BKE_mesh_calc_islands_loop_face_uvmap(float (*vert_positions)[3],
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totvert,
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
blender::int2 *edges,
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totedge,
|
2023-03-01 14:13:05 +01:00
|
|
|
const bool *uv_seams,
|
2023-07-24 22:06:55 +02:00
|
|
|
const blender::OffsetIndices<int> faces,
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const int *corner_verts,
|
|
|
|
|
const int *corner_edges,
|
2016-07-21 16:53:00 +02:00
|
|
|
const int totloop,
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const float (*luvs)[2],
|
2016-07-21 16:53:00 +02:00
|
|
|
MeshIslandStore *r_island_store)
|
|
|
|
|
{
|
2023-03-01 14:13:05 +01:00
|
|
|
UNUSED_VARS(vert_positions, totvert, edges);
|
2022-10-05 13:44:02 -05:00
|
|
|
BLI_assert(luvs != nullptr);
|
2023-07-24 22:06:55 +02:00
|
|
|
return mesh_calc_islands_loop_face_uv(
|
|
|
|
|
totedge, uv_seams, faces, corner_verts, corner_edges, totloop, luvs, r_island_store);
|
2016-07-21 16:53:00 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-12 16:26:11 +11:00
|
|
|
/** \} */
|