Files
test/source/blender/blenlib/BLI_multi_value_map.hh
Jacques Lucke 80441190c6 Nodes: automatically gray out input values that don't affect the output
This patch automatically grays out input values which can't affect the output
currently. It works with inputs of group nodes, geometry nodes modifiers and
node tools.

To achieve this, it analyses the node tree and partially evaluates it to figure
out which group inputs are currently not linked to an output or are disabled by e.g.
some switch node.

Original proposal: https://devtalk.blender.org/t/dynamic-socket-visibility/31874
Related info in blog post:
https://code.blender.org/2023/11/geometry-nodes-workshop-november-2023/#dynamic-socket-visibility

Follow up task for designing a UI that allows hiding sockets: #132706

Limitations:
* The inferencing does not update correctly when a socket starts being
  animated/driven. I haven't found a good way to invalidate the cache in a good
  way reliably yet. It's only a very short term problem though. It fixes itself
  after the next modification of the node tree and is only noticeable when
  animating some specific sockets such as the switch node condition.
* Whether a socket is grayed out is not exposed in the Python API yet. That will
  be done separately.
* Only a partial evaluation is done to determine if an input affects an output.
  There should be no cases where a socket is found to be unused when it can actually
  affect the output. However, there can be cases where a socket is inferenced to be used
  even if it is not due to some complex condition. Depending on the exact circumstances,
  this can either be improved or the condition in the node tree should be simplified.

Pull Request: https://projects.blender.org/blender/blender/pulls/132219
2025-01-21 12:53:24 +01:00

175 lines
4.2 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
/** \file
* \ingroup bli
*
* A `blender::MultiValueMap<Key, Value>` is an unordered associative container that stores
* key-value pairs. It is different from `blender::Map` in that it can store multiple values for
* the same key. The list of values that corresponds to a specific key can contain duplicates
* and their order is maintained.
*
* This data structure is different from a `std::multi_map`, because multi_map can store the same
* key more than once and MultiValueMap can't.
*
* Currently, this class exists mainly for convenience. There are no performance benefits over
* using Map<Key, Vector<Value>>. In the future, a better implementation for this data structure
* can be developed.
*/
#include "BLI_map.hh"
#include "BLI_struct_equality_utils.hh"
#include "BLI_vector.hh"
namespace blender {
template<typename Key, typename Value> class MultiValueMap {
public:
using size_type = int64_t;
private:
using MapType = Map<Key, Vector<Value>>;
MapType map_;
public:
/**
* Add a new value for the given key. If the map contains the key already, the value will be
* appended to the list of corresponding values.
*/
void add(const Key &key, const Value &value)
{
this->add_as(key, value);
}
void add(const Key &key, Value &&value)
{
this->add_as(key, std::move(value));
}
void add(Key &&key, const Value &value)
{
this->add_as(std::move(key), value);
}
void add(Key &&key, Value &&value)
{
this->add_as(std::move(key), std::move(value));
}
template<typename ForwardKey, typename ForwardValue>
void add_as(ForwardKey &&key, ForwardValue &&value)
{
Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
vector.append(std::forward<ForwardValue>(value));
}
void add_non_duplicates(const Key &key, const Value &value)
{
Vector<Value> &vector = map_.lookup_or_add_default_as(key);
vector.append_non_duplicates(value);
}
/**
* Add all given values to the key.
*/
void add_multiple(const Key &key, Span<Value> values)
{
this->add_multiple_as(key, values);
}
void add_multiple(Key &&key, Span<Value> values)
{
this->add_multiple_as(std::move(key), values);
}
template<typename ForwardKey> void add_multiple_as(ForwardKey &&key, Span<Value> values)
{
Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
vector.extend(values);
}
/**
* Get a span to all the values that are stored for the given key.
*/
Span<Value> lookup(const Key &key) const
{
return this->lookup_as(key);
}
template<typename ForwardKey> Span<Value> lookup_as(const ForwardKey &key) const
{
const Vector<Value> *vector = map_.lookup_ptr_as(key);
if (vector != nullptr) {
return vector->as_span();
}
return {};
}
/**
* Get a mutable span to all the values that are stored for the given key.
*/
MutableSpan<Value> lookup(const Key &key)
{
return this->lookup_as(key);
}
template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
{
Vector<Value> *vector = map_.lookup_ptr_as(key);
if (vector != nullptr) {
return vector->as_mutable_span();
}
return {};
}
/**
* Get the number of keys.
*/
int64_t size() const
{
return map_.size();
}
/**
* Returns true if there are no keys in the map.
* NOTE: There may be keys without values. In this case the map is not empty.
*/
bool is_empty() const
{
return map_.is_empty();
}
/**
* NOTE: This signature will change when the implementation changes.
*/
typename MapType::ItemIterator items() const
{
return map_.items();
}
/**
* NOTE: This signature will change when the implementation changes.
*/
typename MapType::KeyIterator keys() const
{
return map_.keys();
}
/**
* NOTE: This signature will change when the implementation changes.
*/
typename MapType::ValueIterator values() const
{
return map_.values();
}
void clear()
{
map_.clear();
}
void clear_and_shrink()
{
map_.clear_and_shrink();
}
BLI_STRUCT_EQUALITY_OPERATORS_1(MultiValueMap, map_)
};
} // namespace blender