Changeset View
Changeset View
Standalone View
Standalone View
source/blender/depsgraph/intern/depsgraph_physics.cc
| Show All 10 Lines | |||||
| #include "MEM_guardedalloc.h" | #include "MEM_guardedalloc.h" | ||||
| #include "BLI_compiler_compat.h" | #include "BLI_compiler_compat.h" | ||||
| #include "BLI_listbase.h" | #include "BLI_listbase.h" | ||||
| #include "BKE_collision.h" | #include "BKE_collision.h" | ||||
| #include "BKE_effect.h" | #include "BKE_effect.h" | ||||
| #include "BKE_gpencil_modifier.h" | |||||
| #include "BKE_modifier.h" | #include "BKE_modifier.h" | ||||
| #include "DNA_collection_types.h" | #include "DNA_collection_types.h" | ||||
| #include "DNA_object_force_types.h" | #include "DNA_object_force_types.h" | ||||
| #include "DNA_object_types.h" | #include "DNA_object_types.h" | ||||
| #include "DEG_depsgraph_build.h" | #include "DEG_depsgraph_build.h" | ||||
| #include "DEG_depsgraph_physics.h" | #include "DEG_depsgraph_physics.h" | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | if (hash == nullptr) { | ||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| /* NOTE: nullptr is a valid lookup key here as it means that the relation is not bound to a | /* NOTE: nullptr is a valid lookup key here as it means that the relation is not bound to a | ||||
| * specific collection. */ | * specific collection. */ | ||||
| ID *collection_orig = DEG_get_original_id(object_id_safe(collection)); | ID *collection_orig = DEG_get_original_id(object_id_safe(collection)); | ||||
| return hash->lookup_default(collection_orig, nullptr); | return hash->lookup_default(collection_orig, nullptr); | ||||
| } | } | ||||
| ListBase *DEG_get_lineart_relations(const Depsgraph *graph, Collection *collection) | |||||
| { | |||||
| const deg::Depsgraph *deg_graph = reinterpret_cast<const deg::Depsgraph *>(graph); | |||||
| blender::Map<const ID *, ListBase *> *hash = | |||||
| deg_graph->physics_relations[DEG_PHYSICS_RELATIONS_LINEART]; | |||||
| if (hash == nullptr) { | |||||
| return nullptr; | |||||
| } | |||||
| /* Using nullptr as lookup key as we don't need to read objects separatedly from different | |||||
| * collections. */ | |||||
| return hash->lookup_default(nullptr, nullptr); | |||||
| } | |||||
| /********************** Depsgraph Building API ************************/ | /********************** Depsgraph Building API ************************/ | ||||
| void DEG_add_collision_relations(DepsNodeHandle *handle, | void DEG_add_collision_relations(DepsNodeHandle *handle, | ||||
| Object *object, | Object *object, | ||||
| Collection *collection, | Collection *collection, | ||||
| unsigned int modifier_type, | unsigned int modifier_type, | ||||
| DEG_CollobjFilterFunction filter_function, | DEG_CollobjFilterFunction filter_function, | ||||
| const char *name) | const char *name) | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | LISTBASE_FOREACH (EffectorRelation *, relation, relations) { | ||||
| /* Absorption forces need collision relation. */ | /* Absorption forces need collision relation. */ | ||||
| if (add_absorption && (relation->pd->flag & PFIELD_VISIBILITY)) { | if (add_absorption && (relation->pd->flag & PFIELD_VISIBILITY)) { | ||||
| DEG_add_collision_relations( | DEG_add_collision_relations( | ||||
| handle, object, nullptr, eModifierType_Collision, nullptr, "Force Absorption"); | handle, object, nullptr, eModifierType_Collision, nullptr, "Force Absorption"); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void DEG_add_lineart_relations(DepsNodeHandle *handle, | |||||
| Object *object, | |||||
| Collection *collection, | |||||
| bool allow_duplicates, | |||||
| const char *name) | |||||
| { | |||||
| Depsgraph *depsgraph = DEG_get_graph_from_handle(handle); | |||||
| deg::Depsgraph *deg_graph = (deg::Depsgraph *)depsgraph; | |||||
| ListBase *relations = build_lineart_relations(deg_graph, collection, allow_duplicates); | |||||
| LISTBASE_FOREACH (LineartRelation *, relation, relations) { | |||||
| DEG_add_object_relation(handle, relation->ob, DEG_OB_COMP_GEOMETRY, name); | |||||
| DEG_add_object_relation(handle, relation->ob, DEG_OB_COMP_TRANSFORM, name); | |||||
| } | |||||
| } | |||||
| /******************************** Internal API ********************************/ | /******************************** Internal API ********************************/ | ||||
| namespace blender::deg { | namespace blender::deg { | ||||
| ListBase *build_effector_relations(Depsgraph *graph, Collection *collection) | ListBase *build_effector_relations(Depsgraph *graph, Collection *collection) | ||||
| { | { | ||||
| Map<const ID *, ListBase *> *hash = graph->physics_relations[DEG_PHYSICS_EFFECTOR]; | Map<const ID *, ListBase *> *hash = graph->physics_relations[DEG_PHYSICS_EFFECTOR]; | ||||
| if (hash == nullptr) { | if (hash == nullptr) { | ||||
| Show All 27 Lines | ListBase *build_collision_relations(Depsgraph *graph, | ||||
| */ | */ | ||||
| ID *collection_id = object_id_safe(collection); | ID *collection_id = object_id_safe(collection); | ||||
| return hash->lookup_or_add_cb(collection_id, [&]() { | return hash->lookup_or_add_cb(collection_id, [&]() { | ||||
| ::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph); | ::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph); | ||||
| return BKE_collision_relations_create(depsgraph, collection, modifier_type); | return BKE_collision_relations_create(depsgraph, collection, modifier_type); | ||||
| }); | }); | ||||
| } | } | ||||
| ListBase *build_lineart_relations(Depsgraph *graph, Collection *collection, bool allow_duplicates) | |||||
| { | |||||
| Map<const ID *, ListBase *> *hash = graph->physics_relations[DEG_PHYSICS_RELATIONS_LINEART]; | |||||
| if (hash == nullptr) { | |||||
| graph->physics_relations[DEG_PHYSICS_RELATIONS_LINEART] = new Map<const ID *, ListBase *>(); | |||||
| hash = graph->physics_relations[DEG_PHYSICS_RELATIONS_LINEART]; | |||||
| } | |||||
| /* Using nullptr as lookup key as we don't need to read objects separatedly from different | |||||
| * collections. */ | |||||
| return hash->lookup_or_add_cb(nullptr, [&]() { | |||||
| ::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph); | |||||
| return BKE_lineart_relations_create(depsgraph, collection, allow_duplicates); | |||||
| }); | |||||
| } | |||||
| void clear_physics_relations(Depsgraph *graph) | void clear_physics_relations(Depsgraph *graph) | ||||
| { | { | ||||
| for (int i = 0; i < DEG_PHYSICS_RELATIONS_NUM; i++) { | for (int i = 0; i < DEG_PHYSICS_RELATIONS_NUM; i++) { | ||||
| Map<const ID *, ListBase *> *hash = graph->physics_relations[i]; | Map<const ID *, ListBase *> *hash = graph->physics_relations[i]; | ||||
| if (hash) { | if (hash) { | ||||
| const ePhysicsRelationType type = (ePhysicsRelationType)i; | const ePhysicsRelationType type = (ePhysicsRelationType)i; | ||||
| switch (type) { | switch (type) { | ||||
| case DEG_PHYSICS_EFFECTOR: | case DEG_PHYSICS_EFFECTOR: | ||||
| for (ListBase *list : hash->values()) { | for (ListBase *list : hash->values()) { | ||||
| BKE_effector_relations_free(list); | BKE_effector_relations_free(list); | ||||
| } | } | ||||
| break; | break; | ||||
| case DEG_PHYSICS_COLLISION: | case DEG_PHYSICS_COLLISION: | ||||
| case DEG_PHYSICS_SMOKE_COLLISION: | case DEG_PHYSICS_SMOKE_COLLISION: | ||||
| case DEG_PHYSICS_DYNAMIC_BRUSH: | case DEG_PHYSICS_DYNAMIC_BRUSH: | ||||
| for (ListBase *list : hash->values()) { | for (ListBase *list : hash->values()) { | ||||
| BKE_collision_relations_free(list); | BKE_collision_relations_free(list); | ||||
| } | } | ||||
| break; | break; | ||||
| case DEG_PHYSICS_RELATIONS_LINEART: | |||||
| for (ListBase *list : hash->values()) { | |||||
| BKE_lineart_relations_free(list); | |||||
| } | |||||
| break; | |||||
| case DEG_PHYSICS_RELATIONS_NUM: | case DEG_PHYSICS_RELATIONS_NUM: | ||||
| break; | break; | ||||
| } | } | ||||
| delete hash; | delete hash; | ||||
| graph->physics_relations[i] = nullptr; | graph->physics_relations[i] = nullptr; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } // namespace blender::deg | } // namespace blender::deg | ||||