BRL-CAD
Loading...
Searching...
No Matches
db_instance.h
Go to the documentation of this file.
1/* D B _ I N S T A N C E . H
2 * BRL-CAD
3 *
4 * Copyright (c) 1993-2026 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/** @file db_instance.h
21 *
22 */
23
24#ifndef RT_DB_INSTANCE_H
25#define RT_DB_INSTANCE_H
26
27#include "common.h"
28
29/* system headers */
30#include <stdio.h> /* for FILE */
31
32/* interface headers */
33#include "bu/magic.h"
34#include "bu/file.h"
35#include "bu/mapped_file.h"
36#include "bu/ptbl.h"
37#include "bn/tol.h"
38#include "rt/mem.h"
39#include "rt/op.h"
40#include "rt/directory.h"
41#include "rt/anim.h"
42#include "rt/tol.h"
43
45
46struct db_i; /* forward declaration */
47struct rt_wdb; /* forward declaration */
48
49/* Callback called when database objects are changed. The int indicates
50 * the change type (0 = mod, 1 = add, 2 = rm). ctx is a user
51 * supplied context and is passed back as the last argument to db_change.
52 */
53typedef void (*dbi_changed_t)(struct db_i *, struct directory *, int, void *);
54
55
56/* Callback called when references are updated. Args are:
57 *
58 * 1. parent dp,
59 * 2. child dp referenced by parent dp
60 * 3. the child name (should be available even if the child dp is null, which can happen with references to
61 * non-existent objects)
62 * 4. the boolean operation used to include the child
63 * 5. the matrix above the child (NULL == IDN matrix)
64 * 6. dbi_u_data (generally application context set for use in these callbacks)
65 *
66 * There are two particular sets of callback args that have special significance:
67 *
68 * NULL, NULL, NULL, DB_OP_UNION, NULL == the beginning of a db_update_nref cycle
69 * NULL, NULL, NULL, DB_OP_SUBTRACT, NULL == the end of a db_update_nref cycle
70 *
71 * NOTE: the contents of the child name and matrix should be copied by the
72 * caller if they want to make use of them - they are not references to
73 * stable storage.
74 *
75 * NOTE: the parent may be a non-comb object, if extrudes or other primitives
76 * that reference other primitives are present in the .g - the caller should
77 * be aware of that when processing the results.
78 *
79 * librt only stores the reference count in d_nref, but applications may
80 * need more explicit awareness of the parent/child relationships. Since
81 * db_update_nref must be called in any case for librt to function
82 * properly, this callback lets the parent application benefit from the
83 * work db_update_nref is already doing to figure out these relationships.
84 * */
85typedef void (*dbi_update_nref_t)(struct db_i *, struct directory *, struct directory *, const char *, db_op_t, matp_t, void *);
86
87/**
88 * One of these structures is used to describe each separate instance
89 * of a BRL-CAD model database ".g" file.
90 *
91 * dbi_filepath is a C-style argv array of places to search when
92 * opening related files (such as data files for EBM solids or
93 * texture-maps). The array and strings are all dynamically
94 * allocated.
95 *
96 * Note that the current working units are specified as a conversion
97 * factor to/from millimeters (they are the 'base' in local2base and
98 * base2local) because database dimensional values are always stored
99 * as millimeters (mm). The units conversion factor only affects the
100 * display and conversion of input values. This helps prevent error
101 * accumulation and improves numerical stability when calculations are
102 * made.
103 *
104 * TODO - make a db_i_internal struct, and move all the LIBRT ONLY
105 * elements into it. That will also give us a place to do fancier
106 * database state management using things like C++ containers without
107 * bothering the public API.
108 */
109struct db_i_internal;
110struct db_i {
111 uint32_t dbi_magic; /**< @brief magic number */
113 /* THESE ELEMENTS ARE AVAILABLE FOR APPLICATIONS TO READ */
114
115 char * dbi_filename; /**< @brief file name */
116 int dbi_read_only; /**< @brief !0 => read only file */
117 double dbi_local2base; /**< @brief local2mm */
118 double dbi_base2local; /**< @brief unit conversion factors */
119 char * dbi_title; /**< @brief title from IDENT rec */
120 char ** dbi_filepath; /**< @brief search path for aux file opens (convenience var) */
122 /* PRIVATE librt-internal state; see src/librt/librt_private.h */
123 struct db_i_internal *i;
125#define DBI_NULL ((struct db_i *)0)
126#define RT_CHECK_DBI(_p) BU_CKMAG(_p, DBI_MAGIC, "struct db_i")
127#define RT_CK_DBI(_p) RT_CHECK_DBI(_p)
129/* Functions for registering and unregistering callbacks with a dbip */
130extern RT_EXPORT int db_add_changed_clbk(struct db_i *dbip, dbi_changed_t c, void *u_data);
131extern RT_EXPORT int db_rm_changed_clbk(struct db_i *dbip, dbi_changed_t c, void *u_data);
133extern RT_EXPORT int db_add_update_nref_clbk(struct db_i *dbip, dbi_update_nref_t c, void *u_data);
134extern RT_EXPORT int db_rm_update_nref_clbk(struct db_i *dbip, dbi_update_nref_t c, void *u_data);
136/**
137 * for db_open(), open the specified file as read-only
138 */
139#define DB_OPEN_READONLY "r"
141/**
142 * for db_open(), open the specified file as read-write
143 */
144#define DB_OPEN_READWRITE "rw"
146/**
147 * Open the named database.
148 *
149 * The 'name' parameter specifies the file or filepath to a .g
150 * geometry database file for reading and/or writing.
151 *
152 * The 'mode' parameter specifies whether to open read-only or in
153 * read-write mode, specified via the DB_OPEN_READONLY and
154 * DB_OPEN_READWRITE symbols respectively.
155 *
156 * As a convenience, the returned db_t structure's dbi_filepath field
157 * is a C-style argv array of dirs to search when attempting to open
158 * related files (such as data files for EBM solids or texture-maps).
159 * The default values are "." and the directory containing the ".g"
160 * file. They may be overridden by setting the environment variable
161 * BRLCAD_FILE_PATH.
162 *
163 * Returns:
164 * DBI_NULL error
165 * db_i * success
166 */
167RT_EXPORT extern struct db_i *
168db_open(const char *name, const char *mode);
170/**
171 * "open" an in-memory-only database instance. this initializes a
172 * dbip for use, creating an inmem dbi_wdbp as the means to add
173 * geometry to the directory (use wdb_export_external()).
174 */
175RT_EXPORT extern struct db_i * db_open_inmem(void);
177
178/**
179 * Create a new database containing just a header record, regardless
180 * of whether the database previously existed or not, and open it for
181 * reading and writing.
182 *
183 * This routine also calls db_dirbuild(), so the caller doesn't need
184 * to.
185 *
186 * Returns:
187 * DBI_NULL on error
188 * db_i * on success
189 */
190RT_EXPORT extern struct db_i *
191db_create(const char *name, int version);
193/**
194 * creates an in-memory-only database. this is very similar to
195 * db_open_inmem() with the exception that the this routine adds a
196 * default _GLOBAL object.
197 */
198RT_EXPORT extern struct db_i * db_create_inmem(void);
200
201/**
202 * Close a database, releasing dynamic memory. Will also release the db_i
203 * struct memory itself, not just the internal struct contents. However, the
204 * actual freeing of memory Waits until last user is done - db_close is a no-op
205 * if dbi_uses is greater than 1.
206 */
207RT_EXPORT extern void db_close(struct db_i *dbip);
209
210/**
211 * Return the i-th directory hash list head for the given database instance.
212 * Used by FOR_ALL_DIRECTORY_START and other iteration code.
213 */
214RT_EXPORT extern struct directory *db_dirptr(const struct db_i *dbip, int index);
216
217/**
218 * Convenience macros for iterating over all dp in a database instance
219 */
220#define FOR_ALL_DIRECTORY_START(_dp, _dbip) { int _i; \
221 for (_i = RT_DBNHASH-1; _i >= 0; _i--) { \
222 for ((_dp) = db_dirptr((_dbip), _i); (_dp); (_dp) = (_dp)->d_forw) {
223
224#define FOR_ALL_DIRECTORY_END }}}
226
228
229#endif /* RT_DB_INSTANCE_H */
230
231/*
232 * Local Variables:
233 * tab-width: 8
234 * mode: C
235 * indent-tabs-mode: t
236 * c-file-style: "stroustrup"
237 * End:
238 * ex: shiftwidth=4 tabstop=8
239 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
void db_close(struct db_i *dbip)
struct db_i * db_create_inmem(void)
int db_rm_update_nref_clbk(struct db_i *dbip, dbi_update_nref_t c, void *u_data)
int db_add_changed_clbk(struct db_i *dbip, dbi_changed_t c, void *u_data)
void(* dbi_changed_t)(struct db_i *, struct directory *, int, void *)
Definition db_instance.h:53
struct db_i * db_open_inmem(void)
void(* dbi_update_nref_t)(struct db_i *, struct directory *, struct directory *, const char *, db_op_t, matp_t, void *)
Definition db_instance.h:85
struct db_i * db_create(const char *name, int version)
struct db_i * db_open(const char *name, const char *mode)
int db_rm_changed_clbk(struct db_i *dbip, dbi_changed_t c, void *u_data)
int db_add_update_nref_clbk(struct db_i *dbip, dbi_update_nref_t c, void *u_data)
struct directory * db_dirptr(const struct db_i *dbip, int index)
fastf_t * matp_t
pointer to a 4x4 matrix
Definition vmath.h:372
Global registry of recognized magic numbers.
db_op_t
Definition op.h:55
double dbi_base2local
unit conversion factors
char ** dbi_filepath
search path for aux file opens (convenience var)
double dbi_local2base
local2mm
int dbi_read_only
!0 => read only file
struct db_i_internal * i
char * dbi_filename
file name
uint32_t dbi_magic
magic number
char * dbi_title
title from IDENT rec
Definition wdb.h:60