BRL-CAD
db_fp.h
Go to the documentation of this file.
1/* D B _ F P . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2014-2022 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
21#ifndef RT_DB_FP_H
22#define RT_DB_FP_H
23
24#include "common.h"
25
26#include "vmath.h"
27
28#include "bu/vls.h"
29#include "rt/defines.h"
30#include "rt/op.h"
31
32__BEGIN_DECLS
33
34struct db_i; /* forward declaration */
35struct directory; /* forward declaration */
36
37/** @addtogroup db_fullpath
38 *
39 * @brief
40 * Structures and routines for collecting and manipulating paths through the database tree.
41 *
42 */
43/** @{ */
44/** @file rt/db_fp.h */
45
46/**
47 * A db_fp pool holds path information - calling code may optionally initialize
48 * a common pool to be used for multiple paths, or forgo use of a pool and rely
49 * on internal db_fp management. In cases where many path nodes are the same,
50 * across many paths there can be saving in using a pool. dp_fp paths using a
51 * pool will share matching nodes.
52 */
53struct db_fp_pool_impl;
54struct db_fp_pool {
55 struct db_fp_pool_impl *i;
56};
57
58/* Create a pool for use in db_fp operations */
61
62/* Garbage collect all nodes in a pool which aren't currently referenced by at
63 * least one db_fp. A geometry editor maintaining a set of active full paths,
64 * for example, will want to do this periodically to clear out stale nodes that
65 * were created by paths subsequently removed in user editing. It will avoid
66 * the need to completely rebuild the path set, and instead free up just the
67 * memory that is no longer needed. */
68void db_fp_pool_gc(struct db_fp_pool *p);
69
70/* Completely eliminate a pool and all its db_fp_nodes. If this operation is
71 * performed and there are db_fp paths using the pool those paths will point
72 * to invalid storage, so callers should use db_fp_pool_gc unless they are sure
73 * they are done with all db_fp instances using the pool. */
76
77/* The container which holds a database path */
78struct db_fp_impl;
79struct db_fp {
80 struct db_fp_impl *i;
81};
82
83/* All other db_fp routines operate on a db_fp created with db_fp_create. The
84 * p parameter specifying a shared node pool is optional - if NULL, the full path
85 * in question will manage its own memory independently. */
86struct db_fp * db_fp_create(struct db_fp_pool *p);
87
88/* A db_fp instance should always be removed with db_fp_destroy. */
89void db_fp_destroy(struct db_fp *fp);
90
91/* Number of nodes in path */
92size_t db_fp_len(struct db_fp *fp);
93
94/* "Pop" the leaf node off the path */
95void db_fp_pop(struct db_fp *fp);
96
97/* "Push" the full path in src onto the end of dest. The leaf of src is the
98 * new leaf in dest. Note that the root node of src will be replaced by a new
99 * node (or possibly a pre-existing matching node if a pool is in use) in the
100 * dest fp, as its parent will no longer be NULL. Caller will need to manage
101 * any user data associated with the root node that they wish to preserve
102 * manually, as it will not be copied by this routine. */
103void db_fp_push(struct db_fp *dest, const struct db_fp *src, size_t start_offset);
104
105/* Add a directory dp + boolean op (and optionally matrix mat) as the new leaf
106 * node of fp. If a pool is in use, will either append an existing node that
107 * matches the specification or (if no such node is found) create a new node.
108 * Will return 1 if assignment would create a circular path or -1 if node creation
109 * fails. Returns 0 on success. */
110int db_fp_append(struct db_fp *fp, struct directory *dp, db_op_t op, mat_t mat);
111
112/* Set the path node at index i to the node associated with dp and boolean op
113 * (and matrix information if specified). If a pool is in use, will either
114 * append an existing node that matches the specification or (if no such node
115 * is found) create a new node. Will return 2 if assignment would create a
116 * circular path, 1 if the index is out of range for fp, or -1 if node creation
117 * fails, and 0 on success.
118 *
119 * Note that it is the caller's responsibility to manage any user data that
120 * might have been associated with the previous node - if a pre-existing node
121 * is found to insert that node's user data will not be altered, and if a new
122 * node is created its user data queue will be empty. The caller should retrieve
123 * any necessary data pointers *before* calling db_fp_set. */
124int db_fp_set(struct db_fp *fp, int i, struct directory *dp, db_op_t op, mat_t mat);
125
126/* Copy old_fp onto new_fp, destroying pre-existing path information in
127 * curr_fp. */
128void db_fp_cpy(struct db_fp *curr_fp, const struct db_fp *old_fp);
129
130/* Retrieve a node's directory pointer. Index of -1 returns the leaf,
131 * 0 returns the root. Returns NULL for an out-of-range index value.*/
132struct directory *db_fp_get_dir(const struct db_fp *fp, int index);
133
134/* Retrieve a nodes associated boolean operator. Index of -1 returns the leaf's
135 * parent op. 0 returns union. Returns DB_OP_NULL for an out-of-range index
136 * value. */
137db_op_t db_fp_get_bool(const struct db_fp *fp, int index);
138
139/* Retrieve a node's associated matrix (NULL may be interpreted as the identity
140 * matrix.) Returns NULL for an out-of-range index value.*/
141matp_t db_fp_get_mat(const struct db_fp *fp, int index);
142
143/* Mechanism to allow calling code to associate and retrieve user
144 * data associated with a particular full path.*/
145void db_fp_push_data(struct db_fp *fp, void *data);
146void *db_fp_get_data(struct db_fp *fp);
147void db_fp_pop_data(struct db_fp *fp);
148
149/* Mechanism to allow calling code to associate and retrieve user
150 * data associated with the nth node in a path. Push is a no-op
151 * and get/pop return NULL for index values not present in the path. */
152void db_fp_push_node_data(struct db_fp *fp, int index, void *data);
153void *db_fp_get_node_data(struct db_fp *fp, int index);
154void db_fp_pop_node_data(struct db_fp *fp, int index);
155
156/* Print out paths as strings */
157#define DB_FP_STR_BOOL 0x1 /* boolean labels flag */
158#define DB_FP_STR_TYPE 0x2 /* object type flag */
159#define DB_FP_STR_MATRIX 0x4 /* matrix flag */
160char *db_fp_to_str(const struct db_fp *fp, int flags);
161void db_fp_to_vls(struct bu_vls *v, const struct db_fp *fp, int flags);
162
163/* Convert string descriptions of paths into path structures. Pool may be NULL */
164int db_str_to_fp(struct db_fp **fp, const struct db_i *dbip, const char *str, struct db_fp_pool *p);
165int db_argv_to_fp(struct db_fp **fp, const struct db_i *dbip, int argc, const char *const*argv, struct db_fp_pool *p);
166
167/* Compare paths, including boolean operations and matrices if defined */
168RT_EXPORT extern int db_fp_identical(const struct db_fp *a, const struct db_fp *b);
169
170/* Any two paths that are identical according to db_fp_identical are also true
171 * in this test, and in addition a more permissive check is performed for
172 * same-solid-different-path conditions if db_fp_identical does not pass. The
173 * latter test is not a full geometric comparison, but will detect cases where
174 * the same solid database object is unioned in from multiple paths without
175 * being moved by a matrix. */
176RT_EXPORT extern int db_fp_identical_solid(const struct db_fp *a, const struct db_fp *b);
177
178/* Returns either the index which denotes the point in a that starts a
179 * series of path nodes which fully match the path nodes of b, or -1 if no
180 * match found. By default, the search will try to match the root node of a
181 * with the root node of b and work left(root) to right(leaf), if leaf_to_root
182 * is set the leaf of a will be matched against the leaf of b and the match
183 * will work right to left. skip_first tells the search how many nodes in a to
184 * ignore before starting to compare.*/
185int db_fp_match(const struct db_fp *a, const struct db_fp *b, unsigned int skip_first, int leaf_to_root);
186
187/* Returns either the first node index whose dp->d_namep matches n, or -1 if no
188 * match is found. Normally the search will be ordered from left(root) to
189 * right(leaf), but if leaf_to_root is set the checks will be done in right to
190 * left order. skip_first tells the search how many nodes of p to ignore before
191 * starting to compare.*/
192int db_fp_search(const struct db_fp *p, const char *n, unsigned int skip_first, int leaf_to_root);
193
194/* Set mat to the evaluated matrix along the path fp, starting from the root node and continuing up
195 * until depth d (0 indicates a full path evaluation). */
196void db_fp_eval_mat(mat_t mat, const struct db_fp *fp, unsigned int depth);
197
198/** @} */
199
200__END_DECLS
201
202#endif /*RT_DB_FP_H*/
203
204/*
205 * Local Variables:
206 * tab-width: 8
207 * mode: C
208 * indent-tabs-mode: t
209 * c-file-style: "stroustrup"
210 * End:
211 * ex: shiftwidth=4 tabstop=8
212 */
Header file for the BRL-CAD common definitions.
void float float int * n
Definition: tig.h:74
int db_fp_append(struct db_fp *fp, struct directory *dp, db_op_t op, mat_t mat)
void * db_fp_get_node_data(struct db_fp *fp, int index)
void db_fp_pop_node_data(struct db_fp *fp, int index)
void db_fp_push_data(struct db_fp *fp, void *data)
void db_fp_push_node_data(struct db_fp *fp, int index, void *data)
db_op_t db_fp_get_bool(const struct db_fp *fp, int index)
struct directory * db_fp_get_dir(const struct db_fp *fp, int index)
void db_fp_pop_data(struct db_fp *fp)
struct db_fp * db_fp_create(struct db_fp_pool *p)
void db_fp_pool_destroy(struct db_fp_pool *p)
int db_fp_pool_init(struct db_fp_pool *p)
int db_str_to_fp(struct db_fp **fp, const struct db_i *dbip, const char *str, struct db_fp_pool *p)
int db_fp_set(struct db_fp *fp, int i, struct directory *dp, db_op_t op, mat_t mat)
int db_fp_identical(const struct db_fp *a, const struct db_fp *b)
void * db_fp_get_data(struct db_fp *fp)
void db_fp_eval_mat(mat_t mat, const struct db_fp *fp, unsigned int depth)
int db_fp_pool_create(struct db_fp_pool **p)
int db_fp_match(const struct db_fp *a, const struct db_fp *b, unsigned int skip_first, int leaf_to_root)
int db_fp_search(const struct db_fp *p, const char *n, unsigned int skip_first, int leaf_to_root)
matp_t db_fp_get_mat(const struct db_fp *fp, int index)
size_t db_fp_len(struct db_fp *fp)
void db_fp_pop(struct db_fp *fp)
void db_fp_pool_gc(struct db_fp_pool *p)
void db_fp_to_vls(struct bu_vls *v, const struct db_fp *fp, int flags)
char * db_fp_to_str(const struct db_fp *fp, int flags)
void db_fp_pool_clear(struct db_fp_pool *p)
void db_fp_destroy(struct db_fp *fp)
int db_argv_to_fp(struct db_fp **fp, const struct db_i *dbip, int argc, const char *const *argv, struct db_fp_pool *p)
int db_fp_identical_solid(const struct db_fp *a, const struct db_fp *b)
void db_fp_cpy(struct db_fp *curr_fp, const struct db_fp *old_fp)
void db_fp_push(struct db_fp *dest, const struct db_fp *src, size_t start_offset)
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition: vmath.h:366
fastf_t * matp_t
pointer to a 4x4 matrix
Definition: vmath.h:369
db_op_t
Definition: op.h:55
Definition: vls.h:53
struct db_fp_pool_impl * i
Definition: db_fp.h:55
Definition: db_fp.h:79
struct db_fp_impl * i
Definition: db_fp.h:80
fundamental vector, matrix, quaternion math macros