Blender V5.3
undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_task.hh"
10
11#include "BKE_attribute.hh"
13#include "BKE_context.hh"
14#include "BKE_main.hh"
15#include "BKE_object.hh"
16#include "BKE_pointcloud.hh"
17#include "BKE_undo_system.hh"
18
19#include "CLG_log.h"
20
21#include "DEG_depsgraph.hh"
22
23#include "ED_pointcloud.hh"
24#include "ED_undo.hh"
25
26#include "WM_api.hh"
27#include "WM_types.hh"
28
29namespace blender {
30
31static CLG_LogRef LOG = {"undo.pointcloud"};
32
33namespace ed::pointcloud {
34namespace undo {
35
36/* -------------------------------------------------------------------- */
41
42struct StepObject {
43 UndoRefID_Object obedit_ref = {};
45 int totpoint = 0;
46 /* Store the bounds caches because they are small. */
49};
50
57
58static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
59{
60 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
61
62 Scene *scene = CTX_data_scene(C);
63 ViewLayer *view_layer = CTX_data_view_layer(C);
64 Vector<Object *> objects = ED_undo_editmode_objects_from_view_layer(*bmain, scene, view_layer);
65
66 us->scene_ref.ptr = scene;
67 new (&us->objects) Array<StepObject>(objects.size());
68
69 threading::parallel_for(us->objects.index_range(), 8, [&](const IndexRange range) {
70 for (const int i : range) {
71 Object *ob = objects[i];
72 StepObject &object = us->objects[i];
73 const PointCloud &pointcloud = *id_cast<const PointCloud *>(ob->data);
74 object.obedit_ref.ptr = ob;
75 object.attribute_storage.wrap() = pointcloud.attribute_storage.wrap();
76 object.bounds_cache = pointcloud.runtime->bounds_cache;
77 object.bounds_with_radius_cache = pointcloud.runtime->bounds_with_radius_cache;
78 object.totpoint = pointcloud.totpoint;
79 }
80 });
81
82 bmain->is_memfile_undo_flush_needed = true;
83
84 return true;
85}
86
87static void step_decode(
88 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
89{
90 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
91 Scene *scene = CTX_data_scene(C);
92 ViewLayer *view_layer = CTX_data_view_layer(C);
93
95 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
97 view_layer,
98 &us->objects.first().obedit_ref.ptr,
99 us->objects.size(),
100 sizeof(decltype(us->objects)::value_type));
101
102 BLI_assert(BKE_object_is_in_editmode(us->objects.first().obedit_ref.ptr));
103
104 for (const StepObject &object : us->objects) {
105 PointCloud &pointcloud = *id_cast<PointCloud *>(object.obedit_ref.ptr->data);
106
107 const bool positions_changed = [&]() {
108 const bke::Attribute *attr_a = pointcloud.attribute_storage.wrap().lookup("position");
109 const bke::Attribute *attr_b = object.attribute_storage.wrap().lookup("position");
110 if (!attr_b && !attr_a) {
111 return false;
112 }
113 if (!attr_a || !attr_b) {
114 return true;
115 }
116 return std::get<bke::Attribute::ArrayData>(attr_a->data()).data !=
117 std::get<bke::Attribute::ArrayData>(attr_b->data()).data;
118 }();
119
120 pointcloud.attribute_storage.wrap() = object.attribute_storage.wrap();
121
122 pointcloud.totpoint = object.totpoint;
123 pointcloud.runtime->bounds_cache = object.bounds_cache;
124 pointcloud.runtime->bounds_with_radius_cache = object.bounds_with_radius_cache;
125 if (positions_changed) {
126 pointcloud.runtime->bvh_cache.tag_dirty();
127 }
129 }
130
132 *bmain, scene, view_layer, us->objects.first().obedit_ref.ptr, us_p->name, &LOG);
133
134 bmain->is_memfile_undo_flush_needed = true;
135
137}
138
139static void step_free(UndoStep *us_p)
140{
141 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
142 us->objects.~Array();
143}
144
145static void foreach_ID_ref(UndoStep *us_p,
146 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
147 void *user_data)
148{
149 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
150
151 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->scene_ref)));
152 for (const StepObject &object : us->objects) {
153 foreach_ID_ref_fn(
154 user_data,
155 (reinterpret_cast<UndoRefID *>(const_cast<UndoRefID_Object *>(&object.obedit_ref))));
156 }
157}
158
160
161} // namespace undo
162
177
178} // namespace ed::pointcloud
179} // namespace blender
General operations, lookup, etc. for blender objects.
General operations for point clouds.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
int64_t size() const
const DataVariant & data() const
static void foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
Definition undo.cc:145
static void step_free(UndoStep *us_p)
Definition undo.cc:139
static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
Definition undo.cc:58
static void step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
Definition undo.cc:87
bool editable_pointcloud_in_edit_mode_poll(bContext *C)
Definition operators.cc:73
void undosys_type_register(UndoType *ut)
Definition undo.cc:163
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
void ED_undo_object_set_active_or_warn(const Main &bmain, Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:1024
Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:1109
ViewLayer * CTX_data_view_layer(const bContext *C)
void DEG_id_tag_update(ID *id, unsigned int flags)
wmWindowManager * CTX_wm_manager(const bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static CLG_LogRef LOG
Definition logging.cpp:12
void ED_undo_object_editmode_restore_helper(Scene *scene, ViewLayer *view_layer, Object **object_array, uint object_array_len, uint object_array_stride)
Definition ed_undo.cc:1064
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
void ED_undo_object_editmode_validate_scene_from_windows(wmWindowManager *wm, const Scene *scene_ref, Scene **scene_p, ViewLayer **view_layer_p)
Definition ed_undo.cc:1047
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier
bke::AttributeStorage attribute_storage
Definition undo.cc:44
SharedCache< Bounds< float3 > > bounds_with_radius_cache
Definition undo.cc:48
SharedCache< Bounds< float3 > > bounds_cache
Definition undo.cc:47