BRL-CAD
Loading...
Searching...
No Matches
dbi.h
Go to the documentation of this file.
1/* D B I . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2008-2025 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20/** @addtogroup ged_defines
21 *
22 * Experimental
23 *
24 * Geometry EDiting Library structures for reflecting the state of
25 * the database and views.
26 *
27 * These are used to provide a fast, explicit expression in memory of the
28 * database and view states, to allow applications to quickly display
29 * hierarchical information and manipulate view data.
30 *
31 * We want this to be visible to C++ APIs like libqtcad, so they can reflect
32 * the state of the .g hierarchy in their own structures without us or them
33 * having to make copies of the data. Pattern this on how we handle ON_Brep
34 */
35/** @{ */
36/** @file ged/defines.h */
37
38#ifndef GED_DBI_H
39#define GED_DBI_H
40
41#include "common.h"
42#include "vmath.h"
43#include "bu/vls.h"
44
45#ifdef __cplusplus
46#include <set>
47#include <map>
48#include <unordered_map>
49#include <unordered_set>
50#include <string>
51#include <vector>
52
54
56 public:
58
59 bool select_path(const char *path, bool update);
61
62 bool deselect_path(const char *path, bool update);
64
65 void clear();
66
67 bool is_selected(unsigned long long);
68 bool is_active(unsigned long long);
69 bool is_active_parent(unsigned long long);
70 bool is_parent_obj(unsigned long long);
71 bool is_immediate_parent_obj(unsigned long long);
72 bool is_grand_parent_obj(unsigned long long);
73
75
76 void expand();
77 void collapse();
78
79 void refresh();
80 bool draw_sync();
81
82 unsigned long long state_hash();
83
87 // To support highlighting closed paths that have selected primitives
88 // below them, we need more information. This is different than
89 // highlighting only the paths related to the specific selected full
90 // path - in this situation, the application wants to know about all
91 // paths that are above the leaf *object* that is selected, in whatever
92 // portion of the database. Immediate parents are combs whose
93 // immediate child is the selected leaf; grand parents are higher level
94 // combs above immediate parents
97
99
100 private:
101 DbiState *dbis;
102
103 void add_paths(
104 unsigned long long c_hash,
106 );
107
108 void clear_paths(
109 unsigned long long c_hash,
111 );
112
113 void expand_paths(
115 unsigned long long c_hash,
117 );
118
119 void collapse_paths(
121 );
122
123 void clear_paths(std::vector<unsigned long long> &path_hashes, unsigned long long c_hash);
124};
125
126
128 public:
130
131
132 // Adds path to the BViewState container, but doesn't trigger a re-draw - that
133 // should be done once all paths to be added in a given draw cycle are added.
134 // The actual drawing (and mode specifications) are done with redraw and a
135 // supplied bv_obj_settings structure.
136 void add_path(const char *path);
138
139 // Erases paths from the view for the given mode. If mode < 0, all
140 // matching paths are erased. For modes that are un-evaluated, all
141 // paths that are subsets of the specified path are removed. For
142 // evaluated modes like 3 (bigE) that generate an evaluated visual
143 // specific to that path, only precise path matches are removed
144 void erase_path(int mode, int argc, const char **argv);
146
147 // Return a sorted vector of strings encoding the drawn paths in the
148 // view. If mode == -1 list all paths, otherwise list those specific
149 // to the mode. If list_collapsed is true, return the minimal path set
150 // that captures what is drawn - otherwise, return the direct list of
151 // scene objects
153
154 // Get a count of the drawn paths
156
157 // Report if a path hash is drawn - 0 == not drawn, 1 == fully drawn, 2 == partially drawn
158 int is_hdrawn(int mode, unsigned long long phash);
159
160 // Clear all drawn objects (TODO - should allow mode specification here)
161 void clear();
163 // A View State refresh regenerates already drawn objects.
164 unsigned long long refresh(struct bview *v, int argc, const char **argv);
166 // A View State redraw can impact multiple views with a shared state - most of
167 // the elements will be the same, but adaptive plotting will be view specific even
168 // with otherwise common objects - we must update accordingly.
169 unsigned long long redraw(struct bv_obj_settings *vs, std::unordered_set<struct bview *> &views, int no_autoview);
171 // Allow callers to calculate the drawing hash of a path
172 unsigned long long path_hash(std::vector<unsigned long long> &path, size_t max_len);
174 // Debugging methods for printing out current states - the use of hashes
175 // means direct inspection of most data isn't informative, so we provide
176 // convenience methods that decode it to user-comprehensible info.
177 void print_view_state(struct bu_vls *o = NULL);
179 private:
180 // Sets defining all drawn solid paths (including invalid paths). The
181 // s_keys holds the ordered individual keys of each drawn solid path - it
182 // is the latter that allows for the collapse operation to populate
183 // drawn_paths. s_map uses the same key as s_keys to map instances to
184 // actual scene objects. Because objects may be represented by more than
185 // one type of scene object (shaded, wireframe, evaluated, etc.) the mapping of
186 // key to scene object is not unique - we must also take scene object type
187 // into account.
190
191 // Called when the parent Db context is getting ready to update the data
192 // structures - we may need to redraw, so we save any necessary information
193 // ahead of the changes. Although this is a public function of the BViewState,
194 // it is practically speaking an implementation detail
195 void cache_collapsed();
196
197 DbiState *dbis;
198
199 int check_status(
202 unsigned long long path_hash,
204 bool leaf_expand
205 );
206
207 void walk_tree(
209 unsigned long long chash,
210 int curr_mode,
211 struct bview *v,
212 struct bv_obj_settings *vs,
213 matp_t m,
216 unsigned long long *ret
217 );
218
219 void gather_paths(
221 unsigned long long c_hash,
222 int curr_mode,
223 struct bview *v,
224 struct bv_obj_settings *vs,
225 matp_t m,
226 matp_t lm,
229 unsigned long long *ret
230 );
231
232 struct bv_scene_obj * scene_obj(
234 int curr_mode,
235 struct bv_obj_settings *vs,
236 matp_t m,
239 struct bview *v
240 );
241
242 int leaf_check(unsigned long long chash, std::vector<unsigned long long> &path_hashes);
243
244 // Paths supplied by commands to be incorporated into the drawn state by redraw method
246
247 // The collapsed drawn paths from the previous db state, organized
248 // by drawn mode
249 void depth_group_collapse(
254 );
257
258 // Set of hashes of all drawn paths and subpaths, constructed during the collapse
259 // operation from the set of drawn solid paths. This allows calling codes to
260 // spot check any path to see if it is active, without having to interrogate
261 // other data structures or walk down the tree.
264
265 // Set of partially drawn paths, constructed during the collapse operation.
266 // This holds the paths that should return 2 for is_hdrawn
268 std::unordered_set<unsigned long long> all_partially_drawn_paths;
269
270 friend class BSelectState;
272
273#define GED_DBISTATE_DB_CHANGE 0x01
274#define GED_DBISTATE_VIEW_CHANGE 0x02
276struct ged_draw_cache;
277
278class GED_EXPORT DbiState {
279 public:
280 DbiState(struct ged *);
283 unsigned long long update();
287 bool path_color(struct bu_color *c, std::vector<unsigned long long> &elements);
289 bool path_is_subtraction(std::vector<unsigned long long> &elements);
290 db_op_t bool_op(unsigned long long, unsigned long long);
292 bool get_matrix(matp_t m, unsigned long long p_key, unsigned long long i_key);
295 bool get_bbox(point_t *bbmin, point_t *bbmax, matp_t curr_mat, unsigned long long hash);
298 bool valid_hash(unsigned long long phash);
300 bool print_hash(struct bu_vls *opath, unsigned long long phash);
301 void print_path(struct bu_vls *opath, std::vector<unsigned long long> &path, size_t pmax = 0, int verbsose = 0);
303 const char *pathstr(std::vector<unsigned long long> &path, size_t pmax = 0);
304 const char *hashstr(unsigned long long);
306 std::vector<unsigned long long> digest_path(const char *path);
308 unsigned long long path_hash(std::vector<unsigned long long> &path, size_t max_len);
310 void clear_cache(struct directory *dp);
312 BViewState *get_view_state(struct bview *);
314 std::vector<BSelectState *> get_selected_states(const char *sname);
315 BSelectState * find_selected_state(const char *sname);
317 void put_selected_state(const char *sname);
318 std::vector<std::string> list_selection_sets();
320 // These maps are the ".g ground truth" of the comb structures - the set
321 // associated with each hash contains all the child hashes from the comb
322 // definition in the database for quick lookup, and the vector preserves
323 // the comb ordering for listing.
325 // Note: to match MGED's 'l' printing you need to use a reverse_iterator
328 // Translate individual object hashes to their directory names. This map must
329 // be updated any time a database object changes to remain valid.
330 struct directory *get_hdp(unsigned long long);
333 // For invalid comb entry strings, we can't point to a directory pointer. This
334 // map must also be updated after every db change - if a directory pointer hash
335 // maps to an entry in this map it needs to be removed, and newly invalid entries
336 // need to be added.
339 // This is a map of non-uniquely named child instances (i.e. instances that must be
340 // numbered) to the .g database name associated with those instances. Allows for
341 // one unique entry in p_c rather than requiring per-instance duplication
345 // Matrices above comb instances are critical to geometry placement. For non-identity
346 // matrices, we store them locally so they may be accessed without having to unpack
347 // the comb from disk.
350 // Similar to matrices, store non-union bool ops for instances
353
354 // Bounding boxes for each solid. To calculate the bbox for a comb, the
355 // children are walked combining the bboxes. The idea is to be able to
356 // retrieve solid bboxes and calculate comb bboxes without having to touch
357 // the disk beyond the initial per-solid calculations, which may be done
358 // once per load and/or dimensional change.
361
362 // We also have a number of standard attributes that can impact drawing,
363 // which are normally only accessible by loading in the attributes of
364 // the object. We stash them in maps to have the information available
365 // without having to interrogate the disk
366 std::unordered_map<unsigned long long, int> c_inherit; // color inheritance flag
367 std::unordered_map<unsigned long long, unsigned int> rgb; // color RGB value (r + (g << 8) + (b << 16))
370
371 // Data to be used by callbacks
378 // The shared view is common to multiple views, so we always update it.
379 // For other associated views (if any), we track their drawn states
380 // separately, but they too need to update in response to database
381 // changes (as well as draw/erase commands).
382 BViewState *shared_vs = NULL;
385 // We have a "default" selection state that is always available,
386 // and applications may define other named selection states.
387 BSelectState *default_selected;
390 // Database Instance associated with this container
391 struct ged *gedp = NULL;
392 struct db_i *dbip = NULL;
394 bool need_update_nref = true;
396 // Debugging methods for printing out current states - the use of hashes
397 // means direct inspection of most data isn't informative, so we provide
398 // convenience methods that decode it to user-comprehensible info.
399 void print_dbi_state(struct bu_vls *o = NULL, bool report_view_states = false);
401 private:
402 void gather_cyclic(
404 unsigned long long c_hash,
406 );
407 void print_leaves(
409 unsigned long long c_hash,
411 );
412
413 void populate_maps(struct directory *dp, unsigned long long phash, int reset);
414 unsigned long long update_dp(struct directory *dp, int reset);
415 unsigned int color_int(struct bu_color *);
416 int int_color(struct bu_color *c, unsigned int);
417 struct resource *res = NULL;
418 struct ged_draw_cache *dcache = NULL;
419 struct bu_vls hash_string = BU_VLS_INIT_ZERO;
420 struct bu_vls path_string = BU_VLS_INIT_ZERO;
421};
422
423
424#else
425
426/* Placeholders to allow for compilation when we're included in a C file */
427typedef struct _dbi_state {
428 int dummy; /* MS Visual C hack which can be removed if the struct contains something meaningful */
429} DbiState;
430typedef struct _bview_state {
431 int dummy; /* MS Visual C hack which can be removed if the struct contains something meaningful */
432} BViewState;
433typedef struct _bselect_state {
434 int dummy; /* MS Visual C hack which can be removed if the struct contains something meaningful */
436
437#endif
438
439#endif /* GED_DBI_H */
440
441/** @} */
442
443/*
444 * Local Variables:
445 * tab-width: 8
446 * mode: C
447 * indent-tabs-mode: t
448 * c-file-style: "stroustrup"
449 * End:
450 * ex: shiftwidth=4 tabstop=8
451 */
void characterize()
std::unordered_set< unsigned long long > grand_parents
Definition dbi.h:96
std::unordered_set< unsigned long long > active_parents
Definition dbi.h:86
bool select_hpath(std::vector< unsigned long long > &hpath)
bool deselect_hpath(std::vector< unsigned long long > &hpath)
void collapse()
void refresh()
bool is_selected(unsigned long long)
std::unordered_map< unsigned long long, std::vector< unsigned long long > > selected
Definition dbi.h:84
bool draw_sync()
bool is_active(unsigned long long)
unsigned long long state_hash()
std::vector< std::string > list_selected_paths()
std::unordered_set< unsigned long long > active_paths
Definition dbi.h:85
std::unordered_set< unsigned long long > immediate_parents
Definition dbi.h:95
bool is_active_parent(unsigned long long)
bool deselect_path(const char *path, bool update)
bool is_parent_obj(unsigned long long)
bool is_grand_parent_obj(unsigned long long)
BSelectState(DbiState *)
bool select_path(const char *path, bool update)
bool is_immediate_parent_obj(unsigned long long)
BViewState(DbiState *)
void erase_path(int mode, int argc, const char **argv)
void erase_hpath(int mode, unsigned long long c_hash, std::vector< unsigned long long > &path_hashes, bool cache_collapse=true)
size_t count_drawn_paths(int mode, bool list_collapsed)
void print_view_state(struct bu_vls *o=NULL)
unsigned long long path_hash(std::vector< unsigned long long > &path, size_t max_len)
unsigned long long refresh(struct bview *v, int argc, const char **argv)
void add_hpath(std::vector< unsigned long long > &path_hashes)
todo should allow mode specification here void clear()
std::vector< std::string > list_drawn_paths(int mode, bool list_collapsed)
int is_hdrawn(int mode, unsigned long long phash)
void add_path(const char *path)
unsigned long long redraw(struct bv_obj_settings *vs, std::unordered_set< struct bview * > &views, int no_autoview)
BSelectState * default_selected
Definition dbi.h:388
std::unordered_map< unsigned long long, std::string > old_names
Definition dbi.h:377
void print_dbi_state(struct bu_vls *o=NULL, bool report_view_states=false)
std::unordered_set< struct directory * > added
Definition dbi.h:373
bool get_path_matrix(matp_t m, std::vector< unsigned long long > &elements)
std::unordered_map< unsigned long long, std::unordered_map< unsigned long long, size_t > > i_bool
Definition dbi.h:352
void put_selected_state(const char *sname)
const char * pathstr(std::vector< unsigned long long > &path, size_t pmax=0)
std::unordered_map< unsigned long long, std::string > invalid_entry_map
Definition dbi.h:338
std::unordered_map< unsigned long long, int > c_inherit
Definition dbi.h:367
std::vector< BSelectState * > get_selected_states(const char *sname)
std::vector< unsigned long long > digest_path(const char *path)
bool get_path_bbox(point_t *bbmin, point_t *bbmax, std::vector< unsigned long long > &elements)
bool valid_hash_path(std::vector< unsigned long long > &phashes)
db_op_t bool_op(unsigned long long, unsigned long long)
std::unordered_map< struct bview *, BViewState * > view_states
Definition dbi.h:384
bool print_hash(struct bu_vls *opath, unsigned long long phash)
std::vector< std::string > list_selection_sets()
const char * hashstr(unsigned long long)
std::vector< unsigned long long > tops(bool show_cyclic)
bool valid_hash(unsigned long long phash)
std::unordered_map< unsigned long long, unsigned int > rgb
Definition dbi.h:368
std::unordered_set< struct directory * > changed
Definition dbi.h:374
unsigned long long path_hash(std::vector< unsigned long long > &path, size_t max_len)
bool get_matrix(matp_t m, unsigned long long p_key, unsigned long long i_key)
std::unordered_set< unsigned long long > removed
Definition dbi.h:376
unsigned long long update()
std::unordered_map< unsigned long long, struct directory * > d_map
Definition dbi.h:332
bool path_is_subtraction(std::vector< unsigned long long > &elements)
std::unordered_map< unsigned long long, std::vector< fastf_t > > bboxes
Definition dbi.h:360
std::unordered_map< unsigned long long, std::unordered_map< unsigned long long, std::vector< fastf_t > > > matrices
Definition dbi.h:349
struct directory * get_hdp(unsigned long long)
BViewState * get_view_state(struct bview *)
DbiState(struct ged *)
void print_path(struct bu_vls *opath, std::vector< unsigned long long > &path, size_t pmax=0, int verbsose=0)
std::unordered_map< unsigned long long, std::string > i_str
Definition dbi.h:344
std::unordered_map< unsigned long long, unsigned long long > i_map
Definition dbi.h:343
std::unordered_map< unsigned long long, std::unordered_set< unsigned long long > > p_c
Definition dbi.h:325
std::unordered_map< unsigned long long, int > region_id
Definition dbi.h:369
bool path_color(struct bu_color *c, std::vector< unsigned long long > &elements)
std::unordered_map< std::string, BSelectState * > selected_sets
Definition dbi.h:389
bool get_bbox(point_t *bbmin, point_t *bbmax, matp_t curr_mat, unsigned long long hash)
BSelectState * find_selected_state(const char *sname)
std::unordered_map< unsigned long long, std::vector< unsigned long long > > p_v
Definition dbi.h:327
void clear_cache(struct directory *dp)
std::unordered_set< unsigned long long > changed_hashes
Definition dbi.h:375
Definition dvec.h:74
dvec(double s)
dvec< LEN > map(const dvec_unop &operation, int limit=LEN)
Header file for the BRL-CAD common definitions.
#define BU_VLS_INIT_ZERO
Definition vls.h:81
void int * elements
Definition tig.h:178
void int char * mode
Definition tig.h:179
void int * c
Definition tig.h:139
fastf_t point_t[ELEMENTS_PER_POINT]
3-tuple point
Definition vmath.h:355
db_op_t
Definition op.h:55
Definition vls.h:53
Definition defines.h:167
fundamental vector, matrix, quaternion math macros