Blender V5.3
id_map.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <cstring>
8
9#include "scene/geometry.h"
10#include "scene/scene.h"
11
12#include "util/map.h"
13#include "util/set.h"
14
15namespace blender {
16struct ID;
17}
18
20
21/* ID Map
22 *
23 * Utility class to map between Blender datablocks and Cycles data structures,
24 * and keep track of recalc tags from the dependency graph. */
25
26template<typename K, typename T, typename Flags = uint> class id_map {
27 public:
28 id_map(Scene *scene_) : scene(scene_) {}
29
31 {
32 set<T *> nodes;
33
34 typename map<K, T *>::iterator jt;
35 for (jt = b_map.begin(); jt != b_map.end(); jt++) {
36 nodes.insert(jt->second);
37 }
38
39 scene->delete_nodes(nodes);
40 }
41
42 T *find(const K &key)
43 {
44 if (b_map.find(key) != b_map.end()) {
45 T *data = b_map[key];
46 return data;
47 }
48
49 return nullptr;
50 }
51
52 void set_recalc(void *id_ptr)
53 {
54 b_recalc.insert(id_ptr);
55 }
56
57 bool check_recalc(const blender::ID *id)
58 {
59 return id && b_recalc.contains(id);
60 }
61
63 {
64 return !(b_recalc.empty());
65 }
66
67 void pre_sync()
68 {
69 used_set.clear();
70 }
71
72 /* Add new data. */
73 void add(const K &key, T *data)
74 {
75 assert(find(key) == nullptr);
76 b_map[key] = data;
77 used(data);
78 }
79
80 /* Update existing data. */
81 bool update(T *data, const blender::ID *id)
82 {
83 return update(data, id, id);
84 }
85 bool update(T *data, const blender::ID *id, const blender::ID *parent)
86 {
87 bool recalc = (b_recalc.contains(id));
88 if (parent && parent != id) {
89 recalc = recalc || (b_recalc.contains(parent));
90 }
91 used(data);
92 return recalc;
93 }
94
95 /* Combined add and update as needed. */
96 bool add_or_update(T **r_data, const blender::ID *id)
97 {
98 return add_or_update(r_data, id, id, id);
99 }
100 bool add_or_update(T **r_data, const blender::ID *id, const K &key)
101 {
102 return add_or_update(r_data, id, id, key);
103 }
104 bool add_or_update(T **r_data, const blender::ID *id, const blender::ID *parent, const K &key)
105 {
106 T *data = find(key);
107 bool recalc;
108
109 if (!data) {
110 /* Add data if it didn't exist yet. */
111 data = scene->create_node<T>();
112 add(key, data);
113 recalc = true;
114 }
115 else {
116 /* check if updated needed. */
117 recalc = update(data, id, parent);
118 }
119
120 *r_data = data;
121 return recalc;
122 }
123
124 /* Combined add or update for convenience. */
125
126 bool is_used(const K &key)
127 {
128 T *data = find(key);
129 return (data) ? used_set.find(data) != used_set.end() : false;
130 }
131
132 void used(T *data)
133 {
134 /* tag data as still in use */
135 used_set.insert(data);
136 }
137
138 void set_default(T *data)
139 {
140 b_map[nullptr] = data;
141 }
142
143 void post_sync(bool do_delete = true)
144 {
145 map<K, T *> new_map;
146 set<T *> nodes_to_delete;
147 using TMapPair = pair<const K, T *>;
148 typename map<K, T *>::iterator jt;
149
150 for (jt = b_map.begin(); jt != b_map.end(); jt++) {
151 TMapPair &pair = *jt;
152
153 if (do_delete && used_set.find(pair.second) == used_set.end()) {
154 flags.erase(pair.second);
155 nodes_to_delete.insert(pair.second);
156 }
157 else {
158 new_map[pair.first] = pair.second;
159 }
160 }
161
162 if (!nodes_to_delete.empty()) {
163 scene->delete_nodes(nodes_to_delete);
164 }
165
166 used_set.clear();
167 b_recalc.clear();
168 b_map = new_map;
169 }
170
171 const map<K, T *> &key_to_scene_data()
172 {
173 return b_map;
174 }
175
176 bool test_flag(T *data, Flags val)
177 {
178 typename map<T *, uint>::iterator it = flags.find(data);
179 return it != flags.end() && (it->second & (1 << val)) != 0;
180 }
181
182 void set_flag(T *data, Flags val)
183 {
184 flags[data] |= (1 << val);
185 }
186
187 void clear_flag(T *data, Flags val)
188 {
189 typename map<T *, uint>::iterator it = flags.find(data);
190 if (it != flags.end()) {
191 it->second &= ~(1 << val);
192
193 if (it->second == 0) {
194 flags.erase(it);
195 }
196 }
197 }
198
199 protected:
200 map<K, T *> b_map;
201 set<T *> used_set;
202 map<T *, uint> flags;
203 set<const void *> b_recalc;
205};
206
207/* Object Key
208 *
209 * To uniquely identify instances, we use the parent, object and persistent instance ID.
210 * We also export separate object for a mesh and its particle hair. */
211
212enum { OBJECT_PERSISTENT_ID_SIZE = 8 /* MAX_DUPLI_RECUR in Blender. */ };
213
214struct ObjectKey {
215 void *parent;
217 void *ob;
219
220 ObjectKey(void *parent_,
221 const int id_[OBJECT_PERSISTENT_ID_SIZE],
222 void *ob_,
223 bool use_particle_hair_)
224 : parent(parent_), ob(ob_), use_particle_hair(use_particle_hair_)
225 {
226 if (id_) {
227 memcpy(id, id_, sizeof(id));
228 }
229 else {
230 memset(id, 0, sizeof(id));
231 }
232 }
233
234 bool operator<(const ObjectKey &k) const
235 {
236 if (ob < k.ob) {
237 return true;
238 }
239 if (ob == k.ob) {
240 if (parent < k.parent) {
241 return true;
242 }
243 if (parent == k.parent) {
245 return true;
246 }
248 return memcmp(id, k.id, sizeof(id)) < 0;
249 }
250 }
251 }
252
253 return false;
254 }
255};
256
257/* Geometry Key
258 *
259 * We export separate geometry for a mesh and its particle hair, so key needs to
260 * distinguish between them. */
261
263 void *id;
265
267
268 bool operator<(const GeometryKey &k) const
269 {
270 if (id < k.id) {
271 return true;
272 }
273 if (id == k.id) {
275 return true;
276 }
277 }
278
279 return false;
280 }
281};
282
283/* Particle System Key */
284
286 void *ob;
288
289 ParticleSystemKey(void *ob_, const int id_[OBJECT_PERSISTENT_ID_SIZE]) : ob(ob_)
290 {
291 if (id_) {
292 memcpy(id, id_, sizeof(id));
293 }
294 else {
295 memset(id, 0, sizeof(id));
296 }
297 }
298
299 bool operator<(const ParticleSystemKey &k) const
300 {
301 /* first id is particle index, we don't compare that */
302 if (ob < k.ob) {
303 return true;
304 }
305 if (ob == k.ob) {
306 return memcmp(id + 1, k.id + 1, sizeof(int) * (OBJECT_PERSISTENT_ID_SIZE - 1)) < 0;
307 }
308
309 return false;
310 }
311};
312
Definition scene.h:133
T * create_node(Args &&...)=delete
void delete_nodes(const set< T * > &nodes)
Definition scene.h:251
void used(T *data)
Definition id_map.h:132
bool update(T *data, const blender::ID *id, const blender::ID *parent)
Definition id_map.h:85
set< const void * > b_recalc
Definition id_map.h:203
~id_map()
Definition id_map.h:30
void pre_sync()
Definition id_map.h:67
bool test_flag(T *data, Flags val)
Definition id_map.h:176
set< T * > used_set
Definition id_map.h:201
Scene * scene
Definition id_map.h:204
bool update(T *data, const blender::ID *id)
Definition id_map.h:81
bool has_recalc()
Definition id_map.h:62
map< K, T * > b_map
Definition id_map.h:200
bool check_recalc(const blender::ID *id)
Definition id_map.h:57
void add(const K &key, T *data)
Definition id_map.h:73
bool is_used(const K &key)
Definition id_map.h:126
T * find(const K &key)
Definition id_map.h:42
id_map(Scene *scene_)
Definition id_map.h:28
void set_flag(T *data, Flags val)
Definition id_map.h:182
void set_recalc(void *id_ptr)
Definition id_map.h:52
map< T *, uint > flags
Definition id_map.h:202
bool add_or_update(T **r_data, const blender::ID *id, const blender::ID *parent, const K &key)
Definition id_map.h:104
void clear_flag(T *data, Flags val)
Definition id_map.h:187
void post_sync(bool do_delete=true)
Definition id_map.h:143
void set_default(T *data)
Definition id_map.h:138
bool add_or_update(T **r_data, const blender::ID *id)
Definition id_map.h:96
bool add_or_update(T **r_data, const blender::ID *id, const K &key)
Definition id_map.h:100
const map< K, T * > & key_to_scene_data()
Definition id_map.h:171
#define CCL_NAMESPACE_END
#define assert(assertion)
@ OBJECT_PERSISTENT_ID_SIZE
Definition id_map.h:212
#define T
BMesh Iterators.
void * id
Definition id_map.h:263
GeometryKey(void *id, Geometry::Type geometry_type)
Definition id_map.h:266
Geometry::Type geometry_type
Definition id_map.h:264
bool operator<(const GeometryKey &k) const
Definition id_map.h:268
bool use_particle_hair
Definition id_map.h:218
void * parent
Definition id_map.h:215
void * ob
Definition id_map.h:217
bool operator<(const ObjectKey &k) const
Definition id_map.h:234
ObjectKey(void *parent_, const int id_[OBJECT_PERSISTENT_ID_SIZE], void *ob_, bool use_particle_hair_)
Definition id_map.h:220
int id[OBJECT_PERSISTENT_ID_SIZE]
Definition id_map.h:216
int id[OBJECT_PERSISTENT_ID_SIZE]
Definition id_map.h:287
bool operator<(const ParticleSystemKey &k) const
Definition id_map.h:299
ParticleSystemKey(void *ob_, const int id_[OBJECT_PERSISTENT_ID_SIZE])
Definition id_map.h:289