Commit Graph

115 Commits

Author SHA1 Message Date
Campbell Barton 9d20632925 Cleanup: ensure trailing newline, delete trailing space 2023-10-05 13:57:14 +11:00
Falk David 00d4b5562b Fix BKE_deform.h compilation error on windows
The issue was that the pure c++ functions were defined within a `extern C` block.

Pull Request: https://projects.blender.org/blender/blender/pulls/112947
2023-09-27 12:11:32 +02:00
Falk David bc7034d9fd CurvesGeometry: Add initial vertex group support
This PR adds vertex groups to `CurvesGeometry` as well as an attribute read/write accessor.

This is also in preparation for GPv3. Since the goal is to have full compatibility with the current grease pencil features, vertex groups need to be supported.

Grease Pencil allows filtering by vertex group for modifiers.To support this, it also makes sense to have read/write access for vertex groups in the attributes API.

In the future, vertex groups should be just another custom attribute on meshes/curves/grease pencil. There are some more issues to be solved before that can happen. This step gets us a bit closer since the vertex weight data is stored in `CustomData`.

Pull Request: https://projects.blender.org/blender/blender/pulls/106944
2023-09-27 10:26:06 +02:00
Jacques Lucke 5c5a041edd Fix #112022: allow vertex groups and attributes with same names again
This was discussed in #112022 and on devtalk:
https://devtalk.blender.org/t/vertex-groups-generic-attributes-and-name-clashing/31073

While vertex groups with the same name as attributes should be avoided, since
it can cause ambiguities when using attributes, it's something we can handle
gracefully for now. Enforcing unique names for vertex groups resulted in breaking
other functionality under some circumstances.

This effectively reverts 12ef20990b, except for
the bug fix in `BKE_id_attribute_new`.

#112891 adds a warning to avoid make users aware of duplicate names so that they
can be avoided in practice.

Pull Request: https://projects.blender.org/blender/blender/pulls/112889
2023-09-26 15:36:12 +02:00
Pratik Borhade d87db8d569 Fix #111024: Crash when adding attribute to curve object
After 12ef20990b, attributes and vertex group names were checked
simultaneously to fix the name collision. But this results in crash when
new attribute is added to curve object (it searches for vertex group
list). To avoid the crash, check for supported id types in new function
`BKE_id_supports_vertex_groups`.

Pull Request: https://projects.blender.org/blender/blender/pulls/111036
2023-08-12 07:37:37 +02:00
Philipp Oeser 12ef20990b Fix #103410: name collisions between vertex groups and attributes
These name collisions should be avoided with attributes, all sorts of
issues can arise from those. We already warned in the attributes
(but not the vertex groups) list if those were found.

Previously, creating a vertex group with the same name as an already
existing attribute would allow this (and give said warning), and creating
an attribute with the same name as an already existing vertex group
would silently fail (as in: not return a layer) -- and then due to an oversight
in 101d04f41f (which assumed a valid layer would always be returned
by `BKE_id_attribute_new`) would even crash.

Now name collisions between vertex groups and attributes are avoided,
unique names will be found across attributes and vertex groups if either
`BKE_id_attribute_calc_unique_name` or `BKE_object_defgroup_unique_name`
is called.

This is done by unifying the checks and callbacks for both into a single.

Pull Request: https://projects.blender.org/blender/blender/pulls/109910
2023-08-08 10:11:10 +02:00
Hans Goudey 5e9ea9243b Mesh: Rename "polys" to "faces"
Implements part of #101689.

The "poly" name was chosen to distinguish the `MLoop` + `MPoly`
combination from the `MFace` struct it replaced. Those two structures
persisted together for a long time, but nowadays `MPoly` is gone, and
`MFace` is only used in some legacy code like the particle system.

To avoid unnecessarily using a different term, increase consistency
with the UI and with BMesh, and generally make code a bit easier to
read, this commit replaces the `poly` term with `poly`. Most variables
that use the term are renamed too. `Mesh.totface` and `Mesh.fdata` now
have a `_legacy` suffix to reduce confusion. In a next step, `pdata`
can be renamed to `face_data` as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/109819
2023-07-24 22:06:55 +02:00
Sergey Sharybin c1bc70b711 Cleanup: Add a copyright notice to files and use SPDX format
A lot of files were missing copyright field in the header and
the Blender Foundation contributed to them in a sense of bug
fixing and general maintenance.

This change makes it explicit that those files are at least
partially copyrighted by the Blender Foundation.

Note that this does not make it so the Blender Foundation is
the only holder of the copyright in those files, and developers
who do not have a signed contract with the foundation still
hold the copyright as well.

Another aspect of this change is using SPDX format for the
header. We already used it for the license specification,
and now we state it for the copyright as well, following the
FAQ:

    https://reuse.software/faq/
2023-05-31 16:19:06 +02:00
Hans Goudey 2a4323c2f5 Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.

This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.

The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.

Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.

Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
  similar to `.corner_verts`, etc. The period means that it the data
  shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
  of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
  `MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.

Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
Hans Goudey 7966cd16d6 Mesh: Replace MPoly struct with offset indices
Implements #95967.

Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.

The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.

Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.

Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.

Some:
- The offset integer array has to be one longer than the face count to
  avoid a branch for every face, which means the data is no longer part
  of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
  until more reusable CoW from #104478 is committed. That will be added
  in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
  copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
  structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
  corners, but just in case, meshes with mismatched order are fixed by
  versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
  necessary here unfortunately. It should be worth it in 3.6, since
  that's the best way to allow loading meshes from 4.0, which is
  important for an LTS version.

Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
Hans Goudey cef82a1d39 Cleanup: Grammar: "it's" vs "its" 2023-03-24 08:34:21 -04:00
Hans Goudey 16fbadde36 Mesh: Replace MLoop struct with generic attributes
Implements #102359.

Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".

The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.

The commit also starts a renaming from "loop" to "corner" in mesh code.

Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.

Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.

For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):

**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
  for (const int64_t i : range) {
    dst[i] = src[loops[i].v];
  }
});
```

**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```

That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.

Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
Hans Goudey 118ec54ec7 Cleanup: Move five mesh related files to C++
To faciliate further mesh data structure refactoring. See #103343.

Pull Request #105354
2023-03-02 23:14:33 +01:00
Sybren A. Stüvel 3efb31ee31 Cleanup: add some deformation-related comments
Add documentation for `BKE_id_defgroup_list_get()` and document that
`CD_MDEFORMVERT` mesh layers contain `MDeformVert` structs.

No functional changes.
2022-12-20 14:17:30 +01:00
Campbell Barton 90fb212a93 Docs: improve BKE_object_defgroup_flip_map doc-string 2022-09-13 13:24:45 +10:00
Campbell Barton 1e6a003860 Cleanup: move return arguments last, use `r_` prefix
Also use '_num' suffix instead of '_tot'.
2022-09-09 16:20:32 +10:00
Nate Rupsis 43b1624eee Fix T96787: Edit mode normalize fails to respect locked groups
Add BKE_object_defgroup_flip_map_unlocked which excludes locked groups
from the flip-map.

Reviewed By: zanqdo, campbellbarton

Ref D15317
2022-09-09 15:58:49 +10:00
Hans Goudey 05952aa94d 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 (e9f82d3dc7, 410a6efb74). 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
Hans Goudey e222e19d82 Cleanup: Use const arguments 2022-05-22 20:06:24 +02:00
Hans Goudey cf69652618 Cleanup: Use const when retrieving custom data layers
Knowing when layers are retrieved for write access will be essential
when adding proper copy-on-write support. This commit makes that
clearer by adding `const` where the retrieved data is not modified.

Ref T95842
2022-05-13 18:35:22 +02:00
Campbell Barton c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00
Campbell Barton 3d3bc74884 Cleanup: remove redundant const qualifiers for POD types
MSVC used to warn about const mismatch for arguments passed by value.
Remove these as newer versions of MSVC no longer show this warning.
2022-01-07 14:16:26 +11:00
Campbell Barton ffc4c126f5 Cleanup: move public doc-strings into headers for 'blenkernel'
- Added space below non doc-string comments to make it clear
  these aren't comments for the symbols directly below them.
- Use doxy sections for some headers.
- Minor improvements to doc-strings.

Ref T92709
2021-12-07 17:38:48 +11:00
Jacques Lucke dab04bc053 Fix T93231: crash when overwriting vertex group with other domain
The problem was that we forgot to actually remove the vertex group when
it should be deleted. We only removed all the data that was attached to it.

Differential Revision: https://developer.blender.org/D13326
2021-11-23 14:38:02 +01:00
Campbell Barton 42017b006e Cleanup: sort struct declarations 2021-07-16 11:48:54 +10:00
Hans Goudey 3b6ee8cee7 Refactor: Move vertex group names to object data
This commit moves the storage of `bDeformGroup` and the active index
to `Mesh`, `Lattice`, and `bGPdata` instead of `Object`. Utility
functions are added to allow easy access to the vertex groups given
an object or an ID.

As explained in T88951, the list of vertex group names is currently
stored separately per object, even though vertex group data is stored
on the geometry. This tends to complicate code and cause bugs,
especially as geometry is created procedurally and tied less closely
to an object.

The "Copy Vertex Groups to Linked" operator is removed, since they
are stored on the geometry anyway.

This patch leaves the object-level python API for vertex groups in
place. Creating a geometry-level RNA API can be a separate step;
the changes in this commit are invasive enough as it is.

Note that opening a file saved in 3.0 in an earlier version means
the vertex groups will not be available.

Differential Revision: https://developer.blender.org/D11689
2021-07-13 12:10:34 -04:00
Alexander Gavrilov 58dae919e5 Weight Paint: avoid creating very small values with locked weights.
When painting using Auto-Normalize or Lock Relative with some
groups locked, the locked weights may not add up precisely to
1 because of precision limitations, which results in creating
nonzero weights close to FLT_EPSILON. With Lock Relative display
mode this is very obvious and annoying (random red points amid
black or blue), so add an epsilon check to consider less than
1e-6 unlocked weight to be the same as 0.

In addition, in cases when no weight can be painted due to locks,
don't create vertex group entries at all if they don't exist yet.
Also, don't run Auto Normalize when not painting a deform group.

Differential Revision: https://developer.blender.org/D10000
2021-01-12 14:32:05 +03:00
Campbell Barton 38a66903e5 Cleanup: sort struct declarations 2020-09-30 11:51:13 +10:00
Sebastian Parborg 2115232a16 Cleanup: Clang-Tidy readability-inconsistent-declaration-parameter-name fix
No functional changes
2020-09-04 21:04:16 +02:00
Jacques Lucke 3dc222ea7b Refactor: move defvert .blend IO to blenkernel
This is part of T76372.
2020-08-28 14:30:45 +02:00
Jacques Lucke 91694b9b58 Code Style: use "#pragma once" in source directory
This replaces header include guards with `#pragma once`.
A couple of include guards are not removed yet (e.g. `__RNA_TYPES_H__`),
because they are used in other places.

This patch has been generated by P1561 followed by `make format`.

Differential Revision: https://developer.blender.org/D8466
2020-08-07 09:50:34 +02:00
Jacques Lucke 725973485a Clang Tidy: enable readability-non-const-parameter warning
Clang Tidy reported a couple of false positives. I disabled
those `NOLINTNEXTLINE`.

Differential Revision: https://developer.blender.org/D8199
2020-07-13 11:27:09 +02:00
Brecht Van Lommel 3e4f49fe71 Revert "Fix T78296: Performance - Use Binary Search for MDeformWeight"
This reverts commit 39b525e0f0 and
3121015dce as tests are failing.
2020-07-10 18:03:21 +02:00
Jeroen Bakker 39b525e0f0 Fix T78296: Performance - Use Binary Search for MDeformWeight
Use binary search for querying deform weights.

Spring 02_020_A.anim.blend on Ryzen 1700X goes from 12.4 to 12.7fps.

During profiling it was detected that adding new items to the head was faster than adding to the tail.

Reviewed By: Campbell Barton

Differential Revision: https://developer.blender.org/D8127
2020-07-10 12:09:40 +02:00
Campbell Barton 827959ff98 Cleanup: use const arguments to deform functions
This changes curve deform code not to set the objects inverse matrix,
this shouldn't cause problems as it's not used elsewhere afterwards.
2020-06-13 16:20:09 +10:00
Alexander Gavrilov 084bf7daee Weight Paint: Implement a new Lock-Relative mode.
This check box alters how weights are displayed and painted,
similar to Multi Paint, but in a different way. Specifically,
weights are presented as if all locked vertex groups were
deleted, and the remaining deform groups normalized.

The new feature is intended for use when balancing weights within
a group of bones while all others are locked. Enabling the option
presents weight as if the locked bones didn't exist, and their
weight was proportionally redistributed to the editable bones.

Conversely, the Multi-Paint feature allows balancing a group of
bones as a whole against all unselected bones, while ignoring
weight distribution within the selected group.

This mode also allows temporarily viewing non-normalized weights
as if they were normalized, without actually changing the values.

Differential Revision: https://developer.blender.org/D3837
2020-03-18 11:55:44 +03:00
Campbell Barton 54ab8c6abd Cleanup: rename 'verify' to 'ensure' for argument name 2020-03-06 13:00:01 +11:00
Campbell Barton bba4a09b2f Cleanup: use 'BKE_' prefix for BKE_deform API calls
- Use 'BKE_object_defgroup' prefix for object functions.

- Rename 'defvert_verify_index' to 'defvert_ensure_index'
  since this adds the group if it isn't found.
2020-03-06 12:56:44 +11:00
Campbell Barton ad2a8400e9 Fix T56108: Crash editing corrupted vertex groups
While the file in this report had corrupted values,
this is avoidable without adding any extra overhead.

Use unsigned vertex group indices since we don't need negative values,
this is an alternative to checking they aren't negative in many places.

Vertex group values over INT_MAX is still considered invalid,
so any accidental unsigned wrapping won't be silently ignored.
2020-02-11 13:20:49 +11:00
Jacques Lucke f3db5a0965 Cleanup: Improve usage of extern "C" 2020-02-07 17:22:11 +01:00
Campbell Barton aa42da0385 Cleanup: comments (long lines) in blenkernel 2019-04-27 12:07:07 +10:00
Campbell Barton e12c08e8d1 ClangFormat: apply to source, most of intern
Apply clang format as proposed in T53211.

For details on usage and instructions for migrating branches
without conflicts, see:

https://wiki.blender.org/wiki/Tools/ClangFormat
2019-04-17 06:21:24 +02:00
Campbell Barton de13d0a80c doxygen: add newline after \file
While \file doesn't need an argument, it can't have another doxy
command after it.
2019-02-18 08:22:12 +11:00
Campbell Barton eef4077f18 Cleanup: remove redundant doxygen \file argument
Move \ingroup onto same line to be more compact and
make it clear the file is in the group.
2019-02-06 15:45:22 +11:00
Campbell Barton 4ef09cf937 Cleanup: remove author/date info from doxy headers 2019-02-02 11:58:24 +11:00
Campbell Barton 65ec7ec524 Cleanup: remove redundant, invalid info from headers
BF-admins agree to remove header information that isn't useful,
to reduce noise.

- BEGIN/END license blocks

  Developers should add non license comments as separate comment blocks.
  No need for separator text.

- Contributors

  This is often invalid, outdated or misleading
  especially when splitting files.

  It's more useful to git-blame to find out who has developed the code.

See P901 for script to perform these edits.
2019-02-02 01:36:28 +11:00
Campbell Barton c0f88ed8a8 Cleanup: sort forward declarations of enum & struct
Done using:
  source/tools/utils_maintenance/c_sort_blocks.py
2019-01-28 21:17:58 +11:00
Bastien Montagne 5ba87cf22e Cleanup: remove another bunch of DM usages, includes etc. 2018-06-29 14:57:02 +02:00
Bastien Montagne eeb9e5316a Make whole ID copying code use const source pointer.
Noisy change, but safe, and better do it sooner than later if we are to
rework copying code. Also, previous commit shows this *is* useful to
catch some mistakes.
2017-06-14 22:38:11 +02:00
Bastien Montagne e0303d0297 Cleanup/refactor: move BKE_deform_flip_side_name & co to BLI_string_utils
Functions like that do not have anything to do in BKE really, even less
when actually more used for bones than vgroups!
2017-01-16 20:36:23 +01:00