2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2001-2002 NaN Holding BV. All rights reserved. */
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2013-09-09 02:11:44 +00:00
|
|
|
*
|
|
|
|
|
* Functions to evaluate mesh data.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-07 13:57:09 +10:00
|
|
|
#include <climits>
|
2013-09-10 12:48:08 +00:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_object_types.h"
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_alloca.h"
|
2013-09-09 02:11:44 +00:00
|
|
|
#include "BLI_bitmap.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_edgehash.h"
|
2022-05-15 20:59:08 +02:00
|
|
|
#include "BLI_index_range.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_math.h"
|
2022-05-15 20:59:08 +02:00
|
|
|
#include "BLI_span.hh"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
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
|
|
|
#include "BLI_virtual_array.hh"
|
2013-09-09 02:11:44 +00:00
|
|
|
|
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
|
|
|
#include "BKE_attribute.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_customdata.h"
|
|
|
|
|
#include "BKE_mesh.hh"
|
2013-09-09 02:11:44 +00:00
|
|
|
#include "BKE_multires.h"
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
using blender::float3;
|
2022-05-15 20:59:08 +02:00
|
|
|
using blender::MutableSpan;
|
|
|
|
|
using blender::Span;
|
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
|
|
|
using blender::VArray;
|
2022-05-15 20:59:08 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Polygon Calculations
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
namespace blender::bke::mesh {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
static float3 poly_center_calc_ngon(const Span<float3> vert_positions,
|
|
|
|
|
const Span<MLoop> poly_loops)
|
|
|
|
|
{
|
|
|
|
|
const float w = 1.0f / float(poly_loops.size());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
float3 center(0);
|
|
|
|
|
for (const int i : poly_loops.index_range()) {
|
|
|
|
|
center += vert_positions[poly_loops[i].v] * w;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2023-03-12 22:29:15 +01:00
|
|
|
return center;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
float3 poly_center_calc(const Span<float3> vert_positions, const Span<MLoop> poly_loops)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
if (poly_loops.size() == 3) {
|
|
|
|
|
float3 center;
|
|
|
|
|
mid_v3_v3v3v3(center,
|
|
|
|
|
vert_positions[poly_loops[0].v],
|
|
|
|
|
vert_positions[poly_loops[1].v],
|
|
|
|
|
vert_positions[poly_loops[2].v]);
|
|
|
|
|
return center;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2023-03-12 22:29:15 +01:00
|
|
|
if (poly_loops.size() == 4) {
|
|
|
|
|
float3 center;
|
|
|
|
|
mid_v3_v3v3v3v3(center,
|
|
|
|
|
vert_positions[poly_loops[0].v],
|
|
|
|
|
vert_positions[poly_loops[1].v],
|
|
|
|
|
vert_positions[poly_loops[2].v],
|
|
|
|
|
vert_positions[poly_loops[3].v]);
|
|
|
|
|
return center;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2023-03-12 22:29:15 +01:00
|
|
|
return poly_center_calc_ngon(vert_positions, poly_loops);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
} // namespace blender::bke::mesh
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_calc_poly_center(const MLoop *poly_loops,
|
|
|
|
|
const int poly_size,
|
|
|
|
|
const float (*vert_positions)[3],
|
|
|
|
|
const int verts_num,
|
|
|
|
|
float r_cent[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2023-03-12 22:29:15 +01:00
|
|
|
copy_v3_v3(r_cent,
|
|
|
|
|
blender::bke::mesh::poly_center_calc(
|
|
|
|
|
{reinterpret_cast<const blender::float3 *>(vert_positions), verts_num},
|
|
|
|
|
{poly_loops, poly_size}));
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
namespace blender::bke::mesh {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
float poly_area_calc(const Span<float3> vert_positions, const Span<MLoop> poly_loops)
|
|
|
|
|
{
|
|
|
|
|
if (poly_loops.size() == 3) {
|
|
|
|
|
return area_tri_v3(vert_positions[poly_loops[0].v],
|
|
|
|
|
vert_positions[poly_loops[1].v],
|
|
|
|
|
vert_positions[poly_loops[2].v]);
|
|
|
|
|
}
|
|
|
|
|
Array<float3, 32> poly_coords(poly_loops.size());
|
|
|
|
|
for (const int i : poly_loops.index_range()) {
|
|
|
|
|
poly_coords[i] = vert_positions[poly_loops[i].v];
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2023-03-12 22:29:15 +01:00
|
|
|
return area_poly_v3((const float(*)[3])poly_coords.data(), poly_loops.size());
|
|
|
|
|
}
|
2020-08-07 12:30:43 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
} // namespace blender::bke::mesh
|
2020-08-07 12:30:43 +02:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
float BKE_mesh_calc_poly_area(const MLoop *poly_loops,
|
|
|
|
|
const int poly_size,
|
|
|
|
|
const float (*vert_positions)[3],
|
|
|
|
|
const int verts_num)
|
|
|
|
|
{
|
|
|
|
|
return blender::bke::mesh::poly_area_calc(
|
|
|
|
|
{reinterpret_cast<const float3 *>(vert_positions), verts_num}, {poly_loops, poly_size});
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:41:12 +02:00
|
|
|
float BKE_mesh_calc_area(const Mesh *me)
|
|
|
|
|
{
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = me->loops();
|
2019-09-09 19:41:12 +02:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
float total_area = 0.0f;
|
|
|
|
|
for (const MPoly &poly : polys) {
|
2023-03-12 22:29:15 +01:00
|
|
|
total_area += blender::bke::mesh::poly_area_calc(positions,
|
|
|
|
|
loops.slice(poly.loopstart, poly.totloop));
|
2019-09-09 19:41:12 +02:00
|
|
|
}
|
|
|
|
|
return total_area;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
static float UNUSED_FUNCTION(mesh_calc_poly_volume_centroid)(const MLoop *poly_loops,
|
|
|
|
|
const int poly_size,
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const float (*positions)[3],
|
2020-06-18 22:27:46 +10:00
|
|
|
float r_cent[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2017-05-12 11:04:03 +10:00
|
|
|
const float *v_pivot, *v_step1;
|
|
|
|
|
float total_volume = 0.0f;
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
zero_v3(r_cent);
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
v_pivot = positions[poly_loops[0].v];
|
|
|
|
|
v_step1 = positions[poly_loops[1].v];
|
2017-05-12 11:04:03 +10:00
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
for (int i = 2; i < poly_size; i++) {
|
|
|
|
|
const float *v_step2 = positions[poly_loops[i].v];
|
2017-05-12 11:04:03 +10:00
|
|
|
|
|
|
|
|
/* Calculate the 6x volume of the tetrahedron formed by the 3 vertices
|
|
|
|
|
* of the triangle and the origin as the fourth vertex */
|
2019-11-27 14:56:16 +01:00
|
|
|
const float tetra_volume = volume_tri_tetrahedron_signed_v3_6x(v_pivot, v_step1, v_step2);
|
2017-05-12 11:04:03 +10:00
|
|
|
total_volume += tetra_volume;
|
|
|
|
|
|
|
|
|
|
/* Calculate the centroid of the tetrahedron formed by the 3 vertices
|
|
|
|
|
* of the triangle and the origin as the fourth vertex.
|
|
|
|
|
* The centroid is simply the average of the 4 vertices.
|
|
|
|
|
*
|
2019-04-27 12:07:07 +10:00
|
|
|
* Note that the vector is 4x the actual centroid
|
|
|
|
|
* so the division can be done once at the end. */
|
2017-05-12 11:04:03 +10:00
|
|
|
for (uint j = 0; j < 3; j++) {
|
|
|
|
|
r_cent[j] += tetra_volume * (v_pivot[j] + v_step1[j] + v_step2[j]);
|
|
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2017-05-12 11:04:03 +10:00
|
|
|
v_step1 = v_step2;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 11:04:03 +10:00
|
|
|
return total_volume;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
namespace blender::bke::mesh {
|
|
|
|
|
|
2020-06-18 22:27:46 +10:00
|
|
|
/**
|
|
|
|
|
* A version of mesh_calc_poly_volume_centroid that takes an initial reference center,
|
|
|
|
|
* use this to increase numeric stability as the quality of the result becomes
|
2023-02-12 14:37:16 +11:00
|
|
|
* very low quality as the value moves away from 0.0, see: #65986.
|
2020-06-18 22:27:46 +10:00
|
|
|
*/
|
2023-03-12 22:29:15 +01:00
|
|
|
static float mesh_calc_poly_volume_centroid_with_reference_center(const Span<float3> positions,
|
|
|
|
|
const Span<MLoop> poly_loops,
|
|
|
|
|
const float3 &reference_center,
|
2020-06-18 22:27:46 +10:00
|
|
|
float r_cent[3])
|
|
|
|
|
{
|
|
|
|
|
/* See: mesh_calc_poly_volume_centroid for comments. */
|
|
|
|
|
float v_pivot[3], v_step1[3];
|
|
|
|
|
float total_volume = 0.0f;
|
|
|
|
|
zero_v3(r_cent);
|
2023-03-12 22:29:15 +01:00
|
|
|
sub_v3_v3v3(v_pivot, positions[poly_loops[0].v], reference_center);
|
|
|
|
|
sub_v3_v3v3(v_step1, positions[poly_loops[1].v], reference_center);
|
|
|
|
|
for (int i = 2; i < poly_loops.size(); i++) {
|
2020-06-18 22:27:46 +10:00
|
|
|
float v_step2[3];
|
2023-03-12 22:29:15 +01:00
|
|
|
sub_v3_v3v3(v_step2, positions[poly_loops[i].v], reference_center);
|
2020-06-18 22:27:46 +10:00
|
|
|
const float tetra_volume = volume_tri_tetrahedron_signed_v3_6x(v_pivot, v_step1, v_step2);
|
|
|
|
|
total_volume += tetra_volume;
|
|
|
|
|
for (uint j = 0; j < 3; j++) {
|
|
|
|
|
r_cent[j] += tetra_volume * (v_pivot[j] + v_step1[j] + v_step2[j]);
|
|
|
|
|
}
|
|
|
|
|
copy_v3_v3(v_step1, v_step2);
|
|
|
|
|
}
|
|
|
|
|
return total_volume;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 15:06:07 +10:00
|
|
|
/**
|
|
|
|
|
* \note
|
|
|
|
|
* - Results won't be correct if polygon is non-planar.
|
|
|
|
|
* - This has the advantage over #mesh_calc_poly_volume_centroid
|
|
|
|
|
* that it doesn't depend on solid geometry, instead it weights the surface by volume.
|
|
|
|
|
*/
|
2023-03-12 22:29:15 +01:00
|
|
|
static float poly_area_centroid_calc(const Span<float3> positions,
|
|
|
|
|
const Span<MLoop> poly_loops,
|
|
|
|
|
float r_cent[3])
|
2017-08-21 15:06:07 +10:00
|
|
|
{
|
|
|
|
|
float total_area = 0.0f;
|
2023-03-12 22:29:15 +01:00
|
|
|
float v1[3], v2[3], v3[3], tri_cent[3];
|
|
|
|
|
|
|
|
|
|
const float3 normal = blender::bke::mesh::poly_normal_calc(positions, poly_loops);
|
|
|
|
|
|
|
|
|
|
copy_v3_v3(v1, positions[poly_loops[0].v]);
|
|
|
|
|
copy_v3_v3(v2, positions[poly_loops[1].v]);
|
2017-08-21 15:06:07 +10:00
|
|
|
|
|
|
|
|
zero_v3(r_cent);
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
for (int i = 2; i < poly_loops.size(); i++) {
|
|
|
|
|
copy_v3_v3(v3, positions[poly_loops[i].v]);
|
2017-08-21 15:06:07 +10:00
|
|
|
|
2020-09-09 16:35:20 +02:00
|
|
|
float tri_area = area_tri_signed_v3(v1, v2, v3, normal);
|
2017-08-21 15:06:07 +10:00
|
|
|
total_area += tri_area;
|
|
|
|
|
|
|
|
|
|
mid_v3_v3v3v3(tri_cent, v1, v2, v3);
|
|
|
|
|
madd_v3_v3fl(r_cent, tri_cent, tri_area);
|
|
|
|
|
|
|
|
|
|
copy_v3_v3(v2, v3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mul_v3_fl(r_cent, 1.0f / total_area);
|
|
|
|
|
|
|
|
|
|
return total_area;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
void poly_angles_calc(const Span<float3> vert_positions,
|
|
|
|
|
const Span<MLoop> poly_loops,
|
|
|
|
|
MutableSpan<float> angles)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
|
|
|
|
float nor_prev[3];
|
|
|
|
|
float nor_next[3];
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
int i_this = poly_loops.size() - 1;
|
2013-09-09 02:11:44 +00:00
|
|
|
int i_next = 0;
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
sub_v3_v3v3(
|
2023-03-12 22:29:15 +01:00
|
|
|
nor_prev, vert_positions[poly_loops[i_this - 1].v], vert_positions[poly_loops[i_this].v]);
|
2013-09-09 02:11:44 +00:00
|
|
|
normalize_v3(nor_prev);
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
while (i_next < poly_loops.size()) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
sub_v3_v3v3(
|
2023-03-12 22:29:15 +01:00
|
|
|
nor_next, vert_positions[poly_loops[i_this].v], vert_positions[poly_loops[i_next].v]);
|
2013-09-09 02:11:44 +00:00
|
|
|
normalize_v3(nor_next);
|
|
|
|
|
angles[i_this] = angle_normalized_v3v3(nor_prev, nor_next);
|
|
|
|
|
|
|
|
|
|
/* step */
|
|
|
|
|
copy_v3_v3(nor_prev, nor_next);
|
|
|
|
|
i_this = i_next;
|
|
|
|
|
i_next++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-12 22:29:15 +01:00
|
|
|
} // namespace blender::bke::mesh
|
|
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Center Calculation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
bool BKE_mesh_center_median(const Mesh *me, float r_cent[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2015-07-11 04:39:27 +10:00
|
|
|
zero_v3(r_cent);
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
for (const int i : positions.index_range()) {
|
|
|
|
|
add_v3_v3(r_cent, positions[i]);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
/* otherwise we get NAN for 0 verts */
|
|
|
|
|
if (me->totvert) {
|
2022-09-25 18:33:28 +10:00
|
|
|
mul_v3_fl(r_cent, 1.0f / float(me->totvert));
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
return (me->totvert != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 22:27:46 +10:00
|
|
|
bool BKE_mesh_center_median_from_polys(const Mesh *me, float r_cent[3])
|
|
|
|
|
{
|
|
|
|
|
int tot = 0;
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = me->loops();
|
2020-06-18 22:27:46 +10:00
|
|
|
zero_v3(r_cent);
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const MPoly &poly : polys) {
|
|
|
|
|
int loopend = poly.loopstart + poly.totloop;
|
|
|
|
|
for (int j = poly.loopstart; j < loopend; j++) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
add_v3_v3(r_cent, positions[loops[j].v]);
|
2020-06-18 22:27:46 +10:00
|
|
|
}
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
tot += poly.totloop;
|
2020-06-18 22:27:46 +10:00
|
|
|
}
|
|
|
|
|
/* otherwise we get NAN for 0 verts */
|
|
|
|
|
if (me->totpoly) {
|
2022-09-25 18:33:28 +10:00
|
|
|
mul_v3_fl(r_cent, 1.0f / float(tot));
|
2020-06-18 22:27:46 +10:00
|
|
|
}
|
|
|
|
|
return (me->totpoly != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
bool BKE_mesh_center_bounds(const Mesh *me, float r_cent[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
|
|
|
|
float min[3], max[3];
|
|
|
|
|
INIT_MINMAX(min, max);
|
|
|
|
|
if (BKE_mesh_minmax(me, min, max)) {
|
2015-07-11 04:39:27 +10:00
|
|
|
mid_v3_v3v3(r_cent, min, max);
|
2013-09-09 02:11:44 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-21 15:06:07 +10:00
|
|
|
bool BKE_mesh_center_of_surface(const Mesh *me, float r_cent[3])
|
|
|
|
|
{
|
|
|
|
|
float poly_area;
|
|
|
|
|
float total_area = 0.0f;
|
|
|
|
|
float poly_cent[3];
|
2023-03-12 22:29:15 +01:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2023-02-23 10:39:51 -05:00
|
|
|
const blender::Span<MPoly> polys = me->polys();
|
|
|
|
|
const blender::Span<MLoop> loops = me->loops();
|
2017-08-21 15:06:07 +10:00
|
|
|
|
|
|
|
|
zero_v3(r_cent);
|
|
|
|
|
|
|
|
|
|
/* calculate a weighted average of polygon centroids */
|
2023-02-23 10:39:51 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
2023-03-12 22:29:15 +01:00
|
|
|
poly_area = blender::bke::mesh::poly_area_centroid_calc(
|
|
|
|
|
positions, loops.slice(polys[i].loopstart, polys[i].totloop), poly_cent);
|
2017-08-21 15:06:07 +10:00
|
|
|
|
|
|
|
|
madd_v3_v3fl(r_cent, poly_cent, poly_area);
|
|
|
|
|
total_area += poly_area;
|
|
|
|
|
}
|
|
|
|
|
/* otherwise we get NAN for 0 polys */
|
|
|
|
|
if (me->totpoly) {
|
|
|
|
|
mul_v3_fl(r_cent, 1.0f / total_area);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* zero area faces cause this, fallback to median */
|
|
|
|
|
if (UNLIKELY(!is_finite_v3(r_cent))) {
|
|
|
|
|
return BKE_mesh_center_median(me, r_cent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (me->totpoly != 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BKE_mesh_center_of_volume(const Mesh *me, float r_cent[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2017-05-12 11:04:03 +10:00
|
|
|
float poly_volume;
|
|
|
|
|
float total_volume = 0.0f;
|
2013-09-09 02:11:44 +00:00
|
|
|
float poly_cent[3];
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2023-02-23 10:39:51 -05:00
|
|
|
const blender::Span<MPoly> polys = me->polys();
|
|
|
|
|
const blender::Span<MLoop> loops = me->loops();
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2020-06-18 22:27:46 +10:00
|
|
|
/* Use an initial center to avoid numeric instability of geometry far away from the center. */
|
|
|
|
|
float init_cent[3];
|
|
|
|
|
const bool init_cent_result = BKE_mesh_center_median_from_polys(me, init_cent);
|
|
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
zero_v3(r_cent);
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2017-05-12 11:04:03 +10:00
|
|
|
/* calculate a weighted average of polyhedron centroids */
|
2023-02-23 10:39:51 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
2023-03-12 22:29:15 +01:00
|
|
|
poly_volume = blender::bke::mesh::mesh_calc_poly_volume_centroid_with_reference_center(
|
|
|
|
|
positions, loops.slice(polys[i].loopstart, polys[i].totloop), init_cent, poly_cent);
|
2013-09-09 02:11:44 +00:00
|
|
|
|
2017-05-12 11:04:03 +10:00
|
|
|
/* poly_cent is already volume-weighted, so no need to multiply by the volume */
|
|
|
|
|
add_v3_v3(r_cent, poly_cent);
|
|
|
|
|
total_volume += poly_volume;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
/* otherwise we get NAN for 0 polys */
|
2017-05-12 11:04:03 +10:00
|
|
|
if (total_volume != 0.0f) {
|
2018-09-24 17:27:41 +02:00
|
|
|
/* multiply by 0.25 to get the correct centroid */
|
2019-04-27 12:07:07 +10:00
|
|
|
/* no need to divide volume by 6 as the centroid is weighted by 6x the volume,
|
|
|
|
|
* so it all cancels out. */
|
2017-05-12 11:04:03 +10:00
|
|
|
mul_v3_fl(r_cent, 0.25f / total_volume);
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 11:04:03 +10:00
|
|
|
/* this can happen for non-manifold objects, fallback to median */
|
2015-07-11 04:39:27 +10:00
|
|
|
if (UNLIKELY(!is_finite_v3(r_cent))) {
|
2020-06-18 22:27:46 +10:00
|
|
|
copy_v3_v3(r_cent, init_cent);
|
|
|
|
|
return init_cent_result;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2020-06-18 22:27:46 +10:00
|
|
|
add_v3_v3(r_cent, init_cent);
|
2013-09-09 02:11:44 +00:00
|
|
|
return (me->totpoly != 0);
|
|
|
|
|
}
|
2017-08-21 15:06:07 +10:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|
|
|
|
|
|
2014-07-11 12:06:13 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Volume Calculation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
static bool mesh_calc_center_centroid_ex(const float (*positions)[3],
|
2022-10-03 17:37:25 -05:00
|
|
|
int /*mverts_num*/,
|
2015-07-20 05:19:47 +10:00
|
|
|
const MLoopTri *looptri,
|
|
|
|
|
int looptri_num,
|
2015-07-19 19:10:41 +02:00
|
|
|
const MLoop *mloop,
|
|
|
|
|
float r_center[3])
|
2014-07-11 12:06:13 +02:00
|
|
|
{
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
zero_v3(r_center);
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (looptri_num == 0) {
|
2014-07-11 12:06:13 +02:00
|
|
|
return false;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2020-09-09 16:35:20 +02:00
|
|
|
float totweight = 0.0f;
|
|
|
|
|
const MLoopTri *lt;
|
|
|
|
|
int i;
|
2015-07-20 05:19:47 +10:00
|
|
|
for (i = 0, lt = looptri; i < looptri_num; i++, lt++) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const float *v1 = positions[mloop[lt->tri[0]].v];
|
|
|
|
|
const float *v2 = positions[mloop[lt->tri[1]].v];
|
|
|
|
|
const float *v3 = positions[mloop[lt->tri[2]].v];
|
2014-07-11 12:06:13 +02:00
|
|
|
float area;
|
2018-06-17 17:05:51 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
area = area_tri_v3(v1, v2, v3);
|
|
|
|
|
madd_v3_v3fl(r_center, v1, area);
|
|
|
|
|
madd_v3_v3fl(r_center, v2, area);
|
|
|
|
|
madd_v3_v3fl(r_center, v3, area);
|
2014-07-11 12:06:13 +02:00
|
|
|
totweight += area;
|
|
|
|
|
}
|
2019-04-22 09:39:35 +10:00
|
|
|
if (totweight == 0.0f) {
|
2014-07-11 12:06:13 +02:00
|
|
|
return false;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2015-07-11 04:39:27 +10:00
|
|
|
mul_v3_fl(r_center, 1.0f / (3.0f * totweight));
|
2018-06-17 17:05:51 +02:00
|
|
|
|
2014-07-11 12:06:13 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
void BKE_mesh_calc_volume(const float (*vert_positions)[3],
|
2015-07-20 05:19:47 +10:00
|
|
|
const int mverts_num,
|
|
|
|
|
const MLoopTri *looptri,
|
|
|
|
|
const int looptri_num,
|
|
|
|
|
const MLoop *mloop,
|
|
|
|
|
float *r_volume,
|
|
|
|
|
float r_center[3])
|
2014-07-11 12:06:13 +02:00
|
|
|
{
|
2015-07-20 05:19:47 +10:00
|
|
|
const MLoopTri *lt;
|
2014-07-11 12:06:13 +02:00
|
|
|
float center[3];
|
|
|
|
|
float totvol;
|
2015-07-20 05:19:47 +10:00
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
if (r_volume) {
|
2015-07-20 05:19:47 +10:00
|
|
|
*r_volume = 0.0f;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
if (r_center) {
|
2015-07-20 05:19:47 +10:00
|
|
|
zero_v3(r_center);
|
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 (looptri_num == 0) {
|
2014-07-11 12:06:13 +02:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
if (!mesh_calc_center_centroid_ex(
|
|
|
|
|
vert_positions, mverts_num, looptri, looptri_num, mloop, center)) {
|
2014-07-11 12:06:13 +02:00
|
|
|
return;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-07-11 12:06:13 +02:00
|
|
|
totvol = 0.0f;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-20 05:19:47 +10:00
|
|
|
for (i = 0, lt = looptri; i < looptri_num; i++, lt++) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const float *v1 = vert_positions[mloop[lt->tri[0]].v];
|
|
|
|
|
const float *v2 = vert_positions[mloop[lt->tri[1]].v];
|
|
|
|
|
const float *v3 = vert_positions[mloop[lt->tri[2]].v];
|
2014-07-11 12:06:13 +02:00
|
|
|
float vol;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
vol = volume_tetrahedron_signed_v3(center, v1, v2, v3);
|
2015-07-20 05:19:47 +10:00
|
|
|
if (r_volume) {
|
2014-07-11 12:06:13 +02:00
|
|
|
totvol += vol;
|
|
|
|
|
}
|
2015-07-20 05:19:47 +10:00
|
|
|
if (r_center) {
|
2015-08-05 20:26:52 +10:00
|
|
|
/* averaging factor 1/3 is applied in the end */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
madd_v3_v3fl(r_center, v1, vol);
|
|
|
|
|
madd_v3_v3fl(r_center, v2, vol);
|
|
|
|
|
madd_v3_v3fl(r_center, v3, vol);
|
2014-07-11 12:06:13 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: Depending on arbitrary centroid position,
|
2014-07-11 12:06:13 +02:00
|
|
|
* totvol can become negative even for a valid mesh.
|
|
|
|
|
* The true value is always the positive value.
|
|
|
|
|
*/
|
2015-07-20 05:19:47 +10:00
|
|
|
if (r_volume) {
|
|
|
|
|
*r_volume = fabsf(totvol);
|
2014-07-11 12:06:13 +02:00
|
|
|
}
|
2015-07-20 05:19:47 +10:00
|
|
|
if (r_center) {
|
2021-07-03 23:08:40 +10:00
|
|
|
/* NOTE: Factor 1/3 is applied once for all vertices here.
|
2014-07-11 12:06:13 +02:00
|
|
|
* This also automatically negates the vector if totvol is negative.
|
|
|
|
|
*/
|
2019-04-22 09:39:35 +10:00
|
|
|
if (totvol != 0.0f) {
|
2015-08-05 20:26:52 +10:00
|
|
|
mul_v3_fl(r_center, (1.0f / 3.0f) / totvol);
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2014-07-11 12:06:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 01:58:46 +11:00
|
|
|
/** \} */
|
2014-07-11 12:06:13 +02:00
|
|
|
|
2016-03-14 15:17:29 +11:00
|
|
|
void BKE_mesh_mdisp_flip(MDisps *md, const bool use_loop_mdisp_flip)
|
|
|
|
|
{
|
|
|
|
|
if (UNLIKELY(!md->totdisp || !md->disps)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
const int sides = int(sqrt(md->totdisp));
|
2016-03-14 15:17:29 +11:00
|
|
|
float(*co)[3] = md->disps;
|
|
|
|
|
|
|
|
|
|
for (int x = 0; x < sides; x++) {
|
|
|
|
|
float *co_a, *co_b;
|
|
|
|
|
|
|
|
|
|
for (int y = 0; y < x; y++) {
|
|
|
|
|
co_a = co[y * sides + x];
|
|
|
|
|
co_b = co[x * sides + y];
|
|
|
|
|
|
|
|
|
|
swap_v3_v3(co_a, co_b);
|
2023-01-09 11:12:03 -05:00
|
|
|
std::swap(co_a[0], co_a[1]);
|
|
|
|
|
std::swap(co_b[0], co_b[1]);
|
2016-03-14 15:17:29 +11:00
|
|
|
|
|
|
|
|
if (use_loop_mdisp_flip) {
|
|
|
|
|
co_a[2] *= -1.0f;
|
|
|
|
|
co_b[2] *= -1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
co_a = co[x * sides + x];
|
|
|
|
|
|
2023-01-09 11:12:03 -05:00
|
|
|
std::swap(co_a[0], co_a[1]);
|
2016-03-14 15:17:29 +11:00
|
|
|
|
|
|
|
|
if (use_loop_mdisp_flip) {
|
|
|
|
|
co_a[2] *= -1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
void BKE_mesh_polygon_flip_ex(const MPoly *poly,
|
2016-03-14 15:32:48 +11:00
|
|
|
MLoop *mloop,
|
|
|
|
|
CustomData *ldata,
|
2016-06-07 13:04:05 +02:00
|
|
|
float (*lnors)[3],
|
|
|
|
|
MDisps *mdisp,
|
|
|
|
|
const bool use_loop_mdisp_flip)
|
2016-02-28 15:29:56 +01:00
|
|
|
{
|
2023-03-01 15:57:50 -05:00
|
|
|
int loopstart = poly->loopstart;
|
|
|
|
|
int loopend = loopstart + poly->totloop - 1;
|
2016-02-28 15:29:56 +01:00
|
|
|
const bool loops_in_ldata = (CustomData_get_layer(ldata, CD_MLOOP) == mloop);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-03-14 15:32:48 +11:00
|
|
|
if (mdisp) {
|
2016-06-07 13:04:05 +02:00
|
|
|
for (int i = loopstart; i <= loopend; i++) {
|
2016-03-14 15:32:48 +11:00
|
|
|
BKE_mesh_mdisp_flip(&mdisp[i], use_loop_mdisp_flip);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-28 15:29:56 +01:00
|
|
|
/* Note that we keep same start vertex for flipped face. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-03-14 13:21:13 +11:00
|
|
|
/* We also have to update loops edge
|
2019-04-27 12:07:07 +10:00
|
|
|
* (they will get their original 'other edge', that is,
|
|
|
|
|
* the original edge of their original previous loop)... */
|
2021-06-18 14:27:41 +10:00
|
|
|
uint prev_edge_index = mloop[loopstart].e;
|
2016-02-28 15:29:56 +01:00
|
|
|
mloop[loopstart].e = mloop[loopend].e;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:29:56 +01:00
|
|
|
for (loopstart++; loopend > loopstart; loopstart++, loopend--) {
|
|
|
|
|
mloop[loopend].e = mloop[loopend - 1].e;
|
2023-01-09 11:12:03 -05:00
|
|
|
std::swap(mloop[loopstart].e, prev_edge_index);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2016-02-28 15:29:56 +01:00
|
|
|
if (!loops_in_ldata) {
|
2023-01-09 11:12:03 -05:00
|
|
|
std::swap(mloop[loopstart], mloop[loopend]);
|
2016-02-28 15:29:56 +01:00
|
|
|
}
|
2016-06-07 13:04:05 +02:00
|
|
|
if (lnors) {
|
|
|
|
|
swap_v3_v3(lnors[loopstart], lnors[loopend]);
|
|
|
|
|
}
|
2016-02-28 15:29:56 +01:00
|
|
|
CustomData_swap(ldata, loopstart, loopend);
|
|
|
|
|
}
|
|
|
|
|
/* Even if we did not swap the other 'pivot' loop, we need to set its swapped edge. */
|
|
|
|
|
if (loopstart == loopend) {
|
|
|
|
|
mloop[loopstart].e = prev_edge_index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
void BKE_mesh_polygon_flip(const MPoly *poly, MLoop *mloop, CustomData *ldata, const int totloop)
|
2016-03-14 15:32:48 +11:00
|
|
|
{
|
2023-01-13 17:21:20 -06:00
|
|
|
MDisps *mdisp = (MDisps *)CustomData_get_layer_for_write(ldata, CD_MDISPS, totloop);
|
2023-03-01 15:57:50 -05:00
|
|
|
BKE_mesh_polygon_flip_ex(poly, mloop, ldata, nullptr, mdisp, true);
|
2016-03-14 15:32:48 +11:00
|
|
|
}
|
|
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
void BKE_mesh_polys_flip(const MPoly *polys, MLoop *mloop, CustomData *ldata, int totpoly)
|
2016-02-28 15:29:56 +01:00
|
|
|
{
|
2023-01-13 17:21:20 -06:00
|
|
|
MDisps *mdisp = (MDisps *)CustomData_get_layer_for_write(ldata, CD_MDISPS, totpoly);
|
2023-03-01 15:57:50 -05:00
|
|
|
for (const int i : blender::IndexRange(totpoly)) {
|
|
|
|
|
BKE_mesh_polygon_flip_ex(&polys[i], mloop, ldata, nullptr, mdisp, true);
|
2016-02-28 15:29:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Flag Flushing
|
|
|
|
|
* \{ */
|
|
|
|
|
|
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
|
|
|
void BKE_mesh_flush_hidden_from_verts(Mesh *me)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
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
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-07 21:41:39 -05:00
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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 VArray<bool> hide_vert = attributes.lookup_or_default<bool>(
|
|
|
|
|
".hide_vert", ATTR_DOMAIN_POINT, false);
|
|
|
|
|
if (hide_vert.is_single() && !hide_vert.get_internal_single()) {
|
|
|
|
|
attributes.remove(".hide_edge");
|
|
|
|
|
attributes.remove(".hide_poly");
|
|
|
|
|
return;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
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 VArraySpan<bool> hide_vert_span{hide_vert};
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MEdge> edges = me->edges();
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = me->loops();
|
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
|
|
|
|
|
|
|
|
/* Hide edges when either of their vertices are hidden. */
|
|
|
|
|
SpanAttributeWriter<bool> hide_edge = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".hide_edge", ATTR_DOMAIN_EDGE);
|
|
|
|
|
for (const int i : edges.index_range()) {
|
|
|
|
|
const MEdge &edge = edges[i];
|
|
|
|
|
hide_edge.span[i] = hide_vert_span[edge.v1] || hide_vert_span[edge.v2];
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
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
|
|
|
hide_edge.finish();
|
|
|
|
|
|
2022-08-12 08:57:38 +10:00
|
|
|
/* Hide polygons when any of their vertices are hidden. */
|
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
|
|
|
SpanAttributeWriter<bool> hide_poly = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".hide_poly", ATTR_DOMAIN_FACE);
|
|
|
|
|
for (const int i : polys.index_range()) {
|
|
|
|
|
const MPoly &poly = polys[i];
|
2022-08-11 14:40:15 -04:00
|
|
|
const Span<MLoop> poly_loops = loops.slice(poly.loopstart, poly.totloop);
|
|
|
|
|
hide_poly.span[i] = std::any_of(poly_loops.begin(), poly_loops.end(), [&](const MLoop &loop) {
|
|
|
|
|
return hide_vert_span[loop.v];
|
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
hide_poly.finish();
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
void BKE_mesh_flush_hidden_from_polys(Mesh *me)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
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
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2022-09-07 21:41:39 -05:00
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
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 VArray<bool> hide_poly = attributes.lookup_or_default<bool>(
|
|
|
|
|
".hide_poly", ATTR_DOMAIN_FACE, false);
|
|
|
|
|
if (hide_poly.is_single() && !hide_poly.get_internal_single()) {
|
|
|
|
|
attributes.remove(".hide_vert");
|
|
|
|
|
attributes.remove(".hide_edge");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-08-12 08:57:38 +10:00
|
|
|
const VArraySpan<bool> hide_poly_span{hide_poly};
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
const Span<MLoop> loops = me->loops();
|
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
|
|
|
SpanAttributeWriter<bool> hide_vert = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".hide_vert", ATTR_DOMAIN_POINT);
|
|
|
|
|
SpanAttributeWriter<bool> hide_edge = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".hide_edge", ATTR_DOMAIN_EDGE);
|
|
|
|
|
|
2022-08-12 08:57:38 +10:00
|
|
|
/* Hide all edges or vertices connected to hidden polygons. */
|
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
|
|
|
for (const int i : polys.index_range()) {
|
2022-08-12 08:57:38 +10:00
|
|
|
if (hide_poly_span[i]) {
|
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 MPoly &poly = polys[i];
|
|
|
|
|
for (const MLoop &loop : loops.slice(poly.loopstart, poly.totloop)) {
|
|
|
|
|
hide_vert.span[loop.v] = true;
|
|
|
|
|
hide_edge.span[loop.e] = true;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-12 08:57:38 +10:00
|
|
|
/* Unhide vertices and edges connected to visible polygons. */
|
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
|
|
|
for (const int i : polys.index_range()) {
|
2022-08-12 08:57:38 +10:00
|
|
|
if (!hide_poly_span[i]) {
|
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 MPoly &poly = polys[i];
|
|
|
|
|
for (const MLoop &loop : loops.slice(poly.loopstart, poly.totloop)) {
|
|
|
|
|
hide_vert.span[loop.v] = false;
|
|
|
|
|
hide_edge.span[loop.e] = false;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
hide_vert.finish();
|
|
|
|
|
hide_edge.finish();
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
void BKE_mesh_flush_select_from_polys(Mesh *me)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
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
|
|
|
using namespace blender::bke;
|
|
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
|
|
|
|
const VArray<bool> select_poly = attributes.lookup_or_default<bool>(
|
|
|
|
|
".select_poly", ATTR_DOMAIN_FACE, false);
|
|
|
|
|
if (select_poly.is_single() && !select_poly.get_internal_single()) {
|
|
|
|
|
attributes.remove(".select_vert");
|
|
|
|
|
attributes.remove(".select_edge");
|
|
|
|
|
return;
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
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
|
|
|
SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".select_vert", ATTR_DOMAIN_POINT);
|
|
|
|
|
SpanAttributeWriter<bool> select_edge = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".select_edge", ATTR_DOMAIN_EDGE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
/* Use generic domain interpolation to read the polygon attribute on the other domains.
|
|
|
|
|
* Assume selected faces are not hidden and none of their vertices/edges are hidden. */
|
|
|
|
|
attributes.lookup_or_default<bool>(".select_poly", ATTR_DOMAIN_POINT, false)
|
|
|
|
|
.materialize(select_vert.span);
|
|
|
|
|
attributes.lookup_or_default<bool>(".select_poly", ATTR_DOMAIN_EDGE, false)
|
|
|
|
|
.materialize(select_edge.span);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
select_vert.finish();
|
|
|
|
|
select_edge.finish();
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
static void mesh_flush_select_from_verts(const Span<MEdge> edges,
|
|
|
|
|
const Span<MPoly> polys,
|
2022-05-15 20:59:08 +02:00
|
|
|
const Span<MLoop> loops,
|
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 VArray<bool> &hide_edge,
|
|
|
|
|
const VArray<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 VArray<bool> &select_vert,
|
|
|
|
|
MutableSpan<bool> select_edge,
|
|
|
|
|
MutableSpan<bool> select_poly)
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
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
|
|
|
/* Select visible edges that have both of their vertices selected. */
|
2022-05-15 20:59:08 +02:00
|
|
|
for (const int i : edges.index_range()) {
|
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
|
|
|
if (!hide_edge[i]) {
|
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 MEdge &edge = edges[i];
|
|
|
|
|
select_edge[i] = select_vert[edge.v1] && select_vert[edge.v2];
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
/* Select visible faces that have all of their vertices selected. */
|
2022-05-15 20:59:08 +02:00
|
|
|
for (const int i : polys.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 (!hide_poly[i]) {
|
|
|
|
|
const MPoly &poly = polys[i];
|
|
|
|
|
const Span<MLoop> poly_loops = loops.slice(poly.loopstart, poly.totloop);
|
|
|
|
|
select_poly[i] = std::all_of(poly_loops.begin(), poly_loops.end(), [&](const MLoop &loop) {
|
|
|
|
|
return select_vert[loop.v];
|
|
|
|
|
});
|
2022-05-15 20:59:08 +02:00
|
|
|
}
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-15 20:59:08 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
void BKE_mesh_flush_select_from_verts(Mesh *me)
|
|
|
|
|
{
|
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
|
|
|
using namespace blender::bke;
|
|
|
|
|
MutableAttributeAccessor attributes = me->attributes_for_write();
|
|
|
|
|
const VArray<bool> select_vert = attributes.lookup_or_default<bool>(
|
|
|
|
|
".select_vert", ATTR_DOMAIN_POINT, false);
|
|
|
|
|
if (select_vert.is_single() && !select_vert.get_internal_single()) {
|
|
|
|
|
attributes.remove(".select_edge");
|
|
|
|
|
attributes.remove(".select_poly");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SpanAttributeWriter<bool> select_edge = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".select_edge", ATTR_DOMAIN_EDGE);
|
|
|
|
|
SpanAttributeWriter<bool> select_poly = attributes.lookup_or_add_for_write_only_span<bool>(
|
|
|
|
|
".select_poly", ATTR_DOMAIN_FACE);
|
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
|
|
|
mesh_flush_select_from_verts(
|
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
|
|
|
me->edges(),
|
|
|
|
|
me->polys(),
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
me->loops(),
|
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
|
|
|
attributes.lookup_or_default<bool>(".hide_edge", ATTR_DOMAIN_EDGE, false),
|
|
|
|
|
attributes.lookup_or_default<bool>(".hide_poly", ATTR_DOMAIN_FACE, false),
|
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
|
|
|
select_vert,
|
|
|
|
|
select_edge.span,
|
|
|
|
|
select_poly.span);
|
|
|
|
|
select_edge.finish();
|
|
|
|
|
select_poly.finish();
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Spatial Calculation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-03-01 15:57:50 -05:00
|
|
|
void BKE_mesh_calc_relative_deform(const MPoly *polys,
|
2013-09-09 02:11:44 +00:00
|
|
|
const int totpoly,
|
|
|
|
|
const MLoop *mloop,
|
|
|
|
|
const int totvert,
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
const float (*vert_cos_src)[3],
|
|
|
|
|
const float (*vert_cos_dst)[3],
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
const float (*vert_cos_org)[3],
|
2018-07-13 08:36:10 +02:00
|
|
|
float (*vert_cos_new)[3])
|
2013-09-09 02:11:44 +00:00
|
|
|
{
|
2022-09-25 18:33:28 +10:00
|
|
|
int *vert_accum = (int *)MEM_calloc_arrayN(size_t(totvert), sizeof(*vert_accum), __func__);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
memset(vert_cos_new, '\0', sizeof(*vert_cos_new) * size_t(totvert));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-03 10:50:54 -05:00
|
|
|
for (const int i : blender::IndexRange(totpoly)) {
|
|
|
|
|
const MPoly &poly = polys[i];
|
|
|
|
|
const MLoop *loopstart = mloop + poly.loopstart;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-03-03 10:50:54 -05:00
|
|
|
for (int j = 0; j < poly.totloop; j++) {
|
|
|
|
|
uint v_prev = loopstart[(poly.totloop + (j - 1)) % poly.totloop].v;
|
2021-06-18 14:27:41 +10:00
|
|
|
uint v_curr = loopstart[j].v;
|
2023-03-03 10:50:54 -05:00
|
|
|
uint v_next = loopstart[(j + 1) % poly.totloop].v;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
float tvec[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-08-13 14:55:45 +10:00
|
|
|
transform_point_by_tri_v3(tvec,
|
|
|
|
|
vert_cos_dst[v_curr],
|
|
|
|
|
vert_cos_org[v_prev],
|
|
|
|
|
vert_cos_org[v_curr],
|
|
|
|
|
vert_cos_org[v_next],
|
|
|
|
|
vert_cos_src[v_prev],
|
|
|
|
|
vert_cos_src[v_curr],
|
|
|
|
|
vert_cos_src[v_next]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
add_v3_v3(vert_cos_new[v_curr], tvec);
|
|
|
|
|
vert_accum[v_curr] += 1;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-03 10:50:54 -05:00
|
|
|
for (int i = 0; i < totvert; i++) {
|
2013-09-09 02:11:44 +00:00
|
|
|
if (vert_accum[i]) {
|
2022-09-25 18:33:28 +10:00
|
|
|
mul_v3_fl(vert_cos_new[i], 1.0f / float(vert_accum[i]));
|
2013-09-09 02:11:44 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(vert_cos_new[i], vert_cos_org[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
MEM_freeN(vert_accum);
|
|
|
|
|
}
|
2021-12-14 15:49:31 +11:00
|
|
|
|
2013-09-09 02:11:44 +00:00
|
|
|
/** \} */
|